xref: /freebsd/sys/cam/cam_xpt.c (revision a29779e8651b8f03a2722ffb6c29cc445636b69d)
1898b0535SWarner Losh /*-
28b8a9b1dSJustin T. Gibbs  * Implementation of the Common Access Method Transport (XPT) layer.
38b8a9b1dSJustin T. Gibbs  *
4c8bead2aSJustin T. Gibbs  * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
559190eaaSKenneth D. Merry  * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
68b8a9b1dSJustin T. Gibbs  * All rights reserved.
78b8a9b1dSJustin T. Gibbs  *
88b8a9b1dSJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
98b8a9b1dSJustin T. Gibbs  * modification, are permitted provided that the following conditions
108b8a9b1dSJustin T. Gibbs  * are met:
118b8a9b1dSJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
128b8a9b1dSJustin T. Gibbs  *    notice, this list of conditions, and the following disclaimer,
138b8a9b1dSJustin T. Gibbs  *    without modification, immediately at the beginning of the file.
148b8a9b1dSJustin T. Gibbs  * 2. The name of the author may not be used to endorse or promote products
158b8a9b1dSJustin T. Gibbs  *    derived from this software without specific prior written permission.
168b8a9b1dSJustin T. Gibbs  *
178b8a9b1dSJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
188b8a9b1dSJustin T. Gibbs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
198b8a9b1dSJustin T. Gibbs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
208b8a9b1dSJustin T. Gibbs  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
218b8a9b1dSJustin T. Gibbs  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
228b8a9b1dSJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
238b8a9b1dSJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
248b8a9b1dSJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
258b8a9b1dSJustin T. Gibbs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
268b8a9b1dSJustin T. Gibbs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
278b8a9b1dSJustin T. Gibbs  * SUCH DAMAGE.
288b8a9b1dSJustin T. Gibbs  */
299c963d87SDavid E. O'Brien 
309c963d87SDavid E. O'Brien #include <sys/cdefs.h>
319c963d87SDavid E. O'Brien __FBSDID("$FreeBSD$");
329c963d87SDavid E. O'Brien 
338b8a9b1dSJustin T. Gibbs #include <sys/param.h>
349a94c9c5SJohn Baldwin #include <sys/bus.h>
358b8a9b1dSJustin T. Gibbs #include <sys/systm.h>
368b8a9b1dSJustin T. Gibbs #include <sys/types.h>
378b8a9b1dSJustin T. Gibbs #include <sys/malloc.h>
388b8a9b1dSJustin T. Gibbs #include <sys/kernel.h>
3987cfaf0eSJustin T. Gibbs #include <sys/time.h>
408b8a9b1dSJustin T. Gibbs #include <sys/conf.h>
418b8a9b1dSJustin T. Gibbs #include <sys/fcntl.h>
425d754af7SMatt Jacob #include <sys/interrupt.h>
433393f8daSKenneth D. Merry #include <sys/sbuf.h>
442b83592fSScott Long #include <sys/taskqueue.h>
458b8a9b1dSJustin T. Gibbs 
46ef3cf714SScott Long #include <sys/lock.h>
47ef3cf714SScott Long #include <sys/mutex.h>
483b87a552SMatt Jacob #include <sys/sysctl.h>
499e6461a2SMatt Jacob #include <sys/kthread.h>
50ef3cf714SScott Long 
518b8a9b1dSJustin T. Gibbs #include <cam/cam.h>
528b8a9b1dSJustin T. Gibbs #include <cam/cam_ccb.h>
538b8a9b1dSJustin T. Gibbs #include <cam/cam_periph.h>
5452c9ce25SScott Long #include <cam/cam_queue.h>
558b8a9b1dSJustin T. Gibbs #include <cam/cam_sim.h>
568b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt.h>
578b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_sim.h>
588b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_periph.h>
5952c9ce25SScott Long #include <cam/cam_xpt_internal.h>
608b8a9b1dSJustin T. Gibbs #include <cam/cam_debug.h>
6125a2902cSScott Long #include <cam/cam_compat.h>
628b8a9b1dSJustin T. Gibbs 
638b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_all.h>
648b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_message.h>
658b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_pass.h>
66abef0e67SMarius Strobl 
67abef0e67SMarius Strobl #include <machine/md_var.h>	/* geometry translation */
68f0d9af51SMatt Jacob #include <machine/stdarg.h>	/* for xpt_print below */
69abef0e67SMarius Strobl 
708b8a9b1dSJustin T. Gibbs #include "opt_cam.h"
718b8a9b1dSJustin T. Gibbs 
7252c9ce25SScott Long /*
7352c9ce25SScott Long  * This is the maximum number of high powered commands (e.g. start unit)
7452c9ce25SScott Long  * that can be outstanding at a particular time.
7552c9ce25SScott Long  */
7652c9ce25SScott Long #ifndef CAM_MAX_HIGHPOWER
7752c9ce25SScott Long #define CAM_MAX_HIGHPOWER  4
7852c9ce25SScott Long #endif
7952c9ce25SScott Long 
808b8a9b1dSJustin T. Gibbs /* Datastructures internal to the xpt layer */
81362abc44STai-hwa Liang MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
82596ee08fSAlexander Motin MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices");
83596ee08fSAlexander Motin MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs");
84596ee08fSAlexander Motin MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths");
858b8a9b1dSJustin T. Gibbs 
862b83592fSScott Long /* Object for defering XPT actions to a taskqueue */
872b83592fSScott Long struct xpt_task {
882b83592fSScott Long 	struct task	task;
8984f82481SScott Long 	void		*data1;
9084f82481SScott Long 	uintptr_t	data2;
912b83592fSScott Long };
922b83592fSScott Long 
938b8a9b1dSJustin T. Gibbs typedef enum {
948b8a9b1dSJustin T. Gibbs 	XPT_FLAG_OPEN		= 0x01
958b8a9b1dSJustin T. Gibbs } xpt_flags;
968b8a9b1dSJustin T. Gibbs 
978b8a9b1dSJustin T. Gibbs struct xpt_softc {
988b8a9b1dSJustin T. Gibbs 	xpt_flags		flags;
992b83592fSScott Long 
1002b83592fSScott Long 	/* number of high powered commands that can go through right now */
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;
1208b8a9b1dSJustin T. Gibbs };
1218b8a9b1dSJustin T. Gibbs 
1228b8a9b1dSJustin T. Gibbs typedef enum {
1238b8a9b1dSJustin T. Gibbs 	DM_RET_COPY		= 0x01,
1248b8a9b1dSJustin T. Gibbs 	DM_RET_FLAG_MASK	= 0x0f,
1258b8a9b1dSJustin T. Gibbs 	DM_RET_NONE		= 0x00,
1268b8a9b1dSJustin T. Gibbs 	DM_RET_STOP		= 0x10,
1278b8a9b1dSJustin T. Gibbs 	DM_RET_DESCEND		= 0x20,
1288b8a9b1dSJustin T. Gibbs 	DM_RET_ERROR		= 0x30,
1298b8a9b1dSJustin T. Gibbs 	DM_RET_ACTION_MASK	= 0xf0
1308b8a9b1dSJustin T. Gibbs } dev_match_ret;
1318b8a9b1dSJustin T. Gibbs 
1328b8a9b1dSJustin T. Gibbs typedef enum {
1338b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_BUS,
1348b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_TARGET,
1358b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_DEVICE,
1368b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_PERIPH
1378b8a9b1dSJustin T. Gibbs } xpt_traverse_depth;
1388b8a9b1dSJustin T. Gibbs 
1398b8a9b1dSJustin T. Gibbs struct xpt_traverse_config {
1408b8a9b1dSJustin T. Gibbs 	xpt_traverse_depth	depth;
1418b8a9b1dSJustin T. Gibbs 	void			*tr_func;
1428b8a9b1dSJustin T. Gibbs 	void			*tr_arg;
1438b8a9b1dSJustin T. Gibbs };
1448b8a9b1dSJustin T. Gibbs 
1458b8a9b1dSJustin T. Gibbs typedef	int	xpt_busfunc_t (struct cam_eb *bus, void *arg);
1468b8a9b1dSJustin T. Gibbs typedef	int	xpt_targetfunc_t (struct cam_et *target, void *arg);
1478b8a9b1dSJustin T. Gibbs typedef	int	xpt_devicefunc_t (struct cam_ed *device, void *arg);
1488b8a9b1dSJustin T. Gibbs typedef	int	xpt_periphfunc_t (struct cam_periph *periph, void *arg);
1498b8a9b1dSJustin T. Gibbs typedef int	xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
1508b8a9b1dSJustin T. Gibbs 
1518b8a9b1dSJustin T. Gibbs /* Transport layer configuration information */
1528b8a9b1dSJustin T. Gibbs static struct xpt_softc xsoftc;
1538b8a9b1dSJustin T. Gibbs 
15483c5d981SAlexander Motin TUNABLE_INT("kern.cam.boot_delay", &xsoftc.boot_delay);
15583c5d981SAlexander Motin SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
15683c5d981SAlexander Motin            &xsoftc.boot_delay, 0, "Bus registration wait time");
15783c5d981SAlexander Motin 
1588b8a9b1dSJustin T. Gibbs /* Queues for our software interrupt handler */
159e3975643SJake Burkholder typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
1609758cc83SScott Long typedef TAILQ_HEAD(cam_simq, cam_sim) cam_simq_t;
1619758cc83SScott Long static cam_simq_t cam_simq;
1629758cc83SScott Long static struct mtx cam_simq_lock;
1638b8a9b1dSJustin T. Gibbs 
1642b83592fSScott Long /* Pointers to software interrupt handlers */
1652b83592fSScott Long static void *cambio_ih;
1668b8a9b1dSJustin T. Gibbs 
1679a1c8571SNick Hibma struct cam_periph *xpt_periph;
1689a1c8571SNick Hibma 
1698b8a9b1dSJustin T. Gibbs static periph_init_t xpt_periph_init;
1708b8a9b1dSJustin T. Gibbs 
1718b8a9b1dSJustin T. Gibbs static struct periph_driver xpt_driver =
1728b8a9b1dSJustin T. Gibbs {
1738b8a9b1dSJustin T. Gibbs 	xpt_periph_init, "xpt",
1741e637ba6SAlexander Motin 	TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
1751e637ba6SAlexander Motin 	CAM_PERIPH_DRV_EARLY
1768b8a9b1dSJustin T. Gibbs };
1778b8a9b1dSJustin T. Gibbs 
1780b7c27b9SPeter Wemm PERIPHDRIVER_DECLARE(xpt, xpt_driver);
1798b8a9b1dSJustin T. Gibbs 
1808b8a9b1dSJustin T. Gibbs static d_open_t xptopen;
1818b8a9b1dSJustin T. Gibbs static d_close_t xptclose;
1828b8a9b1dSJustin T. Gibbs static d_ioctl_t xptioctl;
18325a2902cSScott Long static d_ioctl_t xptdoioctl;
1848b8a9b1dSJustin T. Gibbs 
1854e2f199eSPoul-Henning Kamp static struct cdevsw xpt_cdevsw = {
186dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
1872b83592fSScott Long 	.d_flags =	0,
1887ac40f5fSPoul-Henning Kamp 	.d_open =	xptopen,
1897ac40f5fSPoul-Henning Kamp 	.d_close =	xptclose,
1907ac40f5fSPoul-Henning Kamp 	.d_ioctl =	xptioctl,
1917ac40f5fSPoul-Henning Kamp 	.d_name =	"xpt",
1928b8a9b1dSJustin T. Gibbs };
1938b8a9b1dSJustin T. Gibbs 
1948b8a9b1dSJustin T. Gibbs /* Storage for debugging datastructures */
1958b8a9b1dSJustin T. Gibbs struct cam_path *cam_dpath;
19682b361b1SMatt Jacob u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
19782b361b1SMatt Jacob TUNABLE_INT("kern.cam.dflags", &cam_dflags);
198240577c2SMatthew D Fleming SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RW,
199f0f25b9cSAlexander Motin 	&cam_dflags, 0, "Enabled debug flags");
200f0f25b9cSAlexander Motin u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
20182b361b1SMatt Jacob TUNABLE_INT("kern.cam.debug_delay", &cam_debug_delay);
202240577c2SMatthew D Fleming SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RW,
203f0f25b9cSAlexander Motin 	&cam_debug_delay, 0, "Delay in us after each debug message");
2048b8a9b1dSJustin T. Gibbs 
2056d2a8f1cSPeter Wemm /* Our boot-time initialization hook */
20674bd1c10SNick Hibma static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
20774bd1c10SNick Hibma 
20874bd1c10SNick Hibma static moduledata_t cam_moduledata = {
20974bd1c10SNick Hibma 	"cam",
21074bd1c10SNick Hibma 	cam_module_event_handler,
21174bd1c10SNick Hibma 	NULL
21274bd1c10SNick Hibma };
21374bd1c10SNick Hibma 
2142b83592fSScott Long static int	xpt_init(void *);
21574bd1c10SNick Hibma 
21674bd1c10SNick Hibma DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
21774bd1c10SNick Hibma MODULE_VERSION(cam, 1);
21874bd1c10SNick Hibma 
2198b8a9b1dSJustin T. Gibbs 
2208b8a9b1dSJustin T. Gibbs static void		xpt_async_bcast(struct async_list *async_head,
2218b8a9b1dSJustin T. Gibbs 					u_int32_t async_code,
2228b8a9b1dSJustin T. Gibbs 					struct cam_path *path,
2238b8a9b1dSJustin T. Gibbs 					void *async_arg);
224434bbf6eSJustin T. Gibbs static path_id_t xptnextfreepathid(void);
225434bbf6eSJustin T. Gibbs static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
2268b8a9b1dSJustin T. Gibbs static union ccb *xpt_get_ccb(struct cam_ed *device);
227cccf4220SAlexander Motin static void	 xpt_run_dev_allocq(struct cam_ed *device);
228cccf4220SAlexander Motin static void	 xpt_run_devq(struct cam_devq *devq);
2298b8a9b1dSJustin T. Gibbs static timeout_t xpt_release_devq_timeout;
2302b83592fSScott Long static void	 xpt_release_simq_timeout(void *arg) __unused;
231a5479bc5SJustin T. Gibbs static void	 xpt_release_bus(struct cam_eb *bus);
232cccf4220SAlexander Motin static void	 xpt_release_devq_device(struct cam_ed *dev, u_int count,
233cccf4220SAlexander Motin 		    int run_queue);
2348b8a9b1dSJustin T. Gibbs static struct cam_et*
2358b8a9b1dSJustin T. Gibbs 		 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
236f98d7a47SAlexander Motin static void	 xpt_release_target(struct cam_et *target);
2378b8a9b1dSJustin T. Gibbs static struct cam_eb*
2388b8a9b1dSJustin T. Gibbs 		 xpt_find_bus(path_id_t path_id);
2398b8a9b1dSJustin T. Gibbs static struct cam_et*
2408b8a9b1dSJustin T. Gibbs 		 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
2418b8a9b1dSJustin T. Gibbs static struct cam_ed*
2428b8a9b1dSJustin T. Gibbs 		 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
2438b8a9b1dSJustin T. Gibbs static void	 xpt_config(void *arg);
2448b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t xptpassannouncefunc;
2458b8a9b1dSJustin T. Gibbs static void	 xptaction(struct cam_sim *sim, union ccb *work_ccb);
246434bbf6eSJustin T. Gibbs static void	 xptpoll(struct cam_sim *sim);
2478088699fSJohn Baldwin static void	 camisr(void *);
24839fe6d85SAlexander Motin static void	 camisr_runqueue(struct cam_sim *);
2498b8a9b1dSJustin T. Gibbs static dev_match_ret	xptbusmatch(struct dev_match_pattern *patterns,
2503393f8daSKenneth D. Merry 				    u_int num_patterns, struct cam_eb *bus);
2518b8a9b1dSJustin T. Gibbs static dev_match_ret	xptdevicematch(struct dev_match_pattern *patterns,
2523393f8daSKenneth D. Merry 				       u_int num_patterns,
2533393f8daSKenneth D. Merry 				       struct cam_ed *device);
2548b8a9b1dSJustin T. Gibbs static dev_match_ret	xptperiphmatch(struct dev_match_pattern *patterns,
2553393f8daSKenneth D. Merry 				       u_int num_patterns,
2568b8a9b1dSJustin T. Gibbs 				       struct cam_periph *periph);
2578b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptedtbusfunc;
2588b8a9b1dSJustin T. Gibbs static xpt_targetfunc_t	xptedttargetfunc;
2598b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptedtdevicefunc;
2608b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptedtperiphfunc;
2618b8a9b1dSJustin T. Gibbs static xpt_pdrvfunc_t	xptplistpdrvfunc;
2628b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptplistperiphfunc;
2638b8a9b1dSJustin T. Gibbs static int		xptedtmatch(struct ccb_dev_match *cdm);
2648b8a9b1dSJustin T. Gibbs static int		xptperiphlistmatch(struct ccb_dev_match *cdm);
2658b8a9b1dSJustin T. Gibbs static int		xptbustraverse(struct cam_eb *start_bus,
2668b8a9b1dSJustin T. Gibbs 				       xpt_busfunc_t *tr_func, void *arg);
2678b8a9b1dSJustin T. Gibbs static int		xpttargettraverse(struct cam_eb *bus,
2688b8a9b1dSJustin T. Gibbs 					  struct cam_et *start_target,
2698b8a9b1dSJustin T. Gibbs 					  xpt_targetfunc_t *tr_func, void *arg);
2708b8a9b1dSJustin T. Gibbs static int		xptdevicetraverse(struct cam_et *target,
2718b8a9b1dSJustin T. Gibbs 					  struct cam_ed *start_device,
2728b8a9b1dSJustin T. Gibbs 					  xpt_devicefunc_t *tr_func, void *arg);
2738b8a9b1dSJustin T. Gibbs static int		xptperiphtraverse(struct cam_ed *device,
2748b8a9b1dSJustin T. Gibbs 					  struct cam_periph *start_periph,
2758b8a9b1dSJustin T. Gibbs 					  xpt_periphfunc_t *tr_func, void *arg);
2768b8a9b1dSJustin T. Gibbs static int		xptpdrvtraverse(struct periph_driver **start_pdrv,
2778b8a9b1dSJustin T. Gibbs 					xpt_pdrvfunc_t *tr_func, void *arg);
2788b8a9b1dSJustin T. Gibbs static int		xptpdperiphtraverse(struct periph_driver **pdrv,
2798b8a9b1dSJustin T. Gibbs 					    struct cam_periph *start_periph,
2808b8a9b1dSJustin T. Gibbs 					    xpt_periphfunc_t *tr_func,
2818b8a9b1dSJustin T. Gibbs 					    void *arg);
2828b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptdefbusfunc;
2838b8a9b1dSJustin T. Gibbs static xpt_targetfunc_t	xptdeftargetfunc;
2848b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptdefdevicefunc;
2858b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptdefperiphfunc;
28683c5d981SAlexander Motin static void		xpt_finishconfig_task(void *context, int pending);
28752c9ce25SScott Long static void		xpt_dev_async_default(u_int32_t async_code,
28852c9ce25SScott Long 					      struct cam_eb *bus,
28952c9ce25SScott Long 					      struct cam_et *target,
29052c9ce25SScott Long 					      struct cam_ed *device,
29152c9ce25SScott Long 					      void *async_arg);
29252c9ce25SScott Long static struct cam_ed *	xpt_alloc_device_default(struct cam_eb *bus,
29352c9ce25SScott Long 						 struct cam_et *target,
29452c9ce25SScott Long 						 lun_id_t lun_id);
2958b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptsetasyncfunc;
2968b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptsetasyncbusfunc;
2978b8a9b1dSJustin T. Gibbs static cam_status	xptregister(struct cam_periph *periph,
2988b8a9b1dSJustin T. Gibbs 				    void *arg);
2998b8a9b1dSJustin T. Gibbs static __inline int periph_is_queued(struct cam_periph *periph);
300cccf4220SAlexander Motin static __inline int device_is_queued(struct cam_ed *device);
3018b8a9b1dSJustin T. Gibbs 
3028b8a9b1dSJustin T. Gibbs static __inline int
303cccf4220SAlexander Motin xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
30430a4094fSAlexander Motin {
30530a4094fSAlexander Motin 	int	retval;
30630a4094fSAlexander Motin 
30783c5d981SAlexander Motin 	if ((dev->ccbq.queue.entries > 0) &&
30883c5d981SAlexander Motin 	    (dev->ccbq.dev_openings > 0) &&
309cccf4220SAlexander Motin 	    (dev->ccbq.queue.qfrozen_cnt == 0)) {
31030a4094fSAlexander Motin 		/*
31130a4094fSAlexander Motin 		 * The priority of a device waiting for controller
3126bccea7cSRebecca Cran 		 * resources is that of the highest priority CCB
31330a4094fSAlexander Motin 		 * enqueued.
31430a4094fSAlexander Motin 		 */
31530a4094fSAlexander Motin 		retval =
316cccf4220SAlexander Motin 		    xpt_schedule_dev(&devq->send_queue,
317cccf4220SAlexander Motin 				     &dev->devq_entry.pinfo,
31883c5d981SAlexander Motin 				     CAMQ_GET_PRIO(&dev->ccbq.queue));
31930a4094fSAlexander Motin 	} else {
32030a4094fSAlexander Motin 		retval = 0;
32130a4094fSAlexander Motin 	}
32230a4094fSAlexander Motin 	return (retval);
32330a4094fSAlexander Motin }
32430a4094fSAlexander Motin 
32530a4094fSAlexander Motin static __inline int
3268b8a9b1dSJustin T. Gibbs periph_is_queued(struct cam_periph *periph)
3278b8a9b1dSJustin T. Gibbs {
3288b8a9b1dSJustin T. Gibbs 	return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
3298b8a9b1dSJustin T. Gibbs }
3308b8a9b1dSJustin T. Gibbs 
3318b8a9b1dSJustin T. Gibbs static __inline int
332cccf4220SAlexander Motin device_is_queued(struct cam_ed *device)
3338b8a9b1dSJustin T. Gibbs {
334cccf4220SAlexander Motin 	return (device->devq_entry.pinfo.index != CAM_UNQUEUED_INDEX);
3358b8a9b1dSJustin T. Gibbs }
3368b8a9b1dSJustin T. Gibbs 
3378b8a9b1dSJustin T. Gibbs static void
3388b8a9b1dSJustin T. Gibbs xpt_periph_init()
3398b8a9b1dSJustin T. Gibbs {
34073d26919SKenneth D. Merry 	make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
3418b8a9b1dSJustin T. Gibbs }
3428b8a9b1dSJustin T. Gibbs 
3438b8a9b1dSJustin T. Gibbs static void
3448b8a9b1dSJustin T. Gibbs xptdone(struct cam_periph *periph, union ccb *done_ccb)
3458b8a9b1dSJustin T. Gibbs {
3468b8a9b1dSJustin T. Gibbs 	/* Caller will release the CCB */
3478b8a9b1dSJustin T. Gibbs 	wakeup(&done_ccb->ccb_h.cbfcnp);
3488b8a9b1dSJustin T. Gibbs }
3498b8a9b1dSJustin T. Gibbs 
3508b8a9b1dSJustin T. Gibbs static int
35189c9c53dSPoul-Henning Kamp xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
3528b8a9b1dSJustin T. Gibbs {
3538b8a9b1dSJustin T. Gibbs 
3548b8a9b1dSJustin T. Gibbs 	/*
35566a0780eSKenneth D. Merry 	 * Only allow read-write access.
35666a0780eSKenneth D. Merry 	 */
35766a0780eSKenneth D. Merry 	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
35866a0780eSKenneth D. Merry 		return(EPERM);
35966a0780eSKenneth D. Merry 
36066a0780eSKenneth D. Merry 	/*
3618b8a9b1dSJustin T. Gibbs 	 * We don't allow nonblocking access.
3628b8a9b1dSJustin T. Gibbs 	 */
3638b8a9b1dSJustin T. Gibbs 	if ((flags & O_NONBLOCK) != 0) {
3642b83592fSScott Long 		printf("%s: can't do nonblocking access\n", devtoname(dev));
3658b8a9b1dSJustin T. Gibbs 		return(ENODEV);
3668b8a9b1dSJustin T. Gibbs 	}
3678b8a9b1dSJustin T. Gibbs 
3688b8a9b1dSJustin T. Gibbs 	/* Mark ourselves open */
3692b83592fSScott Long 	mtx_lock(&xsoftc.xpt_lock);
3708b8a9b1dSJustin T. Gibbs 	xsoftc.flags |= XPT_FLAG_OPEN;
3712b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_lock);
3728b8a9b1dSJustin T. Gibbs 
3738b8a9b1dSJustin T. Gibbs 	return(0);
3748b8a9b1dSJustin T. Gibbs }
3758b8a9b1dSJustin T. Gibbs 
3768b8a9b1dSJustin T. Gibbs static int
37789c9c53dSPoul-Henning Kamp xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
3788b8a9b1dSJustin T. Gibbs {
3798b8a9b1dSJustin T. Gibbs 
3808b8a9b1dSJustin T. Gibbs 	/* Mark ourselves closed */
3812b83592fSScott Long 	mtx_lock(&xsoftc.xpt_lock);
3828b8a9b1dSJustin T. Gibbs 	xsoftc.flags &= ~XPT_FLAG_OPEN;
3832b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_lock);
3848b8a9b1dSJustin T. Gibbs 
3858b8a9b1dSJustin T. Gibbs 	return(0);
3868b8a9b1dSJustin T. Gibbs }
3878b8a9b1dSJustin T. Gibbs 
3882b83592fSScott Long /*
3892b83592fSScott Long  * Don't automatically grab the xpt softc lock here even though this is going
3902b83592fSScott Long  * through the xpt device.  The xpt device is really just a back door for
3912b83592fSScott Long  * accessing other devices and SIMs, so the right thing to do is to grab
3922b83592fSScott Long  * the appropriate SIM lock once the bus/SIM is located.
3932b83592fSScott Long  */
3948b8a9b1dSJustin T. Gibbs static int
39589c9c53dSPoul-Henning Kamp xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
3968b8a9b1dSJustin T. Gibbs {
3972b83592fSScott Long 	int error;
3988b8a9b1dSJustin T. Gibbs 
39925a2902cSScott Long 	if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
40025a2902cSScott Long 		error = cam_compat_ioctl(dev, &cmd, &addr, &flag, td);
40125a2902cSScott Long 		if (error == EAGAIN)
40225a2902cSScott Long 			return (xptdoioctl(dev, cmd, addr, flag, td));
40325a2902cSScott Long 	}
40425a2902cSScott Long 	return (error);
40525a2902cSScott Long }
40625a2902cSScott Long 
40725a2902cSScott Long static int
40825a2902cSScott Long xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
40925a2902cSScott Long {
41025a2902cSScott Long 	int error;
41125a2902cSScott Long 
4128b8a9b1dSJustin T. Gibbs 	error = 0;
4138b8a9b1dSJustin T. Gibbs 
4148b8a9b1dSJustin T. Gibbs 	switch(cmd) {
4158b8a9b1dSJustin T. Gibbs 	/*
4168b8a9b1dSJustin T. Gibbs 	 * For the transport layer CAMIOCOMMAND ioctl, we really only want
4178b8a9b1dSJustin T. Gibbs 	 * to accept CCB types that don't quite make sense to send through a
4188c7a96c5SScott Long 	 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
4198c7a96c5SScott Long 	 * in the CAM spec.
4208b8a9b1dSJustin T. Gibbs 	 */
4218b8a9b1dSJustin T. Gibbs 	case CAMIOCOMMAND: {
4228b8a9b1dSJustin T. Gibbs 		union ccb *ccb;
4238b8a9b1dSJustin T. Gibbs 		union ccb *inccb;
4242b83592fSScott Long 		struct cam_eb *bus;
4258b8a9b1dSJustin T. Gibbs 
4268b8a9b1dSJustin T. Gibbs 		inccb = (union ccb *)addr;
4278b8a9b1dSJustin T. Gibbs 
4282b83592fSScott Long 		bus = xpt_find_bus(inccb->ccb_h.path_id);
4290e85f214SMatt Jacob 		if (bus == NULL)
4300e85f214SMatt Jacob 			return (EINVAL);
4310e85f214SMatt Jacob 
4320e85f214SMatt Jacob 		switch (inccb->ccb_h.func_code) {
4330e85f214SMatt Jacob 		case XPT_SCAN_BUS:
4340e85f214SMatt Jacob 		case XPT_RESET_BUS:
4350e85f214SMatt Jacob 			if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
4360e85f214SMatt Jacob 			    inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
4370e85f214SMatt Jacob 				xpt_release_bus(bus);
4380e85f214SMatt Jacob 				return (EINVAL);
4390e85f214SMatt Jacob 			}
4400e85f214SMatt Jacob 			break;
4410e85f214SMatt Jacob 		case XPT_SCAN_TGT:
4420e85f214SMatt Jacob 			if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
4430e85f214SMatt Jacob 			    inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
4440e85f214SMatt Jacob 				xpt_release_bus(bus);
4450e85f214SMatt Jacob 				return (EINVAL);
4460e85f214SMatt Jacob 			}
4470e85f214SMatt Jacob 			break;
4480e85f214SMatt Jacob 		default:
4492b83592fSScott Long 			break;
4502b83592fSScott Long 		}
4512b83592fSScott Long 
4528b8a9b1dSJustin T. Gibbs 		switch(inccb->ccb_h.func_code) {
4538b8a9b1dSJustin T. Gibbs 		case XPT_SCAN_BUS:
4548b8a9b1dSJustin T. Gibbs 		case XPT_RESET_BUS:
4558c7a96c5SScott Long 		case XPT_PATH_INQ:
4568fcf57f5SJustin T. Gibbs 		case XPT_ENG_INQ:
4578b8a9b1dSJustin T. Gibbs 		case XPT_SCAN_LUN:
4580e85f214SMatt Jacob 		case XPT_SCAN_TGT:
4598b8a9b1dSJustin T. Gibbs 
4608008a935SScott Long 			ccb = xpt_alloc_ccb();
4612b83592fSScott Long 
4622b83592fSScott Long 			CAM_SIM_LOCK(bus->sim);
4636ee4d869SAlexander Motin 
4648b8a9b1dSJustin T. Gibbs 			/*
4658b8a9b1dSJustin T. Gibbs 			 * Create a path using the bus, target, and lun the
4668b8a9b1dSJustin T. Gibbs 			 * user passed in.
4678b8a9b1dSJustin T. Gibbs 			 */
468e5dfa058SAlexander Motin 			if (xpt_create_path(&ccb->ccb_h.path, NULL,
4698b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.path_id,
4708b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_id,
4718b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_lun) !=
4728b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP){
4738b8a9b1dSJustin T. Gibbs 				error = EINVAL;
4742b83592fSScott Long 				CAM_SIM_UNLOCK(bus->sim);
4758b8a9b1dSJustin T. Gibbs 				xpt_free_ccb(ccb);
4768b8a9b1dSJustin T. Gibbs 				break;
4778b8a9b1dSJustin T. Gibbs 			}
4788b8a9b1dSJustin T. Gibbs 			/* Ensure all of our fields are correct */
4798b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
4808b8a9b1dSJustin T. Gibbs 				      inccb->ccb_h.pinfo.priority);
4818b8a9b1dSJustin T. Gibbs 			xpt_merge_ccb(ccb, inccb);
4828b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.cbfcnp = xptdone;
4838b8a9b1dSJustin T. Gibbs 			cam_periph_runccb(ccb, NULL, 0, 0, NULL);
4848b8a9b1dSJustin T. Gibbs 			bcopy(ccb, inccb, sizeof(union ccb));
4858b8a9b1dSJustin T. Gibbs 			xpt_free_path(ccb->ccb_h.path);
4868b8a9b1dSJustin T. Gibbs 			xpt_free_ccb(ccb);
4872b83592fSScott Long 			CAM_SIM_UNLOCK(bus->sim);
4888b8a9b1dSJustin T. Gibbs 			break;
4898b8a9b1dSJustin T. Gibbs 
4908b8a9b1dSJustin T. Gibbs 		case XPT_DEBUG: {
4918b8a9b1dSJustin T. Gibbs 			union ccb ccb;
4928b8a9b1dSJustin T. Gibbs 
4938b8a9b1dSJustin T. Gibbs 			/*
494aa872be6SMatt Jacob 			 * This is an immediate CCB, so it's okay to
4958b8a9b1dSJustin T. Gibbs 			 * allocate it on the stack.
4968b8a9b1dSJustin T. Gibbs 			 */
4978b8a9b1dSJustin T. Gibbs 
4982b83592fSScott Long 			CAM_SIM_LOCK(bus->sim);
4992b83592fSScott Long 
5008b8a9b1dSJustin T. Gibbs 			/*
5018b8a9b1dSJustin T. Gibbs 			 * Create a path using the bus, target, and lun the
5028b8a9b1dSJustin T. Gibbs 			 * user passed in.
5038b8a9b1dSJustin T. Gibbs 			 */
504e5dfa058SAlexander Motin 			if (xpt_create_path(&ccb.ccb_h.path, NULL,
5058b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.path_id,
5068b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_id,
5078b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_lun) !=
5088b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP){
5098b8a9b1dSJustin T. Gibbs 				error = EINVAL;
5102ca9ba94SScott Long 				CAM_SIM_UNLOCK(bus->sim);
5118b8a9b1dSJustin T. Gibbs 				break;
5128b8a9b1dSJustin T. Gibbs 			}
5138b8a9b1dSJustin T. Gibbs 			/* Ensure all of our fields are correct */
5148b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
5158b8a9b1dSJustin T. Gibbs 				      inccb->ccb_h.pinfo.priority);
5168b8a9b1dSJustin T. Gibbs 			xpt_merge_ccb(&ccb, inccb);
5178b8a9b1dSJustin T. Gibbs 			ccb.ccb_h.cbfcnp = xptdone;
5188b8a9b1dSJustin T. Gibbs 			xpt_action(&ccb);
5198b8a9b1dSJustin T. Gibbs 			bcopy(&ccb, inccb, sizeof(union ccb));
5208b8a9b1dSJustin T. Gibbs 			xpt_free_path(ccb.ccb_h.path);
52180d6987cSAlexander Motin 			CAM_SIM_UNLOCK(bus->sim);
5228b8a9b1dSJustin T. Gibbs 			break;
5238b8a9b1dSJustin T. Gibbs 
5248b8a9b1dSJustin T. Gibbs 		}
5258b8a9b1dSJustin T. Gibbs 		case XPT_DEV_MATCH: {
5268b8a9b1dSJustin T. Gibbs 			struct cam_periph_map_info mapinfo;
52759190eaaSKenneth D. Merry 			struct cam_path *old_path;
5288b8a9b1dSJustin T. Gibbs 
5298b8a9b1dSJustin T. Gibbs 			/*
5308b8a9b1dSJustin T. Gibbs 			 * We can't deal with physical addresses for this
5318b8a9b1dSJustin T. Gibbs 			 * type of transaction.
5328b8a9b1dSJustin T. Gibbs 			 */
533dd0b4fb6SKonstantin Belousov 			if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
534dd0b4fb6SKonstantin Belousov 			    CAM_DATA_VADDR) {
5358b8a9b1dSJustin T. Gibbs 				error = EINVAL;
5368b8a9b1dSJustin T. Gibbs 				break;
5378b8a9b1dSJustin T. Gibbs 			}
53859190eaaSKenneth D. Merry 
53959190eaaSKenneth D. Merry 			/*
54059190eaaSKenneth D. Merry 			 * Save this in case the caller had it set to
54159190eaaSKenneth D. Merry 			 * something in particular.
54259190eaaSKenneth D. Merry 			 */
54359190eaaSKenneth D. Merry 			old_path = inccb->ccb_h.path;
54459190eaaSKenneth D. Merry 
54559190eaaSKenneth D. Merry 			/*
54659190eaaSKenneth D. Merry 			 * We really don't need a path for the matching
54759190eaaSKenneth D. Merry 			 * code.  The path is needed because of the
54859190eaaSKenneth D. Merry 			 * debugging statements in xpt_action().  They
54959190eaaSKenneth D. Merry 			 * assume that the CCB has a valid path.
55059190eaaSKenneth D. Merry 			 */
55159190eaaSKenneth D. Merry 			inccb->ccb_h.path = xpt_periph->path;
55259190eaaSKenneth D. Merry 
5538b8a9b1dSJustin T. Gibbs 			bzero(&mapinfo, sizeof(mapinfo));
5548b8a9b1dSJustin T. Gibbs 
5558b8a9b1dSJustin T. Gibbs 			/*
5568b8a9b1dSJustin T. Gibbs 			 * Map the pattern and match buffers into kernel
5578b8a9b1dSJustin T. Gibbs 			 * virtual address space.
5588b8a9b1dSJustin T. Gibbs 			 */
5598b8a9b1dSJustin T. Gibbs 			error = cam_periph_mapmem(inccb, &mapinfo);
5608b8a9b1dSJustin T. Gibbs 
56159190eaaSKenneth D. Merry 			if (error) {
56259190eaaSKenneth D. Merry 				inccb->ccb_h.path = old_path;
5638b8a9b1dSJustin T. Gibbs 				break;
56459190eaaSKenneth D. Merry 			}
5658b8a9b1dSJustin T. Gibbs 
5668b8a9b1dSJustin T. Gibbs 			/*
5678b8a9b1dSJustin T. Gibbs 			 * This is an immediate CCB, we can send it on directly.
5688b8a9b1dSJustin T. Gibbs 			 */
56992c40f40SAlexander Motin 			CAM_SIM_LOCK(xpt_path_sim(xpt_periph->path));
5708b8a9b1dSJustin T. Gibbs 			xpt_action(inccb);
57192c40f40SAlexander Motin 			CAM_SIM_UNLOCK(xpt_path_sim(xpt_periph->path));
5728b8a9b1dSJustin T. Gibbs 
5738b8a9b1dSJustin T. Gibbs 			/*
5748b8a9b1dSJustin T. Gibbs 			 * Map the buffers back into user space.
5758b8a9b1dSJustin T. Gibbs 			 */
5768b8a9b1dSJustin T. Gibbs 			cam_periph_unmapmem(inccb, &mapinfo);
5778b8a9b1dSJustin T. Gibbs 
57859190eaaSKenneth D. Merry 			inccb->ccb_h.path = old_path;
57959190eaaSKenneth D. Merry 
5808b8a9b1dSJustin T. Gibbs 			error = 0;
5818b8a9b1dSJustin T. Gibbs 			break;
5828b8a9b1dSJustin T. Gibbs 		}
5838b8a9b1dSJustin T. Gibbs 		default:
5848fcf57f5SJustin T. Gibbs 			error = ENOTSUP;
5858b8a9b1dSJustin T. Gibbs 			break;
5868b8a9b1dSJustin T. Gibbs 		}
587daddc001SScott Long 		xpt_release_bus(bus);
5888b8a9b1dSJustin T. Gibbs 		break;
5898b8a9b1dSJustin T. Gibbs 	}
5908b8a9b1dSJustin T. Gibbs 	/*
5918b8a9b1dSJustin T. Gibbs 	 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
5928b8a9b1dSJustin T. Gibbs 	 * with the periphal driver name and unit name filled in.  The other
5938b8a9b1dSJustin T. Gibbs 	 * fields don't really matter as input.  The passthrough driver name
5948b8a9b1dSJustin T. Gibbs 	 * ("pass"), and unit number are passed back in the ccb.  The current
5958b8a9b1dSJustin T. Gibbs 	 * device generation number, and the index into the device peripheral
5968b8a9b1dSJustin T. Gibbs 	 * driver list, and the status are also passed back.  Note that
5978b8a9b1dSJustin T. Gibbs 	 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
5988b8a9b1dSJustin T. Gibbs 	 * we never return a status of CAM_GDEVLIST_LIST_CHANGED.  It is
5998b8a9b1dSJustin T. Gibbs 	 * (or rather should be) impossible for the device peripheral driver
6008b8a9b1dSJustin T. Gibbs 	 * list to change since we look at the whole thing in one pass, and
60177dc25ccSScott Long 	 * we do it with lock protection.
6028b8a9b1dSJustin T. Gibbs 	 *
6038b8a9b1dSJustin T. Gibbs 	 */
6048b8a9b1dSJustin T. Gibbs 	case CAMGETPASSTHRU: {
6058b8a9b1dSJustin T. Gibbs 		union ccb *ccb;
6068b8a9b1dSJustin T. Gibbs 		struct cam_periph *periph;
6078b8a9b1dSJustin T. Gibbs 		struct periph_driver **p_drv;
6088b8a9b1dSJustin T. Gibbs 		char   *name;
6093393f8daSKenneth D. Merry 		u_int unit;
610621a60d4SKenneth D. Merry 		int base_periph_found;
6118b8a9b1dSJustin T. Gibbs 
6128b8a9b1dSJustin T. Gibbs 		ccb = (union ccb *)addr;
6138b8a9b1dSJustin T. Gibbs 		unit = ccb->cgdl.unit_number;
6148b8a9b1dSJustin T. Gibbs 		name = ccb->cgdl.periph_name;
615621a60d4SKenneth D. Merry 		base_periph_found = 0;
616621a60d4SKenneth D. Merry 
6178b8a9b1dSJustin T. Gibbs 		/*
6188b8a9b1dSJustin T. Gibbs 		 * Sanity check -- make sure we don't get a null peripheral
6198b8a9b1dSJustin T. Gibbs 		 * driver name.
6208b8a9b1dSJustin T. Gibbs 		 */
6218b8a9b1dSJustin T. Gibbs 		if (*ccb->cgdl.periph_name == '\0') {
6228b8a9b1dSJustin T. Gibbs 			error = EINVAL;
6238b8a9b1dSJustin T. Gibbs 			break;
6248b8a9b1dSJustin T. Gibbs 		}
6258b8a9b1dSJustin T. Gibbs 
6268b8a9b1dSJustin T. Gibbs 		/* Keep the list from changing while we traverse it */
6279a7c2696SAlexander Motin 		xpt_lock_buses();
6288b8a9b1dSJustin T. Gibbs 
6298b8a9b1dSJustin T. Gibbs 		/* first find our driver in the list of drivers */
6300b7c27b9SPeter Wemm 		for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
6318b8a9b1dSJustin T. Gibbs 			if (strcmp((*p_drv)->driver_name, name) == 0)
6328b8a9b1dSJustin T. Gibbs 				break;
6338b8a9b1dSJustin T. Gibbs 
6348b8a9b1dSJustin T. Gibbs 		if (*p_drv == NULL) {
6359a7c2696SAlexander Motin 			xpt_unlock_buses();
6368b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
6378b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
6388b8a9b1dSJustin T. Gibbs 			*ccb->cgdl.periph_name = '\0';
6398b8a9b1dSJustin T. Gibbs 			ccb->cgdl.unit_number = 0;
6408b8a9b1dSJustin T. Gibbs 			error = ENOENT;
6418b8a9b1dSJustin T. Gibbs 			break;
6428b8a9b1dSJustin T. Gibbs 		}
6438b8a9b1dSJustin T. Gibbs 
6448b8a9b1dSJustin T. Gibbs 		/*
6458b8a9b1dSJustin T. Gibbs 		 * Run through every peripheral instance of this driver
6468b8a9b1dSJustin T. Gibbs 		 * and check to see whether it matches the unit passed
6478b8a9b1dSJustin T. Gibbs 		 * in by the user.  If it does, get out of the loops and
6488b8a9b1dSJustin T. Gibbs 		 * find the passthrough driver associated with that
6498b8a9b1dSJustin T. Gibbs 		 * peripheral driver.
6508b8a9b1dSJustin T. Gibbs 		 */
6518b8a9b1dSJustin T. Gibbs 		for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
6528b8a9b1dSJustin T. Gibbs 		     periph = TAILQ_NEXT(periph, unit_links)) {
6538b8a9b1dSJustin T. Gibbs 
654*a29779e8SAlexander Motin 			if (periph->unit_number == unit)
6558b8a9b1dSJustin T. Gibbs 				break;
6568b8a9b1dSJustin T. Gibbs 		}
6578b8a9b1dSJustin T. Gibbs 		/*
6588b8a9b1dSJustin T. Gibbs 		 * If we found the peripheral driver that the user passed
6598b8a9b1dSJustin T. Gibbs 		 * in, go through all of the peripheral drivers for that
6608b8a9b1dSJustin T. Gibbs 		 * particular device and look for a passthrough driver.
6618b8a9b1dSJustin T. Gibbs 		 */
6628b8a9b1dSJustin T. Gibbs 		if (periph != NULL) {
6638b8a9b1dSJustin T. Gibbs 			struct cam_ed *device;
6648b8a9b1dSJustin T. Gibbs 			int i;
6658b8a9b1dSJustin T. Gibbs 
666621a60d4SKenneth D. Merry 			base_periph_found = 1;
6678b8a9b1dSJustin T. Gibbs 			device = periph->path->device;
668fc2ffbe6SPoul-Henning Kamp 			for (i = 0, periph = SLIST_FIRST(&device->periphs);
6698b8a9b1dSJustin T. Gibbs 			     periph != NULL;
670fc2ffbe6SPoul-Henning Kamp 			     periph = SLIST_NEXT(periph, periph_links), i++) {
6718b8a9b1dSJustin T. Gibbs 				/*
6728b8a9b1dSJustin T. Gibbs 				 * Check to see whether we have a
6738b8a9b1dSJustin T. Gibbs 				 * passthrough device or not.
6748b8a9b1dSJustin T. Gibbs 				 */
6758b8a9b1dSJustin T. Gibbs 				if (strcmp(periph->periph_name, "pass") == 0) {
6768b8a9b1dSJustin T. Gibbs 					/*
6778b8a9b1dSJustin T. Gibbs 					 * Fill in the getdevlist fields.
6788b8a9b1dSJustin T. Gibbs 					 */
6798b8a9b1dSJustin T. Gibbs 					strcpy(ccb->cgdl.periph_name,
6808b8a9b1dSJustin T. Gibbs 					       periph->periph_name);
6818b8a9b1dSJustin T. Gibbs 					ccb->cgdl.unit_number =
6828b8a9b1dSJustin T. Gibbs 						periph->unit_number;
683fc2ffbe6SPoul-Henning Kamp 					if (SLIST_NEXT(periph, periph_links))
6848b8a9b1dSJustin T. Gibbs 						ccb->cgdl.status =
6858b8a9b1dSJustin T. Gibbs 							CAM_GDEVLIST_MORE_DEVS;
6868b8a9b1dSJustin T. Gibbs 					else
6878b8a9b1dSJustin T. Gibbs 						ccb->cgdl.status =
6888b8a9b1dSJustin T. Gibbs 						       CAM_GDEVLIST_LAST_DEVICE;
6898b8a9b1dSJustin T. Gibbs 					ccb->cgdl.generation =
6908b8a9b1dSJustin T. Gibbs 						device->generation;
6918b8a9b1dSJustin T. Gibbs 					ccb->cgdl.index = i;
6928b8a9b1dSJustin T. Gibbs 					/*
6938b8a9b1dSJustin T. Gibbs 					 * Fill in some CCB header fields
6948b8a9b1dSJustin T. Gibbs 					 * that the user may want.
6958b8a9b1dSJustin T. Gibbs 					 */
6968b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.path_id =
6978b8a9b1dSJustin T. Gibbs 						periph->path->bus->path_id;
6988b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.target_id =
6998b8a9b1dSJustin T. Gibbs 						periph->path->target->target_id;
7008b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.target_lun =
7018b8a9b1dSJustin T. Gibbs 						periph->path->device->lun_id;
7028b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.status = CAM_REQ_CMP;
7038b8a9b1dSJustin T. Gibbs 					break;
7048b8a9b1dSJustin T. Gibbs 				}
7058b8a9b1dSJustin T. Gibbs 			}
7068b8a9b1dSJustin T. Gibbs 		}
7078b8a9b1dSJustin T. Gibbs 
7088b8a9b1dSJustin T. Gibbs 		/*
7098b8a9b1dSJustin T. Gibbs 		 * If the periph is null here, one of two things has
7108b8a9b1dSJustin T. Gibbs 		 * happened.  The first possibility is that we couldn't
7118b8a9b1dSJustin T. Gibbs 		 * find the unit number of the particular peripheral driver
7128b8a9b1dSJustin T. Gibbs 		 * that the user is asking about.  e.g. the user asks for
7138b8a9b1dSJustin T. Gibbs 		 * the passthrough driver for "da11".  We find the list of
7148b8a9b1dSJustin T. Gibbs 		 * "da" peripherals all right, but there is no unit 11.
7158b8a9b1dSJustin T. Gibbs 		 * The other possibility is that we went through the list
7168b8a9b1dSJustin T. Gibbs 		 * of peripheral drivers attached to the device structure,
7178b8a9b1dSJustin T. Gibbs 		 * but didn't find one with the name "pass".  Either way,
7188b8a9b1dSJustin T. Gibbs 		 * we return ENOENT, since we couldn't find something.
7198b8a9b1dSJustin T. Gibbs 		 */
7208b8a9b1dSJustin T. Gibbs 		if (periph == NULL) {
7218b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
7228b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
7238b8a9b1dSJustin T. Gibbs 			*ccb->cgdl.periph_name = '\0';
7248b8a9b1dSJustin T. Gibbs 			ccb->cgdl.unit_number = 0;
7258b8a9b1dSJustin T. Gibbs 			error = ENOENT;
726621a60d4SKenneth D. Merry 			/*
727621a60d4SKenneth D. Merry 			 * It is unfortunate that this is even necessary,
728621a60d4SKenneth D. Merry 			 * but there are many, many clueless users out there.
729621a60d4SKenneth D. Merry 			 * If this is true, the user is looking for the
730621a60d4SKenneth D. Merry 			 * passthrough driver, but doesn't have one in his
731621a60d4SKenneth D. Merry 			 * kernel.
732621a60d4SKenneth D. Merry 			 */
733621a60d4SKenneth D. Merry 			if (base_periph_found == 1) {
734621a60d4SKenneth D. Merry 				printf("xptioctl: pass driver is not in the "
735621a60d4SKenneth D. Merry 				       "kernel\n");
736935c968aSChristian Brueffer 				printf("xptioctl: put \"device pass\" in "
737621a60d4SKenneth D. Merry 				       "your kernel config file\n");
738621a60d4SKenneth D. Merry 			}
7398b8a9b1dSJustin T. Gibbs 		}
7409a7c2696SAlexander Motin 		xpt_unlock_buses();
7418b8a9b1dSJustin T. Gibbs 		break;
7428b8a9b1dSJustin T. Gibbs 		}
7438b8a9b1dSJustin T. Gibbs 	default:
7448b8a9b1dSJustin T. Gibbs 		error = ENOTTY;
7458b8a9b1dSJustin T. Gibbs 		break;
7468b8a9b1dSJustin T. Gibbs 	}
7478b8a9b1dSJustin T. Gibbs 
7488b8a9b1dSJustin T. Gibbs 	return(error);
7498b8a9b1dSJustin T. Gibbs }
7508b8a9b1dSJustin T. Gibbs 
75174bd1c10SNick Hibma static int
75274bd1c10SNick Hibma cam_module_event_handler(module_t mod, int what, void *arg)
75374bd1c10SNick Hibma {
7542b83592fSScott Long 	int error;
7552b83592fSScott Long 
7562b83592fSScott Long 	switch (what) {
7572b83592fSScott Long 	case MOD_LOAD:
7582b83592fSScott Long 		if ((error = xpt_init(NULL)) != 0)
7592b83592fSScott Long 			return (error);
7602b83592fSScott Long 		break;
7612b83592fSScott Long 	case MOD_UNLOAD:
76274bd1c10SNick Hibma 		return EBUSY;
7632b83592fSScott Long 	default:
7643e019deaSPoul-Henning Kamp 		return EOPNOTSUPP;
76574bd1c10SNick Hibma 	}
76674bd1c10SNick Hibma 
76774bd1c10SNick Hibma 	return 0;
76874bd1c10SNick Hibma }
76974bd1c10SNick Hibma 
77083c5d981SAlexander Motin static void
77183c5d981SAlexander Motin xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
77283c5d981SAlexander Motin {
77383c5d981SAlexander Motin 
77483c5d981SAlexander Motin 	if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
77583c5d981SAlexander Motin 		xpt_free_path(done_ccb->ccb_h.path);
77683c5d981SAlexander Motin 		xpt_free_ccb(done_ccb);
77783c5d981SAlexander Motin 	} else {
77883c5d981SAlexander Motin 		done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
77983c5d981SAlexander Motin 		(*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
78083c5d981SAlexander Motin 	}
78183c5d981SAlexander Motin 	xpt_release_boot();
78283c5d981SAlexander Motin }
78383c5d981SAlexander Motin 
7849e6461a2SMatt Jacob /* thread to handle bus rescans */
7859e6461a2SMatt Jacob static void
7869e6461a2SMatt Jacob xpt_scanner_thread(void *dummy)
7879e6461a2SMatt Jacob {
7889e6461a2SMatt Jacob 	union ccb	*ccb;
7892b83592fSScott Long 	struct cam_sim	*sim;
7902b83592fSScott Long 
7912b83592fSScott Long 	xpt_lock_buses();
79283c5d981SAlexander Motin 	for (;;) {
7935a73cc12SKenneth D. Merry 		if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
7942b83592fSScott Long 			msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
7952b83592fSScott Long 			       "ccb_scanq", 0);
79683c5d981SAlexander Motin 		if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
79783c5d981SAlexander Motin 			TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
7982b83592fSScott Long 			xpt_unlock_buses();
7992b83592fSScott Long 
8002b83592fSScott Long 			sim = ccb->ccb_h.path->bus->sim;
80111e4faceSScott Long 			CAM_SIM_LOCK(sim);
80283c5d981SAlexander Motin 			xpt_action(ccb);
80311e4faceSScott Long 			CAM_SIM_UNLOCK(sim);
80483c5d981SAlexander Motin 
80583c5d981SAlexander Motin 			xpt_lock_buses();
8069e6461a2SMatt Jacob 		}
8079e6461a2SMatt Jacob 	}
8089e6461a2SMatt Jacob }
8099e6461a2SMatt Jacob 
8109e6461a2SMatt Jacob void
8119e6461a2SMatt Jacob xpt_rescan(union ccb *ccb)
8129e6461a2SMatt Jacob {
8139e6461a2SMatt Jacob 	struct ccb_hdr *hdr;
8142b83592fSScott Long 
81583c5d981SAlexander Motin 	/* Prepare request */
8160e85f214SMatt Jacob 	if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
817411cadaeSAlexander Motin 	    ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
81883c5d981SAlexander Motin 		ccb->ccb_h.func_code = XPT_SCAN_BUS;
8190e85f214SMatt Jacob 	else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
8200e85f214SMatt Jacob 	    ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
8210e85f214SMatt Jacob 		ccb->ccb_h.func_code = XPT_SCAN_TGT;
8220e85f214SMatt Jacob 	else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
8230e85f214SMatt Jacob 	    ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
82483c5d981SAlexander Motin 		ccb->ccb_h.func_code = XPT_SCAN_LUN;
8250e85f214SMatt Jacob 	else {
8260e85f214SMatt Jacob 		xpt_print(ccb->ccb_h.path, "illegal scan path\n");
8270e85f214SMatt Jacob 		xpt_free_path(ccb->ccb_h.path);
8280e85f214SMatt Jacob 		xpt_free_ccb(ccb);
8290e85f214SMatt Jacob 		return;
8300e85f214SMatt Jacob 	}
83183c5d981SAlexander Motin 	ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
83283c5d981SAlexander Motin 	ccb->ccb_h.cbfcnp = xpt_rescan_done;
83383c5d981SAlexander Motin 	xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
83483c5d981SAlexander Motin 	/* Don't make duplicate entries for the same paths. */
8352b83592fSScott Long 	xpt_lock_buses();
83683c5d981SAlexander Motin 	if (ccb->ccb_h.ppriv_ptr1 == NULL) {
8372b83592fSScott Long 		TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
8389e6461a2SMatt Jacob 			if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
8395a73cc12SKenneth D. Merry 				wakeup(&xsoftc.ccb_scanq);
8402b83592fSScott Long 				xpt_unlock_buses();
8419e6461a2SMatt Jacob 				xpt_print(ccb->ccb_h.path, "rescan already queued\n");
8429e6461a2SMatt Jacob 				xpt_free_path(ccb->ccb_h.path);
8439e6461a2SMatt Jacob 				xpt_free_ccb(ccb);
8449e6461a2SMatt Jacob 				return;
8459e6461a2SMatt Jacob 			}
8469e6461a2SMatt Jacob 		}
84783c5d981SAlexander Motin 	}
8482b83592fSScott Long 	TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
84983c5d981SAlexander Motin 	xsoftc.buses_to_config++;
8502b83592fSScott Long 	wakeup(&xsoftc.ccb_scanq);
8512b83592fSScott Long 	xpt_unlock_buses();
8529e6461a2SMatt Jacob }
8539e6461a2SMatt Jacob 
8548b8a9b1dSJustin T. Gibbs /* Functions accessed by the peripheral drivers */
8552b83592fSScott Long static int
8569e6461a2SMatt Jacob xpt_init(void *dummy)
8578b8a9b1dSJustin T. Gibbs {
8588b8a9b1dSJustin T. Gibbs 	struct cam_sim *xpt_sim;
8598b8a9b1dSJustin T. Gibbs 	struct cam_path *path;
860434bbf6eSJustin T. Gibbs 	struct cam_devq *devq;
8618b8a9b1dSJustin T. Gibbs 	cam_status status;
8628b8a9b1dSJustin T. Gibbs 
8632b83592fSScott Long 	TAILQ_INIT(&xsoftc.xpt_busses);
8649758cc83SScott Long 	TAILQ_INIT(&cam_simq);
8652b83592fSScott Long 	TAILQ_INIT(&xsoftc.ccb_scanq);
8662b83592fSScott Long 	STAILQ_INIT(&xsoftc.highpowerq);
8672b83592fSScott Long 	xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
8688b8a9b1dSJustin T. Gibbs 
8699758cc83SScott Long 	mtx_init(&cam_simq_lock, "CAM SIMQ lock", NULL, MTX_DEF);
8702b83592fSScott Long 	mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
8712b83592fSScott Long 	mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF);
872ef3cf714SScott Long 
8736b156f61SSean Bruno #ifdef CAM_BOOT_DELAY
8746b156f61SSean Bruno 	/*
8756b156f61SSean Bruno 	 * Override this value at compile time to assist our users
8766b156f61SSean Bruno 	 * who don't use loader to boot a kernel.
8776b156f61SSean Bruno 	 */
8786b156f61SSean Bruno 	xsoftc.boot_delay = CAM_BOOT_DELAY;
8796b156f61SSean Bruno #endif
8808b8a9b1dSJustin T. Gibbs 	/*
8818b8a9b1dSJustin T. Gibbs 	 * The xpt layer is, itself, the equivelent of a SIM.
8828b8a9b1dSJustin T. Gibbs 	 * Allow 16 ccbs in the ccb pool for it.  This should
8838b8a9b1dSJustin T. Gibbs 	 * give decent parallelism when we probe busses and
8848b8a9b1dSJustin T. Gibbs 	 * perform other XPT functions.
8858b8a9b1dSJustin T. Gibbs 	 */
886434bbf6eSJustin T. Gibbs 	devq = cam_simq_alloc(16);
887434bbf6eSJustin T. Gibbs 	xpt_sim = cam_sim_alloc(xptaction,
888434bbf6eSJustin T. Gibbs 				xptpoll,
889434bbf6eSJustin T. Gibbs 				"xpt",
890434bbf6eSJustin T. Gibbs 				/*softc*/NULL,
891434bbf6eSJustin T. Gibbs 				/*unit*/0,
8922b83592fSScott Long 				/*mtx*/&xsoftc.xpt_lock,
893434bbf6eSJustin T. Gibbs 				/*max_dev_transactions*/0,
894434bbf6eSJustin T. Gibbs 				/*max_tagged_dev_transactions*/0,
895434bbf6eSJustin T. Gibbs 				devq);
8962b83592fSScott Long 	if (xpt_sim == NULL)
8972b83592fSScott Long 		return (ENOMEM);
8988b8a9b1dSJustin T. Gibbs 
8992b83592fSScott Long 	mtx_lock(&xsoftc.xpt_lock);
900b50569b7SScott Long 	if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
90183c5d981SAlexander Motin 		mtx_unlock(&xsoftc.xpt_lock);
902a2821e04SMatt Jacob 		printf("xpt_init: xpt_bus_register failed with status %#x,"
903df826980SMatt Jacob 		       " failing attach\n", status);
9042b83592fSScott Long 		return (EINVAL);
905df826980SMatt Jacob 	}
9068b8a9b1dSJustin T. Gibbs 
9078b8a9b1dSJustin T. Gibbs 	/*
9088b8a9b1dSJustin T. Gibbs 	 * Looking at the XPT from the SIM layer, the XPT is
9098b8a9b1dSJustin T. Gibbs 	 * the equivelent of a peripheral driver.  Allocate
9108b8a9b1dSJustin T. Gibbs 	 * a peripheral driver entry for us.
9118b8a9b1dSJustin T. Gibbs 	 */
9128b8a9b1dSJustin T. Gibbs 	if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
9138b8a9b1dSJustin T. Gibbs 				      CAM_TARGET_WILDCARD,
9148b8a9b1dSJustin T. Gibbs 				      CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
91583c5d981SAlexander Motin 		mtx_unlock(&xsoftc.xpt_lock);
9168b8a9b1dSJustin T. Gibbs 		printf("xpt_init: xpt_create_path failed with status %#x,"
9178b8a9b1dSJustin T. Gibbs 		       " failing attach\n", status);
9182b83592fSScott Long 		return (EINVAL);
9198b8a9b1dSJustin T. Gibbs 	}
9208b8a9b1dSJustin T. Gibbs 
921ee9c90c7SKenneth D. Merry 	cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
9222b83592fSScott Long 			 path, NULL, 0, xpt_sim);
9238b8a9b1dSJustin T. Gibbs 	xpt_free_path(path);
9242b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_lock);
92583c5d981SAlexander Motin 	/* Install our software interrupt handlers */
92683c5d981SAlexander Motin 	swi_add(NULL, "cambio", camisr, NULL, SWI_CAMBIO, INTR_MPSAFE, &cambio_ih);
9278b8a9b1dSJustin T. Gibbs 	/*
9288b8a9b1dSJustin T. Gibbs 	 * Register a callback for when interrupts are enabled.
9298b8a9b1dSJustin T. Gibbs 	 */
9302b83592fSScott Long 	xsoftc.xpt_config_hook =
9318b8a9b1dSJustin T. Gibbs 	    (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
9320dd50e9bSScott Long 					      M_CAMXPT, M_NOWAIT | M_ZERO);
9332b83592fSScott Long 	if (xsoftc.xpt_config_hook == NULL) {
9348b8a9b1dSJustin T. Gibbs 		printf("xpt_init: Cannot malloc config hook "
9358b8a9b1dSJustin T. Gibbs 		       "- failing attach\n");
9362b83592fSScott Long 		return (ENOMEM);
9378b8a9b1dSJustin T. Gibbs 	}
9382b83592fSScott Long 	xsoftc.xpt_config_hook->ich_func = xpt_config;
9392b83592fSScott Long 	if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
9400dd50e9bSScott Long 		free (xsoftc.xpt_config_hook, M_CAMXPT);
9418b8a9b1dSJustin T. Gibbs 		printf("xpt_init: config_intrhook_establish failed "
9428b8a9b1dSJustin T. Gibbs 		       "- failing attach\n");
9438b8a9b1dSJustin T. Gibbs 	}
9448b8a9b1dSJustin T. Gibbs 
9452b83592fSScott Long 	return (0);
9468b8a9b1dSJustin T. Gibbs }
9478b8a9b1dSJustin T. Gibbs 
9488b8a9b1dSJustin T. Gibbs static cam_status
9498b8a9b1dSJustin T. Gibbs xptregister(struct cam_periph *periph, void *arg)
9508b8a9b1dSJustin T. Gibbs {
9512b83592fSScott Long 	struct cam_sim *xpt_sim;
9522b83592fSScott Long 
9538b8a9b1dSJustin T. Gibbs 	if (periph == NULL) {
9548b8a9b1dSJustin T. Gibbs 		printf("xptregister: periph was NULL!!\n");
9558b8a9b1dSJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
9568b8a9b1dSJustin T. Gibbs 	}
9578b8a9b1dSJustin T. Gibbs 
9582b83592fSScott Long 	xpt_sim = (struct cam_sim *)arg;
9592b83592fSScott Long 	xpt_sim->softc = periph;
9608b8a9b1dSJustin T. Gibbs 	xpt_periph = periph;
9612b83592fSScott Long 	periph->softc = NULL;
9628b8a9b1dSJustin T. Gibbs 
9638b8a9b1dSJustin T. Gibbs 	return(CAM_REQ_CMP);
9648b8a9b1dSJustin T. Gibbs }
9658b8a9b1dSJustin T. Gibbs 
9668b8a9b1dSJustin T. Gibbs int32_t
9678b8a9b1dSJustin T. Gibbs xpt_add_periph(struct cam_periph *periph)
9688b8a9b1dSJustin T. Gibbs {
9698b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
9708b8a9b1dSJustin T. Gibbs 	int32_t	 status;
9718b8a9b1dSJustin T. Gibbs 	struct periph_list *periph_head;
9728b8a9b1dSJustin T. Gibbs 
9732b83592fSScott Long 	mtx_assert(periph->sim->mtx, MA_OWNED);
97468153f43SScott Long 
9758b8a9b1dSJustin T. Gibbs 	device = periph->path->device;
9768b8a9b1dSJustin T. Gibbs 
9778b8a9b1dSJustin T. Gibbs 	periph_head = &device->periphs;
9788b8a9b1dSJustin T. Gibbs 
9798b8a9b1dSJustin T. Gibbs 	status = CAM_REQ_CMP;
9808b8a9b1dSJustin T. Gibbs 
9818b8a9b1dSJustin T. Gibbs 	if (device != NULL) {
9828b8a9b1dSJustin T. Gibbs 		/*
9838b8a9b1dSJustin T. Gibbs 		 * Make room for this peripheral
9848b8a9b1dSJustin T. Gibbs 		 * so it will fit in the queue
9858b8a9b1dSJustin T. Gibbs 		 * when it's scheduled to run
9868b8a9b1dSJustin T. Gibbs 		 */
9878b8a9b1dSJustin T. Gibbs 		status = camq_resize(&device->drvq,
9888b8a9b1dSJustin T. Gibbs 				     device->drvq.array_size + 1);
9898b8a9b1dSJustin T. Gibbs 
9908b8a9b1dSJustin T. Gibbs 		device->generation++;
9918b8a9b1dSJustin T. Gibbs 
9928b8a9b1dSJustin T. Gibbs 		SLIST_INSERT_HEAD(periph_head, periph, periph_links);
9938b8a9b1dSJustin T. Gibbs 	}
9948b8a9b1dSJustin T. Gibbs 
9958b8a9b1dSJustin T. Gibbs 	return (status);
9968b8a9b1dSJustin T. Gibbs }
9978b8a9b1dSJustin T. Gibbs 
9988b8a9b1dSJustin T. Gibbs void
999*a29779e8SAlexander Motin xpt_remove_periph(struct cam_periph *periph)
10008b8a9b1dSJustin T. Gibbs {
10018b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
10028b8a9b1dSJustin T. Gibbs 
10031fa9ee7dSEdward Tomasz Napierala 	mtx_assert(periph->sim->mtx, MA_OWNED);
100468153f43SScott Long 
10058b8a9b1dSJustin T. Gibbs 	device = periph->path->device;
10068b8a9b1dSJustin T. Gibbs 
10078b8a9b1dSJustin T. Gibbs 	if (device != NULL) {
10088b8a9b1dSJustin T. Gibbs 		struct periph_list *periph_head;
10098b8a9b1dSJustin T. Gibbs 
10108b8a9b1dSJustin T. Gibbs 		periph_head = &device->periphs;
10118b8a9b1dSJustin T. Gibbs 
10128b8a9b1dSJustin T. Gibbs 		/* Release the slot for this peripheral */
10138b8a9b1dSJustin T. Gibbs 		camq_resize(&device->drvq, device->drvq.array_size - 1);
10148b8a9b1dSJustin T. Gibbs 
10158b8a9b1dSJustin T. Gibbs 		device->generation++;
10168b8a9b1dSJustin T. Gibbs 
1017e3975643SJake Burkholder 		SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
10188b8a9b1dSJustin T. Gibbs 	}
10198b8a9b1dSJustin T. Gibbs }
10208b8a9b1dSJustin T. Gibbs 
10213393f8daSKenneth D. Merry 
10223393f8daSKenneth D. Merry void
10233393f8daSKenneth D. Merry xpt_announce_periph(struct cam_periph *periph, char *announce_string)
10243393f8daSKenneth D. Merry {
102557079b17SAlexander Motin 	struct	cam_path *path = periph->path;
10263393f8daSKenneth D. Merry 
10272b83592fSScott Long 	mtx_assert(periph->sim->mtx, MA_OWNED);
102868153f43SScott Long 
1029ad413009SAlexander Motin 	printf("%s%d at %s%d bus %d scbus%d target %d lun %d\n",
10303393f8daSKenneth D. Merry 	       periph->periph_name, periph->unit_number,
10313393f8daSKenneth D. Merry 	       path->bus->sim->sim_name,
10323393f8daSKenneth D. Merry 	       path->bus->sim->unit_number,
10333393f8daSKenneth D. Merry 	       path->bus->sim->bus_id,
1034ad413009SAlexander Motin 	       path->bus->path_id,
10353393f8daSKenneth D. Merry 	       path->target->target_id,
10363393f8daSKenneth D. Merry 	       path->device->lun_id);
10373393f8daSKenneth D. Merry 	printf("%s%d: ", periph->periph_name, periph->unit_number);
103852c9ce25SScott Long 	if (path->device->protocol == PROTO_SCSI)
10393393f8daSKenneth D. Merry 		scsi_print_inquiry(&path->device->inq_data);
104052c9ce25SScott Long 	else if (path->device->protocol == PROTO_ATA ||
104152c9ce25SScott Long 	    path->device->protocol == PROTO_SATAPM)
104252c9ce25SScott Long 		ata_print_ident(&path->device->ident_data);
10433089bb2eSAlexander Motin 	else if (path->device->protocol == PROTO_SEMB)
10443089bb2eSAlexander Motin 		semb_print_ident(
10453089bb2eSAlexander Motin 		    (struct sep_identify_data *)&path->device->ident_data);
104652c9ce25SScott Long 	else
104752c9ce25SScott Long 		printf("Unknown protocol device\n");
1048f053d777SMatt Jacob 	if (bootverbose && path->device->serial_num_len > 0) {
10493393f8daSKenneth D. Merry 		/* Don't wrap the screen  - print only the first 60 chars */
10503393f8daSKenneth D. Merry 		printf("%s%d: Serial Number %.60s\n", periph->periph_name,
10513393f8daSKenneth D. Merry 		       periph->unit_number, path->device->serial_num);
10523393f8daSKenneth D. Merry 	}
105357079b17SAlexander Motin 	/* Announce transport details. */
105457079b17SAlexander Motin 	(*(path->bus->xport->announce))(periph);
105557079b17SAlexander Motin 	/* Announce command queueing. */
10563393f8daSKenneth D. Merry 	if (path->device->inq_flags & SID_CmdQue
10573393f8daSKenneth D. Merry 	 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
10580aacc535SAlexander Motin 		printf("%s%d: Command Queueing enabled\n",
10593393f8daSKenneth D. Merry 		       periph->periph_name, periph->unit_number);
10603393f8daSKenneth D. Merry 	}
106157079b17SAlexander Motin 	/* Announce caller's details if they've passed in. */
10623393f8daSKenneth D. Merry 	if (announce_string != NULL)
10633393f8daSKenneth D. Merry 		printf("%s%d: %s\n", periph->periph_name,
10643393f8daSKenneth D. Merry 		       periph->unit_number, announce_string);
10653393f8daSKenneth D. Merry }
10668b8a9b1dSJustin T. Gibbs 
10676fb5c84eSSteven Hartland void
10686fb5c84eSSteven Hartland xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
10696fb5c84eSSteven Hartland {
10706fb5c84eSSteven Hartland 	if (quirks != 0) {
10716fb5c84eSSteven Hartland 		printf("%s%d: quirks=0x%b\n", periph->periph_name,
10726fb5c84eSSteven Hartland 		    periph->unit_number, quirks, bit_string);
10736fb5c84eSSteven Hartland 	}
10746fb5c84eSSteven Hartland }
10756fb5c84eSSteven Hartland 
10763501942bSJustin T. Gibbs int
10773501942bSJustin T. Gibbs xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
10783501942bSJustin T. Gibbs {
1079ccba7102SAlexander Motin 	int ret = -1, l;
10803501942bSJustin T. Gibbs 	struct ccb_dev_advinfo cdai;
1081ccba7102SAlexander Motin 	struct scsi_vpd_id_descriptor *idd;
10823501942bSJustin T. Gibbs 
10836884b662SAlexander Motin 	mtx_assert(path->bus->sim->mtx, MA_OWNED);
10846884b662SAlexander Motin 
10853501942bSJustin T. Gibbs 	memset(&cdai, 0, sizeof(cdai));
10863501942bSJustin T. Gibbs 	xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
10873501942bSJustin T. Gibbs 	cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
10883501942bSJustin T. Gibbs 	cdai.bufsiz = len;
10893501942bSJustin T. Gibbs 
10903501942bSJustin T. Gibbs 	if (!strcmp(attr, "GEOM::ident"))
10913501942bSJustin T. Gibbs 		cdai.buftype = CDAI_TYPE_SERIAL_NUM;
10923501942bSJustin T. Gibbs 	else if (!strcmp(attr, "GEOM::physpath"))
10933501942bSJustin T. Gibbs 		cdai.buftype = CDAI_TYPE_PHYS_PATH;
1094ccba7102SAlexander Motin 	else if (!strcmp(attr, "GEOM::lunid")) {
1095ccba7102SAlexander Motin 		cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1096ccba7102SAlexander Motin 		cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1097ccba7102SAlexander Motin 	} else
10983501942bSJustin T. Gibbs 		goto out;
10993501942bSJustin T. Gibbs 
11003501942bSJustin T. Gibbs 	cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
11013501942bSJustin T. Gibbs 	if (cdai.buf == NULL) {
11023501942bSJustin T. Gibbs 		ret = ENOMEM;
11033501942bSJustin T. Gibbs 		goto out;
11043501942bSJustin T. Gibbs 	}
11053501942bSJustin T. Gibbs 	xpt_action((union ccb *)&cdai); /* can only be synchronous */
11063501942bSJustin T. Gibbs 	if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
11073501942bSJustin T. Gibbs 		cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
11083501942bSJustin T. Gibbs 	if (cdai.provsiz == 0)
11093501942bSJustin T. Gibbs 		goto out;
1110ccba7102SAlexander Motin 	if (cdai.buftype == CDAI_TYPE_SCSI_DEVID) {
1111ccba7102SAlexander Motin 		idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1112ccba7102SAlexander Motin 		    cdai.provsiz, scsi_devid_is_lun_naa);
1113ccba7102SAlexander Motin 		if (idd == NULL)
1114ccba7102SAlexander Motin 			idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1115ccba7102SAlexander Motin 			    cdai.provsiz, scsi_devid_is_lun_eui64);
1116ccba7102SAlexander Motin 		if (idd == NULL)
1117ccba7102SAlexander Motin 			idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1118ccba7102SAlexander Motin 			    cdai.provsiz, scsi_devid_is_lun_t10);
1119ccba7102SAlexander Motin 		if (idd == NULL)
1120ccba7102SAlexander Motin 			idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1121ccba7102SAlexander Motin 			    cdai.provsiz, scsi_devid_is_lun_name);
1122ccba7102SAlexander Motin 		if (idd == NULL)
1123ccba7102SAlexander Motin 			goto out;
1124ccba7102SAlexander Motin 		ret = 0;
1125ccba7102SAlexander Motin 		if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_ASCII ||
1126ccba7102SAlexander Motin 		    (idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_UTF8) {
1127ccba7102SAlexander Motin 			l = strnlen(idd->identifier, idd->length);
1128ccba7102SAlexander Motin 			if (l < len) {
1129ccba7102SAlexander Motin 				bcopy(idd->identifier, buf, l);
1130ccba7102SAlexander Motin 				buf[l] = 0;
1131ccba7102SAlexander Motin 			} else
1132ccba7102SAlexander Motin 				ret = EFAULT;
1133ccba7102SAlexander Motin 		} else {
1134ccba7102SAlexander Motin 			if (idd->length * 2 < len) {
1135ccba7102SAlexander Motin 				for (l = 0; l < idd->length; l++)
1136ccba7102SAlexander Motin 					sprintf(buf + l * 2, "%02x",
1137ccba7102SAlexander Motin 					    idd->identifier[l]);
1138ccba7102SAlexander Motin 			} else
1139ccba7102SAlexander Motin 				ret = EFAULT;
1140ccba7102SAlexander Motin 		}
1141ccba7102SAlexander Motin 	} else {
11423501942bSJustin T. Gibbs 		ret = 0;
11433501942bSJustin T. Gibbs 		if (strlcpy(buf, cdai.buf, len) >= len)
11443501942bSJustin T. Gibbs 			ret = EFAULT;
1145ccba7102SAlexander Motin 	}
11463501942bSJustin T. Gibbs 
11473501942bSJustin T. Gibbs out:
11483501942bSJustin T. Gibbs 	if (cdai.buf != NULL)
11493501942bSJustin T. Gibbs 		free(cdai.buf, M_CAMXPT);
11503501942bSJustin T. Gibbs 	return ret;
11513501942bSJustin T. Gibbs }
11523501942bSJustin T. Gibbs 
11538b8a9b1dSJustin T. Gibbs static dev_match_ret
11543393f8daSKenneth D. Merry xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
11558b8a9b1dSJustin T. Gibbs 	    struct cam_eb *bus)
11568b8a9b1dSJustin T. Gibbs {
11578b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
11588b8a9b1dSJustin T. Gibbs 	int i;
11598b8a9b1dSJustin T. Gibbs 
11608b8a9b1dSJustin T. Gibbs 	retval = DM_RET_NONE;
11618b8a9b1dSJustin T. Gibbs 
11628b8a9b1dSJustin T. Gibbs 	/*
11638b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
11648b8a9b1dSJustin T. Gibbs 	 */
11658b8a9b1dSJustin T. Gibbs 	if (bus == NULL)
11668b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
11678b8a9b1dSJustin T. Gibbs 
11688b8a9b1dSJustin T. Gibbs 	/*
11698b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this bus matches no
11708b8a9b1dSJustin T. Gibbs 	 * matter what.
11718b8a9b1dSJustin T. Gibbs 	 */
11728b8a9b1dSJustin T. Gibbs 	if ((patterns == NULL) || (num_patterns == 0))
11738b8a9b1dSJustin T. Gibbs 		return(DM_RET_DESCEND | DM_RET_COPY);
11748b8a9b1dSJustin T. Gibbs 
11758b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
11768b8a9b1dSJustin T. Gibbs 		struct bus_match_pattern *cur_pattern;
11778b8a9b1dSJustin T. Gibbs 
11788b8a9b1dSJustin T. Gibbs 		/*
11798b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a bus node, we
11808b8a9b1dSJustin T. Gibbs 		 * aren't interested.  However, we do indicate to the
11818b8a9b1dSJustin T. Gibbs 		 * calling routine that we should continue descending the
11828b8a9b1dSJustin T. Gibbs 		 * tree, since the user wants to match against lower-level
11838b8a9b1dSJustin T. Gibbs 		 * EDT elements.
11848b8a9b1dSJustin T. Gibbs 		 */
11858b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_BUS) {
11868b8a9b1dSJustin T. Gibbs 			if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
11878b8a9b1dSJustin T. Gibbs 				retval |= DM_RET_DESCEND;
11888b8a9b1dSJustin T. Gibbs 			continue;
11898b8a9b1dSJustin T. Gibbs 		}
11908b8a9b1dSJustin T. Gibbs 
11918b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.bus_pattern;
11928b8a9b1dSJustin T. Gibbs 
11938b8a9b1dSJustin T. Gibbs 		/*
11948b8a9b1dSJustin T. Gibbs 		 * If they want to match any bus node, we give them any
11958b8a9b1dSJustin T. Gibbs 		 * device node.
11968b8a9b1dSJustin T. Gibbs 		 */
11978b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == BUS_MATCH_ANY) {
11988b8a9b1dSJustin T. Gibbs 			/* set the copy flag */
11998b8a9b1dSJustin T. Gibbs 			retval |= DM_RET_COPY;
12008b8a9b1dSJustin T. Gibbs 
12018b8a9b1dSJustin T. Gibbs 			/*
12028b8a9b1dSJustin T. Gibbs 			 * If we've already decided on an action, go ahead
12038b8a9b1dSJustin T. Gibbs 			 * and return.
12048b8a9b1dSJustin T. Gibbs 			 */
12058b8a9b1dSJustin T. Gibbs 			if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
12068b8a9b1dSJustin T. Gibbs 				return(retval);
12078b8a9b1dSJustin T. Gibbs 		}
12088b8a9b1dSJustin T. Gibbs 
12098b8a9b1dSJustin T. Gibbs 		/*
12108b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
12118b8a9b1dSJustin T. Gibbs 		 */
12128b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == BUS_MATCH_NONE)
12138b8a9b1dSJustin T. Gibbs 			continue;
12148b8a9b1dSJustin T. Gibbs 
12158b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
12168b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != bus->path_id))
12178b8a9b1dSJustin T. Gibbs 			continue;
12188b8a9b1dSJustin T. Gibbs 
12198b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
12208b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->bus_id != bus->sim->bus_id))
12218b8a9b1dSJustin T. Gibbs 			continue;
12228b8a9b1dSJustin T. Gibbs 
12238b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
12248b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->unit_number != bus->sim->unit_number))
12258b8a9b1dSJustin T. Gibbs 			continue;
12268b8a9b1dSJustin T. Gibbs 
12278b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
12288b8a9b1dSJustin T. Gibbs 		 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
12298b8a9b1dSJustin T. Gibbs 			     DEV_IDLEN) != 0))
12308b8a9b1dSJustin T. Gibbs 			continue;
12318b8a9b1dSJustin T. Gibbs 
12328b8a9b1dSJustin T. Gibbs 		/*
12338b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
12348b8a9b1dSJustin T. Gibbs 		 * information on this bus.  So tell the caller to copy the
12358b8a9b1dSJustin T. Gibbs 		 * data out.
12368b8a9b1dSJustin T. Gibbs 		 */
12378b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
12388b8a9b1dSJustin T. Gibbs 
12398b8a9b1dSJustin T. Gibbs 		/*
12408b8a9b1dSJustin T. Gibbs 		 * If the return action has been set to descend, then we
12418b8a9b1dSJustin T. Gibbs 		 * know that we've already seen a non-bus matching
12428b8a9b1dSJustin T. Gibbs 		 * expression, therefore we need to further descend the tree.
12438b8a9b1dSJustin T. Gibbs 		 * This won't change by continuing around the loop, so we
12448b8a9b1dSJustin T. Gibbs 		 * go ahead and return.  If we haven't seen a non-bus
12458b8a9b1dSJustin T. Gibbs 		 * matching expression, we keep going around the loop until
12468b8a9b1dSJustin T. Gibbs 		 * we exhaust the matching expressions.  We'll set the stop
12478b8a9b1dSJustin T. Gibbs 		 * flag once we fall out of the loop.
12488b8a9b1dSJustin T. Gibbs 		 */
12498b8a9b1dSJustin T. Gibbs 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
12508b8a9b1dSJustin T. Gibbs 			return(retval);
12518b8a9b1dSJustin T. Gibbs 	}
12528b8a9b1dSJustin T. Gibbs 
12538b8a9b1dSJustin T. Gibbs 	/*
12548b8a9b1dSJustin T. Gibbs 	 * If the return action hasn't been set to descend yet, that means
12558b8a9b1dSJustin T. Gibbs 	 * we haven't seen anything other than bus matching patterns.  So
12568b8a9b1dSJustin T. Gibbs 	 * tell the caller to stop descending the tree -- the user doesn't
12578b8a9b1dSJustin T. Gibbs 	 * want to match against lower level tree elements.
12588b8a9b1dSJustin T. Gibbs 	 */
12598b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
12608b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_STOP;
12618b8a9b1dSJustin T. Gibbs 
12628b8a9b1dSJustin T. Gibbs 	return(retval);
12638b8a9b1dSJustin T. Gibbs }
12648b8a9b1dSJustin T. Gibbs 
12658b8a9b1dSJustin T. Gibbs static dev_match_ret
12663393f8daSKenneth D. Merry xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
12678b8a9b1dSJustin T. Gibbs 	       struct cam_ed *device)
12688b8a9b1dSJustin T. Gibbs {
12698b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
12708b8a9b1dSJustin T. Gibbs 	int i;
12718b8a9b1dSJustin T. Gibbs 
12728b8a9b1dSJustin T. Gibbs 	retval = DM_RET_NONE;
12738b8a9b1dSJustin T. Gibbs 
12748b8a9b1dSJustin T. Gibbs 	/*
12758b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
12768b8a9b1dSJustin T. Gibbs 	 */
12778b8a9b1dSJustin T. Gibbs 	if (device == NULL)
12788b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
12798b8a9b1dSJustin T. Gibbs 
12808b8a9b1dSJustin T. Gibbs 	/*
12818b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this device matches no
12828b8a9b1dSJustin T. Gibbs 	 * matter what.
12838b8a9b1dSJustin T. Gibbs 	 */
128459e75884SColin Percival 	if ((patterns == NULL) || (num_patterns == 0))
12858b8a9b1dSJustin T. Gibbs 		return(DM_RET_DESCEND | DM_RET_COPY);
12868b8a9b1dSJustin T. Gibbs 
12878b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
12888b8a9b1dSJustin T. Gibbs 		struct device_match_pattern *cur_pattern;
12893501942bSJustin T. Gibbs 		struct scsi_vpd_device_id *device_id_page;
12908b8a9b1dSJustin T. Gibbs 
12918b8a9b1dSJustin T. Gibbs 		/*
12928b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a device node, we
12938b8a9b1dSJustin T. Gibbs 		 * aren't interested.
12948b8a9b1dSJustin T. Gibbs 		 */
12958b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_DEVICE) {
12968b8a9b1dSJustin T. Gibbs 			if ((patterns[i].type == DEV_MATCH_PERIPH)
12978b8a9b1dSJustin T. Gibbs 			 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
12988b8a9b1dSJustin T. Gibbs 				retval |= DM_RET_DESCEND;
12998b8a9b1dSJustin T. Gibbs 			continue;
13008b8a9b1dSJustin T. Gibbs 		}
13018b8a9b1dSJustin T. Gibbs 
13028b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.device_pattern;
13038b8a9b1dSJustin T. Gibbs 
13043501942bSJustin T. Gibbs 		/* Error out if mutually exclusive options are specified. */
13053501942bSJustin T. Gibbs 		if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
13063501942bSJustin T. Gibbs 		 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
13073501942bSJustin T. Gibbs 			return(DM_RET_ERROR);
13083501942bSJustin T. Gibbs 
13098b8a9b1dSJustin T. Gibbs 		/*
13108b8a9b1dSJustin T. Gibbs 		 * If they want to match any device node, we give them any
13118b8a9b1dSJustin T. Gibbs 		 * device node.
13128b8a9b1dSJustin T. Gibbs 		 */
13133501942bSJustin T. Gibbs 		if (cur_pattern->flags == DEV_MATCH_ANY)
13143501942bSJustin T. Gibbs 			goto copy_dev_node;
13158b8a9b1dSJustin T. Gibbs 
13168b8a9b1dSJustin T. Gibbs 		/*
13178b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
13188b8a9b1dSJustin T. Gibbs 		 */
13198b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == DEV_MATCH_NONE)
13208b8a9b1dSJustin T. Gibbs 			continue;
13218b8a9b1dSJustin T. Gibbs 
13228b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
13238b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != device->target->bus->path_id))
13248b8a9b1dSJustin T. Gibbs 			continue;
13258b8a9b1dSJustin T. Gibbs 
13268b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
13278b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->target_id != device->target->target_id))
13288b8a9b1dSJustin T. Gibbs 			continue;
13298b8a9b1dSJustin T. Gibbs 
13308b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
13318b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->target_lun != device->lun_id))
13328b8a9b1dSJustin T. Gibbs 			continue;
13338b8a9b1dSJustin T. Gibbs 
13348b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
13358b8a9b1dSJustin T. Gibbs 		 && (cam_quirkmatch((caddr_t)&device->inq_data,
13363501942bSJustin T. Gibbs 				    (caddr_t)&cur_pattern->data.inq_pat,
13373501942bSJustin T. Gibbs 				    1, sizeof(cur_pattern->data.inq_pat),
13388b8a9b1dSJustin T. Gibbs 				    scsi_static_inquiry_match) == NULL))
13398b8a9b1dSJustin T. Gibbs 			continue;
13408b8a9b1dSJustin T. Gibbs 
13413501942bSJustin T. Gibbs 		device_id_page = (struct scsi_vpd_device_id *)device->device_id;
13423501942bSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
13433501942bSJustin T. Gibbs 		 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
13443501942bSJustin T. Gibbs 		  || scsi_devid_match((uint8_t *)device_id_page->desc_list,
13453501942bSJustin T. Gibbs 				      device->device_id_len
13463501942bSJustin T. Gibbs 				    - SVPD_DEVICE_ID_HDR_LEN,
13473501942bSJustin T. Gibbs 				      cur_pattern->data.devid_pat.id,
13483501942bSJustin T. Gibbs 				      cur_pattern->data.devid_pat.id_len) != 0))
13493501942bSJustin T. Gibbs 			continue;
13503501942bSJustin T. Gibbs 
13513501942bSJustin T. Gibbs copy_dev_node:
13528b8a9b1dSJustin T. Gibbs 		/*
13538b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
13548b8a9b1dSJustin T. Gibbs 		 * information on this device.  So tell the caller to copy
13558b8a9b1dSJustin T. Gibbs 		 * the data out.
13568b8a9b1dSJustin T. Gibbs 		 */
13578b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
13588b8a9b1dSJustin T. Gibbs 
13598b8a9b1dSJustin T. Gibbs 		/*
13608b8a9b1dSJustin T. Gibbs 		 * If the return action has been set to descend, then we
13618b8a9b1dSJustin T. Gibbs 		 * know that we've already seen a peripheral matching
13628b8a9b1dSJustin T. Gibbs 		 * expression, therefore we need to further descend the tree.
13638b8a9b1dSJustin T. Gibbs 		 * This won't change by continuing around the loop, so we
13648b8a9b1dSJustin T. Gibbs 		 * go ahead and return.  If we haven't seen a peripheral
13658b8a9b1dSJustin T. Gibbs 		 * matching expression, we keep going around the loop until
13668b8a9b1dSJustin T. Gibbs 		 * we exhaust the matching expressions.  We'll set the stop
13678b8a9b1dSJustin T. Gibbs 		 * flag once we fall out of the loop.
13688b8a9b1dSJustin T. Gibbs 		 */
13698b8a9b1dSJustin T. Gibbs 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
13708b8a9b1dSJustin T. Gibbs 			return(retval);
13718b8a9b1dSJustin T. Gibbs 	}
13728b8a9b1dSJustin T. Gibbs 
13738b8a9b1dSJustin T. Gibbs 	/*
13748b8a9b1dSJustin T. Gibbs 	 * If the return action hasn't been set to descend yet, that means
13758b8a9b1dSJustin T. Gibbs 	 * we haven't seen any peripheral matching patterns.  So tell the
13768b8a9b1dSJustin T. Gibbs 	 * caller to stop descending the tree -- the user doesn't want to
13778b8a9b1dSJustin T. Gibbs 	 * match against lower level tree elements.
13788b8a9b1dSJustin T. Gibbs 	 */
13798b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
13808b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_STOP;
13818b8a9b1dSJustin T. Gibbs 
13828b8a9b1dSJustin T. Gibbs 	return(retval);
13838b8a9b1dSJustin T. Gibbs }
13848b8a9b1dSJustin T. Gibbs 
13858b8a9b1dSJustin T. Gibbs /*
13868b8a9b1dSJustin T. Gibbs  * Match a single peripheral against any number of match patterns.
13878b8a9b1dSJustin T. Gibbs  */
13888b8a9b1dSJustin T. Gibbs static dev_match_ret
13893393f8daSKenneth D. Merry xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
13908b8a9b1dSJustin T. Gibbs 	       struct cam_periph *periph)
13918b8a9b1dSJustin T. Gibbs {
13928b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
13938b8a9b1dSJustin T. Gibbs 	int i;
13948b8a9b1dSJustin T. Gibbs 
13958b8a9b1dSJustin T. Gibbs 	/*
13968b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
13978b8a9b1dSJustin T. Gibbs 	 */
13988b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
13998b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
14008b8a9b1dSJustin T. Gibbs 
14018b8a9b1dSJustin T. Gibbs 	/*
14028b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this peripheral matches no
14038b8a9b1dSJustin T. Gibbs 	 * matter what.
14048b8a9b1dSJustin T. Gibbs 	 */
14058b8a9b1dSJustin T. Gibbs 	if ((patterns == NULL) || (num_patterns == 0))
14068b8a9b1dSJustin T. Gibbs 		return(DM_RET_STOP | DM_RET_COPY);
14078b8a9b1dSJustin T. Gibbs 
14088b8a9b1dSJustin T. Gibbs 	/*
14098b8a9b1dSJustin T. Gibbs 	 * There aren't any nodes below a peripheral node, so there's no
14108b8a9b1dSJustin T. Gibbs 	 * reason to descend the tree any further.
14118b8a9b1dSJustin T. Gibbs 	 */
14128b8a9b1dSJustin T. Gibbs 	retval = DM_RET_STOP;
14138b8a9b1dSJustin T. Gibbs 
14148b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
14158b8a9b1dSJustin T. Gibbs 		struct periph_match_pattern *cur_pattern;
14168b8a9b1dSJustin T. Gibbs 
14178b8a9b1dSJustin T. Gibbs 		/*
14188b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a peripheral, we
14198b8a9b1dSJustin T. Gibbs 		 * aren't interested.
14208b8a9b1dSJustin T. Gibbs 		 */
14218b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_PERIPH)
14228b8a9b1dSJustin T. Gibbs 			continue;
14238b8a9b1dSJustin T. Gibbs 
14248b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.periph_pattern;
14258b8a9b1dSJustin T. Gibbs 
14268b8a9b1dSJustin T. Gibbs 		/*
14278b8a9b1dSJustin T. Gibbs 		 * If they want to match on anything, then we will do so.
14288b8a9b1dSJustin T. Gibbs 		 */
14298b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == PERIPH_MATCH_ANY) {
14308b8a9b1dSJustin T. Gibbs 			/* set the copy flag */
14318b8a9b1dSJustin T. Gibbs 			retval |= DM_RET_COPY;
14328b8a9b1dSJustin T. Gibbs 
14338b8a9b1dSJustin T. Gibbs 			/*
14348b8a9b1dSJustin T. Gibbs 			 * We've already set the return action to stop,
14358b8a9b1dSJustin T. Gibbs 			 * since there are no nodes below peripherals in
14368b8a9b1dSJustin T. Gibbs 			 * the tree.
14378b8a9b1dSJustin T. Gibbs 			 */
14388b8a9b1dSJustin T. Gibbs 			return(retval);
14398b8a9b1dSJustin T. Gibbs 		}
14408b8a9b1dSJustin T. Gibbs 
14418b8a9b1dSJustin T. Gibbs 		/*
14428b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
14438b8a9b1dSJustin T. Gibbs 		 */
14448b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == PERIPH_MATCH_NONE)
14458b8a9b1dSJustin T. Gibbs 			continue;
14468b8a9b1dSJustin T. Gibbs 
14478b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
14488b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != periph->path->bus->path_id))
14498b8a9b1dSJustin T. Gibbs 			continue;
14508b8a9b1dSJustin T. Gibbs 
14518b8a9b1dSJustin T. Gibbs 		/*
14528b8a9b1dSJustin T. Gibbs 		 * For the target and lun id's, we have to make sure the
14538b8a9b1dSJustin T. Gibbs 		 * target and lun pointers aren't NULL.  The xpt peripheral
14548b8a9b1dSJustin T. Gibbs 		 * has a wildcard target and device.
14558b8a9b1dSJustin T. Gibbs 		 */
14568b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
14578b8a9b1dSJustin T. Gibbs 		 && ((periph->path->target == NULL)
14588b8a9b1dSJustin T. Gibbs 		 ||(cur_pattern->target_id != periph->path->target->target_id)))
14598b8a9b1dSJustin T. Gibbs 			continue;
14608b8a9b1dSJustin T. Gibbs 
14618b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
14628b8a9b1dSJustin T. Gibbs 		 && ((periph->path->device == NULL)
14638b8a9b1dSJustin T. Gibbs 		 || (cur_pattern->target_lun != periph->path->device->lun_id)))
14648b8a9b1dSJustin T. Gibbs 			continue;
14658b8a9b1dSJustin T. Gibbs 
14668b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
14678b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->unit_number != periph->unit_number))
14688b8a9b1dSJustin T. Gibbs 			continue;
14698b8a9b1dSJustin T. Gibbs 
14708b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
14718b8a9b1dSJustin T. Gibbs 		 && (strncmp(cur_pattern->periph_name, periph->periph_name,
14728b8a9b1dSJustin T. Gibbs 			     DEV_IDLEN) != 0))
14738b8a9b1dSJustin T. Gibbs 			continue;
14748b8a9b1dSJustin T. Gibbs 
14758b8a9b1dSJustin T. Gibbs 		/*
14768b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
14778b8a9b1dSJustin T. Gibbs 		 * information on this peripheral.  So tell the caller to
14788b8a9b1dSJustin T. Gibbs 		 * copy the data out.
14798b8a9b1dSJustin T. Gibbs 		 */
14808b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
14818b8a9b1dSJustin T. Gibbs 
14828b8a9b1dSJustin T. Gibbs 		/*
14838b8a9b1dSJustin T. Gibbs 		 * The return action has already been set to stop, since
14848b8a9b1dSJustin T. Gibbs 		 * peripherals don't have any nodes below them in the EDT.
14858b8a9b1dSJustin T. Gibbs 		 */
14868b8a9b1dSJustin T. Gibbs 		return(retval);
14878b8a9b1dSJustin T. Gibbs 	}
14888b8a9b1dSJustin T. Gibbs 
14898b8a9b1dSJustin T. Gibbs 	/*
14908b8a9b1dSJustin T. Gibbs 	 * If we get to this point, the peripheral that was passed in
14918b8a9b1dSJustin T. Gibbs 	 * doesn't match any of the patterns.
14928b8a9b1dSJustin T. Gibbs 	 */
14938b8a9b1dSJustin T. Gibbs 	return(retval);
14948b8a9b1dSJustin T. Gibbs }
14958b8a9b1dSJustin T. Gibbs 
14968b8a9b1dSJustin T. Gibbs static int
14978b8a9b1dSJustin T. Gibbs xptedtbusfunc(struct cam_eb *bus, void *arg)
14988b8a9b1dSJustin T. Gibbs {
14998b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
15008b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
15018b8a9b1dSJustin T. Gibbs 
15028b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
15038b8a9b1dSJustin T. Gibbs 
15048b8a9b1dSJustin T. Gibbs 	/*
15058b8a9b1dSJustin T. Gibbs 	 * If our position is for something deeper in the tree, that means
15068b8a9b1dSJustin T. Gibbs 	 * that we've already seen this node.  So, we keep going down.
15078b8a9b1dSJustin T. Gibbs 	 */
15088b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
15098b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == bus)
15108b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
15118b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target != NULL))
15128b8a9b1dSJustin T. Gibbs 		retval = DM_RET_DESCEND;
15138b8a9b1dSJustin T. Gibbs 	else
15148b8a9b1dSJustin T. Gibbs 		retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
15158b8a9b1dSJustin T. Gibbs 
15168b8a9b1dSJustin T. Gibbs 	/*
15178b8a9b1dSJustin T. Gibbs 	 * If we got an error, bail out of the search.
15188b8a9b1dSJustin T. Gibbs 	 */
15198b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
15208b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
15218b8a9b1dSJustin T. Gibbs 		return(0);
15228b8a9b1dSJustin T. Gibbs 	}
15238b8a9b1dSJustin T. Gibbs 
15248b8a9b1dSJustin T. Gibbs 	/*
15258b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this bus out.
15268b8a9b1dSJustin T. Gibbs 	 */
15278b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
15288b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
15298b8a9b1dSJustin T. Gibbs 
15308b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
15318b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
15328b8a9b1dSJustin T. Gibbs 
15338b8a9b1dSJustin T. Gibbs 		/*
15348b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
15358b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
15368b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
15378b8a9b1dSJustin T. Gibbs 		 */
15388b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
15398b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
15408b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
15418b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
15428b8a9b1dSJustin T. Gibbs 
15438b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = bus;
15448b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
15452b83592fSScott Long 				xsoftc.bus_generation;
15468b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
15478b8a9b1dSJustin T. Gibbs 			return(0);
15488b8a9b1dSJustin T. Gibbs 		}
15498b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
15508b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
15518b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_BUS;
15528b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.path_id = bus->path_id;
15538b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
15548b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.unit_number =
15558b8a9b1dSJustin T. Gibbs 			bus->sim->unit_number;
15568b8a9b1dSJustin T. Gibbs 		strncpy(cdm->matches[j].result.bus_result.dev_name,
15578b8a9b1dSJustin T. Gibbs 			bus->sim->sim_name, DEV_IDLEN);
15588b8a9b1dSJustin T. Gibbs 	}
15598b8a9b1dSJustin T. Gibbs 
15608b8a9b1dSJustin T. Gibbs 	/*
15618b8a9b1dSJustin T. Gibbs 	 * If the user is only interested in busses, there's no
15628b8a9b1dSJustin T. Gibbs 	 * reason to descend to the next level in the tree.
15638b8a9b1dSJustin T. Gibbs 	 */
15648b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
15658b8a9b1dSJustin T. Gibbs 		return(1);
15668b8a9b1dSJustin T. Gibbs 
15678b8a9b1dSJustin T. Gibbs 	/*
15688b8a9b1dSJustin T. Gibbs 	 * If there is a target generation recorded, check it to
15698b8a9b1dSJustin T. Gibbs 	 * make sure the target list hasn't changed.
15708b8a9b1dSJustin T. Gibbs 	 */
15718b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
15728b8a9b1dSJustin T. Gibbs 	 && (bus == cdm->pos.cookie.bus)
15738b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
15748b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
15758b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
15768b8a9b1dSJustin T. Gibbs 	     bus->generation)) {
15778b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
15788b8a9b1dSJustin T. Gibbs 		return(0);
15798b8a9b1dSJustin T. Gibbs 	}
15808b8a9b1dSJustin T. Gibbs 
15818b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
15828b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == bus)
15838b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
15848b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target != NULL))
15858b8a9b1dSJustin T. Gibbs 		return(xpttargettraverse(bus,
15868b8a9b1dSJustin T. Gibbs 					(struct cam_et *)cdm->pos.cookie.target,
15878b8a9b1dSJustin T. Gibbs 					 xptedttargetfunc, arg));
15888b8a9b1dSJustin T. Gibbs 	else
15898b8a9b1dSJustin T. Gibbs 		return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
15908b8a9b1dSJustin T. Gibbs }
15918b8a9b1dSJustin T. Gibbs 
15928b8a9b1dSJustin T. Gibbs static int
15938b8a9b1dSJustin T. Gibbs xptedttargetfunc(struct cam_et *target, void *arg)
15948b8a9b1dSJustin T. Gibbs {
15958b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
15968b8a9b1dSJustin T. Gibbs 
15978b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
15988b8a9b1dSJustin T. Gibbs 
15998b8a9b1dSJustin T. Gibbs 	/*
16008b8a9b1dSJustin T. Gibbs 	 * If there is a device list generation recorded, check it to
16018b8a9b1dSJustin T. Gibbs 	 * make sure the device list hasn't changed.
16028b8a9b1dSJustin T. Gibbs 	 */
16038b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
16048b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == target->bus)
16058b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
16068b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == target)
16078b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
16088b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
16098b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
16108b8a9b1dSJustin T. Gibbs 	     target->generation)) {
16118b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
16128b8a9b1dSJustin T. Gibbs 		return(0);
16138b8a9b1dSJustin T. Gibbs 	}
16148b8a9b1dSJustin T. Gibbs 
16158b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
16168b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == target->bus)
16178b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
16188b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == target)
16198b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
16208b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device != NULL))
16218b8a9b1dSJustin T. Gibbs 		return(xptdevicetraverse(target,
16228b8a9b1dSJustin T. Gibbs 					(struct cam_ed *)cdm->pos.cookie.device,
16238b8a9b1dSJustin T. Gibbs 					 xptedtdevicefunc, arg));
16248b8a9b1dSJustin T. Gibbs 	else
16258b8a9b1dSJustin T. Gibbs 		return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
16268b8a9b1dSJustin T. Gibbs }
16278b8a9b1dSJustin T. Gibbs 
16288b8a9b1dSJustin T. Gibbs static int
16298b8a9b1dSJustin T. Gibbs xptedtdevicefunc(struct cam_ed *device, void *arg)
16308b8a9b1dSJustin T. Gibbs {
16318b8a9b1dSJustin T. Gibbs 
16328b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
16338b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
16348b8a9b1dSJustin T. Gibbs 
16358b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
16368b8a9b1dSJustin T. Gibbs 
16378b8a9b1dSJustin T. Gibbs 	/*
16388b8a9b1dSJustin T. Gibbs 	 * If our position is for something deeper in the tree, that means
16398b8a9b1dSJustin T. Gibbs 	 * that we've already seen this node.  So, we keep going down.
16408b8a9b1dSJustin T. Gibbs 	 */
16418b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
16428b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device == device)
16438b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
16448b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.periph != NULL))
16458b8a9b1dSJustin T. Gibbs 		retval = DM_RET_DESCEND;
16468b8a9b1dSJustin T. Gibbs 	else
16478b8a9b1dSJustin T. Gibbs 		retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
16488b8a9b1dSJustin T. Gibbs 					device);
16498b8a9b1dSJustin T. Gibbs 
16508b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
16518b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
16528b8a9b1dSJustin T. Gibbs 		return(0);
16538b8a9b1dSJustin T. Gibbs 	}
16548b8a9b1dSJustin T. Gibbs 
16558b8a9b1dSJustin T. Gibbs 	/*
16568b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this device out.
16578b8a9b1dSJustin T. Gibbs 	 */
16588b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
16598b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
16608b8a9b1dSJustin T. Gibbs 
16618b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
16628b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
16638b8a9b1dSJustin T. Gibbs 
16648b8a9b1dSJustin T. Gibbs 		/*
16658b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
16668b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
16678b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
16688b8a9b1dSJustin T. Gibbs 		 */
16698b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
16708b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
16718b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
16728b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
16738b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
16748b8a9b1dSJustin T. Gibbs 
16758b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = device->target->bus;
16768b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
16772b83592fSScott Long 				xsoftc.bus_generation;
16788b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.target = device->target;
16798b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_TARGET_GENERATION] =
16808b8a9b1dSJustin T. Gibbs 				device->target->bus->generation;
16818b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.device = device;
16828b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_DEV_GENERATION] =
16838b8a9b1dSJustin T. Gibbs 				device->target->generation;
16848b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
16858b8a9b1dSJustin T. Gibbs 			return(0);
16868b8a9b1dSJustin T. Gibbs 		}
16878b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
16888b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
16898b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_DEVICE;
16908b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.path_id =
16918b8a9b1dSJustin T. Gibbs 			device->target->bus->path_id;
16928b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.target_id =
16938b8a9b1dSJustin T. Gibbs 			device->target->target_id;
16948b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.target_lun =
16958b8a9b1dSJustin T. Gibbs 			device->lun_id;
169652c9ce25SScott Long 		cdm->matches[j].result.device_result.protocol =
169752c9ce25SScott Long 			device->protocol;
16988b8a9b1dSJustin T. Gibbs 		bcopy(&device->inq_data,
16998b8a9b1dSJustin T. Gibbs 		      &cdm->matches[j].result.device_result.inq_data,
17008b8a9b1dSJustin T. Gibbs 		      sizeof(struct scsi_inquiry_data));
170152c9ce25SScott Long 		bcopy(&device->ident_data,
170252c9ce25SScott Long 		      &cdm->matches[j].result.device_result.ident_data,
170352c9ce25SScott Long 		      sizeof(struct ata_params));
17049deea857SKenneth D. Merry 
17059deea857SKenneth D. Merry 		/* Let the user know whether this device is unconfigured */
17069deea857SKenneth D. Merry 		if (device->flags & CAM_DEV_UNCONFIGURED)
17079deea857SKenneth D. Merry 			cdm->matches[j].result.device_result.flags =
17089deea857SKenneth D. Merry 				DEV_RESULT_UNCONFIGURED;
17099deea857SKenneth D. Merry 		else
17109deea857SKenneth D. Merry 			cdm->matches[j].result.device_result.flags =
17119deea857SKenneth D. Merry 				DEV_RESULT_NOFLAG;
17128b8a9b1dSJustin T. Gibbs 	}
17138b8a9b1dSJustin T. Gibbs 
17148b8a9b1dSJustin T. Gibbs 	/*
17158b8a9b1dSJustin T. Gibbs 	 * If the user isn't interested in peripherals, don't descend
17168b8a9b1dSJustin T. Gibbs 	 * the tree any further.
17178b8a9b1dSJustin T. Gibbs 	 */
17188b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
17198b8a9b1dSJustin T. Gibbs 		return(1);
17208b8a9b1dSJustin T. Gibbs 
17218b8a9b1dSJustin T. Gibbs 	/*
17228b8a9b1dSJustin T. Gibbs 	 * If there is a peripheral list generation recorded, make sure
17238b8a9b1dSJustin T. Gibbs 	 * it hasn't changed.
17248b8a9b1dSJustin T. Gibbs 	 */
17258b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
17268b8a9b1dSJustin T. Gibbs 	 && (device->target->bus == cdm->pos.cookie.bus)
17278b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
17288b8a9b1dSJustin T. Gibbs 	 && (device->target == cdm->pos.cookie.target)
17298b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
17308b8a9b1dSJustin T. Gibbs 	 && (device == cdm->pos.cookie.device)
17318b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
17328b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
17338b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
17348b8a9b1dSJustin T. Gibbs 	     device->generation)){
17358b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
17368b8a9b1dSJustin T. Gibbs 		return(0);
17378b8a9b1dSJustin T. Gibbs 	}
17388b8a9b1dSJustin T. Gibbs 
17398b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
17408b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == device->target->bus)
17418b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
17428b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == device->target)
17438b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
17448b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device == device)
17458b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
17468b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.periph != NULL))
17478b8a9b1dSJustin T. Gibbs 		return(xptperiphtraverse(device,
17488b8a9b1dSJustin T. Gibbs 				(struct cam_periph *)cdm->pos.cookie.periph,
17498b8a9b1dSJustin T. Gibbs 				xptedtperiphfunc, arg));
17508b8a9b1dSJustin T. Gibbs 	else
17518b8a9b1dSJustin T. Gibbs 		return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
17528b8a9b1dSJustin T. Gibbs }
17538b8a9b1dSJustin T. Gibbs 
17548b8a9b1dSJustin T. Gibbs static int
17558b8a9b1dSJustin T. Gibbs xptedtperiphfunc(struct cam_periph *periph, void *arg)
17568b8a9b1dSJustin T. Gibbs {
17578b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
17588b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
17598b8a9b1dSJustin T. Gibbs 
17608b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
17618b8a9b1dSJustin T. Gibbs 
17628b8a9b1dSJustin T. Gibbs 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
17638b8a9b1dSJustin T. Gibbs 
17648b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
17658b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
17668b8a9b1dSJustin T. Gibbs 		return(0);
17678b8a9b1dSJustin T. Gibbs 	}
17688b8a9b1dSJustin T. Gibbs 
17698b8a9b1dSJustin T. Gibbs 	/*
17708b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this peripheral out.
17718b8a9b1dSJustin T. Gibbs 	 */
17728b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
17738b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
17748b8a9b1dSJustin T. Gibbs 
17758b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
17768b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
17778b8a9b1dSJustin T. Gibbs 
17788b8a9b1dSJustin T. Gibbs 		/*
17798b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
17808b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
17818b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
17828b8a9b1dSJustin T. Gibbs 		 */
17838b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
17848b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
17858b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
17868b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
17878b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
17888b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PERIPH;
17898b8a9b1dSJustin T. Gibbs 
17908b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = periph->path->bus;
17918b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
17922b83592fSScott Long 				xsoftc.bus_generation;
17938b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.target = periph->path->target;
17948b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_TARGET_GENERATION] =
17958b8a9b1dSJustin T. Gibbs 				periph->path->bus->generation;
17968b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.device = periph->path->device;
17978b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_DEV_GENERATION] =
17988b8a9b1dSJustin T. Gibbs 				periph->path->target->generation;
17998b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.periph = periph;
18008b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
18018b8a9b1dSJustin T. Gibbs 				periph->path->device->generation;
18028b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
18038b8a9b1dSJustin T. Gibbs 			return(0);
18048b8a9b1dSJustin T. Gibbs 		}
18058b8a9b1dSJustin T. Gibbs 
18068b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
18078b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
18088b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_PERIPH;
18098b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.path_id =
18108b8a9b1dSJustin T. Gibbs 			periph->path->bus->path_id;
18118b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.target_id =
18128b8a9b1dSJustin T. Gibbs 			periph->path->target->target_id;
18138b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.target_lun =
18148b8a9b1dSJustin T. Gibbs 			periph->path->device->lun_id;
18158b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.unit_number =
18168b8a9b1dSJustin T. Gibbs 			periph->unit_number;
18178b8a9b1dSJustin T. Gibbs 		strncpy(cdm->matches[j].result.periph_result.periph_name,
18188b8a9b1dSJustin T. Gibbs 			periph->periph_name, DEV_IDLEN);
18198b8a9b1dSJustin T. Gibbs 	}
18208b8a9b1dSJustin T. Gibbs 
18218b8a9b1dSJustin T. Gibbs 	return(1);
18228b8a9b1dSJustin T. Gibbs }
18238b8a9b1dSJustin T. Gibbs 
18248b8a9b1dSJustin T. Gibbs static int
18258b8a9b1dSJustin T. Gibbs xptedtmatch(struct ccb_dev_match *cdm)
18268b8a9b1dSJustin T. Gibbs {
18278b8a9b1dSJustin T. Gibbs 	int ret;
18288b8a9b1dSJustin T. Gibbs 
18298b8a9b1dSJustin T. Gibbs 	cdm->num_matches = 0;
18308b8a9b1dSJustin T. Gibbs 
18318b8a9b1dSJustin T. Gibbs 	/*
18328b8a9b1dSJustin T. Gibbs 	 * Check the bus list generation.  If it has changed, the user
18338b8a9b1dSJustin T. Gibbs 	 * needs to reset everything and start over.
18348b8a9b1dSJustin T. Gibbs 	 */
18358b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
18368b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
18372b83592fSScott Long 	 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) {
18388b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
18398b8a9b1dSJustin T. Gibbs 		return(0);
18408b8a9b1dSJustin T. Gibbs 	}
18418b8a9b1dSJustin T. Gibbs 
18428b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
18438b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus != NULL))
18448b8a9b1dSJustin T. Gibbs 		ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
18458b8a9b1dSJustin T. Gibbs 				     xptedtbusfunc, cdm);
18468b8a9b1dSJustin T. Gibbs 	else
18478b8a9b1dSJustin T. Gibbs 		ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
18488b8a9b1dSJustin T. Gibbs 
18498b8a9b1dSJustin T. Gibbs 	/*
18508b8a9b1dSJustin T. Gibbs 	 * If we get back 0, that means that we had to stop before fully
18518b8a9b1dSJustin T. Gibbs 	 * traversing the EDT.  It also means that one of the subroutines
18528b8a9b1dSJustin T. Gibbs 	 * has set the status field to the proper value.  If we get back 1,
18538b8a9b1dSJustin T. Gibbs 	 * we've fully traversed the EDT and copied out any matching entries.
18548b8a9b1dSJustin T. Gibbs 	 */
18558b8a9b1dSJustin T. Gibbs 	if (ret == 1)
18568b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LAST;
18578b8a9b1dSJustin T. Gibbs 
18588b8a9b1dSJustin T. Gibbs 	return(ret);
18598b8a9b1dSJustin T. Gibbs }
18608b8a9b1dSJustin T. Gibbs 
18618b8a9b1dSJustin T. Gibbs static int
18628b8a9b1dSJustin T. Gibbs xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
18638b8a9b1dSJustin T. Gibbs {
18648b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
18658b8a9b1dSJustin T. Gibbs 
18668b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
18678b8a9b1dSJustin T. Gibbs 
18688b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
18698b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv == pdrv)
18708b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
18718b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
18728b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
18738b8a9b1dSJustin T. Gibbs 	     (*pdrv)->generation)) {
18748b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
18758b8a9b1dSJustin T. Gibbs 		return(0);
18768b8a9b1dSJustin T. Gibbs 	}
18778b8a9b1dSJustin T. Gibbs 
18788b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
18798b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv == pdrv)
18808b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
18818b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.periph != NULL))
18828b8a9b1dSJustin T. Gibbs 		return(xptpdperiphtraverse(pdrv,
18838b8a9b1dSJustin T. Gibbs 				(struct cam_periph *)cdm->pos.cookie.periph,
18848b8a9b1dSJustin T. Gibbs 				xptplistperiphfunc, arg));
18858b8a9b1dSJustin T. Gibbs 	else
18868b8a9b1dSJustin T. Gibbs 		return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
18878b8a9b1dSJustin T. Gibbs }
18888b8a9b1dSJustin T. Gibbs 
18898b8a9b1dSJustin T. Gibbs static int
18908b8a9b1dSJustin T. Gibbs xptplistperiphfunc(struct cam_periph *periph, void *arg)
18918b8a9b1dSJustin T. Gibbs {
18928b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
18938b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
18948b8a9b1dSJustin T. Gibbs 
18958b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
18968b8a9b1dSJustin T. Gibbs 
18978b8a9b1dSJustin T. Gibbs 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
18988b8a9b1dSJustin T. Gibbs 
18998b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
19008b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
19018b8a9b1dSJustin T. Gibbs 		return(0);
19028b8a9b1dSJustin T. Gibbs 	}
19038b8a9b1dSJustin T. Gibbs 
19048b8a9b1dSJustin T. Gibbs 	/*
19058b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this peripheral out.
19068b8a9b1dSJustin T. Gibbs 	 */
19078b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
19088b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
19098b8a9b1dSJustin T. Gibbs 
19108b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
19118b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
19128b8a9b1dSJustin T. Gibbs 
19138b8a9b1dSJustin T. Gibbs 		/*
19148b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
19158b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
19168b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
19178b8a9b1dSJustin T. Gibbs 		 */
19188b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
19198b8a9b1dSJustin T. Gibbs 			struct periph_driver **pdrv;
19208b8a9b1dSJustin T. Gibbs 
19218b8a9b1dSJustin T. Gibbs 			pdrv = NULL;
19228b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
19238b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
19248b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
19258b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PERIPH;
19268b8a9b1dSJustin T. Gibbs 
19278b8a9b1dSJustin T. Gibbs 			/*
19288b8a9b1dSJustin T. Gibbs 			 * This may look a bit non-sensical, but it is
19298b8a9b1dSJustin T. Gibbs 			 * actually quite logical.  There are very few
19308b8a9b1dSJustin T. Gibbs 			 * peripheral drivers, and bloating every peripheral
19318b8a9b1dSJustin T. Gibbs 			 * structure with a pointer back to its parent
19328b8a9b1dSJustin T. Gibbs 			 * peripheral driver linker set entry would cost
19338b8a9b1dSJustin T. Gibbs 			 * more in the long run than doing this quick lookup.
19348b8a9b1dSJustin T. Gibbs 			 */
19350b7c27b9SPeter Wemm 			for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
19368b8a9b1dSJustin T. Gibbs 				if (strcmp((*pdrv)->driver_name,
19378b8a9b1dSJustin T. Gibbs 				    periph->periph_name) == 0)
19388b8a9b1dSJustin T. Gibbs 					break;
19398b8a9b1dSJustin T. Gibbs 			}
19408b8a9b1dSJustin T. Gibbs 
194101910babSScott Long 			if (*pdrv == NULL) {
19428b8a9b1dSJustin T. Gibbs 				cdm->status = CAM_DEV_MATCH_ERROR;
19438b8a9b1dSJustin T. Gibbs 				return(0);
19448b8a9b1dSJustin T. Gibbs 			}
19458b8a9b1dSJustin T. Gibbs 
19468b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.pdrv = pdrv;
19478b8a9b1dSJustin T. Gibbs 			/*
19488b8a9b1dSJustin T. Gibbs 			 * The periph generation slot does double duty, as
19498b8a9b1dSJustin T. Gibbs 			 * does the periph pointer slot.  They are used for
19508b8a9b1dSJustin T. Gibbs 			 * both edt and pdrv lookups and positioning.
19518b8a9b1dSJustin T. Gibbs 			 */
19528b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.periph = periph;
19538b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
19548b8a9b1dSJustin T. Gibbs 				(*pdrv)->generation;
19558b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
19568b8a9b1dSJustin T. Gibbs 			return(0);
19578b8a9b1dSJustin T. Gibbs 		}
19588b8a9b1dSJustin T. Gibbs 
19598b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
19608b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
19618b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_PERIPH;
19628b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.path_id =
19638b8a9b1dSJustin T. Gibbs 			periph->path->bus->path_id;
19648b8a9b1dSJustin T. Gibbs 
19658b8a9b1dSJustin T. Gibbs 		/*
19668b8a9b1dSJustin T. Gibbs 		 * The transport layer peripheral doesn't have a target or
19678b8a9b1dSJustin T. Gibbs 		 * lun.
19688b8a9b1dSJustin T. Gibbs 		 */
19698b8a9b1dSJustin T. Gibbs 		if (periph->path->target)
19708b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_id =
19718b8a9b1dSJustin T. Gibbs 				periph->path->target->target_id;
19728b8a9b1dSJustin T. Gibbs 		else
19738b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_id = -1;
19748b8a9b1dSJustin T. Gibbs 
19758b8a9b1dSJustin T. Gibbs 		if (periph->path->device)
19768b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_lun =
19778b8a9b1dSJustin T. Gibbs 				periph->path->device->lun_id;
19788b8a9b1dSJustin T. Gibbs 		else
19798b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_lun = -1;
19808b8a9b1dSJustin T. Gibbs 
19818b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.unit_number =
19828b8a9b1dSJustin T. Gibbs 			periph->unit_number;
19838b8a9b1dSJustin T. Gibbs 		strncpy(cdm->matches[j].result.periph_result.periph_name,
19848b8a9b1dSJustin T. Gibbs 			periph->periph_name, DEV_IDLEN);
19858b8a9b1dSJustin T. Gibbs 	}
19868b8a9b1dSJustin T. Gibbs 
19878b8a9b1dSJustin T. Gibbs 	return(1);
19888b8a9b1dSJustin T. Gibbs }
19898b8a9b1dSJustin T. Gibbs 
19908b8a9b1dSJustin T. Gibbs static int
19918b8a9b1dSJustin T. Gibbs xptperiphlistmatch(struct ccb_dev_match *cdm)
19928b8a9b1dSJustin T. Gibbs {
19938b8a9b1dSJustin T. Gibbs 	int ret;
19948b8a9b1dSJustin T. Gibbs 
19958b8a9b1dSJustin T. Gibbs 	cdm->num_matches = 0;
19968b8a9b1dSJustin T. Gibbs 
19978b8a9b1dSJustin T. Gibbs 	/*
19988b8a9b1dSJustin T. Gibbs 	 * At this point in the edt traversal function, we check the bus
19998b8a9b1dSJustin T. Gibbs 	 * list generation to make sure that no busses have been added or
20008b8a9b1dSJustin T. Gibbs 	 * removed since the user last sent a XPT_DEV_MATCH ccb through.
20018b8a9b1dSJustin T. Gibbs 	 * For the peripheral driver list traversal function, however, we
20028b8a9b1dSJustin T. Gibbs 	 * don't have to worry about new peripheral driver types coming or
20038b8a9b1dSJustin T. Gibbs 	 * going; they're in a linker set, and therefore can't change
20048b8a9b1dSJustin T. Gibbs 	 * without a recompile.
20058b8a9b1dSJustin T. Gibbs 	 */
20068b8a9b1dSJustin T. Gibbs 
20078b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
20088b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv != NULL))
20098b8a9b1dSJustin T. Gibbs 		ret = xptpdrvtraverse(
20108b8a9b1dSJustin T. Gibbs 				(struct periph_driver **)cdm->pos.cookie.pdrv,
20118b8a9b1dSJustin T. Gibbs 				xptplistpdrvfunc, cdm);
20128b8a9b1dSJustin T. Gibbs 	else
20138b8a9b1dSJustin T. Gibbs 		ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
20148b8a9b1dSJustin T. Gibbs 
20158b8a9b1dSJustin T. Gibbs 	/*
20168b8a9b1dSJustin T. Gibbs 	 * If we get back 0, that means that we had to stop before fully
20178b8a9b1dSJustin T. Gibbs 	 * traversing the peripheral driver tree.  It also means that one of
20188b8a9b1dSJustin T. Gibbs 	 * the subroutines has set the status field to the proper value.  If
20198b8a9b1dSJustin T. Gibbs 	 * we get back 1, we've fully traversed the EDT and copied out any
20208b8a9b1dSJustin T. Gibbs 	 * matching entries.
20218b8a9b1dSJustin T. Gibbs 	 */
20228b8a9b1dSJustin T. Gibbs 	if (ret == 1)
20238b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LAST;
20248b8a9b1dSJustin T. Gibbs 
20258b8a9b1dSJustin T. Gibbs 	return(ret);
20268b8a9b1dSJustin T. Gibbs }
20278b8a9b1dSJustin T. Gibbs 
20288b8a9b1dSJustin T. Gibbs static int
20298b8a9b1dSJustin T. Gibbs xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
20308b8a9b1dSJustin T. Gibbs {
20318b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus, *next_bus;
20328b8a9b1dSJustin T. Gibbs 	int retval;
20338b8a9b1dSJustin T. Gibbs 
20348b8a9b1dSJustin T. Gibbs 	retval = 1;
20358b8a9b1dSJustin T. Gibbs 
20369a7c2696SAlexander Motin 	xpt_lock_buses();
20372b83592fSScott Long 	for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
20388b8a9b1dSJustin T. Gibbs 	     bus != NULL;
20398b8a9b1dSJustin T. Gibbs 	     bus = next_bus) {
20408b8a9b1dSJustin T. Gibbs 
20418900f4b8SKenneth D. Merry 		bus->refcount++;
20428900f4b8SKenneth D. Merry 
20438900f4b8SKenneth D. Merry 		/*
20448900f4b8SKenneth D. Merry 		 * XXX The locking here is obviously very complex.  We
20458900f4b8SKenneth D. Merry 		 * should work to simplify it.
20468900f4b8SKenneth D. Merry 		 */
20479a7c2696SAlexander Motin 		xpt_unlock_buses();
204811e4faceSScott Long 		CAM_SIM_LOCK(bus->sim);
20498b8a9b1dSJustin T. Gibbs 		retval = tr_func(bus, arg);
205011e4faceSScott Long 		CAM_SIM_UNLOCK(bus->sim);
20518900f4b8SKenneth D. Merry 
20529a7c2696SAlexander Motin 		xpt_lock_buses();
20538900f4b8SKenneth D. Merry 		next_bus = TAILQ_NEXT(bus, links);
20549a7c2696SAlexander Motin 		xpt_unlock_buses();
20558900f4b8SKenneth D. Merry 
20568900f4b8SKenneth D. Merry 		xpt_release_bus(bus);
20578900f4b8SKenneth D. Merry 
20588b8a9b1dSJustin T. Gibbs 		if (retval == 0)
20598b8a9b1dSJustin T. Gibbs 			return(retval);
20609a7c2696SAlexander Motin 		xpt_lock_buses();
20618b8a9b1dSJustin T. Gibbs 	}
20629a7c2696SAlexander Motin 	xpt_unlock_buses();
20638b8a9b1dSJustin T. Gibbs 
20648b8a9b1dSJustin T. Gibbs 	return(retval);
20658b8a9b1dSJustin T. Gibbs }
20668b8a9b1dSJustin T. Gibbs 
20678b8a9b1dSJustin T. Gibbs static int
20688b8a9b1dSJustin T. Gibbs xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
20698b8a9b1dSJustin T. Gibbs 		  xpt_targetfunc_t *tr_func, void *arg)
20708b8a9b1dSJustin T. Gibbs {
20718b8a9b1dSJustin T. Gibbs 	struct cam_et *target, *next_target;
20728b8a9b1dSJustin T. Gibbs 	int retval;
20738b8a9b1dSJustin T. Gibbs 
2074dcdf6e74SAlexander Motin 	mtx_assert(bus->sim->mtx, MA_OWNED);
20758b8a9b1dSJustin T. Gibbs 	retval = 1;
20768b8a9b1dSJustin T. Gibbs 	for (target = (start_target ? start_target :
20778b8a9b1dSJustin T. Gibbs 		       TAILQ_FIRST(&bus->et_entries));
20788b8a9b1dSJustin T. Gibbs 	     target != NULL; target = next_target) {
20798b8a9b1dSJustin T. Gibbs 
20808900f4b8SKenneth D. Merry 		target->refcount++;
20818b8a9b1dSJustin T. Gibbs 
20828b8a9b1dSJustin T. Gibbs 		retval = tr_func(target, arg);
20838b8a9b1dSJustin T. Gibbs 
20848900f4b8SKenneth D. Merry 		next_target = TAILQ_NEXT(target, links);
20858900f4b8SKenneth D. Merry 
20868900f4b8SKenneth D. Merry 		xpt_release_target(target);
20878900f4b8SKenneth D. Merry 
20888b8a9b1dSJustin T. Gibbs 		if (retval == 0)
20898b8a9b1dSJustin T. Gibbs 			return(retval);
20908b8a9b1dSJustin T. Gibbs 	}
20918b8a9b1dSJustin T. Gibbs 
20928b8a9b1dSJustin T. Gibbs 	return(retval);
20938b8a9b1dSJustin T. Gibbs }
20948b8a9b1dSJustin T. Gibbs 
20958b8a9b1dSJustin T. Gibbs static int
20968b8a9b1dSJustin T. Gibbs xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
20978b8a9b1dSJustin T. Gibbs 		  xpt_devicefunc_t *tr_func, void *arg)
20988b8a9b1dSJustin T. Gibbs {
20998b8a9b1dSJustin T. Gibbs 	struct cam_ed *device, *next_device;
21008b8a9b1dSJustin T. Gibbs 	int retval;
21018b8a9b1dSJustin T. Gibbs 
2102dcdf6e74SAlexander Motin 	mtx_assert(target->bus->sim->mtx, MA_OWNED);
21038b8a9b1dSJustin T. Gibbs 	retval = 1;
21048b8a9b1dSJustin T. Gibbs 	for (device = (start_device ? start_device :
21058b8a9b1dSJustin T. Gibbs 		       TAILQ_FIRST(&target->ed_entries));
21068b8a9b1dSJustin T. Gibbs 	     device != NULL;
21078b8a9b1dSJustin T. Gibbs 	     device = next_device) {
21088b8a9b1dSJustin T. Gibbs 
21098900f4b8SKenneth D. Merry 		/*
21108900f4b8SKenneth D. Merry 		 * Hold a reference so the current device does not go away
21118900f4b8SKenneth D. Merry 		 * on us.
21128900f4b8SKenneth D. Merry 		 */
21138900f4b8SKenneth D. Merry 		device->refcount++;
21148b8a9b1dSJustin T. Gibbs 
21158b8a9b1dSJustin T. Gibbs 		retval = tr_func(device, arg);
21168b8a9b1dSJustin T. Gibbs 
21178900f4b8SKenneth D. Merry 		/*
21188900f4b8SKenneth D. Merry 		 * Grab our next pointer before we release the current
21198900f4b8SKenneth D. Merry 		 * device.
21208900f4b8SKenneth D. Merry 		 */
21218900f4b8SKenneth D. Merry 		next_device = TAILQ_NEXT(device, links);
21228900f4b8SKenneth D. Merry 
21238900f4b8SKenneth D. Merry 		xpt_release_device(device);
21248900f4b8SKenneth D. Merry 
21258b8a9b1dSJustin T. Gibbs 		if (retval == 0)
21268b8a9b1dSJustin T. Gibbs 			return(retval);
21278b8a9b1dSJustin T. Gibbs 	}
21288b8a9b1dSJustin T. Gibbs 
21298b8a9b1dSJustin T. Gibbs 	return(retval);
21308b8a9b1dSJustin T. Gibbs }
21318b8a9b1dSJustin T. Gibbs 
21328b8a9b1dSJustin T. Gibbs static int
21338b8a9b1dSJustin T. Gibbs xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
21348b8a9b1dSJustin T. Gibbs 		  xpt_periphfunc_t *tr_func, void *arg)
21358b8a9b1dSJustin T. Gibbs {
21368b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph, *next_periph;
21378b8a9b1dSJustin T. Gibbs 	int retval;
21388b8a9b1dSJustin T. Gibbs 
21398b8a9b1dSJustin T. Gibbs 	retval = 1;
21408b8a9b1dSJustin T. Gibbs 
2141dcdf6e74SAlexander Motin 	mtx_assert(device->sim->mtx, MA_OWNED);
21428900f4b8SKenneth D. Merry 	xpt_lock_buses();
21438b8a9b1dSJustin T. Gibbs 	for (periph = (start_periph ? start_periph :
21448b8a9b1dSJustin T. Gibbs 		       SLIST_FIRST(&device->periphs));
21458b8a9b1dSJustin T. Gibbs 	     periph != NULL;
21468b8a9b1dSJustin T. Gibbs 	     periph = next_periph) {
21478b8a9b1dSJustin T. Gibbs 
21488900f4b8SKenneth D. Merry 
21498900f4b8SKenneth D. Merry 		/*
21508900f4b8SKenneth D. Merry 		 * In this case, we want to show peripherals that have been
21518900f4b8SKenneth D. Merry 		 * invalidated, but not peripherals that are scheduled to
21528900f4b8SKenneth D. Merry 		 * be freed.  So instead of calling cam_periph_acquire(),
21538900f4b8SKenneth D. Merry 		 * which will fail if the periph has been invalidated, we
215433a38f74SKenneth D. Merry 		 * just check for the free flag here.  If it is in the
215533a38f74SKenneth D. Merry 		 * process of being freed, we skip to the next periph.
21568900f4b8SKenneth D. Merry 		 */
21578900f4b8SKenneth D. Merry 		if (periph->flags & CAM_PERIPH_FREE) {
21588b8a9b1dSJustin T. Gibbs 			next_periph = SLIST_NEXT(periph, periph_links);
21598900f4b8SKenneth D. Merry 			continue;
21608900f4b8SKenneth D. Merry 		}
21618900f4b8SKenneth D. Merry 
21628900f4b8SKenneth D. Merry 		/*
21638900f4b8SKenneth D. Merry 		 * Acquire a reference to this periph while we call the
21648900f4b8SKenneth D. Merry 		 * traversal function, so it can't go away.
21658900f4b8SKenneth D. Merry 		 */
21668900f4b8SKenneth D. Merry 		periph->refcount++;
21678900f4b8SKenneth D. Merry 
21688b8a9b1dSJustin T. Gibbs 		retval = tr_func(periph, arg);
21698900f4b8SKenneth D. Merry 
21708900f4b8SKenneth D. Merry 		/*
21718900f4b8SKenneth D. Merry 		 * Grab the next peripheral before we release this one, so
21728900f4b8SKenneth D. Merry 		 * our next pointer is still valid.
21738900f4b8SKenneth D. Merry 		 */
21748900f4b8SKenneth D. Merry 		next_periph = SLIST_NEXT(periph, periph_links);
21758900f4b8SKenneth D. Merry 
21768900f4b8SKenneth D. Merry 		cam_periph_release_locked_buses(periph);
21778900f4b8SKenneth D. Merry 
21788b8a9b1dSJustin T. Gibbs 		if (retval == 0)
21798900f4b8SKenneth D. Merry 			goto bailout_done;
21808b8a9b1dSJustin T. Gibbs 	}
21818b8a9b1dSJustin T. Gibbs 
21828900f4b8SKenneth D. Merry bailout_done:
21838900f4b8SKenneth D. Merry 
21848900f4b8SKenneth D. Merry 	xpt_unlock_buses();
21858900f4b8SKenneth D. Merry 
21868b8a9b1dSJustin T. Gibbs 	return(retval);
21878b8a9b1dSJustin T. Gibbs }
21888b8a9b1dSJustin T. Gibbs 
21898b8a9b1dSJustin T. Gibbs static int
21908b8a9b1dSJustin T. Gibbs xptpdrvtraverse(struct periph_driver **start_pdrv,
21918b8a9b1dSJustin T. Gibbs 		xpt_pdrvfunc_t *tr_func, void *arg)
21928b8a9b1dSJustin T. Gibbs {
21938b8a9b1dSJustin T. Gibbs 	struct periph_driver **pdrv;
21948b8a9b1dSJustin T. Gibbs 	int retval;
21958b8a9b1dSJustin T. Gibbs 
21968b8a9b1dSJustin T. Gibbs 	retval = 1;
21978b8a9b1dSJustin T. Gibbs 
21988b8a9b1dSJustin T. Gibbs 	/*
21998b8a9b1dSJustin T. Gibbs 	 * We don't traverse the peripheral driver list like we do the
22008b8a9b1dSJustin T. Gibbs 	 * other lists, because it is a linker set, and therefore cannot be
22018b8a9b1dSJustin T. Gibbs 	 * changed during runtime.  If the peripheral driver list is ever
22028b8a9b1dSJustin T. Gibbs 	 * re-done to be something other than a linker set (i.e. it can
22038b8a9b1dSJustin T. Gibbs 	 * change while the system is running), the list traversal should
22048b8a9b1dSJustin T. Gibbs 	 * be modified to work like the other traversal functions.
22058b8a9b1dSJustin T. Gibbs 	 */
22060b7c27b9SPeter Wemm 	for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
22078b8a9b1dSJustin T. Gibbs 	     *pdrv != NULL; pdrv++) {
22088b8a9b1dSJustin T. Gibbs 		retval = tr_func(pdrv, arg);
22098b8a9b1dSJustin T. Gibbs 
22108b8a9b1dSJustin T. Gibbs 		if (retval == 0)
22118b8a9b1dSJustin T. Gibbs 			return(retval);
22128b8a9b1dSJustin T. Gibbs 	}
22138b8a9b1dSJustin T. Gibbs 
22148b8a9b1dSJustin T. Gibbs 	return(retval);
22158b8a9b1dSJustin T. Gibbs }
22168b8a9b1dSJustin T. Gibbs 
22178b8a9b1dSJustin T. Gibbs static int
22188b8a9b1dSJustin T. Gibbs xptpdperiphtraverse(struct periph_driver **pdrv,
22198b8a9b1dSJustin T. Gibbs 		    struct cam_periph *start_periph,
22208b8a9b1dSJustin T. Gibbs 		    xpt_periphfunc_t *tr_func, void *arg)
22218b8a9b1dSJustin T. Gibbs {
22228b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph, *next_periph;
2223dcdf6e74SAlexander Motin 	struct cam_sim *sim;
22248b8a9b1dSJustin T. Gibbs 	int retval;
22258b8a9b1dSJustin T. Gibbs 
22268b8a9b1dSJustin T. Gibbs 	retval = 1;
22278b8a9b1dSJustin T. Gibbs 
2228f1e2546aSMatt Jacob 	xpt_lock_buses();
22298b8a9b1dSJustin T. Gibbs 	for (periph = (start_periph ? start_periph :
22308b8a9b1dSJustin T. Gibbs 	     TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
22318b8a9b1dSJustin T. Gibbs 	     periph = next_periph) {
22328b8a9b1dSJustin T. Gibbs 
22338900f4b8SKenneth D. Merry 
22348900f4b8SKenneth D. Merry 		/*
22358900f4b8SKenneth D. Merry 		 * In this case, we want to show peripherals that have been
22368900f4b8SKenneth D. Merry 		 * invalidated, but not peripherals that are scheduled to
22378900f4b8SKenneth D. Merry 		 * be freed.  So instead of calling cam_periph_acquire(),
22388900f4b8SKenneth D. Merry 		 * which will fail if the periph has been invalidated, we
22398900f4b8SKenneth D. Merry 		 * just check for the free flag here.  If it is free, we
22408900f4b8SKenneth D. Merry 		 * skip to the next periph.
22418900f4b8SKenneth D. Merry 		 */
22428900f4b8SKenneth D. Merry 		if (periph->flags & CAM_PERIPH_FREE) {
22438900f4b8SKenneth D. Merry 			next_periph = TAILQ_NEXT(periph, unit_links);
22448900f4b8SKenneth D. Merry 			continue;
22458900f4b8SKenneth D. Merry 		}
22468900f4b8SKenneth D. Merry 
22478900f4b8SKenneth D. Merry 		/*
22488900f4b8SKenneth D. Merry 		 * Acquire a reference to this periph while we call the
22498900f4b8SKenneth D. Merry 		 * traversal function, so it can't go away.
22508900f4b8SKenneth D. Merry 		 */
22518900f4b8SKenneth D. Merry 		periph->refcount++;
2252dcdf6e74SAlexander Motin 		sim = periph->sim;
2253dcdf6e74SAlexander Motin 		xpt_unlock_buses();
2254dcdf6e74SAlexander Motin 		CAM_SIM_LOCK(sim);
2255dcdf6e74SAlexander Motin 		xpt_lock_buses();
22568900f4b8SKenneth D. Merry 		retval = tr_func(periph, arg);
22578900f4b8SKenneth D. Merry 
22588900f4b8SKenneth D. Merry 		/*
22598900f4b8SKenneth D. Merry 		 * Grab the next peripheral before we release this one, so
22608900f4b8SKenneth D. Merry 		 * our next pointer is still valid.
22618900f4b8SKenneth D. Merry 		 */
22628b8a9b1dSJustin T. Gibbs 		next_periph = TAILQ_NEXT(periph, unit_links);
22638b8a9b1dSJustin T. Gibbs 
22648900f4b8SKenneth D. Merry 		cam_periph_release_locked_buses(periph);
2265dcdf6e74SAlexander Motin 		CAM_SIM_UNLOCK(sim);
22668900f4b8SKenneth D. Merry 
22678900f4b8SKenneth D. Merry 		if (retval == 0)
22688900f4b8SKenneth D. Merry 			goto bailout_done;
22698b8a9b1dSJustin T. Gibbs 	}
22708900f4b8SKenneth D. Merry bailout_done:
22718900f4b8SKenneth D. Merry 
2272f1e2546aSMatt Jacob 	xpt_unlock_buses();
22738900f4b8SKenneth D. Merry 
22748b8a9b1dSJustin T. Gibbs 	return(retval);
22758b8a9b1dSJustin T. Gibbs }
22768b8a9b1dSJustin T. Gibbs 
22778b8a9b1dSJustin T. Gibbs static int
22788b8a9b1dSJustin T. Gibbs xptdefbusfunc(struct cam_eb *bus, void *arg)
22798b8a9b1dSJustin T. Gibbs {
22808b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
22818b8a9b1dSJustin T. Gibbs 
22828b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
22838b8a9b1dSJustin T. Gibbs 
22848b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_BUS) {
22858b8a9b1dSJustin T. Gibbs 		xpt_busfunc_t *tr_func;
22868b8a9b1dSJustin T. Gibbs 
22878b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_busfunc_t *)tr_config->tr_func;
22888b8a9b1dSJustin T. Gibbs 
22898b8a9b1dSJustin T. Gibbs 		return(tr_func(bus, tr_config->tr_arg));
22908b8a9b1dSJustin T. Gibbs 	} else
22918b8a9b1dSJustin T. Gibbs 		return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
22928b8a9b1dSJustin T. Gibbs }
22938b8a9b1dSJustin T. Gibbs 
22948b8a9b1dSJustin T. Gibbs static int
22958b8a9b1dSJustin T. Gibbs xptdeftargetfunc(struct cam_et *target, void *arg)
22968b8a9b1dSJustin T. Gibbs {
22978b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
22988b8a9b1dSJustin T. Gibbs 
22998b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
23008b8a9b1dSJustin T. Gibbs 
23018b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_TARGET) {
23028b8a9b1dSJustin T. Gibbs 		xpt_targetfunc_t *tr_func;
23038b8a9b1dSJustin T. Gibbs 
23048b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
23058b8a9b1dSJustin T. Gibbs 
23068b8a9b1dSJustin T. Gibbs 		return(tr_func(target, tr_config->tr_arg));
23078b8a9b1dSJustin T. Gibbs 	} else
23088b8a9b1dSJustin T. Gibbs 		return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
23098b8a9b1dSJustin T. Gibbs }
23108b8a9b1dSJustin T. Gibbs 
23118b8a9b1dSJustin T. Gibbs static int
23128b8a9b1dSJustin T. Gibbs xptdefdevicefunc(struct cam_ed *device, void *arg)
23138b8a9b1dSJustin T. Gibbs {
23148b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
23158b8a9b1dSJustin T. Gibbs 
23168b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
23178b8a9b1dSJustin T. Gibbs 
23188b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_DEVICE) {
23198b8a9b1dSJustin T. Gibbs 		xpt_devicefunc_t *tr_func;
23208b8a9b1dSJustin T. Gibbs 
23218b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
23228b8a9b1dSJustin T. Gibbs 
23238b8a9b1dSJustin T. Gibbs 		return(tr_func(device, tr_config->tr_arg));
23248b8a9b1dSJustin T. Gibbs 	} else
23258b8a9b1dSJustin T. Gibbs 		return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
23268b8a9b1dSJustin T. Gibbs }
23278b8a9b1dSJustin T. Gibbs 
23288b8a9b1dSJustin T. Gibbs static int
23298b8a9b1dSJustin T. Gibbs xptdefperiphfunc(struct cam_periph *periph, void *arg)
23308b8a9b1dSJustin T. Gibbs {
23318b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
23328b8a9b1dSJustin T. Gibbs 	xpt_periphfunc_t *tr_func;
23338b8a9b1dSJustin T. Gibbs 
23348b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
23358b8a9b1dSJustin T. Gibbs 
23368b8a9b1dSJustin T. Gibbs 	tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
23378b8a9b1dSJustin T. Gibbs 
23388b8a9b1dSJustin T. Gibbs 	/*
23398b8a9b1dSJustin T. Gibbs 	 * Unlike the other default functions, we don't check for depth
23408b8a9b1dSJustin T. Gibbs 	 * here.  The peripheral driver level is the last level in the EDT,
23418b8a9b1dSJustin T. Gibbs 	 * so if we're here, we should execute the function in question.
23428b8a9b1dSJustin T. Gibbs 	 */
23438b8a9b1dSJustin T. Gibbs 	return(tr_func(periph, tr_config->tr_arg));
23448b8a9b1dSJustin T. Gibbs }
23458b8a9b1dSJustin T. Gibbs 
23468b8a9b1dSJustin T. Gibbs /*
23478b8a9b1dSJustin T. Gibbs  * Execute the given function for every bus in the EDT.
23488b8a9b1dSJustin T. Gibbs  */
23498b8a9b1dSJustin T. Gibbs static int
23508b8a9b1dSJustin T. Gibbs xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
23518b8a9b1dSJustin T. Gibbs {
23528b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
23538b8a9b1dSJustin T. Gibbs 
23548b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_BUS;
23558b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
23568b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
23578b8a9b1dSJustin T. Gibbs 
23588b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
23598b8a9b1dSJustin T. Gibbs }
23608b8a9b1dSJustin T. Gibbs 
23618b8a9b1dSJustin T. Gibbs /*
23628b8a9b1dSJustin T. Gibbs  * Execute the given function for every device in the EDT.
23638b8a9b1dSJustin T. Gibbs  */
23648b8a9b1dSJustin T. Gibbs static int
23658b8a9b1dSJustin T. Gibbs xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
23668b8a9b1dSJustin T. Gibbs {
23678b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
23688b8a9b1dSJustin T. Gibbs 
23698b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_DEVICE;
23708b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
23718b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
23728b8a9b1dSJustin T. Gibbs 
23738b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
23748b8a9b1dSJustin T. Gibbs }
23758b8a9b1dSJustin T. Gibbs 
23768b8a9b1dSJustin T. Gibbs static int
23778b8a9b1dSJustin T. Gibbs xptsetasyncfunc(struct cam_ed *device, void *arg)
23788b8a9b1dSJustin T. Gibbs {
23798b8a9b1dSJustin T. Gibbs 	struct cam_path path;
23808b8a9b1dSJustin T. Gibbs 	struct ccb_getdev cgd;
23817685edecSAlexander Motin 	struct ccb_setasync *csa = (struct ccb_setasync *)arg;
23828b8a9b1dSJustin T. Gibbs 
2383c8bead2aSJustin T. Gibbs 	/*
2384c8bead2aSJustin T. Gibbs 	 * Don't report unconfigured devices (Wildcard devs,
2385c8bead2aSJustin T. Gibbs 	 * devices only for target mode, device instances
2386c8bead2aSJustin T. Gibbs 	 * that have been invalidated but are waiting for
2387c8bead2aSJustin T. Gibbs 	 * their last reference count to be released).
2388c8bead2aSJustin T. Gibbs 	 */
2389c8bead2aSJustin T. Gibbs 	if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2390c8bead2aSJustin T. Gibbs 		return (1);
2391c8bead2aSJustin T. Gibbs 
23928b8a9b1dSJustin T. Gibbs 	xpt_compile_path(&path,
23938b8a9b1dSJustin T. Gibbs 			 NULL,
23948b8a9b1dSJustin T. Gibbs 			 device->target->bus->path_id,
23958b8a9b1dSJustin T. Gibbs 			 device->target->target_id,
23968b8a9b1dSJustin T. Gibbs 			 device->lun_id);
2397bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
23988b8a9b1dSJustin T. Gibbs 	cgd.ccb_h.func_code = XPT_GDEV_TYPE;
23998b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&cgd);
24007685edecSAlexander Motin 	csa->callback(csa->callback_arg,
24018b8a9b1dSJustin T. Gibbs 			    AC_FOUND_DEVICE,
24028b8a9b1dSJustin T. Gibbs 			    &path, &cgd);
24038b8a9b1dSJustin T. Gibbs 	xpt_release_path(&path);
24048b8a9b1dSJustin T. Gibbs 
24058b8a9b1dSJustin T. Gibbs 	return(1);
24068b8a9b1dSJustin T. Gibbs }
2407c8bead2aSJustin T. Gibbs 
24088b8a9b1dSJustin T. Gibbs static int
24098b8a9b1dSJustin T. Gibbs xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
24108b8a9b1dSJustin T. Gibbs {
24118b8a9b1dSJustin T. Gibbs 	struct cam_path path;
24128b8a9b1dSJustin T. Gibbs 	struct ccb_pathinq cpi;
24137685edecSAlexander Motin 	struct ccb_setasync *csa = (struct ccb_setasync *)arg;
24148b8a9b1dSJustin T. Gibbs 
24158b8a9b1dSJustin T. Gibbs 	xpt_compile_path(&path, /*periph*/NULL,
24168b8a9b1dSJustin T. Gibbs 			 bus->sim->path_id,
24178b8a9b1dSJustin T. Gibbs 			 CAM_TARGET_WILDCARD,
24188b8a9b1dSJustin T. Gibbs 			 CAM_LUN_WILDCARD);
2419bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
24208b8a9b1dSJustin T. Gibbs 	cpi.ccb_h.func_code = XPT_PATH_INQ;
24218b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&cpi);
24227685edecSAlexander Motin 	csa->callback(csa->callback_arg,
24238b8a9b1dSJustin T. Gibbs 			    AC_PATH_REGISTERED,
24248b8a9b1dSJustin T. Gibbs 			    &path, &cpi);
24258b8a9b1dSJustin T. Gibbs 	xpt_release_path(&path);
24268b8a9b1dSJustin T. Gibbs 
24278b8a9b1dSJustin T. Gibbs 	return(1);
24288b8a9b1dSJustin T. Gibbs }
24298b8a9b1dSJustin T. Gibbs 
24308b8a9b1dSJustin T. Gibbs void
24318b8a9b1dSJustin T. Gibbs xpt_action(union ccb *start_ccb)
24328b8a9b1dSJustin T. Gibbs {
24339911ecf9SJustin T. Gibbs 
24348b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
24358b8a9b1dSJustin T. Gibbs 
24368b8a9b1dSJustin T. Gibbs 	start_ccb->ccb_h.status = CAM_REQ_INPROG;
243752c9ce25SScott Long 	(*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
243852c9ce25SScott Long }
243952c9ce25SScott Long 
244052c9ce25SScott Long void
244152c9ce25SScott Long xpt_action_default(union ccb *start_ccb)
244252c9ce25SScott Long {
2443da396db2SAlexander Motin 	struct cam_path *path;
244452c9ce25SScott Long 
2445da396db2SAlexander Motin 	path = start_ccb->ccb_h.path;
2446da396db2SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
244752c9ce25SScott Long 
24488b8a9b1dSJustin T. Gibbs 	switch (start_ccb->ccb_h.func_code) {
24498b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
2450d05caa00SKenneth D. Merry 	{
24513393f8daSKenneth D. Merry 		struct cam_ed *device;
2452d05caa00SKenneth D. Merry 
24538b8a9b1dSJustin T. Gibbs 		/*
24548b8a9b1dSJustin T. Gibbs 		 * For the sake of compatibility with SCSI-1
24558b8a9b1dSJustin T. Gibbs 		 * devices that may not understand the identify
24568b8a9b1dSJustin T. Gibbs 		 * message, we include lun information in the
24578b8a9b1dSJustin T. Gibbs 		 * second byte of all commands.  SCSI-1 specifies
24588b8a9b1dSJustin T. Gibbs 		 * that luns are a 3 bit value and reserves only 3
24598b8a9b1dSJustin T. Gibbs 		 * bits for lun information in the CDB.  Later
24608b8a9b1dSJustin T. Gibbs 		 * revisions of the SCSI spec allow for more than 8
24618b8a9b1dSJustin T. Gibbs 		 * luns, but have deprecated lun information in the
24628b8a9b1dSJustin T. Gibbs 		 * CDB.  So, if the lun won't fit, we must omit.
24638b8a9b1dSJustin T. Gibbs 		 *
24648b8a9b1dSJustin T. Gibbs 		 * Also be aware that during initial probing for devices,
24658b8a9b1dSJustin T. Gibbs 		 * the inquiry information is unknown but initialized to 0.
24668b8a9b1dSJustin T. Gibbs 		 * This means that this code will be exercised while probing
24678b8a9b1dSJustin T. Gibbs 		 * devices with an ANSI revision greater than 2.
24688b8a9b1dSJustin T. Gibbs 		 */
2469da396db2SAlexander Motin 		device = path->device;
24703393f8daSKenneth D. Merry 		if (device->protocol_version <= SCSI_REV_2
24718b8a9b1dSJustin T. Gibbs 		 && start_ccb->ccb_h.target_lun < 8
24728b8a9b1dSJustin T. Gibbs 		 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
24738b8a9b1dSJustin T. Gibbs 
24748b8a9b1dSJustin T. Gibbs 			start_ccb->csio.cdb_io.cdb_bytes[1] |=
24758b8a9b1dSJustin T. Gibbs 			    start_ccb->ccb_h.target_lun << 5;
24768b8a9b1dSJustin T. Gibbs 		}
24778b8a9b1dSJustin T. Gibbs 		start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2478d05caa00SKenneth D. Merry 	}
247907c6eac9SPoul-Henning Kamp 	/* FALLTHROUGH */
24808b8a9b1dSJustin T. Gibbs 	case XPT_TARGET_IO:
24818b8a9b1dSJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
24822cefde5fSJustin T. Gibbs 		start_ccb->csio.sense_resid = 0;
24832cefde5fSJustin T. Gibbs 		start_ccb->csio.resid = 0;
24842cefde5fSJustin T. Gibbs 		/* FALLTHROUGH */
248552c9ce25SScott Long 	case XPT_ATA_IO:
2486de9ebb68SAlexander Motin 		if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
248752c9ce25SScott Long 			start_ccb->ataio.resid = 0;
2488b882a6d3SMatt Jacob 		/* FALLTHROUGH */
248910e1cf63SKenneth D. Merry 	case XPT_RESET_DEV:
24908b8a9b1dSJustin T. Gibbs 	case XPT_ENG_EXEC:
249106e79492SKenneth D. Merry 	case XPT_SMP_IO:
2492cccf4220SAlexander Motin 		cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2493cccf4220SAlexander Motin 		if (xpt_schedule_devq(path->bus->sim->devq, path->device))
2494cccf4220SAlexander Motin 			xpt_run_devq(path->bus->sim->devq);
24958b8a9b1dSJustin T. Gibbs 		break;
24968b8a9b1dSJustin T. Gibbs 	case XPT_CALC_GEOMETRY:
24972cefde5fSJustin T. Gibbs 	{
24982cefde5fSJustin T. Gibbs 		struct cam_sim *sim;
24992cefde5fSJustin T. Gibbs 
25008b8a9b1dSJustin T. Gibbs 		/* Filter out garbage */
25018b8a9b1dSJustin T. Gibbs 		if (start_ccb->ccg.block_size == 0
25028b8a9b1dSJustin T. Gibbs 		 || start_ccb->ccg.volume_size == 0) {
25038b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.cylinders = 0;
25048b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.heads = 0;
25058b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.secs_per_track = 0;
25068b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
25078b8a9b1dSJustin T. Gibbs 			break;
25088b8a9b1dSJustin T. Gibbs 		}
2509abef0e67SMarius Strobl #if defined(PC98) || defined(__sparc64__)
25108b8a9b1dSJustin T. Gibbs 		/*
25118b8a9b1dSJustin T. Gibbs 		 * In a PC-98 system, geometry translation depens on
25128b8a9b1dSJustin T. Gibbs 		 * the "real" device geometry obtained from mode page 4.
25138b8a9b1dSJustin T. Gibbs 		 * SCSI geometry translation is performed in the
25148b8a9b1dSJustin T. Gibbs 		 * initialization routine of the SCSI BIOS and the result
25158b8a9b1dSJustin T. Gibbs 		 * stored in host memory.  If the translation is available
25168b8a9b1dSJustin T. Gibbs 		 * in host memory, use it.  If not, rely on the default
25178b8a9b1dSJustin T. Gibbs 		 * translation the device driver performs.
2518abef0e67SMarius Strobl 		 * For sparc64, we may need adjust the geometry of large
2519abef0e67SMarius Strobl 		 * disks in order to fit the limitations of the 16-bit
2520abef0e67SMarius Strobl 		 * fields of the VTOC8 disk label.
25218b8a9b1dSJustin T. Gibbs 		 */
25228b8a9b1dSJustin T. Gibbs 		if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
25238b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
25248b8a9b1dSJustin T. Gibbs 			break;
25258b8a9b1dSJustin T. Gibbs 		}
25268b8a9b1dSJustin T. Gibbs #endif
2527da396db2SAlexander Motin 		sim = path->bus->sim;
25282cefde5fSJustin T. Gibbs 		(*(sim->sim_action))(sim, start_ccb);
25292cefde5fSJustin T. Gibbs 		break;
25302cefde5fSJustin T. Gibbs 	}
2531bb6087e5SJustin T. Gibbs 	case XPT_ABORT:
25322cefde5fSJustin T. Gibbs 	{
25332cefde5fSJustin T. Gibbs 		union ccb* abort_ccb;
25342cefde5fSJustin T. Gibbs 
25352cefde5fSJustin T. Gibbs 		abort_ccb = start_ccb->cab.abort_ccb;
25362cefde5fSJustin T. Gibbs 		if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
25372cefde5fSJustin T. Gibbs 
25382cefde5fSJustin T. Gibbs 			if (abort_ccb->ccb_h.pinfo.index >= 0) {
25392cefde5fSJustin T. Gibbs 				struct cam_ccbq *ccbq;
254083c5d981SAlexander Motin 				struct cam_ed *device;
25412cefde5fSJustin T. Gibbs 
254283c5d981SAlexander Motin 				device = abort_ccb->ccb_h.path->device;
254383c5d981SAlexander Motin 				ccbq = &device->ccbq;
25442cefde5fSJustin T. Gibbs 				cam_ccbq_remove_ccb(ccbq, abort_ccb);
25452cefde5fSJustin T. Gibbs 				abort_ccb->ccb_h.status =
25462cefde5fSJustin T. Gibbs 				    CAM_REQ_ABORTED|CAM_DEV_QFRZN;
25472cefde5fSJustin T. Gibbs 				xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
25482cefde5fSJustin T. Gibbs 				xpt_done(abort_ccb);
25492cefde5fSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
25502cefde5fSJustin T. Gibbs 				break;
25512cefde5fSJustin T. Gibbs 			}
25522cefde5fSJustin T. Gibbs 			if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
25532cefde5fSJustin T. Gibbs 			 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
25542cefde5fSJustin T. Gibbs 				/*
25552cefde5fSJustin T. Gibbs 				 * We've caught this ccb en route to
25562cefde5fSJustin T. Gibbs 				 * the SIM.  Flag it for abort and the
25572cefde5fSJustin T. Gibbs 				 * SIM will do so just before starting
25582cefde5fSJustin T. Gibbs 				 * real work on the CCB.
25592cefde5fSJustin T. Gibbs 				 */
25602cefde5fSJustin T. Gibbs 				abort_ccb->ccb_h.status =
25612cefde5fSJustin T. Gibbs 				    CAM_REQ_ABORTED|CAM_DEV_QFRZN;
25622cefde5fSJustin T. Gibbs 				xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
25632cefde5fSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
25642cefde5fSJustin T. Gibbs 				break;
25652cefde5fSJustin T. Gibbs 			}
25662cefde5fSJustin T. Gibbs 		}
25672cefde5fSJustin T. Gibbs 		if (XPT_FC_IS_QUEUED(abort_ccb)
25682cefde5fSJustin T. Gibbs 		 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
25692cefde5fSJustin T. Gibbs 			/*
25702cefde5fSJustin T. Gibbs 			 * It's already completed but waiting
25712cefde5fSJustin T. Gibbs 			 * for our SWI to get to it.
25722cefde5fSJustin T. Gibbs 			 */
25732cefde5fSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_UA_ABORT;
25742cefde5fSJustin T. Gibbs 			break;
25752cefde5fSJustin T. Gibbs 		}
25762cefde5fSJustin T. Gibbs 		/*
25772cefde5fSJustin T. Gibbs 		 * If we weren't able to take care of the abort request
25782cefde5fSJustin T. Gibbs 		 * in the XPT, pass the request down to the SIM for processing.
25792cefde5fSJustin T. Gibbs 		 */
25802cefde5fSJustin T. Gibbs 	}
258107c6eac9SPoul-Henning Kamp 	/* FALLTHROUGH */
25828b8a9b1dSJustin T. Gibbs 	case XPT_ACCEPT_TARGET_IO:
25838b8a9b1dSJustin T. Gibbs 	case XPT_EN_LUN:
25848b8a9b1dSJustin T. Gibbs 	case XPT_IMMED_NOTIFY:
25858b8a9b1dSJustin T. Gibbs 	case XPT_NOTIFY_ACK:
25868b8a9b1dSJustin T. Gibbs 	case XPT_RESET_BUS:
25872df76c16SMatt Jacob 	case XPT_IMMEDIATE_NOTIFY:
25882df76c16SMatt Jacob 	case XPT_NOTIFY_ACKNOWLEDGE:
25892df76c16SMatt Jacob 	case XPT_GET_SIM_KNOB:
25902df76c16SMatt Jacob 	case XPT_SET_SIM_KNOB:
25918b8a9b1dSJustin T. Gibbs 	{
25928b8a9b1dSJustin T. Gibbs 		struct cam_sim *sim;
25938b8a9b1dSJustin T. Gibbs 
2594da396db2SAlexander Motin 		sim = path->bus->sim;
25958b8a9b1dSJustin T. Gibbs 		(*(sim->sim_action))(sim, start_ccb);
25968b8a9b1dSJustin T. Gibbs 		break;
25978b8a9b1dSJustin T. Gibbs 	}
259887cfaf0eSJustin T. Gibbs 	case XPT_PATH_INQ:
259987cfaf0eSJustin T. Gibbs 	{
260087cfaf0eSJustin T. Gibbs 		struct cam_sim *sim;
260187cfaf0eSJustin T. Gibbs 
2602da396db2SAlexander Motin 		sim = path->bus->sim;
260387cfaf0eSJustin T. Gibbs 		(*(sim->sim_action))(sim, start_ccb);
260487cfaf0eSJustin T. Gibbs 		break;
260587cfaf0eSJustin T. Gibbs 	}
260687cfaf0eSJustin T. Gibbs 	case XPT_PATH_STATS:
2607da396db2SAlexander Motin 		start_ccb->cpis.last_reset = path->bus->last_reset;
260887cfaf0eSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
260987cfaf0eSJustin T. Gibbs 		break;
26108b8a9b1dSJustin T. Gibbs 	case XPT_GDEV_TYPE:
2611a5479bc5SJustin T. Gibbs 	{
261287cfaf0eSJustin T. Gibbs 		struct cam_ed *dev;
2613a5479bc5SJustin T. Gibbs 
2614da396db2SAlexander Motin 		dev = path->device;
261587cfaf0eSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
26168b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
26178b8a9b1dSJustin T. Gibbs 		} else {
26188b8a9b1dSJustin T. Gibbs 			struct ccb_getdev *cgd;
26198b8a9b1dSJustin T. Gibbs 
26208b8a9b1dSJustin T. Gibbs 			cgd = &start_ccb->cgd;
262152c9ce25SScott Long 			cgd->protocol = dev->protocol;
26228b8a9b1dSJustin T. Gibbs 			cgd->inq_data = dev->inq_data;
262352c9ce25SScott Long 			cgd->ident_data = dev->ident_data;
262430a4094fSAlexander Motin 			cgd->inq_flags = dev->inq_flags;
26258b8a9b1dSJustin T. Gibbs 			cgd->ccb_h.status = CAM_REQ_CMP;
26268b8a9b1dSJustin T. Gibbs 			cgd->serial_num_len = dev->serial_num_len;
26278b8a9b1dSJustin T. Gibbs 			if ((dev->serial_num_len > 0)
26288b8a9b1dSJustin T. Gibbs 			 && (dev->serial_num != NULL))
26298b8a9b1dSJustin T. Gibbs 				bcopy(dev->serial_num, cgd->serial_num,
26308b8a9b1dSJustin T. Gibbs 				      dev->serial_num_len);
26318b8a9b1dSJustin T. Gibbs 		}
26328b8a9b1dSJustin T. Gibbs 		break;
2633a5479bc5SJustin T. Gibbs 	}
263487cfaf0eSJustin T. Gibbs 	case XPT_GDEV_STATS:
263587cfaf0eSJustin T. Gibbs 	{
263687cfaf0eSJustin T. Gibbs 		struct cam_ed *dev;
263787cfaf0eSJustin T. Gibbs 
2638da396db2SAlexander Motin 		dev = path->device;
263987cfaf0eSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
264087cfaf0eSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
264187cfaf0eSJustin T. Gibbs 		} else {
264287cfaf0eSJustin T. Gibbs 			struct ccb_getdevstats *cgds;
264387cfaf0eSJustin T. Gibbs 			struct cam_eb *bus;
264487cfaf0eSJustin T. Gibbs 			struct cam_et *tar;
264587cfaf0eSJustin T. Gibbs 
264687cfaf0eSJustin T. Gibbs 			cgds = &start_ccb->cgds;
2647da396db2SAlexander Motin 			bus = path->bus;
2648da396db2SAlexander Motin 			tar = path->target;
264987cfaf0eSJustin T. Gibbs 			cgds->dev_openings = dev->ccbq.dev_openings;
265087cfaf0eSJustin T. Gibbs 			cgds->dev_active = dev->ccbq.dev_active;
265187cfaf0eSJustin T. Gibbs 			cgds->devq_openings = dev->ccbq.devq_openings;
2652ea541bfdSAlexander Motin 			cgds->devq_queued = cam_ccbq_pending_ccb_count(&dev->ccbq);
265387cfaf0eSJustin T. Gibbs 			cgds->held = dev->ccbq.held;
265487cfaf0eSJustin T. Gibbs 			cgds->last_reset = tar->last_reset;
265552c9ce25SScott Long 			cgds->maxtags = dev->maxtags;
265652c9ce25SScott Long 			cgds->mintags = dev->mintags;
265787cfaf0eSJustin T. Gibbs 			if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
265887cfaf0eSJustin T. Gibbs 				cgds->last_reset = bus->last_reset;
265987cfaf0eSJustin T. Gibbs 			cgds->ccb_h.status = CAM_REQ_CMP;
266087cfaf0eSJustin T. Gibbs 		}
266187cfaf0eSJustin T. Gibbs 		break;
266287cfaf0eSJustin T. Gibbs 	}
26638b8a9b1dSJustin T. Gibbs 	case XPT_GDEVLIST:
26648b8a9b1dSJustin T. Gibbs 	{
26658b8a9b1dSJustin T. Gibbs 		struct cam_periph	*nperiph;
26668b8a9b1dSJustin T. Gibbs 		struct periph_list	*periph_head;
26678b8a9b1dSJustin T. Gibbs 		struct ccb_getdevlist	*cgdl;
26683393f8daSKenneth D. Merry 		u_int			i;
26698b8a9b1dSJustin T. Gibbs 		struct cam_ed		*device;
26708b8a9b1dSJustin T. Gibbs 		int			found;
26718b8a9b1dSJustin T. Gibbs 
26728b8a9b1dSJustin T. Gibbs 
26738b8a9b1dSJustin T. Gibbs 		found = 0;
26748b8a9b1dSJustin T. Gibbs 
26758b8a9b1dSJustin T. Gibbs 		/*
26768b8a9b1dSJustin T. Gibbs 		 * Don't want anyone mucking with our data.
26778b8a9b1dSJustin T. Gibbs 		 */
2678da396db2SAlexander Motin 		device = path->device;
26798b8a9b1dSJustin T. Gibbs 		periph_head = &device->periphs;
26808b8a9b1dSJustin T. Gibbs 		cgdl = &start_ccb->cgdl;
26818b8a9b1dSJustin T. Gibbs 
26828b8a9b1dSJustin T. Gibbs 		/*
26838b8a9b1dSJustin T. Gibbs 		 * Check and see if the list has changed since the user
26848b8a9b1dSJustin T. Gibbs 		 * last requested a list member.  If so, tell them that the
26858b8a9b1dSJustin T. Gibbs 		 * list has changed, and therefore they need to start over
26868b8a9b1dSJustin T. Gibbs 		 * from the beginning.
26878b8a9b1dSJustin T. Gibbs 		 */
26888b8a9b1dSJustin T. Gibbs 		if ((cgdl->index != 0) &&
26898b8a9b1dSJustin T. Gibbs 		    (cgdl->generation != device->generation)) {
26908b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
26918b8a9b1dSJustin T. Gibbs 			break;
26928b8a9b1dSJustin T. Gibbs 		}
26938b8a9b1dSJustin T. Gibbs 
26948b8a9b1dSJustin T. Gibbs 		/*
26958b8a9b1dSJustin T. Gibbs 		 * Traverse the list of peripherals and attempt to find
26968b8a9b1dSJustin T. Gibbs 		 * the requested peripheral.
26978b8a9b1dSJustin T. Gibbs 		 */
2698fc2ffbe6SPoul-Henning Kamp 		for (nperiph = SLIST_FIRST(periph_head), i = 0;
26998b8a9b1dSJustin T. Gibbs 		     (nperiph != NULL) && (i <= cgdl->index);
2700fc2ffbe6SPoul-Henning Kamp 		     nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
27018b8a9b1dSJustin T. Gibbs 			if (i == cgdl->index) {
27028b8a9b1dSJustin T. Gibbs 				strncpy(cgdl->periph_name,
27038b8a9b1dSJustin T. Gibbs 					nperiph->periph_name,
27048b8a9b1dSJustin T. Gibbs 					DEV_IDLEN);
27058b8a9b1dSJustin T. Gibbs 				cgdl->unit_number = nperiph->unit_number;
27068b8a9b1dSJustin T. Gibbs 				found = 1;
27078b8a9b1dSJustin T. Gibbs 			}
27088b8a9b1dSJustin T. Gibbs 		}
27098b8a9b1dSJustin T. Gibbs 		if (found == 0) {
27108b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_ERROR;
27118b8a9b1dSJustin T. Gibbs 			break;
27128b8a9b1dSJustin T. Gibbs 		}
27138b8a9b1dSJustin T. Gibbs 
27148b8a9b1dSJustin T. Gibbs 		if (nperiph == NULL)
27158b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
27168b8a9b1dSJustin T. Gibbs 		else
27178b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_MORE_DEVS;
27188b8a9b1dSJustin T. Gibbs 
27198b8a9b1dSJustin T. Gibbs 		cgdl->index++;
27208b8a9b1dSJustin T. Gibbs 		cgdl->generation = device->generation;
27218b8a9b1dSJustin T. Gibbs 
27228b8a9b1dSJustin T. Gibbs 		cgdl->ccb_h.status = CAM_REQ_CMP;
27238b8a9b1dSJustin T. Gibbs 		break;
27248b8a9b1dSJustin T. Gibbs 	}
27258b8a9b1dSJustin T. Gibbs 	case XPT_DEV_MATCH:
27268b8a9b1dSJustin T. Gibbs 	{
27278b8a9b1dSJustin T. Gibbs 		dev_pos_type position_type;
27288b8a9b1dSJustin T. Gibbs 		struct ccb_dev_match *cdm;
27298b8a9b1dSJustin T. Gibbs 
27308b8a9b1dSJustin T. Gibbs 		cdm = &start_ccb->cdm;
27318b8a9b1dSJustin T. Gibbs 
27328b8a9b1dSJustin T. Gibbs 		/*
27338b8a9b1dSJustin T. Gibbs 		 * There are two ways of getting at information in the EDT.
27348b8a9b1dSJustin T. Gibbs 		 * The first way is via the primary EDT tree.  It starts
27358b8a9b1dSJustin T. Gibbs 		 * with a list of busses, then a list of targets on a bus,
27368b8a9b1dSJustin T. Gibbs 		 * then devices/luns on a target, and then peripherals on a
27378b8a9b1dSJustin T. Gibbs 		 * device/lun.  The "other" way is by the peripheral driver
27388b8a9b1dSJustin T. Gibbs 		 * lists.  The peripheral driver lists are organized by
27398b8a9b1dSJustin T. Gibbs 		 * peripheral driver.  (obviously)  So it makes sense to
27408b8a9b1dSJustin T. Gibbs 		 * use the peripheral driver list if the user is looking
27418b8a9b1dSJustin T. Gibbs 		 * for something like "da1", or all "da" devices.  If the
27428b8a9b1dSJustin T. Gibbs 		 * user is looking for something on a particular bus/target
27438b8a9b1dSJustin T. Gibbs 		 * or lun, it's generally better to go through the EDT tree.
27448b8a9b1dSJustin T. Gibbs 		 */
27458b8a9b1dSJustin T. Gibbs 
27468b8a9b1dSJustin T. Gibbs 		if (cdm->pos.position_type != CAM_DEV_POS_NONE)
27478b8a9b1dSJustin T. Gibbs 			position_type = cdm->pos.position_type;
27488b8a9b1dSJustin T. Gibbs 		else {
27493393f8daSKenneth D. Merry 			u_int i;
27508b8a9b1dSJustin T. Gibbs 
27518b8a9b1dSJustin T. Gibbs 			position_type = CAM_DEV_POS_NONE;
27528b8a9b1dSJustin T. Gibbs 
27538b8a9b1dSJustin T. Gibbs 			for (i = 0; i < cdm->num_patterns; i++) {
27548b8a9b1dSJustin T. Gibbs 				if ((cdm->patterns[i].type == DEV_MATCH_BUS)
27558b8a9b1dSJustin T. Gibbs 				 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
27568b8a9b1dSJustin T. Gibbs 					position_type = CAM_DEV_POS_EDT;
27578b8a9b1dSJustin T. Gibbs 					break;
27588b8a9b1dSJustin T. Gibbs 				}
27598b8a9b1dSJustin T. Gibbs 			}
27608b8a9b1dSJustin T. Gibbs 
27618b8a9b1dSJustin T. Gibbs 			if (cdm->num_patterns == 0)
27628b8a9b1dSJustin T. Gibbs 				position_type = CAM_DEV_POS_EDT;
27638b8a9b1dSJustin T. Gibbs 			else if (position_type == CAM_DEV_POS_NONE)
27648b8a9b1dSJustin T. Gibbs 				position_type = CAM_DEV_POS_PDRV;
27658b8a9b1dSJustin T. Gibbs 		}
27668b8a9b1dSJustin T. Gibbs 
276792c40f40SAlexander Motin 		/*
276892c40f40SAlexander Motin 		 * Note that we drop the SIM lock here, because the EDT
276992c40f40SAlexander Motin 		 * traversal code needs to do its own locking.
277092c40f40SAlexander Motin 		 */
277192c40f40SAlexander Motin 		CAM_SIM_UNLOCK(xpt_path_sim(cdm->ccb_h.path));
27728b8a9b1dSJustin T. Gibbs 		switch(position_type & CAM_DEV_POS_TYPEMASK) {
27738b8a9b1dSJustin T. Gibbs 		case CAM_DEV_POS_EDT:
277407c6eac9SPoul-Henning Kamp 			xptedtmatch(cdm);
27758b8a9b1dSJustin T. Gibbs 			break;
27768b8a9b1dSJustin T. Gibbs 		case CAM_DEV_POS_PDRV:
277707c6eac9SPoul-Henning Kamp 			xptperiphlistmatch(cdm);
27788b8a9b1dSJustin T. Gibbs 			break;
27798b8a9b1dSJustin T. Gibbs 		default:
27808b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_ERROR;
27818b8a9b1dSJustin T. Gibbs 			break;
27828b8a9b1dSJustin T. Gibbs 		}
278392c40f40SAlexander Motin 		CAM_SIM_LOCK(xpt_path_sim(cdm->ccb_h.path));
27848b8a9b1dSJustin T. Gibbs 
27858b8a9b1dSJustin T. Gibbs 		if (cdm->status == CAM_DEV_MATCH_ERROR)
27868b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
27878b8a9b1dSJustin T. Gibbs 		else
27888b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
27898b8a9b1dSJustin T. Gibbs 
27908b8a9b1dSJustin T. Gibbs 		break;
27918b8a9b1dSJustin T. Gibbs 	}
27928b8a9b1dSJustin T. Gibbs 	case XPT_SASYNC_CB:
27938b8a9b1dSJustin T. Gibbs 	{
279484f82481SScott Long 		struct ccb_setasync *csa;
279584f82481SScott Long 		struct async_node *cur_entry;
279684f82481SScott Long 		struct async_list *async_head;
279784f82481SScott Long 		u_int32_t added;
279884f82481SScott Long 
279984f82481SScott Long 		csa = &start_ccb->csa;
280084f82481SScott Long 		added = csa->event_enable;
2801da396db2SAlexander Motin 		async_head = &path->device->asyncs;
280284f82481SScott Long 
280384f82481SScott Long 		/*
280484f82481SScott Long 		 * If there is already an entry for us, simply
280584f82481SScott Long 		 * update it.
280684f82481SScott Long 		 */
280784f82481SScott Long 		cur_entry = SLIST_FIRST(async_head);
280884f82481SScott Long 		while (cur_entry != NULL) {
280984f82481SScott Long 			if ((cur_entry->callback_arg == csa->callback_arg)
281084f82481SScott Long 			 && (cur_entry->callback == csa->callback))
281184f82481SScott Long 				break;
281284f82481SScott Long 			cur_entry = SLIST_NEXT(cur_entry, links);
281384f82481SScott Long 		}
281484f82481SScott Long 
281584f82481SScott Long 		if (cur_entry != NULL) {
281684f82481SScott Long 		 	/*
281784f82481SScott Long 			 * If the request has no flags set,
281884f82481SScott Long 			 * remove the entry.
281984f82481SScott Long 			 */
282084f82481SScott Long 			added &= ~cur_entry->event_enable;
282184f82481SScott Long 			if (csa->event_enable == 0) {
282284f82481SScott Long 				SLIST_REMOVE(async_head, cur_entry,
282384f82481SScott Long 					     async_node, links);
2824da396db2SAlexander Motin 				xpt_release_device(path->device);
282584f82481SScott Long 				free(cur_entry, M_CAMXPT);
282684f82481SScott Long 			} else {
282784f82481SScott Long 				cur_entry->event_enable = csa->event_enable;
282884f82481SScott Long 			}
28297685edecSAlexander Motin 			csa->event_enable = added;
283084f82481SScott Long 		} else {
283184f82481SScott Long 			cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
283284f82481SScott Long 					   M_NOWAIT);
283384f82481SScott Long 			if (cur_entry == NULL) {
283484f82481SScott Long 				csa->ccb_h.status = CAM_RESRC_UNAVAIL;
283584f82481SScott Long 				break;
283684f82481SScott Long 			}
283784f82481SScott Long 			cur_entry->event_enable = csa->event_enable;
283884f82481SScott Long 			cur_entry->callback_arg = csa->callback_arg;
283984f82481SScott Long 			cur_entry->callback = csa->callback;
284084f82481SScott Long 			SLIST_INSERT_HEAD(async_head, cur_entry, links);
2841da396db2SAlexander Motin 			xpt_acquire_device(path->device);
284284f82481SScott Long 		}
28438b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
28448b8a9b1dSJustin T. Gibbs 		break;
28458b8a9b1dSJustin T. Gibbs 	}
28468b8a9b1dSJustin T. Gibbs 	case XPT_REL_SIMQ:
28478b8a9b1dSJustin T. Gibbs 	{
28488b8a9b1dSJustin T. Gibbs 		struct ccb_relsim *crs;
28498b8a9b1dSJustin T. Gibbs 		struct cam_ed *dev;
28508b8a9b1dSJustin T. Gibbs 
28518b8a9b1dSJustin T. Gibbs 		crs = &start_ccb->crs;
2852da396db2SAlexander Motin 		dev = path->device;
28538b8a9b1dSJustin T. Gibbs 		if (dev == NULL) {
28548b8a9b1dSJustin T. Gibbs 
28558b8a9b1dSJustin T. Gibbs 			crs->ccb_h.status = CAM_DEV_NOT_THERE;
28568b8a9b1dSJustin T. Gibbs 			break;
28578b8a9b1dSJustin T. Gibbs 		}
28588b8a9b1dSJustin T. Gibbs 
28598b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
28608b8a9b1dSJustin T. Gibbs 
28618b8a9b1dSJustin T. Gibbs 			/* Don't ever go below one opening */
28628b8a9b1dSJustin T. Gibbs 			if (crs->openings > 0) {
28637dc3213dSAlexander Motin 				xpt_dev_ccbq_resize(path, crs->openings);
286479ccc199SJordan K. Hubbard 				if (bootverbose) {
2865da396db2SAlexander Motin 					xpt_print(path,
28667dc3213dSAlexander Motin 					    "number of openings is now %d\n",
28678b8a9b1dSJustin T. Gibbs 					    crs->openings);
28688b8a9b1dSJustin T. Gibbs 				}
28698b8a9b1dSJustin T. Gibbs 			}
28708b8a9b1dSJustin T. Gibbs 		}
28718b8a9b1dSJustin T. Gibbs 
28728b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
28738b8a9b1dSJustin T. Gibbs 
28748b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
28758b8a9b1dSJustin T. Gibbs 
28768b8a9b1dSJustin T. Gibbs 				/*
28778b8a9b1dSJustin T. Gibbs 				 * Just extend the old timeout and decrement
28788b8a9b1dSJustin T. Gibbs 				 * the freeze count so that a single timeout
28798b8a9b1dSJustin T. Gibbs 				 * is sufficient for releasing the queue.
28808b8a9b1dSJustin T. Gibbs 				 */
28818b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
28822b83592fSScott Long 				callout_stop(&dev->callout);
28838b8a9b1dSJustin T. Gibbs 			} else {
28848b8a9b1dSJustin T. Gibbs 
28858b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
28868b8a9b1dSJustin T. Gibbs 			}
28878b8a9b1dSJustin T. Gibbs 
28882b83592fSScott Long 			callout_reset(&dev->callout,
28892b83592fSScott Long 			    (crs->release_timeout * hz) / 1000,
28902b83592fSScott Long 			    xpt_release_devq_timeout, dev);
28918b8a9b1dSJustin T. Gibbs 
28928b8a9b1dSJustin T. Gibbs 			dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
28938b8a9b1dSJustin T. Gibbs 
28948b8a9b1dSJustin T. Gibbs 		}
28958b8a9b1dSJustin T. Gibbs 
28968b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
28978b8a9b1dSJustin T. Gibbs 
28988b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
28998b8a9b1dSJustin T. Gibbs 				/*
29008b8a9b1dSJustin T. Gibbs 				 * Decrement the freeze count so that a single
29018b8a9b1dSJustin T. Gibbs 				 * completion is still sufficient to unfreeze
29028b8a9b1dSJustin T. Gibbs 				 * the queue.
29038b8a9b1dSJustin T. Gibbs 				 */
29048b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
29058b8a9b1dSJustin T. Gibbs 			} else {
29068b8a9b1dSJustin T. Gibbs 
29078b8a9b1dSJustin T. Gibbs 				dev->flags |= CAM_DEV_REL_ON_COMPLETE;
29088b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
29098b8a9b1dSJustin T. Gibbs 			}
29108b8a9b1dSJustin T. Gibbs 		}
29118b8a9b1dSJustin T. Gibbs 
29128b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
29138b8a9b1dSJustin T. Gibbs 
29148b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
29158b8a9b1dSJustin T. Gibbs 			 || (dev->ccbq.dev_active == 0)) {
29168b8a9b1dSJustin T. Gibbs 
29178b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
29188b8a9b1dSJustin T. Gibbs 			} else {
29198b8a9b1dSJustin T. Gibbs 
29208b8a9b1dSJustin T. Gibbs 				dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
29218b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
29228b8a9b1dSJustin T. Gibbs 			}
29238b8a9b1dSJustin T. Gibbs 		}
29248b8a9b1dSJustin T. Gibbs 
2925cccf4220SAlexander Motin 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
2926cccf4220SAlexander Motin 			xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
2927cccf4220SAlexander Motin 		start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
29288b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
29298b8a9b1dSJustin T. Gibbs 		break;
29308b8a9b1dSJustin T. Gibbs 	}
29318b8a9b1dSJustin T. Gibbs 	case XPT_DEBUG: {
293280d6987cSAlexander Motin 		struct cam_path *oldpath;
293380d6987cSAlexander Motin 		struct cam_sim *oldsim;
293480d6987cSAlexander Motin 
2935f0f25b9cSAlexander Motin 		/* Check that all request bits are supported. */
293622c7d606SAlexander Motin 		if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
2937f0f25b9cSAlexander Motin 			start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2938f0f25b9cSAlexander Motin 			break;
2939f0f25b9cSAlexander Motin 		}
2940f0f25b9cSAlexander Motin 
294180d6987cSAlexander Motin 		cam_dflags = CAM_DEBUG_NONE;
29428b8a9b1dSJustin T. Gibbs 		if (cam_dpath != NULL) {
294380d6987cSAlexander Motin 			/* To release the old path we must hold proper lock. */
294480d6987cSAlexander Motin 			oldpath = cam_dpath;
29458b8a9b1dSJustin T. Gibbs 			cam_dpath = NULL;
294680d6987cSAlexander Motin 			oldsim = xpt_path_sim(oldpath);
294780d6987cSAlexander Motin 			CAM_SIM_UNLOCK(xpt_path_sim(start_ccb->ccb_h.path));
294880d6987cSAlexander Motin 			CAM_SIM_LOCK(oldsim);
294980d6987cSAlexander Motin 			xpt_free_path(oldpath);
295080d6987cSAlexander Motin 			CAM_SIM_UNLOCK(oldsim);
295180d6987cSAlexander Motin 			CAM_SIM_LOCK(xpt_path_sim(start_ccb->ccb_h.path));
29528b8a9b1dSJustin T. Gibbs 		}
295380d6987cSAlexander Motin 		if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
2954e5dfa058SAlexander Motin 			if (xpt_create_path(&cam_dpath, NULL,
29558b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.path_id,
29568b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.target_id,
29578b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.target_lun) !=
29588b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP) {
29598b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2960aa872be6SMatt Jacob 			} else {
296180d6987cSAlexander Motin 				cam_dflags = start_ccb->cdbg.flags;
29628b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
2963f0d9af51SMatt Jacob 				xpt_print(cam_dpath, "debugging flags now %x\n",
2964f0d9af51SMatt Jacob 				    cam_dflags);
2965aa872be6SMatt Jacob 			}
296680d6987cSAlexander Motin 		} else
29678b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
29688b8a9b1dSJustin T. Gibbs 		break;
29698b8a9b1dSJustin T. Gibbs 	}
29708b8a9b1dSJustin T. Gibbs 	case XPT_NOOP:
297187cfaf0eSJustin T. Gibbs 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
2972da396db2SAlexander Motin 			xpt_freeze_devq(path, 1);
29738b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
29748b8a9b1dSJustin T. Gibbs 		break;
29758b8a9b1dSJustin T. Gibbs 	default:
29768b8a9b1dSJustin T. Gibbs 	case XPT_SDEV_TYPE:
29778b8a9b1dSJustin T. Gibbs 	case XPT_TERM_IO:
29788b8a9b1dSJustin T. Gibbs 	case XPT_ENG_INQ:
29798b8a9b1dSJustin T. Gibbs 		/* XXX Implement */
29803501942bSJustin T. Gibbs 		printf("%s: CCB type %#x not supported\n", __func__,
29813501942bSJustin T. Gibbs 		       start_ccb->ccb_h.func_code);
29828b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
2983b882a6d3SMatt Jacob 		if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
2984b882a6d3SMatt Jacob 			xpt_done(start_ccb);
2985b882a6d3SMatt Jacob 		}
29868b8a9b1dSJustin T. Gibbs 		break;
29878b8a9b1dSJustin T. Gibbs 	}
29888b8a9b1dSJustin T. Gibbs }
29898b8a9b1dSJustin T. Gibbs 
29908b8a9b1dSJustin T. Gibbs void
29918b8a9b1dSJustin T. Gibbs xpt_polled_action(union ccb *start_ccb)
29928b8a9b1dSJustin T. Gibbs {
29938b8a9b1dSJustin T. Gibbs 	u_int32_t timeout;
29948b8a9b1dSJustin T. Gibbs 	struct	  cam_sim *sim;
29958b8a9b1dSJustin T. Gibbs 	struct	  cam_devq *devq;
29968b8a9b1dSJustin T. Gibbs 	struct	  cam_ed *dev;
29978b8a9b1dSJustin T. Gibbs 
299868153f43SScott Long 
29990069293bSAlexander Motin 	timeout = start_ccb->ccb_h.timeout * 10;
30008b8a9b1dSJustin T. Gibbs 	sim = start_ccb->ccb_h.path->bus->sim;
30018b8a9b1dSJustin T. Gibbs 	devq = sim->devq;
30028b8a9b1dSJustin T. Gibbs 	dev = start_ccb->ccb_h.path->device;
30038b8a9b1dSJustin T. Gibbs 
30042b83592fSScott Long 	mtx_assert(sim->mtx, MA_OWNED);
30058b8a9b1dSJustin T. Gibbs 
30064a612489SAlexander Motin 	/* Don't use ISR for this SIM while polling. */
30074a612489SAlexander Motin 	sim->flags |= CAM_SIM_POLLED;
30084a612489SAlexander Motin 
30098b8a9b1dSJustin T. Gibbs 	/*
30108b8a9b1dSJustin T. Gibbs 	 * Steal an opening so that no other queued requests
30118b8a9b1dSJustin T. Gibbs 	 * can get it before us while we simulate interrupts.
30128b8a9b1dSJustin T. Gibbs 	 */
30138b8a9b1dSJustin T. Gibbs 	dev->ccbq.devq_openings--;
30148b8a9b1dSJustin T. Gibbs 	dev->ccbq.dev_openings--;
30158b8a9b1dSJustin T. Gibbs 
3016d3ef3454SIan Dowse 	while(((devq != NULL && devq->send_openings <= 0) ||
3017d3ef3454SIan Dowse 	   dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
30180069293bSAlexander Motin 		DELAY(100);
30198b8a9b1dSJustin T. Gibbs 		(*(sim->sim_poll))(sim);
302039fe6d85SAlexander Motin 		camisr_runqueue(sim);
30218b8a9b1dSJustin T. Gibbs 	}
30228b8a9b1dSJustin T. Gibbs 
30238b8a9b1dSJustin T. Gibbs 	dev->ccbq.devq_openings++;
30248b8a9b1dSJustin T. Gibbs 	dev->ccbq.dev_openings++;
30258b8a9b1dSJustin T. Gibbs 
30268b8a9b1dSJustin T. Gibbs 	if (timeout != 0) {
30278b8a9b1dSJustin T. Gibbs 		xpt_action(start_ccb);
30288b8a9b1dSJustin T. Gibbs 		while(--timeout > 0) {
30298b8a9b1dSJustin T. Gibbs 			(*(sim->sim_poll))(sim);
303039fe6d85SAlexander Motin 			camisr_runqueue(sim);
30318b8a9b1dSJustin T. Gibbs 			if ((start_ccb->ccb_h.status  & CAM_STATUS_MASK)
30328b8a9b1dSJustin T. Gibbs 			    != CAM_REQ_INPROG)
30338b8a9b1dSJustin T. Gibbs 				break;
30340069293bSAlexander Motin 			DELAY(100);
30358b8a9b1dSJustin T. Gibbs 		}
30368b8a9b1dSJustin T. Gibbs 		if (timeout == 0) {
30378b8a9b1dSJustin T. Gibbs 			/*
30388b8a9b1dSJustin T. Gibbs 			 * XXX Is it worth adding a sim_timeout entry
30398b8a9b1dSJustin T. Gibbs 			 * point so we can attempt recovery?  If
30408b8a9b1dSJustin T. Gibbs 			 * this is only used for dumps, I don't think
30418b8a9b1dSJustin T. Gibbs 			 * it is.
30428b8a9b1dSJustin T. Gibbs 			 */
30438b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
30448b8a9b1dSJustin T. Gibbs 		}
30458b8a9b1dSJustin T. Gibbs 	} else {
30468b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
30478b8a9b1dSJustin T. Gibbs 	}
30484a612489SAlexander Motin 
30494a612489SAlexander Motin 	/* We will use CAM ISR for this SIM again. */
30504a612489SAlexander Motin 	sim->flags &= ~CAM_SIM_POLLED;
30518b8a9b1dSJustin T. Gibbs }
30528b8a9b1dSJustin T. Gibbs 
30538b8a9b1dSJustin T. Gibbs /*
30548b8a9b1dSJustin T. Gibbs  * Schedule a peripheral driver to receive a ccb when it's
30558b8a9b1dSJustin T. Gibbs  * target device has space for more transactions.
30568b8a9b1dSJustin T. Gibbs  */
30578b8a9b1dSJustin T. Gibbs void
30588b8a9b1dSJustin T. Gibbs xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
30598b8a9b1dSJustin T. Gibbs {
30608b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
306183c5d981SAlexander Motin 	int runq = 0;
30628b8a9b1dSJustin T. Gibbs 
30632b83592fSScott Long 	mtx_assert(perph->sim->mtx, MA_OWNED);
306468153f43SScott Long 
30658b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
30668b8a9b1dSJustin T. Gibbs 	device = perph->path->device;
30678b8a9b1dSJustin T. Gibbs 	if (periph_is_queued(perph)) {
30688b8a9b1dSJustin T. Gibbs 		/* Simply reorder based on new priority */
30698b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
30708b8a9b1dSJustin T. Gibbs 			  ("   change priority to %d\n", new_priority));
30718b8a9b1dSJustin T. Gibbs 		if (new_priority < perph->pinfo.priority) {
30728b8a9b1dSJustin T. Gibbs 			camq_change_priority(&device->drvq,
30738b8a9b1dSJustin T. Gibbs 					     perph->pinfo.index,
30748b8a9b1dSJustin T. Gibbs 					     new_priority);
3075cccf4220SAlexander Motin 			runq = 1;
30768b8a9b1dSJustin T. Gibbs 		}
30778b8a9b1dSJustin T. Gibbs 	} else {
30788b8a9b1dSJustin T. Gibbs 		/* New entry on the queue */
30798b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
30808b8a9b1dSJustin T. Gibbs 			  ("   added periph to queue\n"));
30818b8a9b1dSJustin T. Gibbs 		perph->pinfo.priority = new_priority;
30828bad620dSJustin T. Gibbs 		perph->pinfo.generation = ++device->drvq.generation;
30838b8a9b1dSJustin T. Gibbs 		camq_insert(&device->drvq, &perph->pinfo);
3084cccf4220SAlexander Motin 		runq = 1;
30858b8a9b1dSJustin T. Gibbs 	}
30868b8a9b1dSJustin T. Gibbs 	if (runq != 0) {
30878b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3088cccf4220SAlexander Motin 			  ("   calling xpt_run_dev_allocq\n"));
3089cccf4220SAlexander Motin 		xpt_run_dev_allocq(device);
30908b8a9b1dSJustin T. Gibbs 	}
30918b8a9b1dSJustin T. Gibbs }
30928b8a9b1dSJustin T. Gibbs 
30938b8a9b1dSJustin T. Gibbs 
30948b8a9b1dSJustin T. Gibbs /*
30958b8a9b1dSJustin T. Gibbs  * Schedule a device to run on a given queue.
30968b8a9b1dSJustin T. Gibbs  * If the device was inserted as a new entry on the queue,
30978b8a9b1dSJustin T. Gibbs  * return 1 meaning the device queue should be run. If we
30988b8a9b1dSJustin T. Gibbs  * were already queued, implying someone else has already
30998b8a9b1dSJustin T. Gibbs  * started the queue, return 0 so the caller doesn't attempt
310077dc25ccSScott Long  * to run the queue.
31018b8a9b1dSJustin T. Gibbs  */
310252c9ce25SScott Long int
31038b8a9b1dSJustin T. Gibbs xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
31048b8a9b1dSJustin T. Gibbs 		 u_int32_t new_priority)
31058b8a9b1dSJustin T. Gibbs {
31068b8a9b1dSJustin T. Gibbs 	int retval;
31078b8a9b1dSJustin T. Gibbs 	u_int32_t old_priority;
31088b8a9b1dSJustin T. Gibbs 
3109aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
31108b8a9b1dSJustin T. Gibbs 
31118b8a9b1dSJustin T. Gibbs 	old_priority = pinfo->priority;
31128b8a9b1dSJustin T. Gibbs 
31138b8a9b1dSJustin T. Gibbs 	/*
31148b8a9b1dSJustin T. Gibbs 	 * Are we already queued?
31158b8a9b1dSJustin T. Gibbs 	 */
31168b8a9b1dSJustin T. Gibbs 	if (pinfo->index != CAM_UNQUEUED_INDEX) {
31178b8a9b1dSJustin T. Gibbs 		/* Simply reorder based on new priority */
31188b8a9b1dSJustin T. Gibbs 		if (new_priority < old_priority) {
31198b8a9b1dSJustin T. Gibbs 			camq_change_priority(queue, pinfo->index,
31208b8a9b1dSJustin T. Gibbs 					     new_priority);
3121aa872be6SMatt Jacob 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
31228b8a9b1dSJustin T. Gibbs 					("changed priority to %d\n",
31238b8a9b1dSJustin T. Gibbs 					 new_priority));
312483c5d981SAlexander Motin 			retval = 1;
312583c5d981SAlexander Motin 		} else
31268b8a9b1dSJustin T. Gibbs 			retval = 0;
31278b8a9b1dSJustin T. Gibbs 	} else {
31288b8a9b1dSJustin T. Gibbs 		/* New entry on the queue */
31298b8a9b1dSJustin T. Gibbs 		if (new_priority < old_priority)
31308b8a9b1dSJustin T. Gibbs 			pinfo->priority = new_priority;
31318b8a9b1dSJustin T. Gibbs 
3132aa872be6SMatt Jacob 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
31338b8a9b1dSJustin T. Gibbs 				("Inserting onto queue\n"));
31348bad620dSJustin T. Gibbs 		pinfo->generation = ++queue->generation;
31358b8a9b1dSJustin T. Gibbs 		camq_insert(queue, pinfo);
31368b8a9b1dSJustin T. Gibbs 		retval = 1;
31378b8a9b1dSJustin T. Gibbs 	}
31388b8a9b1dSJustin T. Gibbs 	return (retval);
31398b8a9b1dSJustin T. Gibbs }
31408b8a9b1dSJustin T. Gibbs 
31418b8a9b1dSJustin T. Gibbs static void
3142cccf4220SAlexander Motin xpt_run_dev_allocq(struct cam_ed *device)
31438b8a9b1dSJustin T. Gibbs {
31448b8a9b1dSJustin T. Gibbs 	struct camq	*drvq;
31458b8a9b1dSJustin T. Gibbs 
3146cccf4220SAlexander Motin 	if (device->ccbq.devq_allocating)
3147cccf4220SAlexander Motin 		return;
3148cccf4220SAlexander Motin 	device->ccbq.devq_allocating = 1;
3149cccf4220SAlexander Motin 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq(%p)\n", device));
31508b8a9b1dSJustin T. Gibbs 	drvq = &device->drvq;
3151cccf4220SAlexander Motin 	while ((drvq->entries > 0) &&
3152cccf4220SAlexander Motin 	    (device->ccbq.devq_openings > 0 ||
3153cccf4220SAlexander Motin 	     CAMQ_GET_PRIO(drvq) <= CAM_PRIORITY_OOB) &&
3154cccf4220SAlexander Motin 	    (device->ccbq.queue.qfrozen_cnt == 0)) {
3155cccf4220SAlexander Motin 		union	ccb *work_ccb;
3156cccf4220SAlexander Motin 		struct	cam_periph *drv;
3157cccf4220SAlexander Motin 
31582d89c125SAlexander Motin 		KASSERT(drvq->entries > 0, ("xpt_run_dev_allocq: "
31592d89c125SAlexander Motin 		    "Device on queue without any work to do"));
31608b8a9b1dSJustin T. Gibbs 		if ((work_ccb = xpt_get_ccb(device)) != NULL) {
31615a526431SJustin T. Gibbs 			drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
31628b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
31638b8a9b1dSJustin T. Gibbs 				      drv->pinfo.priority);
3164aa872be6SMatt Jacob 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
31658b8a9b1dSJustin T. Gibbs 					("calling periph start\n"));
31668b8a9b1dSJustin T. Gibbs 			drv->periph_start(drv, work_ccb);
31678b8a9b1dSJustin T. Gibbs 		} else {
31688b8a9b1dSJustin T. Gibbs 			/*
31698b8a9b1dSJustin T. Gibbs 			 * Malloc failure in alloc_ccb
31708b8a9b1dSJustin T. Gibbs 			 */
31718b8a9b1dSJustin T. Gibbs 			/*
31728b8a9b1dSJustin T. Gibbs 			 * XXX add us to a list to be run from free_ccb
31738b8a9b1dSJustin T. Gibbs 			 * if we don't have any ccbs active on this
31748b8a9b1dSJustin T. Gibbs 			 * device queue otherwise we may never get run
31758b8a9b1dSJustin T. Gibbs 			 * again.
31768b8a9b1dSJustin T. Gibbs 			 */
31778b8a9b1dSJustin T. Gibbs 			break;
31788b8a9b1dSJustin T. Gibbs 		}
31798b8a9b1dSJustin T. Gibbs 	}
3180cccf4220SAlexander Motin 	device->ccbq.devq_allocating = 0;
31818b8a9b1dSJustin T. Gibbs }
31828b8a9b1dSJustin T. Gibbs 
318383c5d981SAlexander Motin static void
3184cccf4220SAlexander Motin xpt_run_devq(struct cam_devq *devq)
31858b8a9b1dSJustin T. Gibbs {
3186de9ebb68SAlexander Motin 	char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
31878b8a9b1dSJustin T. Gibbs 
3188cccf4220SAlexander Motin 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
31898b8a9b1dSJustin T. Gibbs 
3190cccf4220SAlexander Motin 	devq->send_queue.qfrozen_cnt++;
31918b8a9b1dSJustin T. Gibbs 	while ((devq->send_queue.entries > 0)
3192ec700f26SAlexander Motin 	    && (devq->send_openings > 0)
3193cccf4220SAlexander Motin 	    && (devq->send_queue.qfrozen_cnt <= 1)) {
31948b8a9b1dSJustin T. Gibbs 		struct	cam_ed_qinfo *qinfo;
31958b8a9b1dSJustin T. Gibbs 		struct	cam_ed *device;
31968b8a9b1dSJustin T. Gibbs 		union ccb *work_ccb;
31978b8a9b1dSJustin T. Gibbs 		struct	cam_sim *sim;
31988b8a9b1dSJustin T. Gibbs 
31998b8a9b1dSJustin T. Gibbs 		qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
32005a526431SJustin T. Gibbs 							   CAMQ_HEAD);
32018b8a9b1dSJustin T. Gibbs 		device = qinfo->device;
3202aa872be6SMatt Jacob 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
320350642f18SKenneth D. Merry 				("running device %p\n", device));
32048b8a9b1dSJustin T. Gibbs 
32055a526431SJustin T. Gibbs 		work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
32068b8a9b1dSJustin T. Gibbs 		if (work_ccb == NULL) {
320757b89bbcSNate Lawson 			printf("device on run queue with no ccbs???\n");
32088b8a9b1dSJustin T. Gibbs 			continue;
32098b8a9b1dSJustin T. Gibbs 		}
32108b8a9b1dSJustin T. Gibbs 
32118b8a9b1dSJustin T. Gibbs 		if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
32128b8a9b1dSJustin T. Gibbs 
32132b83592fSScott Long 			mtx_lock(&xsoftc.xpt_lock);
32142b83592fSScott Long 		 	if (xsoftc.num_highpower <= 0) {
32158b8a9b1dSJustin T. Gibbs 				/*
32168b8a9b1dSJustin T. Gibbs 				 * We got a high power command, but we
32178b8a9b1dSJustin T. Gibbs 				 * don't have any available slots.  Freeze
32188b8a9b1dSJustin T. Gibbs 				 * the device queue until we have a slot
32198b8a9b1dSJustin T. Gibbs 				 * available.
32208b8a9b1dSJustin T. Gibbs 				 */
322183c5d981SAlexander Motin 				xpt_freeze_devq(work_ccb->ccb_h.path, 1);
32222b83592fSScott Long 				STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
3223ea541bfdSAlexander Motin 						   work_ccb->ccb_h.path->device,
3224ea541bfdSAlexander Motin 						   highpowerq_entry);
32258b8a9b1dSJustin T. Gibbs 
3226469f9f44SScott Long 				mtx_unlock(&xsoftc.xpt_lock);
32278b8a9b1dSJustin T. Gibbs 				continue;
32288b8a9b1dSJustin T. Gibbs 			} else {
32298b8a9b1dSJustin T. Gibbs 				/*
32308b8a9b1dSJustin T. Gibbs 				 * Consume a high power slot while
32318b8a9b1dSJustin T. Gibbs 				 * this ccb runs.
32328b8a9b1dSJustin T. Gibbs 				 */
32332b83592fSScott Long 				xsoftc.num_highpower--;
32348b8a9b1dSJustin T. Gibbs 			}
32352b83592fSScott Long 			mtx_unlock(&xsoftc.xpt_lock);
32368b8a9b1dSJustin T. Gibbs 		}
32378b8a9b1dSJustin T. Gibbs 		cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
32388b8a9b1dSJustin T. Gibbs 		cam_ccbq_send_ccb(&device->ccbq, work_ccb);
32398b8a9b1dSJustin T. Gibbs 
32408b8a9b1dSJustin T. Gibbs 		devq->send_openings--;
32418b8a9b1dSJustin T. Gibbs 		devq->send_active++;
32428b8a9b1dSJustin T. Gibbs 
3243cccf4220SAlexander Motin 		xpt_schedule_devq(devq, device);
32448b8a9b1dSJustin T. Gibbs 
3245cccf4220SAlexander Motin 		if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
32468b8a9b1dSJustin T. Gibbs 			/*
32478b8a9b1dSJustin T. Gibbs 			 * The client wants to freeze the queue
32488b8a9b1dSJustin T. Gibbs 			 * after this CCB is sent.
32498b8a9b1dSJustin T. Gibbs 			 */
325083c5d981SAlexander Motin 			xpt_freeze_devq(work_ccb->ccb_h.path, 1);
32518b8a9b1dSJustin T. Gibbs 		}
32528b8a9b1dSJustin T. Gibbs 
3253a4eb4f16SMatt Jacob 		/* In Target mode, the peripheral driver knows best... */
3254a4eb4f16SMatt Jacob 		if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3255a4eb4f16SMatt Jacob 			if ((device->inq_flags & SID_CmdQue) != 0
3256a4eb4f16SMatt Jacob 			 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
32578b8a9b1dSJustin T. Gibbs 				work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
32588b8a9b1dSJustin T. Gibbs 			else
32598b8a9b1dSJustin T. Gibbs 				/*
3260a4eb4f16SMatt Jacob 				 * Clear this in case of a retried CCB that
3261a4eb4f16SMatt Jacob 				 * failed due to a rejected tag.
32628b8a9b1dSJustin T. Gibbs 				 */
32638b8a9b1dSJustin T. Gibbs 				work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3264a4eb4f16SMatt Jacob 		}
32658b8a9b1dSJustin T. Gibbs 
3266de9ebb68SAlexander Motin 		switch (work_ccb->ccb_h.func_code) {
3267de9ebb68SAlexander Motin 		case XPT_SCSI_IO:
3268de9ebb68SAlexander Motin 			CAM_DEBUG(work_ccb->ccb_h.path,
3269de9ebb68SAlexander Motin 			    CAM_DEBUG_CDB,("%s. CDB: %s\n",
3270de9ebb68SAlexander Motin 			     scsi_op_desc(work_ccb->csio.cdb_io.cdb_bytes[0],
3271de9ebb68SAlexander Motin 					  &device->inq_data),
3272de9ebb68SAlexander Motin 			     scsi_cdb_string(work_ccb->csio.cdb_io.cdb_bytes,
3273de9ebb68SAlexander Motin 					     cdb_str, sizeof(cdb_str))));
3274de9ebb68SAlexander Motin 			break;
3275de9ebb68SAlexander Motin 		case XPT_ATA_IO:
3276de9ebb68SAlexander Motin 			CAM_DEBUG(work_ccb->ccb_h.path,
3277de9ebb68SAlexander Motin 			    CAM_DEBUG_CDB,("%s. ACB: %s\n",
3278de9ebb68SAlexander Motin 			     ata_op_string(&work_ccb->ataio.cmd),
3279de9ebb68SAlexander Motin 			     ata_cmd_string(&work_ccb->ataio.cmd,
3280de9ebb68SAlexander Motin 					    cdb_str, sizeof(cdb_str))));
3281de9ebb68SAlexander Motin 			break;
3282de9ebb68SAlexander Motin 		default:
3283de9ebb68SAlexander Motin 			break;
3284de9ebb68SAlexander Motin 		}
3285de9ebb68SAlexander Motin 
32868b8a9b1dSJustin T. Gibbs 		/*
32878b8a9b1dSJustin T. Gibbs 		 * Device queues can be shared among multiple sim instances
32888b8a9b1dSJustin T. Gibbs 		 * that reside on different busses.  Use the SIM in the queue
32898b8a9b1dSJustin T. Gibbs 		 * CCB's path, rather than the one in the bus that was passed
32908b8a9b1dSJustin T. Gibbs 		 * into this function.
32918b8a9b1dSJustin T. Gibbs 		 */
32928b8a9b1dSJustin T. Gibbs 		sim = work_ccb->ccb_h.path->bus->sim;
32938b8a9b1dSJustin T. Gibbs 		(*(sim->sim_action))(sim, work_ccb);
32948b8a9b1dSJustin T. Gibbs 	}
3295cccf4220SAlexander Motin 	devq->send_queue.qfrozen_cnt--;
32968b8a9b1dSJustin T. Gibbs }
32978b8a9b1dSJustin T. Gibbs 
32988b8a9b1dSJustin T. Gibbs /*
32998b8a9b1dSJustin T. Gibbs  * This function merges stuff from the slave ccb into the master ccb, while
33008b8a9b1dSJustin T. Gibbs  * keeping important fields in the master ccb constant.
33018b8a9b1dSJustin T. Gibbs  */
33028b8a9b1dSJustin T. Gibbs void
33038b8a9b1dSJustin T. Gibbs xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
33048b8a9b1dSJustin T. Gibbs {
330568153f43SScott Long 
33068b8a9b1dSJustin T. Gibbs 	/*
33078b8a9b1dSJustin T. Gibbs 	 * Pull fields that are valid for peripheral drivers to set
33088b8a9b1dSJustin T. Gibbs 	 * into the master CCB along with the CCB "payload".
33098b8a9b1dSJustin T. Gibbs 	 */
33108b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
33118b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
33128b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
33138b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
33148b8a9b1dSJustin T. Gibbs 	bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
33158b8a9b1dSJustin T. Gibbs 	      sizeof(union ccb) - sizeof(struct ccb_hdr));
33168b8a9b1dSJustin T. Gibbs }
33178b8a9b1dSJustin T. Gibbs 
33188b8a9b1dSJustin T. Gibbs void
33198b8a9b1dSJustin T. Gibbs xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
33208b8a9b1dSJustin T. Gibbs {
332168153f43SScott Long 
33228b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
33238b8a9b1dSJustin T. Gibbs 	ccb_h->pinfo.priority = priority;
33248b8a9b1dSJustin T. Gibbs 	ccb_h->path = path;
33258b8a9b1dSJustin T. Gibbs 	ccb_h->path_id = path->bus->path_id;
33268b8a9b1dSJustin T. Gibbs 	if (path->target)
33278b8a9b1dSJustin T. Gibbs 		ccb_h->target_id = path->target->target_id;
33288b8a9b1dSJustin T. Gibbs 	else
33298b8a9b1dSJustin T. Gibbs 		ccb_h->target_id = CAM_TARGET_WILDCARD;
33308b8a9b1dSJustin T. Gibbs 	if (path->device) {
33318b8a9b1dSJustin T. Gibbs 		ccb_h->target_lun = path->device->lun_id;
33328bad620dSJustin T. Gibbs 		ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
33338b8a9b1dSJustin T. Gibbs 	} else {
33348b8a9b1dSJustin T. Gibbs 		ccb_h->target_lun = CAM_TARGET_WILDCARD;
33358b8a9b1dSJustin T. Gibbs 	}
33368b8a9b1dSJustin T. Gibbs 	ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
33378b8a9b1dSJustin T. Gibbs 	ccb_h->flags = 0;
33388b8a9b1dSJustin T. Gibbs }
33398b8a9b1dSJustin T. Gibbs 
33408b8a9b1dSJustin T. Gibbs /* Path manipulation functions */
33418b8a9b1dSJustin T. Gibbs cam_status
33428b8a9b1dSJustin T. Gibbs xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
33438b8a9b1dSJustin T. Gibbs 		path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
33448b8a9b1dSJustin T. Gibbs {
33458b8a9b1dSJustin T. Gibbs 	struct	   cam_path *path;
33468b8a9b1dSJustin T. Gibbs 	cam_status status;
33478b8a9b1dSJustin T. Gibbs 
3348596ee08fSAlexander Motin 	path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
33498b8a9b1dSJustin T. Gibbs 
33508b8a9b1dSJustin T. Gibbs 	if (path == NULL) {
33518b8a9b1dSJustin T. Gibbs 		status = CAM_RESRC_UNAVAIL;
33528b8a9b1dSJustin T. Gibbs 		return(status);
33538b8a9b1dSJustin T. Gibbs 	}
33548b8a9b1dSJustin T. Gibbs 	status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
33558b8a9b1dSJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
3356596ee08fSAlexander Motin 		free(path, M_CAMPATH);
33578b8a9b1dSJustin T. Gibbs 		path = NULL;
33588b8a9b1dSJustin T. Gibbs 	}
33598b8a9b1dSJustin T. Gibbs 	*new_path_ptr = path;
33608b8a9b1dSJustin T. Gibbs 	return (status);
33618b8a9b1dSJustin T. Gibbs }
33628b8a9b1dSJustin T. Gibbs 
33632b83592fSScott Long cam_status
33642b83592fSScott Long xpt_create_path_unlocked(struct cam_path **new_path_ptr,
33652b83592fSScott Long 			 struct cam_periph *periph, path_id_t path_id,
33662b83592fSScott Long 			 target_id_t target_id, lun_id_t lun_id)
33672b83592fSScott Long {
33682b83592fSScott Long 	struct	   cam_path *path;
33692b83592fSScott Long 	struct	   cam_eb *bus = NULL;
33702b83592fSScott Long 	cam_status status;
33712b83592fSScott Long 
3372596ee08fSAlexander Motin 	path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_WAITOK);
33732b83592fSScott Long 
33742b83592fSScott Long 	bus = xpt_find_bus(path_id);
33759bccc7a9SAlexander Motin 	if (bus != NULL)
337611e4faceSScott Long 		CAM_SIM_LOCK(bus->sim);
33772b83592fSScott Long 	status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
33789bccc7a9SAlexander Motin 	if (bus != NULL) {
337911e4faceSScott Long 		CAM_SIM_UNLOCK(bus->sim);
338015975b7bSMatt Jacob 		xpt_release_bus(bus);
338115975b7bSMatt Jacob 	}
33822b83592fSScott Long 	if (status != CAM_REQ_CMP) {
3383596ee08fSAlexander Motin 		free(path, M_CAMPATH);
33842b83592fSScott Long 		path = NULL;
33852b83592fSScott Long 	}
33862b83592fSScott Long 	*new_path_ptr = path;
33872b83592fSScott Long 	return (status);
33882b83592fSScott Long }
33892b83592fSScott Long 
339052c9ce25SScott Long cam_status
33918b8a9b1dSJustin T. Gibbs xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
33928b8a9b1dSJustin T. Gibbs 		 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
33938b8a9b1dSJustin T. Gibbs {
33948b8a9b1dSJustin T. Gibbs 	struct	     cam_eb *bus;
33958b8a9b1dSJustin T. Gibbs 	struct	     cam_et *target;
33968b8a9b1dSJustin T. Gibbs 	struct	     cam_ed *device;
33978b8a9b1dSJustin T. Gibbs 	cam_status   status;
33988b8a9b1dSJustin T. Gibbs 
33998b8a9b1dSJustin T. Gibbs 	status = CAM_REQ_CMP;	/* Completed without error */
34008b8a9b1dSJustin T. Gibbs 	target = NULL;		/* Wildcarded */
34018b8a9b1dSJustin T. Gibbs 	device = NULL;		/* Wildcarded */
3402a5479bc5SJustin T. Gibbs 
3403a5479bc5SJustin T. Gibbs 	/*
3404a5479bc5SJustin T. Gibbs 	 * We will potentially modify the EDT, so block interrupts
3405a5479bc5SJustin T. Gibbs 	 * that may attempt to create cam paths.
3406a5479bc5SJustin T. Gibbs 	 */
34078b8a9b1dSJustin T. Gibbs 	bus = xpt_find_bus(path_id);
34088b8a9b1dSJustin T. Gibbs 	if (bus == NULL) {
34098b8a9b1dSJustin T. Gibbs 		status = CAM_PATH_INVALID;
3410c8bead2aSJustin T. Gibbs 	} else {
34118b8a9b1dSJustin T. Gibbs 		target = xpt_find_target(bus, target_id);
34128b8a9b1dSJustin T. Gibbs 		if (target == NULL) {
34138b8a9b1dSJustin T. Gibbs 			/* Create one */
34148b8a9b1dSJustin T. Gibbs 			struct cam_et *new_target;
34158b8a9b1dSJustin T. Gibbs 
34168b8a9b1dSJustin T. Gibbs 			new_target = xpt_alloc_target(bus, target_id);
34178b8a9b1dSJustin T. Gibbs 			if (new_target == NULL) {
34188b8a9b1dSJustin T. Gibbs 				status = CAM_RESRC_UNAVAIL;
34198b8a9b1dSJustin T. Gibbs 			} else {
34208b8a9b1dSJustin T. Gibbs 				target = new_target;
34218b8a9b1dSJustin T. Gibbs 			}
34228b8a9b1dSJustin T. Gibbs 		}
3423c8bead2aSJustin T. Gibbs 		if (target != NULL) {
34248b8a9b1dSJustin T. Gibbs 			device = xpt_find_device(target, lun_id);
34258b8a9b1dSJustin T. Gibbs 			if (device == NULL) {
34268b8a9b1dSJustin T. Gibbs 				/* Create one */
34278b8a9b1dSJustin T. Gibbs 				struct cam_ed *new_device;
34288b8a9b1dSJustin T. Gibbs 
342952c9ce25SScott Long 				new_device =
343052c9ce25SScott Long 				    (*(bus->xport->alloc_device))(bus,
34318b8a9b1dSJustin T. Gibbs 								      target,
34328b8a9b1dSJustin T. Gibbs 								      lun_id);
34338b8a9b1dSJustin T. Gibbs 				if (new_device == NULL) {
34348b8a9b1dSJustin T. Gibbs 					status = CAM_RESRC_UNAVAIL;
34358b8a9b1dSJustin T. Gibbs 				} else {
34368b8a9b1dSJustin T. Gibbs 					device = new_device;
34378b8a9b1dSJustin T. Gibbs 				}
34388b8a9b1dSJustin T. Gibbs 			}
34398b8a9b1dSJustin T. Gibbs 		}
34408b8a9b1dSJustin T. Gibbs 	}
34418b8a9b1dSJustin T. Gibbs 
34428b8a9b1dSJustin T. Gibbs 	/*
34438b8a9b1dSJustin T. Gibbs 	 * Only touch the user's data if we are successful.
34448b8a9b1dSJustin T. Gibbs 	 */
34458b8a9b1dSJustin T. Gibbs 	if (status == CAM_REQ_CMP) {
34468b8a9b1dSJustin T. Gibbs 		new_path->periph = perph;
34478b8a9b1dSJustin T. Gibbs 		new_path->bus = bus;
34488b8a9b1dSJustin T. Gibbs 		new_path->target = target;
34498b8a9b1dSJustin T. Gibbs 		new_path->device = device;
34508b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
34518b8a9b1dSJustin T. Gibbs 	} else {
34528b8a9b1dSJustin T. Gibbs 		if (device != NULL)
3453f98d7a47SAlexander Motin 			xpt_release_device(device);
34548b8a9b1dSJustin T. Gibbs 		if (target != NULL)
3455f98d7a47SAlexander Motin 			xpt_release_target(target);
3456a5479bc5SJustin T. Gibbs 		if (bus != NULL)
3457a5479bc5SJustin T. Gibbs 			xpt_release_bus(bus);
34588b8a9b1dSJustin T. Gibbs 	}
34598b8a9b1dSJustin T. Gibbs 	return (status);
34608b8a9b1dSJustin T. Gibbs }
34618b8a9b1dSJustin T. Gibbs 
346252c9ce25SScott Long void
34638b8a9b1dSJustin T. Gibbs xpt_release_path(struct cam_path *path)
34648b8a9b1dSJustin T. Gibbs {
34658b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
34669dd03ecfSJustin T. Gibbs 	if (path->device != NULL) {
3467f98d7a47SAlexander Motin 		xpt_release_device(path->device);
34689dd03ecfSJustin T. Gibbs 		path->device = NULL;
34699dd03ecfSJustin T. Gibbs 	}
34709dd03ecfSJustin T. Gibbs 	if (path->target != NULL) {
3471f98d7a47SAlexander Motin 		xpt_release_target(path->target);
34729dd03ecfSJustin T. Gibbs 		path->target = NULL;
34739dd03ecfSJustin T. Gibbs 	}
34749dd03ecfSJustin T. Gibbs 	if (path->bus != NULL) {
34759dd03ecfSJustin T. Gibbs 		xpt_release_bus(path->bus);
34769dd03ecfSJustin T. Gibbs 		path->bus = NULL;
34779dd03ecfSJustin T. Gibbs 	}
34788b8a9b1dSJustin T. Gibbs }
34798b8a9b1dSJustin T. Gibbs 
34808b8a9b1dSJustin T. Gibbs void
34818b8a9b1dSJustin T. Gibbs xpt_free_path(struct cam_path *path)
34828b8a9b1dSJustin T. Gibbs {
348368153f43SScott Long 
34848b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
34858b8a9b1dSJustin T. Gibbs 	xpt_release_path(path);
3486596ee08fSAlexander Motin 	free(path, M_CAMPATH);
34878b8a9b1dSJustin T. Gibbs }
34888b8a9b1dSJustin T. Gibbs 
348915975b7bSMatt Jacob void
349015975b7bSMatt Jacob xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
349115975b7bSMatt Jacob     uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
349215975b7bSMatt Jacob {
349315975b7bSMatt Jacob 
34949a7c2696SAlexander Motin 	xpt_lock_buses();
349515975b7bSMatt Jacob 	if (bus_ref) {
349615975b7bSMatt Jacob 		if (path->bus)
349715975b7bSMatt Jacob 			*bus_ref = path->bus->refcount;
349815975b7bSMatt Jacob 		else
349915975b7bSMatt Jacob 			*bus_ref = 0;
350015975b7bSMatt Jacob 	}
350115975b7bSMatt Jacob 	if (periph_ref) {
350215975b7bSMatt Jacob 		if (path->periph)
350315975b7bSMatt Jacob 			*periph_ref = path->periph->refcount;
350415975b7bSMatt Jacob 		else
350515975b7bSMatt Jacob 			*periph_ref = 0;
350615975b7bSMatt Jacob 	}
3507dcdf6e74SAlexander Motin 	xpt_unlock_buses();
350815975b7bSMatt Jacob 	if (target_ref) {
350915975b7bSMatt Jacob 		if (path->target)
351015975b7bSMatt Jacob 			*target_ref = path->target->refcount;
351115975b7bSMatt Jacob 		else
351215975b7bSMatt Jacob 			*target_ref = 0;
351315975b7bSMatt Jacob 	}
351415975b7bSMatt Jacob 	if (device_ref) {
351515975b7bSMatt Jacob 		if (path->device)
351615975b7bSMatt Jacob 			*device_ref = path->device->refcount;
351715975b7bSMatt Jacob 		else
351815975b7bSMatt Jacob 			*device_ref = 0;
351915975b7bSMatt Jacob 	}
352015975b7bSMatt Jacob }
35218b8a9b1dSJustin T. Gibbs 
35228b8a9b1dSJustin T. Gibbs /*
35232cefde5fSJustin T. Gibbs  * Return -1 for failure, 0 for exact match, 1 for match with wildcards
35242cefde5fSJustin T. Gibbs  * in path1, 2 for match with wildcards in path2.
35258b8a9b1dSJustin T. Gibbs  */
35268b8a9b1dSJustin T. Gibbs int
35278b8a9b1dSJustin T. Gibbs xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
35288b8a9b1dSJustin T. Gibbs {
35298b8a9b1dSJustin T. Gibbs 	int retval = 0;
35308b8a9b1dSJustin T. Gibbs 
35318b8a9b1dSJustin T. Gibbs 	if (path1->bus != path2->bus) {
35322cefde5fSJustin T. Gibbs 		if (path1->bus->path_id == CAM_BUS_WILDCARD)
35338b8a9b1dSJustin T. Gibbs 			retval = 1;
35342cefde5fSJustin T. Gibbs 		else if (path2->bus->path_id == CAM_BUS_WILDCARD)
35352cefde5fSJustin T. Gibbs 			retval = 2;
35368b8a9b1dSJustin T. Gibbs 		else
35378b8a9b1dSJustin T. Gibbs 			return (-1);
35388b8a9b1dSJustin T. Gibbs 	}
35398b8a9b1dSJustin T. Gibbs 	if (path1->target != path2->target) {
35402cefde5fSJustin T. Gibbs 		if (path1->target->target_id == CAM_TARGET_WILDCARD) {
35412cefde5fSJustin T. Gibbs 			if (retval == 0)
35428b8a9b1dSJustin T. Gibbs 				retval = 1;
35432cefde5fSJustin T. Gibbs 		} else if (path2->target->target_id == CAM_TARGET_WILDCARD)
35442cefde5fSJustin T. Gibbs 			retval = 2;
35458b8a9b1dSJustin T. Gibbs 		else
35468b8a9b1dSJustin T. Gibbs 			return (-1);
35478b8a9b1dSJustin T. Gibbs 	}
35488b8a9b1dSJustin T. Gibbs 	if (path1->device != path2->device) {
35492cefde5fSJustin T. Gibbs 		if (path1->device->lun_id == CAM_LUN_WILDCARD) {
35502cefde5fSJustin T. Gibbs 			if (retval == 0)
35518b8a9b1dSJustin T. Gibbs 				retval = 1;
35522cefde5fSJustin T. Gibbs 		} else if (path2->device->lun_id == CAM_LUN_WILDCARD)
35532cefde5fSJustin T. Gibbs 			retval = 2;
35548b8a9b1dSJustin T. Gibbs 		else
35558b8a9b1dSJustin T. Gibbs 			return (-1);
35568b8a9b1dSJustin T. Gibbs 	}
35578b8a9b1dSJustin T. Gibbs 	return (retval);
35588b8a9b1dSJustin T. Gibbs }
35598b8a9b1dSJustin T. Gibbs 
35608b8a9b1dSJustin T. Gibbs void
35618b8a9b1dSJustin T. Gibbs xpt_print_path(struct cam_path *path)
35628b8a9b1dSJustin T. Gibbs {
356368153f43SScott Long 
35648b8a9b1dSJustin T. Gibbs 	if (path == NULL)
35658b8a9b1dSJustin T. Gibbs 		printf("(nopath): ");
35668b8a9b1dSJustin T. Gibbs 	else {
35678b8a9b1dSJustin T. Gibbs 		if (path->periph != NULL)
35688b8a9b1dSJustin T. Gibbs 			printf("(%s%d:", path->periph->periph_name,
35698b8a9b1dSJustin T. Gibbs 			       path->periph->unit_number);
35708b8a9b1dSJustin T. Gibbs 		else
35718b8a9b1dSJustin T. Gibbs 			printf("(noperiph:");
35728b8a9b1dSJustin T. Gibbs 
35738b8a9b1dSJustin T. Gibbs 		if (path->bus != NULL)
35748b8a9b1dSJustin T. Gibbs 			printf("%s%d:%d:", path->bus->sim->sim_name,
35758b8a9b1dSJustin T. Gibbs 			       path->bus->sim->unit_number,
35768b8a9b1dSJustin T. Gibbs 			       path->bus->sim->bus_id);
35778b8a9b1dSJustin T. Gibbs 		else
35788b8a9b1dSJustin T. Gibbs 			printf("nobus:");
35798b8a9b1dSJustin T. Gibbs 
35808b8a9b1dSJustin T. Gibbs 		if (path->target != NULL)
35818b8a9b1dSJustin T. Gibbs 			printf("%d:", path->target->target_id);
35828b8a9b1dSJustin T. Gibbs 		else
35838b8a9b1dSJustin T. Gibbs 			printf("X:");
35848b8a9b1dSJustin T. Gibbs 
35858b8a9b1dSJustin T. Gibbs 		if (path->device != NULL)
35868b8a9b1dSJustin T. Gibbs 			printf("%d): ", path->device->lun_id);
35878b8a9b1dSJustin T. Gibbs 		else
35888b8a9b1dSJustin T. Gibbs 			printf("X): ");
35898b8a9b1dSJustin T. Gibbs 	}
35908b8a9b1dSJustin T. Gibbs }
35918b8a9b1dSJustin T. Gibbs 
3592f0d9af51SMatt Jacob void
3593f0d9af51SMatt Jacob xpt_print(struct cam_path *path, const char *fmt, ...)
3594f0d9af51SMatt Jacob {
3595f0d9af51SMatt Jacob 	va_list ap;
3596f0d9af51SMatt Jacob 	xpt_print_path(path);
3597f0d9af51SMatt Jacob 	va_start(ap, fmt);
3598f0d9af51SMatt Jacob 	vprintf(fmt, ap);
3599f0d9af51SMatt Jacob 	va_end(ap);
3600f0d9af51SMatt Jacob }
3601f0d9af51SMatt Jacob 
36023393f8daSKenneth D. Merry int
36033393f8daSKenneth D. Merry xpt_path_string(struct cam_path *path, char *str, size_t str_len)
36043393f8daSKenneth D. Merry {
36053393f8daSKenneth D. Merry 	struct sbuf sb;
36063393f8daSKenneth D. Merry 
3607ac37e649SEdward Tomasz Napierala #ifdef INVARIANTS
3608a8d4700dSEdward Tomasz Napierala 	if (path != NULL && path->bus != NULL)
36092b83592fSScott Long 		mtx_assert(path->bus->sim->mtx, MA_OWNED);
3610ac37e649SEdward Tomasz Napierala #endif
361168153f43SScott Long 
36123393f8daSKenneth D. Merry 	sbuf_new(&sb, str, str_len, 0);
36133393f8daSKenneth D. Merry 
36143393f8daSKenneth D. Merry 	if (path == NULL)
36153393f8daSKenneth D. Merry 		sbuf_printf(&sb, "(nopath): ");
36163393f8daSKenneth D. Merry 	else {
36173393f8daSKenneth D. Merry 		if (path->periph != NULL)
36183393f8daSKenneth D. Merry 			sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
36193393f8daSKenneth D. Merry 				    path->periph->unit_number);
36203393f8daSKenneth D. Merry 		else
36213393f8daSKenneth D. Merry 			sbuf_printf(&sb, "(noperiph:");
36223393f8daSKenneth D. Merry 
36233393f8daSKenneth D. Merry 		if (path->bus != NULL)
36243393f8daSKenneth D. Merry 			sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
36253393f8daSKenneth D. Merry 				    path->bus->sim->unit_number,
36263393f8daSKenneth D. Merry 				    path->bus->sim->bus_id);
36273393f8daSKenneth D. Merry 		else
36283393f8daSKenneth D. Merry 			sbuf_printf(&sb, "nobus:");
36293393f8daSKenneth D. Merry 
36303393f8daSKenneth D. Merry 		if (path->target != NULL)
36313393f8daSKenneth D. Merry 			sbuf_printf(&sb, "%d:", path->target->target_id);
36323393f8daSKenneth D. Merry 		else
36333393f8daSKenneth D. Merry 			sbuf_printf(&sb, "X:");
36343393f8daSKenneth D. Merry 
36353393f8daSKenneth D. Merry 		if (path->device != NULL)
36363393f8daSKenneth D. Merry 			sbuf_printf(&sb, "%d): ", path->device->lun_id);
36373393f8daSKenneth D. Merry 		else
36383393f8daSKenneth D. Merry 			sbuf_printf(&sb, "X): ");
36393393f8daSKenneth D. Merry 	}
36403393f8daSKenneth D. Merry 	sbuf_finish(&sb);
36413393f8daSKenneth D. Merry 
36423393f8daSKenneth D. Merry 	return(sbuf_len(&sb));
36433393f8daSKenneth D. Merry }
36443393f8daSKenneth D. Merry 
36458b8a9b1dSJustin T. Gibbs path_id_t
36468b8a9b1dSJustin T. Gibbs xpt_path_path_id(struct cam_path *path)
36478b8a9b1dSJustin T. Gibbs {
36488b8a9b1dSJustin T. Gibbs 	return(path->bus->path_id);
36498b8a9b1dSJustin T. Gibbs }
36508b8a9b1dSJustin T. Gibbs 
36518b8a9b1dSJustin T. Gibbs target_id_t
36528b8a9b1dSJustin T. Gibbs xpt_path_target_id(struct cam_path *path)
36538b8a9b1dSJustin T. Gibbs {
36548b8a9b1dSJustin T. Gibbs 	if (path->target != NULL)
36558b8a9b1dSJustin T. Gibbs 		return (path->target->target_id);
36568b8a9b1dSJustin T. Gibbs 	else
36578b8a9b1dSJustin T. Gibbs 		return (CAM_TARGET_WILDCARD);
36588b8a9b1dSJustin T. Gibbs }
36598b8a9b1dSJustin T. Gibbs 
36608b8a9b1dSJustin T. Gibbs lun_id_t
36618b8a9b1dSJustin T. Gibbs xpt_path_lun_id(struct cam_path *path)
36628b8a9b1dSJustin T. Gibbs {
36638b8a9b1dSJustin T. Gibbs 	if (path->device != NULL)
36648b8a9b1dSJustin T. Gibbs 		return (path->device->lun_id);
36658b8a9b1dSJustin T. Gibbs 	else
36668b8a9b1dSJustin T. Gibbs 		return (CAM_LUN_WILDCARD);
36678b8a9b1dSJustin T. Gibbs }
36688b8a9b1dSJustin T. Gibbs 
36698b8a9b1dSJustin T. Gibbs struct cam_sim *
36708b8a9b1dSJustin T. Gibbs xpt_path_sim(struct cam_path *path)
36718b8a9b1dSJustin T. Gibbs {
367268153f43SScott Long 
36738b8a9b1dSJustin T. Gibbs 	return (path->bus->sim);
36748b8a9b1dSJustin T. Gibbs }
36758b8a9b1dSJustin T. Gibbs 
36768b8a9b1dSJustin T. Gibbs struct cam_periph*
36778b8a9b1dSJustin T. Gibbs xpt_path_periph(struct cam_path *path)
36788b8a9b1dSJustin T. Gibbs {
36792b83592fSScott Long 	mtx_assert(path->bus->sim->mtx, MA_OWNED);
368068153f43SScott Long 
36818b8a9b1dSJustin T. Gibbs 	return (path->periph);
36828b8a9b1dSJustin T. Gibbs }
36838b8a9b1dSJustin T. Gibbs 
36840d307e09SAlexander Motin int
36850d307e09SAlexander Motin xpt_path_legacy_ata_id(struct cam_path *path)
36860d307e09SAlexander Motin {
36870d307e09SAlexander Motin 	struct cam_eb *bus;
36880d307e09SAlexander Motin 	int bus_id;
36890d307e09SAlexander Motin 
36900d307e09SAlexander Motin 	if ((strcmp(path->bus->sim->sim_name, "ata") != 0) &&
36910d307e09SAlexander Motin 	    strcmp(path->bus->sim->sim_name, "ahcich") != 0 &&
36920d307e09SAlexander Motin 	    strcmp(path->bus->sim->sim_name, "mvsch") != 0 &&
36930d307e09SAlexander Motin 	    strcmp(path->bus->sim->sim_name, "siisch") != 0)
36940d307e09SAlexander Motin 		return (-1);
36950d307e09SAlexander Motin 
36960d307e09SAlexander Motin 	if (strcmp(path->bus->sim->sim_name, "ata") == 0 &&
36970d307e09SAlexander Motin 	    path->bus->sim->unit_number < 2) {
36980d307e09SAlexander Motin 		bus_id = path->bus->sim->unit_number;
36990d307e09SAlexander Motin 	} else {
37000d307e09SAlexander Motin 		bus_id = 2;
37010d307e09SAlexander Motin 		xpt_lock_buses();
37020d307e09SAlexander Motin 		TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
37030d307e09SAlexander Motin 			if (bus == path->bus)
37040d307e09SAlexander Motin 				break;
37050d307e09SAlexander Motin 			if ((strcmp(bus->sim->sim_name, "ata") == 0 &&
37060d307e09SAlexander Motin 			     bus->sim->unit_number >= 2) ||
37070d307e09SAlexander Motin 			    strcmp(bus->sim->sim_name, "ahcich") == 0 ||
37080d307e09SAlexander Motin 			    strcmp(bus->sim->sim_name, "mvsch") == 0 ||
37090d307e09SAlexander Motin 			    strcmp(bus->sim->sim_name, "siisch") == 0)
37100d307e09SAlexander Motin 				bus_id++;
37110d307e09SAlexander Motin 		}
37120d307e09SAlexander Motin 		xpt_unlock_buses();
37130d307e09SAlexander Motin 	}
3714ce6cf987SAlexander Motin 	if (path->target != NULL) {
3715ce6cf987SAlexander Motin 		if (path->target->target_id < 2)
37160d307e09SAlexander Motin 			return (bus_id * 2 + path->target->target_id);
37170d307e09SAlexander Motin 		else
3718ce6cf987SAlexander Motin 			return (-1);
3719ce6cf987SAlexander Motin 	} else
37200d307e09SAlexander Motin 		return (bus_id * 2);
37210d307e09SAlexander Motin }
37220d307e09SAlexander Motin 
37238b8a9b1dSJustin T. Gibbs /*
37248b8a9b1dSJustin T. Gibbs  * Release a CAM control block for the caller.  Remit the cost of the structure
37258b8a9b1dSJustin T. Gibbs  * to the device referenced by the path.  If the this device had no 'credits'
37268b8a9b1dSJustin T. Gibbs  * and peripheral drivers have registered async callbacks for this notification
37278b8a9b1dSJustin T. Gibbs  * call them now.
37288b8a9b1dSJustin T. Gibbs  */
37298b8a9b1dSJustin T. Gibbs void
37308b8a9b1dSJustin T. Gibbs xpt_release_ccb(union ccb *free_ccb)
37318b8a9b1dSJustin T. Gibbs {
37328b8a9b1dSJustin T. Gibbs 	struct	 cam_path *path;
37338b8a9b1dSJustin T. Gibbs 	struct	 cam_ed *device;
37348b8a9b1dSJustin T. Gibbs 	struct	 cam_eb *bus;
37352b83592fSScott Long 	struct   cam_sim *sim;
373668153f43SScott Long 
3737aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
37388b8a9b1dSJustin T. Gibbs 	path = free_ccb->ccb_h.path;
37398b8a9b1dSJustin T. Gibbs 	device = path->device;
37408b8a9b1dSJustin T. Gibbs 	bus = path->bus;
37412b83592fSScott Long 	sim = bus->sim;
37422b83592fSScott Long 
37432b83592fSScott Long 	mtx_assert(sim->mtx, MA_OWNED);
37442b83592fSScott Long 
37458b8a9b1dSJustin T. Gibbs 	cam_ccbq_release_opening(&device->ccbq);
37462b83592fSScott Long 	if (sim->ccb_count > sim->max_ccbs) {
37478b8a9b1dSJustin T. Gibbs 		xpt_free_ccb(free_ccb);
37482b83592fSScott Long 		sim->ccb_count--;
37498b8a9b1dSJustin T. Gibbs 	} else {
37502b83592fSScott Long 		SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
37512b83592fSScott Long 		    xpt_links.sle);
37528b8a9b1dSJustin T. Gibbs 	}
3753cccf4220SAlexander Motin 	xpt_run_dev_allocq(device);
37548b8a9b1dSJustin T. Gibbs }
37558b8a9b1dSJustin T. Gibbs 
37568b8a9b1dSJustin T. Gibbs /* Functions accessed by SIM drivers */
37578b8a9b1dSJustin T. Gibbs 
375852c9ce25SScott Long static struct xpt_xport xport_default = {
375952c9ce25SScott Long 	.alloc_device = xpt_alloc_device_default,
376052c9ce25SScott Long 	.action = xpt_action_default,
376152c9ce25SScott Long 	.async = xpt_dev_async_default,
376252c9ce25SScott Long };
376352c9ce25SScott Long 
37648b8a9b1dSJustin T. Gibbs /*
37658b8a9b1dSJustin T. Gibbs  * A sim structure, listing the SIM entry points and instance
37668b8a9b1dSJustin T. Gibbs  * identification info is passed to xpt_bus_register to hook the SIM
37678b8a9b1dSJustin T. Gibbs  * into the CAM framework.  xpt_bus_register creates a cam_eb entry
37688b8a9b1dSJustin T. Gibbs  * for this new bus and places it in the array of busses and assigns
37698b8a9b1dSJustin T. Gibbs  * it a path_id.  The path_id may be influenced by "hard wiring"
37708b8a9b1dSJustin T. Gibbs  * information specified by the user.  Once interrupt services are
377102caf36eSEdward Tomasz Napierala  * available, the bus will be probed.
37728b8a9b1dSJustin T. Gibbs  */
37738b8a9b1dSJustin T. Gibbs int32_t
3774b50569b7SScott Long xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
37758b8a9b1dSJustin T. Gibbs {
37768b8a9b1dSJustin T. Gibbs 	struct cam_eb *new_bus;
3777434bbf6eSJustin T. Gibbs 	struct cam_eb *old_bus;
37788b8a9b1dSJustin T. Gibbs 	struct ccb_pathinq cpi;
377983c5d981SAlexander Motin 	struct cam_path *path;
378052c9ce25SScott Long 	cam_status status;
37818b8a9b1dSJustin T. Gibbs 
37822b83592fSScott Long 	mtx_assert(sim->mtx, MA_OWNED);
378368153f43SScott Long 
37848b8a9b1dSJustin T. Gibbs 	sim->bus_id = bus;
37858b8a9b1dSJustin T. Gibbs 	new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3786362abc44STai-hwa Liang 					  M_CAMXPT, M_NOWAIT);
37878b8a9b1dSJustin T. Gibbs 	if (new_bus == NULL) {
37888b8a9b1dSJustin T. Gibbs 		/* Couldn't satisfy request */
37898b8a9b1dSJustin T. Gibbs 		return (CAM_RESRC_UNAVAIL);
37908b8a9b1dSJustin T. Gibbs 	}
37918b8a9b1dSJustin T. Gibbs 	if (strcmp(sim->sim_name, "xpt") != 0) {
3792434bbf6eSJustin T. Gibbs 		sim->path_id =
3793434bbf6eSJustin T. Gibbs 		    xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
37948b8a9b1dSJustin T. Gibbs 	}
37958b8a9b1dSJustin T. Gibbs 
3796434bbf6eSJustin T. Gibbs 	TAILQ_INIT(&new_bus->et_entries);
37978b8a9b1dSJustin T. Gibbs 	new_bus->path_id = sim->path_id;
3798fa6099fdSEdward Tomasz Napierala 	cam_sim_hold(sim);
37998b8a9b1dSJustin T. Gibbs 	new_bus->sim = sim;
380087cfaf0eSJustin T. Gibbs 	timevalclear(&new_bus->last_reset);
3801434bbf6eSJustin T. Gibbs 	new_bus->flags = 0;
3802a5479bc5SJustin T. Gibbs 	new_bus->refcount = 1;	/* Held until a bus_deregister event */
3803434bbf6eSJustin T. Gibbs 	new_bus->generation = 0;
380452c9ce25SScott Long 
38059a7c2696SAlexander Motin 	xpt_lock_buses();
38062b83592fSScott Long 	old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3807434bbf6eSJustin T. Gibbs 	while (old_bus != NULL
3808434bbf6eSJustin T. Gibbs 	    && old_bus->path_id < new_bus->path_id)
3809434bbf6eSJustin T. Gibbs 		old_bus = TAILQ_NEXT(old_bus, links);
3810434bbf6eSJustin T. Gibbs 	if (old_bus != NULL)
3811434bbf6eSJustin T. Gibbs 		TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3812434bbf6eSJustin T. Gibbs 	else
38132b83592fSScott Long 		TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
38142b83592fSScott Long 	xsoftc.bus_generation++;
38159a7c2696SAlexander Motin 	xpt_unlock_buses();
38168b8a9b1dSJustin T. Gibbs 
381752c9ce25SScott Long 	/*
381852c9ce25SScott Long 	 * Set a default transport so that a PATH_INQ can be issued to
381952c9ce25SScott Long 	 * the SIM.  This will then allow for probing and attaching of
382052c9ce25SScott Long 	 * a more appropriate transport.
382152c9ce25SScott Long 	 */
382252c9ce25SScott Long 	new_bus->xport = &xport_default;
38238b8a9b1dSJustin T. Gibbs 
382432aa80a6SAlexander Motin 	status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
38258b8a9b1dSJustin T. Gibbs 				  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3826627995dcSAlexander Motin 	if (status != CAM_REQ_CMP) {
3827627995dcSAlexander Motin 		xpt_release_bus(new_bus);
3828627995dcSAlexander Motin 		free(path, M_CAMXPT);
3829627995dcSAlexander Motin 		return (CAM_RESRC_UNAVAIL);
3830627995dcSAlexander Motin 	}
383152c9ce25SScott Long 
383283c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
38338b8a9b1dSJustin T. Gibbs 	cpi.ccb_h.func_code = XPT_PATH_INQ;
38348b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&cpi);
383552c9ce25SScott Long 
383652c9ce25SScott Long 	if (cpi.ccb_h.status == CAM_REQ_CMP) {
383752c9ce25SScott Long 		switch (cpi.transport) {
383852c9ce25SScott Long 		case XPORT_SPI:
383952c9ce25SScott Long 		case XPORT_SAS:
384052c9ce25SScott Long 		case XPORT_FC:
384152c9ce25SScott Long 		case XPORT_USB:
384233ea30feSAlexander Motin 		case XPORT_ISCSI:
384333ea30feSAlexander Motin 		case XPORT_PPB:
384452c9ce25SScott Long 			new_bus->xport = scsi_get_xport();
384552c9ce25SScott Long 			break;
384652c9ce25SScott Long 		case XPORT_ATA:
384752c9ce25SScott Long 		case XPORT_SATA:
384852c9ce25SScott Long 			new_bus->xport = ata_get_xport();
384952c9ce25SScott Long 			break;
385052c9ce25SScott Long 		default:
385152c9ce25SScott Long 			new_bus->xport = &xport_default;
385252c9ce25SScott Long 			break;
38538b8a9b1dSJustin T. Gibbs 		}
385452c9ce25SScott Long 	}
385552c9ce25SScott Long 
385652c9ce25SScott Long 	/* Notify interested parties */
385752c9ce25SScott Long 	if (sim->path_id != CAM_XPT_PATH_ID) {
385883c5d981SAlexander Motin 
385983c5d981SAlexander Motin 		xpt_async(AC_PATH_REGISTERED, path, &cpi);
3860b01773b0SKenneth D. Merry 		if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
3861b01773b0SKenneth D. Merry 			union	ccb *scan_ccb;
3862b01773b0SKenneth D. Merry 
386383c5d981SAlexander Motin 			/* Initiate bus rescan. */
386483c5d981SAlexander Motin 			scan_ccb = xpt_alloc_ccb_nowait();
3865e5736ac8SAlexander Motin 			if (scan_ccb != NULL) {
386683c5d981SAlexander Motin 				scan_ccb->ccb_h.path = path;
386783c5d981SAlexander Motin 				scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
386883c5d981SAlexander Motin 				scan_ccb->crcn.flags = 0;
386983c5d981SAlexander Motin 				xpt_rescan(scan_ccb);
387083c5d981SAlexander Motin 			} else
3871b01773b0SKenneth D. Merry 				xpt_print(path,
3872b01773b0SKenneth D. Merry 					  "Can't allocate CCB to scan bus\n");
3873b01773b0SKenneth D. Merry 		} else
3874b01773b0SKenneth D. Merry 			xpt_free_path(path);
3875e5736ac8SAlexander Motin 	} else
387683c5d981SAlexander Motin 		xpt_free_path(path);
38778b8a9b1dSJustin T. Gibbs 	return (CAM_SUCCESS);
38788b8a9b1dSJustin T. Gibbs }
38798b8a9b1dSJustin T. Gibbs 
3880434bbf6eSJustin T. Gibbs int32_t
3881434bbf6eSJustin T. Gibbs xpt_bus_deregister(path_id_t pathid)
38828b8a9b1dSJustin T. Gibbs {
3883434bbf6eSJustin T. Gibbs 	struct cam_path bus_path;
3884434bbf6eSJustin T. Gibbs 	cam_status status;
3885434bbf6eSJustin T. Gibbs 
3886434bbf6eSJustin T. Gibbs 	status = xpt_compile_path(&bus_path, NULL, pathid,
3887434bbf6eSJustin T. Gibbs 				  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3888434bbf6eSJustin T. Gibbs 	if (status != CAM_REQ_CMP)
3889434bbf6eSJustin T. Gibbs 		return (status);
3890434bbf6eSJustin T. Gibbs 
3891434bbf6eSJustin T. Gibbs 	xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3892434bbf6eSJustin T. Gibbs 	xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3893434bbf6eSJustin T. Gibbs 
3894434bbf6eSJustin T. Gibbs 	/* Release the reference count held while registered. */
3895434bbf6eSJustin T. Gibbs 	xpt_release_bus(bus_path.bus);
3896434bbf6eSJustin T. Gibbs 	xpt_release_path(&bus_path);
3897434bbf6eSJustin T. Gibbs 
3898434bbf6eSJustin T. Gibbs 	return (CAM_REQ_CMP);
3899434bbf6eSJustin T. Gibbs }
3900434bbf6eSJustin T. Gibbs 
3901434bbf6eSJustin T. Gibbs static path_id_t
3902434bbf6eSJustin T. Gibbs xptnextfreepathid(void)
3903434bbf6eSJustin T. Gibbs {
3904434bbf6eSJustin T. Gibbs 	struct cam_eb *bus;
3905434bbf6eSJustin T. Gibbs 	path_id_t pathid;
39062398f0cdSPeter Wemm 	const char *strval;
39078b8a9b1dSJustin T. Gibbs 
3908434bbf6eSJustin T. Gibbs 	pathid = 0;
39099a7c2696SAlexander Motin 	xpt_lock_buses();
39102b83592fSScott Long 	bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3911434bbf6eSJustin T. Gibbs retry:
3912434bbf6eSJustin T. Gibbs 	/* Find an unoccupied pathid */
39139e6461a2SMatt Jacob 	while (bus != NULL && bus->path_id <= pathid) {
3914434bbf6eSJustin T. Gibbs 		if (bus->path_id == pathid)
3915434bbf6eSJustin T. Gibbs 			pathid++;
3916434bbf6eSJustin T. Gibbs 		bus = TAILQ_NEXT(bus, links);
3917434bbf6eSJustin T. Gibbs 	}
39189a7c2696SAlexander Motin 	xpt_unlock_buses();
3919434bbf6eSJustin T. Gibbs 
3920434bbf6eSJustin T. Gibbs 	/*
3921434bbf6eSJustin T. Gibbs 	 * Ensure that this pathid is not reserved for
3922434bbf6eSJustin T. Gibbs 	 * a bus that may be registered in the future.
3923434bbf6eSJustin T. Gibbs 	 */
392475f51904SPeter Wemm 	if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
3925434bbf6eSJustin T. Gibbs 		++pathid;
39268b8a9b1dSJustin T. Gibbs 		/* Start the search over */
39279a7c2696SAlexander Motin 		xpt_lock_buses();
3928434bbf6eSJustin T. Gibbs 		goto retry;
39298b8a9b1dSJustin T. Gibbs 	}
3930434bbf6eSJustin T. Gibbs 	return (pathid);
39318b8a9b1dSJustin T. Gibbs }
39328b8a9b1dSJustin T. Gibbs 
3933434bbf6eSJustin T. Gibbs static path_id_t
3934434bbf6eSJustin T. Gibbs xptpathid(const char *sim_name, int sim_unit, int sim_bus)
39358b8a9b1dSJustin T. Gibbs {
39368b8a9b1dSJustin T. Gibbs 	path_id_t pathid;
393775f51904SPeter Wemm 	int i, dunit, val;
3938642f0c46SPeter Wemm 	char buf[32];
39392398f0cdSPeter Wemm 	const char *dname;
39408b8a9b1dSJustin T. Gibbs 
39418b8a9b1dSJustin T. Gibbs 	pathid = CAM_XPT_PATH_ID;
394275f51904SPeter Wemm 	snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
39432398f0cdSPeter Wemm 	i = 0;
39442398f0cdSPeter Wemm 	while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
39452398f0cdSPeter Wemm 		if (strcmp(dname, "scbus")) {
3946642f0c46SPeter Wemm 			/* Avoid a bit of foot shooting. */
3947642f0c46SPeter Wemm 			continue;
3948642f0c46SPeter Wemm 		}
394975f51904SPeter Wemm 		if (dunit < 0)		/* unwired?! */
39508b8a9b1dSJustin T. Gibbs 			continue;
395175f51904SPeter Wemm 		if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
395275f51904SPeter Wemm 			if (sim_bus == val) {
395375f51904SPeter Wemm 				pathid = dunit;
39548b8a9b1dSJustin T. Gibbs 				break;
39558b8a9b1dSJustin T. Gibbs 			}
39568b8a9b1dSJustin T. Gibbs 		} else if (sim_bus == 0) {
39578b8a9b1dSJustin T. Gibbs 			/* Unspecified matches bus 0 */
395875f51904SPeter Wemm 			pathid = dunit;
39598b8a9b1dSJustin T. Gibbs 			break;
39608b8a9b1dSJustin T. Gibbs 		} else {
39618b8a9b1dSJustin T. Gibbs 			printf("Ambiguous scbus configuration for %s%d "
39628b8a9b1dSJustin T. Gibbs 			       "bus %d, cannot wire down.  The kernel "
39638b8a9b1dSJustin T. Gibbs 			       "config entry for scbus%d should "
39648b8a9b1dSJustin T. Gibbs 			       "specify a controller bus.\n"
39658b8a9b1dSJustin T. Gibbs 			       "Scbus will be assigned dynamically.\n",
396675f51904SPeter Wemm 			       sim_name, sim_unit, sim_bus, dunit);
39678b8a9b1dSJustin T. Gibbs 			break;
39688b8a9b1dSJustin T. Gibbs 		}
39698b8a9b1dSJustin T. Gibbs 	}
39708b8a9b1dSJustin T. Gibbs 
3971434bbf6eSJustin T. Gibbs 	if (pathid == CAM_XPT_PATH_ID)
3972434bbf6eSJustin T. Gibbs 		pathid = xptnextfreepathid();
39738b8a9b1dSJustin T. Gibbs 	return (pathid);
39748b8a9b1dSJustin T. Gibbs }
39758b8a9b1dSJustin T. Gibbs 
397622c7d606SAlexander Motin static const char *
397722c7d606SAlexander Motin xpt_async_string(u_int32_t async_code)
397822c7d606SAlexander Motin {
397922c7d606SAlexander Motin 
398022c7d606SAlexander Motin 	switch (async_code) {
398122c7d606SAlexander Motin 	case AC_BUS_RESET: return ("AC_BUS_RESET");
398222c7d606SAlexander Motin 	case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
398322c7d606SAlexander Motin 	case AC_SCSI_AEN: return ("AC_SCSI_AEN");
398422c7d606SAlexander Motin 	case AC_SENT_BDR: return ("AC_SENT_BDR");
398522c7d606SAlexander Motin 	case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
398622c7d606SAlexander Motin 	case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
398722c7d606SAlexander Motin 	case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
398822c7d606SAlexander Motin 	case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
398922c7d606SAlexander Motin 	case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
399022c7d606SAlexander Motin 	case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
399122c7d606SAlexander Motin 	case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
399222c7d606SAlexander Motin 	case AC_CONTRACT: return ("AC_CONTRACT");
399322c7d606SAlexander Motin 	case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
39943631c638SAlexander Motin 	case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
399522c7d606SAlexander Motin 	}
399622c7d606SAlexander Motin 	return ("AC_UNKNOWN");
399722c7d606SAlexander Motin }
399822c7d606SAlexander Motin 
39998b8a9b1dSJustin T. Gibbs void
40008b8a9b1dSJustin T. Gibbs xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
40018b8a9b1dSJustin T. Gibbs {
40028b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus;
40038b8a9b1dSJustin T. Gibbs 	struct cam_et *target, *next_target;
40048b8a9b1dSJustin T. Gibbs 	struct cam_ed *device, *next_device;
40058b8a9b1dSJustin T. Gibbs 
40062b83592fSScott Long 	mtx_assert(path->bus->sim->mtx, MA_OWNED);
400722c7d606SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
400822c7d606SAlexander Motin 	    ("xpt_async(%s)\n", xpt_async_string(async_code)));
40098b8a9b1dSJustin T. Gibbs 
4010a5479bc5SJustin T. Gibbs 	/*
4011a5479bc5SJustin T. Gibbs 	 * Most async events come from a CAM interrupt context.  In
4012a5479bc5SJustin T. Gibbs 	 * a few cases, the error recovery code at the peripheral layer,
4013a5479bc5SJustin T. Gibbs 	 * which may run from our SWI or a process context, may signal
401477dc25ccSScott Long 	 * deferred events with a call to xpt_async.
4015a5479bc5SJustin T. Gibbs 	 */
40168b8a9b1dSJustin T. Gibbs 
40178b8a9b1dSJustin T. Gibbs 	bus = path->bus;
40188b8a9b1dSJustin T. Gibbs 
40198b8a9b1dSJustin T. Gibbs 	if (async_code == AC_BUS_RESET) {
402087cfaf0eSJustin T. Gibbs 		/* Update our notion of when the last reset occurred */
402187cfaf0eSJustin T. Gibbs 		microtime(&bus->last_reset);
40228b8a9b1dSJustin T. Gibbs 	}
40238b8a9b1dSJustin T. Gibbs 
40248b8a9b1dSJustin T. Gibbs 	for (target = TAILQ_FIRST(&bus->et_entries);
40258b8a9b1dSJustin T. Gibbs 	     target != NULL;
40268b8a9b1dSJustin T. Gibbs 	     target = next_target) {
40278b8a9b1dSJustin T. Gibbs 
40288b8a9b1dSJustin T. Gibbs 		next_target = TAILQ_NEXT(target, links);
40298b8a9b1dSJustin T. Gibbs 
40308b8a9b1dSJustin T. Gibbs 		if (path->target != target
40312f22d08dSJustin T. Gibbs 		 && path->target->target_id != CAM_TARGET_WILDCARD
40322f22d08dSJustin T. Gibbs 		 && target->target_id != CAM_TARGET_WILDCARD)
40338b8a9b1dSJustin T. Gibbs 			continue;
40348b8a9b1dSJustin T. Gibbs 
403587cfaf0eSJustin T. Gibbs 		if (async_code == AC_SENT_BDR) {
403687cfaf0eSJustin T. Gibbs 			/* Update our notion of when the last reset occurred */
403787cfaf0eSJustin T. Gibbs 			microtime(&path->target->last_reset);
403887cfaf0eSJustin T. Gibbs 		}
403987cfaf0eSJustin T. Gibbs 
40408b8a9b1dSJustin T. Gibbs 		for (device = TAILQ_FIRST(&target->ed_entries);
40418b8a9b1dSJustin T. Gibbs 		     device != NULL;
40428b8a9b1dSJustin T. Gibbs 		     device = next_device) {
40438b8a9b1dSJustin T. Gibbs 
40448b8a9b1dSJustin T. Gibbs 			next_device = TAILQ_NEXT(device, links);
40458b8a9b1dSJustin T. Gibbs 
40468b8a9b1dSJustin T. Gibbs 			if (path->device != device
40472f22d08dSJustin T. Gibbs 			 && path->device->lun_id != CAM_LUN_WILDCARD
40482f22d08dSJustin T. Gibbs 			 && device->lun_id != CAM_LUN_WILDCARD)
40498b8a9b1dSJustin T. Gibbs 				continue;
40509efda2c9SAlexander Motin 			/*
40519efda2c9SAlexander Motin 			 * The async callback could free the device.
40529efda2c9SAlexander Motin 			 * If it is a broadcast async, it doesn't hold
40539efda2c9SAlexander Motin 			 * device reference, so take our own reference.
40549efda2c9SAlexander Motin 			 */
40559efda2c9SAlexander Motin 			xpt_acquire_device(device);
405652c9ce25SScott Long 			(*(bus->xport->async))(async_code, bus,
405752c9ce25SScott Long 					       target, device,
405852c9ce25SScott Long 					       async_arg);
40598b8a9b1dSJustin T. Gibbs 
40602f22d08dSJustin T. Gibbs 			xpt_async_bcast(&device->asyncs, async_code,
40612f22d08dSJustin T. Gibbs 					path, async_arg);
40629efda2c9SAlexander Motin 			xpt_release_device(device);
40638b8a9b1dSJustin T. Gibbs 		}
40648b8a9b1dSJustin T. Gibbs 	}
4065c8bead2aSJustin T. Gibbs 
4066c8bead2aSJustin T. Gibbs 	/*
4067c8bead2aSJustin T. Gibbs 	 * If this wasn't a fully wildcarded async, tell all
4068c8bead2aSJustin T. Gibbs 	 * clients that want all async events.
4069c8bead2aSJustin T. Gibbs 	 */
4070c8bead2aSJustin T. Gibbs 	if (bus != xpt_periph->path->bus)
4071c8bead2aSJustin T. Gibbs 		xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
40728b8a9b1dSJustin T. Gibbs 				path, async_arg);
40738b8a9b1dSJustin T. Gibbs }
40748b8a9b1dSJustin T. Gibbs 
40758b8a9b1dSJustin T. Gibbs static void
40768b8a9b1dSJustin T. Gibbs xpt_async_bcast(struct async_list *async_head,
40778b8a9b1dSJustin T. Gibbs 		u_int32_t async_code,
40788b8a9b1dSJustin T. Gibbs 		struct cam_path *path, void *async_arg)
40798b8a9b1dSJustin T. Gibbs {
40808b8a9b1dSJustin T. Gibbs 	struct async_node *cur_entry;
40818b8a9b1dSJustin T. Gibbs 
40828b8a9b1dSJustin T. Gibbs 	cur_entry = SLIST_FIRST(async_head);
40838b8a9b1dSJustin T. Gibbs 	while (cur_entry != NULL) {
40848b8a9b1dSJustin T. Gibbs 		struct async_node *next_entry;
40858b8a9b1dSJustin T. Gibbs 		/*
40868b8a9b1dSJustin T. Gibbs 		 * Grab the next list entry before we call the current
40878b8a9b1dSJustin T. Gibbs 		 * entry's callback.  This is because the callback function
40888b8a9b1dSJustin T. Gibbs 		 * can delete its async callback entry.
40898b8a9b1dSJustin T. Gibbs 		 */
40908b8a9b1dSJustin T. Gibbs 		next_entry = SLIST_NEXT(cur_entry, links);
40918b8a9b1dSJustin T. Gibbs 		if ((cur_entry->event_enable & async_code) != 0)
40928b8a9b1dSJustin T. Gibbs 			cur_entry->callback(cur_entry->callback_arg,
40938b8a9b1dSJustin T. Gibbs 					    async_code, path,
40948b8a9b1dSJustin T. Gibbs 					    async_arg);
40958b8a9b1dSJustin T. Gibbs 		cur_entry = next_entry;
40968b8a9b1dSJustin T. Gibbs 	}
40978b8a9b1dSJustin T. Gibbs }
40988b8a9b1dSJustin T. Gibbs 
40992f22d08dSJustin T. Gibbs static void
410052c9ce25SScott Long xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
410152c9ce25SScott Long 		      struct cam_et *target, struct cam_ed *device,
410252c9ce25SScott Long 		      void *async_arg)
41032f22d08dSJustin T. Gibbs {
4104b882a6d3SMatt Jacob 	printf("%s called\n", __func__);
41052f22d08dSJustin T. Gibbs }
41062f22d08dSJustin T. Gibbs 
41078b8a9b1dSJustin T. Gibbs u_int32_t
4108cccf4220SAlexander Motin xpt_freeze_devq(struct cam_path *path, u_int count)
410983c5d981SAlexander Motin {
411083c5d981SAlexander Motin 	struct cam_ed *dev = path->device;
411183c5d981SAlexander Motin 
411283c5d981SAlexander Motin 	mtx_assert(path->bus->sim->mtx, MA_OWNED);
4113cccf4220SAlexander Motin 	dev->ccbq.queue.qfrozen_cnt += count;
411483c5d981SAlexander Motin 	/* Remove frozen device from sendq. */
4115cccf4220SAlexander Motin 	if (device_is_queued(dev)) {
411683c5d981SAlexander Motin 		camq_remove(&dev->sim->devq->send_queue,
4117cccf4220SAlexander Motin 		    dev->devq_entry.pinfo.index);
411883c5d981SAlexander Motin 	}
4119cccf4220SAlexander Motin 	return (dev->ccbq.queue.qfrozen_cnt);
41208b8a9b1dSJustin T. Gibbs }
41218b8a9b1dSJustin T. Gibbs 
41228b8a9b1dSJustin T. Gibbs u_int32_t
41238b8a9b1dSJustin T. Gibbs xpt_freeze_simq(struct cam_sim *sim, u_int count)
41248b8a9b1dSJustin T. Gibbs {
4125ec700f26SAlexander Motin 
41262b83592fSScott Long 	mtx_assert(sim->mtx, MA_OWNED);
4127cccf4220SAlexander Motin 	sim->devq->send_queue.qfrozen_cnt += count;
4128cccf4220SAlexander Motin 	return (sim->devq->send_queue.qfrozen_cnt);
41298b8a9b1dSJustin T. Gibbs }
41308b8a9b1dSJustin T. Gibbs 
41318b8a9b1dSJustin T. Gibbs static void
41328b8a9b1dSJustin T. Gibbs xpt_release_devq_timeout(void *arg)
41338b8a9b1dSJustin T. Gibbs {
41348b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
41358b8a9b1dSJustin T. Gibbs 
41368b8a9b1dSJustin T. Gibbs 	device = (struct cam_ed *)arg;
4137cccf4220SAlexander Motin 	xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
41388b8a9b1dSJustin T. Gibbs }
41398b8a9b1dSJustin T. Gibbs 
41408b8a9b1dSJustin T. Gibbs void
41412cefde5fSJustin T. Gibbs xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
41422cefde5fSJustin T. Gibbs {
414368153f43SScott Long 
4144cccf4220SAlexander Motin 	mtx_assert(path->bus->sim->mtx, MA_OWNED);
4145cccf4220SAlexander Motin 	xpt_release_devq_device(path->device, count, run_queue);
414683c5d981SAlexander Motin }
414783c5d981SAlexander Motin 
414883c5d981SAlexander Motin void
4149cccf4220SAlexander Motin xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
41508b8a9b1dSJustin T. Gibbs {
41518b8a9b1dSJustin T. Gibbs 
4152cccf4220SAlexander Motin 	if (count > dev->ccbq.queue.qfrozen_cnt) {
415383c5d981SAlexander Motin #ifdef INVARIANTS
4154cccf4220SAlexander Motin 		printf("xpt_release_devq(): requested %u > present %u\n",
4155cccf4220SAlexander Motin 		    count, dev->ccbq.queue.qfrozen_cnt);
415683c5d981SAlexander Motin #endif
4157cccf4220SAlexander Motin 		count = dev->ccbq.queue.qfrozen_cnt;
415883c5d981SAlexander Motin 	}
4159cccf4220SAlexander Motin 	dev->ccbq.queue.qfrozen_cnt -= count;
4160cccf4220SAlexander Motin 	if (dev->ccbq.queue.qfrozen_cnt == 0) {
41618b8a9b1dSJustin T. Gibbs 		/*
41628b8a9b1dSJustin T. Gibbs 		 * No longer need to wait for a successful
41638b8a9b1dSJustin T. Gibbs 		 * command completion.
41648b8a9b1dSJustin T. Gibbs 		 */
41658b8a9b1dSJustin T. Gibbs 		dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
41668b8a9b1dSJustin T. Gibbs 		/*
41678b8a9b1dSJustin T. Gibbs 		 * Remove any timeouts that might be scheduled
41688b8a9b1dSJustin T. Gibbs 		 * to release this queue.
41698b8a9b1dSJustin T. Gibbs 		 */
41708b8a9b1dSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
41712b83592fSScott Long 			callout_stop(&dev->callout);
41728b8a9b1dSJustin T. Gibbs 			dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
41738b8a9b1dSJustin T. Gibbs 		}
4174cccf4220SAlexander Motin 		xpt_run_dev_allocq(dev);
417583c5d981SAlexander Motin 		if (run_queue == 0)
417683c5d981SAlexander Motin 			return;
41778b8a9b1dSJustin T. Gibbs 		/*
41788b8a9b1dSJustin T. Gibbs 		 * Now that we are unfrozen schedule the
41798b8a9b1dSJustin T. Gibbs 		 * device so any pending transactions are
41808b8a9b1dSJustin T. Gibbs 		 * run.
41818b8a9b1dSJustin T. Gibbs 		 */
4182cccf4220SAlexander Motin 		if (xpt_schedule_devq(dev->sim->devq, dev))
4183cccf4220SAlexander Motin 			xpt_run_devq(dev->sim->devq);
41848b8a9b1dSJustin T. Gibbs 	}
418583c5d981SAlexander Motin }
41868b8a9b1dSJustin T. Gibbs 
41878b8a9b1dSJustin T. Gibbs void
41888b8a9b1dSJustin T. Gibbs xpt_release_simq(struct cam_sim *sim, int run_queue)
41898b8a9b1dSJustin T. Gibbs {
41908b8a9b1dSJustin T. Gibbs 	struct	camq *sendq;
41918b8a9b1dSJustin T. Gibbs 
41922b83592fSScott Long 	mtx_assert(sim->mtx, MA_OWNED);
41938b8a9b1dSJustin T. Gibbs 	sendq = &(sim->devq->send_queue);
4194cccf4220SAlexander Motin 	if (sendq->qfrozen_cnt <= 0) {
419583c5d981SAlexander Motin #ifdef INVARIANTS
419683c5d981SAlexander Motin 		printf("xpt_release_simq: requested 1 > present %u\n",
4197cccf4220SAlexander Motin 		    sendq->qfrozen_cnt);
419883c5d981SAlexander Motin #endif
419983c5d981SAlexander Motin 	} else
4200cccf4220SAlexander Motin 		sendq->qfrozen_cnt--;
4201cccf4220SAlexander Motin 	if (sendq->qfrozen_cnt == 0) {
42028b8a9b1dSJustin T. Gibbs 		/*
42038b8a9b1dSJustin T. Gibbs 		 * If there is a timeout scheduled to release this
42048b8a9b1dSJustin T. Gibbs 		 * sim queue, remove it.  The queue frozen count is
42058b8a9b1dSJustin T. Gibbs 		 * already at 0.
42068b8a9b1dSJustin T. Gibbs 		 */
42078b8a9b1dSJustin T. Gibbs 		if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
42082b83592fSScott Long 			callout_stop(&sim->callout);
42098b8a9b1dSJustin T. Gibbs 			sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
42108b8a9b1dSJustin T. Gibbs 		}
42118b8a9b1dSJustin T. Gibbs 		if (run_queue) {
42128b8a9b1dSJustin T. Gibbs 			/*
42138b8a9b1dSJustin T. Gibbs 			 * Now that we are unfrozen run the send queue.
42148b8a9b1dSJustin T. Gibbs 			 */
4215cccf4220SAlexander Motin 			xpt_run_devq(sim->devq);
421677dc25ccSScott Long 		}
421777dc25ccSScott Long 	}
42188b8a9b1dSJustin T. Gibbs }
42198b8a9b1dSJustin T. Gibbs 
42202b83592fSScott Long /*
42212b83592fSScott Long  * XXX Appears to be unused.
42222b83592fSScott Long  */
42238b8a9b1dSJustin T. Gibbs static void
42248b8a9b1dSJustin T. Gibbs xpt_release_simq_timeout(void *arg)
42258b8a9b1dSJustin T. Gibbs {
42268b8a9b1dSJustin T. Gibbs 	struct cam_sim *sim;
42278b8a9b1dSJustin T. Gibbs 
42288b8a9b1dSJustin T. Gibbs 	sim = (struct cam_sim *)arg;
42298b8a9b1dSJustin T. Gibbs 	xpt_release_simq(sim, /* run_queue */ TRUE);
42308b8a9b1dSJustin T. Gibbs }
42318b8a9b1dSJustin T. Gibbs 
42328b8a9b1dSJustin T. Gibbs void
4233a5479bc5SJustin T. Gibbs xpt_done(union ccb *done_ccb)
42348b8a9b1dSJustin T. Gibbs {
42359758cc83SScott Long 	struct cam_sim *sim;
4236efa575b6SAlexander Motin 	int	first;
42378b8a9b1dSJustin T. Gibbs 
42388b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
42399deea857SKenneth D. Merry 	if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
42408b8a9b1dSJustin T. Gibbs 		/*
42418b8a9b1dSJustin T. Gibbs 		 * Queue up the request for handling by our SWI handler
42428b8a9b1dSJustin T. Gibbs 		 * any of the "non-immediate" type of ccbs.
42438b8a9b1dSJustin T. Gibbs 		 */
42449758cc83SScott Long 		sim = done_ccb->ccb_h.path->bus->sim;
42459758cc83SScott Long 		TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h,
42468b8a9b1dSJustin T. Gibbs 		    sim_links.tqe);
42478b8a9b1dSJustin T. Gibbs 		done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4248711f6613SAlexander Motin 		if ((sim->flags & (CAM_SIM_ON_DONEQ | CAM_SIM_POLLED |
4249711f6613SAlexander Motin 		    CAM_SIM_BATCH)) == 0) {
42509758cc83SScott Long 			mtx_lock(&cam_simq_lock);
4251efa575b6SAlexander Motin 			first = TAILQ_EMPTY(&cam_simq);
4252efa575b6SAlexander Motin 			TAILQ_INSERT_TAIL(&cam_simq, sim, links);
42539758cc83SScott Long 			mtx_unlock(&cam_simq_lock);
4254788fb376SAlexander Motin 			sim->flags |= CAM_SIM_ON_DONEQ;
4255efa575b6SAlexander Motin 			if (first)
4256c86b6ff5SJohn Baldwin 				swi_sched(cambio_ih, 0);
4257788fb376SAlexander Motin 		}
42588b8a9b1dSJustin T. Gibbs 	}
42598b8a9b1dSJustin T. Gibbs }
42608b8a9b1dSJustin T. Gibbs 
4261711f6613SAlexander Motin void
4262711f6613SAlexander Motin xpt_batch_start(struct cam_sim *sim)
4263711f6613SAlexander Motin {
4264711f6613SAlexander Motin 
4265711f6613SAlexander Motin 	KASSERT((sim->flags & CAM_SIM_BATCH) == 0, ("Batch flag already set"));
4266711f6613SAlexander Motin 	sim->flags |= CAM_SIM_BATCH;
4267711f6613SAlexander Motin }
4268711f6613SAlexander Motin 
4269711f6613SAlexander Motin void
4270711f6613SAlexander Motin xpt_batch_done(struct cam_sim *sim)
4271711f6613SAlexander Motin {
4272711f6613SAlexander Motin 
4273711f6613SAlexander Motin 	KASSERT((sim->flags & CAM_SIM_BATCH) != 0, ("Batch flag was not set"));
4274711f6613SAlexander Motin 	sim->flags &= ~CAM_SIM_BATCH;
4275711f6613SAlexander Motin 	if (!TAILQ_EMPTY(&sim->sim_doneq) &&
4276711f6613SAlexander Motin 	    (sim->flags & CAM_SIM_ON_DONEQ) == 0)
427739fe6d85SAlexander Motin 		camisr_runqueue(sim);
4278711f6613SAlexander Motin }
4279711f6613SAlexander Motin 
42808b8a9b1dSJustin T. Gibbs union ccb *
42818008a935SScott Long xpt_alloc_ccb()
42828b8a9b1dSJustin T. Gibbs {
42838b8a9b1dSJustin T. Gibbs 	union ccb *new_ccb;
42848b8a9b1dSJustin T. Gibbs 
4285596ee08fSAlexander Motin 	new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4286362abc44STai-hwa Liang 	return (new_ccb);
4287362abc44STai-hwa Liang }
4288362abc44STai-hwa Liang 
4289362abc44STai-hwa Liang union ccb *
42908008a935SScott Long xpt_alloc_ccb_nowait()
4291362abc44STai-hwa Liang {
4292362abc44STai-hwa Liang 	union ccb *new_ccb;
4293362abc44STai-hwa Liang 
4294596ee08fSAlexander Motin 	new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
42958b8a9b1dSJustin T. Gibbs 	return (new_ccb);
42968b8a9b1dSJustin T. Gibbs }
42978b8a9b1dSJustin T. Gibbs 
42988b8a9b1dSJustin T. Gibbs void
42998b8a9b1dSJustin T. Gibbs xpt_free_ccb(union ccb *free_ccb)
43008b8a9b1dSJustin T. Gibbs {
4301596ee08fSAlexander Motin 	free(free_ccb, M_CAMCCB);
43028b8a9b1dSJustin T. Gibbs }
43038b8a9b1dSJustin T. Gibbs 
43048b8a9b1dSJustin T. Gibbs 
43058b8a9b1dSJustin T. Gibbs 
43068b8a9b1dSJustin T. Gibbs /* Private XPT functions */
43078b8a9b1dSJustin T. Gibbs 
43088b8a9b1dSJustin T. Gibbs /*
43098b8a9b1dSJustin T. Gibbs  * Get a CAM control block for the caller. Charge the structure to the device
43108b8a9b1dSJustin T. Gibbs  * referenced by the path.  If the this device has no 'credits' then the
43118b8a9b1dSJustin T. Gibbs  * device already has the maximum number of outstanding operations under way
43128b8a9b1dSJustin T. Gibbs  * and we return NULL. If we don't have sufficient resources to allocate more
43138b8a9b1dSJustin T. Gibbs  * ccbs, we also return NULL.
43148b8a9b1dSJustin T. Gibbs  */
43158b8a9b1dSJustin T. Gibbs static union ccb *
43168b8a9b1dSJustin T. Gibbs xpt_get_ccb(struct cam_ed *device)
43178b8a9b1dSJustin T. Gibbs {
43188b8a9b1dSJustin T. Gibbs 	union ccb *new_ccb;
43192b83592fSScott Long 	struct cam_sim *sim;
43208b8a9b1dSJustin T. Gibbs 
43212b83592fSScott Long 	sim = device->sim;
43222b83592fSScott Long 	if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) {
43238008a935SScott Long 		new_ccb = xpt_alloc_ccb_nowait();
43248b8a9b1dSJustin T. Gibbs                 if (new_ccb == NULL) {
43258b8a9b1dSJustin T. Gibbs 			return (NULL);
43268b8a9b1dSJustin T. Gibbs 		}
43278008a935SScott Long 		if ((sim->flags & CAM_SIM_MPSAFE) == 0)
43288008a935SScott Long 			callout_handle_init(&new_ccb->ccb_h.timeout_ch);
43292b83592fSScott Long 		SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h,
43308b8a9b1dSJustin T. Gibbs 				  xpt_links.sle);
43312b83592fSScott Long 		sim->ccb_count++;
43328b8a9b1dSJustin T. Gibbs 	}
43338b8a9b1dSJustin T. Gibbs 	cam_ccbq_take_opening(&device->ccbq);
43342b83592fSScott Long 	SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
43358b8a9b1dSJustin T. Gibbs 	return (new_ccb);
43368b8a9b1dSJustin T. Gibbs }
43378b8a9b1dSJustin T. Gibbs 
4338a5479bc5SJustin T. Gibbs static void
4339a5479bc5SJustin T. Gibbs xpt_release_bus(struct cam_eb *bus)
4340a5479bc5SJustin T. Gibbs {
4341a5479bc5SJustin T. Gibbs 
43429a7c2696SAlexander Motin 	xpt_lock_buses();
434315975b7bSMatt Jacob 	KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4344dcdf6e74SAlexander Motin 	if (--bus->refcount > 0) {
4345dcdf6e74SAlexander Motin 		xpt_unlock_buses();
4346dcdf6e74SAlexander Motin 		return;
4347dcdf6e74SAlexander Motin 	}
4348dcdf6e74SAlexander Motin 	KASSERT(TAILQ_EMPTY(&bus->et_entries),
4349dcdf6e74SAlexander Motin 	    ("refcount is zero, but target list is not empty"));
43502b83592fSScott Long 	TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
43512b83592fSScott Long 	xsoftc.bus_generation++;
43529a7c2696SAlexander Motin 	xpt_unlock_buses();
4353fa6099fdSEdward Tomasz Napierala 	cam_sim_release(bus->sim);
4354362abc44STai-hwa Liang 	free(bus, M_CAMXPT);
4355a5479bc5SJustin T. Gibbs }
43568b8a9b1dSJustin T. Gibbs 
43578b8a9b1dSJustin T. Gibbs static struct cam_et *
43588b8a9b1dSJustin T. Gibbs xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
43598b8a9b1dSJustin T. Gibbs {
4360dcdf6e74SAlexander Motin 	struct cam_et *cur_target, *target;
43618b8a9b1dSJustin T. Gibbs 
4362dcdf6e74SAlexander Motin 	mtx_assert(bus->sim->mtx, MA_OWNED);
43633501942bSJustin T. Gibbs 	target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
43643501942bSJustin T. Gibbs 					 M_NOWAIT|M_ZERO);
4365dcdf6e74SAlexander Motin 	if (target == NULL)
4366dcdf6e74SAlexander Motin 		return (NULL);
43678b8a9b1dSJustin T. Gibbs 
4368434bbf6eSJustin T. Gibbs 	TAILQ_INIT(&target->ed_entries);
43698b8a9b1dSJustin T. Gibbs 	target->bus = bus;
43708b8a9b1dSJustin T. Gibbs 	target->target_id = target_id;
43718b8a9b1dSJustin T. Gibbs 	target->refcount = 1;
4372434bbf6eSJustin T. Gibbs 	target->generation = 0;
437382b361b1SMatt Jacob 	target->luns = NULL;
4374434bbf6eSJustin T. Gibbs 	timevalclear(&target->last_reset);
4375a5479bc5SJustin T. Gibbs 	/*
4376a5479bc5SJustin T. Gibbs 	 * Hold a reference to our parent bus so it
4377a5479bc5SJustin T. Gibbs 	 * will not go away before we do.
4378a5479bc5SJustin T. Gibbs 	 */
43799a7c2696SAlexander Motin 	xpt_lock_buses();
4380a5479bc5SJustin T. Gibbs 	bus->refcount++;
43819a7c2696SAlexander Motin 	xpt_unlock_buses();
43828b8a9b1dSJustin T. Gibbs 
43838b8a9b1dSJustin T. Gibbs 	/* Insertion sort into our bus's target list */
43848b8a9b1dSJustin T. Gibbs 	cur_target = TAILQ_FIRST(&bus->et_entries);
43858b8a9b1dSJustin T. Gibbs 	while (cur_target != NULL && cur_target->target_id < target_id)
43868b8a9b1dSJustin T. Gibbs 		cur_target = TAILQ_NEXT(cur_target, links);
43878b8a9b1dSJustin T. Gibbs 	if (cur_target != NULL) {
43888b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_BEFORE(cur_target, target, links);
43898b8a9b1dSJustin T. Gibbs 	} else {
43908b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
43918b8a9b1dSJustin T. Gibbs 	}
4392a5479bc5SJustin T. Gibbs 	bus->generation++;
43938b8a9b1dSJustin T. Gibbs 	return (target);
43948b8a9b1dSJustin T. Gibbs }
43958b8a9b1dSJustin T. Gibbs 
4396a5479bc5SJustin T. Gibbs static void
4397f98d7a47SAlexander Motin xpt_release_target(struct cam_et *target)
43988b8a9b1dSJustin T. Gibbs {
4399a5479bc5SJustin T. Gibbs 
4400dcdf6e74SAlexander Motin 	mtx_assert(target->bus->sim->mtx, MA_OWNED);
4401dcdf6e74SAlexander Motin 	if (--target->refcount > 0)
4402dcdf6e74SAlexander Motin 		return;
4403dcdf6e74SAlexander Motin 	KASSERT(TAILQ_EMPTY(&target->ed_entries),
4404dcdf6e74SAlexander Motin 	    ("refcount is zero, but device list is not empty"));
4405f98d7a47SAlexander Motin 	TAILQ_REMOVE(&target->bus->et_entries, target, links);
4406f98d7a47SAlexander Motin 	target->bus->generation++;
4407f98d7a47SAlexander Motin 	xpt_release_bus(target->bus);
440882b361b1SMatt Jacob 	if (target->luns)
440982b361b1SMatt Jacob 		free(target->luns, M_CAMXPT);
4410362abc44STai-hwa Liang 	free(target, M_CAMXPT);
441177dc25ccSScott Long }
44128b8a9b1dSJustin T. Gibbs 
44138b8a9b1dSJustin T. Gibbs static struct cam_ed *
441452c9ce25SScott Long xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
441552c9ce25SScott Long 			 lun_id_t lun_id)
441652c9ce25SScott Long {
4417dcdf6e74SAlexander Motin 	struct cam_ed *device;
441852c9ce25SScott Long 
441952c9ce25SScott Long 	device = xpt_alloc_device(bus, target, lun_id);
442052c9ce25SScott Long 	if (device == NULL)
442152c9ce25SScott Long 		return (NULL);
442252c9ce25SScott Long 
442352c9ce25SScott Long 	device->mintags = 1;
442452c9ce25SScott Long 	device->maxtags = 1;
442583c5d981SAlexander Motin 	bus->sim->max_ccbs += device->ccbq.devq_openings;
442652c9ce25SScott Long 	return (device);
442752c9ce25SScott Long }
442852c9ce25SScott Long 
442952c9ce25SScott Long struct cam_ed *
44308b8a9b1dSJustin T. Gibbs xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
44318b8a9b1dSJustin T. Gibbs {
4432dcdf6e74SAlexander Motin 	struct cam_ed	*cur_device, *device;
44338b8a9b1dSJustin T. Gibbs 	struct cam_devq	*devq;
4434a5479bc5SJustin T. Gibbs 	cam_status status;
44358b8a9b1dSJustin T. Gibbs 
4436dcdf6e74SAlexander Motin 	mtx_assert(target->bus->sim->mtx, MA_OWNED);
44378b8a9b1dSJustin T. Gibbs 	/* Make space for us in the device queue on our bus */
44388b8a9b1dSJustin T. Gibbs 	devq = bus->sim->devq;
4439cccf4220SAlexander Motin 	status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4440dcdf6e74SAlexander Motin 	if (status != CAM_REQ_CMP)
4441dcdf6e74SAlexander Motin 		return (NULL);
44428b8a9b1dSJustin T. Gibbs 
44438b8a9b1dSJustin T. Gibbs 	device = (struct cam_ed *)malloc(sizeof(*device),
4444596ee08fSAlexander Motin 					 M_CAMDEV, M_NOWAIT|M_ZERO);
4445dcdf6e74SAlexander Motin 	if (device == NULL)
4446dcdf6e74SAlexander Motin 		return (NULL);
44478b8a9b1dSJustin T. Gibbs 
4448cccf4220SAlexander Motin 	cam_init_pinfo(&device->devq_entry.pinfo);
4449cccf4220SAlexander Motin 	device->devq_entry.device = device;
44508b8a9b1dSJustin T. Gibbs 	device->target = target;
44518b8a9b1dSJustin T. Gibbs 	device->lun_id = lun_id;
44522b83592fSScott Long 	device->sim = bus->sim;
44538b8a9b1dSJustin T. Gibbs 	/* Initialize our queues */
44548b8a9b1dSJustin T. Gibbs 	if (camq_init(&device->drvq, 0) != 0) {
4455596ee08fSAlexander Motin 		free(device, M_CAMDEV);
44568b8a9b1dSJustin T. Gibbs 		return (NULL);
44578b8a9b1dSJustin T. Gibbs 	}
44588b8a9b1dSJustin T. Gibbs 	if (cam_ccbq_init(&device->ccbq,
44598b8a9b1dSJustin T. Gibbs 			  bus->sim->max_dev_openings) != 0) {
44608b8a9b1dSJustin T. Gibbs 		camq_fini(&device->drvq);
4461596ee08fSAlexander Motin 		free(device, M_CAMDEV);
44628b8a9b1dSJustin T. Gibbs 		return (NULL);
44638b8a9b1dSJustin T. Gibbs 	}
4464434bbf6eSJustin T. Gibbs 	SLIST_INIT(&device->asyncs);
4465434bbf6eSJustin T. Gibbs 	SLIST_INIT(&device->periphs);
4466434bbf6eSJustin T. Gibbs 	device->generation = 0;
4467434bbf6eSJustin T. Gibbs 	device->flags = CAM_DEV_UNCONFIGURED;
4468434bbf6eSJustin T. Gibbs 	device->tag_delay_count = 0;
4469df8f9080SJustin T. Gibbs 	device->tag_saved_openings = 0;
4470434bbf6eSJustin T. Gibbs 	device->refcount = 1;
44712b83592fSScott Long 	callout_init_mtx(&device->callout, bus->sim->mtx, 0);
4472434bbf6eSJustin T. Gibbs 
4473dcdf6e74SAlexander Motin 	cur_device = TAILQ_FIRST(&target->ed_entries);
4474dcdf6e74SAlexander Motin 	while (cur_device != NULL && cur_device->lun_id < lun_id)
4475dcdf6e74SAlexander Motin 		cur_device = TAILQ_NEXT(cur_device, links);
4476dcdf6e74SAlexander Motin 	if (cur_device != NULL)
4477dcdf6e74SAlexander Motin 		TAILQ_INSERT_BEFORE(cur_device, device, links);
4478dcdf6e74SAlexander Motin 	else
4479dcdf6e74SAlexander Motin 		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4480434bbf6eSJustin T. Gibbs 	target->refcount++;
4481dcdf6e74SAlexander Motin 	target->generation++;
44828b8a9b1dSJustin T. Gibbs 	return (device);
44838b8a9b1dSJustin T. Gibbs }
44848b8a9b1dSJustin T. Gibbs 
4485f98d7a47SAlexander Motin void
4486f98d7a47SAlexander Motin xpt_acquire_device(struct cam_ed *device)
44878b8a9b1dSJustin T. Gibbs {
44888b8a9b1dSJustin T. Gibbs 
4489dcdf6e74SAlexander Motin 	mtx_assert(device->sim->mtx, MA_OWNED);
4490f98d7a47SAlexander Motin 	device->refcount++;
4491f98d7a47SAlexander Motin }
4492f98d7a47SAlexander Motin 
4493f98d7a47SAlexander Motin void
4494f98d7a47SAlexander Motin xpt_release_device(struct cam_ed *device)
4495f98d7a47SAlexander Motin {
44968b8a9b1dSJustin T. Gibbs 	struct cam_devq *devq;
44978b8a9b1dSJustin T. Gibbs 
4498dcdf6e74SAlexander Motin 	mtx_assert(device->sim->mtx, MA_OWNED);
4499dcdf6e74SAlexander Motin 	if (--device->refcount > 0)
4500dcdf6e74SAlexander Motin 		return;
4501dcdf6e74SAlexander Motin 
4502dcdf6e74SAlexander Motin 	KASSERT(SLIST_EMPTY(&device->periphs),
4503dcdf6e74SAlexander Motin 	    ("refcount is zero, but periphs list is not empty"));
4504cccf4220SAlexander Motin 	if (device->devq_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4505a5479bc5SJustin T. Gibbs 		panic("Removing device while still queued for ccbs");
45062cefde5fSJustin T. Gibbs 
45072cefde5fSJustin T. Gibbs 	if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
45082b83592fSScott Long 		callout_stop(&device->callout);
45092cefde5fSJustin T. Gibbs 
4510f98d7a47SAlexander Motin 	TAILQ_REMOVE(&device->target->ed_entries, device,links);
4511f98d7a47SAlexander Motin 	device->target->generation++;
4512f98d7a47SAlexander Motin 	device->target->bus->sim->max_ccbs -= device->ccbq.devq_openings;
45138b8a9b1dSJustin T. Gibbs 	/* Release our slot in the devq */
4514f98d7a47SAlexander Motin 	devq = device->target->bus->sim->devq;
4515cccf4220SAlexander Motin 	cam_devq_resize(devq, devq->send_queue.array_size - 1);
45167c20d5d7STai-hwa Liang 	camq_fini(&device->drvq);
451720a7933fSAlexander Motin 	cam_ccbq_fini(&device->ccbq);
4518e6bd5983SKenneth D. Merry 	/*
4519e6bd5983SKenneth D. Merry 	 * Free allocated memory.  free(9) does nothing if the
4520e6bd5983SKenneth D. Merry 	 * supplied pointer is NULL, so it is safe to call without
4521e6bd5983SKenneth D. Merry 	 * checking.
4522e6bd5983SKenneth D. Merry 	 */
4523e6bd5983SKenneth D. Merry 	free(device->supported_vpds, M_CAMXPT);
4524e6bd5983SKenneth D. Merry 	free(device->device_id, M_CAMXPT);
4525e6bd5983SKenneth D. Merry 	free(device->physpath, M_CAMXPT);
4526e6bd5983SKenneth D. Merry 	free(device->rcap_buf, M_CAMXPT);
4527e6bd5983SKenneth D. Merry 	free(device->serial_num, M_CAMXPT);
4528e6bd5983SKenneth D. Merry 
4529f98d7a47SAlexander Motin 	xpt_release_target(device->target);
4530596ee08fSAlexander Motin 	free(device, M_CAMDEV);
45318b8a9b1dSJustin T. Gibbs }
45328b8a9b1dSJustin T. Gibbs 
453352c9ce25SScott Long u_int32_t
45348b8a9b1dSJustin T. Gibbs xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
45358b8a9b1dSJustin T. Gibbs {
45368b8a9b1dSJustin T. Gibbs 	int	diff;
45378b8a9b1dSJustin T. Gibbs 	int	result;
45388b8a9b1dSJustin T. Gibbs 	struct	cam_ed *dev;
45398b8a9b1dSJustin T. Gibbs 
45408b8a9b1dSJustin T. Gibbs 	dev = path->device;
45418b8a9b1dSJustin T. Gibbs 
45428b8a9b1dSJustin T. Gibbs 	diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
45438b8a9b1dSJustin T. Gibbs 	result = cam_ccbq_resize(&dev->ccbq, newopenings);
4544df8f9080SJustin T. Gibbs 	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4545df8f9080SJustin T. Gibbs 	 || (dev->inq_flags & SID_CmdQue) != 0)
4546df8f9080SJustin T. Gibbs 		dev->tag_saved_openings = newopenings;
45478b8a9b1dSJustin T. Gibbs 	/* Adjust the global limit */
45482b83592fSScott Long 	dev->sim->max_ccbs += diff;
45498b8a9b1dSJustin T. Gibbs 	return (result);
45508b8a9b1dSJustin T. Gibbs }
45518b8a9b1dSJustin T. Gibbs 
45528b8a9b1dSJustin T. Gibbs static struct cam_eb *
45538b8a9b1dSJustin T. Gibbs xpt_find_bus(path_id_t path_id)
45548b8a9b1dSJustin T. Gibbs {
45558b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus;
45568b8a9b1dSJustin T. Gibbs 
45579a7c2696SAlexander Motin 	xpt_lock_buses();
45582b83592fSScott Long 	for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
45598b8a9b1dSJustin T. Gibbs 	     bus != NULL;
45608b8a9b1dSJustin T. Gibbs 	     bus = TAILQ_NEXT(bus, links)) {
4561a5479bc5SJustin T. Gibbs 		if (bus->path_id == path_id) {
4562a5479bc5SJustin T. Gibbs 			bus->refcount++;
45638b8a9b1dSJustin T. Gibbs 			break;
45648b8a9b1dSJustin T. Gibbs 		}
4565a5479bc5SJustin T. Gibbs 	}
45669a7c2696SAlexander Motin 	xpt_unlock_buses();
45678b8a9b1dSJustin T. Gibbs 	return (bus);
45688b8a9b1dSJustin T. Gibbs }
45698b8a9b1dSJustin T. Gibbs 
45708b8a9b1dSJustin T. Gibbs static struct cam_et *
45718b8a9b1dSJustin T. Gibbs xpt_find_target(struct cam_eb *bus, target_id_t	target_id)
45728b8a9b1dSJustin T. Gibbs {
45738b8a9b1dSJustin T. Gibbs 	struct cam_et *target;
45748b8a9b1dSJustin T. Gibbs 
4575dcdf6e74SAlexander Motin 	mtx_assert(bus->sim->mtx, MA_OWNED);
45768b8a9b1dSJustin T. Gibbs 	for (target = TAILQ_FIRST(&bus->et_entries);
45778b8a9b1dSJustin T. Gibbs 	     target != NULL;
45788b8a9b1dSJustin T. Gibbs 	     target = TAILQ_NEXT(target, links)) {
45798b8a9b1dSJustin T. Gibbs 		if (target->target_id == target_id) {
45808b8a9b1dSJustin T. Gibbs 			target->refcount++;
45818b8a9b1dSJustin T. Gibbs 			break;
45828b8a9b1dSJustin T. Gibbs 		}
45838b8a9b1dSJustin T. Gibbs 	}
45848b8a9b1dSJustin T. Gibbs 	return (target);
45858b8a9b1dSJustin T. Gibbs }
45868b8a9b1dSJustin T. Gibbs 
45878b8a9b1dSJustin T. Gibbs static struct cam_ed *
45888b8a9b1dSJustin T. Gibbs xpt_find_device(struct cam_et *target, lun_id_t lun_id)
45898b8a9b1dSJustin T. Gibbs {
45908b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
45918b8a9b1dSJustin T. Gibbs 
4592dcdf6e74SAlexander Motin 	mtx_assert(target->bus->sim->mtx, MA_OWNED);
45938b8a9b1dSJustin T. Gibbs 	for (device = TAILQ_FIRST(&target->ed_entries);
45948b8a9b1dSJustin T. Gibbs 	     device != NULL;
45958b8a9b1dSJustin T. Gibbs 	     device = TAILQ_NEXT(device, links)) {
45968b8a9b1dSJustin T. Gibbs 		if (device->lun_id == lun_id) {
45978b8a9b1dSJustin T. Gibbs 			device->refcount++;
45988b8a9b1dSJustin T. Gibbs 			break;
45998b8a9b1dSJustin T. Gibbs 		}
46008b8a9b1dSJustin T. Gibbs 	}
46018b8a9b1dSJustin T. Gibbs 	return (device);
46028b8a9b1dSJustin T. Gibbs }
46038b8a9b1dSJustin T. Gibbs 
460430a4094fSAlexander Motin void
4605f0adc790SJustin T. Gibbs xpt_start_tags(struct cam_path *path)
4606f0adc790SJustin T. Gibbs {
4607fd21cc5eSJustin T. Gibbs 	struct ccb_relsim crs;
4608fd21cc5eSJustin T. Gibbs 	struct cam_ed *device;
4609fd21cc5eSJustin T. Gibbs 	struct cam_sim *sim;
4610fd21cc5eSJustin T. Gibbs 	int    newopenings;
4611fd21cc5eSJustin T. Gibbs 
4612fd21cc5eSJustin T. Gibbs 	device = path->device;
4613fd21cc5eSJustin T. Gibbs 	sim = path->bus->sim;
4614fd21cc5eSJustin T. Gibbs 	device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4615fd21cc5eSJustin T. Gibbs 	xpt_freeze_devq(path, /*count*/1);
4616fd21cc5eSJustin T. Gibbs 	device->inq_flags |= SID_CmdQue;
4617df8f9080SJustin T. Gibbs 	if (device->tag_saved_openings != 0)
4618df8f9080SJustin T. Gibbs 		newopenings = device->tag_saved_openings;
4619df8f9080SJustin T. Gibbs 	else
462052c9ce25SScott Long 		newopenings = min(device->maxtags,
4621df8f9080SJustin T. Gibbs 				  sim->max_tagged_dev_openings);
4622f0adc790SJustin T. Gibbs 	xpt_dev_ccbq_resize(path, newopenings);
4623581b2e78SAlexander Motin 	xpt_async(AC_GETDEV_CHANGED, path, NULL);
4624bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4625fd21cc5eSJustin T. Gibbs 	crs.ccb_h.func_code = XPT_REL_SIMQ;
4626fd21cc5eSJustin T. Gibbs 	crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4627fd21cc5eSJustin T. Gibbs 	crs.openings
4628fd21cc5eSJustin T. Gibbs 	    = crs.release_timeout
4629fd21cc5eSJustin T. Gibbs 	    = crs.qfrozen_cnt
4630fd21cc5eSJustin T. Gibbs 	    = 0;
4631fd21cc5eSJustin T. Gibbs 	xpt_action((union ccb *)&crs);
4632fd21cc5eSJustin T. Gibbs }
4633fd21cc5eSJustin T. Gibbs 
463430a4094fSAlexander Motin void
463530a4094fSAlexander Motin xpt_stop_tags(struct cam_path *path)
463630a4094fSAlexander Motin {
463730a4094fSAlexander Motin 	struct ccb_relsim crs;
463830a4094fSAlexander Motin 	struct cam_ed *device;
463930a4094fSAlexander Motin 	struct cam_sim *sim;
464030a4094fSAlexander Motin 
464130a4094fSAlexander Motin 	device = path->device;
464230a4094fSAlexander Motin 	sim = path->bus->sim;
464330a4094fSAlexander Motin 	device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
464430a4094fSAlexander Motin 	device->tag_delay_count = 0;
464530a4094fSAlexander Motin 	xpt_freeze_devq(path, /*count*/1);
464630a4094fSAlexander Motin 	device->inq_flags &= ~SID_CmdQue;
464730a4094fSAlexander Motin 	xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4648581b2e78SAlexander Motin 	xpt_async(AC_GETDEV_CHANGED, path, NULL);
464930a4094fSAlexander Motin 	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
465030a4094fSAlexander Motin 	crs.ccb_h.func_code = XPT_REL_SIMQ;
465130a4094fSAlexander Motin 	crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
465230a4094fSAlexander Motin 	crs.openings
465330a4094fSAlexander Motin 	    = crs.release_timeout
465430a4094fSAlexander Motin 	    = crs.qfrozen_cnt
465530a4094fSAlexander Motin 	    = 0;
465630a4094fSAlexander Motin 	xpt_action((union ccb *)&crs);
465730a4094fSAlexander Motin }
465830a4094fSAlexander Motin 
465983c5d981SAlexander Motin static void
466083c5d981SAlexander Motin xpt_boot_delay(void *arg)
46618b8a9b1dSJustin T. Gibbs {
46622b83592fSScott Long 
466383c5d981SAlexander Motin 	xpt_release_boot();
46648b8a9b1dSJustin T. Gibbs }
46658b8a9b1dSJustin T. Gibbs 
46668b8a9b1dSJustin T. Gibbs static void
46678b8a9b1dSJustin T. Gibbs xpt_config(void *arg)
46688b8a9b1dSJustin T. Gibbs {
46693393f8daSKenneth D. Merry 	/*
46703393f8daSKenneth D. Merry 	 * Now that interrupts are enabled, go find our devices
46713393f8daSKenneth D. Merry 	 */
46728b8a9b1dSJustin T. Gibbs 
4673f0f25b9cSAlexander Motin 	/* Setup debugging path */
46748b8a9b1dSJustin T. Gibbs 	if (cam_dflags != CAM_DEBUG_NONE) {
4675e5dfa058SAlexander Motin 		if (xpt_create_path_unlocked(&cam_dpath, NULL,
46768b8a9b1dSJustin T. Gibbs 				    CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
46778b8a9b1dSJustin T. Gibbs 				    CAM_DEBUG_LUN) != CAM_REQ_CMP) {
46788b8a9b1dSJustin T. Gibbs 			printf("xpt_config: xpt_create_path() failed for debug"
46798b8a9b1dSJustin T. Gibbs 			       " target %d:%d:%d, debugging disabled\n",
46808b8a9b1dSJustin T. Gibbs 			       CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
46818b8a9b1dSJustin T. Gibbs 			cam_dflags = CAM_DEBUG_NONE;
46828b8a9b1dSJustin T. Gibbs 		}
46838b8a9b1dSJustin T. Gibbs 	} else
46848b8a9b1dSJustin T. Gibbs 		cam_dpath = NULL;
46858b8a9b1dSJustin T. Gibbs 
468683c5d981SAlexander Motin 	periphdriver_init(1);
468783c5d981SAlexander Motin 	xpt_hold_boot();
468883c5d981SAlexander Motin 	callout_init(&xsoftc.boot_callout, 1);
468983c5d981SAlexander Motin 	callout_reset(&xsoftc.boot_callout, hz * xsoftc.boot_delay / 1000,
469083c5d981SAlexander Motin 	    xpt_boot_delay, NULL);
469183c5d981SAlexander Motin 	/* Fire up rescan thread. */
469283c5d981SAlexander Motin 	if (kproc_create(xpt_scanner_thread, NULL, NULL, 0, 0, "xpt_thrd")) {
46936f15a274SAlexander Motin 		printf("xpt_config: failed to create rescan thread.\n");
46941e637ba6SAlexander Motin 	}
469583c5d981SAlexander Motin }
46968b8a9b1dSJustin T. Gibbs 
469783c5d981SAlexander Motin void
469883c5d981SAlexander Motin xpt_hold_boot(void)
469983c5d981SAlexander Motin {
470083c5d981SAlexander Motin 	xpt_lock_buses();
470183c5d981SAlexander Motin 	xsoftc.buses_to_config++;
470283c5d981SAlexander Motin 	xpt_unlock_buses();
470383c5d981SAlexander Motin }
470483c5d981SAlexander Motin 
470583c5d981SAlexander Motin void
470683c5d981SAlexander Motin xpt_release_boot(void)
470783c5d981SAlexander Motin {
470883c5d981SAlexander Motin 	xpt_lock_buses();
470983c5d981SAlexander Motin 	xsoftc.buses_to_config--;
471083c5d981SAlexander Motin 	if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
471183c5d981SAlexander Motin 		struct	xpt_task *task;
471283c5d981SAlexander Motin 
471383c5d981SAlexander Motin 		xsoftc.buses_config_done = 1;
471483c5d981SAlexander Motin 		xpt_unlock_buses();
4715ecdf1113SJustin T. Gibbs 		/* Call manually because we don't have any busses */
471683c5d981SAlexander Motin 		task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
471783c5d981SAlexander Motin 		if (task != NULL) {
471883c5d981SAlexander Motin 			TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
471983c5d981SAlexander Motin 			taskqueue_enqueue(taskqueue_thread, &task->task);
47202863f7b1SJustin T. Gibbs 		}
472183c5d981SAlexander Motin 	} else
472283c5d981SAlexander Motin 		xpt_unlock_buses();
47232863f7b1SJustin T. Gibbs }
47248b8a9b1dSJustin T. Gibbs 
47258b8a9b1dSJustin T. Gibbs /*
47268b8a9b1dSJustin T. Gibbs  * If the given device only has one peripheral attached to it, and if that
47278b8a9b1dSJustin T. Gibbs  * peripheral is the passthrough driver, announce it.  This insures that the
47288b8a9b1dSJustin T. Gibbs  * user sees some sort of announcement for every peripheral in their system.
47298b8a9b1dSJustin T. Gibbs  */
47308b8a9b1dSJustin T. Gibbs static int
47318b8a9b1dSJustin T. Gibbs xptpassannouncefunc(struct cam_ed *device, void *arg)
47328b8a9b1dSJustin T. Gibbs {
47338b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph;
47348b8a9b1dSJustin T. Gibbs 	int i;
47358b8a9b1dSJustin T. Gibbs 
47368b8a9b1dSJustin T. Gibbs 	for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
47378b8a9b1dSJustin T. Gibbs 	     periph = SLIST_NEXT(periph, periph_links), i++);
47388b8a9b1dSJustin T. Gibbs 
47398b8a9b1dSJustin T. Gibbs 	periph = SLIST_FIRST(&device->periphs);
47408b8a9b1dSJustin T. Gibbs 	if ((i == 1)
47418b8a9b1dSJustin T. Gibbs 	 && (strncmp(periph->periph_name, "pass", 4) == 0))
47428b8a9b1dSJustin T. Gibbs 		xpt_announce_periph(periph, NULL);
47438b8a9b1dSJustin T. Gibbs 
47448b8a9b1dSJustin T. Gibbs 	return(1);
47458b8a9b1dSJustin T. Gibbs }
47468b8a9b1dSJustin T. Gibbs 
47478b8a9b1dSJustin T. Gibbs static void
47482b83592fSScott Long xpt_finishconfig_task(void *context, int pending)
47498b8a9b1dSJustin T. Gibbs {
47508b8a9b1dSJustin T. Gibbs 
475183c5d981SAlexander Motin 	periphdriver_init(2);
47522b83592fSScott Long 	/*
47532b83592fSScott Long 	 * Check for devices with no "standard" peripheral driver
47542b83592fSScott Long 	 * attached.  For any devices like that, announce the
47552b83592fSScott Long 	 * passthrough driver so the user will see something.
47562b83592fSScott Long 	 */
47573089bb2eSAlexander Motin 	if (!bootverbose)
47582b83592fSScott Long 		xpt_for_all_devices(xptpassannouncefunc, NULL);
47592b83592fSScott Long 
47602b83592fSScott Long 	/* Release our hook so that the boot can continue. */
47612b83592fSScott Long 	config_intrhook_disestablish(xsoftc.xpt_config_hook);
47620dd50e9bSScott Long 	free(xsoftc.xpt_config_hook, M_CAMXPT);
47632b83592fSScott Long 	xsoftc.xpt_config_hook = NULL;
47642b83592fSScott Long 
47652b83592fSScott Long 	free(context, M_CAMXPT);
47662b83592fSScott Long }
47672b83592fSScott Long 
476885d92640SScott Long cam_status
476985d92640SScott Long xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
477085d92640SScott Long 		   struct cam_path *path)
477185d92640SScott Long {
477285d92640SScott Long 	struct ccb_setasync csa;
477385d92640SScott Long 	cam_status status;
477485d92640SScott Long 	int xptpath = 0;
477585d92640SScott Long 
477685d92640SScott Long 	if (path == NULL) {
477785d92640SScott Long 		mtx_lock(&xsoftc.xpt_lock);
477885d92640SScott Long 		status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
477985d92640SScott Long 					 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
478085d92640SScott Long 		if (status != CAM_REQ_CMP) {
478185d92640SScott Long 			mtx_unlock(&xsoftc.xpt_lock);
478285d92640SScott Long 			return (status);
478385d92640SScott Long 		}
478485d92640SScott Long 		xptpath = 1;
478585d92640SScott Long 	}
478685d92640SScott Long 
478783c5d981SAlexander Motin 	xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
478885d92640SScott Long 	csa.ccb_h.func_code = XPT_SASYNC_CB;
478985d92640SScott Long 	csa.event_enable = event;
479085d92640SScott Long 	csa.callback = cbfunc;
479185d92640SScott Long 	csa.callback_arg = cbarg;
479285d92640SScott Long 	xpt_action((union ccb *)&csa);
479385d92640SScott Long 	status = csa.ccb_h.status;
47943501942bSJustin T. Gibbs 
479585d92640SScott Long 	if (xptpath) {
479685d92640SScott Long 		xpt_free_path(path);
479785d92640SScott Long 		mtx_unlock(&xsoftc.xpt_lock);
47983501942bSJustin T. Gibbs 	}
47997685edecSAlexander Motin 
48007685edecSAlexander Motin 	if ((status == CAM_REQ_CMP) &&
48017685edecSAlexander Motin 	    (csa.event_enable & AC_FOUND_DEVICE)) {
48027685edecSAlexander Motin 		/*
48037685edecSAlexander Motin 		 * Get this peripheral up to date with all
48047685edecSAlexander Motin 		 * the currently existing devices.
48057685edecSAlexander Motin 		 */
48067685edecSAlexander Motin 		xpt_for_all_devices(xptsetasyncfunc, &csa);
48077685edecSAlexander Motin 	}
48087685edecSAlexander Motin 	if ((status == CAM_REQ_CMP) &&
48097685edecSAlexander Motin 	    (csa.event_enable & AC_PATH_REGISTERED)) {
48107685edecSAlexander Motin 		/*
48117685edecSAlexander Motin 		 * Get this peripheral up to date with all
48127685edecSAlexander Motin 		 * the currently existing busses.
48137685edecSAlexander Motin 		 */
48147685edecSAlexander Motin 		xpt_for_all_busses(xptsetasyncbusfunc, &csa);
48157685edecSAlexander Motin 	}
48163501942bSJustin T. Gibbs 
481785d92640SScott Long 	return (status);
481885d92640SScott Long }
481985d92640SScott Long 
48208b8a9b1dSJustin T. Gibbs static void
48218b8a9b1dSJustin T. Gibbs xptaction(struct cam_sim *sim, union ccb *work_ccb)
48228b8a9b1dSJustin T. Gibbs {
48238b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
48248b8a9b1dSJustin T. Gibbs 
48258b8a9b1dSJustin T. Gibbs 	switch (work_ccb->ccb_h.func_code) {
48268b8a9b1dSJustin T. Gibbs 	/* Common cases first */
48278b8a9b1dSJustin T. Gibbs 	case XPT_PATH_INQ:		/* Path routing inquiry */
48288b8a9b1dSJustin T. Gibbs 	{
48298b8a9b1dSJustin T. Gibbs 		struct ccb_pathinq *cpi;
48308b8a9b1dSJustin T. Gibbs 
48318b8a9b1dSJustin T. Gibbs 		cpi = &work_ccb->cpi;
48328b8a9b1dSJustin T. Gibbs 		cpi->version_num = 1; /* XXX??? */
48338b8a9b1dSJustin T. Gibbs 		cpi->hba_inquiry = 0;
48348b8a9b1dSJustin T. Gibbs 		cpi->target_sprt = 0;
48358b8a9b1dSJustin T. Gibbs 		cpi->hba_misc = 0;
48368b8a9b1dSJustin T. Gibbs 		cpi->hba_eng_cnt = 0;
48378b8a9b1dSJustin T. Gibbs 		cpi->max_target = 0;
48388b8a9b1dSJustin T. Gibbs 		cpi->max_lun = 0;
48398b8a9b1dSJustin T. Gibbs 		cpi->initiator_id = 0;
48408b8a9b1dSJustin T. Gibbs 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
48418b8a9b1dSJustin T. Gibbs 		strncpy(cpi->hba_vid, "", HBA_IDLEN);
48428b8a9b1dSJustin T. Gibbs 		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
48438b8a9b1dSJustin T. Gibbs 		cpi->unit_number = sim->unit_number;
48448b8a9b1dSJustin T. Gibbs 		cpi->bus_id = sim->bus_id;
48459deea857SKenneth D. Merry 		cpi->base_transfer_speed = 0;
48463393f8daSKenneth D. Merry 		cpi->protocol = PROTO_UNSPECIFIED;
48473393f8daSKenneth D. Merry 		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
48483393f8daSKenneth D. Merry 		cpi->transport = XPORT_UNSPECIFIED;
48493393f8daSKenneth D. Merry 		cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
48508b8a9b1dSJustin T. Gibbs 		cpi->ccb_h.status = CAM_REQ_CMP;
48518b8a9b1dSJustin T. Gibbs 		xpt_done(work_ccb);
48528b8a9b1dSJustin T. Gibbs 		break;
48538b8a9b1dSJustin T. Gibbs 	}
48548b8a9b1dSJustin T. Gibbs 	default:
48558b8a9b1dSJustin T. Gibbs 		work_ccb->ccb_h.status = CAM_REQ_INVALID;
48568b8a9b1dSJustin T. Gibbs 		xpt_done(work_ccb);
48578b8a9b1dSJustin T. Gibbs 		break;
48588b8a9b1dSJustin T. Gibbs 	}
48598b8a9b1dSJustin T. Gibbs }
48608b8a9b1dSJustin T. Gibbs 
48618b8a9b1dSJustin T. Gibbs /*
4862434bbf6eSJustin T. Gibbs  * The xpt as a "controller" has no interrupt sources, so polling
4863434bbf6eSJustin T. Gibbs  * is a no-op.
4864434bbf6eSJustin T. Gibbs  */
4865434bbf6eSJustin T. Gibbs static void
4866434bbf6eSJustin T. Gibbs xptpoll(struct cam_sim *sim)
4867434bbf6eSJustin T. Gibbs {
4868434bbf6eSJustin T. Gibbs }
4869434bbf6eSJustin T. Gibbs 
48702b83592fSScott Long void
48712b83592fSScott Long xpt_lock_buses(void)
48722b83592fSScott Long {
48732b83592fSScott Long 	mtx_lock(&xsoftc.xpt_topo_lock);
48742b83592fSScott Long }
48752b83592fSScott Long 
48762b83592fSScott Long void
48772b83592fSScott Long xpt_unlock_buses(void)
48782b83592fSScott Long {
48792b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_topo_lock);
48802b83592fSScott Long }
48812b83592fSScott Long 
48825d754af7SMatt Jacob static void
48839758cc83SScott Long camisr(void *dummy)
48848b8a9b1dSJustin T. Gibbs {
48859758cc83SScott Long 	cam_simq_t queue;
48862b83592fSScott Long 	struct cam_sim *sim;
48878b8a9b1dSJustin T. Gibbs 
48889758cc83SScott Long 	mtx_lock(&cam_simq_lock);
4889ef3cf714SScott Long 	TAILQ_INIT(&queue);
4890788fb376SAlexander Motin 	while (!TAILQ_EMPTY(&cam_simq)) {
48919758cc83SScott Long 		TAILQ_CONCAT(&queue, &cam_simq, links);
48929758cc83SScott Long 		mtx_unlock(&cam_simq_lock);
4893ef3cf714SScott Long 
48949758cc83SScott Long 		while ((sim = TAILQ_FIRST(&queue)) != NULL) {
48959758cc83SScott Long 			TAILQ_REMOVE(&queue, sim, links);
489611e4faceSScott Long 			CAM_SIM_LOCK(sim);
489739fe6d85SAlexander Motin 			camisr_runqueue(sim);
489810284c8bSAlexander Motin 			sim->flags &= ~CAM_SIM_ON_DONEQ;
489911e4faceSScott Long 			CAM_SIM_UNLOCK(sim);
49009758cc83SScott Long 		}
4901788fb376SAlexander Motin 		mtx_lock(&cam_simq_lock);
4902788fb376SAlexander Motin 	}
4903788fb376SAlexander Motin 	mtx_unlock(&cam_simq_lock);
49049758cc83SScott Long }
49059758cc83SScott Long 
49069758cc83SScott Long static void
490739fe6d85SAlexander Motin camisr_runqueue(struct cam_sim *sim)
49089758cc83SScott Long {
49099758cc83SScott Long 	struct	ccb_hdr *ccb_h;
49109758cc83SScott Long 
491139fe6d85SAlexander Motin 	while ((ccb_h = TAILQ_FIRST(&sim->sim_doneq)) != NULL) {
49128b8a9b1dSJustin T. Gibbs 		int	runq;
49138b8a9b1dSJustin T. Gibbs 
491439fe6d85SAlexander Motin 		TAILQ_REMOVE(&sim->sim_doneq, ccb_h, sim_links.tqe);
49158b8a9b1dSJustin T. Gibbs 		ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
49168b8a9b1dSJustin T. Gibbs 
49178b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
491857b89bbcSNate Lawson 			  ("camisr\n"));
49198b8a9b1dSJustin T. Gibbs 
49208b8a9b1dSJustin T. Gibbs 		runq = FALSE;
49218b8a9b1dSJustin T. Gibbs 
49228b8a9b1dSJustin T. Gibbs 		if (ccb_h->flags & CAM_HIGH_POWER) {
49238b8a9b1dSJustin T. Gibbs 			struct highpowerlist	*hphead;
4924ea541bfdSAlexander Motin 			struct cam_ed		*device;
49258b8a9b1dSJustin T. Gibbs 
49262b83592fSScott Long 			mtx_lock(&xsoftc.xpt_lock);
49272b83592fSScott Long 			hphead = &xsoftc.highpowerq;
49288b8a9b1dSJustin T. Gibbs 
4929ea541bfdSAlexander Motin 			device = STAILQ_FIRST(hphead);
49308b8a9b1dSJustin T. Gibbs 
49318b8a9b1dSJustin T. Gibbs 			/*
49328b8a9b1dSJustin T. Gibbs 			 * Increment the count since this command is done.
49338b8a9b1dSJustin T. Gibbs 			 */
49342b83592fSScott Long 			xsoftc.num_highpower++;
49358b8a9b1dSJustin T. Gibbs 
49368b8a9b1dSJustin T. Gibbs 			/*
49378b8a9b1dSJustin T. Gibbs 			 * Any high powered commands queued up?
49388b8a9b1dSJustin T. Gibbs 			 */
4939ea541bfdSAlexander Motin 			if (device != NULL) {
49408b8a9b1dSJustin T. Gibbs 
4941ea541bfdSAlexander Motin 				STAILQ_REMOVE_HEAD(hphead, highpowerq_entry);
49422b83592fSScott Long 				mtx_unlock(&xsoftc.xpt_lock);
49438b8a9b1dSJustin T. Gibbs 
4944ea541bfdSAlexander Motin 				xpt_release_devq_device(device,
49452cefde5fSJustin T. Gibbs 						 /*count*/1, /*runqueue*/TRUE);
49462b83592fSScott Long 			} else
49472b83592fSScott Long 				mtx_unlock(&xsoftc.xpt_lock);
49488b8a9b1dSJustin T. Gibbs 		}
49492b83592fSScott Long 
49509deea857SKenneth D. Merry 		if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
49518b8a9b1dSJustin T. Gibbs 			struct cam_ed *dev;
49528b8a9b1dSJustin T. Gibbs 
49538b8a9b1dSJustin T. Gibbs 			dev = ccb_h->path->device;
49548b8a9b1dSJustin T. Gibbs 
49558b8a9b1dSJustin T. Gibbs 			cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
495639fe6d85SAlexander Motin 			sim->devq->send_active--;
495739fe6d85SAlexander Motin 			sim->devq->send_openings++;
495883c5d981SAlexander Motin 			runq = TRUE;
49598b8a9b1dSJustin T. Gibbs 
4960b9c473b2SAlexander Motin 			if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
49618b8a9b1dSJustin T. Gibbs 			  && (dev->ccbq.dev_active == 0))) {
4962b9c473b2SAlexander Motin 				dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
4963b9c473b2SAlexander Motin 				xpt_release_devq(ccb_h->path, /*count*/1,
4964b9c473b2SAlexander Motin 						 /*run_queue*/FALSE);
4965b9c473b2SAlexander Motin 			}
4966b9c473b2SAlexander Motin 
4967b9c473b2SAlexander Motin 			if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
4968b9c473b2SAlexander Motin 			  && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
4969b9c473b2SAlexander Motin 				dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
49702cefde5fSJustin T. Gibbs 				xpt_release_devq(ccb_h->path, /*count*/1,
497183c5d981SAlexander Motin 						 /*run_queue*/FALSE);
49728b8a9b1dSJustin T. Gibbs 			}
49738b8a9b1dSJustin T. Gibbs 
4974fd21cc5eSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4975fd21cc5eSJustin T. Gibbs 			 && (--dev->tag_delay_count == 0))
4976fd21cc5eSJustin T. Gibbs 				xpt_start_tags(ccb_h->path);
4977cccf4220SAlexander Motin 			if (!device_is_queued(dev)) {
497839fe6d85SAlexander Motin 				(void)xpt_schedule_devq(sim->devq, dev);
49793501942bSJustin T. Gibbs 			}
49808b8a9b1dSJustin T. Gibbs 		}
49818b8a9b1dSJustin T. Gibbs 
49828b8a9b1dSJustin T. Gibbs 		if (ccb_h->status & CAM_RELEASE_SIMQ) {
498339fe6d85SAlexander Motin 			xpt_release_simq(sim, /*run_queue*/TRUE);
4984434bbf6eSJustin T. Gibbs 			ccb_h->status &= ~CAM_RELEASE_SIMQ;
4985434bbf6eSJustin T. Gibbs 			runq = FALSE;
4986434bbf6eSJustin T. Gibbs 		}
4987434bbf6eSJustin T. Gibbs 
4988434bbf6eSJustin T. Gibbs 		if ((ccb_h->flags & CAM_DEV_QFRZDIS)
49898b8a9b1dSJustin T. Gibbs 		 && (ccb_h->status & CAM_DEV_QFRZN)) {
49902cefde5fSJustin T. Gibbs 			xpt_release_devq(ccb_h->path, /*count*/1,
49918b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
49928b8a9b1dSJustin T. Gibbs 			ccb_h->status &= ~CAM_DEV_QFRZN;
49938b8a9b1dSJustin T. Gibbs 		} else if (runq) {
499439fe6d85SAlexander Motin 			xpt_run_devq(sim->devq);
49958b8a9b1dSJustin T. Gibbs 		}
49968b8a9b1dSJustin T. Gibbs 
49978b8a9b1dSJustin T. Gibbs 		/* Call the peripheral driver's callback */
4998434bbf6eSJustin T. Gibbs 		(*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
49998b8a9b1dSJustin T. Gibbs 	}
50008b8a9b1dSJustin T. Gibbs }
5001