xref: /freebsd/sys/cam/cam_xpt.c (revision ab4327bb3f9e9423c3b52c2563aac95042f1ae51)
1898b0535SWarner Losh /*-
28b8a9b1dSJustin T. Gibbs  * Implementation of the Common Access Method Transport (XPT) layer.
38b8a9b1dSJustin T. Gibbs  *
4c8bead2aSJustin T. Gibbs  * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
559190eaaSKenneth D. Merry  * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
68b8a9b1dSJustin T. Gibbs  * All rights reserved.
78b8a9b1dSJustin T. Gibbs  *
88b8a9b1dSJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
98b8a9b1dSJustin T. Gibbs  * modification, are permitted provided that the following conditions
108b8a9b1dSJustin T. Gibbs  * are met:
118b8a9b1dSJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
128b8a9b1dSJustin T. Gibbs  *    notice, this list of conditions, and the following disclaimer,
138b8a9b1dSJustin T. Gibbs  *    without modification, immediately at the beginning of the file.
148b8a9b1dSJustin T. Gibbs  * 2. The name of the author may not be used to endorse or promote products
158b8a9b1dSJustin T. Gibbs  *    derived from this software without specific prior written permission.
168b8a9b1dSJustin T. Gibbs  *
178b8a9b1dSJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
188b8a9b1dSJustin T. Gibbs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
198b8a9b1dSJustin T. Gibbs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
208b8a9b1dSJustin T. Gibbs  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
218b8a9b1dSJustin T. Gibbs  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
228b8a9b1dSJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
238b8a9b1dSJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
248b8a9b1dSJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
258b8a9b1dSJustin T. Gibbs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
268b8a9b1dSJustin T. Gibbs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
278b8a9b1dSJustin T. Gibbs  * SUCH DAMAGE.
288b8a9b1dSJustin T. Gibbs  */
299c963d87SDavid E. O'Brien 
309c963d87SDavid E. O'Brien #include <sys/cdefs.h>
319c963d87SDavid E. O'Brien __FBSDID("$FreeBSD$");
329c963d87SDavid E. O'Brien 
338b8a9b1dSJustin T. Gibbs #include <sys/param.h>
348532d381SConrad Meyer #include <sys/bio.h>
359a94c9c5SJohn Baldwin #include <sys/bus.h>
368b8a9b1dSJustin T. Gibbs #include <sys/systm.h>
378b8a9b1dSJustin T. Gibbs #include <sys/types.h>
388b8a9b1dSJustin T. Gibbs #include <sys/malloc.h>
398b8a9b1dSJustin T. Gibbs #include <sys/kernel.h>
4087cfaf0eSJustin T. Gibbs #include <sys/time.h>
418b8a9b1dSJustin T. Gibbs #include <sys/conf.h>
428b8a9b1dSJustin T. Gibbs #include <sys/fcntl.h>
435d754af7SMatt Jacob #include <sys/interrupt.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>
568b8a9b1dSJustin T. Gibbs #include <cam/cam_periph.h>
5752c9ce25SScott Long #include <cam/cam_queue.h>
588b8a9b1dSJustin T. Gibbs #include <cam/cam_sim.h>
598b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt.h>
608b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_sim.h>
618b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_periph.h>
6252c9ce25SScott Long #include <cam/cam_xpt_internal.h>
638b8a9b1dSJustin T. Gibbs #include <cam/cam_debug.h>
6425a2902cSScott Long #include <cam/cam_compat.h>
658b8a9b1dSJustin T. Gibbs 
668b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_all.h>
678b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_message.h>
688b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_pass.h>
69abef0e67SMarius Strobl 
70abef0e67SMarius Strobl #include <machine/md_var.h>	/* geometry translation */
71f0d9af51SMatt Jacob #include <machine/stdarg.h>	/* for xpt_print below */
72abef0e67SMarius Strobl 
738b8a9b1dSJustin T. Gibbs #include "opt_cam.h"
748b8a9b1dSJustin T. Gibbs 
7552c9ce25SScott Long /*
7652c9ce25SScott Long  * This is the maximum number of high powered commands (e.g. start unit)
7752c9ce25SScott Long  * that can be outstanding at a particular time.
7852c9ce25SScott Long  */
7952c9ce25SScott Long #ifndef CAM_MAX_HIGHPOWER
8052c9ce25SScott Long #define CAM_MAX_HIGHPOWER  4
8152c9ce25SScott Long #endif
8252c9ce25SScott Long 
838b8a9b1dSJustin T. Gibbs /* Datastructures internal to the xpt layer */
84362abc44STai-hwa Liang MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
85596ee08fSAlexander Motin MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices");
86596ee08fSAlexander Motin MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs");
87596ee08fSAlexander Motin MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths");
888b8a9b1dSJustin T. Gibbs 
892b83592fSScott Long /* Object for defering XPT actions to a taskqueue */
902b83592fSScott Long struct xpt_task {
912b83592fSScott Long 	struct task	task;
9284f82481SScott Long 	void		*data1;
9384f82481SScott Long 	uintptr_t	data2;
942b83592fSScott Long };
952b83592fSScott Long 
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 
1092b83592fSScott Long 	/* Registered busses */
1102b83592fSScott Long 	TAILQ_HEAD(,cam_eb)	xpt_busses;
1112b83592fSScott Long 	u_int			bus_generation;
1122b83592fSScott Long 
1132b83592fSScott Long 	struct intr_config_hook	*xpt_config_hook;
1142b83592fSScott Long 
11583c5d981SAlexander Motin 	int			boot_delay;
11683c5d981SAlexander Motin 	struct callout 		boot_callout;
11783c5d981SAlexander Motin 
1182b83592fSScott Long 	struct mtx		xpt_topo_lock;
1192b83592fSScott Long 	struct mtx		xpt_lock;
120227d67aaSAlexander Motin 	struct taskqueue	*xpt_taskq;
1218b8a9b1dSJustin T. Gibbs };
1228b8a9b1dSJustin T. Gibbs 
1238b8a9b1dSJustin T. Gibbs typedef enum {
1248b8a9b1dSJustin T. Gibbs 	DM_RET_COPY		= 0x01,
1258b8a9b1dSJustin T. Gibbs 	DM_RET_FLAG_MASK	= 0x0f,
1268b8a9b1dSJustin T. Gibbs 	DM_RET_NONE		= 0x00,
1278b8a9b1dSJustin T. Gibbs 	DM_RET_STOP		= 0x10,
1288b8a9b1dSJustin T. Gibbs 	DM_RET_DESCEND		= 0x20,
1298b8a9b1dSJustin T. Gibbs 	DM_RET_ERROR		= 0x30,
1308b8a9b1dSJustin T. Gibbs 	DM_RET_ACTION_MASK	= 0xf0
1318b8a9b1dSJustin T. Gibbs } dev_match_ret;
1328b8a9b1dSJustin T. Gibbs 
1338b8a9b1dSJustin T. Gibbs typedef enum {
1348b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_BUS,
1358b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_TARGET,
1368b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_DEVICE,
1378b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_PERIPH
1388b8a9b1dSJustin T. Gibbs } xpt_traverse_depth;
1398b8a9b1dSJustin T. Gibbs 
1408b8a9b1dSJustin T. Gibbs struct xpt_traverse_config {
1418b8a9b1dSJustin T. Gibbs 	xpt_traverse_depth	depth;
1428b8a9b1dSJustin T. Gibbs 	void			*tr_func;
1438b8a9b1dSJustin T. Gibbs 	void			*tr_arg;
1448b8a9b1dSJustin T. Gibbs };
1458b8a9b1dSJustin T. Gibbs 
1468b8a9b1dSJustin T. Gibbs typedef	int	xpt_busfunc_t (struct cam_eb *bus, void *arg);
1478b8a9b1dSJustin T. Gibbs typedef	int	xpt_targetfunc_t (struct cam_et *target, void *arg);
1488b8a9b1dSJustin T. Gibbs typedef	int	xpt_devicefunc_t (struct cam_ed *device, void *arg);
1498b8a9b1dSJustin T. Gibbs typedef	int	xpt_periphfunc_t (struct cam_periph *periph, void *arg);
1508b8a9b1dSJustin T. Gibbs typedef int	xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
1518b8a9b1dSJustin T. Gibbs 
1528b8a9b1dSJustin T. Gibbs /* Transport layer configuration information */
1538b8a9b1dSJustin T. Gibbs static struct xpt_softc xsoftc;
1548b8a9b1dSJustin T. Gibbs 
1555719711fSEdward Tomasz Napierala MTX_SYSINIT(xpt_topo_init, &xsoftc.xpt_topo_lock, "XPT topology lock", MTX_DEF);
1565719711fSEdward Tomasz Napierala 
15783c5d981SAlexander Motin SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
15883c5d981SAlexander Motin            &xsoftc.boot_delay, 0, "Bus registration wait time");
159636870ffSWill Andrews SYSCTL_UINT(_kern_cam, OID_AUTO, xpt_generation, CTLFLAG_RD,
160636870ffSWill Andrews 	    &xsoftc.xpt_generation, 0, "CAM peripheral generation count");
16183c5d981SAlexander Motin 
162227d67aaSAlexander Motin struct cam_doneq {
163227d67aaSAlexander Motin 	struct mtx_padalign	cam_doneq_mtx;
164227d67aaSAlexander Motin 	STAILQ_HEAD(, ccb_hdr)	cam_doneq;
165227d67aaSAlexander Motin 	int			cam_doneq_sleep;
166227d67aaSAlexander Motin };
1678b8a9b1dSJustin T. Gibbs 
168227d67aaSAlexander Motin static struct cam_doneq cam_doneqs[MAXCPU];
169227d67aaSAlexander Motin static int cam_num_doneqs;
170227d67aaSAlexander Motin static struct proc *cam_proc;
171227d67aaSAlexander Motin 
172227d67aaSAlexander Motin SYSCTL_INT(_kern_cam, OID_AUTO, num_doneqs, CTLFLAG_RDTUN,
173227d67aaSAlexander Motin            &cam_num_doneqs, 0, "Number of completion queues/threads");
1748b8a9b1dSJustin T. Gibbs 
1759a1c8571SNick Hibma struct cam_periph *xpt_periph;
1769a1c8571SNick Hibma 
1778b8a9b1dSJustin T. Gibbs static periph_init_t xpt_periph_init;
1788b8a9b1dSJustin T. Gibbs 
1798b8a9b1dSJustin T. Gibbs static struct periph_driver xpt_driver =
1808b8a9b1dSJustin T. Gibbs {
1818b8a9b1dSJustin T. Gibbs 	xpt_periph_init, "xpt",
1821e637ba6SAlexander Motin 	TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
1831e637ba6SAlexander Motin 	CAM_PERIPH_DRV_EARLY
1848b8a9b1dSJustin T. Gibbs };
1858b8a9b1dSJustin T. Gibbs 
1860b7c27b9SPeter Wemm PERIPHDRIVER_DECLARE(xpt, xpt_driver);
1878b8a9b1dSJustin T. Gibbs 
1888b8a9b1dSJustin T. Gibbs static d_open_t xptopen;
1898b8a9b1dSJustin T. Gibbs static d_close_t xptclose;
1908b8a9b1dSJustin T. Gibbs static d_ioctl_t xptioctl;
19125a2902cSScott Long static d_ioctl_t xptdoioctl;
1928b8a9b1dSJustin T. Gibbs 
1934e2f199eSPoul-Henning Kamp static struct cdevsw xpt_cdevsw = {
194dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
1952b83592fSScott Long 	.d_flags =	0,
1967ac40f5fSPoul-Henning Kamp 	.d_open =	xptopen,
1977ac40f5fSPoul-Henning Kamp 	.d_close =	xptclose,
1987ac40f5fSPoul-Henning Kamp 	.d_ioctl =	xptioctl,
1997ac40f5fSPoul-Henning Kamp 	.d_name =	"xpt",
2008b8a9b1dSJustin T. Gibbs };
2018b8a9b1dSJustin T. Gibbs 
2028b8a9b1dSJustin T. Gibbs /* Storage for debugging datastructures */
2038b8a9b1dSJustin T. Gibbs struct cam_path *cam_dpath;
20482b361b1SMatt Jacob u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
205af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RWTUN,
206f0f25b9cSAlexander Motin 	&cam_dflags, 0, "Enabled debug flags");
207f0f25b9cSAlexander Motin u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
208af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RWTUN,
209f0f25b9cSAlexander Motin 	&cam_debug_delay, 0, "Delay in us after each debug message");
2108b8a9b1dSJustin T. Gibbs 
2116d2a8f1cSPeter Wemm /* Our boot-time initialization hook */
21274bd1c10SNick Hibma static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
21374bd1c10SNick Hibma 
21474bd1c10SNick Hibma static moduledata_t cam_moduledata = {
21574bd1c10SNick Hibma 	"cam",
21674bd1c10SNick Hibma 	cam_module_event_handler,
21774bd1c10SNick Hibma 	NULL
21874bd1c10SNick Hibma };
21974bd1c10SNick Hibma 
2202b83592fSScott Long static int	xpt_init(void *);
22174bd1c10SNick Hibma 
22274bd1c10SNick Hibma DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
22374bd1c10SNick Hibma MODULE_VERSION(cam, 1);
22474bd1c10SNick Hibma 
2258b8a9b1dSJustin T. Gibbs 
2268b8a9b1dSJustin T. Gibbs static void		xpt_async_bcast(struct async_list *async_head,
2278b8a9b1dSJustin T. Gibbs 					u_int32_t async_code,
2288b8a9b1dSJustin T. Gibbs 					struct cam_path *path,
2298b8a9b1dSJustin T. Gibbs 					void *async_arg);
230434bbf6eSJustin T. Gibbs static path_id_t xptnextfreepathid(void);
231434bbf6eSJustin T. Gibbs static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
232227d67aaSAlexander Motin static union ccb *xpt_get_ccb(struct cam_periph *periph);
233227d67aaSAlexander Motin static union ccb *xpt_get_ccb_nowait(struct cam_periph *periph);
234227d67aaSAlexander Motin static void	 xpt_run_allocq(struct cam_periph *periph, int sleep);
235227d67aaSAlexander Motin static void	 xpt_run_allocq_task(void *context, int pending);
236cccf4220SAlexander Motin static void	 xpt_run_devq(struct cam_devq *devq);
2378b8a9b1dSJustin T. Gibbs static timeout_t xpt_release_devq_timeout;
2382b83592fSScott Long static void	 xpt_release_simq_timeout(void *arg) __unused;
239227d67aaSAlexander Motin static void	 xpt_acquire_bus(struct cam_eb *bus);
240a5479bc5SJustin T. Gibbs static void	 xpt_release_bus(struct cam_eb *bus);
241daa5487fSAlexander Motin static uint32_t	 xpt_freeze_devq_device(struct cam_ed *dev, u_int count);
242227d67aaSAlexander Motin static int	 xpt_release_devq_device(struct cam_ed *dev, u_int count,
243cccf4220SAlexander Motin 		    int run_queue);
2448b8a9b1dSJustin T. Gibbs static struct cam_et*
2458b8a9b1dSJustin T. Gibbs 		 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
246227d67aaSAlexander Motin static void	 xpt_acquire_target(struct cam_et *target);
247f98d7a47SAlexander Motin static void	 xpt_release_target(struct cam_et *target);
2488b8a9b1dSJustin T. Gibbs static struct cam_eb*
2498b8a9b1dSJustin T. Gibbs 		 xpt_find_bus(path_id_t path_id);
2508b8a9b1dSJustin T. Gibbs static struct cam_et*
2518b8a9b1dSJustin T. Gibbs 		 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
2528b8a9b1dSJustin T. Gibbs static struct cam_ed*
2538b8a9b1dSJustin T. Gibbs 		 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
2548b8a9b1dSJustin T. Gibbs static void	 xpt_config(void *arg);
255227d67aaSAlexander Motin static int	 xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
256227d67aaSAlexander Motin 				 u_int32_t new_priority);
2578b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t xptpassannouncefunc;
2588b8a9b1dSJustin T. Gibbs static void	 xptaction(struct cam_sim *sim, union ccb *work_ccb);
259434bbf6eSJustin T. Gibbs static void	 xptpoll(struct cam_sim *sim);
260227d67aaSAlexander Motin static void	 camisr_runqueue(void);
261227d67aaSAlexander Motin static void	 xpt_done_process(struct ccb_hdr *ccb_h);
262227d67aaSAlexander Motin static void	 xpt_done_td(void *);
2638b8a9b1dSJustin T. Gibbs static dev_match_ret	xptbusmatch(struct dev_match_pattern *patterns,
2643393f8daSKenneth D. Merry 				    u_int num_patterns, struct cam_eb *bus);
2658b8a9b1dSJustin T. Gibbs static dev_match_ret	xptdevicematch(struct dev_match_pattern *patterns,
2663393f8daSKenneth D. Merry 				       u_int num_patterns,
2673393f8daSKenneth D. Merry 				       struct cam_ed *device);
2688b8a9b1dSJustin T. Gibbs static dev_match_ret	xptperiphmatch(struct dev_match_pattern *patterns,
2693393f8daSKenneth D. Merry 				       u_int num_patterns,
2708b8a9b1dSJustin T. Gibbs 				       struct cam_periph *periph);
2718b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptedtbusfunc;
2728b8a9b1dSJustin T. Gibbs static xpt_targetfunc_t	xptedttargetfunc;
2738b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptedtdevicefunc;
2748b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptedtperiphfunc;
2758b8a9b1dSJustin T. Gibbs static xpt_pdrvfunc_t	xptplistpdrvfunc;
2768b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptplistperiphfunc;
2778b8a9b1dSJustin T. Gibbs static int		xptedtmatch(struct ccb_dev_match *cdm);
2788b8a9b1dSJustin T. Gibbs static int		xptperiphlistmatch(struct ccb_dev_match *cdm);
2798b8a9b1dSJustin T. Gibbs static int		xptbustraverse(struct cam_eb *start_bus,
2808b8a9b1dSJustin T. Gibbs 				       xpt_busfunc_t *tr_func, void *arg);
2818b8a9b1dSJustin T. Gibbs static int		xpttargettraverse(struct cam_eb *bus,
2828b8a9b1dSJustin T. Gibbs 					  struct cam_et *start_target,
2838b8a9b1dSJustin T. Gibbs 					  xpt_targetfunc_t *tr_func, void *arg);
2848b8a9b1dSJustin T. Gibbs static int		xptdevicetraverse(struct cam_et *target,
2858b8a9b1dSJustin T. Gibbs 					  struct cam_ed *start_device,
2868b8a9b1dSJustin T. Gibbs 					  xpt_devicefunc_t *tr_func, void *arg);
2878b8a9b1dSJustin T. Gibbs static int		xptperiphtraverse(struct cam_ed *device,
2888b8a9b1dSJustin T. Gibbs 					  struct cam_periph *start_periph,
2898b8a9b1dSJustin T. Gibbs 					  xpt_periphfunc_t *tr_func, void *arg);
2908b8a9b1dSJustin T. Gibbs static int		xptpdrvtraverse(struct periph_driver **start_pdrv,
2918b8a9b1dSJustin T. Gibbs 					xpt_pdrvfunc_t *tr_func, void *arg);
2928b8a9b1dSJustin T. Gibbs static int		xptpdperiphtraverse(struct periph_driver **pdrv,
2938b8a9b1dSJustin T. Gibbs 					    struct cam_periph *start_periph,
2948b8a9b1dSJustin T. Gibbs 					    xpt_periphfunc_t *tr_func,
2958b8a9b1dSJustin T. Gibbs 					    void *arg);
2968b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptdefbusfunc;
2978b8a9b1dSJustin T. Gibbs static xpt_targetfunc_t	xptdeftargetfunc;
2988b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptdefdevicefunc;
2998b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptdefperiphfunc;
30083c5d981SAlexander Motin static void		xpt_finishconfig_task(void *context, int pending);
30152c9ce25SScott Long static void		xpt_dev_async_default(u_int32_t async_code,
30252c9ce25SScott Long 					      struct cam_eb *bus,
30352c9ce25SScott Long 					      struct cam_et *target,
30452c9ce25SScott Long 					      struct cam_ed *device,
30552c9ce25SScott Long 					      void *async_arg);
30652c9ce25SScott Long static struct cam_ed *	xpt_alloc_device_default(struct cam_eb *bus,
30752c9ce25SScott Long 						 struct cam_et *target,
30852c9ce25SScott Long 						 lun_id_t lun_id);
3098b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptsetasyncfunc;
3108b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptsetasyncbusfunc;
3118b8a9b1dSJustin T. Gibbs static cam_status	xptregister(struct cam_periph *periph,
3128b8a9b1dSJustin T. Gibbs 				    void *arg);
31369be012fSWarner Losh static const char *	xpt_action_name(uint32_t action);
314cccf4220SAlexander Motin static __inline int device_is_queued(struct cam_ed *device);
3158b8a9b1dSJustin T. Gibbs 
3168b8a9b1dSJustin T. Gibbs static __inline int
317cccf4220SAlexander Motin xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
31830a4094fSAlexander Motin {
31930a4094fSAlexander Motin 	int	retval;
32030a4094fSAlexander Motin 
321227d67aaSAlexander Motin 	mtx_assert(&devq->send_mtx, MA_OWNED);
32283c5d981SAlexander Motin 	if ((dev->ccbq.queue.entries > 0) &&
32383c5d981SAlexander Motin 	    (dev->ccbq.dev_openings > 0) &&
324cccf4220SAlexander Motin 	    (dev->ccbq.queue.qfrozen_cnt == 0)) {
32530a4094fSAlexander Motin 		/*
32630a4094fSAlexander Motin 		 * The priority of a device waiting for controller
3276bccea7cSRebecca Cran 		 * resources is that of the highest priority CCB
32830a4094fSAlexander Motin 		 * enqueued.
32930a4094fSAlexander Motin 		 */
33030a4094fSAlexander Motin 		retval =
331cccf4220SAlexander Motin 		    xpt_schedule_dev(&devq->send_queue,
332227d67aaSAlexander Motin 				     &dev->devq_entry,
33383c5d981SAlexander Motin 				     CAMQ_GET_PRIO(&dev->ccbq.queue));
33430a4094fSAlexander Motin 	} else {
33530a4094fSAlexander Motin 		retval = 0;
33630a4094fSAlexander Motin 	}
33730a4094fSAlexander Motin 	return (retval);
33830a4094fSAlexander Motin }
33930a4094fSAlexander Motin 
34030a4094fSAlexander Motin static __inline int
341cccf4220SAlexander Motin device_is_queued(struct cam_ed *device)
3428b8a9b1dSJustin T. Gibbs {
343227d67aaSAlexander Motin 	return (device->devq_entry.index != CAM_UNQUEUED_INDEX);
3448b8a9b1dSJustin T. Gibbs }
3458b8a9b1dSJustin T. Gibbs 
3468b8a9b1dSJustin T. Gibbs static void
3478b8a9b1dSJustin T. Gibbs xpt_periph_init()
3488b8a9b1dSJustin T. Gibbs {
34973d26919SKenneth D. Merry 	make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
3508b8a9b1dSJustin T. Gibbs }
3518b8a9b1dSJustin T. Gibbs 
3528b8a9b1dSJustin T. Gibbs static int
35389c9c53dSPoul-Henning Kamp xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
3548b8a9b1dSJustin T. Gibbs {
3558b8a9b1dSJustin T. Gibbs 
3568b8a9b1dSJustin T. Gibbs 	/*
35766a0780eSKenneth D. Merry 	 * Only allow read-write access.
35866a0780eSKenneth D. Merry 	 */
35966a0780eSKenneth D. Merry 	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
36066a0780eSKenneth D. Merry 		return(EPERM);
36166a0780eSKenneth D. Merry 
36266a0780eSKenneth D. Merry 	/*
3638b8a9b1dSJustin T. Gibbs 	 * We don't allow nonblocking access.
3648b8a9b1dSJustin T. Gibbs 	 */
3658b8a9b1dSJustin T. Gibbs 	if ((flags & O_NONBLOCK) != 0) {
3662b83592fSScott Long 		printf("%s: can't do nonblocking access\n", devtoname(dev));
3678b8a9b1dSJustin T. Gibbs 		return(ENODEV);
3688b8a9b1dSJustin T. Gibbs 	}
3698b8a9b1dSJustin T. Gibbs 
3708b8a9b1dSJustin T. Gibbs 	return(0);
3718b8a9b1dSJustin T. Gibbs }
3728b8a9b1dSJustin T. Gibbs 
3738b8a9b1dSJustin T. Gibbs static int
37489c9c53dSPoul-Henning Kamp xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
3758b8a9b1dSJustin T. Gibbs {
3768b8a9b1dSJustin T. Gibbs 
3778b8a9b1dSJustin T. Gibbs 	return(0);
3788b8a9b1dSJustin T. Gibbs }
3798b8a9b1dSJustin T. Gibbs 
3802b83592fSScott Long /*
3812b83592fSScott Long  * Don't automatically grab the xpt softc lock here even though this is going
3822b83592fSScott Long  * through the xpt device.  The xpt device is really just a back door for
3832b83592fSScott Long  * accessing other devices and SIMs, so the right thing to do is to grab
3842b83592fSScott Long  * the appropriate SIM lock once the bus/SIM is located.
3852b83592fSScott Long  */
3868b8a9b1dSJustin T. Gibbs static int
38789c9c53dSPoul-Henning Kamp xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
3888b8a9b1dSJustin T. Gibbs {
3892b83592fSScott Long 	int error;
3908b8a9b1dSJustin T. Gibbs 
39125a2902cSScott Long 	if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
392f564de00SScott Long 		error = cam_compat_ioctl(dev, cmd, addr, flag, td, xptdoioctl);
39325a2902cSScott Long 	}
39425a2902cSScott Long 	return (error);
39525a2902cSScott Long }
39625a2902cSScott Long 
39725a2902cSScott Long static int
39825a2902cSScott Long xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
39925a2902cSScott Long {
40025a2902cSScott Long 	int error;
40125a2902cSScott Long 
4028b8a9b1dSJustin T. Gibbs 	error = 0;
4038b8a9b1dSJustin T. Gibbs 
4048b8a9b1dSJustin T. Gibbs 	switch(cmd) {
4058b8a9b1dSJustin T. Gibbs 	/*
4068b8a9b1dSJustin T. Gibbs 	 * For the transport layer CAMIOCOMMAND ioctl, we really only want
4078b8a9b1dSJustin T. Gibbs 	 * to accept CCB types that don't quite make sense to send through a
4088c7a96c5SScott Long 	 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
4098c7a96c5SScott Long 	 * in the CAM spec.
4108b8a9b1dSJustin T. Gibbs 	 */
4118b8a9b1dSJustin T. Gibbs 	case CAMIOCOMMAND: {
4128b8a9b1dSJustin T. Gibbs 		union ccb *ccb;
4138b8a9b1dSJustin T. Gibbs 		union ccb *inccb;
4142b83592fSScott Long 		struct cam_eb *bus;
4158b8a9b1dSJustin T. Gibbs 
4168b8a9b1dSJustin T. Gibbs 		inccb = (union ccb *)addr;
4178fc77fffSConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
4188fc77fffSConrad Meyer 		if (inccb->ccb_h.func_code == XPT_SCSI_IO)
4198fc77fffSConrad Meyer 			inccb->csio.bio = NULL;
4208fc77fffSConrad Meyer #endif
4218b8a9b1dSJustin T. Gibbs 
4222b83592fSScott Long 		bus = xpt_find_bus(inccb->ccb_h.path_id);
4230e85f214SMatt Jacob 		if (bus == NULL)
4240e85f214SMatt Jacob 			return (EINVAL);
4250e85f214SMatt Jacob 
4260e85f214SMatt Jacob 		switch (inccb->ccb_h.func_code) {
4270e85f214SMatt Jacob 		case XPT_SCAN_BUS:
4280e85f214SMatt Jacob 		case XPT_RESET_BUS:
4290e85f214SMatt Jacob 			if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
4300e85f214SMatt Jacob 			    inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
4310e85f214SMatt Jacob 				xpt_release_bus(bus);
4320e85f214SMatt Jacob 				return (EINVAL);
4330e85f214SMatt Jacob 			}
4340e85f214SMatt Jacob 			break;
4350e85f214SMatt Jacob 		case XPT_SCAN_TGT:
4360e85f214SMatt Jacob 			if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
4370e85f214SMatt Jacob 			    inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
4380e85f214SMatt Jacob 				xpt_release_bus(bus);
4390e85f214SMatt Jacob 				return (EINVAL);
4400e85f214SMatt Jacob 			}
4410e85f214SMatt Jacob 			break;
4420e85f214SMatt Jacob 		default:
4432b83592fSScott Long 			break;
4442b83592fSScott Long 		}
4452b83592fSScott Long 
4468b8a9b1dSJustin T. Gibbs 		switch(inccb->ccb_h.func_code) {
4478b8a9b1dSJustin T. Gibbs 		case XPT_SCAN_BUS:
4488b8a9b1dSJustin T. Gibbs 		case XPT_RESET_BUS:
4498c7a96c5SScott Long 		case XPT_PATH_INQ:
4508fcf57f5SJustin T. Gibbs 		case XPT_ENG_INQ:
4518b8a9b1dSJustin T. Gibbs 		case XPT_SCAN_LUN:
4520e85f214SMatt Jacob 		case XPT_SCAN_TGT:
4538b8a9b1dSJustin T. Gibbs 
4548008a935SScott Long 			ccb = xpt_alloc_ccb();
4552b83592fSScott Long 
4568b8a9b1dSJustin T. Gibbs 			/*
4578b8a9b1dSJustin T. Gibbs 			 * Create a path using the bus, target, and lun the
4588b8a9b1dSJustin T. Gibbs 			 * user passed in.
4598b8a9b1dSJustin T. Gibbs 			 */
460e5dfa058SAlexander Motin 			if (xpt_create_path(&ccb->ccb_h.path, NULL,
4618b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.path_id,
4628b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_id,
4638b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_lun) !=
4648b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP){
4658b8a9b1dSJustin T. Gibbs 				error = EINVAL;
4668b8a9b1dSJustin T. Gibbs 				xpt_free_ccb(ccb);
4678b8a9b1dSJustin T. Gibbs 				break;
4688b8a9b1dSJustin T. Gibbs 			}
4698b8a9b1dSJustin T. Gibbs 			/* Ensure all of our fields are correct */
4708b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
4718b8a9b1dSJustin T. Gibbs 				      inccb->ccb_h.pinfo.priority);
4728b8a9b1dSJustin T. Gibbs 			xpt_merge_ccb(ccb, inccb);
473227d67aaSAlexander Motin 			xpt_path_lock(ccb->ccb_h.path);
4748b8a9b1dSJustin T. Gibbs 			cam_periph_runccb(ccb, NULL, 0, 0, NULL);
475227d67aaSAlexander Motin 			xpt_path_unlock(ccb->ccb_h.path);
4768b8a9b1dSJustin T. Gibbs 			bcopy(ccb, inccb, sizeof(union ccb));
4778b8a9b1dSJustin T. Gibbs 			xpt_free_path(ccb->ccb_h.path);
4788b8a9b1dSJustin T. Gibbs 			xpt_free_ccb(ccb);
4798b8a9b1dSJustin T. Gibbs 			break;
4808b8a9b1dSJustin T. Gibbs 
4818b8a9b1dSJustin T. Gibbs 		case XPT_DEBUG: {
4828b8a9b1dSJustin T. Gibbs 			union ccb ccb;
4838b8a9b1dSJustin T. Gibbs 
4848b8a9b1dSJustin T. Gibbs 			/*
485aa872be6SMatt Jacob 			 * This is an immediate CCB, so it's okay to
4868b8a9b1dSJustin T. Gibbs 			 * allocate it on the stack.
4878b8a9b1dSJustin T. Gibbs 			 */
4888b8a9b1dSJustin T. Gibbs 
4898b8a9b1dSJustin T. Gibbs 			/*
4908b8a9b1dSJustin T. Gibbs 			 * Create a path using the bus, target, and lun the
4918b8a9b1dSJustin T. Gibbs 			 * user passed in.
4928b8a9b1dSJustin T. Gibbs 			 */
493e5dfa058SAlexander Motin 			if (xpt_create_path(&ccb.ccb_h.path, NULL,
4948b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.path_id,
4958b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_id,
4968b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_lun) !=
4978b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP){
4988b8a9b1dSJustin T. Gibbs 				error = EINVAL;
4998b8a9b1dSJustin T. Gibbs 				break;
5008b8a9b1dSJustin T. Gibbs 			}
5018b8a9b1dSJustin T. Gibbs 			/* Ensure all of our fields are correct */
5028b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
5038b8a9b1dSJustin T. Gibbs 				      inccb->ccb_h.pinfo.priority);
5048b8a9b1dSJustin T. Gibbs 			xpt_merge_ccb(&ccb, inccb);
5058b8a9b1dSJustin T. Gibbs 			xpt_action(&ccb);
5068b8a9b1dSJustin T. Gibbs 			bcopy(&ccb, inccb, sizeof(union ccb));
5078b8a9b1dSJustin T. Gibbs 			xpt_free_path(ccb.ccb_h.path);
5088b8a9b1dSJustin T. Gibbs 			break;
5098b8a9b1dSJustin T. Gibbs 
5108b8a9b1dSJustin T. Gibbs 		}
5118b8a9b1dSJustin T. Gibbs 		case XPT_DEV_MATCH: {
5128b8a9b1dSJustin T. Gibbs 			struct cam_periph_map_info mapinfo;
51359190eaaSKenneth D. Merry 			struct cam_path *old_path;
5148b8a9b1dSJustin T. Gibbs 
5158b8a9b1dSJustin T. Gibbs 			/*
5168b8a9b1dSJustin T. Gibbs 			 * We can't deal with physical addresses for this
5178b8a9b1dSJustin T. Gibbs 			 * type of transaction.
5188b8a9b1dSJustin T. Gibbs 			 */
519dd0b4fb6SKonstantin Belousov 			if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
520dd0b4fb6SKonstantin Belousov 			    CAM_DATA_VADDR) {
5218b8a9b1dSJustin T. Gibbs 				error = EINVAL;
5228b8a9b1dSJustin T. Gibbs 				break;
5238b8a9b1dSJustin T. Gibbs 			}
52459190eaaSKenneth D. Merry 
52559190eaaSKenneth D. Merry 			/*
52659190eaaSKenneth D. Merry 			 * Save this in case the caller had it set to
52759190eaaSKenneth D. Merry 			 * something in particular.
52859190eaaSKenneth D. Merry 			 */
52959190eaaSKenneth D. Merry 			old_path = inccb->ccb_h.path;
53059190eaaSKenneth D. Merry 
53159190eaaSKenneth D. Merry 			/*
53259190eaaSKenneth D. Merry 			 * We really don't need a path for the matching
53359190eaaSKenneth D. Merry 			 * code.  The path is needed because of the
53459190eaaSKenneth D. Merry 			 * debugging statements in xpt_action().  They
53559190eaaSKenneth D. Merry 			 * assume that the CCB has a valid path.
53659190eaaSKenneth D. Merry 			 */
53759190eaaSKenneth D. Merry 			inccb->ccb_h.path = xpt_periph->path;
53859190eaaSKenneth D. Merry 
5398b8a9b1dSJustin T. Gibbs 			bzero(&mapinfo, sizeof(mapinfo));
5408b8a9b1dSJustin T. Gibbs 
5418b8a9b1dSJustin T. Gibbs 			/*
5428b8a9b1dSJustin T. Gibbs 			 * Map the pattern and match buffers into kernel
5438b8a9b1dSJustin T. Gibbs 			 * virtual address space.
5448b8a9b1dSJustin T. Gibbs 			 */
545de239312SAlexander Motin 			error = cam_periph_mapmem(inccb, &mapinfo, MAXPHYS);
5468b8a9b1dSJustin T. Gibbs 
54759190eaaSKenneth D. Merry 			if (error) {
54859190eaaSKenneth D. Merry 				inccb->ccb_h.path = old_path;
5498b8a9b1dSJustin T. Gibbs 				break;
55059190eaaSKenneth D. Merry 			}
5518b8a9b1dSJustin T. Gibbs 
5528b8a9b1dSJustin T. Gibbs 			/*
5538b8a9b1dSJustin T. Gibbs 			 * This is an immediate CCB, we can send it on directly.
5548b8a9b1dSJustin T. Gibbs 			 */
5558b8a9b1dSJustin T. Gibbs 			xpt_action(inccb);
5568b8a9b1dSJustin T. Gibbs 
5578b8a9b1dSJustin T. Gibbs 			/*
5588b8a9b1dSJustin T. Gibbs 			 * Map the buffers back into user space.
5598b8a9b1dSJustin T. Gibbs 			 */
5608b8a9b1dSJustin T. Gibbs 			cam_periph_unmapmem(inccb, &mapinfo);
5618b8a9b1dSJustin T. Gibbs 
56259190eaaSKenneth D. Merry 			inccb->ccb_h.path = old_path;
56359190eaaSKenneth D. Merry 
5648b8a9b1dSJustin T. Gibbs 			error = 0;
5658b8a9b1dSJustin T. Gibbs 			break;
5668b8a9b1dSJustin T. Gibbs 		}
5678b8a9b1dSJustin T. Gibbs 		default:
5688fcf57f5SJustin T. Gibbs 			error = ENOTSUP;
5698b8a9b1dSJustin T. Gibbs 			break;
5708b8a9b1dSJustin T. Gibbs 		}
571daddc001SScott Long 		xpt_release_bus(bus);
5728b8a9b1dSJustin T. Gibbs 		break;
5738b8a9b1dSJustin T. Gibbs 	}
5748b8a9b1dSJustin T. Gibbs 	/*
5758b8a9b1dSJustin T. Gibbs 	 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
5768b8a9b1dSJustin T. Gibbs 	 * with the periphal driver name and unit name filled in.  The other
5778b8a9b1dSJustin T. Gibbs 	 * fields don't really matter as input.  The passthrough driver name
5788b8a9b1dSJustin T. Gibbs 	 * ("pass"), and unit number are passed back in the ccb.  The current
5798b8a9b1dSJustin T. Gibbs 	 * device generation number, and the index into the device peripheral
5808b8a9b1dSJustin T. Gibbs 	 * driver list, and the status are also passed back.  Note that
5818b8a9b1dSJustin T. Gibbs 	 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
5828b8a9b1dSJustin T. Gibbs 	 * we never return a status of CAM_GDEVLIST_LIST_CHANGED.  It is
5838b8a9b1dSJustin T. Gibbs 	 * (or rather should be) impossible for the device peripheral driver
5848b8a9b1dSJustin T. Gibbs 	 * list to change since we look at the whole thing in one pass, and
58577dc25ccSScott Long 	 * we do it with lock protection.
5868b8a9b1dSJustin T. Gibbs 	 *
5878b8a9b1dSJustin T. Gibbs 	 */
5888b8a9b1dSJustin T. Gibbs 	case CAMGETPASSTHRU: {
5898b8a9b1dSJustin T. Gibbs 		union ccb *ccb;
5908b8a9b1dSJustin T. Gibbs 		struct cam_periph *periph;
5918b8a9b1dSJustin T. Gibbs 		struct periph_driver **p_drv;
5928b8a9b1dSJustin T. Gibbs 		char   *name;
5933393f8daSKenneth D. Merry 		u_int unit;
594621a60d4SKenneth D. Merry 		int base_periph_found;
5958b8a9b1dSJustin T. Gibbs 
5968b8a9b1dSJustin T. Gibbs 		ccb = (union ccb *)addr;
5978b8a9b1dSJustin T. Gibbs 		unit = ccb->cgdl.unit_number;
5988b8a9b1dSJustin T. Gibbs 		name = ccb->cgdl.periph_name;
599621a60d4SKenneth D. Merry 		base_periph_found = 0;
6008fc77fffSConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
6018fc77fffSConrad Meyer 		if (ccb->ccb_h.func_code == XPT_SCSI_IO)
6028fc77fffSConrad Meyer 			ccb->csio.bio = NULL;
6038fc77fffSConrad Meyer #endif
604621a60d4SKenneth D. Merry 
6058b8a9b1dSJustin T. Gibbs 		/*
6068b8a9b1dSJustin T. Gibbs 		 * Sanity check -- make sure we don't get a null peripheral
6078b8a9b1dSJustin T. Gibbs 		 * driver name.
6088b8a9b1dSJustin T. Gibbs 		 */
6098b8a9b1dSJustin T. Gibbs 		if (*ccb->cgdl.periph_name == '\0') {
6108b8a9b1dSJustin T. Gibbs 			error = EINVAL;
6118b8a9b1dSJustin T. Gibbs 			break;
6128b8a9b1dSJustin T. Gibbs 		}
6138b8a9b1dSJustin T. Gibbs 
6148b8a9b1dSJustin T. Gibbs 		/* Keep the list from changing while we traverse it */
6159a7c2696SAlexander Motin 		xpt_lock_buses();
6168b8a9b1dSJustin T. Gibbs 
6178b8a9b1dSJustin T. Gibbs 		/* first find our driver in the list of drivers */
6180b7c27b9SPeter Wemm 		for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
6198b8a9b1dSJustin T. Gibbs 			if (strcmp((*p_drv)->driver_name, name) == 0)
6208b8a9b1dSJustin T. Gibbs 				break;
6218b8a9b1dSJustin T. Gibbs 
6228b8a9b1dSJustin T. Gibbs 		if (*p_drv == NULL) {
6239a7c2696SAlexander Motin 			xpt_unlock_buses();
6248b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
6258b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
6268b8a9b1dSJustin T. Gibbs 			*ccb->cgdl.periph_name = '\0';
6278b8a9b1dSJustin T. Gibbs 			ccb->cgdl.unit_number = 0;
6288b8a9b1dSJustin T. Gibbs 			error = ENOENT;
6298b8a9b1dSJustin T. Gibbs 			break;
6308b8a9b1dSJustin T. Gibbs 		}
6318b8a9b1dSJustin T. Gibbs 
6328b8a9b1dSJustin T. Gibbs 		/*
6338b8a9b1dSJustin T. Gibbs 		 * Run through every peripheral instance of this driver
6348b8a9b1dSJustin T. Gibbs 		 * and check to see whether it matches the unit passed
6358b8a9b1dSJustin T. Gibbs 		 * in by the user.  If it does, get out of the loops and
6368b8a9b1dSJustin T. Gibbs 		 * find the passthrough driver associated with that
6378b8a9b1dSJustin T. Gibbs 		 * peripheral driver.
6388b8a9b1dSJustin T. Gibbs 		 */
6398b8a9b1dSJustin T. Gibbs 		for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
6408b8a9b1dSJustin T. Gibbs 		     periph = TAILQ_NEXT(periph, unit_links)) {
6418b8a9b1dSJustin T. Gibbs 
642a29779e8SAlexander Motin 			if (periph->unit_number == unit)
6438b8a9b1dSJustin T. Gibbs 				break;
6448b8a9b1dSJustin T. Gibbs 		}
6458b8a9b1dSJustin T. Gibbs 		/*
6468b8a9b1dSJustin T. Gibbs 		 * If we found the peripheral driver that the user passed
6478b8a9b1dSJustin T. Gibbs 		 * in, go through all of the peripheral drivers for that
6488b8a9b1dSJustin T. Gibbs 		 * particular device and look for a passthrough driver.
6498b8a9b1dSJustin T. Gibbs 		 */
6508b8a9b1dSJustin T. Gibbs 		if (periph != NULL) {
6518b8a9b1dSJustin T. Gibbs 			struct cam_ed *device;
6528b8a9b1dSJustin T. Gibbs 			int i;
6538b8a9b1dSJustin T. Gibbs 
654621a60d4SKenneth D. Merry 			base_periph_found = 1;
6558b8a9b1dSJustin T. Gibbs 			device = periph->path->device;
656fc2ffbe6SPoul-Henning Kamp 			for (i = 0, periph = SLIST_FIRST(&device->periphs);
6578b8a9b1dSJustin T. Gibbs 			     periph != NULL;
658fc2ffbe6SPoul-Henning Kamp 			     periph = SLIST_NEXT(periph, periph_links), i++) {
6598b8a9b1dSJustin T. Gibbs 				/*
6608b8a9b1dSJustin T. Gibbs 				 * Check to see whether we have a
6618b8a9b1dSJustin T. Gibbs 				 * passthrough device or not.
6628b8a9b1dSJustin T. Gibbs 				 */
6638b8a9b1dSJustin T. Gibbs 				if (strcmp(periph->periph_name, "pass") == 0) {
6648b8a9b1dSJustin T. Gibbs 					/*
6658b8a9b1dSJustin T. Gibbs 					 * Fill in the getdevlist fields.
6668b8a9b1dSJustin T. Gibbs 					 */
6678b8a9b1dSJustin T. Gibbs 					strcpy(ccb->cgdl.periph_name,
6688b8a9b1dSJustin T. Gibbs 					       periph->periph_name);
6698b8a9b1dSJustin T. Gibbs 					ccb->cgdl.unit_number =
6708b8a9b1dSJustin T. Gibbs 						periph->unit_number;
671fc2ffbe6SPoul-Henning Kamp 					if (SLIST_NEXT(periph, periph_links))
6728b8a9b1dSJustin T. Gibbs 						ccb->cgdl.status =
6738b8a9b1dSJustin T. Gibbs 							CAM_GDEVLIST_MORE_DEVS;
6748b8a9b1dSJustin T. Gibbs 					else
6758b8a9b1dSJustin T. Gibbs 						ccb->cgdl.status =
6768b8a9b1dSJustin T. Gibbs 						       CAM_GDEVLIST_LAST_DEVICE;
6778b8a9b1dSJustin T. Gibbs 					ccb->cgdl.generation =
6788b8a9b1dSJustin T. Gibbs 						device->generation;
6798b8a9b1dSJustin T. Gibbs 					ccb->cgdl.index = i;
6808b8a9b1dSJustin T. Gibbs 					/*
6818b8a9b1dSJustin T. Gibbs 					 * Fill in some CCB header fields
6828b8a9b1dSJustin T. Gibbs 					 * that the user may want.
6838b8a9b1dSJustin T. Gibbs 					 */
6848b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.path_id =
6858b8a9b1dSJustin T. Gibbs 						periph->path->bus->path_id;
6868b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.target_id =
6878b8a9b1dSJustin T. Gibbs 						periph->path->target->target_id;
6888b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.target_lun =
6898b8a9b1dSJustin T. Gibbs 						periph->path->device->lun_id;
6908b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.status = CAM_REQ_CMP;
6918b8a9b1dSJustin T. Gibbs 					break;
6928b8a9b1dSJustin T. Gibbs 				}
6938b8a9b1dSJustin T. Gibbs 			}
6948b8a9b1dSJustin T. Gibbs 		}
6958b8a9b1dSJustin T. Gibbs 
6968b8a9b1dSJustin T. Gibbs 		/*
6978b8a9b1dSJustin T. Gibbs 		 * If the periph is null here, one of two things has
6988b8a9b1dSJustin T. Gibbs 		 * happened.  The first possibility is that we couldn't
6998b8a9b1dSJustin T. Gibbs 		 * find the unit number of the particular peripheral driver
7008b8a9b1dSJustin T. Gibbs 		 * that the user is asking about.  e.g. the user asks for
7018b8a9b1dSJustin T. Gibbs 		 * the passthrough driver for "da11".  We find the list of
7028b8a9b1dSJustin T. Gibbs 		 * "da" peripherals all right, but there is no unit 11.
7038b8a9b1dSJustin T. Gibbs 		 * The other possibility is that we went through the list
7048b8a9b1dSJustin T. Gibbs 		 * of peripheral drivers attached to the device structure,
7058b8a9b1dSJustin T. Gibbs 		 * but didn't find one with the name "pass".  Either way,
7068b8a9b1dSJustin T. Gibbs 		 * we return ENOENT, since we couldn't find something.
7078b8a9b1dSJustin T. Gibbs 		 */
7088b8a9b1dSJustin T. Gibbs 		if (periph == NULL) {
7098b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
7108b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
7118b8a9b1dSJustin T. Gibbs 			*ccb->cgdl.periph_name = '\0';
7128b8a9b1dSJustin T. Gibbs 			ccb->cgdl.unit_number = 0;
7138b8a9b1dSJustin T. Gibbs 			error = ENOENT;
714621a60d4SKenneth D. Merry 			/*
715621a60d4SKenneth D. Merry 			 * It is unfortunate that this is even necessary,
716621a60d4SKenneth D. Merry 			 * but there are many, many clueless users out there.
717621a60d4SKenneth D. Merry 			 * If this is true, the user is looking for the
718621a60d4SKenneth D. Merry 			 * passthrough driver, but doesn't have one in his
719621a60d4SKenneth D. Merry 			 * kernel.
720621a60d4SKenneth D. Merry 			 */
721621a60d4SKenneth D. Merry 			if (base_periph_found == 1) {
722621a60d4SKenneth D. Merry 				printf("xptioctl: pass driver is not in the "
723621a60d4SKenneth D. Merry 				       "kernel\n");
724935c968aSChristian Brueffer 				printf("xptioctl: put \"device pass\" in "
725621a60d4SKenneth D. Merry 				       "your kernel config file\n");
726621a60d4SKenneth D. Merry 			}
7278b8a9b1dSJustin T. Gibbs 		}
7289a7c2696SAlexander Motin 		xpt_unlock_buses();
7298b8a9b1dSJustin T. Gibbs 		break;
7308b8a9b1dSJustin T. Gibbs 		}
7318b8a9b1dSJustin T. Gibbs 	default:
7328b8a9b1dSJustin T. Gibbs 		error = ENOTTY;
7338b8a9b1dSJustin T. Gibbs 		break;
7348b8a9b1dSJustin T. Gibbs 	}
7358b8a9b1dSJustin T. Gibbs 
7368b8a9b1dSJustin T. Gibbs 	return(error);
7378b8a9b1dSJustin T. Gibbs }
7388b8a9b1dSJustin T. Gibbs 
73974bd1c10SNick Hibma static int
74074bd1c10SNick Hibma cam_module_event_handler(module_t mod, int what, void *arg)
74174bd1c10SNick Hibma {
7422b83592fSScott Long 	int error;
7432b83592fSScott Long 
7442b83592fSScott Long 	switch (what) {
7452b83592fSScott Long 	case MOD_LOAD:
7462b83592fSScott Long 		if ((error = xpt_init(NULL)) != 0)
7472b83592fSScott Long 			return (error);
7482b83592fSScott Long 		break;
7492b83592fSScott Long 	case MOD_UNLOAD:
75074bd1c10SNick Hibma 		return EBUSY;
7512b83592fSScott Long 	default:
7523e019deaSPoul-Henning Kamp 		return EOPNOTSUPP;
75374bd1c10SNick Hibma 	}
75474bd1c10SNick Hibma 
75574bd1c10SNick Hibma 	return 0;
75674bd1c10SNick Hibma }
75774bd1c10SNick Hibma 
75808f13879SWarner Losh static struct xpt_proto *
75908f13879SWarner Losh xpt_proto_find(cam_proto proto)
76008f13879SWarner Losh {
76108f13879SWarner Losh 	struct xpt_proto **pp;
76208f13879SWarner Losh 
76308f13879SWarner Losh 	SET_FOREACH(pp, cam_xpt_proto_set) {
76408f13879SWarner Losh 		if ((*pp)->proto == proto)
76508f13879SWarner Losh 			return *pp;
76608f13879SWarner Losh 	}
76708f13879SWarner Losh 
76808f13879SWarner Losh 	return NULL;
76908f13879SWarner Losh }
77008f13879SWarner Losh 
77183c5d981SAlexander Motin static void
77283c5d981SAlexander Motin xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
77383c5d981SAlexander Motin {
77483c5d981SAlexander Motin 
77583c5d981SAlexander Motin 	if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
77683c5d981SAlexander Motin 		xpt_free_path(done_ccb->ccb_h.path);
77783c5d981SAlexander Motin 		xpt_free_ccb(done_ccb);
77883c5d981SAlexander Motin 	} else {
77983c5d981SAlexander Motin 		done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
78083c5d981SAlexander Motin 		(*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
78183c5d981SAlexander Motin 	}
78283c5d981SAlexander Motin 	xpt_release_boot();
78383c5d981SAlexander Motin }
78483c5d981SAlexander Motin 
7859e6461a2SMatt Jacob /* thread to handle bus rescans */
7869e6461a2SMatt Jacob static void
7879e6461a2SMatt Jacob xpt_scanner_thread(void *dummy)
7889e6461a2SMatt Jacob {
7899e6461a2SMatt Jacob 	union ccb	*ccb;
790227d67aaSAlexander Motin 	struct cam_path	 path;
7912b83592fSScott Long 
7922b83592fSScott Long 	xpt_lock_buses();
79383c5d981SAlexander Motin 	for (;;) {
7945a73cc12SKenneth D. Merry 		if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
7952b83592fSScott Long 			msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
7963710ae64SAlexander Motin 			       "-", 0);
79783c5d981SAlexander Motin 		if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
79883c5d981SAlexander Motin 			TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
7992b83592fSScott Long 			xpt_unlock_buses();
8002b83592fSScott Long 
801227d67aaSAlexander Motin 			/*
802227d67aaSAlexander Motin 			 * Since lock can be dropped inside and path freed
803227d67aaSAlexander Motin 			 * by completion callback even before return here,
804227d67aaSAlexander Motin 			 * take our own path copy for reference.
805227d67aaSAlexander Motin 			 */
806227d67aaSAlexander Motin 			xpt_copy_path(&path, ccb->ccb_h.path);
807227d67aaSAlexander Motin 			xpt_path_lock(&path);
80883c5d981SAlexander Motin 			xpt_action(ccb);
809227d67aaSAlexander Motin 			xpt_path_unlock(&path);
810227d67aaSAlexander Motin 			xpt_release_path(&path);
81183c5d981SAlexander Motin 
81283c5d981SAlexander Motin 			xpt_lock_buses();
8139e6461a2SMatt Jacob 		}
8149e6461a2SMatt Jacob 	}
8159e6461a2SMatt Jacob }
8169e6461a2SMatt Jacob 
8179e6461a2SMatt Jacob void
8189e6461a2SMatt Jacob xpt_rescan(union ccb *ccb)
8199e6461a2SMatt Jacob {
8209e6461a2SMatt Jacob 	struct ccb_hdr *hdr;
8212b83592fSScott Long 
82283c5d981SAlexander Motin 	/* Prepare request */
8230e85f214SMatt Jacob 	if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
824411cadaeSAlexander Motin 	    ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
82583c5d981SAlexander Motin 		ccb->ccb_h.func_code = XPT_SCAN_BUS;
8260e85f214SMatt Jacob 	else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
8270e85f214SMatt Jacob 	    ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
8280e85f214SMatt Jacob 		ccb->ccb_h.func_code = XPT_SCAN_TGT;
8290e85f214SMatt Jacob 	else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
8300e85f214SMatt Jacob 	    ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
83183c5d981SAlexander Motin 		ccb->ccb_h.func_code = XPT_SCAN_LUN;
8320e85f214SMatt Jacob 	else {
8330e85f214SMatt Jacob 		xpt_print(ccb->ccb_h.path, "illegal scan path\n");
8340e85f214SMatt Jacob 		xpt_free_path(ccb->ccb_h.path);
8350e85f214SMatt Jacob 		xpt_free_ccb(ccb);
8360e85f214SMatt Jacob 		return;
8370e85f214SMatt Jacob 	}
83869be012fSWarner Losh 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
83969be012fSWarner Losh 	    ("xpt_rescan: func %#x %s\n", ccb->ccb_h.func_code,
84069be012fSWarner Losh  		xpt_action_name(ccb->ccb_h.func_code)));
84169be012fSWarner Losh 
84283c5d981SAlexander Motin 	ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
84383c5d981SAlexander Motin 	ccb->ccb_h.cbfcnp = xpt_rescan_done;
84483c5d981SAlexander Motin 	xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
84583c5d981SAlexander Motin 	/* Don't make duplicate entries for the same paths. */
8462b83592fSScott Long 	xpt_lock_buses();
84783c5d981SAlexander Motin 	if (ccb->ccb_h.ppriv_ptr1 == NULL) {
8482b83592fSScott Long 		TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
8499e6461a2SMatt Jacob 			if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
8505a73cc12SKenneth D. Merry 				wakeup(&xsoftc.ccb_scanq);
8512b83592fSScott Long 				xpt_unlock_buses();
8529e6461a2SMatt Jacob 				xpt_print(ccb->ccb_h.path, "rescan already queued\n");
8539e6461a2SMatt Jacob 				xpt_free_path(ccb->ccb_h.path);
8549e6461a2SMatt Jacob 				xpt_free_ccb(ccb);
8559e6461a2SMatt Jacob 				return;
8569e6461a2SMatt Jacob 			}
8579e6461a2SMatt Jacob 		}
85883c5d981SAlexander Motin 	}
8592b83592fSScott Long 	TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
86083c5d981SAlexander Motin 	xsoftc.buses_to_config++;
8612b83592fSScott Long 	wakeup(&xsoftc.ccb_scanq);
8622b83592fSScott Long 	xpt_unlock_buses();
8639e6461a2SMatt Jacob }
8649e6461a2SMatt Jacob 
8658b8a9b1dSJustin T. Gibbs /* Functions accessed by the peripheral drivers */
8662b83592fSScott Long static int
8679e6461a2SMatt Jacob xpt_init(void *dummy)
8688b8a9b1dSJustin T. Gibbs {
8698b8a9b1dSJustin T. Gibbs 	struct cam_sim *xpt_sim;
8708b8a9b1dSJustin T. Gibbs 	struct cam_path *path;
871434bbf6eSJustin T. Gibbs 	struct cam_devq *devq;
8728b8a9b1dSJustin T. Gibbs 	cam_status status;
873227d67aaSAlexander Motin 	int error, i;
8748b8a9b1dSJustin T. Gibbs 
8752b83592fSScott Long 	TAILQ_INIT(&xsoftc.xpt_busses);
8762b83592fSScott Long 	TAILQ_INIT(&xsoftc.ccb_scanq);
8772b83592fSScott Long 	STAILQ_INIT(&xsoftc.highpowerq);
8782b83592fSScott Long 	xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
8798b8a9b1dSJustin T. Gibbs 
8802b83592fSScott Long 	mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
881daa5487fSAlexander Motin 	mtx_init(&xsoftc.xpt_highpower_lock, "XPT highpower lock", NULL, MTX_DEF);
882227d67aaSAlexander Motin 	xsoftc.xpt_taskq = taskqueue_create("CAM XPT task", M_WAITOK,
883227d67aaSAlexander Motin 	    taskqueue_thread_enqueue, /*context*/&xsoftc.xpt_taskq);
884ef3cf714SScott Long 
8856b156f61SSean Bruno #ifdef CAM_BOOT_DELAY
8866b156f61SSean Bruno 	/*
8876b156f61SSean Bruno 	 * Override this value at compile time to assist our users
8886b156f61SSean Bruno 	 * who don't use loader to boot a kernel.
8896b156f61SSean Bruno 	 */
8906b156f61SSean Bruno 	xsoftc.boot_delay = CAM_BOOT_DELAY;
8916b156f61SSean Bruno #endif
8928b8a9b1dSJustin T. Gibbs 	/*
8931ffe5851SPedro F. Giffuni 	 * The xpt layer is, itself, the equivalent of a SIM.
8948b8a9b1dSJustin T. Gibbs 	 * Allow 16 ccbs in the ccb pool for it.  This should
8958b8a9b1dSJustin T. Gibbs 	 * give decent parallelism when we probe busses and
8968b8a9b1dSJustin T. Gibbs 	 * perform other XPT functions.
8978b8a9b1dSJustin T. Gibbs 	 */
898434bbf6eSJustin T. Gibbs 	devq = cam_simq_alloc(16);
899434bbf6eSJustin T. Gibbs 	xpt_sim = cam_sim_alloc(xptaction,
900434bbf6eSJustin T. Gibbs 				xptpoll,
901434bbf6eSJustin T. Gibbs 				"xpt",
902434bbf6eSJustin T. Gibbs 				/*softc*/NULL,
903434bbf6eSJustin T. Gibbs 				/*unit*/0,
9042b83592fSScott Long 				/*mtx*/&xsoftc.xpt_lock,
905434bbf6eSJustin T. Gibbs 				/*max_dev_transactions*/0,
906434bbf6eSJustin T. Gibbs 				/*max_tagged_dev_transactions*/0,
907434bbf6eSJustin T. Gibbs 				devq);
9082b83592fSScott Long 	if (xpt_sim == NULL)
9092b83592fSScott Long 		return (ENOMEM);
9108b8a9b1dSJustin T. Gibbs 
9112b83592fSScott Long 	mtx_lock(&xsoftc.xpt_lock);
912b50569b7SScott Long 	if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
91383c5d981SAlexander Motin 		mtx_unlock(&xsoftc.xpt_lock);
914a2821e04SMatt Jacob 		printf("xpt_init: xpt_bus_register failed with status %#x,"
915df826980SMatt Jacob 		       " failing attach\n", status);
9162b83592fSScott Long 		return (EINVAL);
917df826980SMatt Jacob 	}
918daa5487fSAlexander Motin 	mtx_unlock(&xsoftc.xpt_lock);
9198b8a9b1dSJustin T. Gibbs 
9208b8a9b1dSJustin T. Gibbs 	/*
9218b8a9b1dSJustin T. Gibbs 	 * Looking at the XPT from the SIM layer, the XPT is
9221ffe5851SPedro F. Giffuni 	 * the equivalent of a peripheral driver.  Allocate
9238b8a9b1dSJustin T. Gibbs 	 * a peripheral driver entry for us.
9248b8a9b1dSJustin T. Gibbs 	 */
9258b8a9b1dSJustin T. Gibbs 	if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
9268b8a9b1dSJustin T. Gibbs 				      CAM_TARGET_WILDCARD,
9278b8a9b1dSJustin T. Gibbs 				      CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
9288b8a9b1dSJustin T. Gibbs 		printf("xpt_init: xpt_create_path failed with status %#x,"
9298b8a9b1dSJustin T. Gibbs 		       " failing attach\n", status);
9302b83592fSScott Long 		return (EINVAL);
9318b8a9b1dSJustin T. Gibbs 	}
932daa5487fSAlexander Motin 	xpt_path_lock(path);
933ee9c90c7SKenneth D. Merry 	cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
9342b83592fSScott Long 			 path, NULL, 0, xpt_sim);
935daa5487fSAlexander Motin 	xpt_path_unlock(path);
9368b8a9b1dSJustin T. Gibbs 	xpt_free_path(path);
937daa5487fSAlexander Motin 
938227d67aaSAlexander Motin 	if (cam_num_doneqs < 1)
939227d67aaSAlexander Motin 		cam_num_doneqs = 1 + mp_ncpus / 6;
940227d67aaSAlexander Motin 	else if (cam_num_doneqs > MAXCPU)
941227d67aaSAlexander Motin 		cam_num_doneqs = MAXCPU;
942227d67aaSAlexander Motin 	for (i = 0; i < cam_num_doneqs; i++) {
943227d67aaSAlexander Motin 		mtx_init(&cam_doneqs[i].cam_doneq_mtx, "CAM doneq", NULL,
944227d67aaSAlexander Motin 		    MTX_DEF);
945227d67aaSAlexander Motin 		STAILQ_INIT(&cam_doneqs[i].cam_doneq);
946227d67aaSAlexander Motin 		error = kproc_kthread_add(xpt_done_td, &cam_doneqs[i],
947227d67aaSAlexander Motin 		    &cam_proc, NULL, 0, 0, "cam", "doneq%d", i);
948227d67aaSAlexander Motin 		if (error != 0) {
949227d67aaSAlexander Motin 			cam_num_doneqs = i;
950227d67aaSAlexander Motin 			break;
951227d67aaSAlexander Motin 		}
952227d67aaSAlexander Motin 	}
953227d67aaSAlexander Motin 	if (cam_num_doneqs < 1) {
954227d67aaSAlexander Motin 		printf("xpt_init: Cannot init completion queues "
955227d67aaSAlexander Motin 		       "- failing attach\n");
956227d67aaSAlexander Motin 		return (ENOMEM);
957227d67aaSAlexander Motin 	}
9588b8a9b1dSJustin T. Gibbs 	/*
9598b8a9b1dSJustin T. Gibbs 	 * Register a callback for when interrupts are enabled.
9608b8a9b1dSJustin T. Gibbs 	 */
9612b83592fSScott Long 	xsoftc.xpt_config_hook =
9628b8a9b1dSJustin T. Gibbs 	    (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
9630dd50e9bSScott Long 					      M_CAMXPT, M_NOWAIT | M_ZERO);
9642b83592fSScott Long 	if (xsoftc.xpt_config_hook == NULL) {
9658b8a9b1dSJustin T. Gibbs 		printf("xpt_init: Cannot malloc config hook "
9668b8a9b1dSJustin T. Gibbs 		       "- failing attach\n");
9672b83592fSScott Long 		return (ENOMEM);
9688b8a9b1dSJustin T. Gibbs 	}
9692b83592fSScott Long 	xsoftc.xpt_config_hook->ich_func = xpt_config;
9702b83592fSScott Long 	if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
9710dd50e9bSScott Long 		free (xsoftc.xpt_config_hook, M_CAMXPT);
9728b8a9b1dSJustin T. Gibbs 		printf("xpt_init: config_intrhook_establish failed "
9738b8a9b1dSJustin T. Gibbs 		       "- failing attach\n");
9748b8a9b1dSJustin T. Gibbs 	}
9758b8a9b1dSJustin T. Gibbs 
9762b83592fSScott Long 	return (0);
9778b8a9b1dSJustin T. Gibbs }
9788b8a9b1dSJustin T. Gibbs 
9798b8a9b1dSJustin T. Gibbs static cam_status
9808b8a9b1dSJustin T. Gibbs xptregister(struct cam_periph *periph, void *arg)
9818b8a9b1dSJustin T. Gibbs {
9822b83592fSScott Long 	struct cam_sim *xpt_sim;
9832b83592fSScott Long 
9848b8a9b1dSJustin T. Gibbs 	if (periph == NULL) {
9858b8a9b1dSJustin T. Gibbs 		printf("xptregister: periph was NULL!!\n");
9868b8a9b1dSJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
9878b8a9b1dSJustin T. Gibbs 	}
9888b8a9b1dSJustin T. Gibbs 
9892b83592fSScott Long 	xpt_sim = (struct cam_sim *)arg;
9902b83592fSScott Long 	xpt_sim->softc = periph;
9918b8a9b1dSJustin T. Gibbs 	xpt_periph = periph;
9922b83592fSScott Long 	periph->softc = NULL;
9938b8a9b1dSJustin T. Gibbs 
9948b8a9b1dSJustin T. Gibbs 	return(CAM_REQ_CMP);
9958b8a9b1dSJustin T. Gibbs }
9968b8a9b1dSJustin T. Gibbs 
9978b8a9b1dSJustin T. Gibbs int32_t
9988b8a9b1dSJustin T. Gibbs xpt_add_periph(struct cam_periph *periph)
9998b8a9b1dSJustin T. Gibbs {
10008b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
10018b8a9b1dSJustin T. Gibbs 	int32_t	 status;
10028b8a9b1dSJustin T. Gibbs 
1003227d67aaSAlexander Motin 	TASK_INIT(&periph->periph_run_task, 0, xpt_run_allocq_task, periph);
10048b8a9b1dSJustin T. Gibbs 	device = periph->path->device;
10058b8a9b1dSJustin T. Gibbs 	status = CAM_REQ_CMP;
10068b8a9b1dSJustin T. Gibbs 	if (device != NULL) {
1007227d67aaSAlexander Motin 		mtx_lock(&device->target->bus->eb_mtx);
10088b8a9b1dSJustin T. Gibbs 		device->generation++;
1009227d67aaSAlexander Motin 		SLIST_INSERT_HEAD(&device->periphs, periph, periph_links);
1010227d67aaSAlexander Motin 		mtx_unlock(&device->target->bus->eb_mtx);
1011636870ffSWill Andrews 		atomic_add_32(&xsoftc.xpt_generation, 1);
10128b8a9b1dSJustin T. Gibbs 	}
10138b8a9b1dSJustin T. Gibbs 
10148b8a9b1dSJustin T. Gibbs 	return (status);
10158b8a9b1dSJustin T. Gibbs }
10168b8a9b1dSJustin T. Gibbs 
10178b8a9b1dSJustin T. Gibbs void
1018a29779e8SAlexander Motin xpt_remove_periph(struct cam_periph *periph)
10198b8a9b1dSJustin T. Gibbs {
10208b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
10218b8a9b1dSJustin T. Gibbs 
10228b8a9b1dSJustin T. Gibbs 	device = periph->path->device;
10238b8a9b1dSJustin T. Gibbs 	if (device != NULL) {
1024227d67aaSAlexander Motin 		mtx_lock(&device->target->bus->eb_mtx);
10258b8a9b1dSJustin T. Gibbs 		device->generation++;
1026227d67aaSAlexander Motin 		SLIST_REMOVE(&device->periphs, periph, cam_periph, periph_links);
1027227d67aaSAlexander Motin 		mtx_unlock(&device->target->bus->eb_mtx);
1028636870ffSWill Andrews 		atomic_add_32(&xsoftc.xpt_generation, 1);
10298b8a9b1dSJustin T. Gibbs 	}
10308b8a9b1dSJustin T. Gibbs }
10318b8a9b1dSJustin T. Gibbs 
10323393f8daSKenneth D. Merry 
10333393f8daSKenneth D. Merry void
10343393f8daSKenneth D. Merry xpt_announce_periph(struct cam_periph *periph, char *announce_string)
10353393f8daSKenneth D. Merry {
103657079b17SAlexander Motin 	struct	cam_path *path = periph->path;
103708f13879SWarner Losh 	struct  xpt_proto *proto;
10383393f8daSKenneth D. Merry 
1039227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
10408d36a71bSAlexander Motin 	periph->flags |= CAM_PERIPH_ANNOUNCED;
104168153f43SScott Long 
1042abe83505SNathan Whitehorn 	printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
10433393f8daSKenneth D. Merry 	       periph->periph_name, periph->unit_number,
10443393f8daSKenneth D. Merry 	       path->bus->sim->sim_name,
10453393f8daSKenneth D. Merry 	       path->bus->sim->unit_number,
10463393f8daSKenneth D. Merry 	       path->bus->sim->bus_id,
1047ad413009SAlexander Motin 	       path->bus->path_id,
10483393f8daSKenneth D. Merry 	       path->target->target_id,
1049abe83505SNathan Whitehorn 	       (uintmax_t)path->device->lun_id);
10503393f8daSKenneth D. Merry 	printf("%s%d: ", periph->periph_name, periph->unit_number);
105108f13879SWarner Losh 	proto = xpt_proto_find(path->device->protocol);
105208f13879SWarner Losh 	if (proto)
105308f13879SWarner Losh 		proto->ops->announce(path->device);
105452c9ce25SScott Long 	else
105508f13879SWarner Losh 		printf("%s%d: Unknown protocol device %d\n",
105608f13879SWarner Losh 		    periph->periph_name, periph->unit_number,
105708f13879SWarner Losh 		    path->device->protocol);
1058aa93041dSAlexander Motin 	if (path->device->serial_num_len > 0) {
10593393f8daSKenneth D. Merry 		/* Don't wrap the screen  - print only the first 60 chars */
10603393f8daSKenneth D. Merry 		printf("%s%d: Serial Number %.60s\n", periph->periph_name,
10613393f8daSKenneth D. Merry 		       periph->unit_number, path->device->serial_num);
10623393f8daSKenneth D. Merry 	}
106357079b17SAlexander Motin 	/* Announce transport details. */
106408f13879SWarner Losh 	path->bus->xport->ops->announce(periph);
106557079b17SAlexander Motin 	/* Announce command queueing. */
10663393f8daSKenneth D. Merry 	if (path->device->inq_flags & SID_CmdQue
10673393f8daSKenneth D. Merry 	 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
10680aacc535SAlexander Motin 		printf("%s%d: Command Queueing enabled\n",
10693393f8daSKenneth D. Merry 		       periph->periph_name, periph->unit_number);
10703393f8daSKenneth D. Merry 	}
107157079b17SAlexander Motin 	/* Announce caller's details if they've passed in. */
10723393f8daSKenneth D. Merry 	if (announce_string != NULL)
10733393f8daSKenneth D. Merry 		printf("%s%d: %s\n", periph->periph_name,
10743393f8daSKenneth D. Merry 		       periph->unit_number, announce_string);
10753393f8daSKenneth D. Merry }
10768b8a9b1dSJustin T. Gibbs 
10776fb5c84eSSteven Hartland void
10786fb5c84eSSteven Hartland xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
10796fb5c84eSSteven Hartland {
10806fb5c84eSSteven Hartland 	if (quirks != 0) {
10816fb5c84eSSteven Hartland 		printf("%s%d: quirks=0x%b\n", periph->periph_name,
10826fb5c84eSSteven Hartland 		    periph->unit_number, quirks, bit_string);
10836fb5c84eSSteven Hartland 	}
10846fb5c84eSSteven Hartland }
10856fb5c84eSSteven Hartland 
10868d36a71bSAlexander Motin void
10878d36a71bSAlexander Motin xpt_denounce_periph(struct cam_periph *periph)
10888d36a71bSAlexander Motin {
10898d36a71bSAlexander Motin 	struct	cam_path *path = periph->path;
109008f13879SWarner Losh 	struct  xpt_proto *proto;
10918d36a71bSAlexander Motin 
1092227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
1093abe83505SNathan Whitehorn 	printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
10948d36a71bSAlexander Motin 	       periph->periph_name, periph->unit_number,
10958d36a71bSAlexander Motin 	       path->bus->sim->sim_name,
10968d36a71bSAlexander Motin 	       path->bus->sim->unit_number,
10978d36a71bSAlexander Motin 	       path->bus->sim->bus_id,
10988d36a71bSAlexander Motin 	       path->bus->path_id,
10998d36a71bSAlexander Motin 	       path->target->target_id,
1100abe83505SNathan Whitehorn 	       (uintmax_t)path->device->lun_id);
11018d36a71bSAlexander Motin 	printf("%s%d: ", periph->periph_name, periph->unit_number);
110208f13879SWarner Losh 	proto = xpt_proto_find(path->device->protocol);
110308f13879SWarner Losh 	if (proto)
110408f13879SWarner Losh 		proto->ops->denounce(path->device);
11058d36a71bSAlexander Motin 	else
110608f13879SWarner Losh 		printf("%s%d: Unknown protocol device %d\n",
110708f13879SWarner Losh 		    periph->periph_name, periph->unit_number,
110808f13879SWarner Losh 		    path->device->protocol);
11098d36a71bSAlexander Motin 	if (path->device->serial_num_len > 0)
11108d36a71bSAlexander Motin 		printf(" s/n %.60s", path->device->serial_num);
11118d36a71bSAlexander Motin 	printf(" detached\n");
11128d36a71bSAlexander Motin }
11138d36a71bSAlexander Motin 
11148d36a71bSAlexander Motin 
11153501942bSJustin T. Gibbs int
11163501942bSJustin T. Gibbs xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
11173501942bSJustin T. Gibbs {
1118ccba7102SAlexander Motin 	int ret = -1, l;
11193501942bSJustin T. Gibbs 	struct ccb_dev_advinfo cdai;
1120ccba7102SAlexander Motin 	struct scsi_vpd_id_descriptor *idd;
11213501942bSJustin T. Gibbs 
1122227d67aaSAlexander Motin 	xpt_path_assert(path, MA_OWNED);
11236884b662SAlexander Motin 
11243501942bSJustin T. Gibbs 	memset(&cdai, 0, sizeof(cdai));
11253501942bSJustin T. Gibbs 	xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
11263501942bSJustin T. Gibbs 	cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1127*ab4327bbSAlexander Motin 	cdai.flags = CDAI_FLAG_NONE;
11283501942bSJustin T. Gibbs 	cdai.bufsiz = len;
11293501942bSJustin T. Gibbs 
11303501942bSJustin T. Gibbs 	if (!strcmp(attr, "GEOM::ident"))
11313501942bSJustin T. Gibbs 		cdai.buftype = CDAI_TYPE_SERIAL_NUM;
11323501942bSJustin T. Gibbs 	else if (!strcmp(attr, "GEOM::physpath"))
11333501942bSJustin T. Gibbs 		cdai.buftype = CDAI_TYPE_PHYS_PATH;
113440f27d7cSAlexander Motin 	else if (strcmp(attr, "GEOM::lunid") == 0 ||
113540f27d7cSAlexander Motin 		 strcmp(attr, "GEOM::lunname") == 0) {
1136ccba7102SAlexander Motin 		cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1137ccba7102SAlexander Motin 		cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1138ccba7102SAlexander Motin 	} else
11393501942bSJustin T. Gibbs 		goto out;
11403501942bSJustin T. Gibbs 
11413501942bSJustin T. Gibbs 	cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
11423501942bSJustin T. Gibbs 	if (cdai.buf == NULL) {
11433501942bSJustin T. Gibbs 		ret = ENOMEM;
11443501942bSJustin T. Gibbs 		goto out;
11453501942bSJustin T. Gibbs 	}
11463501942bSJustin T. Gibbs 	xpt_action((union ccb *)&cdai); /* can only be synchronous */
11473501942bSJustin T. Gibbs 	if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
11483501942bSJustin T. Gibbs 		cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
11493501942bSJustin T. Gibbs 	if (cdai.provsiz == 0)
11503501942bSJustin T. Gibbs 		goto out;
1151ccba7102SAlexander Motin 	if (cdai.buftype == CDAI_TYPE_SCSI_DEVID) {
115240f27d7cSAlexander Motin 		if (strcmp(attr, "GEOM::lunid") == 0) {
1153ccba7102SAlexander Motin 			idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1154ccba7102SAlexander Motin 			    cdai.provsiz, scsi_devid_is_lun_naa);
1155ccba7102SAlexander Motin 			if (idd == NULL)
1156ccba7102SAlexander Motin 				idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1157ccba7102SAlexander Motin 				    cdai.provsiz, scsi_devid_is_lun_eui64);
115840f27d7cSAlexander Motin 		} else
115940f27d7cSAlexander Motin 			idd = NULL;
1160ccba7102SAlexander Motin 		if (idd == NULL)
1161ccba7102SAlexander Motin 			idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1162ccba7102SAlexander Motin 			    cdai.provsiz, scsi_devid_is_lun_t10);
1163ccba7102SAlexander Motin 		if (idd == NULL)
1164ccba7102SAlexander Motin 			idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1165ccba7102SAlexander Motin 			    cdai.provsiz, scsi_devid_is_lun_name);
1166ccba7102SAlexander Motin 		if (idd == NULL)
1167ccba7102SAlexander Motin 			goto out;
1168ccba7102SAlexander Motin 		ret = 0;
1169fa91cabfSAlexander Motin 		if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_ASCII) {
1170fa91cabfSAlexander Motin 			if (idd->length < len) {
1171fa91cabfSAlexander Motin 				for (l = 0; l < idd->length; l++)
1172fa91cabfSAlexander Motin 					buf[l] = idd->identifier[l] ?
1173fa91cabfSAlexander Motin 					    idd->identifier[l] : ' ';
1174fa91cabfSAlexander Motin 				buf[l] = 0;
1175fa91cabfSAlexander Motin 			} else
1176fa91cabfSAlexander Motin 				ret = EFAULT;
1177fa91cabfSAlexander Motin 		} else if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_UTF8) {
1178ccba7102SAlexander Motin 			l = strnlen(idd->identifier, idd->length);
1179ccba7102SAlexander Motin 			if (l < len) {
1180ccba7102SAlexander Motin 				bcopy(idd->identifier, buf, l);
1181ccba7102SAlexander Motin 				buf[l] = 0;
1182ccba7102SAlexander Motin 			} else
1183ccba7102SAlexander Motin 				ret = EFAULT;
1184ccba7102SAlexander Motin 		} else {
1185ccba7102SAlexander Motin 			if (idd->length * 2 < len) {
1186ccba7102SAlexander Motin 				for (l = 0; l < idd->length; l++)
1187ccba7102SAlexander Motin 					sprintf(buf + l * 2, "%02x",
1188ccba7102SAlexander Motin 					    idd->identifier[l]);
1189ccba7102SAlexander Motin 			} else
1190ccba7102SAlexander Motin 				ret = EFAULT;
1191ccba7102SAlexander Motin 		}
1192ccba7102SAlexander Motin 	} else {
11933501942bSJustin T. Gibbs 		ret = 0;
11943501942bSJustin T. Gibbs 		if (strlcpy(buf, cdai.buf, len) >= len)
11953501942bSJustin T. Gibbs 			ret = EFAULT;
1196ccba7102SAlexander Motin 	}
11973501942bSJustin T. Gibbs 
11983501942bSJustin T. Gibbs out:
11993501942bSJustin T. Gibbs 	if (cdai.buf != NULL)
12003501942bSJustin T. Gibbs 		free(cdai.buf, M_CAMXPT);
12013501942bSJustin T. Gibbs 	return ret;
12023501942bSJustin T. Gibbs }
12033501942bSJustin T. Gibbs 
12048b8a9b1dSJustin T. Gibbs static dev_match_ret
12053393f8daSKenneth D. Merry xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
12068b8a9b1dSJustin T. Gibbs 	    struct cam_eb *bus)
12078b8a9b1dSJustin T. Gibbs {
12088b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
1209167e63e3SPedro F. Giffuni 	u_int i;
12108b8a9b1dSJustin T. Gibbs 
12118b8a9b1dSJustin T. Gibbs 	retval = DM_RET_NONE;
12128b8a9b1dSJustin T. Gibbs 
12138b8a9b1dSJustin T. Gibbs 	/*
12148b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
12158b8a9b1dSJustin T. Gibbs 	 */
12168b8a9b1dSJustin T. Gibbs 	if (bus == NULL)
12178b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
12188b8a9b1dSJustin T. Gibbs 
12198b8a9b1dSJustin T. Gibbs 	/*
12208b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this bus matches no
12218b8a9b1dSJustin T. Gibbs 	 * matter what.
12228b8a9b1dSJustin T. Gibbs 	 */
12238b8a9b1dSJustin T. Gibbs 	if ((patterns == NULL) || (num_patterns == 0))
12248b8a9b1dSJustin T. Gibbs 		return(DM_RET_DESCEND | DM_RET_COPY);
12258b8a9b1dSJustin T. Gibbs 
12268b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
12278b8a9b1dSJustin T. Gibbs 		struct bus_match_pattern *cur_pattern;
12288b8a9b1dSJustin T. Gibbs 
12298b8a9b1dSJustin T. Gibbs 		/*
12308b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a bus node, we
12318b8a9b1dSJustin T. Gibbs 		 * aren't interested.  However, we do indicate to the
12328b8a9b1dSJustin T. Gibbs 		 * calling routine that we should continue descending the
12338b8a9b1dSJustin T. Gibbs 		 * tree, since the user wants to match against lower-level
12348b8a9b1dSJustin T. Gibbs 		 * EDT elements.
12358b8a9b1dSJustin T. Gibbs 		 */
12368b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_BUS) {
12378b8a9b1dSJustin T. Gibbs 			if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
12388b8a9b1dSJustin T. Gibbs 				retval |= DM_RET_DESCEND;
12398b8a9b1dSJustin T. Gibbs 			continue;
12408b8a9b1dSJustin T. Gibbs 		}
12418b8a9b1dSJustin T. Gibbs 
12428b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.bus_pattern;
12438b8a9b1dSJustin T. Gibbs 
12448b8a9b1dSJustin T. Gibbs 		/*
12458b8a9b1dSJustin T. Gibbs 		 * If they want to match any bus node, we give them any
12468b8a9b1dSJustin T. Gibbs 		 * device node.
12478b8a9b1dSJustin T. Gibbs 		 */
12488b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == BUS_MATCH_ANY) {
12498b8a9b1dSJustin T. Gibbs 			/* set the copy flag */
12508b8a9b1dSJustin T. Gibbs 			retval |= DM_RET_COPY;
12518b8a9b1dSJustin T. Gibbs 
12528b8a9b1dSJustin T. Gibbs 			/*
12538b8a9b1dSJustin T. Gibbs 			 * If we've already decided on an action, go ahead
12548b8a9b1dSJustin T. Gibbs 			 * and return.
12558b8a9b1dSJustin T. Gibbs 			 */
12568b8a9b1dSJustin T. Gibbs 			if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
12578b8a9b1dSJustin T. Gibbs 				return(retval);
12588b8a9b1dSJustin T. Gibbs 		}
12598b8a9b1dSJustin T. Gibbs 
12608b8a9b1dSJustin T. Gibbs 		/*
12618b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
12628b8a9b1dSJustin T. Gibbs 		 */
12638b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == BUS_MATCH_NONE)
12648b8a9b1dSJustin T. Gibbs 			continue;
12658b8a9b1dSJustin T. Gibbs 
12668b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
12678b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != bus->path_id))
12688b8a9b1dSJustin T. Gibbs 			continue;
12698b8a9b1dSJustin T. Gibbs 
12708b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
12718b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->bus_id != bus->sim->bus_id))
12728b8a9b1dSJustin T. Gibbs 			continue;
12738b8a9b1dSJustin T. Gibbs 
12748b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
12758b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->unit_number != bus->sim->unit_number))
12768b8a9b1dSJustin T. Gibbs 			continue;
12778b8a9b1dSJustin T. Gibbs 
12788b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
12798b8a9b1dSJustin T. Gibbs 		 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
12808b8a9b1dSJustin T. Gibbs 			     DEV_IDLEN) != 0))
12818b8a9b1dSJustin T. Gibbs 			continue;
12828b8a9b1dSJustin T. Gibbs 
12838b8a9b1dSJustin T. Gibbs 		/*
12848b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
12858b8a9b1dSJustin T. Gibbs 		 * information on this bus.  So tell the caller to copy the
12868b8a9b1dSJustin T. Gibbs 		 * data out.
12878b8a9b1dSJustin T. Gibbs 		 */
12888b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
12898b8a9b1dSJustin T. Gibbs 
12908b8a9b1dSJustin T. Gibbs 		/*
12918b8a9b1dSJustin T. Gibbs 		 * If the return action has been set to descend, then we
12928b8a9b1dSJustin T. Gibbs 		 * know that we've already seen a non-bus matching
12938b8a9b1dSJustin T. Gibbs 		 * expression, therefore we need to further descend the tree.
12948b8a9b1dSJustin T. Gibbs 		 * This won't change by continuing around the loop, so we
12958b8a9b1dSJustin T. Gibbs 		 * go ahead and return.  If we haven't seen a non-bus
12968b8a9b1dSJustin T. Gibbs 		 * matching expression, we keep going around the loop until
12978b8a9b1dSJustin T. Gibbs 		 * we exhaust the matching expressions.  We'll set the stop
12988b8a9b1dSJustin T. Gibbs 		 * flag once we fall out of the loop.
12998b8a9b1dSJustin T. Gibbs 		 */
13008b8a9b1dSJustin T. Gibbs 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
13018b8a9b1dSJustin T. Gibbs 			return(retval);
13028b8a9b1dSJustin T. Gibbs 	}
13038b8a9b1dSJustin T. Gibbs 
13048b8a9b1dSJustin T. Gibbs 	/*
13058b8a9b1dSJustin T. Gibbs 	 * If the return action hasn't been set to descend yet, that means
13068b8a9b1dSJustin T. Gibbs 	 * we haven't seen anything other than bus matching patterns.  So
13078b8a9b1dSJustin T. Gibbs 	 * tell the caller to stop descending the tree -- the user doesn't
13088b8a9b1dSJustin T. Gibbs 	 * want to match against lower level tree elements.
13098b8a9b1dSJustin T. Gibbs 	 */
13108b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
13118b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_STOP;
13128b8a9b1dSJustin T. Gibbs 
13138b8a9b1dSJustin T. Gibbs 	return(retval);
13148b8a9b1dSJustin T. Gibbs }
13158b8a9b1dSJustin T. Gibbs 
13168b8a9b1dSJustin T. Gibbs static dev_match_ret
13173393f8daSKenneth D. Merry xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
13188b8a9b1dSJustin T. Gibbs 	       struct cam_ed *device)
13198b8a9b1dSJustin T. Gibbs {
13208b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
1321167e63e3SPedro F. Giffuni 	u_int i;
13228b8a9b1dSJustin T. Gibbs 
13238b8a9b1dSJustin T. Gibbs 	retval = DM_RET_NONE;
13248b8a9b1dSJustin T. Gibbs 
13258b8a9b1dSJustin T. Gibbs 	/*
13268b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
13278b8a9b1dSJustin T. Gibbs 	 */
13288b8a9b1dSJustin T. Gibbs 	if (device == NULL)
13298b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
13308b8a9b1dSJustin T. Gibbs 
13318b8a9b1dSJustin T. Gibbs 	/*
13328b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this device matches no
13338b8a9b1dSJustin T. Gibbs 	 * matter what.
13348b8a9b1dSJustin T. Gibbs 	 */
133559e75884SColin Percival 	if ((patterns == NULL) || (num_patterns == 0))
13368b8a9b1dSJustin T. Gibbs 		return(DM_RET_DESCEND | DM_RET_COPY);
13378b8a9b1dSJustin T. Gibbs 
13388b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
13398b8a9b1dSJustin T. Gibbs 		struct device_match_pattern *cur_pattern;
13403501942bSJustin T. Gibbs 		struct scsi_vpd_device_id *device_id_page;
13418b8a9b1dSJustin T. Gibbs 
13428b8a9b1dSJustin T. Gibbs 		/*
13438b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a device node, we
13448b8a9b1dSJustin T. Gibbs 		 * aren't interested.
13458b8a9b1dSJustin T. Gibbs 		 */
13468b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_DEVICE) {
13478b8a9b1dSJustin T. Gibbs 			if ((patterns[i].type == DEV_MATCH_PERIPH)
13488b8a9b1dSJustin T. Gibbs 			 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
13498b8a9b1dSJustin T. Gibbs 				retval |= DM_RET_DESCEND;
13508b8a9b1dSJustin T. Gibbs 			continue;
13518b8a9b1dSJustin T. Gibbs 		}
13528b8a9b1dSJustin T. Gibbs 
13538b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.device_pattern;
13548b8a9b1dSJustin T. Gibbs 
13553501942bSJustin T. Gibbs 		/* Error out if mutually exclusive options are specified. */
13563501942bSJustin T. Gibbs 		if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
13573501942bSJustin T. Gibbs 		 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
13583501942bSJustin T. Gibbs 			return(DM_RET_ERROR);
13593501942bSJustin T. Gibbs 
13608b8a9b1dSJustin T. Gibbs 		/*
13618b8a9b1dSJustin T. Gibbs 		 * If they want to match any device node, we give them any
13628b8a9b1dSJustin T. Gibbs 		 * device node.
13638b8a9b1dSJustin T. Gibbs 		 */
13643501942bSJustin T. Gibbs 		if (cur_pattern->flags == DEV_MATCH_ANY)
13653501942bSJustin T. Gibbs 			goto copy_dev_node;
13668b8a9b1dSJustin T. Gibbs 
13678b8a9b1dSJustin T. Gibbs 		/*
13688b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
13698b8a9b1dSJustin T. Gibbs 		 */
13708b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == DEV_MATCH_NONE)
13718b8a9b1dSJustin T. Gibbs 			continue;
13728b8a9b1dSJustin T. Gibbs 
13738b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
13748b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != device->target->bus->path_id))
13758b8a9b1dSJustin T. Gibbs 			continue;
13768b8a9b1dSJustin T. Gibbs 
13778b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
13788b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->target_id != device->target->target_id))
13798b8a9b1dSJustin T. Gibbs 			continue;
13808b8a9b1dSJustin T. Gibbs 
13818b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
13828b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->target_lun != device->lun_id))
13838b8a9b1dSJustin T. Gibbs 			continue;
13848b8a9b1dSJustin T. Gibbs 
13858b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
13868b8a9b1dSJustin T. Gibbs 		 && (cam_quirkmatch((caddr_t)&device->inq_data,
13873501942bSJustin T. Gibbs 				    (caddr_t)&cur_pattern->data.inq_pat,
13883501942bSJustin T. Gibbs 				    1, sizeof(cur_pattern->data.inq_pat),
13898b8a9b1dSJustin T. Gibbs 				    scsi_static_inquiry_match) == NULL))
13908b8a9b1dSJustin T. Gibbs 			continue;
13918b8a9b1dSJustin T. Gibbs 
13923501942bSJustin T. Gibbs 		device_id_page = (struct scsi_vpd_device_id *)device->device_id;
13933501942bSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
13943501942bSJustin T. Gibbs 		 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
13953501942bSJustin T. Gibbs 		  || scsi_devid_match((uint8_t *)device_id_page->desc_list,
13963501942bSJustin T. Gibbs 				      device->device_id_len
13973501942bSJustin T. Gibbs 				    - SVPD_DEVICE_ID_HDR_LEN,
13983501942bSJustin T. Gibbs 				      cur_pattern->data.devid_pat.id,
13993501942bSJustin T. Gibbs 				      cur_pattern->data.devid_pat.id_len) != 0))
14003501942bSJustin T. Gibbs 			continue;
14013501942bSJustin T. Gibbs 
14023501942bSJustin T. Gibbs copy_dev_node:
14038b8a9b1dSJustin T. Gibbs 		/*
14048b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
14058b8a9b1dSJustin T. Gibbs 		 * information on this device.  So tell the caller to copy
14068b8a9b1dSJustin T. Gibbs 		 * the data out.
14078b8a9b1dSJustin T. Gibbs 		 */
14088b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
14098b8a9b1dSJustin T. Gibbs 
14108b8a9b1dSJustin T. Gibbs 		/*
14118b8a9b1dSJustin T. Gibbs 		 * If the return action has been set to descend, then we
14128b8a9b1dSJustin T. Gibbs 		 * know that we've already seen a peripheral matching
14138b8a9b1dSJustin T. Gibbs 		 * expression, therefore we need to further descend the tree.
14148b8a9b1dSJustin T. Gibbs 		 * This won't change by continuing around the loop, so we
14158b8a9b1dSJustin T. Gibbs 		 * go ahead and return.  If we haven't seen a peripheral
14168b8a9b1dSJustin T. Gibbs 		 * matching expression, we keep going around the loop until
14178b8a9b1dSJustin T. Gibbs 		 * we exhaust the matching expressions.  We'll set the stop
14188b8a9b1dSJustin T. Gibbs 		 * flag once we fall out of the loop.
14198b8a9b1dSJustin T. Gibbs 		 */
14208b8a9b1dSJustin T. Gibbs 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
14218b8a9b1dSJustin T. Gibbs 			return(retval);
14228b8a9b1dSJustin T. Gibbs 	}
14238b8a9b1dSJustin T. Gibbs 
14248b8a9b1dSJustin T. Gibbs 	/*
14258b8a9b1dSJustin T. Gibbs 	 * If the return action hasn't been set to descend yet, that means
14268b8a9b1dSJustin T. Gibbs 	 * we haven't seen any peripheral matching patterns.  So tell the
14278b8a9b1dSJustin T. Gibbs 	 * caller to stop descending the tree -- the user doesn't want to
14288b8a9b1dSJustin T. Gibbs 	 * match against lower level tree elements.
14298b8a9b1dSJustin T. Gibbs 	 */
14308b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
14318b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_STOP;
14328b8a9b1dSJustin T. Gibbs 
14338b8a9b1dSJustin T. Gibbs 	return(retval);
14348b8a9b1dSJustin T. Gibbs }
14358b8a9b1dSJustin T. Gibbs 
14368b8a9b1dSJustin T. Gibbs /*
14378b8a9b1dSJustin T. Gibbs  * Match a single peripheral against any number of match patterns.
14388b8a9b1dSJustin T. Gibbs  */
14398b8a9b1dSJustin T. Gibbs static dev_match_ret
14403393f8daSKenneth D. Merry xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
14418b8a9b1dSJustin T. Gibbs 	       struct cam_periph *periph)
14428b8a9b1dSJustin T. Gibbs {
14438b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
1444167e63e3SPedro F. Giffuni 	u_int i;
14458b8a9b1dSJustin T. Gibbs 
14468b8a9b1dSJustin T. Gibbs 	/*
14478b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
14488b8a9b1dSJustin T. Gibbs 	 */
14498b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
14508b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
14518b8a9b1dSJustin T. Gibbs 
14528b8a9b1dSJustin T. Gibbs 	/*
14538b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this peripheral matches no
14548b8a9b1dSJustin T. Gibbs 	 * matter what.
14558b8a9b1dSJustin T. Gibbs 	 */
14568b8a9b1dSJustin T. Gibbs 	if ((patterns == NULL) || (num_patterns == 0))
14578b8a9b1dSJustin T. Gibbs 		return(DM_RET_STOP | DM_RET_COPY);
14588b8a9b1dSJustin T. Gibbs 
14598b8a9b1dSJustin T. Gibbs 	/*
14608b8a9b1dSJustin T. Gibbs 	 * There aren't any nodes below a peripheral node, so there's no
14618b8a9b1dSJustin T. Gibbs 	 * reason to descend the tree any further.
14628b8a9b1dSJustin T. Gibbs 	 */
14638b8a9b1dSJustin T. Gibbs 	retval = DM_RET_STOP;
14648b8a9b1dSJustin T. Gibbs 
14658b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
14668b8a9b1dSJustin T. Gibbs 		struct periph_match_pattern *cur_pattern;
14678b8a9b1dSJustin T. Gibbs 
14688b8a9b1dSJustin T. Gibbs 		/*
14698b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a peripheral, we
14708b8a9b1dSJustin T. Gibbs 		 * aren't interested.
14718b8a9b1dSJustin T. Gibbs 		 */
14728b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_PERIPH)
14738b8a9b1dSJustin T. Gibbs 			continue;
14748b8a9b1dSJustin T. Gibbs 
14758b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.periph_pattern;
14768b8a9b1dSJustin T. Gibbs 
14778b8a9b1dSJustin T. Gibbs 		/*
14788b8a9b1dSJustin T. Gibbs 		 * If they want to match on anything, then we will do so.
14798b8a9b1dSJustin T. Gibbs 		 */
14808b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == PERIPH_MATCH_ANY) {
14818b8a9b1dSJustin T. Gibbs 			/* set the copy flag */
14828b8a9b1dSJustin T. Gibbs 			retval |= DM_RET_COPY;
14838b8a9b1dSJustin T. Gibbs 
14848b8a9b1dSJustin T. Gibbs 			/*
14858b8a9b1dSJustin T. Gibbs 			 * We've already set the return action to stop,
14868b8a9b1dSJustin T. Gibbs 			 * since there are no nodes below peripherals in
14878b8a9b1dSJustin T. Gibbs 			 * the tree.
14888b8a9b1dSJustin T. Gibbs 			 */
14898b8a9b1dSJustin T. Gibbs 			return(retval);
14908b8a9b1dSJustin T. Gibbs 		}
14918b8a9b1dSJustin T. Gibbs 
14928b8a9b1dSJustin T. Gibbs 		/*
14938b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
14948b8a9b1dSJustin T. Gibbs 		 */
14958b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == PERIPH_MATCH_NONE)
14968b8a9b1dSJustin T. Gibbs 			continue;
14978b8a9b1dSJustin T. Gibbs 
14988b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
14998b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != periph->path->bus->path_id))
15008b8a9b1dSJustin T. Gibbs 			continue;
15018b8a9b1dSJustin T. Gibbs 
15028b8a9b1dSJustin T. Gibbs 		/*
15038b8a9b1dSJustin T. Gibbs 		 * For the target and lun id's, we have to make sure the
15048b8a9b1dSJustin T. Gibbs 		 * target and lun pointers aren't NULL.  The xpt peripheral
15058b8a9b1dSJustin T. Gibbs 		 * has a wildcard target and device.
15068b8a9b1dSJustin T. Gibbs 		 */
15078b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
15088b8a9b1dSJustin T. Gibbs 		 && ((periph->path->target == NULL)
15098b8a9b1dSJustin T. Gibbs 		 ||(cur_pattern->target_id != periph->path->target->target_id)))
15108b8a9b1dSJustin T. Gibbs 			continue;
15118b8a9b1dSJustin T. Gibbs 
15128b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
15138b8a9b1dSJustin T. Gibbs 		 && ((periph->path->device == NULL)
15148b8a9b1dSJustin T. Gibbs 		 || (cur_pattern->target_lun != periph->path->device->lun_id)))
15158b8a9b1dSJustin T. Gibbs 			continue;
15168b8a9b1dSJustin T. Gibbs 
15178b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
15188b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->unit_number != periph->unit_number))
15198b8a9b1dSJustin T. Gibbs 			continue;
15208b8a9b1dSJustin T. Gibbs 
15218b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
15228b8a9b1dSJustin T. Gibbs 		 && (strncmp(cur_pattern->periph_name, periph->periph_name,
15238b8a9b1dSJustin T. Gibbs 			     DEV_IDLEN) != 0))
15248b8a9b1dSJustin T. Gibbs 			continue;
15258b8a9b1dSJustin T. Gibbs 
15268b8a9b1dSJustin T. Gibbs 		/*
15278b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
15288b8a9b1dSJustin T. Gibbs 		 * information on this peripheral.  So tell the caller to
15298b8a9b1dSJustin T. Gibbs 		 * copy the data out.
15308b8a9b1dSJustin T. Gibbs 		 */
15318b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
15328b8a9b1dSJustin T. Gibbs 
15338b8a9b1dSJustin T. Gibbs 		/*
15348b8a9b1dSJustin T. Gibbs 		 * The return action has already been set to stop, since
15358b8a9b1dSJustin T. Gibbs 		 * peripherals don't have any nodes below them in the EDT.
15368b8a9b1dSJustin T. Gibbs 		 */
15378b8a9b1dSJustin T. Gibbs 		return(retval);
15388b8a9b1dSJustin T. Gibbs 	}
15398b8a9b1dSJustin T. Gibbs 
15408b8a9b1dSJustin T. Gibbs 	/*
15418b8a9b1dSJustin T. Gibbs 	 * If we get to this point, the peripheral that was passed in
15428b8a9b1dSJustin T. Gibbs 	 * doesn't match any of the patterns.
15438b8a9b1dSJustin T. Gibbs 	 */
15448b8a9b1dSJustin T. Gibbs 	return(retval);
15458b8a9b1dSJustin T. Gibbs }
15468b8a9b1dSJustin T. Gibbs 
15478b8a9b1dSJustin T. Gibbs static int
15488b8a9b1dSJustin T. Gibbs xptedtbusfunc(struct cam_eb *bus, void *arg)
15498b8a9b1dSJustin T. Gibbs {
15508b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
1551227d67aaSAlexander Motin 	struct cam_et *target;
15528b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
15538b8a9b1dSJustin T. Gibbs 
15548b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
15558b8a9b1dSJustin T. Gibbs 
15568b8a9b1dSJustin T. Gibbs 	/*
15578b8a9b1dSJustin T. Gibbs 	 * If our position is for something deeper in the tree, that means
15588b8a9b1dSJustin T. Gibbs 	 * that we've already seen this node.  So, we keep going down.
15598b8a9b1dSJustin T. Gibbs 	 */
15608b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
15618b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == bus)
15628b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
15638b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target != NULL))
15648b8a9b1dSJustin T. Gibbs 		retval = DM_RET_DESCEND;
15658b8a9b1dSJustin T. Gibbs 	else
15668b8a9b1dSJustin T. Gibbs 		retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
15678b8a9b1dSJustin T. Gibbs 
15688b8a9b1dSJustin T. Gibbs 	/*
15698b8a9b1dSJustin T. Gibbs 	 * If we got an error, bail out of the search.
15708b8a9b1dSJustin T. Gibbs 	 */
15718b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
15728b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
15738b8a9b1dSJustin T. Gibbs 		return(0);
15748b8a9b1dSJustin T. Gibbs 	}
15758b8a9b1dSJustin T. Gibbs 
15768b8a9b1dSJustin T. Gibbs 	/*
15778b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this bus out.
15788b8a9b1dSJustin T. Gibbs 	 */
15798b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
15808b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
15818b8a9b1dSJustin T. Gibbs 
15828b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
15838b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
15848b8a9b1dSJustin T. Gibbs 
15858b8a9b1dSJustin T. Gibbs 		/*
15868b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
15878b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
15888b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
15898b8a9b1dSJustin T. Gibbs 		 */
15908b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
15918b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
15928b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
15938b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
15948b8a9b1dSJustin T. Gibbs 
15958b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = bus;
15968b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
15972b83592fSScott Long 				xsoftc.bus_generation;
15988b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
15998b8a9b1dSJustin T. Gibbs 			return(0);
16008b8a9b1dSJustin T. Gibbs 		}
16018b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
16028b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
16038b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_BUS;
16048b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.path_id = bus->path_id;
16058b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
16068b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.unit_number =
16078b8a9b1dSJustin T. Gibbs 			bus->sim->unit_number;
16088b8a9b1dSJustin T. Gibbs 		strncpy(cdm->matches[j].result.bus_result.dev_name,
16098b8a9b1dSJustin T. Gibbs 			bus->sim->sim_name, DEV_IDLEN);
16108b8a9b1dSJustin T. Gibbs 	}
16118b8a9b1dSJustin T. Gibbs 
16128b8a9b1dSJustin T. Gibbs 	/*
16138b8a9b1dSJustin T. Gibbs 	 * If the user is only interested in busses, there's no
16148b8a9b1dSJustin T. Gibbs 	 * reason to descend to the next level in the tree.
16158b8a9b1dSJustin T. Gibbs 	 */
16168b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
16178b8a9b1dSJustin T. Gibbs 		return(1);
16188b8a9b1dSJustin T. Gibbs 
16198b8a9b1dSJustin T. Gibbs 	/*
16208b8a9b1dSJustin T. Gibbs 	 * If there is a target generation recorded, check it to
16218b8a9b1dSJustin T. Gibbs 	 * make sure the target list hasn't changed.
16228b8a9b1dSJustin T. Gibbs 	 */
1623227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
16248b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
16258b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == bus)
16268b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1627227d67aaSAlexander Motin 	 && (cdm->pos.cookie.target != NULL)) {
1628227d67aaSAlexander Motin 		if ((cdm->pos.generations[CAM_TARGET_GENERATION] !=
1629227d67aaSAlexander Motin 		    bus->generation)) {
1630227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
1631227d67aaSAlexander Motin 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1632227d67aaSAlexander Motin 			return (0);
1633227d67aaSAlexander Motin 		}
1634227d67aaSAlexander Motin 		target = (struct cam_et *)cdm->pos.cookie.target;
1635227d67aaSAlexander Motin 		target->refcount++;
1636227d67aaSAlexander Motin 	} else
1637227d67aaSAlexander Motin 		target = NULL;
1638227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
1639227d67aaSAlexander Motin 
1640227d67aaSAlexander Motin 	return (xpttargettraverse(bus, target, xptedttargetfunc, arg));
16418b8a9b1dSJustin T. Gibbs }
16428b8a9b1dSJustin T. Gibbs 
16438b8a9b1dSJustin T. Gibbs static int
16448b8a9b1dSJustin T. Gibbs xptedttargetfunc(struct cam_et *target, void *arg)
16458b8a9b1dSJustin T. Gibbs {
16468b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
1647227d67aaSAlexander Motin 	struct cam_eb *bus;
1648227d67aaSAlexander Motin 	struct cam_ed *device;
16498b8a9b1dSJustin T. Gibbs 
16508b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
1651227d67aaSAlexander Motin 	bus = target->bus;
16528b8a9b1dSJustin T. Gibbs 
16538b8a9b1dSJustin T. Gibbs 	/*
16548b8a9b1dSJustin T. Gibbs 	 * If there is a device list generation recorded, check it to
16558b8a9b1dSJustin T. Gibbs 	 * make sure the device list hasn't changed.
16568b8a9b1dSJustin T. Gibbs 	 */
1657227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
16588b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1659227d67aaSAlexander Motin 	 && (cdm->pos.cookie.bus == bus)
16608b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
16618b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == target)
16628b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1663227d67aaSAlexander Motin 	 && (cdm->pos.cookie.device != NULL)) {
1664227d67aaSAlexander Motin 		if (cdm->pos.generations[CAM_DEV_GENERATION] !=
1665227d67aaSAlexander Motin 		    target->generation) {
1666227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
16678b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
16688b8a9b1dSJustin T. Gibbs 			return(0);
16698b8a9b1dSJustin T. Gibbs 		}
1670227d67aaSAlexander Motin 		device = (struct cam_ed *)cdm->pos.cookie.device;
1671227d67aaSAlexander Motin 		device->refcount++;
1672227d67aaSAlexander Motin 	} else
1673227d67aaSAlexander Motin 		device = NULL;
1674227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
16758b8a9b1dSJustin T. Gibbs 
1676227d67aaSAlexander Motin 	return (xptdevicetraverse(target, device, xptedtdevicefunc, arg));
16778b8a9b1dSJustin T. Gibbs }
16788b8a9b1dSJustin T. Gibbs 
16798b8a9b1dSJustin T. Gibbs static int
16808b8a9b1dSJustin T. Gibbs xptedtdevicefunc(struct cam_ed *device, void *arg)
16818b8a9b1dSJustin T. Gibbs {
1682227d67aaSAlexander Motin 	struct cam_eb *bus;
1683227d67aaSAlexander Motin 	struct cam_periph *periph;
16848b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
16858b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
16868b8a9b1dSJustin T. Gibbs 
16878b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
1688227d67aaSAlexander Motin 	bus = device->target->bus;
16898b8a9b1dSJustin T. Gibbs 
16908b8a9b1dSJustin T. Gibbs 	/*
16918b8a9b1dSJustin T. Gibbs 	 * If our position is for something deeper in the tree, that means
16928b8a9b1dSJustin T. Gibbs 	 * that we've already seen this node.  So, we keep going down.
16938b8a9b1dSJustin T. Gibbs 	 */
16948b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
16958b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device == device)
16968b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
16978b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.periph != NULL))
16988b8a9b1dSJustin T. Gibbs 		retval = DM_RET_DESCEND;
16998b8a9b1dSJustin T. Gibbs 	else
17008b8a9b1dSJustin T. Gibbs 		retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
17018b8a9b1dSJustin T. Gibbs 					device);
17028b8a9b1dSJustin T. Gibbs 
17038b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
17048b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
17058b8a9b1dSJustin T. Gibbs 		return(0);
17068b8a9b1dSJustin T. Gibbs 	}
17078b8a9b1dSJustin T. Gibbs 
17088b8a9b1dSJustin T. Gibbs 	/*
17098b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this device out.
17108b8a9b1dSJustin T. Gibbs 	 */
17118b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
17128b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
17138b8a9b1dSJustin T. Gibbs 
17148b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
17158b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
17168b8a9b1dSJustin T. Gibbs 
17178b8a9b1dSJustin T. Gibbs 		/*
17188b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
17198b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
17208b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
17218b8a9b1dSJustin T. Gibbs 		 */
17228b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
17238b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
17248b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
17258b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
17268b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
17278b8a9b1dSJustin T. Gibbs 
17288b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = device->target->bus;
17298b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
17302b83592fSScott Long 				xsoftc.bus_generation;
17318b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.target = device->target;
17328b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_TARGET_GENERATION] =
17338b8a9b1dSJustin T. Gibbs 				device->target->bus->generation;
17348b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.device = device;
17358b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_DEV_GENERATION] =
17368b8a9b1dSJustin T. Gibbs 				device->target->generation;
17378b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
17388b8a9b1dSJustin T. Gibbs 			return(0);
17398b8a9b1dSJustin T. Gibbs 		}
17408b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
17418b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
17428b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_DEVICE;
17438b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.path_id =
17448b8a9b1dSJustin T. Gibbs 			device->target->bus->path_id;
17458b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.target_id =
17468b8a9b1dSJustin T. Gibbs 			device->target->target_id;
17478b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.target_lun =
17488b8a9b1dSJustin T. Gibbs 			device->lun_id;
174952c9ce25SScott Long 		cdm->matches[j].result.device_result.protocol =
175052c9ce25SScott Long 			device->protocol;
17518b8a9b1dSJustin T. Gibbs 		bcopy(&device->inq_data,
17528b8a9b1dSJustin T. Gibbs 		      &cdm->matches[j].result.device_result.inq_data,
17538b8a9b1dSJustin T. Gibbs 		      sizeof(struct scsi_inquiry_data));
175452c9ce25SScott Long 		bcopy(&device->ident_data,
175552c9ce25SScott Long 		      &cdm->matches[j].result.device_result.ident_data,
175652c9ce25SScott Long 		      sizeof(struct ata_params));
17579deea857SKenneth D. Merry 
17589deea857SKenneth D. Merry 		/* Let the user know whether this device is unconfigured */
17599deea857SKenneth D. Merry 		if (device->flags & CAM_DEV_UNCONFIGURED)
17609deea857SKenneth D. Merry 			cdm->matches[j].result.device_result.flags =
17619deea857SKenneth D. Merry 				DEV_RESULT_UNCONFIGURED;
17629deea857SKenneth D. Merry 		else
17639deea857SKenneth D. Merry 			cdm->matches[j].result.device_result.flags =
17649deea857SKenneth D. Merry 				DEV_RESULT_NOFLAG;
17658b8a9b1dSJustin T. Gibbs 	}
17668b8a9b1dSJustin T. Gibbs 
17678b8a9b1dSJustin T. Gibbs 	/*
17688b8a9b1dSJustin T. Gibbs 	 * If the user isn't interested in peripherals, don't descend
17698b8a9b1dSJustin T. Gibbs 	 * the tree any further.
17708b8a9b1dSJustin T. Gibbs 	 */
17718b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
17728b8a9b1dSJustin T. Gibbs 		return(1);
17738b8a9b1dSJustin T. Gibbs 
17748b8a9b1dSJustin T. Gibbs 	/*
17758b8a9b1dSJustin T. Gibbs 	 * If there is a peripheral list generation recorded, make sure
17768b8a9b1dSJustin T. Gibbs 	 * it hasn't changed.
17778b8a9b1dSJustin T. Gibbs 	 */
1778227d67aaSAlexander Motin 	xpt_lock_buses();
1779227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
17808b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1781227d67aaSAlexander Motin 	 && (cdm->pos.cookie.bus == bus)
17828b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
17838b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == device->target)
17848b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
17858b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device == device)
17868b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1787227d67aaSAlexander Motin 	 && (cdm->pos.cookie.periph != NULL)) {
1788227d67aaSAlexander Motin 		if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1789227d67aaSAlexander Motin 		    device->generation) {
1790227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
1791227d67aaSAlexander Motin 			xpt_unlock_buses();
1792227d67aaSAlexander Motin 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1793227d67aaSAlexander Motin 			return(0);
1794227d67aaSAlexander Motin 		}
1795227d67aaSAlexander Motin 		periph = (struct cam_periph *)cdm->pos.cookie.periph;
1796227d67aaSAlexander Motin 		periph->refcount++;
1797227d67aaSAlexander Motin 	} else
1798227d67aaSAlexander Motin 		periph = NULL;
1799227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
1800227d67aaSAlexander Motin 	xpt_unlock_buses();
1801227d67aaSAlexander Motin 
1802227d67aaSAlexander Motin 	return (xptperiphtraverse(device, periph, xptedtperiphfunc, arg));
18038b8a9b1dSJustin T. Gibbs }
18048b8a9b1dSJustin T. Gibbs 
18058b8a9b1dSJustin T. Gibbs static int
18068b8a9b1dSJustin T. Gibbs xptedtperiphfunc(struct cam_periph *periph, void *arg)
18078b8a9b1dSJustin T. Gibbs {
18088b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
18098b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
18108b8a9b1dSJustin T. Gibbs 
18118b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
18128b8a9b1dSJustin T. Gibbs 
18138b8a9b1dSJustin T. Gibbs 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
18148b8a9b1dSJustin T. Gibbs 
18158b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
18168b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
18178b8a9b1dSJustin T. Gibbs 		return(0);
18188b8a9b1dSJustin T. Gibbs 	}
18198b8a9b1dSJustin T. Gibbs 
18208b8a9b1dSJustin T. Gibbs 	/*
18218b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this peripheral out.
18228b8a9b1dSJustin T. Gibbs 	 */
18238b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
18248b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
18258b8a9b1dSJustin T. Gibbs 
18268b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
18278b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
18288b8a9b1dSJustin T. Gibbs 
18298b8a9b1dSJustin T. Gibbs 		/*
18308b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
18318b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
18328b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
18338b8a9b1dSJustin T. Gibbs 		 */
18348b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
18358b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
18368b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
18378b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
18388b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
18398b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PERIPH;
18408b8a9b1dSJustin T. Gibbs 
18418b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = periph->path->bus;
18428b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
18432b83592fSScott Long 				xsoftc.bus_generation;
18448b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.target = periph->path->target;
18458b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_TARGET_GENERATION] =
18468b8a9b1dSJustin T. Gibbs 				periph->path->bus->generation;
18478b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.device = periph->path->device;
18488b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_DEV_GENERATION] =
18498b8a9b1dSJustin T. Gibbs 				periph->path->target->generation;
18508b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.periph = periph;
18518b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
18528b8a9b1dSJustin T. Gibbs 				periph->path->device->generation;
18538b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
18548b8a9b1dSJustin T. Gibbs 			return(0);
18558b8a9b1dSJustin T. Gibbs 		}
18568b8a9b1dSJustin T. Gibbs 
18578b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
18588b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
18598b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_PERIPH;
18608b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.path_id =
18618b8a9b1dSJustin T. Gibbs 			periph->path->bus->path_id;
18628b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.target_id =
18638b8a9b1dSJustin T. Gibbs 			periph->path->target->target_id;
18648b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.target_lun =
18658b8a9b1dSJustin T. Gibbs 			periph->path->device->lun_id;
18668b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.unit_number =
18678b8a9b1dSJustin T. Gibbs 			periph->unit_number;
18688b8a9b1dSJustin T. Gibbs 		strncpy(cdm->matches[j].result.periph_result.periph_name,
18698b8a9b1dSJustin T. Gibbs 			periph->periph_name, DEV_IDLEN);
18708b8a9b1dSJustin T. Gibbs 	}
18718b8a9b1dSJustin T. Gibbs 
18728b8a9b1dSJustin T. Gibbs 	return(1);
18738b8a9b1dSJustin T. Gibbs }
18748b8a9b1dSJustin T. Gibbs 
18758b8a9b1dSJustin T. Gibbs static int
18768b8a9b1dSJustin T. Gibbs xptedtmatch(struct ccb_dev_match *cdm)
18778b8a9b1dSJustin T. Gibbs {
1878227d67aaSAlexander Motin 	struct cam_eb *bus;
18798b8a9b1dSJustin T. Gibbs 	int ret;
18808b8a9b1dSJustin T. Gibbs 
18818b8a9b1dSJustin T. Gibbs 	cdm->num_matches = 0;
18828b8a9b1dSJustin T. Gibbs 
18838b8a9b1dSJustin T. Gibbs 	/*
18848b8a9b1dSJustin T. Gibbs 	 * Check the bus list generation.  If it has changed, the user
18858b8a9b1dSJustin T. Gibbs 	 * needs to reset everything and start over.
18868b8a9b1dSJustin T. Gibbs 	 */
1887227d67aaSAlexander Motin 	xpt_lock_buses();
18888b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1889227d67aaSAlexander Motin 	 && (cdm->pos.cookie.bus != NULL)) {
1890227d67aaSAlexander Motin 		if (cdm->pos.generations[CAM_BUS_GENERATION] !=
1891227d67aaSAlexander Motin 		    xsoftc.bus_generation) {
1892227d67aaSAlexander Motin 			xpt_unlock_buses();
18938b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
18948b8a9b1dSJustin T. Gibbs 			return(0);
18958b8a9b1dSJustin T. Gibbs 		}
1896227d67aaSAlexander Motin 		bus = (struct cam_eb *)cdm->pos.cookie.bus;
1897227d67aaSAlexander Motin 		bus->refcount++;
1898227d67aaSAlexander Motin 	} else
1899227d67aaSAlexander Motin 		bus = NULL;
1900227d67aaSAlexander Motin 	xpt_unlock_buses();
19018b8a9b1dSJustin T. Gibbs 
1902227d67aaSAlexander Motin 	ret = xptbustraverse(bus, xptedtbusfunc, cdm);
19038b8a9b1dSJustin T. Gibbs 
19048b8a9b1dSJustin T. Gibbs 	/*
19058b8a9b1dSJustin T. Gibbs 	 * If we get back 0, that means that we had to stop before fully
19068b8a9b1dSJustin T. Gibbs 	 * traversing the EDT.  It also means that one of the subroutines
19078b8a9b1dSJustin T. Gibbs 	 * has set the status field to the proper value.  If we get back 1,
19088b8a9b1dSJustin T. Gibbs 	 * we've fully traversed the EDT and copied out any matching entries.
19098b8a9b1dSJustin T. Gibbs 	 */
19108b8a9b1dSJustin T. Gibbs 	if (ret == 1)
19118b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LAST;
19128b8a9b1dSJustin T. Gibbs 
19138b8a9b1dSJustin T. Gibbs 	return(ret);
19148b8a9b1dSJustin T. Gibbs }
19158b8a9b1dSJustin T. Gibbs 
19168b8a9b1dSJustin T. Gibbs static int
19178b8a9b1dSJustin T. Gibbs xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
19188b8a9b1dSJustin T. Gibbs {
1919227d67aaSAlexander Motin 	struct cam_periph *periph;
19208b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
19218b8a9b1dSJustin T. Gibbs 
19228b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
19238b8a9b1dSJustin T. Gibbs 
1924227d67aaSAlexander Motin 	xpt_lock_buses();
19258b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
19268b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv == pdrv)
19278b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1928227d67aaSAlexander Motin 	 && (cdm->pos.cookie.periph != NULL)) {
1929227d67aaSAlexander Motin 		if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1930227d67aaSAlexander Motin 		    (*pdrv)->generation) {
1931227d67aaSAlexander Motin 			xpt_unlock_buses();
19328b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
19338b8a9b1dSJustin T. Gibbs 			return(0);
19348b8a9b1dSJustin T. Gibbs 		}
1935227d67aaSAlexander Motin 		periph = (struct cam_periph *)cdm->pos.cookie.periph;
1936227d67aaSAlexander Motin 		periph->refcount++;
1937227d67aaSAlexander Motin 	} else
1938227d67aaSAlexander Motin 		periph = NULL;
1939227d67aaSAlexander Motin 	xpt_unlock_buses();
19408b8a9b1dSJustin T. Gibbs 
1941227d67aaSAlexander Motin 	return (xptpdperiphtraverse(pdrv, periph, xptplistperiphfunc, arg));
19428b8a9b1dSJustin T. Gibbs }
19438b8a9b1dSJustin T. Gibbs 
19448b8a9b1dSJustin T. Gibbs static int
19458b8a9b1dSJustin T. Gibbs xptplistperiphfunc(struct cam_periph *periph, void *arg)
19468b8a9b1dSJustin T. Gibbs {
19478b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
19488b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
19498b8a9b1dSJustin T. Gibbs 
19508b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
19518b8a9b1dSJustin T. Gibbs 
19528b8a9b1dSJustin T. Gibbs 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
19538b8a9b1dSJustin T. Gibbs 
19548b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
19558b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
19568b8a9b1dSJustin T. Gibbs 		return(0);
19578b8a9b1dSJustin T. Gibbs 	}
19588b8a9b1dSJustin T. Gibbs 
19598b8a9b1dSJustin T. Gibbs 	/*
19608b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this peripheral out.
19618b8a9b1dSJustin T. Gibbs 	 */
19628b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
19638b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
19648b8a9b1dSJustin T. Gibbs 
19658b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
19668b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
19678b8a9b1dSJustin T. Gibbs 
19688b8a9b1dSJustin T. Gibbs 		/*
19698b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
19708b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
19718b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
19728b8a9b1dSJustin T. Gibbs 		 */
19738b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
19748b8a9b1dSJustin T. Gibbs 			struct periph_driver **pdrv;
19758b8a9b1dSJustin T. Gibbs 
19768b8a9b1dSJustin T. Gibbs 			pdrv = NULL;
19778b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
19788b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
19798b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
19808b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PERIPH;
19818b8a9b1dSJustin T. Gibbs 
19828b8a9b1dSJustin T. Gibbs 			/*
19838b8a9b1dSJustin T. Gibbs 			 * This may look a bit non-sensical, but it is
19848b8a9b1dSJustin T. Gibbs 			 * actually quite logical.  There are very few
19858b8a9b1dSJustin T. Gibbs 			 * peripheral drivers, and bloating every peripheral
19868b8a9b1dSJustin T. Gibbs 			 * structure with a pointer back to its parent
19878b8a9b1dSJustin T. Gibbs 			 * peripheral driver linker set entry would cost
19888b8a9b1dSJustin T. Gibbs 			 * more in the long run than doing this quick lookup.
19898b8a9b1dSJustin T. Gibbs 			 */
19900b7c27b9SPeter Wemm 			for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
19918b8a9b1dSJustin T. Gibbs 				if (strcmp((*pdrv)->driver_name,
19928b8a9b1dSJustin T. Gibbs 				    periph->periph_name) == 0)
19938b8a9b1dSJustin T. Gibbs 					break;
19948b8a9b1dSJustin T. Gibbs 			}
19958b8a9b1dSJustin T. Gibbs 
199601910babSScott Long 			if (*pdrv == NULL) {
19978b8a9b1dSJustin T. Gibbs 				cdm->status = CAM_DEV_MATCH_ERROR;
19988b8a9b1dSJustin T. Gibbs 				return(0);
19998b8a9b1dSJustin T. Gibbs 			}
20008b8a9b1dSJustin T. Gibbs 
20018b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.pdrv = pdrv;
20028b8a9b1dSJustin T. Gibbs 			/*
20038b8a9b1dSJustin T. Gibbs 			 * The periph generation slot does double duty, as
20048b8a9b1dSJustin T. Gibbs 			 * does the periph pointer slot.  They are used for
20058b8a9b1dSJustin T. Gibbs 			 * both edt and pdrv lookups and positioning.
20068b8a9b1dSJustin T. Gibbs 			 */
20078b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.periph = periph;
20088b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
20098b8a9b1dSJustin T. Gibbs 				(*pdrv)->generation;
20108b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
20118b8a9b1dSJustin T. Gibbs 			return(0);
20128b8a9b1dSJustin T. Gibbs 		}
20138b8a9b1dSJustin T. Gibbs 
20148b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
20158b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
20168b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_PERIPH;
20178b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.path_id =
20188b8a9b1dSJustin T. Gibbs 			periph->path->bus->path_id;
20198b8a9b1dSJustin T. Gibbs 
20208b8a9b1dSJustin T. Gibbs 		/*
20218b8a9b1dSJustin T. Gibbs 		 * The transport layer peripheral doesn't have a target or
20228b8a9b1dSJustin T. Gibbs 		 * lun.
20238b8a9b1dSJustin T. Gibbs 		 */
20248b8a9b1dSJustin T. Gibbs 		if (periph->path->target)
20258b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_id =
20268b8a9b1dSJustin T. Gibbs 				periph->path->target->target_id;
20278b8a9b1dSJustin T. Gibbs 		else
2028431d3a5bSAlexander Motin 			cdm->matches[j].result.periph_result.target_id =
2029431d3a5bSAlexander Motin 				CAM_TARGET_WILDCARD;
20308b8a9b1dSJustin T. Gibbs 
20318b8a9b1dSJustin T. Gibbs 		if (periph->path->device)
20328b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_lun =
20338b8a9b1dSJustin T. Gibbs 				periph->path->device->lun_id;
20348b8a9b1dSJustin T. Gibbs 		else
2035431d3a5bSAlexander Motin 			cdm->matches[j].result.periph_result.target_lun =
2036431d3a5bSAlexander Motin 				CAM_LUN_WILDCARD;
20378b8a9b1dSJustin T. Gibbs 
20388b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.unit_number =
20398b8a9b1dSJustin T. Gibbs 			periph->unit_number;
20408b8a9b1dSJustin T. Gibbs 		strncpy(cdm->matches[j].result.periph_result.periph_name,
20418b8a9b1dSJustin T. Gibbs 			periph->periph_name, DEV_IDLEN);
20428b8a9b1dSJustin T. Gibbs 	}
20438b8a9b1dSJustin T. Gibbs 
20448b8a9b1dSJustin T. Gibbs 	return(1);
20458b8a9b1dSJustin T. Gibbs }
20468b8a9b1dSJustin T. Gibbs 
20478b8a9b1dSJustin T. Gibbs static int
20488b8a9b1dSJustin T. Gibbs xptperiphlistmatch(struct ccb_dev_match *cdm)
20498b8a9b1dSJustin T. Gibbs {
20508b8a9b1dSJustin T. Gibbs 	int ret;
20518b8a9b1dSJustin T. Gibbs 
20528b8a9b1dSJustin T. Gibbs 	cdm->num_matches = 0;
20538b8a9b1dSJustin T. Gibbs 
20548b8a9b1dSJustin T. Gibbs 	/*
20558b8a9b1dSJustin T. Gibbs 	 * At this point in the edt traversal function, we check the bus
20568b8a9b1dSJustin T. Gibbs 	 * list generation to make sure that no busses have been added or
20578b8a9b1dSJustin T. Gibbs 	 * removed since the user last sent a XPT_DEV_MATCH ccb through.
20588b8a9b1dSJustin T. Gibbs 	 * For the peripheral driver list traversal function, however, we
20598b8a9b1dSJustin T. Gibbs 	 * don't have to worry about new peripheral driver types coming or
20608b8a9b1dSJustin T. Gibbs 	 * going; they're in a linker set, and therefore can't change
20618b8a9b1dSJustin T. Gibbs 	 * without a recompile.
20628b8a9b1dSJustin T. Gibbs 	 */
20638b8a9b1dSJustin T. Gibbs 
20648b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
20658b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv != NULL))
20668b8a9b1dSJustin T. Gibbs 		ret = xptpdrvtraverse(
20678b8a9b1dSJustin T. Gibbs 				(struct periph_driver **)cdm->pos.cookie.pdrv,
20688b8a9b1dSJustin T. Gibbs 				xptplistpdrvfunc, cdm);
20698b8a9b1dSJustin T. Gibbs 	else
20708b8a9b1dSJustin T. Gibbs 		ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
20718b8a9b1dSJustin T. Gibbs 
20728b8a9b1dSJustin T. Gibbs 	/*
20738b8a9b1dSJustin T. Gibbs 	 * If we get back 0, that means that we had to stop before fully
20748b8a9b1dSJustin T. Gibbs 	 * traversing the peripheral driver tree.  It also means that one of
20758b8a9b1dSJustin T. Gibbs 	 * the subroutines has set the status field to the proper value.  If
20768b8a9b1dSJustin T. Gibbs 	 * we get back 1, we've fully traversed the EDT and copied out any
20778b8a9b1dSJustin T. Gibbs 	 * matching entries.
20788b8a9b1dSJustin T. Gibbs 	 */
20798b8a9b1dSJustin T. Gibbs 	if (ret == 1)
20808b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LAST;
20818b8a9b1dSJustin T. Gibbs 
20828b8a9b1dSJustin T. Gibbs 	return(ret);
20838b8a9b1dSJustin T. Gibbs }
20848b8a9b1dSJustin T. Gibbs 
20858b8a9b1dSJustin T. Gibbs static int
20868b8a9b1dSJustin T. Gibbs xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
20878b8a9b1dSJustin T. Gibbs {
20888b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus, *next_bus;
20898b8a9b1dSJustin T. Gibbs 	int retval;
20908b8a9b1dSJustin T. Gibbs 
20918b8a9b1dSJustin T. Gibbs 	retval = 1;
2092227d67aaSAlexander Motin 	if (start_bus)
2093227d67aaSAlexander Motin 		bus = start_bus;
2094227d67aaSAlexander Motin 	else {
20959a7c2696SAlexander Motin 		xpt_lock_buses();
2096227d67aaSAlexander Motin 		bus = TAILQ_FIRST(&xsoftc.xpt_busses);
2097227d67aaSAlexander Motin 		if (bus == NULL) {
20989a7c2696SAlexander Motin 			xpt_unlock_buses();
2099227d67aaSAlexander Motin 			return (retval);
2100227d67aaSAlexander Motin 		}
2101227d67aaSAlexander Motin 		bus->refcount++;
2102227d67aaSAlexander Motin 		xpt_unlock_buses();
2103227d67aaSAlexander Motin 	}
2104227d67aaSAlexander Motin 	for (; bus != NULL; bus = next_bus) {
21058b8a9b1dSJustin T. Gibbs 		retval = tr_func(bus, arg);
2106227d67aaSAlexander Motin 		if (retval == 0) {
2107227d67aaSAlexander Motin 			xpt_release_bus(bus);
2108227d67aaSAlexander Motin 			break;
2109227d67aaSAlexander Motin 		}
21109a7c2696SAlexander Motin 		xpt_lock_buses();
21118900f4b8SKenneth D. Merry 		next_bus = TAILQ_NEXT(bus, links);
2112227d67aaSAlexander Motin 		if (next_bus)
2113227d67aaSAlexander Motin 			next_bus->refcount++;
21149a7c2696SAlexander Motin 		xpt_unlock_buses();
21158900f4b8SKenneth D. Merry 		xpt_release_bus(bus);
21168b8a9b1dSJustin T. Gibbs 	}
21178b8a9b1dSJustin T. Gibbs 	return(retval);
21188b8a9b1dSJustin T. Gibbs }
21198b8a9b1dSJustin T. Gibbs 
21208b8a9b1dSJustin T. Gibbs static int
21218b8a9b1dSJustin T. Gibbs xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
21228b8a9b1dSJustin T. Gibbs 		  xpt_targetfunc_t *tr_func, void *arg)
21238b8a9b1dSJustin T. Gibbs {
21248b8a9b1dSJustin T. Gibbs 	struct cam_et *target, *next_target;
21258b8a9b1dSJustin T. Gibbs 	int retval;
21268b8a9b1dSJustin T. Gibbs 
21278b8a9b1dSJustin T. Gibbs 	retval = 1;
2128227d67aaSAlexander Motin 	if (start_target)
2129227d67aaSAlexander Motin 		target = start_target;
2130227d67aaSAlexander Motin 	else {
2131227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2132227d67aaSAlexander Motin 		target = TAILQ_FIRST(&bus->et_entries);
2133227d67aaSAlexander Motin 		if (target == NULL) {
2134227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
21358b8a9b1dSJustin T. Gibbs 			return (retval);
21368b8a9b1dSJustin T. Gibbs 		}
2137227d67aaSAlexander Motin 		target->refcount++;
2138227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2139227d67aaSAlexander Motin 	}
2140227d67aaSAlexander Motin 	for (; target != NULL; target = next_target) {
2141227d67aaSAlexander Motin 		retval = tr_func(target, arg);
2142227d67aaSAlexander Motin 		if (retval == 0) {
2143227d67aaSAlexander Motin 			xpt_release_target(target);
2144227d67aaSAlexander Motin 			break;
2145227d67aaSAlexander Motin 		}
2146227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2147227d67aaSAlexander Motin 		next_target = TAILQ_NEXT(target, links);
2148227d67aaSAlexander Motin 		if (next_target)
2149227d67aaSAlexander Motin 			next_target->refcount++;
2150227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2151227d67aaSAlexander Motin 		xpt_release_target(target);
2152227d67aaSAlexander Motin 	}
21538b8a9b1dSJustin T. Gibbs 	return(retval);
21548b8a9b1dSJustin T. Gibbs }
21558b8a9b1dSJustin T. Gibbs 
21568b8a9b1dSJustin T. Gibbs static int
21578b8a9b1dSJustin T. Gibbs xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
21588b8a9b1dSJustin T. Gibbs 		  xpt_devicefunc_t *tr_func, void *arg)
21598b8a9b1dSJustin T. Gibbs {
2160227d67aaSAlexander Motin 	struct cam_eb *bus;
21618b8a9b1dSJustin T. Gibbs 	struct cam_ed *device, *next_device;
21628b8a9b1dSJustin T. Gibbs 	int retval;
21638b8a9b1dSJustin T. Gibbs 
21648b8a9b1dSJustin T. Gibbs 	retval = 1;
2165227d67aaSAlexander Motin 	bus = target->bus;
2166227d67aaSAlexander Motin 	if (start_device)
2167227d67aaSAlexander Motin 		device = start_device;
2168227d67aaSAlexander Motin 	else {
2169227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2170227d67aaSAlexander Motin 		device = TAILQ_FIRST(&target->ed_entries);
2171227d67aaSAlexander Motin 		if (device == NULL) {
2172227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
21738b8a9b1dSJustin T. Gibbs 			return (retval);
21748b8a9b1dSJustin T. Gibbs 		}
2175227d67aaSAlexander Motin 		device->refcount++;
2176227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2177227d67aaSAlexander Motin 	}
2178227d67aaSAlexander Motin 	for (; device != NULL; device = next_device) {
2179227d67aaSAlexander Motin 		mtx_lock(&device->device_mtx);
2180227d67aaSAlexander Motin 		retval = tr_func(device, arg);
2181227d67aaSAlexander Motin 		mtx_unlock(&device->device_mtx);
2182227d67aaSAlexander Motin 		if (retval == 0) {
2183227d67aaSAlexander Motin 			xpt_release_device(device);
2184227d67aaSAlexander Motin 			break;
2185227d67aaSAlexander Motin 		}
2186227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2187227d67aaSAlexander Motin 		next_device = TAILQ_NEXT(device, links);
2188227d67aaSAlexander Motin 		if (next_device)
2189227d67aaSAlexander Motin 			next_device->refcount++;
2190227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2191227d67aaSAlexander Motin 		xpt_release_device(device);
2192227d67aaSAlexander Motin 	}
21938b8a9b1dSJustin T. Gibbs 	return(retval);
21948b8a9b1dSJustin T. Gibbs }
21958b8a9b1dSJustin T. Gibbs 
21968b8a9b1dSJustin T. Gibbs static int
21978b8a9b1dSJustin T. Gibbs xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
21988b8a9b1dSJustin T. Gibbs 		  xpt_periphfunc_t *tr_func, void *arg)
21998b8a9b1dSJustin T. Gibbs {
2200227d67aaSAlexander Motin 	struct cam_eb *bus;
22018b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph, *next_periph;
22028b8a9b1dSJustin T. Gibbs 	int retval;
22038b8a9b1dSJustin T. Gibbs 
22048b8a9b1dSJustin T. Gibbs 	retval = 1;
22058b8a9b1dSJustin T. Gibbs 
2206227d67aaSAlexander Motin 	bus = device->target->bus;
2207227d67aaSAlexander Motin 	if (start_periph)
2208227d67aaSAlexander Motin 		periph = start_periph;
2209227d67aaSAlexander Motin 	else {
22108900f4b8SKenneth D. Merry 		xpt_lock_buses();
2211227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2212227d67aaSAlexander Motin 		periph = SLIST_FIRST(&device->periphs);
2213227d67aaSAlexander Motin 		while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2214227d67aaSAlexander Motin 			periph = SLIST_NEXT(periph, periph_links);
2215227d67aaSAlexander Motin 		if (periph == NULL) {
2216227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
22178900f4b8SKenneth D. Merry 			xpt_unlock_buses();
2218227d67aaSAlexander Motin 			return (retval);
2219227d67aaSAlexander Motin 		}
2220227d67aaSAlexander Motin 		periph->refcount++;
2221227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2222227d67aaSAlexander Motin 		xpt_unlock_buses();
2223227d67aaSAlexander Motin 	}
2224227d67aaSAlexander Motin 	for (; periph != NULL; periph = next_periph) {
2225227d67aaSAlexander Motin 		retval = tr_func(periph, arg);
2226227d67aaSAlexander Motin 		if (retval == 0) {
22279813c936SAlexander Motin 			cam_periph_release_locked(periph);
2228227d67aaSAlexander Motin 			break;
2229227d67aaSAlexander Motin 		}
2230227d67aaSAlexander Motin 		xpt_lock_buses();
2231227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2232227d67aaSAlexander Motin 		next_periph = SLIST_NEXT(periph, periph_links);
2233227d67aaSAlexander Motin 		while (next_periph != NULL &&
2234227d67aaSAlexander Motin 		    (next_periph->flags & CAM_PERIPH_FREE) != 0)
2235840a5dd4SAlexander Motin 			next_periph = SLIST_NEXT(next_periph, periph_links);
2236227d67aaSAlexander Motin 		if (next_periph)
2237227d67aaSAlexander Motin 			next_periph->refcount++;
2238227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2239227d67aaSAlexander Motin 		xpt_unlock_buses();
2240227d67aaSAlexander Motin 		cam_periph_release_locked(periph);
2241227d67aaSAlexander Motin 	}
22428b8a9b1dSJustin T. Gibbs 	return(retval);
22438b8a9b1dSJustin T. Gibbs }
22448b8a9b1dSJustin T. Gibbs 
22458b8a9b1dSJustin T. Gibbs static int
22468b8a9b1dSJustin T. Gibbs xptpdrvtraverse(struct periph_driver **start_pdrv,
22478b8a9b1dSJustin T. Gibbs 		xpt_pdrvfunc_t *tr_func, void *arg)
22488b8a9b1dSJustin T. Gibbs {
22498b8a9b1dSJustin T. Gibbs 	struct periph_driver **pdrv;
22508b8a9b1dSJustin T. Gibbs 	int retval;
22518b8a9b1dSJustin T. Gibbs 
22528b8a9b1dSJustin T. Gibbs 	retval = 1;
22538b8a9b1dSJustin T. Gibbs 
22548b8a9b1dSJustin T. Gibbs 	/*
22558b8a9b1dSJustin T. Gibbs 	 * We don't traverse the peripheral driver list like we do the
22568b8a9b1dSJustin T. Gibbs 	 * other lists, because it is a linker set, and therefore cannot be
22578b8a9b1dSJustin T. Gibbs 	 * changed during runtime.  If the peripheral driver list is ever
22588b8a9b1dSJustin T. Gibbs 	 * re-done to be something other than a linker set (i.e. it can
22598b8a9b1dSJustin T. Gibbs 	 * change while the system is running), the list traversal should
22608b8a9b1dSJustin T. Gibbs 	 * be modified to work like the other traversal functions.
22618b8a9b1dSJustin T. Gibbs 	 */
22620b7c27b9SPeter Wemm 	for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
22638b8a9b1dSJustin T. Gibbs 	     *pdrv != NULL; pdrv++) {
22648b8a9b1dSJustin T. Gibbs 		retval = tr_func(pdrv, arg);
22658b8a9b1dSJustin T. Gibbs 
22668b8a9b1dSJustin T. Gibbs 		if (retval == 0)
22678b8a9b1dSJustin T. Gibbs 			return(retval);
22688b8a9b1dSJustin T. Gibbs 	}
22698b8a9b1dSJustin T. Gibbs 
22708b8a9b1dSJustin T. Gibbs 	return(retval);
22718b8a9b1dSJustin T. Gibbs }
22728b8a9b1dSJustin T. Gibbs 
22738b8a9b1dSJustin T. Gibbs static int
22748b8a9b1dSJustin T. Gibbs xptpdperiphtraverse(struct periph_driver **pdrv,
22758b8a9b1dSJustin T. Gibbs 		    struct cam_periph *start_periph,
22768b8a9b1dSJustin T. Gibbs 		    xpt_periphfunc_t *tr_func, void *arg)
22778b8a9b1dSJustin T. Gibbs {
22788b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph, *next_periph;
22798b8a9b1dSJustin T. Gibbs 	int retval;
22808b8a9b1dSJustin T. Gibbs 
22818b8a9b1dSJustin T. Gibbs 	retval = 1;
22828b8a9b1dSJustin T. Gibbs 
2283227d67aaSAlexander Motin 	if (start_periph)
2284227d67aaSAlexander Motin 		periph = start_periph;
2285227d67aaSAlexander Motin 	else {
2286f1e2546aSMatt Jacob 		xpt_lock_buses();
2287227d67aaSAlexander Motin 		periph = TAILQ_FIRST(&(*pdrv)->units);
2288227d67aaSAlexander Motin 		while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2289227d67aaSAlexander Motin 			periph = TAILQ_NEXT(periph, unit_links);
2290227d67aaSAlexander Motin 		if (periph == NULL) {
2291227d67aaSAlexander Motin 			xpt_unlock_buses();
2292227d67aaSAlexander Motin 			return (retval);
22938900f4b8SKenneth D. Merry 		}
22948900f4b8SKenneth D. Merry 		periph->refcount++;
2295dcdf6e74SAlexander Motin 		xpt_unlock_buses();
22968b8a9b1dSJustin T. Gibbs 	}
2297227d67aaSAlexander Motin 	for (; periph != NULL; periph = next_periph) {
2298227d67aaSAlexander Motin 		cam_periph_lock(periph);
2299227d67aaSAlexander Motin 		retval = tr_func(periph, arg);
2300227d67aaSAlexander Motin 		cam_periph_unlock(periph);
2301227d67aaSAlexander Motin 		if (retval == 0) {
2302227d67aaSAlexander Motin 			cam_periph_release(periph);
2303227d67aaSAlexander Motin 			break;
2304227d67aaSAlexander Motin 		}
2305227d67aaSAlexander Motin 		xpt_lock_buses();
2306227d67aaSAlexander Motin 		next_periph = TAILQ_NEXT(periph, unit_links);
2307227d67aaSAlexander Motin 		while (next_periph != NULL &&
2308227d67aaSAlexander Motin 		    (next_periph->flags & CAM_PERIPH_FREE) != 0)
2309840a5dd4SAlexander Motin 			next_periph = TAILQ_NEXT(next_periph, unit_links);
2310227d67aaSAlexander Motin 		if (next_periph)
2311227d67aaSAlexander Motin 			next_periph->refcount++;
2312f1e2546aSMatt Jacob 		xpt_unlock_buses();
2313227d67aaSAlexander Motin 		cam_periph_release(periph);
2314227d67aaSAlexander Motin 	}
23158b8a9b1dSJustin T. Gibbs 	return(retval);
23168b8a9b1dSJustin T. Gibbs }
23178b8a9b1dSJustin T. Gibbs 
23188b8a9b1dSJustin T. Gibbs static int
23198b8a9b1dSJustin T. Gibbs xptdefbusfunc(struct cam_eb *bus, void *arg)
23208b8a9b1dSJustin T. Gibbs {
23218b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
23228b8a9b1dSJustin T. Gibbs 
23238b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
23248b8a9b1dSJustin T. Gibbs 
23258b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_BUS) {
23268b8a9b1dSJustin T. Gibbs 		xpt_busfunc_t *tr_func;
23278b8a9b1dSJustin T. Gibbs 
23288b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_busfunc_t *)tr_config->tr_func;
23298b8a9b1dSJustin T. Gibbs 
23308b8a9b1dSJustin T. Gibbs 		return(tr_func(bus, tr_config->tr_arg));
23318b8a9b1dSJustin T. Gibbs 	} else
23328b8a9b1dSJustin T. Gibbs 		return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
23338b8a9b1dSJustin T. Gibbs }
23348b8a9b1dSJustin T. Gibbs 
23358b8a9b1dSJustin T. Gibbs static int
23368b8a9b1dSJustin T. Gibbs xptdeftargetfunc(struct cam_et *target, void *arg)
23378b8a9b1dSJustin T. Gibbs {
23388b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
23398b8a9b1dSJustin T. Gibbs 
23408b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
23418b8a9b1dSJustin T. Gibbs 
23428b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_TARGET) {
23438b8a9b1dSJustin T. Gibbs 		xpt_targetfunc_t *tr_func;
23448b8a9b1dSJustin T. Gibbs 
23458b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
23468b8a9b1dSJustin T. Gibbs 
23478b8a9b1dSJustin T. Gibbs 		return(tr_func(target, tr_config->tr_arg));
23488b8a9b1dSJustin T. Gibbs 	} else
23498b8a9b1dSJustin T. Gibbs 		return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
23508b8a9b1dSJustin T. Gibbs }
23518b8a9b1dSJustin T. Gibbs 
23528b8a9b1dSJustin T. Gibbs static int
23538b8a9b1dSJustin T. Gibbs xptdefdevicefunc(struct cam_ed *device, void *arg)
23548b8a9b1dSJustin T. Gibbs {
23558b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
23568b8a9b1dSJustin T. Gibbs 
23578b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
23588b8a9b1dSJustin T. Gibbs 
23598b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_DEVICE) {
23608b8a9b1dSJustin T. Gibbs 		xpt_devicefunc_t *tr_func;
23618b8a9b1dSJustin T. Gibbs 
23628b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
23638b8a9b1dSJustin T. Gibbs 
23648b8a9b1dSJustin T. Gibbs 		return(tr_func(device, tr_config->tr_arg));
23658b8a9b1dSJustin T. Gibbs 	} else
23668b8a9b1dSJustin T. Gibbs 		return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
23678b8a9b1dSJustin T. Gibbs }
23688b8a9b1dSJustin T. Gibbs 
23698b8a9b1dSJustin T. Gibbs static int
23708b8a9b1dSJustin T. Gibbs xptdefperiphfunc(struct cam_periph *periph, void *arg)
23718b8a9b1dSJustin T. Gibbs {
23728b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
23738b8a9b1dSJustin T. Gibbs 	xpt_periphfunc_t *tr_func;
23748b8a9b1dSJustin T. Gibbs 
23758b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
23768b8a9b1dSJustin T. Gibbs 
23778b8a9b1dSJustin T. Gibbs 	tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
23788b8a9b1dSJustin T. Gibbs 
23798b8a9b1dSJustin T. Gibbs 	/*
23808b8a9b1dSJustin T. Gibbs 	 * Unlike the other default functions, we don't check for depth
23818b8a9b1dSJustin T. Gibbs 	 * here.  The peripheral driver level is the last level in the EDT,
23828b8a9b1dSJustin T. Gibbs 	 * so if we're here, we should execute the function in question.
23838b8a9b1dSJustin T. Gibbs 	 */
23848b8a9b1dSJustin T. Gibbs 	return(tr_func(periph, tr_config->tr_arg));
23858b8a9b1dSJustin T. Gibbs }
23868b8a9b1dSJustin T. Gibbs 
23878b8a9b1dSJustin T. Gibbs /*
23888b8a9b1dSJustin T. Gibbs  * Execute the given function for every bus in the EDT.
23898b8a9b1dSJustin T. Gibbs  */
23908b8a9b1dSJustin T. Gibbs static int
23918b8a9b1dSJustin T. Gibbs xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
23928b8a9b1dSJustin T. Gibbs {
23938b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
23948b8a9b1dSJustin T. Gibbs 
23958b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_BUS;
23968b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
23978b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
23988b8a9b1dSJustin T. Gibbs 
23998b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
24008b8a9b1dSJustin T. Gibbs }
24018b8a9b1dSJustin T. Gibbs 
24028b8a9b1dSJustin T. Gibbs /*
24038b8a9b1dSJustin T. Gibbs  * Execute the given function for every device in the EDT.
24048b8a9b1dSJustin T. Gibbs  */
24058b8a9b1dSJustin T. Gibbs static int
24068b8a9b1dSJustin T. Gibbs xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
24078b8a9b1dSJustin T. Gibbs {
24088b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
24098b8a9b1dSJustin T. Gibbs 
24108b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_DEVICE;
24118b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
24128b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
24138b8a9b1dSJustin T. Gibbs 
24148b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
24158b8a9b1dSJustin T. Gibbs }
24168b8a9b1dSJustin T. Gibbs 
24178b8a9b1dSJustin T. Gibbs static int
24188b8a9b1dSJustin T. Gibbs xptsetasyncfunc(struct cam_ed *device, void *arg)
24198b8a9b1dSJustin T. Gibbs {
24208b8a9b1dSJustin T. Gibbs 	struct cam_path path;
24218b8a9b1dSJustin T. Gibbs 	struct ccb_getdev cgd;
24227685edecSAlexander Motin 	struct ccb_setasync *csa = (struct ccb_setasync *)arg;
24238b8a9b1dSJustin T. Gibbs 
2424c8bead2aSJustin T. Gibbs 	/*
2425c8bead2aSJustin T. Gibbs 	 * Don't report unconfigured devices (Wildcard devs,
2426c8bead2aSJustin T. Gibbs 	 * devices only for target mode, device instances
2427c8bead2aSJustin T. Gibbs 	 * that have been invalidated but are waiting for
2428c8bead2aSJustin T. Gibbs 	 * their last reference count to be released).
2429c8bead2aSJustin T. Gibbs 	 */
2430c8bead2aSJustin T. Gibbs 	if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2431c8bead2aSJustin T. Gibbs 		return (1);
2432c8bead2aSJustin T. Gibbs 
24338b8a9b1dSJustin T. Gibbs 	xpt_compile_path(&path,
24348b8a9b1dSJustin T. Gibbs 			 NULL,
24358b8a9b1dSJustin T. Gibbs 			 device->target->bus->path_id,
24368b8a9b1dSJustin T. Gibbs 			 device->target->target_id,
24378b8a9b1dSJustin T. Gibbs 			 device->lun_id);
2438bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
24398b8a9b1dSJustin T. Gibbs 	cgd.ccb_h.func_code = XPT_GDEV_TYPE;
24408b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&cgd);
24417685edecSAlexander Motin 	csa->callback(csa->callback_arg,
24428b8a9b1dSJustin T. Gibbs 			    AC_FOUND_DEVICE,
24438b8a9b1dSJustin T. Gibbs 			    &path, &cgd);
24448b8a9b1dSJustin T. Gibbs 	xpt_release_path(&path);
24458b8a9b1dSJustin T. Gibbs 
24468b8a9b1dSJustin T. Gibbs 	return(1);
24478b8a9b1dSJustin T. Gibbs }
2448c8bead2aSJustin T. Gibbs 
24498b8a9b1dSJustin T. Gibbs static int
24508b8a9b1dSJustin T. Gibbs xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
24518b8a9b1dSJustin T. Gibbs {
24528b8a9b1dSJustin T. Gibbs 	struct cam_path path;
24538b8a9b1dSJustin T. Gibbs 	struct ccb_pathinq cpi;
24547685edecSAlexander Motin 	struct ccb_setasync *csa = (struct ccb_setasync *)arg;
24558b8a9b1dSJustin T. Gibbs 
24568b8a9b1dSJustin T. Gibbs 	xpt_compile_path(&path, /*periph*/NULL,
24576dfc67e3SAlexander Motin 			 bus->path_id,
24588b8a9b1dSJustin T. Gibbs 			 CAM_TARGET_WILDCARD,
24598b8a9b1dSJustin T. Gibbs 			 CAM_LUN_WILDCARD);
2460227d67aaSAlexander Motin 	xpt_path_lock(&path);
2461bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
24628b8a9b1dSJustin T. Gibbs 	cpi.ccb_h.func_code = XPT_PATH_INQ;
24638b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&cpi);
24647685edecSAlexander Motin 	csa->callback(csa->callback_arg,
24658b8a9b1dSJustin T. Gibbs 			    AC_PATH_REGISTERED,
24668b8a9b1dSJustin T. Gibbs 			    &path, &cpi);
2467227d67aaSAlexander Motin 	xpt_path_unlock(&path);
24688b8a9b1dSJustin T. Gibbs 	xpt_release_path(&path);
24698b8a9b1dSJustin T. Gibbs 
24708b8a9b1dSJustin T. Gibbs 	return(1);
24718b8a9b1dSJustin T. Gibbs }
24728b8a9b1dSJustin T. Gibbs 
24738b8a9b1dSJustin T. Gibbs void
24748b8a9b1dSJustin T. Gibbs xpt_action(union ccb *start_ccb)
24758b8a9b1dSJustin T. Gibbs {
24769911ecf9SJustin T. Gibbs 
247782727824SWarner Losh 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
247869be012fSWarner Losh 	    ("xpt_action: func %#x %s\n", start_ccb->ccb_h.func_code,
247969be012fSWarner Losh 		xpt_action_name(start_ccb->ccb_h.func_code)));
24808b8a9b1dSJustin T. Gibbs 
24818b8a9b1dSJustin T. Gibbs 	start_ccb->ccb_h.status = CAM_REQ_INPROG;
2482ded2b706SWarner Losh 	(*(start_ccb->ccb_h.path->bus->xport->ops->action))(start_ccb);
248352c9ce25SScott Long }
248452c9ce25SScott Long 
248552c9ce25SScott Long void
248652c9ce25SScott Long xpt_action_default(union ccb *start_ccb)
248752c9ce25SScott Long {
2488da396db2SAlexander Motin 	struct cam_path *path;
2489227d67aaSAlexander Motin 	struct cam_sim *sim;
2490227d67aaSAlexander Motin 	int lock;
249152c9ce25SScott Long 
2492da396db2SAlexander Motin 	path = start_ccb->ccb_h.path;
249382727824SWarner Losh 	CAM_DEBUG(path, CAM_DEBUG_TRACE,
249469be012fSWarner Losh 	    ("xpt_action_default: func %#x %s\n", start_ccb->ccb_h.func_code,
249569be012fSWarner Losh 		xpt_action_name(start_ccb->ccb_h.func_code)));
249652c9ce25SScott Long 
24978b8a9b1dSJustin T. Gibbs 	switch (start_ccb->ccb_h.func_code) {
24988b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
2499d05caa00SKenneth D. Merry 	{
25003393f8daSKenneth D. Merry 		struct cam_ed *device;
2501d05caa00SKenneth D. Merry 
25028b8a9b1dSJustin T. Gibbs 		/*
25038b8a9b1dSJustin T. Gibbs 		 * For the sake of compatibility with SCSI-1
25048b8a9b1dSJustin T. Gibbs 		 * devices that may not understand the identify
25058b8a9b1dSJustin T. Gibbs 		 * message, we include lun information in the
25068b8a9b1dSJustin T. Gibbs 		 * second byte of all commands.  SCSI-1 specifies
25078b8a9b1dSJustin T. Gibbs 		 * that luns are a 3 bit value and reserves only 3
25088b8a9b1dSJustin T. Gibbs 		 * bits for lun information in the CDB.  Later
25098b8a9b1dSJustin T. Gibbs 		 * revisions of the SCSI spec allow for more than 8
25108b8a9b1dSJustin T. Gibbs 		 * luns, but have deprecated lun information in the
25118b8a9b1dSJustin T. Gibbs 		 * CDB.  So, if the lun won't fit, we must omit.
25128b8a9b1dSJustin T. Gibbs 		 *
25138b8a9b1dSJustin T. Gibbs 		 * Also be aware that during initial probing for devices,
25148b8a9b1dSJustin T. Gibbs 		 * the inquiry information is unknown but initialized to 0.
25158b8a9b1dSJustin T. Gibbs 		 * This means that this code will be exercised while probing
25168b8a9b1dSJustin T. Gibbs 		 * devices with an ANSI revision greater than 2.
25178b8a9b1dSJustin T. Gibbs 		 */
2518da396db2SAlexander Motin 		device = path->device;
25193393f8daSKenneth D. Merry 		if (device->protocol_version <= SCSI_REV_2
25208b8a9b1dSJustin T. Gibbs 		 && start_ccb->ccb_h.target_lun < 8
25218b8a9b1dSJustin T. Gibbs 		 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
25228b8a9b1dSJustin T. Gibbs 
25238b8a9b1dSJustin T. Gibbs 			start_ccb->csio.cdb_io.cdb_bytes[1] |=
25248b8a9b1dSJustin T. Gibbs 			    start_ccb->ccb_h.target_lun << 5;
25258b8a9b1dSJustin T. Gibbs 		}
25268b8a9b1dSJustin T. Gibbs 		start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2527d05caa00SKenneth D. Merry 	}
252807c6eac9SPoul-Henning Kamp 	/* FALLTHROUGH */
25298b8a9b1dSJustin T. Gibbs 	case XPT_TARGET_IO:
25308b8a9b1dSJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
25312cefde5fSJustin T. Gibbs 		start_ccb->csio.sense_resid = 0;
25322cefde5fSJustin T. Gibbs 		start_ccb->csio.resid = 0;
25332cefde5fSJustin T. Gibbs 		/* FALLTHROUGH */
253452c9ce25SScott Long 	case XPT_ATA_IO:
2535de9ebb68SAlexander Motin 		if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
253652c9ce25SScott Long 			start_ccb->ataio.resid = 0;
2537b882a6d3SMatt Jacob 		/* FALLTHROUGH */
2538b24ced67SWarner Losh 	case XPT_NVME_IO:
2539b24ced67SWarner Losh 		if (start_ccb->ccb_h.func_code == XPT_NVME_IO)
2540b24ced67SWarner Losh 			start_ccb->nvmeio.resid = 0;
2541b24ced67SWarner Losh 		/* FALLTHROUGH */
254210e1cf63SKenneth D. Merry 	case XPT_RESET_DEV:
25438b8a9b1dSJustin T. Gibbs 	case XPT_ENG_EXEC:
254406e79492SKenneth D. Merry 	case XPT_SMP_IO:
25452cefde5fSJustin T. Gibbs 	{
2546227d67aaSAlexander Motin 		struct cam_devq *devq;
25472cefde5fSJustin T. Gibbs 
2548227d67aaSAlexander Motin 		devq = path->bus->sim->devq;
2549227d67aaSAlexander Motin 		mtx_lock(&devq->send_mtx);
2550227d67aaSAlexander Motin 		cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2551227d67aaSAlexander Motin 		if (xpt_schedule_devq(devq, path->device) != 0)
2552227d67aaSAlexander Motin 			xpt_run_devq(devq);
2553227d67aaSAlexander Motin 		mtx_unlock(&devq->send_mtx);
2554227d67aaSAlexander Motin 		break;
2555227d67aaSAlexander Motin 	}
2556227d67aaSAlexander Motin 	case XPT_CALC_GEOMETRY:
25578b8a9b1dSJustin T. Gibbs 		/* Filter out garbage */
25588b8a9b1dSJustin T. Gibbs 		if (start_ccb->ccg.block_size == 0
25598b8a9b1dSJustin T. Gibbs 		 || start_ccb->ccg.volume_size == 0) {
25608b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.cylinders = 0;
25618b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.heads = 0;
25628b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.secs_per_track = 0;
25638b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
25648b8a9b1dSJustin T. Gibbs 			break;
25658b8a9b1dSJustin T. Gibbs 		}
2566abef0e67SMarius Strobl #if defined(PC98) || defined(__sparc64__)
25678b8a9b1dSJustin T. Gibbs 		/*
25688b8a9b1dSJustin T. Gibbs 		 * In a PC-98 system, geometry translation depens on
25698b8a9b1dSJustin T. Gibbs 		 * the "real" device geometry obtained from mode page 4.
25708b8a9b1dSJustin T. Gibbs 		 * SCSI geometry translation is performed in the
25718b8a9b1dSJustin T. Gibbs 		 * initialization routine of the SCSI BIOS and the result
25728b8a9b1dSJustin T. Gibbs 		 * stored in host memory.  If the translation is available
25738b8a9b1dSJustin T. Gibbs 		 * in host memory, use it.  If not, rely on the default
25748b8a9b1dSJustin T. Gibbs 		 * translation the device driver performs.
2575abef0e67SMarius Strobl 		 * For sparc64, we may need adjust the geometry of large
2576abef0e67SMarius Strobl 		 * disks in order to fit the limitations of the 16-bit
2577abef0e67SMarius Strobl 		 * fields of the VTOC8 disk label.
25788b8a9b1dSJustin T. Gibbs 		 */
25798b8a9b1dSJustin T. Gibbs 		if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
25808b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
25818b8a9b1dSJustin T. Gibbs 			break;
25828b8a9b1dSJustin T. Gibbs 		}
25838b8a9b1dSJustin T. Gibbs #endif
2584227d67aaSAlexander Motin 		goto call_sim;
2585bb6087e5SJustin T. Gibbs 	case XPT_ABORT:
25862cefde5fSJustin T. Gibbs 	{
25872cefde5fSJustin T. Gibbs 		union ccb* abort_ccb;
25882cefde5fSJustin T. Gibbs 
25892cefde5fSJustin T. Gibbs 		abort_ccb = start_ccb->cab.abort_ccb;
25902cefde5fSJustin T. Gibbs 		if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
259183c5d981SAlexander Motin 			struct cam_ed *device;
2592dda945f5SMark Johnston 			struct cam_devq *devq;
25932cefde5fSJustin T. Gibbs 
259483c5d981SAlexander Motin 			device = abort_ccb->ccb_h.path->device;
2595dda945f5SMark Johnston 			devq = device->sim->devq;
2596dda945f5SMark Johnston 
2597dda945f5SMark Johnston 			mtx_lock(&devq->send_mtx);
2598dda945f5SMark Johnston 			if (abort_ccb->ccb_h.pinfo.index > 0) {
2599dda945f5SMark Johnston 				cam_ccbq_remove_ccb(&device->ccbq, abort_ccb);
26002cefde5fSJustin T. Gibbs 				abort_ccb->ccb_h.status =
26012cefde5fSJustin T. Gibbs 				    CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2602dda945f5SMark Johnston 				xpt_freeze_devq_device(device, 1);
2603dda945f5SMark Johnston 				mtx_unlock(&devq->send_mtx);
26042cefde5fSJustin T. Gibbs 				xpt_done(abort_ccb);
26052cefde5fSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
26062cefde5fSJustin T. Gibbs 				break;
26072cefde5fSJustin T. Gibbs 			}
2608dda945f5SMark Johnston 			mtx_unlock(&devq->send_mtx);
2609dda945f5SMark Johnston 
26102cefde5fSJustin T. Gibbs 			if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
26112cefde5fSJustin T. Gibbs 			 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
26122cefde5fSJustin T. Gibbs 				/*
26132cefde5fSJustin T. Gibbs 				 * We've caught this ccb en route to
26142cefde5fSJustin T. Gibbs 				 * the SIM.  Flag it for abort and the
26152cefde5fSJustin T. Gibbs 				 * SIM will do so just before starting
26162cefde5fSJustin T. Gibbs 				 * real work on the CCB.
26172cefde5fSJustin T. Gibbs 				 */
26182cefde5fSJustin T. Gibbs 				abort_ccb->ccb_h.status =
26192cefde5fSJustin T. Gibbs 				    CAM_REQ_ABORTED|CAM_DEV_QFRZN;
26202cefde5fSJustin T. Gibbs 				xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
26212cefde5fSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
26222cefde5fSJustin T. Gibbs 				break;
26232cefde5fSJustin T. Gibbs 			}
26242cefde5fSJustin T. Gibbs 		}
26252cefde5fSJustin T. Gibbs 		if (XPT_FC_IS_QUEUED(abort_ccb)
26262cefde5fSJustin T. Gibbs 		 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
26272cefde5fSJustin T. Gibbs 			/*
26282cefde5fSJustin T. Gibbs 			 * It's already completed but waiting
26292cefde5fSJustin T. Gibbs 			 * for our SWI to get to it.
26302cefde5fSJustin T. Gibbs 			 */
26312cefde5fSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_UA_ABORT;
26322cefde5fSJustin T. Gibbs 			break;
26332cefde5fSJustin T. Gibbs 		}
26342cefde5fSJustin T. Gibbs 		/*
26352cefde5fSJustin T. Gibbs 		 * If we weren't able to take care of the abort request
26362cefde5fSJustin T. Gibbs 		 * in the XPT, pass the request down to the SIM for processing.
26372cefde5fSJustin T. Gibbs 		 */
26382cefde5fSJustin T. Gibbs 	}
263907c6eac9SPoul-Henning Kamp 	/* FALLTHROUGH */
26408b8a9b1dSJustin T. Gibbs 	case XPT_ACCEPT_TARGET_IO:
26418b8a9b1dSJustin T. Gibbs 	case XPT_EN_LUN:
26428b8a9b1dSJustin T. Gibbs 	case XPT_IMMED_NOTIFY:
26438b8a9b1dSJustin T. Gibbs 	case XPT_NOTIFY_ACK:
26448b8a9b1dSJustin T. Gibbs 	case XPT_RESET_BUS:
26452df76c16SMatt Jacob 	case XPT_IMMEDIATE_NOTIFY:
26462df76c16SMatt Jacob 	case XPT_NOTIFY_ACKNOWLEDGE:
2647a2531862SWarner Losh 	case XPT_GET_SIM_KNOB_OLD:
26482df76c16SMatt Jacob 	case XPT_GET_SIM_KNOB:
26492df76c16SMatt Jacob 	case XPT_SET_SIM_KNOB:
2650227d67aaSAlexander Motin 	case XPT_GET_TRAN_SETTINGS:
2651227d67aaSAlexander Motin 	case XPT_SET_TRAN_SETTINGS:
265287cfaf0eSJustin T. Gibbs 	case XPT_PATH_INQ:
2653227d67aaSAlexander Motin call_sim:
2654da396db2SAlexander Motin 		sim = path->bus->sim;
2655227d67aaSAlexander Motin 		lock = (mtx_owned(sim->mtx) == 0);
2656227d67aaSAlexander Motin 		if (lock)
2657227d67aaSAlexander Motin 			CAM_SIM_LOCK(sim);
265882727824SWarner Losh 		CAM_DEBUG(path, CAM_DEBUG_TRACE,
265982727824SWarner Losh 		    ("sim->sim_action: func=%#x\n", start_ccb->ccb_h.func_code));
266087cfaf0eSJustin T. Gibbs 		(*(sim->sim_action))(sim, start_ccb);
266182727824SWarner Losh 		CAM_DEBUG(path, CAM_DEBUG_TRACE,
266282727824SWarner Losh 		    ("sim->sim_action: status=%#x\n", start_ccb->ccb_h.status));
2663227d67aaSAlexander Motin 		if (lock)
2664227d67aaSAlexander Motin 			CAM_SIM_UNLOCK(sim);
266587cfaf0eSJustin T. Gibbs 		break;
266687cfaf0eSJustin T. Gibbs 	case XPT_PATH_STATS:
2667da396db2SAlexander Motin 		start_ccb->cpis.last_reset = path->bus->last_reset;
266887cfaf0eSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
266987cfaf0eSJustin T. Gibbs 		break;
26708b8a9b1dSJustin T. Gibbs 	case XPT_GDEV_TYPE:
2671a5479bc5SJustin T. Gibbs 	{
267287cfaf0eSJustin T. Gibbs 		struct cam_ed *dev;
2673a5479bc5SJustin T. Gibbs 
2674da396db2SAlexander Motin 		dev = path->device;
267587cfaf0eSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
26768b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
26778b8a9b1dSJustin T. Gibbs 		} else {
26788b8a9b1dSJustin T. Gibbs 			struct ccb_getdev *cgd;
26798b8a9b1dSJustin T. Gibbs 
26808b8a9b1dSJustin T. Gibbs 			cgd = &start_ccb->cgd;
268152c9ce25SScott Long 			cgd->protocol = dev->protocol;
26828b8a9b1dSJustin T. Gibbs 			cgd->inq_data = dev->inq_data;
268352c9ce25SScott Long 			cgd->ident_data = dev->ident_data;
268430a4094fSAlexander Motin 			cgd->inq_flags = dev->inq_flags;
2685b24ced67SWarner Losh 			cgd->nvme_data = dev->nvme_data;
2686b24ced67SWarner Losh 			cgd->nvme_cdata = dev->nvme_cdata;
26878b8a9b1dSJustin T. Gibbs 			cgd->ccb_h.status = CAM_REQ_CMP;
26888b8a9b1dSJustin T. Gibbs 			cgd->serial_num_len = dev->serial_num_len;
26898b8a9b1dSJustin T. Gibbs 			if ((dev->serial_num_len > 0)
26908b8a9b1dSJustin T. Gibbs 			 && (dev->serial_num != NULL))
26918b8a9b1dSJustin T. Gibbs 				bcopy(dev->serial_num, cgd->serial_num,
26928b8a9b1dSJustin T. Gibbs 				      dev->serial_num_len);
26938b8a9b1dSJustin T. Gibbs 		}
26948b8a9b1dSJustin T. Gibbs 		break;
2695a5479bc5SJustin T. Gibbs 	}
269687cfaf0eSJustin T. Gibbs 	case XPT_GDEV_STATS:
269787cfaf0eSJustin T. Gibbs 	{
269887cfaf0eSJustin T. Gibbs 		struct cam_ed *dev;
269987cfaf0eSJustin T. Gibbs 
2700da396db2SAlexander Motin 		dev = path->device;
270187cfaf0eSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
270287cfaf0eSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
270387cfaf0eSJustin T. Gibbs 		} else {
270487cfaf0eSJustin T. Gibbs 			struct ccb_getdevstats *cgds;
270587cfaf0eSJustin T. Gibbs 			struct cam_eb *bus;
270687cfaf0eSJustin T. Gibbs 			struct cam_et *tar;
2707959ec258SAlexander Motin 			struct cam_devq *devq;
270887cfaf0eSJustin T. Gibbs 
270987cfaf0eSJustin T. Gibbs 			cgds = &start_ccb->cgds;
2710da396db2SAlexander Motin 			bus = path->bus;
2711da396db2SAlexander Motin 			tar = path->target;
2712959ec258SAlexander Motin 			devq = bus->sim->devq;
2713959ec258SAlexander Motin 			mtx_lock(&devq->send_mtx);
271487cfaf0eSJustin T. Gibbs 			cgds->dev_openings = dev->ccbq.dev_openings;
271587cfaf0eSJustin T. Gibbs 			cgds->dev_active = dev->ccbq.dev_active;
2716959ec258SAlexander Motin 			cgds->allocated = dev->ccbq.allocated;
2717959ec258SAlexander Motin 			cgds->queued = cam_ccbq_pending_ccb_count(&dev->ccbq);
2718959ec258SAlexander Motin 			cgds->held = cgds->allocated - cgds->dev_active -
2719959ec258SAlexander Motin 			    cgds->queued;
272087cfaf0eSJustin T. Gibbs 			cgds->last_reset = tar->last_reset;
272152c9ce25SScott Long 			cgds->maxtags = dev->maxtags;
272252c9ce25SScott Long 			cgds->mintags = dev->mintags;
272387cfaf0eSJustin T. Gibbs 			if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
272487cfaf0eSJustin T. Gibbs 				cgds->last_reset = bus->last_reset;
2725959ec258SAlexander Motin 			mtx_unlock(&devq->send_mtx);
272687cfaf0eSJustin T. Gibbs 			cgds->ccb_h.status = CAM_REQ_CMP;
272787cfaf0eSJustin T. Gibbs 		}
272887cfaf0eSJustin T. Gibbs 		break;
272987cfaf0eSJustin T. Gibbs 	}
27308b8a9b1dSJustin T. Gibbs 	case XPT_GDEVLIST:
27318b8a9b1dSJustin T. Gibbs 	{
27328b8a9b1dSJustin T. Gibbs 		struct cam_periph	*nperiph;
27338b8a9b1dSJustin T. Gibbs 		struct periph_list	*periph_head;
27348b8a9b1dSJustin T. Gibbs 		struct ccb_getdevlist	*cgdl;
27353393f8daSKenneth D. Merry 		u_int			i;
27368b8a9b1dSJustin T. Gibbs 		struct cam_ed		*device;
27378b8a9b1dSJustin T. Gibbs 		int			found;
27388b8a9b1dSJustin T. Gibbs 
27398b8a9b1dSJustin T. Gibbs 
27408b8a9b1dSJustin T. Gibbs 		found = 0;
27418b8a9b1dSJustin T. Gibbs 
27428b8a9b1dSJustin T. Gibbs 		/*
27438b8a9b1dSJustin T. Gibbs 		 * Don't want anyone mucking with our data.
27448b8a9b1dSJustin T. Gibbs 		 */
2745da396db2SAlexander Motin 		device = path->device;
27468b8a9b1dSJustin T. Gibbs 		periph_head = &device->periphs;
27478b8a9b1dSJustin T. Gibbs 		cgdl = &start_ccb->cgdl;
27488b8a9b1dSJustin T. Gibbs 
27498b8a9b1dSJustin T. Gibbs 		/*
27508b8a9b1dSJustin T. Gibbs 		 * Check and see if the list has changed since the user
27518b8a9b1dSJustin T. Gibbs 		 * last requested a list member.  If so, tell them that the
27528b8a9b1dSJustin T. Gibbs 		 * list has changed, and therefore they need to start over
27538b8a9b1dSJustin T. Gibbs 		 * from the beginning.
27548b8a9b1dSJustin T. Gibbs 		 */
27558b8a9b1dSJustin T. Gibbs 		if ((cgdl->index != 0) &&
27568b8a9b1dSJustin T. Gibbs 		    (cgdl->generation != device->generation)) {
27578b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
27588b8a9b1dSJustin T. Gibbs 			break;
27598b8a9b1dSJustin T. Gibbs 		}
27608b8a9b1dSJustin T. Gibbs 
27618b8a9b1dSJustin T. Gibbs 		/*
27628b8a9b1dSJustin T. Gibbs 		 * Traverse the list of peripherals and attempt to find
27638b8a9b1dSJustin T. Gibbs 		 * the requested peripheral.
27648b8a9b1dSJustin T. Gibbs 		 */
2765fc2ffbe6SPoul-Henning Kamp 		for (nperiph = SLIST_FIRST(periph_head), i = 0;
27668b8a9b1dSJustin T. Gibbs 		     (nperiph != NULL) && (i <= cgdl->index);
2767fc2ffbe6SPoul-Henning Kamp 		     nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
27688b8a9b1dSJustin T. Gibbs 			if (i == cgdl->index) {
27698b8a9b1dSJustin T. Gibbs 				strncpy(cgdl->periph_name,
27708b8a9b1dSJustin T. Gibbs 					nperiph->periph_name,
27718b8a9b1dSJustin T. Gibbs 					DEV_IDLEN);
27728b8a9b1dSJustin T. Gibbs 				cgdl->unit_number = nperiph->unit_number;
27738b8a9b1dSJustin T. Gibbs 				found = 1;
27748b8a9b1dSJustin T. Gibbs 			}
27758b8a9b1dSJustin T. Gibbs 		}
27768b8a9b1dSJustin T. Gibbs 		if (found == 0) {
27778b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_ERROR;
27788b8a9b1dSJustin T. Gibbs 			break;
27798b8a9b1dSJustin T. Gibbs 		}
27808b8a9b1dSJustin T. Gibbs 
27818b8a9b1dSJustin T. Gibbs 		if (nperiph == NULL)
27828b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
27838b8a9b1dSJustin T. Gibbs 		else
27848b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_MORE_DEVS;
27858b8a9b1dSJustin T. Gibbs 
27868b8a9b1dSJustin T. Gibbs 		cgdl->index++;
27878b8a9b1dSJustin T. Gibbs 		cgdl->generation = device->generation;
27888b8a9b1dSJustin T. Gibbs 
27898b8a9b1dSJustin T. Gibbs 		cgdl->ccb_h.status = CAM_REQ_CMP;
27908b8a9b1dSJustin T. Gibbs 		break;
27918b8a9b1dSJustin T. Gibbs 	}
27928b8a9b1dSJustin T. Gibbs 	case XPT_DEV_MATCH:
27938b8a9b1dSJustin T. Gibbs 	{
27948b8a9b1dSJustin T. Gibbs 		dev_pos_type position_type;
27958b8a9b1dSJustin T. Gibbs 		struct ccb_dev_match *cdm;
27968b8a9b1dSJustin T. Gibbs 
27978b8a9b1dSJustin T. Gibbs 		cdm = &start_ccb->cdm;
27988b8a9b1dSJustin T. Gibbs 
27998b8a9b1dSJustin T. Gibbs 		/*
28008b8a9b1dSJustin T. Gibbs 		 * There are two ways of getting at information in the EDT.
28018b8a9b1dSJustin T. Gibbs 		 * The first way is via the primary EDT tree.  It starts
28028b8a9b1dSJustin T. Gibbs 		 * with a list of busses, then a list of targets on a bus,
28038b8a9b1dSJustin T. Gibbs 		 * then devices/luns on a target, and then peripherals on a
28048b8a9b1dSJustin T. Gibbs 		 * device/lun.  The "other" way is by the peripheral driver
28058b8a9b1dSJustin T. Gibbs 		 * lists.  The peripheral driver lists are organized by
28068b8a9b1dSJustin T. Gibbs 		 * peripheral driver.  (obviously)  So it makes sense to
28078b8a9b1dSJustin T. Gibbs 		 * use the peripheral driver list if the user is looking
28088b8a9b1dSJustin T. Gibbs 		 * for something like "da1", or all "da" devices.  If the
28098b8a9b1dSJustin T. Gibbs 		 * user is looking for something on a particular bus/target
28108b8a9b1dSJustin T. Gibbs 		 * or lun, it's generally better to go through the EDT tree.
28118b8a9b1dSJustin T. Gibbs 		 */
28128b8a9b1dSJustin T. Gibbs 
28138b8a9b1dSJustin T. Gibbs 		if (cdm->pos.position_type != CAM_DEV_POS_NONE)
28148b8a9b1dSJustin T. Gibbs 			position_type = cdm->pos.position_type;
28158b8a9b1dSJustin T. Gibbs 		else {
28163393f8daSKenneth D. Merry 			u_int i;
28178b8a9b1dSJustin T. Gibbs 
28188b8a9b1dSJustin T. Gibbs 			position_type = CAM_DEV_POS_NONE;
28198b8a9b1dSJustin T. Gibbs 
28208b8a9b1dSJustin T. Gibbs 			for (i = 0; i < cdm->num_patterns; i++) {
28218b8a9b1dSJustin T. Gibbs 				if ((cdm->patterns[i].type == DEV_MATCH_BUS)
28228b8a9b1dSJustin T. Gibbs 				 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
28238b8a9b1dSJustin T. Gibbs 					position_type = CAM_DEV_POS_EDT;
28248b8a9b1dSJustin T. Gibbs 					break;
28258b8a9b1dSJustin T. Gibbs 				}
28268b8a9b1dSJustin T. Gibbs 			}
28278b8a9b1dSJustin T. Gibbs 
28288b8a9b1dSJustin T. Gibbs 			if (cdm->num_patterns == 0)
28298b8a9b1dSJustin T. Gibbs 				position_type = CAM_DEV_POS_EDT;
28308b8a9b1dSJustin T. Gibbs 			else if (position_type == CAM_DEV_POS_NONE)
28318b8a9b1dSJustin T. Gibbs 				position_type = CAM_DEV_POS_PDRV;
28328b8a9b1dSJustin T. Gibbs 		}
28338b8a9b1dSJustin T. Gibbs 
28348b8a9b1dSJustin T. Gibbs 		switch(position_type & CAM_DEV_POS_TYPEMASK) {
28358b8a9b1dSJustin T. Gibbs 		case CAM_DEV_POS_EDT:
283607c6eac9SPoul-Henning Kamp 			xptedtmatch(cdm);
28378b8a9b1dSJustin T. Gibbs 			break;
28388b8a9b1dSJustin T. Gibbs 		case CAM_DEV_POS_PDRV:
283907c6eac9SPoul-Henning Kamp 			xptperiphlistmatch(cdm);
28408b8a9b1dSJustin T. Gibbs 			break;
28418b8a9b1dSJustin T. Gibbs 		default:
28428b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_ERROR;
28438b8a9b1dSJustin T. Gibbs 			break;
28448b8a9b1dSJustin T. Gibbs 		}
28458b8a9b1dSJustin T. Gibbs 
28468b8a9b1dSJustin T. Gibbs 		if (cdm->status == CAM_DEV_MATCH_ERROR)
28478b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
28488b8a9b1dSJustin T. Gibbs 		else
28498b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
28508b8a9b1dSJustin T. Gibbs 
28518b8a9b1dSJustin T. Gibbs 		break;
28528b8a9b1dSJustin T. Gibbs 	}
28538b8a9b1dSJustin T. Gibbs 	case XPT_SASYNC_CB:
28548b8a9b1dSJustin T. Gibbs 	{
285584f82481SScott Long 		struct ccb_setasync *csa;
285684f82481SScott Long 		struct async_node *cur_entry;
285784f82481SScott Long 		struct async_list *async_head;
285884f82481SScott Long 		u_int32_t added;
285984f82481SScott Long 
286084f82481SScott Long 		csa = &start_ccb->csa;
286184f82481SScott Long 		added = csa->event_enable;
2862da396db2SAlexander Motin 		async_head = &path->device->asyncs;
286384f82481SScott Long 
286484f82481SScott Long 		/*
286584f82481SScott Long 		 * If there is already an entry for us, simply
286684f82481SScott Long 		 * update it.
286784f82481SScott Long 		 */
286884f82481SScott Long 		cur_entry = SLIST_FIRST(async_head);
286984f82481SScott Long 		while (cur_entry != NULL) {
287084f82481SScott Long 			if ((cur_entry->callback_arg == csa->callback_arg)
287184f82481SScott Long 			 && (cur_entry->callback == csa->callback))
287284f82481SScott Long 				break;
287384f82481SScott Long 			cur_entry = SLIST_NEXT(cur_entry, links);
287484f82481SScott Long 		}
287584f82481SScott Long 
287684f82481SScott Long 		if (cur_entry != NULL) {
287784f82481SScott Long 		 	/*
287884f82481SScott Long 			 * If the request has no flags set,
287984f82481SScott Long 			 * remove the entry.
288084f82481SScott Long 			 */
288184f82481SScott Long 			added &= ~cur_entry->event_enable;
288284f82481SScott Long 			if (csa->event_enable == 0) {
288384f82481SScott Long 				SLIST_REMOVE(async_head, cur_entry,
288484f82481SScott Long 					     async_node, links);
2885da396db2SAlexander Motin 				xpt_release_device(path->device);
288684f82481SScott Long 				free(cur_entry, M_CAMXPT);
288784f82481SScott Long 			} else {
288884f82481SScott Long 				cur_entry->event_enable = csa->event_enable;
288984f82481SScott Long 			}
28907685edecSAlexander Motin 			csa->event_enable = added;
289184f82481SScott Long 		} else {
289284f82481SScott Long 			cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
289384f82481SScott Long 					   M_NOWAIT);
289484f82481SScott Long 			if (cur_entry == NULL) {
289584f82481SScott Long 				csa->ccb_h.status = CAM_RESRC_UNAVAIL;
289684f82481SScott Long 				break;
289784f82481SScott Long 			}
289884f82481SScott Long 			cur_entry->event_enable = csa->event_enable;
2899227d67aaSAlexander Motin 			cur_entry->event_lock =
2900227d67aaSAlexander Motin 			    mtx_owned(path->bus->sim->mtx) ? 1 : 0;
290184f82481SScott Long 			cur_entry->callback_arg = csa->callback_arg;
290284f82481SScott Long 			cur_entry->callback = csa->callback;
290384f82481SScott Long 			SLIST_INSERT_HEAD(async_head, cur_entry, links);
2904da396db2SAlexander Motin 			xpt_acquire_device(path->device);
290584f82481SScott Long 		}
29068b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
29078b8a9b1dSJustin T. Gibbs 		break;
29088b8a9b1dSJustin T. Gibbs 	}
29098b8a9b1dSJustin T. Gibbs 	case XPT_REL_SIMQ:
29108b8a9b1dSJustin T. Gibbs 	{
29118b8a9b1dSJustin T. Gibbs 		struct ccb_relsim *crs;
29128b8a9b1dSJustin T. Gibbs 		struct cam_ed *dev;
29138b8a9b1dSJustin T. Gibbs 
29148b8a9b1dSJustin T. Gibbs 		crs = &start_ccb->crs;
2915da396db2SAlexander Motin 		dev = path->device;
29168b8a9b1dSJustin T. Gibbs 		if (dev == NULL) {
29178b8a9b1dSJustin T. Gibbs 
29188b8a9b1dSJustin T. Gibbs 			crs->ccb_h.status = CAM_DEV_NOT_THERE;
29198b8a9b1dSJustin T. Gibbs 			break;
29208b8a9b1dSJustin T. Gibbs 		}
29218b8a9b1dSJustin T. Gibbs 
29228b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
29238b8a9b1dSJustin T. Gibbs 
29248b8a9b1dSJustin T. Gibbs 			/* Don't ever go below one opening */
29258b8a9b1dSJustin T. Gibbs 			if (crs->openings > 0) {
29267dc3213dSAlexander Motin 				xpt_dev_ccbq_resize(path, crs->openings);
292779ccc199SJordan K. Hubbard 				if (bootverbose) {
2928da396db2SAlexander Motin 					xpt_print(path,
29297dc3213dSAlexander Motin 					    "number of openings is now %d\n",
29308b8a9b1dSJustin T. Gibbs 					    crs->openings);
29318b8a9b1dSJustin T. Gibbs 				}
29328b8a9b1dSJustin T. Gibbs 			}
29338b8a9b1dSJustin T. Gibbs 		}
29348b8a9b1dSJustin T. Gibbs 
2935227d67aaSAlexander Motin 		mtx_lock(&dev->sim->devq->send_mtx);
29368b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
29378b8a9b1dSJustin T. Gibbs 
29388b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
29398b8a9b1dSJustin T. Gibbs 
29408b8a9b1dSJustin T. Gibbs 				/*
29418b8a9b1dSJustin T. Gibbs 				 * Just extend the old timeout and decrement
29428b8a9b1dSJustin T. Gibbs 				 * the freeze count so that a single timeout
29438b8a9b1dSJustin T. Gibbs 				 * is sufficient for releasing the queue.
29448b8a9b1dSJustin T. Gibbs 				 */
29458b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
29462b83592fSScott Long 				callout_stop(&dev->callout);
29478b8a9b1dSJustin T. Gibbs 			} else {
29488b8a9b1dSJustin T. Gibbs 
29498b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
29508b8a9b1dSJustin T. Gibbs 			}
29518b8a9b1dSJustin T. Gibbs 
295285c9dd9dSSteven Hartland 			callout_reset_sbt(&dev->callout,
295385c9dd9dSSteven Hartland 			    SBT_1MS * crs->release_timeout, 0,
295485c9dd9dSSteven Hartland 			    xpt_release_devq_timeout, dev, 0);
29558b8a9b1dSJustin T. Gibbs 
29568b8a9b1dSJustin T. Gibbs 			dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
29578b8a9b1dSJustin T. Gibbs 
29588b8a9b1dSJustin T. Gibbs 		}
29598b8a9b1dSJustin T. Gibbs 
29608b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
29618b8a9b1dSJustin T. Gibbs 
29628b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
29638b8a9b1dSJustin T. Gibbs 				/*
29648b8a9b1dSJustin T. Gibbs 				 * Decrement the freeze count so that a single
29658b8a9b1dSJustin T. Gibbs 				 * completion is still sufficient to unfreeze
29668b8a9b1dSJustin T. Gibbs 				 * the queue.
29678b8a9b1dSJustin T. Gibbs 				 */
29688b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
29698b8a9b1dSJustin T. Gibbs 			} else {
29708b8a9b1dSJustin T. Gibbs 
29718b8a9b1dSJustin T. Gibbs 				dev->flags |= CAM_DEV_REL_ON_COMPLETE;
29728b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
29738b8a9b1dSJustin T. Gibbs 			}
29748b8a9b1dSJustin T. Gibbs 		}
29758b8a9b1dSJustin T. Gibbs 
29768b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
29778b8a9b1dSJustin T. Gibbs 
29788b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
29798b8a9b1dSJustin T. Gibbs 			 || (dev->ccbq.dev_active == 0)) {
29808b8a9b1dSJustin T. Gibbs 
29818b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
29828b8a9b1dSJustin T. Gibbs 			} else {
29838b8a9b1dSJustin T. Gibbs 
29848b8a9b1dSJustin T. Gibbs 				dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
29858b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
29868b8a9b1dSJustin T. Gibbs 			}
29878b8a9b1dSJustin T. Gibbs 		}
2988227d67aaSAlexander Motin 		mtx_unlock(&dev->sim->devq->send_mtx);
29898b8a9b1dSJustin T. Gibbs 
2990cccf4220SAlexander Motin 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
2991cccf4220SAlexander Motin 			xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
2992cccf4220SAlexander Motin 		start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
29938b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
29948b8a9b1dSJustin T. Gibbs 		break;
29958b8a9b1dSJustin T. Gibbs 	}
29968b8a9b1dSJustin T. Gibbs 	case XPT_DEBUG: {
299780d6987cSAlexander Motin 		struct cam_path *oldpath;
299880d6987cSAlexander Motin 
2999f0f25b9cSAlexander Motin 		/* Check that all request bits are supported. */
300022c7d606SAlexander Motin 		if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
3001f0f25b9cSAlexander Motin 			start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3002f0f25b9cSAlexander Motin 			break;
3003f0f25b9cSAlexander Motin 		}
3004f0f25b9cSAlexander Motin 
300580d6987cSAlexander Motin 		cam_dflags = CAM_DEBUG_NONE;
30068b8a9b1dSJustin T. Gibbs 		if (cam_dpath != NULL) {
300780d6987cSAlexander Motin 			oldpath = cam_dpath;
30088b8a9b1dSJustin T. Gibbs 			cam_dpath = NULL;
300980d6987cSAlexander Motin 			xpt_free_path(oldpath);
30108b8a9b1dSJustin T. Gibbs 		}
301180d6987cSAlexander Motin 		if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
3012e5dfa058SAlexander Motin 			if (xpt_create_path(&cam_dpath, NULL,
30138b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.path_id,
30148b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.target_id,
30158b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.target_lun) !=
30168b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP) {
30178b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3018aa872be6SMatt Jacob 			} else {
301980d6987cSAlexander Motin 				cam_dflags = start_ccb->cdbg.flags;
30208b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
3021f0d9af51SMatt Jacob 				xpt_print(cam_dpath, "debugging flags now %x\n",
3022f0d9af51SMatt Jacob 				    cam_dflags);
3023aa872be6SMatt Jacob 			}
302480d6987cSAlexander Motin 		} else
30258b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
30268b8a9b1dSJustin T. Gibbs 		break;
30278b8a9b1dSJustin T. Gibbs 	}
30288b8a9b1dSJustin T. Gibbs 	case XPT_NOOP:
302987cfaf0eSJustin T. Gibbs 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3030da396db2SAlexander Motin 			xpt_freeze_devq(path, 1);
30318b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
30328b8a9b1dSJustin T. Gibbs 		break;
3033d68fae58SEdward Tomasz Napierala 	case XPT_REPROBE_LUN:
3034d68fae58SEdward Tomasz Napierala 		xpt_async(AC_INQ_CHANGED, path, NULL);
3035d68fae58SEdward Tomasz Napierala 		start_ccb->ccb_h.status = CAM_REQ_CMP;
3036d68fae58SEdward Tomasz Napierala 		xpt_done(start_ccb);
3037d68fae58SEdward Tomasz Napierala 		break;
30388b8a9b1dSJustin T. Gibbs 	default:
30398b8a9b1dSJustin T. Gibbs 	case XPT_SDEV_TYPE:
30408b8a9b1dSJustin T. Gibbs 	case XPT_TERM_IO:
30418b8a9b1dSJustin T. Gibbs 	case XPT_ENG_INQ:
30428b8a9b1dSJustin T. Gibbs 		/* XXX Implement */
3043b24ced67SWarner Losh 		xpt_print_path(start_ccb->ccb_h.path);
3044b24ced67SWarner Losh 		printf("%s: CCB type %#x %s not supported\n", __func__,
3045b24ced67SWarner Losh 		    start_ccb->ccb_h.func_code,
3046b24ced67SWarner Losh 		    xpt_action_name(start_ccb->ccb_h.func_code));
30478b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3048b882a6d3SMatt Jacob 		if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3049b882a6d3SMatt Jacob 			xpt_done(start_ccb);
3050b882a6d3SMatt Jacob 		}
30518b8a9b1dSJustin T. Gibbs 		break;
30528b8a9b1dSJustin T. Gibbs 	}
305369be012fSWarner Losh 	CAM_DEBUG(path, CAM_DEBUG_TRACE,
305469be012fSWarner Losh 	    ("xpt_action_default: func= %#x %s status %#x\n",
305569be012fSWarner Losh 		start_ccb->ccb_h.func_code,
305669be012fSWarner Losh  		xpt_action_name(start_ccb->ccb_h.func_code),
305769be012fSWarner Losh 		start_ccb->ccb_h.status));
30588b8a9b1dSJustin T. Gibbs }
30598b8a9b1dSJustin T. Gibbs 
30608b8a9b1dSJustin T. Gibbs void
30618b8a9b1dSJustin T. Gibbs xpt_polled_action(union ccb *start_ccb)
30628b8a9b1dSJustin T. Gibbs {
30638b8a9b1dSJustin T. Gibbs 	u_int32_t timeout;
30648b8a9b1dSJustin T. Gibbs 	struct	  cam_sim *sim;
30658b8a9b1dSJustin T. Gibbs 	struct	  cam_devq *devq;
30668b8a9b1dSJustin T. Gibbs 	struct	  cam_ed *dev;
30678b8a9b1dSJustin T. Gibbs 
30680069293bSAlexander Motin 	timeout = start_ccb->ccb_h.timeout * 10;
30698b8a9b1dSJustin T. Gibbs 	sim = start_ccb->ccb_h.path->bus->sim;
30708b8a9b1dSJustin T. Gibbs 	devq = sim->devq;
30718b8a9b1dSJustin T. Gibbs 	dev = start_ccb->ccb_h.path->device;
30728b8a9b1dSJustin T. Gibbs 
3073227d67aaSAlexander Motin 	mtx_unlock(&dev->device_mtx);
30744a612489SAlexander Motin 
30758b8a9b1dSJustin T. Gibbs 	/*
30768b8a9b1dSJustin T. Gibbs 	 * Steal an opening so that no other queued requests
30778b8a9b1dSJustin T. Gibbs 	 * can get it before us while we simulate interrupts.
30788b8a9b1dSJustin T. Gibbs 	 */
3079227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
30808b8a9b1dSJustin T. Gibbs 	dev->ccbq.dev_openings--;
3081227d67aaSAlexander Motin 	while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0) &&
3082227d67aaSAlexander Motin 	    (--timeout > 0)) {
3083227d67aaSAlexander Motin 		mtx_unlock(&devq->send_mtx);
30840069293bSAlexander Motin 		DELAY(100);
3085227d67aaSAlexander Motin 		CAM_SIM_LOCK(sim);
30868b8a9b1dSJustin T. Gibbs 		(*(sim->sim_poll))(sim);
3087227d67aaSAlexander Motin 		CAM_SIM_UNLOCK(sim);
3088227d67aaSAlexander Motin 		camisr_runqueue();
3089227d67aaSAlexander Motin 		mtx_lock(&devq->send_mtx);
30908b8a9b1dSJustin T. Gibbs 	}
30918b8a9b1dSJustin T. Gibbs 	dev->ccbq.dev_openings++;
3092227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
30938b8a9b1dSJustin T. Gibbs 
30948b8a9b1dSJustin T. Gibbs 	if (timeout != 0) {
30958b8a9b1dSJustin T. Gibbs 		xpt_action(start_ccb);
30968b8a9b1dSJustin T. Gibbs 		while(--timeout > 0) {
3097227d67aaSAlexander Motin 			CAM_SIM_LOCK(sim);
30988b8a9b1dSJustin T. Gibbs 			(*(sim->sim_poll))(sim);
3099227d67aaSAlexander Motin 			CAM_SIM_UNLOCK(sim);
3100227d67aaSAlexander Motin 			camisr_runqueue();
31018b8a9b1dSJustin T. Gibbs 			if ((start_ccb->ccb_h.status  & CAM_STATUS_MASK)
31028b8a9b1dSJustin T. Gibbs 			    != CAM_REQ_INPROG)
31038b8a9b1dSJustin T. Gibbs 				break;
31040069293bSAlexander Motin 			DELAY(100);
31058b8a9b1dSJustin T. Gibbs 		}
31068b8a9b1dSJustin T. Gibbs 		if (timeout == 0) {
31078b8a9b1dSJustin T. Gibbs 			/*
31088b8a9b1dSJustin T. Gibbs 			 * XXX Is it worth adding a sim_timeout entry
31098b8a9b1dSJustin T. Gibbs 			 * point so we can attempt recovery?  If
31108b8a9b1dSJustin T. Gibbs 			 * this is only used for dumps, I don't think
31118b8a9b1dSJustin T. Gibbs 			 * it is.
31128b8a9b1dSJustin T. Gibbs 			 */
31138b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
31148b8a9b1dSJustin T. Gibbs 		}
31158b8a9b1dSJustin T. Gibbs 	} else {
31168b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
31178b8a9b1dSJustin T. Gibbs 	}
31184a612489SAlexander Motin 
3119227d67aaSAlexander Motin 	mtx_lock(&dev->device_mtx);
31208b8a9b1dSJustin T. Gibbs }
31218b8a9b1dSJustin T. Gibbs 
31228b8a9b1dSJustin T. Gibbs /*
312321cffce5SBryan Drewery  * Schedule a peripheral driver to receive a ccb when its
31248b8a9b1dSJustin T. Gibbs  * target device has space for more transactions.
31258b8a9b1dSJustin T. Gibbs  */
31268b8a9b1dSJustin T. Gibbs void
3127227d67aaSAlexander Motin xpt_schedule(struct cam_periph *periph, u_int32_t new_priority)
31288b8a9b1dSJustin T. Gibbs {
31298b8a9b1dSJustin T. Gibbs 
3130227d67aaSAlexander Motin 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3131227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
3132227d67aaSAlexander Motin 	if (new_priority < periph->scheduled_priority) {
3133227d67aaSAlexander Motin 		periph->scheduled_priority = new_priority;
3134227d67aaSAlexander Motin 		xpt_run_allocq(periph, 0);
31358b8a9b1dSJustin T. Gibbs 	}
31368b8a9b1dSJustin T. Gibbs }
31378b8a9b1dSJustin T. Gibbs 
31388b8a9b1dSJustin T. Gibbs 
31398b8a9b1dSJustin T. Gibbs /*
31408b8a9b1dSJustin T. Gibbs  * Schedule a device to run on a given queue.
31418b8a9b1dSJustin T. Gibbs  * If the device was inserted as a new entry on the queue,
31428b8a9b1dSJustin T. Gibbs  * return 1 meaning the device queue should be run. If we
31438b8a9b1dSJustin T. Gibbs  * were already queued, implying someone else has already
31448b8a9b1dSJustin T. Gibbs  * started the queue, return 0 so the caller doesn't attempt
314577dc25ccSScott Long  * to run the queue.
31468b8a9b1dSJustin T. Gibbs  */
3147227d67aaSAlexander Motin static int
31488b8a9b1dSJustin T. Gibbs xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
31498b8a9b1dSJustin T. Gibbs 		 u_int32_t new_priority)
31508b8a9b1dSJustin T. Gibbs {
31518b8a9b1dSJustin T. Gibbs 	int retval;
31528b8a9b1dSJustin T. Gibbs 	u_int32_t old_priority;
31538b8a9b1dSJustin T. Gibbs 
3154aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
31558b8a9b1dSJustin T. Gibbs 
31568b8a9b1dSJustin T. Gibbs 	old_priority = pinfo->priority;
31578b8a9b1dSJustin T. Gibbs 
31588b8a9b1dSJustin T. Gibbs 	/*
31598b8a9b1dSJustin T. Gibbs 	 * Are we already queued?
31608b8a9b1dSJustin T. Gibbs 	 */
31618b8a9b1dSJustin T. Gibbs 	if (pinfo->index != CAM_UNQUEUED_INDEX) {
31628b8a9b1dSJustin T. Gibbs 		/* Simply reorder based on new priority */
31638b8a9b1dSJustin T. Gibbs 		if (new_priority < old_priority) {
31648b8a9b1dSJustin T. Gibbs 			camq_change_priority(queue, pinfo->index,
31658b8a9b1dSJustin T. Gibbs 					     new_priority);
3166aa872be6SMatt Jacob 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
31678b8a9b1dSJustin T. Gibbs 					("changed priority to %d\n",
31688b8a9b1dSJustin T. Gibbs 					 new_priority));
316983c5d981SAlexander Motin 			retval = 1;
317083c5d981SAlexander Motin 		} else
31718b8a9b1dSJustin T. Gibbs 			retval = 0;
31728b8a9b1dSJustin T. Gibbs 	} else {
31738b8a9b1dSJustin T. Gibbs 		/* New entry on the queue */
31748b8a9b1dSJustin T. Gibbs 		if (new_priority < old_priority)
31758b8a9b1dSJustin T. Gibbs 			pinfo->priority = new_priority;
31768b8a9b1dSJustin T. Gibbs 
3177aa872be6SMatt Jacob 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
31788b8a9b1dSJustin T. Gibbs 				("Inserting onto queue\n"));
31798bad620dSJustin T. Gibbs 		pinfo->generation = ++queue->generation;
31808b8a9b1dSJustin T. Gibbs 		camq_insert(queue, pinfo);
31818b8a9b1dSJustin T. Gibbs 		retval = 1;
31828b8a9b1dSJustin T. Gibbs 	}
31838b8a9b1dSJustin T. Gibbs 	return (retval);
31848b8a9b1dSJustin T. Gibbs }
31858b8a9b1dSJustin T. Gibbs 
31868b8a9b1dSJustin T. Gibbs static void
3187227d67aaSAlexander Motin xpt_run_allocq_task(void *context, int pending)
31888b8a9b1dSJustin T. Gibbs {
3189227d67aaSAlexander Motin 	struct cam_periph *periph = context;
31908b8a9b1dSJustin T. Gibbs 
3191227d67aaSAlexander Motin 	cam_periph_lock(periph);
3192227d67aaSAlexander Motin 	periph->flags &= ~CAM_PERIPH_RUN_TASK;
3193227d67aaSAlexander Motin 	xpt_run_allocq(periph, 1);
3194227d67aaSAlexander Motin 	cam_periph_unlock(periph);
3195227d67aaSAlexander Motin 	cam_periph_release(periph);
3196227d67aaSAlexander Motin }
3197227d67aaSAlexander Motin 
3198227d67aaSAlexander Motin static void
3199227d67aaSAlexander Motin xpt_run_allocq(struct cam_periph *periph, int sleep)
3200227d67aaSAlexander Motin {
3201227d67aaSAlexander Motin 	struct cam_ed	*device;
3202227d67aaSAlexander Motin 	union ccb	*ccb;
3203227d67aaSAlexander Motin 	uint32_t	 prio;
3204227d67aaSAlexander Motin 
3205227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
3206227d67aaSAlexander Motin 	if (periph->periph_allocating)
3207cccf4220SAlexander Motin 		return;
3208227d67aaSAlexander Motin 	periph->periph_allocating = 1;
3209227d67aaSAlexander Motin 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_allocq(%p)\n", periph));
3210227d67aaSAlexander Motin 	device = periph->path->device;
3211227d67aaSAlexander Motin 	ccb = NULL;
3212227d67aaSAlexander Motin restart:
3213227d67aaSAlexander Motin 	while ((prio = min(periph->scheduled_priority,
3214227d67aaSAlexander Motin 	    periph->immediate_priority)) != CAM_PRIORITY_NONE &&
3215227d67aaSAlexander Motin 	    (periph->periph_allocated - (ccb != NULL ? 1 : 0) <
3216227d67aaSAlexander Motin 	     device->ccbq.total_openings || prio <= CAM_PRIORITY_OOB)) {
3217cccf4220SAlexander Motin 
3218227d67aaSAlexander Motin 		if (ccb == NULL &&
3219227d67aaSAlexander Motin 		    (ccb = xpt_get_ccb_nowait(periph)) == NULL) {
3220227d67aaSAlexander Motin 			if (sleep) {
3221227d67aaSAlexander Motin 				ccb = xpt_get_ccb(periph);
3222227d67aaSAlexander Motin 				goto restart;
3223227d67aaSAlexander Motin 			}
32248ec5ab3fSAlexander Motin 			if (periph->flags & CAM_PERIPH_RUN_TASK)
32258b8a9b1dSJustin T. Gibbs 				break;
3226c33e4029SAlexander Motin 			cam_periph_doacquire(periph);
3227227d67aaSAlexander Motin 			periph->flags |= CAM_PERIPH_RUN_TASK;
3228227d67aaSAlexander Motin 			taskqueue_enqueue(xsoftc.xpt_taskq,
3229227d67aaSAlexander Motin 			    &periph->periph_run_task);
3230227d67aaSAlexander Motin 			break;
32318b8a9b1dSJustin T. Gibbs 		}
3232227d67aaSAlexander Motin 		xpt_setup_ccb(&ccb->ccb_h, periph->path, prio);
3233227d67aaSAlexander Motin 		if (prio == periph->immediate_priority) {
3234227d67aaSAlexander Motin 			periph->immediate_priority = CAM_PRIORITY_NONE;
3235227d67aaSAlexander Motin 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3236227d67aaSAlexander Motin 					("waking cam_periph_getccb()\n"));
3237227d67aaSAlexander Motin 			SLIST_INSERT_HEAD(&periph->ccb_list, &ccb->ccb_h,
3238227d67aaSAlexander Motin 					  periph_links.sle);
3239227d67aaSAlexander Motin 			wakeup(&periph->ccb_list);
3240227d67aaSAlexander Motin 		} else {
3241227d67aaSAlexander Motin 			periph->scheduled_priority = CAM_PRIORITY_NONE;
3242227d67aaSAlexander Motin 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3243227d67aaSAlexander Motin 					("calling periph_start()\n"));
3244227d67aaSAlexander Motin 			periph->periph_start(periph, ccb);
3245227d67aaSAlexander Motin 		}
3246227d67aaSAlexander Motin 		ccb = NULL;
3247227d67aaSAlexander Motin 	}
3248227d67aaSAlexander Motin 	if (ccb != NULL)
3249227d67aaSAlexander Motin 		xpt_release_ccb(ccb);
3250227d67aaSAlexander Motin 	periph->periph_allocating = 0;
32518b8a9b1dSJustin T. Gibbs }
32528b8a9b1dSJustin T. Gibbs 
325383c5d981SAlexander Motin static void
3254cccf4220SAlexander Motin xpt_run_devq(struct cam_devq *devq)
32558b8a9b1dSJustin T. Gibbs {
3256227d67aaSAlexander Motin 	int lock;
32578b8a9b1dSJustin T. Gibbs 
3258cccf4220SAlexander Motin 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
32598b8a9b1dSJustin T. Gibbs 
3260cccf4220SAlexander Motin 	devq->send_queue.qfrozen_cnt++;
32618b8a9b1dSJustin T. Gibbs 	while ((devq->send_queue.entries > 0)
3262ec700f26SAlexander Motin 	    && (devq->send_openings > 0)
3263cccf4220SAlexander Motin 	    && (devq->send_queue.qfrozen_cnt <= 1)) {
32648b8a9b1dSJustin T. Gibbs 		struct	cam_ed *device;
32658b8a9b1dSJustin T. Gibbs 		union ccb *work_ccb;
32668b8a9b1dSJustin T. Gibbs 		struct	cam_sim *sim;
326708f13879SWarner Losh 		struct xpt_proto *proto;
32688b8a9b1dSJustin T. Gibbs 
3269227d67aaSAlexander Motin 		device = (struct cam_ed *)camq_remove(&devq->send_queue,
32705a526431SJustin T. Gibbs 							   CAMQ_HEAD);
3271aa872be6SMatt Jacob 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
327250642f18SKenneth D. Merry 				("running device %p\n", device));
32738b8a9b1dSJustin T. Gibbs 
32745a526431SJustin T. Gibbs 		work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
32758b8a9b1dSJustin T. Gibbs 		if (work_ccb == NULL) {
327657b89bbcSNate Lawson 			printf("device on run queue with no ccbs???\n");
32778b8a9b1dSJustin T. Gibbs 			continue;
32788b8a9b1dSJustin T. Gibbs 		}
32798b8a9b1dSJustin T. Gibbs 
32808b8a9b1dSJustin T. Gibbs 		if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
32818b8a9b1dSJustin T. Gibbs 
3282daa5487fSAlexander Motin 			mtx_lock(&xsoftc.xpt_highpower_lock);
32832b83592fSScott Long 		 	if (xsoftc.num_highpower <= 0) {
32848b8a9b1dSJustin T. Gibbs 				/*
32858b8a9b1dSJustin T. Gibbs 				 * We got a high power command, but we
32868b8a9b1dSJustin T. Gibbs 				 * don't have any available slots.  Freeze
32878b8a9b1dSJustin T. Gibbs 				 * the device queue until we have a slot
32888b8a9b1dSJustin T. Gibbs 				 * available.
32898b8a9b1dSJustin T. Gibbs 				 */
3290daa5487fSAlexander Motin 				xpt_freeze_devq_device(device, 1);
3291227d67aaSAlexander Motin 				STAILQ_INSERT_TAIL(&xsoftc.highpowerq, device,
3292ea541bfdSAlexander Motin 						   highpowerq_entry);
32938b8a9b1dSJustin T. Gibbs 
3294daa5487fSAlexander Motin 				mtx_unlock(&xsoftc.xpt_highpower_lock);
32958b8a9b1dSJustin T. Gibbs 				continue;
32968b8a9b1dSJustin T. Gibbs 			} else {
32978b8a9b1dSJustin T. Gibbs 				/*
32988b8a9b1dSJustin T. Gibbs 				 * Consume a high power slot while
32998b8a9b1dSJustin T. Gibbs 				 * this ccb runs.
33008b8a9b1dSJustin T. Gibbs 				 */
33012b83592fSScott Long 				xsoftc.num_highpower--;
33028b8a9b1dSJustin T. Gibbs 			}
3303daa5487fSAlexander Motin 			mtx_unlock(&xsoftc.xpt_highpower_lock);
33048b8a9b1dSJustin T. Gibbs 		}
33058b8a9b1dSJustin T. Gibbs 		cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
33068b8a9b1dSJustin T. Gibbs 		cam_ccbq_send_ccb(&device->ccbq, work_ccb);
33078b8a9b1dSJustin T. Gibbs 		devq->send_openings--;
33088b8a9b1dSJustin T. Gibbs 		devq->send_active++;
3309cccf4220SAlexander Motin 		xpt_schedule_devq(devq, device);
3310227d67aaSAlexander Motin 		mtx_unlock(&devq->send_mtx);
33118b8a9b1dSJustin T. Gibbs 
3312cccf4220SAlexander Motin 		if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
33138b8a9b1dSJustin T. Gibbs 			/*
33148b8a9b1dSJustin T. Gibbs 			 * The client wants to freeze the queue
33158b8a9b1dSJustin T. Gibbs 			 * after this CCB is sent.
33168b8a9b1dSJustin T. Gibbs 			 */
331783c5d981SAlexander Motin 			xpt_freeze_devq(work_ccb->ccb_h.path, 1);
33188b8a9b1dSJustin T. Gibbs 		}
33198b8a9b1dSJustin T. Gibbs 
3320a4eb4f16SMatt Jacob 		/* In Target mode, the peripheral driver knows best... */
3321a4eb4f16SMatt Jacob 		if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3322a4eb4f16SMatt Jacob 			if ((device->inq_flags & SID_CmdQue) != 0
3323a4eb4f16SMatt Jacob 			 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
33248b8a9b1dSJustin T. Gibbs 				work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
33258b8a9b1dSJustin T. Gibbs 			else
33268b8a9b1dSJustin T. Gibbs 				/*
3327a4eb4f16SMatt Jacob 				 * Clear this in case of a retried CCB that
3328a4eb4f16SMatt Jacob 				 * failed due to a rejected tag.
33298b8a9b1dSJustin T. Gibbs 				 */
33308b8a9b1dSJustin T. Gibbs 				work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3331a4eb4f16SMatt Jacob 		}
33328b8a9b1dSJustin T. Gibbs 
333308f13879SWarner Losh 		KASSERT(device == work_ccb->ccb_h.path->device,
333408f13879SWarner Losh 		    ("device (%p) / path->device (%p) mismatch",
333508f13879SWarner Losh 			device, work_ccb->ccb_h.path->device));
333608f13879SWarner Losh 		proto = xpt_proto_find(device->protocol);
333708f13879SWarner Losh 		if (proto && proto->ops->debug_out)
333808f13879SWarner Losh 			proto->ops->debug_out(work_ccb);
3339de9ebb68SAlexander Motin 
33408b8a9b1dSJustin T. Gibbs 		/*
3341227d67aaSAlexander Motin 		 * Device queues can be shared among multiple SIM instances
3342227d67aaSAlexander Motin 		 * that reside on different busses.  Use the SIM from the
3343227d67aaSAlexander Motin 		 * queued device, rather than the one from the calling bus.
33448b8a9b1dSJustin T. Gibbs 		 */
3345227d67aaSAlexander Motin 		sim = device->sim;
3346227d67aaSAlexander Motin 		lock = (mtx_owned(sim->mtx) == 0);
3347227d67aaSAlexander Motin 		if (lock)
3348227d67aaSAlexander Motin 			CAM_SIM_LOCK(sim);
3349a6e0c5daSWarner Losh 		work_ccb->ccb_h.qos.sim_data = sbinuptime(); // xxx uintprt_t too small 32bit platforms
33508b8a9b1dSJustin T. Gibbs 		(*(sim->sim_action))(sim, work_ccb);
3351227d67aaSAlexander Motin 		if (lock)
3352227d67aaSAlexander Motin 			CAM_SIM_UNLOCK(sim);
3353227d67aaSAlexander Motin 		mtx_lock(&devq->send_mtx);
33548b8a9b1dSJustin T. Gibbs 	}
3355cccf4220SAlexander Motin 	devq->send_queue.qfrozen_cnt--;
33568b8a9b1dSJustin T. Gibbs }
33578b8a9b1dSJustin T. Gibbs 
33588b8a9b1dSJustin T. Gibbs /*
33598b8a9b1dSJustin T. Gibbs  * This function merges stuff from the slave ccb into the master ccb, while
33608b8a9b1dSJustin T. Gibbs  * keeping important fields in the master ccb constant.
33618b8a9b1dSJustin T. Gibbs  */
33628b8a9b1dSJustin T. Gibbs void
33638b8a9b1dSJustin T. Gibbs xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
33648b8a9b1dSJustin T. Gibbs {
336568153f43SScott Long 
33668b8a9b1dSJustin T. Gibbs 	/*
33678b8a9b1dSJustin T. Gibbs 	 * Pull fields that are valid for peripheral drivers to set
33688b8a9b1dSJustin T. Gibbs 	 * into the master CCB along with the CCB "payload".
33698b8a9b1dSJustin T. Gibbs 	 */
33708b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
33718b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
33728b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
33738b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
33748b8a9b1dSJustin T. Gibbs 	bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
33758b8a9b1dSJustin T. Gibbs 	      sizeof(union ccb) - sizeof(struct ccb_hdr));
33768b8a9b1dSJustin T. Gibbs }
33778b8a9b1dSJustin T. Gibbs 
33788b8a9b1dSJustin T. Gibbs void
3379a9934668SKenneth D. Merry xpt_setup_ccb_flags(struct ccb_hdr *ccb_h, struct cam_path *path,
3380a9934668SKenneth D. Merry 		    u_int32_t priority, u_int32_t flags)
33818b8a9b1dSJustin T. Gibbs {
338268153f43SScott Long 
33838b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
33848b8a9b1dSJustin T. Gibbs 	ccb_h->pinfo.priority = priority;
33858b8a9b1dSJustin T. Gibbs 	ccb_h->path = path;
33868b8a9b1dSJustin T. Gibbs 	ccb_h->path_id = path->bus->path_id;
33878b8a9b1dSJustin T. Gibbs 	if (path->target)
33888b8a9b1dSJustin T. Gibbs 		ccb_h->target_id = path->target->target_id;
33898b8a9b1dSJustin T. Gibbs 	else
33908b8a9b1dSJustin T. Gibbs 		ccb_h->target_id = CAM_TARGET_WILDCARD;
33918b8a9b1dSJustin T. Gibbs 	if (path->device) {
33928b8a9b1dSJustin T. Gibbs 		ccb_h->target_lun = path->device->lun_id;
33938bad620dSJustin T. Gibbs 		ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
33948b8a9b1dSJustin T. Gibbs 	} else {
33958b8a9b1dSJustin T. Gibbs 		ccb_h->target_lun = CAM_TARGET_WILDCARD;
33968b8a9b1dSJustin T. Gibbs 	}
33978b8a9b1dSJustin T. Gibbs 	ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3398a9934668SKenneth D. Merry 	ccb_h->flags = flags;
3399b5595753SNathan Whitehorn 	ccb_h->xflags = 0;
34008b8a9b1dSJustin T. Gibbs }
34018b8a9b1dSJustin T. Gibbs 
3402a9934668SKenneth D. Merry void
3403a9934668SKenneth D. Merry xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3404a9934668SKenneth D. Merry {
3405a9934668SKenneth D. Merry 	xpt_setup_ccb_flags(ccb_h, path, priority, /*flags*/ 0);
3406a9934668SKenneth D. Merry }
3407a9934668SKenneth D. Merry 
34088b8a9b1dSJustin T. Gibbs /* Path manipulation functions */
34098b8a9b1dSJustin T. Gibbs cam_status
34108b8a9b1dSJustin T. Gibbs xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
34118b8a9b1dSJustin T. Gibbs 		path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
34128b8a9b1dSJustin T. Gibbs {
34138b8a9b1dSJustin T. Gibbs 	struct	   cam_path *path;
34148b8a9b1dSJustin T. Gibbs 	cam_status status;
34158b8a9b1dSJustin T. Gibbs 
3416596ee08fSAlexander Motin 	path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
34178b8a9b1dSJustin T. Gibbs 
34188b8a9b1dSJustin T. Gibbs 	if (path == NULL) {
34198b8a9b1dSJustin T. Gibbs 		status = CAM_RESRC_UNAVAIL;
34208b8a9b1dSJustin T. Gibbs 		return(status);
34218b8a9b1dSJustin T. Gibbs 	}
34228b8a9b1dSJustin T. Gibbs 	status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
34238b8a9b1dSJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
3424596ee08fSAlexander Motin 		free(path, M_CAMPATH);
34258b8a9b1dSJustin T. Gibbs 		path = NULL;
34268b8a9b1dSJustin T. Gibbs 	}
34278b8a9b1dSJustin T. Gibbs 	*new_path_ptr = path;
34288b8a9b1dSJustin T. Gibbs 	return (status);
34298b8a9b1dSJustin T. Gibbs }
34308b8a9b1dSJustin T. Gibbs 
34312b83592fSScott Long cam_status
34322b83592fSScott Long xpt_create_path_unlocked(struct cam_path **new_path_ptr,
34332b83592fSScott Long 			 struct cam_periph *periph, path_id_t path_id,
34342b83592fSScott Long 			 target_id_t target_id, lun_id_t lun_id)
34352b83592fSScott Long {
34362b83592fSScott Long 
3437227d67aaSAlexander Motin 	return (xpt_create_path(new_path_ptr, periph, path_id, target_id,
3438227d67aaSAlexander Motin 	    lun_id));
34392b83592fSScott Long }
34402b83592fSScott Long 
344152c9ce25SScott Long cam_status
34428b8a9b1dSJustin T. Gibbs xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
34438b8a9b1dSJustin T. Gibbs 		 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
34448b8a9b1dSJustin T. Gibbs {
34458b8a9b1dSJustin T. Gibbs 	struct	     cam_eb *bus;
34468b8a9b1dSJustin T. Gibbs 	struct	     cam_et *target;
34478b8a9b1dSJustin T. Gibbs 	struct	     cam_ed *device;
34488b8a9b1dSJustin T. Gibbs 	cam_status   status;
34498b8a9b1dSJustin T. Gibbs 
34508b8a9b1dSJustin T. Gibbs 	status = CAM_REQ_CMP;	/* Completed without error */
34518b8a9b1dSJustin T. Gibbs 	target = NULL;		/* Wildcarded */
34528b8a9b1dSJustin T. Gibbs 	device = NULL;		/* Wildcarded */
3453a5479bc5SJustin T. Gibbs 
3454a5479bc5SJustin T. Gibbs 	/*
3455a5479bc5SJustin T. Gibbs 	 * We will potentially modify the EDT, so block interrupts
3456a5479bc5SJustin T. Gibbs 	 * that may attempt to create cam paths.
3457a5479bc5SJustin T. Gibbs 	 */
34588b8a9b1dSJustin T. Gibbs 	bus = xpt_find_bus(path_id);
34598b8a9b1dSJustin T. Gibbs 	if (bus == NULL) {
34608b8a9b1dSJustin T. Gibbs 		status = CAM_PATH_INVALID;
3461c8bead2aSJustin T. Gibbs 	} else {
3462227d67aaSAlexander Motin 		xpt_lock_buses();
3463227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
34648b8a9b1dSJustin T. Gibbs 		target = xpt_find_target(bus, target_id);
34658b8a9b1dSJustin T. Gibbs 		if (target == NULL) {
34668b8a9b1dSJustin T. Gibbs 			/* Create one */
34678b8a9b1dSJustin T. Gibbs 			struct cam_et *new_target;
34688b8a9b1dSJustin T. Gibbs 
34698b8a9b1dSJustin T. Gibbs 			new_target = xpt_alloc_target(bus, target_id);
34708b8a9b1dSJustin T. Gibbs 			if (new_target == NULL) {
34718b8a9b1dSJustin T. Gibbs 				status = CAM_RESRC_UNAVAIL;
34728b8a9b1dSJustin T. Gibbs 			} else {
34738b8a9b1dSJustin T. Gibbs 				target = new_target;
34748b8a9b1dSJustin T. Gibbs 			}
34758b8a9b1dSJustin T. Gibbs 		}
3476227d67aaSAlexander Motin 		xpt_unlock_buses();
3477c8bead2aSJustin T. Gibbs 		if (target != NULL) {
34788b8a9b1dSJustin T. Gibbs 			device = xpt_find_device(target, lun_id);
34798b8a9b1dSJustin T. Gibbs 			if (device == NULL) {
34808b8a9b1dSJustin T. Gibbs 				/* Create one */
34818b8a9b1dSJustin T. Gibbs 				struct cam_ed *new_device;
34828b8a9b1dSJustin T. Gibbs 
348352c9ce25SScott Long 				new_device =
3484ded2b706SWarner Losh 				    (*(bus->xport->ops->alloc_device))(bus,
34858b8a9b1dSJustin T. Gibbs 								       target,
34868b8a9b1dSJustin T. Gibbs 								       lun_id);
34878b8a9b1dSJustin T. Gibbs 				if (new_device == NULL) {
34888b8a9b1dSJustin T. Gibbs 					status = CAM_RESRC_UNAVAIL;
34898b8a9b1dSJustin T. Gibbs 				} else {
34908b8a9b1dSJustin T. Gibbs 					device = new_device;
34918b8a9b1dSJustin T. Gibbs 				}
34928b8a9b1dSJustin T. Gibbs 			}
34938b8a9b1dSJustin T. Gibbs 		}
3494227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
34958b8a9b1dSJustin T. Gibbs 	}
34968b8a9b1dSJustin T. Gibbs 
34978b8a9b1dSJustin T. Gibbs 	/*
34988b8a9b1dSJustin T. Gibbs 	 * Only touch the user's data if we are successful.
34998b8a9b1dSJustin T. Gibbs 	 */
35008b8a9b1dSJustin T. Gibbs 	if (status == CAM_REQ_CMP) {
35018b8a9b1dSJustin T. Gibbs 		new_path->periph = perph;
35028b8a9b1dSJustin T. Gibbs 		new_path->bus = bus;
35038b8a9b1dSJustin T. Gibbs 		new_path->target = target;
35048b8a9b1dSJustin T. Gibbs 		new_path->device = device;
35058b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
35068b8a9b1dSJustin T. Gibbs 	} else {
35078b8a9b1dSJustin T. Gibbs 		if (device != NULL)
3508f98d7a47SAlexander Motin 			xpt_release_device(device);
35098b8a9b1dSJustin T. Gibbs 		if (target != NULL)
3510f98d7a47SAlexander Motin 			xpt_release_target(target);
3511a5479bc5SJustin T. Gibbs 		if (bus != NULL)
3512a5479bc5SJustin T. Gibbs 			xpt_release_bus(bus);
35138b8a9b1dSJustin T. Gibbs 	}
35148b8a9b1dSJustin T. Gibbs 	return (status);
35158b8a9b1dSJustin T. Gibbs }
35168b8a9b1dSJustin T. Gibbs 
3517227d67aaSAlexander Motin cam_status
3518227d67aaSAlexander Motin xpt_clone_path(struct cam_path **new_path_ptr, struct cam_path *path)
3519227d67aaSAlexander Motin {
3520227d67aaSAlexander Motin 	struct	   cam_path *new_path;
3521227d67aaSAlexander Motin 
3522227d67aaSAlexander Motin 	new_path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3523227d67aaSAlexander Motin 	if (new_path == NULL)
3524227d67aaSAlexander Motin 		return(CAM_RESRC_UNAVAIL);
3525227d67aaSAlexander Motin 	xpt_copy_path(new_path, path);
3526227d67aaSAlexander Motin 	*new_path_ptr = new_path;
3527227d67aaSAlexander Motin 	return (CAM_REQ_CMP);
3528227d67aaSAlexander Motin }
3529227d67aaSAlexander Motin 
3530227d67aaSAlexander Motin void
3531227d67aaSAlexander Motin xpt_copy_path(struct cam_path *new_path, struct cam_path *path)
3532227d67aaSAlexander Motin {
3533227d67aaSAlexander Motin 
3534227d67aaSAlexander Motin 	*new_path = *path;
3535227d67aaSAlexander Motin 	if (path->bus != NULL)
3536227d67aaSAlexander Motin 		xpt_acquire_bus(path->bus);
3537227d67aaSAlexander Motin 	if (path->target != NULL)
3538227d67aaSAlexander Motin 		xpt_acquire_target(path->target);
3539227d67aaSAlexander Motin 	if (path->device != NULL)
3540227d67aaSAlexander Motin 		xpt_acquire_device(path->device);
3541227d67aaSAlexander Motin }
3542227d67aaSAlexander Motin 
354352c9ce25SScott Long void
35448b8a9b1dSJustin T. Gibbs xpt_release_path(struct cam_path *path)
35458b8a9b1dSJustin T. Gibbs {
35468b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
35479dd03ecfSJustin T. Gibbs 	if (path->device != NULL) {
3548f98d7a47SAlexander Motin 		xpt_release_device(path->device);
35499dd03ecfSJustin T. Gibbs 		path->device = NULL;
35509dd03ecfSJustin T. Gibbs 	}
35519dd03ecfSJustin T. Gibbs 	if (path->target != NULL) {
3552f98d7a47SAlexander Motin 		xpt_release_target(path->target);
35539dd03ecfSJustin T. Gibbs 		path->target = NULL;
35549dd03ecfSJustin T. Gibbs 	}
35559dd03ecfSJustin T. Gibbs 	if (path->bus != NULL) {
35569dd03ecfSJustin T. Gibbs 		xpt_release_bus(path->bus);
35579dd03ecfSJustin T. Gibbs 		path->bus = NULL;
35589dd03ecfSJustin T. Gibbs 	}
35598b8a9b1dSJustin T. Gibbs }
35608b8a9b1dSJustin T. Gibbs 
35618b8a9b1dSJustin T. Gibbs void
35628b8a9b1dSJustin T. Gibbs xpt_free_path(struct cam_path *path)
35638b8a9b1dSJustin T. Gibbs {
356468153f43SScott Long 
35658b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
35668b8a9b1dSJustin T. Gibbs 	xpt_release_path(path);
3567596ee08fSAlexander Motin 	free(path, M_CAMPATH);
35688b8a9b1dSJustin T. Gibbs }
35698b8a9b1dSJustin T. Gibbs 
357015975b7bSMatt Jacob void
357115975b7bSMatt Jacob xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
357215975b7bSMatt Jacob     uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
357315975b7bSMatt Jacob {
357415975b7bSMatt Jacob 
35759a7c2696SAlexander Motin 	xpt_lock_buses();
357615975b7bSMatt Jacob 	if (bus_ref) {
357715975b7bSMatt Jacob 		if (path->bus)
357815975b7bSMatt Jacob 			*bus_ref = path->bus->refcount;
357915975b7bSMatt Jacob 		else
358015975b7bSMatt Jacob 			*bus_ref = 0;
358115975b7bSMatt Jacob 	}
358215975b7bSMatt Jacob 	if (periph_ref) {
358315975b7bSMatt Jacob 		if (path->periph)
358415975b7bSMatt Jacob 			*periph_ref = path->periph->refcount;
358515975b7bSMatt Jacob 		else
358615975b7bSMatt Jacob 			*periph_ref = 0;
358715975b7bSMatt Jacob 	}
3588dcdf6e74SAlexander Motin 	xpt_unlock_buses();
358915975b7bSMatt Jacob 	if (target_ref) {
359015975b7bSMatt Jacob 		if (path->target)
359115975b7bSMatt Jacob 			*target_ref = path->target->refcount;
359215975b7bSMatt Jacob 		else
359315975b7bSMatt Jacob 			*target_ref = 0;
359415975b7bSMatt Jacob 	}
359515975b7bSMatt Jacob 	if (device_ref) {
359615975b7bSMatt Jacob 		if (path->device)
359715975b7bSMatt Jacob 			*device_ref = path->device->refcount;
359815975b7bSMatt Jacob 		else
359915975b7bSMatt Jacob 			*device_ref = 0;
360015975b7bSMatt Jacob 	}
360115975b7bSMatt Jacob }
36028b8a9b1dSJustin T. Gibbs 
36038b8a9b1dSJustin T. Gibbs /*
36042cefde5fSJustin T. Gibbs  * Return -1 for failure, 0 for exact match, 1 for match with wildcards
36052cefde5fSJustin T. Gibbs  * in path1, 2 for match with wildcards in path2.
36068b8a9b1dSJustin T. Gibbs  */
36078b8a9b1dSJustin T. Gibbs int
36088b8a9b1dSJustin T. Gibbs xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
36098b8a9b1dSJustin T. Gibbs {
36108b8a9b1dSJustin T. Gibbs 	int retval = 0;
36118b8a9b1dSJustin T. Gibbs 
36128b8a9b1dSJustin T. Gibbs 	if (path1->bus != path2->bus) {
36132cefde5fSJustin T. Gibbs 		if (path1->bus->path_id == CAM_BUS_WILDCARD)
36148b8a9b1dSJustin T. Gibbs 			retval = 1;
36152cefde5fSJustin T. Gibbs 		else if (path2->bus->path_id == CAM_BUS_WILDCARD)
36162cefde5fSJustin T. Gibbs 			retval = 2;
36178b8a9b1dSJustin T. Gibbs 		else
36188b8a9b1dSJustin T. Gibbs 			return (-1);
36198b8a9b1dSJustin T. Gibbs 	}
36208b8a9b1dSJustin T. Gibbs 	if (path1->target != path2->target) {
36212cefde5fSJustin T. Gibbs 		if (path1->target->target_id == CAM_TARGET_WILDCARD) {
36222cefde5fSJustin T. Gibbs 			if (retval == 0)
36238b8a9b1dSJustin T. Gibbs 				retval = 1;
36242cefde5fSJustin T. Gibbs 		} else if (path2->target->target_id == CAM_TARGET_WILDCARD)
36252cefde5fSJustin T. Gibbs 			retval = 2;
36268b8a9b1dSJustin T. Gibbs 		else
36278b8a9b1dSJustin T. Gibbs 			return (-1);
36288b8a9b1dSJustin T. Gibbs 	}
36298b8a9b1dSJustin T. Gibbs 	if (path1->device != path2->device) {
36302cefde5fSJustin T. Gibbs 		if (path1->device->lun_id == CAM_LUN_WILDCARD) {
36312cefde5fSJustin T. Gibbs 			if (retval == 0)
36328b8a9b1dSJustin T. Gibbs 				retval = 1;
36332cefde5fSJustin T. Gibbs 		} else if (path2->device->lun_id == CAM_LUN_WILDCARD)
36342cefde5fSJustin T. Gibbs 			retval = 2;
36358b8a9b1dSJustin T. Gibbs 		else
36368b8a9b1dSJustin T. Gibbs 			return (-1);
36378b8a9b1dSJustin T. Gibbs 	}
36388b8a9b1dSJustin T. Gibbs 	return (retval);
36398b8a9b1dSJustin T. Gibbs }
36408b8a9b1dSJustin T. Gibbs 
36410d4f3c31SAlexander Motin int
36420d4f3c31SAlexander Motin xpt_path_comp_dev(struct cam_path *path, struct cam_ed *dev)
36430d4f3c31SAlexander Motin {
36440d4f3c31SAlexander Motin 	int retval = 0;
36450d4f3c31SAlexander Motin 
36460d4f3c31SAlexander Motin 	if (path->bus != dev->target->bus) {
36470d4f3c31SAlexander Motin 		if (path->bus->path_id == CAM_BUS_WILDCARD)
36480d4f3c31SAlexander Motin 			retval = 1;
36490d4f3c31SAlexander Motin 		else if (dev->target->bus->path_id == CAM_BUS_WILDCARD)
36500d4f3c31SAlexander Motin 			retval = 2;
36510d4f3c31SAlexander Motin 		else
36520d4f3c31SAlexander Motin 			return (-1);
36530d4f3c31SAlexander Motin 	}
36540d4f3c31SAlexander Motin 	if (path->target != dev->target) {
36550d4f3c31SAlexander Motin 		if (path->target->target_id == CAM_TARGET_WILDCARD) {
36560d4f3c31SAlexander Motin 			if (retval == 0)
36570d4f3c31SAlexander Motin 				retval = 1;
36580d4f3c31SAlexander Motin 		} else if (dev->target->target_id == CAM_TARGET_WILDCARD)
36590d4f3c31SAlexander Motin 			retval = 2;
36600d4f3c31SAlexander Motin 		else
36610d4f3c31SAlexander Motin 			return (-1);
36620d4f3c31SAlexander Motin 	}
36630d4f3c31SAlexander Motin 	if (path->device != dev) {
36640d4f3c31SAlexander Motin 		if (path->device->lun_id == CAM_LUN_WILDCARD) {
36650d4f3c31SAlexander Motin 			if (retval == 0)
36660d4f3c31SAlexander Motin 				retval = 1;
36670d4f3c31SAlexander Motin 		} else if (dev->lun_id == CAM_LUN_WILDCARD)
36680d4f3c31SAlexander Motin 			retval = 2;
36690d4f3c31SAlexander Motin 		else
36700d4f3c31SAlexander Motin 			return (-1);
36710d4f3c31SAlexander Motin 	}
36720d4f3c31SAlexander Motin 	return (retval);
36730d4f3c31SAlexander Motin }
36740d4f3c31SAlexander Motin 
36758b8a9b1dSJustin T. Gibbs void
36768b8a9b1dSJustin T. Gibbs xpt_print_path(struct cam_path *path)
36778b8a9b1dSJustin T. Gibbs {
367868153f43SScott Long 
36798b8a9b1dSJustin T. Gibbs 	if (path == NULL)
36808b8a9b1dSJustin T. Gibbs 		printf("(nopath): ");
36818b8a9b1dSJustin T. Gibbs 	else {
36828b8a9b1dSJustin T. Gibbs 		if (path->periph != NULL)
36838b8a9b1dSJustin T. Gibbs 			printf("(%s%d:", path->periph->periph_name,
36848b8a9b1dSJustin T. Gibbs 			       path->periph->unit_number);
36858b8a9b1dSJustin T. Gibbs 		else
36868b8a9b1dSJustin T. Gibbs 			printf("(noperiph:");
36878b8a9b1dSJustin T. Gibbs 
36888b8a9b1dSJustin T. Gibbs 		if (path->bus != NULL)
36898b8a9b1dSJustin T. Gibbs 			printf("%s%d:%d:", path->bus->sim->sim_name,
36908b8a9b1dSJustin T. Gibbs 			       path->bus->sim->unit_number,
36918b8a9b1dSJustin T. Gibbs 			       path->bus->sim->bus_id);
36928b8a9b1dSJustin T. Gibbs 		else
36938b8a9b1dSJustin T. Gibbs 			printf("nobus:");
36948b8a9b1dSJustin T. Gibbs 
36958b8a9b1dSJustin T. Gibbs 		if (path->target != NULL)
36968b8a9b1dSJustin T. Gibbs 			printf("%d:", path->target->target_id);
36978b8a9b1dSJustin T. Gibbs 		else
36988b8a9b1dSJustin T. Gibbs 			printf("X:");
36998b8a9b1dSJustin T. Gibbs 
37008b8a9b1dSJustin T. Gibbs 		if (path->device != NULL)
3701abe83505SNathan Whitehorn 			printf("%jx): ", (uintmax_t)path->device->lun_id);
37028b8a9b1dSJustin T. Gibbs 		else
37038b8a9b1dSJustin T. Gibbs 			printf("X): ");
37048b8a9b1dSJustin T. Gibbs 	}
37058b8a9b1dSJustin T. Gibbs }
37068b8a9b1dSJustin T. Gibbs 
3707f0d9af51SMatt Jacob void
37080d4f3c31SAlexander Motin xpt_print_device(struct cam_ed *device)
37090d4f3c31SAlexander Motin {
37100d4f3c31SAlexander Motin 
37110d4f3c31SAlexander Motin 	if (device == NULL)
37120d4f3c31SAlexander Motin 		printf("(nopath): ");
37130d4f3c31SAlexander Motin 	else {
3714abe83505SNathan Whitehorn 		printf("(noperiph:%s%d:%d:%d:%jx): ", device->sim->sim_name,
37150d4f3c31SAlexander Motin 		       device->sim->unit_number,
37160d4f3c31SAlexander Motin 		       device->sim->bus_id,
37170d4f3c31SAlexander Motin 		       device->target->target_id,
3718abe83505SNathan Whitehorn 		       (uintmax_t)device->lun_id);
37190d4f3c31SAlexander Motin 	}
37200d4f3c31SAlexander Motin }
37210d4f3c31SAlexander Motin 
37220d4f3c31SAlexander Motin void
3723f0d9af51SMatt Jacob xpt_print(struct cam_path *path, const char *fmt, ...)
3724f0d9af51SMatt Jacob {
3725f0d9af51SMatt Jacob 	va_list ap;
3726f0d9af51SMatt Jacob 	xpt_print_path(path);
3727f0d9af51SMatt Jacob 	va_start(ap, fmt);
3728f0d9af51SMatt Jacob 	vprintf(fmt, ap);
3729f0d9af51SMatt Jacob 	va_end(ap);
3730f0d9af51SMatt Jacob }
3731f0d9af51SMatt Jacob 
37323393f8daSKenneth D. Merry int
37333393f8daSKenneth D. Merry xpt_path_string(struct cam_path *path, char *str, size_t str_len)
37343393f8daSKenneth D. Merry {
37353393f8daSKenneth D. Merry 	struct sbuf sb;
37363393f8daSKenneth D. Merry 
37373393f8daSKenneth D. Merry 	sbuf_new(&sb, str, str_len, 0);
37383393f8daSKenneth D. Merry 
37393393f8daSKenneth D. Merry 	if (path == NULL)
37403393f8daSKenneth D. Merry 		sbuf_printf(&sb, "(nopath): ");
37413393f8daSKenneth D. Merry 	else {
37423393f8daSKenneth D. Merry 		if (path->periph != NULL)
37433393f8daSKenneth D. Merry 			sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
37443393f8daSKenneth D. Merry 				    path->periph->unit_number);
37453393f8daSKenneth D. Merry 		else
37463393f8daSKenneth D. Merry 			sbuf_printf(&sb, "(noperiph:");
37473393f8daSKenneth D. Merry 
37483393f8daSKenneth D. Merry 		if (path->bus != NULL)
37493393f8daSKenneth D. Merry 			sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
37503393f8daSKenneth D. Merry 				    path->bus->sim->unit_number,
37513393f8daSKenneth D. Merry 				    path->bus->sim->bus_id);
37523393f8daSKenneth D. Merry 		else
37533393f8daSKenneth D. Merry 			sbuf_printf(&sb, "nobus:");
37543393f8daSKenneth D. Merry 
37553393f8daSKenneth D. Merry 		if (path->target != NULL)
37563393f8daSKenneth D. Merry 			sbuf_printf(&sb, "%d:", path->target->target_id);
37573393f8daSKenneth D. Merry 		else
37583393f8daSKenneth D. Merry 			sbuf_printf(&sb, "X:");
37593393f8daSKenneth D. Merry 
37603393f8daSKenneth D. Merry 		if (path->device != NULL)
3761abe83505SNathan Whitehorn 			sbuf_printf(&sb, "%jx): ",
3762abe83505SNathan Whitehorn 			    (uintmax_t)path->device->lun_id);
37633393f8daSKenneth D. Merry 		else
37643393f8daSKenneth D. Merry 			sbuf_printf(&sb, "X): ");
37653393f8daSKenneth D. Merry 	}
37663393f8daSKenneth D. Merry 	sbuf_finish(&sb);
37673393f8daSKenneth D. Merry 
37683393f8daSKenneth D. Merry 	return(sbuf_len(&sb));
37693393f8daSKenneth D. Merry }
37703393f8daSKenneth D. Merry 
37718b8a9b1dSJustin T. Gibbs path_id_t
37728b8a9b1dSJustin T. Gibbs xpt_path_path_id(struct cam_path *path)
37738b8a9b1dSJustin T. Gibbs {
37748b8a9b1dSJustin T. Gibbs 	return(path->bus->path_id);
37758b8a9b1dSJustin T. Gibbs }
37768b8a9b1dSJustin T. Gibbs 
37778b8a9b1dSJustin T. Gibbs target_id_t
37788b8a9b1dSJustin T. Gibbs xpt_path_target_id(struct cam_path *path)
37798b8a9b1dSJustin T. Gibbs {
37808b8a9b1dSJustin T. Gibbs 	if (path->target != NULL)
37818b8a9b1dSJustin T. Gibbs 		return (path->target->target_id);
37828b8a9b1dSJustin T. Gibbs 	else
37838b8a9b1dSJustin T. Gibbs 		return (CAM_TARGET_WILDCARD);
37848b8a9b1dSJustin T. Gibbs }
37858b8a9b1dSJustin T. Gibbs 
37868b8a9b1dSJustin T. Gibbs lun_id_t
37878b8a9b1dSJustin T. Gibbs xpt_path_lun_id(struct cam_path *path)
37888b8a9b1dSJustin T. Gibbs {
37898b8a9b1dSJustin T. Gibbs 	if (path->device != NULL)
37908b8a9b1dSJustin T. Gibbs 		return (path->device->lun_id);
37918b8a9b1dSJustin T. Gibbs 	else
37928b8a9b1dSJustin T. Gibbs 		return (CAM_LUN_WILDCARD);
37938b8a9b1dSJustin T. Gibbs }
37948b8a9b1dSJustin T. Gibbs 
37958b8a9b1dSJustin T. Gibbs struct cam_sim *
37968b8a9b1dSJustin T. Gibbs xpt_path_sim(struct cam_path *path)
37978b8a9b1dSJustin T. Gibbs {
379868153f43SScott Long 
37998b8a9b1dSJustin T. Gibbs 	return (path->bus->sim);
38008b8a9b1dSJustin T. Gibbs }
38018b8a9b1dSJustin T. Gibbs 
38028b8a9b1dSJustin T. Gibbs struct cam_periph*
38038b8a9b1dSJustin T. Gibbs xpt_path_periph(struct cam_path *path)
38048b8a9b1dSJustin T. Gibbs {
380568153f43SScott Long 
38068b8a9b1dSJustin T. Gibbs 	return (path->periph);
38078b8a9b1dSJustin T. Gibbs }
38088b8a9b1dSJustin T. Gibbs 
38098b8a9b1dSJustin T. Gibbs /*
38108b8a9b1dSJustin T. Gibbs  * Release a CAM control block for the caller.  Remit the cost of the structure
38118b8a9b1dSJustin T. Gibbs  * to the device referenced by the path.  If the this device had no 'credits'
38128b8a9b1dSJustin T. Gibbs  * and peripheral drivers have registered async callbacks for this notification
38138b8a9b1dSJustin T. Gibbs  * call them now.
38148b8a9b1dSJustin T. Gibbs  */
38158b8a9b1dSJustin T. Gibbs void
38168b8a9b1dSJustin T. Gibbs xpt_release_ccb(union ccb *free_ccb)
38178b8a9b1dSJustin T. Gibbs {
38188b8a9b1dSJustin T. Gibbs 	struct	 cam_ed *device;
3819227d67aaSAlexander Motin 	struct	 cam_periph *periph;
382068153f43SScott Long 
3821aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3822227d67aaSAlexander Motin 	xpt_path_assert(free_ccb->ccb_h.path, MA_OWNED);
3823227d67aaSAlexander Motin 	device = free_ccb->ccb_h.path->device;
3824227d67aaSAlexander Motin 	periph = free_ccb->ccb_h.path->periph;
38252b83592fSScott Long 
38268b8a9b1dSJustin T. Gibbs 	xpt_free_ccb(free_ccb);
3827227d67aaSAlexander Motin 	periph->periph_allocated--;
3828227d67aaSAlexander Motin 	cam_ccbq_release_opening(&device->ccbq);
3829227d67aaSAlexander Motin 	xpt_run_allocq(periph, 0);
38308b8a9b1dSJustin T. Gibbs }
38318b8a9b1dSJustin T. Gibbs 
38328b8a9b1dSJustin T. Gibbs /* Functions accessed by SIM drivers */
38338b8a9b1dSJustin T. Gibbs 
3834ded2b706SWarner Losh static struct xpt_xport_ops xport_default_ops = {
383552c9ce25SScott Long 	.alloc_device = xpt_alloc_device_default,
383652c9ce25SScott Long 	.action = xpt_action_default,
383752c9ce25SScott Long 	.async = xpt_dev_async_default,
383852c9ce25SScott Long };
3839ded2b706SWarner Losh static struct xpt_xport xport_default = {
3840ded2b706SWarner Losh 	.xport = XPORT_UNKNOWN,
3841ded2b706SWarner Losh 	.name = "unknown",
3842ded2b706SWarner Losh 	.ops = &xport_default_ops,
3843ded2b706SWarner Losh };
3844ded2b706SWarner Losh 
3845ded2b706SWarner Losh CAM_XPT_XPORT(xport_default);
384652c9ce25SScott Long 
38478b8a9b1dSJustin T. Gibbs /*
38488b8a9b1dSJustin T. Gibbs  * A sim structure, listing the SIM entry points and instance
38498b8a9b1dSJustin T. Gibbs  * identification info is passed to xpt_bus_register to hook the SIM
38508b8a9b1dSJustin T. Gibbs  * into the CAM framework.  xpt_bus_register creates a cam_eb entry
38518b8a9b1dSJustin T. Gibbs  * for this new bus and places it in the array of busses and assigns
38528b8a9b1dSJustin T. Gibbs  * it a path_id.  The path_id may be influenced by "hard wiring"
38538b8a9b1dSJustin T. Gibbs  * information specified by the user.  Once interrupt services are
385402caf36eSEdward Tomasz Napierala  * available, the bus will be probed.
38558b8a9b1dSJustin T. Gibbs  */
38568b8a9b1dSJustin T. Gibbs int32_t
3857b50569b7SScott Long xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
38588b8a9b1dSJustin T. Gibbs {
38598b8a9b1dSJustin T. Gibbs 	struct cam_eb *new_bus;
3860434bbf6eSJustin T. Gibbs 	struct cam_eb *old_bus;
38618b8a9b1dSJustin T. Gibbs 	struct ccb_pathinq cpi;
386283c5d981SAlexander Motin 	struct cam_path *path;
386352c9ce25SScott Long 	cam_status status;
38648b8a9b1dSJustin T. Gibbs 
38652b83592fSScott Long 	mtx_assert(sim->mtx, MA_OWNED);
386668153f43SScott Long 
38678b8a9b1dSJustin T. Gibbs 	sim->bus_id = bus;
38688b8a9b1dSJustin T. Gibbs 	new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3869227d67aaSAlexander Motin 					  M_CAMXPT, M_NOWAIT|M_ZERO);
38708b8a9b1dSJustin T. Gibbs 	if (new_bus == NULL) {
38718b8a9b1dSJustin T. Gibbs 		/* Couldn't satisfy request */
38728b8a9b1dSJustin T. Gibbs 		return (CAM_RESRC_UNAVAIL);
38738b8a9b1dSJustin T. Gibbs 	}
38748b8a9b1dSJustin T. Gibbs 
3875227d67aaSAlexander Motin 	mtx_init(&new_bus->eb_mtx, "CAM bus lock", NULL, MTX_DEF);
3876434bbf6eSJustin T. Gibbs 	TAILQ_INIT(&new_bus->et_entries);
3877fa6099fdSEdward Tomasz Napierala 	cam_sim_hold(sim);
38788b8a9b1dSJustin T. Gibbs 	new_bus->sim = sim;
387987cfaf0eSJustin T. Gibbs 	timevalclear(&new_bus->last_reset);
3880434bbf6eSJustin T. Gibbs 	new_bus->flags = 0;
3881a5479bc5SJustin T. Gibbs 	new_bus->refcount = 1;	/* Held until a bus_deregister event */
3882434bbf6eSJustin T. Gibbs 	new_bus->generation = 0;
388352c9ce25SScott Long 
38849a7c2696SAlexander Motin 	xpt_lock_buses();
38856dfc67e3SAlexander Motin 	sim->path_id = new_bus->path_id =
38866dfc67e3SAlexander Motin 	    xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
38872b83592fSScott Long 	old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3888434bbf6eSJustin T. Gibbs 	while (old_bus != NULL
3889434bbf6eSJustin T. Gibbs 	    && old_bus->path_id < new_bus->path_id)
3890434bbf6eSJustin T. Gibbs 		old_bus = TAILQ_NEXT(old_bus, links);
3891434bbf6eSJustin T. Gibbs 	if (old_bus != NULL)
3892434bbf6eSJustin T. Gibbs 		TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3893434bbf6eSJustin T. Gibbs 	else
38942b83592fSScott Long 		TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
38952b83592fSScott Long 	xsoftc.bus_generation++;
38969a7c2696SAlexander Motin 	xpt_unlock_buses();
38978b8a9b1dSJustin T. Gibbs 
389852c9ce25SScott Long 	/*
389952c9ce25SScott Long 	 * Set a default transport so that a PATH_INQ can be issued to
390052c9ce25SScott Long 	 * the SIM.  This will then allow for probing and attaching of
390152c9ce25SScott Long 	 * a more appropriate transport.
390252c9ce25SScott Long 	 */
390352c9ce25SScott Long 	new_bus->xport = &xport_default;
39048b8a9b1dSJustin T. Gibbs 
390532aa80a6SAlexander Motin 	status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
39068b8a9b1dSJustin T. Gibbs 				  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3907627995dcSAlexander Motin 	if (status != CAM_REQ_CMP) {
3908627995dcSAlexander Motin 		xpt_release_bus(new_bus);
3909627995dcSAlexander Motin 		free(path, M_CAMXPT);
3910627995dcSAlexander Motin 		return (CAM_RESRC_UNAVAIL);
3911627995dcSAlexander Motin 	}
391252c9ce25SScott Long 
391383c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
39148b8a9b1dSJustin T. Gibbs 	cpi.ccb_h.func_code = XPT_PATH_INQ;
39158b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&cpi);
391652c9ce25SScott Long 
391752c9ce25SScott Long 	if (cpi.ccb_h.status == CAM_REQ_CMP) {
3918ded2b706SWarner Losh 		struct xpt_xport **xpt;
3919ded2b706SWarner Losh 
3920ded2b706SWarner Losh 		SET_FOREACH(xpt, cam_xpt_xport_set) {
3921ded2b706SWarner Losh 			if ((*xpt)->xport == cpi.transport) {
3922ded2b706SWarner Losh 				new_bus->xport = *xpt;
392352c9ce25SScott Long 				break;
3924ded2b706SWarner Losh 			}
3925ded2b706SWarner Losh 		}
3926ded2b706SWarner Losh 		if (new_bus->xport == NULL) {
3927ded2b706SWarner Losh 			xpt_print_path(path);
3928ded2b706SWarner Losh 			printf("No transport found for %d\n", cpi.transport);
3929ded2b706SWarner Losh 			xpt_release_bus(new_bus);
3930ded2b706SWarner Losh 			free(path, M_CAMXPT);
3931ded2b706SWarner Losh 			return (CAM_RESRC_UNAVAIL);
39328b8a9b1dSJustin T. Gibbs 		}
393352c9ce25SScott Long 	}
393452c9ce25SScott Long 
393552c9ce25SScott Long 	/* Notify interested parties */
393652c9ce25SScott Long 	if (sim->path_id != CAM_XPT_PATH_ID) {
393783c5d981SAlexander Motin 
393883c5d981SAlexander Motin 		xpt_async(AC_PATH_REGISTERED, path, &cpi);
3939b01773b0SKenneth D. Merry 		if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
3940b01773b0SKenneth D. Merry 			union	ccb *scan_ccb;
3941b01773b0SKenneth D. Merry 
394283c5d981SAlexander Motin 			/* Initiate bus rescan. */
394383c5d981SAlexander Motin 			scan_ccb = xpt_alloc_ccb_nowait();
3944e5736ac8SAlexander Motin 			if (scan_ccb != NULL) {
394583c5d981SAlexander Motin 				scan_ccb->ccb_h.path = path;
394683c5d981SAlexander Motin 				scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
394783c5d981SAlexander Motin 				scan_ccb->crcn.flags = 0;
394883c5d981SAlexander Motin 				xpt_rescan(scan_ccb);
39497f7aacb4SAlexander Motin 			} else {
3950b01773b0SKenneth D. Merry 				xpt_print(path,
3951b01773b0SKenneth D. Merry 					  "Can't allocate CCB to scan bus\n");
39527f7aacb4SAlexander Motin 				xpt_free_path(path);
39537f7aacb4SAlexander Motin 			}
3954b01773b0SKenneth D. Merry 		} else
3955b01773b0SKenneth D. Merry 			xpt_free_path(path);
3956e5736ac8SAlexander Motin 	} else
395783c5d981SAlexander Motin 		xpt_free_path(path);
39588b8a9b1dSJustin T. Gibbs 	return (CAM_SUCCESS);
39598b8a9b1dSJustin T. Gibbs }
39608b8a9b1dSJustin T. Gibbs 
3961434bbf6eSJustin T. Gibbs int32_t
3962434bbf6eSJustin T. Gibbs xpt_bus_deregister(path_id_t pathid)
39638b8a9b1dSJustin T. Gibbs {
3964434bbf6eSJustin T. Gibbs 	struct cam_path bus_path;
3965434bbf6eSJustin T. Gibbs 	cam_status status;
3966434bbf6eSJustin T. Gibbs 
3967434bbf6eSJustin T. Gibbs 	status = xpt_compile_path(&bus_path, NULL, pathid,
3968434bbf6eSJustin T. Gibbs 				  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3969434bbf6eSJustin T. Gibbs 	if (status != CAM_REQ_CMP)
3970434bbf6eSJustin T. Gibbs 		return (status);
3971434bbf6eSJustin T. Gibbs 
3972434bbf6eSJustin T. Gibbs 	xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3973434bbf6eSJustin T. Gibbs 	xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3974434bbf6eSJustin T. Gibbs 
3975434bbf6eSJustin T. Gibbs 	/* Release the reference count held while registered. */
3976434bbf6eSJustin T. Gibbs 	xpt_release_bus(bus_path.bus);
3977434bbf6eSJustin T. Gibbs 	xpt_release_path(&bus_path);
3978434bbf6eSJustin T. Gibbs 
3979434bbf6eSJustin T. Gibbs 	return (CAM_REQ_CMP);
3980434bbf6eSJustin T. Gibbs }
3981434bbf6eSJustin T. Gibbs 
3982434bbf6eSJustin T. Gibbs static path_id_t
3983434bbf6eSJustin T. Gibbs xptnextfreepathid(void)
3984434bbf6eSJustin T. Gibbs {
3985434bbf6eSJustin T. Gibbs 	struct cam_eb *bus;
3986434bbf6eSJustin T. Gibbs 	path_id_t pathid;
39872398f0cdSPeter Wemm 	const char *strval;
39888b8a9b1dSJustin T. Gibbs 
39896dfc67e3SAlexander Motin 	mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
3990434bbf6eSJustin T. Gibbs 	pathid = 0;
39912b83592fSScott Long 	bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3992434bbf6eSJustin T. Gibbs retry:
3993434bbf6eSJustin T. Gibbs 	/* Find an unoccupied pathid */
39949e6461a2SMatt Jacob 	while (bus != NULL && bus->path_id <= pathid) {
3995434bbf6eSJustin T. Gibbs 		if (bus->path_id == pathid)
3996434bbf6eSJustin T. Gibbs 			pathid++;
3997434bbf6eSJustin T. Gibbs 		bus = TAILQ_NEXT(bus, links);
3998434bbf6eSJustin T. Gibbs 	}
3999434bbf6eSJustin T. Gibbs 
4000434bbf6eSJustin T. Gibbs 	/*
4001434bbf6eSJustin T. Gibbs 	 * Ensure that this pathid is not reserved for
4002434bbf6eSJustin T. Gibbs 	 * a bus that may be registered in the future.
4003434bbf6eSJustin T. Gibbs 	 */
400475f51904SPeter Wemm 	if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4005434bbf6eSJustin T. Gibbs 		++pathid;
40068b8a9b1dSJustin T. Gibbs 		/* Start the search over */
4007434bbf6eSJustin T. Gibbs 		goto retry;
40088b8a9b1dSJustin T. Gibbs 	}
4009434bbf6eSJustin T. Gibbs 	return (pathid);
40108b8a9b1dSJustin T. Gibbs }
40118b8a9b1dSJustin T. Gibbs 
4012434bbf6eSJustin T. Gibbs static path_id_t
4013434bbf6eSJustin T. Gibbs xptpathid(const char *sim_name, int sim_unit, int sim_bus)
40148b8a9b1dSJustin T. Gibbs {
40158b8a9b1dSJustin T. Gibbs 	path_id_t pathid;
401675f51904SPeter Wemm 	int i, dunit, val;
4017642f0c46SPeter Wemm 	char buf[32];
40182398f0cdSPeter Wemm 	const char *dname;
40198b8a9b1dSJustin T. Gibbs 
40208b8a9b1dSJustin T. Gibbs 	pathid = CAM_XPT_PATH_ID;
402175f51904SPeter Wemm 	snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
40226dfc67e3SAlexander Motin 	if (strcmp(buf, "xpt0") == 0 && sim_bus == 0)
40236dfc67e3SAlexander Motin 		return (pathid);
40242398f0cdSPeter Wemm 	i = 0;
40252398f0cdSPeter Wemm 	while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
40262398f0cdSPeter Wemm 		if (strcmp(dname, "scbus")) {
4027642f0c46SPeter Wemm 			/* Avoid a bit of foot shooting. */
4028642f0c46SPeter Wemm 			continue;
4029642f0c46SPeter Wemm 		}
403075f51904SPeter Wemm 		if (dunit < 0)		/* unwired?! */
40318b8a9b1dSJustin T. Gibbs 			continue;
403275f51904SPeter Wemm 		if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
403375f51904SPeter Wemm 			if (sim_bus == val) {
403475f51904SPeter Wemm 				pathid = dunit;
40358b8a9b1dSJustin T. Gibbs 				break;
40368b8a9b1dSJustin T. Gibbs 			}
40378b8a9b1dSJustin T. Gibbs 		} else if (sim_bus == 0) {
40388b8a9b1dSJustin T. Gibbs 			/* Unspecified matches bus 0 */
403975f51904SPeter Wemm 			pathid = dunit;
40408b8a9b1dSJustin T. Gibbs 			break;
40418b8a9b1dSJustin T. Gibbs 		} else {
40428b8a9b1dSJustin T. Gibbs 			printf("Ambiguous scbus configuration for %s%d "
40438b8a9b1dSJustin T. Gibbs 			       "bus %d, cannot wire down.  The kernel "
40448b8a9b1dSJustin T. Gibbs 			       "config entry for scbus%d should "
40458b8a9b1dSJustin T. Gibbs 			       "specify a controller bus.\n"
40468b8a9b1dSJustin T. Gibbs 			       "Scbus will be assigned dynamically.\n",
404775f51904SPeter Wemm 			       sim_name, sim_unit, sim_bus, dunit);
40488b8a9b1dSJustin T. Gibbs 			break;
40498b8a9b1dSJustin T. Gibbs 		}
40508b8a9b1dSJustin T. Gibbs 	}
40518b8a9b1dSJustin T. Gibbs 
4052434bbf6eSJustin T. Gibbs 	if (pathid == CAM_XPT_PATH_ID)
4053434bbf6eSJustin T. Gibbs 		pathid = xptnextfreepathid();
40548b8a9b1dSJustin T. Gibbs 	return (pathid);
40558b8a9b1dSJustin T. Gibbs }
40568b8a9b1dSJustin T. Gibbs 
405722c7d606SAlexander Motin static const char *
405822c7d606SAlexander Motin xpt_async_string(u_int32_t async_code)
405922c7d606SAlexander Motin {
406022c7d606SAlexander Motin 
406122c7d606SAlexander Motin 	switch (async_code) {
406222c7d606SAlexander Motin 	case AC_BUS_RESET: return ("AC_BUS_RESET");
406322c7d606SAlexander Motin 	case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
406422c7d606SAlexander Motin 	case AC_SCSI_AEN: return ("AC_SCSI_AEN");
406522c7d606SAlexander Motin 	case AC_SENT_BDR: return ("AC_SENT_BDR");
406622c7d606SAlexander Motin 	case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
406722c7d606SAlexander Motin 	case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
406822c7d606SAlexander Motin 	case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
406922c7d606SAlexander Motin 	case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
407022c7d606SAlexander Motin 	case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
407122c7d606SAlexander Motin 	case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
407222c7d606SAlexander Motin 	case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
407322c7d606SAlexander Motin 	case AC_CONTRACT: return ("AC_CONTRACT");
407422c7d606SAlexander Motin 	case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
40753631c638SAlexander Motin 	case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
407622c7d606SAlexander Motin 	}
407722c7d606SAlexander Motin 	return ("AC_UNKNOWN");
407822c7d606SAlexander Motin }
407922c7d606SAlexander Motin 
4080227d67aaSAlexander Motin static int
4081227d67aaSAlexander Motin xpt_async_size(u_int32_t async_code)
40828b8a9b1dSJustin T. Gibbs {
40838b8a9b1dSJustin T. Gibbs 
4084227d67aaSAlexander Motin 	switch (async_code) {
4085227d67aaSAlexander Motin 	case AC_BUS_RESET: return (0);
4086227d67aaSAlexander Motin 	case AC_UNSOL_RESEL: return (0);
4087227d67aaSAlexander Motin 	case AC_SCSI_AEN: return (0);
4088227d67aaSAlexander Motin 	case AC_SENT_BDR: return (0);
4089227d67aaSAlexander Motin 	case AC_PATH_REGISTERED: return (sizeof(struct ccb_pathinq));
4090227d67aaSAlexander Motin 	case AC_PATH_DEREGISTERED: return (0);
4091227d67aaSAlexander Motin 	case AC_FOUND_DEVICE: return (sizeof(struct ccb_getdev));
4092227d67aaSAlexander Motin 	case AC_LOST_DEVICE: return (0);
4093227d67aaSAlexander Motin 	case AC_TRANSFER_NEG: return (sizeof(struct ccb_trans_settings));
4094227d67aaSAlexander Motin 	case AC_INQ_CHANGED: return (0);
4095227d67aaSAlexander Motin 	case AC_GETDEV_CHANGED: return (0);
4096227d67aaSAlexander Motin 	case AC_CONTRACT: return (sizeof(struct ac_contract));
4097227d67aaSAlexander Motin 	case AC_ADVINFO_CHANGED: return (-1);
4098227d67aaSAlexander Motin 	case AC_UNIT_ATTENTION: return (sizeof(struct ccb_scsiio));
4099227d67aaSAlexander Motin 	}
4100227d67aaSAlexander Motin 	return (0);
4101227d67aaSAlexander Motin }
4102227d67aaSAlexander Motin 
4103227d67aaSAlexander Motin static int
4104227d67aaSAlexander Motin xpt_async_process_dev(struct cam_ed *device, void *arg)
4105227d67aaSAlexander Motin {
4106227d67aaSAlexander Motin 	union ccb *ccb = arg;
4107227d67aaSAlexander Motin 	struct cam_path *path = ccb->ccb_h.path;
4108227d67aaSAlexander Motin 	void *async_arg = ccb->casync.async_arg_ptr;
4109227d67aaSAlexander Motin 	u_int32_t async_code = ccb->casync.async_code;
4110227d67aaSAlexander Motin 	int relock;
4111227d67aaSAlexander Motin 
4112227d67aaSAlexander Motin 	if (path->device != device
4113227d67aaSAlexander Motin 	 && path->device->lun_id != CAM_LUN_WILDCARD
4114227d67aaSAlexander Motin 	 && device->lun_id != CAM_LUN_WILDCARD)
4115227d67aaSAlexander Motin 		return (1);
41168b8a9b1dSJustin T. Gibbs 
4117a5479bc5SJustin T. Gibbs 	/*
4118227d67aaSAlexander Motin 	 * The async callback could free the device.
4119227d67aaSAlexander Motin 	 * If it is a broadcast async, it doesn't hold
4120227d67aaSAlexander Motin 	 * device reference, so take our own reference.
4121a5479bc5SJustin T. Gibbs 	 */
4122227d67aaSAlexander Motin 	xpt_acquire_device(device);
41238b8a9b1dSJustin T. Gibbs 
4124227d67aaSAlexander Motin 	/*
4125227d67aaSAlexander Motin 	 * If async for specific device is to be delivered to
4126227d67aaSAlexander Motin 	 * the wildcard client, take the specific device lock.
4127227d67aaSAlexander Motin 	 * XXX: We may need a way for client to specify it.
4128227d67aaSAlexander Motin 	 */
4129227d67aaSAlexander Motin 	if ((device->lun_id == CAM_LUN_WILDCARD &&
4130227d67aaSAlexander Motin 	     path->device->lun_id != CAM_LUN_WILDCARD) ||
4131227d67aaSAlexander Motin 	    (device->target->target_id == CAM_TARGET_WILDCARD &&
4132227d67aaSAlexander Motin 	     path->target->target_id != CAM_TARGET_WILDCARD) ||
4133227d67aaSAlexander Motin 	    (device->target->bus->path_id == CAM_BUS_WILDCARD &&
4134227d67aaSAlexander Motin 	     path->target->bus->path_id != CAM_BUS_WILDCARD)) {
4135227d67aaSAlexander Motin 		mtx_unlock(&device->device_mtx);
4136227d67aaSAlexander Motin 		xpt_path_lock(path);
4137227d67aaSAlexander Motin 		relock = 1;
4138227d67aaSAlexander Motin 	} else
4139227d67aaSAlexander Motin 		relock = 0;
4140227d67aaSAlexander Motin 
4141ded2b706SWarner Losh 	(*(device->target->bus->xport->ops->async))(async_code,
4142227d67aaSAlexander Motin 	    device->target->bus, device->target, device, async_arg);
4143227d67aaSAlexander Motin 	xpt_async_bcast(&device->asyncs, async_code, path, async_arg);
4144227d67aaSAlexander Motin 
4145227d67aaSAlexander Motin 	if (relock) {
4146227d67aaSAlexander Motin 		xpt_path_unlock(path);
4147227d67aaSAlexander Motin 		mtx_lock(&device->device_mtx);
4148227d67aaSAlexander Motin 	}
4149227d67aaSAlexander Motin 	xpt_release_device(device);
4150227d67aaSAlexander Motin 	return (1);
4151227d67aaSAlexander Motin }
4152227d67aaSAlexander Motin 
4153227d67aaSAlexander Motin static int
4154227d67aaSAlexander Motin xpt_async_process_tgt(struct cam_et *target, void *arg)
4155227d67aaSAlexander Motin {
4156227d67aaSAlexander Motin 	union ccb *ccb = arg;
4157227d67aaSAlexander Motin 	struct cam_path *path = ccb->ccb_h.path;
4158227d67aaSAlexander Motin 
4159227d67aaSAlexander Motin 	if (path->target != target
4160227d67aaSAlexander Motin 	 && path->target->target_id != CAM_TARGET_WILDCARD
4161227d67aaSAlexander Motin 	 && target->target_id != CAM_TARGET_WILDCARD)
4162227d67aaSAlexander Motin 		return (1);
4163227d67aaSAlexander Motin 
4164227d67aaSAlexander Motin 	if (ccb->casync.async_code == AC_SENT_BDR) {
4165227d67aaSAlexander Motin 		/* Update our notion of when the last reset occurred */
4166227d67aaSAlexander Motin 		microtime(&target->last_reset);
4167227d67aaSAlexander Motin 	}
4168227d67aaSAlexander Motin 
4169227d67aaSAlexander Motin 	return (xptdevicetraverse(target, NULL, xpt_async_process_dev, ccb));
4170227d67aaSAlexander Motin }
4171227d67aaSAlexander Motin 
4172227d67aaSAlexander Motin static void
4173227d67aaSAlexander Motin xpt_async_process(struct cam_periph *periph, union ccb *ccb)
4174227d67aaSAlexander Motin {
4175227d67aaSAlexander Motin 	struct cam_eb *bus;
4176227d67aaSAlexander Motin 	struct cam_path *path;
4177227d67aaSAlexander Motin 	void *async_arg;
4178227d67aaSAlexander Motin 	u_int32_t async_code;
4179227d67aaSAlexander Motin 
4180227d67aaSAlexander Motin 	path = ccb->ccb_h.path;
4181227d67aaSAlexander Motin 	async_code = ccb->casync.async_code;
4182227d67aaSAlexander Motin 	async_arg = ccb->casync.async_arg_ptr;
4183227d67aaSAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4184227d67aaSAlexander Motin 	    ("xpt_async(%s)\n", xpt_async_string(async_code)));
41858b8a9b1dSJustin T. Gibbs 	bus = path->bus;
41868b8a9b1dSJustin T. Gibbs 
41878b8a9b1dSJustin T. Gibbs 	if (async_code == AC_BUS_RESET) {
418887cfaf0eSJustin T. Gibbs 		/* Update our notion of when the last reset occurred */
418987cfaf0eSJustin T. Gibbs 		microtime(&bus->last_reset);
41908b8a9b1dSJustin T. Gibbs 	}
41918b8a9b1dSJustin T. Gibbs 
4192227d67aaSAlexander Motin 	xpttargettraverse(bus, NULL, xpt_async_process_tgt, ccb);
4193c8bead2aSJustin T. Gibbs 
4194c8bead2aSJustin T. Gibbs 	/*
4195c8bead2aSJustin T. Gibbs 	 * If this wasn't a fully wildcarded async, tell all
4196c8bead2aSJustin T. Gibbs 	 * clients that want all async events.
4197c8bead2aSJustin T. Gibbs 	 */
4198227d67aaSAlexander Motin 	if (bus != xpt_periph->path->bus) {
4199227d67aaSAlexander Motin 		xpt_path_lock(xpt_periph->path);
4200227d67aaSAlexander Motin 		xpt_async_process_dev(xpt_periph->path->device, ccb);
4201227d67aaSAlexander Motin 		xpt_path_unlock(xpt_periph->path);
4202227d67aaSAlexander Motin 	}
4203227d67aaSAlexander Motin 
4204227d67aaSAlexander Motin 	if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4205227d67aaSAlexander Motin 		xpt_release_devq(path, 1, TRUE);
4206227d67aaSAlexander Motin 	else
4207227d67aaSAlexander Motin 		xpt_release_simq(path->bus->sim, TRUE);
4208227d67aaSAlexander Motin 	if (ccb->casync.async_arg_size > 0)
4209227d67aaSAlexander Motin 		free(async_arg, M_CAMXPT);
4210227d67aaSAlexander Motin 	xpt_free_path(path);
4211227d67aaSAlexander Motin 	xpt_free_ccb(ccb);
42128b8a9b1dSJustin T. Gibbs }
42138b8a9b1dSJustin T. Gibbs 
42148b8a9b1dSJustin T. Gibbs static void
42158b8a9b1dSJustin T. Gibbs xpt_async_bcast(struct async_list *async_head,
42168b8a9b1dSJustin T. Gibbs 		u_int32_t async_code,
42178b8a9b1dSJustin T. Gibbs 		struct cam_path *path, void *async_arg)
42188b8a9b1dSJustin T. Gibbs {
42198b8a9b1dSJustin T. Gibbs 	struct async_node *cur_entry;
4220227d67aaSAlexander Motin 	int lock;
42218b8a9b1dSJustin T. Gibbs 
42228b8a9b1dSJustin T. Gibbs 	cur_entry = SLIST_FIRST(async_head);
42238b8a9b1dSJustin T. Gibbs 	while (cur_entry != NULL) {
42248b8a9b1dSJustin T. Gibbs 		struct async_node *next_entry;
42258b8a9b1dSJustin T. Gibbs 		/*
42268b8a9b1dSJustin T. Gibbs 		 * Grab the next list entry before we call the current
42278b8a9b1dSJustin T. Gibbs 		 * entry's callback.  This is because the callback function
42288b8a9b1dSJustin T. Gibbs 		 * can delete its async callback entry.
42298b8a9b1dSJustin T. Gibbs 		 */
42308b8a9b1dSJustin T. Gibbs 		next_entry = SLIST_NEXT(cur_entry, links);
4231227d67aaSAlexander Motin 		if ((cur_entry->event_enable & async_code) != 0) {
4232227d67aaSAlexander Motin 			lock = cur_entry->event_lock;
4233227d67aaSAlexander Motin 			if (lock)
4234227d67aaSAlexander Motin 				CAM_SIM_LOCK(path->device->sim);
42358b8a9b1dSJustin T. Gibbs 			cur_entry->callback(cur_entry->callback_arg,
42368b8a9b1dSJustin T. Gibbs 					    async_code, path,
42378b8a9b1dSJustin T. Gibbs 					    async_arg);
4238227d67aaSAlexander Motin 			if (lock)
4239227d67aaSAlexander Motin 				CAM_SIM_UNLOCK(path->device->sim);
4240227d67aaSAlexander Motin 		}
42418b8a9b1dSJustin T. Gibbs 		cur_entry = next_entry;
42428b8a9b1dSJustin T. Gibbs 	}
42438b8a9b1dSJustin T. Gibbs }
42448b8a9b1dSJustin T. Gibbs 
4245227d67aaSAlexander Motin void
4246227d67aaSAlexander Motin xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4247227d67aaSAlexander Motin {
4248227d67aaSAlexander Motin 	union ccb *ccb;
4249227d67aaSAlexander Motin 	int size;
4250227d67aaSAlexander Motin 
4251227d67aaSAlexander Motin 	ccb = xpt_alloc_ccb_nowait();
4252227d67aaSAlexander Motin 	if (ccb == NULL) {
4253227d67aaSAlexander Motin 		xpt_print(path, "Can't allocate CCB to send %s\n",
4254227d67aaSAlexander Motin 		    xpt_async_string(async_code));
4255227d67aaSAlexander Motin 		return;
4256227d67aaSAlexander Motin 	}
4257227d67aaSAlexander Motin 
4258227d67aaSAlexander Motin 	if (xpt_clone_path(&ccb->ccb_h.path, path) != CAM_REQ_CMP) {
4259227d67aaSAlexander Motin 		xpt_print(path, "Can't allocate path to send %s\n",
4260227d67aaSAlexander Motin 		    xpt_async_string(async_code));
4261227d67aaSAlexander Motin 		xpt_free_ccb(ccb);
4262227d67aaSAlexander Motin 		return;
4263227d67aaSAlexander Motin 	}
4264227d67aaSAlexander Motin 	ccb->ccb_h.path->periph = NULL;
4265227d67aaSAlexander Motin 	ccb->ccb_h.func_code = XPT_ASYNC;
4266227d67aaSAlexander Motin 	ccb->ccb_h.cbfcnp = xpt_async_process;
4267227d67aaSAlexander Motin 	ccb->ccb_h.flags |= CAM_UNLOCKED;
4268227d67aaSAlexander Motin 	ccb->casync.async_code = async_code;
4269227d67aaSAlexander Motin 	ccb->casync.async_arg_size = 0;
4270227d67aaSAlexander Motin 	size = xpt_async_size(async_code);
427169be012fSWarner Losh 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
427269be012fSWarner Losh 	    ("xpt_async: func %#x %s aync_code %d %s\n",
427369be012fSWarner Losh 		ccb->ccb_h.func_code,
427469be012fSWarner Losh 		xpt_action_name(ccb->ccb_h.func_code),
427569be012fSWarner Losh 		async_code,
427669be012fSWarner Losh 		xpt_async_string(async_code)));
4277227d67aaSAlexander Motin 	if (size > 0 && async_arg != NULL) {
4278227d67aaSAlexander Motin 		ccb->casync.async_arg_ptr = malloc(size, M_CAMXPT, M_NOWAIT);
4279227d67aaSAlexander Motin 		if (ccb->casync.async_arg_ptr == NULL) {
4280227d67aaSAlexander Motin 			xpt_print(path, "Can't allocate argument to send %s\n",
4281227d67aaSAlexander Motin 			    xpt_async_string(async_code));
4282227d67aaSAlexander Motin 			xpt_free_path(ccb->ccb_h.path);
4283227d67aaSAlexander Motin 			xpt_free_ccb(ccb);
4284227d67aaSAlexander Motin 			return;
4285227d67aaSAlexander Motin 		}
4286227d67aaSAlexander Motin 		memcpy(ccb->casync.async_arg_ptr, async_arg, size);
4287227d67aaSAlexander Motin 		ccb->casync.async_arg_size = size;
4288738fd166SAlan Somers 	} else if (size < 0) {
4289738fd166SAlan Somers 		ccb->casync.async_arg_ptr = async_arg;
4290227d67aaSAlexander Motin 		ccb->casync.async_arg_size = size;
4291738fd166SAlan Somers 	}
4292227d67aaSAlexander Motin 	if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4293227d67aaSAlexander Motin 		xpt_freeze_devq(path, 1);
4294227d67aaSAlexander Motin 	else
4295227d67aaSAlexander Motin 		xpt_freeze_simq(path->bus->sim, 1);
4296227d67aaSAlexander Motin 	xpt_done(ccb);
4297227d67aaSAlexander Motin }
4298227d67aaSAlexander Motin 
42992f22d08dSJustin T. Gibbs static void
430052c9ce25SScott Long xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
430152c9ce25SScott Long 		      struct cam_et *target, struct cam_ed *device,
430252c9ce25SScott Long 		      void *async_arg)
43032f22d08dSJustin T. Gibbs {
4304227d67aaSAlexander Motin 
4305227d67aaSAlexander Motin 	/*
4306227d67aaSAlexander Motin 	 * We only need to handle events for real devices.
4307227d67aaSAlexander Motin 	 */
4308227d67aaSAlexander Motin 	if (target->target_id == CAM_TARGET_WILDCARD
4309227d67aaSAlexander Motin 	 || device->lun_id == CAM_LUN_WILDCARD)
4310227d67aaSAlexander Motin 		return;
4311227d67aaSAlexander Motin 
4312b882a6d3SMatt Jacob 	printf("%s called\n", __func__);
43132f22d08dSJustin T. Gibbs }
43142f22d08dSJustin T. Gibbs 
4315daa5487fSAlexander Motin static uint32_t
4316daa5487fSAlexander Motin xpt_freeze_devq_device(struct cam_ed *dev, u_int count)
4317daa5487fSAlexander Motin {
4318daa5487fSAlexander Motin 	struct cam_devq	*devq;
4319daa5487fSAlexander Motin 	uint32_t freeze;
4320daa5487fSAlexander Motin 
4321daa5487fSAlexander Motin 	devq = dev->sim->devq;
4322daa5487fSAlexander Motin 	mtx_assert(&devq->send_mtx, MA_OWNED);
4323daa5487fSAlexander Motin 	CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4324daa5487fSAlexander Motin 	    ("xpt_freeze_devq_device(%d) %u->%u\n", count,
4325daa5487fSAlexander Motin 	    dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt + count));
4326daa5487fSAlexander Motin 	freeze = (dev->ccbq.queue.qfrozen_cnt += count);
4327daa5487fSAlexander Motin 	/* Remove frozen device from sendq. */
4328daa5487fSAlexander Motin 	if (device_is_queued(dev))
4329daa5487fSAlexander Motin 		camq_remove(&devq->send_queue, dev->devq_entry.index);
4330daa5487fSAlexander Motin 	return (freeze);
4331daa5487fSAlexander Motin }
4332daa5487fSAlexander Motin 
43338b8a9b1dSJustin T. Gibbs u_int32_t
4334cccf4220SAlexander Motin xpt_freeze_devq(struct cam_path *path, u_int count)
433583c5d981SAlexander Motin {
433683c5d981SAlexander Motin 	struct cam_ed	*dev = path->device;
4337227d67aaSAlexander Motin 	struct cam_devq	*devq;
4338227d67aaSAlexander Motin 	uint32_t	 freeze;
433983c5d981SAlexander Motin 
4340227d67aaSAlexander Motin 	devq = dev->sim->devq;
4341227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4342daa5487fSAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_freeze_devq(%d)\n", count));
4343daa5487fSAlexander Motin 	freeze = xpt_freeze_devq_device(dev, count);
4344227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
4345227d67aaSAlexander Motin 	return (freeze);
43468b8a9b1dSJustin T. Gibbs }
43478b8a9b1dSJustin T. Gibbs 
43488b8a9b1dSJustin T. Gibbs u_int32_t
43498b8a9b1dSJustin T. Gibbs xpt_freeze_simq(struct cam_sim *sim, u_int count)
43508b8a9b1dSJustin T. Gibbs {
4351227d67aaSAlexander Motin 	struct cam_devq	*devq;
4352227d67aaSAlexander Motin 	uint32_t	 freeze;
4353ec700f26SAlexander Motin 
4354227d67aaSAlexander Motin 	devq = sim->devq;
4355227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4356227d67aaSAlexander Motin 	freeze = (devq->send_queue.qfrozen_cnt += count);
4357227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
4358227d67aaSAlexander Motin 	return (freeze);
43598b8a9b1dSJustin T. Gibbs }
43608b8a9b1dSJustin T. Gibbs 
43618b8a9b1dSJustin T. Gibbs static void
43628b8a9b1dSJustin T. Gibbs xpt_release_devq_timeout(void *arg)
43638b8a9b1dSJustin T. Gibbs {
4364227d67aaSAlexander Motin 	struct cam_ed *dev;
4365227d67aaSAlexander Motin 	struct cam_devq *devq;
43668b8a9b1dSJustin T. Gibbs 
4367227d67aaSAlexander Motin 	dev = (struct cam_ed *)arg;
4368227d67aaSAlexander Motin 	CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, ("xpt_release_devq_timeout\n"));
4369227d67aaSAlexander Motin 	devq = dev->sim->devq;
4370227d67aaSAlexander Motin 	mtx_assert(&devq->send_mtx, MA_OWNED);
4371227d67aaSAlexander Motin 	if (xpt_release_devq_device(dev, /*count*/1, /*run_queue*/TRUE))
4372227d67aaSAlexander Motin 		xpt_run_devq(devq);
43738b8a9b1dSJustin T. Gibbs }
43748b8a9b1dSJustin T. Gibbs 
43758b8a9b1dSJustin T. Gibbs void
43762cefde5fSJustin T. Gibbs xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
43772cefde5fSJustin T. Gibbs {
4378227d67aaSAlexander Motin 	struct cam_ed *dev;
4379227d67aaSAlexander Motin 	struct cam_devq *devq;
438068153f43SScott Long 
43810d4f3c31SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_devq(%d, %d)\n",
43820d4f3c31SAlexander Motin 	    count, run_queue));
4383227d67aaSAlexander Motin 	dev = path->device;
4384227d67aaSAlexander Motin 	devq = dev->sim->devq;
4385227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4386227d67aaSAlexander Motin 	if (xpt_release_devq_device(dev, count, run_queue))
4387227d67aaSAlexander Motin 		xpt_run_devq(dev->sim->devq);
4388227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
438983c5d981SAlexander Motin }
439083c5d981SAlexander Motin 
4391227d67aaSAlexander Motin static int
4392cccf4220SAlexander Motin xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
43938b8a9b1dSJustin T. Gibbs {
43948b8a9b1dSJustin T. Gibbs 
4395227d67aaSAlexander Motin 	mtx_assert(&dev->sim->devq->send_mtx, MA_OWNED);
43960d4f3c31SAlexander Motin 	CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
43970d4f3c31SAlexander Motin 	    ("xpt_release_devq_device(%d, %d) %u->%u\n", count, run_queue,
43980d4f3c31SAlexander Motin 	    dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt - count));
4399cccf4220SAlexander Motin 	if (count > dev->ccbq.queue.qfrozen_cnt) {
440083c5d981SAlexander Motin #ifdef INVARIANTS
4401cccf4220SAlexander Motin 		printf("xpt_release_devq(): requested %u > present %u\n",
4402cccf4220SAlexander Motin 		    count, dev->ccbq.queue.qfrozen_cnt);
440383c5d981SAlexander Motin #endif
4404cccf4220SAlexander Motin 		count = dev->ccbq.queue.qfrozen_cnt;
440583c5d981SAlexander Motin 	}
4406cccf4220SAlexander Motin 	dev->ccbq.queue.qfrozen_cnt -= count;
4407cccf4220SAlexander Motin 	if (dev->ccbq.queue.qfrozen_cnt == 0) {
44088b8a9b1dSJustin T. Gibbs 		/*
44098b8a9b1dSJustin T. Gibbs 		 * No longer need to wait for a successful
44108b8a9b1dSJustin T. Gibbs 		 * command completion.
44118b8a9b1dSJustin T. Gibbs 		 */
44128b8a9b1dSJustin T. Gibbs 		dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
44138b8a9b1dSJustin T. Gibbs 		/*
44148b8a9b1dSJustin T. Gibbs 		 * Remove any timeouts that might be scheduled
44158b8a9b1dSJustin T. Gibbs 		 * to release this queue.
44168b8a9b1dSJustin T. Gibbs 		 */
44178b8a9b1dSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
44182b83592fSScott Long 			callout_stop(&dev->callout);
44198b8a9b1dSJustin T. Gibbs 			dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
44208b8a9b1dSJustin T. Gibbs 		}
44218b8a9b1dSJustin T. Gibbs 		/*
44228b8a9b1dSJustin T. Gibbs 		 * Now that we are unfrozen schedule the
44238b8a9b1dSJustin T. Gibbs 		 * device so any pending transactions are
44248b8a9b1dSJustin T. Gibbs 		 * run.
44258b8a9b1dSJustin T. Gibbs 		 */
4426227d67aaSAlexander Motin 		xpt_schedule_devq(dev->sim->devq, dev);
4427227d67aaSAlexander Motin 	} else
4428227d67aaSAlexander Motin 		run_queue = 0;
4429227d67aaSAlexander Motin 	return (run_queue);
443083c5d981SAlexander Motin }
44318b8a9b1dSJustin T. Gibbs 
44328b8a9b1dSJustin T. Gibbs void
44338b8a9b1dSJustin T. Gibbs xpt_release_simq(struct cam_sim *sim, int run_queue)
44348b8a9b1dSJustin T. Gibbs {
4435227d67aaSAlexander Motin 	struct cam_devq	*devq;
44368b8a9b1dSJustin T. Gibbs 
4437227d67aaSAlexander Motin 	devq = sim->devq;
4438227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4439227d67aaSAlexander Motin 	if (devq->send_queue.qfrozen_cnt <= 0) {
444083c5d981SAlexander Motin #ifdef INVARIANTS
444183c5d981SAlexander Motin 		printf("xpt_release_simq: requested 1 > present %u\n",
4442227d67aaSAlexander Motin 		    devq->send_queue.qfrozen_cnt);
444383c5d981SAlexander Motin #endif
444483c5d981SAlexander Motin 	} else
4445227d67aaSAlexander Motin 		devq->send_queue.qfrozen_cnt--;
4446227d67aaSAlexander Motin 	if (devq->send_queue.qfrozen_cnt == 0) {
44478b8a9b1dSJustin T. Gibbs 		/*
44488b8a9b1dSJustin T. Gibbs 		 * If there is a timeout scheduled to release this
44498b8a9b1dSJustin T. Gibbs 		 * sim queue, remove it.  The queue frozen count is
44508b8a9b1dSJustin T. Gibbs 		 * already at 0.
44518b8a9b1dSJustin T. Gibbs 		 */
44528b8a9b1dSJustin T. Gibbs 		if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
44532b83592fSScott Long 			callout_stop(&sim->callout);
44548b8a9b1dSJustin T. Gibbs 			sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
44558b8a9b1dSJustin T. Gibbs 		}
44568b8a9b1dSJustin T. Gibbs 		if (run_queue) {
44578b8a9b1dSJustin T. Gibbs 			/*
44588b8a9b1dSJustin T. Gibbs 			 * Now that we are unfrozen run the send queue.
44598b8a9b1dSJustin T. Gibbs 			 */
4460cccf4220SAlexander Motin 			xpt_run_devq(sim->devq);
446177dc25ccSScott Long 		}
446277dc25ccSScott Long 	}
4463227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
44648b8a9b1dSJustin T. Gibbs }
44658b8a9b1dSJustin T. Gibbs 
44662b83592fSScott Long /*
44672b83592fSScott Long  * XXX Appears to be unused.
44682b83592fSScott Long  */
44698b8a9b1dSJustin T. Gibbs static void
44708b8a9b1dSJustin T. Gibbs xpt_release_simq_timeout(void *arg)
44718b8a9b1dSJustin T. Gibbs {
44728b8a9b1dSJustin T. Gibbs 	struct cam_sim *sim;
44738b8a9b1dSJustin T. Gibbs 
44748b8a9b1dSJustin T. Gibbs 	sim = (struct cam_sim *)arg;
44758b8a9b1dSJustin T. Gibbs 	xpt_release_simq(sim, /* run_queue */ TRUE);
44768b8a9b1dSJustin T. Gibbs }
44778b8a9b1dSJustin T. Gibbs 
44788b8a9b1dSJustin T. Gibbs void
4479a5479bc5SJustin T. Gibbs xpt_done(union ccb *done_ccb)
44808b8a9b1dSJustin T. Gibbs {
4481227d67aaSAlexander Motin 	struct cam_doneq *queue;
4482227d67aaSAlexander Motin 	int	run, hash;
44838b8a9b1dSJustin T. Gibbs 
44848532d381SConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
44858532d381SConrad Meyer 	if (done_ccb->ccb_h.func_code == XPT_SCSI_IO &&
44868532d381SConrad Meyer 	    done_ccb->csio.bio != NULL)
44878532d381SConrad Meyer 		biotrack(done_ccb->csio.bio, __func__);
44888532d381SConrad Meyer #endif
44898532d381SConrad Meyer 
449069be012fSWarner Losh 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
449169be012fSWarner Losh 	    ("xpt_done: func= %#x %s status %#x\n",
449269be012fSWarner Losh 		done_ccb->ccb_h.func_code,
449369be012fSWarner Losh 		xpt_action_name(done_ccb->ccb_h.func_code),
449469be012fSWarner Losh 		done_ccb->ccb_h.status));
4495227d67aaSAlexander Motin 	if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4496227d67aaSAlexander Motin 		return;
4497227d67aaSAlexander Motin 
4498a6e0c5daSWarner Losh 	/* Store the time the ccb was in the sim */
4499a6e0c5daSWarner Losh 	done_ccb->ccb_h.qos.sim_data = sbinuptime() - done_ccb->ccb_h.qos.sim_data;
4500227d67aaSAlexander Motin 	hash = (done_ccb->ccb_h.path_id + done_ccb->ccb_h.target_id +
4501227d67aaSAlexander Motin 	    done_ccb->ccb_h.target_lun) % cam_num_doneqs;
4502227d67aaSAlexander Motin 	queue = &cam_doneqs[hash];
4503227d67aaSAlexander Motin 	mtx_lock(&queue->cam_doneq_mtx);
4504227d67aaSAlexander Motin 	run = (queue->cam_doneq_sleep && STAILQ_EMPTY(&queue->cam_doneq));
4505227d67aaSAlexander Motin 	STAILQ_INSERT_TAIL(&queue->cam_doneq, &done_ccb->ccb_h, sim_links.stqe);
45068b8a9b1dSJustin T. Gibbs 	done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4507227d67aaSAlexander Motin 	mtx_unlock(&queue->cam_doneq_mtx);
4508227d67aaSAlexander Motin 	if (run)
4509227d67aaSAlexander Motin 		wakeup(&queue->cam_doneq);
45108b8a9b1dSJustin T. Gibbs }
45118b8a9b1dSJustin T. Gibbs 
4512711f6613SAlexander Motin void
4513227d67aaSAlexander Motin xpt_done_direct(union ccb *done_ccb)
4514711f6613SAlexander Motin {
4515711f6613SAlexander Motin 
451669be012fSWarner Losh 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
451769be012fSWarner Losh 	    ("xpt_done_direct: status %#x\n", done_ccb->ccb_h.status));
4518227d67aaSAlexander Motin 	if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4519227d67aaSAlexander Motin 		return;
4520711f6613SAlexander Motin 
4521a6e0c5daSWarner Losh 	/* Store the time the ccb was in the sim */
4522a6e0c5daSWarner Losh 	done_ccb->ccb_h.qos.sim_data = sbinuptime() - done_ccb->ccb_h.qos.sim_data;
4523227d67aaSAlexander Motin 	xpt_done_process(&done_ccb->ccb_h);
4524711f6613SAlexander Motin }
4525711f6613SAlexander Motin 
45268b8a9b1dSJustin T. Gibbs union ccb *
45278008a935SScott Long xpt_alloc_ccb()
45288b8a9b1dSJustin T. Gibbs {
45298b8a9b1dSJustin T. Gibbs 	union ccb *new_ccb;
45308b8a9b1dSJustin T. Gibbs 
4531596ee08fSAlexander Motin 	new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4532362abc44STai-hwa Liang 	return (new_ccb);
4533362abc44STai-hwa Liang }
4534362abc44STai-hwa Liang 
4535362abc44STai-hwa Liang union ccb *
45368008a935SScott Long xpt_alloc_ccb_nowait()
4537362abc44STai-hwa Liang {
4538362abc44STai-hwa Liang 	union ccb *new_ccb;
4539362abc44STai-hwa Liang 
4540596ee08fSAlexander Motin 	new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
45418b8a9b1dSJustin T. Gibbs 	return (new_ccb);
45428b8a9b1dSJustin T. Gibbs }
45438b8a9b1dSJustin T. Gibbs 
45448b8a9b1dSJustin T. Gibbs void
45458b8a9b1dSJustin T. Gibbs xpt_free_ccb(union ccb *free_ccb)
45468b8a9b1dSJustin T. Gibbs {
4547596ee08fSAlexander Motin 	free(free_ccb, M_CAMCCB);
45488b8a9b1dSJustin T. Gibbs }
45498b8a9b1dSJustin T. Gibbs 
45508b8a9b1dSJustin T. Gibbs 
45518b8a9b1dSJustin T. Gibbs 
45528b8a9b1dSJustin T. Gibbs /* Private XPT functions */
45538b8a9b1dSJustin T. Gibbs 
45548b8a9b1dSJustin T. Gibbs /*
45558b8a9b1dSJustin T. Gibbs  * Get a CAM control block for the caller. Charge the structure to the device
4556227d67aaSAlexander Motin  * referenced by the path.  If we don't have sufficient resources to allocate
4557227d67aaSAlexander Motin  * more ccbs, we return NULL.
45588b8a9b1dSJustin T. Gibbs  */
45598b8a9b1dSJustin T. Gibbs static union ccb *
4560227d67aaSAlexander Motin xpt_get_ccb_nowait(struct cam_periph *periph)
45618b8a9b1dSJustin T. Gibbs {
45628b8a9b1dSJustin T. Gibbs 	union ccb *new_ccb;
45638b8a9b1dSJustin T. Gibbs 
4564d3995fddSBenno Rice 	new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4565227d67aaSAlexander Motin 	if (new_ccb == NULL)
45668b8a9b1dSJustin T. Gibbs 		return (NULL);
4567227d67aaSAlexander Motin 	periph->periph_allocated++;
4568227d67aaSAlexander Motin 	cam_ccbq_take_opening(&periph->path->device->ccbq);
45698b8a9b1dSJustin T. Gibbs 	return (new_ccb);
45708b8a9b1dSJustin T. Gibbs }
45718b8a9b1dSJustin T. Gibbs 
4572227d67aaSAlexander Motin static union ccb *
4573227d67aaSAlexander Motin xpt_get_ccb(struct cam_periph *periph)
4574227d67aaSAlexander Motin {
4575227d67aaSAlexander Motin 	union ccb *new_ccb;
4576227d67aaSAlexander Motin 
4577227d67aaSAlexander Motin 	cam_periph_unlock(periph);
4578d3995fddSBenno Rice 	new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4579227d67aaSAlexander Motin 	cam_periph_lock(periph);
4580227d67aaSAlexander Motin 	periph->periph_allocated++;
4581227d67aaSAlexander Motin 	cam_ccbq_take_opening(&periph->path->device->ccbq);
4582227d67aaSAlexander Motin 	return (new_ccb);
4583227d67aaSAlexander Motin }
4584227d67aaSAlexander Motin 
4585227d67aaSAlexander Motin union ccb *
4586227d67aaSAlexander Motin cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
4587227d67aaSAlexander Motin {
4588227d67aaSAlexander Motin 	struct ccb_hdr *ccb_h;
4589227d67aaSAlexander Motin 
4590227d67aaSAlexander Motin 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("cam_periph_getccb\n"));
4591227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
4592227d67aaSAlexander Motin 	while ((ccb_h = SLIST_FIRST(&periph->ccb_list)) == NULL ||
4593227d67aaSAlexander Motin 	    ccb_h->pinfo.priority != priority) {
4594227d67aaSAlexander Motin 		if (priority < periph->immediate_priority) {
4595227d67aaSAlexander Motin 			periph->immediate_priority = priority;
4596227d67aaSAlexander Motin 			xpt_run_allocq(periph, 0);
4597227d67aaSAlexander Motin 		} else
4598227d67aaSAlexander Motin 			cam_periph_sleep(periph, &periph->ccb_list, PRIBIO,
4599227d67aaSAlexander Motin 			    "cgticb", 0);
4600227d67aaSAlexander Motin 	}
4601227d67aaSAlexander Motin 	SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
4602227d67aaSAlexander Motin 	return ((union ccb *)ccb_h);
4603227d67aaSAlexander Motin }
4604227d67aaSAlexander Motin 
4605227d67aaSAlexander Motin static void
4606227d67aaSAlexander Motin xpt_acquire_bus(struct cam_eb *bus)
4607227d67aaSAlexander Motin {
4608227d67aaSAlexander Motin 
4609227d67aaSAlexander Motin 	xpt_lock_buses();
4610227d67aaSAlexander Motin 	bus->refcount++;
4611227d67aaSAlexander Motin 	xpt_unlock_buses();
4612227d67aaSAlexander Motin }
4613227d67aaSAlexander Motin 
4614a5479bc5SJustin T. Gibbs static void
4615a5479bc5SJustin T. Gibbs xpt_release_bus(struct cam_eb *bus)
4616a5479bc5SJustin T. Gibbs {
4617a5479bc5SJustin T. Gibbs 
46189a7c2696SAlexander Motin 	xpt_lock_buses();
461915975b7bSMatt Jacob 	KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4620dcdf6e74SAlexander Motin 	if (--bus->refcount > 0) {
4621dcdf6e74SAlexander Motin 		xpt_unlock_buses();
4622dcdf6e74SAlexander Motin 		return;
4623dcdf6e74SAlexander Motin 	}
46242b83592fSScott Long 	TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
46252b83592fSScott Long 	xsoftc.bus_generation++;
46269a7c2696SAlexander Motin 	xpt_unlock_buses();
4627227d67aaSAlexander Motin 	KASSERT(TAILQ_EMPTY(&bus->et_entries),
4628227d67aaSAlexander Motin 	    ("destroying bus, but target list is not empty"));
4629fa6099fdSEdward Tomasz Napierala 	cam_sim_release(bus->sim);
4630227d67aaSAlexander Motin 	mtx_destroy(&bus->eb_mtx);
4631362abc44STai-hwa Liang 	free(bus, M_CAMXPT);
4632a5479bc5SJustin T. Gibbs }
46338b8a9b1dSJustin T. Gibbs 
46348b8a9b1dSJustin T. Gibbs static struct cam_et *
46358b8a9b1dSJustin T. Gibbs xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
46368b8a9b1dSJustin T. Gibbs {
4637dcdf6e74SAlexander Motin 	struct cam_et *cur_target, *target;
46388b8a9b1dSJustin T. Gibbs 
4639227d67aaSAlexander Motin 	mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4640227d67aaSAlexander Motin 	mtx_assert(&bus->eb_mtx, MA_OWNED);
46413501942bSJustin T. Gibbs 	target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
46423501942bSJustin T. Gibbs 					 M_NOWAIT|M_ZERO);
4643dcdf6e74SAlexander Motin 	if (target == NULL)
4644dcdf6e74SAlexander Motin 		return (NULL);
46458b8a9b1dSJustin T. Gibbs 
4646434bbf6eSJustin T. Gibbs 	TAILQ_INIT(&target->ed_entries);
46478b8a9b1dSJustin T. Gibbs 	target->bus = bus;
46488b8a9b1dSJustin T. Gibbs 	target->target_id = target_id;
46498b8a9b1dSJustin T. Gibbs 	target->refcount = 1;
4650434bbf6eSJustin T. Gibbs 	target->generation = 0;
465182b361b1SMatt Jacob 	target->luns = NULL;
4652227d67aaSAlexander Motin 	mtx_init(&target->luns_mtx, "CAM LUNs lock", NULL, MTX_DEF);
4653434bbf6eSJustin T. Gibbs 	timevalclear(&target->last_reset);
4654a5479bc5SJustin T. Gibbs 	/*
4655a5479bc5SJustin T. Gibbs 	 * Hold a reference to our parent bus so it
4656a5479bc5SJustin T. Gibbs 	 * will not go away before we do.
4657a5479bc5SJustin T. Gibbs 	 */
4658a5479bc5SJustin T. Gibbs 	bus->refcount++;
46598b8a9b1dSJustin T. Gibbs 
46608b8a9b1dSJustin T. Gibbs 	/* Insertion sort into our bus's target list */
46618b8a9b1dSJustin T. Gibbs 	cur_target = TAILQ_FIRST(&bus->et_entries);
46628b8a9b1dSJustin T. Gibbs 	while (cur_target != NULL && cur_target->target_id < target_id)
46638b8a9b1dSJustin T. Gibbs 		cur_target = TAILQ_NEXT(cur_target, links);
46648b8a9b1dSJustin T. Gibbs 	if (cur_target != NULL) {
46658b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_BEFORE(cur_target, target, links);
46668b8a9b1dSJustin T. Gibbs 	} else {
46678b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
46688b8a9b1dSJustin T. Gibbs 	}
4669a5479bc5SJustin T. Gibbs 	bus->generation++;
46708b8a9b1dSJustin T. Gibbs 	return (target);
46718b8a9b1dSJustin T. Gibbs }
46728b8a9b1dSJustin T. Gibbs 
4673a5479bc5SJustin T. Gibbs static void
4674227d67aaSAlexander Motin xpt_acquire_target(struct cam_et *target)
4675227d67aaSAlexander Motin {
4676227d67aaSAlexander Motin 	struct cam_eb *bus = target->bus;
4677227d67aaSAlexander Motin 
4678227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
4679227d67aaSAlexander Motin 	target->refcount++;
4680227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
4681227d67aaSAlexander Motin }
4682227d67aaSAlexander Motin 
4683227d67aaSAlexander Motin static void
4684f98d7a47SAlexander Motin xpt_release_target(struct cam_et *target)
46858b8a9b1dSJustin T. Gibbs {
4686227d67aaSAlexander Motin 	struct cam_eb *bus = target->bus;
4687a5479bc5SJustin T. Gibbs 
4688227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
4689227d67aaSAlexander Motin 	if (--target->refcount > 0) {
4690227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
4691dcdf6e74SAlexander Motin 		return;
4692227d67aaSAlexander Motin 	}
4693227d67aaSAlexander Motin 	TAILQ_REMOVE(&bus->et_entries, target, links);
4694227d67aaSAlexander Motin 	bus->generation++;
4695227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
4696dcdf6e74SAlexander Motin 	KASSERT(TAILQ_EMPTY(&target->ed_entries),
4697227d67aaSAlexander Motin 	    ("destroying target, but device list is not empty"));
4698227d67aaSAlexander Motin 	xpt_release_bus(bus);
4699227d67aaSAlexander Motin 	mtx_destroy(&target->luns_mtx);
470082b361b1SMatt Jacob 	if (target->luns)
470182b361b1SMatt Jacob 		free(target->luns, M_CAMXPT);
4702362abc44STai-hwa Liang 	free(target, M_CAMXPT);
470377dc25ccSScott Long }
47048b8a9b1dSJustin T. Gibbs 
47058b8a9b1dSJustin T. Gibbs static struct cam_ed *
470652c9ce25SScott Long xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
470752c9ce25SScott Long 			 lun_id_t lun_id)
470852c9ce25SScott Long {
4709dcdf6e74SAlexander Motin 	struct cam_ed *device;
471052c9ce25SScott Long 
471152c9ce25SScott Long 	device = xpt_alloc_device(bus, target, lun_id);
471252c9ce25SScott Long 	if (device == NULL)
471352c9ce25SScott Long 		return (NULL);
471452c9ce25SScott Long 
471552c9ce25SScott Long 	device->mintags = 1;
471652c9ce25SScott Long 	device->maxtags = 1;
471752c9ce25SScott Long 	return (device);
471852c9ce25SScott Long }
471952c9ce25SScott Long 
4720227d67aaSAlexander Motin static void
4721227d67aaSAlexander Motin xpt_destroy_device(void *context, int pending)
4722227d67aaSAlexander Motin {
4723227d67aaSAlexander Motin 	struct cam_ed	*device = context;
4724227d67aaSAlexander Motin 
4725227d67aaSAlexander Motin 	mtx_lock(&device->device_mtx);
4726227d67aaSAlexander Motin 	mtx_destroy(&device->device_mtx);
4727227d67aaSAlexander Motin 	free(device, M_CAMDEV);
4728227d67aaSAlexander Motin }
4729227d67aaSAlexander Motin 
473052c9ce25SScott Long struct cam_ed *
47318b8a9b1dSJustin T. Gibbs xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
47328b8a9b1dSJustin T. Gibbs {
4733dcdf6e74SAlexander Motin 	struct cam_ed	*cur_device, *device;
47348b8a9b1dSJustin T. Gibbs 	struct cam_devq	*devq;
4735a5479bc5SJustin T. Gibbs 	cam_status status;
47368b8a9b1dSJustin T. Gibbs 
4737227d67aaSAlexander Motin 	mtx_assert(&bus->eb_mtx, MA_OWNED);
47388b8a9b1dSJustin T. Gibbs 	/* Make space for us in the device queue on our bus */
47398b8a9b1dSJustin T. Gibbs 	devq = bus->sim->devq;
4740227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4741cccf4220SAlexander Motin 	status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4742227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
4743dcdf6e74SAlexander Motin 	if (status != CAM_REQ_CMP)
4744dcdf6e74SAlexander Motin 		return (NULL);
47458b8a9b1dSJustin T. Gibbs 
47468b8a9b1dSJustin T. Gibbs 	device = (struct cam_ed *)malloc(sizeof(*device),
4747596ee08fSAlexander Motin 					 M_CAMDEV, M_NOWAIT|M_ZERO);
4748dcdf6e74SAlexander Motin 	if (device == NULL)
4749dcdf6e74SAlexander Motin 		return (NULL);
47508b8a9b1dSJustin T. Gibbs 
4751227d67aaSAlexander Motin 	cam_init_pinfo(&device->devq_entry);
47528b8a9b1dSJustin T. Gibbs 	device->target = target;
47538b8a9b1dSJustin T. Gibbs 	device->lun_id = lun_id;
47542b83592fSScott Long 	device->sim = bus->sim;
47558b8a9b1dSJustin T. Gibbs 	if (cam_ccbq_init(&device->ccbq,
47568b8a9b1dSJustin T. Gibbs 			  bus->sim->max_dev_openings) != 0) {
4757596ee08fSAlexander Motin 		free(device, M_CAMDEV);
47588b8a9b1dSJustin T. Gibbs 		return (NULL);
47598b8a9b1dSJustin T. Gibbs 	}
4760434bbf6eSJustin T. Gibbs 	SLIST_INIT(&device->asyncs);
4761434bbf6eSJustin T. Gibbs 	SLIST_INIT(&device->periphs);
4762434bbf6eSJustin T. Gibbs 	device->generation = 0;
4763434bbf6eSJustin T. Gibbs 	device->flags = CAM_DEV_UNCONFIGURED;
4764434bbf6eSJustin T. Gibbs 	device->tag_delay_count = 0;
4765df8f9080SJustin T. Gibbs 	device->tag_saved_openings = 0;
4766434bbf6eSJustin T. Gibbs 	device->refcount = 1;
4767227d67aaSAlexander Motin 	mtx_init(&device->device_mtx, "CAM device lock", NULL, MTX_DEF);
4768227d67aaSAlexander Motin 	callout_init_mtx(&device->callout, &devq->send_mtx, 0);
4769227d67aaSAlexander Motin 	TASK_INIT(&device->device_destroy_task, 0, xpt_destroy_device, device);
4770227d67aaSAlexander Motin 	/*
4771227d67aaSAlexander Motin 	 * Hold a reference to our parent bus so it
4772227d67aaSAlexander Motin 	 * will not go away before we do.
4773227d67aaSAlexander Motin 	 */
4774227d67aaSAlexander Motin 	target->refcount++;
4775434bbf6eSJustin T. Gibbs 
4776dcdf6e74SAlexander Motin 	cur_device = TAILQ_FIRST(&target->ed_entries);
4777dcdf6e74SAlexander Motin 	while (cur_device != NULL && cur_device->lun_id < lun_id)
4778dcdf6e74SAlexander Motin 		cur_device = TAILQ_NEXT(cur_device, links);
4779dcdf6e74SAlexander Motin 	if (cur_device != NULL)
4780dcdf6e74SAlexander Motin 		TAILQ_INSERT_BEFORE(cur_device, device, links);
4781dcdf6e74SAlexander Motin 	else
4782dcdf6e74SAlexander Motin 		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4783dcdf6e74SAlexander Motin 	target->generation++;
47848b8a9b1dSJustin T. Gibbs 	return (device);
47858b8a9b1dSJustin T. Gibbs }
47868b8a9b1dSJustin T. Gibbs 
4787f98d7a47SAlexander Motin void
4788f98d7a47SAlexander Motin xpt_acquire_device(struct cam_ed *device)
47898b8a9b1dSJustin T. Gibbs {
4790227d67aaSAlexander Motin 	struct cam_eb *bus = device->target->bus;
47918b8a9b1dSJustin T. Gibbs 
4792227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
4793f98d7a47SAlexander Motin 	device->refcount++;
4794227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
4795f98d7a47SAlexander Motin }
4796f98d7a47SAlexander Motin 
4797f98d7a47SAlexander Motin void
4798f98d7a47SAlexander Motin xpt_release_device(struct cam_ed *device)
4799f98d7a47SAlexander Motin {
4800227d67aaSAlexander Motin 	struct cam_eb *bus = device->target->bus;
48018b8a9b1dSJustin T. Gibbs 	struct cam_devq *devq;
48028b8a9b1dSJustin T. Gibbs 
4803227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
4804227d67aaSAlexander Motin 	if (--device->refcount > 0) {
4805227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
4806dcdf6e74SAlexander Motin 		return;
4807227d67aaSAlexander Motin 	}
4808227d67aaSAlexander Motin 
4809227d67aaSAlexander Motin 	TAILQ_REMOVE(&device->target->ed_entries, device,links);
4810227d67aaSAlexander Motin 	device->target->generation++;
4811227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
4812227d67aaSAlexander Motin 
4813227d67aaSAlexander Motin 	/* Release our slot in the devq */
4814227d67aaSAlexander Motin 	devq = bus->sim->devq;
4815227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4816227d67aaSAlexander Motin 	cam_devq_resize(devq, devq->send_queue.array_size - 1);
4817227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
4818dcdf6e74SAlexander Motin 
4819dcdf6e74SAlexander Motin 	KASSERT(SLIST_EMPTY(&device->periphs),
4820227d67aaSAlexander Motin 	    ("destroying device, but periphs list is not empty"));
4821227d67aaSAlexander Motin 	KASSERT(device->devq_entry.index == CAM_UNQUEUED_INDEX,
4822227d67aaSAlexander Motin 	    ("destroying device while still queued for ccbs"));
48232cefde5fSJustin T. Gibbs 
48242cefde5fSJustin T. Gibbs 	if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
48252b83592fSScott Long 		callout_stop(&device->callout);
48262cefde5fSJustin T. Gibbs 
4827227d67aaSAlexander Motin 	xpt_release_target(device->target);
4828227d67aaSAlexander Motin 
482920a7933fSAlexander Motin 	cam_ccbq_fini(&device->ccbq);
4830e6bd5983SKenneth D. Merry 	/*
4831e6bd5983SKenneth D. Merry 	 * Free allocated memory.  free(9) does nothing if the
4832e6bd5983SKenneth D. Merry 	 * supplied pointer is NULL, so it is safe to call without
4833e6bd5983SKenneth D. Merry 	 * checking.
4834e6bd5983SKenneth D. Merry 	 */
4835e6bd5983SKenneth D. Merry 	free(device->supported_vpds, M_CAMXPT);
4836e6bd5983SKenneth D. Merry 	free(device->device_id, M_CAMXPT);
48373bba3152SKenneth D. Merry 	free(device->ext_inq, M_CAMXPT);
4838e6bd5983SKenneth D. Merry 	free(device->physpath, M_CAMXPT);
4839e6bd5983SKenneth D. Merry 	free(device->rcap_buf, M_CAMXPT);
4840e6bd5983SKenneth D. Merry 	free(device->serial_num, M_CAMXPT);
4841227d67aaSAlexander Motin 	taskqueue_enqueue(xsoftc.xpt_taskq, &device->device_destroy_task);
48428b8a9b1dSJustin T. Gibbs }
48438b8a9b1dSJustin T. Gibbs 
484452c9ce25SScott Long u_int32_t
48458b8a9b1dSJustin T. Gibbs xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
48468b8a9b1dSJustin T. Gibbs {
48478b8a9b1dSJustin T. Gibbs 	int	result;
48488b8a9b1dSJustin T. Gibbs 	struct	cam_ed *dev;
48498b8a9b1dSJustin T. Gibbs 
48508b8a9b1dSJustin T. Gibbs 	dev = path->device;
4851227d67aaSAlexander Motin 	mtx_lock(&dev->sim->devq->send_mtx);
48528b8a9b1dSJustin T. Gibbs 	result = cam_ccbq_resize(&dev->ccbq, newopenings);
4853227d67aaSAlexander Motin 	mtx_unlock(&dev->sim->devq->send_mtx);
4854df8f9080SJustin T. Gibbs 	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4855df8f9080SJustin T. Gibbs 	 || (dev->inq_flags & SID_CmdQue) != 0)
4856df8f9080SJustin T. Gibbs 		dev->tag_saved_openings = newopenings;
48578b8a9b1dSJustin T. Gibbs 	return (result);
48588b8a9b1dSJustin T. Gibbs }
48598b8a9b1dSJustin T. Gibbs 
48608b8a9b1dSJustin T. Gibbs static struct cam_eb *
48618b8a9b1dSJustin T. Gibbs xpt_find_bus(path_id_t path_id)
48628b8a9b1dSJustin T. Gibbs {
48638b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus;
48648b8a9b1dSJustin T. Gibbs 
48659a7c2696SAlexander Motin 	xpt_lock_buses();
48662b83592fSScott Long 	for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
48678b8a9b1dSJustin T. Gibbs 	     bus != NULL;
48688b8a9b1dSJustin T. Gibbs 	     bus = TAILQ_NEXT(bus, links)) {
4869a5479bc5SJustin T. Gibbs 		if (bus->path_id == path_id) {
4870a5479bc5SJustin T. Gibbs 			bus->refcount++;
48718b8a9b1dSJustin T. Gibbs 			break;
48728b8a9b1dSJustin T. Gibbs 		}
4873a5479bc5SJustin T. Gibbs 	}
48749a7c2696SAlexander Motin 	xpt_unlock_buses();
48758b8a9b1dSJustin T. Gibbs 	return (bus);
48768b8a9b1dSJustin T. Gibbs }
48778b8a9b1dSJustin T. Gibbs 
48788b8a9b1dSJustin T. Gibbs static struct cam_et *
48798b8a9b1dSJustin T. Gibbs xpt_find_target(struct cam_eb *bus, target_id_t	target_id)
48808b8a9b1dSJustin T. Gibbs {
48818b8a9b1dSJustin T. Gibbs 	struct cam_et *target;
48828b8a9b1dSJustin T. Gibbs 
4883227d67aaSAlexander Motin 	mtx_assert(&bus->eb_mtx, MA_OWNED);
48848b8a9b1dSJustin T. Gibbs 	for (target = TAILQ_FIRST(&bus->et_entries);
48858b8a9b1dSJustin T. Gibbs 	     target != NULL;
48868b8a9b1dSJustin T. Gibbs 	     target = TAILQ_NEXT(target, links)) {
48878b8a9b1dSJustin T. Gibbs 		if (target->target_id == target_id) {
48888b8a9b1dSJustin T. Gibbs 			target->refcount++;
48898b8a9b1dSJustin T. Gibbs 			break;
48908b8a9b1dSJustin T. Gibbs 		}
48918b8a9b1dSJustin T. Gibbs 	}
48928b8a9b1dSJustin T. Gibbs 	return (target);
48938b8a9b1dSJustin T. Gibbs }
48948b8a9b1dSJustin T. Gibbs 
48958b8a9b1dSJustin T. Gibbs static struct cam_ed *
48968b8a9b1dSJustin T. Gibbs xpt_find_device(struct cam_et *target, lun_id_t lun_id)
48978b8a9b1dSJustin T. Gibbs {
48988b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
48998b8a9b1dSJustin T. Gibbs 
4900227d67aaSAlexander Motin 	mtx_assert(&target->bus->eb_mtx, MA_OWNED);
49018b8a9b1dSJustin T. Gibbs 	for (device = TAILQ_FIRST(&target->ed_entries);
49028b8a9b1dSJustin T. Gibbs 	     device != NULL;
49038b8a9b1dSJustin T. Gibbs 	     device = TAILQ_NEXT(device, links)) {
49048b8a9b1dSJustin T. Gibbs 		if (device->lun_id == lun_id) {
49058b8a9b1dSJustin T. Gibbs 			device->refcount++;
49068b8a9b1dSJustin T. Gibbs 			break;
49078b8a9b1dSJustin T. Gibbs 		}
49088b8a9b1dSJustin T. Gibbs 	}
49098b8a9b1dSJustin T. Gibbs 	return (device);
49108b8a9b1dSJustin T. Gibbs }
49118b8a9b1dSJustin T. Gibbs 
491230a4094fSAlexander Motin void
4913f0adc790SJustin T. Gibbs xpt_start_tags(struct cam_path *path)
4914f0adc790SJustin T. Gibbs {
4915fd21cc5eSJustin T. Gibbs 	struct ccb_relsim crs;
4916fd21cc5eSJustin T. Gibbs 	struct cam_ed *device;
4917fd21cc5eSJustin T. Gibbs 	struct cam_sim *sim;
4918fd21cc5eSJustin T. Gibbs 	int    newopenings;
4919fd21cc5eSJustin T. Gibbs 
4920fd21cc5eSJustin T. Gibbs 	device = path->device;
4921fd21cc5eSJustin T. Gibbs 	sim = path->bus->sim;
4922fd21cc5eSJustin T. Gibbs 	device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4923fd21cc5eSJustin T. Gibbs 	xpt_freeze_devq(path, /*count*/1);
4924fd21cc5eSJustin T. Gibbs 	device->inq_flags |= SID_CmdQue;
4925df8f9080SJustin T. Gibbs 	if (device->tag_saved_openings != 0)
4926df8f9080SJustin T. Gibbs 		newopenings = device->tag_saved_openings;
4927df8f9080SJustin T. Gibbs 	else
492852c9ce25SScott Long 		newopenings = min(device->maxtags,
4929df8f9080SJustin T. Gibbs 				  sim->max_tagged_dev_openings);
4930f0adc790SJustin T. Gibbs 	xpt_dev_ccbq_resize(path, newopenings);
4931581b2e78SAlexander Motin 	xpt_async(AC_GETDEV_CHANGED, path, NULL);
4932bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4933fd21cc5eSJustin T. Gibbs 	crs.ccb_h.func_code = XPT_REL_SIMQ;
4934fd21cc5eSJustin T. Gibbs 	crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4935fd21cc5eSJustin T. Gibbs 	crs.openings
4936fd21cc5eSJustin T. Gibbs 	    = crs.release_timeout
4937fd21cc5eSJustin T. Gibbs 	    = crs.qfrozen_cnt
4938fd21cc5eSJustin T. Gibbs 	    = 0;
4939fd21cc5eSJustin T. Gibbs 	xpt_action((union ccb *)&crs);
4940fd21cc5eSJustin T. Gibbs }
4941fd21cc5eSJustin T. Gibbs 
494230a4094fSAlexander Motin void
494330a4094fSAlexander Motin xpt_stop_tags(struct cam_path *path)
494430a4094fSAlexander Motin {
494530a4094fSAlexander Motin 	struct ccb_relsim crs;
494630a4094fSAlexander Motin 	struct cam_ed *device;
494730a4094fSAlexander Motin 	struct cam_sim *sim;
494830a4094fSAlexander Motin 
494930a4094fSAlexander Motin 	device = path->device;
495030a4094fSAlexander Motin 	sim = path->bus->sim;
495130a4094fSAlexander Motin 	device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
495230a4094fSAlexander Motin 	device->tag_delay_count = 0;
495330a4094fSAlexander Motin 	xpt_freeze_devq(path, /*count*/1);
495430a4094fSAlexander Motin 	device->inq_flags &= ~SID_CmdQue;
495530a4094fSAlexander Motin 	xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4956581b2e78SAlexander Motin 	xpt_async(AC_GETDEV_CHANGED, path, NULL);
495730a4094fSAlexander Motin 	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
495830a4094fSAlexander Motin 	crs.ccb_h.func_code = XPT_REL_SIMQ;
495930a4094fSAlexander Motin 	crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
496030a4094fSAlexander Motin 	crs.openings
496130a4094fSAlexander Motin 	    = crs.release_timeout
496230a4094fSAlexander Motin 	    = crs.qfrozen_cnt
496330a4094fSAlexander Motin 	    = 0;
496430a4094fSAlexander Motin 	xpt_action((union ccb *)&crs);
496530a4094fSAlexander Motin }
496630a4094fSAlexander Motin 
496783c5d981SAlexander Motin static void
496883c5d981SAlexander Motin xpt_boot_delay(void *arg)
49698b8a9b1dSJustin T. Gibbs {
49702b83592fSScott Long 
497183c5d981SAlexander Motin 	xpt_release_boot();
49728b8a9b1dSJustin T. Gibbs }
49738b8a9b1dSJustin T. Gibbs 
49748b8a9b1dSJustin T. Gibbs static void
49758b8a9b1dSJustin T. Gibbs xpt_config(void *arg)
49768b8a9b1dSJustin T. Gibbs {
49773393f8daSKenneth D. Merry 	/*
49783393f8daSKenneth D. Merry 	 * Now that interrupts are enabled, go find our devices
49793393f8daSKenneth D. Merry 	 */
4980227d67aaSAlexander Motin 	if (taskqueue_start_threads(&xsoftc.xpt_taskq, 1, PRIBIO, "CAM taskq"))
4981227d67aaSAlexander Motin 		printf("xpt_config: failed to create taskqueue thread.\n");
49828b8a9b1dSJustin T. Gibbs 
4983f0f25b9cSAlexander Motin 	/* Setup debugging path */
49848b8a9b1dSJustin T. Gibbs 	if (cam_dflags != CAM_DEBUG_NONE) {
4985227d67aaSAlexander Motin 		if (xpt_create_path(&cam_dpath, NULL,
49868b8a9b1dSJustin T. Gibbs 				    CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
49878b8a9b1dSJustin T. Gibbs 				    CAM_DEBUG_LUN) != CAM_REQ_CMP) {
49888b8a9b1dSJustin T. Gibbs 			printf("xpt_config: xpt_create_path() failed for debug"
49898b8a9b1dSJustin T. Gibbs 			       " target %d:%d:%d, debugging disabled\n",
49908b8a9b1dSJustin T. Gibbs 			       CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
49918b8a9b1dSJustin T. Gibbs 			cam_dflags = CAM_DEBUG_NONE;
49928b8a9b1dSJustin T. Gibbs 		}
49938b8a9b1dSJustin T. Gibbs 	} else
49948b8a9b1dSJustin T. Gibbs 		cam_dpath = NULL;
49958b8a9b1dSJustin T. Gibbs 
499683c5d981SAlexander Motin 	periphdriver_init(1);
499783c5d981SAlexander Motin 	xpt_hold_boot();
499883c5d981SAlexander Motin 	callout_init(&xsoftc.boot_callout, 1);
499985c9dd9dSSteven Hartland 	callout_reset_sbt(&xsoftc.boot_callout, SBT_1MS * xsoftc.boot_delay, 0,
500085c9dd9dSSteven Hartland 	    xpt_boot_delay, NULL, 0);
500183c5d981SAlexander Motin 	/* Fire up rescan thread. */
5002227d67aaSAlexander Motin 	if (kproc_kthread_add(xpt_scanner_thread, NULL, &cam_proc, NULL, 0, 0,
5003227d67aaSAlexander Motin 	    "cam", "scanner")) {
50046f15a274SAlexander Motin 		printf("xpt_config: failed to create rescan thread.\n");
50051e637ba6SAlexander Motin 	}
500683c5d981SAlexander Motin }
50078b8a9b1dSJustin T. Gibbs 
500883c5d981SAlexander Motin void
500983c5d981SAlexander Motin xpt_hold_boot(void)
501083c5d981SAlexander Motin {
501183c5d981SAlexander Motin 	xpt_lock_buses();
501283c5d981SAlexander Motin 	xsoftc.buses_to_config++;
501383c5d981SAlexander Motin 	xpt_unlock_buses();
501483c5d981SAlexander Motin }
501583c5d981SAlexander Motin 
501683c5d981SAlexander Motin void
501783c5d981SAlexander Motin xpt_release_boot(void)
501883c5d981SAlexander Motin {
501983c5d981SAlexander Motin 	xpt_lock_buses();
502083c5d981SAlexander Motin 	xsoftc.buses_to_config--;
502183c5d981SAlexander Motin 	if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
502283c5d981SAlexander Motin 		struct	xpt_task *task;
502383c5d981SAlexander Motin 
502483c5d981SAlexander Motin 		xsoftc.buses_config_done = 1;
502583c5d981SAlexander Motin 		xpt_unlock_buses();
5026ecdf1113SJustin T. Gibbs 		/* Call manually because we don't have any busses */
502783c5d981SAlexander Motin 		task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
502883c5d981SAlexander Motin 		if (task != NULL) {
502983c5d981SAlexander Motin 			TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
503083c5d981SAlexander Motin 			taskqueue_enqueue(taskqueue_thread, &task->task);
50312863f7b1SJustin T. Gibbs 		}
503283c5d981SAlexander Motin 	} else
503383c5d981SAlexander Motin 		xpt_unlock_buses();
50342863f7b1SJustin T. Gibbs }
50358b8a9b1dSJustin T. Gibbs 
50368b8a9b1dSJustin T. Gibbs /*
50378b8a9b1dSJustin T. Gibbs  * If the given device only has one peripheral attached to it, and if that
50388b8a9b1dSJustin T. Gibbs  * peripheral is the passthrough driver, announce it.  This insures that the
50398b8a9b1dSJustin T. Gibbs  * user sees some sort of announcement for every peripheral in their system.
50408b8a9b1dSJustin T. Gibbs  */
50418b8a9b1dSJustin T. Gibbs static int
50428b8a9b1dSJustin T. Gibbs xptpassannouncefunc(struct cam_ed *device, void *arg)
50438b8a9b1dSJustin T. Gibbs {
50448b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph;
50458b8a9b1dSJustin T. Gibbs 	int i;
50468b8a9b1dSJustin T. Gibbs 
50478b8a9b1dSJustin T. Gibbs 	for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
50488b8a9b1dSJustin T. Gibbs 	     periph = SLIST_NEXT(periph, periph_links), i++);
50498b8a9b1dSJustin T. Gibbs 
50508b8a9b1dSJustin T. Gibbs 	periph = SLIST_FIRST(&device->periphs);
50518b8a9b1dSJustin T. Gibbs 	if ((i == 1)
50528b8a9b1dSJustin T. Gibbs 	 && (strncmp(periph->periph_name, "pass", 4) == 0))
50538b8a9b1dSJustin T. Gibbs 		xpt_announce_periph(periph, NULL);
50548b8a9b1dSJustin T. Gibbs 
50558b8a9b1dSJustin T. Gibbs 	return(1);
50568b8a9b1dSJustin T. Gibbs }
50578b8a9b1dSJustin T. Gibbs 
50588b8a9b1dSJustin T. Gibbs static void
50592b83592fSScott Long xpt_finishconfig_task(void *context, int pending)
50608b8a9b1dSJustin T. Gibbs {
50618b8a9b1dSJustin T. Gibbs 
506283c5d981SAlexander Motin 	periphdriver_init(2);
50632b83592fSScott Long 	/*
50642b83592fSScott Long 	 * Check for devices with no "standard" peripheral driver
50652b83592fSScott Long 	 * attached.  For any devices like that, announce the
50662b83592fSScott Long 	 * passthrough driver so the user will see something.
50672b83592fSScott Long 	 */
50683089bb2eSAlexander Motin 	if (!bootverbose)
50692b83592fSScott Long 		xpt_for_all_devices(xptpassannouncefunc, NULL);
50702b83592fSScott Long 
50712b83592fSScott Long 	/* Release our hook so that the boot can continue. */
50722b83592fSScott Long 	config_intrhook_disestablish(xsoftc.xpt_config_hook);
50730dd50e9bSScott Long 	free(xsoftc.xpt_config_hook, M_CAMXPT);
50742b83592fSScott Long 	xsoftc.xpt_config_hook = NULL;
50752b83592fSScott Long 
50762b83592fSScott Long 	free(context, M_CAMXPT);
50772b83592fSScott Long }
50782b83592fSScott Long 
507985d92640SScott Long cam_status
508085d92640SScott Long xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
508185d92640SScott Long 		   struct cam_path *path)
508285d92640SScott Long {
508385d92640SScott Long 	struct ccb_setasync csa;
508485d92640SScott Long 	cam_status status;
508585d92640SScott Long 	int xptpath = 0;
508685d92640SScott Long 
508785d92640SScott Long 	if (path == NULL) {
508885d92640SScott Long 		status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
508985d92640SScott Long 					 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5090227d67aaSAlexander Motin 		if (status != CAM_REQ_CMP)
509185d92640SScott Long 			return (status);
5092227d67aaSAlexander Motin 		xpt_path_lock(path);
509385d92640SScott Long 		xptpath = 1;
509485d92640SScott Long 	}
509585d92640SScott Long 
509683c5d981SAlexander Motin 	xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
509785d92640SScott Long 	csa.ccb_h.func_code = XPT_SASYNC_CB;
509885d92640SScott Long 	csa.event_enable = event;
509985d92640SScott Long 	csa.callback = cbfunc;
510085d92640SScott Long 	csa.callback_arg = cbarg;
510185d92640SScott Long 	xpt_action((union ccb *)&csa);
510285d92640SScott Long 	status = csa.ccb_h.status;
51033501942bSJustin T. Gibbs 
510469be012fSWarner Losh 	CAM_DEBUG(csa.ccb_h.path, CAM_DEBUG_TRACE,
510569be012fSWarner Losh 	    ("xpt_register_async: func %p\n", cbfunc));
510669be012fSWarner Losh 
510785d92640SScott Long 	if (xptpath) {
5108227d67aaSAlexander Motin 		xpt_path_unlock(path);
510985d92640SScott Long 		xpt_free_path(path);
51103501942bSJustin T. Gibbs 	}
51117685edecSAlexander Motin 
51127685edecSAlexander Motin 	if ((status == CAM_REQ_CMP) &&
51137685edecSAlexander Motin 	    (csa.event_enable & AC_FOUND_DEVICE)) {
51147685edecSAlexander Motin 		/*
51157685edecSAlexander Motin 		 * Get this peripheral up to date with all
51167685edecSAlexander Motin 		 * the currently existing devices.
51177685edecSAlexander Motin 		 */
51187685edecSAlexander Motin 		xpt_for_all_devices(xptsetasyncfunc, &csa);
51197685edecSAlexander Motin 	}
51207685edecSAlexander Motin 	if ((status == CAM_REQ_CMP) &&
51217685edecSAlexander Motin 	    (csa.event_enable & AC_PATH_REGISTERED)) {
51227685edecSAlexander Motin 		/*
51237685edecSAlexander Motin 		 * Get this peripheral up to date with all
51247685edecSAlexander Motin 		 * the currently existing busses.
51257685edecSAlexander Motin 		 */
51267685edecSAlexander Motin 		xpt_for_all_busses(xptsetasyncbusfunc, &csa);
51277685edecSAlexander Motin 	}
51283501942bSJustin T. Gibbs 
512985d92640SScott Long 	return (status);
513085d92640SScott Long }
513185d92640SScott Long 
51328b8a9b1dSJustin T. Gibbs static void
51338b8a9b1dSJustin T. Gibbs xptaction(struct cam_sim *sim, union ccb *work_ccb)
51348b8a9b1dSJustin T. Gibbs {
51358b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
51368b8a9b1dSJustin T. Gibbs 
51378b8a9b1dSJustin T. Gibbs 	switch (work_ccb->ccb_h.func_code) {
51388b8a9b1dSJustin T. Gibbs 	/* Common cases first */
51398b8a9b1dSJustin T. Gibbs 	case XPT_PATH_INQ:		/* Path routing inquiry */
51408b8a9b1dSJustin T. Gibbs 	{
51418b8a9b1dSJustin T. Gibbs 		struct ccb_pathinq *cpi;
51428b8a9b1dSJustin T. Gibbs 
51438b8a9b1dSJustin T. Gibbs 		cpi = &work_ccb->cpi;
51448b8a9b1dSJustin T. Gibbs 		cpi->version_num = 1; /* XXX??? */
51458b8a9b1dSJustin T. Gibbs 		cpi->hba_inquiry = 0;
51468b8a9b1dSJustin T. Gibbs 		cpi->target_sprt = 0;
51478b8a9b1dSJustin T. Gibbs 		cpi->hba_misc = 0;
51488b8a9b1dSJustin T. Gibbs 		cpi->hba_eng_cnt = 0;
51498b8a9b1dSJustin T. Gibbs 		cpi->max_target = 0;
51508b8a9b1dSJustin T. Gibbs 		cpi->max_lun = 0;
51518b8a9b1dSJustin T. Gibbs 		cpi->initiator_id = 0;
51528b8a9b1dSJustin T. Gibbs 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
51538b8a9b1dSJustin T. Gibbs 		strncpy(cpi->hba_vid, "", HBA_IDLEN);
51548b8a9b1dSJustin T. Gibbs 		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
51558b8a9b1dSJustin T. Gibbs 		cpi->unit_number = sim->unit_number;
51568b8a9b1dSJustin T. Gibbs 		cpi->bus_id = sim->bus_id;
51579deea857SKenneth D. Merry 		cpi->base_transfer_speed = 0;
51583393f8daSKenneth D. Merry 		cpi->protocol = PROTO_UNSPECIFIED;
51593393f8daSKenneth D. Merry 		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
51603393f8daSKenneth D. Merry 		cpi->transport = XPORT_UNSPECIFIED;
51613393f8daSKenneth D. Merry 		cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
51628b8a9b1dSJustin T. Gibbs 		cpi->ccb_h.status = CAM_REQ_CMP;
51638b8a9b1dSJustin T. Gibbs 		xpt_done(work_ccb);
51648b8a9b1dSJustin T. Gibbs 		break;
51658b8a9b1dSJustin T. Gibbs 	}
51668b8a9b1dSJustin T. Gibbs 	default:
51678b8a9b1dSJustin T. Gibbs 		work_ccb->ccb_h.status = CAM_REQ_INVALID;
51688b8a9b1dSJustin T. Gibbs 		xpt_done(work_ccb);
51698b8a9b1dSJustin T. Gibbs 		break;
51708b8a9b1dSJustin T. Gibbs 	}
51718b8a9b1dSJustin T. Gibbs }
51728b8a9b1dSJustin T. Gibbs 
51738b8a9b1dSJustin T. Gibbs /*
5174434bbf6eSJustin T. Gibbs  * The xpt as a "controller" has no interrupt sources, so polling
5175434bbf6eSJustin T. Gibbs  * is a no-op.
5176434bbf6eSJustin T. Gibbs  */
5177434bbf6eSJustin T. Gibbs static void
5178434bbf6eSJustin T. Gibbs xptpoll(struct cam_sim *sim)
5179434bbf6eSJustin T. Gibbs {
5180434bbf6eSJustin T. Gibbs }
5181434bbf6eSJustin T. Gibbs 
51822b83592fSScott Long void
51832b83592fSScott Long xpt_lock_buses(void)
51842b83592fSScott Long {
51852b83592fSScott Long 	mtx_lock(&xsoftc.xpt_topo_lock);
51862b83592fSScott Long }
51872b83592fSScott Long 
51882b83592fSScott Long void
51892b83592fSScott Long xpt_unlock_buses(void)
51902b83592fSScott Long {
51912b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_topo_lock);
51922b83592fSScott Long }
51932b83592fSScott Long 
5194227d67aaSAlexander Motin struct mtx *
5195227d67aaSAlexander Motin xpt_path_mtx(struct cam_path *path)
51968b8a9b1dSJustin T. Gibbs {
5197227d67aaSAlexander Motin 
5198227d67aaSAlexander Motin 	return (&path->device->device_mtx);
5199227d67aaSAlexander Motin }
5200227d67aaSAlexander Motin 
5201227d67aaSAlexander Motin static void
5202227d67aaSAlexander Motin xpt_done_process(struct ccb_hdr *ccb_h)
5203227d67aaSAlexander Motin {
52042b83592fSScott Long 	struct cam_sim *sim;
5205227d67aaSAlexander Motin 	struct cam_devq *devq;
5206227d67aaSAlexander Motin 	struct mtx *mtx = NULL;
52078b8a9b1dSJustin T. Gibbs 
52088532d381SConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
52098532d381SConrad Meyer 	struct ccb_scsiio *csio;
52108532d381SConrad Meyer 
52118532d381SConrad Meyer 	if (ccb_h->func_code == XPT_SCSI_IO) {
52128532d381SConrad Meyer 		csio = &((union ccb *)ccb_h)->csio;
52138532d381SConrad Meyer 		if (csio->bio != NULL)
52148532d381SConrad Meyer 			biotrack(csio->bio, __func__);
52158532d381SConrad Meyer 	}
52168532d381SConrad Meyer #endif
52178532d381SConrad Meyer 
52188b8a9b1dSJustin T. Gibbs 	if (ccb_h->flags & CAM_HIGH_POWER) {
52198b8a9b1dSJustin T. Gibbs 		struct highpowerlist	*hphead;
5220ea541bfdSAlexander Motin 		struct cam_ed		*device;
52218b8a9b1dSJustin T. Gibbs 
5222daa5487fSAlexander Motin 		mtx_lock(&xsoftc.xpt_highpower_lock);
52232b83592fSScott Long 		hphead = &xsoftc.highpowerq;
52248b8a9b1dSJustin T. Gibbs 
5225ea541bfdSAlexander Motin 		device = STAILQ_FIRST(hphead);
52268b8a9b1dSJustin T. Gibbs 
52278b8a9b1dSJustin T. Gibbs 		/*
52288b8a9b1dSJustin T. Gibbs 		 * Increment the count since this command is done.
52298b8a9b1dSJustin T. Gibbs 		 */
52302b83592fSScott Long 		xsoftc.num_highpower++;
52318b8a9b1dSJustin T. Gibbs 
52328b8a9b1dSJustin T. Gibbs 		/*
52338b8a9b1dSJustin T. Gibbs 		 * Any high powered commands queued up?
52348b8a9b1dSJustin T. Gibbs 		 */
5235ea541bfdSAlexander Motin 		if (device != NULL) {
52368b8a9b1dSJustin T. Gibbs 
5237ea541bfdSAlexander Motin 			STAILQ_REMOVE_HEAD(hphead, highpowerq_entry);
5238daa5487fSAlexander Motin 			mtx_unlock(&xsoftc.xpt_highpower_lock);
52398b8a9b1dSJustin T. Gibbs 
5240227d67aaSAlexander Motin 			mtx_lock(&device->sim->devq->send_mtx);
5241ea541bfdSAlexander Motin 			xpt_release_devq_device(device,
52422cefde5fSJustin T. Gibbs 					 /*count*/1, /*runqueue*/TRUE);
5243227d67aaSAlexander Motin 			mtx_unlock(&device->sim->devq->send_mtx);
52442b83592fSScott Long 		} else
5245daa5487fSAlexander Motin 			mtx_unlock(&xsoftc.xpt_highpower_lock);
52468b8a9b1dSJustin T. Gibbs 	}
52472b83592fSScott Long 
5248227d67aaSAlexander Motin 	sim = ccb_h->path->bus->sim;
5249227d67aaSAlexander Motin 
5250227d67aaSAlexander Motin 	if (ccb_h->status & CAM_RELEASE_SIMQ) {
5251227d67aaSAlexander Motin 		xpt_release_simq(sim, /*run_queue*/FALSE);
5252227d67aaSAlexander Motin 		ccb_h->status &= ~CAM_RELEASE_SIMQ;
5253227d67aaSAlexander Motin 	}
5254227d67aaSAlexander Motin 
5255227d67aaSAlexander Motin 	if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5256227d67aaSAlexander Motin 	 && (ccb_h->status & CAM_DEV_QFRZN)) {
5257d718926bSAlexander Motin 		xpt_release_devq(ccb_h->path, /*count*/1, /*run_queue*/TRUE);
5258227d67aaSAlexander Motin 		ccb_h->status &= ~CAM_DEV_QFRZN;
5259227d67aaSAlexander Motin 	}
5260227d67aaSAlexander Motin 
5261227d67aaSAlexander Motin 	devq = sim->devq;
52629deea857SKenneth D. Merry 	if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5263227d67aaSAlexander Motin 		struct cam_ed *dev = ccb_h->path->device;
52648b8a9b1dSJustin T. Gibbs 
5265227d67aaSAlexander Motin 		mtx_lock(&devq->send_mtx);
5266227d67aaSAlexander Motin 		devq->send_active--;
5267227d67aaSAlexander Motin 		devq->send_openings++;
52688b8a9b1dSJustin T. Gibbs 		cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
52698b8a9b1dSJustin T. Gibbs 
5270b9c473b2SAlexander Motin 		if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
52718b8a9b1dSJustin T. Gibbs 		  && (dev->ccbq.dev_active == 0))) {
5272b9c473b2SAlexander Motin 			dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5273227d67aaSAlexander Motin 			xpt_release_devq_device(dev, /*count*/1,
5274b9c473b2SAlexander Motin 					 /*run_queue*/FALSE);
5275b9c473b2SAlexander Motin 		}
5276b9c473b2SAlexander Motin 
5277b9c473b2SAlexander Motin 		if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5278b9c473b2SAlexander Motin 		  && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5279b9c473b2SAlexander Motin 			dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5280227d67aaSAlexander Motin 			xpt_release_devq_device(dev, /*count*/1,
528183c5d981SAlexander Motin 					 /*run_queue*/FALSE);
52828b8a9b1dSJustin T. Gibbs 		}
52838b8a9b1dSJustin T. Gibbs 
5284227d67aaSAlexander Motin 		if (!device_is_queued(dev))
5285227d67aaSAlexander Motin 			(void)xpt_schedule_devq(devq, dev);
5286d718926bSAlexander Motin 		xpt_run_devq(devq);
5287227d67aaSAlexander Motin 		mtx_unlock(&devq->send_mtx);
5288227d67aaSAlexander Motin 
5289227d67aaSAlexander Motin 		if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0) {
5290227d67aaSAlexander Motin 			mtx = xpt_path_mtx(ccb_h->path);
5291227d67aaSAlexander Motin 			mtx_lock(mtx);
5292227d67aaSAlexander Motin 
5293fd21cc5eSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5294fd21cc5eSJustin T. Gibbs 			 && (--dev->tag_delay_count == 0))
5295fd21cc5eSJustin T. Gibbs 				xpt_start_tags(ccb_h->path);
52963501942bSJustin T. Gibbs 		}
52978b8a9b1dSJustin T. Gibbs 	}
52988b8a9b1dSJustin T. Gibbs 
5299227d67aaSAlexander Motin 	if ((ccb_h->flags & CAM_UNLOCKED) == 0) {
5300227d67aaSAlexander Motin 		if (mtx == NULL) {
5301227d67aaSAlexander Motin 			mtx = xpt_path_mtx(ccb_h->path);
5302227d67aaSAlexander Motin 			mtx_lock(mtx);
5303434bbf6eSJustin T. Gibbs 		}
5304227d67aaSAlexander Motin 	} else {
5305227d67aaSAlexander Motin 		if (mtx != NULL) {
5306227d67aaSAlexander Motin 			mtx_unlock(mtx);
5307227d67aaSAlexander Motin 			mtx = NULL;
5308227d67aaSAlexander Motin 		}
53098b8a9b1dSJustin T. Gibbs 	}
53108b8a9b1dSJustin T. Gibbs 
53118b8a9b1dSJustin T. Gibbs 	/* Call the peripheral driver's callback */
5312f1486b51SAlexander Motin 	ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5313434bbf6eSJustin T. Gibbs 	(*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
5314227d67aaSAlexander Motin 	if (mtx != NULL)
5315227d67aaSAlexander Motin 		mtx_unlock(mtx);
5316227d67aaSAlexander Motin }
5317227d67aaSAlexander Motin 
5318227d67aaSAlexander Motin void
5319227d67aaSAlexander Motin xpt_done_td(void *arg)
5320227d67aaSAlexander Motin {
5321227d67aaSAlexander Motin 	struct cam_doneq *queue = arg;
5322227d67aaSAlexander Motin 	struct ccb_hdr *ccb_h;
5323227d67aaSAlexander Motin 	STAILQ_HEAD(, ccb_hdr)	doneq;
5324227d67aaSAlexander Motin 
5325227d67aaSAlexander Motin 	STAILQ_INIT(&doneq);
5326227d67aaSAlexander Motin 	mtx_lock(&queue->cam_doneq_mtx);
5327227d67aaSAlexander Motin 	while (1) {
5328227d67aaSAlexander Motin 		while (STAILQ_EMPTY(&queue->cam_doneq)) {
5329227d67aaSAlexander Motin 			queue->cam_doneq_sleep = 1;
5330227d67aaSAlexander Motin 			msleep(&queue->cam_doneq, &queue->cam_doneq_mtx,
5331227d67aaSAlexander Motin 			    PRIBIO, "-", 0);
5332227d67aaSAlexander Motin 			queue->cam_doneq_sleep = 0;
5333227d67aaSAlexander Motin 		}
5334227d67aaSAlexander Motin 		STAILQ_CONCAT(&doneq, &queue->cam_doneq);
5335227d67aaSAlexander Motin 		mtx_unlock(&queue->cam_doneq_mtx);
5336227d67aaSAlexander Motin 
5337227d67aaSAlexander Motin 		THREAD_NO_SLEEPING();
5338227d67aaSAlexander Motin 		while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) {
5339227d67aaSAlexander Motin 			STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe);
5340227d67aaSAlexander Motin 			xpt_done_process(ccb_h);
5341227d67aaSAlexander Motin 		}
5342227d67aaSAlexander Motin 		THREAD_SLEEPING_OK();
5343227d67aaSAlexander Motin 
5344227d67aaSAlexander Motin 		mtx_lock(&queue->cam_doneq_mtx);
5345227d67aaSAlexander Motin 	}
5346227d67aaSAlexander Motin }
5347227d67aaSAlexander Motin 
5348227d67aaSAlexander Motin static void
5349227d67aaSAlexander Motin camisr_runqueue(void)
5350227d67aaSAlexander Motin {
5351227d67aaSAlexander Motin 	struct	ccb_hdr *ccb_h;
5352227d67aaSAlexander Motin 	struct cam_doneq *queue;
5353227d67aaSAlexander Motin 	int i;
5354227d67aaSAlexander Motin 
5355227d67aaSAlexander Motin 	/* Process global queues. */
5356227d67aaSAlexander Motin 	for (i = 0; i < cam_num_doneqs; i++) {
5357227d67aaSAlexander Motin 		queue = &cam_doneqs[i];
5358227d67aaSAlexander Motin 		mtx_lock(&queue->cam_doneq_mtx);
5359227d67aaSAlexander Motin 		while ((ccb_h = STAILQ_FIRST(&queue->cam_doneq)) != NULL) {
5360227d67aaSAlexander Motin 			STAILQ_REMOVE_HEAD(&queue->cam_doneq, sim_links.stqe);
5361227d67aaSAlexander Motin 			mtx_unlock(&queue->cam_doneq_mtx);
5362227d67aaSAlexander Motin 			xpt_done_process(ccb_h);
5363227d67aaSAlexander Motin 			mtx_lock(&queue->cam_doneq_mtx);
5364227d67aaSAlexander Motin 		}
5365227d67aaSAlexander Motin 		mtx_unlock(&queue->cam_doneq_mtx);
53668b8a9b1dSJustin T. Gibbs 	}
53678b8a9b1dSJustin T. Gibbs }
536869be012fSWarner Losh 
536969be012fSWarner Losh struct kv
537069be012fSWarner Losh {
537169be012fSWarner Losh 	uint32_t v;
537269be012fSWarner Losh 	const char *name;
537369be012fSWarner Losh };
537469be012fSWarner Losh 
537569be012fSWarner Losh static struct kv map[] = {
537669be012fSWarner Losh 	{ XPT_NOOP, "XPT_NOOP" },
537769be012fSWarner Losh 	{ XPT_SCSI_IO, "XPT_SCSI_IO" },
537869be012fSWarner Losh 	{ XPT_GDEV_TYPE, "XPT_GDEV_TYPE" },
537969be012fSWarner Losh 	{ XPT_GDEVLIST, "XPT_GDEVLIST" },
538069be012fSWarner Losh 	{ XPT_PATH_INQ, "XPT_PATH_INQ" },
538169be012fSWarner Losh 	{ XPT_REL_SIMQ, "XPT_REL_SIMQ" },
538269be012fSWarner Losh 	{ XPT_SASYNC_CB, "XPT_SASYNC_CB" },
538369be012fSWarner Losh 	{ XPT_SDEV_TYPE, "XPT_SDEV_TYPE" },
538469be012fSWarner Losh 	{ XPT_SCAN_BUS, "XPT_SCAN_BUS" },
538569be012fSWarner Losh 	{ XPT_DEV_MATCH, "XPT_DEV_MATCH" },
538669be012fSWarner Losh 	{ XPT_DEBUG, "XPT_DEBUG" },
538769be012fSWarner Losh 	{ XPT_PATH_STATS, "XPT_PATH_STATS" },
538869be012fSWarner Losh 	{ XPT_GDEV_STATS, "XPT_GDEV_STATS" },
538969be012fSWarner Losh 	{ XPT_DEV_ADVINFO, "XPT_DEV_ADVINFO" },
539069be012fSWarner Losh 	{ XPT_ASYNC, "XPT_ASYNC" },
539169be012fSWarner Losh 	{ XPT_ABORT, "XPT_ABORT" },
539269be012fSWarner Losh 	{ XPT_RESET_BUS, "XPT_RESET_BUS" },
539369be012fSWarner Losh 	{ XPT_RESET_DEV, "XPT_RESET_DEV" },
539469be012fSWarner Losh 	{ XPT_TERM_IO, "XPT_TERM_IO" },
539569be012fSWarner Losh 	{ XPT_SCAN_LUN, "XPT_SCAN_LUN" },
539669be012fSWarner Losh 	{ XPT_GET_TRAN_SETTINGS, "XPT_GET_TRAN_SETTINGS" },
539769be012fSWarner Losh 	{ XPT_SET_TRAN_SETTINGS, "XPT_SET_TRAN_SETTINGS" },
539869be012fSWarner Losh 	{ XPT_CALC_GEOMETRY, "XPT_CALC_GEOMETRY" },
539969be012fSWarner Losh 	{ XPT_ATA_IO, "XPT_ATA_IO" },
540069be012fSWarner Losh 	{ XPT_GET_SIM_KNOB, "XPT_GET_SIM_KNOB" },
540169be012fSWarner Losh 	{ XPT_SET_SIM_KNOB, "XPT_SET_SIM_KNOB" },
54027b05c3e3SWarner Losh 	{ XPT_NVME_IO, "XPT_NVME_IO" },
5403bbb19fc7SWarner Losh 	{ XPT_MMCSD_IO, "XPT_MMCSD_IO" },
540469be012fSWarner Losh 	{ XPT_SMP_IO, "XPT_SMP_IO" },
540569be012fSWarner Losh 	{ XPT_SCAN_TGT, "XPT_SCAN_TGT" },
540669be012fSWarner Losh 	{ XPT_ENG_INQ, "XPT_ENG_INQ" },
540769be012fSWarner Losh 	{ XPT_ENG_EXEC, "XPT_ENG_EXEC" },
540869be012fSWarner Losh 	{ XPT_EN_LUN, "XPT_EN_LUN" },
540969be012fSWarner Losh 	{ XPT_TARGET_IO, "XPT_TARGET_IO" },
541069be012fSWarner Losh 	{ XPT_ACCEPT_TARGET_IO, "XPT_ACCEPT_TARGET_IO" },
541169be012fSWarner Losh 	{ XPT_CONT_TARGET_IO, "XPT_CONT_TARGET_IO" },
541269be012fSWarner Losh 	{ XPT_IMMED_NOTIFY, "XPT_IMMED_NOTIFY" },
541369be012fSWarner Losh 	{ XPT_NOTIFY_ACK, "XPT_NOTIFY_ACK" },
541469be012fSWarner Losh 	{ XPT_IMMEDIATE_NOTIFY, "XPT_IMMEDIATE_NOTIFY" },
541569be012fSWarner Losh 	{ XPT_NOTIFY_ACKNOWLEDGE, "XPT_NOTIFY_ACKNOWLEDGE" },
541669be012fSWarner Losh 	{ 0, 0 }
541769be012fSWarner Losh };
541869be012fSWarner Losh 
541969be012fSWarner Losh static const char *
542069be012fSWarner Losh xpt_action_name(uint32_t action)
542169be012fSWarner Losh {
542269be012fSWarner Losh 	static char buffer[32];	/* Only for unknown messages -- racy */
542369be012fSWarner Losh 	struct kv *walker = map;
542469be012fSWarner Losh 
542569be012fSWarner Losh 	while (walker->name != NULL) {
542669be012fSWarner Losh 		if (walker->v == action)
542769be012fSWarner Losh 			return (walker->name);
542869be012fSWarner Losh 		walker++;
542969be012fSWarner Losh 	}
543069be012fSWarner Losh 
543169be012fSWarner Losh 	snprintf(buffer, sizeof(buffer), "%#x", action);
543269be012fSWarner Losh 	return (buffer);
543369be012fSWarner Losh }
5434