xref: /freebsd/sys/cam/cam_xpt.c (revision e5736ac88c4dd7a353c742e8e4fb654000fea534)
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 	u_int32_t		xpt_generation;
1002b83592fSScott Long 
1012b83592fSScott Long 	/* number of high powered commands that can go through right now */
1022b83592fSScott Long 	STAILQ_HEAD(highpowerlist, ccb_hdr)	highpowerq;
1032b83592fSScott Long 	int			num_highpower;
1042b83592fSScott Long 
1052b83592fSScott Long 	/* queue for handling async rescan requests. */
1062b83592fSScott Long 	TAILQ_HEAD(, ccb_hdr) ccb_scanq;
10783c5d981SAlexander Motin 	int buses_to_config;
10883c5d981SAlexander Motin 	int buses_config_done;
1092b83592fSScott Long 
1102b83592fSScott Long 	/* Registered busses */
1112b83592fSScott Long 	TAILQ_HEAD(,cam_eb)	xpt_busses;
1122b83592fSScott Long 	u_int			bus_generation;
1132b83592fSScott Long 
1142b83592fSScott Long 	struct intr_config_hook	*xpt_config_hook;
1152b83592fSScott Long 
11683c5d981SAlexander Motin 	int			boot_delay;
11783c5d981SAlexander Motin 	struct callout 		boot_callout;
11883c5d981SAlexander Motin 
1192b83592fSScott Long 	struct mtx		xpt_topo_lock;
1202b83592fSScott Long 	struct mtx		xpt_lock;
1218b8a9b1dSJustin T. Gibbs };
1228b8a9b1dSJustin T. Gibbs 
1238b8a9b1dSJustin T. Gibbs typedef enum {
1248b8a9b1dSJustin T. Gibbs 	DM_RET_COPY		= 0x01,
1258b8a9b1dSJustin T. Gibbs 	DM_RET_FLAG_MASK	= 0x0f,
1268b8a9b1dSJustin T. Gibbs 	DM_RET_NONE		= 0x00,
1278b8a9b1dSJustin T. Gibbs 	DM_RET_STOP		= 0x10,
1288b8a9b1dSJustin T. Gibbs 	DM_RET_DESCEND		= 0x20,
1298b8a9b1dSJustin T. Gibbs 	DM_RET_ERROR		= 0x30,
1308b8a9b1dSJustin T. Gibbs 	DM_RET_ACTION_MASK	= 0xf0
1318b8a9b1dSJustin T. Gibbs } dev_match_ret;
1328b8a9b1dSJustin T. Gibbs 
1338b8a9b1dSJustin T. Gibbs typedef enum {
1348b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_BUS,
1358b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_TARGET,
1368b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_DEVICE,
1378b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_PERIPH
1388b8a9b1dSJustin T. Gibbs } xpt_traverse_depth;
1398b8a9b1dSJustin T. Gibbs 
1408b8a9b1dSJustin T. Gibbs struct xpt_traverse_config {
1418b8a9b1dSJustin T. Gibbs 	xpt_traverse_depth	depth;
1428b8a9b1dSJustin T. Gibbs 	void			*tr_func;
1438b8a9b1dSJustin T. Gibbs 	void			*tr_arg;
1448b8a9b1dSJustin T. Gibbs };
1458b8a9b1dSJustin T. Gibbs 
1468b8a9b1dSJustin T. Gibbs typedef	int	xpt_busfunc_t (struct cam_eb *bus, void *arg);
1478b8a9b1dSJustin T. Gibbs typedef	int	xpt_targetfunc_t (struct cam_et *target, void *arg);
1488b8a9b1dSJustin T. Gibbs typedef	int	xpt_devicefunc_t (struct cam_ed *device, void *arg);
1498b8a9b1dSJustin T. Gibbs typedef	int	xpt_periphfunc_t (struct cam_periph *periph, void *arg);
1508b8a9b1dSJustin T. Gibbs typedef int	xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
1518b8a9b1dSJustin T. Gibbs 
1528b8a9b1dSJustin T. Gibbs /* Transport layer configuration information */
1538b8a9b1dSJustin T. Gibbs static struct xpt_softc xsoftc;
1548b8a9b1dSJustin T. Gibbs 
15583c5d981SAlexander Motin TUNABLE_INT("kern.cam.boot_delay", &xsoftc.boot_delay);
15683c5d981SAlexander Motin SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
15783c5d981SAlexander Motin            &xsoftc.boot_delay, 0, "Bus registration wait time");
15883c5d981SAlexander Motin 
1598b8a9b1dSJustin T. Gibbs /* Queues for our software interrupt handler */
160e3975643SJake Burkholder typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
1619758cc83SScott Long typedef TAILQ_HEAD(cam_simq, cam_sim) cam_simq_t;
1629758cc83SScott Long static cam_simq_t cam_simq;
1639758cc83SScott Long static struct mtx cam_simq_lock;
1648b8a9b1dSJustin T. Gibbs 
1652b83592fSScott Long /* Pointers to software interrupt handlers */
1662b83592fSScott Long static void *cambio_ih;
1678b8a9b1dSJustin T. Gibbs 
1689a1c8571SNick Hibma struct cam_periph *xpt_periph;
1699a1c8571SNick Hibma 
1708b8a9b1dSJustin T. Gibbs static periph_init_t xpt_periph_init;
1718b8a9b1dSJustin T. Gibbs 
1728b8a9b1dSJustin T. Gibbs static struct periph_driver xpt_driver =
1738b8a9b1dSJustin T. Gibbs {
1748b8a9b1dSJustin T. Gibbs 	xpt_periph_init, "xpt",
1751e637ba6SAlexander Motin 	TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
1761e637ba6SAlexander Motin 	CAM_PERIPH_DRV_EARLY
1778b8a9b1dSJustin T. Gibbs };
1788b8a9b1dSJustin T. Gibbs 
1790b7c27b9SPeter Wemm PERIPHDRIVER_DECLARE(xpt, xpt_driver);
1808b8a9b1dSJustin T. Gibbs 
1818b8a9b1dSJustin T. Gibbs static d_open_t xptopen;
1828b8a9b1dSJustin T. Gibbs static d_close_t xptclose;
1838b8a9b1dSJustin T. Gibbs static d_ioctl_t xptioctl;
18425a2902cSScott Long static d_ioctl_t xptdoioctl;
1858b8a9b1dSJustin T. Gibbs 
1864e2f199eSPoul-Henning Kamp static struct cdevsw xpt_cdevsw = {
187dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
1882b83592fSScott Long 	.d_flags =	0,
1897ac40f5fSPoul-Henning Kamp 	.d_open =	xptopen,
1907ac40f5fSPoul-Henning Kamp 	.d_close =	xptclose,
1917ac40f5fSPoul-Henning Kamp 	.d_ioctl =	xptioctl,
1927ac40f5fSPoul-Henning Kamp 	.d_name =	"xpt",
1938b8a9b1dSJustin T. Gibbs };
1948b8a9b1dSJustin T. Gibbs 
1958b8a9b1dSJustin T. Gibbs /* Storage for debugging datastructures */
1968b8a9b1dSJustin T. Gibbs struct cam_path *cam_dpath;
19782b361b1SMatt Jacob u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
19882b361b1SMatt Jacob TUNABLE_INT("kern.cam.dflags", &cam_dflags);
199240577c2SMatthew D Fleming SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RW,
200f0f25b9cSAlexander Motin 	&cam_dflags, 0, "Enabled debug flags");
201f0f25b9cSAlexander Motin u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
20282b361b1SMatt Jacob TUNABLE_INT("kern.cam.debug_delay", &cam_debug_delay);
203240577c2SMatthew D Fleming SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RW,
204f0f25b9cSAlexander Motin 	&cam_debug_delay, 0, "Delay in us after each debug message");
2058b8a9b1dSJustin T. Gibbs 
2066d2a8f1cSPeter Wemm /* Our boot-time initialization hook */
20774bd1c10SNick Hibma static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
20874bd1c10SNick Hibma 
20974bd1c10SNick Hibma static moduledata_t cam_moduledata = {
21074bd1c10SNick Hibma 	"cam",
21174bd1c10SNick Hibma 	cam_module_event_handler,
21274bd1c10SNick Hibma 	NULL
21374bd1c10SNick Hibma };
21474bd1c10SNick Hibma 
2152b83592fSScott Long static int	xpt_init(void *);
21674bd1c10SNick Hibma 
21774bd1c10SNick Hibma DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
21874bd1c10SNick Hibma MODULE_VERSION(cam, 1);
21974bd1c10SNick Hibma 
2208b8a9b1dSJustin T. Gibbs 
2218b8a9b1dSJustin T. Gibbs static void		xpt_async_bcast(struct async_list *async_head,
2228b8a9b1dSJustin T. Gibbs 					u_int32_t async_code,
2238b8a9b1dSJustin T. Gibbs 					struct cam_path *path,
2248b8a9b1dSJustin T. Gibbs 					void *async_arg);
225434bbf6eSJustin T. Gibbs static path_id_t xptnextfreepathid(void);
226434bbf6eSJustin T. Gibbs static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
2278b8a9b1dSJustin T. Gibbs static union ccb *xpt_get_ccb(struct cam_ed *device);
228cccf4220SAlexander Motin static void	 xpt_run_dev_allocq(struct cam_ed *device);
229cccf4220SAlexander Motin static void	 xpt_run_devq(struct cam_devq *devq);
2308b8a9b1dSJustin T. Gibbs static timeout_t xpt_release_devq_timeout;
2312b83592fSScott Long static void	 xpt_release_simq_timeout(void *arg) __unused;
232a5479bc5SJustin T. Gibbs static void	 xpt_release_bus(struct cam_eb *bus);
233cccf4220SAlexander Motin static void	 xpt_release_devq_device(struct cam_ed *dev, u_int count,
234cccf4220SAlexander Motin 		    int run_queue);
2358b8a9b1dSJustin T. Gibbs static struct cam_et*
2368b8a9b1dSJustin T. Gibbs 		 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
237f98d7a47SAlexander Motin static void	 xpt_release_target(struct cam_et *target);
2388b8a9b1dSJustin T. Gibbs static struct cam_eb*
2398b8a9b1dSJustin T. Gibbs 		 xpt_find_bus(path_id_t path_id);
2408b8a9b1dSJustin T. Gibbs static struct cam_et*
2418b8a9b1dSJustin T. Gibbs 		 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
2428b8a9b1dSJustin T. Gibbs static struct cam_ed*
2438b8a9b1dSJustin T. Gibbs 		 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
2448b8a9b1dSJustin T. Gibbs static void	 xpt_config(void *arg);
2458b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t xptpassannouncefunc;
2468b8a9b1dSJustin T. Gibbs static void	 xptaction(struct cam_sim *sim, union ccb *work_ccb);
247434bbf6eSJustin T. Gibbs static void	 xptpoll(struct cam_sim *sim);
2488088699fSJohn Baldwin static void	 camisr(void *);
2499758cc83SScott Long static void	 camisr_runqueue(void *);
2508b8a9b1dSJustin T. Gibbs static dev_match_ret	xptbusmatch(struct dev_match_pattern *patterns,
2513393f8daSKenneth D. Merry 				    u_int num_patterns, struct cam_eb *bus);
2528b8a9b1dSJustin T. Gibbs static dev_match_ret	xptdevicematch(struct dev_match_pattern *patterns,
2533393f8daSKenneth D. Merry 				       u_int num_patterns,
2543393f8daSKenneth D. Merry 				       struct cam_ed *device);
2558b8a9b1dSJustin T. Gibbs static dev_match_ret	xptperiphmatch(struct dev_match_pattern *patterns,
2563393f8daSKenneth D. Merry 				       u_int num_patterns,
2578b8a9b1dSJustin T. Gibbs 				       struct cam_periph *periph);
2588b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptedtbusfunc;
2598b8a9b1dSJustin T. Gibbs static xpt_targetfunc_t	xptedttargetfunc;
2608b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptedtdevicefunc;
2618b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptedtperiphfunc;
2628b8a9b1dSJustin T. Gibbs static xpt_pdrvfunc_t	xptplistpdrvfunc;
2638b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptplistperiphfunc;
2648b8a9b1dSJustin T. Gibbs static int		xptedtmatch(struct ccb_dev_match *cdm);
2658b8a9b1dSJustin T. Gibbs static int		xptperiphlistmatch(struct ccb_dev_match *cdm);
2668b8a9b1dSJustin T. Gibbs static int		xptbustraverse(struct cam_eb *start_bus,
2678b8a9b1dSJustin T. Gibbs 				       xpt_busfunc_t *tr_func, void *arg);
2688b8a9b1dSJustin T. Gibbs static int		xpttargettraverse(struct cam_eb *bus,
2698b8a9b1dSJustin T. Gibbs 					  struct cam_et *start_target,
2708b8a9b1dSJustin T. Gibbs 					  xpt_targetfunc_t *tr_func, void *arg);
2718b8a9b1dSJustin T. Gibbs static int		xptdevicetraverse(struct cam_et *target,
2728b8a9b1dSJustin T. Gibbs 					  struct cam_ed *start_device,
2738b8a9b1dSJustin T. Gibbs 					  xpt_devicefunc_t *tr_func, void *arg);
2748b8a9b1dSJustin T. Gibbs static int		xptperiphtraverse(struct cam_ed *device,
2758b8a9b1dSJustin T. Gibbs 					  struct cam_periph *start_periph,
2768b8a9b1dSJustin T. Gibbs 					  xpt_periphfunc_t *tr_func, void *arg);
2778b8a9b1dSJustin T. Gibbs static int		xptpdrvtraverse(struct periph_driver **start_pdrv,
2788b8a9b1dSJustin T. Gibbs 					xpt_pdrvfunc_t *tr_func, void *arg);
2798b8a9b1dSJustin T. Gibbs static int		xptpdperiphtraverse(struct periph_driver **pdrv,
2808b8a9b1dSJustin T. Gibbs 					    struct cam_periph *start_periph,
2818b8a9b1dSJustin T. Gibbs 					    xpt_periphfunc_t *tr_func,
2828b8a9b1dSJustin T. Gibbs 					    void *arg);
2838b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptdefbusfunc;
2848b8a9b1dSJustin T. Gibbs static xpt_targetfunc_t	xptdeftargetfunc;
2858b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptdefdevicefunc;
2868b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptdefperiphfunc;
28783c5d981SAlexander Motin static void		xpt_finishconfig_task(void *context, int pending);
28852c9ce25SScott Long static void		xpt_dev_async_default(u_int32_t async_code,
28952c9ce25SScott Long 					      struct cam_eb *bus,
29052c9ce25SScott Long 					      struct cam_et *target,
29152c9ce25SScott Long 					      struct cam_ed *device,
29252c9ce25SScott Long 					      void *async_arg);
29352c9ce25SScott Long static struct cam_ed *	xpt_alloc_device_default(struct cam_eb *bus,
29452c9ce25SScott Long 						 struct cam_et *target,
29552c9ce25SScott Long 						 lun_id_t lun_id);
2968b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptsetasyncfunc;
2978b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptsetasyncbusfunc;
2988b8a9b1dSJustin T. Gibbs static cam_status	xptregister(struct cam_periph *periph,
2998b8a9b1dSJustin T. Gibbs 				    void *arg);
3008b8a9b1dSJustin T. Gibbs static __inline int periph_is_queued(struct cam_periph *periph);
301cccf4220SAlexander Motin static __inline int device_is_queued(struct cam_ed *device);
3028b8a9b1dSJustin T. Gibbs 
3038b8a9b1dSJustin T. Gibbs static __inline int
304cccf4220SAlexander Motin xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
30530a4094fSAlexander Motin {
30630a4094fSAlexander Motin 	int	retval;
30730a4094fSAlexander Motin 
30883c5d981SAlexander Motin 	if ((dev->ccbq.queue.entries > 0) &&
30983c5d981SAlexander Motin 	    (dev->ccbq.dev_openings > 0) &&
310cccf4220SAlexander Motin 	    (dev->ccbq.queue.qfrozen_cnt == 0)) {
31130a4094fSAlexander Motin 		/*
31230a4094fSAlexander Motin 		 * The priority of a device waiting for controller
3136bccea7cSRebecca Cran 		 * resources is that of the highest priority CCB
31430a4094fSAlexander Motin 		 * enqueued.
31530a4094fSAlexander Motin 		 */
31630a4094fSAlexander Motin 		retval =
317cccf4220SAlexander Motin 		    xpt_schedule_dev(&devq->send_queue,
318cccf4220SAlexander Motin 				     &dev->devq_entry.pinfo,
31983c5d981SAlexander Motin 				     CAMQ_GET_PRIO(&dev->ccbq.queue));
32030a4094fSAlexander Motin 	} else {
32130a4094fSAlexander Motin 		retval = 0;
32230a4094fSAlexander Motin 	}
32330a4094fSAlexander Motin 	return (retval);
32430a4094fSAlexander Motin }
32530a4094fSAlexander Motin 
32630a4094fSAlexander Motin static __inline int
3278b8a9b1dSJustin T. Gibbs periph_is_queued(struct cam_periph *periph)
3288b8a9b1dSJustin T. Gibbs {
3298b8a9b1dSJustin T. Gibbs 	return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
3308b8a9b1dSJustin T. Gibbs }
3318b8a9b1dSJustin T. Gibbs 
3328b8a9b1dSJustin T. Gibbs static __inline int
333cccf4220SAlexander Motin device_is_queued(struct cam_ed *device)
3348b8a9b1dSJustin T. Gibbs {
335cccf4220SAlexander Motin 	return (device->devq_entry.pinfo.index != CAM_UNQUEUED_INDEX);
3368b8a9b1dSJustin T. Gibbs }
3378b8a9b1dSJustin T. Gibbs 
3388b8a9b1dSJustin T. Gibbs static void
3398b8a9b1dSJustin T. Gibbs xpt_periph_init()
3408b8a9b1dSJustin T. Gibbs {
34173d26919SKenneth D. Merry 	make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
3428b8a9b1dSJustin T. Gibbs }
3438b8a9b1dSJustin T. Gibbs 
3448b8a9b1dSJustin T. Gibbs static void
3458b8a9b1dSJustin T. Gibbs xptdone(struct cam_periph *periph, union ccb *done_ccb)
3468b8a9b1dSJustin T. Gibbs {
3478b8a9b1dSJustin T. Gibbs 	/* Caller will release the CCB */
3488b8a9b1dSJustin T. Gibbs 	wakeup(&done_ccb->ccb_h.cbfcnp);
3498b8a9b1dSJustin T. Gibbs }
3508b8a9b1dSJustin T. Gibbs 
3518b8a9b1dSJustin T. Gibbs static int
35289c9c53dSPoul-Henning Kamp xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
3538b8a9b1dSJustin T. Gibbs {
3548b8a9b1dSJustin T. Gibbs 
3558b8a9b1dSJustin T. Gibbs 	/*
35666a0780eSKenneth D. Merry 	 * Only allow read-write access.
35766a0780eSKenneth D. Merry 	 */
35866a0780eSKenneth D. Merry 	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
35966a0780eSKenneth D. Merry 		return(EPERM);
36066a0780eSKenneth D. Merry 
36166a0780eSKenneth D. Merry 	/*
3628b8a9b1dSJustin T. Gibbs 	 * We don't allow nonblocking access.
3638b8a9b1dSJustin T. Gibbs 	 */
3648b8a9b1dSJustin T. Gibbs 	if ((flags & O_NONBLOCK) != 0) {
3652b83592fSScott Long 		printf("%s: can't do nonblocking access\n", devtoname(dev));
3668b8a9b1dSJustin T. Gibbs 		return(ENODEV);
3678b8a9b1dSJustin T. Gibbs 	}
3688b8a9b1dSJustin T. Gibbs 
3698b8a9b1dSJustin T. Gibbs 	/* Mark ourselves open */
3702b83592fSScott Long 	mtx_lock(&xsoftc.xpt_lock);
3718b8a9b1dSJustin T. Gibbs 	xsoftc.flags |= XPT_FLAG_OPEN;
3722b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_lock);
3738b8a9b1dSJustin T. Gibbs 
3748b8a9b1dSJustin T. Gibbs 	return(0);
3758b8a9b1dSJustin T. Gibbs }
3768b8a9b1dSJustin T. Gibbs 
3778b8a9b1dSJustin T. Gibbs static int
37889c9c53dSPoul-Henning Kamp xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
3798b8a9b1dSJustin T. Gibbs {
3808b8a9b1dSJustin T. Gibbs 
3818b8a9b1dSJustin T. Gibbs 	/* Mark ourselves closed */
3822b83592fSScott Long 	mtx_lock(&xsoftc.xpt_lock);
3838b8a9b1dSJustin T. Gibbs 	xsoftc.flags &= ~XPT_FLAG_OPEN;
3842b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_lock);
3858b8a9b1dSJustin T. Gibbs 
3868b8a9b1dSJustin T. Gibbs 	return(0);
3878b8a9b1dSJustin T. Gibbs }
3888b8a9b1dSJustin T. Gibbs 
3892b83592fSScott Long /*
3902b83592fSScott Long  * Don't automatically grab the xpt softc lock here even though this is going
3912b83592fSScott Long  * through the xpt device.  The xpt device is really just a back door for
3922b83592fSScott Long  * accessing other devices and SIMs, so the right thing to do is to grab
3932b83592fSScott Long  * the appropriate SIM lock once the bus/SIM is located.
3942b83592fSScott Long  */
3958b8a9b1dSJustin T. Gibbs static int
39689c9c53dSPoul-Henning Kamp xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
3978b8a9b1dSJustin T. Gibbs {
3982b83592fSScott Long 	int error;
3998b8a9b1dSJustin T. Gibbs 
40025a2902cSScott Long 	if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
40125a2902cSScott Long 		error = cam_compat_ioctl(dev, &cmd, &addr, &flag, td);
40225a2902cSScott Long 		if (error == EAGAIN)
40325a2902cSScott Long 			return (xptdoioctl(dev, cmd, addr, flag, td));
40425a2902cSScott Long 	}
40525a2902cSScott Long 	return (error);
40625a2902cSScott Long }
40725a2902cSScott Long 
40825a2902cSScott Long static int
40925a2902cSScott Long xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
41025a2902cSScott Long {
41125a2902cSScott Long 	int error;
41225a2902cSScott Long 
4138b8a9b1dSJustin T. Gibbs 	error = 0;
4148b8a9b1dSJustin T. Gibbs 
4158b8a9b1dSJustin T. Gibbs 	switch(cmd) {
4168b8a9b1dSJustin T. Gibbs 	/*
4178b8a9b1dSJustin T. Gibbs 	 * For the transport layer CAMIOCOMMAND ioctl, we really only want
4188b8a9b1dSJustin T. Gibbs 	 * to accept CCB types that don't quite make sense to send through a
4198c7a96c5SScott Long 	 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
4208c7a96c5SScott Long 	 * in the CAM spec.
4218b8a9b1dSJustin T. Gibbs 	 */
4228b8a9b1dSJustin T. Gibbs 	case CAMIOCOMMAND: {
4238b8a9b1dSJustin T. Gibbs 		union ccb *ccb;
4248b8a9b1dSJustin T. Gibbs 		union ccb *inccb;
4252b83592fSScott Long 		struct cam_eb *bus;
4268b8a9b1dSJustin T. Gibbs 
4278b8a9b1dSJustin T. Gibbs 		inccb = (union ccb *)addr;
4288b8a9b1dSJustin T. Gibbs 
4292b83592fSScott Long 		bus = xpt_find_bus(inccb->ccb_h.path_id);
4300e85f214SMatt Jacob 		if (bus == NULL)
4310e85f214SMatt Jacob 			return (EINVAL);
4320e85f214SMatt Jacob 
4330e85f214SMatt Jacob 		switch (inccb->ccb_h.func_code) {
4340e85f214SMatt Jacob 		case XPT_SCAN_BUS:
4350e85f214SMatt Jacob 		case XPT_RESET_BUS:
4360e85f214SMatt Jacob 			if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
4370e85f214SMatt Jacob 			    inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
4380e85f214SMatt Jacob 				xpt_release_bus(bus);
4390e85f214SMatt Jacob 				return (EINVAL);
4400e85f214SMatt Jacob 			}
4410e85f214SMatt Jacob 			break;
4420e85f214SMatt Jacob 		case XPT_SCAN_TGT:
4430e85f214SMatt Jacob 			if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
4440e85f214SMatt Jacob 			    inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
4450e85f214SMatt Jacob 				xpt_release_bus(bus);
4460e85f214SMatt Jacob 				return (EINVAL);
4470e85f214SMatt Jacob 			}
4480e85f214SMatt Jacob 			break;
4490e85f214SMatt Jacob 		default:
4502b83592fSScott Long 			break;
4512b83592fSScott Long 		}
4522b83592fSScott Long 
4538b8a9b1dSJustin T. Gibbs 		switch(inccb->ccb_h.func_code) {
4548b8a9b1dSJustin T. Gibbs 		case XPT_SCAN_BUS:
4558b8a9b1dSJustin T. Gibbs 		case XPT_RESET_BUS:
4568c7a96c5SScott Long 		case XPT_PATH_INQ:
4578fcf57f5SJustin T. Gibbs 		case XPT_ENG_INQ:
4588b8a9b1dSJustin T. Gibbs 		case XPT_SCAN_LUN:
4590e85f214SMatt Jacob 		case XPT_SCAN_TGT:
4608b8a9b1dSJustin T. Gibbs 
4618008a935SScott Long 			ccb = xpt_alloc_ccb();
4622b83592fSScott Long 
4632b83592fSScott Long 			CAM_SIM_LOCK(bus->sim);
4646ee4d869SAlexander Motin 
4658b8a9b1dSJustin T. Gibbs 			/*
4668b8a9b1dSJustin T. Gibbs 			 * Create a path using the bus, target, and lun the
4678b8a9b1dSJustin T. Gibbs 			 * user passed in.
4688b8a9b1dSJustin T. Gibbs 			 */
469e5dfa058SAlexander Motin 			if (xpt_create_path(&ccb->ccb_h.path, NULL,
4708b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.path_id,
4718b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_id,
4728b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_lun) !=
4738b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP){
4748b8a9b1dSJustin T. Gibbs 				error = EINVAL;
4752b83592fSScott Long 				CAM_SIM_UNLOCK(bus->sim);
4768b8a9b1dSJustin T. Gibbs 				xpt_free_ccb(ccb);
4778b8a9b1dSJustin T. Gibbs 				break;
4788b8a9b1dSJustin T. Gibbs 			}
4798b8a9b1dSJustin T. Gibbs 			/* Ensure all of our fields are correct */
4808b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
4818b8a9b1dSJustin T. Gibbs 				      inccb->ccb_h.pinfo.priority);
4828b8a9b1dSJustin T. Gibbs 			xpt_merge_ccb(ccb, inccb);
4838b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.cbfcnp = xptdone;
4848b8a9b1dSJustin T. Gibbs 			cam_periph_runccb(ccb, NULL, 0, 0, NULL);
4858b8a9b1dSJustin T. Gibbs 			bcopy(ccb, inccb, sizeof(union ccb));
4868b8a9b1dSJustin T. Gibbs 			xpt_free_path(ccb->ccb_h.path);
4878b8a9b1dSJustin T. Gibbs 			xpt_free_ccb(ccb);
4882b83592fSScott Long 			CAM_SIM_UNLOCK(bus->sim);
4898b8a9b1dSJustin T. Gibbs 			break;
4908b8a9b1dSJustin T. Gibbs 
4918b8a9b1dSJustin T. Gibbs 		case XPT_DEBUG: {
4928b8a9b1dSJustin T. Gibbs 			union ccb ccb;
4938b8a9b1dSJustin T. Gibbs 
4948b8a9b1dSJustin T. Gibbs 			/*
495aa872be6SMatt Jacob 			 * This is an immediate CCB, so it's okay to
4968b8a9b1dSJustin T. Gibbs 			 * allocate it on the stack.
4978b8a9b1dSJustin T. Gibbs 			 */
4988b8a9b1dSJustin T. Gibbs 
4992b83592fSScott Long 			CAM_SIM_LOCK(bus->sim);
5002b83592fSScott Long 
5018b8a9b1dSJustin T. Gibbs 			/*
5028b8a9b1dSJustin T. Gibbs 			 * Create a path using the bus, target, and lun the
5038b8a9b1dSJustin T. Gibbs 			 * user passed in.
5048b8a9b1dSJustin T. Gibbs 			 */
505e5dfa058SAlexander Motin 			if (xpt_create_path(&ccb.ccb_h.path, NULL,
5068b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.path_id,
5078b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_id,
5088b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_lun) !=
5098b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP){
5108b8a9b1dSJustin T. Gibbs 				error = EINVAL;
5112ca9ba94SScott Long 				CAM_SIM_UNLOCK(bus->sim);
5128b8a9b1dSJustin T. Gibbs 				break;
5138b8a9b1dSJustin T. Gibbs 			}
5148b8a9b1dSJustin T. Gibbs 			/* Ensure all of our fields are correct */
5158b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
5168b8a9b1dSJustin T. Gibbs 				      inccb->ccb_h.pinfo.priority);
5178b8a9b1dSJustin T. Gibbs 			xpt_merge_ccb(&ccb, inccb);
5188b8a9b1dSJustin T. Gibbs 			ccb.ccb_h.cbfcnp = xptdone;
5198b8a9b1dSJustin T. Gibbs 			xpt_action(&ccb);
5208b8a9b1dSJustin T. Gibbs 			bcopy(&ccb, inccb, sizeof(union ccb));
5218b8a9b1dSJustin T. Gibbs 			xpt_free_path(ccb.ccb_h.path);
52280d6987cSAlexander Motin 			CAM_SIM_UNLOCK(bus->sim);
5238b8a9b1dSJustin T. Gibbs 			break;
5248b8a9b1dSJustin T. Gibbs 
5258b8a9b1dSJustin T. Gibbs 		}
5268b8a9b1dSJustin T. Gibbs 		case XPT_DEV_MATCH: {
5278b8a9b1dSJustin T. Gibbs 			struct cam_periph_map_info mapinfo;
52859190eaaSKenneth D. Merry 			struct cam_path *old_path;
5298b8a9b1dSJustin T. Gibbs 
5308b8a9b1dSJustin T. Gibbs 			/*
5318b8a9b1dSJustin T. Gibbs 			 * We can't deal with physical addresses for this
5328b8a9b1dSJustin T. Gibbs 			 * type of transaction.
5338b8a9b1dSJustin T. Gibbs 			 */
534dd0b4fb6SKonstantin Belousov 			if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
535dd0b4fb6SKonstantin Belousov 			    CAM_DATA_VADDR) {
5368b8a9b1dSJustin T. Gibbs 				error = EINVAL;
5378b8a9b1dSJustin T. Gibbs 				break;
5388b8a9b1dSJustin T. Gibbs 			}
53959190eaaSKenneth D. Merry 
54059190eaaSKenneth D. Merry 			/*
54159190eaaSKenneth D. Merry 			 * Save this in case the caller had it set to
54259190eaaSKenneth D. Merry 			 * something in particular.
54359190eaaSKenneth D. Merry 			 */
54459190eaaSKenneth D. Merry 			old_path = inccb->ccb_h.path;
54559190eaaSKenneth D. Merry 
54659190eaaSKenneth D. Merry 			/*
54759190eaaSKenneth D. Merry 			 * We really don't need a path for the matching
54859190eaaSKenneth D. Merry 			 * code.  The path is needed because of the
54959190eaaSKenneth D. Merry 			 * debugging statements in xpt_action().  They
55059190eaaSKenneth D. Merry 			 * assume that the CCB has a valid path.
55159190eaaSKenneth D. Merry 			 */
55259190eaaSKenneth D. Merry 			inccb->ccb_h.path = xpt_periph->path;
55359190eaaSKenneth D. Merry 
5548b8a9b1dSJustin T. Gibbs 			bzero(&mapinfo, sizeof(mapinfo));
5558b8a9b1dSJustin T. Gibbs 
5568b8a9b1dSJustin T. Gibbs 			/*
5578b8a9b1dSJustin T. Gibbs 			 * Map the pattern and match buffers into kernel
5588b8a9b1dSJustin T. Gibbs 			 * virtual address space.
5598b8a9b1dSJustin T. Gibbs 			 */
5608b8a9b1dSJustin T. Gibbs 			error = cam_periph_mapmem(inccb, &mapinfo);
5618b8a9b1dSJustin T. Gibbs 
56259190eaaSKenneth D. Merry 			if (error) {
56359190eaaSKenneth D. Merry 				inccb->ccb_h.path = old_path;
5648b8a9b1dSJustin T. Gibbs 				break;
56559190eaaSKenneth D. Merry 			}
5668b8a9b1dSJustin T. Gibbs 
5678b8a9b1dSJustin T. Gibbs 			/*
5688b8a9b1dSJustin T. Gibbs 			 * This is an immediate CCB, we can send it on directly.
5698b8a9b1dSJustin T. Gibbs 			 */
57092c40f40SAlexander Motin 			CAM_SIM_LOCK(xpt_path_sim(xpt_periph->path));
5718b8a9b1dSJustin T. Gibbs 			xpt_action(inccb);
57292c40f40SAlexander Motin 			CAM_SIM_UNLOCK(xpt_path_sim(xpt_periph->path));
5738b8a9b1dSJustin T. Gibbs 
5748b8a9b1dSJustin T. Gibbs 			/*
5758b8a9b1dSJustin T. Gibbs 			 * Map the buffers back into user space.
5768b8a9b1dSJustin T. Gibbs 			 */
5778b8a9b1dSJustin T. Gibbs 			cam_periph_unmapmem(inccb, &mapinfo);
5788b8a9b1dSJustin T. Gibbs 
57959190eaaSKenneth D. Merry 			inccb->ccb_h.path = old_path;
58059190eaaSKenneth D. Merry 
5818b8a9b1dSJustin T. Gibbs 			error = 0;
5828b8a9b1dSJustin T. Gibbs 			break;
5838b8a9b1dSJustin T. Gibbs 		}
5848b8a9b1dSJustin T. Gibbs 		default:
5858fcf57f5SJustin T. Gibbs 			error = ENOTSUP;
5868b8a9b1dSJustin T. Gibbs 			break;
5878b8a9b1dSJustin T. Gibbs 		}
588daddc001SScott Long 		xpt_release_bus(bus);
5898b8a9b1dSJustin T. Gibbs 		break;
5908b8a9b1dSJustin T. Gibbs 	}
5918b8a9b1dSJustin T. Gibbs 	/*
5928b8a9b1dSJustin T. Gibbs 	 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
5938b8a9b1dSJustin T. Gibbs 	 * with the periphal driver name and unit name filled in.  The other
5948b8a9b1dSJustin T. Gibbs 	 * fields don't really matter as input.  The passthrough driver name
5958b8a9b1dSJustin T. Gibbs 	 * ("pass"), and unit number are passed back in the ccb.  The current
5968b8a9b1dSJustin T. Gibbs 	 * device generation number, and the index into the device peripheral
5978b8a9b1dSJustin T. Gibbs 	 * driver list, and the status are also passed back.  Note that
5988b8a9b1dSJustin T. Gibbs 	 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
5998b8a9b1dSJustin T. Gibbs 	 * we never return a status of CAM_GDEVLIST_LIST_CHANGED.  It is
6008b8a9b1dSJustin T. Gibbs 	 * (or rather should be) impossible for the device peripheral driver
6018b8a9b1dSJustin T. Gibbs 	 * list to change since we look at the whole thing in one pass, and
60277dc25ccSScott Long 	 * we do it with lock protection.
6038b8a9b1dSJustin T. Gibbs 	 *
6048b8a9b1dSJustin T. Gibbs 	 */
6058b8a9b1dSJustin T. Gibbs 	case CAMGETPASSTHRU: {
6068b8a9b1dSJustin T. Gibbs 		union ccb *ccb;
6078b8a9b1dSJustin T. Gibbs 		struct cam_periph *periph;
6088b8a9b1dSJustin T. Gibbs 		struct periph_driver **p_drv;
6098b8a9b1dSJustin T. Gibbs 		char   *name;
6103393f8daSKenneth D. Merry 		u_int unit;
6113393f8daSKenneth D. Merry 		u_int cur_generation;
612621a60d4SKenneth D. Merry 		int base_periph_found;
6138b8a9b1dSJustin T. Gibbs 		int splbreaknum;
6148b8a9b1dSJustin T. Gibbs 
6158b8a9b1dSJustin T. Gibbs 		ccb = (union ccb *)addr;
6168b8a9b1dSJustin T. Gibbs 		unit = ccb->cgdl.unit_number;
6178b8a9b1dSJustin T. Gibbs 		name = ccb->cgdl.periph_name;
6188b8a9b1dSJustin T. Gibbs 		/*
61977dc25ccSScott Long 		 * Every 100 devices, we want to drop our lock protection to
6208b8a9b1dSJustin T. Gibbs 		 * give the software interrupt handler a chance to run.
6218b8a9b1dSJustin T. Gibbs 		 * Most systems won't run into this check, but this should
6228b8a9b1dSJustin T. Gibbs 		 * avoid starvation in the software interrupt handler in
6238b8a9b1dSJustin T. Gibbs 		 * large systems.
6248b8a9b1dSJustin T. Gibbs 		 */
6258b8a9b1dSJustin T. Gibbs 		splbreaknum = 100;
6268b8a9b1dSJustin T. Gibbs 
6278b8a9b1dSJustin T. Gibbs 		ccb = (union ccb *)addr;
6288b8a9b1dSJustin T. Gibbs 
629621a60d4SKenneth D. Merry 		base_periph_found = 0;
630621a60d4SKenneth D. Merry 
6318b8a9b1dSJustin T. Gibbs 		/*
6328b8a9b1dSJustin T. Gibbs 		 * Sanity check -- make sure we don't get a null peripheral
6338b8a9b1dSJustin T. Gibbs 		 * driver name.
6348b8a9b1dSJustin T. Gibbs 		 */
6358b8a9b1dSJustin T. Gibbs 		if (*ccb->cgdl.periph_name == '\0') {
6368b8a9b1dSJustin T. Gibbs 			error = EINVAL;
6378b8a9b1dSJustin T. Gibbs 			break;
6388b8a9b1dSJustin T. Gibbs 		}
6398b8a9b1dSJustin T. Gibbs 
6408b8a9b1dSJustin T. Gibbs 		/* Keep the list from changing while we traverse it */
6419a7c2696SAlexander Motin 		xpt_lock_buses();
6428b8a9b1dSJustin T. Gibbs ptstartover:
6432b83592fSScott Long 		cur_generation = xsoftc.xpt_generation;
6448b8a9b1dSJustin T. Gibbs 
6458b8a9b1dSJustin T. Gibbs 		/* first find our driver in the list of drivers */
6460b7c27b9SPeter Wemm 		for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
6478b8a9b1dSJustin T. Gibbs 			if (strcmp((*p_drv)->driver_name, name) == 0)
6488b8a9b1dSJustin T. Gibbs 				break;
6498b8a9b1dSJustin T. Gibbs 
6508b8a9b1dSJustin T. Gibbs 		if (*p_drv == NULL) {
6519a7c2696SAlexander Motin 			xpt_unlock_buses();
6528b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
6538b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
6548b8a9b1dSJustin T. Gibbs 			*ccb->cgdl.periph_name = '\0';
6558b8a9b1dSJustin T. Gibbs 			ccb->cgdl.unit_number = 0;
6568b8a9b1dSJustin T. Gibbs 			error = ENOENT;
6578b8a9b1dSJustin T. Gibbs 			break;
6588b8a9b1dSJustin T. Gibbs 		}
6598b8a9b1dSJustin T. Gibbs 
6608b8a9b1dSJustin T. Gibbs 		/*
6618b8a9b1dSJustin T. Gibbs 		 * Run through every peripheral instance of this driver
6628b8a9b1dSJustin T. Gibbs 		 * and check to see whether it matches the unit passed
6638b8a9b1dSJustin T. Gibbs 		 * in by the user.  If it does, get out of the loops and
6648b8a9b1dSJustin T. Gibbs 		 * find the passthrough driver associated with that
6658b8a9b1dSJustin T. Gibbs 		 * peripheral driver.
6668b8a9b1dSJustin T. Gibbs 		 */
6678b8a9b1dSJustin T. Gibbs 		for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
6688b8a9b1dSJustin T. Gibbs 		     periph = TAILQ_NEXT(periph, unit_links)) {
6698b8a9b1dSJustin T. Gibbs 
6708b8a9b1dSJustin T. Gibbs 			if (periph->unit_number == unit) {
6718b8a9b1dSJustin T. Gibbs 				break;
6728b8a9b1dSJustin T. Gibbs 			} else if (--splbreaknum == 0) {
6739a7c2696SAlexander Motin 				xpt_unlock_buses();
6749a7c2696SAlexander Motin 				xpt_lock_buses();
6758b8a9b1dSJustin T. Gibbs 				splbreaknum = 100;
6762b83592fSScott Long 				if (cur_generation != xsoftc.xpt_generation)
6778b8a9b1dSJustin T. Gibbs 				       goto ptstartover;
6788b8a9b1dSJustin T. Gibbs 			}
6798b8a9b1dSJustin T. Gibbs 		}
6808b8a9b1dSJustin T. Gibbs 		/*
6818b8a9b1dSJustin T. Gibbs 		 * If we found the peripheral driver that the user passed
6828b8a9b1dSJustin T. Gibbs 		 * in, go through all of the peripheral drivers for that
6838b8a9b1dSJustin T. Gibbs 		 * particular device and look for a passthrough driver.
6848b8a9b1dSJustin T. Gibbs 		 */
6858b8a9b1dSJustin T. Gibbs 		if (periph != NULL) {
6868b8a9b1dSJustin T. Gibbs 			struct cam_ed *device;
6878b8a9b1dSJustin T. Gibbs 			int i;
6888b8a9b1dSJustin T. Gibbs 
689621a60d4SKenneth D. Merry 			base_periph_found = 1;
6908b8a9b1dSJustin T. Gibbs 			device = periph->path->device;
691fc2ffbe6SPoul-Henning Kamp 			for (i = 0, periph = SLIST_FIRST(&device->periphs);
6928b8a9b1dSJustin T. Gibbs 			     periph != NULL;
693fc2ffbe6SPoul-Henning Kamp 			     periph = SLIST_NEXT(periph, periph_links), i++) {
6948b8a9b1dSJustin T. Gibbs 				/*
6958b8a9b1dSJustin T. Gibbs 				 * Check to see whether we have a
6968b8a9b1dSJustin T. Gibbs 				 * passthrough device or not.
6978b8a9b1dSJustin T. Gibbs 				 */
6988b8a9b1dSJustin T. Gibbs 				if (strcmp(periph->periph_name, "pass") == 0) {
6998b8a9b1dSJustin T. Gibbs 					/*
7008b8a9b1dSJustin T. Gibbs 					 * Fill in the getdevlist fields.
7018b8a9b1dSJustin T. Gibbs 					 */
7028b8a9b1dSJustin T. Gibbs 					strcpy(ccb->cgdl.periph_name,
7038b8a9b1dSJustin T. Gibbs 					       periph->periph_name);
7048b8a9b1dSJustin T. Gibbs 					ccb->cgdl.unit_number =
7058b8a9b1dSJustin T. Gibbs 						periph->unit_number;
706fc2ffbe6SPoul-Henning Kamp 					if (SLIST_NEXT(periph, periph_links))
7078b8a9b1dSJustin T. Gibbs 						ccb->cgdl.status =
7088b8a9b1dSJustin T. Gibbs 							CAM_GDEVLIST_MORE_DEVS;
7098b8a9b1dSJustin T. Gibbs 					else
7108b8a9b1dSJustin T. Gibbs 						ccb->cgdl.status =
7118b8a9b1dSJustin T. Gibbs 						       CAM_GDEVLIST_LAST_DEVICE;
7128b8a9b1dSJustin T. Gibbs 					ccb->cgdl.generation =
7138b8a9b1dSJustin T. Gibbs 						device->generation;
7148b8a9b1dSJustin T. Gibbs 					ccb->cgdl.index = i;
7158b8a9b1dSJustin T. Gibbs 					/*
7168b8a9b1dSJustin T. Gibbs 					 * Fill in some CCB header fields
7178b8a9b1dSJustin T. Gibbs 					 * that the user may want.
7188b8a9b1dSJustin T. Gibbs 					 */
7198b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.path_id =
7208b8a9b1dSJustin T. Gibbs 						periph->path->bus->path_id;
7218b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.target_id =
7228b8a9b1dSJustin T. Gibbs 						periph->path->target->target_id;
7238b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.target_lun =
7248b8a9b1dSJustin T. Gibbs 						periph->path->device->lun_id;
7258b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.status = CAM_REQ_CMP;
7268b8a9b1dSJustin T. Gibbs 					break;
7278b8a9b1dSJustin T. Gibbs 				}
7288b8a9b1dSJustin T. Gibbs 			}
7298b8a9b1dSJustin T. Gibbs 		}
7308b8a9b1dSJustin T. Gibbs 
7318b8a9b1dSJustin T. Gibbs 		/*
7328b8a9b1dSJustin T. Gibbs 		 * If the periph is null here, one of two things has
7338b8a9b1dSJustin T. Gibbs 		 * happened.  The first possibility is that we couldn't
7348b8a9b1dSJustin T. Gibbs 		 * find the unit number of the particular peripheral driver
7358b8a9b1dSJustin T. Gibbs 		 * that the user is asking about.  e.g. the user asks for
7368b8a9b1dSJustin T. Gibbs 		 * the passthrough driver for "da11".  We find the list of
7378b8a9b1dSJustin T. Gibbs 		 * "da" peripherals all right, but there is no unit 11.
7388b8a9b1dSJustin T. Gibbs 		 * The other possibility is that we went through the list
7398b8a9b1dSJustin T. Gibbs 		 * of peripheral drivers attached to the device structure,
7408b8a9b1dSJustin T. Gibbs 		 * but didn't find one with the name "pass".  Either way,
7418b8a9b1dSJustin T. Gibbs 		 * we return ENOENT, since we couldn't find something.
7428b8a9b1dSJustin T. Gibbs 		 */
7438b8a9b1dSJustin T. Gibbs 		if (periph == NULL) {
7448b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
7458b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
7468b8a9b1dSJustin T. Gibbs 			*ccb->cgdl.periph_name = '\0';
7478b8a9b1dSJustin T. Gibbs 			ccb->cgdl.unit_number = 0;
7488b8a9b1dSJustin T. Gibbs 			error = ENOENT;
749621a60d4SKenneth D. Merry 			/*
750621a60d4SKenneth D. Merry 			 * It is unfortunate that this is even necessary,
751621a60d4SKenneth D. Merry 			 * but there are many, many clueless users out there.
752621a60d4SKenneth D. Merry 			 * If this is true, the user is looking for the
753621a60d4SKenneth D. Merry 			 * passthrough driver, but doesn't have one in his
754621a60d4SKenneth D. Merry 			 * kernel.
755621a60d4SKenneth D. Merry 			 */
756621a60d4SKenneth D. Merry 			if (base_periph_found == 1) {
757621a60d4SKenneth D. Merry 				printf("xptioctl: pass driver is not in the "
758621a60d4SKenneth D. Merry 				       "kernel\n");
759935c968aSChristian Brueffer 				printf("xptioctl: put \"device pass\" in "
760621a60d4SKenneth D. Merry 				       "your kernel config file\n");
761621a60d4SKenneth D. Merry 			}
7628b8a9b1dSJustin T. Gibbs 		}
7639a7c2696SAlexander Motin 		xpt_unlock_buses();
7648b8a9b1dSJustin T. Gibbs 		break;
7658b8a9b1dSJustin T. Gibbs 		}
7668b8a9b1dSJustin T. Gibbs 	default:
7678b8a9b1dSJustin T. Gibbs 		error = ENOTTY;
7688b8a9b1dSJustin T. Gibbs 		break;
7698b8a9b1dSJustin T. Gibbs 	}
7708b8a9b1dSJustin T. Gibbs 
7718b8a9b1dSJustin T. Gibbs 	return(error);
7728b8a9b1dSJustin T. Gibbs }
7738b8a9b1dSJustin T. Gibbs 
77474bd1c10SNick Hibma static int
77574bd1c10SNick Hibma cam_module_event_handler(module_t mod, int what, void *arg)
77674bd1c10SNick Hibma {
7772b83592fSScott Long 	int error;
7782b83592fSScott Long 
7792b83592fSScott Long 	switch (what) {
7802b83592fSScott Long 	case MOD_LOAD:
7812b83592fSScott Long 		if ((error = xpt_init(NULL)) != 0)
7822b83592fSScott Long 			return (error);
7832b83592fSScott Long 		break;
7842b83592fSScott Long 	case MOD_UNLOAD:
78574bd1c10SNick Hibma 		return EBUSY;
7862b83592fSScott Long 	default:
7873e019deaSPoul-Henning Kamp 		return EOPNOTSUPP;
78874bd1c10SNick Hibma 	}
78974bd1c10SNick Hibma 
79074bd1c10SNick Hibma 	return 0;
79174bd1c10SNick Hibma }
79274bd1c10SNick Hibma 
79383c5d981SAlexander Motin static void
79483c5d981SAlexander Motin xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
79583c5d981SAlexander Motin {
79683c5d981SAlexander Motin 
79783c5d981SAlexander Motin 	if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
79883c5d981SAlexander Motin 		xpt_free_path(done_ccb->ccb_h.path);
79983c5d981SAlexander Motin 		xpt_free_ccb(done_ccb);
80083c5d981SAlexander Motin 	} else {
80183c5d981SAlexander Motin 		done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
80283c5d981SAlexander Motin 		(*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
80383c5d981SAlexander Motin 	}
80483c5d981SAlexander Motin 	xpt_release_boot();
80583c5d981SAlexander Motin }
80683c5d981SAlexander Motin 
8079e6461a2SMatt Jacob /* thread to handle bus rescans */
8089e6461a2SMatt Jacob static void
8099e6461a2SMatt Jacob xpt_scanner_thread(void *dummy)
8109e6461a2SMatt Jacob {
8119e6461a2SMatt Jacob 	union ccb	*ccb;
8122b83592fSScott Long 	struct cam_sim	*sim;
8132b83592fSScott Long 
8142b83592fSScott Long 	xpt_lock_buses();
81583c5d981SAlexander Motin 	for (;;) {
8165a73cc12SKenneth D. Merry 		if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
8172b83592fSScott Long 			msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
8182b83592fSScott Long 			       "ccb_scanq", 0);
81983c5d981SAlexander Motin 		if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
82083c5d981SAlexander Motin 			TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
8212b83592fSScott Long 			xpt_unlock_buses();
8222b83592fSScott Long 
8232b83592fSScott Long 			sim = ccb->ccb_h.path->bus->sim;
82411e4faceSScott Long 			CAM_SIM_LOCK(sim);
82583c5d981SAlexander Motin 			xpt_action(ccb);
82611e4faceSScott Long 			CAM_SIM_UNLOCK(sim);
82783c5d981SAlexander Motin 
82883c5d981SAlexander Motin 			xpt_lock_buses();
8299e6461a2SMatt Jacob 		}
8309e6461a2SMatt Jacob 	}
8319e6461a2SMatt Jacob }
8329e6461a2SMatt Jacob 
8339e6461a2SMatt Jacob void
8349e6461a2SMatt Jacob xpt_rescan(union ccb *ccb)
8359e6461a2SMatt Jacob {
8369e6461a2SMatt Jacob 	struct ccb_hdr *hdr;
8372b83592fSScott Long 
83883c5d981SAlexander Motin 	/* Prepare request */
8390e85f214SMatt Jacob 	if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
840411cadaeSAlexander Motin 	    ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
84183c5d981SAlexander Motin 		ccb->ccb_h.func_code = XPT_SCAN_BUS;
8420e85f214SMatt Jacob 	else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
8430e85f214SMatt Jacob 	    ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
8440e85f214SMatt Jacob 		ccb->ccb_h.func_code = XPT_SCAN_TGT;
8450e85f214SMatt Jacob 	else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
8460e85f214SMatt Jacob 	    ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
84783c5d981SAlexander Motin 		ccb->ccb_h.func_code = XPT_SCAN_LUN;
8480e85f214SMatt Jacob 	else {
8490e85f214SMatt Jacob 		xpt_print(ccb->ccb_h.path, "illegal scan path\n");
8500e85f214SMatt Jacob 		xpt_free_path(ccb->ccb_h.path);
8510e85f214SMatt Jacob 		xpt_free_ccb(ccb);
8520e85f214SMatt Jacob 		return;
8530e85f214SMatt Jacob 	}
85483c5d981SAlexander Motin 	ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
85583c5d981SAlexander Motin 	ccb->ccb_h.cbfcnp = xpt_rescan_done;
85683c5d981SAlexander Motin 	xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
85783c5d981SAlexander Motin 	/* Don't make duplicate entries for the same paths. */
8582b83592fSScott Long 	xpt_lock_buses();
85983c5d981SAlexander Motin 	if (ccb->ccb_h.ppriv_ptr1 == NULL) {
8602b83592fSScott Long 		TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
8619e6461a2SMatt Jacob 			if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
8625a73cc12SKenneth D. Merry 				wakeup(&xsoftc.ccb_scanq);
8632b83592fSScott Long 				xpt_unlock_buses();
8649e6461a2SMatt Jacob 				xpt_print(ccb->ccb_h.path, "rescan already queued\n");
8659e6461a2SMatt Jacob 				xpt_free_path(ccb->ccb_h.path);
8669e6461a2SMatt Jacob 				xpt_free_ccb(ccb);
8679e6461a2SMatt Jacob 				return;
8689e6461a2SMatt Jacob 			}
8699e6461a2SMatt Jacob 		}
87083c5d981SAlexander Motin 	}
8712b83592fSScott Long 	TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
87283c5d981SAlexander Motin 	xsoftc.buses_to_config++;
8732b83592fSScott Long 	wakeup(&xsoftc.ccb_scanq);
8742b83592fSScott Long 	xpt_unlock_buses();
8759e6461a2SMatt Jacob }
8769e6461a2SMatt Jacob 
8778b8a9b1dSJustin T. Gibbs /* Functions accessed by the peripheral drivers */
8782b83592fSScott Long static int
8799e6461a2SMatt Jacob xpt_init(void *dummy)
8808b8a9b1dSJustin T. Gibbs {
8818b8a9b1dSJustin T. Gibbs 	struct cam_sim *xpt_sim;
8828b8a9b1dSJustin T. Gibbs 	struct cam_path *path;
883434bbf6eSJustin T. Gibbs 	struct cam_devq *devq;
8848b8a9b1dSJustin T. Gibbs 	cam_status status;
8858b8a9b1dSJustin T. Gibbs 
8862b83592fSScott Long 	TAILQ_INIT(&xsoftc.xpt_busses);
8879758cc83SScott Long 	TAILQ_INIT(&cam_simq);
8882b83592fSScott Long 	TAILQ_INIT(&xsoftc.ccb_scanq);
8892b83592fSScott Long 	STAILQ_INIT(&xsoftc.highpowerq);
8902b83592fSScott Long 	xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
8918b8a9b1dSJustin T. Gibbs 
8929758cc83SScott Long 	mtx_init(&cam_simq_lock, "CAM SIMQ lock", NULL, MTX_DEF);
8932b83592fSScott Long 	mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
8942b83592fSScott Long 	mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF);
895ef3cf714SScott Long 
8966b156f61SSean Bruno #ifdef CAM_BOOT_DELAY
8976b156f61SSean Bruno 	/*
8986b156f61SSean Bruno 	 * Override this value at compile time to assist our users
8996b156f61SSean Bruno 	 * who don't use loader to boot a kernel.
9006b156f61SSean Bruno 	 */
9016b156f61SSean Bruno 	xsoftc.boot_delay = CAM_BOOT_DELAY;
9026b156f61SSean Bruno #endif
9038b8a9b1dSJustin T. Gibbs 	/*
9048b8a9b1dSJustin T. Gibbs 	 * The xpt layer is, itself, the equivelent of a SIM.
9058b8a9b1dSJustin T. Gibbs 	 * Allow 16 ccbs in the ccb pool for it.  This should
9068b8a9b1dSJustin T. Gibbs 	 * give decent parallelism when we probe busses and
9078b8a9b1dSJustin T. Gibbs 	 * perform other XPT functions.
9088b8a9b1dSJustin T. Gibbs 	 */
909434bbf6eSJustin T. Gibbs 	devq = cam_simq_alloc(16);
910434bbf6eSJustin T. Gibbs 	xpt_sim = cam_sim_alloc(xptaction,
911434bbf6eSJustin T. Gibbs 				xptpoll,
912434bbf6eSJustin T. Gibbs 				"xpt",
913434bbf6eSJustin T. Gibbs 				/*softc*/NULL,
914434bbf6eSJustin T. Gibbs 				/*unit*/0,
9152b83592fSScott Long 				/*mtx*/&xsoftc.xpt_lock,
916434bbf6eSJustin T. Gibbs 				/*max_dev_transactions*/0,
917434bbf6eSJustin T. Gibbs 				/*max_tagged_dev_transactions*/0,
918434bbf6eSJustin T. Gibbs 				devq);
9192b83592fSScott Long 	if (xpt_sim == NULL)
9202b83592fSScott Long 		return (ENOMEM);
9218b8a9b1dSJustin T. Gibbs 
9222b83592fSScott Long 	mtx_lock(&xsoftc.xpt_lock);
923b50569b7SScott Long 	if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
92483c5d981SAlexander Motin 		mtx_unlock(&xsoftc.xpt_lock);
925a2821e04SMatt Jacob 		printf("xpt_init: xpt_bus_register failed with status %#x,"
926df826980SMatt Jacob 		       " failing attach\n", status);
9272b83592fSScott Long 		return (EINVAL);
928df826980SMatt Jacob 	}
9298b8a9b1dSJustin T. Gibbs 
9308b8a9b1dSJustin T. Gibbs 	/*
9318b8a9b1dSJustin T. Gibbs 	 * Looking at the XPT from the SIM layer, the XPT is
9328b8a9b1dSJustin T. Gibbs 	 * the equivelent of a peripheral driver.  Allocate
9338b8a9b1dSJustin T. Gibbs 	 * a peripheral driver entry for us.
9348b8a9b1dSJustin T. Gibbs 	 */
9358b8a9b1dSJustin T. Gibbs 	if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
9368b8a9b1dSJustin T. Gibbs 				      CAM_TARGET_WILDCARD,
9378b8a9b1dSJustin T. Gibbs 				      CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
93883c5d981SAlexander Motin 		mtx_unlock(&xsoftc.xpt_lock);
9398b8a9b1dSJustin T. Gibbs 		printf("xpt_init: xpt_create_path failed with status %#x,"
9408b8a9b1dSJustin T. Gibbs 		       " failing attach\n", status);
9412b83592fSScott Long 		return (EINVAL);
9428b8a9b1dSJustin T. Gibbs 	}
9438b8a9b1dSJustin T. Gibbs 
944ee9c90c7SKenneth D. Merry 	cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
9452b83592fSScott Long 			 path, NULL, 0, xpt_sim);
9468b8a9b1dSJustin T. Gibbs 	xpt_free_path(path);
9472b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_lock);
94883c5d981SAlexander Motin 	/* Install our software interrupt handlers */
94983c5d981SAlexander Motin 	swi_add(NULL, "cambio", camisr, NULL, SWI_CAMBIO, INTR_MPSAFE, &cambio_ih);
9508b8a9b1dSJustin T. Gibbs 	/*
9518b8a9b1dSJustin T. Gibbs 	 * Register a callback for when interrupts are enabled.
9528b8a9b1dSJustin T. Gibbs 	 */
9532b83592fSScott Long 	xsoftc.xpt_config_hook =
9548b8a9b1dSJustin T. Gibbs 	    (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
9550dd50e9bSScott Long 					      M_CAMXPT, M_NOWAIT | M_ZERO);
9562b83592fSScott Long 	if (xsoftc.xpt_config_hook == NULL) {
9578b8a9b1dSJustin T. Gibbs 		printf("xpt_init: Cannot malloc config hook "
9588b8a9b1dSJustin T. Gibbs 		       "- failing attach\n");
9592b83592fSScott Long 		return (ENOMEM);
9608b8a9b1dSJustin T. Gibbs 	}
9612b83592fSScott Long 	xsoftc.xpt_config_hook->ich_func = xpt_config;
9622b83592fSScott Long 	if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
9630dd50e9bSScott Long 		free (xsoftc.xpt_config_hook, M_CAMXPT);
9648b8a9b1dSJustin T. Gibbs 		printf("xpt_init: config_intrhook_establish failed "
9658b8a9b1dSJustin T. Gibbs 		       "- failing attach\n");
9668b8a9b1dSJustin T. Gibbs 	}
9678b8a9b1dSJustin T. Gibbs 
9682b83592fSScott Long 	return (0);
9698b8a9b1dSJustin T. Gibbs }
9708b8a9b1dSJustin T. Gibbs 
9718b8a9b1dSJustin T. Gibbs static cam_status
9728b8a9b1dSJustin T. Gibbs xptregister(struct cam_periph *periph, void *arg)
9738b8a9b1dSJustin T. Gibbs {
9742b83592fSScott Long 	struct cam_sim *xpt_sim;
9752b83592fSScott Long 
9768b8a9b1dSJustin T. Gibbs 	if (periph == NULL) {
9778b8a9b1dSJustin T. Gibbs 		printf("xptregister: periph was NULL!!\n");
9788b8a9b1dSJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
9798b8a9b1dSJustin T. Gibbs 	}
9808b8a9b1dSJustin T. Gibbs 
9812b83592fSScott Long 	xpt_sim = (struct cam_sim *)arg;
9822b83592fSScott Long 	xpt_sim->softc = periph;
9838b8a9b1dSJustin T. Gibbs 	xpt_periph = periph;
9842b83592fSScott Long 	periph->softc = NULL;
9858b8a9b1dSJustin T. Gibbs 
9868b8a9b1dSJustin T. Gibbs 	return(CAM_REQ_CMP);
9878b8a9b1dSJustin T. Gibbs }
9888b8a9b1dSJustin T. Gibbs 
9898b8a9b1dSJustin T. Gibbs int32_t
9908b8a9b1dSJustin T. Gibbs xpt_add_periph(struct cam_periph *periph)
9918b8a9b1dSJustin T. Gibbs {
9928b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
9938b8a9b1dSJustin T. Gibbs 	int32_t	 status;
9948b8a9b1dSJustin T. Gibbs 	struct periph_list *periph_head;
9958b8a9b1dSJustin T. Gibbs 
9962b83592fSScott Long 	mtx_assert(periph->sim->mtx, MA_OWNED);
99768153f43SScott Long 
9988b8a9b1dSJustin T. Gibbs 	device = periph->path->device;
9998b8a9b1dSJustin T. Gibbs 
10008b8a9b1dSJustin T. Gibbs 	periph_head = &device->periphs;
10018b8a9b1dSJustin T. Gibbs 
10028b8a9b1dSJustin T. Gibbs 	status = CAM_REQ_CMP;
10038b8a9b1dSJustin T. Gibbs 
10048b8a9b1dSJustin T. Gibbs 	if (device != NULL) {
10058b8a9b1dSJustin T. Gibbs 		/*
10068b8a9b1dSJustin T. Gibbs 		 * Make room for this peripheral
10078b8a9b1dSJustin T. Gibbs 		 * so it will fit in the queue
10088b8a9b1dSJustin T. Gibbs 		 * when it's scheduled to run
10098b8a9b1dSJustin T. Gibbs 		 */
10108b8a9b1dSJustin T. Gibbs 		status = camq_resize(&device->drvq,
10118b8a9b1dSJustin T. Gibbs 				     device->drvq.array_size + 1);
10128b8a9b1dSJustin T. Gibbs 
10138b8a9b1dSJustin T. Gibbs 		device->generation++;
10148b8a9b1dSJustin T. Gibbs 
10158b8a9b1dSJustin T. Gibbs 		SLIST_INSERT_HEAD(periph_head, periph, periph_links);
10168b8a9b1dSJustin T. Gibbs 	}
10178b8a9b1dSJustin T. Gibbs 
10189a7c2696SAlexander Motin 	xpt_lock_buses();
101977dc25ccSScott Long 	xsoftc.xpt_generation++;
10209a7c2696SAlexander Motin 	xpt_unlock_buses();
10218b8a9b1dSJustin T. Gibbs 
10228b8a9b1dSJustin T. Gibbs 	return (status);
10238b8a9b1dSJustin T. Gibbs }
10248b8a9b1dSJustin T. Gibbs 
10258b8a9b1dSJustin T. Gibbs void
1026ea37f519SKenneth D. Merry xpt_remove_periph(struct cam_periph *periph, int topology_lock_held)
10278b8a9b1dSJustin T. Gibbs {
10288b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
10298b8a9b1dSJustin T. Gibbs 
10301fa9ee7dSEdward Tomasz Napierala 	mtx_assert(periph->sim->mtx, MA_OWNED);
103168153f43SScott Long 
10328b8a9b1dSJustin T. Gibbs 	device = periph->path->device;
10338b8a9b1dSJustin T. Gibbs 
10348b8a9b1dSJustin T. Gibbs 	if (device != NULL) {
10358b8a9b1dSJustin T. Gibbs 		struct periph_list *periph_head;
10368b8a9b1dSJustin T. Gibbs 
10378b8a9b1dSJustin T. Gibbs 		periph_head = &device->periphs;
10388b8a9b1dSJustin T. Gibbs 
10398b8a9b1dSJustin T. Gibbs 		/* Release the slot for this peripheral */
10408b8a9b1dSJustin T. Gibbs 		camq_resize(&device->drvq, device->drvq.array_size - 1);
10418b8a9b1dSJustin T. Gibbs 
10428b8a9b1dSJustin T. Gibbs 		device->generation++;
10438b8a9b1dSJustin T. Gibbs 
1044e3975643SJake Burkholder 		SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
10458b8a9b1dSJustin T. Gibbs 	}
10468b8a9b1dSJustin T. Gibbs 
1047ea37f519SKenneth D. Merry 	if (topology_lock_held == 0)
10489a7c2696SAlexander Motin 		xpt_lock_buses();
1049ea37f519SKenneth D. Merry 
105077dc25ccSScott Long 	xsoftc.xpt_generation++;
1051ea37f519SKenneth D. Merry 
1052ea37f519SKenneth D. Merry 	if (topology_lock_held == 0)
10539a7c2696SAlexander Motin 		xpt_unlock_buses();
10548b8a9b1dSJustin T. Gibbs }
10558b8a9b1dSJustin T. Gibbs 
10563393f8daSKenneth D. Merry 
10573393f8daSKenneth D. Merry void
10583393f8daSKenneth D. Merry xpt_announce_periph(struct cam_periph *periph, char *announce_string)
10593393f8daSKenneth D. Merry {
106057079b17SAlexander Motin 	struct	cam_path *path = periph->path;
10613393f8daSKenneth D. Merry 
10622b83592fSScott Long 	mtx_assert(periph->sim->mtx, MA_OWNED);
106368153f43SScott Long 
1064ad413009SAlexander Motin 	printf("%s%d at %s%d bus %d scbus%d target %d lun %d\n",
10653393f8daSKenneth D. Merry 	       periph->periph_name, periph->unit_number,
10663393f8daSKenneth D. Merry 	       path->bus->sim->sim_name,
10673393f8daSKenneth D. Merry 	       path->bus->sim->unit_number,
10683393f8daSKenneth D. Merry 	       path->bus->sim->bus_id,
1069ad413009SAlexander Motin 	       path->bus->path_id,
10703393f8daSKenneth D. Merry 	       path->target->target_id,
10713393f8daSKenneth D. Merry 	       path->device->lun_id);
10723393f8daSKenneth D. Merry 	printf("%s%d: ", periph->periph_name, periph->unit_number);
107352c9ce25SScott Long 	if (path->device->protocol == PROTO_SCSI)
10743393f8daSKenneth D. Merry 		scsi_print_inquiry(&path->device->inq_data);
107552c9ce25SScott Long 	else if (path->device->protocol == PROTO_ATA ||
107652c9ce25SScott Long 	    path->device->protocol == PROTO_SATAPM)
107752c9ce25SScott Long 		ata_print_ident(&path->device->ident_data);
10783089bb2eSAlexander Motin 	else if (path->device->protocol == PROTO_SEMB)
10793089bb2eSAlexander Motin 		semb_print_ident(
10803089bb2eSAlexander Motin 		    (struct sep_identify_data *)&path->device->ident_data);
108152c9ce25SScott Long 	else
108252c9ce25SScott Long 		printf("Unknown protocol device\n");
1083f053d777SMatt Jacob 	if (bootverbose && path->device->serial_num_len > 0) {
10843393f8daSKenneth D. Merry 		/* Don't wrap the screen  - print only the first 60 chars */
10853393f8daSKenneth D. Merry 		printf("%s%d: Serial Number %.60s\n", periph->periph_name,
10863393f8daSKenneth D. Merry 		       periph->unit_number, path->device->serial_num);
10873393f8daSKenneth D. Merry 	}
108857079b17SAlexander Motin 	/* Announce transport details. */
108957079b17SAlexander Motin 	(*(path->bus->xport->announce))(periph);
109057079b17SAlexander Motin 	/* Announce command queueing. */
10913393f8daSKenneth D. Merry 	if (path->device->inq_flags & SID_CmdQue
10923393f8daSKenneth D. Merry 	 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
10930aacc535SAlexander Motin 		printf("%s%d: Command Queueing enabled\n",
10943393f8daSKenneth D. Merry 		       periph->periph_name, periph->unit_number);
10953393f8daSKenneth D. Merry 	}
109657079b17SAlexander Motin 	/* Announce caller's details if they've passed in. */
10973393f8daSKenneth D. Merry 	if (announce_string != NULL)
10983393f8daSKenneth D. Merry 		printf("%s%d: %s\n", periph->periph_name,
10993393f8daSKenneth D. Merry 		       periph->unit_number, announce_string);
11003393f8daSKenneth D. Merry }
11018b8a9b1dSJustin T. Gibbs 
11026fb5c84eSSteven Hartland void
11036fb5c84eSSteven Hartland xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
11046fb5c84eSSteven Hartland {
11056fb5c84eSSteven Hartland 	if (quirks != 0) {
11066fb5c84eSSteven Hartland 		printf("%s%d: quirks=0x%b\n", periph->periph_name,
11076fb5c84eSSteven Hartland 		    periph->unit_number, quirks, bit_string);
11086fb5c84eSSteven Hartland 	}
11096fb5c84eSSteven Hartland }
11106fb5c84eSSteven Hartland 
11113501942bSJustin T. Gibbs int
11123501942bSJustin T. Gibbs xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
11133501942bSJustin T. Gibbs {
1114ccba7102SAlexander Motin 	int ret = -1, l;
11153501942bSJustin T. Gibbs 	struct ccb_dev_advinfo cdai;
1116ccba7102SAlexander Motin 	struct scsi_vpd_id_descriptor *idd;
11173501942bSJustin T. Gibbs 
11186884b662SAlexander Motin 	mtx_assert(path->bus->sim->mtx, MA_OWNED);
11196884b662SAlexander Motin 
11203501942bSJustin T. Gibbs 	memset(&cdai, 0, sizeof(cdai));
11213501942bSJustin T. Gibbs 	xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
11223501942bSJustin T. Gibbs 	cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
11233501942bSJustin T. Gibbs 	cdai.bufsiz = len;
11243501942bSJustin T. Gibbs 
11253501942bSJustin T. Gibbs 	if (!strcmp(attr, "GEOM::ident"))
11263501942bSJustin T. Gibbs 		cdai.buftype = CDAI_TYPE_SERIAL_NUM;
11273501942bSJustin T. Gibbs 	else if (!strcmp(attr, "GEOM::physpath"))
11283501942bSJustin T. Gibbs 		cdai.buftype = CDAI_TYPE_PHYS_PATH;
1129ccba7102SAlexander Motin 	else if (!strcmp(attr, "GEOM::lunid")) {
1130ccba7102SAlexander Motin 		cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1131ccba7102SAlexander Motin 		cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1132ccba7102SAlexander Motin 	} else
11333501942bSJustin T. Gibbs 		goto out;
11343501942bSJustin T. Gibbs 
11353501942bSJustin T. Gibbs 	cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
11363501942bSJustin T. Gibbs 	if (cdai.buf == NULL) {
11373501942bSJustin T. Gibbs 		ret = ENOMEM;
11383501942bSJustin T. Gibbs 		goto out;
11393501942bSJustin T. Gibbs 	}
11403501942bSJustin T. Gibbs 	xpt_action((union ccb *)&cdai); /* can only be synchronous */
11413501942bSJustin T. Gibbs 	if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
11423501942bSJustin T. Gibbs 		cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
11433501942bSJustin T. Gibbs 	if (cdai.provsiz == 0)
11443501942bSJustin T. Gibbs 		goto out;
1145ccba7102SAlexander Motin 	if (cdai.buftype == CDAI_TYPE_SCSI_DEVID) {
1146ccba7102SAlexander Motin 		idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1147ccba7102SAlexander Motin 		    cdai.provsiz, scsi_devid_is_lun_naa);
1148ccba7102SAlexander Motin 		if (idd == NULL)
1149ccba7102SAlexander Motin 			idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1150ccba7102SAlexander Motin 			    cdai.provsiz, scsi_devid_is_lun_eui64);
1151ccba7102SAlexander Motin 		if (idd == NULL)
1152ccba7102SAlexander Motin 			idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1153ccba7102SAlexander Motin 			    cdai.provsiz, scsi_devid_is_lun_t10);
1154ccba7102SAlexander Motin 		if (idd == NULL)
1155ccba7102SAlexander Motin 			idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1156ccba7102SAlexander Motin 			    cdai.provsiz, scsi_devid_is_lun_name);
1157ccba7102SAlexander Motin 		if (idd == NULL)
1158ccba7102SAlexander Motin 			goto out;
1159ccba7102SAlexander Motin 		ret = 0;
1160ccba7102SAlexander Motin 		if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_ASCII ||
1161ccba7102SAlexander Motin 		    (idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_UTF8) {
1162ccba7102SAlexander Motin 			l = strnlen(idd->identifier, idd->length);
1163ccba7102SAlexander Motin 			if (l < len) {
1164ccba7102SAlexander Motin 				bcopy(idd->identifier, buf, l);
1165ccba7102SAlexander Motin 				buf[l] = 0;
1166ccba7102SAlexander Motin 			} else
1167ccba7102SAlexander Motin 				ret = EFAULT;
1168ccba7102SAlexander Motin 		} else {
1169ccba7102SAlexander Motin 			if (idd->length * 2 < len) {
1170ccba7102SAlexander Motin 				for (l = 0; l < idd->length; l++)
1171ccba7102SAlexander Motin 					sprintf(buf + l * 2, "%02x",
1172ccba7102SAlexander Motin 					    idd->identifier[l]);
1173ccba7102SAlexander Motin 			} else
1174ccba7102SAlexander Motin 				ret = EFAULT;
1175ccba7102SAlexander Motin 		}
1176ccba7102SAlexander Motin 	} else {
11773501942bSJustin T. Gibbs 		ret = 0;
11783501942bSJustin T. Gibbs 		if (strlcpy(buf, cdai.buf, len) >= len)
11793501942bSJustin T. Gibbs 			ret = EFAULT;
1180ccba7102SAlexander Motin 	}
11813501942bSJustin T. Gibbs 
11823501942bSJustin T. Gibbs out:
11833501942bSJustin T. Gibbs 	if (cdai.buf != NULL)
11843501942bSJustin T. Gibbs 		free(cdai.buf, M_CAMXPT);
11853501942bSJustin T. Gibbs 	return ret;
11863501942bSJustin T. Gibbs }
11873501942bSJustin T. Gibbs 
11888b8a9b1dSJustin T. Gibbs static dev_match_ret
11893393f8daSKenneth D. Merry xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
11908b8a9b1dSJustin T. Gibbs 	    struct cam_eb *bus)
11918b8a9b1dSJustin T. Gibbs {
11928b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
11938b8a9b1dSJustin T. Gibbs 	int i;
11948b8a9b1dSJustin T. Gibbs 
11958b8a9b1dSJustin T. Gibbs 	retval = DM_RET_NONE;
11968b8a9b1dSJustin T. Gibbs 
11978b8a9b1dSJustin T. Gibbs 	/*
11988b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
11998b8a9b1dSJustin T. Gibbs 	 */
12008b8a9b1dSJustin T. Gibbs 	if (bus == NULL)
12018b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
12028b8a9b1dSJustin T. Gibbs 
12038b8a9b1dSJustin T. Gibbs 	/*
12048b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this bus matches no
12058b8a9b1dSJustin T. Gibbs 	 * matter what.
12068b8a9b1dSJustin T. Gibbs 	 */
12078b8a9b1dSJustin T. Gibbs 	if ((patterns == NULL) || (num_patterns == 0))
12088b8a9b1dSJustin T. Gibbs 		return(DM_RET_DESCEND | DM_RET_COPY);
12098b8a9b1dSJustin T. Gibbs 
12108b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
12118b8a9b1dSJustin T. Gibbs 		struct bus_match_pattern *cur_pattern;
12128b8a9b1dSJustin T. Gibbs 
12138b8a9b1dSJustin T. Gibbs 		/*
12148b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a bus node, we
12158b8a9b1dSJustin T. Gibbs 		 * aren't interested.  However, we do indicate to the
12168b8a9b1dSJustin T. Gibbs 		 * calling routine that we should continue descending the
12178b8a9b1dSJustin T. Gibbs 		 * tree, since the user wants to match against lower-level
12188b8a9b1dSJustin T. Gibbs 		 * EDT elements.
12198b8a9b1dSJustin T. Gibbs 		 */
12208b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_BUS) {
12218b8a9b1dSJustin T. Gibbs 			if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
12228b8a9b1dSJustin T. Gibbs 				retval |= DM_RET_DESCEND;
12238b8a9b1dSJustin T. Gibbs 			continue;
12248b8a9b1dSJustin T. Gibbs 		}
12258b8a9b1dSJustin T. Gibbs 
12268b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.bus_pattern;
12278b8a9b1dSJustin T. Gibbs 
12288b8a9b1dSJustin T. Gibbs 		/*
12298b8a9b1dSJustin T. Gibbs 		 * If they want to match any bus node, we give them any
12308b8a9b1dSJustin T. Gibbs 		 * device node.
12318b8a9b1dSJustin T. Gibbs 		 */
12328b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == BUS_MATCH_ANY) {
12338b8a9b1dSJustin T. Gibbs 			/* set the copy flag */
12348b8a9b1dSJustin T. Gibbs 			retval |= DM_RET_COPY;
12358b8a9b1dSJustin T. Gibbs 
12368b8a9b1dSJustin T. Gibbs 			/*
12378b8a9b1dSJustin T. Gibbs 			 * If we've already decided on an action, go ahead
12388b8a9b1dSJustin T. Gibbs 			 * and return.
12398b8a9b1dSJustin T. Gibbs 			 */
12408b8a9b1dSJustin T. Gibbs 			if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
12418b8a9b1dSJustin T. Gibbs 				return(retval);
12428b8a9b1dSJustin T. Gibbs 		}
12438b8a9b1dSJustin T. Gibbs 
12448b8a9b1dSJustin T. Gibbs 		/*
12458b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
12468b8a9b1dSJustin T. Gibbs 		 */
12478b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == BUS_MATCH_NONE)
12488b8a9b1dSJustin T. Gibbs 			continue;
12498b8a9b1dSJustin T. Gibbs 
12508b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
12518b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != bus->path_id))
12528b8a9b1dSJustin T. Gibbs 			continue;
12538b8a9b1dSJustin T. Gibbs 
12548b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
12558b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->bus_id != bus->sim->bus_id))
12568b8a9b1dSJustin T. Gibbs 			continue;
12578b8a9b1dSJustin T. Gibbs 
12588b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
12598b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->unit_number != bus->sim->unit_number))
12608b8a9b1dSJustin T. Gibbs 			continue;
12618b8a9b1dSJustin T. Gibbs 
12628b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
12638b8a9b1dSJustin T. Gibbs 		 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
12648b8a9b1dSJustin T. Gibbs 			     DEV_IDLEN) != 0))
12658b8a9b1dSJustin T. Gibbs 			continue;
12668b8a9b1dSJustin T. Gibbs 
12678b8a9b1dSJustin T. Gibbs 		/*
12688b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
12698b8a9b1dSJustin T. Gibbs 		 * information on this bus.  So tell the caller to copy the
12708b8a9b1dSJustin T. Gibbs 		 * data out.
12718b8a9b1dSJustin T. Gibbs 		 */
12728b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
12738b8a9b1dSJustin T. Gibbs 
12748b8a9b1dSJustin T. Gibbs 		/*
12758b8a9b1dSJustin T. Gibbs 		 * If the return action has been set to descend, then we
12768b8a9b1dSJustin T. Gibbs 		 * know that we've already seen a non-bus matching
12778b8a9b1dSJustin T. Gibbs 		 * expression, therefore we need to further descend the tree.
12788b8a9b1dSJustin T. Gibbs 		 * This won't change by continuing around the loop, so we
12798b8a9b1dSJustin T. Gibbs 		 * go ahead and return.  If we haven't seen a non-bus
12808b8a9b1dSJustin T. Gibbs 		 * matching expression, we keep going around the loop until
12818b8a9b1dSJustin T. Gibbs 		 * we exhaust the matching expressions.  We'll set the stop
12828b8a9b1dSJustin T. Gibbs 		 * flag once we fall out of the loop.
12838b8a9b1dSJustin T. Gibbs 		 */
12848b8a9b1dSJustin T. Gibbs 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
12858b8a9b1dSJustin T. Gibbs 			return(retval);
12868b8a9b1dSJustin T. Gibbs 	}
12878b8a9b1dSJustin T. Gibbs 
12888b8a9b1dSJustin T. Gibbs 	/*
12898b8a9b1dSJustin T. Gibbs 	 * If the return action hasn't been set to descend yet, that means
12908b8a9b1dSJustin T. Gibbs 	 * we haven't seen anything other than bus matching patterns.  So
12918b8a9b1dSJustin T. Gibbs 	 * tell the caller to stop descending the tree -- the user doesn't
12928b8a9b1dSJustin T. Gibbs 	 * want to match against lower level tree elements.
12938b8a9b1dSJustin T. Gibbs 	 */
12948b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
12958b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_STOP;
12968b8a9b1dSJustin T. Gibbs 
12978b8a9b1dSJustin T. Gibbs 	return(retval);
12988b8a9b1dSJustin T. Gibbs }
12998b8a9b1dSJustin T. Gibbs 
13008b8a9b1dSJustin T. Gibbs static dev_match_ret
13013393f8daSKenneth D. Merry xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
13028b8a9b1dSJustin T. Gibbs 	       struct cam_ed *device)
13038b8a9b1dSJustin T. Gibbs {
13048b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
13058b8a9b1dSJustin T. Gibbs 	int i;
13068b8a9b1dSJustin T. Gibbs 
13078b8a9b1dSJustin T. Gibbs 	retval = DM_RET_NONE;
13088b8a9b1dSJustin T. Gibbs 
13098b8a9b1dSJustin T. Gibbs 	/*
13108b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
13118b8a9b1dSJustin T. Gibbs 	 */
13128b8a9b1dSJustin T. Gibbs 	if (device == NULL)
13138b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
13148b8a9b1dSJustin T. Gibbs 
13158b8a9b1dSJustin T. Gibbs 	/*
13168b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this device matches no
13178b8a9b1dSJustin T. Gibbs 	 * matter what.
13188b8a9b1dSJustin T. Gibbs 	 */
131959e75884SColin Percival 	if ((patterns == NULL) || (num_patterns == 0))
13208b8a9b1dSJustin T. Gibbs 		return(DM_RET_DESCEND | DM_RET_COPY);
13218b8a9b1dSJustin T. Gibbs 
13228b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
13238b8a9b1dSJustin T. Gibbs 		struct device_match_pattern *cur_pattern;
13243501942bSJustin T. Gibbs 		struct scsi_vpd_device_id *device_id_page;
13258b8a9b1dSJustin T. Gibbs 
13268b8a9b1dSJustin T. Gibbs 		/*
13278b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a device node, we
13288b8a9b1dSJustin T. Gibbs 		 * aren't interested.
13298b8a9b1dSJustin T. Gibbs 		 */
13308b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_DEVICE) {
13318b8a9b1dSJustin T. Gibbs 			if ((patterns[i].type == DEV_MATCH_PERIPH)
13328b8a9b1dSJustin T. Gibbs 			 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
13338b8a9b1dSJustin T. Gibbs 				retval |= DM_RET_DESCEND;
13348b8a9b1dSJustin T. Gibbs 			continue;
13358b8a9b1dSJustin T. Gibbs 		}
13368b8a9b1dSJustin T. Gibbs 
13378b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.device_pattern;
13388b8a9b1dSJustin T. Gibbs 
13393501942bSJustin T. Gibbs 		/* Error out if mutually exclusive options are specified. */
13403501942bSJustin T. Gibbs 		if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
13413501942bSJustin T. Gibbs 		 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
13423501942bSJustin T. Gibbs 			return(DM_RET_ERROR);
13433501942bSJustin T. Gibbs 
13448b8a9b1dSJustin T. Gibbs 		/*
13458b8a9b1dSJustin T. Gibbs 		 * If they want to match any device node, we give them any
13468b8a9b1dSJustin T. Gibbs 		 * device node.
13478b8a9b1dSJustin T. Gibbs 		 */
13483501942bSJustin T. Gibbs 		if (cur_pattern->flags == DEV_MATCH_ANY)
13493501942bSJustin T. Gibbs 			goto copy_dev_node;
13508b8a9b1dSJustin T. Gibbs 
13518b8a9b1dSJustin T. Gibbs 		/*
13528b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
13538b8a9b1dSJustin T. Gibbs 		 */
13548b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == DEV_MATCH_NONE)
13558b8a9b1dSJustin T. Gibbs 			continue;
13568b8a9b1dSJustin T. Gibbs 
13578b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
13588b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != device->target->bus->path_id))
13598b8a9b1dSJustin T. Gibbs 			continue;
13608b8a9b1dSJustin T. Gibbs 
13618b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
13628b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->target_id != device->target->target_id))
13638b8a9b1dSJustin T. Gibbs 			continue;
13648b8a9b1dSJustin T. Gibbs 
13658b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
13668b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->target_lun != device->lun_id))
13678b8a9b1dSJustin T. Gibbs 			continue;
13688b8a9b1dSJustin T. Gibbs 
13698b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
13708b8a9b1dSJustin T. Gibbs 		 && (cam_quirkmatch((caddr_t)&device->inq_data,
13713501942bSJustin T. Gibbs 				    (caddr_t)&cur_pattern->data.inq_pat,
13723501942bSJustin T. Gibbs 				    1, sizeof(cur_pattern->data.inq_pat),
13738b8a9b1dSJustin T. Gibbs 				    scsi_static_inquiry_match) == NULL))
13748b8a9b1dSJustin T. Gibbs 			continue;
13758b8a9b1dSJustin T. Gibbs 
13763501942bSJustin T. Gibbs 		device_id_page = (struct scsi_vpd_device_id *)device->device_id;
13773501942bSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
13783501942bSJustin T. Gibbs 		 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
13793501942bSJustin T. Gibbs 		  || scsi_devid_match((uint8_t *)device_id_page->desc_list,
13803501942bSJustin T. Gibbs 				      device->device_id_len
13813501942bSJustin T. Gibbs 				    - SVPD_DEVICE_ID_HDR_LEN,
13823501942bSJustin T. Gibbs 				      cur_pattern->data.devid_pat.id,
13833501942bSJustin T. Gibbs 				      cur_pattern->data.devid_pat.id_len) != 0))
13843501942bSJustin T. Gibbs 			continue;
13853501942bSJustin T. Gibbs 
13863501942bSJustin T. Gibbs copy_dev_node:
13878b8a9b1dSJustin T. Gibbs 		/*
13888b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
13898b8a9b1dSJustin T. Gibbs 		 * information on this device.  So tell the caller to copy
13908b8a9b1dSJustin T. Gibbs 		 * the data out.
13918b8a9b1dSJustin T. Gibbs 		 */
13928b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
13938b8a9b1dSJustin T. Gibbs 
13948b8a9b1dSJustin T. Gibbs 		/*
13958b8a9b1dSJustin T. Gibbs 		 * If the return action has been set to descend, then we
13968b8a9b1dSJustin T. Gibbs 		 * know that we've already seen a peripheral matching
13978b8a9b1dSJustin T. Gibbs 		 * expression, therefore we need to further descend the tree.
13988b8a9b1dSJustin T. Gibbs 		 * This won't change by continuing around the loop, so we
13998b8a9b1dSJustin T. Gibbs 		 * go ahead and return.  If we haven't seen a peripheral
14008b8a9b1dSJustin T. Gibbs 		 * matching expression, we keep going around the loop until
14018b8a9b1dSJustin T. Gibbs 		 * we exhaust the matching expressions.  We'll set the stop
14028b8a9b1dSJustin T. Gibbs 		 * flag once we fall out of the loop.
14038b8a9b1dSJustin T. Gibbs 		 */
14048b8a9b1dSJustin T. Gibbs 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
14058b8a9b1dSJustin T. Gibbs 			return(retval);
14068b8a9b1dSJustin T. Gibbs 	}
14078b8a9b1dSJustin T. Gibbs 
14088b8a9b1dSJustin T. Gibbs 	/*
14098b8a9b1dSJustin T. Gibbs 	 * If the return action hasn't been set to descend yet, that means
14108b8a9b1dSJustin T. Gibbs 	 * we haven't seen any peripheral matching patterns.  So tell the
14118b8a9b1dSJustin T. Gibbs 	 * caller to stop descending the tree -- the user doesn't want to
14128b8a9b1dSJustin T. Gibbs 	 * match against lower level tree elements.
14138b8a9b1dSJustin T. Gibbs 	 */
14148b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
14158b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_STOP;
14168b8a9b1dSJustin T. Gibbs 
14178b8a9b1dSJustin T. Gibbs 	return(retval);
14188b8a9b1dSJustin T. Gibbs }
14198b8a9b1dSJustin T. Gibbs 
14208b8a9b1dSJustin T. Gibbs /*
14218b8a9b1dSJustin T. Gibbs  * Match a single peripheral against any number of match patterns.
14228b8a9b1dSJustin T. Gibbs  */
14238b8a9b1dSJustin T. Gibbs static dev_match_ret
14243393f8daSKenneth D. Merry xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
14258b8a9b1dSJustin T. Gibbs 	       struct cam_periph *periph)
14268b8a9b1dSJustin T. Gibbs {
14278b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
14288b8a9b1dSJustin T. Gibbs 	int i;
14298b8a9b1dSJustin T. Gibbs 
14308b8a9b1dSJustin T. Gibbs 	/*
14318b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
14328b8a9b1dSJustin T. Gibbs 	 */
14338b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
14348b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
14358b8a9b1dSJustin T. Gibbs 
14368b8a9b1dSJustin T. Gibbs 	/*
14378b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this peripheral matches no
14388b8a9b1dSJustin T. Gibbs 	 * matter what.
14398b8a9b1dSJustin T. Gibbs 	 */
14408b8a9b1dSJustin T. Gibbs 	if ((patterns == NULL) || (num_patterns == 0))
14418b8a9b1dSJustin T. Gibbs 		return(DM_RET_STOP | DM_RET_COPY);
14428b8a9b1dSJustin T. Gibbs 
14438b8a9b1dSJustin T. Gibbs 	/*
14448b8a9b1dSJustin T. Gibbs 	 * There aren't any nodes below a peripheral node, so there's no
14458b8a9b1dSJustin T. Gibbs 	 * reason to descend the tree any further.
14468b8a9b1dSJustin T. Gibbs 	 */
14478b8a9b1dSJustin T. Gibbs 	retval = DM_RET_STOP;
14488b8a9b1dSJustin T. Gibbs 
14498b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
14508b8a9b1dSJustin T. Gibbs 		struct periph_match_pattern *cur_pattern;
14518b8a9b1dSJustin T. Gibbs 
14528b8a9b1dSJustin T. Gibbs 		/*
14538b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a peripheral, we
14548b8a9b1dSJustin T. Gibbs 		 * aren't interested.
14558b8a9b1dSJustin T. Gibbs 		 */
14568b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_PERIPH)
14578b8a9b1dSJustin T. Gibbs 			continue;
14588b8a9b1dSJustin T. Gibbs 
14598b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.periph_pattern;
14608b8a9b1dSJustin T. Gibbs 
14618b8a9b1dSJustin T. Gibbs 		/*
14628b8a9b1dSJustin T. Gibbs 		 * If they want to match on anything, then we will do so.
14638b8a9b1dSJustin T. Gibbs 		 */
14648b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == PERIPH_MATCH_ANY) {
14658b8a9b1dSJustin T. Gibbs 			/* set the copy flag */
14668b8a9b1dSJustin T. Gibbs 			retval |= DM_RET_COPY;
14678b8a9b1dSJustin T. Gibbs 
14688b8a9b1dSJustin T. Gibbs 			/*
14698b8a9b1dSJustin T. Gibbs 			 * We've already set the return action to stop,
14708b8a9b1dSJustin T. Gibbs 			 * since there are no nodes below peripherals in
14718b8a9b1dSJustin T. Gibbs 			 * the tree.
14728b8a9b1dSJustin T. Gibbs 			 */
14738b8a9b1dSJustin T. Gibbs 			return(retval);
14748b8a9b1dSJustin T. Gibbs 		}
14758b8a9b1dSJustin T. Gibbs 
14768b8a9b1dSJustin T. Gibbs 		/*
14778b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
14788b8a9b1dSJustin T. Gibbs 		 */
14798b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == PERIPH_MATCH_NONE)
14808b8a9b1dSJustin T. Gibbs 			continue;
14818b8a9b1dSJustin T. Gibbs 
14828b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
14838b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != periph->path->bus->path_id))
14848b8a9b1dSJustin T. Gibbs 			continue;
14858b8a9b1dSJustin T. Gibbs 
14868b8a9b1dSJustin T. Gibbs 		/*
14878b8a9b1dSJustin T. Gibbs 		 * For the target and lun id's, we have to make sure the
14888b8a9b1dSJustin T. Gibbs 		 * target and lun pointers aren't NULL.  The xpt peripheral
14898b8a9b1dSJustin T. Gibbs 		 * has a wildcard target and device.
14908b8a9b1dSJustin T. Gibbs 		 */
14918b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
14928b8a9b1dSJustin T. Gibbs 		 && ((periph->path->target == NULL)
14938b8a9b1dSJustin T. Gibbs 		 ||(cur_pattern->target_id != periph->path->target->target_id)))
14948b8a9b1dSJustin T. Gibbs 			continue;
14958b8a9b1dSJustin T. Gibbs 
14968b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
14978b8a9b1dSJustin T. Gibbs 		 && ((periph->path->device == NULL)
14988b8a9b1dSJustin T. Gibbs 		 || (cur_pattern->target_lun != periph->path->device->lun_id)))
14998b8a9b1dSJustin T. Gibbs 			continue;
15008b8a9b1dSJustin T. Gibbs 
15018b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
15028b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->unit_number != periph->unit_number))
15038b8a9b1dSJustin T. Gibbs 			continue;
15048b8a9b1dSJustin T. Gibbs 
15058b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
15068b8a9b1dSJustin T. Gibbs 		 && (strncmp(cur_pattern->periph_name, periph->periph_name,
15078b8a9b1dSJustin T. Gibbs 			     DEV_IDLEN) != 0))
15088b8a9b1dSJustin T. Gibbs 			continue;
15098b8a9b1dSJustin T. Gibbs 
15108b8a9b1dSJustin T. Gibbs 		/*
15118b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
15128b8a9b1dSJustin T. Gibbs 		 * information on this peripheral.  So tell the caller to
15138b8a9b1dSJustin T. Gibbs 		 * copy the data out.
15148b8a9b1dSJustin T. Gibbs 		 */
15158b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
15168b8a9b1dSJustin T. Gibbs 
15178b8a9b1dSJustin T. Gibbs 		/*
15188b8a9b1dSJustin T. Gibbs 		 * The return action has already been set to stop, since
15198b8a9b1dSJustin T. Gibbs 		 * peripherals don't have any nodes below them in the EDT.
15208b8a9b1dSJustin T. Gibbs 		 */
15218b8a9b1dSJustin T. Gibbs 		return(retval);
15228b8a9b1dSJustin T. Gibbs 	}
15238b8a9b1dSJustin T. Gibbs 
15248b8a9b1dSJustin T. Gibbs 	/*
15258b8a9b1dSJustin T. Gibbs 	 * If we get to this point, the peripheral that was passed in
15268b8a9b1dSJustin T. Gibbs 	 * doesn't match any of the patterns.
15278b8a9b1dSJustin T. Gibbs 	 */
15288b8a9b1dSJustin T. Gibbs 	return(retval);
15298b8a9b1dSJustin T. Gibbs }
15308b8a9b1dSJustin T. Gibbs 
15318b8a9b1dSJustin T. Gibbs static int
15328b8a9b1dSJustin T. Gibbs xptedtbusfunc(struct cam_eb *bus, void *arg)
15338b8a9b1dSJustin T. Gibbs {
15348b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
15358b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
15368b8a9b1dSJustin T. Gibbs 
15378b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
15388b8a9b1dSJustin T. Gibbs 
15398b8a9b1dSJustin T. Gibbs 	/*
15408b8a9b1dSJustin T. Gibbs 	 * If our position is for something deeper in the tree, that means
15418b8a9b1dSJustin T. Gibbs 	 * that we've already seen this node.  So, we keep going down.
15428b8a9b1dSJustin T. Gibbs 	 */
15438b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
15448b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == bus)
15458b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
15468b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target != NULL))
15478b8a9b1dSJustin T. Gibbs 		retval = DM_RET_DESCEND;
15488b8a9b1dSJustin T. Gibbs 	else
15498b8a9b1dSJustin T. Gibbs 		retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
15508b8a9b1dSJustin T. Gibbs 
15518b8a9b1dSJustin T. Gibbs 	/*
15528b8a9b1dSJustin T. Gibbs 	 * If we got an error, bail out of the search.
15538b8a9b1dSJustin T. Gibbs 	 */
15548b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
15558b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
15568b8a9b1dSJustin T. Gibbs 		return(0);
15578b8a9b1dSJustin T. Gibbs 	}
15588b8a9b1dSJustin T. Gibbs 
15598b8a9b1dSJustin T. Gibbs 	/*
15608b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this bus out.
15618b8a9b1dSJustin T. Gibbs 	 */
15628b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
15638b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
15648b8a9b1dSJustin T. Gibbs 
15658b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
15668b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
15678b8a9b1dSJustin T. Gibbs 
15688b8a9b1dSJustin T. Gibbs 		/*
15698b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
15708b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
15718b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
15728b8a9b1dSJustin T. Gibbs 		 */
15738b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
15748b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
15758b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
15768b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
15778b8a9b1dSJustin T. Gibbs 
15788b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = bus;
15798b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
15802b83592fSScott Long 				xsoftc.bus_generation;
15818b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
15828b8a9b1dSJustin T. Gibbs 			return(0);
15838b8a9b1dSJustin T. Gibbs 		}
15848b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
15858b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
15868b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_BUS;
15878b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.path_id = bus->path_id;
15888b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
15898b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.unit_number =
15908b8a9b1dSJustin T. Gibbs 			bus->sim->unit_number;
15918b8a9b1dSJustin T. Gibbs 		strncpy(cdm->matches[j].result.bus_result.dev_name,
15928b8a9b1dSJustin T. Gibbs 			bus->sim->sim_name, DEV_IDLEN);
15938b8a9b1dSJustin T. Gibbs 	}
15948b8a9b1dSJustin T. Gibbs 
15958b8a9b1dSJustin T. Gibbs 	/*
15968b8a9b1dSJustin T. Gibbs 	 * If the user is only interested in busses, there's no
15978b8a9b1dSJustin T. Gibbs 	 * reason to descend to the next level in the tree.
15988b8a9b1dSJustin T. Gibbs 	 */
15998b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
16008b8a9b1dSJustin T. Gibbs 		return(1);
16018b8a9b1dSJustin T. Gibbs 
16028b8a9b1dSJustin T. Gibbs 	/*
16038b8a9b1dSJustin T. Gibbs 	 * If there is a target generation recorded, check it to
16048b8a9b1dSJustin T. Gibbs 	 * make sure the target list hasn't changed.
16058b8a9b1dSJustin T. Gibbs 	 */
16068b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
16078b8a9b1dSJustin T. Gibbs 	 && (bus == cdm->pos.cookie.bus)
16088b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
16098b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
16108b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
16118b8a9b1dSJustin T. Gibbs 	     bus->generation)) {
16128b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
16138b8a9b1dSJustin T. Gibbs 		return(0);
16148b8a9b1dSJustin T. Gibbs 	}
16158b8a9b1dSJustin T. Gibbs 
16168b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
16178b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == bus)
16188b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
16198b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target != NULL))
16208b8a9b1dSJustin T. Gibbs 		return(xpttargettraverse(bus,
16218b8a9b1dSJustin T. Gibbs 					(struct cam_et *)cdm->pos.cookie.target,
16228b8a9b1dSJustin T. Gibbs 					 xptedttargetfunc, arg));
16238b8a9b1dSJustin T. Gibbs 	else
16248b8a9b1dSJustin T. Gibbs 		return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
16258b8a9b1dSJustin T. Gibbs }
16268b8a9b1dSJustin T. Gibbs 
16278b8a9b1dSJustin T. Gibbs static int
16288b8a9b1dSJustin T. Gibbs xptedttargetfunc(struct cam_et *target, void *arg)
16298b8a9b1dSJustin T. Gibbs {
16308b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
16318b8a9b1dSJustin T. Gibbs 
16328b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
16338b8a9b1dSJustin T. Gibbs 
16348b8a9b1dSJustin T. Gibbs 	/*
16358b8a9b1dSJustin T. Gibbs 	 * If there is a device list generation recorded, check it to
16368b8a9b1dSJustin T. Gibbs 	 * make sure the device list hasn't changed.
16378b8a9b1dSJustin T. Gibbs 	 */
16388b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
16398b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == target->bus)
16408b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
16418b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == target)
16428b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
16438b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
16448b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
16458b8a9b1dSJustin T. Gibbs 	     target->generation)) {
16468b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
16478b8a9b1dSJustin T. Gibbs 		return(0);
16488b8a9b1dSJustin T. Gibbs 	}
16498b8a9b1dSJustin T. Gibbs 
16508b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
16518b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == target->bus)
16528b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
16538b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == target)
16548b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
16558b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device != NULL))
16568b8a9b1dSJustin T. Gibbs 		return(xptdevicetraverse(target,
16578b8a9b1dSJustin T. Gibbs 					(struct cam_ed *)cdm->pos.cookie.device,
16588b8a9b1dSJustin T. Gibbs 					 xptedtdevicefunc, arg));
16598b8a9b1dSJustin T. Gibbs 	else
16608b8a9b1dSJustin T. Gibbs 		return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
16618b8a9b1dSJustin T. Gibbs }
16628b8a9b1dSJustin T. Gibbs 
16638b8a9b1dSJustin T. Gibbs static int
16648b8a9b1dSJustin T. Gibbs xptedtdevicefunc(struct cam_ed *device, void *arg)
16658b8a9b1dSJustin T. Gibbs {
16668b8a9b1dSJustin T. Gibbs 
16678b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
16688b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
16698b8a9b1dSJustin T. Gibbs 
16708b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
16718b8a9b1dSJustin T. Gibbs 
16728b8a9b1dSJustin T. Gibbs 	/*
16738b8a9b1dSJustin T. Gibbs 	 * If our position is for something deeper in the tree, that means
16748b8a9b1dSJustin T. Gibbs 	 * that we've already seen this node.  So, we keep going down.
16758b8a9b1dSJustin T. Gibbs 	 */
16768b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
16778b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device == device)
16788b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
16798b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.periph != NULL))
16808b8a9b1dSJustin T. Gibbs 		retval = DM_RET_DESCEND;
16818b8a9b1dSJustin T. Gibbs 	else
16828b8a9b1dSJustin T. Gibbs 		retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
16838b8a9b1dSJustin T. Gibbs 					device);
16848b8a9b1dSJustin T. Gibbs 
16858b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
16868b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
16878b8a9b1dSJustin T. Gibbs 		return(0);
16888b8a9b1dSJustin T. Gibbs 	}
16898b8a9b1dSJustin T. Gibbs 
16908b8a9b1dSJustin T. Gibbs 	/*
16918b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this device out.
16928b8a9b1dSJustin T. Gibbs 	 */
16938b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
16948b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
16958b8a9b1dSJustin T. Gibbs 
16968b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
16978b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
16988b8a9b1dSJustin T. Gibbs 
16998b8a9b1dSJustin T. Gibbs 		/*
17008b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
17018b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
17028b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
17038b8a9b1dSJustin T. Gibbs 		 */
17048b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
17058b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
17068b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
17078b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
17088b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
17098b8a9b1dSJustin T. Gibbs 
17108b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = device->target->bus;
17118b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
17122b83592fSScott Long 				xsoftc.bus_generation;
17138b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.target = device->target;
17148b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_TARGET_GENERATION] =
17158b8a9b1dSJustin T. Gibbs 				device->target->bus->generation;
17168b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.device = device;
17178b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_DEV_GENERATION] =
17188b8a9b1dSJustin T. Gibbs 				device->target->generation;
17198b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
17208b8a9b1dSJustin T. Gibbs 			return(0);
17218b8a9b1dSJustin T. Gibbs 		}
17228b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
17238b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
17248b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_DEVICE;
17258b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.path_id =
17268b8a9b1dSJustin T. Gibbs 			device->target->bus->path_id;
17278b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.target_id =
17288b8a9b1dSJustin T. Gibbs 			device->target->target_id;
17298b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.target_lun =
17308b8a9b1dSJustin T. Gibbs 			device->lun_id;
173152c9ce25SScott Long 		cdm->matches[j].result.device_result.protocol =
173252c9ce25SScott Long 			device->protocol;
17338b8a9b1dSJustin T. Gibbs 		bcopy(&device->inq_data,
17348b8a9b1dSJustin T. Gibbs 		      &cdm->matches[j].result.device_result.inq_data,
17358b8a9b1dSJustin T. Gibbs 		      sizeof(struct scsi_inquiry_data));
173652c9ce25SScott Long 		bcopy(&device->ident_data,
173752c9ce25SScott Long 		      &cdm->matches[j].result.device_result.ident_data,
173852c9ce25SScott Long 		      sizeof(struct ata_params));
17399deea857SKenneth D. Merry 
17409deea857SKenneth D. Merry 		/* Let the user know whether this device is unconfigured */
17419deea857SKenneth D. Merry 		if (device->flags & CAM_DEV_UNCONFIGURED)
17429deea857SKenneth D. Merry 			cdm->matches[j].result.device_result.flags =
17439deea857SKenneth D. Merry 				DEV_RESULT_UNCONFIGURED;
17449deea857SKenneth D. Merry 		else
17459deea857SKenneth D. Merry 			cdm->matches[j].result.device_result.flags =
17469deea857SKenneth D. Merry 				DEV_RESULT_NOFLAG;
17478b8a9b1dSJustin T. Gibbs 	}
17488b8a9b1dSJustin T. Gibbs 
17498b8a9b1dSJustin T. Gibbs 	/*
17508b8a9b1dSJustin T. Gibbs 	 * If the user isn't interested in peripherals, don't descend
17518b8a9b1dSJustin T. Gibbs 	 * the tree any further.
17528b8a9b1dSJustin T. Gibbs 	 */
17538b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
17548b8a9b1dSJustin T. Gibbs 		return(1);
17558b8a9b1dSJustin T. Gibbs 
17568b8a9b1dSJustin T. Gibbs 	/*
17578b8a9b1dSJustin T. Gibbs 	 * If there is a peripheral list generation recorded, make sure
17588b8a9b1dSJustin T. Gibbs 	 * it hasn't changed.
17598b8a9b1dSJustin T. Gibbs 	 */
17608b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
17618b8a9b1dSJustin T. Gibbs 	 && (device->target->bus == cdm->pos.cookie.bus)
17628b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
17638b8a9b1dSJustin T. Gibbs 	 && (device->target == cdm->pos.cookie.target)
17648b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
17658b8a9b1dSJustin T. Gibbs 	 && (device == cdm->pos.cookie.device)
17668b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
17678b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
17688b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
17698b8a9b1dSJustin T. Gibbs 	     device->generation)){
17708b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
17718b8a9b1dSJustin T. Gibbs 		return(0);
17728b8a9b1dSJustin T. Gibbs 	}
17738b8a9b1dSJustin T. Gibbs 
17748b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
17758b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == device->target->bus)
17768b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
17778b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == device->target)
17788b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
17798b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device == device)
17808b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
17818b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.periph != NULL))
17828b8a9b1dSJustin T. Gibbs 		return(xptperiphtraverse(device,
17838b8a9b1dSJustin T. Gibbs 				(struct cam_periph *)cdm->pos.cookie.periph,
17848b8a9b1dSJustin T. Gibbs 				xptedtperiphfunc, arg));
17858b8a9b1dSJustin T. Gibbs 	else
17868b8a9b1dSJustin T. Gibbs 		return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
17878b8a9b1dSJustin T. Gibbs }
17888b8a9b1dSJustin T. Gibbs 
17898b8a9b1dSJustin T. Gibbs static int
17908b8a9b1dSJustin T. Gibbs xptedtperiphfunc(struct cam_periph *periph, void *arg)
17918b8a9b1dSJustin T. Gibbs {
17928b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
17938b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
17948b8a9b1dSJustin T. Gibbs 
17958b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
17968b8a9b1dSJustin T. Gibbs 
17978b8a9b1dSJustin T. Gibbs 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
17988b8a9b1dSJustin T. Gibbs 
17998b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
18008b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
18018b8a9b1dSJustin T. Gibbs 		return(0);
18028b8a9b1dSJustin T. Gibbs 	}
18038b8a9b1dSJustin T. Gibbs 
18048b8a9b1dSJustin T. Gibbs 	/*
18058b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this peripheral out.
18068b8a9b1dSJustin T. Gibbs 	 */
18078b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
18088b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
18098b8a9b1dSJustin T. Gibbs 
18108b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
18118b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
18128b8a9b1dSJustin T. Gibbs 
18138b8a9b1dSJustin T. Gibbs 		/*
18148b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
18158b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
18168b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
18178b8a9b1dSJustin T. Gibbs 		 */
18188b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
18198b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
18208b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
18218b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
18228b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
18238b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PERIPH;
18248b8a9b1dSJustin T. Gibbs 
18258b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = periph->path->bus;
18268b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
18272b83592fSScott Long 				xsoftc.bus_generation;
18288b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.target = periph->path->target;
18298b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_TARGET_GENERATION] =
18308b8a9b1dSJustin T. Gibbs 				periph->path->bus->generation;
18318b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.device = periph->path->device;
18328b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_DEV_GENERATION] =
18338b8a9b1dSJustin T. Gibbs 				periph->path->target->generation;
18348b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.periph = periph;
18358b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
18368b8a9b1dSJustin T. Gibbs 				periph->path->device->generation;
18378b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
18388b8a9b1dSJustin T. Gibbs 			return(0);
18398b8a9b1dSJustin T. Gibbs 		}
18408b8a9b1dSJustin T. Gibbs 
18418b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
18428b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
18438b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_PERIPH;
18448b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.path_id =
18458b8a9b1dSJustin T. Gibbs 			periph->path->bus->path_id;
18468b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.target_id =
18478b8a9b1dSJustin T. Gibbs 			periph->path->target->target_id;
18488b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.target_lun =
18498b8a9b1dSJustin T. Gibbs 			periph->path->device->lun_id;
18508b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.unit_number =
18518b8a9b1dSJustin T. Gibbs 			periph->unit_number;
18528b8a9b1dSJustin T. Gibbs 		strncpy(cdm->matches[j].result.periph_result.periph_name,
18538b8a9b1dSJustin T. Gibbs 			periph->periph_name, DEV_IDLEN);
18548b8a9b1dSJustin T. Gibbs 	}
18558b8a9b1dSJustin T. Gibbs 
18568b8a9b1dSJustin T. Gibbs 	return(1);
18578b8a9b1dSJustin T. Gibbs }
18588b8a9b1dSJustin T. Gibbs 
18598b8a9b1dSJustin T. Gibbs static int
18608b8a9b1dSJustin T. Gibbs xptedtmatch(struct ccb_dev_match *cdm)
18618b8a9b1dSJustin T. Gibbs {
18628b8a9b1dSJustin T. Gibbs 	int ret;
18638b8a9b1dSJustin T. Gibbs 
18648b8a9b1dSJustin T. Gibbs 	cdm->num_matches = 0;
18658b8a9b1dSJustin T. Gibbs 
18668b8a9b1dSJustin T. Gibbs 	/*
18678b8a9b1dSJustin T. Gibbs 	 * Check the bus list generation.  If it has changed, the user
18688b8a9b1dSJustin T. Gibbs 	 * needs to reset everything and start over.
18698b8a9b1dSJustin T. Gibbs 	 */
18708b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
18718b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
18722b83592fSScott Long 	 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) {
18738b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
18748b8a9b1dSJustin T. Gibbs 		return(0);
18758b8a9b1dSJustin T. Gibbs 	}
18768b8a9b1dSJustin T. Gibbs 
18778b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
18788b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus != NULL))
18798b8a9b1dSJustin T. Gibbs 		ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
18808b8a9b1dSJustin T. Gibbs 				     xptedtbusfunc, cdm);
18818b8a9b1dSJustin T. Gibbs 	else
18828b8a9b1dSJustin T. Gibbs 		ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
18838b8a9b1dSJustin T. Gibbs 
18848b8a9b1dSJustin T. Gibbs 	/*
18858b8a9b1dSJustin T. Gibbs 	 * If we get back 0, that means that we had to stop before fully
18868b8a9b1dSJustin T. Gibbs 	 * traversing the EDT.  It also means that one of the subroutines
18878b8a9b1dSJustin T. Gibbs 	 * has set the status field to the proper value.  If we get back 1,
18888b8a9b1dSJustin T. Gibbs 	 * we've fully traversed the EDT and copied out any matching entries.
18898b8a9b1dSJustin T. Gibbs 	 */
18908b8a9b1dSJustin T. Gibbs 	if (ret == 1)
18918b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LAST;
18928b8a9b1dSJustin T. Gibbs 
18938b8a9b1dSJustin T. Gibbs 	return(ret);
18948b8a9b1dSJustin T. Gibbs }
18958b8a9b1dSJustin T. Gibbs 
18968b8a9b1dSJustin T. Gibbs static int
18978b8a9b1dSJustin T. Gibbs xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
18988b8a9b1dSJustin T. Gibbs {
18998b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
19008b8a9b1dSJustin T. Gibbs 
19018b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
19028b8a9b1dSJustin T. Gibbs 
19038b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
19048b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv == pdrv)
19058b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
19068b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
19078b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
19088b8a9b1dSJustin T. Gibbs 	     (*pdrv)->generation)) {
19098b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
19108b8a9b1dSJustin T. Gibbs 		return(0);
19118b8a9b1dSJustin T. Gibbs 	}
19128b8a9b1dSJustin T. Gibbs 
19138b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
19148b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv == pdrv)
19158b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
19168b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.periph != NULL))
19178b8a9b1dSJustin T. Gibbs 		return(xptpdperiphtraverse(pdrv,
19188b8a9b1dSJustin T. Gibbs 				(struct cam_periph *)cdm->pos.cookie.periph,
19198b8a9b1dSJustin T. Gibbs 				xptplistperiphfunc, arg));
19208b8a9b1dSJustin T. Gibbs 	else
19218b8a9b1dSJustin T. Gibbs 		return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
19228b8a9b1dSJustin T. Gibbs }
19238b8a9b1dSJustin T. Gibbs 
19248b8a9b1dSJustin T. Gibbs static int
19258b8a9b1dSJustin T. Gibbs xptplistperiphfunc(struct cam_periph *periph, void *arg)
19268b8a9b1dSJustin T. Gibbs {
19278b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
19288b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
19298b8a9b1dSJustin T. Gibbs 
19308b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
19318b8a9b1dSJustin T. Gibbs 
19328b8a9b1dSJustin T. Gibbs 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
19338b8a9b1dSJustin T. Gibbs 
19348b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
19358b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
19368b8a9b1dSJustin T. Gibbs 		return(0);
19378b8a9b1dSJustin T. Gibbs 	}
19388b8a9b1dSJustin T. Gibbs 
19398b8a9b1dSJustin T. Gibbs 	/*
19408b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this peripheral out.
19418b8a9b1dSJustin T. Gibbs 	 */
19428b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
19438b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
19448b8a9b1dSJustin T. Gibbs 
19458b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
19468b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
19478b8a9b1dSJustin T. Gibbs 
19488b8a9b1dSJustin T. Gibbs 		/*
19498b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
19508b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
19518b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
19528b8a9b1dSJustin T. Gibbs 		 */
19538b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
19548b8a9b1dSJustin T. Gibbs 			struct periph_driver **pdrv;
19558b8a9b1dSJustin T. Gibbs 
19568b8a9b1dSJustin T. Gibbs 			pdrv = NULL;
19578b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
19588b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
19598b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
19608b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PERIPH;
19618b8a9b1dSJustin T. Gibbs 
19628b8a9b1dSJustin T. Gibbs 			/*
19638b8a9b1dSJustin T. Gibbs 			 * This may look a bit non-sensical, but it is
19648b8a9b1dSJustin T. Gibbs 			 * actually quite logical.  There are very few
19658b8a9b1dSJustin T. Gibbs 			 * peripheral drivers, and bloating every peripheral
19668b8a9b1dSJustin T. Gibbs 			 * structure with a pointer back to its parent
19678b8a9b1dSJustin T. Gibbs 			 * peripheral driver linker set entry would cost
19688b8a9b1dSJustin T. Gibbs 			 * more in the long run than doing this quick lookup.
19698b8a9b1dSJustin T. Gibbs 			 */
19700b7c27b9SPeter Wemm 			for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
19718b8a9b1dSJustin T. Gibbs 				if (strcmp((*pdrv)->driver_name,
19728b8a9b1dSJustin T. Gibbs 				    periph->periph_name) == 0)
19738b8a9b1dSJustin T. Gibbs 					break;
19748b8a9b1dSJustin T. Gibbs 			}
19758b8a9b1dSJustin T. Gibbs 
197601910babSScott Long 			if (*pdrv == NULL) {
19778b8a9b1dSJustin T. Gibbs 				cdm->status = CAM_DEV_MATCH_ERROR;
19788b8a9b1dSJustin T. Gibbs 				return(0);
19798b8a9b1dSJustin T. Gibbs 			}
19808b8a9b1dSJustin T. Gibbs 
19818b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.pdrv = pdrv;
19828b8a9b1dSJustin T. Gibbs 			/*
19838b8a9b1dSJustin T. Gibbs 			 * The periph generation slot does double duty, as
19848b8a9b1dSJustin T. Gibbs 			 * does the periph pointer slot.  They are used for
19858b8a9b1dSJustin T. Gibbs 			 * both edt and pdrv lookups and positioning.
19868b8a9b1dSJustin T. Gibbs 			 */
19878b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.periph = periph;
19888b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
19898b8a9b1dSJustin T. Gibbs 				(*pdrv)->generation;
19908b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
19918b8a9b1dSJustin T. Gibbs 			return(0);
19928b8a9b1dSJustin T. Gibbs 		}
19938b8a9b1dSJustin T. Gibbs 
19948b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
19958b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
19968b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_PERIPH;
19978b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.path_id =
19988b8a9b1dSJustin T. Gibbs 			periph->path->bus->path_id;
19998b8a9b1dSJustin T. Gibbs 
20008b8a9b1dSJustin T. Gibbs 		/*
20018b8a9b1dSJustin T. Gibbs 		 * The transport layer peripheral doesn't have a target or
20028b8a9b1dSJustin T. Gibbs 		 * lun.
20038b8a9b1dSJustin T. Gibbs 		 */
20048b8a9b1dSJustin T. Gibbs 		if (periph->path->target)
20058b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_id =
20068b8a9b1dSJustin T. Gibbs 				periph->path->target->target_id;
20078b8a9b1dSJustin T. Gibbs 		else
20088b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_id = -1;
20098b8a9b1dSJustin T. Gibbs 
20108b8a9b1dSJustin T. Gibbs 		if (periph->path->device)
20118b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_lun =
20128b8a9b1dSJustin T. Gibbs 				periph->path->device->lun_id;
20138b8a9b1dSJustin T. Gibbs 		else
20148b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_lun = -1;
20158b8a9b1dSJustin T. Gibbs 
20168b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.unit_number =
20178b8a9b1dSJustin T. Gibbs 			periph->unit_number;
20188b8a9b1dSJustin T. Gibbs 		strncpy(cdm->matches[j].result.periph_result.periph_name,
20198b8a9b1dSJustin T. Gibbs 			periph->periph_name, DEV_IDLEN);
20208b8a9b1dSJustin T. Gibbs 	}
20218b8a9b1dSJustin T. Gibbs 
20228b8a9b1dSJustin T. Gibbs 	return(1);
20238b8a9b1dSJustin T. Gibbs }
20248b8a9b1dSJustin T. Gibbs 
20258b8a9b1dSJustin T. Gibbs static int
20268b8a9b1dSJustin T. Gibbs xptperiphlistmatch(struct ccb_dev_match *cdm)
20278b8a9b1dSJustin T. Gibbs {
20288b8a9b1dSJustin T. Gibbs 	int ret;
20298b8a9b1dSJustin T. Gibbs 
20308b8a9b1dSJustin T. Gibbs 	cdm->num_matches = 0;
20318b8a9b1dSJustin T. Gibbs 
20328b8a9b1dSJustin T. Gibbs 	/*
20338b8a9b1dSJustin T. Gibbs 	 * At this point in the edt traversal function, we check the bus
20348b8a9b1dSJustin T. Gibbs 	 * list generation to make sure that no busses have been added or
20358b8a9b1dSJustin T. Gibbs 	 * removed since the user last sent a XPT_DEV_MATCH ccb through.
20368b8a9b1dSJustin T. Gibbs 	 * For the peripheral driver list traversal function, however, we
20378b8a9b1dSJustin T. Gibbs 	 * don't have to worry about new peripheral driver types coming or
20388b8a9b1dSJustin T. Gibbs 	 * going; they're in a linker set, and therefore can't change
20398b8a9b1dSJustin T. Gibbs 	 * without a recompile.
20408b8a9b1dSJustin T. Gibbs 	 */
20418b8a9b1dSJustin T. Gibbs 
20428b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
20438b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv != NULL))
20448b8a9b1dSJustin T. Gibbs 		ret = xptpdrvtraverse(
20458b8a9b1dSJustin T. Gibbs 				(struct periph_driver **)cdm->pos.cookie.pdrv,
20468b8a9b1dSJustin T. Gibbs 				xptplistpdrvfunc, cdm);
20478b8a9b1dSJustin T. Gibbs 	else
20488b8a9b1dSJustin T. Gibbs 		ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
20498b8a9b1dSJustin T. Gibbs 
20508b8a9b1dSJustin T. Gibbs 	/*
20518b8a9b1dSJustin T. Gibbs 	 * If we get back 0, that means that we had to stop before fully
20528b8a9b1dSJustin T. Gibbs 	 * traversing the peripheral driver tree.  It also means that one of
20538b8a9b1dSJustin T. Gibbs 	 * the subroutines has set the status field to the proper value.  If
20548b8a9b1dSJustin T. Gibbs 	 * we get back 1, we've fully traversed the EDT and copied out any
20558b8a9b1dSJustin T. Gibbs 	 * matching entries.
20568b8a9b1dSJustin T. Gibbs 	 */
20578b8a9b1dSJustin T. Gibbs 	if (ret == 1)
20588b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LAST;
20598b8a9b1dSJustin T. Gibbs 
20608b8a9b1dSJustin T. Gibbs 	return(ret);
20618b8a9b1dSJustin T. Gibbs }
20628b8a9b1dSJustin T. Gibbs 
20638b8a9b1dSJustin T. Gibbs static int
20648b8a9b1dSJustin T. Gibbs xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
20658b8a9b1dSJustin T. Gibbs {
20668b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus, *next_bus;
20678b8a9b1dSJustin T. Gibbs 	int retval;
20688b8a9b1dSJustin T. Gibbs 
20698b8a9b1dSJustin T. Gibbs 	retval = 1;
20708b8a9b1dSJustin T. Gibbs 
20719a7c2696SAlexander Motin 	xpt_lock_buses();
20722b83592fSScott Long 	for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
20738b8a9b1dSJustin T. Gibbs 	     bus != NULL;
20748b8a9b1dSJustin T. Gibbs 	     bus = next_bus) {
20758b8a9b1dSJustin T. Gibbs 
20768900f4b8SKenneth D. Merry 		bus->refcount++;
20778900f4b8SKenneth D. Merry 
20788900f4b8SKenneth D. Merry 		/*
20798900f4b8SKenneth D. Merry 		 * XXX The locking here is obviously very complex.  We
20808900f4b8SKenneth D. Merry 		 * should work to simplify it.
20818900f4b8SKenneth D. Merry 		 */
20829a7c2696SAlexander Motin 		xpt_unlock_buses();
208311e4faceSScott Long 		CAM_SIM_LOCK(bus->sim);
20848b8a9b1dSJustin T. Gibbs 		retval = tr_func(bus, arg);
208511e4faceSScott Long 		CAM_SIM_UNLOCK(bus->sim);
20868900f4b8SKenneth D. Merry 
20879a7c2696SAlexander Motin 		xpt_lock_buses();
20888900f4b8SKenneth D. Merry 		next_bus = TAILQ_NEXT(bus, links);
20899a7c2696SAlexander Motin 		xpt_unlock_buses();
20908900f4b8SKenneth D. Merry 
20918900f4b8SKenneth D. Merry 		xpt_release_bus(bus);
20928900f4b8SKenneth D. Merry 
20938b8a9b1dSJustin T. Gibbs 		if (retval == 0)
20948b8a9b1dSJustin T. Gibbs 			return(retval);
20959a7c2696SAlexander Motin 		xpt_lock_buses();
20968b8a9b1dSJustin T. Gibbs 	}
20979a7c2696SAlexander Motin 	xpt_unlock_buses();
20988b8a9b1dSJustin T. Gibbs 
20998b8a9b1dSJustin T. Gibbs 	return(retval);
21008b8a9b1dSJustin T. Gibbs }
21018b8a9b1dSJustin T. Gibbs 
21028b8a9b1dSJustin T. Gibbs static int
21038b8a9b1dSJustin T. Gibbs xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
21048b8a9b1dSJustin T. Gibbs 		  xpt_targetfunc_t *tr_func, void *arg)
21058b8a9b1dSJustin T. Gibbs {
21068b8a9b1dSJustin T. Gibbs 	struct cam_et *target, *next_target;
21078b8a9b1dSJustin T. Gibbs 	int retval;
21088b8a9b1dSJustin T. Gibbs 
2109dcdf6e74SAlexander Motin 	mtx_assert(bus->sim->mtx, MA_OWNED);
21108b8a9b1dSJustin T. Gibbs 	retval = 1;
21118b8a9b1dSJustin T. Gibbs 	for (target = (start_target ? start_target :
21128b8a9b1dSJustin T. Gibbs 		       TAILQ_FIRST(&bus->et_entries));
21138b8a9b1dSJustin T. Gibbs 	     target != NULL; target = next_target) {
21148b8a9b1dSJustin T. Gibbs 
21158900f4b8SKenneth D. Merry 		target->refcount++;
21168b8a9b1dSJustin T. Gibbs 
21178b8a9b1dSJustin T. Gibbs 		retval = tr_func(target, arg);
21188b8a9b1dSJustin T. Gibbs 
21198900f4b8SKenneth D. Merry 		next_target = TAILQ_NEXT(target, links);
21208900f4b8SKenneth D. Merry 
21218900f4b8SKenneth D. Merry 		xpt_release_target(target);
21228900f4b8SKenneth D. Merry 
21238b8a9b1dSJustin T. Gibbs 		if (retval == 0)
21248b8a9b1dSJustin T. Gibbs 			return(retval);
21258b8a9b1dSJustin T. Gibbs 	}
21268b8a9b1dSJustin T. Gibbs 
21278b8a9b1dSJustin T. Gibbs 	return(retval);
21288b8a9b1dSJustin T. Gibbs }
21298b8a9b1dSJustin T. Gibbs 
21308b8a9b1dSJustin T. Gibbs static int
21318b8a9b1dSJustin T. Gibbs xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
21328b8a9b1dSJustin T. Gibbs 		  xpt_devicefunc_t *tr_func, void *arg)
21338b8a9b1dSJustin T. Gibbs {
21348b8a9b1dSJustin T. Gibbs 	struct cam_ed *device, *next_device;
21358b8a9b1dSJustin T. Gibbs 	int retval;
21368b8a9b1dSJustin T. Gibbs 
2137dcdf6e74SAlexander Motin 	mtx_assert(target->bus->sim->mtx, MA_OWNED);
21388b8a9b1dSJustin T. Gibbs 	retval = 1;
21398b8a9b1dSJustin T. Gibbs 	for (device = (start_device ? start_device :
21408b8a9b1dSJustin T. Gibbs 		       TAILQ_FIRST(&target->ed_entries));
21418b8a9b1dSJustin T. Gibbs 	     device != NULL;
21428b8a9b1dSJustin T. Gibbs 	     device = next_device) {
21438b8a9b1dSJustin T. Gibbs 
21448900f4b8SKenneth D. Merry 		/*
21458900f4b8SKenneth D. Merry 		 * Hold a reference so the current device does not go away
21468900f4b8SKenneth D. Merry 		 * on us.
21478900f4b8SKenneth D. Merry 		 */
21488900f4b8SKenneth D. Merry 		device->refcount++;
21498b8a9b1dSJustin T. Gibbs 
21508b8a9b1dSJustin T. Gibbs 		retval = tr_func(device, arg);
21518b8a9b1dSJustin T. Gibbs 
21528900f4b8SKenneth D. Merry 		/*
21538900f4b8SKenneth D. Merry 		 * Grab our next pointer before we release the current
21548900f4b8SKenneth D. Merry 		 * device.
21558900f4b8SKenneth D. Merry 		 */
21568900f4b8SKenneth D. Merry 		next_device = TAILQ_NEXT(device, links);
21578900f4b8SKenneth D. Merry 
21588900f4b8SKenneth D. Merry 		xpt_release_device(device);
21598900f4b8SKenneth D. Merry 
21608b8a9b1dSJustin T. Gibbs 		if (retval == 0)
21618b8a9b1dSJustin T. Gibbs 			return(retval);
21628b8a9b1dSJustin T. Gibbs 	}
21638b8a9b1dSJustin T. Gibbs 
21648b8a9b1dSJustin T. Gibbs 	return(retval);
21658b8a9b1dSJustin T. Gibbs }
21668b8a9b1dSJustin T. Gibbs 
21678b8a9b1dSJustin T. Gibbs static int
21688b8a9b1dSJustin T. Gibbs xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
21698b8a9b1dSJustin T. Gibbs 		  xpt_periphfunc_t *tr_func, void *arg)
21708b8a9b1dSJustin T. Gibbs {
21718b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph, *next_periph;
21728b8a9b1dSJustin T. Gibbs 	int retval;
21738b8a9b1dSJustin T. Gibbs 
21748b8a9b1dSJustin T. Gibbs 	retval = 1;
21758b8a9b1dSJustin T. Gibbs 
2176dcdf6e74SAlexander Motin 	mtx_assert(device->sim->mtx, MA_OWNED);
21778900f4b8SKenneth D. Merry 	xpt_lock_buses();
21788b8a9b1dSJustin T. Gibbs 	for (periph = (start_periph ? start_periph :
21798b8a9b1dSJustin T. Gibbs 		       SLIST_FIRST(&device->periphs));
21808b8a9b1dSJustin T. Gibbs 	     periph != NULL;
21818b8a9b1dSJustin T. Gibbs 	     periph = next_periph) {
21828b8a9b1dSJustin T. Gibbs 
21838900f4b8SKenneth D. Merry 
21848900f4b8SKenneth D. Merry 		/*
21858900f4b8SKenneth D. Merry 		 * In this case, we want to show peripherals that have been
21868900f4b8SKenneth D. Merry 		 * invalidated, but not peripherals that are scheduled to
21878900f4b8SKenneth D. Merry 		 * be freed.  So instead of calling cam_periph_acquire(),
21888900f4b8SKenneth D. Merry 		 * which will fail if the periph has been invalidated, we
218933a38f74SKenneth D. Merry 		 * just check for the free flag here.  If it is in the
219033a38f74SKenneth D. Merry 		 * process of being freed, we skip to the next periph.
21918900f4b8SKenneth D. Merry 		 */
21928900f4b8SKenneth D. Merry 		if (periph->flags & CAM_PERIPH_FREE) {
21938b8a9b1dSJustin T. Gibbs 			next_periph = SLIST_NEXT(periph, periph_links);
21948900f4b8SKenneth D. Merry 			continue;
21958900f4b8SKenneth D. Merry 		}
21968900f4b8SKenneth D. Merry 
21978900f4b8SKenneth D. Merry 		/*
21988900f4b8SKenneth D. Merry 		 * Acquire a reference to this periph while we call the
21998900f4b8SKenneth D. Merry 		 * traversal function, so it can't go away.
22008900f4b8SKenneth D. Merry 		 */
22018900f4b8SKenneth D. Merry 		periph->refcount++;
22028900f4b8SKenneth D. Merry 
22038b8a9b1dSJustin T. Gibbs 		retval = tr_func(periph, arg);
22048900f4b8SKenneth D. Merry 
22058900f4b8SKenneth D. Merry 		/*
22068900f4b8SKenneth D. Merry 		 * Grab the next peripheral before we release this one, so
22078900f4b8SKenneth D. Merry 		 * our next pointer is still valid.
22088900f4b8SKenneth D. Merry 		 */
22098900f4b8SKenneth D. Merry 		next_periph = SLIST_NEXT(periph, periph_links);
22108900f4b8SKenneth D. Merry 
22118900f4b8SKenneth D. Merry 		cam_periph_release_locked_buses(periph);
22128900f4b8SKenneth D. Merry 
22138b8a9b1dSJustin T. Gibbs 		if (retval == 0)
22148900f4b8SKenneth D. Merry 			goto bailout_done;
22158b8a9b1dSJustin T. Gibbs 	}
22168b8a9b1dSJustin T. Gibbs 
22178900f4b8SKenneth D. Merry bailout_done:
22188900f4b8SKenneth D. Merry 
22198900f4b8SKenneth D. Merry 	xpt_unlock_buses();
22208900f4b8SKenneth D. Merry 
22218b8a9b1dSJustin T. Gibbs 	return(retval);
22228b8a9b1dSJustin T. Gibbs }
22238b8a9b1dSJustin T. Gibbs 
22248b8a9b1dSJustin T. Gibbs static int
22258b8a9b1dSJustin T. Gibbs xptpdrvtraverse(struct periph_driver **start_pdrv,
22268b8a9b1dSJustin T. Gibbs 		xpt_pdrvfunc_t *tr_func, void *arg)
22278b8a9b1dSJustin T. Gibbs {
22288b8a9b1dSJustin T. Gibbs 	struct periph_driver **pdrv;
22298b8a9b1dSJustin T. Gibbs 	int retval;
22308b8a9b1dSJustin T. Gibbs 
22318b8a9b1dSJustin T. Gibbs 	retval = 1;
22328b8a9b1dSJustin T. Gibbs 
22338b8a9b1dSJustin T. Gibbs 	/*
22348b8a9b1dSJustin T. Gibbs 	 * We don't traverse the peripheral driver list like we do the
22358b8a9b1dSJustin T. Gibbs 	 * other lists, because it is a linker set, and therefore cannot be
22368b8a9b1dSJustin T. Gibbs 	 * changed during runtime.  If the peripheral driver list is ever
22378b8a9b1dSJustin T. Gibbs 	 * re-done to be something other than a linker set (i.e. it can
22388b8a9b1dSJustin T. Gibbs 	 * change while the system is running), the list traversal should
22398b8a9b1dSJustin T. Gibbs 	 * be modified to work like the other traversal functions.
22408b8a9b1dSJustin T. Gibbs 	 */
22410b7c27b9SPeter Wemm 	for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
22428b8a9b1dSJustin T. Gibbs 	     *pdrv != NULL; pdrv++) {
22438b8a9b1dSJustin T. Gibbs 		retval = tr_func(pdrv, arg);
22448b8a9b1dSJustin T. Gibbs 
22458b8a9b1dSJustin T. Gibbs 		if (retval == 0)
22468b8a9b1dSJustin T. Gibbs 			return(retval);
22478b8a9b1dSJustin T. Gibbs 	}
22488b8a9b1dSJustin T. Gibbs 
22498b8a9b1dSJustin T. Gibbs 	return(retval);
22508b8a9b1dSJustin T. Gibbs }
22518b8a9b1dSJustin T. Gibbs 
22528b8a9b1dSJustin T. Gibbs static int
22538b8a9b1dSJustin T. Gibbs xptpdperiphtraverse(struct periph_driver **pdrv,
22548b8a9b1dSJustin T. Gibbs 		    struct cam_periph *start_periph,
22558b8a9b1dSJustin T. Gibbs 		    xpt_periphfunc_t *tr_func, void *arg)
22568b8a9b1dSJustin T. Gibbs {
22578b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph, *next_periph;
2258dcdf6e74SAlexander Motin 	struct cam_sim *sim;
22598b8a9b1dSJustin T. Gibbs 	int retval;
22608b8a9b1dSJustin T. Gibbs 
22618b8a9b1dSJustin T. Gibbs 	retval = 1;
22628b8a9b1dSJustin T. Gibbs 
2263f1e2546aSMatt Jacob 	xpt_lock_buses();
22648b8a9b1dSJustin T. Gibbs 	for (periph = (start_periph ? start_periph :
22658b8a9b1dSJustin T. Gibbs 	     TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
22668b8a9b1dSJustin T. Gibbs 	     periph = next_periph) {
22678b8a9b1dSJustin T. Gibbs 
22688900f4b8SKenneth D. Merry 
22698900f4b8SKenneth D. Merry 		/*
22708900f4b8SKenneth D. Merry 		 * In this case, we want to show peripherals that have been
22718900f4b8SKenneth D. Merry 		 * invalidated, but not peripherals that are scheduled to
22728900f4b8SKenneth D. Merry 		 * be freed.  So instead of calling cam_periph_acquire(),
22738900f4b8SKenneth D. Merry 		 * which will fail if the periph has been invalidated, we
22748900f4b8SKenneth D. Merry 		 * just check for the free flag here.  If it is free, we
22758900f4b8SKenneth D. Merry 		 * skip to the next periph.
22768900f4b8SKenneth D. Merry 		 */
22778900f4b8SKenneth D. Merry 		if (periph->flags & CAM_PERIPH_FREE) {
22788900f4b8SKenneth D. Merry 			next_periph = TAILQ_NEXT(periph, unit_links);
22798900f4b8SKenneth D. Merry 			continue;
22808900f4b8SKenneth D. Merry 		}
22818900f4b8SKenneth D. Merry 
22828900f4b8SKenneth D. Merry 		/*
22838900f4b8SKenneth D. Merry 		 * Acquire a reference to this periph while we call the
22848900f4b8SKenneth D. Merry 		 * traversal function, so it can't go away.
22858900f4b8SKenneth D. Merry 		 */
22868900f4b8SKenneth D. Merry 		periph->refcount++;
2287dcdf6e74SAlexander Motin 		sim = periph->sim;
2288dcdf6e74SAlexander Motin 		xpt_unlock_buses();
2289dcdf6e74SAlexander Motin 		CAM_SIM_LOCK(sim);
2290dcdf6e74SAlexander Motin 		xpt_lock_buses();
22918900f4b8SKenneth D. Merry 		retval = tr_func(periph, arg);
22928900f4b8SKenneth D. Merry 
22938900f4b8SKenneth D. Merry 		/*
22948900f4b8SKenneth D. Merry 		 * Grab the next peripheral before we release this one, so
22958900f4b8SKenneth D. Merry 		 * our next pointer is still valid.
22968900f4b8SKenneth D. Merry 		 */
22978b8a9b1dSJustin T. Gibbs 		next_periph = TAILQ_NEXT(periph, unit_links);
22988b8a9b1dSJustin T. Gibbs 
22998900f4b8SKenneth D. Merry 		cam_periph_release_locked_buses(periph);
2300dcdf6e74SAlexander Motin 		CAM_SIM_UNLOCK(sim);
23018900f4b8SKenneth D. Merry 
23028900f4b8SKenneth D. Merry 		if (retval == 0)
23038900f4b8SKenneth D. Merry 			goto bailout_done;
23048b8a9b1dSJustin T. Gibbs 	}
23058900f4b8SKenneth D. Merry bailout_done:
23068900f4b8SKenneth D. Merry 
2307f1e2546aSMatt Jacob 	xpt_unlock_buses();
23088900f4b8SKenneth D. Merry 
23098b8a9b1dSJustin T. Gibbs 	return(retval);
23108b8a9b1dSJustin T. Gibbs }
23118b8a9b1dSJustin T. Gibbs 
23128b8a9b1dSJustin T. Gibbs static int
23138b8a9b1dSJustin T. Gibbs xptdefbusfunc(struct cam_eb *bus, void *arg)
23148b8a9b1dSJustin T. Gibbs {
23158b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
23168b8a9b1dSJustin T. Gibbs 
23178b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
23188b8a9b1dSJustin T. Gibbs 
23198b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_BUS) {
23208b8a9b1dSJustin T. Gibbs 		xpt_busfunc_t *tr_func;
23218b8a9b1dSJustin T. Gibbs 
23228b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_busfunc_t *)tr_config->tr_func;
23238b8a9b1dSJustin T. Gibbs 
23248b8a9b1dSJustin T. Gibbs 		return(tr_func(bus, tr_config->tr_arg));
23258b8a9b1dSJustin T. Gibbs 	} else
23268b8a9b1dSJustin T. Gibbs 		return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
23278b8a9b1dSJustin T. Gibbs }
23288b8a9b1dSJustin T. Gibbs 
23298b8a9b1dSJustin T. Gibbs static int
23308b8a9b1dSJustin T. Gibbs xptdeftargetfunc(struct cam_et *target, void *arg)
23318b8a9b1dSJustin T. Gibbs {
23328b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
23338b8a9b1dSJustin T. Gibbs 
23348b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
23358b8a9b1dSJustin T. Gibbs 
23368b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_TARGET) {
23378b8a9b1dSJustin T. Gibbs 		xpt_targetfunc_t *tr_func;
23388b8a9b1dSJustin T. Gibbs 
23398b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
23408b8a9b1dSJustin T. Gibbs 
23418b8a9b1dSJustin T. Gibbs 		return(tr_func(target, tr_config->tr_arg));
23428b8a9b1dSJustin T. Gibbs 	} else
23438b8a9b1dSJustin T. Gibbs 		return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
23448b8a9b1dSJustin T. Gibbs }
23458b8a9b1dSJustin T. Gibbs 
23468b8a9b1dSJustin T. Gibbs static int
23478b8a9b1dSJustin T. Gibbs xptdefdevicefunc(struct cam_ed *device, void *arg)
23488b8a9b1dSJustin T. Gibbs {
23498b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
23508b8a9b1dSJustin T. Gibbs 
23518b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
23528b8a9b1dSJustin T. Gibbs 
23538b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_DEVICE) {
23548b8a9b1dSJustin T. Gibbs 		xpt_devicefunc_t *tr_func;
23558b8a9b1dSJustin T. Gibbs 
23568b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
23578b8a9b1dSJustin T. Gibbs 
23588b8a9b1dSJustin T. Gibbs 		return(tr_func(device, tr_config->tr_arg));
23598b8a9b1dSJustin T. Gibbs 	} else
23608b8a9b1dSJustin T. Gibbs 		return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
23618b8a9b1dSJustin T. Gibbs }
23628b8a9b1dSJustin T. Gibbs 
23638b8a9b1dSJustin T. Gibbs static int
23648b8a9b1dSJustin T. Gibbs xptdefperiphfunc(struct cam_periph *periph, void *arg)
23658b8a9b1dSJustin T. Gibbs {
23668b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
23678b8a9b1dSJustin T. Gibbs 	xpt_periphfunc_t *tr_func;
23688b8a9b1dSJustin T. Gibbs 
23698b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
23708b8a9b1dSJustin T. Gibbs 
23718b8a9b1dSJustin T. Gibbs 	tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
23728b8a9b1dSJustin T. Gibbs 
23738b8a9b1dSJustin T. Gibbs 	/*
23748b8a9b1dSJustin T. Gibbs 	 * Unlike the other default functions, we don't check for depth
23758b8a9b1dSJustin T. Gibbs 	 * here.  The peripheral driver level is the last level in the EDT,
23768b8a9b1dSJustin T. Gibbs 	 * so if we're here, we should execute the function in question.
23778b8a9b1dSJustin T. Gibbs 	 */
23788b8a9b1dSJustin T. Gibbs 	return(tr_func(periph, tr_config->tr_arg));
23798b8a9b1dSJustin T. Gibbs }
23808b8a9b1dSJustin T. Gibbs 
23818b8a9b1dSJustin T. Gibbs /*
23828b8a9b1dSJustin T. Gibbs  * Execute the given function for every bus in the EDT.
23838b8a9b1dSJustin T. Gibbs  */
23848b8a9b1dSJustin T. Gibbs static int
23858b8a9b1dSJustin T. Gibbs xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
23868b8a9b1dSJustin T. Gibbs {
23878b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
23888b8a9b1dSJustin T. Gibbs 
23898b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_BUS;
23908b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
23918b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
23928b8a9b1dSJustin T. Gibbs 
23938b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
23948b8a9b1dSJustin T. Gibbs }
23958b8a9b1dSJustin T. Gibbs 
23968b8a9b1dSJustin T. Gibbs /*
23978b8a9b1dSJustin T. Gibbs  * Execute the given function for every device in the EDT.
23988b8a9b1dSJustin T. Gibbs  */
23998b8a9b1dSJustin T. Gibbs static int
24008b8a9b1dSJustin T. Gibbs xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
24018b8a9b1dSJustin T. Gibbs {
24028b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
24038b8a9b1dSJustin T. Gibbs 
24048b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_DEVICE;
24058b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
24068b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
24078b8a9b1dSJustin T. Gibbs 
24088b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
24098b8a9b1dSJustin T. Gibbs }
24108b8a9b1dSJustin T. Gibbs 
24118b8a9b1dSJustin T. Gibbs static int
24128b8a9b1dSJustin T. Gibbs xptsetasyncfunc(struct cam_ed *device, void *arg)
24138b8a9b1dSJustin T. Gibbs {
24148b8a9b1dSJustin T. Gibbs 	struct cam_path path;
24158b8a9b1dSJustin T. Gibbs 	struct ccb_getdev cgd;
24167685edecSAlexander Motin 	struct ccb_setasync *csa = (struct ccb_setasync *)arg;
24178b8a9b1dSJustin T. Gibbs 
2418c8bead2aSJustin T. Gibbs 	/*
2419c8bead2aSJustin T. Gibbs 	 * Don't report unconfigured devices (Wildcard devs,
2420c8bead2aSJustin T. Gibbs 	 * devices only for target mode, device instances
2421c8bead2aSJustin T. Gibbs 	 * that have been invalidated but are waiting for
2422c8bead2aSJustin T. Gibbs 	 * their last reference count to be released).
2423c8bead2aSJustin T. Gibbs 	 */
2424c8bead2aSJustin T. Gibbs 	if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2425c8bead2aSJustin T. Gibbs 		return (1);
2426c8bead2aSJustin T. Gibbs 
24278b8a9b1dSJustin T. Gibbs 	xpt_compile_path(&path,
24288b8a9b1dSJustin T. Gibbs 			 NULL,
24298b8a9b1dSJustin T. Gibbs 			 device->target->bus->path_id,
24308b8a9b1dSJustin T. Gibbs 			 device->target->target_id,
24318b8a9b1dSJustin T. Gibbs 			 device->lun_id);
2432bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
24338b8a9b1dSJustin T. Gibbs 	cgd.ccb_h.func_code = XPT_GDEV_TYPE;
24348b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&cgd);
24357685edecSAlexander Motin 	csa->callback(csa->callback_arg,
24368b8a9b1dSJustin T. Gibbs 			    AC_FOUND_DEVICE,
24378b8a9b1dSJustin T. Gibbs 			    &path, &cgd);
24388b8a9b1dSJustin T. Gibbs 	xpt_release_path(&path);
24398b8a9b1dSJustin T. Gibbs 
24408b8a9b1dSJustin T. Gibbs 	return(1);
24418b8a9b1dSJustin T. Gibbs }
2442c8bead2aSJustin T. Gibbs 
24438b8a9b1dSJustin T. Gibbs static int
24448b8a9b1dSJustin T. Gibbs xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
24458b8a9b1dSJustin T. Gibbs {
24468b8a9b1dSJustin T. Gibbs 	struct cam_path path;
24478b8a9b1dSJustin T. Gibbs 	struct ccb_pathinq cpi;
24487685edecSAlexander Motin 	struct ccb_setasync *csa = (struct ccb_setasync *)arg;
24498b8a9b1dSJustin T. Gibbs 
24508b8a9b1dSJustin T. Gibbs 	xpt_compile_path(&path, /*periph*/NULL,
24518b8a9b1dSJustin T. Gibbs 			 bus->sim->path_id,
24528b8a9b1dSJustin T. Gibbs 			 CAM_TARGET_WILDCARD,
24538b8a9b1dSJustin T. Gibbs 			 CAM_LUN_WILDCARD);
2454bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
24558b8a9b1dSJustin T. Gibbs 	cpi.ccb_h.func_code = XPT_PATH_INQ;
24568b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&cpi);
24577685edecSAlexander Motin 	csa->callback(csa->callback_arg,
24588b8a9b1dSJustin T. Gibbs 			    AC_PATH_REGISTERED,
24598b8a9b1dSJustin T. Gibbs 			    &path, &cpi);
24608b8a9b1dSJustin T. Gibbs 	xpt_release_path(&path);
24618b8a9b1dSJustin T. Gibbs 
24628b8a9b1dSJustin T. Gibbs 	return(1);
24638b8a9b1dSJustin T. Gibbs }
24648b8a9b1dSJustin T. Gibbs 
24658b8a9b1dSJustin T. Gibbs void
24668b8a9b1dSJustin T. Gibbs xpt_action(union ccb *start_ccb)
24678b8a9b1dSJustin T. Gibbs {
24689911ecf9SJustin T. Gibbs 
24698b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
24708b8a9b1dSJustin T. Gibbs 
24718b8a9b1dSJustin T. Gibbs 	start_ccb->ccb_h.status = CAM_REQ_INPROG;
247252c9ce25SScott Long 	(*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
247352c9ce25SScott Long }
247452c9ce25SScott Long 
247552c9ce25SScott Long void
247652c9ce25SScott Long xpt_action_default(union ccb *start_ccb)
247752c9ce25SScott Long {
2478da396db2SAlexander Motin 	struct cam_path *path;
247952c9ce25SScott Long 
2480da396db2SAlexander Motin 	path = start_ccb->ccb_h.path;
2481da396db2SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
248252c9ce25SScott Long 
24838b8a9b1dSJustin T. Gibbs 	switch (start_ccb->ccb_h.func_code) {
24848b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
2485d05caa00SKenneth D. Merry 	{
24863393f8daSKenneth D. Merry 		struct cam_ed *device;
2487d05caa00SKenneth D. Merry 
24888b8a9b1dSJustin T. Gibbs 		/*
24898b8a9b1dSJustin T. Gibbs 		 * For the sake of compatibility with SCSI-1
24908b8a9b1dSJustin T. Gibbs 		 * devices that may not understand the identify
24918b8a9b1dSJustin T. Gibbs 		 * message, we include lun information in the
24928b8a9b1dSJustin T. Gibbs 		 * second byte of all commands.  SCSI-1 specifies
24938b8a9b1dSJustin T. Gibbs 		 * that luns are a 3 bit value and reserves only 3
24948b8a9b1dSJustin T. Gibbs 		 * bits for lun information in the CDB.  Later
24958b8a9b1dSJustin T. Gibbs 		 * revisions of the SCSI spec allow for more than 8
24968b8a9b1dSJustin T. Gibbs 		 * luns, but have deprecated lun information in the
24978b8a9b1dSJustin T. Gibbs 		 * CDB.  So, if the lun won't fit, we must omit.
24988b8a9b1dSJustin T. Gibbs 		 *
24998b8a9b1dSJustin T. Gibbs 		 * Also be aware that during initial probing for devices,
25008b8a9b1dSJustin T. Gibbs 		 * the inquiry information is unknown but initialized to 0.
25018b8a9b1dSJustin T. Gibbs 		 * This means that this code will be exercised while probing
25028b8a9b1dSJustin T. Gibbs 		 * devices with an ANSI revision greater than 2.
25038b8a9b1dSJustin T. Gibbs 		 */
2504da396db2SAlexander Motin 		device = path->device;
25053393f8daSKenneth D. Merry 		if (device->protocol_version <= SCSI_REV_2
25068b8a9b1dSJustin T. Gibbs 		 && start_ccb->ccb_h.target_lun < 8
25078b8a9b1dSJustin T. Gibbs 		 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
25088b8a9b1dSJustin T. Gibbs 
25098b8a9b1dSJustin T. Gibbs 			start_ccb->csio.cdb_io.cdb_bytes[1] |=
25108b8a9b1dSJustin T. Gibbs 			    start_ccb->ccb_h.target_lun << 5;
25118b8a9b1dSJustin T. Gibbs 		}
25128b8a9b1dSJustin T. Gibbs 		start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2513d05caa00SKenneth D. Merry 	}
251407c6eac9SPoul-Henning Kamp 	/* FALLTHROUGH */
25158b8a9b1dSJustin T. Gibbs 	case XPT_TARGET_IO:
25168b8a9b1dSJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
25172cefde5fSJustin T. Gibbs 		start_ccb->csio.sense_resid = 0;
25182cefde5fSJustin T. Gibbs 		start_ccb->csio.resid = 0;
25192cefde5fSJustin T. Gibbs 		/* FALLTHROUGH */
252052c9ce25SScott Long 	case XPT_ATA_IO:
2521de9ebb68SAlexander Motin 		if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
252252c9ce25SScott Long 			start_ccb->ataio.resid = 0;
2523b882a6d3SMatt Jacob 		/* FALLTHROUGH */
252410e1cf63SKenneth D. Merry 	case XPT_RESET_DEV:
25258b8a9b1dSJustin T. Gibbs 	case XPT_ENG_EXEC:
252606e79492SKenneth D. Merry 	case XPT_SMP_IO:
2527cccf4220SAlexander Motin 		cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2528cccf4220SAlexander Motin 		if (xpt_schedule_devq(path->bus->sim->devq, path->device))
2529cccf4220SAlexander Motin 			xpt_run_devq(path->bus->sim->devq);
25308b8a9b1dSJustin T. Gibbs 		break;
25318b8a9b1dSJustin T. Gibbs 	case XPT_CALC_GEOMETRY:
25322cefde5fSJustin T. Gibbs 	{
25332cefde5fSJustin T. Gibbs 		struct cam_sim *sim;
25342cefde5fSJustin T. Gibbs 
25358b8a9b1dSJustin T. Gibbs 		/* Filter out garbage */
25368b8a9b1dSJustin T. Gibbs 		if (start_ccb->ccg.block_size == 0
25378b8a9b1dSJustin T. Gibbs 		 || start_ccb->ccg.volume_size == 0) {
25388b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.cylinders = 0;
25398b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.heads = 0;
25408b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.secs_per_track = 0;
25418b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
25428b8a9b1dSJustin T. Gibbs 			break;
25438b8a9b1dSJustin T. Gibbs 		}
2544abef0e67SMarius Strobl #if defined(PC98) || defined(__sparc64__)
25458b8a9b1dSJustin T. Gibbs 		/*
25468b8a9b1dSJustin T. Gibbs 		 * In a PC-98 system, geometry translation depens on
25478b8a9b1dSJustin T. Gibbs 		 * the "real" device geometry obtained from mode page 4.
25488b8a9b1dSJustin T. Gibbs 		 * SCSI geometry translation is performed in the
25498b8a9b1dSJustin T. Gibbs 		 * initialization routine of the SCSI BIOS and the result
25508b8a9b1dSJustin T. Gibbs 		 * stored in host memory.  If the translation is available
25518b8a9b1dSJustin T. Gibbs 		 * in host memory, use it.  If not, rely on the default
25528b8a9b1dSJustin T. Gibbs 		 * translation the device driver performs.
2553abef0e67SMarius Strobl 		 * For sparc64, we may need adjust the geometry of large
2554abef0e67SMarius Strobl 		 * disks in order to fit the limitations of the 16-bit
2555abef0e67SMarius Strobl 		 * fields of the VTOC8 disk label.
25568b8a9b1dSJustin T. Gibbs 		 */
25578b8a9b1dSJustin T. Gibbs 		if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
25588b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
25598b8a9b1dSJustin T. Gibbs 			break;
25608b8a9b1dSJustin T. Gibbs 		}
25618b8a9b1dSJustin T. Gibbs #endif
2562da396db2SAlexander Motin 		sim = path->bus->sim;
25632cefde5fSJustin T. Gibbs 		(*(sim->sim_action))(sim, start_ccb);
25642cefde5fSJustin T. Gibbs 		break;
25652cefde5fSJustin T. Gibbs 	}
2566bb6087e5SJustin T. Gibbs 	case XPT_ABORT:
25672cefde5fSJustin T. Gibbs 	{
25682cefde5fSJustin T. Gibbs 		union ccb* abort_ccb;
25692cefde5fSJustin T. Gibbs 
25702cefde5fSJustin T. Gibbs 		abort_ccb = start_ccb->cab.abort_ccb;
25712cefde5fSJustin T. Gibbs 		if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
25722cefde5fSJustin T. Gibbs 
25732cefde5fSJustin T. Gibbs 			if (abort_ccb->ccb_h.pinfo.index >= 0) {
25742cefde5fSJustin T. Gibbs 				struct cam_ccbq *ccbq;
257583c5d981SAlexander Motin 				struct cam_ed *device;
25762cefde5fSJustin T. Gibbs 
257783c5d981SAlexander Motin 				device = abort_ccb->ccb_h.path->device;
257883c5d981SAlexander Motin 				ccbq = &device->ccbq;
25792cefde5fSJustin T. Gibbs 				cam_ccbq_remove_ccb(ccbq, abort_ccb);
25802cefde5fSJustin T. Gibbs 				abort_ccb->ccb_h.status =
25812cefde5fSJustin T. Gibbs 				    CAM_REQ_ABORTED|CAM_DEV_QFRZN;
25822cefde5fSJustin T. Gibbs 				xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
25832cefde5fSJustin T. Gibbs 				xpt_done(abort_ccb);
25842cefde5fSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
25852cefde5fSJustin T. Gibbs 				break;
25862cefde5fSJustin T. Gibbs 			}
25872cefde5fSJustin T. Gibbs 			if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
25882cefde5fSJustin T. Gibbs 			 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
25892cefde5fSJustin T. Gibbs 				/*
25902cefde5fSJustin T. Gibbs 				 * We've caught this ccb en route to
25912cefde5fSJustin T. Gibbs 				 * the SIM.  Flag it for abort and the
25922cefde5fSJustin T. Gibbs 				 * SIM will do so just before starting
25932cefde5fSJustin T. Gibbs 				 * real work on the CCB.
25942cefde5fSJustin T. Gibbs 				 */
25952cefde5fSJustin T. Gibbs 				abort_ccb->ccb_h.status =
25962cefde5fSJustin T. Gibbs 				    CAM_REQ_ABORTED|CAM_DEV_QFRZN;
25972cefde5fSJustin T. Gibbs 				xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
25982cefde5fSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
25992cefde5fSJustin T. Gibbs 				break;
26002cefde5fSJustin T. Gibbs 			}
26012cefde5fSJustin T. Gibbs 		}
26022cefde5fSJustin T. Gibbs 		if (XPT_FC_IS_QUEUED(abort_ccb)
26032cefde5fSJustin T. Gibbs 		 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
26042cefde5fSJustin T. Gibbs 			/*
26052cefde5fSJustin T. Gibbs 			 * It's already completed but waiting
26062cefde5fSJustin T. Gibbs 			 * for our SWI to get to it.
26072cefde5fSJustin T. Gibbs 			 */
26082cefde5fSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_UA_ABORT;
26092cefde5fSJustin T. Gibbs 			break;
26102cefde5fSJustin T. Gibbs 		}
26112cefde5fSJustin T. Gibbs 		/*
26122cefde5fSJustin T. Gibbs 		 * If we weren't able to take care of the abort request
26132cefde5fSJustin T. Gibbs 		 * in the XPT, pass the request down to the SIM for processing.
26142cefde5fSJustin T. Gibbs 		 */
26152cefde5fSJustin T. Gibbs 	}
261607c6eac9SPoul-Henning Kamp 	/* FALLTHROUGH */
26178b8a9b1dSJustin T. Gibbs 	case XPT_ACCEPT_TARGET_IO:
26188b8a9b1dSJustin T. Gibbs 	case XPT_EN_LUN:
26198b8a9b1dSJustin T. Gibbs 	case XPT_IMMED_NOTIFY:
26208b8a9b1dSJustin T. Gibbs 	case XPT_NOTIFY_ACK:
26218b8a9b1dSJustin T. Gibbs 	case XPT_RESET_BUS:
26222df76c16SMatt Jacob 	case XPT_IMMEDIATE_NOTIFY:
26232df76c16SMatt Jacob 	case XPT_NOTIFY_ACKNOWLEDGE:
26242df76c16SMatt Jacob 	case XPT_GET_SIM_KNOB:
26252df76c16SMatt Jacob 	case XPT_SET_SIM_KNOB:
26268b8a9b1dSJustin T. Gibbs 	{
26278b8a9b1dSJustin T. Gibbs 		struct cam_sim *sim;
26288b8a9b1dSJustin T. Gibbs 
2629da396db2SAlexander Motin 		sim = path->bus->sim;
26308b8a9b1dSJustin T. Gibbs 		(*(sim->sim_action))(sim, start_ccb);
26318b8a9b1dSJustin T. Gibbs 		break;
26328b8a9b1dSJustin T. Gibbs 	}
263387cfaf0eSJustin T. Gibbs 	case XPT_PATH_INQ:
263487cfaf0eSJustin T. Gibbs 	{
263587cfaf0eSJustin T. Gibbs 		struct cam_sim *sim;
263687cfaf0eSJustin T. Gibbs 
2637da396db2SAlexander Motin 		sim = path->bus->sim;
263887cfaf0eSJustin T. Gibbs 		(*(sim->sim_action))(sim, start_ccb);
263987cfaf0eSJustin T. Gibbs 		break;
264087cfaf0eSJustin T. Gibbs 	}
264187cfaf0eSJustin T. Gibbs 	case XPT_PATH_STATS:
2642da396db2SAlexander Motin 		start_ccb->cpis.last_reset = path->bus->last_reset;
264387cfaf0eSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
264487cfaf0eSJustin T. Gibbs 		break;
26458b8a9b1dSJustin T. Gibbs 	case XPT_GDEV_TYPE:
2646a5479bc5SJustin T. Gibbs 	{
264787cfaf0eSJustin T. Gibbs 		struct cam_ed *dev;
2648a5479bc5SJustin T. Gibbs 
2649da396db2SAlexander Motin 		dev = path->device;
265087cfaf0eSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
26518b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
26528b8a9b1dSJustin T. Gibbs 		} else {
26538b8a9b1dSJustin T. Gibbs 			struct ccb_getdev *cgd;
26548b8a9b1dSJustin T. Gibbs 
26558b8a9b1dSJustin T. Gibbs 			cgd = &start_ccb->cgd;
265652c9ce25SScott Long 			cgd->protocol = dev->protocol;
26578b8a9b1dSJustin T. Gibbs 			cgd->inq_data = dev->inq_data;
265852c9ce25SScott Long 			cgd->ident_data = dev->ident_data;
265930a4094fSAlexander Motin 			cgd->inq_flags = dev->inq_flags;
26608b8a9b1dSJustin T. Gibbs 			cgd->ccb_h.status = CAM_REQ_CMP;
26618b8a9b1dSJustin T. Gibbs 			cgd->serial_num_len = dev->serial_num_len;
26628b8a9b1dSJustin T. Gibbs 			if ((dev->serial_num_len > 0)
26638b8a9b1dSJustin T. Gibbs 			 && (dev->serial_num != NULL))
26648b8a9b1dSJustin T. Gibbs 				bcopy(dev->serial_num, cgd->serial_num,
26658b8a9b1dSJustin T. Gibbs 				      dev->serial_num_len);
26668b8a9b1dSJustin T. Gibbs 		}
26678b8a9b1dSJustin T. Gibbs 		break;
2668a5479bc5SJustin T. Gibbs 	}
266987cfaf0eSJustin T. Gibbs 	case XPT_GDEV_STATS:
267087cfaf0eSJustin T. Gibbs 	{
267187cfaf0eSJustin T. Gibbs 		struct cam_ed *dev;
267287cfaf0eSJustin T. Gibbs 
2673da396db2SAlexander Motin 		dev = path->device;
267487cfaf0eSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
267587cfaf0eSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
267687cfaf0eSJustin T. Gibbs 		} else {
267787cfaf0eSJustin T. Gibbs 			struct ccb_getdevstats *cgds;
267887cfaf0eSJustin T. Gibbs 			struct cam_eb *bus;
267987cfaf0eSJustin T. Gibbs 			struct cam_et *tar;
268087cfaf0eSJustin T. Gibbs 
268187cfaf0eSJustin T. Gibbs 			cgds = &start_ccb->cgds;
2682da396db2SAlexander Motin 			bus = path->bus;
2683da396db2SAlexander Motin 			tar = path->target;
268487cfaf0eSJustin T. Gibbs 			cgds->dev_openings = dev->ccbq.dev_openings;
268587cfaf0eSJustin T. Gibbs 			cgds->dev_active = dev->ccbq.dev_active;
268687cfaf0eSJustin T. Gibbs 			cgds->devq_openings = dev->ccbq.devq_openings;
268787cfaf0eSJustin T. Gibbs 			cgds->devq_queued = dev->ccbq.queue.entries;
268887cfaf0eSJustin T. Gibbs 			cgds->held = dev->ccbq.held;
268987cfaf0eSJustin T. Gibbs 			cgds->last_reset = tar->last_reset;
269052c9ce25SScott Long 			cgds->maxtags = dev->maxtags;
269152c9ce25SScott Long 			cgds->mintags = dev->mintags;
269287cfaf0eSJustin T. Gibbs 			if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
269387cfaf0eSJustin T. Gibbs 				cgds->last_reset = bus->last_reset;
269487cfaf0eSJustin T. Gibbs 			cgds->ccb_h.status = CAM_REQ_CMP;
269587cfaf0eSJustin T. Gibbs 		}
269687cfaf0eSJustin T. Gibbs 		break;
269787cfaf0eSJustin T. Gibbs 	}
26988b8a9b1dSJustin T. Gibbs 	case XPT_GDEVLIST:
26998b8a9b1dSJustin T. Gibbs 	{
27008b8a9b1dSJustin T. Gibbs 		struct cam_periph	*nperiph;
27018b8a9b1dSJustin T. Gibbs 		struct periph_list	*periph_head;
27028b8a9b1dSJustin T. Gibbs 		struct ccb_getdevlist	*cgdl;
27033393f8daSKenneth D. Merry 		u_int			i;
27048b8a9b1dSJustin T. Gibbs 		struct cam_ed		*device;
27058b8a9b1dSJustin T. Gibbs 		int			found;
27068b8a9b1dSJustin T. Gibbs 
27078b8a9b1dSJustin T. Gibbs 
27088b8a9b1dSJustin T. Gibbs 		found = 0;
27098b8a9b1dSJustin T. Gibbs 
27108b8a9b1dSJustin T. Gibbs 		/*
27118b8a9b1dSJustin T. Gibbs 		 * Don't want anyone mucking with our data.
27128b8a9b1dSJustin T. Gibbs 		 */
2713da396db2SAlexander Motin 		device = path->device;
27148b8a9b1dSJustin T. Gibbs 		periph_head = &device->periphs;
27158b8a9b1dSJustin T. Gibbs 		cgdl = &start_ccb->cgdl;
27168b8a9b1dSJustin T. Gibbs 
27178b8a9b1dSJustin T. Gibbs 		/*
27188b8a9b1dSJustin T. Gibbs 		 * Check and see if the list has changed since the user
27198b8a9b1dSJustin T. Gibbs 		 * last requested a list member.  If so, tell them that the
27208b8a9b1dSJustin T. Gibbs 		 * list has changed, and therefore they need to start over
27218b8a9b1dSJustin T. Gibbs 		 * from the beginning.
27228b8a9b1dSJustin T. Gibbs 		 */
27238b8a9b1dSJustin T. Gibbs 		if ((cgdl->index != 0) &&
27248b8a9b1dSJustin T. Gibbs 		    (cgdl->generation != device->generation)) {
27258b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
27268b8a9b1dSJustin T. Gibbs 			break;
27278b8a9b1dSJustin T. Gibbs 		}
27288b8a9b1dSJustin T. Gibbs 
27298b8a9b1dSJustin T. Gibbs 		/*
27308b8a9b1dSJustin T. Gibbs 		 * Traverse the list of peripherals and attempt to find
27318b8a9b1dSJustin T. Gibbs 		 * the requested peripheral.
27328b8a9b1dSJustin T. Gibbs 		 */
2733fc2ffbe6SPoul-Henning Kamp 		for (nperiph = SLIST_FIRST(periph_head), i = 0;
27348b8a9b1dSJustin T. Gibbs 		     (nperiph != NULL) && (i <= cgdl->index);
2735fc2ffbe6SPoul-Henning Kamp 		     nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
27368b8a9b1dSJustin T. Gibbs 			if (i == cgdl->index) {
27378b8a9b1dSJustin T. Gibbs 				strncpy(cgdl->periph_name,
27388b8a9b1dSJustin T. Gibbs 					nperiph->periph_name,
27398b8a9b1dSJustin T. Gibbs 					DEV_IDLEN);
27408b8a9b1dSJustin T. Gibbs 				cgdl->unit_number = nperiph->unit_number;
27418b8a9b1dSJustin T. Gibbs 				found = 1;
27428b8a9b1dSJustin T. Gibbs 			}
27438b8a9b1dSJustin T. Gibbs 		}
27448b8a9b1dSJustin T. Gibbs 		if (found == 0) {
27458b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_ERROR;
27468b8a9b1dSJustin T. Gibbs 			break;
27478b8a9b1dSJustin T. Gibbs 		}
27488b8a9b1dSJustin T. Gibbs 
27498b8a9b1dSJustin T. Gibbs 		if (nperiph == NULL)
27508b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
27518b8a9b1dSJustin T. Gibbs 		else
27528b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_MORE_DEVS;
27538b8a9b1dSJustin T. Gibbs 
27548b8a9b1dSJustin T. Gibbs 		cgdl->index++;
27558b8a9b1dSJustin T. Gibbs 		cgdl->generation = device->generation;
27568b8a9b1dSJustin T. Gibbs 
27578b8a9b1dSJustin T. Gibbs 		cgdl->ccb_h.status = CAM_REQ_CMP;
27588b8a9b1dSJustin T. Gibbs 		break;
27598b8a9b1dSJustin T. Gibbs 	}
27608b8a9b1dSJustin T. Gibbs 	case XPT_DEV_MATCH:
27618b8a9b1dSJustin T. Gibbs 	{
27628b8a9b1dSJustin T. Gibbs 		dev_pos_type position_type;
27638b8a9b1dSJustin T. Gibbs 		struct ccb_dev_match *cdm;
27648b8a9b1dSJustin T. Gibbs 
27658b8a9b1dSJustin T. Gibbs 		cdm = &start_ccb->cdm;
27668b8a9b1dSJustin T. Gibbs 
27678b8a9b1dSJustin T. Gibbs 		/*
27688b8a9b1dSJustin T. Gibbs 		 * There are two ways of getting at information in the EDT.
27698b8a9b1dSJustin T. Gibbs 		 * The first way is via the primary EDT tree.  It starts
27708b8a9b1dSJustin T. Gibbs 		 * with a list of busses, then a list of targets on a bus,
27718b8a9b1dSJustin T. Gibbs 		 * then devices/luns on a target, and then peripherals on a
27728b8a9b1dSJustin T. Gibbs 		 * device/lun.  The "other" way is by the peripheral driver
27738b8a9b1dSJustin T. Gibbs 		 * lists.  The peripheral driver lists are organized by
27748b8a9b1dSJustin T. Gibbs 		 * peripheral driver.  (obviously)  So it makes sense to
27758b8a9b1dSJustin T. Gibbs 		 * use the peripheral driver list if the user is looking
27768b8a9b1dSJustin T. Gibbs 		 * for something like "da1", or all "da" devices.  If the
27778b8a9b1dSJustin T. Gibbs 		 * user is looking for something on a particular bus/target
27788b8a9b1dSJustin T. Gibbs 		 * or lun, it's generally better to go through the EDT tree.
27798b8a9b1dSJustin T. Gibbs 		 */
27808b8a9b1dSJustin T. Gibbs 
27818b8a9b1dSJustin T. Gibbs 		if (cdm->pos.position_type != CAM_DEV_POS_NONE)
27828b8a9b1dSJustin T. Gibbs 			position_type = cdm->pos.position_type;
27838b8a9b1dSJustin T. Gibbs 		else {
27843393f8daSKenneth D. Merry 			u_int i;
27858b8a9b1dSJustin T. Gibbs 
27868b8a9b1dSJustin T. Gibbs 			position_type = CAM_DEV_POS_NONE;
27878b8a9b1dSJustin T. Gibbs 
27888b8a9b1dSJustin T. Gibbs 			for (i = 0; i < cdm->num_patterns; i++) {
27898b8a9b1dSJustin T. Gibbs 				if ((cdm->patterns[i].type == DEV_MATCH_BUS)
27908b8a9b1dSJustin T. Gibbs 				 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
27918b8a9b1dSJustin T. Gibbs 					position_type = CAM_DEV_POS_EDT;
27928b8a9b1dSJustin T. Gibbs 					break;
27938b8a9b1dSJustin T. Gibbs 				}
27948b8a9b1dSJustin T. Gibbs 			}
27958b8a9b1dSJustin T. Gibbs 
27968b8a9b1dSJustin T. Gibbs 			if (cdm->num_patterns == 0)
27978b8a9b1dSJustin T. Gibbs 				position_type = CAM_DEV_POS_EDT;
27988b8a9b1dSJustin T. Gibbs 			else if (position_type == CAM_DEV_POS_NONE)
27998b8a9b1dSJustin T. Gibbs 				position_type = CAM_DEV_POS_PDRV;
28008b8a9b1dSJustin T. Gibbs 		}
28018b8a9b1dSJustin T. Gibbs 
280292c40f40SAlexander Motin 		/*
280392c40f40SAlexander Motin 		 * Note that we drop the SIM lock here, because the EDT
280492c40f40SAlexander Motin 		 * traversal code needs to do its own locking.
280592c40f40SAlexander Motin 		 */
280692c40f40SAlexander Motin 		CAM_SIM_UNLOCK(xpt_path_sim(cdm->ccb_h.path));
28078b8a9b1dSJustin T. Gibbs 		switch(position_type & CAM_DEV_POS_TYPEMASK) {
28088b8a9b1dSJustin T. Gibbs 		case CAM_DEV_POS_EDT:
280907c6eac9SPoul-Henning Kamp 			xptedtmatch(cdm);
28108b8a9b1dSJustin T. Gibbs 			break;
28118b8a9b1dSJustin T. Gibbs 		case CAM_DEV_POS_PDRV:
281207c6eac9SPoul-Henning Kamp 			xptperiphlistmatch(cdm);
28138b8a9b1dSJustin T. Gibbs 			break;
28148b8a9b1dSJustin T. Gibbs 		default:
28158b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_ERROR;
28168b8a9b1dSJustin T. Gibbs 			break;
28178b8a9b1dSJustin T. Gibbs 		}
281892c40f40SAlexander Motin 		CAM_SIM_LOCK(xpt_path_sim(cdm->ccb_h.path));
28198b8a9b1dSJustin T. Gibbs 
28208b8a9b1dSJustin T. Gibbs 		if (cdm->status == CAM_DEV_MATCH_ERROR)
28218b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
28228b8a9b1dSJustin T. Gibbs 		else
28238b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
28248b8a9b1dSJustin T. Gibbs 
28258b8a9b1dSJustin T. Gibbs 		break;
28268b8a9b1dSJustin T. Gibbs 	}
28278b8a9b1dSJustin T. Gibbs 	case XPT_SASYNC_CB:
28288b8a9b1dSJustin T. Gibbs 	{
282984f82481SScott Long 		struct ccb_setasync *csa;
283084f82481SScott Long 		struct async_node *cur_entry;
283184f82481SScott Long 		struct async_list *async_head;
283284f82481SScott Long 		u_int32_t added;
283384f82481SScott Long 
283484f82481SScott Long 		csa = &start_ccb->csa;
283584f82481SScott Long 		added = csa->event_enable;
2836da396db2SAlexander Motin 		async_head = &path->device->asyncs;
283784f82481SScott Long 
283884f82481SScott Long 		/*
283984f82481SScott Long 		 * If there is already an entry for us, simply
284084f82481SScott Long 		 * update it.
284184f82481SScott Long 		 */
284284f82481SScott Long 		cur_entry = SLIST_FIRST(async_head);
284384f82481SScott Long 		while (cur_entry != NULL) {
284484f82481SScott Long 			if ((cur_entry->callback_arg == csa->callback_arg)
284584f82481SScott Long 			 && (cur_entry->callback == csa->callback))
284684f82481SScott Long 				break;
284784f82481SScott Long 			cur_entry = SLIST_NEXT(cur_entry, links);
284884f82481SScott Long 		}
284984f82481SScott Long 
285084f82481SScott Long 		if (cur_entry != NULL) {
285184f82481SScott Long 		 	/*
285284f82481SScott Long 			 * If the request has no flags set,
285384f82481SScott Long 			 * remove the entry.
285484f82481SScott Long 			 */
285584f82481SScott Long 			added &= ~cur_entry->event_enable;
285684f82481SScott Long 			if (csa->event_enable == 0) {
285784f82481SScott Long 				SLIST_REMOVE(async_head, cur_entry,
285884f82481SScott Long 					     async_node, links);
2859da396db2SAlexander Motin 				xpt_release_device(path->device);
286084f82481SScott Long 				free(cur_entry, M_CAMXPT);
286184f82481SScott Long 			} else {
286284f82481SScott Long 				cur_entry->event_enable = csa->event_enable;
286384f82481SScott Long 			}
28647685edecSAlexander Motin 			csa->event_enable = added;
286584f82481SScott Long 		} else {
286684f82481SScott Long 			cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
286784f82481SScott Long 					   M_NOWAIT);
286884f82481SScott Long 			if (cur_entry == NULL) {
286984f82481SScott Long 				csa->ccb_h.status = CAM_RESRC_UNAVAIL;
287084f82481SScott Long 				break;
287184f82481SScott Long 			}
287284f82481SScott Long 			cur_entry->event_enable = csa->event_enable;
287384f82481SScott Long 			cur_entry->callback_arg = csa->callback_arg;
287484f82481SScott Long 			cur_entry->callback = csa->callback;
287584f82481SScott Long 			SLIST_INSERT_HEAD(async_head, cur_entry, links);
2876da396db2SAlexander Motin 			xpt_acquire_device(path->device);
287784f82481SScott Long 		}
28788b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
28798b8a9b1dSJustin T. Gibbs 		break;
28808b8a9b1dSJustin T. Gibbs 	}
28818b8a9b1dSJustin T. Gibbs 	case XPT_REL_SIMQ:
28828b8a9b1dSJustin T. Gibbs 	{
28838b8a9b1dSJustin T. Gibbs 		struct ccb_relsim *crs;
28848b8a9b1dSJustin T. Gibbs 		struct cam_ed *dev;
28858b8a9b1dSJustin T. Gibbs 
28868b8a9b1dSJustin T. Gibbs 		crs = &start_ccb->crs;
2887da396db2SAlexander Motin 		dev = path->device;
28888b8a9b1dSJustin T. Gibbs 		if (dev == NULL) {
28898b8a9b1dSJustin T. Gibbs 
28908b8a9b1dSJustin T. Gibbs 			crs->ccb_h.status = CAM_DEV_NOT_THERE;
28918b8a9b1dSJustin T. Gibbs 			break;
28928b8a9b1dSJustin T. Gibbs 		}
28938b8a9b1dSJustin T. Gibbs 
28948b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
28958b8a9b1dSJustin T. Gibbs 
28968b8a9b1dSJustin T. Gibbs 			/* Don't ever go below one opening */
28978b8a9b1dSJustin T. Gibbs 			if (crs->openings > 0) {
28987dc3213dSAlexander Motin 				xpt_dev_ccbq_resize(path, crs->openings);
289979ccc199SJordan K. Hubbard 				if (bootverbose) {
2900da396db2SAlexander Motin 					xpt_print(path,
29017dc3213dSAlexander Motin 					    "number of openings is now %d\n",
29028b8a9b1dSJustin T. Gibbs 					    crs->openings);
29038b8a9b1dSJustin T. Gibbs 				}
29048b8a9b1dSJustin T. Gibbs 			}
29058b8a9b1dSJustin T. Gibbs 		}
29068b8a9b1dSJustin T. Gibbs 
29078b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
29088b8a9b1dSJustin T. Gibbs 
29098b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
29108b8a9b1dSJustin T. Gibbs 
29118b8a9b1dSJustin T. Gibbs 				/*
29128b8a9b1dSJustin T. Gibbs 				 * Just extend the old timeout and decrement
29138b8a9b1dSJustin T. Gibbs 				 * the freeze count so that a single timeout
29148b8a9b1dSJustin T. Gibbs 				 * is sufficient for releasing the queue.
29158b8a9b1dSJustin T. Gibbs 				 */
29168b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
29172b83592fSScott Long 				callout_stop(&dev->callout);
29188b8a9b1dSJustin T. Gibbs 			} else {
29198b8a9b1dSJustin T. Gibbs 
29208b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
29218b8a9b1dSJustin T. Gibbs 			}
29228b8a9b1dSJustin T. Gibbs 
29232b83592fSScott Long 			callout_reset(&dev->callout,
29242b83592fSScott Long 			    (crs->release_timeout * hz) / 1000,
29252b83592fSScott Long 			    xpt_release_devq_timeout, dev);
29268b8a9b1dSJustin T. Gibbs 
29278b8a9b1dSJustin T. Gibbs 			dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
29288b8a9b1dSJustin T. Gibbs 
29298b8a9b1dSJustin T. Gibbs 		}
29308b8a9b1dSJustin T. Gibbs 
29318b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
29328b8a9b1dSJustin T. Gibbs 
29338b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
29348b8a9b1dSJustin T. Gibbs 				/*
29358b8a9b1dSJustin T. Gibbs 				 * Decrement the freeze count so that a single
29368b8a9b1dSJustin T. Gibbs 				 * completion is still sufficient to unfreeze
29378b8a9b1dSJustin T. Gibbs 				 * the queue.
29388b8a9b1dSJustin T. Gibbs 				 */
29398b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
29408b8a9b1dSJustin T. Gibbs 			} else {
29418b8a9b1dSJustin T. Gibbs 
29428b8a9b1dSJustin T. Gibbs 				dev->flags |= CAM_DEV_REL_ON_COMPLETE;
29438b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
29448b8a9b1dSJustin T. Gibbs 			}
29458b8a9b1dSJustin T. Gibbs 		}
29468b8a9b1dSJustin T. Gibbs 
29478b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
29488b8a9b1dSJustin T. Gibbs 
29498b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
29508b8a9b1dSJustin T. Gibbs 			 || (dev->ccbq.dev_active == 0)) {
29518b8a9b1dSJustin T. Gibbs 
29528b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
29538b8a9b1dSJustin T. Gibbs 			} else {
29548b8a9b1dSJustin T. Gibbs 
29558b8a9b1dSJustin T. Gibbs 				dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
29568b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
29578b8a9b1dSJustin T. Gibbs 			}
29588b8a9b1dSJustin T. Gibbs 		}
29598b8a9b1dSJustin T. Gibbs 
2960cccf4220SAlexander Motin 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
2961cccf4220SAlexander Motin 			xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
2962cccf4220SAlexander Motin 		start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
29638b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
29648b8a9b1dSJustin T. Gibbs 		break;
29658b8a9b1dSJustin T. Gibbs 	}
29668b8a9b1dSJustin T. Gibbs 	case XPT_DEBUG: {
296780d6987cSAlexander Motin 		struct cam_path *oldpath;
296880d6987cSAlexander Motin 		struct cam_sim *oldsim;
296980d6987cSAlexander Motin 
2970f0f25b9cSAlexander Motin 		/* Check that all request bits are supported. */
297122c7d606SAlexander Motin 		if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
2972f0f25b9cSAlexander Motin 			start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2973f0f25b9cSAlexander Motin 			break;
2974f0f25b9cSAlexander Motin 		}
2975f0f25b9cSAlexander Motin 
297680d6987cSAlexander Motin 		cam_dflags = CAM_DEBUG_NONE;
29778b8a9b1dSJustin T. Gibbs 		if (cam_dpath != NULL) {
297880d6987cSAlexander Motin 			/* To release the old path we must hold proper lock. */
297980d6987cSAlexander Motin 			oldpath = cam_dpath;
29808b8a9b1dSJustin T. Gibbs 			cam_dpath = NULL;
298180d6987cSAlexander Motin 			oldsim = xpt_path_sim(oldpath);
298280d6987cSAlexander Motin 			CAM_SIM_UNLOCK(xpt_path_sim(start_ccb->ccb_h.path));
298380d6987cSAlexander Motin 			CAM_SIM_LOCK(oldsim);
298480d6987cSAlexander Motin 			xpt_free_path(oldpath);
298580d6987cSAlexander Motin 			CAM_SIM_UNLOCK(oldsim);
298680d6987cSAlexander Motin 			CAM_SIM_LOCK(xpt_path_sim(start_ccb->ccb_h.path));
29878b8a9b1dSJustin T. Gibbs 		}
298880d6987cSAlexander Motin 		if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
2989e5dfa058SAlexander Motin 			if (xpt_create_path(&cam_dpath, NULL,
29908b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.path_id,
29918b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.target_id,
29928b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.target_lun) !=
29938b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP) {
29948b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2995aa872be6SMatt Jacob 			} else {
299680d6987cSAlexander Motin 				cam_dflags = start_ccb->cdbg.flags;
29978b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
2998f0d9af51SMatt Jacob 				xpt_print(cam_dpath, "debugging flags now %x\n",
2999f0d9af51SMatt Jacob 				    cam_dflags);
3000aa872be6SMatt Jacob 			}
300180d6987cSAlexander Motin 		} else
30028b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
30038b8a9b1dSJustin T. Gibbs 		break;
30048b8a9b1dSJustin T. Gibbs 	}
30058b8a9b1dSJustin T. Gibbs 	case XPT_NOOP:
300687cfaf0eSJustin T. Gibbs 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3007da396db2SAlexander Motin 			xpt_freeze_devq(path, 1);
30088b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
30098b8a9b1dSJustin T. Gibbs 		break;
30108b8a9b1dSJustin T. Gibbs 	default:
30118b8a9b1dSJustin T. Gibbs 	case XPT_SDEV_TYPE:
30128b8a9b1dSJustin T. Gibbs 	case XPT_TERM_IO:
30138b8a9b1dSJustin T. Gibbs 	case XPT_ENG_INQ:
30148b8a9b1dSJustin T. Gibbs 		/* XXX Implement */
30153501942bSJustin T. Gibbs 		printf("%s: CCB type %#x not supported\n", __func__,
30163501942bSJustin T. Gibbs 		       start_ccb->ccb_h.func_code);
30178b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3018b882a6d3SMatt Jacob 		if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3019b882a6d3SMatt Jacob 			xpt_done(start_ccb);
3020b882a6d3SMatt Jacob 		}
30218b8a9b1dSJustin T. Gibbs 		break;
30228b8a9b1dSJustin T. Gibbs 	}
30238b8a9b1dSJustin T. Gibbs }
30248b8a9b1dSJustin T. Gibbs 
30258b8a9b1dSJustin T. Gibbs void
30268b8a9b1dSJustin T. Gibbs xpt_polled_action(union ccb *start_ccb)
30278b8a9b1dSJustin T. Gibbs {
30288b8a9b1dSJustin T. Gibbs 	u_int32_t timeout;
30298b8a9b1dSJustin T. Gibbs 	struct	  cam_sim *sim;
30308b8a9b1dSJustin T. Gibbs 	struct	  cam_devq *devq;
30318b8a9b1dSJustin T. Gibbs 	struct	  cam_ed *dev;
30328b8a9b1dSJustin T. Gibbs 
303368153f43SScott Long 
30340069293bSAlexander Motin 	timeout = start_ccb->ccb_h.timeout * 10;
30358b8a9b1dSJustin T. Gibbs 	sim = start_ccb->ccb_h.path->bus->sim;
30368b8a9b1dSJustin T. Gibbs 	devq = sim->devq;
30378b8a9b1dSJustin T. Gibbs 	dev = start_ccb->ccb_h.path->device;
30388b8a9b1dSJustin T. Gibbs 
30392b83592fSScott Long 	mtx_assert(sim->mtx, MA_OWNED);
30408b8a9b1dSJustin T. Gibbs 
30414a612489SAlexander Motin 	/* Don't use ISR for this SIM while polling. */
30424a612489SAlexander Motin 	sim->flags |= CAM_SIM_POLLED;
30434a612489SAlexander Motin 
30448b8a9b1dSJustin T. Gibbs 	/*
30458b8a9b1dSJustin T. Gibbs 	 * Steal an opening so that no other queued requests
30468b8a9b1dSJustin T. Gibbs 	 * can get it before us while we simulate interrupts.
30478b8a9b1dSJustin T. Gibbs 	 */
30488b8a9b1dSJustin T. Gibbs 	dev->ccbq.devq_openings--;
30498b8a9b1dSJustin T. Gibbs 	dev->ccbq.dev_openings--;
30508b8a9b1dSJustin T. Gibbs 
3051d3ef3454SIan Dowse 	while(((devq != NULL && devq->send_openings <= 0) ||
3052d3ef3454SIan Dowse 	   dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
30530069293bSAlexander Motin 		DELAY(100);
30548b8a9b1dSJustin T. Gibbs 		(*(sim->sim_poll))(sim);
30559758cc83SScott Long 		camisr_runqueue(&sim->sim_doneq);
30568b8a9b1dSJustin T. Gibbs 	}
30578b8a9b1dSJustin T. Gibbs 
30588b8a9b1dSJustin T. Gibbs 	dev->ccbq.devq_openings++;
30598b8a9b1dSJustin T. Gibbs 	dev->ccbq.dev_openings++;
30608b8a9b1dSJustin T. Gibbs 
30618b8a9b1dSJustin T. Gibbs 	if (timeout != 0) {
30628b8a9b1dSJustin T. Gibbs 		xpt_action(start_ccb);
30638b8a9b1dSJustin T. Gibbs 		while(--timeout > 0) {
30648b8a9b1dSJustin T. Gibbs 			(*(sim->sim_poll))(sim);
30659758cc83SScott Long 			camisr_runqueue(&sim->sim_doneq);
30668b8a9b1dSJustin T. Gibbs 			if ((start_ccb->ccb_h.status  & CAM_STATUS_MASK)
30678b8a9b1dSJustin T. Gibbs 			    != CAM_REQ_INPROG)
30688b8a9b1dSJustin T. Gibbs 				break;
30690069293bSAlexander Motin 			DELAY(100);
30708b8a9b1dSJustin T. Gibbs 		}
30718b8a9b1dSJustin T. Gibbs 		if (timeout == 0) {
30728b8a9b1dSJustin T. Gibbs 			/*
30738b8a9b1dSJustin T. Gibbs 			 * XXX Is it worth adding a sim_timeout entry
30748b8a9b1dSJustin T. Gibbs 			 * point so we can attempt recovery?  If
30758b8a9b1dSJustin T. Gibbs 			 * this is only used for dumps, I don't think
30768b8a9b1dSJustin T. Gibbs 			 * it is.
30778b8a9b1dSJustin T. Gibbs 			 */
30788b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
30798b8a9b1dSJustin T. Gibbs 		}
30808b8a9b1dSJustin T. Gibbs 	} else {
30818b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
30828b8a9b1dSJustin T. Gibbs 	}
30834a612489SAlexander Motin 
30844a612489SAlexander Motin 	/* We will use CAM ISR for this SIM again. */
30854a612489SAlexander Motin 	sim->flags &= ~CAM_SIM_POLLED;
30868b8a9b1dSJustin T. Gibbs }
30878b8a9b1dSJustin T. Gibbs 
30888b8a9b1dSJustin T. Gibbs /*
30898b8a9b1dSJustin T. Gibbs  * Schedule a peripheral driver to receive a ccb when it's
30908b8a9b1dSJustin T. Gibbs  * target device has space for more transactions.
30918b8a9b1dSJustin T. Gibbs  */
30928b8a9b1dSJustin T. Gibbs void
30938b8a9b1dSJustin T. Gibbs xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
30948b8a9b1dSJustin T. Gibbs {
30958b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
309683c5d981SAlexander Motin 	int runq = 0;
30978b8a9b1dSJustin T. Gibbs 
30982b83592fSScott Long 	mtx_assert(perph->sim->mtx, MA_OWNED);
309968153f43SScott Long 
31008b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
31018b8a9b1dSJustin T. Gibbs 	device = perph->path->device;
31028b8a9b1dSJustin T. Gibbs 	if (periph_is_queued(perph)) {
31038b8a9b1dSJustin T. Gibbs 		/* Simply reorder based on new priority */
31048b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
31058b8a9b1dSJustin T. Gibbs 			  ("   change priority to %d\n", new_priority));
31068b8a9b1dSJustin T. Gibbs 		if (new_priority < perph->pinfo.priority) {
31078b8a9b1dSJustin T. Gibbs 			camq_change_priority(&device->drvq,
31088b8a9b1dSJustin T. Gibbs 					     perph->pinfo.index,
31098b8a9b1dSJustin T. Gibbs 					     new_priority);
3110cccf4220SAlexander Motin 			runq = 1;
31118b8a9b1dSJustin T. Gibbs 		}
31128b8a9b1dSJustin T. Gibbs 	} else {
31138b8a9b1dSJustin T. Gibbs 		/* New entry on the queue */
31148b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
31158b8a9b1dSJustin T. Gibbs 			  ("   added periph to queue\n"));
31168b8a9b1dSJustin T. Gibbs 		perph->pinfo.priority = new_priority;
31178bad620dSJustin T. Gibbs 		perph->pinfo.generation = ++device->drvq.generation;
31188b8a9b1dSJustin T. Gibbs 		camq_insert(&device->drvq, &perph->pinfo);
3119cccf4220SAlexander Motin 		runq = 1;
31208b8a9b1dSJustin T. Gibbs 	}
31218b8a9b1dSJustin T. Gibbs 	if (runq != 0) {
31228b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3123cccf4220SAlexander Motin 			  ("   calling xpt_run_dev_allocq\n"));
3124cccf4220SAlexander Motin 		xpt_run_dev_allocq(device);
31258b8a9b1dSJustin T. Gibbs 	}
31268b8a9b1dSJustin T. Gibbs }
31278b8a9b1dSJustin T. Gibbs 
31288b8a9b1dSJustin T. Gibbs 
31298b8a9b1dSJustin T. Gibbs /*
31308b8a9b1dSJustin T. Gibbs  * Schedule a device to run on a given queue.
31318b8a9b1dSJustin T. Gibbs  * If the device was inserted as a new entry on the queue,
31328b8a9b1dSJustin T. Gibbs  * return 1 meaning the device queue should be run. If we
31338b8a9b1dSJustin T. Gibbs  * were already queued, implying someone else has already
31348b8a9b1dSJustin T. Gibbs  * started the queue, return 0 so the caller doesn't attempt
313577dc25ccSScott Long  * to run the queue.
31368b8a9b1dSJustin T. Gibbs  */
313752c9ce25SScott Long int
31388b8a9b1dSJustin T. Gibbs xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
31398b8a9b1dSJustin T. Gibbs 		 u_int32_t new_priority)
31408b8a9b1dSJustin T. Gibbs {
31418b8a9b1dSJustin T. Gibbs 	int retval;
31428b8a9b1dSJustin T. Gibbs 	u_int32_t old_priority;
31438b8a9b1dSJustin T. Gibbs 
3144aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
31458b8a9b1dSJustin T. Gibbs 
31468b8a9b1dSJustin T. Gibbs 	old_priority = pinfo->priority;
31478b8a9b1dSJustin T. Gibbs 
31488b8a9b1dSJustin T. Gibbs 	/*
31498b8a9b1dSJustin T. Gibbs 	 * Are we already queued?
31508b8a9b1dSJustin T. Gibbs 	 */
31518b8a9b1dSJustin T. Gibbs 	if (pinfo->index != CAM_UNQUEUED_INDEX) {
31528b8a9b1dSJustin T. Gibbs 		/* Simply reorder based on new priority */
31538b8a9b1dSJustin T. Gibbs 		if (new_priority < old_priority) {
31548b8a9b1dSJustin T. Gibbs 			camq_change_priority(queue, pinfo->index,
31558b8a9b1dSJustin T. Gibbs 					     new_priority);
3156aa872be6SMatt Jacob 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
31578b8a9b1dSJustin T. Gibbs 					("changed priority to %d\n",
31588b8a9b1dSJustin T. Gibbs 					 new_priority));
315983c5d981SAlexander Motin 			retval = 1;
316083c5d981SAlexander Motin 		} else
31618b8a9b1dSJustin T. Gibbs 			retval = 0;
31628b8a9b1dSJustin T. Gibbs 	} else {
31638b8a9b1dSJustin T. Gibbs 		/* New entry on the queue */
31648b8a9b1dSJustin T. Gibbs 		if (new_priority < old_priority)
31658b8a9b1dSJustin T. Gibbs 			pinfo->priority = new_priority;
31668b8a9b1dSJustin T. Gibbs 
3167aa872be6SMatt Jacob 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
31688b8a9b1dSJustin T. Gibbs 				("Inserting onto queue\n"));
31698bad620dSJustin T. Gibbs 		pinfo->generation = ++queue->generation;
31708b8a9b1dSJustin T. Gibbs 		camq_insert(queue, pinfo);
31718b8a9b1dSJustin T. Gibbs 		retval = 1;
31728b8a9b1dSJustin T. Gibbs 	}
31738b8a9b1dSJustin T. Gibbs 	return (retval);
31748b8a9b1dSJustin T. Gibbs }
31758b8a9b1dSJustin T. Gibbs 
31768b8a9b1dSJustin T. Gibbs static void
3177cccf4220SAlexander Motin xpt_run_dev_allocq(struct cam_ed *device)
31788b8a9b1dSJustin T. Gibbs {
31798b8a9b1dSJustin T. Gibbs 	struct camq	*drvq;
31808b8a9b1dSJustin T. Gibbs 
3181cccf4220SAlexander Motin 	if (device->ccbq.devq_allocating)
3182cccf4220SAlexander Motin 		return;
3183cccf4220SAlexander Motin 	device->ccbq.devq_allocating = 1;
3184cccf4220SAlexander Motin 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq(%p)\n", device));
31858b8a9b1dSJustin T. Gibbs 	drvq = &device->drvq;
3186cccf4220SAlexander Motin 	while ((drvq->entries > 0) &&
3187cccf4220SAlexander Motin 	    (device->ccbq.devq_openings > 0 ||
3188cccf4220SAlexander Motin 	     CAMQ_GET_PRIO(drvq) <= CAM_PRIORITY_OOB) &&
3189cccf4220SAlexander Motin 	    (device->ccbq.queue.qfrozen_cnt == 0)) {
3190cccf4220SAlexander Motin 		union	ccb *work_ccb;
3191cccf4220SAlexander Motin 		struct	cam_periph *drv;
3192cccf4220SAlexander Motin 
31932d89c125SAlexander Motin 		KASSERT(drvq->entries > 0, ("xpt_run_dev_allocq: "
31942d89c125SAlexander Motin 		    "Device on queue without any work to do"));
31958b8a9b1dSJustin T. Gibbs 		if ((work_ccb = xpt_get_ccb(device)) != NULL) {
31965a526431SJustin T. Gibbs 			drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
31978b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
31988b8a9b1dSJustin T. Gibbs 				      drv->pinfo.priority);
3199aa872be6SMatt Jacob 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
32008b8a9b1dSJustin T. Gibbs 					("calling periph start\n"));
32018b8a9b1dSJustin T. Gibbs 			drv->periph_start(drv, work_ccb);
32028b8a9b1dSJustin T. Gibbs 		} else {
32038b8a9b1dSJustin T. Gibbs 			/*
32048b8a9b1dSJustin T. Gibbs 			 * Malloc failure in alloc_ccb
32058b8a9b1dSJustin T. Gibbs 			 */
32068b8a9b1dSJustin T. Gibbs 			/*
32078b8a9b1dSJustin T. Gibbs 			 * XXX add us to a list to be run from free_ccb
32088b8a9b1dSJustin T. Gibbs 			 * if we don't have any ccbs active on this
32098b8a9b1dSJustin T. Gibbs 			 * device queue otherwise we may never get run
32108b8a9b1dSJustin T. Gibbs 			 * again.
32118b8a9b1dSJustin T. Gibbs 			 */
32128b8a9b1dSJustin T. Gibbs 			break;
32138b8a9b1dSJustin T. Gibbs 		}
32148b8a9b1dSJustin T. Gibbs 	}
3215cccf4220SAlexander Motin 	device->ccbq.devq_allocating = 0;
32168b8a9b1dSJustin T. Gibbs }
32178b8a9b1dSJustin T. Gibbs 
321883c5d981SAlexander Motin static void
3219cccf4220SAlexander Motin xpt_run_devq(struct cam_devq *devq)
32208b8a9b1dSJustin T. Gibbs {
3221de9ebb68SAlexander Motin 	char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
32228b8a9b1dSJustin T. Gibbs 
3223cccf4220SAlexander Motin 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
32248b8a9b1dSJustin T. Gibbs 
3225cccf4220SAlexander Motin 	devq->send_queue.qfrozen_cnt++;
32268b8a9b1dSJustin T. Gibbs 	while ((devq->send_queue.entries > 0)
3227ec700f26SAlexander Motin 	    && (devq->send_openings > 0)
3228cccf4220SAlexander Motin 	    && (devq->send_queue.qfrozen_cnt <= 1)) {
32298b8a9b1dSJustin T. Gibbs 		struct	cam_ed_qinfo *qinfo;
32308b8a9b1dSJustin T. Gibbs 		struct	cam_ed *device;
32318b8a9b1dSJustin T. Gibbs 		union ccb *work_ccb;
32328b8a9b1dSJustin T. Gibbs 		struct	cam_sim *sim;
32338b8a9b1dSJustin T. Gibbs 
32348b8a9b1dSJustin T. Gibbs 		qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
32355a526431SJustin T. Gibbs 							   CAMQ_HEAD);
32368b8a9b1dSJustin T. Gibbs 		device = qinfo->device;
3237aa872be6SMatt Jacob 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
323850642f18SKenneth D. Merry 				("running device %p\n", device));
32398b8a9b1dSJustin T. Gibbs 
32405a526431SJustin T. Gibbs 		work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
32418b8a9b1dSJustin T. Gibbs 		if (work_ccb == NULL) {
324257b89bbcSNate Lawson 			printf("device on run queue with no ccbs???\n");
32438b8a9b1dSJustin T. Gibbs 			continue;
32448b8a9b1dSJustin T. Gibbs 		}
32458b8a9b1dSJustin T. Gibbs 
32468b8a9b1dSJustin T. Gibbs 		if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
32478b8a9b1dSJustin T. Gibbs 
32482b83592fSScott Long 			mtx_lock(&xsoftc.xpt_lock);
32492b83592fSScott Long 		 	if (xsoftc.num_highpower <= 0) {
32508b8a9b1dSJustin T. Gibbs 				/*
32518b8a9b1dSJustin T. Gibbs 				 * We got a high power command, but we
32528b8a9b1dSJustin T. Gibbs 				 * don't have any available slots.  Freeze
32538b8a9b1dSJustin T. Gibbs 				 * the device queue until we have a slot
32548b8a9b1dSJustin T. Gibbs 				 * available.
32558b8a9b1dSJustin T. Gibbs 				 */
325683c5d981SAlexander Motin 				xpt_freeze_devq(work_ccb->ccb_h.path, 1);
32572b83592fSScott Long 				STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
32588b8a9b1dSJustin T. Gibbs 						   &work_ccb->ccb_h,
32598b8a9b1dSJustin T. Gibbs 						   xpt_links.stqe);
32608b8a9b1dSJustin T. Gibbs 
3261469f9f44SScott Long 				mtx_unlock(&xsoftc.xpt_lock);
32628b8a9b1dSJustin T. Gibbs 				continue;
32638b8a9b1dSJustin T. Gibbs 			} else {
32648b8a9b1dSJustin T. Gibbs 				/*
32658b8a9b1dSJustin T. Gibbs 				 * Consume a high power slot while
32668b8a9b1dSJustin T. Gibbs 				 * this ccb runs.
32678b8a9b1dSJustin T. Gibbs 				 */
32682b83592fSScott Long 				xsoftc.num_highpower--;
32698b8a9b1dSJustin T. Gibbs 			}
32702b83592fSScott Long 			mtx_unlock(&xsoftc.xpt_lock);
32718b8a9b1dSJustin T. Gibbs 		}
32728b8a9b1dSJustin T. Gibbs 		cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
32738b8a9b1dSJustin T. Gibbs 		cam_ccbq_send_ccb(&device->ccbq, work_ccb);
32748b8a9b1dSJustin T. Gibbs 
32758b8a9b1dSJustin T. Gibbs 		devq->send_openings--;
32768b8a9b1dSJustin T. Gibbs 		devq->send_active++;
32778b8a9b1dSJustin T. Gibbs 
3278cccf4220SAlexander Motin 		xpt_schedule_devq(devq, device);
32798b8a9b1dSJustin T. Gibbs 
3280cccf4220SAlexander Motin 		if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
32818b8a9b1dSJustin T. Gibbs 			/*
32828b8a9b1dSJustin T. Gibbs 			 * The client wants to freeze the queue
32838b8a9b1dSJustin T. Gibbs 			 * after this CCB is sent.
32848b8a9b1dSJustin T. Gibbs 			 */
328583c5d981SAlexander Motin 			xpt_freeze_devq(work_ccb->ccb_h.path, 1);
32868b8a9b1dSJustin T. Gibbs 		}
32878b8a9b1dSJustin T. Gibbs 
3288a4eb4f16SMatt Jacob 		/* In Target mode, the peripheral driver knows best... */
3289a4eb4f16SMatt Jacob 		if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3290a4eb4f16SMatt Jacob 			if ((device->inq_flags & SID_CmdQue) != 0
3291a4eb4f16SMatt Jacob 			 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
32928b8a9b1dSJustin T. Gibbs 				work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
32938b8a9b1dSJustin T. Gibbs 			else
32948b8a9b1dSJustin T. Gibbs 				/*
3295a4eb4f16SMatt Jacob 				 * Clear this in case of a retried CCB that
3296a4eb4f16SMatt Jacob 				 * failed due to a rejected tag.
32978b8a9b1dSJustin T. Gibbs 				 */
32988b8a9b1dSJustin T. Gibbs 				work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3299a4eb4f16SMatt Jacob 		}
33008b8a9b1dSJustin T. Gibbs 
3301de9ebb68SAlexander Motin 		switch (work_ccb->ccb_h.func_code) {
3302de9ebb68SAlexander Motin 		case XPT_SCSI_IO:
3303de9ebb68SAlexander Motin 			CAM_DEBUG(work_ccb->ccb_h.path,
3304de9ebb68SAlexander Motin 			    CAM_DEBUG_CDB,("%s. CDB: %s\n",
3305de9ebb68SAlexander Motin 			     scsi_op_desc(work_ccb->csio.cdb_io.cdb_bytes[0],
3306de9ebb68SAlexander Motin 					  &device->inq_data),
3307de9ebb68SAlexander Motin 			     scsi_cdb_string(work_ccb->csio.cdb_io.cdb_bytes,
3308de9ebb68SAlexander Motin 					     cdb_str, sizeof(cdb_str))));
3309de9ebb68SAlexander Motin 			break;
3310de9ebb68SAlexander Motin 		case XPT_ATA_IO:
3311de9ebb68SAlexander Motin 			CAM_DEBUG(work_ccb->ccb_h.path,
3312de9ebb68SAlexander Motin 			    CAM_DEBUG_CDB,("%s. ACB: %s\n",
3313de9ebb68SAlexander Motin 			     ata_op_string(&work_ccb->ataio.cmd),
3314de9ebb68SAlexander Motin 			     ata_cmd_string(&work_ccb->ataio.cmd,
3315de9ebb68SAlexander Motin 					    cdb_str, sizeof(cdb_str))));
3316de9ebb68SAlexander Motin 			break;
3317de9ebb68SAlexander Motin 		default:
3318de9ebb68SAlexander Motin 			break;
3319de9ebb68SAlexander Motin 		}
3320de9ebb68SAlexander Motin 
33218b8a9b1dSJustin T. Gibbs 		/*
33228b8a9b1dSJustin T. Gibbs 		 * Device queues can be shared among multiple sim instances
33238b8a9b1dSJustin T. Gibbs 		 * that reside on different busses.  Use the SIM in the queue
33248b8a9b1dSJustin T. Gibbs 		 * CCB's path, rather than the one in the bus that was passed
33258b8a9b1dSJustin T. Gibbs 		 * into this function.
33268b8a9b1dSJustin T. Gibbs 		 */
33278b8a9b1dSJustin T. Gibbs 		sim = work_ccb->ccb_h.path->bus->sim;
33288b8a9b1dSJustin T. Gibbs 		(*(sim->sim_action))(sim, work_ccb);
33298b8a9b1dSJustin T. Gibbs 	}
3330cccf4220SAlexander Motin 	devq->send_queue.qfrozen_cnt--;
33318b8a9b1dSJustin T. Gibbs }
33328b8a9b1dSJustin T. Gibbs 
33338b8a9b1dSJustin T. Gibbs /*
33348b8a9b1dSJustin T. Gibbs  * This function merges stuff from the slave ccb into the master ccb, while
33358b8a9b1dSJustin T. Gibbs  * keeping important fields in the master ccb constant.
33368b8a9b1dSJustin T. Gibbs  */
33378b8a9b1dSJustin T. Gibbs void
33388b8a9b1dSJustin T. Gibbs xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
33398b8a9b1dSJustin T. Gibbs {
334068153f43SScott Long 
33418b8a9b1dSJustin T. Gibbs 	/*
33428b8a9b1dSJustin T. Gibbs 	 * Pull fields that are valid for peripheral drivers to set
33438b8a9b1dSJustin T. Gibbs 	 * into the master CCB along with the CCB "payload".
33448b8a9b1dSJustin T. Gibbs 	 */
33458b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
33468b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
33478b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
33488b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
33498b8a9b1dSJustin T. Gibbs 	bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
33508b8a9b1dSJustin T. Gibbs 	      sizeof(union ccb) - sizeof(struct ccb_hdr));
33518b8a9b1dSJustin T. Gibbs }
33528b8a9b1dSJustin T. Gibbs 
33538b8a9b1dSJustin T. Gibbs void
33548b8a9b1dSJustin T. Gibbs xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
33558b8a9b1dSJustin T. Gibbs {
335668153f43SScott Long 
33578b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
33588b8a9b1dSJustin T. Gibbs 	ccb_h->pinfo.priority = priority;
33598b8a9b1dSJustin T. Gibbs 	ccb_h->path = path;
33608b8a9b1dSJustin T. Gibbs 	ccb_h->path_id = path->bus->path_id;
33618b8a9b1dSJustin T. Gibbs 	if (path->target)
33628b8a9b1dSJustin T. Gibbs 		ccb_h->target_id = path->target->target_id;
33638b8a9b1dSJustin T. Gibbs 	else
33648b8a9b1dSJustin T. Gibbs 		ccb_h->target_id = CAM_TARGET_WILDCARD;
33658b8a9b1dSJustin T. Gibbs 	if (path->device) {
33668b8a9b1dSJustin T. Gibbs 		ccb_h->target_lun = path->device->lun_id;
33678bad620dSJustin T. Gibbs 		ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
33688b8a9b1dSJustin T. Gibbs 	} else {
33698b8a9b1dSJustin T. Gibbs 		ccb_h->target_lun = CAM_TARGET_WILDCARD;
33708b8a9b1dSJustin T. Gibbs 	}
33718b8a9b1dSJustin T. Gibbs 	ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
33728b8a9b1dSJustin T. Gibbs 	ccb_h->flags = 0;
33738b8a9b1dSJustin T. Gibbs }
33748b8a9b1dSJustin T. Gibbs 
33758b8a9b1dSJustin T. Gibbs /* Path manipulation functions */
33768b8a9b1dSJustin T. Gibbs cam_status
33778b8a9b1dSJustin T. Gibbs xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
33788b8a9b1dSJustin T. Gibbs 		path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
33798b8a9b1dSJustin T. Gibbs {
33808b8a9b1dSJustin T. Gibbs 	struct	   cam_path *path;
33818b8a9b1dSJustin T. Gibbs 	cam_status status;
33828b8a9b1dSJustin T. Gibbs 
3383596ee08fSAlexander Motin 	path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
33848b8a9b1dSJustin T. Gibbs 
33858b8a9b1dSJustin T. Gibbs 	if (path == NULL) {
33868b8a9b1dSJustin T. Gibbs 		status = CAM_RESRC_UNAVAIL;
33878b8a9b1dSJustin T. Gibbs 		return(status);
33888b8a9b1dSJustin T. Gibbs 	}
33898b8a9b1dSJustin T. Gibbs 	status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
33908b8a9b1dSJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
3391596ee08fSAlexander Motin 		free(path, M_CAMPATH);
33928b8a9b1dSJustin T. Gibbs 		path = NULL;
33938b8a9b1dSJustin T. Gibbs 	}
33948b8a9b1dSJustin T. Gibbs 	*new_path_ptr = path;
33958b8a9b1dSJustin T. Gibbs 	return (status);
33968b8a9b1dSJustin T. Gibbs }
33978b8a9b1dSJustin T. Gibbs 
33982b83592fSScott Long cam_status
33992b83592fSScott Long xpt_create_path_unlocked(struct cam_path **new_path_ptr,
34002b83592fSScott Long 			 struct cam_periph *periph, path_id_t path_id,
34012b83592fSScott Long 			 target_id_t target_id, lun_id_t lun_id)
34022b83592fSScott Long {
34032b83592fSScott Long 	struct	   cam_path *path;
34042b83592fSScott Long 	struct	   cam_eb *bus = NULL;
34052b83592fSScott Long 	cam_status status;
34062b83592fSScott Long 
3407596ee08fSAlexander Motin 	path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_WAITOK);
34082b83592fSScott Long 
34092b83592fSScott Long 	bus = xpt_find_bus(path_id);
34109bccc7a9SAlexander Motin 	if (bus != NULL)
341111e4faceSScott Long 		CAM_SIM_LOCK(bus->sim);
34122b83592fSScott Long 	status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
34139bccc7a9SAlexander Motin 	if (bus != NULL) {
341411e4faceSScott Long 		CAM_SIM_UNLOCK(bus->sim);
341515975b7bSMatt Jacob 		xpt_release_bus(bus);
341615975b7bSMatt Jacob 	}
34172b83592fSScott Long 	if (status != CAM_REQ_CMP) {
3418596ee08fSAlexander Motin 		free(path, M_CAMPATH);
34192b83592fSScott Long 		path = NULL;
34202b83592fSScott Long 	}
34212b83592fSScott Long 	*new_path_ptr = path;
34222b83592fSScott Long 	return (status);
34232b83592fSScott Long }
34242b83592fSScott Long 
342552c9ce25SScott Long cam_status
34268b8a9b1dSJustin T. Gibbs xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
34278b8a9b1dSJustin T. Gibbs 		 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
34288b8a9b1dSJustin T. Gibbs {
34298b8a9b1dSJustin T. Gibbs 	struct	     cam_eb *bus;
34308b8a9b1dSJustin T. Gibbs 	struct	     cam_et *target;
34318b8a9b1dSJustin T. Gibbs 	struct	     cam_ed *device;
34328b8a9b1dSJustin T. Gibbs 	cam_status   status;
34338b8a9b1dSJustin T. Gibbs 
34348b8a9b1dSJustin T. Gibbs 	status = CAM_REQ_CMP;	/* Completed without error */
34358b8a9b1dSJustin T. Gibbs 	target = NULL;		/* Wildcarded */
34368b8a9b1dSJustin T. Gibbs 	device = NULL;		/* Wildcarded */
3437a5479bc5SJustin T. Gibbs 
3438a5479bc5SJustin T. Gibbs 	/*
3439a5479bc5SJustin T. Gibbs 	 * We will potentially modify the EDT, so block interrupts
3440a5479bc5SJustin T. Gibbs 	 * that may attempt to create cam paths.
3441a5479bc5SJustin T. Gibbs 	 */
34428b8a9b1dSJustin T. Gibbs 	bus = xpt_find_bus(path_id);
34438b8a9b1dSJustin T. Gibbs 	if (bus == NULL) {
34448b8a9b1dSJustin T. Gibbs 		status = CAM_PATH_INVALID;
3445c8bead2aSJustin T. Gibbs 	} else {
34468b8a9b1dSJustin T. Gibbs 		target = xpt_find_target(bus, target_id);
34478b8a9b1dSJustin T. Gibbs 		if (target == NULL) {
34488b8a9b1dSJustin T. Gibbs 			/* Create one */
34498b8a9b1dSJustin T. Gibbs 			struct cam_et *new_target;
34508b8a9b1dSJustin T. Gibbs 
34518b8a9b1dSJustin T. Gibbs 			new_target = xpt_alloc_target(bus, target_id);
34528b8a9b1dSJustin T. Gibbs 			if (new_target == NULL) {
34538b8a9b1dSJustin T. Gibbs 				status = CAM_RESRC_UNAVAIL;
34548b8a9b1dSJustin T. Gibbs 			} else {
34558b8a9b1dSJustin T. Gibbs 				target = new_target;
34568b8a9b1dSJustin T. Gibbs 			}
34578b8a9b1dSJustin T. Gibbs 		}
3458c8bead2aSJustin T. Gibbs 		if (target != NULL) {
34598b8a9b1dSJustin T. Gibbs 			device = xpt_find_device(target, lun_id);
34608b8a9b1dSJustin T. Gibbs 			if (device == NULL) {
34618b8a9b1dSJustin T. Gibbs 				/* Create one */
34628b8a9b1dSJustin T. Gibbs 				struct cam_ed *new_device;
34638b8a9b1dSJustin T. Gibbs 
346452c9ce25SScott Long 				new_device =
346552c9ce25SScott Long 				    (*(bus->xport->alloc_device))(bus,
34668b8a9b1dSJustin T. Gibbs 								      target,
34678b8a9b1dSJustin T. Gibbs 								      lun_id);
34688b8a9b1dSJustin T. Gibbs 				if (new_device == NULL) {
34698b8a9b1dSJustin T. Gibbs 					status = CAM_RESRC_UNAVAIL;
34708b8a9b1dSJustin T. Gibbs 				} else {
34718b8a9b1dSJustin T. Gibbs 					device = new_device;
34728b8a9b1dSJustin T. Gibbs 				}
34738b8a9b1dSJustin T. Gibbs 			}
34748b8a9b1dSJustin T. Gibbs 		}
34758b8a9b1dSJustin T. Gibbs 	}
34768b8a9b1dSJustin T. Gibbs 
34778b8a9b1dSJustin T. Gibbs 	/*
34788b8a9b1dSJustin T. Gibbs 	 * Only touch the user's data if we are successful.
34798b8a9b1dSJustin T. Gibbs 	 */
34808b8a9b1dSJustin T. Gibbs 	if (status == CAM_REQ_CMP) {
34818b8a9b1dSJustin T. Gibbs 		new_path->periph = perph;
34828b8a9b1dSJustin T. Gibbs 		new_path->bus = bus;
34838b8a9b1dSJustin T. Gibbs 		new_path->target = target;
34848b8a9b1dSJustin T. Gibbs 		new_path->device = device;
34858b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
34868b8a9b1dSJustin T. Gibbs 	} else {
34878b8a9b1dSJustin T. Gibbs 		if (device != NULL)
3488f98d7a47SAlexander Motin 			xpt_release_device(device);
34898b8a9b1dSJustin T. Gibbs 		if (target != NULL)
3490f98d7a47SAlexander Motin 			xpt_release_target(target);
3491a5479bc5SJustin T. Gibbs 		if (bus != NULL)
3492a5479bc5SJustin T. Gibbs 			xpt_release_bus(bus);
34938b8a9b1dSJustin T. Gibbs 	}
34948b8a9b1dSJustin T. Gibbs 	return (status);
34958b8a9b1dSJustin T. Gibbs }
34968b8a9b1dSJustin T. Gibbs 
349752c9ce25SScott Long void
34988b8a9b1dSJustin T. Gibbs xpt_release_path(struct cam_path *path)
34998b8a9b1dSJustin T. Gibbs {
35008b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
35019dd03ecfSJustin T. Gibbs 	if (path->device != NULL) {
3502f98d7a47SAlexander Motin 		xpt_release_device(path->device);
35039dd03ecfSJustin T. Gibbs 		path->device = NULL;
35049dd03ecfSJustin T. Gibbs 	}
35059dd03ecfSJustin T. Gibbs 	if (path->target != NULL) {
3506f98d7a47SAlexander Motin 		xpt_release_target(path->target);
35079dd03ecfSJustin T. Gibbs 		path->target = NULL;
35089dd03ecfSJustin T. Gibbs 	}
35099dd03ecfSJustin T. Gibbs 	if (path->bus != NULL) {
35109dd03ecfSJustin T. Gibbs 		xpt_release_bus(path->bus);
35119dd03ecfSJustin T. Gibbs 		path->bus = NULL;
35129dd03ecfSJustin T. Gibbs 	}
35138b8a9b1dSJustin T. Gibbs }
35148b8a9b1dSJustin T. Gibbs 
35158b8a9b1dSJustin T. Gibbs void
35168b8a9b1dSJustin T. Gibbs xpt_free_path(struct cam_path *path)
35178b8a9b1dSJustin T. Gibbs {
351868153f43SScott Long 
35198b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
35208b8a9b1dSJustin T. Gibbs 	xpt_release_path(path);
3521596ee08fSAlexander Motin 	free(path, M_CAMPATH);
35228b8a9b1dSJustin T. Gibbs }
35238b8a9b1dSJustin T. Gibbs 
352415975b7bSMatt Jacob void
352515975b7bSMatt Jacob xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
352615975b7bSMatt Jacob     uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
352715975b7bSMatt Jacob {
352815975b7bSMatt Jacob 
35299a7c2696SAlexander Motin 	xpt_lock_buses();
353015975b7bSMatt Jacob 	if (bus_ref) {
353115975b7bSMatt Jacob 		if (path->bus)
353215975b7bSMatt Jacob 			*bus_ref = path->bus->refcount;
353315975b7bSMatt Jacob 		else
353415975b7bSMatt Jacob 			*bus_ref = 0;
353515975b7bSMatt Jacob 	}
353615975b7bSMatt Jacob 	if (periph_ref) {
353715975b7bSMatt Jacob 		if (path->periph)
353815975b7bSMatt Jacob 			*periph_ref = path->periph->refcount;
353915975b7bSMatt Jacob 		else
354015975b7bSMatt Jacob 			*periph_ref = 0;
354115975b7bSMatt Jacob 	}
3542dcdf6e74SAlexander Motin 	xpt_unlock_buses();
354315975b7bSMatt Jacob 	if (target_ref) {
354415975b7bSMatt Jacob 		if (path->target)
354515975b7bSMatt Jacob 			*target_ref = path->target->refcount;
354615975b7bSMatt Jacob 		else
354715975b7bSMatt Jacob 			*target_ref = 0;
354815975b7bSMatt Jacob 	}
354915975b7bSMatt Jacob 	if (device_ref) {
355015975b7bSMatt Jacob 		if (path->device)
355115975b7bSMatt Jacob 			*device_ref = path->device->refcount;
355215975b7bSMatt Jacob 		else
355315975b7bSMatt Jacob 			*device_ref = 0;
355415975b7bSMatt Jacob 	}
355515975b7bSMatt Jacob }
35568b8a9b1dSJustin T. Gibbs 
35578b8a9b1dSJustin T. Gibbs /*
35582cefde5fSJustin T. Gibbs  * Return -1 for failure, 0 for exact match, 1 for match with wildcards
35592cefde5fSJustin T. Gibbs  * in path1, 2 for match with wildcards in path2.
35608b8a9b1dSJustin T. Gibbs  */
35618b8a9b1dSJustin T. Gibbs int
35628b8a9b1dSJustin T. Gibbs xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
35638b8a9b1dSJustin T. Gibbs {
35648b8a9b1dSJustin T. Gibbs 	int retval = 0;
35658b8a9b1dSJustin T. Gibbs 
35668b8a9b1dSJustin T. Gibbs 	if (path1->bus != path2->bus) {
35672cefde5fSJustin T. Gibbs 		if (path1->bus->path_id == CAM_BUS_WILDCARD)
35688b8a9b1dSJustin T. Gibbs 			retval = 1;
35692cefde5fSJustin T. Gibbs 		else if (path2->bus->path_id == CAM_BUS_WILDCARD)
35702cefde5fSJustin T. Gibbs 			retval = 2;
35718b8a9b1dSJustin T. Gibbs 		else
35728b8a9b1dSJustin T. Gibbs 			return (-1);
35738b8a9b1dSJustin T. Gibbs 	}
35748b8a9b1dSJustin T. Gibbs 	if (path1->target != path2->target) {
35752cefde5fSJustin T. Gibbs 		if (path1->target->target_id == CAM_TARGET_WILDCARD) {
35762cefde5fSJustin T. Gibbs 			if (retval == 0)
35778b8a9b1dSJustin T. Gibbs 				retval = 1;
35782cefde5fSJustin T. Gibbs 		} else if (path2->target->target_id == CAM_TARGET_WILDCARD)
35792cefde5fSJustin T. Gibbs 			retval = 2;
35808b8a9b1dSJustin T. Gibbs 		else
35818b8a9b1dSJustin T. Gibbs 			return (-1);
35828b8a9b1dSJustin T. Gibbs 	}
35838b8a9b1dSJustin T. Gibbs 	if (path1->device != path2->device) {
35842cefde5fSJustin T. Gibbs 		if (path1->device->lun_id == CAM_LUN_WILDCARD) {
35852cefde5fSJustin T. Gibbs 			if (retval == 0)
35868b8a9b1dSJustin T. Gibbs 				retval = 1;
35872cefde5fSJustin T. Gibbs 		} else if (path2->device->lun_id == CAM_LUN_WILDCARD)
35882cefde5fSJustin T. Gibbs 			retval = 2;
35898b8a9b1dSJustin T. Gibbs 		else
35908b8a9b1dSJustin T. Gibbs 			return (-1);
35918b8a9b1dSJustin T. Gibbs 	}
35928b8a9b1dSJustin T. Gibbs 	return (retval);
35938b8a9b1dSJustin T. Gibbs }
35948b8a9b1dSJustin T. Gibbs 
35958b8a9b1dSJustin T. Gibbs void
35968b8a9b1dSJustin T. Gibbs xpt_print_path(struct cam_path *path)
35978b8a9b1dSJustin T. Gibbs {
359868153f43SScott Long 
35998b8a9b1dSJustin T. Gibbs 	if (path == NULL)
36008b8a9b1dSJustin T. Gibbs 		printf("(nopath): ");
36018b8a9b1dSJustin T. Gibbs 	else {
36028b8a9b1dSJustin T. Gibbs 		if (path->periph != NULL)
36038b8a9b1dSJustin T. Gibbs 			printf("(%s%d:", path->periph->periph_name,
36048b8a9b1dSJustin T. Gibbs 			       path->periph->unit_number);
36058b8a9b1dSJustin T. Gibbs 		else
36068b8a9b1dSJustin T. Gibbs 			printf("(noperiph:");
36078b8a9b1dSJustin T. Gibbs 
36088b8a9b1dSJustin T. Gibbs 		if (path->bus != NULL)
36098b8a9b1dSJustin T. Gibbs 			printf("%s%d:%d:", path->bus->sim->sim_name,
36108b8a9b1dSJustin T. Gibbs 			       path->bus->sim->unit_number,
36118b8a9b1dSJustin T. Gibbs 			       path->bus->sim->bus_id);
36128b8a9b1dSJustin T. Gibbs 		else
36138b8a9b1dSJustin T. Gibbs 			printf("nobus:");
36148b8a9b1dSJustin T. Gibbs 
36158b8a9b1dSJustin T. Gibbs 		if (path->target != NULL)
36168b8a9b1dSJustin T. Gibbs 			printf("%d:", path->target->target_id);
36178b8a9b1dSJustin T. Gibbs 		else
36188b8a9b1dSJustin T. Gibbs 			printf("X:");
36198b8a9b1dSJustin T. Gibbs 
36208b8a9b1dSJustin T. Gibbs 		if (path->device != NULL)
36218b8a9b1dSJustin T. Gibbs 			printf("%d): ", path->device->lun_id);
36228b8a9b1dSJustin T. Gibbs 		else
36238b8a9b1dSJustin T. Gibbs 			printf("X): ");
36248b8a9b1dSJustin T. Gibbs 	}
36258b8a9b1dSJustin T. Gibbs }
36268b8a9b1dSJustin T. Gibbs 
3627f0d9af51SMatt Jacob void
3628f0d9af51SMatt Jacob xpt_print(struct cam_path *path, const char *fmt, ...)
3629f0d9af51SMatt Jacob {
3630f0d9af51SMatt Jacob 	va_list ap;
3631f0d9af51SMatt Jacob 	xpt_print_path(path);
3632f0d9af51SMatt Jacob 	va_start(ap, fmt);
3633f0d9af51SMatt Jacob 	vprintf(fmt, ap);
3634f0d9af51SMatt Jacob 	va_end(ap);
3635f0d9af51SMatt Jacob }
3636f0d9af51SMatt Jacob 
36373393f8daSKenneth D. Merry int
36383393f8daSKenneth D. Merry xpt_path_string(struct cam_path *path, char *str, size_t str_len)
36393393f8daSKenneth D. Merry {
36403393f8daSKenneth D. Merry 	struct sbuf sb;
36413393f8daSKenneth D. Merry 
3642ac37e649SEdward Tomasz Napierala #ifdef INVARIANTS
3643a8d4700dSEdward Tomasz Napierala 	if (path != NULL && path->bus != NULL)
36442b83592fSScott Long 		mtx_assert(path->bus->sim->mtx, MA_OWNED);
3645ac37e649SEdward Tomasz Napierala #endif
364668153f43SScott Long 
36473393f8daSKenneth D. Merry 	sbuf_new(&sb, str, str_len, 0);
36483393f8daSKenneth D. Merry 
36493393f8daSKenneth D. Merry 	if (path == NULL)
36503393f8daSKenneth D. Merry 		sbuf_printf(&sb, "(nopath): ");
36513393f8daSKenneth D. Merry 	else {
36523393f8daSKenneth D. Merry 		if (path->periph != NULL)
36533393f8daSKenneth D. Merry 			sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
36543393f8daSKenneth D. Merry 				    path->periph->unit_number);
36553393f8daSKenneth D. Merry 		else
36563393f8daSKenneth D. Merry 			sbuf_printf(&sb, "(noperiph:");
36573393f8daSKenneth D. Merry 
36583393f8daSKenneth D. Merry 		if (path->bus != NULL)
36593393f8daSKenneth D. Merry 			sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
36603393f8daSKenneth D. Merry 				    path->bus->sim->unit_number,
36613393f8daSKenneth D. Merry 				    path->bus->sim->bus_id);
36623393f8daSKenneth D. Merry 		else
36633393f8daSKenneth D. Merry 			sbuf_printf(&sb, "nobus:");
36643393f8daSKenneth D. Merry 
36653393f8daSKenneth D. Merry 		if (path->target != NULL)
36663393f8daSKenneth D. Merry 			sbuf_printf(&sb, "%d:", path->target->target_id);
36673393f8daSKenneth D. Merry 		else
36683393f8daSKenneth D. Merry 			sbuf_printf(&sb, "X:");
36693393f8daSKenneth D. Merry 
36703393f8daSKenneth D. Merry 		if (path->device != NULL)
36713393f8daSKenneth D. Merry 			sbuf_printf(&sb, "%d): ", path->device->lun_id);
36723393f8daSKenneth D. Merry 		else
36733393f8daSKenneth D. Merry 			sbuf_printf(&sb, "X): ");
36743393f8daSKenneth D. Merry 	}
36753393f8daSKenneth D. Merry 	sbuf_finish(&sb);
36763393f8daSKenneth D. Merry 
36773393f8daSKenneth D. Merry 	return(sbuf_len(&sb));
36783393f8daSKenneth D. Merry }
36793393f8daSKenneth D. Merry 
36808b8a9b1dSJustin T. Gibbs path_id_t
36818b8a9b1dSJustin T. Gibbs xpt_path_path_id(struct cam_path *path)
36828b8a9b1dSJustin T. Gibbs {
36838b8a9b1dSJustin T. Gibbs 	return(path->bus->path_id);
36848b8a9b1dSJustin T. Gibbs }
36858b8a9b1dSJustin T. Gibbs 
36868b8a9b1dSJustin T. Gibbs target_id_t
36878b8a9b1dSJustin T. Gibbs xpt_path_target_id(struct cam_path *path)
36888b8a9b1dSJustin T. Gibbs {
36898b8a9b1dSJustin T. Gibbs 	if (path->target != NULL)
36908b8a9b1dSJustin T. Gibbs 		return (path->target->target_id);
36918b8a9b1dSJustin T. Gibbs 	else
36928b8a9b1dSJustin T. Gibbs 		return (CAM_TARGET_WILDCARD);
36938b8a9b1dSJustin T. Gibbs }
36948b8a9b1dSJustin T. Gibbs 
36958b8a9b1dSJustin T. Gibbs lun_id_t
36968b8a9b1dSJustin T. Gibbs xpt_path_lun_id(struct cam_path *path)
36978b8a9b1dSJustin T. Gibbs {
36988b8a9b1dSJustin T. Gibbs 	if (path->device != NULL)
36998b8a9b1dSJustin T. Gibbs 		return (path->device->lun_id);
37008b8a9b1dSJustin T. Gibbs 	else
37018b8a9b1dSJustin T. Gibbs 		return (CAM_LUN_WILDCARD);
37028b8a9b1dSJustin T. Gibbs }
37038b8a9b1dSJustin T. Gibbs 
37048b8a9b1dSJustin T. Gibbs struct cam_sim *
37058b8a9b1dSJustin T. Gibbs xpt_path_sim(struct cam_path *path)
37068b8a9b1dSJustin T. Gibbs {
370768153f43SScott Long 
37088b8a9b1dSJustin T. Gibbs 	return (path->bus->sim);
37098b8a9b1dSJustin T. Gibbs }
37108b8a9b1dSJustin T. Gibbs 
37118b8a9b1dSJustin T. Gibbs struct cam_periph*
37128b8a9b1dSJustin T. Gibbs xpt_path_periph(struct cam_path *path)
37138b8a9b1dSJustin T. Gibbs {
37142b83592fSScott Long 	mtx_assert(path->bus->sim->mtx, MA_OWNED);
371568153f43SScott Long 
37168b8a9b1dSJustin T. Gibbs 	return (path->periph);
37178b8a9b1dSJustin T. Gibbs }
37188b8a9b1dSJustin T. Gibbs 
37190d307e09SAlexander Motin int
37200d307e09SAlexander Motin xpt_path_legacy_ata_id(struct cam_path *path)
37210d307e09SAlexander Motin {
37220d307e09SAlexander Motin 	struct cam_eb *bus;
37230d307e09SAlexander Motin 	int bus_id;
37240d307e09SAlexander Motin 
37250d307e09SAlexander Motin 	if ((strcmp(path->bus->sim->sim_name, "ata") != 0) &&
37260d307e09SAlexander Motin 	    strcmp(path->bus->sim->sim_name, "ahcich") != 0 &&
37270d307e09SAlexander Motin 	    strcmp(path->bus->sim->sim_name, "mvsch") != 0 &&
37280d307e09SAlexander Motin 	    strcmp(path->bus->sim->sim_name, "siisch") != 0)
37290d307e09SAlexander Motin 		return (-1);
37300d307e09SAlexander Motin 
37310d307e09SAlexander Motin 	if (strcmp(path->bus->sim->sim_name, "ata") == 0 &&
37320d307e09SAlexander Motin 	    path->bus->sim->unit_number < 2) {
37330d307e09SAlexander Motin 		bus_id = path->bus->sim->unit_number;
37340d307e09SAlexander Motin 	} else {
37350d307e09SAlexander Motin 		bus_id = 2;
37360d307e09SAlexander Motin 		xpt_lock_buses();
37370d307e09SAlexander Motin 		TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
37380d307e09SAlexander Motin 			if (bus == path->bus)
37390d307e09SAlexander Motin 				break;
37400d307e09SAlexander Motin 			if ((strcmp(bus->sim->sim_name, "ata") == 0 &&
37410d307e09SAlexander Motin 			     bus->sim->unit_number >= 2) ||
37420d307e09SAlexander Motin 			    strcmp(bus->sim->sim_name, "ahcich") == 0 ||
37430d307e09SAlexander Motin 			    strcmp(bus->sim->sim_name, "mvsch") == 0 ||
37440d307e09SAlexander Motin 			    strcmp(bus->sim->sim_name, "siisch") == 0)
37450d307e09SAlexander Motin 				bus_id++;
37460d307e09SAlexander Motin 		}
37470d307e09SAlexander Motin 		xpt_unlock_buses();
37480d307e09SAlexander Motin 	}
3749ce6cf987SAlexander Motin 	if (path->target != NULL) {
3750ce6cf987SAlexander Motin 		if (path->target->target_id < 2)
37510d307e09SAlexander Motin 			return (bus_id * 2 + path->target->target_id);
37520d307e09SAlexander Motin 		else
3753ce6cf987SAlexander Motin 			return (-1);
3754ce6cf987SAlexander Motin 	} else
37550d307e09SAlexander Motin 		return (bus_id * 2);
37560d307e09SAlexander Motin }
37570d307e09SAlexander Motin 
37588b8a9b1dSJustin T. Gibbs /*
37598b8a9b1dSJustin T. Gibbs  * Release a CAM control block for the caller.  Remit the cost of the structure
37608b8a9b1dSJustin T. Gibbs  * to the device referenced by the path.  If the this device had no 'credits'
37618b8a9b1dSJustin T. Gibbs  * and peripheral drivers have registered async callbacks for this notification
37628b8a9b1dSJustin T. Gibbs  * call them now.
37638b8a9b1dSJustin T. Gibbs  */
37648b8a9b1dSJustin T. Gibbs void
37658b8a9b1dSJustin T. Gibbs xpt_release_ccb(union ccb *free_ccb)
37668b8a9b1dSJustin T. Gibbs {
37678b8a9b1dSJustin T. Gibbs 	struct	 cam_path *path;
37688b8a9b1dSJustin T. Gibbs 	struct	 cam_ed *device;
37698b8a9b1dSJustin T. Gibbs 	struct	 cam_eb *bus;
37702b83592fSScott Long 	struct   cam_sim *sim;
377168153f43SScott Long 
3772aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
37738b8a9b1dSJustin T. Gibbs 	path = free_ccb->ccb_h.path;
37748b8a9b1dSJustin T. Gibbs 	device = path->device;
37758b8a9b1dSJustin T. Gibbs 	bus = path->bus;
37762b83592fSScott Long 	sim = bus->sim;
37772b83592fSScott Long 
37782b83592fSScott Long 	mtx_assert(sim->mtx, MA_OWNED);
37792b83592fSScott Long 
37808b8a9b1dSJustin T. Gibbs 	cam_ccbq_release_opening(&device->ccbq);
378130a4094fSAlexander Motin 	if (device->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) {
378230a4094fSAlexander Motin 		device->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
378330a4094fSAlexander Motin 		cam_ccbq_resize(&device->ccbq,
378430a4094fSAlexander Motin 		    device->ccbq.dev_openings + device->ccbq.dev_active);
378530a4094fSAlexander Motin 	}
37862b83592fSScott Long 	if (sim->ccb_count > sim->max_ccbs) {
37878b8a9b1dSJustin T. Gibbs 		xpt_free_ccb(free_ccb);
37882b83592fSScott Long 		sim->ccb_count--;
37898b8a9b1dSJustin T. Gibbs 	} else {
37902b83592fSScott Long 		SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
37912b83592fSScott Long 		    xpt_links.sle);
37928b8a9b1dSJustin T. Gibbs 	}
3793cccf4220SAlexander Motin 	xpt_run_dev_allocq(device);
37948b8a9b1dSJustin T. Gibbs }
37958b8a9b1dSJustin T. Gibbs 
37968b8a9b1dSJustin T. Gibbs /* Functions accessed by SIM drivers */
37978b8a9b1dSJustin T. Gibbs 
379852c9ce25SScott Long static struct xpt_xport xport_default = {
379952c9ce25SScott Long 	.alloc_device = xpt_alloc_device_default,
380052c9ce25SScott Long 	.action = xpt_action_default,
380152c9ce25SScott Long 	.async = xpt_dev_async_default,
380252c9ce25SScott Long };
380352c9ce25SScott Long 
38048b8a9b1dSJustin T. Gibbs /*
38058b8a9b1dSJustin T. Gibbs  * A sim structure, listing the SIM entry points and instance
38068b8a9b1dSJustin T. Gibbs  * identification info is passed to xpt_bus_register to hook the SIM
38078b8a9b1dSJustin T. Gibbs  * into the CAM framework.  xpt_bus_register creates a cam_eb entry
38088b8a9b1dSJustin T. Gibbs  * for this new bus and places it in the array of busses and assigns
38098b8a9b1dSJustin T. Gibbs  * it a path_id.  The path_id may be influenced by "hard wiring"
38108b8a9b1dSJustin T. Gibbs  * information specified by the user.  Once interrupt services are
381102caf36eSEdward Tomasz Napierala  * available, the bus will be probed.
38128b8a9b1dSJustin T. Gibbs  */
38138b8a9b1dSJustin T. Gibbs int32_t
3814b50569b7SScott Long xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
38158b8a9b1dSJustin T. Gibbs {
38168b8a9b1dSJustin T. Gibbs 	struct cam_eb *new_bus;
3817434bbf6eSJustin T. Gibbs 	struct cam_eb *old_bus;
38188b8a9b1dSJustin T. Gibbs 	struct ccb_pathinq cpi;
381983c5d981SAlexander Motin 	struct cam_path *path;
382052c9ce25SScott Long 	cam_status status;
38218b8a9b1dSJustin T. Gibbs 
38222b83592fSScott Long 	mtx_assert(sim->mtx, MA_OWNED);
382368153f43SScott Long 
38248b8a9b1dSJustin T. Gibbs 	sim->bus_id = bus;
38258b8a9b1dSJustin T. Gibbs 	new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3826362abc44STai-hwa Liang 					  M_CAMXPT, M_NOWAIT);
38278b8a9b1dSJustin T. Gibbs 	if (new_bus == NULL) {
38288b8a9b1dSJustin T. Gibbs 		/* Couldn't satisfy request */
38298b8a9b1dSJustin T. Gibbs 		return (CAM_RESRC_UNAVAIL);
38308b8a9b1dSJustin T. Gibbs 	}
38318b8a9b1dSJustin T. Gibbs 	if (strcmp(sim->sim_name, "xpt") != 0) {
3832434bbf6eSJustin T. Gibbs 		sim->path_id =
3833434bbf6eSJustin T. Gibbs 		    xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
38348b8a9b1dSJustin T. Gibbs 	}
38358b8a9b1dSJustin T. Gibbs 
3836434bbf6eSJustin T. Gibbs 	TAILQ_INIT(&new_bus->et_entries);
38378b8a9b1dSJustin T. Gibbs 	new_bus->path_id = sim->path_id;
3838fa6099fdSEdward Tomasz Napierala 	cam_sim_hold(sim);
38398b8a9b1dSJustin T. Gibbs 	new_bus->sim = sim;
384087cfaf0eSJustin T. Gibbs 	timevalclear(&new_bus->last_reset);
3841434bbf6eSJustin T. Gibbs 	new_bus->flags = 0;
3842a5479bc5SJustin T. Gibbs 	new_bus->refcount = 1;	/* Held until a bus_deregister event */
3843434bbf6eSJustin T. Gibbs 	new_bus->generation = 0;
384452c9ce25SScott Long 
38459a7c2696SAlexander Motin 	xpt_lock_buses();
38462b83592fSScott Long 	old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3847434bbf6eSJustin T. Gibbs 	while (old_bus != NULL
3848434bbf6eSJustin T. Gibbs 	    && old_bus->path_id < new_bus->path_id)
3849434bbf6eSJustin T. Gibbs 		old_bus = TAILQ_NEXT(old_bus, links);
3850434bbf6eSJustin T. Gibbs 	if (old_bus != NULL)
3851434bbf6eSJustin T. Gibbs 		TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3852434bbf6eSJustin T. Gibbs 	else
38532b83592fSScott Long 		TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
38542b83592fSScott Long 	xsoftc.bus_generation++;
38559a7c2696SAlexander Motin 	xpt_unlock_buses();
38568b8a9b1dSJustin T. Gibbs 
385752c9ce25SScott Long 	/*
385852c9ce25SScott Long 	 * Set a default transport so that a PATH_INQ can be issued to
385952c9ce25SScott Long 	 * the SIM.  This will then allow for probing and attaching of
386052c9ce25SScott Long 	 * a more appropriate transport.
386152c9ce25SScott Long 	 */
386252c9ce25SScott Long 	new_bus->xport = &xport_default;
38638b8a9b1dSJustin T. Gibbs 
386432aa80a6SAlexander Motin 	status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
38658b8a9b1dSJustin T. Gibbs 				  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3866627995dcSAlexander Motin 	if (status != CAM_REQ_CMP) {
3867627995dcSAlexander Motin 		xpt_release_bus(new_bus);
3868627995dcSAlexander Motin 		free(path, M_CAMXPT);
3869627995dcSAlexander Motin 		return (CAM_RESRC_UNAVAIL);
3870627995dcSAlexander Motin 	}
387152c9ce25SScott Long 
387283c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
38738b8a9b1dSJustin T. Gibbs 	cpi.ccb_h.func_code = XPT_PATH_INQ;
38748b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&cpi);
387552c9ce25SScott Long 
387652c9ce25SScott Long 	if (cpi.ccb_h.status == CAM_REQ_CMP) {
387752c9ce25SScott Long 		switch (cpi.transport) {
387852c9ce25SScott Long 		case XPORT_SPI:
387952c9ce25SScott Long 		case XPORT_SAS:
388052c9ce25SScott Long 		case XPORT_FC:
388152c9ce25SScott Long 		case XPORT_USB:
388233ea30feSAlexander Motin 		case XPORT_ISCSI:
388333ea30feSAlexander Motin 		case XPORT_PPB:
388452c9ce25SScott Long 			new_bus->xport = scsi_get_xport();
388552c9ce25SScott Long 			break;
388652c9ce25SScott Long 		case XPORT_ATA:
388752c9ce25SScott Long 		case XPORT_SATA:
388852c9ce25SScott Long 			new_bus->xport = ata_get_xport();
388952c9ce25SScott Long 			break;
389052c9ce25SScott Long 		default:
389152c9ce25SScott Long 			new_bus->xport = &xport_default;
389252c9ce25SScott Long 			break;
38938b8a9b1dSJustin T. Gibbs 		}
389452c9ce25SScott Long 	}
389552c9ce25SScott Long 
389652c9ce25SScott Long 	/* Notify interested parties */
389752c9ce25SScott Long 	if (sim->path_id != CAM_XPT_PATH_ID) {
389883c5d981SAlexander Motin 		union	ccb *scan_ccb;
389983c5d981SAlexander Motin 
390083c5d981SAlexander Motin 		xpt_async(AC_PATH_REGISTERED, path, &cpi);
390183c5d981SAlexander Motin 		/* Initiate bus rescan. */
390283c5d981SAlexander Motin 		scan_ccb = xpt_alloc_ccb_nowait();
3903*e5736ac8SAlexander Motin 		if (scan_ccb != NULL) {
390483c5d981SAlexander Motin 			scan_ccb->ccb_h.path = path;
390583c5d981SAlexander Motin 			scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
390683c5d981SAlexander Motin 			scan_ccb->crcn.flags = 0;
390783c5d981SAlexander Motin 			xpt_rescan(scan_ccb);
390883c5d981SAlexander Motin 		} else
3909*e5736ac8SAlexander Motin 			xpt_print(path, "Can't allocate CCB to scan bus\n");
3910*e5736ac8SAlexander Motin 	} else
391183c5d981SAlexander Motin 		xpt_free_path(path);
39128b8a9b1dSJustin T. Gibbs 	return (CAM_SUCCESS);
39138b8a9b1dSJustin T. Gibbs }
39148b8a9b1dSJustin T. Gibbs 
3915434bbf6eSJustin T. Gibbs int32_t
3916434bbf6eSJustin T. Gibbs xpt_bus_deregister(path_id_t pathid)
39178b8a9b1dSJustin T. Gibbs {
3918434bbf6eSJustin T. Gibbs 	struct cam_path bus_path;
3919434bbf6eSJustin T. Gibbs 	cam_status status;
3920434bbf6eSJustin T. Gibbs 
3921434bbf6eSJustin T. Gibbs 	status = xpt_compile_path(&bus_path, NULL, pathid,
3922434bbf6eSJustin T. Gibbs 				  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3923434bbf6eSJustin T. Gibbs 	if (status != CAM_REQ_CMP)
3924434bbf6eSJustin T. Gibbs 		return (status);
3925434bbf6eSJustin T. Gibbs 
3926434bbf6eSJustin T. Gibbs 	xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3927434bbf6eSJustin T. Gibbs 	xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3928434bbf6eSJustin T. Gibbs 
3929434bbf6eSJustin T. Gibbs 	/* Release the reference count held while registered. */
3930434bbf6eSJustin T. Gibbs 	xpt_release_bus(bus_path.bus);
3931434bbf6eSJustin T. Gibbs 	xpt_release_path(&bus_path);
3932434bbf6eSJustin T. Gibbs 
3933434bbf6eSJustin T. Gibbs 	return (CAM_REQ_CMP);
3934434bbf6eSJustin T. Gibbs }
3935434bbf6eSJustin T. Gibbs 
3936434bbf6eSJustin T. Gibbs static path_id_t
3937434bbf6eSJustin T. Gibbs xptnextfreepathid(void)
3938434bbf6eSJustin T. Gibbs {
3939434bbf6eSJustin T. Gibbs 	struct cam_eb *bus;
3940434bbf6eSJustin T. Gibbs 	path_id_t pathid;
39412398f0cdSPeter Wemm 	const char *strval;
39428b8a9b1dSJustin T. Gibbs 
3943434bbf6eSJustin T. Gibbs 	pathid = 0;
39449a7c2696SAlexander Motin 	xpt_lock_buses();
39452b83592fSScott Long 	bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3946434bbf6eSJustin T. Gibbs retry:
3947434bbf6eSJustin T. Gibbs 	/* Find an unoccupied pathid */
39489e6461a2SMatt Jacob 	while (bus != NULL && bus->path_id <= pathid) {
3949434bbf6eSJustin T. Gibbs 		if (bus->path_id == pathid)
3950434bbf6eSJustin T. Gibbs 			pathid++;
3951434bbf6eSJustin T. Gibbs 		bus = TAILQ_NEXT(bus, links);
3952434bbf6eSJustin T. Gibbs 	}
39539a7c2696SAlexander Motin 	xpt_unlock_buses();
3954434bbf6eSJustin T. Gibbs 
3955434bbf6eSJustin T. Gibbs 	/*
3956434bbf6eSJustin T. Gibbs 	 * Ensure that this pathid is not reserved for
3957434bbf6eSJustin T. Gibbs 	 * a bus that may be registered in the future.
3958434bbf6eSJustin T. Gibbs 	 */
395975f51904SPeter Wemm 	if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
3960434bbf6eSJustin T. Gibbs 		++pathid;
39618b8a9b1dSJustin T. Gibbs 		/* Start the search over */
39629a7c2696SAlexander Motin 		xpt_lock_buses();
3963434bbf6eSJustin T. Gibbs 		goto retry;
39648b8a9b1dSJustin T. Gibbs 	}
3965434bbf6eSJustin T. Gibbs 	return (pathid);
39668b8a9b1dSJustin T. Gibbs }
39678b8a9b1dSJustin T. Gibbs 
3968434bbf6eSJustin T. Gibbs static path_id_t
3969434bbf6eSJustin T. Gibbs xptpathid(const char *sim_name, int sim_unit, int sim_bus)
39708b8a9b1dSJustin T. Gibbs {
39718b8a9b1dSJustin T. Gibbs 	path_id_t pathid;
397275f51904SPeter Wemm 	int i, dunit, val;
3973642f0c46SPeter Wemm 	char buf[32];
39742398f0cdSPeter Wemm 	const char *dname;
39758b8a9b1dSJustin T. Gibbs 
39768b8a9b1dSJustin T. Gibbs 	pathid = CAM_XPT_PATH_ID;
397775f51904SPeter Wemm 	snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
39782398f0cdSPeter Wemm 	i = 0;
39792398f0cdSPeter Wemm 	while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
39802398f0cdSPeter Wemm 		if (strcmp(dname, "scbus")) {
3981642f0c46SPeter Wemm 			/* Avoid a bit of foot shooting. */
3982642f0c46SPeter Wemm 			continue;
3983642f0c46SPeter Wemm 		}
398475f51904SPeter Wemm 		if (dunit < 0)		/* unwired?! */
39858b8a9b1dSJustin T. Gibbs 			continue;
398675f51904SPeter Wemm 		if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
398775f51904SPeter Wemm 			if (sim_bus == val) {
398875f51904SPeter Wemm 				pathid = dunit;
39898b8a9b1dSJustin T. Gibbs 				break;
39908b8a9b1dSJustin T. Gibbs 			}
39918b8a9b1dSJustin T. Gibbs 		} else if (sim_bus == 0) {
39928b8a9b1dSJustin T. Gibbs 			/* Unspecified matches bus 0 */
399375f51904SPeter Wemm 			pathid = dunit;
39948b8a9b1dSJustin T. Gibbs 			break;
39958b8a9b1dSJustin T. Gibbs 		} else {
39968b8a9b1dSJustin T. Gibbs 			printf("Ambiguous scbus configuration for %s%d "
39978b8a9b1dSJustin T. Gibbs 			       "bus %d, cannot wire down.  The kernel "
39988b8a9b1dSJustin T. Gibbs 			       "config entry for scbus%d should "
39998b8a9b1dSJustin T. Gibbs 			       "specify a controller bus.\n"
40008b8a9b1dSJustin T. Gibbs 			       "Scbus will be assigned dynamically.\n",
400175f51904SPeter Wemm 			       sim_name, sim_unit, sim_bus, dunit);
40028b8a9b1dSJustin T. Gibbs 			break;
40038b8a9b1dSJustin T. Gibbs 		}
40048b8a9b1dSJustin T. Gibbs 	}
40058b8a9b1dSJustin T. Gibbs 
4006434bbf6eSJustin T. Gibbs 	if (pathid == CAM_XPT_PATH_ID)
4007434bbf6eSJustin T. Gibbs 		pathid = xptnextfreepathid();
40088b8a9b1dSJustin T. Gibbs 	return (pathid);
40098b8a9b1dSJustin T. Gibbs }
40108b8a9b1dSJustin T. Gibbs 
401122c7d606SAlexander Motin static const char *
401222c7d606SAlexander Motin xpt_async_string(u_int32_t async_code)
401322c7d606SAlexander Motin {
401422c7d606SAlexander Motin 
401522c7d606SAlexander Motin 	switch (async_code) {
401622c7d606SAlexander Motin 	case AC_BUS_RESET: return ("AC_BUS_RESET");
401722c7d606SAlexander Motin 	case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
401822c7d606SAlexander Motin 	case AC_SCSI_AEN: return ("AC_SCSI_AEN");
401922c7d606SAlexander Motin 	case AC_SENT_BDR: return ("AC_SENT_BDR");
402022c7d606SAlexander Motin 	case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
402122c7d606SAlexander Motin 	case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
402222c7d606SAlexander Motin 	case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
402322c7d606SAlexander Motin 	case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
402422c7d606SAlexander Motin 	case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
402522c7d606SAlexander Motin 	case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
402622c7d606SAlexander Motin 	case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
402722c7d606SAlexander Motin 	case AC_CONTRACT: return ("AC_CONTRACT");
402822c7d606SAlexander Motin 	case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
40293631c638SAlexander Motin 	case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
403022c7d606SAlexander Motin 	}
403122c7d606SAlexander Motin 	return ("AC_UNKNOWN");
403222c7d606SAlexander Motin }
403322c7d606SAlexander Motin 
40348b8a9b1dSJustin T. Gibbs void
40358b8a9b1dSJustin T. Gibbs xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
40368b8a9b1dSJustin T. Gibbs {
40378b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus;
40388b8a9b1dSJustin T. Gibbs 	struct cam_et *target, *next_target;
40398b8a9b1dSJustin T. Gibbs 	struct cam_ed *device, *next_device;
40408b8a9b1dSJustin T. Gibbs 
40412b83592fSScott Long 	mtx_assert(path->bus->sim->mtx, MA_OWNED);
404222c7d606SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
404322c7d606SAlexander Motin 	    ("xpt_async(%s)\n", xpt_async_string(async_code)));
40448b8a9b1dSJustin T. Gibbs 
4045a5479bc5SJustin T. Gibbs 	/*
4046a5479bc5SJustin T. Gibbs 	 * Most async events come from a CAM interrupt context.  In
4047a5479bc5SJustin T. Gibbs 	 * a few cases, the error recovery code at the peripheral layer,
4048a5479bc5SJustin T. Gibbs 	 * which may run from our SWI or a process context, may signal
404977dc25ccSScott Long 	 * deferred events with a call to xpt_async.
4050a5479bc5SJustin T. Gibbs 	 */
40518b8a9b1dSJustin T. Gibbs 
40528b8a9b1dSJustin T. Gibbs 	bus = path->bus;
40538b8a9b1dSJustin T. Gibbs 
40548b8a9b1dSJustin T. Gibbs 	if (async_code == AC_BUS_RESET) {
405587cfaf0eSJustin T. Gibbs 		/* Update our notion of when the last reset occurred */
405687cfaf0eSJustin T. Gibbs 		microtime(&bus->last_reset);
40578b8a9b1dSJustin T. Gibbs 	}
40588b8a9b1dSJustin T. Gibbs 
40598b8a9b1dSJustin T. Gibbs 	for (target = TAILQ_FIRST(&bus->et_entries);
40608b8a9b1dSJustin T. Gibbs 	     target != NULL;
40618b8a9b1dSJustin T. Gibbs 	     target = next_target) {
40628b8a9b1dSJustin T. Gibbs 
40638b8a9b1dSJustin T. Gibbs 		next_target = TAILQ_NEXT(target, links);
40648b8a9b1dSJustin T. Gibbs 
40658b8a9b1dSJustin T. Gibbs 		if (path->target != target
40662f22d08dSJustin T. Gibbs 		 && path->target->target_id != CAM_TARGET_WILDCARD
40672f22d08dSJustin T. Gibbs 		 && target->target_id != CAM_TARGET_WILDCARD)
40688b8a9b1dSJustin T. Gibbs 			continue;
40698b8a9b1dSJustin T. Gibbs 
407087cfaf0eSJustin T. Gibbs 		if (async_code == AC_SENT_BDR) {
407187cfaf0eSJustin T. Gibbs 			/* Update our notion of when the last reset occurred */
407287cfaf0eSJustin T. Gibbs 			microtime(&path->target->last_reset);
407387cfaf0eSJustin T. Gibbs 		}
407487cfaf0eSJustin T. Gibbs 
40758b8a9b1dSJustin T. Gibbs 		for (device = TAILQ_FIRST(&target->ed_entries);
40768b8a9b1dSJustin T. Gibbs 		     device != NULL;
40778b8a9b1dSJustin T. Gibbs 		     device = next_device) {
40788b8a9b1dSJustin T. Gibbs 
40798b8a9b1dSJustin T. Gibbs 			next_device = TAILQ_NEXT(device, links);
40808b8a9b1dSJustin T. Gibbs 
40818b8a9b1dSJustin T. Gibbs 			if (path->device != device
40822f22d08dSJustin T. Gibbs 			 && path->device->lun_id != CAM_LUN_WILDCARD
40832f22d08dSJustin T. Gibbs 			 && device->lun_id != CAM_LUN_WILDCARD)
40848b8a9b1dSJustin T. Gibbs 				continue;
40859efda2c9SAlexander Motin 			/*
40869efda2c9SAlexander Motin 			 * The async callback could free the device.
40879efda2c9SAlexander Motin 			 * If it is a broadcast async, it doesn't hold
40889efda2c9SAlexander Motin 			 * device reference, so take our own reference.
40899efda2c9SAlexander Motin 			 */
40909efda2c9SAlexander Motin 			xpt_acquire_device(device);
409152c9ce25SScott Long 			(*(bus->xport->async))(async_code, bus,
409252c9ce25SScott Long 					       target, device,
409352c9ce25SScott Long 					       async_arg);
40948b8a9b1dSJustin T. Gibbs 
40952f22d08dSJustin T. Gibbs 			xpt_async_bcast(&device->asyncs, async_code,
40962f22d08dSJustin T. Gibbs 					path, async_arg);
40979efda2c9SAlexander Motin 			xpt_release_device(device);
40988b8a9b1dSJustin T. Gibbs 		}
40998b8a9b1dSJustin T. Gibbs 	}
4100c8bead2aSJustin T. Gibbs 
4101c8bead2aSJustin T. Gibbs 	/*
4102c8bead2aSJustin T. Gibbs 	 * If this wasn't a fully wildcarded async, tell all
4103c8bead2aSJustin T. Gibbs 	 * clients that want all async events.
4104c8bead2aSJustin T. Gibbs 	 */
4105c8bead2aSJustin T. Gibbs 	if (bus != xpt_periph->path->bus)
4106c8bead2aSJustin T. Gibbs 		xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
41078b8a9b1dSJustin T. Gibbs 				path, async_arg);
41088b8a9b1dSJustin T. Gibbs }
41098b8a9b1dSJustin T. Gibbs 
41108b8a9b1dSJustin T. Gibbs static void
41118b8a9b1dSJustin T. Gibbs xpt_async_bcast(struct async_list *async_head,
41128b8a9b1dSJustin T. Gibbs 		u_int32_t async_code,
41138b8a9b1dSJustin T. Gibbs 		struct cam_path *path, void *async_arg)
41148b8a9b1dSJustin T. Gibbs {
41158b8a9b1dSJustin T. Gibbs 	struct async_node *cur_entry;
41168b8a9b1dSJustin T. Gibbs 
41178b8a9b1dSJustin T. Gibbs 	cur_entry = SLIST_FIRST(async_head);
41188b8a9b1dSJustin T. Gibbs 	while (cur_entry != NULL) {
41198b8a9b1dSJustin T. Gibbs 		struct async_node *next_entry;
41208b8a9b1dSJustin T. Gibbs 		/*
41218b8a9b1dSJustin T. Gibbs 		 * Grab the next list entry before we call the current
41228b8a9b1dSJustin T. Gibbs 		 * entry's callback.  This is because the callback function
41238b8a9b1dSJustin T. Gibbs 		 * can delete its async callback entry.
41248b8a9b1dSJustin T. Gibbs 		 */
41258b8a9b1dSJustin T. Gibbs 		next_entry = SLIST_NEXT(cur_entry, links);
41268b8a9b1dSJustin T. Gibbs 		if ((cur_entry->event_enable & async_code) != 0)
41278b8a9b1dSJustin T. Gibbs 			cur_entry->callback(cur_entry->callback_arg,
41288b8a9b1dSJustin T. Gibbs 					    async_code, path,
41298b8a9b1dSJustin T. Gibbs 					    async_arg);
41308b8a9b1dSJustin T. Gibbs 		cur_entry = next_entry;
41318b8a9b1dSJustin T. Gibbs 	}
41328b8a9b1dSJustin T. Gibbs }
41338b8a9b1dSJustin T. Gibbs 
41342f22d08dSJustin T. Gibbs static void
413552c9ce25SScott Long xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
413652c9ce25SScott Long 		      struct cam_et *target, struct cam_ed *device,
413752c9ce25SScott Long 		      void *async_arg)
41382f22d08dSJustin T. Gibbs {
4139b882a6d3SMatt Jacob 	printf("%s called\n", __func__);
41402f22d08dSJustin T. Gibbs }
41412f22d08dSJustin T. Gibbs 
41428b8a9b1dSJustin T. Gibbs u_int32_t
4143cccf4220SAlexander Motin xpt_freeze_devq(struct cam_path *path, u_int count)
414483c5d981SAlexander Motin {
414583c5d981SAlexander Motin 	struct cam_ed *dev = path->device;
414683c5d981SAlexander Motin 
414783c5d981SAlexander Motin 	mtx_assert(path->bus->sim->mtx, MA_OWNED);
4148cccf4220SAlexander Motin 	dev->ccbq.queue.qfrozen_cnt += count;
414983c5d981SAlexander Motin 	/* Remove frozen device from sendq. */
4150cccf4220SAlexander Motin 	if (device_is_queued(dev)) {
415183c5d981SAlexander Motin 		camq_remove(&dev->sim->devq->send_queue,
4152cccf4220SAlexander Motin 		    dev->devq_entry.pinfo.index);
415383c5d981SAlexander Motin 	}
4154cccf4220SAlexander Motin 	return (dev->ccbq.queue.qfrozen_cnt);
41558b8a9b1dSJustin T. Gibbs }
41568b8a9b1dSJustin T. Gibbs 
41578b8a9b1dSJustin T. Gibbs u_int32_t
41588b8a9b1dSJustin T. Gibbs xpt_freeze_simq(struct cam_sim *sim, u_int count)
41598b8a9b1dSJustin T. Gibbs {
4160ec700f26SAlexander Motin 
41612b83592fSScott Long 	mtx_assert(sim->mtx, MA_OWNED);
4162cccf4220SAlexander Motin 	sim->devq->send_queue.qfrozen_cnt += count;
4163cccf4220SAlexander Motin 	return (sim->devq->send_queue.qfrozen_cnt);
41648b8a9b1dSJustin T. Gibbs }
41658b8a9b1dSJustin T. Gibbs 
41668b8a9b1dSJustin T. Gibbs static void
41678b8a9b1dSJustin T. Gibbs xpt_release_devq_timeout(void *arg)
41688b8a9b1dSJustin T. Gibbs {
41698b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
41708b8a9b1dSJustin T. Gibbs 
41718b8a9b1dSJustin T. Gibbs 	device = (struct cam_ed *)arg;
4172cccf4220SAlexander Motin 	xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
41738b8a9b1dSJustin T. Gibbs }
41748b8a9b1dSJustin T. Gibbs 
41758b8a9b1dSJustin T. Gibbs void
41762cefde5fSJustin T. Gibbs xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
41772cefde5fSJustin T. Gibbs {
417868153f43SScott Long 
4179cccf4220SAlexander Motin 	mtx_assert(path->bus->sim->mtx, MA_OWNED);
4180cccf4220SAlexander Motin 	xpt_release_devq_device(path->device, count, run_queue);
418183c5d981SAlexander Motin }
418283c5d981SAlexander Motin 
418383c5d981SAlexander Motin void
4184cccf4220SAlexander Motin xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
41858b8a9b1dSJustin T. Gibbs {
41868b8a9b1dSJustin T. Gibbs 
4187cccf4220SAlexander Motin 	if (count > dev->ccbq.queue.qfrozen_cnt) {
418883c5d981SAlexander Motin #ifdef INVARIANTS
4189cccf4220SAlexander Motin 		printf("xpt_release_devq(): requested %u > present %u\n",
4190cccf4220SAlexander Motin 		    count, dev->ccbq.queue.qfrozen_cnt);
419183c5d981SAlexander Motin #endif
4192cccf4220SAlexander Motin 		count = dev->ccbq.queue.qfrozen_cnt;
419383c5d981SAlexander Motin 	}
4194cccf4220SAlexander Motin 	dev->ccbq.queue.qfrozen_cnt -= count;
4195cccf4220SAlexander Motin 	if (dev->ccbq.queue.qfrozen_cnt == 0) {
41968b8a9b1dSJustin T. Gibbs 		/*
41978b8a9b1dSJustin T. Gibbs 		 * No longer need to wait for a successful
41988b8a9b1dSJustin T. Gibbs 		 * command completion.
41998b8a9b1dSJustin T. Gibbs 		 */
42008b8a9b1dSJustin T. Gibbs 		dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
42018b8a9b1dSJustin T. Gibbs 		/*
42028b8a9b1dSJustin T. Gibbs 		 * Remove any timeouts that might be scheduled
42038b8a9b1dSJustin T. Gibbs 		 * to release this queue.
42048b8a9b1dSJustin T. Gibbs 		 */
42058b8a9b1dSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
42062b83592fSScott Long 			callout_stop(&dev->callout);
42078b8a9b1dSJustin T. Gibbs 			dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
42088b8a9b1dSJustin T. Gibbs 		}
4209cccf4220SAlexander Motin 		xpt_run_dev_allocq(dev);
421083c5d981SAlexander Motin 		if (run_queue == 0)
421183c5d981SAlexander Motin 			return;
42128b8a9b1dSJustin T. Gibbs 		/*
42138b8a9b1dSJustin T. Gibbs 		 * Now that we are unfrozen schedule the
42148b8a9b1dSJustin T. Gibbs 		 * device so any pending transactions are
42158b8a9b1dSJustin T. Gibbs 		 * run.
42168b8a9b1dSJustin T. Gibbs 		 */
4217cccf4220SAlexander Motin 		if (xpt_schedule_devq(dev->sim->devq, dev))
4218cccf4220SAlexander Motin 			xpt_run_devq(dev->sim->devq);
42198b8a9b1dSJustin T. Gibbs 	}
422083c5d981SAlexander Motin }
42218b8a9b1dSJustin T. Gibbs 
42228b8a9b1dSJustin T. Gibbs void
42238b8a9b1dSJustin T. Gibbs xpt_release_simq(struct cam_sim *sim, int run_queue)
42248b8a9b1dSJustin T. Gibbs {
42258b8a9b1dSJustin T. Gibbs 	struct	camq *sendq;
42268b8a9b1dSJustin T. Gibbs 
42272b83592fSScott Long 	mtx_assert(sim->mtx, MA_OWNED);
42288b8a9b1dSJustin T. Gibbs 	sendq = &(sim->devq->send_queue);
4229cccf4220SAlexander Motin 	if (sendq->qfrozen_cnt <= 0) {
423083c5d981SAlexander Motin #ifdef INVARIANTS
423183c5d981SAlexander Motin 		printf("xpt_release_simq: requested 1 > present %u\n",
4232cccf4220SAlexander Motin 		    sendq->qfrozen_cnt);
423383c5d981SAlexander Motin #endif
423483c5d981SAlexander Motin 	} else
4235cccf4220SAlexander Motin 		sendq->qfrozen_cnt--;
4236cccf4220SAlexander Motin 	if (sendq->qfrozen_cnt == 0) {
42378b8a9b1dSJustin T. Gibbs 		/*
42388b8a9b1dSJustin T. Gibbs 		 * If there is a timeout scheduled to release this
42398b8a9b1dSJustin T. Gibbs 		 * sim queue, remove it.  The queue frozen count is
42408b8a9b1dSJustin T. Gibbs 		 * already at 0.
42418b8a9b1dSJustin T. Gibbs 		 */
42428b8a9b1dSJustin T. Gibbs 		if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
42432b83592fSScott Long 			callout_stop(&sim->callout);
42448b8a9b1dSJustin T. Gibbs 			sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
42458b8a9b1dSJustin T. Gibbs 		}
42468b8a9b1dSJustin T. Gibbs 		if (run_queue) {
42478b8a9b1dSJustin T. Gibbs 			/*
42488b8a9b1dSJustin T. Gibbs 			 * Now that we are unfrozen run the send queue.
42498b8a9b1dSJustin T. Gibbs 			 */
4250cccf4220SAlexander Motin 			xpt_run_devq(sim->devq);
425177dc25ccSScott Long 		}
425277dc25ccSScott Long 	}
42538b8a9b1dSJustin T. Gibbs }
42548b8a9b1dSJustin T. Gibbs 
42552b83592fSScott Long /*
42562b83592fSScott Long  * XXX Appears to be unused.
42572b83592fSScott Long  */
42588b8a9b1dSJustin T. Gibbs static void
42598b8a9b1dSJustin T. Gibbs xpt_release_simq_timeout(void *arg)
42608b8a9b1dSJustin T. Gibbs {
42618b8a9b1dSJustin T. Gibbs 	struct cam_sim *sim;
42628b8a9b1dSJustin T. Gibbs 
42638b8a9b1dSJustin T. Gibbs 	sim = (struct cam_sim *)arg;
42648b8a9b1dSJustin T. Gibbs 	xpt_release_simq(sim, /* run_queue */ TRUE);
42658b8a9b1dSJustin T. Gibbs }
42668b8a9b1dSJustin T. Gibbs 
42678b8a9b1dSJustin T. Gibbs void
4268a5479bc5SJustin T. Gibbs xpt_done(union ccb *done_ccb)
42698b8a9b1dSJustin T. Gibbs {
42709758cc83SScott Long 	struct cam_sim *sim;
4271efa575b6SAlexander Motin 	int	first;
42728b8a9b1dSJustin T. Gibbs 
42738b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
42749deea857SKenneth D. Merry 	if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
42758b8a9b1dSJustin T. Gibbs 		/*
42768b8a9b1dSJustin T. Gibbs 		 * Queue up the request for handling by our SWI handler
42778b8a9b1dSJustin T. Gibbs 		 * any of the "non-immediate" type of ccbs.
42788b8a9b1dSJustin T. Gibbs 		 */
42799758cc83SScott Long 		sim = done_ccb->ccb_h.path->bus->sim;
42809758cc83SScott Long 		TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h,
42818b8a9b1dSJustin T. Gibbs 		    sim_links.tqe);
42828b8a9b1dSJustin T. Gibbs 		done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4283711f6613SAlexander Motin 		if ((sim->flags & (CAM_SIM_ON_DONEQ | CAM_SIM_POLLED |
4284711f6613SAlexander Motin 		    CAM_SIM_BATCH)) == 0) {
42859758cc83SScott Long 			mtx_lock(&cam_simq_lock);
4286efa575b6SAlexander Motin 			first = TAILQ_EMPTY(&cam_simq);
4287efa575b6SAlexander Motin 			TAILQ_INSERT_TAIL(&cam_simq, sim, links);
42889758cc83SScott Long 			mtx_unlock(&cam_simq_lock);
4289788fb376SAlexander Motin 			sim->flags |= CAM_SIM_ON_DONEQ;
4290efa575b6SAlexander Motin 			if (first)
4291c86b6ff5SJohn Baldwin 				swi_sched(cambio_ih, 0);
4292788fb376SAlexander Motin 		}
42938b8a9b1dSJustin T. Gibbs 	}
42948b8a9b1dSJustin T. Gibbs }
42958b8a9b1dSJustin T. Gibbs 
4296711f6613SAlexander Motin void
4297711f6613SAlexander Motin xpt_batch_start(struct cam_sim *sim)
4298711f6613SAlexander Motin {
4299711f6613SAlexander Motin 
4300711f6613SAlexander Motin 	KASSERT((sim->flags & CAM_SIM_BATCH) == 0, ("Batch flag already set"));
4301711f6613SAlexander Motin 	sim->flags |= CAM_SIM_BATCH;
4302711f6613SAlexander Motin }
4303711f6613SAlexander Motin 
4304711f6613SAlexander Motin void
4305711f6613SAlexander Motin xpt_batch_done(struct cam_sim *sim)
4306711f6613SAlexander Motin {
4307711f6613SAlexander Motin 
4308711f6613SAlexander Motin 	KASSERT((sim->flags & CAM_SIM_BATCH) != 0, ("Batch flag was not set"));
4309711f6613SAlexander Motin 	sim->flags &= ~CAM_SIM_BATCH;
4310711f6613SAlexander Motin 	if (!TAILQ_EMPTY(&sim->sim_doneq) &&
4311711f6613SAlexander Motin 	    (sim->flags & CAM_SIM_ON_DONEQ) == 0)
4312711f6613SAlexander Motin 		camisr_runqueue(&sim->sim_doneq);
4313711f6613SAlexander Motin }
4314711f6613SAlexander Motin 
43158b8a9b1dSJustin T. Gibbs union ccb *
43168008a935SScott Long xpt_alloc_ccb()
43178b8a9b1dSJustin T. Gibbs {
43188b8a9b1dSJustin T. Gibbs 	union ccb *new_ccb;
43198b8a9b1dSJustin T. Gibbs 
4320596ee08fSAlexander Motin 	new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4321362abc44STai-hwa Liang 	return (new_ccb);
4322362abc44STai-hwa Liang }
4323362abc44STai-hwa Liang 
4324362abc44STai-hwa Liang union ccb *
43258008a935SScott Long xpt_alloc_ccb_nowait()
4326362abc44STai-hwa Liang {
4327362abc44STai-hwa Liang 	union ccb *new_ccb;
4328362abc44STai-hwa Liang 
4329596ee08fSAlexander Motin 	new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
43308b8a9b1dSJustin T. Gibbs 	return (new_ccb);
43318b8a9b1dSJustin T. Gibbs }
43328b8a9b1dSJustin T. Gibbs 
43338b8a9b1dSJustin T. Gibbs void
43348b8a9b1dSJustin T. Gibbs xpt_free_ccb(union ccb *free_ccb)
43358b8a9b1dSJustin T. Gibbs {
4336596ee08fSAlexander Motin 	free(free_ccb, M_CAMCCB);
43378b8a9b1dSJustin T. Gibbs }
43388b8a9b1dSJustin T. Gibbs 
43398b8a9b1dSJustin T. Gibbs 
43408b8a9b1dSJustin T. Gibbs 
43418b8a9b1dSJustin T. Gibbs /* Private XPT functions */
43428b8a9b1dSJustin T. Gibbs 
43438b8a9b1dSJustin T. Gibbs /*
43448b8a9b1dSJustin T. Gibbs  * Get a CAM control block for the caller. Charge the structure to the device
43458b8a9b1dSJustin T. Gibbs  * referenced by the path.  If the this device has no 'credits' then the
43468b8a9b1dSJustin T. Gibbs  * device already has the maximum number of outstanding operations under way
43478b8a9b1dSJustin T. Gibbs  * and we return NULL. If we don't have sufficient resources to allocate more
43488b8a9b1dSJustin T. Gibbs  * ccbs, we also return NULL.
43498b8a9b1dSJustin T. Gibbs  */
43508b8a9b1dSJustin T. Gibbs static union ccb *
43518b8a9b1dSJustin T. Gibbs xpt_get_ccb(struct cam_ed *device)
43528b8a9b1dSJustin T. Gibbs {
43538b8a9b1dSJustin T. Gibbs 	union ccb *new_ccb;
43542b83592fSScott Long 	struct cam_sim *sim;
43558b8a9b1dSJustin T. Gibbs 
43562b83592fSScott Long 	sim = device->sim;
43572b83592fSScott Long 	if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) {
43588008a935SScott Long 		new_ccb = xpt_alloc_ccb_nowait();
43598b8a9b1dSJustin T. Gibbs                 if (new_ccb == NULL) {
43608b8a9b1dSJustin T. Gibbs 			return (NULL);
43618b8a9b1dSJustin T. Gibbs 		}
43628008a935SScott Long 		if ((sim->flags & CAM_SIM_MPSAFE) == 0)
43638008a935SScott Long 			callout_handle_init(&new_ccb->ccb_h.timeout_ch);
43642b83592fSScott Long 		SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h,
43658b8a9b1dSJustin T. Gibbs 				  xpt_links.sle);
43662b83592fSScott Long 		sim->ccb_count++;
43678b8a9b1dSJustin T. Gibbs 	}
43688b8a9b1dSJustin T. Gibbs 	cam_ccbq_take_opening(&device->ccbq);
43692b83592fSScott Long 	SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
43708b8a9b1dSJustin T. Gibbs 	return (new_ccb);
43718b8a9b1dSJustin T. Gibbs }
43728b8a9b1dSJustin T. Gibbs 
4373a5479bc5SJustin T. Gibbs static void
4374a5479bc5SJustin T. Gibbs xpt_release_bus(struct cam_eb *bus)
4375a5479bc5SJustin T. Gibbs {
4376a5479bc5SJustin T. Gibbs 
43779a7c2696SAlexander Motin 	xpt_lock_buses();
437815975b7bSMatt Jacob 	KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4379dcdf6e74SAlexander Motin 	if (--bus->refcount > 0) {
4380dcdf6e74SAlexander Motin 		xpt_unlock_buses();
4381dcdf6e74SAlexander Motin 		return;
4382dcdf6e74SAlexander Motin 	}
4383dcdf6e74SAlexander Motin 	KASSERT(TAILQ_EMPTY(&bus->et_entries),
4384dcdf6e74SAlexander Motin 	    ("refcount is zero, but target list is not empty"));
43852b83592fSScott Long 	TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
43862b83592fSScott Long 	xsoftc.bus_generation++;
43879a7c2696SAlexander Motin 	xpt_unlock_buses();
4388fa6099fdSEdward Tomasz Napierala 	cam_sim_release(bus->sim);
4389362abc44STai-hwa Liang 	free(bus, M_CAMXPT);
4390a5479bc5SJustin T. Gibbs }
43918b8a9b1dSJustin T. Gibbs 
43928b8a9b1dSJustin T. Gibbs static struct cam_et *
43938b8a9b1dSJustin T. Gibbs xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
43948b8a9b1dSJustin T. Gibbs {
4395dcdf6e74SAlexander Motin 	struct cam_et *cur_target, *target;
43968b8a9b1dSJustin T. Gibbs 
4397dcdf6e74SAlexander Motin 	mtx_assert(bus->sim->mtx, MA_OWNED);
43983501942bSJustin T. Gibbs 	target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
43993501942bSJustin T. Gibbs 					 M_NOWAIT|M_ZERO);
4400dcdf6e74SAlexander Motin 	if (target == NULL)
4401dcdf6e74SAlexander Motin 		return (NULL);
44028b8a9b1dSJustin T. Gibbs 
4403434bbf6eSJustin T. Gibbs 	TAILQ_INIT(&target->ed_entries);
44048b8a9b1dSJustin T. Gibbs 	target->bus = bus;
44058b8a9b1dSJustin T. Gibbs 	target->target_id = target_id;
44068b8a9b1dSJustin T. Gibbs 	target->refcount = 1;
4407434bbf6eSJustin T. Gibbs 	target->generation = 0;
440882b361b1SMatt Jacob 	target->luns = NULL;
4409434bbf6eSJustin T. Gibbs 	timevalclear(&target->last_reset);
4410a5479bc5SJustin T. Gibbs 	/*
4411a5479bc5SJustin T. Gibbs 	 * Hold a reference to our parent bus so it
4412a5479bc5SJustin T. Gibbs 	 * will not go away before we do.
4413a5479bc5SJustin T. Gibbs 	 */
44149a7c2696SAlexander Motin 	xpt_lock_buses();
4415a5479bc5SJustin T. Gibbs 	bus->refcount++;
44169a7c2696SAlexander Motin 	xpt_unlock_buses();
44178b8a9b1dSJustin T. Gibbs 
44188b8a9b1dSJustin T. Gibbs 	/* Insertion sort into our bus's target list */
44198b8a9b1dSJustin T. Gibbs 	cur_target = TAILQ_FIRST(&bus->et_entries);
44208b8a9b1dSJustin T. Gibbs 	while (cur_target != NULL && cur_target->target_id < target_id)
44218b8a9b1dSJustin T. Gibbs 		cur_target = TAILQ_NEXT(cur_target, links);
44228b8a9b1dSJustin T. Gibbs 	if (cur_target != NULL) {
44238b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_BEFORE(cur_target, target, links);
44248b8a9b1dSJustin T. Gibbs 	} else {
44258b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
44268b8a9b1dSJustin T. Gibbs 	}
4427a5479bc5SJustin T. Gibbs 	bus->generation++;
44288b8a9b1dSJustin T. Gibbs 	return (target);
44298b8a9b1dSJustin T. Gibbs }
44308b8a9b1dSJustin T. Gibbs 
4431a5479bc5SJustin T. Gibbs static void
4432f98d7a47SAlexander Motin xpt_release_target(struct cam_et *target)
44338b8a9b1dSJustin T. Gibbs {
4434a5479bc5SJustin T. Gibbs 
4435dcdf6e74SAlexander Motin 	mtx_assert(target->bus->sim->mtx, MA_OWNED);
4436dcdf6e74SAlexander Motin 	if (--target->refcount > 0)
4437dcdf6e74SAlexander Motin 		return;
4438dcdf6e74SAlexander Motin 	KASSERT(TAILQ_EMPTY(&target->ed_entries),
4439dcdf6e74SAlexander Motin 	    ("refcount is zero, but device list is not empty"));
4440f98d7a47SAlexander Motin 	TAILQ_REMOVE(&target->bus->et_entries, target, links);
4441f98d7a47SAlexander Motin 	target->bus->generation++;
4442f98d7a47SAlexander Motin 	xpt_release_bus(target->bus);
444382b361b1SMatt Jacob 	if (target->luns)
444482b361b1SMatt Jacob 		free(target->luns, M_CAMXPT);
4445362abc44STai-hwa Liang 	free(target, M_CAMXPT);
444677dc25ccSScott Long }
44478b8a9b1dSJustin T. Gibbs 
44488b8a9b1dSJustin T. Gibbs static struct cam_ed *
444952c9ce25SScott Long xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
445052c9ce25SScott Long 			 lun_id_t lun_id)
445152c9ce25SScott Long {
4452dcdf6e74SAlexander Motin 	struct cam_ed *device;
445352c9ce25SScott Long 
445452c9ce25SScott Long 	device = xpt_alloc_device(bus, target, lun_id);
445552c9ce25SScott Long 	if (device == NULL)
445652c9ce25SScott Long 		return (NULL);
445752c9ce25SScott Long 
445852c9ce25SScott Long 	device->mintags = 1;
445952c9ce25SScott Long 	device->maxtags = 1;
446083c5d981SAlexander Motin 	bus->sim->max_ccbs += device->ccbq.devq_openings;
446152c9ce25SScott Long 	return (device);
446252c9ce25SScott Long }
446352c9ce25SScott Long 
446452c9ce25SScott Long struct cam_ed *
44658b8a9b1dSJustin T. Gibbs xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
44668b8a9b1dSJustin T. Gibbs {
4467dcdf6e74SAlexander Motin 	struct cam_ed	*cur_device, *device;
44688b8a9b1dSJustin T. Gibbs 	struct cam_devq	*devq;
4469a5479bc5SJustin T. Gibbs 	cam_status status;
44708b8a9b1dSJustin T. Gibbs 
4471dcdf6e74SAlexander Motin 	mtx_assert(target->bus->sim->mtx, MA_OWNED);
44728b8a9b1dSJustin T. Gibbs 	/* Make space for us in the device queue on our bus */
44738b8a9b1dSJustin T. Gibbs 	devq = bus->sim->devq;
4474cccf4220SAlexander Motin 	status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4475dcdf6e74SAlexander Motin 	if (status != CAM_REQ_CMP)
4476dcdf6e74SAlexander Motin 		return (NULL);
44778b8a9b1dSJustin T. Gibbs 
44788b8a9b1dSJustin T. Gibbs 	device = (struct cam_ed *)malloc(sizeof(*device),
4479596ee08fSAlexander Motin 					 M_CAMDEV, M_NOWAIT|M_ZERO);
4480dcdf6e74SAlexander Motin 	if (device == NULL)
4481dcdf6e74SAlexander Motin 		return (NULL);
44828b8a9b1dSJustin T. Gibbs 
4483cccf4220SAlexander Motin 	cam_init_pinfo(&device->devq_entry.pinfo);
4484cccf4220SAlexander Motin 	device->devq_entry.device = device;
44858b8a9b1dSJustin T. Gibbs 	device->target = target;
44868b8a9b1dSJustin T. Gibbs 	device->lun_id = lun_id;
44872b83592fSScott Long 	device->sim = bus->sim;
44888b8a9b1dSJustin T. Gibbs 	/* Initialize our queues */
44898b8a9b1dSJustin T. Gibbs 	if (camq_init(&device->drvq, 0) != 0) {
4490596ee08fSAlexander Motin 		free(device, M_CAMDEV);
44918b8a9b1dSJustin T. Gibbs 		return (NULL);
44928b8a9b1dSJustin T. Gibbs 	}
44938b8a9b1dSJustin T. Gibbs 	if (cam_ccbq_init(&device->ccbq,
44948b8a9b1dSJustin T. Gibbs 			  bus->sim->max_dev_openings) != 0) {
44958b8a9b1dSJustin T. Gibbs 		camq_fini(&device->drvq);
4496596ee08fSAlexander Motin 		free(device, M_CAMDEV);
44978b8a9b1dSJustin T. Gibbs 		return (NULL);
44988b8a9b1dSJustin T. Gibbs 	}
4499434bbf6eSJustin T. Gibbs 	SLIST_INIT(&device->asyncs);
4500434bbf6eSJustin T. Gibbs 	SLIST_INIT(&device->periphs);
4501434bbf6eSJustin T. Gibbs 	device->generation = 0;
4502434bbf6eSJustin T. Gibbs 	device->flags = CAM_DEV_UNCONFIGURED;
4503434bbf6eSJustin T. Gibbs 	device->tag_delay_count = 0;
4504df8f9080SJustin T. Gibbs 	device->tag_saved_openings = 0;
4505434bbf6eSJustin T. Gibbs 	device->refcount = 1;
45062b83592fSScott Long 	callout_init_mtx(&device->callout, bus->sim->mtx, 0);
4507434bbf6eSJustin T. Gibbs 
4508dcdf6e74SAlexander Motin 	cur_device = TAILQ_FIRST(&target->ed_entries);
4509dcdf6e74SAlexander Motin 	while (cur_device != NULL && cur_device->lun_id < lun_id)
4510dcdf6e74SAlexander Motin 		cur_device = TAILQ_NEXT(cur_device, links);
4511dcdf6e74SAlexander Motin 	if (cur_device != NULL)
4512dcdf6e74SAlexander Motin 		TAILQ_INSERT_BEFORE(cur_device, device, links);
4513dcdf6e74SAlexander Motin 	else
4514dcdf6e74SAlexander Motin 		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4515434bbf6eSJustin T. Gibbs 	target->refcount++;
4516dcdf6e74SAlexander Motin 	target->generation++;
45178b8a9b1dSJustin T. Gibbs 	return (device);
45188b8a9b1dSJustin T. Gibbs }
45198b8a9b1dSJustin T. Gibbs 
4520f98d7a47SAlexander Motin void
4521f98d7a47SAlexander Motin xpt_acquire_device(struct cam_ed *device)
45228b8a9b1dSJustin T. Gibbs {
45238b8a9b1dSJustin T. Gibbs 
4524dcdf6e74SAlexander Motin 	mtx_assert(device->sim->mtx, MA_OWNED);
4525f98d7a47SAlexander Motin 	device->refcount++;
4526f98d7a47SAlexander Motin }
4527f98d7a47SAlexander Motin 
4528f98d7a47SAlexander Motin void
4529f98d7a47SAlexander Motin xpt_release_device(struct cam_ed *device)
4530f98d7a47SAlexander Motin {
45318b8a9b1dSJustin T. Gibbs 	struct cam_devq *devq;
45328b8a9b1dSJustin T. Gibbs 
4533dcdf6e74SAlexander Motin 	mtx_assert(device->sim->mtx, MA_OWNED);
4534dcdf6e74SAlexander Motin 	if (--device->refcount > 0)
4535dcdf6e74SAlexander Motin 		return;
4536dcdf6e74SAlexander Motin 
4537dcdf6e74SAlexander Motin 	KASSERT(SLIST_EMPTY(&device->periphs),
4538dcdf6e74SAlexander Motin 	    ("refcount is zero, but periphs list is not empty"));
4539cccf4220SAlexander Motin 	if (device->devq_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4540a5479bc5SJustin T. Gibbs 		panic("Removing device while still queued for ccbs");
45412cefde5fSJustin T. Gibbs 
45422cefde5fSJustin T. Gibbs 	if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
45432b83592fSScott Long 		callout_stop(&device->callout);
45442cefde5fSJustin T. Gibbs 
4545f98d7a47SAlexander Motin 	TAILQ_REMOVE(&device->target->ed_entries, device,links);
4546f98d7a47SAlexander Motin 	device->target->generation++;
4547f98d7a47SAlexander Motin 	device->target->bus->sim->max_ccbs -= device->ccbq.devq_openings;
45488b8a9b1dSJustin T. Gibbs 	/* Release our slot in the devq */
4549f98d7a47SAlexander Motin 	devq = device->target->bus->sim->devq;
4550cccf4220SAlexander Motin 	cam_devq_resize(devq, devq->send_queue.array_size - 1);
45517c20d5d7STai-hwa Liang 	camq_fini(&device->drvq);
455220a7933fSAlexander Motin 	cam_ccbq_fini(&device->ccbq);
4553e6bd5983SKenneth D. Merry 	/*
4554e6bd5983SKenneth D. Merry 	 * Free allocated memory.  free(9) does nothing if the
4555e6bd5983SKenneth D. Merry 	 * supplied pointer is NULL, so it is safe to call without
4556e6bd5983SKenneth D. Merry 	 * checking.
4557e6bd5983SKenneth D. Merry 	 */
4558e6bd5983SKenneth D. Merry 	free(device->supported_vpds, M_CAMXPT);
4559e6bd5983SKenneth D. Merry 	free(device->device_id, M_CAMXPT);
4560e6bd5983SKenneth D. Merry 	free(device->physpath, M_CAMXPT);
4561e6bd5983SKenneth D. Merry 	free(device->rcap_buf, M_CAMXPT);
4562e6bd5983SKenneth D. Merry 	free(device->serial_num, M_CAMXPT);
4563e6bd5983SKenneth D. Merry 
4564f98d7a47SAlexander Motin 	xpt_release_target(device->target);
4565596ee08fSAlexander Motin 	free(device, M_CAMDEV);
45668b8a9b1dSJustin T. Gibbs }
45678b8a9b1dSJustin T. Gibbs 
456852c9ce25SScott Long u_int32_t
45698b8a9b1dSJustin T. Gibbs xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
45708b8a9b1dSJustin T. Gibbs {
45718b8a9b1dSJustin T. Gibbs 	int	diff;
45728b8a9b1dSJustin T. Gibbs 	int	result;
45738b8a9b1dSJustin T. Gibbs 	struct	cam_ed *dev;
45748b8a9b1dSJustin T. Gibbs 
45758b8a9b1dSJustin T. Gibbs 	dev = path->device;
45768b8a9b1dSJustin T. Gibbs 
45778b8a9b1dSJustin T. Gibbs 	diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
45788b8a9b1dSJustin T. Gibbs 	result = cam_ccbq_resize(&dev->ccbq, newopenings);
45798b8a9b1dSJustin T. Gibbs 	if (result == CAM_REQ_CMP && (diff < 0)) {
45808b8a9b1dSJustin T. Gibbs 		dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
45818b8a9b1dSJustin T. Gibbs 	}
4582df8f9080SJustin T. Gibbs 	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4583df8f9080SJustin T. Gibbs 	 || (dev->inq_flags & SID_CmdQue) != 0)
4584df8f9080SJustin T. Gibbs 		dev->tag_saved_openings = newopenings;
45858b8a9b1dSJustin T. Gibbs 	/* Adjust the global limit */
45862b83592fSScott Long 	dev->sim->max_ccbs += diff;
45878b8a9b1dSJustin T. Gibbs 	return (result);
45888b8a9b1dSJustin T. Gibbs }
45898b8a9b1dSJustin T. Gibbs 
45908b8a9b1dSJustin T. Gibbs static struct cam_eb *
45918b8a9b1dSJustin T. Gibbs xpt_find_bus(path_id_t path_id)
45928b8a9b1dSJustin T. Gibbs {
45938b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus;
45948b8a9b1dSJustin T. Gibbs 
45959a7c2696SAlexander Motin 	xpt_lock_buses();
45962b83592fSScott Long 	for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
45978b8a9b1dSJustin T. Gibbs 	     bus != NULL;
45988b8a9b1dSJustin T. Gibbs 	     bus = TAILQ_NEXT(bus, links)) {
4599a5479bc5SJustin T. Gibbs 		if (bus->path_id == path_id) {
4600a5479bc5SJustin T. Gibbs 			bus->refcount++;
46018b8a9b1dSJustin T. Gibbs 			break;
46028b8a9b1dSJustin T. Gibbs 		}
4603a5479bc5SJustin T. Gibbs 	}
46049a7c2696SAlexander Motin 	xpt_unlock_buses();
46058b8a9b1dSJustin T. Gibbs 	return (bus);
46068b8a9b1dSJustin T. Gibbs }
46078b8a9b1dSJustin T. Gibbs 
46088b8a9b1dSJustin T. Gibbs static struct cam_et *
46098b8a9b1dSJustin T. Gibbs xpt_find_target(struct cam_eb *bus, target_id_t	target_id)
46108b8a9b1dSJustin T. Gibbs {
46118b8a9b1dSJustin T. Gibbs 	struct cam_et *target;
46128b8a9b1dSJustin T. Gibbs 
4613dcdf6e74SAlexander Motin 	mtx_assert(bus->sim->mtx, MA_OWNED);
46148b8a9b1dSJustin T. Gibbs 	for (target = TAILQ_FIRST(&bus->et_entries);
46158b8a9b1dSJustin T. Gibbs 	     target != NULL;
46168b8a9b1dSJustin T. Gibbs 	     target = TAILQ_NEXT(target, links)) {
46178b8a9b1dSJustin T. Gibbs 		if (target->target_id == target_id) {
46188b8a9b1dSJustin T. Gibbs 			target->refcount++;
46198b8a9b1dSJustin T. Gibbs 			break;
46208b8a9b1dSJustin T. Gibbs 		}
46218b8a9b1dSJustin T. Gibbs 	}
46228b8a9b1dSJustin T. Gibbs 	return (target);
46238b8a9b1dSJustin T. Gibbs }
46248b8a9b1dSJustin T. Gibbs 
46258b8a9b1dSJustin T. Gibbs static struct cam_ed *
46268b8a9b1dSJustin T. Gibbs xpt_find_device(struct cam_et *target, lun_id_t lun_id)
46278b8a9b1dSJustin T. Gibbs {
46288b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
46298b8a9b1dSJustin T. Gibbs 
4630dcdf6e74SAlexander Motin 	mtx_assert(target->bus->sim->mtx, MA_OWNED);
46318b8a9b1dSJustin T. Gibbs 	for (device = TAILQ_FIRST(&target->ed_entries);
46328b8a9b1dSJustin T. Gibbs 	     device != NULL;
46338b8a9b1dSJustin T. Gibbs 	     device = TAILQ_NEXT(device, links)) {
46348b8a9b1dSJustin T. Gibbs 		if (device->lun_id == lun_id) {
46358b8a9b1dSJustin T. Gibbs 			device->refcount++;
46368b8a9b1dSJustin T. Gibbs 			break;
46378b8a9b1dSJustin T. Gibbs 		}
46388b8a9b1dSJustin T. Gibbs 	}
46398b8a9b1dSJustin T. Gibbs 	return (device);
46408b8a9b1dSJustin T. Gibbs }
46418b8a9b1dSJustin T. Gibbs 
464230a4094fSAlexander Motin void
4643f0adc790SJustin T. Gibbs xpt_start_tags(struct cam_path *path)
4644f0adc790SJustin T. Gibbs {
4645fd21cc5eSJustin T. Gibbs 	struct ccb_relsim crs;
4646fd21cc5eSJustin T. Gibbs 	struct cam_ed *device;
4647fd21cc5eSJustin T. Gibbs 	struct cam_sim *sim;
4648fd21cc5eSJustin T. Gibbs 	int    newopenings;
4649fd21cc5eSJustin T. Gibbs 
4650fd21cc5eSJustin T. Gibbs 	device = path->device;
4651fd21cc5eSJustin T. Gibbs 	sim = path->bus->sim;
4652fd21cc5eSJustin T. Gibbs 	device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4653fd21cc5eSJustin T. Gibbs 	xpt_freeze_devq(path, /*count*/1);
4654fd21cc5eSJustin T. Gibbs 	device->inq_flags |= SID_CmdQue;
4655df8f9080SJustin T. Gibbs 	if (device->tag_saved_openings != 0)
4656df8f9080SJustin T. Gibbs 		newopenings = device->tag_saved_openings;
4657df8f9080SJustin T. Gibbs 	else
465852c9ce25SScott Long 		newopenings = min(device->maxtags,
4659df8f9080SJustin T. Gibbs 				  sim->max_tagged_dev_openings);
4660f0adc790SJustin T. Gibbs 	xpt_dev_ccbq_resize(path, newopenings);
4661581b2e78SAlexander Motin 	xpt_async(AC_GETDEV_CHANGED, path, NULL);
4662bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4663fd21cc5eSJustin T. Gibbs 	crs.ccb_h.func_code = XPT_REL_SIMQ;
4664fd21cc5eSJustin T. Gibbs 	crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4665fd21cc5eSJustin T. Gibbs 	crs.openings
4666fd21cc5eSJustin T. Gibbs 	    = crs.release_timeout
4667fd21cc5eSJustin T. Gibbs 	    = crs.qfrozen_cnt
4668fd21cc5eSJustin T. Gibbs 	    = 0;
4669fd21cc5eSJustin T. Gibbs 	xpt_action((union ccb *)&crs);
4670fd21cc5eSJustin T. Gibbs }
4671fd21cc5eSJustin T. Gibbs 
467230a4094fSAlexander Motin void
467330a4094fSAlexander Motin xpt_stop_tags(struct cam_path *path)
467430a4094fSAlexander Motin {
467530a4094fSAlexander Motin 	struct ccb_relsim crs;
467630a4094fSAlexander Motin 	struct cam_ed *device;
467730a4094fSAlexander Motin 	struct cam_sim *sim;
467830a4094fSAlexander Motin 
467930a4094fSAlexander Motin 	device = path->device;
468030a4094fSAlexander Motin 	sim = path->bus->sim;
468130a4094fSAlexander Motin 	device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
468230a4094fSAlexander Motin 	device->tag_delay_count = 0;
468330a4094fSAlexander Motin 	xpt_freeze_devq(path, /*count*/1);
468430a4094fSAlexander Motin 	device->inq_flags &= ~SID_CmdQue;
468530a4094fSAlexander Motin 	xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4686581b2e78SAlexander Motin 	xpt_async(AC_GETDEV_CHANGED, path, NULL);
468730a4094fSAlexander Motin 	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
468830a4094fSAlexander Motin 	crs.ccb_h.func_code = XPT_REL_SIMQ;
468930a4094fSAlexander Motin 	crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
469030a4094fSAlexander Motin 	crs.openings
469130a4094fSAlexander Motin 	    = crs.release_timeout
469230a4094fSAlexander Motin 	    = crs.qfrozen_cnt
469330a4094fSAlexander Motin 	    = 0;
469430a4094fSAlexander Motin 	xpt_action((union ccb *)&crs);
469530a4094fSAlexander Motin }
469630a4094fSAlexander Motin 
469783c5d981SAlexander Motin static void
469883c5d981SAlexander Motin xpt_boot_delay(void *arg)
46998b8a9b1dSJustin T. Gibbs {
47002b83592fSScott Long 
470183c5d981SAlexander Motin 	xpt_release_boot();
47028b8a9b1dSJustin T. Gibbs }
47038b8a9b1dSJustin T. Gibbs 
47048b8a9b1dSJustin T. Gibbs static void
47058b8a9b1dSJustin T. Gibbs xpt_config(void *arg)
47068b8a9b1dSJustin T. Gibbs {
47073393f8daSKenneth D. Merry 	/*
47083393f8daSKenneth D. Merry 	 * Now that interrupts are enabled, go find our devices
47093393f8daSKenneth D. Merry 	 */
47108b8a9b1dSJustin T. Gibbs 
4711f0f25b9cSAlexander Motin 	/* Setup debugging path */
47128b8a9b1dSJustin T. Gibbs 	if (cam_dflags != CAM_DEBUG_NONE) {
4713e5dfa058SAlexander Motin 		if (xpt_create_path_unlocked(&cam_dpath, NULL,
47148b8a9b1dSJustin T. Gibbs 				    CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
47158b8a9b1dSJustin T. Gibbs 				    CAM_DEBUG_LUN) != CAM_REQ_CMP) {
47168b8a9b1dSJustin T. Gibbs 			printf("xpt_config: xpt_create_path() failed for debug"
47178b8a9b1dSJustin T. Gibbs 			       " target %d:%d:%d, debugging disabled\n",
47188b8a9b1dSJustin T. Gibbs 			       CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
47198b8a9b1dSJustin T. Gibbs 			cam_dflags = CAM_DEBUG_NONE;
47208b8a9b1dSJustin T. Gibbs 		}
47218b8a9b1dSJustin T. Gibbs 	} else
47228b8a9b1dSJustin T. Gibbs 		cam_dpath = NULL;
47238b8a9b1dSJustin T. Gibbs 
472483c5d981SAlexander Motin 	periphdriver_init(1);
472583c5d981SAlexander Motin 	xpt_hold_boot();
472683c5d981SAlexander Motin 	callout_init(&xsoftc.boot_callout, 1);
472783c5d981SAlexander Motin 	callout_reset(&xsoftc.boot_callout, hz * xsoftc.boot_delay / 1000,
472883c5d981SAlexander Motin 	    xpt_boot_delay, NULL);
472983c5d981SAlexander Motin 	/* Fire up rescan thread. */
473083c5d981SAlexander Motin 	if (kproc_create(xpt_scanner_thread, NULL, NULL, 0, 0, "xpt_thrd")) {
47316f15a274SAlexander Motin 		printf("xpt_config: failed to create rescan thread.\n");
47321e637ba6SAlexander Motin 	}
473383c5d981SAlexander Motin }
47348b8a9b1dSJustin T. Gibbs 
473583c5d981SAlexander Motin void
473683c5d981SAlexander Motin xpt_hold_boot(void)
473783c5d981SAlexander Motin {
473883c5d981SAlexander Motin 	xpt_lock_buses();
473983c5d981SAlexander Motin 	xsoftc.buses_to_config++;
474083c5d981SAlexander Motin 	xpt_unlock_buses();
474183c5d981SAlexander Motin }
474283c5d981SAlexander Motin 
474383c5d981SAlexander Motin void
474483c5d981SAlexander Motin xpt_release_boot(void)
474583c5d981SAlexander Motin {
474683c5d981SAlexander Motin 	xpt_lock_buses();
474783c5d981SAlexander Motin 	xsoftc.buses_to_config--;
474883c5d981SAlexander Motin 	if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
474983c5d981SAlexander Motin 		struct	xpt_task *task;
475083c5d981SAlexander Motin 
475183c5d981SAlexander Motin 		xsoftc.buses_config_done = 1;
475283c5d981SAlexander Motin 		xpt_unlock_buses();
4753ecdf1113SJustin T. Gibbs 		/* Call manually because we don't have any busses */
475483c5d981SAlexander Motin 		task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
475583c5d981SAlexander Motin 		if (task != NULL) {
475683c5d981SAlexander Motin 			TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
475783c5d981SAlexander Motin 			taskqueue_enqueue(taskqueue_thread, &task->task);
47582863f7b1SJustin T. Gibbs 		}
475983c5d981SAlexander Motin 	} else
476083c5d981SAlexander Motin 		xpt_unlock_buses();
47612863f7b1SJustin T. Gibbs }
47628b8a9b1dSJustin T. Gibbs 
47638b8a9b1dSJustin T. Gibbs /*
47648b8a9b1dSJustin T. Gibbs  * If the given device only has one peripheral attached to it, and if that
47658b8a9b1dSJustin T. Gibbs  * peripheral is the passthrough driver, announce it.  This insures that the
47668b8a9b1dSJustin T. Gibbs  * user sees some sort of announcement for every peripheral in their system.
47678b8a9b1dSJustin T. Gibbs  */
47688b8a9b1dSJustin T. Gibbs static int
47698b8a9b1dSJustin T. Gibbs xptpassannouncefunc(struct cam_ed *device, void *arg)
47708b8a9b1dSJustin T. Gibbs {
47718b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph;
47728b8a9b1dSJustin T. Gibbs 	int i;
47738b8a9b1dSJustin T. Gibbs 
47748b8a9b1dSJustin T. Gibbs 	for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
47758b8a9b1dSJustin T. Gibbs 	     periph = SLIST_NEXT(periph, periph_links), i++);
47768b8a9b1dSJustin T. Gibbs 
47778b8a9b1dSJustin T. Gibbs 	periph = SLIST_FIRST(&device->periphs);
47788b8a9b1dSJustin T. Gibbs 	if ((i == 1)
47798b8a9b1dSJustin T. Gibbs 	 && (strncmp(periph->periph_name, "pass", 4) == 0))
47808b8a9b1dSJustin T. Gibbs 		xpt_announce_periph(periph, NULL);
47818b8a9b1dSJustin T. Gibbs 
47828b8a9b1dSJustin T. Gibbs 	return(1);
47838b8a9b1dSJustin T. Gibbs }
47848b8a9b1dSJustin T. Gibbs 
47858b8a9b1dSJustin T. Gibbs static void
47862b83592fSScott Long xpt_finishconfig_task(void *context, int pending)
47878b8a9b1dSJustin T. Gibbs {
47888b8a9b1dSJustin T. Gibbs 
478983c5d981SAlexander Motin 	periphdriver_init(2);
47902b83592fSScott Long 	/*
47912b83592fSScott Long 	 * Check for devices with no "standard" peripheral driver
47922b83592fSScott Long 	 * attached.  For any devices like that, announce the
47932b83592fSScott Long 	 * passthrough driver so the user will see something.
47942b83592fSScott Long 	 */
47953089bb2eSAlexander Motin 	if (!bootverbose)
47962b83592fSScott Long 		xpt_for_all_devices(xptpassannouncefunc, NULL);
47972b83592fSScott Long 
47982b83592fSScott Long 	/* Release our hook so that the boot can continue. */
47992b83592fSScott Long 	config_intrhook_disestablish(xsoftc.xpt_config_hook);
48000dd50e9bSScott Long 	free(xsoftc.xpt_config_hook, M_CAMXPT);
48012b83592fSScott Long 	xsoftc.xpt_config_hook = NULL;
48022b83592fSScott Long 
48032b83592fSScott Long 	free(context, M_CAMXPT);
48042b83592fSScott Long }
48052b83592fSScott Long 
480685d92640SScott Long cam_status
480785d92640SScott Long xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
480885d92640SScott Long 		   struct cam_path *path)
480985d92640SScott Long {
481085d92640SScott Long 	struct ccb_setasync csa;
481185d92640SScott Long 	cam_status status;
481285d92640SScott Long 	int xptpath = 0;
481385d92640SScott Long 
481485d92640SScott Long 	if (path == NULL) {
481585d92640SScott Long 		mtx_lock(&xsoftc.xpt_lock);
481685d92640SScott Long 		status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
481785d92640SScott Long 					 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
481885d92640SScott Long 		if (status != CAM_REQ_CMP) {
481985d92640SScott Long 			mtx_unlock(&xsoftc.xpt_lock);
482085d92640SScott Long 			return (status);
482185d92640SScott Long 		}
482285d92640SScott Long 		xptpath = 1;
482385d92640SScott Long 	}
482485d92640SScott Long 
482583c5d981SAlexander Motin 	xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
482685d92640SScott Long 	csa.ccb_h.func_code = XPT_SASYNC_CB;
482785d92640SScott Long 	csa.event_enable = event;
482885d92640SScott Long 	csa.callback = cbfunc;
482985d92640SScott Long 	csa.callback_arg = cbarg;
483085d92640SScott Long 	xpt_action((union ccb *)&csa);
483185d92640SScott Long 	status = csa.ccb_h.status;
48323501942bSJustin T. Gibbs 
483385d92640SScott Long 	if (xptpath) {
483485d92640SScott Long 		xpt_free_path(path);
483585d92640SScott Long 		mtx_unlock(&xsoftc.xpt_lock);
48363501942bSJustin T. Gibbs 	}
48377685edecSAlexander Motin 
48387685edecSAlexander Motin 	if ((status == CAM_REQ_CMP) &&
48397685edecSAlexander Motin 	    (csa.event_enable & AC_FOUND_DEVICE)) {
48407685edecSAlexander Motin 		/*
48417685edecSAlexander Motin 		 * Get this peripheral up to date with all
48427685edecSAlexander Motin 		 * the currently existing devices.
48437685edecSAlexander Motin 		 */
48447685edecSAlexander Motin 		xpt_for_all_devices(xptsetasyncfunc, &csa);
48457685edecSAlexander Motin 	}
48467685edecSAlexander Motin 	if ((status == CAM_REQ_CMP) &&
48477685edecSAlexander Motin 	    (csa.event_enable & AC_PATH_REGISTERED)) {
48487685edecSAlexander Motin 		/*
48497685edecSAlexander Motin 		 * Get this peripheral up to date with all
48507685edecSAlexander Motin 		 * the currently existing busses.
48517685edecSAlexander Motin 		 */
48527685edecSAlexander Motin 		xpt_for_all_busses(xptsetasyncbusfunc, &csa);
48537685edecSAlexander Motin 	}
48543501942bSJustin T. Gibbs 
485585d92640SScott Long 	return (status);
485685d92640SScott Long }
485785d92640SScott Long 
48588b8a9b1dSJustin T. Gibbs static void
48598b8a9b1dSJustin T. Gibbs xptaction(struct cam_sim *sim, union ccb *work_ccb)
48608b8a9b1dSJustin T. Gibbs {
48618b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
48628b8a9b1dSJustin T. Gibbs 
48638b8a9b1dSJustin T. Gibbs 	switch (work_ccb->ccb_h.func_code) {
48648b8a9b1dSJustin T. Gibbs 	/* Common cases first */
48658b8a9b1dSJustin T. Gibbs 	case XPT_PATH_INQ:		/* Path routing inquiry */
48668b8a9b1dSJustin T. Gibbs 	{
48678b8a9b1dSJustin T. Gibbs 		struct ccb_pathinq *cpi;
48688b8a9b1dSJustin T. Gibbs 
48698b8a9b1dSJustin T. Gibbs 		cpi = &work_ccb->cpi;
48708b8a9b1dSJustin T. Gibbs 		cpi->version_num = 1; /* XXX??? */
48718b8a9b1dSJustin T. Gibbs 		cpi->hba_inquiry = 0;
48728b8a9b1dSJustin T. Gibbs 		cpi->target_sprt = 0;
48738b8a9b1dSJustin T. Gibbs 		cpi->hba_misc = 0;
48748b8a9b1dSJustin T. Gibbs 		cpi->hba_eng_cnt = 0;
48758b8a9b1dSJustin T. Gibbs 		cpi->max_target = 0;
48768b8a9b1dSJustin T. Gibbs 		cpi->max_lun = 0;
48778b8a9b1dSJustin T. Gibbs 		cpi->initiator_id = 0;
48788b8a9b1dSJustin T. Gibbs 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
48798b8a9b1dSJustin T. Gibbs 		strncpy(cpi->hba_vid, "", HBA_IDLEN);
48808b8a9b1dSJustin T. Gibbs 		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
48818b8a9b1dSJustin T. Gibbs 		cpi->unit_number = sim->unit_number;
48828b8a9b1dSJustin T. Gibbs 		cpi->bus_id = sim->bus_id;
48839deea857SKenneth D. Merry 		cpi->base_transfer_speed = 0;
48843393f8daSKenneth D. Merry 		cpi->protocol = PROTO_UNSPECIFIED;
48853393f8daSKenneth D. Merry 		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
48863393f8daSKenneth D. Merry 		cpi->transport = XPORT_UNSPECIFIED;
48873393f8daSKenneth D. Merry 		cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
48888b8a9b1dSJustin T. Gibbs 		cpi->ccb_h.status = CAM_REQ_CMP;
48898b8a9b1dSJustin T. Gibbs 		xpt_done(work_ccb);
48908b8a9b1dSJustin T. Gibbs 		break;
48918b8a9b1dSJustin T. Gibbs 	}
48928b8a9b1dSJustin T. Gibbs 	default:
48938b8a9b1dSJustin T. Gibbs 		work_ccb->ccb_h.status = CAM_REQ_INVALID;
48948b8a9b1dSJustin T. Gibbs 		xpt_done(work_ccb);
48958b8a9b1dSJustin T. Gibbs 		break;
48968b8a9b1dSJustin T. Gibbs 	}
48978b8a9b1dSJustin T. Gibbs }
48988b8a9b1dSJustin T. Gibbs 
48998b8a9b1dSJustin T. Gibbs /*
4900434bbf6eSJustin T. Gibbs  * The xpt as a "controller" has no interrupt sources, so polling
4901434bbf6eSJustin T. Gibbs  * is a no-op.
4902434bbf6eSJustin T. Gibbs  */
4903434bbf6eSJustin T. Gibbs static void
4904434bbf6eSJustin T. Gibbs xptpoll(struct cam_sim *sim)
4905434bbf6eSJustin T. Gibbs {
4906434bbf6eSJustin T. Gibbs }
4907434bbf6eSJustin T. Gibbs 
49082b83592fSScott Long void
49092b83592fSScott Long xpt_lock_buses(void)
49102b83592fSScott Long {
49112b83592fSScott Long 	mtx_lock(&xsoftc.xpt_topo_lock);
49122b83592fSScott Long }
49132b83592fSScott Long 
49142b83592fSScott Long void
49152b83592fSScott Long xpt_unlock_buses(void)
49162b83592fSScott Long {
49172b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_topo_lock);
49182b83592fSScott Long }
49192b83592fSScott Long 
49205d754af7SMatt Jacob static void
49219758cc83SScott Long camisr(void *dummy)
49228b8a9b1dSJustin T. Gibbs {
49239758cc83SScott Long 	cam_simq_t queue;
49242b83592fSScott Long 	struct cam_sim *sim;
49258b8a9b1dSJustin T. Gibbs 
49269758cc83SScott Long 	mtx_lock(&cam_simq_lock);
4927ef3cf714SScott Long 	TAILQ_INIT(&queue);
4928788fb376SAlexander Motin 	while (!TAILQ_EMPTY(&cam_simq)) {
49299758cc83SScott Long 		TAILQ_CONCAT(&queue, &cam_simq, links);
49309758cc83SScott Long 		mtx_unlock(&cam_simq_lock);
4931ef3cf714SScott Long 
49329758cc83SScott Long 		while ((sim = TAILQ_FIRST(&queue)) != NULL) {
49339758cc83SScott Long 			TAILQ_REMOVE(&queue, sim, links);
493411e4faceSScott Long 			CAM_SIM_LOCK(sim);
49359758cc83SScott Long 			camisr_runqueue(&sim->sim_doneq);
493610284c8bSAlexander Motin 			sim->flags &= ~CAM_SIM_ON_DONEQ;
493711e4faceSScott Long 			CAM_SIM_UNLOCK(sim);
49389758cc83SScott Long 		}
4939788fb376SAlexander Motin 		mtx_lock(&cam_simq_lock);
4940788fb376SAlexander Motin 	}
4941788fb376SAlexander Motin 	mtx_unlock(&cam_simq_lock);
49429758cc83SScott Long }
49439758cc83SScott Long 
49449758cc83SScott Long static void
49459758cc83SScott Long camisr_runqueue(void *V_queue)
49469758cc83SScott Long {
49479758cc83SScott Long 	cam_isrq_t *queue = V_queue;
49489758cc83SScott Long 	struct	ccb_hdr *ccb_h;
49499758cc83SScott Long 
49509758cc83SScott Long 	while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
49518b8a9b1dSJustin T. Gibbs 		int	runq;
49528b8a9b1dSJustin T. Gibbs 
49539758cc83SScott Long 		TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
49548b8a9b1dSJustin T. Gibbs 		ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
49558b8a9b1dSJustin T. Gibbs 
49568b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
495757b89bbcSNate Lawson 			  ("camisr\n"));
49588b8a9b1dSJustin T. Gibbs 
49598b8a9b1dSJustin T. Gibbs 		runq = FALSE;
49608b8a9b1dSJustin T. Gibbs 
49618b8a9b1dSJustin T. Gibbs 		if (ccb_h->flags & CAM_HIGH_POWER) {
49628b8a9b1dSJustin T. Gibbs 			struct highpowerlist	*hphead;
49638b8a9b1dSJustin T. Gibbs 			union ccb		*send_ccb;
49648b8a9b1dSJustin T. Gibbs 
49652b83592fSScott Long 			mtx_lock(&xsoftc.xpt_lock);
49662b83592fSScott Long 			hphead = &xsoftc.highpowerq;
49678b8a9b1dSJustin T. Gibbs 
49688b8a9b1dSJustin T. Gibbs 			send_ccb = (union ccb *)STAILQ_FIRST(hphead);
49698b8a9b1dSJustin T. Gibbs 
49708b8a9b1dSJustin T. Gibbs 			/*
49718b8a9b1dSJustin T. Gibbs 			 * Increment the count since this command is done.
49728b8a9b1dSJustin T. Gibbs 			 */
49732b83592fSScott Long 			xsoftc.num_highpower++;
49748b8a9b1dSJustin T. Gibbs 
49758b8a9b1dSJustin T. Gibbs 			/*
49768b8a9b1dSJustin T. Gibbs 			 * Any high powered commands queued up?
49778b8a9b1dSJustin T. Gibbs 			 */
49788b8a9b1dSJustin T. Gibbs 			if (send_ccb != NULL) {
49798b8a9b1dSJustin T. Gibbs 
49808b8a9b1dSJustin T. Gibbs 				STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
49812b83592fSScott Long 				mtx_unlock(&xsoftc.xpt_lock);
49828b8a9b1dSJustin T. Gibbs 
49832cefde5fSJustin T. Gibbs 				xpt_release_devq(send_ccb->ccb_h.path,
49842cefde5fSJustin T. Gibbs 						 /*count*/1, /*runqueue*/TRUE);
49852b83592fSScott Long 			} else
49862b83592fSScott Long 				mtx_unlock(&xsoftc.xpt_lock);
49878b8a9b1dSJustin T. Gibbs 		}
49882b83592fSScott Long 
49899deea857SKenneth D. Merry 		if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
49908b8a9b1dSJustin T. Gibbs 			struct cam_ed *dev;
49918b8a9b1dSJustin T. Gibbs 
49928b8a9b1dSJustin T. Gibbs 			dev = ccb_h->path->device;
49938b8a9b1dSJustin T. Gibbs 
49948b8a9b1dSJustin T. Gibbs 			cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
49958b8a9b1dSJustin T. Gibbs 			ccb_h->path->bus->sim->devq->send_active--;
49968b8a9b1dSJustin T. Gibbs 			ccb_h->path->bus->sim->devq->send_openings++;
499783c5d981SAlexander Motin 			runq = TRUE;
49988b8a9b1dSJustin T. Gibbs 
4999b9c473b2SAlexander Motin 			if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
50008b8a9b1dSJustin T. Gibbs 			  && (dev->ccbq.dev_active == 0))) {
5001b9c473b2SAlexander Motin 				dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5002b9c473b2SAlexander Motin 				xpt_release_devq(ccb_h->path, /*count*/1,
5003b9c473b2SAlexander Motin 						 /*run_queue*/FALSE);
5004b9c473b2SAlexander Motin 			}
5005b9c473b2SAlexander Motin 
5006b9c473b2SAlexander Motin 			if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5007b9c473b2SAlexander Motin 			  && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5008b9c473b2SAlexander Motin 				dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
50092cefde5fSJustin T. Gibbs 				xpt_release_devq(ccb_h->path, /*count*/1,
501083c5d981SAlexander Motin 						 /*run_queue*/FALSE);
50118b8a9b1dSJustin T. Gibbs 			}
50128b8a9b1dSJustin T. Gibbs 
5013fd21cc5eSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5014fd21cc5eSJustin T. Gibbs 			 && (--dev->tag_delay_count == 0))
5015fd21cc5eSJustin T. Gibbs 				xpt_start_tags(ccb_h->path);
5016cccf4220SAlexander Motin 			if (!device_is_queued(dev)) {
5017cccf4220SAlexander Motin 				(void)xpt_schedule_devq(
5018cccf4220SAlexander Motin 				    ccb_h->path->bus->sim->devq, dev);
50193501942bSJustin T. Gibbs 			}
50208b8a9b1dSJustin T. Gibbs 		}
50218b8a9b1dSJustin T. Gibbs 
50228b8a9b1dSJustin T. Gibbs 		if (ccb_h->status & CAM_RELEASE_SIMQ) {
50238b8a9b1dSJustin T. Gibbs 			xpt_release_simq(ccb_h->path->bus->sim,
50248b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
5025434bbf6eSJustin T. Gibbs 			ccb_h->status &= ~CAM_RELEASE_SIMQ;
5026434bbf6eSJustin T. Gibbs 			runq = FALSE;
5027434bbf6eSJustin T. Gibbs 		}
5028434bbf6eSJustin T. Gibbs 
5029434bbf6eSJustin T. Gibbs 		if ((ccb_h->flags & CAM_DEV_QFRZDIS)
50308b8a9b1dSJustin T. Gibbs 		 && (ccb_h->status & CAM_DEV_QFRZN)) {
50312cefde5fSJustin T. Gibbs 			xpt_release_devq(ccb_h->path, /*count*/1,
50328b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
50338b8a9b1dSJustin T. Gibbs 			ccb_h->status &= ~CAM_DEV_QFRZN;
50348b8a9b1dSJustin T. Gibbs 		} else if (runq) {
5035cccf4220SAlexander Motin 			xpt_run_devq(ccb_h->path->bus->sim->devq);
50368b8a9b1dSJustin T. Gibbs 		}
50378b8a9b1dSJustin T. Gibbs 
50388b8a9b1dSJustin T. Gibbs 		/* Call the peripheral driver's callback */
5039434bbf6eSJustin T. Gibbs 		(*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
50408b8a9b1dSJustin T. Gibbs 	}
50418b8a9b1dSJustin T. Gibbs }
5042