xref: /freebsd/sys/cam/cam_xpt.c (revision 0dd50e9b5ecd1d7c7c4b282544a9727b96fad77a)
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>
428b8a9b1dSJustin T. Gibbs #include <sys/md5.h>
435d754af7SMatt Jacob #include <sys/interrupt.h>
443393f8daSKenneth D. Merry #include <sys/sbuf.h>
452b83592fSScott Long #include <sys/taskqueue.h>
468b8a9b1dSJustin T. Gibbs 
47ef3cf714SScott Long #include <sys/lock.h>
48ef3cf714SScott Long #include <sys/mutex.h>
493b87a552SMatt Jacob #include <sys/sysctl.h>
509e6461a2SMatt Jacob #include <sys/kthread.h>
51ef3cf714SScott Long 
528b8a9b1dSJustin T. Gibbs #ifdef PC98
538b8a9b1dSJustin T. Gibbs #include <pc98/pc98/pc98_machdep.h>	/* geometry translation */
548b8a9b1dSJustin T. Gibbs #endif
558b8a9b1dSJustin T. Gibbs 
568b8a9b1dSJustin T. Gibbs #include <cam/cam.h>
578b8a9b1dSJustin T. Gibbs #include <cam/cam_ccb.h>
588b8a9b1dSJustin T. Gibbs #include <cam/cam_periph.h>
598b8a9b1dSJustin T. Gibbs #include <cam/cam_sim.h>
608b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt.h>
618b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_sim.h>
628b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_periph.h>
638b8a9b1dSJustin T. Gibbs #include <cam/cam_debug.h>
648b8a9b1dSJustin T. Gibbs 
658b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_all.h>
668b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_message.h>
678b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_pass.h>
68f0d9af51SMatt Jacob #include <machine/stdarg.h>	/* for xpt_print below */
698b8a9b1dSJustin T. Gibbs #include "opt_cam.h"
708b8a9b1dSJustin T. Gibbs 
718b8a9b1dSJustin T. Gibbs /* Datastructures internal to the xpt layer */
72362abc44STai-hwa Liang MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
738b8a9b1dSJustin T. Gibbs 
742b83592fSScott Long /* Object for defering XPT actions to a taskqueue */
752b83592fSScott Long struct xpt_task {
762b83592fSScott Long 	struct task	task;
7784f82481SScott Long 	void		*data1;
7884f82481SScott Long 	uintptr_t	data2;
792b83592fSScott Long };
802b83592fSScott Long 
818b8a9b1dSJustin T. Gibbs /*
828b8a9b1dSJustin T. Gibbs  * Definition of an async handler callback block.  These are used to add
838b8a9b1dSJustin T. Gibbs  * SIMs and peripherals to the async callback lists.
848b8a9b1dSJustin T. Gibbs  */
858b8a9b1dSJustin T. Gibbs struct async_node {
86e3975643SJake Burkholder 	SLIST_ENTRY(async_node)	links;
878b8a9b1dSJustin T. Gibbs 	u_int32_t	event_enable;	/* Async Event enables */
888b8a9b1dSJustin T. Gibbs 	void		(*callback)(void *arg, u_int32_t code,
898b8a9b1dSJustin T. Gibbs 				    struct cam_path *path, void *args);
908b8a9b1dSJustin T. Gibbs 	void		*callback_arg;
918b8a9b1dSJustin T. Gibbs };
928b8a9b1dSJustin T. Gibbs 
93e3975643SJake Burkholder SLIST_HEAD(async_list, async_node);
94e3975643SJake Burkholder SLIST_HEAD(periph_list, cam_periph);
958b8a9b1dSJustin T. Gibbs 
968b8a9b1dSJustin T. Gibbs /*
978b8a9b1dSJustin T. Gibbs  * This is the maximum number of high powered commands (e.g. start unit)
988b8a9b1dSJustin T. Gibbs  * that can be outstanding at a particular time.
998b8a9b1dSJustin T. Gibbs  */
1008b8a9b1dSJustin T. Gibbs #ifndef CAM_MAX_HIGHPOWER
1018b8a9b1dSJustin T. Gibbs #define CAM_MAX_HIGHPOWER  4
1028b8a9b1dSJustin T. Gibbs #endif
1038b8a9b1dSJustin T. Gibbs 
1048b8a9b1dSJustin T. Gibbs /*
1058b8a9b1dSJustin T. Gibbs  * Structure for queueing a device in a run queue.
1068b8a9b1dSJustin T. Gibbs  * There is one run queue for allocating new ccbs,
1078b8a9b1dSJustin T. Gibbs  * and another for sending ccbs to the controller.
1088b8a9b1dSJustin T. Gibbs  */
1098b8a9b1dSJustin T. Gibbs struct cam_ed_qinfo {
1108b8a9b1dSJustin T. Gibbs 	cam_pinfo pinfo;
1118b8a9b1dSJustin T. Gibbs 	struct	  cam_ed *device;
1128b8a9b1dSJustin T. Gibbs };
1138b8a9b1dSJustin T. Gibbs 
1148b8a9b1dSJustin T. Gibbs /*
1158b8a9b1dSJustin T. Gibbs  * The CAM EDT (Existing Device Table) contains the device information for
1168b8a9b1dSJustin T. Gibbs  * all devices for all busses in the system.  The table contains a
1178b8a9b1dSJustin T. Gibbs  * cam_ed structure for each device on the bus.
1188b8a9b1dSJustin T. Gibbs  */
1198b8a9b1dSJustin T. Gibbs struct cam_ed {
120e3975643SJake Burkholder 	TAILQ_ENTRY(cam_ed) links;
1218b8a9b1dSJustin T. Gibbs 	struct	cam_ed_qinfo alloc_ccb_entry;
1228b8a9b1dSJustin T. Gibbs 	struct	cam_ed_qinfo send_ccb_entry;
1238b8a9b1dSJustin T. Gibbs 	struct	cam_et	 *target;
1242b83592fSScott Long 	struct	cam_sim  *sim;
1258b8a9b1dSJustin T. Gibbs 	lun_id_t	 lun_id;
1268b8a9b1dSJustin T. Gibbs 	struct	camq drvq;		/*
1278b8a9b1dSJustin T. Gibbs 					 * Queue of type drivers wanting to do
1288b8a9b1dSJustin T. Gibbs 					 * work on this device.
1298b8a9b1dSJustin T. Gibbs 					 */
1308b8a9b1dSJustin T. Gibbs 	struct	cam_ccbq ccbq;		/* Queue of pending ccbs */
1318b8a9b1dSJustin T. Gibbs 	struct	async_list asyncs;	/* Async callback info for this B/T/L */
1328b8a9b1dSJustin T. Gibbs 	struct	periph_list periphs;	/* All attached devices */
1338b8a9b1dSJustin T. Gibbs 	u_int	generation;		/* Generation number */
1348b8a9b1dSJustin T. Gibbs 	struct	cam_periph *owner;	/* Peripheral driver's ownership tag */
1358b8a9b1dSJustin T. Gibbs 	struct	xpt_quirk_entry *quirk;	/* Oddities about this device */
1368b8a9b1dSJustin T. Gibbs 					/* Storage for the inquiry data */
1373393f8daSKenneth D. Merry 	cam_proto	 protocol;
1383393f8daSKenneth D. Merry 	u_int		 protocol_version;
1393393f8daSKenneth D. Merry 	cam_xport	 transport;
1403393f8daSKenneth D. Merry 	u_int		 transport_version;
1418b8a9b1dSJustin T. Gibbs 	struct		 scsi_inquiry_data inq_data;
1428b8a9b1dSJustin T. Gibbs 	u_int8_t	 inq_flags;	/*
1438b8a9b1dSJustin T. Gibbs 					 * Current settings for inquiry flags.
1448b8a9b1dSJustin T. Gibbs 					 * This allows us to override settings
1458b8a9b1dSJustin T. Gibbs 					 * like disconnection and tagged
1468b8a9b1dSJustin T. Gibbs 					 * queuing for a device.
1478b8a9b1dSJustin T. Gibbs 					 */
1488b8a9b1dSJustin T. Gibbs 	u_int8_t	 queue_flags;	/* Queue flags from the control page */
1498b8a9b1dSJustin T. Gibbs 	u_int8_t	 serial_num_len;
1509ec5d7cdSMatt Jacob 	u_int8_t	*serial_num;
1518b8a9b1dSJustin T. Gibbs 	u_int32_t	 qfrozen_cnt;
1528b8a9b1dSJustin T. Gibbs 	u_int32_t	 flags;
1538b8a9b1dSJustin T. Gibbs #define CAM_DEV_UNCONFIGURED	 	0x01
1548b8a9b1dSJustin T. Gibbs #define CAM_DEV_REL_TIMEOUT_PENDING	0x02
1558b8a9b1dSJustin T. Gibbs #define CAM_DEV_REL_ON_COMPLETE		0x04
1568b8a9b1dSJustin T. Gibbs #define CAM_DEV_REL_ON_QUEUE_EMPTY	0x08
1578b8a9b1dSJustin T. Gibbs #define CAM_DEV_RESIZE_QUEUE_NEEDED	0x10
158fd21cc5eSJustin T. Gibbs #define CAM_DEV_TAG_AFTER_COUNT		0x20
15987cfaf0eSJustin T. Gibbs #define CAM_DEV_INQUIRY_DATA_VALID	0x40
1602afca7acSMatt Jacob #define	CAM_DEV_IN_DV			0x80
1612afca7acSMatt Jacob #define	CAM_DEV_DV_HIT_BOTTOM		0x100
162fd21cc5eSJustin T. Gibbs 	u_int32_t	 tag_delay_count;
163fd21cc5eSJustin T. Gibbs #define	CAM_TAG_DELAY_COUNT		5
164df8f9080SJustin T. Gibbs 	u_int32_t	 tag_saved_openings;
1658b8a9b1dSJustin T. Gibbs 	u_int32_t	 refcount;
1662b83592fSScott Long 	struct callout	 callout;
1678b8a9b1dSJustin T. Gibbs };
1688b8a9b1dSJustin T. Gibbs 
1698b8a9b1dSJustin T. Gibbs /*
1708b8a9b1dSJustin T. Gibbs  * Each target is represented by an ET (Existing Target).  These
1718b8a9b1dSJustin T. Gibbs  * entries are created when a target is successfully probed with an
1728b8a9b1dSJustin T. Gibbs  * identify, and removed when a device fails to respond after a number
1738b8a9b1dSJustin T. Gibbs  * of retries, or a bus rescan finds the device missing.
1748b8a9b1dSJustin T. Gibbs  */
1758b8a9b1dSJustin T. Gibbs struct cam_et {
176e3975643SJake Burkholder 	TAILQ_HEAD(, cam_ed) ed_entries;
177e3975643SJake Burkholder 	TAILQ_ENTRY(cam_et) links;
1788b8a9b1dSJustin T. Gibbs 	struct	cam_eb	*bus;
1798b8a9b1dSJustin T. Gibbs 	target_id_t	target_id;
1808b8a9b1dSJustin T. Gibbs 	u_int32_t	refcount;
1818b8a9b1dSJustin T. Gibbs 	u_int		generation;
18287cfaf0eSJustin T. Gibbs 	struct		timeval last_reset;
1838b8a9b1dSJustin T. Gibbs };
1848b8a9b1dSJustin T. Gibbs 
1858b8a9b1dSJustin T. Gibbs /*
1868b8a9b1dSJustin T. Gibbs  * Each bus is represented by an EB (Existing Bus).  These entries
1878b8a9b1dSJustin T. Gibbs  * are created by calls to xpt_bus_register and deleted by calls to
1888b8a9b1dSJustin T. Gibbs  * xpt_bus_deregister.
1898b8a9b1dSJustin T. Gibbs  */
1908b8a9b1dSJustin T. Gibbs struct cam_eb {
191e3975643SJake Burkholder 	TAILQ_HEAD(, cam_et) et_entries;
192e3975643SJake Burkholder 	TAILQ_ENTRY(cam_eb)  links;
1938b8a9b1dSJustin T. Gibbs 	path_id_t	     path_id;
1948b8a9b1dSJustin T. Gibbs 	struct cam_sim	     *sim;
19587cfaf0eSJustin T. Gibbs 	struct timeval	     last_reset;
1968b8a9b1dSJustin T. Gibbs 	u_int32_t	     flags;
1978b8a9b1dSJustin T. Gibbs #define	CAM_EB_RUNQ_SCHEDULED	0x01
198a5479bc5SJustin T. Gibbs 	u_int32_t	     refcount;
1998b8a9b1dSJustin T. Gibbs 	u_int		     generation;
2008b8a9b1dSJustin T. Gibbs };
2018b8a9b1dSJustin T. Gibbs 
2028b8a9b1dSJustin T. Gibbs struct cam_path {
2038b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph;
2048b8a9b1dSJustin T. Gibbs 	struct cam_eb	  *bus;
2058b8a9b1dSJustin T. Gibbs 	struct cam_et	  *target;
2068b8a9b1dSJustin T. Gibbs 	struct cam_ed	  *device;
2078b8a9b1dSJustin T. Gibbs };
2088b8a9b1dSJustin T. Gibbs 
2098b8a9b1dSJustin T. Gibbs struct xpt_quirk_entry {
2108b8a9b1dSJustin T. Gibbs 	struct scsi_inquiry_pattern inq_pat;
2118b8a9b1dSJustin T. Gibbs 	u_int8_t quirks;
2128b8a9b1dSJustin T. Gibbs #define	CAM_QUIRK_NOLUNS	0x01
2138b8a9b1dSJustin T. Gibbs #define	CAM_QUIRK_NOSERIAL	0x02
21448f6456dSMatt Jacob #define	CAM_QUIRK_HILUNS	0x04
215f4322bc8SMatt Jacob #define	CAM_QUIRK_NOHILUNS	0x08
2169deea857SKenneth D. Merry 	u_int mintags;
2179deea857SKenneth D. Merry 	u_int maxtags;
2188b8a9b1dSJustin T. Gibbs };
2193b87a552SMatt Jacob 
2203b87a552SMatt Jacob static int cam_srch_hi = 0;
2213b87a552SMatt Jacob TUNABLE_INT("kern.cam.cam_srch_hi", &cam_srch_hi);
2223b87a552SMatt Jacob static int sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS);
2233b87a552SMatt Jacob SYSCTL_PROC(_kern_cam, OID_AUTO, cam_srch_hi, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
2243b87a552SMatt Jacob     sysctl_cam_search_luns, "I",
2253b87a552SMatt Jacob     "allow search above LUN 7 for SCSI3 and greater devices");
2263b87a552SMatt Jacob 
22748f6456dSMatt Jacob #define	CAM_SCSI2_MAXLUN	8
228f4322bc8SMatt Jacob /*
229f4322bc8SMatt Jacob  * If we're not quirked to search <= the first 8 luns
230f4322bc8SMatt Jacob  * and we are either quirked to search above lun 8,
2313b87a552SMatt Jacob  * or we're > SCSI-2 and we've enabled hilun searching,
2323b87a552SMatt Jacob  * or we're > SCSI-2 and the last lun was a success,
2333b87a552SMatt Jacob  * we can look for luns above lun 8.
234f4322bc8SMatt Jacob  */
2353b87a552SMatt Jacob #define	CAN_SRCH_HI_SPARSE(dv)				\
236f4322bc8SMatt Jacob   (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) 	\
237f4322bc8SMatt Jacob   && ((dv->quirk->quirks & CAM_QUIRK_HILUNS)		\
2383b87a552SMatt Jacob   || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2 && cam_srch_hi)))
2393b87a552SMatt Jacob 
2403b87a552SMatt Jacob #define	CAN_SRCH_HI_DENSE(dv)				\
2413b87a552SMatt Jacob   (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) 	\
2423b87a552SMatt Jacob   && ((dv->quirk->quirks & CAM_QUIRK_HILUNS)		\
2433b87a552SMatt Jacob   || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2)))
2448b8a9b1dSJustin T. Gibbs 
2458b8a9b1dSJustin T. Gibbs typedef enum {
2468b8a9b1dSJustin T. Gibbs 	XPT_FLAG_OPEN		= 0x01
2478b8a9b1dSJustin T. Gibbs } xpt_flags;
2488b8a9b1dSJustin T. Gibbs 
2498b8a9b1dSJustin T. Gibbs struct xpt_softc {
2508b8a9b1dSJustin T. Gibbs 	xpt_flags		flags;
2512b83592fSScott Long 	u_int32_t		xpt_generation;
2522b83592fSScott Long 
2532b83592fSScott Long 	/* number of high powered commands that can go through right now */
2542b83592fSScott Long 	STAILQ_HEAD(highpowerlist, ccb_hdr)	highpowerq;
2552b83592fSScott Long 	int			num_highpower;
2562b83592fSScott Long 
2572b83592fSScott Long 	/* queue for handling async rescan requests. */
2582b83592fSScott Long 	TAILQ_HEAD(, ccb_hdr) ccb_scanq;
2592b83592fSScott Long 
2602b83592fSScott Long 	/* Registered busses */
2612b83592fSScott Long 	TAILQ_HEAD(,cam_eb)	xpt_busses;
2622b83592fSScott Long 	u_int			bus_generation;
2632b83592fSScott Long 
2642b83592fSScott Long 	struct intr_config_hook	*xpt_config_hook;
2652b83592fSScott Long 
2662b83592fSScott Long 	struct mtx		xpt_topo_lock;
2672b83592fSScott Long 	struct mtx		xpt_lock;
2688b8a9b1dSJustin T. Gibbs };
2698b8a9b1dSJustin T. Gibbs 
2708b8a9b1dSJustin T. Gibbs static const char quantum[] = "QUANTUM";
271f24c39c7SKenneth D. Merry static const char sony[] = "SONY";
272f24c39c7SKenneth D. Merry static const char west_digital[] = "WDIGTL";
2731f04f5abSKenneth D. Merry static const char samsung[] = "SAMSUNG";
274560d629fSKenneth D. Merry static const char seagate[] = "SEAGATE";
2755acba6ecSJohn-Mark Gurney static const char microp[] = "MICROP";
2768b8a9b1dSJustin T. Gibbs 
2778b8a9b1dSJustin T. Gibbs static struct xpt_quirk_entry xpt_quirk_table[] =
2788b8a9b1dSJustin T. Gibbs {
2798b8a9b1dSJustin T. Gibbs 	{
2808b8a9b1dSJustin T. Gibbs 		/* Reports QUEUE FULL for temporary resource shortages */
2816747168dSKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
2828b8a9b1dSJustin T. Gibbs 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
2838b8a9b1dSJustin T. Gibbs 	},
2848b8a9b1dSJustin T. Gibbs 	{
2858b8a9b1dSJustin T. Gibbs 		/* Reports QUEUE FULL for temporary resource shortages */
2866747168dSKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
2876747168dSKenneth D. Merry 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
2886747168dSKenneth D. Merry 	},
2896747168dSKenneth D. Merry 	{
2906747168dSKenneth D. Merry 		/* Reports QUEUE FULL for temporary resource shortages */
2916747168dSKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
2928b8a9b1dSJustin T. Gibbs 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
2938b8a9b1dSJustin T. Gibbs 	},
2948b8a9b1dSJustin T. Gibbs 	{
2958b8a9b1dSJustin T. Gibbs 		/* Broken tagged queuing drive */
2965acba6ecSJohn-Mark Gurney 		{ T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
2975acba6ecSJohn-Mark Gurney 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
2985acba6ecSJohn-Mark Gurney 	},
2995acba6ecSJohn-Mark Gurney 	{
3005acba6ecSJohn-Mark Gurney 		/* Broken tagged queuing drive */
3018b8a9b1dSJustin T. Gibbs 		{ T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
3028b8a9b1dSJustin T. Gibbs 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
3038b8a9b1dSJustin T. Gibbs 	},
3048b8a9b1dSJustin T. Gibbs 	{
3058b8a9b1dSJustin T. Gibbs 		/* Broken tagged queuing drive */
3065acba6ecSJohn-Mark Gurney 		{ T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
3078b8a9b1dSJustin T. Gibbs 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
3088b8a9b1dSJustin T. Gibbs 	},
3098b8a9b1dSJustin T. Gibbs 	{
3108597eafeSKenneth D. Merry 		/*
3114cdd0221SKenneth D. Merry 		 * Unfortunately, the Quantum Atlas III has the same
3124cdd0221SKenneth D. Merry 		 * problem as the Atlas II drives above.
3134cdd0221SKenneth D. Merry 		 * Reported by: "Johan Granlund" <johan@granlund.nu>
3144cdd0221SKenneth D. Merry 		 *
3154cdd0221SKenneth D. Merry 		 * For future reference, the drive with the problem was:
3164cdd0221SKenneth D. Merry 		 * QUANTUM QM39100TD-SW N1B0
3174cdd0221SKenneth D. Merry 		 *
3184cdd0221SKenneth D. Merry 		 * It's possible that Quantum will fix the problem in later
3194cdd0221SKenneth D. Merry 		 * firmware revisions.  If that happens, the quirk entry
3204cdd0221SKenneth D. Merry 		 * will need to be made specific to the firmware revisions
3214cdd0221SKenneth D. Merry 		 * with the problem.
3224cdd0221SKenneth D. Merry 		 *
3234cdd0221SKenneth D. Merry 		 */
3244cdd0221SKenneth D. Merry 		/* Reports QUEUE FULL for temporary resource shortages */
3254cdd0221SKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
3264cdd0221SKenneth D. Merry 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
3274cdd0221SKenneth D. Merry 	},
3284cdd0221SKenneth D. Merry 	{
3294cdd0221SKenneth D. Merry 		/*
330588b47e5SKenneth D. Merry 		 * 18 Gig Atlas III, same problem as the 9G version.
331588b47e5SKenneth D. Merry 		 * Reported by: Andre Albsmeier
332588b47e5SKenneth D. Merry 		 *		<andre.albsmeier@mchp.siemens.de>
333588b47e5SKenneth D. Merry 		 *
334588b47e5SKenneth D. Merry 		 * For future reference, the drive with the problem was:
335588b47e5SKenneth D. Merry 		 * QUANTUM QM318000TD-S N491
336588b47e5SKenneth D. Merry 		 */
337588b47e5SKenneth D. Merry 		/* Reports QUEUE FULL for temporary resource shortages */
338588b47e5SKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
339588b47e5SKenneth D. Merry 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
340588b47e5SKenneth D. Merry 	},
341588b47e5SKenneth D. Merry 	{
342588b47e5SKenneth D. Merry 		/*
3438597eafeSKenneth D. Merry 		 * Broken tagged queuing drive
3448597eafeSKenneth D. Merry 		 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
3458597eafeSKenneth D. Merry 		 *         and: Martin Renters <martin@tdc.on.ca>
3468597eafeSKenneth D. Merry 		 */
347560d629fSKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
3488597eafeSKenneth D. Merry 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
3498597eafeSKenneth D. Merry 	},
350560d629fSKenneth D. Merry 		/*
351c816e025SKenneth D. Merry 		 * The Seagate Medalist Pro drives have very poor write
352c816e025SKenneth D. Merry 		 * performance with anything more than 2 tags.
353560d629fSKenneth D. Merry 		 *
354560d629fSKenneth D. Merry 		 * Reported by:  Paul van der Zwan <paulz@trantor.xs4all.nl>
355560d629fSKenneth D. Merry 		 * Drive:  <SEAGATE ST36530N 1444>
356c816e025SKenneth D. Merry 		 *
357c816e025SKenneth D. Merry 		 * Reported by:  Jeremy Lea <reg@shale.csir.co.za>
358c816e025SKenneth D. Merry 		 * Drive:  <SEAGATE ST34520W 1281>
359c816e025SKenneth D. Merry 		 *
360c816e025SKenneth D. Merry 		 * No one has actually reported that the 9G version
361c816e025SKenneth D. Merry 		 * (ST39140*) of the Medalist Pro has the same problem, but
362c816e025SKenneth D. Merry 		 * we're assuming that it does because the 4G and 6.5G
363c816e025SKenneth D. Merry 		 * versions of the drive are broken.
364560d629fSKenneth D. Merry 		 */
365c816e025SKenneth D. Merry 	{
366c816e025SKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
367c816e025SKenneth D. Merry 		/*quirks*/0, /*mintags*/2, /*maxtags*/2
368c816e025SKenneth D. Merry 	},
369c816e025SKenneth D. Merry 	{
370560d629fSKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
371560d629fSKenneth D. Merry 		/*quirks*/0, /*mintags*/2, /*maxtags*/2
372560d629fSKenneth D. Merry 	},
373560d629fSKenneth D. Merry 	{
374c816e025SKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
375c816e025SKenneth D. Merry 		/*quirks*/0, /*mintags*/2, /*maxtags*/2
376c816e025SKenneth D. Merry 	},
377c816e025SKenneth D. Merry 	{
378ca3b51e9SKenneth D. Merry 		/*
379ca3b51e9SKenneth D. Merry 		 * Slow when tagged queueing is enabled.  Write performance
380ca3b51e9SKenneth D. Merry 		 * steadily drops off with more and more concurrent
381ca3b51e9SKenneth D. Merry 		 * transactions.  Best sequential write performance with
382ca3b51e9SKenneth D. Merry 		 * tagged queueing turned off and write caching turned on.
383ca3b51e9SKenneth D. Merry 		 *
384ca3b51e9SKenneth D. Merry 		 * PR:  kern/10398
385ca3b51e9SKenneth D. Merry 		 * Submitted by:  Hideaki Okada <hokada@isl.melco.co.jp>
386ca3b51e9SKenneth D. Merry 		 * Drive:  DCAS-34330 w/ "S65A" firmware.
387ca3b51e9SKenneth D. Merry 		 *
388ca3b51e9SKenneth D. Merry 		 * The drive with the problem had the "S65A" firmware
389ca3b51e9SKenneth D. Merry 		 * revision, and has also been reported (by Stephen J.
390ca3b51e9SKenneth D. Merry 		 * Roznowski <sjr@home.net>) for a drive with the "S61A"
391ca3b51e9SKenneth D. Merry 		 * firmware revision.
392ca3b51e9SKenneth D. Merry 		 *
393ca3b51e9SKenneth D. Merry 		 * Although no one has reported problems with the 2 gig
394ca3b51e9SKenneth D. Merry 		 * version of the DCAS drive, the assumption is that it
395ca3b51e9SKenneth D. Merry 		 * has the same problems as the 4 gig version.  Therefore
396ca3b51e9SKenneth D. Merry 		 * this quirk entries disables tagged queueing for all
397ca3b51e9SKenneth D. Merry 		 * DCAS drives.
398ca3b51e9SKenneth D. Merry 		 */
399ca3b51e9SKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
400ca3b51e9SKenneth D. Merry 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
401ca3b51e9SKenneth D. Merry 	},
402ca3b51e9SKenneth D. Merry 	{
4038b8a9b1dSJustin T. Gibbs 		/* Broken tagged queuing drive */
4048b8a9b1dSJustin T. Gibbs 		{ T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
4058b8a9b1dSJustin T. Gibbs 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
4068b8a9b1dSJustin T. Gibbs 	},
4078b8a9b1dSJustin T. Gibbs 	{
4082ba01e6fSJustin T. Gibbs 		/* Broken tagged queuing drive */
4096dd76100SKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
4102ba01e6fSJustin T. Gibbs 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
4112ba01e6fSJustin T. Gibbs 	},
4122ba01e6fSJustin T. Gibbs 	{
4139fdeccedSMatt Jacob 		/* This does not support other than LUN 0 */
4145ce1b004SMatt Jacob 		{ T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*" },
415df4b14f4SMatt Jacob 		CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
416015a972cSMatt Jacob 	},
417015a972cSMatt Jacob 	{
418e471e974SJustin T. Gibbs 		/*
4191f04f5abSKenneth D. Merry 		 * Broken tagged queuing drive.
4201f04f5abSKenneth D. Merry 		 * Submitted by:
4211f04f5abSKenneth D. Merry 		 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
4221f04f5abSKenneth D. Merry 		 * in PR kern/9535
4231f04f5abSKenneth D. Merry 		 */
4241f04f5abSKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
4251f04f5abSKenneth D. Merry 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
4261f04f5abSKenneth D. Merry 	},
4271f04f5abSKenneth D. Merry         {
4281f04f5abSKenneth D. Merry 		/*
429f24c39c7SKenneth D. Merry 		 * Slow when tagged queueing is enabled. (1.5MB/sec versus
430f24c39c7SKenneth D. Merry 		 * 8MB/sec.)
431f24c39c7SKenneth D. Merry 		 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
432ca3b51e9SKenneth D. Merry 		 * Best performance with these drives is achieved with
433ca3b51e9SKenneth D. Merry 		 * tagged queueing turned off, and write caching turned on.
434f24c39c7SKenneth D. Merry 		 */
435f24c39c7SKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
436f24c39c7SKenneth D. Merry 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
437f24c39c7SKenneth D. Merry         },
438f24c39c7SKenneth D. Merry         {
439f24c39c7SKenneth D. Merry 		/*
440f24c39c7SKenneth D. Merry 		 * Slow when tagged queueing is enabled. (1.5MB/sec versus
441f24c39c7SKenneth D. Merry 		 * 8MB/sec.)
442f24c39c7SKenneth D. Merry 		 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
443ca3b51e9SKenneth D. Merry 		 * Best performance with these drives is achieved with
444ca3b51e9SKenneth D. Merry 		 * tagged queueing turned off, and write caching turned on.
445f24c39c7SKenneth D. Merry 		 */
446f24c39c7SKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
447f24c39c7SKenneth D. Merry 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
448f24c39c7SKenneth D. Merry         },
449f24c39c7SKenneth D. Merry 	{
450f24c39c7SKenneth D. Merry 		/*
451e471e974SJustin T. Gibbs 		 * Doesn't handle queue full condition correctly,
452e471e974SJustin T. Gibbs 		 * so we need to limit maxtags to what the device
453e471e974SJustin T. Gibbs 		 * can handle instead of determining this automatically.
454e471e974SJustin T. Gibbs 		 */
4551f04f5abSKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
456fd21cc5eSJustin T. Gibbs 		/*quirks*/0, /*mintags*/2, /*maxtags*/32
457e471e974SJustin T. Gibbs 	},
458e471e974SJustin T. Gibbs 	{
4598b8a9b1dSJustin T. Gibbs 		/* Really only one LUN */
460f2538844SMatt Jacob 		{ T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
4618b8a9b1dSJustin T. Gibbs 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
4628b8a9b1dSJustin T. Gibbs 	},
4638b8a9b1dSJustin T. Gibbs 	{
464fd21cc5eSJustin T. Gibbs 		/* I can't believe we need a quirk for DPT volumes. */
4654b4c702bSMatt Jacob 		{ T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
466fd21cc5eSJustin T. Gibbs 		CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
467fd21cc5eSJustin T. Gibbs 		/*mintags*/0, /*maxtags*/255
468fd21cc5eSJustin T. Gibbs 	},
469fd21cc5eSJustin T. Gibbs 	{
470f24c39c7SKenneth D. Merry 		/*
47125e5ca27SKenneth D. Merry 		 * Many Sony CDROM drives don't like multi-LUN probing.
472f24c39c7SKenneth D. Merry 		 */
4734b4c702bSMatt Jacob 		{ T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
474f24c39c7SKenneth D. Merry 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
475f24c39c7SKenneth D. Merry 	},
476f24c39c7SKenneth D. Merry 	{
477f24c39c7SKenneth D. Merry 		/*
478f24c39c7SKenneth D. Merry 		 * This drive doesn't like multiple LUN probing.
479f24c39c7SKenneth D. Merry 		 * Submitted by:  Parag Patel <parag@cgt.com>
480f24c39c7SKenneth D. Merry 		 */
4814b4c702bSMatt Jacob 		{ T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R   CDU9*", "*" },
482f24c39c7SKenneth D. Merry 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
483f24c39c7SKenneth D. Merry 	},
484f24c39c7SKenneth D. Merry 	{
485ca2b07adSKenneth D. Merry 		{ T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
486ca2b07adSKenneth D. Merry 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
487ca2b07adSKenneth D. Merry 	},
488ca2b07adSKenneth D. Merry 	{
489b1d26455SKenneth D. Merry 		/*
490b1d26455SKenneth D. Merry 		 * The 8200 doesn't like multi-lun probing, and probably
491b1d26455SKenneth D. Merry 		 * don't like serial number requests either.
492b1d26455SKenneth D. Merry 		 */
493b1d26455SKenneth D. Merry 		{
494b1d26455SKenneth D. Merry 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
495b1d26455SKenneth D. Merry 			"EXB-8200*", "*"
496b1d26455SKenneth D. Merry 		},
497b1d26455SKenneth D. Merry 		CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
498b1d26455SKenneth D. Merry 	},
499b1d26455SKenneth D. Merry 	{
50036230d67SJoerg Wunsch 		/*
501f2538844SMatt Jacob 		 * Let's try the same as above, but for a drive that says
502f2538844SMatt Jacob 		 * it's an IPL-6860 but is actually an EXB 8200.
503f2538844SMatt Jacob 		 */
504f2538844SMatt Jacob 		{
505f2538844SMatt Jacob 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
506f2538844SMatt Jacob 			"IPL-6860*", "*"
507f2538844SMatt Jacob 		},
508f2538844SMatt Jacob 		CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
509f2538844SMatt Jacob 	},
510f2538844SMatt Jacob 	{
511f2538844SMatt Jacob 		/*
512a6cb9949SKenneth D. Merry 		 * These Hitachi drives don't like multi-lun probing.
513a6cb9949SKenneth D. Merry 		 * The PR submitter has a DK319H, but says that the Linux
514a6cb9949SKenneth D. Merry 		 * kernel has a similar work-around for the DK312 and DK314,
515a6cb9949SKenneth D. Merry 		 * so all DK31* drives are quirked here.
516a6cb9949SKenneth D. Merry 		 * PR:            misc/18793
517a6cb9949SKenneth D. Merry 		 * Submitted by:  Paul Haddad <paul@pth.com>
518a6cb9949SKenneth D. Merry 		 */
519a6cb9949SKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
520a6cb9949SKenneth D. Merry 		CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
521a6cb9949SKenneth D. Merry 	},
522a6cb9949SKenneth D. Merry 	{
523a6cb9949SKenneth D. Merry 		/*
524136b546aSDavid Greenman 		 * The Hitachi CJ series with J8A8 firmware apparantly has
525136b546aSDavid Greenman 		 * problems with tagged commands.
526136b546aSDavid Greenman 		 * PR: 23536
527136b546aSDavid Greenman 		 * Reported by: amagai@nue.org
528136b546aSDavid Greenman 		 */
529136b546aSDavid Greenman 		{ T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
530136b546aSDavid Greenman 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
531136b546aSDavid Greenman 	},
532136b546aSDavid Greenman 	{
533136b546aSDavid Greenman 		/*
53478900a83SMatt Jacob 		 * These are the large storage arrays.
53578900a83SMatt Jacob 		 * Submitted by:  William Carrel <william.carrel@infospace.com>
53678900a83SMatt Jacob 		 */
53778900a83SMatt Jacob 		{ T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
53878900a83SMatt Jacob 		CAM_QUIRK_HILUNS, 2, 1024
53978900a83SMatt Jacob 	},
54078900a83SMatt Jacob 	{
54178900a83SMatt Jacob 		/*
54236230d67SJoerg Wunsch 		 * This old revision of the TDC3600 is also SCSI-1, and
54336230d67SJoerg Wunsch 		 * hangs upon serial number probing.
54436230d67SJoerg Wunsch 		 */
54536230d67SJoerg Wunsch 		{
54636230d67SJoerg Wunsch 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
54736230d67SJoerg Wunsch 			" TDC 3600", "U07:"
54836230d67SJoerg Wunsch 		},
54936230d67SJoerg Wunsch 		CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
55036230d67SJoerg Wunsch 	},
55136230d67SJoerg Wunsch 	{
5522c3cca96SMatt Jacob 		/*
5537afa4afbSHidetoshi Shimokawa 		 * Maxtor Personal Storage 3000XT (Firewire)
5547afa4afbSHidetoshi Shimokawa 		 * hangs upon serial number probing.
5557afa4afbSHidetoshi Shimokawa 		 */
5567afa4afbSHidetoshi Shimokawa 		{
5577afa4afbSHidetoshi Shimokawa 			T_DIRECT, SIP_MEDIA_FIXED, "Maxtor",
5587afa4afbSHidetoshi Shimokawa 			"1394 storage", "*"
5597afa4afbSHidetoshi Shimokawa 		},
5607afa4afbSHidetoshi Shimokawa 		CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
5617afa4afbSHidetoshi Shimokawa 	},
5627afa4afbSHidetoshi Shimokawa 	{
5637afa4afbSHidetoshi Shimokawa 		/*
5642c3cca96SMatt Jacob 		 * Would repond to all LUNs if asked for.
5652c3cca96SMatt Jacob 		 */
5662c3cca96SMatt Jacob 		{
5672c3cca96SMatt Jacob 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
5682c3cca96SMatt Jacob 			"CP150", "*"
5692c3cca96SMatt Jacob 		},
5702c3cca96SMatt Jacob 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
5712c3cca96SMatt Jacob 	},
5722c3cca96SMatt Jacob 	{
573040816ecSMatt Jacob 		/*
574040816ecSMatt Jacob 		 * Would repond to all LUNs if asked for.
575040816ecSMatt Jacob 		 */
576040816ecSMatt Jacob 		{
577040816ecSMatt Jacob 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
578040816ecSMatt Jacob 			"96X2*", "*"
579040816ecSMatt Jacob 		},
580040816ecSMatt Jacob 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
581040816ecSMatt Jacob 	},
582040816ecSMatt Jacob 	{
5834b4c702bSMatt Jacob 		/* Submitted by: Matthew Dodd <winter@jurai.net> */
5844b4c702bSMatt Jacob 		{ T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
5854b4c702bSMatt Jacob 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
5864b4c702bSMatt Jacob 	},
5874b4c702bSMatt Jacob 	{
5884b4c702bSMatt Jacob 		/* Submitted by: Matthew Dodd <winter@jurai.net> */
5894b4c702bSMatt Jacob 		{ T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
5904b4c702bSMatt Jacob 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
5914b4c702bSMatt Jacob 	},
5924b4c702bSMatt Jacob 	{
593da6efda6SDavid Greenman 		/* TeraSolutions special settings for TRC-22 RAID */
594da6efda6SDavid Greenman 		{ T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
595da6efda6SDavid Greenman 		  /*quirks*/0, /*mintags*/55, /*maxtags*/255
596da6efda6SDavid Greenman 	},
597da6efda6SDavid Greenman 	{
598c7948a94SMatt Jacob 		/* Veritas Storage Appliance */
599c7948a94SMatt Jacob 		{ T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
600c7948a94SMatt Jacob 		  CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
601c7948a94SMatt Jacob 	},
602c7948a94SMatt Jacob 	{
6033b9d81dcSJoerg Wunsch 		/*
6043b9d81dcSJoerg Wunsch 		 * Would respond to all LUNs.  Device type and removable
6053b9d81dcSJoerg Wunsch 		 * flag are jumper-selectable.
6063b9d81dcSJoerg Wunsch 		 */
6073b9d81dcSJoerg Wunsch 		{ T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
6083b9d81dcSJoerg Wunsch 		  "Tahiti 1", "*"
6093b9d81dcSJoerg Wunsch 		},
6103b9d81dcSJoerg Wunsch 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
6113b9d81dcSJoerg Wunsch 	},
6123b9d81dcSJoerg Wunsch 	{
6138924340fSMichael Reifenberger 		/* EasyRAID E5A aka. areca ARC-6010 */
6148924340fSMichael Reifenberger 		{ T_DIRECT, SIP_MEDIA_FIXED, "easyRAID", "*", "*" },
6158924340fSMichael Reifenberger 		  CAM_QUIRK_NOHILUNS, /*mintags*/2, /*maxtags*/255
6168924340fSMichael Reifenberger 	},
6178924340fSMichael Reifenberger 	{
6181190a85bSMatt Jacob 		{ T_ENCLOSURE, SIP_MEDIA_FIXED, "DP", "BACKPLANE", "*" },
6191190a85bSMatt Jacob 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
6201190a85bSMatt Jacob 	},
6211190a85bSMatt Jacob 	{
6228b8a9b1dSJustin T. Gibbs 		/* Default tagged queuing parameters for all devices */
6238b8a9b1dSJustin T. Gibbs 		{
6248b8a9b1dSJustin T. Gibbs 		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
6258b8a9b1dSJustin T. Gibbs 		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
6268b8a9b1dSJustin T. Gibbs 		},
627fd21cc5eSJustin T. Gibbs 		/*quirks*/0, /*mintags*/2, /*maxtags*/255
6288b8a9b1dSJustin T. Gibbs 	},
6298b8a9b1dSJustin T. Gibbs };
630fd21cc5eSJustin T. Gibbs 
63187cfaf0eSJustin T. Gibbs static const int xpt_quirk_table_size =
63287cfaf0eSJustin T. Gibbs 	sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
63387cfaf0eSJustin T. Gibbs 
6348b8a9b1dSJustin T. Gibbs typedef enum {
6358b8a9b1dSJustin T. Gibbs 	DM_RET_COPY		= 0x01,
6368b8a9b1dSJustin T. Gibbs 	DM_RET_FLAG_MASK	= 0x0f,
6378b8a9b1dSJustin T. Gibbs 	DM_RET_NONE		= 0x00,
6388b8a9b1dSJustin T. Gibbs 	DM_RET_STOP		= 0x10,
6398b8a9b1dSJustin T. Gibbs 	DM_RET_DESCEND		= 0x20,
6408b8a9b1dSJustin T. Gibbs 	DM_RET_ERROR		= 0x30,
6418b8a9b1dSJustin T. Gibbs 	DM_RET_ACTION_MASK	= 0xf0
6428b8a9b1dSJustin T. Gibbs } dev_match_ret;
6438b8a9b1dSJustin T. Gibbs 
6448b8a9b1dSJustin T. Gibbs typedef enum {
6458b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_BUS,
6468b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_TARGET,
6478b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_DEVICE,
6488b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_PERIPH
6498b8a9b1dSJustin T. Gibbs } xpt_traverse_depth;
6508b8a9b1dSJustin T. Gibbs 
6518b8a9b1dSJustin T. Gibbs struct xpt_traverse_config {
6528b8a9b1dSJustin T. Gibbs 	xpt_traverse_depth	depth;
6538b8a9b1dSJustin T. Gibbs 	void			*tr_func;
6548b8a9b1dSJustin T. Gibbs 	void			*tr_arg;
6558b8a9b1dSJustin T. Gibbs };
6568b8a9b1dSJustin T. Gibbs 
6578b8a9b1dSJustin T. Gibbs typedef	int	xpt_busfunc_t (struct cam_eb *bus, void *arg);
6588b8a9b1dSJustin T. Gibbs typedef	int	xpt_targetfunc_t (struct cam_et *target, void *arg);
6598b8a9b1dSJustin T. Gibbs typedef	int	xpt_devicefunc_t (struct cam_ed *device, void *arg);
6608b8a9b1dSJustin T. Gibbs typedef	int	xpt_periphfunc_t (struct cam_periph *periph, void *arg);
6618b8a9b1dSJustin T. Gibbs typedef int	xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
6628b8a9b1dSJustin T. Gibbs 
6638b8a9b1dSJustin T. Gibbs /* Transport layer configuration information */
6648b8a9b1dSJustin T. Gibbs static struct xpt_softc xsoftc;
6658b8a9b1dSJustin T. Gibbs 
6668b8a9b1dSJustin T. Gibbs /* Queues for our software interrupt handler */
667e3975643SJake Burkholder typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
6689758cc83SScott Long typedef TAILQ_HEAD(cam_simq, cam_sim) cam_simq_t;
6699758cc83SScott Long static cam_simq_t cam_simq;
6709758cc83SScott Long static struct mtx cam_simq_lock;
6718b8a9b1dSJustin T. Gibbs 
6722b83592fSScott Long /* Pointers to software interrupt handlers */
6732b83592fSScott Long static void *cambio_ih;
6748b8a9b1dSJustin T. Gibbs 
6759a1c8571SNick Hibma struct cam_periph *xpt_periph;
6769a1c8571SNick Hibma 
6778b8a9b1dSJustin T. Gibbs static periph_init_t xpt_periph_init;
6788b8a9b1dSJustin T. Gibbs 
6798b8a9b1dSJustin T. Gibbs static periph_init_t probe_periph_init;
6808b8a9b1dSJustin T. Gibbs 
6818b8a9b1dSJustin T. Gibbs static struct periph_driver xpt_driver =
6828b8a9b1dSJustin T. Gibbs {
6838b8a9b1dSJustin T. Gibbs 	xpt_periph_init, "xpt",
6848b8a9b1dSJustin T. Gibbs 	TAILQ_HEAD_INITIALIZER(xpt_driver.units)
6858b8a9b1dSJustin T. Gibbs };
6868b8a9b1dSJustin T. Gibbs 
6878b8a9b1dSJustin T. Gibbs static struct periph_driver probe_driver =
6888b8a9b1dSJustin T. Gibbs {
6898b8a9b1dSJustin T. Gibbs 	probe_periph_init, "probe",
6908b8a9b1dSJustin T. Gibbs 	TAILQ_HEAD_INITIALIZER(probe_driver.units)
6918b8a9b1dSJustin T. Gibbs };
6928b8a9b1dSJustin T. Gibbs 
6930b7c27b9SPeter Wemm PERIPHDRIVER_DECLARE(xpt, xpt_driver);
6940b7c27b9SPeter Wemm PERIPHDRIVER_DECLARE(probe, probe_driver);
6958b8a9b1dSJustin T. Gibbs 
6968b8a9b1dSJustin T. Gibbs 
6978b8a9b1dSJustin T. Gibbs static d_open_t xptopen;
6988b8a9b1dSJustin T. Gibbs static d_close_t xptclose;
6998b8a9b1dSJustin T. Gibbs static d_ioctl_t xptioctl;
7008b8a9b1dSJustin T. Gibbs 
7014e2f199eSPoul-Henning Kamp static struct cdevsw xpt_cdevsw = {
702dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
7032b83592fSScott Long 	.d_flags =	0,
7047ac40f5fSPoul-Henning Kamp 	.d_open =	xptopen,
7057ac40f5fSPoul-Henning Kamp 	.d_close =	xptclose,
7067ac40f5fSPoul-Henning Kamp 	.d_ioctl =	xptioctl,
7077ac40f5fSPoul-Henning Kamp 	.d_name =	"xpt",
7088b8a9b1dSJustin T. Gibbs };
7098b8a9b1dSJustin T. Gibbs 
7108b8a9b1dSJustin T. Gibbs 
711d3ef3454SIan Dowse static void dead_sim_action(struct cam_sim *sim, union ccb *ccb);
712d3ef3454SIan Dowse static void dead_sim_poll(struct cam_sim *sim);
713d3ef3454SIan Dowse 
714d3ef3454SIan Dowse /* Dummy SIM that is used when the real one has gone. */
715d3ef3454SIan Dowse static struct cam_sim cam_dead_sim = {
716d3ef3454SIan Dowse 	.sim_action =	dead_sim_action,
717d3ef3454SIan Dowse 	.sim_poll =	dead_sim_poll,
718d3ef3454SIan Dowse 	.sim_name =	"dead_sim",
719d3ef3454SIan Dowse };
720d3ef3454SIan Dowse 
721d3ef3454SIan Dowse #define SIM_DEAD(sim)	((sim) == &cam_dead_sim)
722d3ef3454SIan Dowse 
7238b8a9b1dSJustin T. Gibbs 
7248b8a9b1dSJustin T. Gibbs /* Storage for debugging datastructures */
7258b8a9b1dSJustin T. Gibbs #ifdef	CAMDEBUG
7268b8a9b1dSJustin T. Gibbs struct cam_path *cam_dpath;
7278b8a9b1dSJustin T. Gibbs u_int32_t cam_dflags;
7282cefde5fSJustin T. Gibbs u_int32_t cam_debug_delay;
7298b8a9b1dSJustin T. Gibbs #endif
7308b8a9b1dSJustin T. Gibbs 
7318b8a9b1dSJustin T. Gibbs #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
7328b8a9b1dSJustin T. Gibbs #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
7338b8a9b1dSJustin T. Gibbs #endif
7348b8a9b1dSJustin T. Gibbs 
7358b8a9b1dSJustin T. Gibbs /*
7368b8a9b1dSJustin T. Gibbs  * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
7378b8a9b1dSJustin T. Gibbs  * enabled.  Also, the user must have either none, or all of CAM_DEBUG_BUS,
7388b8a9b1dSJustin T. Gibbs  * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
7398b8a9b1dSJustin T. Gibbs  */
7408b8a9b1dSJustin T. Gibbs #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
7418b8a9b1dSJustin T. Gibbs     || defined(CAM_DEBUG_LUN)
7428b8a9b1dSJustin T. Gibbs #ifdef CAMDEBUG
7438b8a9b1dSJustin T. Gibbs #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
7448b8a9b1dSJustin T. Gibbs     || !defined(CAM_DEBUG_LUN)
7458b8a9b1dSJustin T. Gibbs #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
7468b8a9b1dSJustin T. Gibbs         and CAM_DEBUG_LUN"
7478b8a9b1dSJustin T. Gibbs #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
7488b8a9b1dSJustin T. Gibbs #else /* !CAMDEBUG */
7498b8a9b1dSJustin T. Gibbs #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
7508b8a9b1dSJustin T. Gibbs #endif /* CAMDEBUG */
7518b8a9b1dSJustin T. Gibbs #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
7528b8a9b1dSJustin T. Gibbs 
7536d2a8f1cSPeter Wemm /* Our boot-time initialization hook */
75474bd1c10SNick Hibma static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
75574bd1c10SNick Hibma 
75674bd1c10SNick Hibma static moduledata_t cam_moduledata = {
75774bd1c10SNick Hibma 	"cam",
75874bd1c10SNick Hibma 	cam_module_event_handler,
75974bd1c10SNick Hibma 	NULL
76074bd1c10SNick Hibma };
76174bd1c10SNick Hibma 
7622b83592fSScott Long static int	xpt_init(void *);
76374bd1c10SNick Hibma 
76474bd1c10SNick Hibma DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
76574bd1c10SNick Hibma MODULE_VERSION(cam, 1);
76674bd1c10SNick Hibma 
7678b8a9b1dSJustin T. Gibbs 
7688b8a9b1dSJustin T. Gibbs static cam_status	xpt_compile_path(struct cam_path *new_path,
7698b8a9b1dSJustin T. Gibbs 					 struct cam_periph *perph,
7708b8a9b1dSJustin T. Gibbs 					 path_id_t path_id,
7718b8a9b1dSJustin T. Gibbs 					 target_id_t target_id,
7728b8a9b1dSJustin T. Gibbs 					 lun_id_t lun_id);
7738b8a9b1dSJustin T. Gibbs 
7748b8a9b1dSJustin T. Gibbs static void		xpt_release_path(struct cam_path *path);
7758b8a9b1dSJustin T. Gibbs 
7768b8a9b1dSJustin T. Gibbs static void		xpt_async_bcast(struct async_list *async_head,
7778b8a9b1dSJustin T. Gibbs 					u_int32_t async_code,
7788b8a9b1dSJustin T. Gibbs 					struct cam_path *path,
7798b8a9b1dSJustin T. Gibbs 					void *async_arg);
7802f22d08dSJustin T. Gibbs static void		xpt_dev_async(u_int32_t async_code,
7812f22d08dSJustin T. Gibbs 				      struct cam_eb *bus,
7822f22d08dSJustin T. Gibbs 				      struct cam_et *target,
7832f22d08dSJustin T. Gibbs 				      struct cam_ed *device,
7842f22d08dSJustin T. Gibbs 				      void *async_arg);
785434bbf6eSJustin T. Gibbs static path_id_t xptnextfreepathid(void);
786434bbf6eSJustin T. Gibbs static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
7878b8a9b1dSJustin T. Gibbs static union ccb *xpt_get_ccb(struct cam_ed *device);
7888b8a9b1dSJustin T. Gibbs static int	 xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
7898b8a9b1dSJustin T. Gibbs 				  u_int32_t new_priority);
7908b8a9b1dSJustin T. Gibbs static void	 xpt_run_dev_allocq(struct cam_eb *bus);
7918b8a9b1dSJustin T. Gibbs static void	 xpt_run_dev_sendq(struct cam_eb *bus);
7928b8a9b1dSJustin T. Gibbs static timeout_t xpt_release_devq_timeout;
7932b83592fSScott Long static void	 xpt_release_simq_timeout(void *arg) __unused;
794a5479bc5SJustin T. Gibbs static void	 xpt_release_bus(struct cam_eb *bus);
7952cefde5fSJustin T. Gibbs static void	 xpt_release_devq_device(struct cam_ed *dev, u_int count,
7962cefde5fSJustin T. Gibbs 					 int run_queue);
7978b8a9b1dSJustin T. Gibbs static struct cam_et*
7988b8a9b1dSJustin T. Gibbs 		 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
7998b8a9b1dSJustin T. Gibbs static void	 xpt_release_target(struct cam_eb *bus, struct cam_et *target);
8008b8a9b1dSJustin T. Gibbs static struct cam_ed*
8018b8a9b1dSJustin T. Gibbs 		 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
8028b8a9b1dSJustin T. Gibbs 				  lun_id_t lun_id);
8038b8a9b1dSJustin T. Gibbs static void	 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
8048b8a9b1dSJustin T. Gibbs 				    struct cam_ed *device);
8058b8a9b1dSJustin T. Gibbs static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
8068b8a9b1dSJustin T. Gibbs static struct cam_eb*
8078b8a9b1dSJustin T. Gibbs 		 xpt_find_bus(path_id_t path_id);
8088b8a9b1dSJustin T. Gibbs static struct cam_et*
8098b8a9b1dSJustin T. Gibbs 		 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
8108b8a9b1dSJustin T. Gibbs static struct cam_ed*
8118b8a9b1dSJustin T. Gibbs 		 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
8128b8a9b1dSJustin T. Gibbs static void	 xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
8138b8a9b1dSJustin T. Gibbs static void	 xpt_scan_lun(struct cam_periph *periph,
8148b8a9b1dSJustin T. Gibbs 			      struct cam_path *path, cam_flags flags,
8158b8a9b1dSJustin T. Gibbs 			      union ccb *ccb);
8168b8a9b1dSJustin T. Gibbs static void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
8178b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptconfigbuscountfunc;
8188b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptconfigfunc;
8198b8a9b1dSJustin T. Gibbs static void	 xpt_config(void *arg);
8208b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t xptpassannouncefunc;
8218b8a9b1dSJustin T. Gibbs static void	 xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
8228b8a9b1dSJustin T. Gibbs static void	 xptaction(struct cam_sim *sim, union ccb *work_ccb);
823434bbf6eSJustin T. Gibbs static void	 xptpoll(struct cam_sim *sim);
8248088699fSJohn Baldwin static void	 camisr(void *);
8259758cc83SScott Long static void	 camisr_runqueue(void *);
8268b8a9b1dSJustin T. Gibbs static dev_match_ret	xptbusmatch(struct dev_match_pattern *patterns,
8273393f8daSKenneth D. Merry 				    u_int num_patterns, struct cam_eb *bus);
8288b8a9b1dSJustin T. Gibbs static dev_match_ret	xptdevicematch(struct dev_match_pattern *patterns,
8293393f8daSKenneth D. Merry 				       u_int num_patterns,
8303393f8daSKenneth D. Merry 				       struct cam_ed *device);
8318b8a9b1dSJustin T. Gibbs static dev_match_ret	xptperiphmatch(struct dev_match_pattern *patterns,
8323393f8daSKenneth D. Merry 				       u_int num_patterns,
8338b8a9b1dSJustin T. Gibbs 				       struct cam_periph *periph);
8348b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptedtbusfunc;
8358b8a9b1dSJustin T. Gibbs static xpt_targetfunc_t	xptedttargetfunc;
8368b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptedtdevicefunc;
8378b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptedtperiphfunc;
8388b8a9b1dSJustin T. Gibbs static xpt_pdrvfunc_t	xptplistpdrvfunc;
8398b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptplistperiphfunc;
8408b8a9b1dSJustin T. Gibbs static int		xptedtmatch(struct ccb_dev_match *cdm);
8418b8a9b1dSJustin T. Gibbs static int		xptperiphlistmatch(struct ccb_dev_match *cdm);
8428b8a9b1dSJustin T. Gibbs static int		xptbustraverse(struct cam_eb *start_bus,
8438b8a9b1dSJustin T. Gibbs 				       xpt_busfunc_t *tr_func, void *arg);
8448b8a9b1dSJustin T. Gibbs static int		xpttargettraverse(struct cam_eb *bus,
8458b8a9b1dSJustin T. Gibbs 					  struct cam_et *start_target,
8468b8a9b1dSJustin T. Gibbs 					  xpt_targetfunc_t *tr_func, void *arg);
8478b8a9b1dSJustin T. Gibbs static int		xptdevicetraverse(struct cam_et *target,
8488b8a9b1dSJustin T. Gibbs 					  struct cam_ed *start_device,
8498b8a9b1dSJustin T. Gibbs 					  xpt_devicefunc_t *tr_func, void *arg);
8508b8a9b1dSJustin T. Gibbs static int		xptperiphtraverse(struct cam_ed *device,
8518b8a9b1dSJustin T. Gibbs 					  struct cam_periph *start_periph,
8528b8a9b1dSJustin T. Gibbs 					  xpt_periphfunc_t *tr_func, void *arg);
8538b8a9b1dSJustin T. Gibbs static int		xptpdrvtraverse(struct periph_driver **start_pdrv,
8548b8a9b1dSJustin T. Gibbs 					xpt_pdrvfunc_t *tr_func, void *arg);
8558b8a9b1dSJustin T. Gibbs static int		xptpdperiphtraverse(struct periph_driver **pdrv,
8568b8a9b1dSJustin T. Gibbs 					    struct cam_periph *start_periph,
8578b8a9b1dSJustin T. Gibbs 					    xpt_periphfunc_t *tr_func,
8588b8a9b1dSJustin T. Gibbs 					    void *arg);
8598b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptdefbusfunc;
8608b8a9b1dSJustin T. Gibbs static xpt_targetfunc_t	xptdeftargetfunc;
8618b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptdefdevicefunc;
8628b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptdefperiphfunc;
8638b8a9b1dSJustin T. Gibbs static int		xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
8648b8a9b1dSJustin T. Gibbs static int		xpt_for_all_devices(xpt_devicefunc_t *tr_func,
8658b8a9b1dSJustin T. Gibbs 					    void *arg);
8668b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptsetasyncfunc;
8678b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptsetasyncbusfunc;
8688b8a9b1dSJustin T. Gibbs static cam_status	xptregister(struct cam_periph *periph,
8698b8a9b1dSJustin T. Gibbs 				    void *arg);
8708b8a9b1dSJustin T. Gibbs static cam_status	proberegister(struct cam_periph *periph,
8718b8a9b1dSJustin T. Gibbs 				      void *arg);
8728b8a9b1dSJustin T. Gibbs static void	 probeschedule(struct cam_periph *probe_periph);
8738b8a9b1dSJustin T. Gibbs static void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
87487cfaf0eSJustin T. Gibbs static void	 proberequestdefaultnegotiation(struct cam_periph *periph);
8752afca7acSMatt Jacob static int       proberequestbackoff(struct cam_periph *periph,
8762afca7acSMatt Jacob 				     struct cam_ed *device);
8778b8a9b1dSJustin T. Gibbs static void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
8788b8a9b1dSJustin T. Gibbs static void	 probecleanup(struct cam_periph *periph);
8798b8a9b1dSJustin T. Gibbs static void	 xpt_find_quirk(struct cam_ed *device);
8803393f8daSKenneth D. Merry static void	 xpt_devise_transport(struct cam_path *path);
8818b8a9b1dSJustin T. Gibbs static void	 xpt_set_transfer_settings(struct ccb_trans_settings *cts,
88203e3511bSJustin T. Gibbs 					   struct cam_ed *device,
8838b8a9b1dSJustin T. Gibbs 					   int async_update);
884f0adc790SJustin T. Gibbs static void	 xpt_toggle_tags(struct cam_path *path);
885fd21cc5eSJustin T. Gibbs static void	 xpt_start_tags(struct cam_path *path);
8868b8a9b1dSJustin T. Gibbs static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
8878b8a9b1dSJustin T. Gibbs 					    struct cam_ed *dev);
8888b8a9b1dSJustin T. Gibbs static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
8898b8a9b1dSJustin T. Gibbs 					   struct cam_ed *dev);
8908b8a9b1dSJustin T. Gibbs static __inline int periph_is_queued(struct cam_periph *periph);
8918b8a9b1dSJustin T. Gibbs static __inline int device_is_alloc_queued(struct cam_ed *device);
8928b8a9b1dSJustin T. Gibbs static __inline int device_is_send_queued(struct cam_ed *device);
8938b8a9b1dSJustin T. Gibbs static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
8948b8a9b1dSJustin T. Gibbs 
8958b8a9b1dSJustin T. Gibbs static __inline int
8968b8a9b1dSJustin T. Gibbs xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
8978b8a9b1dSJustin T. Gibbs {
8988b8a9b1dSJustin T. Gibbs 	int retval;
8998b8a9b1dSJustin T. Gibbs 
9008b8a9b1dSJustin T. Gibbs 	if (dev->ccbq.devq_openings > 0) {
9018b8a9b1dSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
9028b8a9b1dSJustin T. Gibbs 			cam_ccbq_resize(&dev->ccbq,
9038b8a9b1dSJustin T. Gibbs 					dev->ccbq.dev_openings
9048b8a9b1dSJustin T. Gibbs 					+ dev->ccbq.dev_active);
9058b8a9b1dSJustin T. Gibbs 			dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
9068b8a9b1dSJustin T. Gibbs 		}
9075a526431SJustin T. Gibbs 		/*
9085a526431SJustin T. Gibbs 		 * The priority of a device waiting for CCB resources
9095a526431SJustin T. Gibbs 		 * is that of the the highest priority peripheral driver
9105a526431SJustin T. Gibbs 		 * enqueued.
9115a526431SJustin T. Gibbs 		 */
9128b8a9b1dSJustin T. Gibbs 		retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
9138b8a9b1dSJustin T. Gibbs 					  &dev->alloc_ccb_entry.pinfo,
9145a526431SJustin T. Gibbs 					  CAMQ_GET_HEAD(&dev->drvq)->priority);
9158b8a9b1dSJustin T. Gibbs 	} else {
9168b8a9b1dSJustin T. Gibbs 		retval = 0;
9178b8a9b1dSJustin T. Gibbs 	}
9188b8a9b1dSJustin T. Gibbs 
9198b8a9b1dSJustin T. Gibbs 	return (retval);
9208b8a9b1dSJustin T. Gibbs }
9218b8a9b1dSJustin T. Gibbs 
9228b8a9b1dSJustin T. Gibbs static __inline int
9238b8a9b1dSJustin T. Gibbs xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
9248b8a9b1dSJustin T. Gibbs {
9258b8a9b1dSJustin T. Gibbs 	int	retval;
9268b8a9b1dSJustin T. Gibbs 
9278b8a9b1dSJustin T. Gibbs 	if (dev->ccbq.dev_openings > 0) {
9285a526431SJustin T. Gibbs 		/*
9295a526431SJustin T. Gibbs 		 * The priority of a device waiting for controller
9305a526431SJustin T. Gibbs 		 * resources is that of the the highest priority CCB
9315a526431SJustin T. Gibbs 		 * enqueued.
9325a526431SJustin T. Gibbs 		 */
9335a526431SJustin T. Gibbs 		retval =
9345a526431SJustin T. Gibbs 		    xpt_schedule_dev(&bus->sim->devq->send_queue,
9358b8a9b1dSJustin T. Gibbs 				     &dev->send_ccb_entry.pinfo,
9365a526431SJustin T. Gibbs 				     CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
9378b8a9b1dSJustin T. Gibbs 	} else {
9388b8a9b1dSJustin T. Gibbs 		retval = 0;
9398b8a9b1dSJustin T. Gibbs 	}
9408b8a9b1dSJustin T. Gibbs 	return (retval);
9418b8a9b1dSJustin T. Gibbs }
9428b8a9b1dSJustin T. Gibbs 
9438b8a9b1dSJustin T. Gibbs static __inline int
9448b8a9b1dSJustin T. Gibbs periph_is_queued(struct cam_periph *periph)
9458b8a9b1dSJustin T. Gibbs {
9468b8a9b1dSJustin T. Gibbs 	return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
9478b8a9b1dSJustin T. Gibbs }
9488b8a9b1dSJustin T. Gibbs 
9498b8a9b1dSJustin T. Gibbs static __inline int
9508b8a9b1dSJustin T. Gibbs device_is_alloc_queued(struct cam_ed *device)
9518b8a9b1dSJustin T. Gibbs {
9528b8a9b1dSJustin T. Gibbs 	return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
9538b8a9b1dSJustin T. Gibbs }
9548b8a9b1dSJustin T. Gibbs 
9558b8a9b1dSJustin T. Gibbs static __inline int
9568b8a9b1dSJustin T. Gibbs device_is_send_queued(struct cam_ed *device)
9578b8a9b1dSJustin T. Gibbs {
9588b8a9b1dSJustin T. Gibbs 	return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
9598b8a9b1dSJustin T. Gibbs }
9608b8a9b1dSJustin T. Gibbs 
9618b8a9b1dSJustin T. Gibbs static __inline int
9628b8a9b1dSJustin T. Gibbs dev_allocq_is_runnable(struct cam_devq *devq)
9638b8a9b1dSJustin T. Gibbs {
9648b8a9b1dSJustin T. Gibbs 	/*
9658b8a9b1dSJustin T. Gibbs 	 * Have work to do.
9668b8a9b1dSJustin T. Gibbs 	 * Have space to do more work.
9678b8a9b1dSJustin T. Gibbs 	 * Allowed to do work.
9688b8a9b1dSJustin T. Gibbs 	 */
9698b8a9b1dSJustin T. Gibbs 	return ((devq->alloc_queue.qfrozen_cnt == 0)
9708b8a9b1dSJustin T. Gibbs 	     && (devq->alloc_queue.entries > 0)
9718b8a9b1dSJustin T. Gibbs 	     && (devq->alloc_openings > 0));
9728b8a9b1dSJustin T. Gibbs }
9738b8a9b1dSJustin T. Gibbs 
9748b8a9b1dSJustin T. Gibbs static void
9758b8a9b1dSJustin T. Gibbs xpt_periph_init()
9768b8a9b1dSJustin T. Gibbs {
97773d26919SKenneth D. Merry 	make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
9788b8a9b1dSJustin T. Gibbs }
9798b8a9b1dSJustin T. Gibbs 
9808b8a9b1dSJustin T. Gibbs static void
9818b8a9b1dSJustin T. Gibbs probe_periph_init()
9828b8a9b1dSJustin T. Gibbs {
9838b8a9b1dSJustin T. Gibbs }
9848b8a9b1dSJustin T. Gibbs 
9858b8a9b1dSJustin T. Gibbs 
9868b8a9b1dSJustin T. Gibbs static void
9878b8a9b1dSJustin T. Gibbs xptdone(struct cam_periph *periph, union ccb *done_ccb)
9888b8a9b1dSJustin T. Gibbs {
9898b8a9b1dSJustin T. Gibbs 	/* Caller will release the CCB */
9908b8a9b1dSJustin T. Gibbs 	wakeup(&done_ccb->ccb_h.cbfcnp);
9918b8a9b1dSJustin T. Gibbs }
9928b8a9b1dSJustin T. Gibbs 
9938b8a9b1dSJustin T. Gibbs static int
99489c9c53dSPoul-Henning Kamp xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
9958b8a9b1dSJustin T. Gibbs {
9968b8a9b1dSJustin T. Gibbs 
9978b8a9b1dSJustin T. Gibbs 	/*
99866a0780eSKenneth D. Merry 	 * Only allow read-write access.
99966a0780eSKenneth D. Merry 	 */
100066a0780eSKenneth D. Merry 	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
100166a0780eSKenneth D. Merry 		return(EPERM);
100266a0780eSKenneth D. Merry 
100366a0780eSKenneth D. Merry 	/*
10048b8a9b1dSJustin T. Gibbs 	 * We don't allow nonblocking access.
10058b8a9b1dSJustin T. Gibbs 	 */
10068b8a9b1dSJustin T. Gibbs 	if ((flags & O_NONBLOCK) != 0) {
10072b83592fSScott Long 		printf("%s: can't do nonblocking access\n", devtoname(dev));
10088b8a9b1dSJustin T. Gibbs 		return(ENODEV);
10098b8a9b1dSJustin T. Gibbs 	}
10108b8a9b1dSJustin T. Gibbs 
10118b8a9b1dSJustin T. Gibbs 	/* Mark ourselves open */
10122b83592fSScott Long 	mtx_lock(&xsoftc.xpt_lock);
10138b8a9b1dSJustin T. Gibbs 	xsoftc.flags |= XPT_FLAG_OPEN;
10142b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_lock);
10158b8a9b1dSJustin T. Gibbs 
10168b8a9b1dSJustin T. Gibbs 	return(0);
10178b8a9b1dSJustin T. Gibbs }
10188b8a9b1dSJustin T. Gibbs 
10198b8a9b1dSJustin T. Gibbs static int
102089c9c53dSPoul-Henning Kamp xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
10218b8a9b1dSJustin T. Gibbs {
10228b8a9b1dSJustin T. Gibbs 
10238b8a9b1dSJustin T. Gibbs 	/* Mark ourselves closed */
10242b83592fSScott Long 	mtx_lock(&xsoftc.xpt_lock);
10258b8a9b1dSJustin T. Gibbs 	xsoftc.flags &= ~XPT_FLAG_OPEN;
10262b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_lock);
10278b8a9b1dSJustin T. Gibbs 
10288b8a9b1dSJustin T. Gibbs 	return(0);
10298b8a9b1dSJustin T. Gibbs }
10308b8a9b1dSJustin T. Gibbs 
10312b83592fSScott Long /*
10322b83592fSScott Long  * Don't automatically grab the xpt softc lock here even though this is going
10332b83592fSScott Long  * through the xpt device.  The xpt device is really just a back door for
10342b83592fSScott Long  * accessing other devices and SIMs, so the right thing to do is to grab
10352b83592fSScott Long  * the appropriate SIM lock once the bus/SIM is located.
10362b83592fSScott Long  */
10378b8a9b1dSJustin T. Gibbs static int
103889c9c53dSPoul-Henning Kamp xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
10398b8a9b1dSJustin T. Gibbs {
10402b83592fSScott Long 	int error;
10418b8a9b1dSJustin T. Gibbs 
10428b8a9b1dSJustin T. Gibbs 	error = 0;
10438b8a9b1dSJustin T. Gibbs 
10448b8a9b1dSJustin T. Gibbs 	switch(cmd) {
10458b8a9b1dSJustin T. Gibbs 	/*
10468b8a9b1dSJustin T. Gibbs 	 * For the transport layer CAMIOCOMMAND ioctl, we really only want
10478b8a9b1dSJustin T. Gibbs 	 * to accept CCB types that don't quite make sense to send through a
10488c7a96c5SScott Long 	 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
10498c7a96c5SScott Long 	 * in the CAM spec.
10508b8a9b1dSJustin T. Gibbs 	 */
10518b8a9b1dSJustin T. Gibbs 	case CAMIOCOMMAND: {
10528b8a9b1dSJustin T. Gibbs 		union ccb *ccb;
10538b8a9b1dSJustin T. Gibbs 		union ccb *inccb;
10542b83592fSScott Long 		struct cam_eb *bus;
10558b8a9b1dSJustin T. Gibbs 
10568b8a9b1dSJustin T. Gibbs 		inccb = (union ccb *)addr;
10578b8a9b1dSJustin T. Gibbs 
10582b83592fSScott Long 		bus = xpt_find_bus(inccb->ccb_h.path_id);
10592b83592fSScott Long 		if (bus == NULL) {
10602b83592fSScott Long 			error = EINVAL;
10612b83592fSScott Long 			break;
10622b83592fSScott Long 		}
10632b83592fSScott Long 
10648b8a9b1dSJustin T. Gibbs 		switch(inccb->ccb_h.func_code) {
10658b8a9b1dSJustin T. Gibbs 		case XPT_SCAN_BUS:
10668b8a9b1dSJustin T. Gibbs 		case XPT_RESET_BUS:
10678b8a9b1dSJustin T. Gibbs 			if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
10688b8a9b1dSJustin T. Gibbs 			 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
10698b8a9b1dSJustin T. Gibbs 				error = EINVAL;
10708b8a9b1dSJustin T. Gibbs 				break;
10718b8a9b1dSJustin T. Gibbs 			}
10728b8a9b1dSJustin T. Gibbs 			/* FALLTHROUGH */
10738c7a96c5SScott Long 		case XPT_PATH_INQ:
10748fcf57f5SJustin T. Gibbs 		case XPT_ENG_INQ:
10758b8a9b1dSJustin T. Gibbs 		case XPT_SCAN_LUN:
10768b8a9b1dSJustin T. Gibbs 
10778008a935SScott Long 			ccb = xpt_alloc_ccb();
10782b83592fSScott Long 
10792b83592fSScott Long 			CAM_SIM_LOCK(bus->sim);
10808b8a9b1dSJustin T. Gibbs 
10818b8a9b1dSJustin T. Gibbs 			/*
10828b8a9b1dSJustin T. Gibbs 			 * Create a path using the bus, target, and lun the
10838b8a9b1dSJustin T. Gibbs 			 * user passed in.
10848b8a9b1dSJustin T. Gibbs 			 */
10858b8a9b1dSJustin T. Gibbs 			if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
10868b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.path_id,
10878b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_id,
10888b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_lun) !=
10898b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP){
10908b8a9b1dSJustin T. Gibbs 				error = EINVAL;
10912b83592fSScott Long 				CAM_SIM_UNLOCK(bus->sim);
10928b8a9b1dSJustin T. Gibbs 				xpt_free_ccb(ccb);
10938b8a9b1dSJustin T. Gibbs 				break;
10948b8a9b1dSJustin T. Gibbs 			}
10958b8a9b1dSJustin T. Gibbs 			/* Ensure all of our fields are correct */
10968b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
10978b8a9b1dSJustin T. Gibbs 				      inccb->ccb_h.pinfo.priority);
10988b8a9b1dSJustin T. Gibbs 			xpt_merge_ccb(ccb, inccb);
10998b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.cbfcnp = xptdone;
11008b8a9b1dSJustin T. Gibbs 			cam_periph_runccb(ccb, NULL, 0, 0, NULL);
11018b8a9b1dSJustin T. Gibbs 			bcopy(ccb, inccb, sizeof(union ccb));
11028b8a9b1dSJustin T. Gibbs 			xpt_free_path(ccb->ccb_h.path);
11038b8a9b1dSJustin T. Gibbs 			xpt_free_ccb(ccb);
11042b83592fSScott Long 			CAM_SIM_UNLOCK(bus->sim);
11058b8a9b1dSJustin T. Gibbs 			break;
11068b8a9b1dSJustin T. Gibbs 
11078b8a9b1dSJustin T. Gibbs 		case XPT_DEBUG: {
11088b8a9b1dSJustin T. Gibbs 			union ccb ccb;
11098b8a9b1dSJustin T. Gibbs 
11108b8a9b1dSJustin T. Gibbs 			/*
1111aa872be6SMatt Jacob 			 * This is an immediate CCB, so it's okay to
11128b8a9b1dSJustin T. Gibbs 			 * allocate it on the stack.
11138b8a9b1dSJustin T. Gibbs 			 */
11148b8a9b1dSJustin T. Gibbs 
11152b83592fSScott Long 			CAM_SIM_LOCK(bus->sim);
11162b83592fSScott Long 
11178b8a9b1dSJustin T. Gibbs 			/*
11188b8a9b1dSJustin T. Gibbs 			 * Create a path using the bus, target, and lun the
11198b8a9b1dSJustin T. Gibbs 			 * user passed in.
11208b8a9b1dSJustin T. Gibbs 			 */
11218b8a9b1dSJustin T. Gibbs 			if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
11228b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.path_id,
11238b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_id,
11248b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_lun) !=
11258b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP){
11268b8a9b1dSJustin T. Gibbs 				error = EINVAL;
11278b8a9b1dSJustin T. Gibbs 				break;
11288b8a9b1dSJustin T. Gibbs 			}
11298b8a9b1dSJustin T. Gibbs 			/* Ensure all of our fields are correct */
11308b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
11318b8a9b1dSJustin T. Gibbs 				      inccb->ccb_h.pinfo.priority);
11328b8a9b1dSJustin T. Gibbs 			xpt_merge_ccb(&ccb, inccb);
11338b8a9b1dSJustin T. Gibbs 			ccb.ccb_h.cbfcnp = xptdone;
11348b8a9b1dSJustin T. Gibbs 			xpt_action(&ccb);
11352b83592fSScott Long 			CAM_SIM_UNLOCK(bus->sim);
11368b8a9b1dSJustin T. Gibbs 			bcopy(&ccb, inccb, sizeof(union ccb));
11378b8a9b1dSJustin T. Gibbs 			xpt_free_path(ccb.ccb_h.path);
11388b8a9b1dSJustin T. Gibbs 			break;
11398b8a9b1dSJustin T. Gibbs 
11408b8a9b1dSJustin T. Gibbs 		}
11418b8a9b1dSJustin T. Gibbs 		case XPT_DEV_MATCH: {
11428b8a9b1dSJustin T. Gibbs 			struct cam_periph_map_info mapinfo;
114359190eaaSKenneth D. Merry 			struct cam_path *old_path;
11448b8a9b1dSJustin T. Gibbs 
11458b8a9b1dSJustin T. Gibbs 			/*
11468b8a9b1dSJustin T. Gibbs 			 * We can't deal with physical addresses for this
11478b8a9b1dSJustin T. Gibbs 			 * type of transaction.
11488b8a9b1dSJustin T. Gibbs 			 */
11498b8a9b1dSJustin T. Gibbs 			if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
11508b8a9b1dSJustin T. Gibbs 				error = EINVAL;
11518b8a9b1dSJustin T. Gibbs 				break;
11528b8a9b1dSJustin T. Gibbs 			}
115359190eaaSKenneth D. Merry 
115459190eaaSKenneth D. Merry 			/*
115559190eaaSKenneth D. Merry 			 * Save this in case the caller had it set to
115659190eaaSKenneth D. Merry 			 * something in particular.
115759190eaaSKenneth D. Merry 			 */
115859190eaaSKenneth D. Merry 			old_path = inccb->ccb_h.path;
115959190eaaSKenneth D. Merry 
116059190eaaSKenneth D. Merry 			/*
116159190eaaSKenneth D. Merry 			 * We really don't need a path for the matching
116259190eaaSKenneth D. Merry 			 * code.  The path is needed because of the
116359190eaaSKenneth D. Merry 			 * debugging statements in xpt_action().  They
116459190eaaSKenneth D. Merry 			 * assume that the CCB has a valid path.
116559190eaaSKenneth D. Merry 			 */
116659190eaaSKenneth D. Merry 			inccb->ccb_h.path = xpt_periph->path;
116759190eaaSKenneth D. Merry 
11688b8a9b1dSJustin T. Gibbs 			bzero(&mapinfo, sizeof(mapinfo));
11698b8a9b1dSJustin T. Gibbs 
11708b8a9b1dSJustin T. Gibbs 			/*
11718b8a9b1dSJustin T. Gibbs 			 * Map the pattern and match buffers into kernel
11728b8a9b1dSJustin T. Gibbs 			 * virtual address space.
11738b8a9b1dSJustin T. Gibbs 			 */
11748b8a9b1dSJustin T. Gibbs 			error = cam_periph_mapmem(inccb, &mapinfo);
11758b8a9b1dSJustin T. Gibbs 
117659190eaaSKenneth D. Merry 			if (error) {
117759190eaaSKenneth D. Merry 				inccb->ccb_h.path = old_path;
11788b8a9b1dSJustin T. Gibbs 				break;
117959190eaaSKenneth D. Merry 			}
11808b8a9b1dSJustin T. Gibbs 
11818b8a9b1dSJustin T. Gibbs 			/*
11828b8a9b1dSJustin T. Gibbs 			 * This is an immediate CCB, we can send it on directly.
11838b8a9b1dSJustin T. Gibbs 			 */
11848b8a9b1dSJustin T. Gibbs 			xpt_action(inccb);
11858b8a9b1dSJustin T. Gibbs 
11868b8a9b1dSJustin T. Gibbs 			/*
11878b8a9b1dSJustin T. Gibbs 			 * Map the buffers back into user space.
11888b8a9b1dSJustin T. Gibbs 			 */
11898b8a9b1dSJustin T. Gibbs 			cam_periph_unmapmem(inccb, &mapinfo);
11908b8a9b1dSJustin T. Gibbs 
119159190eaaSKenneth D. Merry 			inccb->ccb_h.path = old_path;
119259190eaaSKenneth D. Merry 
11938b8a9b1dSJustin T. Gibbs 			error = 0;
11948b8a9b1dSJustin T. Gibbs 			break;
11958b8a9b1dSJustin T. Gibbs 		}
11968b8a9b1dSJustin T. Gibbs 		default:
11978fcf57f5SJustin T. Gibbs 			error = ENOTSUP;
11988b8a9b1dSJustin T. Gibbs 			break;
11998b8a9b1dSJustin T. Gibbs 		}
12008b8a9b1dSJustin T. Gibbs 		break;
12018b8a9b1dSJustin T. Gibbs 	}
12028b8a9b1dSJustin T. Gibbs 	/*
12038b8a9b1dSJustin T. Gibbs 	 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
12048b8a9b1dSJustin T. Gibbs 	 * with the periphal driver name and unit name filled in.  The other
12058b8a9b1dSJustin T. Gibbs 	 * fields don't really matter as input.  The passthrough driver name
12068b8a9b1dSJustin T. Gibbs 	 * ("pass"), and unit number are passed back in the ccb.  The current
12078b8a9b1dSJustin T. Gibbs 	 * device generation number, and the index into the device peripheral
12088b8a9b1dSJustin T. Gibbs 	 * driver list, and the status are also passed back.  Note that
12098b8a9b1dSJustin T. Gibbs 	 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
12108b8a9b1dSJustin T. Gibbs 	 * we never return a status of CAM_GDEVLIST_LIST_CHANGED.  It is
12118b8a9b1dSJustin T. Gibbs 	 * (or rather should be) impossible for the device peripheral driver
12128b8a9b1dSJustin T. Gibbs 	 * list to change since we look at the whole thing in one pass, and
121377dc25ccSScott Long 	 * we do it with lock protection.
12148b8a9b1dSJustin T. Gibbs 	 *
12158b8a9b1dSJustin T. Gibbs 	 */
12168b8a9b1dSJustin T. Gibbs 	case CAMGETPASSTHRU: {
12178b8a9b1dSJustin T. Gibbs 		union ccb *ccb;
12188b8a9b1dSJustin T. Gibbs 		struct cam_periph *periph;
12198b8a9b1dSJustin T. Gibbs 		struct periph_driver **p_drv;
12208b8a9b1dSJustin T. Gibbs 		char   *name;
12213393f8daSKenneth D. Merry 		u_int unit;
12223393f8daSKenneth D. Merry 		u_int cur_generation;
1223621a60d4SKenneth D. Merry 		int base_periph_found;
12248b8a9b1dSJustin T. Gibbs 		int splbreaknum;
12258b8a9b1dSJustin T. Gibbs 
12268b8a9b1dSJustin T. Gibbs 		ccb = (union ccb *)addr;
12278b8a9b1dSJustin T. Gibbs 		unit = ccb->cgdl.unit_number;
12288b8a9b1dSJustin T. Gibbs 		name = ccb->cgdl.periph_name;
12298b8a9b1dSJustin T. Gibbs 		/*
123077dc25ccSScott Long 		 * Every 100 devices, we want to drop our lock protection to
12318b8a9b1dSJustin T. Gibbs 		 * give the software interrupt handler a chance to run.
12328b8a9b1dSJustin T. Gibbs 		 * Most systems won't run into this check, but this should
12338b8a9b1dSJustin T. Gibbs 		 * avoid starvation in the software interrupt handler in
12348b8a9b1dSJustin T. Gibbs 		 * large systems.
12358b8a9b1dSJustin T. Gibbs 		 */
12368b8a9b1dSJustin T. Gibbs 		splbreaknum = 100;
12378b8a9b1dSJustin T. Gibbs 
12388b8a9b1dSJustin T. Gibbs 		ccb = (union ccb *)addr;
12398b8a9b1dSJustin T. Gibbs 
1240621a60d4SKenneth D. Merry 		base_periph_found = 0;
1241621a60d4SKenneth D. Merry 
12428b8a9b1dSJustin T. Gibbs 		/*
12438b8a9b1dSJustin T. Gibbs 		 * Sanity check -- make sure we don't get a null peripheral
12448b8a9b1dSJustin T. Gibbs 		 * driver name.
12458b8a9b1dSJustin T. Gibbs 		 */
12468b8a9b1dSJustin T. Gibbs 		if (*ccb->cgdl.periph_name == '\0') {
12478b8a9b1dSJustin T. Gibbs 			error = EINVAL;
12488b8a9b1dSJustin T. Gibbs 			break;
12498b8a9b1dSJustin T. Gibbs 		}
12508b8a9b1dSJustin T. Gibbs 
12518b8a9b1dSJustin T. Gibbs 		/* Keep the list from changing while we traverse it */
125277dc25ccSScott Long 		mtx_lock(&xsoftc.xpt_topo_lock);
12538b8a9b1dSJustin T. Gibbs ptstartover:
12542b83592fSScott Long 		cur_generation = xsoftc.xpt_generation;
12558b8a9b1dSJustin T. Gibbs 
12568b8a9b1dSJustin T. Gibbs 		/* first find our driver in the list of drivers */
12570b7c27b9SPeter Wemm 		for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
12588b8a9b1dSJustin T. Gibbs 			if (strcmp((*p_drv)->driver_name, name) == 0)
12598b8a9b1dSJustin T. Gibbs 				break;
12608b8a9b1dSJustin T. Gibbs 
12618b8a9b1dSJustin T. Gibbs 		if (*p_drv == NULL) {
126277dc25ccSScott Long 			mtx_unlock(&xsoftc.xpt_topo_lock);
12638b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
12648b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
12658b8a9b1dSJustin T. Gibbs 			*ccb->cgdl.periph_name = '\0';
12668b8a9b1dSJustin T. Gibbs 			ccb->cgdl.unit_number = 0;
12678b8a9b1dSJustin T. Gibbs 			error = ENOENT;
12688b8a9b1dSJustin T. Gibbs 			break;
12698b8a9b1dSJustin T. Gibbs 		}
12708b8a9b1dSJustin T. Gibbs 
12718b8a9b1dSJustin T. Gibbs 		/*
12728b8a9b1dSJustin T. Gibbs 		 * Run through every peripheral instance of this driver
12738b8a9b1dSJustin T. Gibbs 		 * and check to see whether it matches the unit passed
12748b8a9b1dSJustin T. Gibbs 		 * in by the user.  If it does, get out of the loops and
12758b8a9b1dSJustin T. Gibbs 		 * find the passthrough driver associated with that
12768b8a9b1dSJustin T. Gibbs 		 * peripheral driver.
12778b8a9b1dSJustin T. Gibbs 		 */
12788b8a9b1dSJustin T. Gibbs 		for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
12798b8a9b1dSJustin T. Gibbs 		     periph = TAILQ_NEXT(periph, unit_links)) {
12808b8a9b1dSJustin T. Gibbs 
12818b8a9b1dSJustin T. Gibbs 			if (periph->unit_number == unit) {
12828b8a9b1dSJustin T. Gibbs 				break;
12838b8a9b1dSJustin T. Gibbs 			} else if (--splbreaknum == 0) {
128477dc25ccSScott Long 				mtx_unlock(&xsoftc.xpt_topo_lock);
128577dc25ccSScott Long 				mtx_lock(&xsoftc.xpt_topo_lock);
12868b8a9b1dSJustin T. Gibbs 				splbreaknum = 100;
12872b83592fSScott Long 				if (cur_generation != xsoftc.xpt_generation)
12888b8a9b1dSJustin T. Gibbs 				       goto ptstartover;
12898b8a9b1dSJustin T. Gibbs 			}
12908b8a9b1dSJustin T. Gibbs 		}
12918b8a9b1dSJustin T. Gibbs 		/*
12928b8a9b1dSJustin T. Gibbs 		 * If we found the peripheral driver that the user passed
12938b8a9b1dSJustin T. Gibbs 		 * in, go through all of the peripheral drivers for that
12948b8a9b1dSJustin T. Gibbs 		 * particular device and look for a passthrough driver.
12958b8a9b1dSJustin T. Gibbs 		 */
12968b8a9b1dSJustin T. Gibbs 		if (periph != NULL) {
12978b8a9b1dSJustin T. Gibbs 			struct cam_ed *device;
12988b8a9b1dSJustin T. Gibbs 			int i;
12998b8a9b1dSJustin T. Gibbs 
1300621a60d4SKenneth D. Merry 			base_periph_found = 1;
13018b8a9b1dSJustin T. Gibbs 			device = periph->path->device;
1302fc2ffbe6SPoul-Henning Kamp 			for (i = 0, periph = SLIST_FIRST(&device->periphs);
13038b8a9b1dSJustin T. Gibbs 			     periph != NULL;
1304fc2ffbe6SPoul-Henning Kamp 			     periph = SLIST_NEXT(periph, periph_links), i++) {
13058b8a9b1dSJustin T. Gibbs 				/*
13068b8a9b1dSJustin T. Gibbs 				 * Check to see whether we have a
13078b8a9b1dSJustin T. Gibbs 				 * passthrough device or not.
13088b8a9b1dSJustin T. Gibbs 				 */
13098b8a9b1dSJustin T. Gibbs 				if (strcmp(periph->periph_name, "pass") == 0) {
13108b8a9b1dSJustin T. Gibbs 					/*
13118b8a9b1dSJustin T. Gibbs 					 * Fill in the getdevlist fields.
13128b8a9b1dSJustin T. Gibbs 					 */
13138b8a9b1dSJustin T. Gibbs 					strcpy(ccb->cgdl.periph_name,
13148b8a9b1dSJustin T. Gibbs 					       periph->periph_name);
13158b8a9b1dSJustin T. Gibbs 					ccb->cgdl.unit_number =
13168b8a9b1dSJustin T. Gibbs 						periph->unit_number;
1317fc2ffbe6SPoul-Henning Kamp 					if (SLIST_NEXT(periph, periph_links))
13188b8a9b1dSJustin T. Gibbs 						ccb->cgdl.status =
13198b8a9b1dSJustin T. Gibbs 							CAM_GDEVLIST_MORE_DEVS;
13208b8a9b1dSJustin T. Gibbs 					else
13218b8a9b1dSJustin T. Gibbs 						ccb->cgdl.status =
13228b8a9b1dSJustin T. Gibbs 						       CAM_GDEVLIST_LAST_DEVICE;
13238b8a9b1dSJustin T. Gibbs 					ccb->cgdl.generation =
13248b8a9b1dSJustin T. Gibbs 						device->generation;
13258b8a9b1dSJustin T. Gibbs 					ccb->cgdl.index = i;
13268b8a9b1dSJustin T. Gibbs 					/*
13278b8a9b1dSJustin T. Gibbs 					 * Fill in some CCB header fields
13288b8a9b1dSJustin T. Gibbs 					 * that the user may want.
13298b8a9b1dSJustin T. Gibbs 					 */
13308b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.path_id =
13318b8a9b1dSJustin T. Gibbs 						periph->path->bus->path_id;
13328b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.target_id =
13338b8a9b1dSJustin T. Gibbs 						periph->path->target->target_id;
13348b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.target_lun =
13358b8a9b1dSJustin T. Gibbs 						periph->path->device->lun_id;
13368b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.status = CAM_REQ_CMP;
13378b8a9b1dSJustin T. Gibbs 					break;
13388b8a9b1dSJustin T. Gibbs 				}
13398b8a9b1dSJustin T. Gibbs 			}
13408b8a9b1dSJustin T. Gibbs 		}
13418b8a9b1dSJustin T. Gibbs 
13428b8a9b1dSJustin T. Gibbs 		/*
13438b8a9b1dSJustin T. Gibbs 		 * If the periph is null here, one of two things has
13448b8a9b1dSJustin T. Gibbs 		 * happened.  The first possibility is that we couldn't
13458b8a9b1dSJustin T. Gibbs 		 * find the unit number of the particular peripheral driver
13468b8a9b1dSJustin T. Gibbs 		 * that the user is asking about.  e.g. the user asks for
13478b8a9b1dSJustin T. Gibbs 		 * the passthrough driver for "da11".  We find the list of
13488b8a9b1dSJustin T. Gibbs 		 * "da" peripherals all right, but there is no unit 11.
13498b8a9b1dSJustin T. Gibbs 		 * The other possibility is that we went through the list
13508b8a9b1dSJustin T. Gibbs 		 * of peripheral drivers attached to the device structure,
13518b8a9b1dSJustin T. Gibbs 		 * but didn't find one with the name "pass".  Either way,
13528b8a9b1dSJustin T. Gibbs 		 * we return ENOENT, since we couldn't find something.
13538b8a9b1dSJustin T. Gibbs 		 */
13548b8a9b1dSJustin T. Gibbs 		if (periph == NULL) {
13558b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
13568b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
13578b8a9b1dSJustin T. Gibbs 			*ccb->cgdl.periph_name = '\0';
13588b8a9b1dSJustin T. Gibbs 			ccb->cgdl.unit_number = 0;
13598b8a9b1dSJustin T. Gibbs 			error = ENOENT;
1360621a60d4SKenneth D. Merry 			/*
1361621a60d4SKenneth D. Merry 			 * It is unfortunate that this is even necessary,
1362621a60d4SKenneth D. Merry 			 * but there are many, many clueless users out there.
1363621a60d4SKenneth D. Merry 			 * If this is true, the user is looking for the
1364621a60d4SKenneth D. Merry 			 * passthrough driver, but doesn't have one in his
1365621a60d4SKenneth D. Merry 			 * kernel.
1366621a60d4SKenneth D. Merry 			 */
1367621a60d4SKenneth D. Merry 			if (base_periph_found == 1) {
1368621a60d4SKenneth D. Merry 				printf("xptioctl: pass driver is not in the "
1369621a60d4SKenneth D. Merry 				       "kernel\n");
1370621a60d4SKenneth D. Merry 				printf("xptioctl: put \"device pass0\" in "
1371621a60d4SKenneth D. Merry 				       "your kernel config file\n");
1372621a60d4SKenneth D. Merry 			}
13738b8a9b1dSJustin T. Gibbs 		}
137477dc25ccSScott Long 		mtx_unlock(&xsoftc.xpt_topo_lock);
13758b8a9b1dSJustin T. Gibbs 		break;
13768b8a9b1dSJustin T. Gibbs 		}
13778b8a9b1dSJustin T. Gibbs 	default:
13788b8a9b1dSJustin T. Gibbs 		error = ENOTTY;
13798b8a9b1dSJustin T. Gibbs 		break;
13808b8a9b1dSJustin T. Gibbs 	}
13818b8a9b1dSJustin T. Gibbs 
13828b8a9b1dSJustin T. Gibbs 	return(error);
13838b8a9b1dSJustin T. Gibbs }
13848b8a9b1dSJustin T. Gibbs 
138574bd1c10SNick Hibma static int
138674bd1c10SNick Hibma cam_module_event_handler(module_t mod, int what, void *arg)
138774bd1c10SNick Hibma {
13882b83592fSScott Long 	int error;
13892b83592fSScott Long 
13902b83592fSScott Long 	switch (what) {
13912b83592fSScott Long 	case MOD_LOAD:
13922b83592fSScott Long 		if ((error = xpt_init(NULL)) != 0)
13932b83592fSScott Long 			return (error);
13942b83592fSScott Long 		break;
13952b83592fSScott Long 	case MOD_UNLOAD:
139674bd1c10SNick Hibma 		return EBUSY;
13972b83592fSScott Long 	default:
13983e019deaSPoul-Henning Kamp 		return EOPNOTSUPP;
139974bd1c10SNick Hibma 	}
140074bd1c10SNick Hibma 
140174bd1c10SNick Hibma 	return 0;
140274bd1c10SNick Hibma }
140374bd1c10SNick Hibma 
14049e6461a2SMatt Jacob /* thread to handle bus rescans */
14059e6461a2SMatt Jacob static void
14069e6461a2SMatt Jacob xpt_scanner_thread(void *dummy)
14079e6461a2SMatt Jacob {
14082b83592fSScott Long 	cam_isrq_t	queue;
14099e6461a2SMatt Jacob 	union ccb	*ccb;
14102b83592fSScott Long 	struct cam_sim	*sim;
14112b83592fSScott Long 
14122b83592fSScott Long 	for (;;) {
14132b83592fSScott Long 		/*
14142b83592fSScott Long 		 * Wait for a rescan request to come in.  When it does, splice
14152b83592fSScott Long 		 * it onto a queue from local storage so that the xpt lock
14162b83592fSScott Long 		 * doesn't need to be held while the requests are being
14172b83592fSScott Long 		 * processed.
14182b83592fSScott Long 		 */
14192b83592fSScott Long 		xpt_lock_buses();
14202b83592fSScott Long 		msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
14212b83592fSScott Long 		    "ccb_scanq", 0);
14222b83592fSScott Long 		TAILQ_INIT(&queue);
14232b83592fSScott Long 		TAILQ_CONCAT(&queue, &xsoftc.ccb_scanq, sim_links.tqe);
14242b83592fSScott Long 		xpt_unlock_buses();
14252b83592fSScott Long 
14262b83592fSScott Long 		while ((ccb = (union ccb *)TAILQ_FIRST(&queue)) != NULL) {
14272b83592fSScott Long 			TAILQ_REMOVE(&queue, &ccb->ccb_h, sim_links.tqe);
14282b83592fSScott Long 
14292b83592fSScott Long 			sim = ccb->ccb_h.path->bus->sim;
143011e4faceSScott Long 			CAM_SIM_LOCK(sim);
14312b83592fSScott Long 
14329e6461a2SMatt Jacob 			ccb->ccb_h.func_code = XPT_SCAN_BUS;
14339e6461a2SMatt Jacob 			ccb->ccb_h.cbfcnp = xptdone;
14349e6461a2SMatt Jacob 			xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 5);
14359e6461a2SMatt Jacob 			cam_periph_runccb(ccb, NULL, 0, 0, NULL);
14369e6461a2SMatt Jacob 			xpt_free_path(ccb->ccb_h.path);
14379e6461a2SMatt Jacob 			xpt_free_ccb(ccb);
143811e4faceSScott Long 			CAM_SIM_UNLOCK(sim);
14399e6461a2SMatt Jacob 		}
14409e6461a2SMatt Jacob 	}
14419e6461a2SMatt Jacob }
14429e6461a2SMatt Jacob 
14439e6461a2SMatt Jacob void
14449e6461a2SMatt Jacob xpt_rescan(union ccb *ccb)
14459e6461a2SMatt Jacob {
14469e6461a2SMatt Jacob 	struct ccb_hdr *hdr;
14472b83592fSScott Long 
14489e6461a2SMatt Jacob 	/*
14499e6461a2SMatt Jacob 	 * Don't make duplicate entries for the same paths.
14509e6461a2SMatt Jacob 	 */
14512b83592fSScott Long 	xpt_lock_buses();
14522b83592fSScott Long 	TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
14539e6461a2SMatt Jacob 		if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
14542b83592fSScott Long 			xpt_unlock_buses();
14559e6461a2SMatt Jacob 			xpt_print(ccb->ccb_h.path, "rescan already queued\n");
14569e6461a2SMatt Jacob 			xpt_free_path(ccb->ccb_h.path);
14579e6461a2SMatt Jacob 			xpt_free_ccb(ccb);
14589e6461a2SMatt Jacob 			return;
14599e6461a2SMatt Jacob 		}
14609e6461a2SMatt Jacob 	}
14612b83592fSScott Long 	TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
14622b83592fSScott Long 	wakeup(&xsoftc.ccb_scanq);
14632b83592fSScott Long 	xpt_unlock_buses();
14649e6461a2SMatt Jacob }
14659e6461a2SMatt Jacob 
14668b8a9b1dSJustin T. Gibbs /* Functions accessed by the peripheral drivers */
14672b83592fSScott Long static int
14689e6461a2SMatt Jacob xpt_init(void *dummy)
14698b8a9b1dSJustin T. Gibbs {
14708b8a9b1dSJustin T. Gibbs 	struct cam_sim *xpt_sim;
14718b8a9b1dSJustin T. Gibbs 	struct cam_path *path;
1472434bbf6eSJustin T. Gibbs 	struct cam_devq *devq;
14738b8a9b1dSJustin T. Gibbs 	cam_status status;
14748b8a9b1dSJustin T. Gibbs 
14752b83592fSScott Long 	TAILQ_INIT(&xsoftc.xpt_busses);
14769758cc83SScott Long 	TAILQ_INIT(&cam_simq);
14772b83592fSScott Long 	TAILQ_INIT(&xsoftc.ccb_scanq);
14782b83592fSScott Long 	STAILQ_INIT(&xsoftc.highpowerq);
14792b83592fSScott Long 	xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
14808b8a9b1dSJustin T. Gibbs 
14819758cc83SScott Long 	mtx_init(&cam_simq_lock, "CAM SIMQ lock", NULL, MTX_DEF);
14822b83592fSScott Long 	mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
14832b83592fSScott Long 	mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF);
1484ef3cf714SScott Long 
14858b8a9b1dSJustin T. Gibbs 	/*
14868b8a9b1dSJustin T. Gibbs 	 * The xpt layer is, itself, the equivelent of a SIM.
14878b8a9b1dSJustin T. Gibbs 	 * Allow 16 ccbs in the ccb pool for it.  This should
14888b8a9b1dSJustin T. Gibbs 	 * give decent parallelism when we probe busses and
14898b8a9b1dSJustin T. Gibbs 	 * perform other XPT functions.
14908b8a9b1dSJustin T. Gibbs 	 */
1491434bbf6eSJustin T. Gibbs 	devq = cam_simq_alloc(16);
1492434bbf6eSJustin T. Gibbs 	xpt_sim = cam_sim_alloc(xptaction,
1493434bbf6eSJustin T. Gibbs 				xptpoll,
1494434bbf6eSJustin T. Gibbs 				"xpt",
1495434bbf6eSJustin T. Gibbs 				/*softc*/NULL,
1496434bbf6eSJustin T. Gibbs 				/*unit*/0,
14972b83592fSScott Long 				/*mtx*/&xsoftc.xpt_lock,
1498434bbf6eSJustin T. Gibbs 				/*max_dev_transactions*/0,
1499434bbf6eSJustin T. Gibbs 				/*max_tagged_dev_transactions*/0,
1500434bbf6eSJustin T. Gibbs 				devq);
15012b83592fSScott Long 	if (xpt_sim == NULL)
15022b83592fSScott Long 		return (ENOMEM);
15038b8a9b1dSJustin T. Gibbs 
15042b83592fSScott Long 	xpt_sim->max_ccbs = 16;
15052b83592fSScott Long 
15062b83592fSScott Long 	mtx_lock(&xsoftc.xpt_lock);
1507df826980SMatt Jacob 	if ((status = xpt_bus_register(xpt_sim, /*bus #*/0)) != CAM_SUCCESS) {
1508a2821e04SMatt Jacob 		printf("xpt_init: xpt_bus_register failed with status %#x,"
1509df826980SMatt Jacob 		       " failing attach\n", status);
15102b83592fSScott Long 		return (EINVAL);
1511df826980SMatt Jacob 	}
15128b8a9b1dSJustin T. Gibbs 
15138b8a9b1dSJustin T. Gibbs 	/*
15148b8a9b1dSJustin T. Gibbs 	 * Looking at the XPT from the SIM layer, the XPT is
15158b8a9b1dSJustin T. Gibbs 	 * the equivelent of a peripheral driver.  Allocate
15168b8a9b1dSJustin T. Gibbs 	 * a peripheral driver entry for us.
15178b8a9b1dSJustin T. Gibbs 	 */
15188b8a9b1dSJustin T. Gibbs 	if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
15198b8a9b1dSJustin T. Gibbs 				      CAM_TARGET_WILDCARD,
15208b8a9b1dSJustin T. Gibbs 				      CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
15218b8a9b1dSJustin T. Gibbs 		printf("xpt_init: xpt_create_path failed with status %#x,"
15228b8a9b1dSJustin T. Gibbs 		       " failing attach\n", status);
15232b83592fSScott Long 		return (EINVAL);
15248b8a9b1dSJustin T. Gibbs 	}
15258b8a9b1dSJustin T. Gibbs 
1526ee9c90c7SKenneth D. Merry 	cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
15272b83592fSScott Long 			 path, NULL, 0, xpt_sim);
15288b8a9b1dSJustin T. Gibbs 	xpt_free_path(path);
15292b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_lock);
15308b8a9b1dSJustin T. Gibbs 
15318b8a9b1dSJustin T. Gibbs 	/*
15328b8a9b1dSJustin T. Gibbs 	 * Register a callback for when interrupts are enabled.
15338b8a9b1dSJustin T. Gibbs 	 */
15342b83592fSScott Long 	xsoftc.xpt_config_hook =
15358b8a9b1dSJustin T. Gibbs 	    (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
15360dd50e9bSScott Long 					      M_CAMXPT, M_NOWAIT | M_ZERO);
15372b83592fSScott Long 	if (xsoftc.xpt_config_hook == NULL) {
15388b8a9b1dSJustin T. Gibbs 		printf("xpt_init: Cannot malloc config hook "
15398b8a9b1dSJustin T. Gibbs 		       "- failing attach\n");
15402b83592fSScott Long 		return (ENOMEM);
15418b8a9b1dSJustin T. Gibbs 	}
15428b8a9b1dSJustin T. Gibbs 
15432b83592fSScott Long 	xsoftc.xpt_config_hook->ich_func = xpt_config;
15442b83592fSScott Long 	if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
15450dd50e9bSScott Long 		free (xsoftc.xpt_config_hook, M_CAMXPT);
15468b8a9b1dSJustin T. Gibbs 		printf("xpt_init: config_intrhook_establish failed "
15478b8a9b1dSJustin T. Gibbs 		       "- failing attach\n");
15488b8a9b1dSJustin T. Gibbs 	}
15498b8a9b1dSJustin T. Gibbs 
15509e6461a2SMatt Jacob 	/* fire up rescan thread */
15519e6461a2SMatt Jacob 	if (kthread_create(xpt_scanner_thread, NULL, NULL, 0, 0, "xpt_thrd")) {
15529e6461a2SMatt Jacob 		printf("xpt_init: failed to create rescan thread\n");
15539e6461a2SMatt Jacob 	}
15548b8a9b1dSJustin T. Gibbs 	/* Install our software interrupt handlers */
15559758cc83SScott Long 	swi_add(NULL, "cambio", camisr, NULL, SWI_CAMBIO, INTR_MPSAFE, &cambio_ih);
15562b83592fSScott Long 
15572b83592fSScott Long 	return (0);
15588b8a9b1dSJustin T. Gibbs }
15598b8a9b1dSJustin T. Gibbs 
15608b8a9b1dSJustin T. Gibbs static cam_status
15618b8a9b1dSJustin T. Gibbs xptregister(struct cam_periph *periph, void *arg)
15628b8a9b1dSJustin T. Gibbs {
15632b83592fSScott Long 	struct cam_sim *xpt_sim;
15642b83592fSScott Long 
15658b8a9b1dSJustin T. Gibbs 	if (periph == NULL) {
15668b8a9b1dSJustin T. Gibbs 		printf("xptregister: periph was NULL!!\n");
15678b8a9b1dSJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
15688b8a9b1dSJustin T. Gibbs 	}
15698b8a9b1dSJustin T. Gibbs 
15702b83592fSScott Long 	xpt_sim = (struct cam_sim *)arg;
15712b83592fSScott Long 	xpt_sim->softc = periph;
15728b8a9b1dSJustin T. Gibbs 	xpt_periph = periph;
15732b83592fSScott Long 	periph->softc = NULL;
15748b8a9b1dSJustin T. Gibbs 
15758b8a9b1dSJustin T. Gibbs 	return(CAM_REQ_CMP);
15768b8a9b1dSJustin T. Gibbs }
15778b8a9b1dSJustin T. Gibbs 
15788b8a9b1dSJustin T. Gibbs int32_t
15798b8a9b1dSJustin T. Gibbs xpt_add_periph(struct cam_periph *periph)
15808b8a9b1dSJustin T. Gibbs {
15818b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
15828b8a9b1dSJustin T. Gibbs 	int32_t	 status;
15838b8a9b1dSJustin T. Gibbs 	struct periph_list *periph_head;
15848b8a9b1dSJustin T. Gibbs 
15852b83592fSScott Long 	mtx_assert(periph->sim->mtx, MA_OWNED);
158668153f43SScott Long 
15878b8a9b1dSJustin T. Gibbs 	device = periph->path->device;
15888b8a9b1dSJustin T. Gibbs 
15898b8a9b1dSJustin T. Gibbs 	periph_head = &device->periphs;
15908b8a9b1dSJustin T. Gibbs 
15918b8a9b1dSJustin T. Gibbs 	status = CAM_REQ_CMP;
15928b8a9b1dSJustin T. Gibbs 
15938b8a9b1dSJustin T. Gibbs 	if (device != NULL) {
15948b8a9b1dSJustin T. Gibbs 		/*
15958b8a9b1dSJustin T. Gibbs 		 * Make room for this peripheral
15968b8a9b1dSJustin T. Gibbs 		 * so it will fit in the queue
15978b8a9b1dSJustin T. Gibbs 		 * when it's scheduled to run
15988b8a9b1dSJustin T. Gibbs 		 */
15998b8a9b1dSJustin T. Gibbs 		status = camq_resize(&device->drvq,
16008b8a9b1dSJustin T. Gibbs 				     device->drvq.array_size + 1);
16018b8a9b1dSJustin T. Gibbs 
16028b8a9b1dSJustin T. Gibbs 		device->generation++;
16038b8a9b1dSJustin T. Gibbs 
16048b8a9b1dSJustin T. Gibbs 		SLIST_INSERT_HEAD(periph_head, periph, periph_links);
16058b8a9b1dSJustin T. Gibbs 	}
16068b8a9b1dSJustin T. Gibbs 
160777dc25ccSScott Long 	mtx_lock(&xsoftc.xpt_topo_lock);
160877dc25ccSScott Long 	xsoftc.xpt_generation++;
160977dc25ccSScott Long 	mtx_unlock(&xsoftc.xpt_topo_lock);
16108b8a9b1dSJustin T. Gibbs 
16118b8a9b1dSJustin T. Gibbs 	return (status);
16128b8a9b1dSJustin T. Gibbs }
16138b8a9b1dSJustin T. Gibbs 
16148b8a9b1dSJustin T. Gibbs void
16158b8a9b1dSJustin T. Gibbs xpt_remove_periph(struct cam_periph *periph)
16168b8a9b1dSJustin T. Gibbs {
16178b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
16188b8a9b1dSJustin T. Gibbs 
16192b83592fSScott Long 	mtx_assert(periph->sim->mtx, MA_OWNED);
162068153f43SScott Long 
16218b8a9b1dSJustin T. Gibbs 	device = periph->path->device;
16228b8a9b1dSJustin T. Gibbs 
16238b8a9b1dSJustin T. Gibbs 	if (device != NULL) {
16248b8a9b1dSJustin T. Gibbs 		struct periph_list *periph_head;
16258b8a9b1dSJustin T. Gibbs 
16268b8a9b1dSJustin T. Gibbs 		periph_head = &device->periphs;
16278b8a9b1dSJustin T. Gibbs 
16288b8a9b1dSJustin T. Gibbs 		/* Release the slot for this peripheral */
16298b8a9b1dSJustin T. Gibbs 		camq_resize(&device->drvq, device->drvq.array_size - 1);
16308b8a9b1dSJustin T. Gibbs 
16318b8a9b1dSJustin T. Gibbs 		device->generation++;
16328b8a9b1dSJustin T. Gibbs 
1633e3975643SJake Burkholder 		SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
16348b8a9b1dSJustin T. Gibbs 	}
16358b8a9b1dSJustin T. Gibbs 
163677dc25ccSScott Long 	mtx_lock(&xsoftc.xpt_topo_lock);
163777dc25ccSScott Long 	xsoftc.xpt_generation++;
163877dc25ccSScott Long 	mtx_unlock(&xsoftc.xpt_topo_lock);
16398b8a9b1dSJustin T. Gibbs }
16408b8a9b1dSJustin T. Gibbs 
16413393f8daSKenneth D. Merry 
16423393f8daSKenneth D. Merry void
16433393f8daSKenneth D. Merry xpt_announce_periph(struct cam_periph *periph, char *announce_string)
16443393f8daSKenneth D. Merry {
16453393f8daSKenneth D. Merry 	struct	ccb_pathinq cpi;
16463393f8daSKenneth D. Merry 	struct	ccb_trans_settings cts;
16473393f8daSKenneth D. Merry 	struct	cam_path *path;
16483393f8daSKenneth D. Merry 	u_int	speed;
16493393f8daSKenneth D. Merry 	u_int	freq;
16503393f8daSKenneth D. Merry 	u_int	mb;
16513393f8daSKenneth D. Merry 
16522b83592fSScott Long 	mtx_assert(periph->sim->mtx, MA_OWNED);
165368153f43SScott Long 
16543393f8daSKenneth D. Merry 	path = periph->path;
16553393f8daSKenneth D. Merry 	/*
16563393f8daSKenneth D. Merry 	 * To ensure that this is printed in one piece,
16573393f8daSKenneth D. Merry 	 * mask out CAM interrupts.
16583393f8daSKenneth D. Merry 	 */
16593393f8daSKenneth D. Merry 	printf("%s%d at %s%d bus %d target %d lun %d\n",
16603393f8daSKenneth D. Merry 	       periph->periph_name, periph->unit_number,
16613393f8daSKenneth D. Merry 	       path->bus->sim->sim_name,
16623393f8daSKenneth D. Merry 	       path->bus->sim->unit_number,
16633393f8daSKenneth D. Merry 	       path->bus->sim->bus_id,
16643393f8daSKenneth D. Merry 	       path->target->target_id,
16653393f8daSKenneth D. Merry 	       path->device->lun_id);
16663393f8daSKenneth D. Merry 	printf("%s%d: ", periph->periph_name, periph->unit_number);
16673393f8daSKenneth D. Merry 	scsi_print_inquiry(&path->device->inq_data);
1668f053d777SMatt Jacob 	if (bootverbose && path->device->serial_num_len > 0) {
16693393f8daSKenneth D. Merry 		/* Don't wrap the screen  - print only the first 60 chars */
16703393f8daSKenneth D. Merry 		printf("%s%d: Serial Number %.60s\n", periph->periph_name,
16713393f8daSKenneth D. Merry 		       periph->unit_number, path->device->serial_num);
16723393f8daSKenneth D. Merry 	}
16733393f8daSKenneth D. Merry 	xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
16743393f8daSKenneth D. Merry 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
16753393f8daSKenneth D. Merry 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
16763393f8daSKenneth D. Merry 	xpt_action((union ccb*)&cts);
16770a480cf0SMatt Jacob 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
16780a480cf0SMatt Jacob 		return;
16790a480cf0SMatt Jacob 	}
16803393f8daSKenneth D. Merry 
16813393f8daSKenneth D. Merry 	/* Ask the SIM for its base transfer speed */
16823393f8daSKenneth D. Merry 	xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
16833393f8daSKenneth D. Merry 	cpi.ccb_h.func_code = XPT_PATH_INQ;
16843393f8daSKenneth D. Merry 	xpt_action((union ccb *)&cpi);
16853393f8daSKenneth D. Merry 
16863393f8daSKenneth D. Merry 	speed = cpi.base_transfer_speed;
16873393f8daSKenneth D. Merry 	freq = 0;
1688f053d777SMatt Jacob 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
16893393f8daSKenneth D. Merry 		struct	ccb_trans_settings_spi *spi;
16903393f8daSKenneth D. Merry 
16913393f8daSKenneth D. Merry 		spi = &cts.xport_specific.spi;
16923393f8daSKenneth D. Merry 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
16933393f8daSKenneth D. Merry 		  && spi->sync_offset != 0) {
16943393f8daSKenneth D. Merry 			freq = scsi_calc_syncsrate(spi->sync_period);
16953393f8daSKenneth D. Merry 			speed = freq;
16963393f8daSKenneth D. Merry 		}
16973393f8daSKenneth D. Merry 
16983393f8daSKenneth D. Merry 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
16993393f8daSKenneth D. Merry 			speed *= (0x01 << spi->bus_width);
17003393f8daSKenneth D. Merry 	}
17012c7d0b8dSMatt Jacob 
1702f053d777SMatt Jacob 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1703f053d777SMatt Jacob 		struct	ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1704f053d777SMatt Jacob 		if (fc->valid & CTS_FC_VALID_SPEED) {
17052c7d0b8dSMatt Jacob 			speed = fc->bitrate;
17062c7d0b8dSMatt Jacob 		}
1707f053d777SMatt Jacob 	}
17083393f8daSKenneth D. Merry 
1709c4270cb6SMatt Jacob 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SAS) {
1710c4270cb6SMatt Jacob 		struct	ccb_trans_settings_sas *sas = &cts.xport_specific.sas;
1711c4270cb6SMatt Jacob 		if (sas->valid & CTS_SAS_VALID_SPEED) {
1712c4270cb6SMatt Jacob 			speed = sas->bitrate;
1713c4270cb6SMatt Jacob 		}
1714c4270cb6SMatt Jacob 	}
1715c4270cb6SMatt Jacob 
17163393f8daSKenneth D. Merry 	mb = speed / 1000;
17173393f8daSKenneth D. Merry 	if (mb > 0)
17183393f8daSKenneth D. Merry 		printf("%s%d: %d.%03dMB/s transfers",
17193393f8daSKenneth D. Merry 		       periph->periph_name, periph->unit_number,
17203393f8daSKenneth D. Merry 		       mb, speed % 1000);
17213393f8daSKenneth D. Merry 	else
17223393f8daSKenneth D. Merry 		printf("%s%d: %dKB/s transfers", periph->periph_name,
17233393f8daSKenneth D. Merry 		       periph->unit_number, speed);
17243393f8daSKenneth D. Merry 	/* Report additional information about SPI connections */
1725f053d777SMatt Jacob 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
17263393f8daSKenneth D. Merry 		struct	ccb_trans_settings_spi *spi;
17273393f8daSKenneth D. Merry 
17283393f8daSKenneth D. Merry 		spi = &cts.xport_specific.spi;
17293393f8daSKenneth D. Merry 		if (freq != 0) {
17303393f8daSKenneth D. Merry 			printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
17313393f8daSKenneth D. Merry 			       freq % 1000,
17323393f8daSKenneth D. Merry 			       (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
17333393f8daSKenneth D. Merry 			     ? " DT" : "",
17343393f8daSKenneth D. Merry 			       spi->sync_offset);
17353393f8daSKenneth D. Merry 		}
17363393f8daSKenneth D. Merry 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
17373393f8daSKenneth D. Merry 		 && spi->bus_width > 0) {
17383393f8daSKenneth D. Merry 			if (freq != 0) {
17393393f8daSKenneth D. Merry 				printf(", ");
17403393f8daSKenneth D. Merry 			} else {
17413393f8daSKenneth D. Merry 				printf(" (");
17423393f8daSKenneth D. Merry 			}
17433393f8daSKenneth D. Merry 			printf("%dbit)", 8 * (0x01 << spi->bus_width));
17443393f8daSKenneth D. Merry 		} else if (freq != 0) {
17453393f8daSKenneth D. Merry 			printf(")");
17463393f8daSKenneth D. Merry 		}
17473393f8daSKenneth D. Merry 	}
1748f053d777SMatt Jacob 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
17492c7d0b8dSMatt Jacob 		struct	ccb_trans_settings_fc *fc;
17502c7d0b8dSMatt Jacob 
17512c7d0b8dSMatt Jacob 		fc = &cts.xport_specific.fc;
1752f053d777SMatt Jacob 		if (fc->valid & CTS_FC_VALID_WWNN)
1753f053d777SMatt Jacob 			printf(" WWNN 0x%llx", (long long) fc->wwnn);
1754f053d777SMatt Jacob 		if (fc->valid & CTS_FC_VALID_WWPN)
1755f053d777SMatt Jacob 			printf(" WWPN 0x%llx", (long long) fc->wwpn);
1756f053d777SMatt Jacob 		if (fc->valid & CTS_FC_VALID_PORT)
1757f053d777SMatt Jacob 			printf(" PortID 0x%x", fc->port);
17582c7d0b8dSMatt Jacob 	}
17593393f8daSKenneth D. Merry 
17603393f8daSKenneth D. Merry 	if (path->device->inq_flags & SID_CmdQue
17613393f8daSKenneth D. Merry 	 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
17627ffbfcd7SMatt Jacob 		printf("\n%s%d: Command Queueing Enabled",
17633393f8daSKenneth D. Merry 		       periph->periph_name, periph->unit_number);
17643393f8daSKenneth D. Merry 	}
17653393f8daSKenneth D. Merry 	printf("\n");
17663393f8daSKenneth D. Merry 
17673393f8daSKenneth D. Merry 	/*
17683393f8daSKenneth D. Merry 	 * We only want to print the caller's announce string if they've
17693393f8daSKenneth D. Merry 	 * passed one in..
17703393f8daSKenneth D. Merry 	 */
17713393f8daSKenneth D. Merry 	if (announce_string != NULL)
17723393f8daSKenneth D. Merry 		printf("%s%d: %s\n", periph->periph_name,
17733393f8daSKenneth D. Merry 		       periph->unit_number, announce_string);
17743393f8daSKenneth D. Merry }
17758b8a9b1dSJustin T. Gibbs 
17768b8a9b1dSJustin T. Gibbs static dev_match_ret
17773393f8daSKenneth D. Merry xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
17788b8a9b1dSJustin T. Gibbs 	    struct cam_eb *bus)
17798b8a9b1dSJustin T. Gibbs {
17808b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
17818b8a9b1dSJustin T. Gibbs 	int i;
17828b8a9b1dSJustin T. Gibbs 
17838b8a9b1dSJustin T. Gibbs 	retval = DM_RET_NONE;
17848b8a9b1dSJustin T. Gibbs 
17858b8a9b1dSJustin T. Gibbs 	/*
17868b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
17878b8a9b1dSJustin T. Gibbs 	 */
17888b8a9b1dSJustin T. Gibbs 	if (bus == NULL)
17898b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
17908b8a9b1dSJustin T. Gibbs 
17918b8a9b1dSJustin T. Gibbs 	/*
17928b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this bus matches no
17938b8a9b1dSJustin T. Gibbs 	 * matter what.
17948b8a9b1dSJustin T. Gibbs 	 */
17958b8a9b1dSJustin T. Gibbs 	if ((patterns == NULL) || (num_patterns == 0))
17968b8a9b1dSJustin T. Gibbs 		return(DM_RET_DESCEND | DM_RET_COPY);
17978b8a9b1dSJustin T. Gibbs 
17988b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
17998b8a9b1dSJustin T. Gibbs 		struct bus_match_pattern *cur_pattern;
18008b8a9b1dSJustin T. Gibbs 
18018b8a9b1dSJustin T. Gibbs 		/*
18028b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a bus node, we
18038b8a9b1dSJustin T. Gibbs 		 * aren't interested.  However, we do indicate to the
18048b8a9b1dSJustin T. Gibbs 		 * calling routine that we should continue descending the
18058b8a9b1dSJustin T. Gibbs 		 * tree, since the user wants to match against lower-level
18068b8a9b1dSJustin T. Gibbs 		 * EDT elements.
18078b8a9b1dSJustin T. Gibbs 		 */
18088b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_BUS) {
18098b8a9b1dSJustin T. Gibbs 			if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
18108b8a9b1dSJustin T. Gibbs 				retval |= DM_RET_DESCEND;
18118b8a9b1dSJustin T. Gibbs 			continue;
18128b8a9b1dSJustin T. Gibbs 		}
18138b8a9b1dSJustin T. Gibbs 
18148b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.bus_pattern;
18158b8a9b1dSJustin T. Gibbs 
18168b8a9b1dSJustin T. Gibbs 		/*
18178b8a9b1dSJustin T. Gibbs 		 * If they want to match any bus node, we give them any
18188b8a9b1dSJustin T. Gibbs 		 * device node.
18198b8a9b1dSJustin T. Gibbs 		 */
18208b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == BUS_MATCH_ANY) {
18218b8a9b1dSJustin T. Gibbs 			/* set the copy flag */
18228b8a9b1dSJustin T. Gibbs 			retval |= DM_RET_COPY;
18238b8a9b1dSJustin T. Gibbs 
18248b8a9b1dSJustin T. Gibbs 			/*
18258b8a9b1dSJustin T. Gibbs 			 * If we've already decided on an action, go ahead
18268b8a9b1dSJustin T. Gibbs 			 * and return.
18278b8a9b1dSJustin T. Gibbs 			 */
18288b8a9b1dSJustin T. Gibbs 			if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
18298b8a9b1dSJustin T. Gibbs 				return(retval);
18308b8a9b1dSJustin T. Gibbs 		}
18318b8a9b1dSJustin T. Gibbs 
18328b8a9b1dSJustin T. Gibbs 		/*
18338b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
18348b8a9b1dSJustin T. Gibbs 		 */
18358b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == BUS_MATCH_NONE)
18368b8a9b1dSJustin T. Gibbs 			continue;
18378b8a9b1dSJustin T. Gibbs 
18388b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
18398b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != bus->path_id))
18408b8a9b1dSJustin T. Gibbs 			continue;
18418b8a9b1dSJustin T. Gibbs 
18428b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
18438b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->bus_id != bus->sim->bus_id))
18448b8a9b1dSJustin T. Gibbs 			continue;
18458b8a9b1dSJustin T. Gibbs 
18468b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
18478b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->unit_number != bus->sim->unit_number))
18488b8a9b1dSJustin T. Gibbs 			continue;
18498b8a9b1dSJustin T. Gibbs 
18508b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
18518b8a9b1dSJustin T. Gibbs 		 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
18528b8a9b1dSJustin T. Gibbs 			     DEV_IDLEN) != 0))
18538b8a9b1dSJustin T. Gibbs 			continue;
18548b8a9b1dSJustin T. Gibbs 
18558b8a9b1dSJustin T. Gibbs 		/*
18568b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
18578b8a9b1dSJustin T. Gibbs 		 * information on this bus.  So tell the caller to copy the
18588b8a9b1dSJustin T. Gibbs 		 * data out.
18598b8a9b1dSJustin T. Gibbs 		 */
18608b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
18618b8a9b1dSJustin T. Gibbs 
18628b8a9b1dSJustin T. Gibbs 		/*
18638b8a9b1dSJustin T. Gibbs 		 * If the return action has been set to descend, then we
18648b8a9b1dSJustin T. Gibbs 		 * know that we've already seen a non-bus matching
18658b8a9b1dSJustin T. Gibbs 		 * expression, therefore we need to further descend the tree.
18668b8a9b1dSJustin T. Gibbs 		 * This won't change by continuing around the loop, so we
18678b8a9b1dSJustin T. Gibbs 		 * go ahead and return.  If we haven't seen a non-bus
18688b8a9b1dSJustin T. Gibbs 		 * matching expression, we keep going around the loop until
18698b8a9b1dSJustin T. Gibbs 		 * we exhaust the matching expressions.  We'll set the stop
18708b8a9b1dSJustin T. Gibbs 		 * flag once we fall out of the loop.
18718b8a9b1dSJustin T. Gibbs 		 */
18728b8a9b1dSJustin T. Gibbs 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
18738b8a9b1dSJustin T. Gibbs 			return(retval);
18748b8a9b1dSJustin T. Gibbs 	}
18758b8a9b1dSJustin T. Gibbs 
18768b8a9b1dSJustin T. Gibbs 	/*
18778b8a9b1dSJustin T. Gibbs 	 * If the return action hasn't been set to descend yet, that means
18788b8a9b1dSJustin T. Gibbs 	 * we haven't seen anything other than bus matching patterns.  So
18798b8a9b1dSJustin T. Gibbs 	 * tell the caller to stop descending the tree -- the user doesn't
18808b8a9b1dSJustin T. Gibbs 	 * want to match against lower level tree elements.
18818b8a9b1dSJustin T. Gibbs 	 */
18828b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
18838b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_STOP;
18848b8a9b1dSJustin T. Gibbs 
18858b8a9b1dSJustin T. Gibbs 	return(retval);
18868b8a9b1dSJustin T. Gibbs }
18878b8a9b1dSJustin T. Gibbs 
18888b8a9b1dSJustin T. Gibbs static dev_match_ret
18893393f8daSKenneth D. Merry xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
18908b8a9b1dSJustin T. Gibbs 	       struct cam_ed *device)
18918b8a9b1dSJustin T. Gibbs {
18928b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
18938b8a9b1dSJustin T. Gibbs 	int i;
18948b8a9b1dSJustin T. Gibbs 
18958b8a9b1dSJustin T. Gibbs 	retval = DM_RET_NONE;
18968b8a9b1dSJustin T. Gibbs 
18978b8a9b1dSJustin T. Gibbs 	/*
18988b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
18998b8a9b1dSJustin T. Gibbs 	 */
19008b8a9b1dSJustin T. Gibbs 	if (device == NULL)
19018b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
19028b8a9b1dSJustin T. Gibbs 
19038b8a9b1dSJustin T. Gibbs 	/*
19048b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this device matches no
19058b8a9b1dSJustin T. Gibbs 	 * matter what.
19068b8a9b1dSJustin T. Gibbs 	 */
190759e75884SColin Percival 	if ((patterns == NULL) || (num_patterns == 0))
19088b8a9b1dSJustin T. Gibbs 		return(DM_RET_DESCEND | DM_RET_COPY);
19098b8a9b1dSJustin T. Gibbs 
19108b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
19118b8a9b1dSJustin T. Gibbs 		struct device_match_pattern *cur_pattern;
19128b8a9b1dSJustin T. Gibbs 
19138b8a9b1dSJustin T. Gibbs 		/*
19148b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a device node, we
19158b8a9b1dSJustin T. Gibbs 		 * aren't interested.
19168b8a9b1dSJustin T. Gibbs 		 */
19178b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_DEVICE) {
19188b8a9b1dSJustin T. Gibbs 			if ((patterns[i].type == DEV_MATCH_PERIPH)
19198b8a9b1dSJustin T. Gibbs 			 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
19208b8a9b1dSJustin T. Gibbs 				retval |= DM_RET_DESCEND;
19218b8a9b1dSJustin T. Gibbs 			continue;
19228b8a9b1dSJustin T. Gibbs 		}
19238b8a9b1dSJustin T. Gibbs 
19248b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.device_pattern;
19258b8a9b1dSJustin T. Gibbs 
19268b8a9b1dSJustin T. Gibbs 		/*
19278b8a9b1dSJustin T. Gibbs 		 * If they want to match any device node, we give them any
19288b8a9b1dSJustin T. Gibbs 		 * device node.
19298b8a9b1dSJustin T. Gibbs 		 */
19308b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == DEV_MATCH_ANY) {
19318b8a9b1dSJustin T. Gibbs 			/* set the copy flag */
19328b8a9b1dSJustin T. Gibbs 			retval |= DM_RET_COPY;
19338b8a9b1dSJustin T. Gibbs 
19348b8a9b1dSJustin T. Gibbs 
19358b8a9b1dSJustin T. Gibbs 			/*
19368b8a9b1dSJustin T. Gibbs 			 * If we've already decided on an action, go ahead
19378b8a9b1dSJustin T. Gibbs 			 * and return.
19388b8a9b1dSJustin T. Gibbs 			 */
19398b8a9b1dSJustin T. Gibbs 			if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
19408b8a9b1dSJustin T. Gibbs 				return(retval);
19418b8a9b1dSJustin T. Gibbs 		}
19428b8a9b1dSJustin T. Gibbs 
19438b8a9b1dSJustin T. Gibbs 		/*
19448b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
19458b8a9b1dSJustin T. Gibbs 		 */
19468b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == DEV_MATCH_NONE)
19478b8a9b1dSJustin T. Gibbs 			continue;
19488b8a9b1dSJustin T. Gibbs 
19498b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
19508b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != device->target->bus->path_id))
19518b8a9b1dSJustin T. Gibbs 			continue;
19528b8a9b1dSJustin T. Gibbs 
19538b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
19548b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->target_id != device->target->target_id))
19558b8a9b1dSJustin T. Gibbs 			continue;
19568b8a9b1dSJustin T. Gibbs 
19578b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
19588b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->target_lun != device->lun_id))
19598b8a9b1dSJustin T. Gibbs 			continue;
19608b8a9b1dSJustin T. Gibbs 
19618b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
19628b8a9b1dSJustin T. Gibbs 		 && (cam_quirkmatch((caddr_t)&device->inq_data,
19638b8a9b1dSJustin T. Gibbs 				    (caddr_t)&cur_pattern->inq_pat,
19648b8a9b1dSJustin T. Gibbs 				    1, sizeof(cur_pattern->inq_pat),
19658b8a9b1dSJustin T. Gibbs 				    scsi_static_inquiry_match) == NULL))
19668b8a9b1dSJustin T. Gibbs 			continue;
19678b8a9b1dSJustin T. Gibbs 
19688b8a9b1dSJustin T. Gibbs 		/*
19698b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
19708b8a9b1dSJustin T. Gibbs 		 * information on this device.  So tell the caller to copy
19718b8a9b1dSJustin T. Gibbs 		 * the data out.
19728b8a9b1dSJustin T. Gibbs 		 */
19738b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
19748b8a9b1dSJustin T. Gibbs 
19758b8a9b1dSJustin T. Gibbs 		/*
19768b8a9b1dSJustin T. Gibbs 		 * If the return action has been set to descend, then we
19778b8a9b1dSJustin T. Gibbs 		 * know that we've already seen a peripheral matching
19788b8a9b1dSJustin T. Gibbs 		 * expression, therefore we need to further descend the tree.
19798b8a9b1dSJustin T. Gibbs 		 * This won't change by continuing around the loop, so we
19808b8a9b1dSJustin T. Gibbs 		 * go ahead and return.  If we haven't seen a peripheral
19818b8a9b1dSJustin T. Gibbs 		 * matching expression, we keep going around the loop until
19828b8a9b1dSJustin T. Gibbs 		 * we exhaust the matching expressions.  We'll set the stop
19838b8a9b1dSJustin T. Gibbs 		 * flag once we fall out of the loop.
19848b8a9b1dSJustin T. Gibbs 		 */
19858b8a9b1dSJustin T. Gibbs 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
19868b8a9b1dSJustin T. Gibbs 			return(retval);
19878b8a9b1dSJustin T. Gibbs 	}
19888b8a9b1dSJustin T. Gibbs 
19898b8a9b1dSJustin T. Gibbs 	/*
19908b8a9b1dSJustin T. Gibbs 	 * If the return action hasn't been set to descend yet, that means
19918b8a9b1dSJustin T. Gibbs 	 * we haven't seen any peripheral matching patterns.  So tell the
19928b8a9b1dSJustin T. Gibbs 	 * caller to stop descending the tree -- the user doesn't want to
19938b8a9b1dSJustin T. Gibbs 	 * match against lower level tree elements.
19948b8a9b1dSJustin T. Gibbs 	 */
19958b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
19968b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_STOP;
19978b8a9b1dSJustin T. Gibbs 
19988b8a9b1dSJustin T. Gibbs 	return(retval);
19998b8a9b1dSJustin T. Gibbs }
20008b8a9b1dSJustin T. Gibbs 
20018b8a9b1dSJustin T. Gibbs /*
20028b8a9b1dSJustin T. Gibbs  * Match a single peripheral against any number of match patterns.
20038b8a9b1dSJustin T. Gibbs  */
20048b8a9b1dSJustin T. Gibbs static dev_match_ret
20053393f8daSKenneth D. Merry xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
20068b8a9b1dSJustin T. Gibbs 	       struct cam_periph *periph)
20078b8a9b1dSJustin T. Gibbs {
20088b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
20098b8a9b1dSJustin T. Gibbs 	int i;
20108b8a9b1dSJustin T. Gibbs 
20118b8a9b1dSJustin T. Gibbs 	/*
20128b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
20138b8a9b1dSJustin T. Gibbs 	 */
20148b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
20158b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
20168b8a9b1dSJustin T. Gibbs 
20178b8a9b1dSJustin T. Gibbs 	/*
20188b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this peripheral matches no
20198b8a9b1dSJustin T. Gibbs 	 * matter what.
20208b8a9b1dSJustin T. Gibbs 	 */
20218b8a9b1dSJustin T. Gibbs 	if ((patterns == NULL) || (num_patterns == 0))
20228b8a9b1dSJustin T. Gibbs 		return(DM_RET_STOP | DM_RET_COPY);
20238b8a9b1dSJustin T. Gibbs 
20248b8a9b1dSJustin T. Gibbs 	/*
20258b8a9b1dSJustin T. Gibbs 	 * There aren't any nodes below a peripheral node, so there's no
20268b8a9b1dSJustin T. Gibbs 	 * reason to descend the tree any further.
20278b8a9b1dSJustin T. Gibbs 	 */
20288b8a9b1dSJustin T. Gibbs 	retval = DM_RET_STOP;
20298b8a9b1dSJustin T. Gibbs 
20308b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
20318b8a9b1dSJustin T. Gibbs 		struct periph_match_pattern *cur_pattern;
20328b8a9b1dSJustin T. Gibbs 
20338b8a9b1dSJustin T. Gibbs 		/*
20348b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a peripheral, we
20358b8a9b1dSJustin T. Gibbs 		 * aren't interested.
20368b8a9b1dSJustin T. Gibbs 		 */
20378b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_PERIPH)
20388b8a9b1dSJustin T. Gibbs 			continue;
20398b8a9b1dSJustin T. Gibbs 
20408b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.periph_pattern;
20418b8a9b1dSJustin T. Gibbs 
20428b8a9b1dSJustin T. Gibbs 		/*
20438b8a9b1dSJustin T. Gibbs 		 * If they want to match on anything, then we will do so.
20448b8a9b1dSJustin T. Gibbs 		 */
20458b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == PERIPH_MATCH_ANY) {
20468b8a9b1dSJustin T. Gibbs 			/* set the copy flag */
20478b8a9b1dSJustin T. Gibbs 			retval |= DM_RET_COPY;
20488b8a9b1dSJustin T. Gibbs 
20498b8a9b1dSJustin T. Gibbs 			/*
20508b8a9b1dSJustin T. Gibbs 			 * We've already set the return action to stop,
20518b8a9b1dSJustin T. Gibbs 			 * since there are no nodes below peripherals in
20528b8a9b1dSJustin T. Gibbs 			 * the tree.
20538b8a9b1dSJustin T. Gibbs 			 */
20548b8a9b1dSJustin T. Gibbs 			return(retval);
20558b8a9b1dSJustin T. Gibbs 		}
20568b8a9b1dSJustin T. Gibbs 
20578b8a9b1dSJustin T. Gibbs 		/*
20588b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
20598b8a9b1dSJustin T. Gibbs 		 */
20608b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == PERIPH_MATCH_NONE)
20618b8a9b1dSJustin T. Gibbs 			continue;
20628b8a9b1dSJustin T. Gibbs 
20638b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
20648b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != periph->path->bus->path_id))
20658b8a9b1dSJustin T. Gibbs 			continue;
20668b8a9b1dSJustin T. Gibbs 
20678b8a9b1dSJustin T. Gibbs 		/*
20688b8a9b1dSJustin T. Gibbs 		 * For the target and lun id's, we have to make sure the
20698b8a9b1dSJustin T. Gibbs 		 * target and lun pointers aren't NULL.  The xpt peripheral
20708b8a9b1dSJustin T. Gibbs 		 * has a wildcard target and device.
20718b8a9b1dSJustin T. Gibbs 		 */
20728b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
20738b8a9b1dSJustin T. Gibbs 		 && ((periph->path->target == NULL)
20748b8a9b1dSJustin T. Gibbs 		 ||(cur_pattern->target_id != periph->path->target->target_id)))
20758b8a9b1dSJustin T. Gibbs 			continue;
20768b8a9b1dSJustin T. Gibbs 
20778b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
20788b8a9b1dSJustin T. Gibbs 		 && ((periph->path->device == NULL)
20798b8a9b1dSJustin T. Gibbs 		 || (cur_pattern->target_lun != periph->path->device->lun_id)))
20808b8a9b1dSJustin T. Gibbs 			continue;
20818b8a9b1dSJustin T. Gibbs 
20828b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
20838b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->unit_number != periph->unit_number))
20848b8a9b1dSJustin T. Gibbs 			continue;
20858b8a9b1dSJustin T. Gibbs 
20868b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
20878b8a9b1dSJustin T. Gibbs 		 && (strncmp(cur_pattern->periph_name, periph->periph_name,
20888b8a9b1dSJustin T. Gibbs 			     DEV_IDLEN) != 0))
20898b8a9b1dSJustin T. Gibbs 			continue;
20908b8a9b1dSJustin T. Gibbs 
20918b8a9b1dSJustin T. Gibbs 		/*
20928b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
20938b8a9b1dSJustin T. Gibbs 		 * information on this peripheral.  So tell the caller to
20948b8a9b1dSJustin T. Gibbs 		 * copy the data out.
20958b8a9b1dSJustin T. Gibbs 		 */
20968b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
20978b8a9b1dSJustin T. Gibbs 
20988b8a9b1dSJustin T. Gibbs 		/*
20998b8a9b1dSJustin T. Gibbs 		 * The return action has already been set to stop, since
21008b8a9b1dSJustin T. Gibbs 		 * peripherals don't have any nodes below them in the EDT.
21018b8a9b1dSJustin T. Gibbs 		 */
21028b8a9b1dSJustin T. Gibbs 		return(retval);
21038b8a9b1dSJustin T. Gibbs 	}
21048b8a9b1dSJustin T. Gibbs 
21058b8a9b1dSJustin T. Gibbs 	/*
21068b8a9b1dSJustin T. Gibbs 	 * If we get to this point, the peripheral that was passed in
21078b8a9b1dSJustin T. Gibbs 	 * doesn't match any of the patterns.
21088b8a9b1dSJustin T. Gibbs 	 */
21098b8a9b1dSJustin T. Gibbs 	return(retval);
21108b8a9b1dSJustin T. Gibbs }
21118b8a9b1dSJustin T. Gibbs 
21128b8a9b1dSJustin T. Gibbs static int
21138b8a9b1dSJustin T. Gibbs xptedtbusfunc(struct cam_eb *bus, void *arg)
21148b8a9b1dSJustin T. Gibbs {
21158b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
21168b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
21178b8a9b1dSJustin T. Gibbs 
21188b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
21198b8a9b1dSJustin T. Gibbs 
21208b8a9b1dSJustin T. Gibbs 	/*
21218b8a9b1dSJustin T. Gibbs 	 * If our position is for something deeper in the tree, that means
21228b8a9b1dSJustin T. Gibbs 	 * that we've already seen this node.  So, we keep going down.
21238b8a9b1dSJustin T. Gibbs 	 */
21248b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
21258b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == bus)
21268b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
21278b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target != NULL))
21288b8a9b1dSJustin T. Gibbs 		retval = DM_RET_DESCEND;
21298b8a9b1dSJustin T. Gibbs 	else
21308b8a9b1dSJustin T. Gibbs 		retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
21318b8a9b1dSJustin T. Gibbs 
21328b8a9b1dSJustin T. Gibbs 	/*
21338b8a9b1dSJustin T. Gibbs 	 * If we got an error, bail out of the search.
21348b8a9b1dSJustin T. Gibbs 	 */
21358b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
21368b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
21378b8a9b1dSJustin T. Gibbs 		return(0);
21388b8a9b1dSJustin T. Gibbs 	}
21398b8a9b1dSJustin T. Gibbs 
21408b8a9b1dSJustin T. Gibbs 	/*
21418b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this bus out.
21428b8a9b1dSJustin T. Gibbs 	 */
21438b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
21448b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
21458b8a9b1dSJustin T. Gibbs 
21468b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
21478b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
21488b8a9b1dSJustin T. Gibbs 
21498b8a9b1dSJustin T. Gibbs 		/*
21508b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
21518b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
21528b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
21538b8a9b1dSJustin T. Gibbs 		 */
21548b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
21558b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
21568b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
21578b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
21588b8a9b1dSJustin T. Gibbs 
21598b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = bus;
21608b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
21612b83592fSScott Long 				xsoftc.bus_generation;
21628b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
21638b8a9b1dSJustin T. Gibbs 			return(0);
21648b8a9b1dSJustin T. Gibbs 		}
21658b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
21668b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
21678b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_BUS;
21688b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.path_id = bus->path_id;
21698b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
21708b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.unit_number =
21718b8a9b1dSJustin T. Gibbs 			bus->sim->unit_number;
21728b8a9b1dSJustin T. Gibbs 		strncpy(cdm->matches[j].result.bus_result.dev_name,
21738b8a9b1dSJustin T. Gibbs 			bus->sim->sim_name, DEV_IDLEN);
21748b8a9b1dSJustin T. Gibbs 	}
21758b8a9b1dSJustin T. Gibbs 
21768b8a9b1dSJustin T. Gibbs 	/*
21778b8a9b1dSJustin T. Gibbs 	 * If the user is only interested in busses, there's no
21788b8a9b1dSJustin T. Gibbs 	 * reason to descend to the next level in the tree.
21798b8a9b1dSJustin T. Gibbs 	 */
21808b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
21818b8a9b1dSJustin T. Gibbs 		return(1);
21828b8a9b1dSJustin T. Gibbs 
21838b8a9b1dSJustin T. Gibbs 	/*
21848b8a9b1dSJustin T. Gibbs 	 * If there is a target generation recorded, check it to
21858b8a9b1dSJustin T. Gibbs 	 * make sure the target list hasn't changed.
21868b8a9b1dSJustin T. Gibbs 	 */
21878b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
21888b8a9b1dSJustin T. Gibbs 	 && (bus == cdm->pos.cookie.bus)
21898b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
21908b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
21918b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
21928b8a9b1dSJustin T. Gibbs 	     bus->generation)) {
21938b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
21948b8a9b1dSJustin T. Gibbs 		return(0);
21958b8a9b1dSJustin T. Gibbs 	}
21968b8a9b1dSJustin T. Gibbs 
21978b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
21988b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == bus)
21998b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
22008b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target != NULL))
22018b8a9b1dSJustin T. Gibbs 		return(xpttargettraverse(bus,
22028b8a9b1dSJustin T. Gibbs 					(struct cam_et *)cdm->pos.cookie.target,
22038b8a9b1dSJustin T. Gibbs 					 xptedttargetfunc, arg));
22048b8a9b1dSJustin T. Gibbs 	else
22058b8a9b1dSJustin T. Gibbs 		return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
22068b8a9b1dSJustin T. Gibbs }
22078b8a9b1dSJustin T. Gibbs 
22088b8a9b1dSJustin T. Gibbs static int
22098b8a9b1dSJustin T. Gibbs xptedttargetfunc(struct cam_et *target, void *arg)
22108b8a9b1dSJustin T. Gibbs {
22118b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
22128b8a9b1dSJustin T. Gibbs 
22138b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
22148b8a9b1dSJustin T. Gibbs 
22158b8a9b1dSJustin T. Gibbs 	/*
22168b8a9b1dSJustin T. Gibbs 	 * If there is a device list generation recorded, check it to
22178b8a9b1dSJustin T. Gibbs 	 * make sure the device list hasn't changed.
22188b8a9b1dSJustin T. Gibbs 	 */
22198b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
22208b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == target->bus)
22218b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
22228b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == target)
22238b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
22248b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
22258b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
22268b8a9b1dSJustin T. Gibbs 	     target->generation)) {
22278b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
22288b8a9b1dSJustin T. Gibbs 		return(0);
22298b8a9b1dSJustin T. Gibbs 	}
22308b8a9b1dSJustin T. Gibbs 
22318b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
22328b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == target->bus)
22338b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
22348b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == target)
22358b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
22368b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device != NULL))
22378b8a9b1dSJustin T. Gibbs 		return(xptdevicetraverse(target,
22388b8a9b1dSJustin T. Gibbs 					(struct cam_ed *)cdm->pos.cookie.device,
22398b8a9b1dSJustin T. Gibbs 					 xptedtdevicefunc, arg));
22408b8a9b1dSJustin T. Gibbs 	else
22418b8a9b1dSJustin T. Gibbs 		return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
22428b8a9b1dSJustin T. Gibbs }
22438b8a9b1dSJustin T. Gibbs 
22448b8a9b1dSJustin T. Gibbs static int
22458b8a9b1dSJustin T. Gibbs xptedtdevicefunc(struct cam_ed *device, void *arg)
22468b8a9b1dSJustin T. Gibbs {
22478b8a9b1dSJustin T. Gibbs 
22488b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
22498b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
22508b8a9b1dSJustin T. Gibbs 
22518b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
22528b8a9b1dSJustin T. Gibbs 
22538b8a9b1dSJustin T. Gibbs 	/*
22548b8a9b1dSJustin T. Gibbs 	 * If our position is for something deeper in the tree, that means
22558b8a9b1dSJustin T. Gibbs 	 * that we've already seen this node.  So, we keep going down.
22568b8a9b1dSJustin T. Gibbs 	 */
22578b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
22588b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device == device)
22598b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
22608b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.periph != NULL))
22618b8a9b1dSJustin T. Gibbs 		retval = DM_RET_DESCEND;
22628b8a9b1dSJustin T. Gibbs 	else
22638b8a9b1dSJustin T. Gibbs 		retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
22648b8a9b1dSJustin T. Gibbs 					device);
22658b8a9b1dSJustin T. Gibbs 
22668b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
22678b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
22688b8a9b1dSJustin T. Gibbs 		return(0);
22698b8a9b1dSJustin T. Gibbs 	}
22708b8a9b1dSJustin T. Gibbs 
22718b8a9b1dSJustin T. Gibbs 	/*
22728b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this device out.
22738b8a9b1dSJustin T. Gibbs 	 */
22748b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
22758b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
22768b8a9b1dSJustin T. Gibbs 
22778b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
22788b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
22798b8a9b1dSJustin T. Gibbs 
22808b8a9b1dSJustin T. Gibbs 		/*
22818b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
22828b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
22838b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
22848b8a9b1dSJustin T. Gibbs 		 */
22858b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
22868b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
22878b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
22888b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
22898b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
22908b8a9b1dSJustin T. Gibbs 
22918b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = device->target->bus;
22928b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
22932b83592fSScott Long 				xsoftc.bus_generation;
22948b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.target = device->target;
22958b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_TARGET_GENERATION] =
22968b8a9b1dSJustin T. Gibbs 				device->target->bus->generation;
22978b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.device = device;
22988b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_DEV_GENERATION] =
22998b8a9b1dSJustin T. Gibbs 				device->target->generation;
23008b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
23018b8a9b1dSJustin T. Gibbs 			return(0);
23028b8a9b1dSJustin T. Gibbs 		}
23038b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
23048b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
23058b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_DEVICE;
23068b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.path_id =
23078b8a9b1dSJustin T. Gibbs 			device->target->bus->path_id;
23088b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.target_id =
23098b8a9b1dSJustin T. Gibbs 			device->target->target_id;
23108b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.target_lun =
23118b8a9b1dSJustin T. Gibbs 			device->lun_id;
23128b8a9b1dSJustin T. Gibbs 		bcopy(&device->inq_data,
23138b8a9b1dSJustin T. Gibbs 		      &cdm->matches[j].result.device_result.inq_data,
23148b8a9b1dSJustin T. Gibbs 		      sizeof(struct scsi_inquiry_data));
23159deea857SKenneth D. Merry 
23169deea857SKenneth D. Merry 		/* Let the user know whether this device is unconfigured */
23179deea857SKenneth D. Merry 		if (device->flags & CAM_DEV_UNCONFIGURED)
23189deea857SKenneth D. Merry 			cdm->matches[j].result.device_result.flags =
23199deea857SKenneth D. Merry 				DEV_RESULT_UNCONFIGURED;
23209deea857SKenneth D. Merry 		else
23219deea857SKenneth D. Merry 			cdm->matches[j].result.device_result.flags =
23229deea857SKenneth D. Merry 				DEV_RESULT_NOFLAG;
23238b8a9b1dSJustin T. Gibbs 	}
23248b8a9b1dSJustin T. Gibbs 
23258b8a9b1dSJustin T. Gibbs 	/*
23268b8a9b1dSJustin T. Gibbs 	 * If the user isn't interested in peripherals, don't descend
23278b8a9b1dSJustin T. Gibbs 	 * the tree any further.
23288b8a9b1dSJustin T. Gibbs 	 */
23298b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
23308b8a9b1dSJustin T. Gibbs 		return(1);
23318b8a9b1dSJustin T. Gibbs 
23328b8a9b1dSJustin T. Gibbs 	/*
23338b8a9b1dSJustin T. Gibbs 	 * If there is a peripheral list generation recorded, make sure
23348b8a9b1dSJustin T. Gibbs 	 * it hasn't changed.
23358b8a9b1dSJustin T. Gibbs 	 */
23368b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
23378b8a9b1dSJustin T. Gibbs 	 && (device->target->bus == cdm->pos.cookie.bus)
23388b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
23398b8a9b1dSJustin T. Gibbs 	 && (device->target == cdm->pos.cookie.target)
23408b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
23418b8a9b1dSJustin T. Gibbs 	 && (device == cdm->pos.cookie.device)
23428b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
23438b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
23448b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
23458b8a9b1dSJustin T. Gibbs 	     device->generation)){
23468b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
23478b8a9b1dSJustin T. Gibbs 		return(0);
23488b8a9b1dSJustin T. Gibbs 	}
23498b8a9b1dSJustin T. Gibbs 
23508b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
23518b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == device->target->bus)
23528b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
23538b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == device->target)
23548b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
23558b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device == device)
23568b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
23578b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.periph != NULL))
23588b8a9b1dSJustin T. Gibbs 		return(xptperiphtraverse(device,
23598b8a9b1dSJustin T. Gibbs 				(struct cam_periph *)cdm->pos.cookie.periph,
23608b8a9b1dSJustin T. Gibbs 				xptedtperiphfunc, arg));
23618b8a9b1dSJustin T. Gibbs 	else
23628b8a9b1dSJustin T. Gibbs 		return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
23638b8a9b1dSJustin T. Gibbs }
23648b8a9b1dSJustin T. Gibbs 
23658b8a9b1dSJustin T. Gibbs static int
23668b8a9b1dSJustin T. Gibbs xptedtperiphfunc(struct cam_periph *periph, void *arg)
23678b8a9b1dSJustin T. Gibbs {
23688b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
23698b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
23708b8a9b1dSJustin T. Gibbs 
23718b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
23728b8a9b1dSJustin T. Gibbs 
23738b8a9b1dSJustin T. Gibbs 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
23748b8a9b1dSJustin T. Gibbs 
23758b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
23768b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
23778b8a9b1dSJustin T. Gibbs 		return(0);
23788b8a9b1dSJustin T. Gibbs 	}
23798b8a9b1dSJustin T. Gibbs 
23808b8a9b1dSJustin T. Gibbs 	/*
23818b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this peripheral out.
23828b8a9b1dSJustin T. Gibbs 	 */
23838b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
23848b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
23858b8a9b1dSJustin T. Gibbs 
23868b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
23878b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
23888b8a9b1dSJustin T. Gibbs 
23898b8a9b1dSJustin T. Gibbs 		/*
23908b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
23918b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
23928b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
23938b8a9b1dSJustin T. Gibbs 		 */
23948b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
23958b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
23968b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
23978b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
23988b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
23998b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PERIPH;
24008b8a9b1dSJustin T. Gibbs 
24018b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = periph->path->bus;
24028b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
24032b83592fSScott Long 				xsoftc.bus_generation;
24048b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.target = periph->path->target;
24058b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_TARGET_GENERATION] =
24068b8a9b1dSJustin T. Gibbs 				periph->path->bus->generation;
24078b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.device = periph->path->device;
24088b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_DEV_GENERATION] =
24098b8a9b1dSJustin T. Gibbs 				periph->path->target->generation;
24108b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.periph = periph;
24118b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
24128b8a9b1dSJustin T. Gibbs 				periph->path->device->generation;
24138b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
24148b8a9b1dSJustin T. Gibbs 			return(0);
24158b8a9b1dSJustin T. Gibbs 		}
24168b8a9b1dSJustin T. Gibbs 
24178b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
24188b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
24198b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_PERIPH;
24208b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.path_id =
24218b8a9b1dSJustin T. Gibbs 			periph->path->bus->path_id;
24228b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.target_id =
24238b8a9b1dSJustin T. Gibbs 			periph->path->target->target_id;
24248b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.target_lun =
24258b8a9b1dSJustin T. Gibbs 			periph->path->device->lun_id;
24268b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.unit_number =
24278b8a9b1dSJustin T. Gibbs 			periph->unit_number;
24288b8a9b1dSJustin T. Gibbs 		strncpy(cdm->matches[j].result.periph_result.periph_name,
24298b8a9b1dSJustin T. Gibbs 			periph->periph_name, DEV_IDLEN);
24308b8a9b1dSJustin T. Gibbs 	}
24318b8a9b1dSJustin T. Gibbs 
24328b8a9b1dSJustin T. Gibbs 	return(1);
24338b8a9b1dSJustin T. Gibbs }
24348b8a9b1dSJustin T. Gibbs 
24358b8a9b1dSJustin T. Gibbs static int
24368b8a9b1dSJustin T. Gibbs xptedtmatch(struct ccb_dev_match *cdm)
24378b8a9b1dSJustin T. Gibbs {
24388b8a9b1dSJustin T. Gibbs 	int ret;
24398b8a9b1dSJustin T. Gibbs 
24408b8a9b1dSJustin T. Gibbs 	cdm->num_matches = 0;
24418b8a9b1dSJustin T. Gibbs 
24428b8a9b1dSJustin T. Gibbs 	/*
24438b8a9b1dSJustin T. Gibbs 	 * Check the bus list generation.  If it has changed, the user
24448b8a9b1dSJustin T. Gibbs 	 * needs to reset everything and start over.
24458b8a9b1dSJustin T. Gibbs 	 */
24468b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
24478b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
24482b83592fSScott Long 	 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) {
24498b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
24508b8a9b1dSJustin T. Gibbs 		return(0);
24518b8a9b1dSJustin T. Gibbs 	}
24528b8a9b1dSJustin T. Gibbs 
24538b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
24548b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus != NULL))
24558b8a9b1dSJustin T. Gibbs 		ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
24568b8a9b1dSJustin T. Gibbs 				     xptedtbusfunc, cdm);
24578b8a9b1dSJustin T. Gibbs 	else
24588b8a9b1dSJustin T. Gibbs 		ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
24598b8a9b1dSJustin T. Gibbs 
24608b8a9b1dSJustin T. Gibbs 	/*
24618b8a9b1dSJustin T. Gibbs 	 * If we get back 0, that means that we had to stop before fully
24628b8a9b1dSJustin T. Gibbs 	 * traversing the EDT.  It also means that one of the subroutines
24638b8a9b1dSJustin T. Gibbs 	 * has set the status field to the proper value.  If we get back 1,
24648b8a9b1dSJustin T. Gibbs 	 * we've fully traversed the EDT and copied out any matching entries.
24658b8a9b1dSJustin T. Gibbs 	 */
24668b8a9b1dSJustin T. Gibbs 	if (ret == 1)
24678b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LAST;
24688b8a9b1dSJustin T. Gibbs 
24698b8a9b1dSJustin T. Gibbs 	return(ret);
24708b8a9b1dSJustin T. Gibbs }
24718b8a9b1dSJustin T. Gibbs 
24728b8a9b1dSJustin T. Gibbs static int
24738b8a9b1dSJustin T. Gibbs xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
24748b8a9b1dSJustin T. Gibbs {
24758b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
24768b8a9b1dSJustin T. Gibbs 
24778b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
24788b8a9b1dSJustin T. Gibbs 
24798b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
24808b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv == pdrv)
24818b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
24828b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
24838b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
24848b8a9b1dSJustin T. Gibbs 	     (*pdrv)->generation)) {
24858b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
24868b8a9b1dSJustin T. Gibbs 		return(0);
24878b8a9b1dSJustin T. Gibbs 	}
24888b8a9b1dSJustin T. Gibbs 
24898b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
24908b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv == pdrv)
24918b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
24928b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.periph != NULL))
24938b8a9b1dSJustin T. Gibbs 		return(xptpdperiphtraverse(pdrv,
24948b8a9b1dSJustin T. Gibbs 				(struct cam_periph *)cdm->pos.cookie.periph,
24958b8a9b1dSJustin T. Gibbs 				xptplistperiphfunc, arg));
24968b8a9b1dSJustin T. Gibbs 	else
24978b8a9b1dSJustin T. Gibbs 		return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
24988b8a9b1dSJustin T. Gibbs }
24998b8a9b1dSJustin T. Gibbs 
25008b8a9b1dSJustin T. Gibbs static int
25018b8a9b1dSJustin T. Gibbs xptplistperiphfunc(struct cam_periph *periph, void *arg)
25028b8a9b1dSJustin T. Gibbs {
25038b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
25048b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
25058b8a9b1dSJustin T. Gibbs 
25068b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
25078b8a9b1dSJustin T. Gibbs 
25088b8a9b1dSJustin T. Gibbs 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
25098b8a9b1dSJustin T. Gibbs 
25108b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
25118b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
25128b8a9b1dSJustin T. Gibbs 		return(0);
25138b8a9b1dSJustin T. Gibbs 	}
25148b8a9b1dSJustin T. Gibbs 
25158b8a9b1dSJustin T. Gibbs 	/*
25168b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this peripheral out.
25178b8a9b1dSJustin T. Gibbs 	 */
25188b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
25198b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
25208b8a9b1dSJustin T. Gibbs 
25218b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
25228b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
25238b8a9b1dSJustin T. Gibbs 
25248b8a9b1dSJustin T. Gibbs 		/*
25258b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
25268b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
25278b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
25288b8a9b1dSJustin T. Gibbs 		 */
25298b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
25308b8a9b1dSJustin T. Gibbs 			struct periph_driver **pdrv;
25318b8a9b1dSJustin T. Gibbs 
25328b8a9b1dSJustin T. Gibbs 			pdrv = NULL;
25338b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
25348b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
25358b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
25368b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PERIPH;
25378b8a9b1dSJustin T. Gibbs 
25388b8a9b1dSJustin T. Gibbs 			/*
25398b8a9b1dSJustin T. Gibbs 			 * This may look a bit non-sensical, but it is
25408b8a9b1dSJustin T. Gibbs 			 * actually quite logical.  There are very few
25418b8a9b1dSJustin T. Gibbs 			 * peripheral drivers, and bloating every peripheral
25428b8a9b1dSJustin T. Gibbs 			 * structure with a pointer back to its parent
25438b8a9b1dSJustin T. Gibbs 			 * peripheral driver linker set entry would cost
25448b8a9b1dSJustin T. Gibbs 			 * more in the long run than doing this quick lookup.
25458b8a9b1dSJustin T. Gibbs 			 */
25460b7c27b9SPeter Wemm 			for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
25478b8a9b1dSJustin T. Gibbs 				if (strcmp((*pdrv)->driver_name,
25488b8a9b1dSJustin T. Gibbs 				    periph->periph_name) == 0)
25498b8a9b1dSJustin T. Gibbs 					break;
25508b8a9b1dSJustin T. Gibbs 			}
25518b8a9b1dSJustin T. Gibbs 
255201910babSScott Long 			if (*pdrv == NULL) {
25538b8a9b1dSJustin T. Gibbs 				cdm->status = CAM_DEV_MATCH_ERROR;
25548b8a9b1dSJustin T. Gibbs 				return(0);
25558b8a9b1dSJustin T. Gibbs 			}
25568b8a9b1dSJustin T. Gibbs 
25578b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.pdrv = pdrv;
25588b8a9b1dSJustin T. Gibbs 			/*
25598b8a9b1dSJustin T. Gibbs 			 * The periph generation slot does double duty, as
25608b8a9b1dSJustin T. Gibbs 			 * does the periph pointer slot.  They are used for
25618b8a9b1dSJustin T. Gibbs 			 * both edt and pdrv lookups and positioning.
25628b8a9b1dSJustin T. Gibbs 			 */
25638b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.periph = periph;
25648b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
25658b8a9b1dSJustin T. Gibbs 				(*pdrv)->generation;
25668b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
25678b8a9b1dSJustin T. Gibbs 			return(0);
25688b8a9b1dSJustin T. Gibbs 		}
25698b8a9b1dSJustin T. Gibbs 
25708b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
25718b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
25728b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_PERIPH;
25738b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.path_id =
25748b8a9b1dSJustin T. Gibbs 			periph->path->bus->path_id;
25758b8a9b1dSJustin T. Gibbs 
25768b8a9b1dSJustin T. Gibbs 		/*
25778b8a9b1dSJustin T. Gibbs 		 * The transport layer peripheral doesn't have a target or
25788b8a9b1dSJustin T. Gibbs 		 * lun.
25798b8a9b1dSJustin T. Gibbs 		 */
25808b8a9b1dSJustin T. Gibbs 		if (periph->path->target)
25818b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_id =
25828b8a9b1dSJustin T. Gibbs 				periph->path->target->target_id;
25838b8a9b1dSJustin T. Gibbs 		else
25848b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_id = -1;
25858b8a9b1dSJustin T. Gibbs 
25868b8a9b1dSJustin T. Gibbs 		if (periph->path->device)
25878b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_lun =
25888b8a9b1dSJustin T. Gibbs 				periph->path->device->lun_id;
25898b8a9b1dSJustin T. Gibbs 		else
25908b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_lun = -1;
25918b8a9b1dSJustin T. Gibbs 
25928b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.unit_number =
25938b8a9b1dSJustin T. Gibbs 			periph->unit_number;
25948b8a9b1dSJustin T. Gibbs 		strncpy(cdm->matches[j].result.periph_result.periph_name,
25958b8a9b1dSJustin T. Gibbs 			periph->periph_name, DEV_IDLEN);
25968b8a9b1dSJustin T. Gibbs 	}
25978b8a9b1dSJustin T. Gibbs 
25988b8a9b1dSJustin T. Gibbs 	return(1);
25998b8a9b1dSJustin T. Gibbs }
26008b8a9b1dSJustin T. Gibbs 
26018b8a9b1dSJustin T. Gibbs static int
26028b8a9b1dSJustin T. Gibbs xptperiphlistmatch(struct ccb_dev_match *cdm)
26038b8a9b1dSJustin T. Gibbs {
26048b8a9b1dSJustin T. Gibbs 	int ret;
26058b8a9b1dSJustin T. Gibbs 
26068b8a9b1dSJustin T. Gibbs 	cdm->num_matches = 0;
26078b8a9b1dSJustin T. Gibbs 
26088b8a9b1dSJustin T. Gibbs 	/*
26098b8a9b1dSJustin T. Gibbs 	 * At this point in the edt traversal function, we check the bus
26108b8a9b1dSJustin T. Gibbs 	 * list generation to make sure that no busses have been added or
26118b8a9b1dSJustin T. Gibbs 	 * removed since the user last sent a XPT_DEV_MATCH ccb through.
26128b8a9b1dSJustin T. Gibbs 	 * For the peripheral driver list traversal function, however, we
26138b8a9b1dSJustin T. Gibbs 	 * don't have to worry about new peripheral driver types coming or
26148b8a9b1dSJustin T. Gibbs 	 * going; they're in a linker set, and therefore can't change
26158b8a9b1dSJustin T. Gibbs 	 * without a recompile.
26168b8a9b1dSJustin T. Gibbs 	 */
26178b8a9b1dSJustin T. Gibbs 
26188b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
26198b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv != NULL))
26208b8a9b1dSJustin T. Gibbs 		ret = xptpdrvtraverse(
26218b8a9b1dSJustin T. Gibbs 				(struct periph_driver **)cdm->pos.cookie.pdrv,
26228b8a9b1dSJustin T. Gibbs 				xptplistpdrvfunc, cdm);
26238b8a9b1dSJustin T. Gibbs 	else
26248b8a9b1dSJustin T. Gibbs 		ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
26258b8a9b1dSJustin T. Gibbs 
26268b8a9b1dSJustin T. Gibbs 	/*
26278b8a9b1dSJustin T. Gibbs 	 * If we get back 0, that means that we had to stop before fully
26288b8a9b1dSJustin T. Gibbs 	 * traversing the peripheral driver tree.  It also means that one of
26298b8a9b1dSJustin T. Gibbs 	 * the subroutines has set the status field to the proper value.  If
26308b8a9b1dSJustin T. Gibbs 	 * we get back 1, we've fully traversed the EDT and copied out any
26318b8a9b1dSJustin T. Gibbs 	 * matching entries.
26328b8a9b1dSJustin T. Gibbs 	 */
26338b8a9b1dSJustin T. Gibbs 	if (ret == 1)
26348b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LAST;
26358b8a9b1dSJustin T. Gibbs 
26368b8a9b1dSJustin T. Gibbs 	return(ret);
26378b8a9b1dSJustin T. Gibbs }
26388b8a9b1dSJustin T. Gibbs 
26398b8a9b1dSJustin T. Gibbs static int
26408b8a9b1dSJustin T. Gibbs xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
26418b8a9b1dSJustin T. Gibbs {
26428b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus, *next_bus;
26438b8a9b1dSJustin T. Gibbs 	int retval;
26448b8a9b1dSJustin T. Gibbs 
26458b8a9b1dSJustin T. Gibbs 	retval = 1;
26468b8a9b1dSJustin T. Gibbs 
26472b83592fSScott Long 	mtx_lock(&xsoftc.xpt_topo_lock);
26482b83592fSScott Long 	for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
26498b8a9b1dSJustin T. Gibbs 	     bus != NULL;
26508b8a9b1dSJustin T. Gibbs 	     bus = next_bus) {
26518b8a9b1dSJustin T. Gibbs 		next_bus = TAILQ_NEXT(bus, links);
26528b8a9b1dSJustin T. Gibbs 
26532b83592fSScott Long 		mtx_unlock(&xsoftc.xpt_topo_lock);
265411e4faceSScott Long 		CAM_SIM_LOCK(bus->sim);
26558b8a9b1dSJustin T. Gibbs 		retval = tr_func(bus, arg);
265611e4faceSScott Long 		CAM_SIM_UNLOCK(bus->sim);
26578b8a9b1dSJustin T. Gibbs 		if (retval == 0)
26588b8a9b1dSJustin T. Gibbs 			return(retval);
26592b83592fSScott Long 		mtx_lock(&xsoftc.xpt_topo_lock);
26608b8a9b1dSJustin T. Gibbs 	}
26612b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_topo_lock);
26628b8a9b1dSJustin T. Gibbs 
26638b8a9b1dSJustin T. Gibbs 	return(retval);
26648b8a9b1dSJustin T. Gibbs }
26658b8a9b1dSJustin T. Gibbs 
26668b8a9b1dSJustin T. Gibbs static int
26678b8a9b1dSJustin T. Gibbs xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
26688b8a9b1dSJustin T. Gibbs 		  xpt_targetfunc_t *tr_func, void *arg)
26698b8a9b1dSJustin T. Gibbs {
26708b8a9b1dSJustin T. Gibbs 	struct cam_et *target, *next_target;
26718b8a9b1dSJustin T. Gibbs 	int retval;
26728b8a9b1dSJustin T. Gibbs 
26738b8a9b1dSJustin T. Gibbs 	retval = 1;
26748b8a9b1dSJustin T. Gibbs 	for (target = (start_target ? start_target :
26758b8a9b1dSJustin T. Gibbs 		       TAILQ_FIRST(&bus->et_entries));
26768b8a9b1dSJustin T. Gibbs 	     target != NULL; target = next_target) {
26778b8a9b1dSJustin T. Gibbs 
26788b8a9b1dSJustin T. Gibbs 		next_target = TAILQ_NEXT(target, links);
26798b8a9b1dSJustin T. Gibbs 
26808b8a9b1dSJustin T. Gibbs 		retval = tr_func(target, arg);
26818b8a9b1dSJustin T. Gibbs 
26828b8a9b1dSJustin T. Gibbs 		if (retval == 0)
26838b8a9b1dSJustin T. Gibbs 			return(retval);
26848b8a9b1dSJustin T. Gibbs 	}
26858b8a9b1dSJustin T. Gibbs 
26868b8a9b1dSJustin T. Gibbs 	return(retval);
26878b8a9b1dSJustin T. Gibbs }
26888b8a9b1dSJustin T. Gibbs 
26898b8a9b1dSJustin T. Gibbs static int
26908b8a9b1dSJustin T. Gibbs xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
26918b8a9b1dSJustin T. Gibbs 		  xpt_devicefunc_t *tr_func, void *arg)
26928b8a9b1dSJustin T. Gibbs {
26938b8a9b1dSJustin T. Gibbs 	struct cam_ed *device, *next_device;
26948b8a9b1dSJustin T. Gibbs 	int retval;
26958b8a9b1dSJustin T. Gibbs 
26968b8a9b1dSJustin T. Gibbs 	retval = 1;
26978b8a9b1dSJustin T. Gibbs 	for (device = (start_device ? start_device :
26988b8a9b1dSJustin T. Gibbs 		       TAILQ_FIRST(&target->ed_entries));
26998b8a9b1dSJustin T. Gibbs 	     device != NULL;
27008b8a9b1dSJustin T. Gibbs 	     device = next_device) {
27018b8a9b1dSJustin T. Gibbs 
27028b8a9b1dSJustin T. Gibbs 		next_device = TAILQ_NEXT(device, links);
27038b8a9b1dSJustin T. Gibbs 
27048b8a9b1dSJustin T. Gibbs 		retval = tr_func(device, arg);
27058b8a9b1dSJustin T. Gibbs 
27068b8a9b1dSJustin T. Gibbs 		if (retval == 0)
27078b8a9b1dSJustin T. Gibbs 			return(retval);
27088b8a9b1dSJustin T. Gibbs 	}
27098b8a9b1dSJustin T. Gibbs 
27108b8a9b1dSJustin T. Gibbs 	return(retval);
27118b8a9b1dSJustin T. Gibbs }
27128b8a9b1dSJustin T. Gibbs 
27138b8a9b1dSJustin T. Gibbs static int
27148b8a9b1dSJustin T. Gibbs xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
27158b8a9b1dSJustin T. Gibbs 		  xpt_periphfunc_t *tr_func, void *arg)
27168b8a9b1dSJustin T. Gibbs {
27178b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph, *next_periph;
27188b8a9b1dSJustin T. Gibbs 	int retval;
27198b8a9b1dSJustin T. Gibbs 
27208b8a9b1dSJustin T. Gibbs 	retval = 1;
27218b8a9b1dSJustin T. Gibbs 
27228b8a9b1dSJustin T. Gibbs 	for (periph = (start_periph ? start_periph :
27238b8a9b1dSJustin T. Gibbs 		       SLIST_FIRST(&device->periphs));
27248b8a9b1dSJustin T. Gibbs 	     periph != NULL;
27258b8a9b1dSJustin T. Gibbs 	     periph = next_periph) {
27268b8a9b1dSJustin T. Gibbs 
27278b8a9b1dSJustin T. Gibbs 		next_periph = SLIST_NEXT(periph, periph_links);
27288b8a9b1dSJustin T. Gibbs 
27298b8a9b1dSJustin T. Gibbs 		retval = tr_func(periph, arg);
27308b8a9b1dSJustin T. Gibbs 		if (retval == 0)
27318b8a9b1dSJustin T. Gibbs 			return(retval);
27328b8a9b1dSJustin T. Gibbs 	}
27338b8a9b1dSJustin T. Gibbs 
27348b8a9b1dSJustin T. Gibbs 	return(retval);
27358b8a9b1dSJustin T. Gibbs }
27368b8a9b1dSJustin T. Gibbs 
27378b8a9b1dSJustin T. Gibbs static int
27388b8a9b1dSJustin T. Gibbs xptpdrvtraverse(struct periph_driver **start_pdrv,
27398b8a9b1dSJustin T. Gibbs 		xpt_pdrvfunc_t *tr_func, void *arg)
27408b8a9b1dSJustin T. Gibbs {
27418b8a9b1dSJustin T. Gibbs 	struct periph_driver **pdrv;
27428b8a9b1dSJustin T. Gibbs 	int retval;
27438b8a9b1dSJustin T. Gibbs 
27448b8a9b1dSJustin T. Gibbs 	retval = 1;
27458b8a9b1dSJustin T. Gibbs 
27468b8a9b1dSJustin T. Gibbs 	/*
27478b8a9b1dSJustin T. Gibbs 	 * We don't traverse the peripheral driver list like we do the
27488b8a9b1dSJustin T. Gibbs 	 * other lists, because it is a linker set, and therefore cannot be
27498b8a9b1dSJustin T. Gibbs 	 * changed during runtime.  If the peripheral driver list is ever
27508b8a9b1dSJustin T. Gibbs 	 * re-done to be something other than a linker set (i.e. it can
27518b8a9b1dSJustin T. Gibbs 	 * change while the system is running), the list traversal should
27528b8a9b1dSJustin T. Gibbs 	 * be modified to work like the other traversal functions.
27538b8a9b1dSJustin T. Gibbs 	 */
27540b7c27b9SPeter Wemm 	for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
27558b8a9b1dSJustin T. Gibbs 	     *pdrv != NULL; pdrv++) {
27568b8a9b1dSJustin T. Gibbs 		retval = tr_func(pdrv, arg);
27578b8a9b1dSJustin T. Gibbs 
27588b8a9b1dSJustin T. Gibbs 		if (retval == 0)
27598b8a9b1dSJustin T. Gibbs 			return(retval);
27608b8a9b1dSJustin T. Gibbs 	}
27618b8a9b1dSJustin T. Gibbs 
27628b8a9b1dSJustin T. Gibbs 	return(retval);
27638b8a9b1dSJustin T. Gibbs }
27648b8a9b1dSJustin T. Gibbs 
27658b8a9b1dSJustin T. Gibbs static int
27668b8a9b1dSJustin T. Gibbs xptpdperiphtraverse(struct periph_driver **pdrv,
27678b8a9b1dSJustin T. Gibbs 		    struct cam_periph *start_periph,
27688b8a9b1dSJustin T. Gibbs 		    xpt_periphfunc_t *tr_func, void *arg)
27698b8a9b1dSJustin T. Gibbs {
27708b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph, *next_periph;
27718b8a9b1dSJustin T. Gibbs 	int retval;
27728b8a9b1dSJustin T. Gibbs 
27738b8a9b1dSJustin T. Gibbs 	retval = 1;
27748b8a9b1dSJustin T. Gibbs 
27758b8a9b1dSJustin T. Gibbs 	for (periph = (start_periph ? start_periph :
27768b8a9b1dSJustin T. Gibbs 	     TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
27778b8a9b1dSJustin T. Gibbs 	     periph = next_periph) {
27788b8a9b1dSJustin T. Gibbs 
27798b8a9b1dSJustin T. Gibbs 		next_periph = TAILQ_NEXT(periph, unit_links);
27808b8a9b1dSJustin T. Gibbs 
27818b8a9b1dSJustin T. Gibbs 		retval = tr_func(periph, arg);
27828b8a9b1dSJustin T. Gibbs 		if (retval == 0)
27838b8a9b1dSJustin T. Gibbs 			return(retval);
27848b8a9b1dSJustin T. Gibbs 	}
27858b8a9b1dSJustin T. Gibbs 	return(retval);
27868b8a9b1dSJustin T. Gibbs }
27878b8a9b1dSJustin T. Gibbs 
27888b8a9b1dSJustin T. Gibbs static int
27898b8a9b1dSJustin T. Gibbs xptdefbusfunc(struct cam_eb *bus, void *arg)
27908b8a9b1dSJustin T. Gibbs {
27918b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
27928b8a9b1dSJustin T. Gibbs 
27938b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
27948b8a9b1dSJustin T. Gibbs 
27958b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_BUS) {
27968b8a9b1dSJustin T. Gibbs 		xpt_busfunc_t *tr_func;
27978b8a9b1dSJustin T. Gibbs 
27988b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_busfunc_t *)tr_config->tr_func;
27998b8a9b1dSJustin T. Gibbs 
28008b8a9b1dSJustin T. Gibbs 		return(tr_func(bus, tr_config->tr_arg));
28018b8a9b1dSJustin T. Gibbs 	} else
28028b8a9b1dSJustin T. Gibbs 		return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
28038b8a9b1dSJustin T. Gibbs }
28048b8a9b1dSJustin T. Gibbs 
28058b8a9b1dSJustin T. Gibbs static int
28068b8a9b1dSJustin T. Gibbs xptdeftargetfunc(struct cam_et *target, void *arg)
28078b8a9b1dSJustin T. Gibbs {
28088b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
28098b8a9b1dSJustin T. Gibbs 
28108b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
28118b8a9b1dSJustin T. Gibbs 
28128b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_TARGET) {
28138b8a9b1dSJustin T. Gibbs 		xpt_targetfunc_t *tr_func;
28148b8a9b1dSJustin T. Gibbs 
28158b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
28168b8a9b1dSJustin T. Gibbs 
28178b8a9b1dSJustin T. Gibbs 		return(tr_func(target, tr_config->tr_arg));
28188b8a9b1dSJustin T. Gibbs 	} else
28198b8a9b1dSJustin T. Gibbs 		return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
28208b8a9b1dSJustin T. Gibbs }
28218b8a9b1dSJustin T. Gibbs 
28228b8a9b1dSJustin T. Gibbs static int
28238b8a9b1dSJustin T. Gibbs xptdefdevicefunc(struct cam_ed *device, void *arg)
28248b8a9b1dSJustin T. Gibbs {
28258b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
28268b8a9b1dSJustin T. Gibbs 
28278b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
28288b8a9b1dSJustin T. Gibbs 
28298b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_DEVICE) {
28308b8a9b1dSJustin T. Gibbs 		xpt_devicefunc_t *tr_func;
28318b8a9b1dSJustin T. Gibbs 
28328b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
28338b8a9b1dSJustin T. Gibbs 
28348b8a9b1dSJustin T. Gibbs 		return(tr_func(device, tr_config->tr_arg));
28358b8a9b1dSJustin T. Gibbs 	} else
28368b8a9b1dSJustin T. Gibbs 		return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
28378b8a9b1dSJustin T. Gibbs }
28388b8a9b1dSJustin T. Gibbs 
28398b8a9b1dSJustin T. Gibbs static int
28408b8a9b1dSJustin T. Gibbs xptdefperiphfunc(struct cam_periph *periph, void *arg)
28418b8a9b1dSJustin T. Gibbs {
28428b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
28438b8a9b1dSJustin T. Gibbs 	xpt_periphfunc_t *tr_func;
28448b8a9b1dSJustin T. Gibbs 
28458b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
28468b8a9b1dSJustin T. Gibbs 
28478b8a9b1dSJustin T. Gibbs 	tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
28488b8a9b1dSJustin T. Gibbs 
28498b8a9b1dSJustin T. Gibbs 	/*
28508b8a9b1dSJustin T. Gibbs 	 * Unlike the other default functions, we don't check for depth
28518b8a9b1dSJustin T. Gibbs 	 * here.  The peripheral driver level is the last level in the EDT,
28528b8a9b1dSJustin T. Gibbs 	 * so if we're here, we should execute the function in question.
28538b8a9b1dSJustin T. Gibbs 	 */
28548b8a9b1dSJustin T. Gibbs 	return(tr_func(periph, tr_config->tr_arg));
28558b8a9b1dSJustin T. Gibbs }
28568b8a9b1dSJustin T. Gibbs 
28578b8a9b1dSJustin T. Gibbs /*
28588b8a9b1dSJustin T. Gibbs  * Execute the given function for every bus in the EDT.
28598b8a9b1dSJustin T. Gibbs  */
28608b8a9b1dSJustin T. Gibbs static int
28618b8a9b1dSJustin T. Gibbs xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
28628b8a9b1dSJustin T. Gibbs {
28638b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
28648b8a9b1dSJustin T. Gibbs 
28658b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_BUS;
28668b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
28678b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
28688b8a9b1dSJustin T. Gibbs 
28698b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
28708b8a9b1dSJustin T. Gibbs }
28718b8a9b1dSJustin T. Gibbs 
28728b8a9b1dSJustin T. Gibbs /*
28738b8a9b1dSJustin T. Gibbs  * Execute the given function for every device in the EDT.
28748b8a9b1dSJustin T. Gibbs  */
28758b8a9b1dSJustin T. Gibbs static int
28768b8a9b1dSJustin T. Gibbs xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
28778b8a9b1dSJustin T. Gibbs {
28788b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
28798b8a9b1dSJustin T. Gibbs 
28808b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_DEVICE;
28818b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
28828b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
28838b8a9b1dSJustin T. Gibbs 
28848b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
28858b8a9b1dSJustin T. Gibbs }
28868b8a9b1dSJustin T. Gibbs 
28878b8a9b1dSJustin T. Gibbs static int
28888b8a9b1dSJustin T. Gibbs xptsetasyncfunc(struct cam_ed *device, void *arg)
28898b8a9b1dSJustin T. Gibbs {
28908b8a9b1dSJustin T. Gibbs 	struct cam_path path;
28918b8a9b1dSJustin T. Gibbs 	struct ccb_getdev cgd;
28928b8a9b1dSJustin T. Gibbs 	struct async_node *cur_entry;
28938b8a9b1dSJustin T. Gibbs 
28948b8a9b1dSJustin T. Gibbs 	cur_entry = (struct async_node *)arg;
28958b8a9b1dSJustin T. Gibbs 
2896c8bead2aSJustin T. Gibbs 	/*
2897c8bead2aSJustin T. Gibbs 	 * Don't report unconfigured devices (Wildcard devs,
2898c8bead2aSJustin T. Gibbs 	 * devices only for target mode, device instances
2899c8bead2aSJustin T. Gibbs 	 * that have been invalidated but are waiting for
2900c8bead2aSJustin T. Gibbs 	 * their last reference count to be released).
2901c8bead2aSJustin T. Gibbs 	 */
2902c8bead2aSJustin T. Gibbs 	if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2903c8bead2aSJustin T. Gibbs 		return (1);
2904c8bead2aSJustin T. Gibbs 
29058b8a9b1dSJustin T. Gibbs 	xpt_compile_path(&path,
29068b8a9b1dSJustin T. Gibbs 			 NULL,
29078b8a9b1dSJustin T. Gibbs 			 device->target->bus->path_id,
29088b8a9b1dSJustin T. Gibbs 			 device->target->target_id,
29098b8a9b1dSJustin T. Gibbs 			 device->lun_id);
29108b8a9b1dSJustin T. Gibbs 	xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
29118b8a9b1dSJustin T. Gibbs 	cgd.ccb_h.func_code = XPT_GDEV_TYPE;
29128b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&cgd);
29138b8a9b1dSJustin T. Gibbs 	cur_entry->callback(cur_entry->callback_arg,
29148b8a9b1dSJustin T. Gibbs 			    AC_FOUND_DEVICE,
29158b8a9b1dSJustin T. Gibbs 			    &path, &cgd);
29168b8a9b1dSJustin T. Gibbs 	xpt_release_path(&path);
29178b8a9b1dSJustin T. Gibbs 
29188b8a9b1dSJustin T. Gibbs 	return(1);
29198b8a9b1dSJustin T. Gibbs }
2920c8bead2aSJustin T. Gibbs 
29218b8a9b1dSJustin T. Gibbs static int
29228b8a9b1dSJustin T. Gibbs xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
29238b8a9b1dSJustin T. Gibbs {
29248b8a9b1dSJustin T. Gibbs 	struct cam_path path;
29258b8a9b1dSJustin T. Gibbs 	struct ccb_pathinq cpi;
29268b8a9b1dSJustin T. Gibbs 	struct async_node *cur_entry;
29278b8a9b1dSJustin T. Gibbs 
29288b8a9b1dSJustin T. Gibbs 	cur_entry = (struct async_node *)arg;
29298b8a9b1dSJustin T. Gibbs 
29308b8a9b1dSJustin T. Gibbs 	xpt_compile_path(&path, /*periph*/NULL,
29318b8a9b1dSJustin T. Gibbs 			 bus->sim->path_id,
29328b8a9b1dSJustin T. Gibbs 			 CAM_TARGET_WILDCARD,
29338b8a9b1dSJustin T. Gibbs 			 CAM_LUN_WILDCARD);
29348b8a9b1dSJustin T. Gibbs 	xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
29358b8a9b1dSJustin T. Gibbs 	cpi.ccb_h.func_code = XPT_PATH_INQ;
29368b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&cpi);
29378b8a9b1dSJustin T. Gibbs 	cur_entry->callback(cur_entry->callback_arg,
29388b8a9b1dSJustin T. Gibbs 			    AC_PATH_REGISTERED,
29398b8a9b1dSJustin T. Gibbs 			    &path, &cpi);
29408b8a9b1dSJustin T. Gibbs 	xpt_release_path(&path);
29418b8a9b1dSJustin T. Gibbs 
29428b8a9b1dSJustin T. Gibbs 	return(1);
29438b8a9b1dSJustin T. Gibbs }
29448b8a9b1dSJustin T. Gibbs 
29452b83592fSScott Long static void
29462b83592fSScott Long xpt_action_sasync_cb(void *context, int pending)
29472b83592fSScott Long {
29482b83592fSScott Long 	struct async_node *cur_entry;
294984f82481SScott Long 	struct xpt_task *task;
295084f82481SScott Long 	uint32_t added;
29512b83592fSScott Long 
29522b83592fSScott Long 	task = (struct xpt_task *)context;
295384f82481SScott Long 	cur_entry = (struct async_node *)task->data1;
295484f82481SScott Long 	added = task->data2;
29552b83592fSScott Long 
29562b83592fSScott Long 	if ((added & AC_FOUND_DEVICE) != 0) {
29572b83592fSScott Long 		/*
29582b83592fSScott Long 		 * Get this peripheral up to date with all
29592b83592fSScott Long 		 * the currently existing devices.
29602b83592fSScott Long 		 */
29612b83592fSScott Long 		xpt_for_all_devices(xptsetasyncfunc, cur_entry);
29622b83592fSScott Long 	}
29632b83592fSScott Long 	if ((added & AC_PATH_REGISTERED) != 0) {
29642b83592fSScott Long 		/*
29652b83592fSScott Long 		 * Get this peripheral up to date with all
29662b83592fSScott Long 		 * the currently existing busses.
29672b83592fSScott Long 		 */
29682b83592fSScott Long 		xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
29692b83592fSScott Long 		}
29702b83592fSScott Long 
29712b83592fSScott Long 	free(task, M_CAMXPT);
29722b83592fSScott Long }
29732b83592fSScott Long 
29748b8a9b1dSJustin T. Gibbs void
29758b8a9b1dSJustin T. Gibbs xpt_action(union ccb *start_ccb)
29768b8a9b1dSJustin T. Gibbs {
29779911ecf9SJustin T. Gibbs 
29788b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
29798b8a9b1dSJustin T. Gibbs 
29808b8a9b1dSJustin T. Gibbs 	start_ccb->ccb_h.status = CAM_REQ_INPROG;
29818b8a9b1dSJustin T. Gibbs 
29828b8a9b1dSJustin T. Gibbs 	switch (start_ccb->ccb_h.func_code) {
29838b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
2984d05caa00SKenneth D. Merry 	{
29853393f8daSKenneth D. Merry 		struct cam_ed *device;
2986d05caa00SKenneth D. Merry #ifdef CAMDEBUG
2987d05caa00SKenneth D. Merry 		char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2988d05caa00SKenneth D. Merry 		struct cam_path *path;
2989d05caa00SKenneth D. Merry 
2990d05caa00SKenneth D. Merry 		path = start_ccb->ccb_h.path;
2991d05caa00SKenneth D. Merry #endif
2992d05caa00SKenneth D. Merry 
29938b8a9b1dSJustin T. Gibbs 		/*
29948b8a9b1dSJustin T. Gibbs 		 * For the sake of compatibility with SCSI-1
29958b8a9b1dSJustin T. Gibbs 		 * devices that may not understand the identify
29968b8a9b1dSJustin T. Gibbs 		 * message, we include lun information in the
29978b8a9b1dSJustin T. Gibbs 		 * second byte of all commands.  SCSI-1 specifies
29988b8a9b1dSJustin T. Gibbs 		 * that luns are a 3 bit value and reserves only 3
29998b8a9b1dSJustin T. Gibbs 		 * bits for lun information in the CDB.  Later
30008b8a9b1dSJustin T. Gibbs 		 * revisions of the SCSI spec allow for more than 8
30018b8a9b1dSJustin T. Gibbs 		 * luns, but have deprecated lun information in the
30028b8a9b1dSJustin T. Gibbs 		 * CDB.  So, if the lun won't fit, we must omit.
30038b8a9b1dSJustin T. Gibbs 		 *
30048b8a9b1dSJustin T. Gibbs 		 * Also be aware that during initial probing for devices,
30058b8a9b1dSJustin T. Gibbs 		 * the inquiry information is unknown but initialized to 0.
30068b8a9b1dSJustin T. Gibbs 		 * This means that this code will be exercised while probing
30078b8a9b1dSJustin T. Gibbs 		 * devices with an ANSI revision greater than 2.
30088b8a9b1dSJustin T. Gibbs 		 */
30093393f8daSKenneth D. Merry 		device = start_ccb->ccb_h.path->device;
30103393f8daSKenneth D. Merry 		if (device->protocol_version <= SCSI_REV_2
30118b8a9b1dSJustin T. Gibbs 		 && start_ccb->ccb_h.target_lun < 8
30128b8a9b1dSJustin T. Gibbs 		 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
30138b8a9b1dSJustin T. Gibbs 
30148b8a9b1dSJustin T. Gibbs 			start_ccb->csio.cdb_io.cdb_bytes[1] |=
30158b8a9b1dSJustin T. Gibbs 			    start_ccb->ccb_h.target_lun << 5;
30168b8a9b1dSJustin T. Gibbs 		}
30178b8a9b1dSJustin T. Gibbs 		start_ccb->csio.scsi_status = SCSI_STATUS_OK;
3018d05caa00SKenneth D. Merry 		CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
3019d05caa00SKenneth D. Merry 			  scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
3020d05caa00SKenneth D. Merry 			  	       &path->device->inq_data),
3021d05caa00SKenneth D. Merry 			  scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
302250642f18SKenneth D. Merry 					  cdb_str, sizeof(cdb_str))));
3023d05caa00SKenneth D. Merry 	}
302407c6eac9SPoul-Henning Kamp 	/* FALLTHROUGH */
30258b8a9b1dSJustin T. Gibbs 	case XPT_TARGET_IO:
30268b8a9b1dSJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
30272cefde5fSJustin T. Gibbs 		start_ccb->csio.sense_resid = 0;
30282cefde5fSJustin T. Gibbs 		start_ccb->csio.resid = 0;
30292cefde5fSJustin T. Gibbs 		/* FALLTHROUGH */
303010e1cf63SKenneth D. Merry 	case XPT_RESET_DEV:
30318b8a9b1dSJustin T. Gibbs 	case XPT_ENG_EXEC:
30328b8a9b1dSJustin T. Gibbs 	{
30338b8a9b1dSJustin T. Gibbs 		struct cam_path *path;
3034d3ef3454SIan Dowse 		struct cam_sim *sim;
30358b8a9b1dSJustin T. Gibbs 		int runq;
30368b8a9b1dSJustin T. Gibbs 
30378b8a9b1dSJustin T. Gibbs 		path = start_ccb->ccb_h.path;
30388b8a9b1dSJustin T. Gibbs 
3039d3ef3454SIan Dowse 		sim = path->bus->sim;
3040d3ef3454SIan Dowse 		if (SIM_DEAD(sim)) {
3041d3ef3454SIan Dowse 			/* The SIM has gone; just execute the CCB directly. */
3042d3ef3454SIan Dowse 			cam_ccbq_send_ccb(&path->device->ccbq, start_ccb);
3043d3ef3454SIan Dowse 			(*(sim->sim_action))(sim, start_ccb);
3044d3ef3454SIan Dowse 			break;
3045d3ef3454SIan Dowse 		}
3046d3ef3454SIan Dowse 
30478b8a9b1dSJustin T. Gibbs 		cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
30488b8a9b1dSJustin T. Gibbs 		if (path->device->qfrozen_cnt == 0)
30498b8a9b1dSJustin T. Gibbs 			runq = xpt_schedule_dev_sendq(path->bus, path->device);
30508b8a9b1dSJustin T. Gibbs 		else
30518b8a9b1dSJustin T. Gibbs 			runq = 0;
30528b8a9b1dSJustin T. Gibbs 		if (runq != 0)
30538b8a9b1dSJustin T. Gibbs 			xpt_run_dev_sendq(path->bus);
30548b8a9b1dSJustin T. Gibbs 		break;
30558b8a9b1dSJustin T. Gibbs 	}
30568b8a9b1dSJustin T. Gibbs 	case XPT_SET_TRAN_SETTINGS:
30578b8a9b1dSJustin T. Gibbs 	{
30588b8a9b1dSJustin T. Gibbs 		xpt_set_transfer_settings(&start_ccb->cts,
305903e3511bSJustin T. Gibbs 					  start_ccb->ccb_h.path->device,
30608b8a9b1dSJustin T. Gibbs 					  /*async_update*/FALSE);
30618b8a9b1dSJustin T. Gibbs 		break;
30628b8a9b1dSJustin T. Gibbs 	}
30638b8a9b1dSJustin T. Gibbs 	case XPT_CALC_GEOMETRY:
30642cefde5fSJustin T. Gibbs 	{
30652cefde5fSJustin T. Gibbs 		struct cam_sim *sim;
30662cefde5fSJustin T. Gibbs 
30678b8a9b1dSJustin T. Gibbs 		/* Filter out garbage */
30688b8a9b1dSJustin T. Gibbs 		if (start_ccb->ccg.block_size == 0
30698b8a9b1dSJustin T. Gibbs 		 || start_ccb->ccg.volume_size == 0) {
30708b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.cylinders = 0;
30718b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.heads = 0;
30728b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.secs_per_track = 0;
30738b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
30748b8a9b1dSJustin T. Gibbs 			break;
30758b8a9b1dSJustin T. Gibbs 		}
30768b8a9b1dSJustin T. Gibbs #ifdef PC98
30778b8a9b1dSJustin T. Gibbs 		/*
30788b8a9b1dSJustin T. Gibbs 		 * In a PC-98 system, geometry translation depens on
30798b8a9b1dSJustin T. Gibbs 		 * the "real" device geometry obtained from mode page 4.
30808b8a9b1dSJustin T. Gibbs 		 * SCSI geometry translation is performed in the
30818b8a9b1dSJustin T. Gibbs 		 * initialization routine of the SCSI BIOS and the result
30828b8a9b1dSJustin T. Gibbs 		 * stored in host memory.  If the translation is available
30838b8a9b1dSJustin T. Gibbs 		 * in host memory, use it.  If not, rely on the default
30848b8a9b1dSJustin T. Gibbs 		 * translation the device driver performs.
30858b8a9b1dSJustin T. Gibbs 		 */
30868b8a9b1dSJustin T. Gibbs 		if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
30878b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
30888b8a9b1dSJustin T. Gibbs 			break;
30898b8a9b1dSJustin T. Gibbs 		}
30908b8a9b1dSJustin T. Gibbs #endif
30912cefde5fSJustin T. Gibbs 		sim = start_ccb->ccb_h.path->bus->sim;
30922cefde5fSJustin T. Gibbs 		(*(sim->sim_action))(sim, start_ccb);
30932cefde5fSJustin T. Gibbs 		break;
30942cefde5fSJustin T. Gibbs 	}
3095bb6087e5SJustin T. Gibbs 	case XPT_ABORT:
30962cefde5fSJustin T. Gibbs 	{
30972cefde5fSJustin T. Gibbs 		union ccb* abort_ccb;
30982cefde5fSJustin T. Gibbs 
30992cefde5fSJustin T. Gibbs 		abort_ccb = start_ccb->cab.abort_ccb;
31002cefde5fSJustin T. Gibbs 		if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
31012cefde5fSJustin T. Gibbs 
31022cefde5fSJustin T. Gibbs 			if (abort_ccb->ccb_h.pinfo.index >= 0) {
31032cefde5fSJustin T. Gibbs 				struct cam_ccbq *ccbq;
31042cefde5fSJustin T. Gibbs 
31052cefde5fSJustin T. Gibbs 				ccbq = &abort_ccb->ccb_h.path->device->ccbq;
31062cefde5fSJustin T. Gibbs 				cam_ccbq_remove_ccb(ccbq, abort_ccb);
31072cefde5fSJustin T. Gibbs 				abort_ccb->ccb_h.status =
31082cefde5fSJustin T. Gibbs 				    CAM_REQ_ABORTED|CAM_DEV_QFRZN;
31092cefde5fSJustin T. Gibbs 				xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
31102cefde5fSJustin T. Gibbs 				xpt_done(abort_ccb);
31112cefde5fSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
31122cefde5fSJustin T. Gibbs 				break;
31132cefde5fSJustin T. Gibbs 			}
31142cefde5fSJustin T. Gibbs 			if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
31152cefde5fSJustin T. Gibbs 			 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
31162cefde5fSJustin T. Gibbs 				/*
31172cefde5fSJustin T. Gibbs 				 * We've caught this ccb en route to
31182cefde5fSJustin T. Gibbs 				 * the SIM.  Flag it for abort and the
31192cefde5fSJustin T. Gibbs 				 * SIM will do so just before starting
31202cefde5fSJustin T. Gibbs 				 * real work on the CCB.
31212cefde5fSJustin T. Gibbs 				 */
31222cefde5fSJustin T. Gibbs 				abort_ccb->ccb_h.status =
31232cefde5fSJustin T. Gibbs 				    CAM_REQ_ABORTED|CAM_DEV_QFRZN;
31242cefde5fSJustin T. Gibbs 				xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
31252cefde5fSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
31262cefde5fSJustin T. Gibbs 				break;
31272cefde5fSJustin T. Gibbs 			}
31282cefde5fSJustin T. Gibbs 		}
31292cefde5fSJustin T. Gibbs 		if (XPT_FC_IS_QUEUED(abort_ccb)
31302cefde5fSJustin T. Gibbs 		 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
31312cefde5fSJustin T. Gibbs 			/*
31322cefde5fSJustin T. Gibbs 			 * It's already completed but waiting
31332cefde5fSJustin T. Gibbs 			 * for our SWI to get to it.
31342cefde5fSJustin T. Gibbs 			 */
31352cefde5fSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_UA_ABORT;
31362cefde5fSJustin T. Gibbs 			break;
31372cefde5fSJustin T. Gibbs 		}
31382cefde5fSJustin T. Gibbs 		/*
31392cefde5fSJustin T. Gibbs 		 * If we weren't able to take care of the abort request
31402cefde5fSJustin T. Gibbs 		 * in the XPT, pass the request down to the SIM for processing.
31412cefde5fSJustin T. Gibbs 		 */
31422cefde5fSJustin T. Gibbs 	}
314307c6eac9SPoul-Henning Kamp 	/* FALLTHROUGH */
31448b8a9b1dSJustin T. Gibbs 	case XPT_ACCEPT_TARGET_IO:
31458b8a9b1dSJustin T. Gibbs 	case XPT_EN_LUN:
31468b8a9b1dSJustin T. Gibbs 	case XPT_IMMED_NOTIFY:
31478b8a9b1dSJustin T. Gibbs 	case XPT_NOTIFY_ACK:
31488b8a9b1dSJustin T. Gibbs 	case XPT_GET_TRAN_SETTINGS:
31498b8a9b1dSJustin T. Gibbs 	case XPT_RESET_BUS:
31508b8a9b1dSJustin T. Gibbs 	{
31518b8a9b1dSJustin T. Gibbs 		struct cam_sim *sim;
31528b8a9b1dSJustin T. Gibbs 
31538b8a9b1dSJustin T. Gibbs 		sim = start_ccb->ccb_h.path->bus->sim;
31548b8a9b1dSJustin T. Gibbs 		(*(sim->sim_action))(sim, start_ccb);
31558b8a9b1dSJustin T. Gibbs 		break;
31568b8a9b1dSJustin T. Gibbs 	}
315787cfaf0eSJustin T. Gibbs 	case XPT_PATH_INQ:
315887cfaf0eSJustin T. Gibbs 	{
315987cfaf0eSJustin T. Gibbs 		struct cam_sim *sim;
316087cfaf0eSJustin T. Gibbs 
316187cfaf0eSJustin T. Gibbs 		sim = start_ccb->ccb_h.path->bus->sim;
316287cfaf0eSJustin T. Gibbs 		(*(sim->sim_action))(sim, start_ccb);
316387cfaf0eSJustin T. Gibbs 		break;
316487cfaf0eSJustin T. Gibbs 	}
316587cfaf0eSJustin T. Gibbs 	case XPT_PATH_STATS:
316687cfaf0eSJustin T. Gibbs 		start_ccb->cpis.last_reset =
316787cfaf0eSJustin T. Gibbs 			start_ccb->ccb_h.path->bus->last_reset;
316887cfaf0eSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
316987cfaf0eSJustin T. Gibbs 		break;
31708b8a9b1dSJustin T. Gibbs 	case XPT_GDEV_TYPE:
3171a5479bc5SJustin T. Gibbs 	{
317287cfaf0eSJustin T. Gibbs 		struct cam_ed *dev;
3173a5479bc5SJustin T. Gibbs 
317487cfaf0eSJustin T. Gibbs 		dev = start_ccb->ccb_h.path->device;
317587cfaf0eSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
31768b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
31778b8a9b1dSJustin T. Gibbs 		} else {
31788b8a9b1dSJustin T. Gibbs 			struct ccb_getdev *cgd;
317987cfaf0eSJustin T. Gibbs 			struct cam_eb *bus;
31808b8a9b1dSJustin T. Gibbs 			struct cam_et *tar;
31818b8a9b1dSJustin T. Gibbs 
31828b8a9b1dSJustin T. Gibbs 			cgd = &start_ccb->cgd;
318387cfaf0eSJustin T. Gibbs 			bus = cgd->ccb_h.path->bus;
31848b8a9b1dSJustin T. Gibbs 			tar = cgd->ccb_h.path->target;
31858b8a9b1dSJustin T. Gibbs 			cgd->inq_data = dev->inq_data;
31868b8a9b1dSJustin T. Gibbs 			cgd->ccb_h.status = CAM_REQ_CMP;
31878b8a9b1dSJustin T. Gibbs 			cgd->serial_num_len = dev->serial_num_len;
31888b8a9b1dSJustin T. Gibbs 			if ((dev->serial_num_len > 0)
31898b8a9b1dSJustin T. Gibbs 			 && (dev->serial_num != NULL))
31908b8a9b1dSJustin T. Gibbs 				bcopy(dev->serial_num, cgd->serial_num,
31918b8a9b1dSJustin T. Gibbs 				      dev->serial_num_len);
31928b8a9b1dSJustin T. Gibbs 		}
31938b8a9b1dSJustin T. Gibbs 		break;
3194a5479bc5SJustin T. Gibbs 	}
319587cfaf0eSJustin T. Gibbs 	case XPT_GDEV_STATS:
319687cfaf0eSJustin T. Gibbs 	{
319787cfaf0eSJustin T. Gibbs 		struct cam_ed *dev;
319887cfaf0eSJustin T. Gibbs 
319987cfaf0eSJustin T. Gibbs 		dev = start_ccb->ccb_h.path->device;
320087cfaf0eSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
320187cfaf0eSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
320287cfaf0eSJustin T. Gibbs 		} else {
320387cfaf0eSJustin T. Gibbs 			struct ccb_getdevstats *cgds;
320487cfaf0eSJustin T. Gibbs 			struct cam_eb *bus;
320587cfaf0eSJustin T. Gibbs 			struct cam_et *tar;
320687cfaf0eSJustin T. Gibbs 
320787cfaf0eSJustin T. Gibbs 			cgds = &start_ccb->cgds;
320887cfaf0eSJustin T. Gibbs 			bus = cgds->ccb_h.path->bus;
320987cfaf0eSJustin T. Gibbs 			tar = cgds->ccb_h.path->target;
321087cfaf0eSJustin T. Gibbs 			cgds->dev_openings = dev->ccbq.dev_openings;
321187cfaf0eSJustin T. Gibbs 			cgds->dev_active = dev->ccbq.dev_active;
321287cfaf0eSJustin T. Gibbs 			cgds->devq_openings = dev->ccbq.devq_openings;
321387cfaf0eSJustin T. Gibbs 			cgds->devq_queued = dev->ccbq.queue.entries;
321487cfaf0eSJustin T. Gibbs 			cgds->held = dev->ccbq.held;
321587cfaf0eSJustin T. Gibbs 			cgds->last_reset = tar->last_reset;
321682815562SJustin T. Gibbs 			cgds->maxtags = dev->quirk->maxtags;
321782815562SJustin T. Gibbs 			cgds->mintags = dev->quirk->mintags;
321887cfaf0eSJustin T. Gibbs 			if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
321987cfaf0eSJustin T. Gibbs 				cgds->last_reset = bus->last_reset;
322087cfaf0eSJustin T. Gibbs 			cgds->ccb_h.status = CAM_REQ_CMP;
322187cfaf0eSJustin T. Gibbs 		}
322287cfaf0eSJustin T. Gibbs 		break;
322387cfaf0eSJustin T. Gibbs 	}
32248b8a9b1dSJustin T. Gibbs 	case XPT_GDEVLIST:
32258b8a9b1dSJustin T. Gibbs 	{
32268b8a9b1dSJustin T. Gibbs 		struct cam_periph	*nperiph;
32278b8a9b1dSJustin T. Gibbs 		struct periph_list	*periph_head;
32288b8a9b1dSJustin T. Gibbs 		struct ccb_getdevlist	*cgdl;
32293393f8daSKenneth D. Merry 		u_int			i;
32308b8a9b1dSJustin T. Gibbs 		struct cam_ed		*device;
32318b8a9b1dSJustin T. Gibbs 		int			found;
32328b8a9b1dSJustin T. Gibbs 
32338b8a9b1dSJustin T. Gibbs 
32348b8a9b1dSJustin T. Gibbs 		found = 0;
32358b8a9b1dSJustin T. Gibbs 
32368b8a9b1dSJustin T. Gibbs 		/*
32378b8a9b1dSJustin T. Gibbs 		 * Don't want anyone mucking with our data.
32388b8a9b1dSJustin T. Gibbs 		 */
32398b8a9b1dSJustin T. Gibbs 		device = start_ccb->ccb_h.path->device;
32408b8a9b1dSJustin T. Gibbs 		periph_head = &device->periphs;
32418b8a9b1dSJustin T. Gibbs 		cgdl = &start_ccb->cgdl;
32428b8a9b1dSJustin T. Gibbs 
32438b8a9b1dSJustin T. Gibbs 		/*
32448b8a9b1dSJustin T. Gibbs 		 * Check and see if the list has changed since the user
32458b8a9b1dSJustin T. Gibbs 		 * last requested a list member.  If so, tell them that the
32468b8a9b1dSJustin T. Gibbs 		 * list has changed, and therefore they need to start over
32478b8a9b1dSJustin T. Gibbs 		 * from the beginning.
32488b8a9b1dSJustin T. Gibbs 		 */
32498b8a9b1dSJustin T. Gibbs 		if ((cgdl->index != 0) &&
32508b8a9b1dSJustin T. Gibbs 		    (cgdl->generation != device->generation)) {
32518b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
32528b8a9b1dSJustin T. Gibbs 			break;
32538b8a9b1dSJustin T. Gibbs 		}
32548b8a9b1dSJustin T. Gibbs 
32558b8a9b1dSJustin T. Gibbs 		/*
32568b8a9b1dSJustin T. Gibbs 		 * Traverse the list of peripherals and attempt to find
32578b8a9b1dSJustin T. Gibbs 		 * the requested peripheral.
32588b8a9b1dSJustin T. Gibbs 		 */
3259fc2ffbe6SPoul-Henning Kamp 		for (nperiph = SLIST_FIRST(periph_head), i = 0;
32608b8a9b1dSJustin T. Gibbs 		     (nperiph != NULL) && (i <= cgdl->index);
3261fc2ffbe6SPoul-Henning Kamp 		     nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
32628b8a9b1dSJustin T. Gibbs 			if (i == cgdl->index) {
32638b8a9b1dSJustin T. Gibbs 				strncpy(cgdl->periph_name,
32648b8a9b1dSJustin T. Gibbs 					nperiph->periph_name,
32658b8a9b1dSJustin T. Gibbs 					DEV_IDLEN);
32668b8a9b1dSJustin T. Gibbs 				cgdl->unit_number = nperiph->unit_number;
32678b8a9b1dSJustin T. Gibbs 				found = 1;
32688b8a9b1dSJustin T. Gibbs 			}
32698b8a9b1dSJustin T. Gibbs 		}
32708b8a9b1dSJustin T. Gibbs 		if (found == 0) {
32718b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_ERROR;
32728b8a9b1dSJustin T. Gibbs 			break;
32738b8a9b1dSJustin T. Gibbs 		}
32748b8a9b1dSJustin T. Gibbs 
32758b8a9b1dSJustin T. Gibbs 		if (nperiph == NULL)
32768b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
32778b8a9b1dSJustin T. Gibbs 		else
32788b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_MORE_DEVS;
32798b8a9b1dSJustin T. Gibbs 
32808b8a9b1dSJustin T. Gibbs 		cgdl->index++;
32818b8a9b1dSJustin T. Gibbs 		cgdl->generation = device->generation;
32828b8a9b1dSJustin T. Gibbs 
32838b8a9b1dSJustin T. Gibbs 		cgdl->ccb_h.status = CAM_REQ_CMP;
32848b8a9b1dSJustin T. Gibbs 		break;
32858b8a9b1dSJustin T. Gibbs 	}
32868b8a9b1dSJustin T. Gibbs 	case XPT_DEV_MATCH:
32878b8a9b1dSJustin T. Gibbs 	{
32888b8a9b1dSJustin T. Gibbs 		dev_pos_type position_type;
32898b8a9b1dSJustin T. Gibbs 		struct ccb_dev_match *cdm;
32908b8a9b1dSJustin T. Gibbs 
32918b8a9b1dSJustin T. Gibbs 		cdm = &start_ccb->cdm;
32928b8a9b1dSJustin T. Gibbs 
32938b8a9b1dSJustin T. Gibbs 		/*
32948b8a9b1dSJustin T. Gibbs 		 * There are two ways of getting at information in the EDT.
32958b8a9b1dSJustin T. Gibbs 		 * The first way is via the primary EDT tree.  It starts
32968b8a9b1dSJustin T. Gibbs 		 * with a list of busses, then a list of targets on a bus,
32978b8a9b1dSJustin T. Gibbs 		 * then devices/luns on a target, and then peripherals on a
32988b8a9b1dSJustin T. Gibbs 		 * device/lun.  The "other" way is by the peripheral driver
32998b8a9b1dSJustin T. Gibbs 		 * lists.  The peripheral driver lists are organized by
33008b8a9b1dSJustin T. Gibbs 		 * peripheral driver.  (obviously)  So it makes sense to
33018b8a9b1dSJustin T. Gibbs 		 * use the peripheral driver list if the user is looking
33028b8a9b1dSJustin T. Gibbs 		 * for something like "da1", or all "da" devices.  If the
33038b8a9b1dSJustin T. Gibbs 		 * user is looking for something on a particular bus/target
33048b8a9b1dSJustin T. Gibbs 		 * or lun, it's generally better to go through the EDT tree.
33058b8a9b1dSJustin T. Gibbs 		 */
33068b8a9b1dSJustin T. Gibbs 
33078b8a9b1dSJustin T. Gibbs 		if (cdm->pos.position_type != CAM_DEV_POS_NONE)
33088b8a9b1dSJustin T. Gibbs 			position_type = cdm->pos.position_type;
33098b8a9b1dSJustin T. Gibbs 		else {
33103393f8daSKenneth D. Merry 			u_int i;
33118b8a9b1dSJustin T. Gibbs 
33128b8a9b1dSJustin T. Gibbs 			position_type = CAM_DEV_POS_NONE;
33138b8a9b1dSJustin T. Gibbs 
33148b8a9b1dSJustin T. Gibbs 			for (i = 0; i < cdm->num_patterns; i++) {
33158b8a9b1dSJustin T. Gibbs 				if ((cdm->patterns[i].type == DEV_MATCH_BUS)
33168b8a9b1dSJustin T. Gibbs 				 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
33178b8a9b1dSJustin T. Gibbs 					position_type = CAM_DEV_POS_EDT;
33188b8a9b1dSJustin T. Gibbs 					break;
33198b8a9b1dSJustin T. Gibbs 				}
33208b8a9b1dSJustin T. Gibbs 			}
33218b8a9b1dSJustin T. Gibbs 
33228b8a9b1dSJustin T. Gibbs 			if (cdm->num_patterns == 0)
33238b8a9b1dSJustin T. Gibbs 				position_type = CAM_DEV_POS_EDT;
33248b8a9b1dSJustin T. Gibbs 			else if (position_type == CAM_DEV_POS_NONE)
33258b8a9b1dSJustin T. Gibbs 				position_type = CAM_DEV_POS_PDRV;
33268b8a9b1dSJustin T. Gibbs 		}
33278b8a9b1dSJustin T. Gibbs 
33288b8a9b1dSJustin T. Gibbs 		switch(position_type & CAM_DEV_POS_TYPEMASK) {
33298b8a9b1dSJustin T. Gibbs 		case CAM_DEV_POS_EDT:
333007c6eac9SPoul-Henning Kamp 			xptedtmatch(cdm);
33318b8a9b1dSJustin T. Gibbs 			break;
33328b8a9b1dSJustin T. Gibbs 		case CAM_DEV_POS_PDRV:
333307c6eac9SPoul-Henning Kamp 			xptperiphlistmatch(cdm);
33348b8a9b1dSJustin T. Gibbs 			break;
33358b8a9b1dSJustin T. Gibbs 		default:
33368b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_ERROR;
33378b8a9b1dSJustin T. Gibbs 			break;
33388b8a9b1dSJustin T. Gibbs 		}
33398b8a9b1dSJustin T. Gibbs 
33408b8a9b1dSJustin T. Gibbs 		if (cdm->status == CAM_DEV_MATCH_ERROR)
33418b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
33428b8a9b1dSJustin T. Gibbs 		else
33438b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
33448b8a9b1dSJustin T. Gibbs 
33458b8a9b1dSJustin T. Gibbs 		break;
33468b8a9b1dSJustin T. Gibbs 	}
33478b8a9b1dSJustin T. Gibbs 	case XPT_SASYNC_CB:
33488b8a9b1dSJustin T. Gibbs 	{
334984f82481SScott Long 		struct ccb_setasync *csa;
335084f82481SScott Long 		struct async_node *cur_entry;
335184f82481SScott Long 		struct async_list *async_head;
335284f82481SScott Long 		u_int32_t added;
335384f82481SScott Long 
335484f82481SScott Long 		csa = &start_ccb->csa;
335584f82481SScott Long 		added = csa->event_enable;
335684f82481SScott Long 		async_head = &csa->ccb_h.path->device->asyncs;
335784f82481SScott Long 
335884f82481SScott Long 		/*
335984f82481SScott Long 		 * If there is already an entry for us, simply
336084f82481SScott Long 		 * update it.
336184f82481SScott Long 		 */
336284f82481SScott Long 		cur_entry = SLIST_FIRST(async_head);
336384f82481SScott Long 		while (cur_entry != NULL) {
336484f82481SScott Long 			if ((cur_entry->callback_arg == csa->callback_arg)
336584f82481SScott Long 			 && (cur_entry->callback == csa->callback))
336684f82481SScott Long 				break;
336784f82481SScott Long 			cur_entry = SLIST_NEXT(cur_entry, links);
336884f82481SScott Long 		}
336984f82481SScott Long 
337084f82481SScott Long 		if (cur_entry != NULL) {
337184f82481SScott Long 		 	/*
337284f82481SScott Long 			 * If the request has no flags set,
337384f82481SScott Long 			 * remove the entry.
337484f82481SScott Long 			 */
337584f82481SScott Long 			added &= ~cur_entry->event_enable;
337684f82481SScott Long 			if (csa->event_enable == 0) {
337784f82481SScott Long 				SLIST_REMOVE(async_head, cur_entry,
337884f82481SScott Long 					     async_node, links);
337984f82481SScott Long 				csa->ccb_h.path->device->refcount--;
338084f82481SScott Long 				free(cur_entry, M_CAMXPT);
338184f82481SScott Long 			} else {
338284f82481SScott Long 				cur_entry->event_enable = csa->event_enable;
338384f82481SScott Long 			}
338484f82481SScott Long 		} else {
338584f82481SScott Long 			cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
338684f82481SScott Long 					   M_NOWAIT);
338784f82481SScott Long 			if (cur_entry == NULL) {
338884f82481SScott Long 				csa->ccb_h.status = CAM_RESRC_UNAVAIL;
338984f82481SScott Long 				break;
339084f82481SScott Long 			}
339184f82481SScott Long 			cur_entry->event_enable = csa->event_enable;
339284f82481SScott Long 			cur_entry->callback_arg = csa->callback_arg;
339384f82481SScott Long 			cur_entry->callback = csa->callback;
339484f82481SScott Long 			SLIST_INSERT_HEAD(async_head, cur_entry, links);
339584f82481SScott Long 			csa->ccb_h.path->device->refcount++;
339684f82481SScott Long 		}
33978b8a9b1dSJustin T. Gibbs 
33988b8a9b1dSJustin T. Gibbs 		/*
33992b83592fSScott Long 		 * Need to decouple this operation via a taqskqueue so that
340084f82481SScott Long 		 * the locking doesn't become a mess.
34018b8a9b1dSJustin T. Gibbs 		 */
340284f82481SScott Long 		if ((added & (AC_FOUND_DEVICE | AC_PATH_REGISTERED)) != 0) {
340384f82481SScott Long 			struct xpt_task *task;
34048b8a9b1dSJustin T. Gibbs 
340584f82481SScott Long 			task = malloc(sizeof(struct xpt_task), M_CAMXPT,
340684f82481SScott Long 				      M_NOWAIT);
34072b83592fSScott Long 			if (task == NULL) {
340884f82481SScott Long 				csa->ccb_h.status = CAM_RESRC_UNAVAIL;
34092b83592fSScott Long 				break;
34108b8a9b1dSJustin T. Gibbs 			}
34112b83592fSScott Long 
34122b83592fSScott Long 			TASK_INIT(&task->task, 0, xpt_action_sasync_cb, task);
341384f82481SScott Long 			task->data1 = cur_entry;
341484f82481SScott Long 			task->data2 = added;
34152b83592fSScott Long 			taskqueue_enqueue(taskqueue_thread, &task->task);
341684f82481SScott Long 		}
34172b83592fSScott Long 
34188b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
34198b8a9b1dSJustin T. Gibbs 		break;
34208b8a9b1dSJustin T. Gibbs 	}
34218b8a9b1dSJustin T. Gibbs 	case XPT_REL_SIMQ:
34228b8a9b1dSJustin T. Gibbs 	{
34238b8a9b1dSJustin T. Gibbs 		struct ccb_relsim *crs;
34248b8a9b1dSJustin T. Gibbs 		struct cam_ed *dev;
34258b8a9b1dSJustin T. Gibbs 
34268b8a9b1dSJustin T. Gibbs 		crs = &start_ccb->crs;
34278b8a9b1dSJustin T. Gibbs 		dev = crs->ccb_h.path->device;
34288b8a9b1dSJustin T. Gibbs 		if (dev == NULL) {
34298b8a9b1dSJustin T. Gibbs 
34308b8a9b1dSJustin T. Gibbs 			crs->ccb_h.status = CAM_DEV_NOT_THERE;
34318b8a9b1dSJustin T. Gibbs 			break;
34328b8a9b1dSJustin T. Gibbs 		}
34338b8a9b1dSJustin T. Gibbs 
34348b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
34358b8a9b1dSJustin T. Gibbs 
34364e359f5aSMatt Jacob  			if (INQ_DATA_TQ_ENABLED(&dev->inq_data)) {
34378b8a9b1dSJustin T. Gibbs 				/* Don't ever go below one opening */
34388b8a9b1dSJustin T. Gibbs 				if (crs->openings > 0) {
34398b8a9b1dSJustin T. Gibbs 					xpt_dev_ccbq_resize(crs->ccb_h.path,
34408b8a9b1dSJustin T. Gibbs 							    crs->openings);
34418b8a9b1dSJustin T. Gibbs 
344279ccc199SJordan K. Hubbard 					if (bootverbose) {
3443f0d9af51SMatt Jacob 						xpt_print(crs->ccb_h.path,
3444f0d9af51SMatt Jacob 						    "tagged openings now %d\n",
34458b8a9b1dSJustin T. Gibbs 						    crs->openings);
34468b8a9b1dSJustin T. Gibbs 					}
34478b8a9b1dSJustin T. Gibbs 				}
34488b8a9b1dSJustin T. Gibbs 			}
34498b8a9b1dSJustin T. Gibbs 		}
34508b8a9b1dSJustin T. Gibbs 
34518b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
34528b8a9b1dSJustin T. Gibbs 
34538b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
34548b8a9b1dSJustin T. Gibbs 
34558b8a9b1dSJustin T. Gibbs 				/*
34568b8a9b1dSJustin T. Gibbs 				 * Just extend the old timeout and decrement
34578b8a9b1dSJustin T. Gibbs 				 * the freeze count so that a single timeout
34588b8a9b1dSJustin T. Gibbs 				 * is sufficient for releasing the queue.
34598b8a9b1dSJustin T. Gibbs 				 */
34608b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
34612b83592fSScott Long 				callout_stop(&dev->callout);
34628b8a9b1dSJustin T. Gibbs 			} else {
34638b8a9b1dSJustin T. Gibbs 
34648b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
34658b8a9b1dSJustin T. Gibbs 			}
34668b8a9b1dSJustin T. Gibbs 
34672b83592fSScott Long 			callout_reset(&dev->callout,
34682b83592fSScott Long 			    (crs->release_timeout * hz) / 1000,
34692b83592fSScott Long 			    xpt_release_devq_timeout, dev);
34708b8a9b1dSJustin T. Gibbs 
34718b8a9b1dSJustin T. Gibbs 			dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
34728b8a9b1dSJustin T. Gibbs 
34738b8a9b1dSJustin T. Gibbs 		}
34748b8a9b1dSJustin T. Gibbs 
34758b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
34768b8a9b1dSJustin T. Gibbs 
34778b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
34788b8a9b1dSJustin T. Gibbs 				/*
34798b8a9b1dSJustin T. Gibbs 				 * Decrement the freeze count so that a single
34808b8a9b1dSJustin T. Gibbs 				 * completion is still sufficient to unfreeze
34818b8a9b1dSJustin T. Gibbs 				 * the queue.
34828b8a9b1dSJustin T. Gibbs 				 */
34838b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
34848b8a9b1dSJustin T. Gibbs 			} else {
34858b8a9b1dSJustin T. Gibbs 
34868b8a9b1dSJustin T. Gibbs 				dev->flags |= CAM_DEV_REL_ON_COMPLETE;
34878b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
34888b8a9b1dSJustin T. Gibbs 			}
34898b8a9b1dSJustin T. Gibbs 		}
34908b8a9b1dSJustin T. Gibbs 
34918b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
34928b8a9b1dSJustin T. Gibbs 
34938b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
34948b8a9b1dSJustin T. Gibbs 			 || (dev->ccbq.dev_active == 0)) {
34958b8a9b1dSJustin T. Gibbs 
34968b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
34978b8a9b1dSJustin T. Gibbs 			} else {
34988b8a9b1dSJustin T. Gibbs 
34998b8a9b1dSJustin T. Gibbs 				dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
35008b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
35018b8a9b1dSJustin T. Gibbs 			}
35028b8a9b1dSJustin T. Gibbs 		}
35038b8a9b1dSJustin T. Gibbs 
35048b8a9b1dSJustin T. Gibbs 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
35058b8a9b1dSJustin T. Gibbs 
35062cefde5fSJustin T. Gibbs 			xpt_release_devq(crs->ccb_h.path, /*count*/1,
35078b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
35088b8a9b1dSJustin T. Gibbs 		}
35098b8a9b1dSJustin T. Gibbs 		start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
35108b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
35118b8a9b1dSJustin T. Gibbs 		break;
35128b8a9b1dSJustin T. Gibbs 	}
35138b8a9b1dSJustin T. Gibbs 	case XPT_SCAN_BUS:
35148b8a9b1dSJustin T. Gibbs 		xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
35158b8a9b1dSJustin T. Gibbs 		break;
35168b8a9b1dSJustin T. Gibbs 	case XPT_SCAN_LUN:
35178b8a9b1dSJustin T. Gibbs 		xpt_scan_lun(start_ccb->ccb_h.path->periph,
35188b8a9b1dSJustin T. Gibbs 			     start_ccb->ccb_h.path, start_ccb->crcn.flags,
35198b8a9b1dSJustin T. Gibbs 			     start_ccb);
35208b8a9b1dSJustin T. Gibbs 		break;
35218b8a9b1dSJustin T. Gibbs 	case XPT_DEBUG: {
35228b8a9b1dSJustin T. Gibbs #ifdef CAMDEBUG
35232cefde5fSJustin T. Gibbs #ifdef CAM_DEBUG_DELAY
35242cefde5fSJustin T. Gibbs 		cam_debug_delay = CAM_DEBUG_DELAY;
35252cefde5fSJustin T. Gibbs #endif
35268b8a9b1dSJustin T. Gibbs 		cam_dflags = start_ccb->cdbg.flags;
35278b8a9b1dSJustin T. Gibbs 		if (cam_dpath != NULL) {
35288b8a9b1dSJustin T. Gibbs 			xpt_free_path(cam_dpath);
35298b8a9b1dSJustin T. Gibbs 			cam_dpath = NULL;
35308b8a9b1dSJustin T. Gibbs 		}
35318b8a9b1dSJustin T. Gibbs 
35328b8a9b1dSJustin T. Gibbs 		if (cam_dflags != CAM_DEBUG_NONE) {
35338b8a9b1dSJustin T. Gibbs 			if (xpt_create_path(&cam_dpath, xpt_periph,
35348b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.path_id,
35358b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.target_id,
35368b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.target_lun) !=
35378b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP) {
35388b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
35398b8a9b1dSJustin T. Gibbs 				cam_dflags = CAM_DEBUG_NONE;
3540aa872be6SMatt Jacob 			} else {
35418b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
3542f0d9af51SMatt Jacob 				xpt_print(cam_dpath, "debugging flags now %x\n",
3543f0d9af51SMatt Jacob 				    cam_dflags);
3544aa872be6SMatt Jacob 			}
35458b8a9b1dSJustin T. Gibbs 		} else {
35468b8a9b1dSJustin T. Gibbs 			cam_dpath = NULL;
35478b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
35488b8a9b1dSJustin T. Gibbs 		}
35498b8a9b1dSJustin T. Gibbs #else /* !CAMDEBUG */
35508b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
35518b8a9b1dSJustin T. Gibbs #endif /* CAMDEBUG */
35528b8a9b1dSJustin T. Gibbs 		break;
35538b8a9b1dSJustin T. Gibbs 	}
35548b8a9b1dSJustin T. Gibbs 	case XPT_NOOP:
355587cfaf0eSJustin T. Gibbs 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
355687cfaf0eSJustin T. Gibbs 			xpt_freeze_devq(start_ccb->ccb_h.path, 1);
35578b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
35588b8a9b1dSJustin T. Gibbs 		break;
35598b8a9b1dSJustin T. Gibbs 	default:
35608b8a9b1dSJustin T. Gibbs 	case XPT_SDEV_TYPE:
35618b8a9b1dSJustin T. Gibbs 	case XPT_TERM_IO:
35628b8a9b1dSJustin T. Gibbs 	case XPT_ENG_INQ:
35638b8a9b1dSJustin T. Gibbs 		/* XXX Implement */
35648b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
35658b8a9b1dSJustin T. Gibbs 		break;
35668b8a9b1dSJustin T. Gibbs 	}
35678b8a9b1dSJustin T. Gibbs }
35688b8a9b1dSJustin T. Gibbs 
35698b8a9b1dSJustin T. Gibbs void
35708b8a9b1dSJustin T. Gibbs xpt_polled_action(union ccb *start_ccb)
35718b8a9b1dSJustin T. Gibbs {
35728b8a9b1dSJustin T. Gibbs 	u_int32_t timeout;
35738b8a9b1dSJustin T. Gibbs 	struct	  cam_sim *sim;
35748b8a9b1dSJustin T. Gibbs 	struct	  cam_devq *devq;
35758b8a9b1dSJustin T. Gibbs 	struct	  cam_ed *dev;
35768b8a9b1dSJustin T. Gibbs 
357768153f43SScott Long 
35788b8a9b1dSJustin T. Gibbs 	timeout = start_ccb->ccb_h.timeout;
35798b8a9b1dSJustin T. Gibbs 	sim = start_ccb->ccb_h.path->bus->sim;
35808b8a9b1dSJustin T. Gibbs 	devq = sim->devq;
35818b8a9b1dSJustin T. Gibbs 	dev = start_ccb->ccb_h.path->device;
35828b8a9b1dSJustin T. Gibbs 
35832b83592fSScott Long 	mtx_assert(sim->mtx, MA_OWNED);
35848b8a9b1dSJustin T. Gibbs 
35858b8a9b1dSJustin T. Gibbs 	/*
35868b8a9b1dSJustin T. Gibbs 	 * Steal an opening so that no other queued requests
35878b8a9b1dSJustin T. Gibbs 	 * can get it before us while we simulate interrupts.
35888b8a9b1dSJustin T. Gibbs 	 */
35898b8a9b1dSJustin T. Gibbs 	dev->ccbq.devq_openings--;
35908b8a9b1dSJustin T. Gibbs 	dev->ccbq.dev_openings--;
35918b8a9b1dSJustin T. Gibbs 
3592d3ef3454SIan Dowse 	while(((devq != NULL && devq->send_openings <= 0) ||
3593d3ef3454SIan Dowse 	   dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
35948b8a9b1dSJustin T. Gibbs 		DELAY(1000);
35958b8a9b1dSJustin T. Gibbs 		(*(sim->sim_poll))(sim);
35969758cc83SScott Long 		camisr_runqueue(&sim->sim_doneq);
35978b8a9b1dSJustin T. Gibbs 	}
35988b8a9b1dSJustin T. Gibbs 
35998b8a9b1dSJustin T. Gibbs 	dev->ccbq.devq_openings++;
36008b8a9b1dSJustin T. Gibbs 	dev->ccbq.dev_openings++;
36018b8a9b1dSJustin T. Gibbs 
36028b8a9b1dSJustin T. Gibbs 	if (timeout != 0) {
36038b8a9b1dSJustin T. Gibbs 		xpt_action(start_ccb);
36048b8a9b1dSJustin T. Gibbs 		while(--timeout > 0) {
36058b8a9b1dSJustin T. Gibbs 			(*(sim->sim_poll))(sim);
36069758cc83SScott Long 			camisr_runqueue(&sim->sim_doneq);
36078b8a9b1dSJustin T. Gibbs 			if ((start_ccb->ccb_h.status  & CAM_STATUS_MASK)
36088b8a9b1dSJustin T. Gibbs 			    != CAM_REQ_INPROG)
36098b8a9b1dSJustin T. Gibbs 				break;
36108b8a9b1dSJustin T. Gibbs 			DELAY(1000);
36118b8a9b1dSJustin T. Gibbs 		}
36128b8a9b1dSJustin T. Gibbs 		if (timeout == 0) {
36138b8a9b1dSJustin T. Gibbs 			/*
36148b8a9b1dSJustin T. Gibbs 			 * XXX Is it worth adding a sim_timeout entry
36158b8a9b1dSJustin T. Gibbs 			 * point so we can attempt recovery?  If
36168b8a9b1dSJustin T. Gibbs 			 * this is only used for dumps, I don't think
36178b8a9b1dSJustin T. Gibbs 			 * it is.
36188b8a9b1dSJustin T. Gibbs 			 */
36198b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
36208b8a9b1dSJustin T. Gibbs 		}
36218b8a9b1dSJustin T. Gibbs 	} else {
36228b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
36238b8a9b1dSJustin T. Gibbs 	}
36248b8a9b1dSJustin T. Gibbs }
36258b8a9b1dSJustin T. Gibbs 
36268b8a9b1dSJustin T. Gibbs /*
36278b8a9b1dSJustin T. Gibbs  * Schedule a peripheral driver to receive a ccb when it's
36288b8a9b1dSJustin T. Gibbs  * target device has space for more transactions.
36298b8a9b1dSJustin T. Gibbs  */
36308b8a9b1dSJustin T. Gibbs void
36318b8a9b1dSJustin T. Gibbs xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
36328b8a9b1dSJustin T. Gibbs {
36338b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
3634d3ef3454SIan Dowse 	union ccb *work_ccb;
36358b8a9b1dSJustin T. Gibbs 	int runq;
36368b8a9b1dSJustin T. Gibbs 
36372b83592fSScott Long 	mtx_assert(perph->sim->mtx, MA_OWNED);
363868153f43SScott Long 
36398b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
36408b8a9b1dSJustin T. Gibbs 	device = perph->path->device;
36418b8a9b1dSJustin T. Gibbs 	if (periph_is_queued(perph)) {
36428b8a9b1dSJustin T. Gibbs 		/* Simply reorder based on new priority */
36438b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
36448b8a9b1dSJustin T. Gibbs 			  ("   change priority to %d\n", new_priority));
36458b8a9b1dSJustin T. Gibbs 		if (new_priority < perph->pinfo.priority) {
36468b8a9b1dSJustin T. Gibbs 			camq_change_priority(&device->drvq,
36478b8a9b1dSJustin T. Gibbs 					     perph->pinfo.index,
36488b8a9b1dSJustin T. Gibbs 					     new_priority);
36498b8a9b1dSJustin T. Gibbs 		}
36508b8a9b1dSJustin T. Gibbs 		runq = 0;
3651d3ef3454SIan Dowse 	} else if (SIM_DEAD(perph->path->bus->sim)) {
3652d3ef3454SIan Dowse 		/* The SIM is gone so just call periph_start directly. */
3653d3ef3454SIan Dowse 		work_ccb = xpt_get_ccb(perph->path->device);
3654d3ef3454SIan Dowse 		if (work_ccb == NULL)
3655d3ef3454SIan Dowse 			return; /* XXX */
3656d3ef3454SIan Dowse 		xpt_setup_ccb(&work_ccb->ccb_h, perph->path, new_priority);
3657d3ef3454SIan Dowse 		perph->pinfo.priority = new_priority;
3658d3ef3454SIan Dowse 		perph->periph_start(perph, work_ccb);
3659d3ef3454SIan Dowse 		return;
36608b8a9b1dSJustin T. Gibbs 	} else {
36618b8a9b1dSJustin T. Gibbs 		/* New entry on the queue */
36628b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
36638b8a9b1dSJustin T. Gibbs 			  ("   added periph to queue\n"));
36648b8a9b1dSJustin T. Gibbs 		perph->pinfo.priority = new_priority;
36658bad620dSJustin T. Gibbs 		perph->pinfo.generation = ++device->drvq.generation;
36668b8a9b1dSJustin T. Gibbs 		camq_insert(&device->drvq, &perph->pinfo);
36678b8a9b1dSJustin T. Gibbs 		runq = xpt_schedule_dev_allocq(perph->path->bus, device);
36688b8a9b1dSJustin T. Gibbs 	}
36698b8a9b1dSJustin T. Gibbs 	if (runq != 0) {
36708b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
36718b8a9b1dSJustin T. Gibbs 			  ("   calling xpt_run_devq\n"));
36728b8a9b1dSJustin T. Gibbs 		xpt_run_dev_allocq(perph->path->bus);
36738b8a9b1dSJustin T. Gibbs 	}
36748b8a9b1dSJustin T. Gibbs }
36758b8a9b1dSJustin T. Gibbs 
36768b8a9b1dSJustin T. Gibbs 
36778b8a9b1dSJustin T. Gibbs /*
36788b8a9b1dSJustin T. Gibbs  * Schedule a device to run on a given queue.
36798b8a9b1dSJustin T. Gibbs  * If the device was inserted as a new entry on the queue,
36808b8a9b1dSJustin T. Gibbs  * return 1 meaning the device queue should be run. If we
36818b8a9b1dSJustin T. Gibbs  * were already queued, implying someone else has already
36828b8a9b1dSJustin T. Gibbs  * started the queue, return 0 so the caller doesn't attempt
368377dc25ccSScott Long  * to run the queue.
36848b8a9b1dSJustin T. Gibbs  */
36858b8a9b1dSJustin T. Gibbs static int
36868b8a9b1dSJustin T. Gibbs xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
36878b8a9b1dSJustin T. Gibbs 		 u_int32_t new_priority)
36888b8a9b1dSJustin T. Gibbs {
36898b8a9b1dSJustin T. Gibbs 	int retval;
36908b8a9b1dSJustin T. Gibbs 	u_int32_t old_priority;
36918b8a9b1dSJustin T. Gibbs 
3692aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
36938b8a9b1dSJustin T. Gibbs 
36948b8a9b1dSJustin T. Gibbs 	old_priority = pinfo->priority;
36958b8a9b1dSJustin T. Gibbs 
36968b8a9b1dSJustin T. Gibbs 	/*
36978b8a9b1dSJustin T. Gibbs 	 * Are we already queued?
36988b8a9b1dSJustin T. Gibbs 	 */
36998b8a9b1dSJustin T. Gibbs 	if (pinfo->index != CAM_UNQUEUED_INDEX) {
37008b8a9b1dSJustin T. Gibbs 		/* Simply reorder based on new priority */
37018b8a9b1dSJustin T. Gibbs 		if (new_priority < old_priority) {
37028b8a9b1dSJustin T. Gibbs 			camq_change_priority(queue, pinfo->index,
37038b8a9b1dSJustin T. Gibbs 					     new_priority);
3704aa872be6SMatt Jacob 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
37058b8a9b1dSJustin T. Gibbs 					("changed priority to %d\n",
37068b8a9b1dSJustin T. Gibbs 					 new_priority));
37078b8a9b1dSJustin T. Gibbs 		}
37088b8a9b1dSJustin T. Gibbs 		retval = 0;
37098b8a9b1dSJustin T. Gibbs 	} else {
37108b8a9b1dSJustin T. Gibbs 		/* New entry on the queue */
37118b8a9b1dSJustin T. Gibbs 		if (new_priority < old_priority)
37128b8a9b1dSJustin T. Gibbs 			pinfo->priority = new_priority;
37138b8a9b1dSJustin T. Gibbs 
3714aa872be6SMatt Jacob 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
37158b8a9b1dSJustin T. Gibbs 				("Inserting onto queue\n"));
37168bad620dSJustin T. Gibbs 		pinfo->generation = ++queue->generation;
37178b8a9b1dSJustin T. Gibbs 		camq_insert(queue, pinfo);
37188b8a9b1dSJustin T. Gibbs 		retval = 1;
37198b8a9b1dSJustin T. Gibbs 	}
37208b8a9b1dSJustin T. Gibbs 	return (retval);
37218b8a9b1dSJustin T. Gibbs }
37228b8a9b1dSJustin T. Gibbs 
37238b8a9b1dSJustin T. Gibbs static void
37248b8a9b1dSJustin T. Gibbs xpt_run_dev_allocq(struct cam_eb *bus)
37258b8a9b1dSJustin T. Gibbs {
37268b8a9b1dSJustin T. Gibbs 	struct	cam_devq *devq;
37278b8a9b1dSJustin T. Gibbs 
3728aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
37298b8a9b1dSJustin T. Gibbs 	devq = bus->sim->devq;
37308b8a9b1dSJustin T. Gibbs 
3731aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
37328b8a9b1dSJustin T. Gibbs 			("   qfrozen_cnt == 0x%x, entries == %d, "
37338b8a9b1dSJustin T. Gibbs 			 "openings == %d, active == %d\n",
37348b8a9b1dSJustin T. Gibbs 			 devq->alloc_queue.qfrozen_cnt,
37358b8a9b1dSJustin T. Gibbs 			 devq->alloc_queue.entries,
37368b8a9b1dSJustin T. Gibbs 			 devq->alloc_openings,
37378b8a9b1dSJustin T. Gibbs 			 devq->alloc_active));
37388b8a9b1dSJustin T. Gibbs 
37398b8a9b1dSJustin T. Gibbs 	devq->alloc_queue.qfrozen_cnt++;
37408b8a9b1dSJustin T. Gibbs 	while ((devq->alloc_queue.entries > 0)
37418b8a9b1dSJustin T. Gibbs 	    && (devq->alloc_openings > 0)
37428b8a9b1dSJustin T. Gibbs 	    && (devq->alloc_queue.qfrozen_cnt <= 1)) {
37438b8a9b1dSJustin T. Gibbs 		struct	cam_ed_qinfo *qinfo;
37448b8a9b1dSJustin T. Gibbs 		struct	cam_ed *device;
37458b8a9b1dSJustin T. Gibbs 		union	ccb *work_ccb;
37468b8a9b1dSJustin T. Gibbs 		struct	cam_periph *drv;
37478b8a9b1dSJustin T. Gibbs 		struct	camq *drvq;
37488b8a9b1dSJustin T. Gibbs 
37498b8a9b1dSJustin T. Gibbs 		qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
37505a526431SJustin T. Gibbs 							   CAMQ_HEAD);
37518b8a9b1dSJustin T. Gibbs 		device = qinfo->device;
37528b8a9b1dSJustin T. Gibbs 
3753aa872be6SMatt Jacob 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
375450642f18SKenneth D. Merry 				("running device %p\n", device));
37558b8a9b1dSJustin T. Gibbs 
37568b8a9b1dSJustin T. Gibbs 		drvq = &device->drvq;
37578b8a9b1dSJustin T. Gibbs 
37588b8a9b1dSJustin T. Gibbs #ifdef CAMDEBUG
37598b8a9b1dSJustin T. Gibbs 		if (drvq->entries <= 0) {
37608b8a9b1dSJustin T. Gibbs 			panic("xpt_run_dev_allocq: "
37618b8a9b1dSJustin T. Gibbs 			      "Device on queue without any work to do");
37628b8a9b1dSJustin T. Gibbs 		}
37638b8a9b1dSJustin T. Gibbs #endif
37648b8a9b1dSJustin T. Gibbs 		if ((work_ccb = xpt_get_ccb(device)) != NULL) {
37658b8a9b1dSJustin T. Gibbs 			devq->alloc_openings--;
37668b8a9b1dSJustin T. Gibbs 			devq->alloc_active++;
37675a526431SJustin T. Gibbs 			drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
37688b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
37698b8a9b1dSJustin T. Gibbs 				      drv->pinfo.priority);
3770aa872be6SMatt Jacob 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
37718b8a9b1dSJustin T. Gibbs 					("calling periph start\n"));
37728b8a9b1dSJustin T. Gibbs 			drv->periph_start(drv, work_ccb);
37738b8a9b1dSJustin T. Gibbs 		} else {
37748b8a9b1dSJustin T. Gibbs 			/*
37758b8a9b1dSJustin T. Gibbs 			 * Malloc failure in alloc_ccb
37768b8a9b1dSJustin T. Gibbs 			 */
37778b8a9b1dSJustin T. Gibbs 			/*
37788b8a9b1dSJustin T. Gibbs 			 * XXX add us to a list to be run from free_ccb
37798b8a9b1dSJustin T. Gibbs 			 * if we don't have any ccbs active on this
37808b8a9b1dSJustin T. Gibbs 			 * device queue otherwise we may never get run
37818b8a9b1dSJustin T. Gibbs 			 * again.
37828b8a9b1dSJustin T. Gibbs 			 */
37838b8a9b1dSJustin T. Gibbs 			break;
37848b8a9b1dSJustin T. Gibbs 		}
37858b8a9b1dSJustin T. Gibbs 
37868b8a9b1dSJustin T. Gibbs 		if (drvq->entries > 0) {
37878b8a9b1dSJustin T. Gibbs 			/* We have more work.  Attempt to reschedule */
37888b8a9b1dSJustin T. Gibbs 			xpt_schedule_dev_allocq(bus, device);
37898b8a9b1dSJustin T. Gibbs 		}
37908b8a9b1dSJustin T. Gibbs 	}
37918b8a9b1dSJustin T. Gibbs 	devq->alloc_queue.qfrozen_cnt--;
37928b8a9b1dSJustin T. Gibbs }
37938b8a9b1dSJustin T. Gibbs 
37948b8a9b1dSJustin T. Gibbs static void
37958b8a9b1dSJustin T. Gibbs xpt_run_dev_sendq(struct cam_eb *bus)
37968b8a9b1dSJustin T. Gibbs {
37978b8a9b1dSJustin T. Gibbs 	struct	cam_devq *devq;
37988b8a9b1dSJustin T. Gibbs 
3799aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
38008b8a9b1dSJustin T. Gibbs 
38018b8a9b1dSJustin T. Gibbs 	devq = bus->sim->devq;
38028b8a9b1dSJustin T. Gibbs 
38038b8a9b1dSJustin T. Gibbs 	devq->send_queue.qfrozen_cnt++;
38048b8a9b1dSJustin T. Gibbs 	while ((devq->send_queue.entries > 0)
38058b8a9b1dSJustin T. Gibbs 	    && (devq->send_openings > 0)) {
38068b8a9b1dSJustin T. Gibbs 		struct	cam_ed_qinfo *qinfo;
38078b8a9b1dSJustin T. Gibbs 		struct	cam_ed *device;
38088b8a9b1dSJustin T. Gibbs 		union ccb *work_ccb;
38098b8a9b1dSJustin T. Gibbs 		struct	cam_sim *sim;
38108b8a9b1dSJustin T. Gibbs 
38118b8a9b1dSJustin T. Gibbs 	    	if (devq->send_queue.qfrozen_cnt > 1) {
38128b8a9b1dSJustin T. Gibbs 			break;
38138b8a9b1dSJustin T. Gibbs 		}
38148b8a9b1dSJustin T. Gibbs 
38158b8a9b1dSJustin T. Gibbs 		qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
38165a526431SJustin T. Gibbs 							   CAMQ_HEAD);
38178b8a9b1dSJustin T. Gibbs 		device = qinfo->device;
38188b8a9b1dSJustin T. Gibbs 
38198b8a9b1dSJustin T. Gibbs 		/*
38208b8a9b1dSJustin T. Gibbs 		 * If the device has been "frozen", don't attempt
38218b8a9b1dSJustin T. Gibbs 		 * to run it.
38228b8a9b1dSJustin T. Gibbs 		 */
38238b8a9b1dSJustin T. Gibbs 		if (device->qfrozen_cnt > 0) {
38248b8a9b1dSJustin T. Gibbs 			continue;
38258b8a9b1dSJustin T. Gibbs 		}
38268b8a9b1dSJustin T. Gibbs 
3827aa872be6SMatt Jacob 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
382850642f18SKenneth D. Merry 				("running device %p\n", device));
38298b8a9b1dSJustin T. Gibbs 
38305a526431SJustin T. Gibbs 		work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
38318b8a9b1dSJustin T. Gibbs 		if (work_ccb == NULL) {
383257b89bbcSNate Lawson 			printf("device on run queue with no ccbs???\n");
38338b8a9b1dSJustin T. Gibbs 			continue;
38348b8a9b1dSJustin T. Gibbs 		}
38358b8a9b1dSJustin T. Gibbs 
38368b8a9b1dSJustin T. Gibbs 		if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
38378b8a9b1dSJustin T. Gibbs 
38382b83592fSScott Long 			mtx_lock(&xsoftc.xpt_lock);
38392b83592fSScott Long 		 	if (xsoftc.num_highpower <= 0) {
38408b8a9b1dSJustin T. Gibbs 				/*
38418b8a9b1dSJustin T. Gibbs 				 * We got a high power command, but we
38428b8a9b1dSJustin T. Gibbs 				 * don't have any available slots.  Freeze
38438b8a9b1dSJustin T. Gibbs 				 * the device queue until we have a slot
38448b8a9b1dSJustin T. Gibbs 				 * available.
38458b8a9b1dSJustin T. Gibbs 				 */
38468b8a9b1dSJustin T. Gibbs 				device->qfrozen_cnt++;
38472b83592fSScott Long 				STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
38488b8a9b1dSJustin T. Gibbs 						   &work_ccb->ccb_h,
38498b8a9b1dSJustin T. Gibbs 						   xpt_links.stqe);
38508b8a9b1dSJustin T. Gibbs 
38518b8a9b1dSJustin T. Gibbs 				continue;
38528b8a9b1dSJustin T. Gibbs 			} else {
38538b8a9b1dSJustin T. Gibbs 				/*
38548b8a9b1dSJustin T. Gibbs 				 * Consume a high power slot while
38558b8a9b1dSJustin T. Gibbs 				 * this ccb runs.
38568b8a9b1dSJustin T. Gibbs 				 */
38572b83592fSScott Long 				xsoftc.num_highpower--;
38588b8a9b1dSJustin T. Gibbs 			}
38592b83592fSScott Long 			mtx_unlock(&xsoftc.xpt_lock);
38608b8a9b1dSJustin T. Gibbs 		}
38618b8a9b1dSJustin T. Gibbs 		devq->active_dev = device;
38628b8a9b1dSJustin T. Gibbs 		cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
38638b8a9b1dSJustin T. Gibbs 
38648b8a9b1dSJustin T. Gibbs 		cam_ccbq_send_ccb(&device->ccbq, work_ccb);
38658b8a9b1dSJustin T. Gibbs 
38668b8a9b1dSJustin T. Gibbs 		devq->send_openings--;
38678b8a9b1dSJustin T. Gibbs 		devq->send_active++;
38688b8a9b1dSJustin T. Gibbs 
38695a526431SJustin T. Gibbs 		if (device->ccbq.queue.entries > 0)
38708b8a9b1dSJustin T. Gibbs 			xpt_schedule_dev_sendq(bus, device);
38718b8a9b1dSJustin T. Gibbs 
38728b8a9b1dSJustin T. Gibbs 		if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
38738b8a9b1dSJustin T. Gibbs 			/*
38748b8a9b1dSJustin T. Gibbs 			 * The client wants to freeze the queue
38758b8a9b1dSJustin T. Gibbs 			 * after this CCB is sent.
38768b8a9b1dSJustin T. Gibbs 			 */
38778b8a9b1dSJustin T. Gibbs 			device->qfrozen_cnt++;
38788b8a9b1dSJustin T. Gibbs 		}
38798b8a9b1dSJustin T. Gibbs 
3880a4eb4f16SMatt Jacob 		/* In Target mode, the peripheral driver knows best... */
3881a4eb4f16SMatt Jacob 		if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3882a4eb4f16SMatt Jacob 			if ((device->inq_flags & SID_CmdQue) != 0
3883a4eb4f16SMatt Jacob 			 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
38848b8a9b1dSJustin T. Gibbs 				work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
38858b8a9b1dSJustin T. Gibbs 			else
38868b8a9b1dSJustin T. Gibbs 				/*
3887a4eb4f16SMatt Jacob 				 * Clear this in case of a retried CCB that
3888a4eb4f16SMatt Jacob 				 * failed due to a rejected tag.
38898b8a9b1dSJustin T. Gibbs 				 */
38908b8a9b1dSJustin T. Gibbs 				work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3891a4eb4f16SMatt Jacob 		}
38928b8a9b1dSJustin T. Gibbs 
38938b8a9b1dSJustin T. Gibbs 		/*
38948b8a9b1dSJustin T. Gibbs 		 * Device queues can be shared among multiple sim instances
38958b8a9b1dSJustin T. Gibbs 		 * that reside on different busses.  Use the SIM in the queue
38968b8a9b1dSJustin T. Gibbs 		 * CCB's path, rather than the one in the bus that was passed
38978b8a9b1dSJustin T. Gibbs 		 * into this function.
38988b8a9b1dSJustin T. Gibbs 		 */
38998b8a9b1dSJustin T. Gibbs 		sim = work_ccb->ccb_h.path->bus->sim;
39008b8a9b1dSJustin T. Gibbs 		(*(sim->sim_action))(sim, work_ccb);
39018b8a9b1dSJustin T. Gibbs 
39028b8a9b1dSJustin T. Gibbs 		devq->active_dev = NULL;
39038b8a9b1dSJustin T. Gibbs 	}
39048b8a9b1dSJustin T. Gibbs 	devq->send_queue.qfrozen_cnt--;
39058b8a9b1dSJustin T. Gibbs }
39068b8a9b1dSJustin T. Gibbs 
39078b8a9b1dSJustin T. Gibbs /*
39088b8a9b1dSJustin T. Gibbs  * This function merges stuff from the slave ccb into the master ccb, while
39098b8a9b1dSJustin T. Gibbs  * keeping important fields in the master ccb constant.
39108b8a9b1dSJustin T. Gibbs  */
39118b8a9b1dSJustin T. Gibbs void
39128b8a9b1dSJustin T. Gibbs xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
39138b8a9b1dSJustin T. Gibbs {
391468153f43SScott Long 
39158b8a9b1dSJustin T. Gibbs 	/*
39168b8a9b1dSJustin T. Gibbs 	 * Pull fields that are valid for peripheral drivers to set
39178b8a9b1dSJustin T. Gibbs 	 * into the master CCB along with the CCB "payload".
39188b8a9b1dSJustin T. Gibbs 	 */
39198b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
39208b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
39218b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
39228b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
39238b8a9b1dSJustin T. Gibbs 	bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
39248b8a9b1dSJustin T. Gibbs 	      sizeof(union ccb) - sizeof(struct ccb_hdr));
39258b8a9b1dSJustin T. Gibbs }
39268b8a9b1dSJustin T. Gibbs 
39278b8a9b1dSJustin T. Gibbs void
39288b8a9b1dSJustin T. Gibbs xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
39298b8a9b1dSJustin T. Gibbs {
393068153f43SScott Long 
39318b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
39328b8a9b1dSJustin T. Gibbs 	ccb_h->pinfo.priority = priority;
39338b8a9b1dSJustin T. Gibbs 	ccb_h->path = path;
39348b8a9b1dSJustin T. Gibbs 	ccb_h->path_id = path->bus->path_id;
39358b8a9b1dSJustin T. Gibbs 	if (path->target)
39368b8a9b1dSJustin T. Gibbs 		ccb_h->target_id = path->target->target_id;
39378b8a9b1dSJustin T. Gibbs 	else
39388b8a9b1dSJustin T. Gibbs 		ccb_h->target_id = CAM_TARGET_WILDCARD;
39398b8a9b1dSJustin T. Gibbs 	if (path->device) {
39408b8a9b1dSJustin T. Gibbs 		ccb_h->target_lun = path->device->lun_id;
39418bad620dSJustin T. Gibbs 		ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
39428b8a9b1dSJustin T. Gibbs 	} else {
39438b8a9b1dSJustin T. Gibbs 		ccb_h->target_lun = CAM_TARGET_WILDCARD;
39448b8a9b1dSJustin T. Gibbs 	}
39458b8a9b1dSJustin T. Gibbs 	ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
39468b8a9b1dSJustin T. Gibbs 	ccb_h->flags = 0;
39478b8a9b1dSJustin T. Gibbs }
39488b8a9b1dSJustin T. Gibbs 
39498b8a9b1dSJustin T. Gibbs /* Path manipulation functions */
39508b8a9b1dSJustin T. Gibbs cam_status
39518b8a9b1dSJustin T. Gibbs xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
39528b8a9b1dSJustin T. Gibbs 		path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
39538b8a9b1dSJustin T. Gibbs {
39548b8a9b1dSJustin T. Gibbs 	struct	   cam_path *path;
39558b8a9b1dSJustin T. Gibbs 	cam_status status;
39568b8a9b1dSJustin T. Gibbs 
3957362abc44STai-hwa Liang 	path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
39588b8a9b1dSJustin T. Gibbs 
39598b8a9b1dSJustin T. Gibbs 	if (path == NULL) {
39608b8a9b1dSJustin T. Gibbs 		status = CAM_RESRC_UNAVAIL;
39618b8a9b1dSJustin T. Gibbs 		return(status);
39628b8a9b1dSJustin T. Gibbs 	}
39638b8a9b1dSJustin T. Gibbs 	status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
39648b8a9b1dSJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
3965362abc44STai-hwa Liang 		free(path, M_CAMXPT);
39668b8a9b1dSJustin T. Gibbs 		path = NULL;
39678b8a9b1dSJustin T. Gibbs 	}
39688b8a9b1dSJustin T. Gibbs 	*new_path_ptr = path;
39698b8a9b1dSJustin T. Gibbs 	return (status);
39708b8a9b1dSJustin T. Gibbs }
39718b8a9b1dSJustin T. Gibbs 
39722b83592fSScott Long cam_status
39732b83592fSScott Long xpt_create_path_unlocked(struct cam_path **new_path_ptr,
39742b83592fSScott Long 			 struct cam_periph *periph, path_id_t path_id,
39752b83592fSScott Long 			 target_id_t target_id, lun_id_t lun_id)
39762b83592fSScott Long {
39772b83592fSScott Long 	struct	   cam_path *path;
39782b83592fSScott Long 	struct	   cam_eb *bus = NULL;
39792b83592fSScott Long 	cam_status status;
39802b83592fSScott Long 	int	   need_unlock = 0;
39812b83592fSScott Long 
39822b83592fSScott Long 	path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_WAITOK);
39832b83592fSScott Long 
39842b83592fSScott Long 	if (path_id != CAM_BUS_WILDCARD) {
39852b83592fSScott Long 		bus = xpt_find_bus(path_id);
39862b83592fSScott Long 		if (bus != NULL) {
39872b83592fSScott Long 			need_unlock = 1;
398811e4faceSScott Long 			CAM_SIM_LOCK(bus->sim);
39892b83592fSScott Long 		}
39902b83592fSScott Long 	}
39912b83592fSScott Long 	status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
39922b83592fSScott Long 	if (need_unlock)
399311e4faceSScott Long 		CAM_SIM_UNLOCK(bus->sim);
39942b83592fSScott Long 	if (status != CAM_REQ_CMP) {
39952b83592fSScott Long 		free(path, M_CAMXPT);
39962b83592fSScott Long 		path = NULL;
39972b83592fSScott Long 	}
39982b83592fSScott Long 	*new_path_ptr = path;
39992b83592fSScott Long 	return (status);
40002b83592fSScott Long }
40012b83592fSScott Long 
40028b8a9b1dSJustin T. Gibbs static cam_status
40038b8a9b1dSJustin T. Gibbs xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
40048b8a9b1dSJustin T. Gibbs 		 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
40058b8a9b1dSJustin T. Gibbs {
40068b8a9b1dSJustin T. Gibbs 	struct	     cam_eb *bus;
40078b8a9b1dSJustin T. Gibbs 	struct	     cam_et *target;
40088b8a9b1dSJustin T. Gibbs 	struct	     cam_ed *device;
40098b8a9b1dSJustin T. Gibbs 	cam_status   status;
40108b8a9b1dSJustin T. Gibbs 
40118b8a9b1dSJustin T. Gibbs 	status = CAM_REQ_CMP;	/* Completed without error */
40128b8a9b1dSJustin T. Gibbs 	target = NULL;		/* Wildcarded */
40138b8a9b1dSJustin T. Gibbs 	device = NULL;		/* Wildcarded */
4014a5479bc5SJustin T. Gibbs 
4015a5479bc5SJustin T. Gibbs 	/*
4016a5479bc5SJustin T. Gibbs 	 * We will potentially modify the EDT, so block interrupts
4017a5479bc5SJustin T. Gibbs 	 * that may attempt to create cam paths.
4018a5479bc5SJustin T. Gibbs 	 */
40198b8a9b1dSJustin T. Gibbs 	bus = xpt_find_bus(path_id);
40208b8a9b1dSJustin T. Gibbs 	if (bus == NULL) {
40218b8a9b1dSJustin T. Gibbs 		status = CAM_PATH_INVALID;
4022c8bead2aSJustin T. Gibbs 	} else {
40238b8a9b1dSJustin T. Gibbs 		target = xpt_find_target(bus, target_id);
40248b8a9b1dSJustin T. Gibbs 		if (target == NULL) {
40258b8a9b1dSJustin T. Gibbs 			/* Create one */
40268b8a9b1dSJustin T. Gibbs 			struct cam_et *new_target;
40278b8a9b1dSJustin T. Gibbs 
40288b8a9b1dSJustin T. Gibbs 			new_target = xpt_alloc_target(bus, target_id);
40298b8a9b1dSJustin T. Gibbs 			if (new_target == NULL) {
40308b8a9b1dSJustin T. Gibbs 				status = CAM_RESRC_UNAVAIL;
40318b8a9b1dSJustin T. Gibbs 			} else {
40328b8a9b1dSJustin T. Gibbs 				target = new_target;
40338b8a9b1dSJustin T. Gibbs 			}
40348b8a9b1dSJustin T. Gibbs 		}
4035c8bead2aSJustin T. Gibbs 		if (target != NULL) {
40368b8a9b1dSJustin T. Gibbs 			device = xpt_find_device(target, lun_id);
40378b8a9b1dSJustin T. Gibbs 			if (device == NULL) {
40388b8a9b1dSJustin T. Gibbs 				/* Create one */
40398b8a9b1dSJustin T. Gibbs 				struct cam_ed *new_device;
40408b8a9b1dSJustin T. Gibbs 
40418b8a9b1dSJustin T. Gibbs 				new_device = xpt_alloc_device(bus,
40428b8a9b1dSJustin T. Gibbs 							      target,
40438b8a9b1dSJustin T. Gibbs 							      lun_id);
40448b8a9b1dSJustin T. Gibbs 				if (new_device == NULL) {
40458b8a9b1dSJustin T. Gibbs 					status = CAM_RESRC_UNAVAIL;
40468b8a9b1dSJustin T. Gibbs 				} else {
40478b8a9b1dSJustin T. Gibbs 					device = new_device;
40488b8a9b1dSJustin T. Gibbs 				}
40498b8a9b1dSJustin T. Gibbs 			}
40508b8a9b1dSJustin T. Gibbs 		}
40518b8a9b1dSJustin T. Gibbs 	}
40528b8a9b1dSJustin T. Gibbs 
40538b8a9b1dSJustin T. Gibbs 	/*
40548b8a9b1dSJustin T. Gibbs 	 * Only touch the user's data if we are successful.
40558b8a9b1dSJustin T. Gibbs 	 */
40568b8a9b1dSJustin T. Gibbs 	if (status == CAM_REQ_CMP) {
40578b8a9b1dSJustin T. Gibbs 		new_path->periph = perph;
40588b8a9b1dSJustin T. Gibbs 		new_path->bus = bus;
40598b8a9b1dSJustin T. Gibbs 		new_path->target = target;
40608b8a9b1dSJustin T. Gibbs 		new_path->device = device;
40618b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
40628b8a9b1dSJustin T. Gibbs 	} else {
40638b8a9b1dSJustin T. Gibbs 		if (device != NULL)
40648b8a9b1dSJustin T. Gibbs 			xpt_release_device(bus, target, device);
40658b8a9b1dSJustin T. Gibbs 		if (target != NULL)
40668b8a9b1dSJustin T. Gibbs 			xpt_release_target(bus, target);
4067a5479bc5SJustin T. Gibbs 		if (bus != NULL)
4068a5479bc5SJustin T. Gibbs 			xpt_release_bus(bus);
40698b8a9b1dSJustin T. Gibbs 	}
40708b8a9b1dSJustin T. Gibbs 	return (status);
40718b8a9b1dSJustin T. Gibbs }
40728b8a9b1dSJustin T. Gibbs 
40738b8a9b1dSJustin T. Gibbs static void
40748b8a9b1dSJustin T. Gibbs xpt_release_path(struct cam_path *path)
40758b8a9b1dSJustin T. Gibbs {
40768b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
40779dd03ecfSJustin T. Gibbs 	if (path->device != NULL) {
40788b8a9b1dSJustin T. Gibbs 		xpt_release_device(path->bus, path->target, path->device);
40799dd03ecfSJustin T. Gibbs 		path->device = NULL;
40809dd03ecfSJustin T. Gibbs 	}
40819dd03ecfSJustin T. Gibbs 	if (path->target != NULL) {
40828b8a9b1dSJustin T. Gibbs 		xpt_release_target(path->bus, path->target);
40839dd03ecfSJustin T. Gibbs 		path->target = NULL;
40849dd03ecfSJustin T. Gibbs 	}
40859dd03ecfSJustin T. Gibbs 	if (path->bus != NULL) {
40869dd03ecfSJustin T. Gibbs 		xpt_release_bus(path->bus);
40879dd03ecfSJustin T. Gibbs 		path->bus = NULL;
40889dd03ecfSJustin T. Gibbs 	}
40898b8a9b1dSJustin T. Gibbs }
40908b8a9b1dSJustin T. Gibbs 
40918b8a9b1dSJustin T. Gibbs void
40928b8a9b1dSJustin T. Gibbs xpt_free_path(struct cam_path *path)
40938b8a9b1dSJustin T. Gibbs {
409468153f43SScott Long 
40958b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
40968b8a9b1dSJustin T. Gibbs 	xpt_release_path(path);
4097362abc44STai-hwa Liang 	free(path, M_CAMXPT);
40988b8a9b1dSJustin T. Gibbs }
40998b8a9b1dSJustin T. Gibbs 
41008b8a9b1dSJustin T. Gibbs 
41018b8a9b1dSJustin T. Gibbs /*
41022cefde5fSJustin T. Gibbs  * Return -1 for failure, 0 for exact match, 1 for match with wildcards
41032cefde5fSJustin T. Gibbs  * in path1, 2 for match with wildcards in path2.
41048b8a9b1dSJustin T. Gibbs  */
41058b8a9b1dSJustin T. Gibbs int
41068b8a9b1dSJustin T. Gibbs xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
41078b8a9b1dSJustin T. Gibbs {
41088b8a9b1dSJustin T. Gibbs 	int retval = 0;
41098b8a9b1dSJustin T. Gibbs 
41108b8a9b1dSJustin T. Gibbs 	if (path1->bus != path2->bus) {
41112cefde5fSJustin T. Gibbs 		if (path1->bus->path_id == CAM_BUS_WILDCARD)
41128b8a9b1dSJustin T. Gibbs 			retval = 1;
41132cefde5fSJustin T. Gibbs 		else if (path2->bus->path_id == CAM_BUS_WILDCARD)
41142cefde5fSJustin T. Gibbs 			retval = 2;
41158b8a9b1dSJustin T. Gibbs 		else
41168b8a9b1dSJustin T. Gibbs 			return (-1);
41178b8a9b1dSJustin T. Gibbs 	}
41188b8a9b1dSJustin T. Gibbs 	if (path1->target != path2->target) {
41192cefde5fSJustin T. Gibbs 		if (path1->target->target_id == CAM_TARGET_WILDCARD) {
41202cefde5fSJustin T. Gibbs 			if (retval == 0)
41218b8a9b1dSJustin T. Gibbs 				retval = 1;
41222cefde5fSJustin T. Gibbs 		} else if (path2->target->target_id == CAM_TARGET_WILDCARD)
41232cefde5fSJustin T. Gibbs 			retval = 2;
41248b8a9b1dSJustin T. Gibbs 		else
41258b8a9b1dSJustin T. Gibbs 			return (-1);
41268b8a9b1dSJustin T. Gibbs 	}
41278b8a9b1dSJustin T. Gibbs 	if (path1->device != path2->device) {
41282cefde5fSJustin T. Gibbs 		if (path1->device->lun_id == CAM_LUN_WILDCARD) {
41292cefde5fSJustin T. Gibbs 			if (retval == 0)
41308b8a9b1dSJustin T. Gibbs 				retval = 1;
41312cefde5fSJustin T. Gibbs 		} else if (path2->device->lun_id == CAM_LUN_WILDCARD)
41322cefde5fSJustin T. Gibbs 			retval = 2;
41338b8a9b1dSJustin T. Gibbs 		else
41348b8a9b1dSJustin T. Gibbs 			return (-1);
41358b8a9b1dSJustin T. Gibbs 	}
41368b8a9b1dSJustin T. Gibbs 	return (retval);
41378b8a9b1dSJustin T. Gibbs }
41388b8a9b1dSJustin T. Gibbs 
41398b8a9b1dSJustin T. Gibbs void
41408b8a9b1dSJustin T. Gibbs xpt_print_path(struct cam_path *path)
41418b8a9b1dSJustin T. Gibbs {
41422b83592fSScott Long 	mtx_assert(path->bus->sim->mtx, MA_OWNED);
414368153f43SScott Long 
41448b8a9b1dSJustin T. Gibbs 	if (path == NULL)
41458b8a9b1dSJustin T. Gibbs 		printf("(nopath): ");
41468b8a9b1dSJustin T. Gibbs 	else {
41478b8a9b1dSJustin T. Gibbs 		if (path->periph != NULL)
41488b8a9b1dSJustin T. Gibbs 			printf("(%s%d:", path->periph->periph_name,
41498b8a9b1dSJustin T. Gibbs 			       path->periph->unit_number);
41508b8a9b1dSJustin T. Gibbs 		else
41518b8a9b1dSJustin T. Gibbs 			printf("(noperiph:");
41528b8a9b1dSJustin T. Gibbs 
41538b8a9b1dSJustin T. Gibbs 		if (path->bus != NULL)
41548b8a9b1dSJustin T. Gibbs 			printf("%s%d:%d:", path->bus->sim->sim_name,
41558b8a9b1dSJustin T. Gibbs 			       path->bus->sim->unit_number,
41568b8a9b1dSJustin T. Gibbs 			       path->bus->sim->bus_id);
41578b8a9b1dSJustin T. Gibbs 		else
41588b8a9b1dSJustin T. Gibbs 			printf("nobus:");
41598b8a9b1dSJustin T. Gibbs 
41608b8a9b1dSJustin T. Gibbs 		if (path->target != NULL)
41618b8a9b1dSJustin T. Gibbs 			printf("%d:", path->target->target_id);
41628b8a9b1dSJustin T. Gibbs 		else
41638b8a9b1dSJustin T. Gibbs 			printf("X:");
41648b8a9b1dSJustin T. Gibbs 
41658b8a9b1dSJustin T. Gibbs 		if (path->device != NULL)
41668b8a9b1dSJustin T. Gibbs 			printf("%d): ", path->device->lun_id);
41678b8a9b1dSJustin T. Gibbs 		else
41688b8a9b1dSJustin T. Gibbs 			printf("X): ");
41698b8a9b1dSJustin T. Gibbs 	}
41708b8a9b1dSJustin T. Gibbs }
41718b8a9b1dSJustin T. Gibbs 
4172f0d9af51SMatt Jacob void
4173f0d9af51SMatt Jacob xpt_print(struct cam_path *path, const char *fmt, ...)
4174f0d9af51SMatt Jacob {
4175f0d9af51SMatt Jacob 	va_list ap;
4176f0d9af51SMatt Jacob 	xpt_print_path(path);
4177f0d9af51SMatt Jacob 	va_start(ap, fmt);
4178f0d9af51SMatt Jacob 	vprintf(fmt, ap);
4179f0d9af51SMatt Jacob 	va_end(ap);
4180f0d9af51SMatt Jacob }
4181f0d9af51SMatt Jacob 
41823393f8daSKenneth D. Merry int
41833393f8daSKenneth D. Merry xpt_path_string(struct cam_path *path, char *str, size_t str_len)
41843393f8daSKenneth D. Merry {
41853393f8daSKenneth D. Merry 	struct sbuf sb;
41863393f8daSKenneth D. Merry 
41872b83592fSScott Long 	mtx_assert(path->bus->sim->mtx, MA_OWNED);
418868153f43SScott Long 
41893393f8daSKenneth D. Merry 	sbuf_new(&sb, str, str_len, 0);
41903393f8daSKenneth D. Merry 
41913393f8daSKenneth D. Merry 	if (path == NULL)
41923393f8daSKenneth D. Merry 		sbuf_printf(&sb, "(nopath): ");
41933393f8daSKenneth D. Merry 	else {
41943393f8daSKenneth D. Merry 		if (path->periph != NULL)
41953393f8daSKenneth D. Merry 			sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
41963393f8daSKenneth D. Merry 				    path->periph->unit_number);
41973393f8daSKenneth D. Merry 		else
41983393f8daSKenneth D. Merry 			sbuf_printf(&sb, "(noperiph:");
41993393f8daSKenneth D. Merry 
42003393f8daSKenneth D. Merry 		if (path->bus != NULL)
42013393f8daSKenneth D. Merry 			sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
42023393f8daSKenneth D. Merry 				    path->bus->sim->unit_number,
42033393f8daSKenneth D. Merry 				    path->bus->sim->bus_id);
42043393f8daSKenneth D. Merry 		else
42053393f8daSKenneth D. Merry 			sbuf_printf(&sb, "nobus:");
42063393f8daSKenneth D. Merry 
42073393f8daSKenneth D. Merry 		if (path->target != NULL)
42083393f8daSKenneth D. Merry 			sbuf_printf(&sb, "%d:", path->target->target_id);
42093393f8daSKenneth D. Merry 		else
42103393f8daSKenneth D. Merry 			sbuf_printf(&sb, "X:");
42113393f8daSKenneth D. Merry 
42123393f8daSKenneth D. Merry 		if (path->device != NULL)
42133393f8daSKenneth D. Merry 			sbuf_printf(&sb, "%d): ", path->device->lun_id);
42143393f8daSKenneth D. Merry 		else
42153393f8daSKenneth D. Merry 			sbuf_printf(&sb, "X): ");
42163393f8daSKenneth D. Merry 	}
42173393f8daSKenneth D. Merry 	sbuf_finish(&sb);
42183393f8daSKenneth D. Merry 
42193393f8daSKenneth D. Merry 	return(sbuf_len(&sb));
42203393f8daSKenneth D. Merry }
42213393f8daSKenneth D. Merry 
42228b8a9b1dSJustin T. Gibbs path_id_t
42238b8a9b1dSJustin T. Gibbs xpt_path_path_id(struct cam_path *path)
42248b8a9b1dSJustin T. Gibbs {
42252b83592fSScott Long 	mtx_assert(path->bus->sim->mtx, MA_OWNED);
422668153f43SScott Long 
42278b8a9b1dSJustin T. Gibbs 	return(path->bus->path_id);
42288b8a9b1dSJustin T. Gibbs }
42298b8a9b1dSJustin T. Gibbs 
42308b8a9b1dSJustin T. Gibbs target_id_t
42318b8a9b1dSJustin T. Gibbs xpt_path_target_id(struct cam_path *path)
42328b8a9b1dSJustin T. Gibbs {
42332b83592fSScott Long 	mtx_assert(path->bus->sim->mtx, MA_OWNED);
423468153f43SScott Long 
42358b8a9b1dSJustin T. Gibbs 	if (path->target != NULL)
42368b8a9b1dSJustin T. Gibbs 		return (path->target->target_id);
42378b8a9b1dSJustin T. Gibbs 	else
42388b8a9b1dSJustin T. Gibbs 		return (CAM_TARGET_WILDCARD);
42398b8a9b1dSJustin T. Gibbs }
42408b8a9b1dSJustin T. Gibbs 
42418b8a9b1dSJustin T. Gibbs lun_id_t
42428b8a9b1dSJustin T. Gibbs xpt_path_lun_id(struct cam_path *path)
42438b8a9b1dSJustin T. Gibbs {
42442b83592fSScott Long 	mtx_assert(path->bus->sim->mtx, MA_OWNED);
424568153f43SScott Long 
42468b8a9b1dSJustin T. Gibbs 	if (path->device != NULL)
42478b8a9b1dSJustin T. Gibbs 		return (path->device->lun_id);
42488b8a9b1dSJustin T. Gibbs 	else
42498b8a9b1dSJustin T. Gibbs 		return (CAM_LUN_WILDCARD);
42508b8a9b1dSJustin T. Gibbs }
42518b8a9b1dSJustin T. Gibbs 
42528b8a9b1dSJustin T. Gibbs struct cam_sim *
42538b8a9b1dSJustin T. Gibbs xpt_path_sim(struct cam_path *path)
42548b8a9b1dSJustin T. Gibbs {
425568153f43SScott Long 
42568b8a9b1dSJustin T. Gibbs 	return (path->bus->sim);
42578b8a9b1dSJustin T. Gibbs }
42588b8a9b1dSJustin T. Gibbs 
42598b8a9b1dSJustin T. Gibbs struct cam_periph*
42608b8a9b1dSJustin T. Gibbs xpt_path_periph(struct cam_path *path)
42618b8a9b1dSJustin T. Gibbs {
42622b83592fSScott Long 	mtx_assert(path->bus->sim->mtx, MA_OWNED);
426368153f43SScott Long 
42648b8a9b1dSJustin T. Gibbs 	return (path->periph);
42658b8a9b1dSJustin T. Gibbs }
42668b8a9b1dSJustin T. Gibbs 
42678b8a9b1dSJustin T. Gibbs /*
42688b8a9b1dSJustin T. Gibbs  * Release a CAM control block for the caller.  Remit the cost of the structure
42698b8a9b1dSJustin T. Gibbs  * to the device referenced by the path.  If the this device had no 'credits'
42708b8a9b1dSJustin T. Gibbs  * and peripheral drivers have registered async callbacks for this notification
42718b8a9b1dSJustin T. Gibbs  * call them now.
42728b8a9b1dSJustin T. Gibbs  */
42738b8a9b1dSJustin T. Gibbs void
42748b8a9b1dSJustin T. Gibbs xpt_release_ccb(union ccb *free_ccb)
42758b8a9b1dSJustin T. Gibbs {
42768b8a9b1dSJustin T. Gibbs 	struct	 cam_path *path;
42778b8a9b1dSJustin T. Gibbs 	struct	 cam_ed *device;
42788b8a9b1dSJustin T. Gibbs 	struct	 cam_eb *bus;
42792b83592fSScott Long 	struct   cam_sim *sim;
428068153f43SScott Long 
4281aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
42828b8a9b1dSJustin T. Gibbs 	path = free_ccb->ccb_h.path;
42838b8a9b1dSJustin T. Gibbs 	device = path->device;
42848b8a9b1dSJustin T. Gibbs 	bus = path->bus;
42852b83592fSScott Long 	sim = bus->sim;
42862b83592fSScott Long 
42872b83592fSScott Long 	mtx_assert(sim->mtx, MA_OWNED);
42882b83592fSScott Long 
42898b8a9b1dSJustin T. Gibbs 	cam_ccbq_release_opening(&device->ccbq);
42902b83592fSScott Long 	if (sim->ccb_count > sim->max_ccbs) {
42918b8a9b1dSJustin T. Gibbs 		xpt_free_ccb(free_ccb);
42922b83592fSScott Long 		sim->ccb_count--;
42938b8a9b1dSJustin T. Gibbs 	} else {
42942b83592fSScott Long 		SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
42952b83592fSScott Long 		    xpt_links.sle);
42968b8a9b1dSJustin T. Gibbs 	}
42972b83592fSScott Long 	if (sim->devq == NULL) {
4298d3ef3454SIan Dowse 		return;
4299d3ef3454SIan Dowse 	}
43002b83592fSScott Long 	sim->devq->alloc_openings++;
43012b83592fSScott Long 	sim->devq->alloc_active--;
43028b8a9b1dSJustin T. Gibbs 	/* XXX Turn this into an inline function - xpt_run_device?? */
43038b8a9b1dSJustin T. Gibbs 	if ((device_is_alloc_queued(device) == 0)
43048b8a9b1dSJustin T. Gibbs 	 && (device->drvq.entries > 0)) {
43058b8a9b1dSJustin T. Gibbs 		xpt_schedule_dev_allocq(bus, device);
43068b8a9b1dSJustin T. Gibbs 	}
43072b83592fSScott Long 	if (dev_allocq_is_runnable(sim->devq))
43088b8a9b1dSJustin T. Gibbs 		xpt_run_dev_allocq(bus);
43098b8a9b1dSJustin T. Gibbs }
43108b8a9b1dSJustin T. Gibbs 
43118b8a9b1dSJustin T. Gibbs /* Functions accessed by SIM drivers */
43128b8a9b1dSJustin T. Gibbs 
43138b8a9b1dSJustin T. Gibbs /*
43148b8a9b1dSJustin T. Gibbs  * A sim structure, listing the SIM entry points and instance
43158b8a9b1dSJustin T. Gibbs  * identification info is passed to xpt_bus_register to hook the SIM
43168b8a9b1dSJustin T. Gibbs  * into the CAM framework.  xpt_bus_register creates a cam_eb entry
43178b8a9b1dSJustin T. Gibbs  * for this new bus and places it in the array of busses and assigns
43188b8a9b1dSJustin T. Gibbs  * it a path_id.  The path_id may be influenced by "hard wiring"
43198b8a9b1dSJustin T. Gibbs  * information specified by the user.  Once interrupt services are
43208b8a9b1dSJustin T. Gibbs  * availible, the bus will be probed.
43218b8a9b1dSJustin T. Gibbs  */
43228b8a9b1dSJustin T. Gibbs int32_t
43238b8a9b1dSJustin T. Gibbs xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
43248b8a9b1dSJustin T. Gibbs {
43258b8a9b1dSJustin T. Gibbs 	struct cam_eb *new_bus;
4326434bbf6eSJustin T. Gibbs 	struct cam_eb *old_bus;
43278b8a9b1dSJustin T. Gibbs 	struct ccb_pathinq cpi;
43288b8a9b1dSJustin T. Gibbs 
43292b83592fSScott Long 	mtx_assert(sim->mtx, MA_OWNED);
433068153f43SScott Long 
43318b8a9b1dSJustin T. Gibbs 	sim->bus_id = bus;
43328b8a9b1dSJustin T. Gibbs 	new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4333362abc44STai-hwa Liang 					  M_CAMXPT, M_NOWAIT);
43348b8a9b1dSJustin T. Gibbs 	if (new_bus == NULL) {
43358b8a9b1dSJustin T. Gibbs 		/* Couldn't satisfy request */
43368b8a9b1dSJustin T. Gibbs 		return (CAM_RESRC_UNAVAIL);
43378b8a9b1dSJustin T. Gibbs 	}
43388b8a9b1dSJustin T. Gibbs 
43398b8a9b1dSJustin T. Gibbs 	if (strcmp(sim->sim_name, "xpt") != 0) {
43408b8a9b1dSJustin T. Gibbs 
4341434bbf6eSJustin T. Gibbs 		sim->path_id =
4342434bbf6eSJustin T. Gibbs 		    xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
43438b8a9b1dSJustin T. Gibbs 	}
43448b8a9b1dSJustin T. Gibbs 
4345434bbf6eSJustin T. Gibbs 	TAILQ_INIT(&new_bus->et_entries);
43468b8a9b1dSJustin T. Gibbs 	new_bus->path_id = sim->path_id;
43478b8a9b1dSJustin T. Gibbs 	new_bus->sim = sim;
434887cfaf0eSJustin T. Gibbs 	timevalclear(&new_bus->last_reset);
4349434bbf6eSJustin T. Gibbs 	new_bus->flags = 0;
4350a5479bc5SJustin T. Gibbs 	new_bus->refcount = 1;	/* Held until a bus_deregister event */
4351434bbf6eSJustin T. Gibbs 	new_bus->generation = 0;
43522b83592fSScott Long 	mtx_lock(&xsoftc.xpt_topo_lock);
43532b83592fSScott Long 	old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4354434bbf6eSJustin T. Gibbs 	while (old_bus != NULL
4355434bbf6eSJustin T. Gibbs 	    && old_bus->path_id < new_bus->path_id)
4356434bbf6eSJustin T. Gibbs 		old_bus = TAILQ_NEXT(old_bus, links);
4357434bbf6eSJustin T. Gibbs 	if (old_bus != NULL)
4358434bbf6eSJustin T. Gibbs 		TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4359434bbf6eSJustin T. Gibbs 	else
43602b83592fSScott Long 		TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
43612b83592fSScott Long 	xsoftc.bus_generation++;
43622b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_topo_lock);
43638b8a9b1dSJustin T. Gibbs 
43648b8a9b1dSJustin T. Gibbs 	/* Notify interested parties */
43658b8a9b1dSJustin T. Gibbs 	if (sim->path_id != CAM_XPT_PATH_ID) {
43668b8a9b1dSJustin T. Gibbs 		struct cam_path path;
43678b8a9b1dSJustin T. Gibbs 
43688b8a9b1dSJustin T. Gibbs 		xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
43698b8a9b1dSJustin T. Gibbs 			         CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
43708b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
43718b8a9b1dSJustin T. Gibbs 		cpi.ccb_h.func_code = XPT_PATH_INQ;
43728b8a9b1dSJustin T. Gibbs 		xpt_action((union ccb *)&cpi);
43733393f8daSKenneth D. Merry 		xpt_async(AC_PATH_REGISTERED, &path, &cpi);
43748b8a9b1dSJustin T. Gibbs 		xpt_release_path(&path);
43758b8a9b1dSJustin T. Gibbs 	}
43768b8a9b1dSJustin T. Gibbs 	return (CAM_SUCCESS);
43778b8a9b1dSJustin T. Gibbs }
43788b8a9b1dSJustin T. Gibbs 
4379434bbf6eSJustin T. Gibbs int32_t
4380434bbf6eSJustin T. Gibbs xpt_bus_deregister(path_id_t pathid)
43818b8a9b1dSJustin T. Gibbs {
4382434bbf6eSJustin T. Gibbs 	struct cam_path bus_path;
4383d3ef3454SIan Dowse 	struct cam_ed *device;
4384d3ef3454SIan Dowse 	struct cam_ed_qinfo *qinfo;
4385d3ef3454SIan Dowse 	struct cam_devq *devq;
4386d3ef3454SIan Dowse 	struct cam_periph *periph;
4387d3ef3454SIan Dowse 	struct cam_sim *ccbsim;
4388d3ef3454SIan Dowse 	union ccb *work_ccb;
4389434bbf6eSJustin T. Gibbs 	cam_status status;
4390434bbf6eSJustin T. Gibbs 
439168153f43SScott Long 
4392434bbf6eSJustin T. Gibbs 	status = xpt_compile_path(&bus_path, NULL, pathid,
4393434bbf6eSJustin T. Gibbs 				  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4394434bbf6eSJustin T. Gibbs 	if (status != CAM_REQ_CMP)
4395434bbf6eSJustin T. Gibbs 		return (status);
4396434bbf6eSJustin T. Gibbs 
4397434bbf6eSJustin T. Gibbs 	xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4398434bbf6eSJustin T. Gibbs 	xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4399434bbf6eSJustin T. Gibbs 
4400d3ef3454SIan Dowse 	/* The SIM may be gone, so use a dummy SIM for any stray operations. */
4401d3ef3454SIan Dowse 	devq = bus_path.bus->sim->devq;
44029758cc83SScott Long 	ccbsim = bus_path.bus->sim;
4403d3ef3454SIan Dowse 	bus_path.bus->sim = &cam_dead_sim;
4404d3ef3454SIan Dowse 
4405d3ef3454SIan Dowse 	/* Execute any pending operations now. */
4406d3ef3454SIan Dowse 	while ((qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
4407d3ef3454SIan Dowse 	    CAMQ_HEAD)) != NULL ||
4408d3ef3454SIan Dowse 	    (qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
4409d3ef3454SIan Dowse 	    CAMQ_HEAD)) != NULL) {
4410d3ef3454SIan Dowse 		do {
4411d3ef3454SIan Dowse 			device = qinfo->device;
4412d3ef3454SIan Dowse 			work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
4413d3ef3454SIan Dowse 			if (work_ccb != NULL) {
4414d3ef3454SIan Dowse 				devq->active_dev = device;
4415d3ef3454SIan Dowse 				cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
4416d3ef3454SIan Dowse 				cam_ccbq_send_ccb(&device->ccbq, work_ccb);
4417d3ef3454SIan Dowse 				(*(ccbsim->sim_action))(ccbsim, work_ccb);
4418d3ef3454SIan Dowse 			}
4419d3ef3454SIan Dowse 
4420d3ef3454SIan Dowse 			periph = (struct cam_periph *)camq_remove(&device->drvq,
4421d3ef3454SIan Dowse 			    CAMQ_HEAD);
4422d3ef3454SIan Dowse 			if (periph != NULL)
4423d3ef3454SIan Dowse 				xpt_schedule(periph, periph->pinfo.priority);
4424d3ef3454SIan Dowse 		} while (work_ccb != NULL || periph != NULL);
4425d3ef3454SIan Dowse 	}
4426d3ef3454SIan Dowse 
4427d3ef3454SIan Dowse 	/* Make sure all completed CCBs are processed. */
44289758cc83SScott Long 	while (!TAILQ_EMPTY(&ccbsim->sim_doneq)) {
44299758cc83SScott Long 		camisr_runqueue(&ccbsim->sim_doneq);
4430d3ef3454SIan Dowse 
4431d3ef3454SIan Dowse 		/* Repeat the async's for the benefit of any new devices. */
4432d3ef3454SIan Dowse 		xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4433d3ef3454SIan Dowse 		xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4434d3ef3454SIan Dowse 	}
4435d3ef3454SIan Dowse 
4436434bbf6eSJustin T. Gibbs 	/* Release the reference count held while registered. */
4437434bbf6eSJustin T. Gibbs 	xpt_release_bus(bus_path.bus);
4438434bbf6eSJustin T. Gibbs 	xpt_release_path(&bus_path);
4439434bbf6eSJustin T. Gibbs 
4440434bbf6eSJustin T. Gibbs 	return (CAM_REQ_CMP);
4441434bbf6eSJustin T. Gibbs }
4442434bbf6eSJustin T. Gibbs 
4443434bbf6eSJustin T. Gibbs static path_id_t
4444434bbf6eSJustin T. Gibbs xptnextfreepathid(void)
4445434bbf6eSJustin T. Gibbs {
4446434bbf6eSJustin T. Gibbs 	struct cam_eb *bus;
4447434bbf6eSJustin T. Gibbs 	path_id_t pathid;
44482398f0cdSPeter Wemm 	const char *strval;
44498b8a9b1dSJustin T. Gibbs 
4450434bbf6eSJustin T. Gibbs 	pathid = 0;
44512b83592fSScott Long 	mtx_lock(&xsoftc.xpt_topo_lock);
44522b83592fSScott Long 	bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4453434bbf6eSJustin T. Gibbs retry:
4454434bbf6eSJustin T. Gibbs 	/* Find an unoccupied pathid */
44559e6461a2SMatt Jacob 	while (bus != NULL && bus->path_id <= pathid) {
4456434bbf6eSJustin T. Gibbs 		if (bus->path_id == pathid)
4457434bbf6eSJustin T. Gibbs 			pathid++;
4458434bbf6eSJustin T. Gibbs 		bus = TAILQ_NEXT(bus, links);
4459434bbf6eSJustin T. Gibbs 	}
44602b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_topo_lock);
4461434bbf6eSJustin T. Gibbs 
4462434bbf6eSJustin T. Gibbs 	/*
4463434bbf6eSJustin T. Gibbs 	 * Ensure that this pathid is not reserved for
4464434bbf6eSJustin T. Gibbs 	 * a bus that may be registered in the future.
4465434bbf6eSJustin T. Gibbs 	 */
446675f51904SPeter Wemm 	if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4467434bbf6eSJustin T. Gibbs 		++pathid;
44688b8a9b1dSJustin T. Gibbs 		/* Start the search over */
44692b83592fSScott Long 		mtx_lock(&xsoftc.xpt_topo_lock);
4470434bbf6eSJustin T. Gibbs 		goto retry;
44718b8a9b1dSJustin T. Gibbs 	}
4472434bbf6eSJustin T. Gibbs 	return (pathid);
44738b8a9b1dSJustin T. Gibbs }
44748b8a9b1dSJustin T. Gibbs 
4475434bbf6eSJustin T. Gibbs static path_id_t
4476434bbf6eSJustin T. Gibbs xptpathid(const char *sim_name, int sim_unit, int sim_bus)
44778b8a9b1dSJustin T. Gibbs {
44788b8a9b1dSJustin T. Gibbs 	path_id_t pathid;
447975f51904SPeter Wemm 	int i, dunit, val;
4480642f0c46SPeter Wemm 	char buf[32];
44812398f0cdSPeter Wemm 	const char *dname;
44828b8a9b1dSJustin T. Gibbs 
44838b8a9b1dSJustin T. Gibbs 	pathid = CAM_XPT_PATH_ID;
448475f51904SPeter Wemm 	snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
44852398f0cdSPeter Wemm 	i = 0;
44862398f0cdSPeter Wemm 	while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
44872398f0cdSPeter Wemm 		if (strcmp(dname, "scbus")) {
4488642f0c46SPeter Wemm 			/* Avoid a bit of foot shooting. */
4489642f0c46SPeter Wemm 			continue;
4490642f0c46SPeter Wemm 		}
449175f51904SPeter Wemm 		if (dunit < 0)		/* unwired?! */
44928b8a9b1dSJustin T. Gibbs 			continue;
449375f51904SPeter Wemm 		if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
449475f51904SPeter Wemm 			if (sim_bus == val) {
449575f51904SPeter Wemm 				pathid = dunit;
44968b8a9b1dSJustin T. Gibbs 				break;
44978b8a9b1dSJustin T. Gibbs 			}
44988b8a9b1dSJustin T. Gibbs 		} else if (sim_bus == 0) {
44998b8a9b1dSJustin T. Gibbs 			/* Unspecified matches bus 0 */
450075f51904SPeter Wemm 			pathid = dunit;
45018b8a9b1dSJustin T. Gibbs 			break;
45028b8a9b1dSJustin T. Gibbs 		} else {
45038b8a9b1dSJustin T. Gibbs 			printf("Ambiguous scbus configuration for %s%d "
45048b8a9b1dSJustin T. Gibbs 			       "bus %d, cannot wire down.  The kernel "
45058b8a9b1dSJustin T. Gibbs 			       "config entry for scbus%d should "
45068b8a9b1dSJustin T. Gibbs 			       "specify a controller bus.\n"
45078b8a9b1dSJustin T. Gibbs 			       "Scbus will be assigned dynamically.\n",
450875f51904SPeter Wemm 			       sim_name, sim_unit, sim_bus, dunit);
45098b8a9b1dSJustin T. Gibbs 			break;
45108b8a9b1dSJustin T. Gibbs 		}
45118b8a9b1dSJustin T. Gibbs 	}
45128b8a9b1dSJustin T. Gibbs 
4513434bbf6eSJustin T. Gibbs 	if (pathid == CAM_XPT_PATH_ID)
4514434bbf6eSJustin T. Gibbs 		pathid = xptnextfreepathid();
45158b8a9b1dSJustin T. Gibbs 	return (pathid);
45168b8a9b1dSJustin T. Gibbs }
45178b8a9b1dSJustin T. Gibbs 
45188b8a9b1dSJustin T. Gibbs void
45198b8a9b1dSJustin T. Gibbs xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
45208b8a9b1dSJustin T. Gibbs {
45218b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus;
45228b8a9b1dSJustin T. Gibbs 	struct cam_et *target, *next_target;
45238b8a9b1dSJustin T. Gibbs 	struct cam_ed *device, *next_device;
45248b8a9b1dSJustin T. Gibbs 
45252b83592fSScott Long 	mtx_assert(path->bus->sim->mtx, MA_OWNED);
452668153f43SScott Long 
45278b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
45288b8a9b1dSJustin T. Gibbs 
4529a5479bc5SJustin T. Gibbs 	/*
4530a5479bc5SJustin T. Gibbs 	 * Most async events come from a CAM interrupt context.  In
4531a5479bc5SJustin T. Gibbs 	 * a few cases, the error recovery code at the peripheral layer,
4532a5479bc5SJustin T. Gibbs 	 * which may run from our SWI or a process context, may signal
453377dc25ccSScott Long 	 * deferred events with a call to xpt_async.
4534a5479bc5SJustin T. Gibbs 	 */
45358b8a9b1dSJustin T. Gibbs 
45368b8a9b1dSJustin T. Gibbs 	bus = path->bus;
45378b8a9b1dSJustin T. Gibbs 
45388b8a9b1dSJustin T. Gibbs 	if (async_code == AC_BUS_RESET) {
453987cfaf0eSJustin T. Gibbs 		/* Update our notion of when the last reset occurred */
454087cfaf0eSJustin T. Gibbs 		microtime(&bus->last_reset);
45418b8a9b1dSJustin T. Gibbs 	}
45428b8a9b1dSJustin T. Gibbs 
45438b8a9b1dSJustin T. Gibbs 	for (target = TAILQ_FIRST(&bus->et_entries);
45448b8a9b1dSJustin T. Gibbs 	     target != NULL;
45458b8a9b1dSJustin T. Gibbs 	     target = next_target) {
45468b8a9b1dSJustin T. Gibbs 
45478b8a9b1dSJustin T. Gibbs 		next_target = TAILQ_NEXT(target, links);
45488b8a9b1dSJustin T. Gibbs 
45498b8a9b1dSJustin T. Gibbs 		if (path->target != target
45502f22d08dSJustin T. Gibbs 		 && path->target->target_id != CAM_TARGET_WILDCARD
45512f22d08dSJustin T. Gibbs 		 && target->target_id != CAM_TARGET_WILDCARD)
45528b8a9b1dSJustin T. Gibbs 			continue;
45538b8a9b1dSJustin T. Gibbs 
455487cfaf0eSJustin T. Gibbs 		if (async_code == AC_SENT_BDR) {
455587cfaf0eSJustin T. Gibbs 			/* Update our notion of when the last reset occurred */
455687cfaf0eSJustin T. Gibbs 			microtime(&path->target->last_reset);
455787cfaf0eSJustin T. Gibbs 		}
455887cfaf0eSJustin T. Gibbs 
45598b8a9b1dSJustin T. Gibbs 		for (device = TAILQ_FIRST(&target->ed_entries);
45608b8a9b1dSJustin T. Gibbs 		     device != NULL;
45618b8a9b1dSJustin T. Gibbs 		     device = next_device) {
45628b8a9b1dSJustin T. Gibbs 
45638b8a9b1dSJustin T. Gibbs 			next_device = TAILQ_NEXT(device, links);
45648b8a9b1dSJustin T. Gibbs 
45658b8a9b1dSJustin T. Gibbs 			if (path->device != device
45662f22d08dSJustin T. Gibbs 			 && path->device->lun_id != CAM_LUN_WILDCARD
45672f22d08dSJustin T. Gibbs 			 && device->lun_id != CAM_LUN_WILDCARD)
45688b8a9b1dSJustin T. Gibbs 				continue;
45698b8a9b1dSJustin T. Gibbs 
45702f22d08dSJustin T. Gibbs 			xpt_dev_async(async_code, bus, target,
45712f22d08dSJustin T. Gibbs 				      device, async_arg);
45728b8a9b1dSJustin T. Gibbs 
45732f22d08dSJustin T. Gibbs 			xpt_async_bcast(&device->asyncs, async_code,
45742f22d08dSJustin T. Gibbs 					path, async_arg);
45758b8a9b1dSJustin T. Gibbs 		}
45768b8a9b1dSJustin T. Gibbs 	}
4577c8bead2aSJustin T. Gibbs 
4578c8bead2aSJustin T. Gibbs 	/*
4579c8bead2aSJustin T. Gibbs 	 * If this wasn't a fully wildcarded async, tell all
4580c8bead2aSJustin T. Gibbs 	 * clients that want all async events.
4581c8bead2aSJustin T. Gibbs 	 */
4582c8bead2aSJustin T. Gibbs 	if (bus != xpt_periph->path->bus)
4583c8bead2aSJustin T. Gibbs 		xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
45848b8a9b1dSJustin T. Gibbs 				path, async_arg);
45858b8a9b1dSJustin T. Gibbs }
45868b8a9b1dSJustin T. Gibbs 
45878b8a9b1dSJustin T. Gibbs static void
45888b8a9b1dSJustin T. Gibbs xpt_async_bcast(struct async_list *async_head,
45898b8a9b1dSJustin T. Gibbs 		u_int32_t async_code,
45908b8a9b1dSJustin T. Gibbs 		struct cam_path *path, void *async_arg)
45918b8a9b1dSJustin T. Gibbs {
45928b8a9b1dSJustin T. Gibbs 	struct async_node *cur_entry;
45938b8a9b1dSJustin T. Gibbs 
45948b8a9b1dSJustin T. Gibbs 	cur_entry = SLIST_FIRST(async_head);
45958b8a9b1dSJustin T. Gibbs 	while (cur_entry != NULL) {
45968b8a9b1dSJustin T. Gibbs 		struct async_node *next_entry;
45978b8a9b1dSJustin T. Gibbs 		/*
45988b8a9b1dSJustin T. Gibbs 		 * Grab the next list entry before we call the current
45998b8a9b1dSJustin T. Gibbs 		 * entry's callback.  This is because the callback function
46008b8a9b1dSJustin T. Gibbs 		 * can delete its async callback entry.
46018b8a9b1dSJustin T. Gibbs 		 */
46028b8a9b1dSJustin T. Gibbs 		next_entry = SLIST_NEXT(cur_entry, links);
46038b8a9b1dSJustin T. Gibbs 		if ((cur_entry->event_enable & async_code) != 0)
46048b8a9b1dSJustin T. Gibbs 			cur_entry->callback(cur_entry->callback_arg,
46058b8a9b1dSJustin T. Gibbs 					    async_code, path,
46068b8a9b1dSJustin T. Gibbs 					    async_arg);
46078b8a9b1dSJustin T. Gibbs 		cur_entry = next_entry;
46088b8a9b1dSJustin T. Gibbs 	}
46098b8a9b1dSJustin T. Gibbs }
46108b8a9b1dSJustin T. Gibbs 
46112f22d08dSJustin T. Gibbs /*
46122f22d08dSJustin T. Gibbs  * Handle any per-device event notifications that require action by the XPT.
46132f22d08dSJustin T. Gibbs  */
46142f22d08dSJustin T. Gibbs static void
46152f22d08dSJustin T. Gibbs xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
46162f22d08dSJustin T. Gibbs 	      struct cam_ed *device, void *async_arg)
46172f22d08dSJustin T. Gibbs {
46182f22d08dSJustin T. Gibbs 	cam_status status;
46192f22d08dSJustin T. Gibbs 	struct cam_path newpath;
46202f22d08dSJustin T. Gibbs 
46212f22d08dSJustin T. Gibbs 	/*
46222f22d08dSJustin T. Gibbs 	 * We only need to handle events for real devices.
46232f22d08dSJustin T. Gibbs 	 */
46242f22d08dSJustin T. Gibbs 	if (target->target_id == CAM_TARGET_WILDCARD
46252f22d08dSJustin T. Gibbs 	 || device->lun_id == CAM_LUN_WILDCARD)
46262f22d08dSJustin T. Gibbs 		return;
46272f22d08dSJustin T. Gibbs 
46282f22d08dSJustin T. Gibbs 	/*
46292f22d08dSJustin T. Gibbs 	 * We need our own path with wildcards expanded to
46302f22d08dSJustin T. Gibbs 	 * handle certain types of events.
46312f22d08dSJustin T. Gibbs 	 */
46322f22d08dSJustin T. Gibbs 	if ((async_code == AC_SENT_BDR)
46332f22d08dSJustin T. Gibbs 	 || (async_code == AC_BUS_RESET)
46342f22d08dSJustin T. Gibbs 	 || (async_code == AC_INQ_CHANGED))
46352f22d08dSJustin T. Gibbs 		status = xpt_compile_path(&newpath, NULL,
46362f22d08dSJustin T. Gibbs 					  bus->path_id,
46372f22d08dSJustin T. Gibbs 					  target->target_id,
46382f22d08dSJustin T. Gibbs 					  device->lun_id);
46392f22d08dSJustin T. Gibbs 	else
46402f22d08dSJustin T. Gibbs 		status = CAM_REQ_CMP_ERR;
46412f22d08dSJustin T. Gibbs 
46422f22d08dSJustin T. Gibbs 	if (status == CAM_REQ_CMP) {
46432f22d08dSJustin T. Gibbs 
46442f22d08dSJustin T. Gibbs 		/*
46452f22d08dSJustin T. Gibbs 		 * Allow transfer negotiation to occur in a
46462f22d08dSJustin T. Gibbs 		 * tag free environment.
46472f22d08dSJustin T. Gibbs 		 */
46482f22d08dSJustin T. Gibbs 		if (async_code == AC_SENT_BDR
46492f22d08dSJustin T. Gibbs 		 || async_code == AC_BUS_RESET)
46502f22d08dSJustin T. Gibbs 			xpt_toggle_tags(&newpath);
46512f22d08dSJustin T. Gibbs 
46522f22d08dSJustin T. Gibbs 		if (async_code == AC_INQ_CHANGED) {
46532f22d08dSJustin T. Gibbs 			/*
46542f22d08dSJustin T. Gibbs 			 * We've sent a start unit command, or
46552f22d08dSJustin T. Gibbs 			 * something similar to a device that
46562f22d08dSJustin T. Gibbs 			 * may have caused its inquiry data to
46572f22d08dSJustin T. Gibbs 			 * change. So we re-scan the device to
46582f22d08dSJustin T. Gibbs 			 * refresh the inquiry data for it.
46592f22d08dSJustin T. Gibbs 			 */
46602f22d08dSJustin T. Gibbs 			xpt_scan_lun(newpath.periph, &newpath,
46612f22d08dSJustin T. Gibbs 				     CAM_EXPECT_INQ_CHANGE, NULL);
46622f22d08dSJustin T. Gibbs 		}
46632f22d08dSJustin T. Gibbs 		xpt_release_path(&newpath);
46642f22d08dSJustin T. Gibbs 	} else if (async_code == AC_LOST_DEVICE) {
46652f22d08dSJustin T. Gibbs 		device->flags |= CAM_DEV_UNCONFIGURED;
46662f22d08dSJustin T. Gibbs 	} else if (async_code == AC_TRANSFER_NEG) {
46672f22d08dSJustin T. Gibbs 		struct ccb_trans_settings *settings;
46682f22d08dSJustin T. Gibbs 
46692f22d08dSJustin T. Gibbs 		settings = (struct ccb_trans_settings *)async_arg;
46702f22d08dSJustin T. Gibbs 		xpt_set_transfer_settings(settings, device,
46712f22d08dSJustin T. Gibbs 					  /*async_update*/TRUE);
46722f22d08dSJustin T. Gibbs 	}
46732f22d08dSJustin T. Gibbs }
46742f22d08dSJustin T. Gibbs 
46758b8a9b1dSJustin T. Gibbs u_int32_t
46768b8a9b1dSJustin T. Gibbs xpt_freeze_devq(struct cam_path *path, u_int count)
46778b8a9b1dSJustin T. Gibbs {
46788b8a9b1dSJustin T. Gibbs 	struct ccb_hdr *ccbh;
46798b8a9b1dSJustin T. Gibbs 
46802b83592fSScott Long 	mtx_assert(path->bus->sim->mtx, MA_OWNED);
468168153f43SScott Long 
46828b8a9b1dSJustin T. Gibbs 	path->device->qfrozen_cnt += count;
46838b8a9b1dSJustin T. Gibbs 
46848b8a9b1dSJustin T. Gibbs 	/*
46858b8a9b1dSJustin T. Gibbs 	 * Mark the last CCB in the queue as needing
46868b8a9b1dSJustin T. Gibbs 	 * to be requeued if the driver hasn't
46878b8a9b1dSJustin T. Gibbs 	 * changed it's state yet.  This fixes a race
46888b8a9b1dSJustin T. Gibbs 	 * where a ccb is just about to be queued to
46898b8a9b1dSJustin T. Gibbs 	 * a controller driver when it's interrupt routine
46908b8a9b1dSJustin T. Gibbs 	 * freezes the queue.  To completly close the
46918b8a9b1dSJustin T. Gibbs 	 * hole, controller drives must check to see
46928b8a9b1dSJustin T. Gibbs 	 * if a ccb's status is still CAM_REQ_INPROG
469377dc25ccSScott Long 	 * just before they queue
46948b8a9b1dSJustin T. Gibbs 	 * the CCB.  See ahc_action/ahc_freeze_devq for
46958b8a9b1dSJustin T. Gibbs 	 * an example.
46968b8a9b1dSJustin T. Gibbs 	 */
4697bb6087e5SJustin T. Gibbs 	ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
46988b8a9b1dSJustin T. Gibbs 	if (ccbh && ccbh->status == CAM_REQ_INPROG)
46998b8a9b1dSJustin T. Gibbs 		ccbh->status = CAM_REQUEUE_REQ;
47008b8a9b1dSJustin T. Gibbs 	return (path->device->qfrozen_cnt);
47018b8a9b1dSJustin T. Gibbs }
47028b8a9b1dSJustin T. Gibbs 
47038b8a9b1dSJustin T. Gibbs u_int32_t
47048b8a9b1dSJustin T. Gibbs xpt_freeze_simq(struct cam_sim *sim, u_int count)
47058b8a9b1dSJustin T. Gibbs {
47062b83592fSScott Long 	mtx_assert(sim->mtx, MA_OWNED);
470768153f43SScott Long 
47088b8a9b1dSJustin T. Gibbs 	sim->devq->send_queue.qfrozen_cnt += count;
47098b8a9b1dSJustin T. Gibbs 	if (sim->devq->active_dev != NULL) {
47108b8a9b1dSJustin T. Gibbs 		struct ccb_hdr *ccbh;
47118b8a9b1dSJustin T. Gibbs 
47128b8a9b1dSJustin T. Gibbs 		ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4713bb6087e5SJustin T. Gibbs 				  ccb_hdr_tailq);
47148b8a9b1dSJustin T. Gibbs 		if (ccbh && ccbh->status == CAM_REQ_INPROG)
47158b8a9b1dSJustin T. Gibbs 			ccbh->status = CAM_REQUEUE_REQ;
47168b8a9b1dSJustin T. Gibbs 	}
47178b8a9b1dSJustin T. Gibbs 	return (sim->devq->send_queue.qfrozen_cnt);
47188b8a9b1dSJustin T. Gibbs }
47198b8a9b1dSJustin T. Gibbs 
47208b8a9b1dSJustin T. Gibbs static void
47218b8a9b1dSJustin T. Gibbs xpt_release_devq_timeout(void *arg)
47228b8a9b1dSJustin T. Gibbs {
47238b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
47248b8a9b1dSJustin T. Gibbs 
47258b8a9b1dSJustin T. Gibbs 	device = (struct cam_ed *)arg;
47268b8a9b1dSJustin T. Gibbs 
47272cefde5fSJustin T. Gibbs 	xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
47288b8a9b1dSJustin T. Gibbs }
47298b8a9b1dSJustin T. Gibbs 
47308b8a9b1dSJustin T. Gibbs void
47312cefde5fSJustin T. Gibbs xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
47322cefde5fSJustin T. Gibbs {
47332b83592fSScott Long 	mtx_assert(path->bus->sim->mtx, MA_OWNED);
473468153f43SScott Long 
47352cefde5fSJustin T. Gibbs 	xpt_release_devq_device(path->device, count, run_queue);
47362cefde5fSJustin T. Gibbs }
47372cefde5fSJustin T. Gibbs 
47382cefde5fSJustin T. Gibbs static void
47392cefde5fSJustin T. Gibbs xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
47408b8a9b1dSJustin T. Gibbs {
47418b8a9b1dSJustin T. Gibbs 	int	rundevq;
47428b8a9b1dSJustin T. Gibbs 
47438b8a9b1dSJustin T. Gibbs 	rundevq = 0;
47448b8a9b1dSJustin T. Gibbs 	if (dev->qfrozen_cnt > 0) {
47458b8a9b1dSJustin T. Gibbs 
47462cefde5fSJustin T. Gibbs 		count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
47472cefde5fSJustin T. Gibbs 		dev->qfrozen_cnt -= count;
47488b8a9b1dSJustin T. Gibbs 		if (dev->qfrozen_cnt == 0) {
47498b8a9b1dSJustin T. Gibbs 
47508b8a9b1dSJustin T. Gibbs 			/*
47518b8a9b1dSJustin T. Gibbs 			 * No longer need to wait for a successful
47528b8a9b1dSJustin T. Gibbs 			 * command completion.
47538b8a9b1dSJustin T. Gibbs 			 */
47548b8a9b1dSJustin T. Gibbs 			dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
47558b8a9b1dSJustin T. Gibbs 
47568b8a9b1dSJustin T. Gibbs 			/*
47578b8a9b1dSJustin T. Gibbs 			 * Remove any timeouts that might be scheduled
47588b8a9b1dSJustin T. Gibbs 			 * to release this queue.
47598b8a9b1dSJustin T. Gibbs 			 */
47608b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
47612b83592fSScott Long 				callout_stop(&dev->callout);
47628b8a9b1dSJustin T. Gibbs 				dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
47638b8a9b1dSJustin T. Gibbs 			}
47648b8a9b1dSJustin T. Gibbs 
47658b8a9b1dSJustin T. Gibbs 			/*
47668b8a9b1dSJustin T. Gibbs 			 * Now that we are unfrozen schedule the
47678b8a9b1dSJustin T. Gibbs 			 * device so any pending transactions are
47688b8a9b1dSJustin T. Gibbs 			 * run.
47698b8a9b1dSJustin T. Gibbs 			 */
47708b8a9b1dSJustin T. Gibbs 			if ((dev->ccbq.queue.entries > 0)
47718b8a9b1dSJustin T. Gibbs 			 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
47728b8a9b1dSJustin T. Gibbs 			 && (run_queue != 0)) {
47738b8a9b1dSJustin T. Gibbs 				rundevq = 1;
47748b8a9b1dSJustin T. Gibbs 			}
47758b8a9b1dSJustin T. Gibbs 		}
47768b8a9b1dSJustin T. Gibbs 	}
47778b8a9b1dSJustin T. Gibbs 	if (rundevq != 0)
47788b8a9b1dSJustin T. Gibbs 		xpt_run_dev_sendq(dev->target->bus);
47798b8a9b1dSJustin T. Gibbs }
47808b8a9b1dSJustin T. Gibbs 
47818b8a9b1dSJustin T. Gibbs void
47828b8a9b1dSJustin T. Gibbs xpt_release_simq(struct cam_sim *sim, int run_queue)
47838b8a9b1dSJustin T. Gibbs {
47848b8a9b1dSJustin T. Gibbs 	struct	camq *sendq;
47858b8a9b1dSJustin T. Gibbs 
47862b83592fSScott Long 	mtx_assert(sim->mtx, MA_OWNED);
478768153f43SScott Long 
47888b8a9b1dSJustin T. Gibbs 	sendq = &(sim->devq->send_queue);
47898b8a9b1dSJustin T. Gibbs 	if (sendq->qfrozen_cnt > 0) {
47908b8a9b1dSJustin T. Gibbs 
47918b8a9b1dSJustin T. Gibbs 		sendq->qfrozen_cnt--;
47928b8a9b1dSJustin T. Gibbs 		if (sendq->qfrozen_cnt == 0) {
4793a5479bc5SJustin T. Gibbs 			struct cam_eb *bus;
47948b8a9b1dSJustin T. Gibbs 
47958b8a9b1dSJustin T. Gibbs 			/*
47968b8a9b1dSJustin T. Gibbs 			 * If there is a timeout scheduled to release this
47978b8a9b1dSJustin T. Gibbs 			 * sim queue, remove it.  The queue frozen count is
47988b8a9b1dSJustin T. Gibbs 			 * already at 0.
47998b8a9b1dSJustin T. Gibbs 			 */
48008b8a9b1dSJustin T. Gibbs 			if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
48012b83592fSScott Long 				callout_stop(&sim->callout);
48028b8a9b1dSJustin T. Gibbs 				sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
48038b8a9b1dSJustin T. Gibbs 			}
4804a5479bc5SJustin T. Gibbs 			bus = xpt_find_bus(sim->path_id);
48058b8a9b1dSJustin T. Gibbs 
48068b8a9b1dSJustin T. Gibbs 			if (run_queue) {
48078b8a9b1dSJustin T. Gibbs 				/*
48088b8a9b1dSJustin T. Gibbs 				 * Now that we are unfrozen run the send queue.
48098b8a9b1dSJustin T. Gibbs 				 */
4810a5479bc5SJustin T. Gibbs 				xpt_run_dev_sendq(bus);
48118b8a9b1dSJustin T. Gibbs 			}
4812a5479bc5SJustin T. Gibbs 			xpt_release_bus(bus);
481377dc25ccSScott Long 		}
481477dc25ccSScott Long 	}
48158b8a9b1dSJustin T. Gibbs }
48168b8a9b1dSJustin T. Gibbs 
48172b83592fSScott Long /*
48182b83592fSScott Long  * XXX Appears to be unused.
48192b83592fSScott Long  */
48208b8a9b1dSJustin T. Gibbs static void
48218b8a9b1dSJustin T. Gibbs xpt_release_simq_timeout(void *arg)
48228b8a9b1dSJustin T. Gibbs {
48238b8a9b1dSJustin T. Gibbs 	struct cam_sim *sim;
48248b8a9b1dSJustin T. Gibbs 
48258b8a9b1dSJustin T. Gibbs 	sim = (struct cam_sim *)arg;
48268b8a9b1dSJustin T. Gibbs 	xpt_release_simq(sim, /* run_queue */ TRUE);
48278b8a9b1dSJustin T. Gibbs }
48288b8a9b1dSJustin T. Gibbs 
48298b8a9b1dSJustin T. Gibbs void
4830a5479bc5SJustin T. Gibbs xpt_done(union ccb *done_ccb)
48318b8a9b1dSJustin T. Gibbs {
48329758cc83SScott Long 	struct cam_sim *sim;
48338b8a9b1dSJustin T. Gibbs 
48348b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
48359deea857SKenneth D. Merry 	if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
48368b8a9b1dSJustin T. Gibbs 		/*
48378b8a9b1dSJustin T. Gibbs 		 * Queue up the request for handling by our SWI handler
48388b8a9b1dSJustin T. Gibbs 		 * any of the "non-immediate" type of ccbs.
48398b8a9b1dSJustin T. Gibbs 		 */
48409758cc83SScott Long 		sim = done_ccb->ccb_h.path->bus->sim;
48418b8a9b1dSJustin T. Gibbs 		switch (done_ccb->ccb_h.path->periph->type) {
48428b8a9b1dSJustin T. Gibbs 		case CAM_PERIPH_BIO:
48439758cc83SScott Long 			TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h,
48448b8a9b1dSJustin T. Gibbs 					  sim_links.tqe);
48458b8a9b1dSJustin T. Gibbs 			done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
48469758cc83SScott Long 			if ((sim->flags & CAM_SIM_ON_DONEQ) == 0) {
48479758cc83SScott Long 				mtx_lock(&cam_simq_lock);
48489758cc83SScott Long 				TAILQ_INSERT_TAIL(&cam_simq, sim,
48499758cc83SScott Long 						  links);
48509758cc83SScott Long 				sim->flags |= CAM_SIM_ON_DONEQ;
48519758cc83SScott Long 				mtx_unlock(&cam_simq_lock);
48529758cc83SScott Long 			}
48532b83592fSScott Long 			if ((done_ccb->ccb_h.path->periph->flags &
48542b83592fSScott Long 			    CAM_PERIPH_POLLED) == 0)
4855c86b6ff5SJohn Baldwin 				swi_sched(cambio_ih, 0);
48568b8a9b1dSJustin T. Gibbs 			break;
48572e8f0ae6SScott Long 		default:
48582e8f0ae6SScott Long 			panic("unknown periph type %d",
48592e8f0ae6SScott Long 			    done_ccb->ccb_h.path->periph->type);
48608b8a9b1dSJustin T. Gibbs 		}
48618b8a9b1dSJustin T. Gibbs 	}
48628b8a9b1dSJustin T. Gibbs }
48638b8a9b1dSJustin T. Gibbs 
48648b8a9b1dSJustin T. Gibbs union ccb *
48658008a935SScott Long xpt_alloc_ccb()
48668b8a9b1dSJustin T. Gibbs {
48678b8a9b1dSJustin T. Gibbs 	union ccb *new_ccb;
48688b8a9b1dSJustin T. Gibbs 
48692a30c7ddSScott Long 	new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_WAITOK);
4870362abc44STai-hwa Liang 	return (new_ccb);
4871362abc44STai-hwa Liang }
4872362abc44STai-hwa Liang 
4873362abc44STai-hwa Liang union ccb *
48748008a935SScott Long xpt_alloc_ccb_nowait()
4875362abc44STai-hwa Liang {
4876362abc44STai-hwa Liang 	union ccb *new_ccb;
4877362abc44STai-hwa Liang 
48782a30c7ddSScott Long 	new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_NOWAIT);
48798b8a9b1dSJustin T. Gibbs 	return (new_ccb);
48808b8a9b1dSJustin T. Gibbs }
48818b8a9b1dSJustin T. Gibbs 
48828b8a9b1dSJustin T. Gibbs void
48838b8a9b1dSJustin T. Gibbs xpt_free_ccb(union ccb *free_ccb)
48848b8a9b1dSJustin T. Gibbs {
4885362abc44STai-hwa Liang 	free(free_ccb, M_CAMXPT);
48868b8a9b1dSJustin T. Gibbs }
48878b8a9b1dSJustin T. Gibbs 
48888b8a9b1dSJustin T. Gibbs 
48898b8a9b1dSJustin T. Gibbs 
48908b8a9b1dSJustin T. Gibbs /* Private XPT functions */
48918b8a9b1dSJustin T. Gibbs 
48928b8a9b1dSJustin T. Gibbs /*
48938b8a9b1dSJustin T. Gibbs  * Get a CAM control block for the caller. Charge the structure to the device
48948b8a9b1dSJustin T. Gibbs  * referenced by the path.  If the this device has no 'credits' then the
48958b8a9b1dSJustin T. Gibbs  * device already has the maximum number of outstanding operations under way
48968b8a9b1dSJustin T. Gibbs  * and we return NULL. If we don't have sufficient resources to allocate more
48978b8a9b1dSJustin T. Gibbs  * ccbs, we also return NULL.
48988b8a9b1dSJustin T. Gibbs  */
48998b8a9b1dSJustin T. Gibbs static union ccb *
49008b8a9b1dSJustin T. Gibbs xpt_get_ccb(struct cam_ed *device)
49018b8a9b1dSJustin T. Gibbs {
49028b8a9b1dSJustin T. Gibbs 	union ccb *new_ccb;
49032b83592fSScott Long 	struct cam_sim *sim;
49048b8a9b1dSJustin T. Gibbs 
49052b83592fSScott Long 	sim = device->sim;
49062b83592fSScott Long 	if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) {
49078008a935SScott Long 		new_ccb = xpt_alloc_ccb_nowait();
49088b8a9b1dSJustin T. Gibbs                 if (new_ccb == NULL) {
49098b8a9b1dSJustin T. Gibbs 			return (NULL);
49108b8a9b1dSJustin T. Gibbs 		}
49118008a935SScott Long 		if ((sim->flags & CAM_SIM_MPSAFE) == 0)
49128008a935SScott Long 			callout_handle_init(&new_ccb->ccb_h.timeout_ch);
49132b83592fSScott Long 		SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h,
49148b8a9b1dSJustin T. Gibbs 				  xpt_links.sle);
49152b83592fSScott Long 		sim->ccb_count++;
49168b8a9b1dSJustin T. Gibbs 	}
49178b8a9b1dSJustin T. Gibbs 	cam_ccbq_take_opening(&device->ccbq);
49182b83592fSScott Long 	SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
49198b8a9b1dSJustin T. Gibbs 	return (new_ccb);
49208b8a9b1dSJustin T. Gibbs }
49218b8a9b1dSJustin T. Gibbs 
4922a5479bc5SJustin T. Gibbs static void
4923a5479bc5SJustin T. Gibbs xpt_release_bus(struct cam_eb *bus)
4924a5479bc5SJustin T. Gibbs {
4925a5479bc5SJustin T. Gibbs 
4926a5479bc5SJustin T. Gibbs 	if ((--bus->refcount == 0)
4927a5479bc5SJustin T. Gibbs 	 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
49282b83592fSScott Long 		mtx_lock(&xsoftc.xpt_topo_lock);
49292b83592fSScott Long 		TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
49302b83592fSScott Long 		xsoftc.bus_generation++;
49312b83592fSScott Long 		mtx_unlock(&xsoftc.xpt_topo_lock);
4932362abc44STai-hwa Liang 		free(bus, M_CAMXPT);
493377dc25ccSScott Long 	}
4934a5479bc5SJustin T. Gibbs }
49358b8a9b1dSJustin T. Gibbs 
49368b8a9b1dSJustin T. Gibbs static struct cam_et *
49378b8a9b1dSJustin T. Gibbs xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
49388b8a9b1dSJustin T. Gibbs {
49398b8a9b1dSJustin T. Gibbs 	struct cam_et *target;
49408b8a9b1dSJustin T. Gibbs 
4941362abc44STai-hwa Liang 	target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, M_NOWAIT);
49428b8a9b1dSJustin T. Gibbs 	if (target != NULL) {
49438b8a9b1dSJustin T. Gibbs 		struct cam_et *cur_target;
49448b8a9b1dSJustin T. Gibbs 
4945434bbf6eSJustin T. Gibbs 		TAILQ_INIT(&target->ed_entries);
49468b8a9b1dSJustin T. Gibbs 		target->bus = bus;
49478b8a9b1dSJustin T. Gibbs 		target->target_id = target_id;
49488b8a9b1dSJustin T. Gibbs 		target->refcount = 1;
4949434bbf6eSJustin T. Gibbs 		target->generation = 0;
4950434bbf6eSJustin T. Gibbs 		timevalclear(&target->last_reset);
4951a5479bc5SJustin T. Gibbs 		/*
4952a5479bc5SJustin T. Gibbs 		 * Hold a reference to our parent bus so it
4953a5479bc5SJustin T. Gibbs 		 * will not go away before we do.
4954a5479bc5SJustin T. Gibbs 		 */
4955a5479bc5SJustin T. Gibbs 		bus->refcount++;
49568b8a9b1dSJustin T. Gibbs 
49578b8a9b1dSJustin T. Gibbs 		/* Insertion sort into our bus's target list */
49588b8a9b1dSJustin T. Gibbs 		cur_target = TAILQ_FIRST(&bus->et_entries);
49598b8a9b1dSJustin T. Gibbs 		while (cur_target != NULL && cur_target->target_id < target_id)
49608b8a9b1dSJustin T. Gibbs 			cur_target = TAILQ_NEXT(cur_target, links);
49618b8a9b1dSJustin T. Gibbs 
49628b8a9b1dSJustin T. Gibbs 		if (cur_target != NULL) {
49638b8a9b1dSJustin T. Gibbs 			TAILQ_INSERT_BEFORE(cur_target, target, links);
49648b8a9b1dSJustin T. Gibbs 		} else {
49658b8a9b1dSJustin T. Gibbs 			TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
49668b8a9b1dSJustin T. Gibbs 		}
4967a5479bc5SJustin T. Gibbs 		bus->generation++;
49688b8a9b1dSJustin T. Gibbs 	}
49698b8a9b1dSJustin T. Gibbs 	return (target);
49708b8a9b1dSJustin T. Gibbs }
49718b8a9b1dSJustin T. Gibbs 
4972a5479bc5SJustin T. Gibbs static void
49738b8a9b1dSJustin T. Gibbs xpt_release_target(struct cam_eb *bus, struct cam_et *target)
49748b8a9b1dSJustin T. Gibbs {
4975a5479bc5SJustin T. Gibbs 
49768b8a9b1dSJustin T. Gibbs 	if ((--target->refcount == 0)
49778b8a9b1dSJustin T. Gibbs 	 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
49788b8a9b1dSJustin T. Gibbs 		TAILQ_REMOVE(&bus->et_entries, target, links);
49798b8a9b1dSJustin T. Gibbs 		bus->generation++;
4980362abc44STai-hwa Liang 		free(target, M_CAMXPT);
4981a5479bc5SJustin T. Gibbs 		xpt_release_bus(bus);
498277dc25ccSScott Long 	}
49838b8a9b1dSJustin T. Gibbs }
49848b8a9b1dSJustin T. Gibbs 
49858b8a9b1dSJustin T. Gibbs static struct cam_ed *
49868b8a9b1dSJustin T. Gibbs xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
49878b8a9b1dSJustin T. Gibbs {
49883393f8daSKenneth D. Merry 	struct	   cam_path path;
49898b8a9b1dSJustin T. Gibbs 	struct	   cam_ed *device;
49908b8a9b1dSJustin T. Gibbs 	struct	   cam_devq *devq;
4991a5479bc5SJustin T. Gibbs 	cam_status status;
49928b8a9b1dSJustin T. Gibbs 
4993d3ef3454SIan Dowse 	if (SIM_DEAD(bus->sim))
4994d3ef3454SIan Dowse 		return (NULL);
4995d3ef3454SIan Dowse 
49968b8a9b1dSJustin T. Gibbs 	/* Make space for us in the device queue on our bus */
49978b8a9b1dSJustin T. Gibbs 	devq = bus->sim->devq;
49988b8a9b1dSJustin T. Gibbs 	status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
49998b8a9b1dSJustin T. Gibbs 
50008b8a9b1dSJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
50018b8a9b1dSJustin T. Gibbs 		device = NULL;
50028b8a9b1dSJustin T. Gibbs 	} else {
50038b8a9b1dSJustin T. Gibbs 		device = (struct cam_ed *)malloc(sizeof(*device),
5004362abc44STai-hwa Liang 						 M_CAMXPT, M_NOWAIT);
50058b8a9b1dSJustin T. Gibbs 	}
50068b8a9b1dSJustin T. Gibbs 
50078b8a9b1dSJustin T. Gibbs 	if (device != NULL) {
50088b8a9b1dSJustin T. Gibbs 		struct cam_ed *cur_device;
50098b8a9b1dSJustin T. Gibbs 
50108b8a9b1dSJustin T. Gibbs 		cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
50118b8a9b1dSJustin T. Gibbs 		device->alloc_ccb_entry.device = device;
50128b8a9b1dSJustin T. Gibbs 		cam_init_pinfo(&device->send_ccb_entry.pinfo);
50138b8a9b1dSJustin T. Gibbs 		device->send_ccb_entry.device = device;
50148b8a9b1dSJustin T. Gibbs 		device->target = target;
50158b8a9b1dSJustin T. Gibbs 		device->lun_id = lun_id;
50162b83592fSScott Long 		device->sim = bus->sim;
50178b8a9b1dSJustin T. Gibbs 		/* Initialize our queues */
50188b8a9b1dSJustin T. Gibbs 		if (camq_init(&device->drvq, 0) != 0) {
5019362abc44STai-hwa Liang 			free(device, M_CAMXPT);
50208b8a9b1dSJustin T. Gibbs 			return (NULL);
50218b8a9b1dSJustin T. Gibbs 		}
50228b8a9b1dSJustin T. Gibbs 		if (cam_ccbq_init(&device->ccbq,
50238b8a9b1dSJustin T. Gibbs 				  bus->sim->max_dev_openings) != 0) {
50248b8a9b1dSJustin T. Gibbs 			camq_fini(&device->drvq);
5025362abc44STai-hwa Liang 			free(device, M_CAMXPT);
50268b8a9b1dSJustin T. Gibbs 			return (NULL);
50278b8a9b1dSJustin T. Gibbs 		}
5028434bbf6eSJustin T. Gibbs 		SLIST_INIT(&device->asyncs);
5029434bbf6eSJustin T. Gibbs 		SLIST_INIT(&device->periphs);
5030434bbf6eSJustin T. Gibbs 		device->generation = 0;
5031434bbf6eSJustin T. Gibbs 		device->owner = NULL;
5032434bbf6eSJustin T. Gibbs 		/*
5033434bbf6eSJustin T. Gibbs 		 * Take the default quirk entry until we have inquiry
5034434bbf6eSJustin T. Gibbs 		 * data and can determine a better quirk to use.
5035434bbf6eSJustin T. Gibbs 		 */
5036434bbf6eSJustin T. Gibbs 		device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
5037434bbf6eSJustin T. Gibbs 		bzero(&device->inq_data, sizeof(device->inq_data));
5038434bbf6eSJustin T. Gibbs 		device->inq_flags = 0;
5039434bbf6eSJustin T. Gibbs 		device->queue_flags = 0;
5040434bbf6eSJustin T. Gibbs 		device->serial_num = NULL;
5041434bbf6eSJustin T. Gibbs 		device->serial_num_len = 0;
5042434bbf6eSJustin T. Gibbs 		device->qfrozen_cnt = 0;
5043434bbf6eSJustin T. Gibbs 		device->flags = CAM_DEV_UNCONFIGURED;
5044434bbf6eSJustin T. Gibbs 		device->tag_delay_count = 0;
5045df8f9080SJustin T. Gibbs 		device->tag_saved_openings = 0;
5046434bbf6eSJustin T. Gibbs 		device->refcount = 1;
50472b83592fSScott Long 		if (bus->sim->flags & CAM_SIM_MPSAFE)
50482b83592fSScott Long 			callout_init_mtx(&device->callout, bus->sim->mtx, 0);
50492b83592fSScott Long 		else
50502b83592fSScott Long 			callout_init_mtx(&device->callout, &Giant, 0);
5051434bbf6eSJustin T. Gibbs 
5052434bbf6eSJustin T. Gibbs 		/*
5053434bbf6eSJustin T. Gibbs 		 * Hold a reference to our parent target so it
5054434bbf6eSJustin T. Gibbs 		 * will not go away before we do.
5055434bbf6eSJustin T. Gibbs 		 */
5056434bbf6eSJustin T. Gibbs 		target->refcount++;
5057434bbf6eSJustin T. Gibbs 
50588b8a9b1dSJustin T. Gibbs 		/*
50598b8a9b1dSJustin T. Gibbs 		 * XXX should be limited by number of CCBs this bus can
50608b8a9b1dSJustin T. Gibbs 		 * do.
50618b8a9b1dSJustin T. Gibbs 		 */
50622b83592fSScott Long 		bus->sim->max_ccbs += device->ccbq.devq_openings;
50638b8a9b1dSJustin T. Gibbs 		/* Insertion sort into our target's device list */
50648b8a9b1dSJustin T. Gibbs 		cur_device = TAILQ_FIRST(&target->ed_entries);
50658b8a9b1dSJustin T. Gibbs 		while (cur_device != NULL && cur_device->lun_id < lun_id)
50668b8a9b1dSJustin T. Gibbs 			cur_device = TAILQ_NEXT(cur_device, links);
50678b8a9b1dSJustin T. Gibbs 		if (cur_device != NULL) {
50688b8a9b1dSJustin T. Gibbs 			TAILQ_INSERT_BEFORE(cur_device, device, links);
50698b8a9b1dSJustin T. Gibbs 		} else {
50708b8a9b1dSJustin T. Gibbs 			TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
50718b8a9b1dSJustin T. Gibbs 		}
5072a5479bc5SJustin T. Gibbs 		target->generation++;
50733393f8daSKenneth D. Merry 		if (lun_id != CAM_LUN_WILDCARD) {
50743393f8daSKenneth D. Merry 			xpt_compile_path(&path,
50753393f8daSKenneth D. Merry 					 NULL,
50763393f8daSKenneth D. Merry 					 bus->path_id,
50773393f8daSKenneth D. Merry 					 target->target_id,
50783393f8daSKenneth D. Merry 					 lun_id);
50793393f8daSKenneth D. Merry 			xpt_devise_transport(&path);
50803393f8daSKenneth D. Merry 			xpt_release_path(&path);
50813393f8daSKenneth D. Merry 		}
50828b8a9b1dSJustin T. Gibbs 	}
50838b8a9b1dSJustin T. Gibbs 	return (device);
50848b8a9b1dSJustin T. Gibbs }
50858b8a9b1dSJustin T. Gibbs 
50868b8a9b1dSJustin T. Gibbs static void
50878b8a9b1dSJustin T. Gibbs xpt_release_device(struct cam_eb *bus, struct cam_et *target,
50888b8a9b1dSJustin T. Gibbs 		   struct cam_ed *device)
50898b8a9b1dSJustin T. Gibbs {
50908b8a9b1dSJustin T. Gibbs 
50918b8a9b1dSJustin T. Gibbs 	if ((--device->refcount == 0)
50928b8a9b1dSJustin T. Gibbs 	 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
50938b8a9b1dSJustin T. Gibbs 		struct cam_devq *devq;
50948b8a9b1dSJustin T. Gibbs 
5095a5479bc5SJustin T. Gibbs 		if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
5096a5479bc5SJustin T. Gibbs 		 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
5097a5479bc5SJustin T. Gibbs 			panic("Removing device while still queued for ccbs");
50982cefde5fSJustin T. Gibbs 
50992cefde5fSJustin T. Gibbs 		if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
51002b83592fSScott Long 				callout_stop(&device->callout);
51012cefde5fSJustin T. Gibbs 
51028b8a9b1dSJustin T. Gibbs 		TAILQ_REMOVE(&target->ed_entries, device,links);
51038b8a9b1dSJustin T. Gibbs 		target->generation++;
51042b83592fSScott Long 		bus->sim->max_ccbs -= device->ccbq.devq_openings;
5105d3ef3454SIan Dowse 		if (!SIM_DEAD(bus->sim)) {
51068b8a9b1dSJustin T. Gibbs 			/* Release our slot in the devq */
51078b8a9b1dSJustin T. Gibbs 			devq = bus->sim->devq;
51088b8a9b1dSJustin T. Gibbs 			cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
5109d3ef3454SIan Dowse 		}
51107c20d5d7STai-hwa Liang 		camq_fini(&device->drvq);
51117c20d5d7STai-hwa Liang 		camq_fini(&device->ccbq.queue);
5112362abc44STai-hwa Liang 		free(device, M_CAMXPT);
5113434bbf6eSJustin T. Gibbs 		xpt_release_target(bus, target);
511477dc25ccSScott Long 	}
51158b8a9b1dSJustin T. Gibbs }
51168b8a9b1dSJustin T. Gibbs 
51178b8a9b1dSJustin T. Gibbs static u_int32_t
51188b8a9b1dSJustin T. Gibbs xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
51198b8a9b1dSJustin T. Gibbs {
51208b8a9b1dSJustin T. Gibbs 	int	diff;
51218b8a9b1dSJustin T. Gibbs 	int	result;
51228b8a9b1dSJustin T. Gibbs 	struct	cam_ed *dev;
51238b8a9b1dSJustin T. Gibbs 
51248b8a9b1dSJustin T. Gibbs 	dev = path->device;
51258b8a9b1dSJustin T. Gibbs 
51268b8a9b1dSJustin T. Gibbs 	diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
51278b8a9b1dSJustin T. Gibbs 	result = cam_ccbq_resize(&dev->ccbq, newopenings);
51288b8a9b1dSJustin T. Gibbs 	if (result == CAM_REQ_CMP && (diff < 0)) {
51298b8a9b1dSJustin T. Gibbs 		dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
51308b8a9b1dSJustin T. Gibbs 	}
5131df8f9080SJustin T. Gibbs 	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5132df8f9080SJustin T. Gibbs 	 || (dev->inq_flags & SID_CmdQue) != 0)
5133df8f9080SJustin T. Gibbs 		dev->tag_saved_openings = newopenings;
51348b8a9b1dSJustin T. Gibbs 	/* Adjust the global limit */
51352b83592fSScott Long 	dev->sim->max_ccbs += diff;
51368b8a9b1dSJustin T. Gibbs 	return (result);
51378b8a9b1dSJustin T. Gibbs }
51388b8a9b1dSJustin T. Gibbs 
51398b8a9b1dSJustin T. Gibbs static struct cam_eb *
51408b8a9b1dSJustin T. Gibbs xpt_find_bus(path_id_t path_id)
51418b8a9b1dSJustin T. Gibbs {
51428b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus;
51438b8a9b1dSJustin T. Gibbs 
51442b83592fSScott Long 	mtx_lock(&xsoftc.xpt_topo_lock);
51452b83592fSScott Long 	for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
51468b8a9b1dSJustin T. Gibbs 	     bus != NULL;
51478b8a9b1dSJustin T. Gibbs 	     bus = TAILQ_NEXT(bus, links)) {
5148a5479bc5SJustin T. Gibbs 		if (bus->path_id == path_id) {
5149a5479bc5SJustin T. Gibbs 			bus->refcount++;
51508b8a9b1dSJustin T. Gibbs 			break;
51518b8a9b1dSJustin T. Gibbs 		}
5152a5479bc5SJustin T. Gibbs 	}
51532b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_topo_lock);
51548b8a9b1dSJustin T. Gibbs 	return (bus);
51558b8a9b1dSJustin T. Gibbs }
51568b8a9b1dSJustin T. Gibbs 
51578b8a9b1dSJustin T. Gibbs static struct cam_et *
51588b8a9b1dSJustin T. Gibbs xpt_find_target(struct cam_eb *bus, target_id_t	target_id)
51598b8a9b1dSJustin T. Gibbs {
51608b8a9b1dSJustin T. Gibbs 	struct cam_et *target;
51618b8a9b1dSJustin T. Gibbs 
51628b8a9b1dSJustin T. Gibbs 	for (target = TAILQ_FIRST(&bus->et_entries);
51638b8a9b1dSJustin T. Gibbs 	     target != NULL;
51648b8a9b1dSJustin T. Gibbs 	     target = TAILQ_NEXT(target, links)) {
51658b8a9b1dSJustin T. Gibbs 		if (target->target_id == target_id) {
51668b8a9b1dSJustin T. Gibbs 			target->refcount++;
51678b8a9b1dSJustin T. Gibbs 			break;
51688b8a9b1dSJustin T. Gibbs 		}
51698b8a9b1dSJustin T. Gibbs 	}
51708b8a9b1dSJustin T. Gibbs 	return (target);
51718b8a9b1dSJustin T. Gibbs }
51728b8a9b1dSJustin T. Gibbs 
51738b8a9b1dSJustin T. Gibbs static struct cam_ed *
51748b8a9b1dSJustin T. Gibbs xpt_find_device(struct cam_et *target, lun_id_t lun_id)
51758b8a9b1dSJustin T. Gibbs {
51768b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
51778b8a9b1dSJustin T. Gibbs 
51788b8a9b1dSJustin T. Gibbs 	for (device = TAILQ_FIRST(&target->ed_entries);
51798b8a9b1dSJustin T. Gibbs 	     device != NULL;
51808b8a9b1dSJustin T. Gibbs 	     device = TAILQ_NEXT(device, links)) {
51818b8a9b1dSJustin T. Gibbs 		if (device->lun_id == lun_id) {
51828b8a9b1dSJustin T. Gibbs 			device->refcount++;
51838b8a9b1dSJustin T. Gibbs 			break;
51848b8a9b1dSJustin T. Gibbs 		}
51858b8a9b1dSJustin T. Gibbs 	}
51868b8a9b1dSJustin T. Gibbs 	return (device);
51878b8a9b1dSJustin T. Gibbs }
51888b8a9b1dSJustin T. Gibbs 
51898b8a9b1dSJustin T. Gibbs typedef struct {
51908b8a9b1dSJustin T. Gibbs 	union	ccb *request_ccb;
51918b8a9b1dSJustin T. Gibbs 	struct 	ccb_pathinq *cpi;
5192c1c3139eSMatt Jacob 	int	counter;
51938b8a9b1dSJustin T. Gibbs } xpt_scan_bus_info;
51948b8a9b1dSJustin T. Gibbs 
51958b8a9b1dSJustin T. Gibbs /*
51968b8a9b1dSJustin T. Gibbs  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
51978b8a9b1dSJustin T. Gibbs  * As the scan progresses, xpt_scan_bus is used as the
51988b8a9b1dSJustin T. Gibbs  * callback on completion function.
51998b8a9b1dSJustin T. Gibbs  */
52008b8a9b1dSJustin T. Gibbs static void
52018b8a9b1dSJustin T. Gibbs xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
52028b8a9b1dSJustin T. Gibbs {
52038b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
52048b8a9b1dSJustin T. Gibbs 		  ("xpt_scan_bus\n"));
52058b8a9b1dSJustin T. Gibbs 	switch (request_ccb->ccb_h.func_code) {
52068b8a9b1dSJustin T. Gibbs 	case XPT_SCAN_BUS:
52078b8a9b1dSJustin T. Gibbs 	{
52088b8a9b1dSJustin T. Gibbs 		xpt_scan_bus_info *scan_info;
52098b8a9b1dSJustin T. Gibbs 		union	ccb *work_ccb;
52108b8a9b1dSJustin T. Gibbs 		struct	cam_path *path;
52118b8a9b1dSJustin T. Gibbs 		u_int	i;
52128b8a9b1dSJustin T. Gibbs 		u_int	max_target;
52138b8a9b1dSJustin T. Gibbs 		u_int	initiator_id;
52148b8a9b1dSJustin T. Gibbs 
52158b8a9b1dSJustin T. Gibbs 		/* Find out the characteristics of the bus */
52168008a935SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
52178008a935SScott Long 		if (work_ccb == NULL) {
52188008a935SScott Long 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
52198008a935SScott Long 			xpt_done(request_ccb);
52208008a935SScott Long 			return;
52218008a935SScott Long 		}
52228b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
52238b8a9b1dSJustin T. Gibbs 			      request_ccb->ccb_h.pinfo.priority);
52248b8a9b1dSJustin T. Gibbs 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
52258b8a9b1dSJustin T. Gibbs 		xpt_action(work_ccb);
52268b8a9b1dSJustin T. Gibbs 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
52278b8a9b1dSJustin T. Gibbs 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
52288b8a9b1dSJustin T. Gibbs 			xpt_free_ccb(work_ccb);
52298b8a9b1dSJustin T. Gibbs 			xpt_done(request_ccb);
52308b8a9b1dSJustin T. Gibbs 			return;
52318b8a9b1dSJustin T. Gibbs 		}
52328b8a9b1dSJustin T. Gibbs 
523398192658SJustin T. Gibbs 		if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
523498192658SJustin T. Gibbs 			/*
523598192658SJustin T. Gibbs 			 * Can't scan the bus on an adapter that
523698192658SJustin T. Gibbs 			 * cannot perform the initiator role.
523798192658SJustin T. Gibbs 			 */
523898192658SJustin T. Gibbs 			request_ccb->ccb_h.status = CAM_REQ_CMP;
523998192658SJustin T. Gibbs 			xpt_free_ccb(work_ccb);
524098192658SJustin T. Gibbs 			xpt_done(request_ccb);
524198192658SJustin T. Gibbs 			return;
524298192658SJustin T. Gibbs 		}
524398192658SJustin T. Gibbs 
52448b8a9b1dSJustin T. Gibbs 		/* Save some state for use while we probe for devices */
52458b8a9b1dSJustin T. Gibbs 		scan_info = (xpt_scan_bus_info *)
52460dd50e9bSScott Long 		    malloc(sizeof(xpt_scan_bus_info), M_CAMXPT, M_NOWAIT);
52478b8a9b1dSJustin T. Gibbs 		scan_info->request_ccb = request_ccb;
52488b8a9b1dSJustin T. Gibbs 		scan_info->cpi = &work_ccb->cpi;
52498b8a9b1dSJustin T. Gibbs 
52508b8a9b1dSJustin T. Gibbs 		/* Cache on our stack so we can work asynchronously */
52518b8a9b1dSJustin T. Gibbs 		max_target = scan_info->cpi->max_target;
52528b8a9b1dSJustin T. Gibbs 		initiator_id = scan_info->cpi->initiator_id;
52538b8a9b1dSJustin T. Gibbs 
5254c1c3139eSMatt Jacob 
52558b8a9b1dSJustin T. Gibbs 		/*
5256c1c3139eSMatt Jacob 		 * We can scan all targets in parallel, or do it sequentially.
52578b8a9b1dSJustin T. Gibbs 		 */
5258c1c3139eSMatt Jacob 		if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
5259c1c3139eSMatt Jacob 			max_target = 0;
5260c1c3139eSMatt Jacob 			scan_info->counter = 0;
5261c1c3139eSMatt Jacob 		} else {
5262c1c3139eSMatt Jacob 			scan_info->counter = scan_info->cpi->max_target + 1;
5263c1c3139eSMatt Jacob 			if (scan_info->cpi->initiator_id < scan_info->counter) {
5264c1c3139eSMatt Jacob 				scan_info->counter--;
5265c1c3139eSMatt Jacob 			}
5266c1c3139eSMatt Jacob 		}
52678b8a9b1dSJustin T. Gibbs 
52688b8a9b1dSJustin T. Gibbs 		for (i = 0; i <= max_target; i++) {
52698b8a9b1dSJustin T. Gibbs 			cam_status status;
52708b8a9b1dSJustin T. Gibbs 			if (i == initiator_id)
52718b8a9b1dSJustin T. Gibbs 				continue;
52728b8a9b1dSJustin T. Gibbs 
52738b8a9b1dSJustin T. Gibbs 			status = xpt_create_path(&path, xpt_periph,
52748b8a9b1dSJustin T. Gibbs 						 request_ccb->ccb_h.path_id,
52758b8a9b1dSJustin T. Gibbs 						 i, 0);
52768b8a9b1dSJustin T. Gibbs 			if (status != CAM_REQ_CMP) {
52778b8a9b1dSJustin T. Gibbs 				printf("xpt_scan_bus: xpt_create_path failed"
52788b8a9b1dSJustin T. Gibbs 				       " with status %#x, bus scan halted\n",
52798b8a9b1dSJustin T. Gibbs 				       status);
52800dd50e9bSScott Long 				free(scan_info, M_CAMXPT);
5281c1c3139eSMatt Jacob 				request_ccb->ccb_h.status = status;
5282c1c3139eSMatt Jacob 				xpt_free_ccb(work_ccb);
5283c1c3139eSMatt Jacob 				xpt_done(request_ccb);
52848b8a9b1dSJustin T. Gibbs 				break;
52858b8a9b1dSJustin T. Gibbs 			}
52868008a935SScott Long 			work_ccb = xpt_alloc_ccb_nowait();
52878008a935SScott Long 			if (work_ccb == NULL) {
52880dd50e9bSScott Long 				free(scan_info, M_CAMXPT);
52898008a935SScott Long 				xpt_free_path(path);
52908008a935SScott Long 				request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
52918008a935SScott Long 				xpt_done(request_ccb);
52928008a935SScott Long 				break;
52938008a935SScott Long 			}
52948b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&work_ccb->ccb_h, path,
52958b8a9b1dSJustin T. Gibbs 				      request_ccb->ccb_h.pinfo.priority);
52968b8a9b1dSJustin T. Gibbs 			work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
52978b8a9b1dSJustin T. Gibbs 			work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
52988b8a9b1dSJustin T. Gibbs 			work_ccb->ccb_h.ppriv_ptr0 = scan_info;
52998b8a9b1dSJustin T. Gibbs 			work_ccb->crcn.flags = request_ccb->crcn.flags;
53008b8a9b1dSJustin T. Gibbs 			xpt_action(work_ccb);
53018b8a9b1dSJustin T. Gibbs 		}
53028b8a9b1dSJustin T. Gibbs 		break;
53038b8a9b1dSJustin T. Gibbs 	}
53048b8a9b1dSJustin T. Gibbs 	case XPT_SCAN_LUN:
53058b8a9b1dSJustin T. Gibbs 	{
5306c1c3139eSMatt Jacob 		cam_status status;
5307c1c3139eSMatt Jacob 		struct cam_path *path;
53088b8a9b1dSJustin T. Gibbs 		xpt_scan_bus_info *scan_info;
53098b8a9b1dSJustin T. Gibbs 		path_id_t path_id;
53108b8a9b1dSJustin T. Gibbs 		target_id_t target_id;
53118b8a9b1dSJustin T. Gibbs 		lun_id_t lun_id;
53128b8a9b1dSJustin T. Gibbs 
53138b8a9b1dSJustin T. Gibbs 		/* Reuse the same CCB to query if a device was really found */
53148b8a9b1dSJustin T. Gibbs 		scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
53158b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
53168b8a9b1dSJustin T. Gibbs 			      request_ccb->ccb_h.pinfo.priority);
53178b8a9b1dSJustin T. Gibbs 		request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
53188b8a9b1dSJustin T. Gibbs 
53198b8a9b1dSJustin T. Gibbs 		path_id = request_ccb->ccb_h.path_id;
53208b8a9b1dSJustin T. Gibbs 		target_id = request_ccb->ccb_h.target_id;
53218b8a9b1dSJustin T. Gibbs 		lun_id = request_ccb->ccb_h.target_lun;
53228b8a9b1dSJustin T. Gibbs 		xpt_action(request_ccb);
53238b8a9b1dSJustin T. Gibbs 
53248b8a9b1dSJustin T. Gibbs 		if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
53258b8a9b1dSJustin T. Gibbs 			struct cam_ed *device;
53268b8a9b1dSJustin T. Gibbs 			struct cam_et *target;
532777dc25ccSScott Long 			int phl;
53288b8a9b1dSJustin T. Gibbs 
53298b8a9b1dSJustin T. Gibbs 			/*
53308b8a9b1dSJustin T. Gibbs 			 * If we already probed lun 0 successfully, or
53318b8a9b1dSJustin T. Gibbs 			 * we have additional configured luns on this
53328b8a9b1dSJustin T. Gibbs 			 * target that might have "gone away", go onto
53338b8a9b1dSJustin T. Gibbs 			 * the next lun.
53348b8a9b1dSJustin T. Gibbs 			 */
53358b8a9b1dSJustin T. Gibbs 			target = request_ccb->ccb_h.path->target;
5336b6a0d1abSMatt Jacob 			/*
5337b6a0d1abSMatt Jacob 			 * We may touch devices that we don't
5338b6a0d1abSMatt Jacob 			 * hold references too, so ensure they
5339b6a0d1abSMatt Jacob 			 * don't disappear out from under us.
5340b6a0d1abSMatt Jacob 			 * The target above is referenced by the
5341b6a0d1abSMatt Jacob 			 * path in the request ccb.
5342b6a0d1abSMatt Jacob 			 */
5343b6a0d1abSMatt Jacob 			phl = 0;
53448b8a9b1dSJustin T. Gibbs 			device = TAILQ_FIRST(&target->ed_entries);
5345b6a0d1abSMatt Jacob 			if (device != NULL) {
53463b87a552SMatt Jacob 				phl = CAN_SRCH_HI_SPARSE(device);
5347b6a0d1abSMatt Jacob 				if (device->lun_id == 0)
53488b8a9b1dSJustin T. Gibbs 					device = TAILQ_NEXT(device, links);
5349b6a0d1abSMatt Jacob 			}
53508b8a9b1dSJustin T. Gibbs 			if ((lun_id != 0) || (device != NULL)) {
5351b6a0d1abSMatt Jacob 				if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
53528b8a9b1dSJustin T. Gibbs 					lun_id++;
53538b8a9b1dSJustin T. Gibbs 			}
53548b8a9b1dSJustin T. Gibbs 		} else {
53558b8a9b1dSJustin T. Gibbs 			struct cam_ed *device;
53568b8a9b1dSJustin T. Gibbs 
53578b8a9b1dSJustin T. Gibbs 			device = request_ccb->ccb_h.path->device;
53588b8a9b1dSJustin T. Gibbs 
53598b8a9b1dSJustin T. Gibbs 			if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
53608b8a9b1dSJustin T. Gibbs 				/* Try the next lun */
5361f4322bc8SMatt Jacob 				if (lun_id < (CAM_SCSI2_MAXLUN-1)
53623b87a552SMatt Jacob 				  || CAN_SRCH_HI_DENSE(device))
53638b8a9b1dSJustin T. Gibbs 					lun_id++;
53648b8a9b1dSJustin T. Gibbs 			}
53658b8a9b1dSJustin T. Gibbs 		}
53668b8a9b1dSJustin T. Gibbs 
5367c1c3139eSMatt Jacob 		/*
5368c1c3139eSMatt Jacob 		 * Free the current request path- we're done with it.
5369c1c3139eSMatt Jacob 		 */
53708b8a9b1dSJustin T. Gibbs 		xpt_free_path(request_ccb->ccb_h.path);
53718b8a9b1dSJustin T. Gibbs 
5372c1c3139eSMatt Jacob 		/*
5373c1c3139eSMatt Jacob 		 * Check to see if we scan any further luns.
5374c1c3139eSMatt Jacob 		 */
5375c1c3139eSMatt Jacob 		if (lun_id == request_ccb->ccb_h.target_lun
53768b8a9b1dSJustin T. Gibbs                  || lun_id > scan_info->cpi->max_lun) {
5377c1c3139eSMatt Jacob 			int done;
53788b8a9b1dSJustin T. Gibbs 
5379c1c3139eSMatt Jacob  hop_again:
5380c1c3139eSMatt Jacob 			done = 0;
5381c1c3139eSMatt Jacob 			if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
5382c1c3139eSMatt Jacob 				scan_info->counter++;
5383c1c3139eSMatt Jacob 				if (scan_info->counter ==
5384c1c3139eSMatt Jacob 				    scan_info->cpi->initiator_id) {
5385c1c3139eSMatt Jacob 					scan_info->counter++;
5386c1c3139eSMatt Jacob 				}
5387c1c3139eSMatt Jacob 				if (scan_info->counter >=
5388c1c3139eSMatt Jacob 				    scan_info->cpi->max_target+1) {
5389c1c3139eSMatt Jacob 					done = 1;
5390c1c3139eSMatt Jacob 				}
5391c1c3139eSMatt Jacob 			} else {
5392c1c3139eSMatt Jacob 				scan_info->counter--;
5393c1c3139eSMatt Jacob 				if (scan_info->counter == 0) {
5394c1c3139eSMatt Jacob 					done = 1;
5395c1c3139eSMatt Jacob 				}
5396c1c3139eSMatt Jacob 			}
5397c1c3139eSMatt Jacob 			if (done) {
53988b8a9b1dSJustin T. Gibbs 				xpt_free_ccb(request_ccb);
53998b8a9b1dSJustin T. Gibbs 				xpt_free_ccb((union ccb *)scan_info->cpi);
54008b8a9b1dSJustin T. Gibbs 				request_ccb = scan_info->request_ccb;
54010dd50e9bSScott Long 				free(scan_info, M_CAMXPT);
54028b8a9b1dSJustin T. Gibbs 				request_ccb->ccb_h.status = CAM_REQ_CMP;
54038b8a9b1dSJustin T. Gibbs 				xpt_done(request_ccb);
5404c1c3139eSMatt Jacob 				break;
54058b8a9b1dSJustin T. Gibbs 			}
54068b8a9b1dSJustin T. Gibbs 
5407c1c3139eSMatt Jacob 			if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
5408c1c3139eSMatt Jacob 				break;
5409c1c3139eSMatt Jacob 			}
54108b8a9b1dSJustin T. Gibbs 			status = xpt_create_path(&path, xpt_periph,
5411c1c3139eSMatt Jacob 			    scan_info->request_ccb->ccb_h.path_id,
5412c1c3139eSMatt Jacob 			    scan_info->counter, 0);
54138b8a9b1dSJustin T. Gibbs 			if (status != CAM_REQ_CMP) {
54148b8a9b1dSJustin T. Gibbs 				printf("xpt_scan_bus: xpt_create_path failed"
5415c1c3139eSMatt Jacob 				    " with status %#x, bus scan halted\n",
54168b8a9b1dSJustin T. Gibbs 			       	    status);
54178b8a9b1dSJustin T. Gibbs 				xpt_free_ccb(request_ccb);
5418c1c3139eSMatt Jacob 				xpt_free_ccb((union ccb *)scan_info->cpi);
54198b8a9b1dSJustin T. Gibbs 				request_ccb = scan_info->request_ccb;
54200dd50e9bSScott Long 				free(scan_info, M_CAMXPT);
5421c1c3139eSMatt Jacob 				request_ccb->ccb_h.status = status;
54228b8a9b1dSJustin T. Gibbs 				xpt_done(request_ccb);
5423ec04feb2SSam Leffler 				break;
54248b8a9b1dSJustin T. Gibbs 			}
54258b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&request_ccb->ccb_h, path,
54268b8a9b1dSJustin T. Gibbs 			    request_ccb->ccb_h.pinfo.priority);
54278b8a9b1dSJustin T. Gibbs 			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
54288b8a9b1dSJustin T. Gibbs 			request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
54298b8a9b1dSJustin T. Gibbs 			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
54308b8a9b1dSJustin T. Gibbs 			request_ccb->crcn.flags =
54318b8a9b1dSJustin T. Gibbs 			    scan_info->request_ccb->crcn.flags;
5432c1c3139eSMatt Jacob 		} else {
5433c1c3139eSMatt Jacob 			status = xpt_create_path(&path, xpt_periph,
5434c1c3139eSMatt Jacob 						 path_id, target_id, lun_id);
5435c1c3139eSMatt Jacob 			if (status != CAM_REQ_CMP) {
5436c1c3139eSMatt Jacob 				printf("xpt_scan_bus: xpt_create_path failed "
5437c1c3139eSMatt Jacob 				       "with status %#x, halting LUN scan\n",
5438c1c3139eSMatt Jacob 			 	       status);
5439c1c3139eSMatt Jacob 				goto hop_again;
54408b8a9b1dSJustin T. Gibbs 			}
5441c1c3139eSMatt Jacob 			xpt_setup_ccb(&request_ccb->ccb_h, path,
5442c1c3139eSMatt Jacob 				      request_ccb->ccb_h.pinfo.priority);
5443c1c3139eSMatt Jacob 			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5444c1c3139eSMatt Jacob 			request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5445c1c3139eSMatt Jacob 			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5446c1c3139eSMatt Jacob 			request_ccb->crcn.flags =
5447c1c3139eSMatt Jacob 				scan_info->request_ccb->crcn.flags;
5448c1c3139eSMatt Jacob 		}
5449c1c3139eSMatt Jacob 		xpt_action(request_ccb);
54508b8a9b1dSJustin T. Gibbs 		break;
54518b8a9b1dSJustin T. Gibbs 	}
54528b8a9b1dSJustin T. Gibbs 	default:
54538b8a9b1dSJustin T. Gibbs 		break;
54548b8a9b1dSJustin T. Gibbs 	}
54558b8a9b1dSJustin T. Gibbs }
54568b8a9b1dSJustin T. Gibbs 
54578b8a9b1dSJustin T. Gibbs typedef enum {
54588b8a9b1dSJustin T. Gibbs 	PROBE_TUR,
54592afca7acSMatt Jacob 	PROBE_INQUIRY,	/* this counts as DV0 for Basic Domain Validation */
54609ec5d7cdSMatt Jacob 	PROBE_FULL_INQUIRY,
54618b8a9b1dSJustin T. Gibbs 	PROBE_MODE_SENSE,
54628b8a9b1dSJustin T. Gibbs 	PROBE_SERIAL_NUM,
54632afca7acSMatt Jacob 	PROBE_TUR_FOR_NEGOTIATION,
54642afca7acSMatt Jacob 	PROBE_INQUIRY_BASIC_DV1,
54652afca7acSMatt Jacob 	PROBE_INQUIRY_BASIC_DV2,
54662afca7acSMatt Jacob 	PROBE_DV_EXIT
54678b8a9b1dSJustin T. Gibbs } probe_action;
54688b8a9b1dSJustin T. Gibbs 
54698b8a9b1dSJustin T. Gibbs typedef enum {
54708b8a9b1dSJustin T. Gibbs 	PROBE_INQUIRY_CKSUM	= 0x01,
54718b8a9b1dSJustin T. Gibbs 	PROBE_SERIAL_CKSUM	= 0x02,
54728b8a9b1dSJustin T. Gibbs 	PROBE_NO_ANNOUNCE	= 0x04
54738b8a9b1dSJustin T. Gibbs } probe_flags;
54748b8a9b1dSJustin T. Gibbs 
54758b8a9b1dSJustin T. Gibbs typedef struct {
5476e3975643SJake Burkholder 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
54778b8a9b1dSJustin T. Gibbs 	probe_action	action;
54788b8a9b1dSJustin T. Gibbs 	union ccb	saved_ccb;
54798b8a9b1dSJustin T. Gibbs 	probe_flags	flags;
54808b8a9b1dSJustin T. Gibbs 	MD5_CTX		context;
54818b8a9b1dSJustin T. Gibbs 	u_int8_t	digest[16];
54828b8a9b1dSJustin T. Gibbs } probe_softc;
54838b8a9b1dSJustin T. Gibbs 
54848b8a9b1dSJustin T. Gibbs static void
54858b8a9b1dSJustin T. Gibbs xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
54868b8a9b1dSJustin T. Gibbs 	     cam_flags flags, union ccb *request_ccb)
54878b8a9b1dSJustin T. Gibbs {
548898192658SJustin T. Gibbs 	struct ccb_pathinq cpi;
54898b8a9b1dSJustin T. Gibbs 	cam_status status;
54908b8a9b1dSJustin T. Gibbs 	struct cam_path *new_path;
54918b8a9b1dSJustin T. Gibbs 	struct cam_periph *old_periph;
54928b8a9b1dSJustin T. Gibbs 
54938b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
54948b8a9b1dSJustin T. Gibbs 		  ("xpt_scan_lun\n"));
54958b8a9b1dSJustin T. Gibbs 
549698192658SJustin T. Gibbs 	xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
549798192658SJustin T. Gibbs 	cpi.ccb_h.func_code = XPT_PATH_INQ;
549898192658SJustin T. Gibbs 	xpt_action((union ccb *)&cpi);
549998192658SJustin T. Gibbs 
550098192658SJustin T. Gibbs 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
550198192658SJustin T. Gibbs 		if (request_ccb != NULL) {
550298192658SJustin T. Gibbs 			request_ccb->ccb_h.status = cpi.ccb_h.status;
550398192658SJustin T. Gibbs 			xpt_done(request_ccb);
550498192658SJustin T. Gibbs 		}
550598192658SJustin T. Gibbs 		return;
550698192658SJustin T. Gibbs 	}
550798192658SJustin T. Gibbs 
550898192658SJustin T. Gibbs 	if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
550998192658SJustin T. Gibbs 		/*
551098192658SJustin T. Gibbs 		 * Can't scan the bus on an adapter that
551198192658SJustin T. Gibbs 		 * cannot perform the initiator role.
551298192658SJustin T. Gibbs 		 */
551398192658SJustin T. Gibbs 		if (request_ccb != NULL) {
551498192658SJustin T. Gibbs 			request_ccb->ccb_h.status = CAM_REQ_CMP;
551598192658SJustin T. Gibbs 			xpt_done(request_ccb);
551698192658SJustin T. Gibbs 		}
551798192658SJustin T. Gibbs 		return;
551898192658SJustin T. Gibbs 	}
551998192658SJustin T. Gibbs 
55208b8a9b1dSJustin T. Gibbs 	if (request_ccb == NULL) {
55210dd50e9bSScott Long 		request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
55228b8a9b1dSJustin T. Gibbs 		if (request_ccb == NULL) {
5523f0d9af51SMatt Jacob 			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
5524f0d9af51SMatt Jacob 			    "can't continue\n");
55258b8a9b1dSJustin T. Gibbs 			return;
55268b8a9b1dSJustin T. Gibbs 		}
55270dd50e9bSScott Long 		new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
55288b8a9b1dSJustin T. Gibbs 		if (new_path == NULL) {
5529f0d9af51SMatt Jacob 			xpt_print(path, "xpt_scan_lun: can't allocate path, "
5530f0d9af51SMatt Jacob 			    "can't continue\n");
55310dd50e9bSScott Long 			free(request_ccb, M_CAMXPT);
55328b8a9b1dSJustin T. Gibbs 			return;
55338b8a9b1dSJustin T. Gibbs 		}
5534fce84cb4SKenneth D. Merry 		status = xpt_compile_path(new_path, xpt_periph,
5535fce84cb4SKenneth D. Merry 					  path->bus->path_id,
55368b8a9b1dSJustin T. Gibbs 					  path->target->target_id,
55378b8a9b1dSJustin T. Gibbs 					  path->device->lun_id);
55388b8a9b1dSJustin T. Gibbs 
55398b8a9b1dSJustin T. Gibbs 		if (status != CAM_REQ_CMP) {
5540f0d9af51SMatt Jacob 			xpt_print(path, "xpt_scan_lun: can't compile path, "
5541f0d9af51SMatt Jacob 			    "can't continue\n");
55420dd50e9bSScott Long 			free(request_ccb, M_CAMXPT);
55430dd50e9bSScott Long 			free(new_path, M_CAMXPT);
55448b8a9b1dSJustin T. Gibbs 			return;
55458b8a9b1dSJustin T. Gibbs 		}
55468b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
55478b8a9b1dSJustin T. Gibbs 		request_ccb->ccb_h.cbfcnp = xptscandone;
55488b8a9b1dSJustin T. Gibbs 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
55498b8a9b1dSJustin T. Gibbs 		request_ccb->crcn.flags = flags;
55508b8a9b1dSJustin T. Gibbs 	}
55518b8a9b1dSJustin T. Gibbs 
55528b8a9b1dSJustin T. Gibbs 	if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
55538b8a9b1dSJustin T. Gibbs 		probe_softc *softc;
55548b8a9b1dSJustin T. Gibbs 
55558b8a9b1dSJustin T. Gibbs 		softc = (probe_softc *)old_periph->softc;
55568b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
55578b8a9b1dSJustin T. Gibbs 				  periph_links.tqe);
55588b8a9b1dSJustin T. Gibbs 	} else {
5559ee9c90c7SKenneth D. Merry 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
55608b8a9b1dSJustin T. Gibbs 					  probestart, "probe",
55618b8a9b1dSJustin T. Gibbs 					  CAM_PERIPH_BIO,
55628b8a9b1dSJustin T. Gibbs 					  request_ccb->ccb_h.path, NULL, 0,
55638b8a9b1dSJustin T. Gibbs 					  request_ccb);
55648b8a9b1dSJustin T. Gibbs 
55658b8a9b1dSJustin T. Gibbs 		if (status != CAM_REQ_CMP) {
5566f0d9af51SMatt Jacob 			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
5567f0d9af51SMatt Jacob 			    "returned an error, can't continue probe\n");
55688b8a9b1dSJustin T. Gibbs 			request_ccb->ccb_h.status = status;
55698b8a9b1dSJustin T. Gibbs 			xpt_done(request_ccb);
55708b8a9b1dSJustin T. Gibbs 		}
55718b8a9b1dSJustin T. Gibbs 	}
55728b8a9b1dSJustin T. Gibbs }
55738b8a9b1dSJustin T. Gibbs 
55748b8a9b1dSJustin T. Gibbs static void
55758b8a9b1dSJustin T. Gibbs xptscandone(struct cam_periph *periph, union ccb *done_ccb)
55768b8a9b1dSJustin T. Gibbs {
55778b8a9b1dSJustin T. Gibbs 	xpt_release_path(done_ccb->ccb_h.path);
55780dd50e9bSScott Long 	free(done_ccb->ccb_h.path, M_CAMXPT);
55790dd50e9bSScott Long 	free(done_ccb, M_CAMXPT);
55808b8a9b1dSJustin T. Gibbs }
55818b8a9b1dSJustin T. Gibbs 
55828b8a9b1dSJustin T. Gibbs static cam_status
55838b8a9b1dSJustin T. Gibbs proberegister(struct cam_periph *periph, void *arg)
55848b8a9b1dSJustin T. Gibbs {
558587cfaf0eSJustin T. Gibbs 	union ccb *request_ccb;	/* CCB representing the probe request */
558600b08b94SMatt Jacob 	cam_status status;
55878b8a9b1dSJustin T. Gibbs 	probe_softc *softc;
55888b8a9b1dSJustin T. Gibbs 
558987cfaf0eSJustin T. Gibbs 	request_ccb = (union ccb *)arg;
55908b8a9b1dSJustin T. Gibbs 	if (periph == NULL) {
55918b8a9b1dSJustin T. Gibbs 		printf("proberegister: periph was NULL!!\n");
55928b8a9b1dSJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
55938b8a9b1dSJustin T. Gibbs 	}
55948b8a9b1dSJustin T. Gibbs 
559587cfaf0eSJustin T. Gibbs 	if (request_ccb == NULL) {
5596434bbf6eSJustin T. Gibbs 		printf("proberegister: no probe CCB, "
5597434bbf6eSJustin T. Gibbs 		       "can't register device\n");
55988b8a9b1dSJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
55998b8a9b1dSJustin T. Gibbs 	}
56008b8a9b1dSJustin T. Gibbs 
56010dd50e9bSScott Long 	softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
56028b8a9b1dSJustin T. Gibbs 
56038b8a9b1dSJustin T. Gibbs 	if (softc == NULL) {
56048b8a9b1dSJustin T. Gibbs 		printf("proberegister: Unable to probe new device. "
56058b8a9b1dSJustin T. Gibbs 		       "Unable to allocate softc\n");
56068b8a9b1dSJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
56078b8a9b1dSJustin T. Gibbs 	}
56088b8a9b1dSJustin T. Gibbs 	TAILQ_INIT(&softc->request_ccbs);
560987cfaf0eSJustin T. Gibbs 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
561087cfaf0eSJustin T. Gibbs 			  periph_links.tqe);
56118b8a9b1dSJustin T. Gibbs 	softc->flags = 0;
56128b8a9b1dSJustin T. Gibbs 	periph->softc = softc;
561300b08b94SMatt Jacob 	status = cam_periph_acquire(periph);
561400b08b94SMatt Jacob 	if (status != CAM_REQ_CMP) {
561500b08b94SMatt Jacob 		return (status);
561600b08b94SMatt Jacob 	}
561700b08b94SMatt Jacob 
561800b08b94SMatt Jacob 
561987cfaf0eSJustin T. Gibbs 	/*
562087cfaf0eSJustin T. Gibbs 	 * Ensure we've waited at least a bus settle
562187cfaf0eSJustin T. Gibbs 	 * delay before attempting to probe the device.
5622cda9006fSMatt Jacob 	 * For HBAs that don't do bus resets, this won't make a difference.
562387cfaf0eSJustin T. Gibbs 	 */
562487cfaf0eSJustin T. Gibbs 	cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
56253a937198SBrooks Davis 				      scsi_delay);
56268b8a9b1dSJustin T. Gibbs 	probeschedule(periph);
56278b8a9b1dSJustin T. Gibbs 	return(CAM_REQ_CMP);
56288b8a9b1dSJustin T. Gibbs }
56298b8a9b1dSJustin T. Gibbs 
56308b8a9b1dSJustin T. Gibbs static void
56318b8a9b1dSJustin T. Gibbs probeschedule(struct cam_periph *periph)
56328b8a9b1dSJustin T. Gibbs {
563387cfaf0eSJustin T. Gibbs 	struct ccb_pathinq cpi;
56348b8a9b1dSJustin T. Gibbs 	union ccb *ccb;
56358b8a9b1dSJustin T. Gibbs 	probe_softc *softc;
56368b8a9b1dSJustin T. Gibbs 
56378b8a9b1dSJustin T. Gibbs 	softc = (probe_softc *)periph->softc;
56388b8a9b1dSJustin T. Gibbs 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
56398b8a9b1dSJustin T. Gibbs 
564087cfaf0eSJustin T. Gibbs 	xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
564187cfaf0eSJustin T. Gibbs 	cpi.ccb_h.func_code = XPT_PATH_INQ;
564287cfaf0eSJustin T. Gibbs 	xpt_action((union ccb *)&cpi);
564387cfaf0eSJustin T. Gibbs 
56448b8a9b1dSJustin T. Gibbs 	/*
56458b8a9b1dSJustin T. Gibbs 	 * If a device has gone away and another device, or the same one,
56468b8a9b1dSJustin T. Gibbs 	 * is back in the same place, it should have a unit attention
56478b8a9b1dSJustin T. Gibbs 	 * condition pending.  It will not report the unit attention in
56488b8a9b1dSJustin T. Gibbs 	 * response to an inquiry, which may leave invalid transfer
56498b8a9b1dSJustin T. Gibbs 	 * negotiations in effect.  The TUR will reveal the unit attention
56508b8a9b1dSJustin T. Gibbs 	 * condition.  Only send the TUR for lun 0, since some devices
56518b8a9b1dSJustin T. Gibbs 	 * will get confused by commands other than inquiry to non-existent
56528b8a9b1dSJustin T. Gibbs 	 * luns.  If you think a device has gone away start your scan from
56538b8a9b1dSJustin T. Gibbs 	 * lun 0.  This will insure that any bogus transfer settings are
56548b8a9b1dSJustin T. Gibbs 	 * invalidated.
565587cfaf0eSJustin T. Gibbs 	 *
565687cfaf0eSJustin T. Gibbs 	 * If we haven't seen the device before and the controller supports
565787cfaf0eSJustin T. Gibbs 	 * some kind of transfer negotiation, negotiate with the first
565887cfaf0eSJustin T. Gibbs 	 * sent command if no bus reset was performed at startup.  This
565987cfaf0eSJustin T. Gibbs 	 * ensures that the device is not confused by transfer negotiation
566087cfaf0eSJustin T. Gibbs 	 * settings left over by loader or BIOS action.
56618b8a9b1dSJustin T. Gibbs 	 */
56628b8a9b1dSJustin T. Gibbs 	if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
566387cfaf0eSJustin T. Gibbs 	 && (ccb->ccb_h.target_lun == 0)) {
56648b8a9b1dSJustin T. Gibbs 		softc->action = PROBE_TUR;
566587cfaf0eSJustin T. Gibbs 	} else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
566687cfaf0eSJustin T. Gibbs 	      && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
566787cfaf0eSJustin T. Gibbs 		proberequestdefaultnegotiation(periph);
56688b8a9b1dSJustin T. Gibbs 		softc->action = PROBE_INQUIRY;
566987cfaf0eSJustin T. Gibbs 	} else {
567087cfaf0eSJustin T. Gibbs 		softc->action = PROBE_INQUIRY;
567187cfaf0eSJustin T. Gibbs 	}
56728b8a9b1dSJustin T. Gibbs 
56738b8a9b1dSJustin T. Gibbs 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
56748b8a9b1dSJustin T. Gibbs 		softc->flags |= PROBE_NO_ANNOUNCE;
56758b8a9b1dSJustin T. Gibbs 	else
56768b8a9b1dSJustin T. Gibbs 		softc->flags &= ~PROBE_NO_ANNOUNCE;
56778b8a9b1dSJustin T. Gibbs 
56788b8a9b1dSJustin T. Gibbs 	xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
56798b8a9b1dSJustin T. Gibbs }
56808b8a9b1dSJustin T. Gibbs 
56818b8a9b1dSJustin T. Gibbs static void
56828b8a9b1dSJustin T. Gibbs probestart(struct cam_periph *periph, union ccb *start_ccb)
56838b8a9b1dSJustin T. Gibbs {
56848b8a9b1dSJustin T. Gibbs 	/* Probe the device that our peripheral driver points to */
56858b8a9b1dSJustin T. Gibbs 	struct ccb_scsiio *csio;
56868b8a9b1dSJustin T. Gibbs 	probe_softc *softc;
56878b8a9b1dSJustin T. Gibbs 
56888b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
56898b8a9b1dSJustin T. Gibbs 
56908b8a9b1dSJustin T. Gibbs 	softc = (probe_softc *)periph->softc;
56918b8a9b1dSJustin T. Gibbs 	csio = &start_ccb->csio;
56928b8a9b1dSJustin T. Gibbs 
56938b8a9b1dSJustin T. Gibbs 	switch (softc->action) {
56948b8a9b1dSJustin T. Gibbs 	case PROBE_TUR:
56958b8a9b1dSJustin T. Gibbs 	case PROBE_TUR_FOR_NEGOTIATION:
56962afca7acSMatt Jacob 	case PROBE_DV_EXIT:
56978b8a9b1dSJustin T. Gibbs 	{
56988b8a9b1dSJustin T. Gibbs 		scsi_test_unit_ready(csio,
56998b8a9b1dSJustin T. Gibbs 				     /*retries*/4,
57008b8a9b1dSJustin T. Gibbs 				     probedone,
57018b8a9b1dSJustin T. Gibbs 				     MSG_SIMPLE_Q_TAG,
57028b8a9b1dSJustin T. Gibbs 				     SSD_FULL_SIZE,
5703e471e974SJustin T. Gibbs 				     /*timeout*/60000);
57048b8a9b1dSJustin T. Gibbs 		break;
57058b8a9b1dSJustin T. Gibbs 	}
57068b8a9b1dSJustin T. Gibbs 	case PROBE_INQUIRY:
57079ec5d7cdSMatt Jacob 	case PROBE_FULL_INQUIRY:
57082afca7acSMatt Jacob 	case PROBE_INQUIRY_BASIC_DV1:
57092afca7acSMatt Jacob 	case PROBE_INQUIRY_BASIC_DV2:
57108b8a9b1dSJustin T. Gibbs 	{
5711c19cf05dSMatt Jacob 		u_int inquiry_len;
57128b8a9b1dSJustin T. Gibbs 		struct scsi_inquiry_data *inq_buf;
57138b8a9b1dSJustin T. Gibbs 
57148b8a9b1dSJustin T. Gibbs 		inq_buf = &periph->path->device->inq_data;
57152afca7acSMatt Jacob 
57168b8a9b1dSJustin T. Gibbs 		/*
57178b8a9b1dSJustin T. Gibbs 		 * If the device is currently configured, we calculate an
57188b8a9b1dSJustin T. Gibbs 		 * MD5 checksum of the inquiry data, and if the serial number
57198b8a9b1dSJustin T. Gibbs 		 * length is greater than 0, add the serial number data
57208b8a9b1dSJustin T. Gibbs 		 * into the checksum as well.  Once the inquiry and the
57218b8a9b1dSJustin T. Gibbs 		 * serial number check finish, we attempt to figure out
57228b8a9b1dSJustin T. Gibbs 		 * whether we still have the same device.
57238b8a9b1dSJustin T. Gibbs 		 */
57248b8a9b1dSJustin T. Gibbs 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
57258b8a9b1dSJustin T. Gibbs 
57268b8a9b1dSJustin T. Gibbs 			MD5Init(&softc->context);
57278b8a9b1dSJustin T. Gibbs 			MD5Update(&softc->context, (unsigned char *)inq_buf,
57288b8a9b1dSJustin T. Gibbs 				  sizeof(struct scsi_inquiry_data));
57298b8a9b1dSJustin T. Gibbs 			softc->flags |= PROBE_INQUIRY_CKSUM;
57308b8a9b1dSJustin T. Gibbs 			if (periph->path->device->serial_num_len > 0) {
57318b8a9b1dSJustin T. Gibbs 				MD5Update(&softc->context,
57328b8a9b1dSJustin T. Gibbs 					  periph->path->device->serial_num,
57338b8a9b1dSJustin T. Gibbs 					  periph->path->device->serial_num_len);
57348b8a9b1dSJustin T. Gibbs 				softc->flags |= PROBE_SERIAL_CKSUM;
57358b8a9b1dSJustin T. Gibbs 			}
57368b8a9b1dSJustin T. Gibbs 			MD5Final(softc->digest, &softc->context);
57378b8a9b1dSJustin T. Gibbs 		}
57388b8a9b1dSJustin T. Gibbs 
57399ec5d7cdSMatt Jacob 		if (softc->action == PROBE_INQUIRY)
5740c19cf05dSMatt Jacob 			inquiry_len = SHORT_INQUIRY_LENGTH;
5741c19cf05dSMatt Jacob 		else
57422afca7acSMatt Jacob 			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
5743709a936fSJustin T. Gibbs 
5744709a936fSJustin T. Gibbs 		/*
5745709a936fSJustin T. Gibbs 		 * Some parallel SCSI devices fail to send an
5746709a936fSJustin T. Gibbs 		 * ignore wide residue message when dealing with
5747709a936fSJustin T. Gibbs 		 * odd length inquiry requests.  Round up to be
5748709a936fSJustin T. Gibbs 		 * safe.
5749709a936fSJustin T. Gibbs 		 */
5750709a936fSJustin T. Gibbs 		inquiry_len = roundup2(inquiry_len, 2);
57519ec5d7cdSMatt Jacob 
57522afca7acSMatt Jacob 		if (softc->action == PROBE_INQUIRY_BASIC_DV1
57532afca7acSMatt Jacob 		 || softc->action == PROBE_INQUIRY_BASIC_DV2) {
57540dd50e9bSScott Long 			inq_buf = malloc(inquiry_len, M_CAMXPT, M_NOWAIT);
57552afca7acSMatt Jacob 		}
57562afca7acSMatt Jacob 		if (inq_buf == NULL) {
5757f0d9af51SMatt Jacob 			xpt_print(periph->path, "malloc failure- skipping Basic"
5758f0d9af51SMatt Jacob 			    "Domain Validation\n");
57592afca7acSMatt Jacob 			softc->action = PROBE_DV_EXIT;
57602afca7acSMatt Jacob 			scsi_test_unit_ready(csio,
57612afca7acSMatt Jacob 					     /*retries*/4,
57622afca7acSMatt Jacob 					     probedone,
57632afca7acSMatt Jacob 					     MSG_SIMPLE_Q_TAG,
57642afca7acSMatt Jacob 					     SSD_FULL_SIZE,
57652afca7acSMatt Jacob 					     /*timeout*/60000);
57662afca7acSMatt Jacob 			break;
57672afca7acSMatt Jacob 		}
57688b8a9b1dSJustin T. Gibbs 		scsi_inquiry(csio,
57698b8a9b1dSJustin T. Gibbs 			     /*retries*/4,
57708b8a9b1dSJustin T. Gibbs 			     probedone,
57718b8a9b1dSJustin T. Gibbs 			     MSG_SIMPLE_Q_TAG,
57728b8a9b1dSJustin T. Gibbs 			     (u_int8_t *)inq_buf,
5773c19cf05dSMatt Jacob 			     inquiry_len,
57748b8a9b1dSJustin T. Gibbs 			     /*evpd*/FALSE,
57758b8a9b1dSJustin T. Gibbs 			     /*page_code*/0,
57768b8a9b1dSJustin T. Gibbs 			     SSD_MIN_SIZE,
5777e471e974SJustin T. Gibbs 			     /*timeout*/60 * 1000);
57788b8a9b1dSJustin T. Gibbs 		break;
57798b8a9b1dSJustin T. Gibbs 	}
57808b8a9b1dSJustin T. Gibbs 	case PROBE_MODE_SENSE:
57818b8a9b1dSJustin T. Gibbs 	{
57828b8a9b1dSJustin T. Gibbs 		void  *mode_buf;
57838b8a9b1dSJustin T. Gibbs 		int    mode_buf_len;
57848b8a9b1dSJustin T. Gibbs 
57858b8a9b1dSJustin T. Gibbs 		mode_buf_len = sizeof(struct scsi_mode_header_6)
57868b8a9b1dSJustin T. Gibbs 			     + sizeof(struct scsi_mode_blk_desc)
57878b8a9b1dSJustin T. Gibbs 			     + sizeof(struct scsi_control_page);
57880dd50e9bSScott Long 		mode_buf = malloc(mode_buf_len, M_CAMXPT, M_NOWAIT);
57898b8a9b1dSJustin T. Gibbs 		if (mode_buf != NULL) {
57908b8a9b1dSJustin T. Gibbs 	                scsi_mode_sense(csio,
57918b8a9b1dSJustin T. Gibbs 					/*retries*/4,
57928b8a9b1dSJustin T. Gibbs 					probedone,
57938b8a9b1dSJustin T. Gibbs 					MSG_SIMPLE_Q_TAG,
57948b8a9b1dSJustin T. Gibbs 					/*dbd*/FALSE,
57958b8a9b1dSJustin T. Gibbs 					SMS_PAGE_CTRL_CURRENT,
57968b8a9b1dSJustin T. Gibbs 					SMS_CONTROL_MODE_PAGE,
57978b8a9b1dSJustin T. Gibbs 					mode_buf,
57988b8a9b1dSJustin T. Gibbs 					mode_buf_len,
57998b8a9b1dSJustin T. Gibbs 					SSD_FULL_SIZE,
5800e471e974SJustin T. Gibbs 					/*timeout*/60000);
58018b8a9b1dSJustin T. Gibbs 			break;
58028b8a9b1dSJustin T. Gibbs 		}
5803f0d9af51SMatt Jacob 		xpt_print(periph->path, "Unable to mode sense control page - "
5804f0d9af51SMatt Jacob 		    "malloc failure\n");
58058b8a9b1dSJustin T. Gibbs 		softc->action = PROBE_SERIAL_NUM;
58068b8a9b1dSJustin T. Gibbs 	}
580707c6eac9SPoul-Henning Kamp 	/* FALLTHROUGH */
58088b8a9b1dSJustin T. Gibbs 	case PROBE_SERIAL_NUM:
58098b8a9b1dSJustin T. Gibbs 	{
58108b8a9b1dSJustin T. Gibbs 		struct scsi_vpd_unit_serial_number *serial_buf;
58118b8a9b1dSJustin T. Gibbs 		struct cam_ed* device;
58128b8a9b1dSJustin T. Gibbs 
58138b8a9b1dSJustin T. Gibbs 		serial_buf = NULL;
58148b8a9b1dSJustin T. Gibbs 		device = periph->path->device;
58158b8a9b1dSJustin T. Gibbs 		device->serial_num = NULL;
58168b8a9b1dSJustin T. Gibbs 		device->serial_num_len = 0;
58178b8a9b1dSJustin T. Gibbs 
58188b8a9b1dSJustin T. Gibbs 		if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
58198b8a9b1dSJustin T. Gibbs 			serial_buf = (struct scsi_vpd_unit_serial_number *)
58200dd50e9bSScott Long 				malloc(sizeof(*serial_buf), M_CAMXPT,
58215417ec4dSDavid Malone 					M_NOWAIT | M_ZERO);
58228b8a9b1dSJustin T. Gibbs 
58238b8a9b1dSJustin T. Gibbs 		if (serial_buf != NULL) {
58248b8a9b1dSJustin T. Gibbs 			scsi_inquiry(csio,
58258b8a9b1dSJustin T. Gibbs 				     /*retries*/4,
58268b8a9b1dSJustin T. Gibbs 				     probedone,
58278b8a9b1dSJustin T. Gibbs 				     MSG_SIMPLE_Q_TAG,
58288b8a9b1dSJustin T. Gibbs 				     (u_int8_t *)serial_buf,
58298b8a9b1dSJustin T. Gibbs 				     sizeof(*serial_buf),
58308b8a9b1dSJustin T. Gibbs 				     /*evpd*/TRUE,
58318b8a9b1dSJustin T. Gibbs 				     SVPD_UNIT_SERIAL_NUMBER,
58328b8a9b1dSJustin T. Gibbs 				     SSD_MIN_SIZE,
5833e471e974SJustin T. Gibbs 				     /*timeout*/60 * 1000);
58348b8a9b1dSJustin T. Gibbs 			break;
58358b8a9b1dSJustin T. Gibbs 		}
58368b8a9b1dSJustin T. Gibbs 		/*
58378b8a9b1dSJustin T. Gibbs 		 * We'll have to do without, let our probedone
58388b8a9b1dSJustin T. Gibbs 		 * routine finish up for us.
58398b8a9b1dSJustin T. Gibbs 		 */
58408b8a9b1dSJustin T. Gibbs 		start_ccb->csio.data_ptr = NULL;
58418b8a9b1dSJustin T. Gibbs 		probedone(periph, start_ccb);
58428b8a9b1dSJustin T. Gibbs 		return;
58438b8a9b1dSJustin T. Gibbs 	}
58448b8a9b1dSJustin T. Gibbs 	}
58458b8a9b1dSJustin T. Gibbs 	xpt_action(start_ccb);
58468b8a9b1dSJustin T. Gibbs }
58478b8a9b1dSJustin T. Gibbs 
58488b8a9b1dSJustin T. Gibbs static void
584987cfaf0eSJustin T. Gibbs proberequestdefaultnegotiation(struct cam_periph *periph)
585087cfaf0eSJustin T. Gibbs {
585187cfaf0eSJustin T. Gibbs 	struct ccb_trans_settings cts;
585287cfaf0eSJustin T. Gibbs 
585387cfaf0eSJustin T. Gibbs 	xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
585487cfaf0eSJustin T. Gibbs 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
58553393f8daSKenneth D. Merry 	cts.type = CTS_TYPE_USER_SETTINGS;
585687cfaf0eSJustin T. Gibbs 	xpt_action((union ccb *)&cts);
58570a480cf0SMatt Jacob 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
58580a480cf0SMatt Jacob 		return;
58590a480cf0SMatt Jacob 	}
586087cfaf0eSJustin T. Gibbs 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
58613393f8daSKenneth D. Merry 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
586287cfaf0eSJustin T. Gibbs 	xpt_action((union ccb *)&cts);
586387cfaf0eSJustin T. Gibbs }
586487cfaf0eSJustin T. Gibbs 
58652afca7acSMatt Jacob /*
58662afca7acSMatt Jacob  * Backoff Negotiation Code- only pertinent for SPI devices.
58672afca7acSMatt Jacob  */
58682afca7acSMatt Jacob static int
58692afca7acSMatt Jacob proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
58702afca7acSMatt Jacob {
58712afca7acSMatt Jacob 	struct ccb_trans_settings cts;
58722afca7acSMatt Jacob 	struct ccb_trans_settings_spi *spi;
58732afca7acSMatt Jacob 
58742afca7acSMatt Jacob 	memset(&cts, 0, sizeof (cts));
58752afca7acSMatt Jacob 	xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
58762afca7acSMatt Jacob 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
58772afca7acSMatt Jacob 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
58782afca7acSMatt Jacob 	xpt_action((union ccb *)&cts);
58792afca7acSMatt Jacob 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
58802afca7acSMatt Jacob 		if (bootverbose) {
5881f0d9af51SMatt Jacob 			xpt_print(periph->path,
5882f0d9af51SMatt Jacob 			    "failed to get current device settings\n");
58832afca7acSMatt Jacob 		}
58842afca7acSMatt Jacob 		return (0);
58852afca7acSMatt Jacob 	}
58862afca7acSMatt Jacob 	if (cts.transport != XPORT_SPI) {
58872afca7acSMatt Jacob 		if (bootverbose) {
5888f0d9af51SMatt Jacob 			xpt_print(periph->path, "not SPI transport\n");
58892afca7acSMatt Jacob 		}
58902afca7acSMatt Jacob 		return (0);
58912afca7acSMatt Jacob 	}
58922afca7acSMatt Jacob 	spi = &cts.xport_specific.spi;
58932afca7acSMatt Jacob 
58942afca7acSMatt Jacob 	/*
58952afca7acSMatt Jacob 	 * We cannot renegotiate sync rate if we don't have one.
58962afca7acSMatt Jacob 	 */
58972afca7acSMatt Jacob 	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
58982afca7acSMatt Jacob 		if (bootverbose) {
5899f0d9af51SMatt Jacob 			xpt_print(periph->path, "no sync rate known\n");
59002afca7acSMatt Jacob 		}
59012afca7acSMatt Jacob 		return (0);
59022afca7acSMatt Jacob 	}
59032afca7acSMatt Jacob 
59042afca7acSMatt Jacob 	/*
59052afca7acSMatt Jacob 	 * We'll assert that we don't have to touch PPR options- the
59062afca7acSMatt Jacob 	 * SIM will see what we do with period and offset and adjust
59072afca7acSMatt Jacob 	 * the PPR options as appropriate.
59082afca7acSMatt Jacob 	 */
59092afca7acSMatt Jacob 
59102afca7acSMatt Jacob 	/*
59112afca7acSMatt Jacob 	 * A sync rate with unknown or zero offset is nonsensical.
59122afca7acSMatt Jacob 	 * A sync period of zero means Async.
59132afca7acSMatt Jacob 	 */
59142afca7acSMatt Jacob 	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
59152afca7acSMatt Jacob 	 || spi->sync_offset == 0 || spi->sync_period == 0) {
59162afca7acSMatt Jacob 		if (bootverbose) {
5917f0d9af51SMatt Jacob 			xpt_print(periph->path, "no sync rate available\n");
59182afca7acSMatt Jacob 		}
59192afca7acSMatt Jacob 		return (0);
59202afca7acSMatt Jacob 	}
59212afca7acSMatt Jacob 
59222afca7acSMatt Jacob 	if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
59232afca7acSMatt Jacob 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
59242afca7acSMatt Jacob 		    ("hit async: giving up on DV\n"));
59252afca7acSMatt Jacob 		return (0);
59262afca7acSMatt Jacob 	}
59272afca7acSMatt Jacob 
59282afca7acSMatt Jacob 
59292afca7acSMatt Jacob 	/*
59302afca7acSMatt Jacob 	 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
59312afca7acSMatt Jacob 	 * We don't try to remember 'last' settings to see if the SIM actually
59322afca7acSMatt Jacob 	 * gets into the speed we want to set. We check on the SIM telling
59332afca7acSMatt Jacob 	 * us that a requested speed is bad, but otherwise don't try and
59342afca7acSMatt Jacob 	 * check the speed due to the asynchronous and handshake nature
59352afca7acSMatt Jacob 	 * of speed setting.
59362afca7acSMatt Jacob 	 */
59372afca7acSMatt Jacob 	spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
59382afca7acSMatt Jacob 	for (;;) {
59392afca7acSMatt Jacob 		spi->sync_period++;
59402afca7acSMatt Jacob 		if (spi->sync_period >= 0xf) {
59412afca7acSMatt Jacob 			spi->sync_period = 0;
59422afca7acSMatt Jacob 			spi->sync_offset = 0;
59432afca7acSMatt Jacob 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
59442afca7acSMatt Jacob 			    ("setting to async for DV\n"));
59452afca7acSMatt Jacob 			/*
59462afca7acSMatt Jacob 			 * Once we hit async, we don't want to try
59472afca7acSMatt Jacob 			 * any more settings.
59482afca7acSMatt Jacob 			 */
59492afca7acSMatt Jacob 			device->flags |= CAM_DEV_DV_HIT_BOTTOM;
59502afca7acSMatt Jacob 		} else if (bootverbose) {
59512afca7acSMatt Jacob 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
59522afca7acSMatt Jacob 			    ("DV: period 0x%x\n", spi->sync_period));
59532afca7acSMatt Jacob 			printf("setting period to 0x%x\n", spi->sync_period);
59542afca7acSMatt Jacob 		}
59552afca7acSMatt Jacob 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
59562afca7acSMatt Jacob 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
59572afca7acSMatt Jacob 		xpt_action((union ccb *)&cts);
59582afca7acSMatt Jacob 		if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
59592afca7acSMatt Jacob 			break;
59602afca7acSMatt Jacob 		}
59612afca7acSMatt Jacob 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
59622afca7acSMatt Jacob 		    ("DV: failed to set period 0x%x\n", spi->sync_period));
59632afca7acSMatt Jacob 		if (spi->sync_period == 0) {
59642afca7acSMatt Jacob 			return (0);
59652afca7acSMatt Jacob 		}
59662afca7acSMatt Jacob 	}
59672afca7acSMatt Jacob 	return (1);
59682afca7acSMatt Jacob }
59692afca7acSMatt Jacob 
597087cfaf0eSJustin T. Gibbs static void
59718b8a9b1dSJustin T. Gibbs probedone(struct cam_periph *periph, union ccb *done_ccb)
59728b8a9b1dSJustin T. Gibbs {
59738b8a9b1dSJustin T. Gibbs 	probe_softc *softc;
59748b8a9b1dSJustin T. Gibbs 	struct cam_path *path;
59758b8a9b1dSJustin T. Gibbs 	u_int32_t  priority;
59768b8a9b1dSJustin T. Gibbs 
59778b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
59788b8a9b1dSJustin T. Gibbs 
59798b8a9b1dSJustin T. Gibbs 	softc = (probe_softc *)periph->softc;
59808b8a9b1dSJustin T. Gibbs 	path = done_ccb->ccb_h.path;
59818b8a9b1dSJustin T. Gibbs 	priority = done_ccb->ccb_h.pinfo.priority;
59828b8a9b1dSJustin T. Gibbs 
59838b8a9b1dSJustin T. Gibbs 	switch (softc->action) {
59848b8a9b1dSJustin T. Gibbs 	case PROBE_TUR:
59858b8a9b1dSJustin T. Gibbs 	{
59868b8a9b1dSJustin T. Gibbs 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
59878b8a9b1dSJustin T. Gibbs 
59888b8a9b1dSJustin T. Gibbs 			if (cam_periph_error(done_ccb, 0,
59898b8a9b1dSJustin T. Gibbs 					     SF_NO_PRINT, NULL) == ERESTART)
59908b8a9b1dSJustin T. Gibbs 				return;
59918b8a9b1dSJustin T. Gibbs 			else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
59928b8a9b1dSJustin T. Gibbs 				/* Don't wedge the queue */
59932cefde5fSJustin T. Gibbs 				xpt_release_devq(done_ccb->ccb_h.path,
59942cefde5fSJustin T. Gibbs 						 /*count*/1,
59958b8a9b1dSJustin T. Gibbs 						 /*run_queue*/TRUE);
59968b8a9b1dSJustin T. Gibbs 		}
59978b8a9b1dSJustin T. Gibbs 		softc->action = PROBE_INQUIRY;
59988b8a9b1dSJustin T. Gibbs 		xpt_release_ccb(done_ccb);
59998b8a9b1dSJustin T. Gibbs 		xpt_schedule(periph, priority);
60008b8a9b1dSJustin T. Gibbs 		return;
60018b8a9b1dSJustin T. Gibbs 	}
60028b8a9b1dSJustin T. Gibbs 	case PROBE_INQUIRY:
60039ec5d7cdSMatt Jacob 	case PROBE_FULL_INQUIRY:
60048b8a9b1dSJustin T. Gibbs 	{
60058b8a9b1dSJustin T. Gibbs 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
60068b8a9b1dSJustin T. Gibbs 			struct scsi_inquiry_data *inq_buf;
60078b8a9b1dSJustin T. Gibbs 			u_int8_t periph_qual;
60088b8a9b1dSJustin T. Gibbs 
600987cfaf0eSJustin T. Gibbs 			path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
60108b8a9b1dSJustin T. Gibbs 			inq_buf = &path->device->inq_data;
60118b8a9b1dSJustin T. Gibbs 
60128b8a9b1dSJustin T. Gibbs 			periph_qual = SID_QUAL(inq_buf);
60139ec5d7cdSMatt Jacob 
6014c19cf05dSMatt Jacob 			switch(periph_qual) {
6015c19cf05dSMatt Jacob 			case SID_QUAL_LU_CONNECTED:
6016c19cf05dSMatt Jacob 			{
60179a4f1a4dSJustin T. Gibbs 				u_int8_t len;
6018c19cf05dSMatt Jacob 
60199ec5d7cdSMatt Jacob 				/*
6020c19cf05dSMatt Jacob 				 * We conservatively request only
6021c19cf05dSMatt Jacob 				 * SHORT_INQUIRY_LEN bytes of inquiry
6022c19cf05dSMatt Jacob 				 * information during our first try
6023c19cf05dSMatt Jacob 				 * at sending an INQUIRY. If the device
6024c19cf05dSMatt Jacob 				 * has more information to give,
6025c19cf05dSMatt Jacob 				 * perform a second request specifying
6026c19cf05dSMatt Jacob 				 * the amount of information the device
6027c19cf05dSMatt Jacob 				 * is willing to give.
60289ec5d7cdSMatt Jacob 				 */
60299a4f1a4dSJustin T. Gibbs 				len = inq_buf->additional_length
60309a4f1a4dSJustin T. Gibbs 				    + offsetof(struct scsi_inquiry_data,
60319a4f1a4dSJustin T. Gibbs                                                additional_length) + 1;
6032c19cf05dSMatt Jacob 				if (softc->action == PROBE_INQUIRY
60339a4f1a4dSJustin T. Gibbs 				 && len > SHORT_INQUIRY_LENGTH) {
60345cd818b1SKenneth D. Merry 					softc->action = PROBE_FULL_INQUIRY;
60359ec5d7cdSMatt Jacob 					xpt_release_ccb(done_ccb);
60369ec5d7cdSMatt Jacob 					xpt_schedule(periph, priority);
60379ec5d7cdSMatt Jacob 					return;
60389ec5d7cdSMatt Jacob 				}
60399ec5d7cdSMatt Jacob 
60408b8a9b1dSJustin T. Gibbs 				xpt_find_quirk(path->device);
60418b8a9b1dSJustin T. Gibbs 
60423393f8daSKenneth D. Merry 				xpt_devise_transport(path);
60434e359f5aSMatt Jacob 				if (INQ_DATA_TQ_ENABLED(inq_buf))
60445cd818b1SKenneth D. Merry 					softc->action = PROBE_MODE_SENSE;
60458b8a9b1dSJustin T. Gibbs 				else
60465cd818b1SKenneth D. Merry 					softc->action = PROBE_SERIAL_NUM;
60478b8a9b1dSJustin T. Gibbs 
60485cd818b1SKenneth D. Merry 				path->device->flags &= ~CAM_DEV_UNCONFIGURED;
60498b8a9b1dSJustin T. Gibbs 
60508b8a9b1dSJustin T. Gibbs 				xpt_release_ccb(done_ccb);
60518b8a9b1dSJustin T. Gibbs 				xpt_schedule(periph, priority);
60528b8a9b1dSJustin T. Gibbs 				return;
60538b8a9b1dSJustin T. Gibbs 			}
60548b8a9b1dSJustin T. Gibbs 			default:
60558b8a9b1dSJustin T. Gibbs 				break;
60568b8a9b1dSJustin T. Gibbs 			}
60578b8a9b1dSJustin T. Gibbs 		} else if (cam_periph_error(done_ccb, 0,
60588b8a9b1dSJustin T. Gibbs 					    done_ccb->ccb_h.target_lun > 0
60598b8a9b1dSJustin T. Gibbs 					    ? SF_RETRY_UA|SF_QUIET_IR
60608b8a9b1dSJustin T. Gibbs 					    : SF_RETRY_UA,
60618b8a9b1dSJustin T. Gibbs 					    &softc->saved_ccb) == ERESTART) {
60628b8a9b1dSJustin T. Gibbs 			return;
60638b8a9b1dSJustin T. Gibbs 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
60648b8a9b1dSJustin T. Gibbs 			/* Don't wedge the queue */
60652cefde5fSJustin T. Gibbs 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
60668b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
60678b8a9b1dSJustin T. Gibbs 		}
60688b8a9b1dSJustin T. Gibbs 		/*
60698b8a9b1dSJustin T. Gibbs 		 * If we get to this point, we got an error status back
60708b8a9b1dSJustin T. Gibbs 		 * from the inquiry and the error status doesn't require
60718b8a9b1dSJustin T. Gibbs 		 * automatically retrying the command.  Therefore, the
60728b8a9b1dSJustin T. Gibbs 		 * inquiry failed.  If we had inquiry information before
60738b8a9b1dSJustin T. Gibbs 		 * for this device, but this latest inquiry command failed,
60748b8a9b1dSJustin T. Gibbs 		 * the device has probably gone away.  If this device isn't
60758b8a9b1dSJustin T. Gibbs 		 * already marked unconfigured, notify the peripheral
60768b8a9b1dSJustin T. Gibbs 		 * drivers that this device is no more.
60778b8a9b1dSJustin T. Gibbs 		 */
60788b8a9b1dSJustin T. Gibbs 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
60798b8a9b1dSJustin T. Gibbs 			/* Send the async notification. */
60808b8a9b1dSJustin T. Gibbs 			xpt_async(AC_LOST_DEVICE, path, NULL);
60818b8a9b1dSJustin T. Gibbs 
60828b8a9b1dSJustin T. Gibbs 		xpt_release_ccb(done_ccb);
60838b8a9b1dSJustin T. Gibbs 		break;
60848b8a9b1dSJustin T. Gibbs 	}
60858b8a9b1dSJustin T. Gibbs 	case PROBE_MODE_SENSE:
60868b8a9b1dSJustin T. Gibbs 	{
60878b8a9b1dSJustin T. Gibbs 		struct ccb_scsiio *csio;
60888b8a9b1dSJustin T. Gibbs 		struct scsi_mode_header_6 *mode_hdr;
60898b8a9b1dSJustin T. Gibbs 
60908b8a9b1dSJustin T. Gibbs 		csio = &done_ccb->csio;
60918b8a9b1dSJustin T. Gibbs 		mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
60928b8a9b1dSJustin T. Gibbs 		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
60938b8a9b1dSJustin T. Gibbs 			struct scsi_control_page *page;
60948b8a9b1dSJustin T. Gibbs 			u_int8_t *offset;
60958b8a9b1dSJustin T. Gibbs 
60968b8a9b1dSJustin T. Gibbs 			offset = ((u_int8_t *)&mode_hdr[1])
60978b8a9b1dSJustin T. Gibbs 			    + mode_hdr->blk_desc_len;
60988b8a9b1dSJustin T. Gibbs 			page = (struct scsi_control_page *)offset;
60998b8a9b1dSJustin T. Gibbs 			path->device->queue_flags = page->queue_flags;
61008b8a9b1dSJustin T. Gibbs 		} else if (cam_periph_error(done_ccb, 0,
61018b8a9b1dSJustin T. Gibbs 					    SF_RETRY_UA|SF_NO_PRINT,
61028b8a9b1dSJustin T. Gibbs 					    &softc->saved_ccb) == ERESTART) {
61038b8a9b1dSJustin T. Gibbs 			return;
61048b8a9b1dSJustin T. Gibbs 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
61058b8a9b1dSJustin T. Gibbs 			/* Don't wedge the queue */
61062cefde5fSJustin T. Gibbs 			xpt_release_devq(done_ccb->ccb_h.path,
61072cefde5fSJustin T. Gibbs 					 /*count*/1, /*run_queue*/TRUE);
61088b8a9b1dSJustin T. Gibbs 		}
61098b8a9b1dSJustin T. Gibbs 		xpt_release_ccb(done_ccb);
61100dd50e9bSScott Long 		free(mode_hdr, M_CAMXPT);
61118b8a9b1dSJustin T. Gibbs 		softc->action = PROBE_SERIAL_NUM;
61128b8a9b1dSJustin T. Gibbs 		xpt_schedule(periph, priority);
61138b8a9b1dSJustin T. Gibbs 		return;
61148b8a9b1dSJustin T. Gibbs 	}
61158b8a9b1dSJustin T. Gibbs 	case PROBE_SERIAL_NUM:
61168b8a9b1dSJustin T. Gibbs 	{
61178b8a9b1dSJustin T. Gibbs 		struct ccb_scsiio *csio;
61188b8a9b1dSJustin T. Gibbs 		struct scsi_vpd_unit_serial_number *serial_buf;
61198b8a9b1dSJustin T. Gibbs 		u_int32_t  priority;
61208b8a9b1dSJustin T. Gibbs 		int changed;
61218b8a9b1dSJustin T. Gibbs 		int have_serialnum;
61228b8a9b1dSJustin T. Gibbs 
61238b8a9b1dSJustin T. Gibbs 		changed = 1;
61248b8a9b1dSJustin T. Gibbs 		have_serialnum = 0;
61258b8a9b1dSJustin T. Gibbs 		csio = &done_ccb->csio;
61268b8a9b1dSJustin T. Gibbs 		priority = done_ccb->ccb_h.pinfo.priority;
61278b8a9b1dSJustin T. Gibbs 		serial_buf =
61288b8a9b1dSJustin T. Gibbs 		    (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
61298b8a9b1dSJustin T. Gibbs 
61308b8a9b1dSJustin T. Gibbs 		/* Clean up from previous instance of this device */
61318b8a9b1dSJustin T. Gibbs 		if (path->device->serial_num != NULL) {
6132362abc44STai-hwa Liang 			free(path->device->serial_num, M_CAMXPT);
61338b8a9b1dSJustin T. Gibbs 			path->device->serial_num = NULL;
61348b8a9b1dSJustin T. Gibbs 			path->device->serial_num_len = 0;
61358b8a9b1dSJustin T. Gibbs 		}
61368b8a9b1dSJustin T. Gibbs 
61378b8a9b1dSJustin T. Gibbs 		if (serial_buf == NULL) {
61388b8a9b1dSJustin T. Gibbs 			/*
61398b8a9b1dSJustin T. Gibbs 			 * Don't process the command as it was never sent
61408b8a9b1dSJustin T. Gibbs 			 */
61418b8a9b1dSJustin T. Gibbs 		} else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
61428b8a9b1dSJustin T. Gibbs 			&& (serial_buf->length > 0)) {
61438b8a9b1dSJustin T. Gibbs 
61448b8a9b1dSJustin T. Gibbs 			have_serialnum = 1;
61458b8a9b1dSJustin T. Gibbs 			path->device->serial_num =
61468b8a9b1dSJustin T. Gibbs 				(u_int8_t *)malloc((serial_buf->length + 1),
6147362abc44STai-hwa Liang 						   M_CAMXPT, M_NOWAIT);
61488b8a9b1dSJustin T. Gibbs 			if (path->device->serial_num != NULL) {
61498b8a9b1dSJustin T. Gibbs 				bcopy(serial_buf->serial_num,
61508b8a9b1dSJustin T. Gibbs 				      path->device->serial_num,
61518b8a9b1dSJustin T. Gibbs 				      serial_buf->length);
61528b8a9b1dSJustin T. Gibbs 				path->device->serial_num_len =
61538b8a9b1dSJustin T. Gibbs 				    serial_buf->length;
61548b8a9b1dSJustin T. Gibbs 				path->device->serial_num[serial_buf->length]
61558b8a9b1dSJustin T. Gibbs 				    = '\0';
61568b8a9b1dSJustin T. Gibbs 			}
61578b8a9b1dSJustin T. Gibbs 		} else if (cam_periph_error(done_ccb, 0,
61588b8a9b1dSJustin T. Gibbs 					    SF_RETRY_UA|SF_NO_PRINT,
61598b8a9b1dSJustin T. Gibbs 					    &softc->saved_ccb) == ERESTART) {
61608b8a9b1dSJustin T. Gibbs 			return;
61618b8a9b1dSJustin T. Gibbs 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
61628b8a9b1dSJustin T. Gibbs 			/* Don't wedge the queue */
61632cefde5fSJustin T. Gibbs 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
61648b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
61658b8a9b1dSJustin T. Gibbs 		}
61668b8a9b1dSJustin T. Gibbs 
61678b8a9b1dSJustin T. Gibbs 		/*
61688b8a9b1dSJustin T. Gibbs 		 * Let's see if we have seen this device before.
61698b8a9b1dSJustin T. Gibbs 		 */
61708b8a9b1dSJustin T. Gibbs 		if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
61718b8a9b1dSJustin T. Gibbs 			MD5_CTX context;
61728b8a9b1dSJustin T. Gibbs 			u_int8_t digest[16];
61738b8a9b1dSJustin T. Gibbs 
61748b8a9b1dSJustin T. Gibbs 			MD5Init(&context);
61758b8a9b1dSJustin T. Gibbs 
61768b8a9b1dSJustin T. Gibbs 			MD5Update(&context,
61778b8a9b1dSJustin T. Gibbs 				  (unsigned char *)&path->device->inq_data,
61788b8a9b1dSJustin T. Gibbs 				  sizeof(struct scsi_inquiry_data));
61798b8a9b1dSJustin T. Gibbs 
61808b8a9b1dSJustin T. Gibbs 			if (have_serialnum)
61818b8a9b1dSJustin T. Gibbs 				MD5Update(&context, serial_buf->serial_num,
61828b8a9b1dSJustin T. Gibbs 					  serial_buf->length);
61838b8a9b1dSJustin T. Gibbs 
61848b8a9b1dSJustin T. Gibbs 			MD5Final(digest, &context);
61858b8a9b1dSJustin T. Gibbs 			if (bcmp(softc->digest, digest, 16) == 0)
61868b8a9b1dSJustin T. Gibbs 				changed = 0;
61878b8a9b1dSJustin T. Gibbs 
61888b8a9b1dSJustin T. Gibbs 			/*
61898b8a9b1dSJustin T. Gibbs 			 * XXX Do we need to do a TUR in order to ensure
61908b8a9b1dSJustin T. Gibbs 			 *     that the device really hasn't changed???
61918b8a9b1dSJustin T. Gibbs 			 */
61928b8a9b1dSJustin T. Gibbs 			if ((changed != 0)
61938b8a9b1dSJustin T. Gibbs 			 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
61948b8a9b1dSJustin T. Gibbs 				xpt_async(AC_LOST_DEVICE, path, NULL);
61958b8a9b1dSJustin T. Gibbs 		}
61968b8a9b1dSJustin T. Gibbs 		if (serial_buf != NULL)
61970dd50e9bSScott Long 			free(serial_buf, M_CAMXPT);
61988b8a9b1dSJustin T. Gibbs 
61998b8a9b1dSJustin T. Gibbs 		if (changed != 0) {
62008b8a9b1dSJustin T. Gibbs 			/*
62018b8a9b1dSJustin T. Gibbs 			 * Now that we have all the necessary
62028b8a9b1dSJustin T. Gibbs 			 * information to safely perform transfer
62038b8a9b1dSJustin T. Gibbs 			 * negotiations... Controllers don't perform
62048b8a9b1dSJustin T. Gibbs 			 * any negotiation or tagged queuing until
62058b8a9b1dSJustin T. Gibbs 			 * after the first XPT_SET_TRAN_SETTINGS ccb is
62062afca7acSMatt Jacob 			 * received.  So, on a new device, just retrieve
62078b8a9b1dSJustin T. Gibbs 			 * the user settings, and set them as the current
62088b8a9b1dSJustin T. Gibbs 			 * settings to set the device up.
62098b8a9b1dSJustin T. Gibbs 			 */
621087cfaf0eSJustin T. Gibbs 			proberequestdefaultnegotiation(periph);
62118b8a9b1dSJustin T. Gibbs 			xpt_release_ccb(done_ccb);
62128b8a9b1dSJustin T. Gibbs 
62138b8a9b1dSJustin T. Gibbs 			/*
62148b8a9b1dSJustin T. Gibbs 			 * Perform a TUR to allow the controller to
62158b8a9b1dSJustin T. Gibbs 			 * perform any necessary transfer negotiation.
62168b8a9b1dSJustin T. Gibbs 			 */
62178b8a9b1dSJustin T. Gibbs 			softc->action = PROBE_TUR_FOR_NEGOTIATION;
62188b8a9b1dSJustin T. Gibbs 			xpt_schedule(periph, priority);
62198b8a9b1dSJustin T. Gibbs 			return;
62208b8a9b1dSJustin T. Gibbs 		}
62218b8a9b1dSJustin T. Gibbs 		xpt_release_ccb(done_ccb);
62228b8a9b1dSJustin T. Gibbs 		break;
62238b8a9b1dSJustin T. Gibbs 	}
62248b8a9b1dSJustin T. Gibbs 	case PROBE_TUR_FOR_NEGOTIATION:
62252afca7acSMatt Jacob 	case PROBE_DV_EXIT:
62268b8a9b1dSJustin T. Gibbs 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
62278b8a9b1dSJustin T. Gibbs 			/* Don't wedge the queue */
62282cefde5fSJustin T. Gibbs 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
62298b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
62308b8a9b1dSJustin T. Gibbs 		}
62312afca7acSMatt Jacob 		/*
62322afca7acSMatt Jacob 		 * Do Domain Validation for lun 0 on devices that claim
62332afca7acSMatt Jacob 		 * to support Synchronous Transfer modes.
62342afca7acSMatt Jacob 		 */
62352afca7acSMatt Jacob 	 	if (softc->action == PROBE_TUR_FOR_NEGOTIATION
62362afca7acSMatt Jacob 		 && done_ccb->ccb_h.target_lun == 0
62372afca7acSMatt Jacob 		 && (path->device->inq_data.flags & SID_Sync) != 0
62382afca7acSMatt Jacob                  && (path->device->flags & CAM_DEV_IN_DV) == 0) {
62392afca7acSMatt Jacob 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
62402afca7acSMatt Jacob 			    ("Begin Domain Validation\n"));
62412afca7acSMatt Jacob 			path->device->flags |= CAM_DEV_IN_DV;
62422afca7acSMatt Jacob 			xpt_release_ccb(done_ccb);
62432afca7acSMatt Jacob 			softc->action = PROBE_INQUIRY_BASIC_DV1;
62442afca7acSMatt Jacob 			xpt_schedule(periph, priority);
62452afca7acSMatt Jacob 			return;
62462afca7acSMatt Jacob 		}
62472afca7acSMatt Jacob 		if (softc->action == PROBE_DV_EXIT) {
62482afca7acSMatt Jacob 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
62492afca7acSMatt Jacob 			    ("Leave Domain Validation\n"));
62502afca7acSMatt Jacob 		}
62512afca7acSMatt Jacob 		path->device->flags &=
62522afca7acSMatt Jacob 		    ~(CAM_DEV_UNCONFIGURED|CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
62538b8a9b1dSJustin T. Gibbs 		if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
62548b8a9b1dSJustin T. Gibbs 			/* Inform the XPT that a new device has been found */
62558b8a9b1dSJustin T. Gibbs 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
62568b8a9b1dSJustin T. Gibbs 			xpt_action(done_ccb);
6257df8f9080SJustin T. Gibbs 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
6258df8f9080SJustin T. Gibbs 				  done_ccb);
62598b8a9b1dSJustin T. Gibbs 		}
62608b8a9b1dSJustin T. Gibbs 		xpt_release_ccb(done_ccb);
62618b8a9b1dSJustin T. Gibbs 		break;
62622afca7acSMatt Jacob 	case PROBE_INQUIRY_BASIC_DV1:
62632afca7acSMatt Jacob 	case PROBE_INQUIRY_BASIC_DV2:
62642afca7acSMatt Jacob 	{
62652afca7acSMatt Jacob 		struct scsi_inquiry_data *nbuf;
62662afca7acSMatt Jacob 		struct ccb_scsiio *csio;
62672afca7acSMatt Jacob 
62682afca7acSMatt Jacob 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
62692afca7acSMatt Jacob 			/* Don't wedge the queue */
62702afca7acSMatt Jacob 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
62712afca7acSMatt Jacob 					 /*run_queue*/TRUE);
62722afca7acSMatt Jacob 		}
62732afca7acSMatt Jacob 		csio = &done_ccb->csio;
62742afca7acSMatt Jacob 		nbuf = (struct scsi_inquiry_data *)csio->data_ptr;
62752afca7acSMatt Jacob 		if (bcmp(nbuf, &path->device->inq_data, SHORT_INQUIRY_LENGTH)) {
6276f0d9af51SMatt Jacob 			xpt_print(path,
6277f0d9af51SMatt Jacob 			    "inquiry data fails comparison at DV%d step\n",
62782afca7acSMatt Jacob 			    softc->action == PROBE_INQUIRY_BASIC_DV1? 1 : 2);
62792afca7acSMatt Jacob 			if (proberequestbackoff(periph, path->device)) {
62802afca7acSMatt Jacob 				path->device->flags &= ~CAM_DEV_IN_DV;
62812afca7acSMatt Jacob 				softc->action = PROBE_TUR_FOR_NEGOTIATION;
62822afca7acSMatt Jacob 			} else {
62832afca7acSMatt Jacob 				/* give up */
62842afca7acSMatt Jacob 				softc->action = PROBE_DV_EXIT;
62852afca7acSMatt Jacob 			}
62860dd50e9bSScott Long 			free(nbuf, M_CAMXPT);
62872afca7acSMatt Jacob 			xpt_release_ccb(done_ccb);
62882afca7acSMatt Jacob 			xpt_schedule(periph, priority);
62892afca7acSMatt Jacob 			return;
62902afca7acSMatt Jacob 		}
62910dd50e9bSScott Long 		free(nbuf, M_CAMXPT);
62922afca7acSMatt Jacob 		if (softc->action == PROBE_INQUIRY_BASIC_DV1) {
62932afca7acSMatt Jacob 			softc->action = PROBE_INQUIRY_BASIC_DV2;
62942afca7acSMatt Jacob 			xpt_release_ccb(done_ccb);
62952afca7acSMatt Jacob 			xpt_schedule(periph, priority);
62962afca7acSMatt Jacob 			return;
62972afca7acSMatt Jacob 		}
62982afca7acSMatt Jacob 		if (softc->action == PROBE_DV_EXIT) {
62992afca7acSMatt Jacob 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
63002afca7acSMatt Jacob 			    ("Leave Domain Validation Successfully\n"));
63012afca7acSMatt Jacob 		}
63022afca7acSMatt Jacob 		path->device->flags &=
63032afca7acSMatt Jacob 		    ~(CAM_DEV_UNCONFIGURED|CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
63042afca7acSMatt Jacob 		if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
63052afca7acSMatt Jacob 			/* Inform the XPT that a new device has been found */
63062afca7acSMatt Jacob 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
63072afca7acSMatt Jacob 			xpt_action(done_ccb);
63082afca7acSMatt Jacob 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
63092afca7acSMatt Jacob 				  done_ccb);
63102afca7acSMatt Jacob 		}
63112afca7acSMatt Jacob 		xpt_release_ccb(done_ccb);
63122afca7acSMatt Jacob 		break;
63132afca7acSMatt Jacob 	}
63148b8a9b1dSJustin T. Gibbs 	}
63158b8a9b1dSJustin T. Gibbs 	done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
63168b8a9b1dSJustin T. Gibbs 	TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
63178b8a9b1dSJustin T. Gibbs 	done_ccb->ccb_h.status = CAM_REQ_CMP;
63188b8a9b1dSJustin T. Gibbs 	xpt_done(done_ccb);
63198b8a9b1dSJustin T. Gibbs 	if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
63208b8a9b1dSJustin T. Gibbs 		cam_periph_invalidate(periph);
63218b8a9b1dSJustin T. Gibbs 		cam_periph_release(periph);
63228b8a9b1dSJustin T. Gibbs 	} else {
63238b8a9b1dSJustin T. Gibbs 		probeschedule(periph);
63248b8a9b1dSJustin T. Gibbs 	}
63258b8a9b1dSJustin T. Gibbs }
63268b8a9b1dSJustin T. Gibbs 
63278b8a9b1dSJustin T. Gibbs static void
63288b8a9b1dSJustin T. Gibbs probecleanup(struct cam_periph *periph)
63298b8a9b1dSJustin T. Gibbs {
63300dd50e9bSScott Long 	free(periph->softc, M_CAMXPT);
63318b8a9b1dSJustin T. Gibbs }
63328b8a9b1dSJustin T. Gibbs 
63338b8a9b1dSJustin T. Gibbs static void
63348b8a9b1dSJustin T. Gibbs xpt_find_quirk(struct cam_ed *device)
63358b8a9b1dSJustin T. Gibbs {
63368b8a9b1dSJustin T. Gibbs 	caddr_t	match;
63378b8a9b1dSJustin T. Gibbs 
63388b8a9b1dSJustin T. Gibbs 	match = cam_quirkmatch((caddr_t)&device->inq_data,
63398b8a9b1dSJustin T. Gibbs 			       (caddr_t)xpt_quirk_table,
63408b8a9b1dSJustin T. Gibbs 			       sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
63418b8a9b1dSJustin T. Gibbs 			       sizeof(*xpt_quirk_table), scsi_inquiry_match);
63428b8a9b1dSJustin T. Gibbs 
63438b8a9b1dSJustin T. Gibbs 	if (match == NULL)
63448b8a9b1dSJustin T. Gibbs 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
63458b8a9b1dSJustin T. Gibbs 
63468b8a9b1dSJustin T. Gibbs 	device->quirk = (struct xpt_quirk_entry *)match;
63478b8a9b1dSJustin T. Gibbs }
63488b8a9b1dSJustin T. Gibbs 
63493b87a552SMatt Jacob static int
63503b87a552SMatt Jacob sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS)
63513b87a552SMatt Jacob {
63523b87a552SMatt Jacob 	int error, bool;
63533b87a552SMatt Jacob 
63543b87a552SMatt Jacob 	bool = cam_srch_hi;
63553b87a552SMatt Jacob 	error = sysctl_handle_int(oidp, &bool, sizeof(bool), req);
63563b87a552SMatt Jacob 	if (error != 0 || req->newptr == NULL)
63573b87a552SMatt Jacob 		return (error);
63583b87a552SMatt Jacob 	if (bool == 0 || bool == 1) {
63593b87a552SMatt Jacob 		cam_srch_hi = bool;
63603b87a552SMatt Jacob 		return (0);
63613b87a552SMatt Jacob 	} else {
63623b87a552SMatt Jacob 		return (EINVAL);
63633b87a552SMatt Jacob 	}
63643b87a552SMatt Jacob }
63653b87a552SMatt Jacob 
63663393f8daSKenneth D. Merry 
63673393f8daSKenneth D. Merry static void
63683393f8daSKenneth D. Merry xpt_devise_transport(struct cam_path *path)
63693393f8daSKenneth D. Merry {
63703393f8daSKenneth D. Merry 	struct ccb_pathinq cpi;
63713393f8daSKenneth D. Merry 	struct ccb_trans_settings cts;
63723393f8daSKenneth D. Merry 	struct scsi_inquiry_data *inq_buf;
63733393f8daSKenneth D. Merry 
63743393f8daSKenneth D. Merry 	/* Get transport information from the SIM */
63753393f8daSKenneth D. Merry 	xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
63763393f8daSKenneth D. Merry 	cpi.ccb_h.func_code = XPT_PATH_INQ;
63773393f8daSKenneth D. Merry 	xpt_action((union ccb *)&cpi);
63783393f8daSKenneth D. Merry 
63793393f8daSKenneth D. Merry 	inq_buf = NULL;
63803393f8daSKenneth D. Merry 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
63813393f8daSKenneth D. Merry 		inq_buf = &path->device->inq_data;
63823393f8daSKenneth D. Merry 	path->device->protocol = PROTO_SCSI;
63833393f8daSKenneth D. Merry 	path->device->protocol_version =
63843393f8daSKenneth D. Merry 	    inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
63853393f8daSKenneth D. Merry 	path->device->transport = cpi.transport;
63863393f8daSKenneth D. Merry 	path->device->transport_version = cpi.transport_version;
63873393f8daSKenneth D. Merry 
63883393f8daSKenneth D. Merry 	/*
63893393f8daSKenneth D. Merry 	 * Any device not using SPI3 features should
63903393f8daSKenneth D. Merry 	 * be considered SPI2 or lower.
63913393f8daSKenneth D. Merry 	 */
63923393f8daSKenneth D. Merry 	if (inq_buf != NULL) {
63933393f8daSKenneth D. Merry 		if (path->device->transport == XPORT_SPI
63943393f8daSKenneth D. Merry 		 && (inq_buf->spi3data & SID_SPI_MASK) == 0
63953393f8daSKenneth D. Merry 		 && path->device->transport_version > 2)
63963393f8daSKenneth D. Merry 			path->device->transport_version = 2;
63973393f8daSKenneth D. Merry 	} else {
63983393f8daSKenneth D. Merry 		struct cam_ed* otherdev;
63993393f8daSKenneth D. Merry 
64003393f8daSKenneth D. Merry 		for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
64013393f8daSKenneth D. Merry 		     otherdev != NULL;
64023393f8daSKenneth D. Merry 		     otherdev = TAILQ_NEXT(otherdev, links)) {
64033393f8daSKenneth D. Merry 			if (otherdev != path->device)
64043393f8daSKenneth D. Merry 				break;
64053393f8daSKenneth D. Merry 		}
64063393f8daSKenneth D. Merry 
64073393f8daSKenneth D. Merry 		if (otherdev != NULL) {
64083393f8daSKenneth D. Merry 			/*
64093393f8daSKenneth D. Merry 			 * Initially assume the same versioning as
64103393f8daSKenneth D. Merry 			 * prior luns for this target.
64113393f8daSKenneth D. Merry 			 */
64123393f8daSKenneth D. Merry 			path->device->protocol_version =
64133393f8daSKenneth D. Merry 			    otherdev->protocol_version;
64143393f8daSKenneth D. Merry 			path->device->transport_version =
64153393f8daSKenneth D. Merry 			    otherdev->transport_version;
64163393f8daSKenneth D. Merry 		} else {
64173393f8daSKenneth D. Merry 			/* Until we know better, opt for safty */
64183393f8daSKenneth D. Merry 			path->device->protocol_version = 2;
64193393f8daSKenneth D. Merry 			if (path->device->transport == XPORT_SPI)
64203393f8daSKenneth D. Merry 				path->device->transport_version = 2;
64213393f8daSKenneth D. Merry 			else
64223393f8daSKenneth D. Merry 				path->device->transport_version = 0;
64233393f8daSKenneth D. Merry 		}
64243393f8daSKenneth D. Merry 	}
64253393f8daSKenneth D. Merry 
64263393f8daSKenneth D. Merry 	/*
64273393f8daSKenneth D. Merry 	 * XXX
64283393f8daSKenneth D. Merry 	 * For a device compliant with SPC-2 we should be able
64293393f8daSKenneth D. Merry 	 * to determine the transport version supported by
64303393f8daSKenneth D. Merry 	 * scrutinizing the version descriptors in the
64313393f8daSKenneth D. Merry 	 * inquiry buffer.
64323393f8daSKenneth D. Merry 	 */
64333393f8daSKenneth D. Merry 
64343393f8daSKenneth D. Merry 	/* Tell the controller what we think */
64353393f8daSKenneth D. Merry 	xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
64363393f8daSKenneth D. Merry 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
64373393f8daSKenneth D. Merry 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
64383393f8daSKenneth D. Merry 	cts.transport = path->device->transport;
64393393f8daSKenneth D. Merry 	cts.transport_version = path->device->transport_version;
64403393f8daSKenneth D. Merry 	cts.protocol = path->device->protocol;
64413393f8daSKenneth D. Merry 	cts.protocol_version = path->device->protocol_version;
64423393f8daSKenneth D. Merry 	cts.proto_specific.valid = 0;
64433393f8daSKenneth D. Merry 	cts.xport_specific.valid = 0;
64443393f8daSKenneth D. Merry 	xpt_action((union ccb *)&cts);
64453393f8daSKenneth D. Merry }
64463393f8daSKenneth D. Merry 
64473393f8daSKenneth D. Merry static void
64483393f8daSKenneth D. Merry xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
64493393f8daSKenneth D. Merry 			  int async_update)
64503393f8daSKenneth D. Merry {
64513393f8daSKenneth D. Merry 	struct	ccb_pathinq cpi;
64523393f8daSKenneth D. Merry 	struct	ccb_trans_settings cur_cts;
64533393f8daSKenneth D. Merry 	struct	ccb_trans_settings_scsi *scsi;
64543393f8daSKenneth D. Merry 	struct	ccb_trans_settings_scsi *cur_scsi;
64553393f8daSKenneth D. Merry 	struct	cam_sim *sim;
64563393f8daSKenneth D. Merry 	struct	scsi_inquiry_data *inq_data;
64573393f8daSKenneth D. Merry 
64583393f8daSKenneth D. Merry 	if (device == NULL) {
64593393f8daSKenneth D. Merry 		cts->ccb_h.status = CAM_PATH_INVALID;
64603393f8daSKenneth D. Merry 		xpt_done((union ccb *)cts);
64613393f8daSKenneth D. Merry 		return;
64623393f8daSKenneth D. Merry 	}
64633393f8daSKenneth D. Merry 
64643393f8daSKenneth D. Merry 	if (cts->protocol == PROTO_UNKNOWN
64653393f8daSKenneth D. Merry 	 || cts->protocol == PROTO_UNSPECIFIED) {
64663393f8daSKenneth D. Merry 		cts->protocol = device->protocol;
64673393f8daSKenneth D. Merry 		cts->protocol_version = device->protocol_version;
64683393f8daSKenneth D. Merry 	}
64693393f8daSKenneth D. Merry 
64703393f8daSKenneth D. Merry 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
64713393f8daSKenneth D. Merry 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
64723393f8daSKenneth D. Merry 		cts->protocol_version = device->protocol_version;
64733393f8daSKenneth D. Merry 
64743393f8daSKenneth D. Merry 	if (cts->protocol != device->protocol) {
6475f0d9af51SMatt Jacob 		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
64763393f8daSKenneth D. Merry 		       cts->protocol, device->protocol);
64773393f8daSKenneth D. Merry 		cts->protocol = device->protocol;
64783393f8daSKenneth D. Merry 	}
64793393f8daSKenneth D. Merry 
64803393f8daSKenneth D. Merry 	if (cts->protocol_version > device->protocol_version) {
64813393f8daSKenneth D. Merry 		if (bootverbose) {
6482f0d9af51SMatt Jacob 			xpt_print(cts->ccb_h.path, "Down reving Protocol "
6483f0d9af51SMatt Jacob 			    "Version from %d to %d?\n", cts->protocol_version,
6484f0d9af51SMatt Jacob 			    device->protocol_version);
64853393f8daSKenneth D. Merry 		}
64863393f8daSKenneth D. Merry 		cts->protocol_version = device->protocol_version;
64873393f8daSKenneth D. Merry 	}
64883393f8daSKenneth D. Merry 
64893393f8daSKenneth D. Merry 	if (cts->transport == XPORT_UNKNOWN
64903393f8daSKenneth D. Merry 	 || cts->transport == XPORT_UNSPECIFIED) {
64913393f8daSKenneth D. Merry 		cts->transport = device->transport;
64923393f8daSKenneth D. Merry 		cts->transport_version = device->transport_version;
64933393f8daSKenneth D. Merry 	}
64943393f8daSKenneth D. Merry 
64953393f8daSKenneth D. Merry 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
64963393f8daSKenneth D. Merry 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
64973393f8daSKenneth D. Merry 		cts->transport_version = device->transport_version;
64983393f8daSKenneth D. Merry 
64993393f8daSKenneth D. Merry 	if (cts->transport != device->transport) {
6500f0d9af51SMatt Jacob 		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
65013393f8daSKenneth D. Merry 		    cts->transport, device->transport);
65023393f8daSKenneth D. Merry 		cts->transport = device->transport;
65033393f8daSKenneth D. Merry 	}
65043393f8daSKenneth D. Merry 
65053393f8daSKenneth D. Merry 	if (cts->transport_version > device->transport_version) {
65063393f8daSKenneth D. Merry 		if (bootverbose) {
6507f0d9af51SMatt Jacob 			xpt_print(cts->ccb_h.path, "Down reving Transport "
6508f0d9af51SMatt Jacob 			    "Version from %d to %d?\n", cts->transport_version,
65093393f8daSKenneth D. Merry 			    device->transport_version);
65103393f8daSKenneth D. Merry 		}
65113393f8daSKenneth D. Merry 		cts->transport_version = device->transport_version;
65123393f8daSKenneth D. Merry 	}
65133393f8daSKenneth D. Merry 
65143393f8daSKenneth D. Merry 	sim = cts->ccb_h.path->bus->sim;
65153393f8daSKenneth D. Merry 
65163393f8daSKenneth D. Merry 	/*
65173393f8daSKenneth D. Merry 	 * Nothing more of interest to do unless
65183393f8daSKenneth D. Merry 	 * this is a device connected via the
65193393f8daSKenneth D. Merry 	 * SCSI protocol.
65203393f8daSKenneth D. Merry 	 */
65213393f8daSKenneth D. Merry 	if (cts->protocol != PROTO_SCSI) {
65223393f8daSKenneth D. Merry 		if (async_update == FALSE)
65233393f8daSKenneth D. Merry 			(*(sim->sim_action))(sim, (union ccb *)cts);
65243393f8daSKenneth D. Merry 		return;
65253393f8daSKenneth D. Merry 	}
65263393f8daSKenneth D. Merry 
65273393f8daSKenneth D. Merry 	inq_data = &device->inq_data;
65283393f8daSKenneth D. Merry 	scsi = &cts->proto_specific.scsi;
65293393f8daSKenneth D. Merry 	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
65303393f8daSKenneth D. Merry 	cpi.ccb_h.func_code = XPT_PATH_INQ;
65313393f8daSKenneth D. Merry 	xpt_action((union ccb *)&cpi);
65323393f8daSKenneth D. Merry 
65333393f8daSKenneth D. Merry 	/* SCSI specific sanity checking */
65343393f8daSKenneth D. Merry 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
65354e359f5aSMatt Jacob 	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
65363393f8daSKenneth D. Merry 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
65373393f8daSKenneth D. Merry 	 || (device->quirk->mintags == 0)) {
65383393f8daSKenneth D. Merry 		/*
65393393f8daSKenneth D. Merry 		 * Can't tag on hardware that doesn't support tags,
65403393f8daSKenneth D. Merry 		 * doesn't have it enabled, or has broken tag support.
65413393f8daSKenneth D. Merry 		 */
65423393f8daSKenneth D. Merry 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
65433393f8daSKenneth D. Merry 	}
65443393f8daSKenneth D. Merry 
65453393f8daSKenneth D. Merry 	if (async_update == FALSE) {
65463393f8daSKenneth D. Merry 		/*
65473393f8daSKenneth D. Merry 		 * Perform sanity checking against what the
65483393f8daSKenneth D. Merry 		 * controller and device can do.
65493393f8daSKenneth D. Merry 		 */
65503393f8daSKenneth D. Merry 		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
65513393f8daSKenneth D. Merry 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
65523393f8daSKenneth D. Merry 		cur_cts.type = cts->type;
65533393f8daSKenneth D. Merry 		xpt_action((union ccb *)&cur_cts);
65540a480cf0SMatt Jacob 		if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
65550a480cf0SMatt Jacob 			return;
65560a480cf0SMatt Jacob 		}
65573393f8daSKenneth D. Merry 		cur_scsi = &cur_cts.proto_specific.scsi;
65583393f8daSKenneth D. Merry 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
65593393f8daSKenneth D. Merry 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
65603393f8daSKenneth D. Merry 			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
65613393f8daSKenneth D. Merry 		}
65623393f8daSKenneth D. Merry 		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
65633393f8daSKenneth D. Merry 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
65643393f8daSKenneth D. Merry 	}
65653393f8daSKenneth D. Merry 
65663393f8daSKenneth D. Merry 	/* SPI specific sanity checking */
6567f053d777SMatt Jacob 	if (cts->transport == XPORT_SPI && async_update == FALSE) {
65683393f8daSKenneth D. Merry 		u_int spi3caps;
65693393f8daSKenneth D. Merry 		struct ccb_trans_settings_spi *spi;
65703393f8daSKenneth D. Merry 		struct ccb_trans_settings_spi *cur_spi;
65713393f8daSKenneth D. Merry 
65723393f8daSKenneth D. Merry 		spi = &cts->xport_specific.spi;
65733393f8daSKenneth D. Merry 
65743393f8daSKenneth D. Merry 		cur_spi = &cur_cts.xport_specific.spi;
65753393f8daSKenneth D. Merry 
65763393f8daSKenneth D. Merry 		/* Fill in any gaps in what the user gave us */
65773393f8daSKenneth D. Merry 		if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
65783393f8daSKenneth D. Merry 			spi->sync_period = cur_spi->sync_period;
65793393f8daSKenneth D. Merry 		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
65803393f8daSKenneth D. Merry 			spi->sync_period = 0;
65813393f8daSKenneth D. Merry 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
65823393f8daSKenneth D. Merry 			spi->sync_offset = cur_spi->sync_offset;
65833393f8daSKenneth D. Merry 		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
65843393f8daSKenneth D. Merry 			spi->sync_offset = 0;
65853393f8daSKenneth D. Merry 		if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
65863393f8daSKenneth D. Merry 			spi->ppr_options = cur_spi->ppr_options;
65873393f8daSKenneth D. Merry 		if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
65883393f8daSKenneth D. Merry 			spi->ppr_options = 0;
65893393f8daSKenneth D. Merry 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
65903393f8daSKenneth D. Merry 			spi->bus_width = cur_spi->bus_width;
65913393f8daSKenneth D. Merry 		if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
65923393f8daSKenneth D. Merry 			spi->bus_width = 0;
65933393f8daSKenneth D. Merry 		if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
65943393f8daSKenneth D. Merry 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
65953393f8daSKenneth D. Merry 			spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
65963393f8daSKenneth D. Merry 		}
65973393f8daSKenneth D. Merry 		if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
65983393f8daSKenneth D. Merry 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
65993393f8daSKenneth D. Merry 		if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
66003393f8daSKenneth D. Merry 		  && (inq_data->flags & SID_Sync) == 0
66013393f8daSKenneth D. Merry 		  && cts->type == CTS_TYPE_CURRENT_SETTINGS)
66023393f8daSKenneth D. Merry 		 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6603c144ae3eSMatt Jacob 		 || (spi->sync_offset == 0)
6604c144ae3eSMatt Jacob 		 || (spi->sync_period == 0)) {
66053393f8daSKenneth D. Merry 			/* Force async */
66063393f8daSKenneth D. Merry 			spi->sync_period = 0;
66073393f8daSKenneth D. Merry 			spi->sync_offset = 0;
66083393f8daSKenneth D. Merry 		}
66093393f8daSKenneth D. Merry 
66103393f8daSKenneth D. Merry 		switch (spi->bus_width) {
66113393f8daSKenneth D. Merry 		case MSG_EXT_WDTR_BUS_32_BIT:
66123393f8daSKenneth D. Merry 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
66133393f8daSKenneth D. Merry 			  || (inq_data->flags & SID_WBus32) != 0
66143393f8daSKenneth D. Merry 			  || cts->type == CTS_TYPE_USER_SETTINGS)
66153393f8daSKenneth D. Merry 			 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
66163393f8daSKenneth D. Merry 				break;
66173393f8daSKenneth D. Merry 			/* Fall Through to 16-bit */
66183393f8daSKenneth D. Merry 		case MSG_EXT_WDTR_BUS_16_BIT:
66193393f8daSKenneth D. Merry 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
66203393f8daSKenneth D. Merry 			  || (inq_data->flags & SID_WBus16) != 0
66213393f8daSKenneth D. Merry 			  || cts->type == CTS_TYPE_USER_SETTINGS)
66223393f8daSKenneth D. Merry 			 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
66233393f8daSKenneth D. Merry 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
66243393f8daSKenneth D. Merry 				break;
66253393f8daSKenneth D. Merry 			}
66263393f8daSKenneth D. Merry 			/* Fall Through to 8-bit */
66273393f8daSKenneth D. Merry 		default: /* New bus width?? */
66283393f8daSKenneth D. Merry 		case MSG_EXT_WDTR_BUS_8_BIT:
66293393f8daSKenneth D. Merry 			/* All targets can do this */
66303393f8daSKenneth D. Merry 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
66313393f8daSKenneth D. Merry 			break;
66323393f8daSKenneth D. Merry 		}
66333393f8daSKenneth D. Merry 
66343393f8daSKenneth D. Merry 		spi3caps = cpi.xport_specific.spi.ppr_options;
66353393f8daSKenneth D. Merry 		if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
66363393f8daSKenneth D. Merry 		 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
66373393f8daSKenneth D. Merry 			spi3caps &= inq_data->spi3data;
66383393f8daSKenneth D. Merry 
66393393f8daSKenneth D. Merry 		if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
66403393f8daSKenneth D. Merry 			spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
66413393f8daSKenneth D. Merry 
66423393f8daSKenneth D. Merry 		if ((spi3caps & SID_SPI_IUS) == 0)
66433393f8daSKenneth D. Merry 			spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
66443393f8daSKenneth D. Merry 
66453393f8daSKenneth D. Merry 		if ((spi3caps & SID_SPI_QAS) == 0)
66463393f8daSKenneth D. Merry 			spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
66473393f8daSKenneth D. Merry 
66483393f8daSKenneth D. Merry 		/* No SPI Transfer settings are allowed unless we are wide */
66493393f8daSKenneth D. Merry 		if (spi->bus_width == 0)
66503393f8daSKenneth D. Merry 			spi->ppr_options = 0;
66513393f8daSKenneth D. Merry 
66523393f8daSKenneth D. Merry 		if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0) {
66533393f8daSKenneth D. Merry 			/*
66543393f8daSKenneth D. Merry 			 * Can't tag queue without disconnection.
66553393f8daSKenneth D. Merry 			 */
66563393f8daSKenneth D. Merry 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
66573393f8daSKenneth D. Merry 			scsi->valid |= CTS_SCSI_VALID_TQ;
66583393f8daSKenneth D. Merry 		}
66593393f8daSKenneth D. Merry 
66603393f8daSKenneth D. Merry 		/*
66613393f8daSKenneth D. Merry 		 * If we are currently performing tagged transactions to
66623393f8daSKenneth D. Merry 		 * this device and want to change its negotiation parameters,
66633393f8daSKenneth D. Merry 		 * go non-tagged for a bit to give the controller a chance to
66643393f8daSKenneth D. Merry 		 * negotiate unhampered by tag messages.
66653393f8daSKenneth D. Merry 		 */
66663393f8daSKenneth D. Merry 		if (cts->type == CTS_TYPE_CURRENT_SETTINGS
66673393f8daSKenneth D. Merry 		 && (device->inq_flags & SID_CmdQue) != 0
66683393f8daSKenneth D. Merry 		 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
66693393f8daSKenneth D. Merry 		 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
66703393f8daSKenneth D. Merry 				   CTS_SPI_VALID_SYNC_OFFSET|
66713393f8daSKenneth D. Merry 				   CTS_SPI_VALID_BUS_WIDTH)) != 0)
66723393f8daSKenneth D. Merry 			xpt_toggle_tags(cts->ccb_h.path);
66733393f8daSKenneth D. Merry 	}
66743393f8daSKenneth D. Merry 
66753393f8daSKenneth D. Merry 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
66763393f8daSKenneth D. Merry 	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
66773393f8daSKenneth D. Merry 		int device_tagenb;
66783393f8daSKenneth D. Merry 
66793393f8daSKenneth D. Merry 		/*
66803393f8daSKenneth D. Merry 		 * If we are transitioning from tags to no-tags or
66813393f8daSKenneth D. Merry 		 * vice-versa, we need to carefully freeze and restart
66823393f8daSKenneth D. Merry 		 * the queue so that we don't overlap tagged and non-tagged
66833393f8daSKenneth D. Merry 		 * commands.  We also temporarily stop tags if there is
66843393f8daSKenneth D. Merry 		 * a change in transfer negotiation settings to allow
66853393f8daSKenneth D. Merry 		 * "tag-less" negotiation.
66863393f8daSKenneth D. Merry 		 */
66873393f8daSKenneth D. Merry 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
66883393f8daSKenneth D. Merry 		 || (device->inq_flags & SID_CmdQue) != 0)
66893393f8daSKenneth D. Merry 			device_tagenb = TRUE;
66903393f8daSKenneth D. Merry 		else
66913393f8daSKenneth D. Merry 			device_tagenb = FALSE;
66923393f8daSKenneth D. Merry 
66933393f8daSKenneth D. Merry 		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
66943393f8daSKenneth D. Merry 		  && device_tagenb == FALSE)
66953393f8daSKenneth D. Merry 		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
66963393f8daSKenneth D. Merry 		  && device_tagenb == TRUE)) {
66973393f8daSKenneth D. Merry 
66983393f8daSKenneth D. Merry 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
66993393f8daSKenneth D. Merry 				/*
67003393f8daSKenneth D. Merry 				 * Delay change to use tags until after a
67013393f8daSKenneth D. Merry 				 * few commands have gone to this device so
67023393f8daSKenneth D. Merry 				 * the controller has time to perform transfer
67033393f8daSKenneth D. Merry 				 * negotiations without tagged messages getting
67043393f8daSKenneth D. Merry 				 * in the way.
67053393f8daSKenneth D. Merry 				 */
67063393f8daSKenneth D. Merry 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
67073393f8daSKenneth D. Merry 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
67083393f8daSKenneth D. Merry 			} else {
67093393f8daSKenneth D. Merry 				struct ccb_relsim crs;
67103393f8daSKenneth D. Merry 
67113393f8daSKenneth D. Merry 				xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
67123393f8daSKenneth D. Merry 		  		device->inq_flags &= ~SID_CmdQue;
67133393f8daSKenneth D. Merry 				xpt_dev_ccbq_resize(cts->ccb_h.path,
67143393f8daSKenneth D. Merry 						    sim->max_dev_openings);
67153393f8daSKenneth D. Merry 				device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
67163393f8daSKenneth D. Merry 				device->tag_delay_count = 0;
67173393f8daSKenneth D. Merry 
67183393f8daSKenneth D. Merry 				xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
67193393f8daSKenneth D. Merry 					      /*priority*/1);
67203393f8daSKenneth D. Merry 				crs.ccb_h.func_code = XPT_REL_SIMQ;
67213393f8daSKenneth D. Merry 				crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
67223393f8daSKenneth D. Merry 				crs.openings
67233393f8daSKenneth D. Merry 				    = crs.release_timeout
67243393f8daSKenneth D. Merry 				    = crs.qfrozen_cnt
67253393f8daSKenneth D. Merry 				    = 0;
67263393f8daSKenneth D. Merry 				xpt_action((union ccb *)&crs);
67273393f8daSKenneth D. Merry 			}
67283393f8daSKenneth D. Merry 		}
67293393f8daSKenneth D. Merry 	}
67303393f8daSKenneth D. Merry 	if (async_update == FALSE)
67313393f8daSKenneth D. Merry 		(*(sim->sim_action))(sim, (union ccb *)cts);
67323393f8daSKenneth D. Merry }
67333393f8daSKenneth D. Merry 
67343393f8daSKenneth D. Merry 
6735fd21cc5eSJustin T. Gibbs static void
6736f0adc790SJustin T. Gibbs xpt_toggle_tags(struct cam_path *path)
6737f0adc790SJustin T. Gibbs {
673887cfaf0eSJustin T. Gibbs 	struct cam_ed *dev;
673987cfaf0eSJustin T. Gibbs 
6740f0adc790SJustin T. Gibbs 	/*
6741f0adc790SJustin T. Gibbs 	 * Give controllers a chance to renegotiate
6742f0adc790SJustin T. Gibbs 	 * before starting tag operations.  We
6743f0adc790SJustin T. Gibbs 	 * "toggle" tagged queuing off then on
6744f0adc790SJustin T. Gibbs 	 * which causes the tag enable command delay
6745f0adc790SJustin T. Gibbs 	 * counter to come into effect.
6746f0adc790SJustin T. Gibbs 	 */
674787cfaf0eSJustin T. Gibbs 	dev = path->device;
674887cfaf0eSJustin T. Gibbs 	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
674987cfaf0eSJustin T. Gibbs 	 || ((dev->inq_flags & SID_CmdQue) != 0
675087cfaf0eSJustin T. Gibbs  	  && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
6751f0adc790SJustin T. Gibbs 		struct ccb_trans_settings cts;
6752f0adc790SJustin T. Gibbs 
6753f0adc790SJustin T. Gibbs 		xpt_setup_ccb(&cts.ccb_h, path, 1);
67543393f8daSKenneth D. Merry 		cts.protocol = PROTO_SCSI;
67553393f8daSKenneth D. Merry 		cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
67563393f8daSKenneth D. Merry 		cts.transport = XPORT_UNSPECIFIED;
67573393f8daSKenneth D. Merry 		cts.transport_version = XPORT_VERSION_UNSPECIFIED;
67583393f8daSKenneth D. Merry 		cts.proto_specific.scsi.flags = 0;
67593393f8daSKenneth D. Merry 		cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
676003e3511bSJustin T. Gibbs 		xpt_set_transfer_settings(&cts, path->device,
676103e3511bSJustin T. Gibbs 					  /*async_update*/TRUE);
67623393f8daSKenneth D. Merry 		cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
676303e3511bSJustin T. Gibbs 		xpt_set_transfer_settings(&cts, path->device,
676403e3511bSJustin T. Gibbs 					  /*async_update*/TRUE);
6765f0adc790SJustin T. Gibbs 	}
6766f0adc790SJustin T. Gibbs }
6767f0adc790SJustin T. Gibbs 
6768f0adc790SJustin T. Gibbs static void
6769f0adc790SJustin T. Gibbs xpt_start_tags(struct cam_path *path)
6770f0adc790SJustin T. Gibbs {
6771fd21cc5eSJustin T. Gibbs 	struct ccb_relsim crs;
6772fd21cc5eSJustin T. Gibbs 	struct cam_ed *device;
6773fd21cc5eSJustin T. Gibbs 	struct cam_sim *sim;
6774fd21cc5eSJustin T. Gibbs 	int    newopenings;
6775fd21cc5eSJustin T. Gibbs 
6776fd21cc5eSJustin T. Gibbs 	device = path->device;
6777fd21cc5eSJustin T. Gibbs 	sim = path->bus->sim;
6778fd21cc5eSJustin T. Gibbs 	device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6779fd21cc5eSJustin T. Gibbs 	xpt_freeze_devq(path, /*count*/1);
6780fd21cc5eSJustin T. Gibbs 	device->inq_flags |= SID_CmdQue;
6781df8f9080SJustin T. Gibbs 	if (device->tag_saved_openings != 0)
6782df8f9080SJustin T. Gibbs 		newopenings = device->tag_saved_openings;
6783df8f9080SJustin T. Gibbs 	else
6784df8f9080SJustin T. Gibbs 		newopenings = min(device->quirk->maxtags,
6785df8f9080SJustin T. Gibbs 				  sim->max_tagged_dev_openings);
6786f0adc790SJustin T. Gibbs 	xpt_dev_ccbq_resize(path, newopenings);
6787fd21cc5eSJustin T. Gibbs 	xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6788fd21cc5eSJustin T. Gibbs 	crs.ccb_h.func_code = XPT_REL_SIMQ;
6789fd21cc5eSJustin T. Gibbs 	crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6790fd21cc5eSJustin T. Gibbs 	crs.openings
6791fd21cc5eSJustin T. Gibbs 	    = crs.release_timeout
6792fd21cc5eSJustin T. Gibbs 	    = crs.qfrozen_cnt
6793fd21cc5eSJustin T. Gibbs 	    = 0;
6794fd21cc5eSJustin T. Gibbs 	xpt_action((union ccb *)&crs);
6795fd21cc5eSJustin T. Gibbs }
6796fd21cc5eSJustin T. Gibbs 
67978b8a9b1dSJustin T. Gibbs static int busses_to_config;
679887cfaf0eSJustin T. Gibbs static int busses_to_reset;
67998b8a9b1dSJustin T. Gibbs 
68008b8a9b1dSJustin T. Gibbs static int
68018b8a9b1dSJustin T. Gibbs xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
68028b8a9b1dSJustin T. Gibbs {
68032b83592fSScott Long 
68042b83592fSScott Long 	mtx_assert(bus->sim->mtx, MA_OWNED);
68052b83592fSScott Long 
680687cfaf0eSJustin T. Gibbs 	if (bus->path_id != CAM_XPT_PATH_ID) {
680787cfaf0eSJustin T. Gibbs 		struct cam_path path;
680887cfaf0eSJustin T. Gibbs 		struct ccb_pathinq cpi;
680987cfaf0eSJustin T. Gibbs 		int can_negotiate;
681087cfaf0eSJustin T. Gibbs 
68118b8a9b1dSJustin T. Gibbs 		busses_to_config++;
681287cfaf0eSJustin T. Gibbs 		xpt_compile_path(&path, NULL, bus->path_id,
681387cfaf0eSJustin T. Gibbs 				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
681487cfaf0eSJustin T. Gibbs 		xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
681587cfaf0eSJustin T. Gibbs 		cpi.ccb_h.func_code = XPT_PATH_INQ;
681687cfaf0eSJustin T. Gibbs 		xpt_action((union ccb *)&cpi);
681787cfaf0eSJustin T. Gibbs 		can_negotiate = cpi.hba_inquiry;
681887cfaf0eSJustin T. Gibbs 		can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
681987cfaf0eSJustin T. Gibbs 		if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
682087cfaf0eSJustin T. Gibbs 		 && can_negotiate)
682187cfaf0eSJustin T. Gibbs 			busses_to_reset++;
682287cfaf0eSJustin T. Gibbs 		xpt_release_path(&path);
682387cfaf0eSJustin T. Gibbs 	}
68248b8a9b1dSJustin T. Gibbs 
68258b8a9b1dSJustin T. Gibbs 	return(1);
68268b8a9b1dSJustin T. Gibbs }
68278b8a9b1dSJustin T. Gibbs 
68288b8a9b1dSJustin T. Gibbs static int
68298b8a9b1dSJustin T. Gibbs xptconfigfunc(struct cam_eb *bus, void *arg)
68308b8a9b1dSJustin T. Gibbs {
68318b8a9b1dSJustin T. Gibbs 	struct	cam_path *path;
68328b8a9b1dSJustin T. Gibbs 	union	ccb *work_ccb;
68338b8a9b1dSJustin T. Gibbs 
68342b83592fSScott Long 	mtx_assert(bus->sim->mtx, MA_OWNED);
68352b83592fSScott Long 
68368b8a9b1dSJustin T. Gibbs 	if (bus->path_id != CAM_XPT_PATH_ID) {
68378b8a9b1dSJustin T. Gibbs 		cam_status status;
683887cfaf0eSJustin T. Gibbs 		int can_negotiate;
68398b8a9b1dSJustin T. Gibbs 
68408008a935SScott Long 		work_ccb = xpt_alloc_ccb_nowait();
68418008a935SScott Long 		if (work_ccb == NULL) {
68428008a935SScott Long 			busses_to_config--;
68438008a935SScott Long 			xpt_finishconfig(xpt_periph, NULL);
68448008a935SScott Long 			return(0);
68458008a935SScott Long 		}
68468b8a9b1dSJustin T. Gibbs 		if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
68478b8a9b1dSJustin T. Gibbs 					      CAM_TARGET_WILDCARD,
68488b8a9b1dSJustin T. Gibbs 					      CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
68498b8a9b1dSJustin T. Gibbs 			printf("xptconfigfunc: xpt_create_path failed with "
68508b8a9b1dSJustin T. Gibbs 			       "status %#x for bus %d\n", status, bus->path_id);
68518b8a9b1dSJustin T. Gibbs 			printf("xptconfigfunc: halting bus configuration\n");
68528b8a9b1dSJustin T. Gibbs 			xpt_free_ccb(work_ccb);
685398192658SJustin T. Gibbs 			busses_to_config--;
685498192658SJustin T. Gibbs 			xpt_finishconfig(xpt_periph, NULL);
68558b8a9b1dSJustin T. Gibbs 			return(0);
68568b8a9b1dSJustin T. Gibbs 		}
68578b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
685898192658SJustin T. Gibbs 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
685998192658SJustin T. Gibbs 		xpt_action(work_ccb);
686098192658SJustin T. Gibbs 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
686198192658SJustin T. Gibbs 			printf("xptconfigfunc: CPI failed on bus %d "
686298192658SJustin T. Gibbs 			       "with status %d\n", bus->path_id,
686398192658SJustin T. Gibbs 			       work_ccb->ccb_h.status);
686498192658SJustin T. Gibbs 			xpt_finishconfig(xpt_periph, work_ccb);
686598192658SJustin T. Gibbs 			return(1);
686698192658SJustin T. Gibbs 		}
686798192658SJustin T. Gibbs 
686887cfaf0eSJustin T. Gibbs 		can_negotiate = work_ccb->cpi.hba_inquiry;
686987cfaf0eSJustin T. Gibbs 		can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
687087cfaf0eSJustin T. Gibbs 		if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
687187cfaf0eSJustin T. Gibbs 		 && (can_negotiate != 0)) {
687298192658SJustin T. Gibbs 			xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
68738b8a9b1dSJustin T. Gibbs 			work_ccb->ccb_h.func_code = XPT_RESET_BUS;
68748b8a9b1dSJustin T. Gibbs 			work_ccb->ccb_h.cbfcnp = NULL;
68758b8a9b1dSJustin T. Gibbs 			CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
68768b8a9b1dSJustin T. Gibbs 				  ("Resetting Bus\n"));
68778b8a9b1dSJustin T. Gibbs 			xpt_action(work_ccb);
68788b8a9b1dSJustin T. Gibbs 			xpt_finishconfig(xpt_periph, work_ccb);
687998192658SJustin T. Gibbs 		} else {
688098192658SJustin T. Gibbs 			/* Act as though we performed a successful BUS RESET */
688198192658SJustin T. Gibbs 			work_ccb->ccb_h.func_code = XPT_RESET_BUS;
688298192658SJustin T. Gibbs 			xpt_finishconfig(xpt_periph, work_ccb);
688398192658SJustin T. Gibbs 		}
68848b8a9b1dSJustin T. Gibbs 	}
68858b8a9b1dSJustin T. Gibbs 
68868b8a9b1dSJustin T. Gibbs 	return(1);
68878b8a9b1dSJustin T. Gibbs }
68888b8a9b1dSJustin T. Gibbs 
68898b8a9b1dSJustin T. Gibbs static void
68908b8a9b1dSJustin T. Gibbs xpt_config(void *arg)
68918b8a9b1dSJustin T. Gibbs {
68923393f8daSKenneth D. Merry 	/*
68933393f8daSKenneth D. Merry 	 * Now that interrupts are enabled, go find our devices
68943393f8daSKenneth D. Merry 	 */
68958b8a9b1dSJustin T. Gibbs 
68968b8a9b1dSJustin T. Gibbs #ifdef CAMDEBUG
68978b8a9b1dSJustin T. Gibbs 	/* Setup debugging flags and path */
68988b8a9b1dSJustin T. Gibbs #ifdef CAM_DEBUG_FLAGS
68998b8a9b1dSJustin T. Gibbs 	cam_dflags = CAM_DEBUG_FLAGS;
69008b8a9b1dSJustin T. Gibbs #else /* !CAM_DEBUG_FLAGS */
69018b8a9b1dSJustin T. Gibbs 	cam_dflags = CAM_DEBUG_NONE;
69028b8a9b1dSJustin T. Gibbs #endif /* CAM_DEBUG_FLAGS */
69038b8a9b1dSJustin T. Gibbs #ifdef CAM_DEBUG_BUS
69048b8a9b1dSJustin T. Gibbs 	if (cam_dflags != CAM_DEBUG_NONE) {
69052b83592fSScott Long 		/*
69062b83592fSScott Long 		 * Locking is specifically omitted here.  No SIMs have
69072b83592fSScott Long 		 * registered yet, so xpt_create_path will only be searching
69082b83592fSScott Long 		 * empty lists of targets and devices.
69092b83592fSScott Long 		 */
69108b8a9b1dSJustin T. Gibbs 		if (xpt_create_path(&cam_dpath, xpt_periph,
69118b8a9b1dSJustin T. Gibbs 				    CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
69128b8a9b1dSJustin T. Gibbs 				    CAM_DEBUG_LUN) != CAM_REQ_CMP) {
69138b8a9b1dSJustin T. Gibbs 			printf("xpt_config: xpt_create_path() failed for debug"
69148b8a9b1dSJustin T. Gibbs 			       " target %d:%d:%d, debugging disabled\n",
69158b8a9b1dSJustin T. Gibbs 			       CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
69168b8a9b1dSJustin T. Gibbs 			cam_dflags = CAM_DEBUG_NONE;
69178b8a9b1dSJustin T. Gibbs 		}
69188b8a9b1dSJustin T. Gibbs 	} else
69198b8a9b1dSJustin T. Gibbs 		cam_dpath = NULL;
69208b8a9b1dSJustin T. Gibbs #else /* !CAM_DEBUG_BUS */
69218b8a9b1dSJustin T. Gibbs 	cam_dpath = NULL;
69228b8a9b1dSJustin T. Gibbs #endif /* CAM_DEBUG_BUS */
69238b8a9b1dSJustin T. Gibbs #endif /* CAMDEBUG */
69248b8a9b1dSJustin T. Gibbs 
6925a5479bc5SJustin T. Gibbs 	/*
6926a5479bc5SJustin T. Gibbs 	 * Scan all installed busses.
6927a5479bc5SJustin T. Gibbs 	 */
69288b8a9b1dSJustin T. Gibbs 	xpt_for_all_busses(xptconfigbuscountfunc, NULL);
69298b8a9b1dSJustin T. Gibbs 
6930ecdf1113SJustin T. Gibbs 	if (busses_to_config == 0) {
6931ecdf1113SJustin T. Gibbs 		/* Call manually because we don't have any busses */
69328b8a9b1dSJustin T. Gibbs 		xpt_finishconfig(xpt_periph, NULL);
69332863f7b1SJustin T. Gibbs 	} else  {
69343a937198SBrooks Davis 		if (busses_to_reset > 0 && scsi_delay >= 2000) {
69352863f7b1SJustin T. Gibbs 			printf("Waiting %d seconds for SCSI "
69363a937198SBrooks Davis 			       "devices to settle\n", scsi_delay/1000);
69372863f7b1SJustin T. Gibbs 		}
6938ecdf1113SJustin T. Gibbs 		xpt_for_all_busses(xptconfigfunc, NULL);
69398b8a9b1dSJustin T. Gibbs 	}
69402863f7b1SJustin T. Gibbs }
69418b8a9b1dSJustin T. Gibbs 
69428b8a9b1dSJustin T. Gibbs /*
69438b8a9b1dSJustin T. Gibbs  * If the given device only has one peripheral attached to it, and if that
69448b8a9b1dSJustin T. Gibbs  * peripheral is the passthrough driver, announce it.  This insures that the
69458b8a9b1dSJustin T. Gibbs  * user sees some sort of announcement for every peripheral in their system.
69468b8a9b1dSJustin T. Gibbs  */
69478b8a9b1dSJustin T. Gibbs static int
69488b8a9b1dSJustin T. Gibbs xptpassannouncefunc(struct cam_ed *device, void *arg)
69498b8a9b1dSJustin T. Gibbs {
69508b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph;
69518b8a9b1dSJustin T. Gibbs 	int i;
69528b8a9b1dSJustin T. Gibbs 
69538b8a9b1dSJustin T. Gibbs 	for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
69548b8a9b1dSJustin T. Gibbs 	     periph = SLIST_NEXT(periph, periph_links), i++);
69558b8a9b1dSJustin T. Gibbs 
69568b8a9b1dSJustin T. Gibbs 	periph = SLIST_FIRST(&device->periphs);
69578b8a9b1dSJustin T. Gibbs 	if ((i == 1)
69588b8a9b1dSJustin T. Gibbs 	 && (strncmp(periph->periph_name, "pass", 4) == 0))
69598b8a9b1dSJustin T. Gibbs 		xpt_announce_periph(periph, NULL);
69608b8a9b1dSJustin T. Gibbs 
69618b8a9b1dSJustin T. Gibbs 	return(1);
69628b8a9b1dSJustin T. Gibbs }
69638b8a9b1dSJustin T. Gibbs 
69648b8a9b1dSJustin T. Gibbs static void
69652b83592fSScott Long xpt_finishconfig_task(void *context, int pending)
69668b8a9b1dSJustin T. Gibbs {
69678b8a9b1dSJustin T. Gibbs 	struct	periph_driver **p_drv;
69688b8a9b1dSJustin T. Gibbs 	int	i;
69698b8a9b1dSJustin T. Gibbs 
69702b83592fSScott Long 	if (busses_to_config == 0) {
69712b83592fSScott Long 		/* Register all the peripheral drivers */
69722b83592fSScott Long 		/* XXX This will have to change when we have loadable modules */
69732b83592fSScott Long 		p_drv = periph_drivers;
69742b83592fSScott Long 		for (i = 0; p_drv[i] != NULL; i++) {
69752b83592fSScott Long 			(*p_drv[i]->init)();
69762b83592fSScott Long 		}
69772b83592fSScott Long 
69782b83592fSScott Long 		/*
69792b83592fSScott Long 		 * Check for devices with no "standard" peripheral driver
69802b83592fSScott Long 		 * attached.  For any devices like that, announce the
69812b83592fSScott Long 		 * passthrough driver so the user will see something.
69822b83592fSScott Long 		 */
69832b83592fSScott Long 		xpt_for_all_devices(xptpassannouncefunc, NULL);
69842b83592fSScott Long 
69852b83592fSScott Long 		/* Release our hook so that the boot can continue. */
69862b83592fSScott Long 		config_intrhook_disestablish(xsoftc.xpt_config_hook);
69870dd50e9bSScott Long 		free(xsoftc.xpt_config_hook, M_CAMXPT);
69882b83592fSScott Long 		xsoftc.xpt_config_hook = NULL;
69892b83592fSScott Long 	}
69902b83592fSScott Long 
69912b83592fSScott Long 	free(context, M_CAMXPT);
69922b83592fSScott Long }
69932b83592fSScott Long 
69942b83592fSScott Long static void
69952b83592fSScott Long xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
69962b83592fSScott Long {
69972b83592fSScott Long 	struct	xpt_task *task;
69982b83592fSScott Long 
69998b8a9b1dSJustin T. Gibbs 	if (done_ccb != NULL) {
70008b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
70018b8a9b1dSJustin T. Gibbs 			  ("xpt_finishconfig\n"));
70028b8a9b1dSJustin T. Gibbs 		switch(done_ccb->ccb_h.func_code) {
70038b8a9b1dSJustin T. Gibbs 		case XPT_RESET_BUS:
70048b8a9b1dSJustin T. Gibbs 			if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
70058b8a9b1dSJustin T. Gibbs 				done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
70068b8a9b1dSJustin T. Gibbs 				done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
7007df8f9080SJustin T. Gibbs 				done_ccb->crcn.flags = 0;
70088b8a9b1dSJustin T. Gibbs 				xpt_action(done_ccb);
70098b8a9b1dSJustin T. Gibbs 				return;
70108b8a9b1dSJustin T. Gibbs 			}
70118b8a9b1dSJustin T. Gibbs 			/* FALLTHROUGH */
70128b8a9b1dSJustin T. Gibbs 		case XPT_SCAN_BUS:
701398192658SJustin T. Gibbs 		default:
70148b8a9b1dSJustin T. Gibbs 			xpt_free_path(done_ccb->ccb_h.path);
70158b8a9b1dSJustin T. Gibbs 			busses_to_config--;
70168b8a9b1dSJustin T. Gibbs 			break;
70178b8a9b1dSJustin T. Gibbs 		}
70188b8a9b1dSJustin T. Gibbs 	}
70198b8a9b1dSJustin T. Gibbs 
70202524e4a8SScott Long 	if (busses_to_config == 0) {
70212b83592fSScott Long 		task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
70222b83592fSScott Long 		if (task != NULL) {
70232b83592fSScott Long 			TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
70242b83592fSScott Long 			taskqueue_enqueue(taskqueue_thread, &task->task);
70258b8a9b1dSJustin T. Gibbs 		}
70262524e4a8SScott Long 	}
70278b8a9b1dSJustin T. Gibbs 
70288b8a9b1dSJustin T. Gibbs 	if (done_ccb != NULL)
70298b8a9b1dSJustin T. Gibbs 		xpt_free_ccb(done_ccb);
70308b8a9b1dSJustin T. Gibbs }
70318b8a9b1dSJustin T. Gibbs 
70328b8a9b1dSJustin T. Gibbs static void
70338b8a9b1dSJustin T. Gibbs xptaction(struct cam_sim *sim, union ccb *work_ccb)
70348b8a9b1dSJustin T. Gibbs {
70358b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
70368b8a9b1dSJustin T. Gibbs 
70378b8a9b1dSJustin T. Gibbs 	switch (work_ccb->ccb_h.func_code) {
70388b8a9b1dSJustin T. Gibbs 	/* Common cases first */
70398b8a9b1dSJustin T. Gibbs 	case XPT_PATH_INQ:		/* Path routing inquiry */
70408b8a9b1dSJustin T. Gibbs 	{
70418b8a9b1dSJustin T. Gibbs 		struct ccb_pathinq *cpi;
70428b8a9b1dSJustin T. Gibbs 
70438b8a9b1dSJustin T. Gibbs 		cpi = &work_ccb->cpi;
70448b8a9b1dSJustin T. Gibbs 		cpi->version_num = 1; /* XXX??? */
70458b8a9b1dSJustin T. Gibbs 		cpi->hba_inquiry = 0;
70468b8a9b1dSJustin T. Gibbs 		cpi->target_sprt = 0;
70478b8a9b1dSJustin T. Gibbs 		cpi->hba_misc = 0;
70488b8a9b1dSJustin T. Gibbs 		cpi->hba_eng_cnt = 0;
70498b8a9b1dSJustin T. Gibbs 		cpi->max_target = 0;
70508b8a9b1dSJustin T. Gibbs 		cpi->max_lun = 0;
70518b8a9b1dSJustin T. Gibbs 		cpi->initiator_id = 0;
70528b8a9b1dSJustin T. Gibbs 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
70538b8a9b1dSJustin T. Gibbs 		strncpy(cpi->hba_vid, "", HBA_IDLEN);
70548b8a9b1dSJustin T. Gibbs 		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
70558b8a9b1dSJustin T. Gibbs 		cpi->unit_number = sim->unit_number;
70568b8a9b1dSJustin T. Gibbs 		cpi->bus_id = sim->bus_id;
70579deea857SKenneth D. Merry 		cpi->base_transfer_speed = 0;
70583393f8daSKenneth D. Merry 		cpi->protocol = PROTO_UNSPECIFIED;
70593393f8daSKenneth D. Merry 		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
70603393f8daSKenneth D. Merry 		cpi->transport = XPORT_UNSPECIFIED;
70613393f8daSKenneth D. Merry 		cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
70628b8a9b1dSJustin T. Gibbs 		cpi->ccb_h.status = CAM_REQ_CMP;
70638b8a9b1dSJustin T. Gibbs 		xpt_done(work_ccb);
70648b8a9b1dSJustin T. Gibbs 		break;
70658b8a9b1dSJustin T. Gibbs 	}
70668b8a9b1dSJustin T. Gibbs 	default:
70678b8a9b1dSJustin T. Gibbs 		work_ccb->ccb_h.status = CAM_REQ_INVALID;
70688b8a9b1dSJustin T. Gibbs 		xpt_done(work_ccb);
70698b8a9b1dSJustin T. Gibbs 		break;
70708b8a9b1dSJustin T. Gibbs 	}
70718b8a9b1dSJustin T. Gibbs }
70728b8a9b1dSJustin T. Gibbs 
70738b8a9b1dSJustin T. Gibbs /*
7074434bbf6eSJustin T. Gibbs  * The xpt as a "controller" has no interrupt sources, so polling
7075434bbf6eSJustin T. Gibbs  * is a no-op.
7076434bbf6eSJustin T. Gibbs  */
7077434bbf6eSJustin T. Gibbs static void
7078434bbf6eSJustin T. Gibbs xptpoll(struct cam_sim *sim)
7079434bbf6eSJustin T. Gibbs {
7080434bbf6eSJustin T. Gibbs }
7081434bbf6eSJustin T. Gibbs 
70822b83592fSScott Long void
70832b83592fSScott Long xpt_lock_buses(void)
70842b83592fSScott Long {
70852b83592fSScott Long 	mtx_lock(&xsoftc.xpt_topo_lock);
70862b83592fSScott Long }
70872b83592fSScott Long 
70882b83592fSScott Long void
70892b83592fSScott Long xpt_unlock_buses(void)
70902b83592fSScott Long {
70912b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_topo_lock);
70922b83592fSScott Long }
70932b83592fSScott Long 
70945d754af7SMatt Jacob static void
70959758cc83SScott Long camisr(void *dummy)
70968b8a9b1dSJustin T. Gibbs {
70979758cc83SScott Long 	cam_simq_t queue;
70982b83592fSScott Long 	struct cam_sim *sim;
70998b8a9b1dSJustin T. Gibbs 
71009758cc83SScott Long 	mtx_lock(&cam_simq_lock);
7101ef3cf714SScott Long 	TAILQ_INIT(&queue);
71029758cc83SScott Long 	TAILQ_CONCAT(&queue, &cam_simq, links);
71039758cc83SScott Long 	mtx_unlock(&cam_simq_lock);
7104ef3cf714SScott Long 
71059758cc83SScott Long 	while ((sim = TAILQ_FIRST(&queue)) != NULL) {
71069758cc83SScott Long 		TAILQ_REMOVE(&queue, sim, links);
710711e4faceSScott Long 		CAM_SIM_LOCK(sim);
71089758cc83SScott Long 		sim->flags &= ~CAM_SIM_ON_DONEQ;
71099758cc83SScott Long 		camisr_runqueue(&sim->sim_doneq);
711011e4faceSScott Long 		CAM_SIM_UNLOCK(sim);
71119758cc83SScott Long 	}
71129758cc83SScott Long }
71139758cc83SScott Long 
71149758cc83SScott Long static void
71159758cc83SScott Long camisr_runqueue(void *V_queue)
71169758cc83SScott Long {
71179758cc83SScott Long 	cam_isrq_t *queue = V_queue;
71189758cc83SScott Long 	struct	ccb_hdr *ccb_h;
71199758cc83SScott Long 
71209758cc83SScott Long 	while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
71218b8a9b1dSJustin T. Gibbs 		int	runq;
71228b8a9b1dSJustin T. Gibbs 
71239758cc83SScott Long 		TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
71248b8a9b1dSJustin T. Gibbs 		ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
71258b8a9b1dSJustin T. Gibbs 
71268b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
712757b89bbcSNate Lawson 			  ("camisr\n"));
71288b8a9b1dSJustin T. Gibbs 
71298b8a9b1dSJustin T. Gibbs 		runq = FALSE;
71308b8a9b1dSJustin T. Gibbs 
71318b8a9b1dSJustin T. Gibbs 		if (ccb_h->flags & CAM_HIGH_POWER) {
71328b8a9b1dSJustin T. Gibbs 			struct highpowerlist	*hphead;
71338b8a9b1dSJustin T. Gibbs 			union ccb		*send_ccb;
71348b8a9b1dSJustin T. Gibbs 
71352b83592fSScott Long 			mtx_lock(&xsoftc.xpt_lock);
71362b83592fSScott Long 			hphead = &xsoftc.highpowerq;
71378b8a9b1dSJustin T. Gibbs 
71388b8a9b1dSJustin T. Gibbs 			send_ccb = (union ccb *)STAILQ_FIRST(hphead);
71398b8a9b1dSJustin T. Gibbs 
71408b8a9b1dSJustin T. Gibbs 			/*
71418b8a9b1dSJustin T. Gibbs 			 * Increment the count since this command is done.
71428b8a9b1dSJustin T. Gibbs 			 */
71432b83592fSScott Long 			xsoftc.num_highpower++;
71448b8a9b1dSJustin T. Gibbs 
71458b8a9b1dSJustin T. Gibbs 			/*
71468b8a9b1dSJustin T. Gibbs 			 * Any high powered commands queued up?
71478b8a9b1dSJustin T. Gibbs 			 */
71488b8a9b1dSJustin T. Gibbs 			if (send_ccb != NULL) {
71498b8a9b1dSJustin T. Gibbs 
71508b8a9b1dSJustin T. Gibbs 				STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
71512b83592fSScott Long 				mtx_unlock(&xsoftc.xpt_lock);
71528b8a9b1dSJustin T. Gibbs 
71532cefde5fSJustin T. Gibbs 				xpt_release_devq(send_ccb->ccb_h.path,
71542cefde5fSJustin T. Gibbs 						 /*count*/1, /*runqueue*/TRUE);
71552b83592fSScott Long 			} else
71562b83592fSScott Long 				mtx_unlock(&xsoftc.xpt_lock);
71578b8a9b1dSJustin T. Gibbs 		}
71582b83592fSScott Long 
71599deea857SKenneth D. Merry 		if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
71608b8a9b1dSJustin T. Gibbs 			struct cam_ed *dev;
71618b8a9b1dSJustin T. Gibbs 
71628b8a9b1dSJustin T. Gibbs 			dev = ccb_h->path->device;
71638b8a9b1dSJustin T. Gibbs 
71648b8a9b1dSJustin T. Gibbs 			cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
71658b8a9b1dSJustin T. Gibbs 
7166d3ef3454SIan Dowse 			if (!SIM_DEAD(ccb_h->path->bus->sim)) {
71678b8a9b1dSJustin T. Gibbs 				ccb_h->path->bus->sim->devq->send_active--;
71688b8a9b1dSJustin T. Gibbs 				ccb_h->path->bus->sim->devq->send_openings++;
7169d3ef3454SIan Dowse 			}
71708b8a9b1dSJustin T. Gibbs 
71713393f8daSKenneth D. Merry 			if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
71723393f8daSKenneth D. Merry 			  && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
71738b8a9b1dSJustin T. Gibbs 			 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
71748b8a9b1dSJustin T. Gibbs 			  && (dev->ccbq.dev_active == 0))) {
71758b8a9b1dSJustin T. Gibbs 
71762cefde5fSJustin T. Gibbs 				xpt_release_devq(ccb_h->path, /*count*/1,
71778b8a9b1dSJustin T. Gibbs 						 /*run_queue*/TRUE);
71788b8a9b1dSJustin T. Gibbs 			}
71798b8a9b1dSJustin T. Gibbs 
7180fd21cc5eSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
7181fd21cc5eSJustin T. Gibbs 			 && (--dev->tag_delay_count == 0))
7182fd21cc5eSJustin T. Gibbs 				xpt_start_tags(ccb_h->path);
7183fd21cc5eSJustin T. Gibbs 
71848b8a9b1dSJustin T. Gibbs 			if ((dev->ccbq.queue.entries > 0)
71858b8a9b1dSJustin T. Gibbs 			 && (dev->qfrozen_cnt == 0)
71868b8a9b1dSJustin T. Gibbs 			 && (device_is_send_queued(dev) == 0)) {
71878b8a9b1dSJustin T. Gibbs 				runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
71888b8a9b1dSJustin T. Gibbs 							      dev);
71898b8a9b1dSJustin T. Gibbs 			}
71908b8a9b1dSJustin T. Gibbs 		}
71918b8a9b1dSJustin T. Gibbs 
71928b8a9b1dSJustin T. Gibbs 		if (ccb_h->status & CAM_RELEASE_SIMQ) {
71938b8a9b1dSJustin T. Gibbs 			xpt_release_simq(ccb_h->path->bus->sim,
71948b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
7195434bbf6eSJustin T. Gibbs 			ccb_h->status &= ~CAM_RELEASE_SIMQ;
7196434bbf6eSJustin T. Gibbs 			runq = FALSE;
7197434bbf6eSJustin T. Gibbs 		}
7198434bbf6eSJustin T. Gibbs 
7199434bbf6eSJustin T. Gibbs 		if ((ccb_h->flags & CAM_DEV_QFRZDIS)
72008b8a9b1dSJustin T. Gibbs 		 && (ccb_h->status & CAM_DEV_QFRZN)) {
72012cefde5fSJustin T. Gibbs 			xpt_release_devq(ccb_h->path, /*count*/1,
72028b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
72038b8a9b1dSJustin T. Gibbs 			ccb_h->status &= ~CAM_DEV_QFRZN;
72048b8a9b1dSJustin T. Gibbs 		} else if (runq) {
72058b8a9b1dSJustin T. Gibbs 			xpt_run_dev_sendq(ccb_h->path->bus);
72068b8a9b1dSJustin T. Gibbs 		}
72078b8a9b1dSJustin T. Gibbs 
72088b8a9b1dSJustin T. Gibbs 		/* Call the peripheral driver's callback */
7209434bbf6eSJustin T. Gibbs 		(*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
72108b8a9b1dSJustin T. Gibbs 	}
72118b8a9b1dSJustin T. Gibbs }
7212d3ef3454SIan Dowse 
7213d3ef3454SIan Dowse static void
7214d3ef3454SIan Dowse dead_sim_action(struct cam_sim *sim, union ccb *ccb)
7215d3ef3454SIan Dowse {
7216d3ef3454SIan Dowse 
7217d3ef3454SIan Dowse 	ccb->ccb_h.status = CAM_DEV_NOT_THERE;
7218d3ef3454SIan Dowse 	xpt_done(ccb);
7219d3ef3454SIan Dowse }
7220d3ef3454SIan Dowse 
7221d3ef3454SIan Dowse static void
7222d3ef3454SIan Dowse dead_sim_poll(struct cam_sim *sim)
7223d3ef3454SIan Dowse {
7224d3ef3454SIan Dowse }
7225