xref: /freebsd/sys/cam/cam_xpt.c (revision a2821e04a79e87b450678afc094e326fd1a863db)
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>
458b8a9b1dSJustin T. Gibbs 
46ef3cf714SScott Long #include <sys/lock.h>
47ef3cf714SScott Long #include <sys/mutex.h>
483b87a552SMatt Jacob #include <sys/sysctl.h>
49ef3cf714SScott Long 
508b8a9b1dSJustin T. Gibbs #ifdef PC98
518b8a9b1dSJustin T. Gibbs #include <pc98/pc98/pc98_machdep.h>	/* geometry translation */
528b8a9b1dSJustin T. Gibbs #endif
538b8a9b1dSJustin T. Gibbs 
548b8a9b1dSJustin T. Gibbs #include <cam/cam.h>
558b8a9b1dSJustin T. Gibbs #include <cam/cam_ccb.h>
568b8a9b1dSJustin T. Gibbs #include <cam/cam_periph.h>
578b8a9b1dSJustin T. Gibbs #include <cam/cam_sim.h>
588b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt.h>
598b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_sim.h>
608b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_periph.h>
618b8a9b1dSJustin T. Gibbs #include <cam/cam_debug.h>
628b8a9b1dSJustin T. Gibbs 
638b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_all.h>
648b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_message.h>
658b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_pass.h>
66f0d9af51SMatt Jacob #include <machine/stdarg.h>	/* for xpt_print below */
678b8a9b1dSJustin T. Gibbs #include "opt_cam.h"
688b8a9b1dSJustin T. Gibbs 
698b8a9b1dSJustin T. Gibbs /* Datastructures internal to the xpt layer */
70362abc44STai-hwa Liang MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
718b8a9b1dSJustin T. Gibbs 
728b8a9b1dSJustin T. Gibbs /*
738b8a9b1dSJustin T. Gibbs  * Definition of an async handler callback block.  These are used to add
748b8a9b1dSJustin T. Gibbs  * SIMs and peripherals to the async callback lists.
758b8a9b1dSJustin T. Gibbs  */
768b8a9b1dSJustin T. Gibbs struct async_node {
77e3975643SJake Burkholder 	SLIST_ENTRY(async_node)	links;
788b8a9b1dSJustin T. Gibbs 	u_int32_t	event_enable;	/* Async Event enables */
798b8a9b1dSJustin T. Gibbs 	void		(*callback)(void *arg, u_int32_t code,
808b8a9b1dSJustin T. Gibbs 				    struct cam_path *path, void *args);
818b8a9b1dSJustin T. Gibbs 	void		*callback_arg;
828b8a9b1dSJustin T. Gibbs };
838b8a9b1dSJustin T. Gibbs 
84e3975643SJake Burkholder SLIST_HEAD(async_list, async_node);
85e3975643SJake Burkholder SLIST_HEAD(periph_list, cam_periph);
86e3975643SJake Burkholder static STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
878b8a9b1dSJustin T. Gibbs 
888b8a9b1dSJustin T. Gibbs /*
898b8a9b1dSJustin T. Gibbs  * This is the maximum number of high powered commands (e.g. start unit)
908b8a9b1dSJustin T. Gibbs  * that can be outstanding at a particular time.
918b8a9b1dSJustin T. Gibbs  */
928b8a9b1dSJustin T. Gibbs #ifndef CAM_MAX_HIGHPOWER
938b8a9b1dSJustin T. Gibbs #define CAM_MAX_HIGHPOWER  4
948b8a9b1dSJustin T. Gibbs #endif
958b8a9b1dSJustin T. Gibbs 
968b8a9b1dSJustin T. Gibbs /* number of high powered commands that can go through right now */
978b8a9b1dSJustin T. Gibbs static int num_highpower = CAM_MAX_HIGHPOWER;
988b8a9b1dSJustin T. Gibbs 
998b8a9b1dSJustin T. Gibbs /*
1008b8a9b1dSJustin T. Gibbs  * Structure for queueing a device in a run queue.
1018b8a9b1dSJustin T. Gibbs  * There is one run queue for allocating new ccbs,
1028b8a9b1dSJustin T. Gibbs  * and another for sending ccbs to the controller.
1038b8a9b1dSJustin T. Gibbs  */
1048b8a9b1dSJustin T. Gibbs struct cam_ed_qinfo {
1058b8a9b1dSJustin T. Gibbs 	cam_pinfo pinfo;
1068b8a9b1dSJustin T. Gibbs 	struct	  cam_ed *device;
1078b8a9b1dSJustin T. Gibbs };
1088b8a9b1dSJustin T. Gibbs 
1098b8a9b1dSJustin T. Gibbs /*
1108b8a9b1dSJustin T. Gibbs  * The CAM EDT (Existing Device Table) contains the device information for
1118b8a9b1dSJustin T. Gibbs  * all devices for all busses in the system.  The table contains a
1128b8a9b1dSJustin T. Gibbs  * cam_ed structure for each device on the bus.
1138b8a9b1dSJustin T. Gibbs  */
1148b8a9b1dSJustin T. Gibbs struct cam_ed {
115e3975643SJake Burkholder 	TAILQ_ENTRY(cam_ed) links;
1168b8a9b1dSJustin T. Gibbs 	struct	cam_ed_qinfo alloc_ccb_entry;
1178b8a9b1dSJustin T. Gibbs 	struct	cam_ed_qinfo send_ccb_entry;
1188b8a9b1dSJustin T. Gibbs 	struct	cam_et	 *target;
1198b8a9b1dSJustin T. Gibbs 	lun_id_t	 lun_id;
1208b8a9b1dSJustin T. Gibbs 	struct	camq drvq;		/*
1218b8a9b1dSJustin T. Gibbs 					 * Queue of type drivers wanting to do
1228b8a9b1dSJustin T. Gibbs 					 * work on this device.
1238b8a9b1dSJustin T. Gibbs 					 */
1248b8a9b1dSJustin T. Gibbs 	struct	cam_ccbq ccbq;		/* Queue of pending ccbs */
1258b8a9b1dSJustin T. Gibbs 	struct	async_list asyncs;	/* Async callback info for this B/T/L */
1268b8a9b1dSJustin T. Gibbs 	struct	periph_list periphs;	/* All attached devices */
1278b8a9b1dSJustin T. Gibbs 	u_int	generation;		/* Generation number */
1288b8a9b1dSJustin T. Gibbs 	struct	cam_periph *owner;	/* Peripheral driver's ownership tag */
1298b8a9b1dSJustin T. Gibbs 	struct	xpt_quirk_entry *quirk;	/* Oddities about this device */
1308b8a9b1dSJustin T. Gibbs 					/* Storage for the inquiry data */
1313393f8daSKenneth D. Merry 	cam_proto	 protocol;
1323393f8daSKenneth D. Merry 	u_int		 protocol_version;
1333393f8daSKenneth D. Merry 	cam_xport	 transport;
1343393f8daSKenneth D. Merry 	u_int		 transport_version;
1358b8a9b1dSJustin T. Gibbs 	struct		 scsi_inquiry_data inq_data;
1368b8a9b1dSJustin T. Gibbs 	u_int8_t	 inq_flags;	/*
1378b8a9b1dSJustin T. Gibbs 					 * Current settings for inquiry flags.
1388b8a9b1dSJustin T. Gibbs 					 * This allows us to override settings
1398b8a9b1dSJustin T. Gibbs 					 * like disconnection and tagged
1408b8a9b1dSJustin T. Gibbs 					 * queuing for a device.
1418b8a9b1dSJustin T. Gibbs 					 */
1428b8a9b1dSJustin T. Gibbs 	u_int8_t	 queue_flags;	/* Queue flags from the control page */
1438b8a9b1dSJustin T. Gibbs 	u_int8_t	 serial_num_len;
1449ec5d7cdSMatt Jacob 	u_int8_t	*serial_num;
1458b8a9b1dSJustin T. Gibbs 	u_int32_t	 qfrozen_cnt;
1468b8a9b1dSJustin T. Gibbs 	u_int32_t	 flags;
1478b8a9b1dSJustin T. Gibbs #define CAM_DEV_UNCONFIGURED	 	0x01
1488b8a9b1dSJustin T. Gibbs #define CAM_DEV_REL_TIMEOUT_PENDING	0x02
1498b8a9b1dSJustin T. Gibbs #define CAM_DEV_REL_ON_COMPLETE		0x04
1508b8a9b1dSJustin T. Gibbs #define CAM_DEV_REL_ON_QUEUE_EMPTY	0x08
1518b8a9b1dSJustin T. Gibbs #define CAM_DEV_RESIZE_QUEUE_NEEDED	0x10
152fd21cc5eSJustin T. Gibbs #define CAM_DEV_TAG_AFTER_COUNT		0x20
15387cfaf0eSJustin T. Gibbs #define CAM_DEV_INQUIRY_DATA_VALID	0x40
1542afca7acSMatt Jacob #define	CAM_DEV_IN_DV			0x80
1552afca7acSMatt Jacob #define	CAM_DEV_DV_HIT_BOTTOM		0x100
156fd21cc5eSJustin T. Gibbs 	u_int32_t	 tag_delay_count;
157fd21cc5eSJustin T. Gibbs #define	CAM_TAG_DELAY_COUNT		5
158df8f9080SJustin T. Gibbs 	u_int32_t	 tag_saved_openings;
1598b8a9b1dSJustin T. Gibbs 	u_int32_t	 refcount;
1608b8a9b1dSJustin T. Gibbs 	struct		 callout_handle c_handle;
1618b8a9b1dSJustin T. Gibbs };
1628b8a9b1dSJustin T. Gibbs 
1638b8a9b1dSJustin T. Gibbs /*
1648b8a9b1dSJustin T. Gibbs  * Each target is represented by an ET (Existing Target).  These
1658b8a9b1dSJustin T. Gibbs  * entries are created when a target is successfully probed with an
1668b8a9b1dSJustin T. Gibbs  * identify, and removed when a device fails to respond after a number
1678b8a9b1dSJustin T. Gibbs  * of retries, or a bus rescan finds the device missing.
1688b8a9b1dSJustin T. Gibbs  */
1698b8a9b1dSJustin T. Gibbs struct cam_et {
170e3975643SJake Burkholder 	TAILQ_HEAD(, cam_ed) ed_entries;
171e3975643SJake Burkholder 	TAILQ_ENTRY(cam_et) links;
1728b8a9b1dSJustin T. Gibbs 	struct	cam_eb	*bus;
1738b8a9b1dSJustin T. Gibbs 	target_id_t	target_id;
1748b8a9b1dSJustin T. Gibbs 	u_int32_t	refcount;
1758b8a9b1dSJustin T. Gibbs 	u_int		generation;
17687cfaf0eSJustin T. Gibbs 	struct		timeval last_reset;
1778b8a9b1dSJustin T. Gibbs };
1788b8a9b1dSJustin T. Gibbs 
1798b8a9b1dSJustin T. Gibbs /*
1808b8a9b1dSJustin T. Gibbs  * Each bus is represented by an EB (Existing Bus).  These entries
1818b8a9b1dSJustin T. Gibbs  * are created by calls to xpt_bus_register and deleted by calls to
1828b8a9b1dSJustin T. Gibbs  * xpt_bus_deregister.
1838b8a9b1dSJustin T. Gibbs  */
1848b8a9b1dSJustin T. Gibbs struct cam_eb {
185e3975643SJake Burkholder 	TAILQ_HEAD(, cam_et) et_entries;
186e3975643SJake Burkholder 	TAILQ_ENTRY(cam_eb)  links;
1878b8a9b1dSJustin T. Gibbs 	path_id_t	     path_id;
1888b8a9b1dSJustin T. Gibbs 	struct cam_sim	     *sim;
18987cfaf0eSJustin T. Gibbs 	struct timeval	     last_reset;
1908b8a9b1dSJustin T. Gibbs 	u_int32_t	     flags;
1918b8a9b1dSJustin T. Gibbs #define	CAM_EB_RUNQ_SCHEDULED	0x01
192a5479bc5SJustin T. Gibbs 	u_int32_t	     refcount;
1938b8a9b1dSJustin T. Gibbs 	u_int		     generation;
1948b8a9b1dSJustin T. Gibbs };
1958b8a9b1dSJustin T. Gibbs 
1968b8a9b1dSJustin T. Gibbs struct cam_path {
1978b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph;
1988b8a9b1dSJustin T. Gibbs 	struct cam_eb	  *bus;
1998b8a9b1dSJustin T. Gibbs 	struct cam_et	  *target;
2008b8a9b1dSJustin T. Gibbs 	struct cam_ed	  *device;
2018b8a9b1dSJustin T. Gibbs };
2028b8a9b1dSJustin T. Gibbs 
2038b8a9b1dSJustin T. Gibbs struct xpt_quirk_entry {
2048b8a9b1dSJustin T. Gibbs 	struct scsi_inquiry_pattern inq_pat;
2058b8a9b1dSJustin T. Gibbs 	u_int8_t quirks;
2068b8a9b1dSJustin T. Gibbs #define	CAM_QUIRK_NOLUNS	0x01
2078b8a9b1dSJustin T. Gibbs #define	CAM_QUIRK_NOSERIAL	0x02
20848f6456dSMatt Jacob #define	CAM_QUIRK_HILUNS	0x04
209f4322bc8SMatt Jacob #define	CAM_QUIRK_NOHILUNS	0x08
2109deea857SKenneth D. Merry 	u_int mintags;
2119deea857SKenneth D. Merry 	u_int maxtags;
2128b8a9b1dSJustin T. Gibbs };
2133b87a552SMatt Jacob 
2143b87a552SMatt Jacob static int cam_srch_hi = 0;
2153b87a552SMatt Jacob TUNABLE_INT("kern.cam.cam_srch_hi", &cam_srch_hi);
2163b87a552SMatt Jacob static int sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS);
2173b87a552SMatt Jacob SYSCTL_PROC(_kern_cam, OID_AUTO, cam_srch_hi, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
2183b87a552SMatt Jacob     sysctl_cam_search_luns, "I",
2193b87a552SMatt Jacob     "allow search above LUN 7 for SCSI3 and greater devices");
2203b87a552SMatt Jacob 
22148f6456dSMatt Jacob #define	CAM_SCSI2_MAXLUN	8
222f4322bc8SMatt Jacob /*
223f4322bc8SMatt Jacob  * If we're not quirked to search <= the first 8 luns
224f4322bc8SMatt Jacob  * and we are either quirked to search above lun 8,
2253b87a552SMatt Jacob  * or we're > SCSI-2 and we've enabled hilun searching,
2263b87a552SMatt Jacob  * or we're > SCSI-2 and the last lun was a success,
2273b87a552SMatt Jacob  * we can look for luns above lun 8.
228f4322bc8SMatt Jacob  */
2293b87a552SMatt Jacob #define	CAN_SRCH_HI_SPARSE(dv)				\
230f4322bc8SMatt Jacob   (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) 	\
231f4322bc8SMatt Jacob   && ((dv->quirk->quirks & CAM_QUIRK_HILUNS)		\
2323b87a552SMatt Jacob   || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2 && cam_srch_hi)))
2333b87a552SMatt Jacob 
2343b87a552SMatt Jacob #define	CAN_SRCH_HI_DENSE(dv)				\
2353b87a552SMatt Jacob   (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) 	\
2363b87a552SMatt Jacob   && ((dv->quirk->quirks & CAM_QUIRK_HILUNS)		\
2373b87a552SMatt Jacob   || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2)))
2388b8a9b1dSJustin T. Gibbs 
2398b8a9b1dSJustin T. Gibbs typedef enum {
2408b8a9b1dSJustin T. Gibbs 	XPT_FLAG_OPEN		= 0x01
2418b8a9b1dSJustin T. Gibbs } xpt_flags;
2428b8a9b1dSJustin T. Gibbs 
2438b8a9b1dSJustin T. Gibbs struct xpt_softc {
2448b8a9b1dSJustin T. Gibbs 	xpt_flags	flags;
2458b8a9b1dSJustin T. Gibbs 	u_int32_t	generation;
2468b8a9b1dSJustin T. Gibbs };
2478b8a9b1dSJustin T. Gibbs 
2488b8a9b1dSJustin T. Gibbs static const char quantum[] = "QUANTUM";
249f24c39c7SKenneth D. Merry static const char sony[] = "SONY";
250f24c39c7SKenneth D. Merry static const char west_digital[] = "WDIGTL";
2511f04f5abSKenneth D. Merry static const char samsung[] = "SAMSUNG";
252560d629fSKenneth D. Merry static const char seagate[] = "SEAGATE";
2535acba6ecSJohn-Mark Gurney static const char microp[] = "MICROP";
2548b8a9b1dSJustin T. Gibbs 
2558b8a9b1dSJustin T. Gibbs static struct xpt_quirk_entry xpt_quirk_table[] =
2568b8a9b1dSJustin T. Gibbs {
2578b8a9b1dSJustin T. Gibbs 	{
2588b8a9b1dSJustin T. Gibbs 		/* Reports QUEUE FULL for temporary resource shortages */
2596747168dSKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
2608b8a9b1dSJustin T. Gibbs 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
2618b8a9b1dSJustin T. Gibbs 	},
2628b8a9b1dSJustin T. Gibbs 	{
2638b8a9b1dSJustin T. Gibbs 		/* Reports QUEUE FULL for temporary resource shortages */
2646747168dSKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
2656747168dSKenneth D. Merry 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
2666747168dSKenneth D. Merry 	},
2676747168dSKenneth D. Merry 	{
2686747168dSKenneth D. Merry 		/* Reports QUEUE FULL for temporary resource shortages */
2696747168dSKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
2708b8a9b1dSJustin T. Gibbs 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
2718b8a9b1dSJustin T. Gibbs 	},
2728b8a9b1dSJustin T. Gibbs 	{
2738b8a9b1dSJustin T. Gibbs 		/* Broken tagged queuing drive */
2745acba6ecSJohn-Mark Gurney 		{ T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
2755acba6ecSJohn-Mark Gurney 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
2765acba6ecSJohn-Mark Gurney 	},
2775acba6ecSJohn-Mark Gurney 	{
2785acba6ecSJohn-Mark Gurney 		/* Broken tagged queuing drive */
2798b8a9b1dSJustin T. Gibbs 		{ T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
2808b8a9b1dSJustin T. Gibbs 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
2818b8a9b1dSJustin T. Gibbs 	},
2828b8a9b1dSJustin T. Gibbs 	{
2838b8a9b1dSJustin T. Gibbs 		/* Broken tagged queuing drive */
2845acba6ecSJohn-Mark Gurney 		{ T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
2858b8a9b1dSJustin T. Gibbs 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
2868b8a9b1dSJustin T. Gibbs 	},
2878b8a9b1dSJustin T. Gibbs 	{
2888597eafeSKenneth D. Merry 		/*
2894cdd0221SKenneth D. Merry 		 * Unfortunately, the Quantum Atlas III has the same
2904cdd0221SKenneth D. Merry 		 * problem as the Atlas II drives above.
2914cdd0221SKenneth D. Merry 		 * Reported by: "Johan Granlund" <johan@granlund.nu>
2924cdd0221SKenneth D. Merry 		 *
2934cdd0221SKenneth D. Merry 		 * For future reference, the drive with the problem was:
2944cdd0221SKenneth D. Merry 		 * QUANTUM QM39100TD-SW N1B0
2954cdd0221SKenneth D. Merry 		 *
2964cdd0221SKenneth D. Merry 		 * It's possible that Quantum will fix the problem in later
2974cdd0221SKenneth D. Merry 		 * firmware revisions.  If that happens, the quirk entry
2984cdd0221SKenneth D. Merry 		 * will need to be made specific to the firmware revisions
2994cdd0221SKenneth D. Merry 		 * with the problem.
3004cdd0221SKenneth D. Merry 		 *
3014cdd0221SKenneth D. Merry 		 */
3024cdd0221SKenneth D. Merry 		/* Reports QUEUE FULL for temporary resource shortages */
3034cdd0221SKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
3044cdd0221SKenneth D. Merry 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
3054cdd0221SKenneth D. Merry 	},
3064cdd0221SKenneth D. Merry 	{
3074cdd0221SKenneth D. Merry 		/*
308588b47e5SKenneth D. Merry 		 * 18 Gig Atlas III, same problem as the 9G version.
309588b47e5SKenneth D. Merry 		 * Reported by: Andre Albsmeier
310588b47e5SKenneth D. Merry 		 *		<andre.albsmeier@mchp.siemens.de>
311588b47e5SKenneth D. Merry 		 *
312588b47e5SKenneth D. Merry 		 * For future reference, the drive with the problem was:
313588b47e5SKenneth D. Merry 		 * QUANTUM QM318000TD-S N491
314588b47e5SKenneth D. Merry 		 */
315588b47e5SKenneth D. Merry 		/* Reports QUEUE FULL for temporary resource shortages */
316588b47e5SKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
317588b47e5SKenneth D. Merry 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
318588b47e5SKenneth D. Merry 	},
319588b47e5SKenneth D. Merry 	{
320588b47e5SKenneth D. Merry 		/*
3218597eafeSKenneth D. Merry 		 * Broken tagged queuing drive
3228597eafeSKenneth D. Merry 		 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
3238597eafeSKenneth D. Merry 		 *         and: Martin Renters <martin@tdc.on.ca>
3248597eafeSKenneth D. Merry 		 */
325560d629fSKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
3268597eafeSKenneth D. Merry 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
3278597eafeSKenneth D. Merry 	},
328560d629fSKenneth D. Merry 		/*
329c816e025SKenneth D. Merry 		 * The Seagate Medalist Pro drives have very poor write
330c816e025SKenneth D. Merry 		 * performance with anything more than 2 tags.
331560d629fSKenneth D. Merry 		 *
332560d629fSKenneth D. Merry 		 * Reported by:  Paul van der Zwan <paulz@trantor.xs4all.nl>
333560d629fSKenneth D. Merry 		 * Drive:  <SEAGATE ST36530N 1444>
334c816e025SKenneth D. Merry 		 *
335c816e025SKenneth D. Merry 		 * Reported by:  Jeremy Lea <reg@shale.csir.co.za>
336c816e025SKenneth D. Merry 		 * Drive:  <SEAGATE ST34520W 1281>
337c816e025SKenneth D. Merry 		 *
338c816e025SKenneth D. Merry 		 * No one has actually reported that the 9G version
339c816e025SKenneth D. Merry 		 * (ST39140*) of the Medalist Pro has the same problem, but
340c816e025SKenneth D. Merry 		 * we're assuming that it does because the 4G and 6.5G
341c816e025SKenneth D. Merry 		 * versions of the drive are broken.
342560d629fSKenneth D. Merry 		 */
343c816e025SKenneth D. Merry 	{
344c816e025SKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
345c816e025SKenneth D. Merry 		/*quirks*/0, /*mintags*/2, /*maxtags*/2
346c816e025SKenneth D. Merry 	},
347c816e025SKenneth D. Merry 	{
348560d629fSKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
349560d629fSKenneth D. Merry 		/*quirks*/0, /*mintags*/2, /*maxtags*/2
350560d629fSKenneth D. Merry 	},
351560d629fSKenneth D. Merry 	{
352c816e025SKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
353c816e025SKenneth D. Merry 		/*quirks*/0, /*mintags*/2, /*maxtags*/2
354c816e025SKenneth D. Merry 	},
355c816e025SKenneth D. Merry 	{
356ca3b51e9SKenneth D. Merry 		/*
357ca3b51e9SKenneth D. Merry 		 * Slow when tagged queueing is enabled.  Write performance
358ca3b51e9SKenneth D. Merry 		 * steadily drops off with more and more concurrent
359ca3b51e9SKenneth D. Merry 		 * transactions.  Best sequential write performance with
360ca3b51e9SKenneth D. Merry 		 * tagged queueing turned off and write caching turned on.
361ca3b51e9SKenneth D. Merry 		 *
362ca3b51e9SKenneth D. Merry 		 * PR:  kern/10398
363ca3b51e9SKenneth D. Merry 		 * Submitted by:  Hideaki Okada <hokada@isl.melco.co.jp>
364ca3b51e9SKenneth D. Merry 		 * Drive:  DCAS-34330 w/ "S65A" firmware.
365ca3b51e9SKenneth D. Merry 		 *
366ca3b51e9SKenneth D. Merry 		 * The drive with the problem had the "S65A" firmware
367ca3b51e9SKenneth D. Merry 		 * revision, and has also been reported (by Stephen J.
368ca3b51e9SKenneth D. Merry 		 * Roznowski <sjr@home.net>) for a drive with the "S61A"
369ca3b51e9SKenneth D. Merry 		 * firmware revision.
370ca3b51e9SKenneth D. Merry 		 *
371ca3b51e9SKenneth D. Merry 		 * Although no one has reported problems with the 2 gig
372ca3b51e9SKenneth D. Merry 		 * version of the DCAS drive, the assumption is that it
373ca3b51e9SKenneth D. Merry 		 * has the same problems as the 4 gig version.  Therefore
374ca3b51e9SKenneth D. Merry 		 * this quirk entries disables tagged queueing for all
375ca3b51e9SKenneth D. Merry 		 * DCAS drives.
376ca3b51e9SKenneth D. Merry 		 */
377ca3b51e9SKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
378ca3b51e9SKenneth D. Merry 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
379ca3b51e9SKenneth D. Merry 	},
380ca3b51e9SKenneth D. Merry 	{
3818b8a9b1dSJustin T. Gibbs 		/* Broken tagged queuing drive */
3828b8a9b1dSJustin T. Gibbs 		{ T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
3838b8a9b1dSJustin T. Gibbs 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
3848b8a9b1dSJustin T. Gibbs 	},
3858b8a9b1dSJustin T. Gibbs 	{
3862ba01e6fSJustin T. Gibbs 		/* Broken tagged queuing drive */
3876dd76100SKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
3882ba01e6fSJustin T. Gibbs 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
3892ba01e6fSJustin T. Gibbs 	},
3902ba01e6fSJustin T. Gibbs 	{
3919fdeccedSMatt Jacob 		/* This does not support other than LUN 0 */
3925ce1b004SMatt Jacob 		{ T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*" },
393df4b14f4SMatt Jacob 		CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
394015a972cSMatt Jacob 	},
395015a972cSMatt Jacob 	{
396e471e974SJustin T. Gibbs 		/*
3971f04f5abSKenneth D. Merry 		 * Broken tagged queuing drive.
3981f04f5abSKenneth D. Merry 		 * Submitted by:
3991f04f5abSKenneth D. Merry 		 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
4001f04f5abSKenneth D. Merry 		 * in PR kern/9535
4011f04f5abSKenneth D. Merry 		 */
4021f04f5abSKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
4031f04f5abSKenneth D. Merry 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
4041f04f5abSKenneth D. Merry 	},
4051f04f5abSKenneth D. Merry         {
4061f04f5abSKenneth D. Merry 		/*
407f24c39c7SKenneth D. Merry 		 * Slow when tagged queueing is enabled. (1.5MB/sec versus
408f24c39c7SKenneth D. Merry 		 * 8MB/sec.)
409f24c39c7SKenneth D. Merry 		 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
410ca3b51e9SKenneth D. Merry 		 * Best performance with these drives is achieved with
411ca3b51e9SKenneth D. Merry 		 * tagged queueing turned off, and write caching turned on.
412f24c39c7SKenneth D. Merry 		 */
413f24c39c7SKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
414f24c39c7SKenneth D. Merry 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
415f24c39c7SKenneth D. Merry         },
416f24c39c7SKenneth D. Merry         {
417f24c39c7SKenneth D. Merry 		/*
418f24c39c7SKenneth D. Merry 		 * Slow when tagged queueing is enabled. (1.5MB/sec versus
419f24c39c7SKenneth D. Merry 		 * 8MB/sec.)
420f24c39c7SKenneth D. Merry 		 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
421ca3b51e9SKenneth D. Merry 		 * Best performance with these drives is achieved with
422ca3b51e9SKenneth D. Merry 		 * tagged queueing turned off, and write caching turned on.
423f24c39c7SKenneth D. Merry 		 */
424f24c39c7SKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
425f24c39c7SKenneth D. Merry 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
426f24c39c7SKenneth D. Merry         },
427f24c39c7SKenneth D. Merry 	{
428f24c39c7SKenneth D. Merry 		/*
429e471e974SJustin T. Gibbs 		 * Doesn't handle queue full condition correctly,
430e471e974SJustin T. Gibbs 		 * so we need to limit maxtags to what the device
431e471e974SJustin T. Gibbs 		 * can handle instead of determining this automatically.
432e471e974SJustin T. Gibbs 		 */
4331f04f5abSKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
434fd21cc5eSJustin T. Gibbs 		/*quirks*/0, /*mintags*/2, /*maxtags*/32
435e471e974SJustin T. Gibbs 	},
436e471e974SJustin T. Gibbs 	{
4378b8a9b1dSJustin T. Gibbs 		/* Really only one LUN */
438f2538844SMatt Jacob 		{ T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
4398b8a9b1dSJustin T. Gibbs 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
4408b8a9b1dSJustin T. Gibbs 	},
4418b8a9b1dSJustin T. Gibbs 	{
442fd21cc5eSJustin T. Gibbs 		/* I can't believe we need a quirk for DPT volumes. */
4434b4c702bSMatt Jacob 		{ T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
444fd21cc5eSJustin T. Gibbs 		CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
445fd21cc5eSJustin T. Gibbs 		/*mintags*/0, /*maxtags*/255
446fd21cc5eSJustin T. Gibbs 	},
447fd21cc5eSJustin T. Gibbs 	{
448f24c39c7SKenneth D. Merry 		/*
44925e5ca27SKenneth D. Merry 		 * Many Sony CDROM drives don't like multi-LUN probing.
450f24c39c7SKenneth D. Merry 		 */
4514b4c702bSMatt Jacob 		{ T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
452f24c39c7SKenneth D. Merry 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
453f24c39c7SKenneth D. Merry 	},
454f24c39c7SKenneth D. Merry 	{
455f24c39c7SKenneth D. Merry 		/*
456f24c39c7SKenneth D. Merry 		 * This drive doesn't like multiple LUN probing.
457f24c39c7SKenneth D. Merry 		 * Submitted by:  Parag Patel <parag@cgt.com>
458f24c39c7SKenneth D. Merry 		 */
4594b4c702bSMatt Jacob 		{ T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R   CDU9*", "*" },
460f24c39c7SKenneth D. Merry 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
461f24c39c7SKenneth D. Merry 	},
462f24c39c7SKenneth D. Merry 	{
463ca2b07adSKenneth D. Merry 		{ T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
464ca2b07adSKenneth D. Merry 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
465ca2b07adSKenneth D. Merry 	},
466ca2b07adSKenneth D. Merry 	{
467b1d26455SKenneth D. Merry 		/*
468b1d26455SKenneth D. Merry 		 * The 8200 doesn't like multi-lun probing, and probably
469b1d26455SKenneth D. Merry 		 * don't like serial number requests either.
470b1d26455SKenneth D. Merry 		 */
471b1d26455SKenneth D. Merry 		{
472b1d26455SKenneth D. Merry 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
473b1d26455SKenneth D. Merry 			"EXB-8200*", "*"
474b1d26455SKenneth D. Merry 		},
475b1d26455SKenneth D. Merry 		CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
476b1d26455SKenneth D. Merry 	},
477b1d26455SKenneth D. Merry 	{
47836230d67SJoerg Wunsch 		/*
479f2538844SMatt Jacob 		 * Let's try the same as above, but for a drive that says
480f2538844SMatt Jacob 		 * it's an IPL-6860 but is actually an EXB 8200.
481f2538844SMatt Jacob 		 */
482f2538844SMatt Jacob 		{
483f2538844SMatt Jacob 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
484f2538844SMatt Jacob 			"IPL-6860*", "*"
485f2538844SMatt Jacob 		},
486f2538844SMatt Jacob 		CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
487f2538844SMatt Jacob 	},
488f2538844SMatt Jacob 	{
489f2538844SMatt Jacob 		/*
490a6cb9949SKenneth D. Merry 		 * These Hitachi drives don't like multi-lun probing.
491a6cb9949SKenneth D. Merry 		 * The PR submitter has a DK319H, but says that the Linux
492a6cb9949SKenneth D. Merry 		 * kernel has a similar work-around for the DK312 and DK314,
493a6cb9949SKenneth D. Merry 		 * so all DK31* drives are quirked here.
494a6cb9949SKenneth D. Merry 		 * PR:            misc/18793
495a6cb9949SKenneth D. Merry 		 * Submitted by:  Paul Haddad <paul@pth.com>
496a6cb9949SKenneth D. Merry 		 */
497a6cb9949SKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
498a6cb9949SKenneth D. Merry 		CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
499a6cb9949SKenneth D. Merry 	},
500a6cb9949SKenneth D. Merry 	{
501a6cb9949SKenneth D. Merry 		/*
502136b546aSDavid Greenman 		 * The Hitachi CJ series with J8A8 firmware apparantly has
503136b546aSDavid Greenman 		 * problems with tagged commands.
504136b546aSDavid Greenman 		 * PR: 23536
505136b546aSDavid Greenman 		 * Reported by: amagai@nue.org
506136b546aSDavid Greenman 		 */
507136b546aSDavid Greenman 		{ T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
508136b546aSDavid Greenman 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
509136b546aSDavid Greenman 	},
510136b546aSDavid Greenman 	{
511136b546aSDavid Greenman 		/*
51278900a83SMatt Jacob 		 * These are the large storage arrays.
51378900a83SMatt Jacob 		 * Submitted by:  William Carrel <william.carrel@infospace.com>
51478900a83SMatt Jacob 		 */
51578900a83SMatt Jacob 		{ T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
51678900a83SMatt Jacob 		CAM_QUIRK_HILUNS, 2, 1024
51778900a83SMatt Jacob 	},
51878900a83SMatt Jacob 	{
51978900a83SMatt Jacob 		/*
52036230d67SJoerg Wunsch 		 * This old revision of the TDC3600 is also SCSI-1, and
52136230d67SJoerg Wunsch 		 * hangs upon serial number probing.
52236230d67SJoerg Wunsch 		 */
52336230d67SJoerg Wunsch 		{
52436230d67SJoerg Wunsch 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
52536230d67SJoerg Wunsch 			" TDC 3600", "U07:"
52636230d67SJoerg Wunsch 		},
52736230d67SJoerg Wunsch 		CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
52836230d67SJoerg Wunsch 	},
52936230d67SJoerg Wunsch 	{
5302c3cca96SMatt Jacob 		/*
5317afa4afbSHidetoshi Shimokawa 		 * Maxtor Personal Storage 3000XT (Firewire)
5327afa4afbSHidetoshi Shimokawa 		 * hangs upon serial number probing.
5337afa4afbSHidetoshi Shimokawa 		 */
5347afa4afbSHidetoshi Shimokawa 		{
5357afa4afbSHidetoshi Shimokawa 			T_DIRECT, SIP_MEDIA_FIXED, "Maxtor",
5367afa4afbSHidetoshi Shimokawa 			"1394 storage", "*"
5377afa4afbSHidetoshi Shimokawa 		},
5387afa4afbSHidetoshi Shimokawa 		CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
5397afa4afbSHidetoshi Shimokawa 	},
5407afa4afbSHidetoshi Shimokawa 	{
5417afa4afbSHidetoshi Shimokawa 		/*
5422c3cca96SMatt Jacob 		 * Would repond to all LUNs if asked for.
5432c3cca96SMatt Jacob 		 */
5442c3cca96SMatt Jacob 		{
5452c3cca96SMatt Jacob 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
5462c3cca96SMatt Jacob 			"CP150", "*"
5472c3cca96SMatt Jacob 		},
5482c3cca96SMatt Jacob 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
5492c3cca96SMatt Jacob 	},
5502c3cca96SMatt Jacob 	{
551040816ecSMatt Jacob 		/*
552040816ecSMatt Jacob 		 * Would repond to all LUNs if asked for.
553040816ecSMatt Jacob 		 */
554040816ecSMatt Jacob 		{
555040816ecSMatt Jacob 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
556040816ecSMatt Jacob 			"96X2*", "*"
557040816ecSMatt Jacob 		},
558040816ecSMatt Jacob 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
559040816ecSMatt Jacob 	},
560040816ecSMatt Jacob 	{
5614b4c702bSMatt Jacob 		/* Submitted by: Matthew Dodd <winter@jurai.net> */
5624b4c702bSMatt Jacob 		{ T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
5634b4c702bSMatt Jacob 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
5644b4c702bSMatt Jacob 	},
5654b4c702bSMatt Jacob 	{
5664b4c702bSMatt Jacob 		/* Submitted by: Matthew Dodd <winter@jurai.net> */
5674b4c702bSMatt Jacob 		{ T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
5684b4c702bSMatt Jacob 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
5694b4c702bSMatt Jacob 	},
5704b4c702bSMatt Jacob 	{
571da6efda6SDavid Greenman 		/* TeraSolutions special settings for TRC-22 RAID */
572da6efda6SDavid Greenman 		{ T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
573da6efda6SDavid Greenman 		  /*quirks*/0, /*mintags*/55, /*maxtags*/255
574da6efda6SDavid Greenman 	},
575da6efda6SDavid Greenman 	{
576c7948a94SMatt Jacob 		/* Veritas Storage Appliance */
577c7948a94SMatt Jacob 		{ T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
578c7948a94SMatt Jacob 		  CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
579c7948a94SMatt Jacob 	},
580c7948a94SMatt Jacob 	{
5813b9d81dcSJoerg Wunsch 		/*
5823b9d81dcSJoerg Wunsch 		 * Would respond to all LUNs.  Device type and removable
5833b9d81dcSJoerg Wunsch 		 * flag are jumper-selectable.
5843b9d81dcSJoerg Wunsch 		 */
5853b9d81dcSJoerg Wunsch 		{ T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
5863b9d81dcSJoerg Wunsch 		  "Tahiti 1", "*"
5873b9d81dcSJoerg Wunsch 		},
5883b9d81dcSJoerg Wunsch 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
5893b9d81dcSJoerg Wunsch 	},
5903b9d81dcSJoerg Wunsch 	{
5918924340fSMichael Reifenberger 		/* EasyRAID E5A aka. areca ARC-6010 */
5928924340fSMichael Reifenberger 		{ T_DIRECT, SIP_MEDIA_FIXED, "easyRAID", "*", "*" },
5938924340fSMichael Reifenberger 		  CAM_QUIRK_NOHILUNS, /*mintags*/2, /*maxtags*/255
5948924340fSMichael Reifenberger 	},
5958924340fSMichael Reifenberger 	{
5961190a85bSMatt Jacob 		{ T_ENCLOSURE, SIP_MEDIA_FIXED, "DP", "BACKPLANE", "*" },
5971190a85bSMatt Jacob 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
5981190a85bSMatt Jacob 	},
5991190a85bSMatt Jacob 	{
6008b8a9b1dSJustin T. Gibbs 		/* Default tagged queuing parameters for all devices */
6018b8a9b1dSJustin T. Gibbs 		{
6028b8a9b1dSJustin T. Gibbs 		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
6038b8a9b1dSJustin T. Gibbs 		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
6048b8a9b1dSJustin T. Gibbs 		},
605fd21cc5eSJustin T. Gibbs 		/*quirks*/0, /*mintags*/2, /*maxtags*/255
6068b8a9b1dSJustin T. Gibbs 	},
6078b8a9b1dSJustin T. Gibbs };
608fd21cc5eSJustin T. Gibbs 
60987cfaf0eSJustin T. Gibbs static const int xpt_quirk_table_size =
61087cfaf0eSJustin T. Gibbs 	sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
61187cfaf0eSJustin T. Gibbs 
6128b8a9b1dSJustin T. Gibbs typedef enum {
6138b8a9b1dSJustin T. Gibbs 	DM_RET_COPY		= 0x01,
6148b8a9b1dSJustin T. Gibbs 	DM_RET_FLAG_MASK	= 0x0f,
6158b8a9b1dSJustin T. Gibbs 	DM_RET_NONE		= 0x00,
6168b8a9b1dSJustin T. Gibbs 	DM_RET_STOP		= 0x10,
6178b8a9b1dSJustin T. Gibbs 	DM_RET_DESCEND		= 0x20,
6188b8a9b1dSJustin T. Gibbs 	DM_RET_ERROR		= 0x30,
6198b8a9b1dSJustin T. Gibbs 	DM_RET_ACTION_MASK	= 0xf0
6208b8a9b1dSJustin T. Gibbs } dev_match_ret;
6218b8a9b1dSJustin T. Gibbs 
6228b8a9b1dSJustin T. Gibbs typedef enum {
6238b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_BUS,
6248b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_TARGET,
6258b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_DEVICE,
6268b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_PERIPH
6278b8a9b1dSJustin T. Gibbs } xpt_traverse_depth;
6288b8a9b1dSJustin T. Gibbs 
6298b8a9b1dSJustin T. Gibbs struct xpt_traverse_config {
6308b8a9b1dSJustin T. Gibbs 	xpt_traverse_depth	depth;
6318b8a9b1dSJustin T. Gibbs 	void			*tr_func;
6328b8a9b1dSJustin T. Gibbs 	void			*tr_arg;
6338b8a9b1dSJustin T. Gibbs };
6348b8a9b1dSJustin T. Gibbs 
6358b8a9b1dSJustin T. Gibbs typedef	int	xpt_busfunc_t (struct cam_eb *bus, void *arg);
6368b8a9b1dSJustin T. Gibbs typedef	int	xpt_targetfunc_t (struct cam_et *target, void *arg);
6378b8a9b1dSJustin T. Gibbs typedef	int	xpt_devicefunc_t (struct cam_ed *device, void *arg);
6388b8a9b1dSJustin T. Gibbs typedef	int	xpt_periphfunc_t (struct cam_periph *periph, void *arg);
6398b8a9b1dSJustin T. Gibbs typedef int	xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
6408b8a9b1dSJustin T. Gibbs 
6418b8a9b1dSJustin T. Gibbs /* Transport layer configuration information */
6428b8a9b1dSJustin T. Gibbs static struct xpt_softc xsoftc;
6438b8a9b1dSJustin T. Gibbs 
6448b8a9b1dSJustin T. Gibbs /* Queues for our software interrupt handler */
645e3975643SJake Burkholder typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
6468b8a9b1dSJustin T. Gibbs static cam_isrq_t cam_bioq;
647ef3cf714SScott Long static struct mtx cam_bioq_lock;
6488b8a9b1dSJustin T. Gibbs 
6498b8a9b1dSJustin T. Gibbs /* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
650e3975643SJake Burkholder static SLIST_HEAD(,ccb_hdr) ccb_freeq;
6518b8a9b1dSJustin T. Gibbs static u_int xpt_max_ccbs;	/*
6528b8a9b1dSJustin T. Gibbs 				 * Maximum size of ccb pool.  Modified as
6538b8a9b1dSJustin T. Gibbs 				 * devices are added/removed or have their
6548b8a9b1dSJustin T. Gibbs 				 * opening counts changed.
6558b8a9b1dSJustin T. Gibbs 				 */
6568b8a9b1dSJustin T. Gibbs static u_int xpt_ccb_count;	/* Current count of allocated ccbs */
6578b8a9b1dSJustin T. Gibbs 
6589a1c8571SNick Hibma struct cam_periph *xpt_periph;
6599a1c8571SNick Hibma 
6608b8a9b1dSJustin T. Gibbs static periph_init_t xpt_periph_init;
6618b8a9b1dSJustin T. Gibbs 
6628b8a9b1dSJustin T. Gibbs static periph_init_t probe_periph_init;
6638b8a9b1dSJustin T. Gibbs 
6648b8a9b1dSJustin T. Gibbs static struct periph_driver xpt_driver =
6658b8a9b1dSJustin T. Gibbs {
6668b8a9b1dSJustin T. Gibbs 	xpt_periph_init, "xpt",
6678b8a9b1dSJustin T. Gibbs 	TAILQ_HEAD_INITIALIZER(xpt_driver.units)
6688b8a9b1dSJustin T. Gibbs };
6698b8a9b1dSJustin T. Gibbs 
6708b8a9b1dSJustin T. Gibbs static struct periph_driver probe_driver =
6718b8a9b1dSJustin T. Gibbs {
6728b8a9b1dSJustin T. Gibbs 	probe_periph_init, "probe",
6738b8a9b1dSJustin T. Gibbs 	TAILQ_HEAD_INITIALIZER(probe_driver.units)
6748b8a9b1dSJustin T. Gibbs };
6758b8a9b1dSJustin T. Gibbs 
6760b7c27b9SPeter Wemm PERIPHDRIVER_DECLARE(xpt, xpt_driver);
6770b7c27b9SPeter Wemm PERIPHDRIVER_DECLARE(probe, probe_driver);
6788b8a9b1dSJustin T. Gibbs 
6798b8a9b1dSJustin T. Gibbs 
6808b8a9b1dSJustin T. Gibbs static d_open_t xptopen;
6818b8a9b1dSJustin T. Gibbs static d_close_t xptclose;
6828b8a9b1dSJustin T. Gibbs static d_ioctl_t xptioctl;
6838b8a9b1dSJustin T. Gibbs 
6844e2f199eSPoul-Henning Kamp static struct cdevsw xpt_cdevsw = {
685dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
686dc08ffecSPoul-Henning Kamp 	.d_flags =	D_NEEDGIANT,
6877ac40f5fSPoul-Henning Kamp 	.d_open =	xptopen,
6887ac40f5fSPoul-Henning Kamp 	.d_close =	xptclose,
6897ac40f5fSPoul-Henning Kamp 	.d_ioctl =	xptioctl,
6907ac40f5fSPoul-Henning Kamp 	.d_name =	"xpt",
6918b8a9b1dSJustin T. Gibbs };
6928b8a9b1dSJustin T. Gibbs 
6938b8a9b1dSJustin T. Gibbs static struct intr_config_hook *xpt_config_hook;
6948b8a9b1dSJustin T. Gibbs 
695d3ef3454SIan Dowse static void dead_sim_action(struct cam_sim *sim, union ccb *ccb);
696d3ef3454SIan Dowse static void dead_sim_poll(struct cam_sim *sim);
697d3ef3454SIan Dowse 
698d3ef3454SIan Dowse /* Dummy SIM that is used when the real one has gone. */
699d3ef3454SIan Dowse static struct cam_sim cam_dead_sim = {
700d3ef3454SIan Dowse 	.sim_action =	dead_sim_action,
701d3ef3454SIan Dowse 	.sim_poll =	dead_sim_poll,
702d3ef3454SIan Dowse 	.sim_name =	"dead_sim",
703d3ef3454SIan Dowse };
704d3ef3454SIan Dowse 
705d3ef3454SIan Dowse #define SIM_DEAD(sim)	((sim) == &cam_dead_sim)
706d3ef3454SIan Dowse 
7078b8a9b1dSJustin T. Gibbs /* Registered busses */
708e3975643SJake Burkholder static TAILQ_HEAD(,cam_eb) xpt_busses;
7098b8a9b1dSJustin T. Gibbs static u_int bus_generation;
7108b8a9b1dSJustin T. Gibbs 
7118b8a9b1dSJustin T. Gibbs /* Storage for debugging datastructures */
7128b8a9b1dSJustin T. Gibbs #ifdef	CAMDEBUG
7138b8a9b1dSJustin T. Gibbs struct cam_path *cam_dpath;
7148b8a9b1dSJustin T. Gibbs u_int32_t cam_dflags;
7152cefde5fSJustin T. Gibbs u_int32_t cam_debug_delay;
7168b8a9b1dSJustin T. Gibbs #endif
7178b8a9b1dSJustin T. Gibbs 
7188088699fSJohn Baldwin /* Pointers to software interrupt handlers */
71965c38256SMike Smith static void *cambio_ih;
7208088699fSJohn Baldwin 
7218b8a9b1dSJustin T. Gibbs #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
7228b8a9b1dSJustin T. Gibbs #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
7238b8a9b1dSJustin T. Gibbs #endif
7248b8a9b1dSJustin T. Gibbs 
7258b8a9b1dSJustin T. Gibbs /*
7268b8a9b1dSJustin T. Gibbs  * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
7278b8a9b1dSJustin T. Gibbs  * enabled.  Also, the user must have either none, or all of CAM_DEBUG_BUS,
7288b8a9b1dSJustin T. Gibbs  * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
7298b8a9b1dSJustin T. Gibbs  */
7308b8a9b1dSJustin T. Gibbs #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
7318b8a9b1dSJustin T. Gibbs     || defined(CAM_DEBUG_LUN)
7328b8a9b1dSJustin T. Gibbs #ifdef CAMDEBUG
7338b8a9b1dSJustin T. Gibbs #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
7348b8a9b1dSJustin T. Gibbs     || !defined(CAM_DEBUG_LUN)
7358b8a9b1dSJustin T. Gibbs #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
7368b8a9b1dSJustin T. Gibbs         and CAM_DEBUG_LUN"
7378b8a9b1dSJustin T. Gibbs #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
7388b8a9b1dSJustin T. Gibbs #else /* !CAMDEBUG */
7398b8a9b1dSJustin T. Gibbs #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
7408b8a9b1dSJustin T. Gibbs #endif /* CAMDEBUG */
7418b8a9b1dSJustin T. Gibbs #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
7428b8a9b1dSJustin T. Gibbs 
7436d2a8f1cSPeter Wemm /* Our boot-time initialization hook */
74474bd1c10SNick Hibma static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
74574bd1c10SNick Hibma 
74674bd1c10SNick Hibma static moduledata_t cam_moduledata = {
74774bd1c10SNick Hibma 	"cam",
74874bd1c10SNick Hibma 	cam_module_event_handler,
74974bd1c10SNick Hibma 	NULL
75074bd1c10SNick Hibma };
75174bd1c10SNick Hibma 
7526d2a8f1cSPeter Wemm static void	xpt_init(void *);
75374bd1c10SNick Hibma 
75474bd1c10SNick Hibma DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
75574bd1c10SNick Hibma MODULE_VERSION(cam, 1);
75674bd1c10SNick Hibma 
7578b8a9b1dSJustin T. Gibbs 
7588b8a9b1dSJustin T. Gibbs static cam_status	xpt_compile_path(struct cam_path *new_path,
7598b8a9b1dSJustin T. Gibbs 					 struct cam_periph *perph,
7608b8a9b1dSJustin T. Gibbs 					 path_id_t path_id,
7618b8a9b1dSJustin T. Gibbs 					 target_id_t target_id,
7628b8a9b1dSJustin T. Gibbs 					 lun_id_t lun_id);
7638b8a9b1dSJustin T. Gibbs 
7648b8a9b1dSJustin T. Gibbs static void		xpt_release_path(struct cam_path *path);
7658b8a9b1dSJustin T. Gibbs 
7668b8a9b1dSJustin T. Gibbs static void		xpt_async_bcast(struct async_list *async_head,
7678b8a9b1dSJustin T. Gibbs 					u_int32_t async_code,
7688b8a9b1dSJustin T. Gibbs 					struct cam_path *path,
7698b8a9b1dSJustin T. Gibbs 					void *async_arg);
7702f22d08dSJustin T. Gibbs static void		xpt_dev_async(u_int32_t async_code,
7712f22d08dSJustin T. Gibbs 				      struct cam_eb *bus,
7722f22d08dSJustin T. Gibbs 				      struct cam_et *target,
7732f22d08dSJustin T. Gibbs 				      struct cam_ed *device,
7742f22d08dSJustin T. Gibbs 				      void *async_arg);
775434bbf6eSJustin T. Gibbs static path_id_t xptnextfreepathid(void);
776434bbf6eSJustin T. Gibbs static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
7778b8a9b1dSJustin T. Gibbs static union ccb *xpt_get_ccb(struct cam_ed *device);
7788b8a9b1dSJustin T. Gibbs static int	 xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
7798b8a9b1dSJustin T. Gibbs 				  u_int32_t new_priority);
7808b8a9b1dSJustin T. Gibbs static void	 xpt_run_dev_allocq(struct cam_eb *bus);
7818b8a9b1dSJustin T. Gibbs static void	 xpt_run_dev_sendq(struct cam_eb *bus);
7828b8a9b1dSJustin T. Gibbs static timeout_t xpt_release_devq_timeout;
7838b8a9b1dSJustin T. Gibbs static timeout_t xpt_release_simq_timeout;
784a5479bc5SJustin T. Gibbs static void	 xpt_release_bus(struct cam_eb *bus);
7852cefde5fSJustin T. Gibbs static void	 xpt_release_devq_device(struct cam_ed *dev, u_int count,
7862cefde5fSJustin T. Gibbs 					 int run_queue);
7878b8a9b1dSJustin T. Gibbs static struct cam_et*
7888b8a9b1dSJustin T. Gibbs 		 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
7898b8a9b1dSJustin T. Gibbs static void	 xpt_release_target(struct cam_eb *bus, struct cam_et *target);
7908b8a9b1dSJustin T. Gibbs static struct cam_ed*
7918b8a9b1dSJustin T. Gibbs 		 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
7928b8a9b1dSJustin T. Gibbs 				  lun_id_t lun_id);
7938b8a9b1dSJustin T. Gibbs static void	 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
7948b8a9b1dSJustin T. Gibbs 				    struct cam_ed *device);
7958b8a9b1dSJustin T. Gibbs static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
7968b8a9b1dSJustin T. Gibbs static struct cam_eb*
7978b8a9b1dSJustin T. Gibbs 		 xpt_find_bus(path_id_t path_id);
7988b8a9b1dSJustin T. Gibbs static struct cam_et*
7998b8a9b1dSJustin T. Gibbs 		 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
8008b8a9b1dSJustin T. Gibbs static struct cam_ed*
8018b8a9b1dSJustin T. Gibbs 		 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
8028b8a9b1dSJustin T. Gibbs static void	 xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
8038b8a9b1dSJustin T. Gibbs static void	 xpt_scan_lun(struct cam_periph *periph,
8048b8a9b1dSJustin T. Gibbs 			      struct cam_path *path, cam_flags flags,
8058b8a9b1dSJustin T. Gibbs 			      union ccb *ccb);
8068b8a9b1dSJustin T. Gibbs static void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
8078b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptconfigbuscountfunc;
8088b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptconfigfunc;
8098b8a9b1dSJustin T. Gibbs static void	 xpt_config(void *arg);
8108b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t xptpassannouncefunc;
8118b8a9b1dSJustin T. Gibbs static void	 xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
8128b8a9b1dSJustin T. Gibbs static void	 xptaction(struct cam_sim *sim, union ccb *work_ccb);
813434bbf6eSJustin T. Gibbs static void	 xptpoll(struct cam_sim *sim);
8148088699fSJohn Baldwin static void	 camisr(void *);
8158b8a9b1dSJustin T. Gibbs #if 0
8168b8a9b1dSJustin T. Gibbs static void	 xptstart(struct cam_periph *periph, union ccb *work_ccb);
8178b8a9b1dSJustin T. Gibbs static void	 xptasync(struct cam_periph *periph,
8188b8a9b1dSJustin T. Gibbs 			  u_int32_t code, cam_path *path);
8198b8a9b1dSJustin T. Gibbs #endif
8208b8a9b1dSJustin T. Gibbs static dev_match_ret	xptbusmatch(struct dev_match_pattern *patterns,
8213393f8daSKenneth D. Merry 				    u_int num_patterns, struct cam_eb *bus);
8228b8a9b1dSJustin T. Gibbs static dev_match_ret	xptdevicematch(struct dev_match_pattern *patterns,
8233393f8daSKenneth D. Merry 				       u_int num_patterns,
8243393f8daSKenneth D. Merry 				       struct cam_ed *device);
8258b8a9b1dSJustin T. Gibbs static dev_match_ret	xptperiphmatch(struct dev_match_pattern *patterns,
8263393f8daSKenneth D. Merry 				       u_int num_patterns,
8278b8a9b1dSJustin T. Gibbs 				       struct cam_periph *periph);
8288b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptedtbusfunc;
8298b8a9b1dSJustin T. Gibbs static xpt_targetfunc_t	xptedttargetfunc;
8308b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptedtdevicefunc;
8318b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptedtperiphfunc;
8328b8a9b1dSJustin T. Gibbs static xpt_pdrvfunc_t	xptplistpdrvfunc;
8338b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptplistperiphfunc;
8348b8a9b1dSJustin T. Gibbs static int		xptedtmatch(struct ccb_dev_match *cdm);
8358b8a9b1dSJustin T. Gibbs static int		xptperiphlistmatch(struct ccb_dev_match *cdm);
8368b8a9b1dSJustin T. Gibbs static int		xptbustraverse(struct cam_eb *start_bus,
8378b8a9b1dSJustin T. Gibbs 				       xpt_busfunc_t *tr_func, void *arg);
8388b8a9b1dSJustin T. Gibbs static int		xpttargettraverse(struct cam_eb *bus,
8398b8a9b1dSJustin T. Gibbs 					  struct cam_et *start_target,
8408b8a9b1dSJustin T. Gibbs 					  xpt_targetfunc_t *tr_func, void *arg);
8418b8a9b1dSJustin T. Gibbs static int		xptdevicetraverse(struct cam_et *target,
8428b8a9b1dSJustin T. Gibbs 					  struct cam_ed *start_device,
8438b8a9b1dSJustin T. Gibbs 					  xpt_devicefunc_t *tr_func, void *arg);
8448b8a9b1dSJustin T. Gibbs static int		xptperiphtraverse(struct cam_ed *device,
8458b8a9b1dSJustin T. Gibbs 					  struct cam_periph *start_periph,
8468b8a9b1dSJustin T. Gibbs 					  xpt_periphfunc_t *tr_func, void *arg);
8478b8a9b1dSJustin T. Gibbs static int		xptpdrvtraverse(struct periph_driver **start_pdrv,
8488b8a9b1dSJustin T. Gibbs 					xpt_pdrvfunc_t *tr_func, void *arg);
8498b8a9b1dSJustin T. Gibbs static int		xptpdperiphtraverse(struct periph_driver **pdrv,
8508b8a9b1dSJustin T. Gibbs 					    struct cam_periph *start_periph,
8518b8a9b1dSJustin T. Gibbs 					    xpt_periphfunc_t *tr_func,
8528b8a9b1dSJustin T. Gibbs 					    void *arg);
8538b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptdefbusfunc;
8548b8a9b1dSJustin T. Gibbs static xpt_targetfunc_t	xptdeftargetfunc;
8558b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptdefdevicefunc;
8568b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptdefperiphfunc;
8578b8a9b1dSJustin T. Gibbs static int		xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
858bfc0eb0fSKenneth D. Merry #ifdef notusedyet
8598b8a9b1dSJustin T. Gibbs static int		xpt_for_all_targets(xpt_targetfunc_t *tr_func,
8608b8a9b1dSJustin T. Gibbs 					    void *arg);
861bfc0eb0fSKenneth D. Merry #endif
8628b8a9b1dSJustin T. Gibbs static int		xpt_for_all_devices(xpt_devicefunc_t *tr_func,
8638b8a9b1dSJustin T. Gibbs 					    void *arg);
864bfc0eb0fSKenneth D. Merry #ifdef notusedyet
8658b8a9b1dSJustin T. Gibbs static int		xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
8668b8a9b1dSJustin T. Gibbs 					    void *arg);
867bfc0eb0fSKenneth D. Merry #endif
8688b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptsetasyncfunc;
8698b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptsetasyncbusfunc;
8708b8a9b1dSJustin T. Gibbs static cam_status	xptregister(struct cam_periph *periph,
8718b8a9b1dSJustin T. Gibbs 				    void *arg);
8728b8a9b1dSJustin T. Gibbs static cam_status	proberegister(struct cam_periph *periph,
8738b8a9b1dSJustin T. Gibbs 				      void *arg);
8748b8a9b1dSJustin T. Gibbs static void	 probeschedule(struct cam_periph *probe_periph);
8758b8a9b1dSJustin T. Gibbs static void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
87687cfaf0eSJustin T. Gibbs static void	 proberequestdefaultnegotiation(struct cam_periph *periph);
8772afca7acSMatt Jacob static int       proberequestbackoff(struct cam_periph *periph,
8782afca7acSMatt Jacob 				     struct cam_ed *device);
8798b8a9b1dSJustin T. Gibbs static void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
8808b8a9b1dSJustin T. Gibbs static void	 probecleanup(struct cam_periph *periph);
8818b8a9b1dSJustin T. Gibbs static void	 xpt_find_quirk(struct cam_ed *device);
8823393f8daSKenneth D. Merry static void	 xpt_devise_transport(struct cam_path *path);
8838b8a9b1dSJustin T. Gibbs static void	 xpt_set_transfer_settings(struct ccb_trans_settings *cts,
88403e3511bSJustin T. Gibbs 					   struct cam_ed *device,
8858b8a9b1dSJustin T. Gibbs 					   int async_update);
886f0adc790SJustin T. Gibbs static void	 xpt_toggle_tags(struct cam_path *path);
887fd21cc5eSJustin T. Gibbs static void	 xpt_start_tags(struct cam_path *path);
8888b8a9b1dSJustin T. Gibbs static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
8898b8a9b1dSJustin T. Gibbs 					    struct cam_ed *dev);
8908b8a9b1dSJustin T. Gibbs static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
8918b8a9b1dSJustin T. Gibbs 					   struct cam_ed *dev);
8928b8a9b1dSJustin T. Gibbs static __inline int periph_is_queued(struct cam_periph *periph);
8938b8a9b1dSJustin T. Gibbs static __inline int device_is_alloc_queued(struct cam_ed *device);
8948b8a9b1dSJustin T. Gibbs static __inline int device_is_send_queued(struct cam_ed *device);
8958b8a9b1dSJustin T. Gibbs static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
8968b8a9b1dSJustin T. Gibbs 
8978b8a9b1dSJustin T. Gibbs static __inline int
8988b8a9b1dSJustin T. Gibbs xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
8998b8a9b1dSJustin T. Gibbs {
9008b8a9b1dSJustin T. Gibbs 	int retval;
9018b8a9b1dSJustin T. Gibbs 
9028b8a9b1dSJustin T. Gibbs 	if (dev->ccbq.devq_openings > 0) {
9038b8a9b1dSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
9048b8a9b1dSJustin T. Gibbs 			cam_ccbq_resize(&dev->ccbq,
9058b8a9b1dSJustin T. Gibbs 					dev->ccbq.dev_openings
9068b8a9b1dSJustin T. Gibbs 					+ dev->ccbq.dev_active);
9078b8a9b1dSJustin T. Gibbs 			dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
9088b8a9b1dSJustin T. Gibbs 		}
9095a526431SJustin T. Gibbs 		/*
9105a526431SJustin T. Gibbs 		 * The priority of a device waiting for CCB resources
9115a526431SJustin T. Gibbs 		 * is that of the the highest priority peripheral driver
9125a526431SJustin T. Gibbs 		 * enqueued.
9135a526431SJustin T. Gibbs 		 */
9148b8a9b1dSJustin T. Gibbs 		retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
9158b8a9b1dSJustin T. Gibbs 					  &dev->alloc_ccb_entry.pinfo,
9165a526431SJustin T. Gibbs 					  CAMQ_GET_HEAD(&dev->drvq)->priority);
9178b8a9b1dSJustin T. Gibbs 	} else {
9188b8a9b1dSJustin T. Gibbs 		retval = 0;
9198b8a9b1dSJustin T. Gibbs 	}
9208b8a9b1dSJustin T. Gibbs 
9218b8a9b1dSJustin T. Gibbs 	return (retval);
9228b8a9b1dSJustin T. Gibbs }
9238b8a9b1dSJustin T. Gibbs 
9248b8a9b1dSJustin T. Gibbs static __inline int
9258b8a9b1dSJustin T. Gibbs xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
9268b8a9b1dSJustin T. Gibbs {
9278b8a9b1dSJustin T. Gibbs 	int	retval;
9288b8a9b1dSJustin T. Gibbs 
9298b8a9b1dSJustin T. Gibbs 	if (dev->ccbq.dev_openings > 0) {
9305a526431SJustin T. Gibbs 		/*
9315a526431SJustin T. Gibbs 		 * The priority of a device waiting for controller
9325a526431SJustin T. Gibbs 		 * resources is that of the the highest priority CCB
9335a526431SJustin T. Gibbs 		 * enqueued.
9345a526431SJustin T. Gibbs 		 */
9355a526431SJustin T. Gibbs 		retval =
9365a526431SJustin T. Gibbs 		    xpt_schedule_dev(&bus->sim->devq->send_queue,
9378b8a9b1dSJustin T. Gibbs 				     &dev->send_ccb_entry.pinfo,
9385a526431SJustin T. Gibbs 				     CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
9398b8a9b1dSJustin T. Gibbs 	} else {
9408b8a9b1dSJustin T. Gibbs 		retval = 0;
9418b8a9b1dSJustin T. Gibbs 	}
9428b8a9b1dSJustin T. Gibbs 	return (retval);
9438b8a9b1dSJustin T. Gibbs }
9448b8a9b1dSJustin T. Gibbs 
9458b8a9b1dSJustin T. Gibbs static __inline int
9468b8a9b1dSJustin T. Gibbs periph_is_queued(struct cam_periph *periph)
9478b8a9b1dSJustin T. Gibbs {
9488b8a9b1dSJustin T. Gibbs 	return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
9498b8a9b1dSJustin T. Gibbs }
9508b8a9b1dSJustin T. Gibbs 
9518b8a9b1dSJustin T. Gibbs static __inline int
9528b8a9b1dSJustin T. Gibbs device_is_alloc_queued(struct cam_ed *device)
9538b8a9b1dSJustin T. Gibbs {
9548b8a9b1dSJustin T. Gibbs 	return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
9558b8a9b1dSJustin T. Gibbs }
9568b8a9b1dSJustin T. Gibbs 
9578b8a9b1dSJustin T. Gibbs static __inline int
9588b8a9b1dSJustin T. Gibbs device_is_send_queued(struct cam_ed *device)
9598b8a9b1dSJustin T. Gibbs {
9608b8a9b1dSJustin T. Gibbs 	return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
9618b8a9b1dSJustin T. Gibbs }
9628b8a9b1dSJustin T. Gibbs 
9638b8a9b1dSJustin T. Gibbs static __inline int
9648b8a9b1dSJustin T. Gibbs dev_allocq_is_runnable(struct cam_devq *devq)
9658b8a9b1dSJustin T. Gibbs {
9668b8a9b1dSJustin T. Gibbs 	/*
9678b8a9b1dSJustin T. Gibbs 	 * Have work to do.
9688b8a9b1dSJustin T. Gibbs 	 * Have space to do more work.
9698b8a9b1dSJustin T. Gibbs 	 * Allowed to do work.
9708b8a9b1dSJustin T. Gibbs 	 */
9718b8a9b1dSJustin T. Gibbs 	return ((devq->alloc_queue.qfrozen_cnt == 0)
9728b8a9b1dSJustin T. Gibbs 	     && (devq->alloc_queue.entries > 0)
9738b8a9b1dSJustin T. Gibbs 	     && (devq->alloc_openings > 0));
9748b8a9b1dSJustin T. Gibbs }
9758b8a9b1dSJustin T. Gibbs 
9768b8a9b1dSJustin T. Gibbs static void
9778b8a9b1dSJustin T. Gibbs xpt_periph_init()
9788b8a9b1dSJustin T. Gibbs {
97973d26919SKenneth D. Merry 	make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
9808b8a9b1dSJustin T. Gibbs }
9818b8a9b1dSJustin T. Gibbs 
9828b8a9b1dSJustin T. Gibbs static void
9838b8a9b1dSJustin T. Gibbs probe_periph_init()
9848b8a9b1dSJustin T. Gibbs {
9858b8a9b1dSJustin T. Gibbs }
9868b8a9b1dSJustin T. Gibbs 
9878b8a9b1dSJustin T. Gibbs 
9888b8a9b1dSJustin T. Gibbs static void
9898b8a9b1dSJustin T. Gibbs xptdone(struct cam_periph *periph, union ccb *done_ccb)
9908b8a9b1dSJustin T. Gibbs {
9918b8a9b1dSJustin T. Gibbs 	/* Caller will release the CCB */
9928b8a9b1dSJustin T. Gibbs 	wakeup(&done_ccb->ccb_h.cbfcnp);
9938b8a9b1dSJustin T. Gibbs }
9948b8a9b1dSJustin T. Gibbs 
9958b8a9b1dSJustin T. Gibbs static int
99689c9c53dSPoul-Henning Kamp xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
9978b8a9b1dSJustin T. Gibbs {
9988b8a9b1dSJustin T. Gibbs 	int unit;
9998b8a9b1dSJustin T. Gibbs 
10008b8a9b1dSJustin T. Gibbs 	unit = minor(dev) & 0xff;
10018b8a9b1dSJustin T. Gibbs 
10028b8a9b1dSJustin T. Gibbs 	/*
100366a0780eSKenneth D. Merry 	 * Only allow read-write access.
100466a0780eSKenneth D. Merry 	 */
100566a0780eSKenneth D. Merry 	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
100666a0780eSKenneth D. Merry 		return(EPERM);
100766a0780eSKenneth D. Merry 
100866a0780eSKenneth D. Merry 	/*
10098b8a9b1dSJustin T. Gibbs 	 * We don't allow nonblocking access.
10108b8a9b1dSJustin T. Gibbs 	 */
10118b8a9b1dSJustin T. Gibbs 	if ((flags & O_NONBLOCK) != 0) {
101257b89bbcSNate Lawson 		printf("xpt%d: can't do nonblocking access\n", unit);
10138b8a9b1dSJustin T. Gibbs 		return(ENODEV);
10148b8a9b1dSJustin T. Gibbs 	}
10158b8a9b1dSJustin T. Gibbs 
10168b8a9b1dSJustin T. Gibbs 	/*
10178b8a9b1dSJustin T. Gibbs 	 * We only have one transport layer right now.  If someone accesses
10188b8a9b1dSJustin T. Gibbs 	 * us via something other than minor number 1, point out their
10198b8a9b1dSJustin T. Gibbs 	 * mistake.
10208b8a9b1dSJustin T. Gibbs 	 */
10218b8a9b1dSJustin T. Gibbs 	if (unit != 0) {
10228b8a9b1dSJustin T. Gibbs 		printf("xptopen: got invalid xpt unit %d\n", unit);
10238b8a9b1dSJustin T. Gibbs 		return(ENXIO);
10248b8a9b1dSJustin T. Gibbs 	}
10258b8a9b1dSJustin T. Gibbs 
10268b8a9b1dSJustin T. Gibbs 	/* Mark ourselves open */
10278b8a9b1dSJustin T. Gibbs 	xsoftc.flags |= XPT_FLAG_OPEN;
10288b8a9b1dSJustin T. Gibbs 
10298b8a9b1dSJustin T. Gibbs 	return(0);
10308b8a9b1dSJustin T. Gibbs }
10318b8a9b1dSJustin T. Gibbs 
10328b8a9b1dSJustin T. Gibbs static int
103389c9c53dSPoul-Henning Kamp xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
10348b8a9b1dSJustin T. Gibbs {
10358b8a9b1dSJustin T. Gibbs 	int unit;
10368b8a9b1dSJustin T. Gibbs 
10378b8a9b1dSJustin T. Gibbs 	unit = minor(dev) & 0xff;
10388b8a9b1dSJustin T. Gibbs 
10398b8a9b1dSJustin T. Gibbs 	/*
10408b8a9b1dSJustin T. Gibbs 	 * We only have one transport layer right now.  If someone accesses
10418b8a9b1dSJustin T. Gibbs 	 * us via something other than minor number 1, point out their
10428b8a9b1dSJustin T. Gibbs 	 * mistake.
10438b8a9b1dSJustin T. Gibbs 	 */
10448b8a9b1dSJustin T. Gibbs 	if (unit != 0) {
10458b8a9b1dSJustin T. Gibbs 		printf("xptclose: got invalid xpt unit %d\n", unit);
10468b8a9b1dSJustin T. Gibbs 		return(ENXIO);
10478b8a9b1dSJustin T. Gibbs 	}
10488b8a9b1dSJustin T. Gibbs 
10498b8a9b1dSJustin T. Gibbs 	/* Mark ourselves closed */
10508b8a9b1dSJustin T. Gibbs 	xsoftc.flags &= ~XPT_FLAG_OPEN;
10518b8a9b1dSJustin T. Gibbs 
10528b8a9b1dSJustin T. Gibbs 	return(0);
10538b8a9b1dSJustin T. Gibbs }
10548b8a9b1dSJustin T. Gibbs 
10558b8a9b1dSJustin T. Gibbs static int
105689c9c53dSPoul-Henning Kamp xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
10578b8a9b1dSJustin T. Gibbs {
10588b8a9b1dSJustin T. Gibbs 	int unit, error;
10598b8a9b1dSJustin T. Gibbs 
10608b8a9b1dSJustin T. Gibbs 	error = 0;
10618b8a9b1dSJustin T. Gibbs 	unit = minor(dev) & 0xff;
10628b8a9b1dSJustin T. Gibbs 
10638b8a9b1dSJustin T. Gibbs 	/*
10648b8a9b1dSJustin T. Gibbs 	 * We only have one transport layer right now.  If someone accesses
10658b8a9b1dSJustin T. Gibbs 	 * us via something other than minor number 1, point out their
10668b8a9b1dSJustin T. Gibbs 	 * mistake.
10678b8a9b1dSJustin T. Gibbs 	 */
10688b8a9b1dSJustin T. Gibbs 	if (unit != 0) {
10698b8a9b1dSJustin T. Gibbs 		printf("xptioctl: got invalid xpt unit %d\n", unit);
10708b8a9b1dSJustin T. Gibbs 		return(ENXIO);
10718b8a9b1dSJustin T. Gibbs 	}
10728b8a9b1dSJustin T. Gibbs 
10738b8a9b1dSJustin T. Gibbs 	switch(cmd) {
10748b8a9b1dSJustin T. Gibbs 	/*
10758b8a9b1dSJustin T. Gibbs 	 * For the transport layer CAMIOCOMMAND ioctl, we really only want
10768b8a9b1dSJustin T. Gibbs 	 * to accept CCB types that don't quite make sense to send through a
10778c7a96c5SScott Long 	 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
10788c7a96c5SScott Long 	 * in the CAM spec.
10798b8a9b1dSJustin T. Gibbs 	 */
10808b8a9b1dSJustin T. Gibbs 	case CAMIOCOMMAND: {
10818b8a9b1dSJustin T. Gibbs 		union ccb *ccb;
10828b8a9b1dSJustin T. Gibbs 		union ccb *inccb;
10838b8a9b1dSJustin T. Gibbs 
10848b8a9b1dSJustin T. Gibbs 		inccb = (union ccb *)addr;
10858b8a9b1dSJustin T. Gibbs 
10868b8a9b1dSJustin T. Gibbs 		switch(inccb->ccb_h.func_code) {
10878b8a9b1dSJustin T. Gibbs 		case XPT_SCAN_BUS:
10888b8a9b1dSJustin T. Gibbs 		case XPT_RESET_BUS:
10898b8a9b1dSJustin T. Gibbs 			if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
10908b8a9b1dSJustin T. Gibbs 			 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
10918b8a9b1dSJustin T. Gibbs 				error = EINVAL;
10928b8a9b1dSJustin T. Gibbs 				break;
10938b8a9b1dSJustin T. Gibbs 			}
10948b8a9b1dSJustin T. Gibbs 			/* FALLTHROUGH */
10958c7a96c5SScott Long 		case XPT_PATH_INQ:
10968fcf57f5SJustin T. Gibbs 		case XPT_ENG_INQ:
10978b8a9b1dSJustin T. Gibbs 		case XPT_SCAN_LUN:
10988b8a9b1dSJustin T. Gibbs 
10998b8a9b1dSJustin T. Gibbs 			ccb = xpt_alloc_ccb();
11008b8a9b1dSJustin T. Gibbs 
11018b8a9b1dSJustin T. Gibbs 			/*
11028b8a9b1dSJustin T. Gibbs 			 * Create a path using the bus, target, and lun the
11038b8a9b1dSJustin T. Gibbs 			 * user passed in.
11048b8a9b1dSJustin T. Gibbs 			 */
11058b8a9b1dSJustin T. Gibbs 			if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
11068b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.path_id,
11078b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_id,
11088b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_lun) !=
11098b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP){
11108b8a9b1dSJustin T. Gibbs 				error = EINVAL;
11118b8a9b1dSJustin T. Gibbs 				xpt_free_ccb(ccb);
11128b8a9b1dSJustin T. Gibbs 				break;
11138b8a9b1dSJustin T. Gibbs 			}
11148b8a9b1dSJustin T. Gibbs 			/* Ensure all of our fields are correct */
11158b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
11168b8a9b1dSJustin T. Gibbs 				      inccb->ccb_h.pinfo.priority);
11178b8a9b1dSJustin T. Gibbs 			xpt_merge_ccb(ccb, inccb);
11188b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.cbfcnp = xptdone;
11198b8a9b1dSJustin T. Gibbs 			cam_periph_runccb(ccb, NULL, 0, 0, NULL);
11208b8a9b1dSJustin T. Gibbs 			bcopy(ccb, inccb, sizeof(union ccb));
11218b8a9b1dSJustin T. Gibbs 			xpt_free_path(ccb->ccb_h.path);
11228b8a9b1dSJustin T. Gibbs 			xpt_free_ccb(ccb);
11238b8a9b1dSJustin T. Gibbs 			break;
11248b8a9b1dSJustin T. Gibbs 
11258b8a9b1dSJustin T. Gibbs 		case XPT_DEBUG: {
11268b8a9b1dSJustin T. Gibbs 			union ccb ccb;
11278b8a9b1dSJustin T. Gibbs 
11288b8a9b1dSJustin T. Gibbs 			/*
1129aa872be6SMatt Jacob 			 * This is an immediate CCB, so it's okay to
11308b8a9b1dSJustin T. Gibbs 			 * allocate it on the stack.
11318b8a9b1dSJustin T. Gibbs 			 */
11328b8a9b1dSJustin T. Gibbs 
11338b8a9b1dSJustin T. Gibbs 			/*
11348b8a9b1dSJustin T. Gibbs 			 * Create a path using the bus, target, and lun the
11358b8a9b1dSJustin T. Gibbs 			 * user passed in.
11368b8a9b1dSJustin T. Gibbs 			 */
11378b8a9b1dSJustin T. Gibbs 			if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
11388b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.path_id,
11398b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_id,
11408b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_lun) !=
11418b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP){
11428b8a9b1dSJustin T. Gibbs 				error = EINVAL;
11438b8a9b1dSJustin T. Gibbs 				break;
11448b8a9b1dSJustin T. Gibbs 			}
11458b8a9b1dSJustin T. Gibbs 			/* Ensure all of our fields are correct */
11468b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
11478b8a9b1dSJustin T. Gibbs 				      inccb->ccb_h.pinfo.priority);
11488b8a9b1dSJustin T. Gibbs 			xpt_merge_ccb(&ccb, inccb);
11498b8a9b1dSJustin T. Gibbs 			ccb.ccb_h.cbfcnp = xptdone;
11508b8a9b1dSJustin T. Gibbs 			xpt_action(&ccb);
11518b8a9b1dSJustin T. Gibbs 			bcopy(&ccb, inccb, sizeof(union ccb));
11528b8a9b1dSJustin T. Gibbs 			xpt_free_path(ccb.ccb_h.path);
11538b8a9b1dSJustin T. Gibbs 			break;
11548b8a9b1dSJustin T. Gibbs 
11558b8a9b1dSJustin T. Gibbs 		}
11568b8a9b1dSJustin T. Gibbs 		case XPT_DEV_MATCH: {
11578b8a9b1dSJustin T. Gibbs 			struct cam_periph_map_info mapinfo;
115859190eaaSKenneth D. Merry 			struct cam_path *old_path;
11598b8a9b1dSJustin T. Gibbs 
11608b8a9b1dSJustin T. Gibbs 			/*
11618b8a9b1dSJustin T. Gibbs 			 * We can't deal with physical addresses for this
11628b8a9b1dSJustin T. Gibbs 			 * type of transaction.
11638b8a9b1dSJustin T. Gibbs 			 */
11648b8a9b1dSJustin T. Gibbs 			if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
11658b8a9b1dSJustin T. Gibbs 				error = EINVAL;
11668b8a9b1dSJustin T. Gibbs 				break;
11678b8a9b1dSJustin T. Gibbs 			}
116859190eaaSKenneth D. Merry 
116959190eaaSKenneth D. Merry 			/*
117059190eaaSKenneth D. Merry 			 * Save this in case the caller had it set to
117159190eaaSKenneth D. Merry 			 * something in particular.
117259190eaaSKenneth D. Merry 			 */
117359190eaaSKenneth D. Merry 			old_path = inccb->ccb_h.path;
117459190eaaSKenneth D. Merry 
117559190eaaSKenneth D. Merry 			/*
117659190eaaSKenneth D. Merry 			 * We really don't need a path for the matching
117759190eaaSKenneth D. Merry 			 * code.  The path is needed because of the
117859190eaaSKenneth D. Merry 			 * debugging statements in xpt_action().  They
117959190eaaSKenneth D. Merry 			 * assume that the CCB has a valid path.
118059190eaaSKenneth D. Merry 			 */
118159190eaaSKenneth D. Merry 			inccb->ccb_h.path = xpt_periph->path;
118259190eaaSKenneth D. Merry 
11838b8a9b1dSJustin T. Gibbs 			bzero(&mapinfo, sizeof(mapinfo));
11848b8a9b1dSJustin T. Gibbs 
11858b8a9b1dSJustin T. Gibbs 			/*
11868b8a9b1dSJustin T. Gibbs 			 * Map the pattern and match buffers into kernel
11878b8a9b1dSJustin T. Gibbs 			 * virtual address space.
11888b8a9b1dSJustin T. Gibbs 			 */
11898b8a9b1dSJustin T. Gibbs 			error = cam_periph_mapmem(inccb, &mapinfo);
11908b8a9b1dSJustin T. Gibbs 
119159190eaaSKenneth D. Merry 			if (error) {
119259190eaaSKenneth D. Merry 				inccb->ccb_h.path = old_path;
11938b8a9b1dSJustin T. Gibbs 				break;
119459190eaaSKenneth D. Merry 			}
11958b8a9b1dSJustin T. Gibbs 
11968b8a9b1dSJustin T. Gibbs 			/*
11978b8a9b1dSJustin T. Gibbs 			 * This is an immediate CCB, we can send it on directly.
11988b8a9b1dSJustin T. Gibbs 			 */
11998b8a9b1dSJustin T. Gibbs 			xpt_action(inccb);
12008b8a9b1dSJustin T. Gibbs 
12018b8a9b1dSJustin T. Gibbs 			/*
12028b8a9b1dSJustin T. Gibbs 			 * Map the buffers back into user space.
12038b8a9b1dSJustin T. Gibbs 			 */
12048b8a9b1dSJustin T. Gibbs 			cam_periph_unmapmem(inccb, &mapinfo);
12058b8a9b1dSJustin T. Gibbs 
120659190eaaSKenneth D. Merry 			inccb->ccb_h.path = old_path;
120759190eaaSKenneth D. Merry 
12088b8a9b1dSJustin T. Gibbs 			error = 0;
12098b8a9b1dSJustin T. Gibbs 			break;
12108b8a9b1dSJustin T. Gibbs 		}
12118b8a9b1dSJustin T. Gibbs 		default:
12128fcf57f5SJustin T. Gibbs 			error = ENOTSUP;
12138b8a9b1dSJustin T. Gibbs 			break;
12148b8a9b1dSJustin T. Gibbs 		}
12158b8a9b1dSJustin T. Gibbs 		break;
12168b8a9b1dSJustin T. Gibbs 	}
12178b8a9b1dSJustin T. Gibbs 	/*
12188b8a9b1dSJustin T. Gibbs 	 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
12198b8a9b1dSJustin T. Gibbs 	 * with the periphal driver name and unit name filled in.  The other
12208b8a9b1dSJustin T. Gibbs 	 * fields don't really matter as input.  The passthrough driver name
12218b8a9b1dSJustin T. Gibbs 	 * ("pass"), and unit number are passed back in the ccb.  The current
12228b8a9b1dSJustin T. Gibbs 	 * device generation number, and the index into the device peripheral
12238b8a9b1dSJustin T. Gibbs 	 * driver list, and the status are also passed back.  Note that
12248b8a9b1dSJustin T. Gibbs 	 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
12258b8a9b1dSJustin T. Gibbs 	 * we never return a status of CAM_GDEVLIST_LIST_CHANGED.  It is
12268b8a9b1dSJustin T. Gibbs 	 * (or rather should be) impossible for the device peripheral driver
12278b8a9b1dSJustin T. Gibbs 	 * list to change since we look at the whole thing in one pass, and
1228a5479bc5SJustin T. Gibbs 	 * we do it with splcam protection.
12298b8a9b1dSJustin T. Gibbs 	 *
12308b8a9b1dSJustin T. Gibbs 	 */
12318b8a9b1dSJustin T. Gibbs 	case CAMGETPASSTHRU: {
12328b8a9b1dSJustin T. Gibbs 		union ccb *ccb;
12338b8a9b1dSJustin T. Gibbs 		struct cam_periph *periph;
12348b8a9b1dSJustin T. Gibbs 		struct periph_driver **p_drv;
12358b8a9b1dSJustin T. Gibbs 		char   *name;
12363393f8daSKenneth D. Merry 		u_int unit;
12373393f8daSKenneth D. Merry 		u_int cur_generation;
1238621a60d4SKenneth D. Merry 		int base_periph_found;
12398b8a9b1dSJustin T. Gibbs 		int splbreaknum;
12408b8a9b1dSJustin T. Gibbs 		int s;
12418b8a9b1dSJustin T. Gibbs 
12428b8a9b1dSJustin T. Gibbs 		ccb = (union ccb *)addr;
12438b8a9b1dSJustin T. Gibbs 		unit = ccb->cgdl.unit_number;
12448b8a9b1dSJustin T. Gibbs 		name = ccb->cgdl.periph_name;
12458b8a9b1dSJustin T. Gibbs 		/*
12468b8a9b1dSJustin T. Gibbs 		 * Every 100 devices, we want to drop our spl protection to
12478b8a9b1dSJustin T. Gibbs 		 * give the software interrupt handler a chance to run.
12488b8a9b1dSJustin T. Gibbs 		 * Most systems won't run into this check, but this should
12498b8a9b1dSJustin T. Gibbs 		 * avoid starvation in the software interrupt handler in
12508b8a9b1dSJustin T. Gibbs 		 * large systems.
12518b8a9b1dSJustin T. Gibbs 		 */
12528b8a9b1dSJustin T. Gibbs 		splbreaknum = 100;
12538b8a9b1dSJustin T. Gibbs 
12548b8a9b1dSJustin T. Gibbs 		ccb = (union ccb *)addr;
12558b8a9b1dSJustin T. Gibbs 
1256621a60d4SKenneth D. Merry 		base_periph_found = 0;
1257621a60d4SKenneth D. Merry 
12588b8a9b1dSJustin T. Gibbs 		/*
12598b8a9b1dSJustin T. Gibbs 		 * Sanity check -- make sure we don't get a null peripheral
12608b8a9b1dSJustin T. Gibbs 		 * driver name.
12618b8a9b1dSJustin T. Gibbs 		 */
12628b8a9b1dSJustin T. Gibbs 		if (*ccb->cgdl.periph_name == '\0') {
12638b8a9b1dSJustin T. Gibbs 			error = EINVAL;
12648b8a9b1dSJustin T. Gibbs 			break;
12658b8a9b1dSJustin T. Gibbs 		}
12668b8a9b1dSJustin T. Gibbs 
12678b8a9b1dSJustin T. Gibbs 		/* Keep the list from changing while we traverse it */
1268a5479bc5SJustin T. Gibbs 		s = splcam();
12698b8a9b1dSJustin T. Gibbs ptstartover:
12708b8a9b1dSJustin T. Gibbs 		cur_generation = xsoftc.generation;
12718b8a9b1dSJustin T. Gibbs 
12728b8a9b1dSJustin T. Gibbs 		/* first find our driver in the list of drivers */
12730b7c27b9SPeter Wemm 		for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
12748b8a9b1dSJustin T. Gibbs 			if (strcmp((*p_drv)->driver_name, name) == 0)
12758b8a9b1dSJustin T. Gibbs 				break;
12768b8a9b1dSJustin T. Gibbs 
12778b8a9b1dSJustin T. Gibbs 		if (*p_drv == NULL) {
12788b8a9b1dSJustin T. Gibbs 			splx(s);
12798b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
12808b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
12818b8a9b1dSJustin T. Gibbs 			*ccb->cgdl.periph_name = '\0';
12828b8a9b1dSJustin T. Gibbs 			ccb->cgdl.unit_number = 0;
12838b8a9b1dSJustin T. Gibbs 			error = ENOENT;
12848b8a9b1dSJustin T. Gibbs 			break;
12858b8a9b1dSJustin T. Gibbs 		}
12868b8a9b1dSJustin T. Gibbs 
12878b8a9b1dSJustin T. Gibbs 		/*
12888b8a9b1dSJustin T. Gibbs 		 * Run through every peripheral instance of this driver
12898b8a9b1dSJustin T. Gibbs 		 * and check to see whether it matches the unit passed
12908b8a9b1dSJustin T. Gibbs 		 * in by the user.  If it does, get out of the loops and
12918b8a9b1dSJustin T. Gibbs 		 * find the passthrough driver associated with that
12928b8a9b1dSJustin T. Gibbs 		 * peripheral driver.
12938b8a9b1dSJustin T. Gibbs 		 */
12948b8a9b1dSJustin T. Gibbs 		for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
12958b8a9b1dSJustin T. Gibbs 		     periph = TAILQ_NEXT(periph, unit_links)) {
12968b8a9b1dSJustin T. Gibbs 
12978b8a9b1dSJustin T. Gibbs 			if (periph->unit_number == unit) {
12988b8a9b1dSJustin T. Gibbs 				break;
12998b8a9b1dSJustin T. Gibbs 			} else if (--splbreaknum == 0) {
13008b8a9b1dSJustin T. Gibbs 				splx(s);
1301a5479bc5SJustin T. Gibbs 				s = splcam();
13028b8a9b1dSJustin T. Gibbs 				splbreaknum = 100;
13038b8a9b1dSJustin T. Gibbs 				if (cur_generation != xsoftc.generation)
13048b8a9b1dSJustin T. Gibbs 				       goto ptstartover;
13058b8a9b1dSJustin T. Gibbs 			}
13068b8a9b1dSJustin T. Gibbs 		}
13078b8a9b1dSJustin T. Gibbs 		/*
13088b8a9b1dSJustin T. Gibbs 		 * If we found the peripheral driver that the user passed
13098b8a9b1dSJustin T. Gibbs 		 * in, go through all of the peripheral drivers for that
13108b8a9b1dSJustin T. Gibbs 		 * particular device and look for a passthrough driver.
13118b8a9b1dSJustin T. Gibbs 		 */
13128b8a9b1dSJustin T. Gibbs 		if (periph != NULL) {
13138b8a9b1dSJustin T. Gibbs 			struct cam_ed *device;
13148b8a9b1dSJustin T. Gibbs 			int i;
13158b8a9b1dSJustin T. Gibbs 
1316621a60d4SKenneth D. Merry 			base_periph_found = 1;
13178b8a9b1dSJustin T. Gibbs 			device = periph->path->device;
1318fc2ffbe6SPoul-Henning Kamp 			for (i = 0, periph = SLIST_FIRST(&device->periphs);
13198b8a9b1dSJustin T. Gibbs 			     periph != NULL;
1320fc2ffbe6SPoul-Henning Kamp 			     periph = SLIST_NEXT(periph, periph_links), i++) {
13218b8a9b1dSJustin T. Gibbs 				/*
13228b8a9b1dSJustin T. Gibbs 				 * Check to see whether we have a
13238b8a9b1dSJustin T. Gibbs 				 * passthrough device or not.
13248b8a9b1dSJustin T. Gibbs 				 */
13258b8a9b1dSJustin T. Gibbs 				if (strcmp(periph->periph_name, "pass") == 0) {
13268b8a9b1dSJustin T. Gibbs 					/*
13278b8a9b1dSJustin T. Gibbs 					 * Fill in the getdevlist fields.
13288b8a9b1dSJustin T. Gibbs 					 */
13298b8a9b1dSJustin T. Gibbs 					strcpy(ccb->cgdl.periph_name,
13308b8a9b1dSJustin T. Gibbs 					       periph->periph_name);
13318b8a9b1dSJustin T. Gibbs 					ccb->cgdl.unit_number =
13328b8a9b1dSJustin T. Gibbs 						periph->unit_number;
1333fc2ffbe6SPoul-Henning Kamp 					if (SLIST_NEXT(periph, periph_links))
13348b8a9b1dSJustin T. Gibbs 						ccb->cgdl.status =
13358b8a9b1dSJustin T. Gibbs 							CAM_GDEVLIST_MORE_DEVS;
13368b8a9b1dSJustin T. Gibbs 					else
13378b8a9b1dSJustin T. Gibbs 						ccb->cgdl.status =
13388b8a9b1dSJustin T. Gibbs 						       CAM_GDEVLIST_LAST_DEVICE;
13398b8a9b1dSJustin T. Gibbs 					ccb->cgdl.generation =
13408b8a9b1dSJustin T. Gibbs 						device->generation;
13418b8a9b1dSJustin T. Gibbs 					ccb->cgdl.index = i;
13428b8a9b1dSJustin T. Gibbs 					/*
13438b8a9b1dSJustin T. Gibbs 					 * Fill in some CCB header fields
13448b8a9b1dSJustin T. Gibbs 					 * that the user may want.
13458b8a9b1dSJustin T. Gibbs 					 */
13468b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.path_id =
13478b8a9b1dSJustin T. Gibbs 						periph->path->bus->path_id;
13488b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.target_id =
13498b8a9b1dSJustin T. Gibbs 						periph->path->target->target_id;
13508b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.target_lun =
13518b8a9b1dSJustin T. Gibbs 						periph->path->device->lun_id;
13528b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.status = CAM_REQ_CMP;
13538b8a9b1dSJustin T. Gibbs 					break;
13548b8a9b1dSJustin T. Gibbs 				}
13558b8a9b1dSJustin T. Gibbs 			}
13568b8a9b1dSJustin T. Gibbs 		}
13578b8a9b1dSJustin T. Gibbs 
13588b8a9b1dSJustin T. Gibbs 		/*
13598b8a9b1dSJustin T. Gibbs 		 * If the periph is null here, one of two things has
13608b8a9b1dSJustin T. Gibbs 		 * happened.  The first possibility is that we couldn't
13618b8a9b1dSJustin T. Gibbs 		 * find the unit number of the particular peripheral driver
13628b8a9b1dSJustin T. Gibbs 		 * that the user is asking about.  e.g. the user asks for
13638b8a9b1dSJustin T. Gibbs 		 * the passthrough driver for "da11".  We find the list of
13648b8a9b1dSJustin T. Gibbs 		 * "da" peripherals all right, but there is no unit 11.
13658b8a9b1dSJustin T. Gibbs 		 * The other possibility is that we went through the list
13668b8a9b1dSJustin T. Gibbs 		 * of peripheral drivers attached to the device structure,
13678b8a9b1dSJustin T. Gibbs 		 * but didn't find one with the name "pass".  Either way,
13688b8a9b1dSJustin T. Gibbs 		 * we return ENOENT, since we couldn't find something.
13698b8a9b1dSJustin T. Gibbs 		 */
13708b8a9b1dSJustin T. Gibbs 		if (periph == NULL) {
13718b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
13728b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
13738b8a9b1dSJustin T. Gibbs 			*ccb->cgdl.periph_name = '\0';
13748b8a9b1dSJustin T. Gibbs 			ccb->cgdl.unit_number = 0;
13758b8a9b1dSJustin T. Gibbs 			error = ENOENT;
1376621a60d4SKenneth D. Merry 			/*
1377621a60d4SKenneth D. Merry 			 * It is unfortunate that this is even necessary,
1378621a60d4SKenneth D. Merry 			 * but there are many, many clueless users out there.
1379621a60d4SKenneth D. Merry 			 * If this is true, the user is looking for the
1380621a60d4SKenneth D. Merry 			 * passthrough driver, but doesn't have one in his
1381621a60d4SKenneth D. Merry 			 * kernel.
1382621a60d4SKenneth D. Merry 			 */
1383621a60d4SKenneth D. Merry 			if (base_periph_found == 1) {
1384621a60d4SKenneth D. Merry 				printf("xptioctl: pass driver is not in the "
1385621a60d4SKenneth D. Merry 				       "kernel\n");
1386621a60d4SKenneth D. Merry 				printf("xptioctl: put \"device pass0\" in "
1387621a60d4SKenneth D. Merry 				       "your kernel config file\n");
1388621a60d4SKenneth D. Merry 			}
13898b8a9b1dSJustin T. Gibbs 		}
13908b8a9b1dSJustin T. Gibbs 		splx(s);
13918b8a9b1dSJustin T. Gibbs 		break;
13928b8a9b1dSJustin T. Gibbs 		}
13938b8a9b1dSJustin T. Gibbs 	default:
13948b8a9b1dSJustin T. Gibbs 		error = ENOTTY;
13958b8a9b1dSJustin T. Gibbs 		break;
13968b8a9b1dSJustin T. Gibbs 	}
13978b8a9b1dSJustin T. Gibbs 
13988b8a9b1dSJustin T. Gibbs 	return(error);
13998b8a9b1dSJustin T. Gibbs }
14008b8a9b1dSJustin T. Gibbs 
140174bd1c10SNick Hibma static int
140274bd1c10SNick Hibma cam_module_event_handler(module_t mod, int what, void *arg)
140374bd1c10SNick Hibma {
140474bd1c10SNick Hibma 	if (what == MOD_LOAD) {
140574bd1c10SNick Hibma 		xpt_init(NULL);
140674bd1c10SNick Hibma 	} else if (what == MOD_UNLOAD) {
140774bd1c10SNick Hibma 		return EBUSY;
14083e019deaSPoul-Henning Kamp 	} else {
14093e019deaSPoul-Henning Kamp 		return EOPNOTSUPP;
141074bd1c10SNick Hibma 	}
141174bd1c10SNick Hibma 
141274bd1c10SNick Hibma 	return 0;
141374bd1c10SNick Hibma }
141474bd1c10SNick Hibma 
14158b8a9b1dSJustin T. Gibbs /* Functions accessed by the peripheral drivers */
1416667d00a7SMatt Jacob static void
14176d2a8f1cSPeter Wemm xpt_init(dummy)
14186d2a8f1cSPeter Wemm 	void *dummy;
14198b8a9b1dSJustin T. Gibbs {
14208b8a9b1dSJustin T. Gibbs 	struct cam_sim *xpt_sim;
14218b8a9b1dSJustin T. Gibbs 	struct cam_path *path;
1422434bbf6eSJustin T. Gibbs 	struct cam_devq *devq;
14238b8a9b1dSJustin T. Gibbs 	cam_status status;
14248b8a9b1dSJustin T. Gibbs 
14258b8a9b1dSJustin T. Gibbs 	TAILQ_INIT(&xpt_busses);
14268b8a9b1dSJustin T. Gibbs 	TAILQ_INIT(&cam_bioq);
14278b8a9b1dSJustin T. Gibbs 	SLIST_INIT(&ccb_freeq);
14288b8a9b1dSJustin T. Gibbs 	STAILQ_INIT(&highpowerq);
14298b8a9b1dSJustin T. Gibbs 
1430ef3cf714SScott Long 	mtx_init(&cam_bioq_lock, "CAM BIOQ lock", NULL, MTX_DEF);
1431ef3cf714SScott Long 
14328b8a9b1dSJustin T. Gibbs 	/*
14338b8a9b1dSJustin T. Gibbs 	 * The xpt layer is, itself, the equivelent of a SIM.
14348b8a9b1dSJustin T. Gibbs 	 * Allow 16 ccbs in the ccb pool for it.  This should
14358b8a9b1dSJustin T. Gibbs 	 * give decent parallelism when we probe busses and
14368b8a9b1dSJustin T. Gibbs 	 * perform other XPT functions.
14378b8a9b1dSJustin T. Gibbs 	 */
1438434bbf6eSJustin T. Gibbs 	devq = cam_simq_alloc(16);
1439434bbf6eSJustin T. Gibbs 	xpt_sim = cam_sim_alloc(xptaction,
1440434bbf6eSJustin T. Gibbs 				xptpoll,
1441434bbf6eSJustin T. Gibbs 				"xpt",
1442434bbf6eSJustin T. Gibbs 				/*softc*/NULL,
1443434bbf6eSJustin T. Gibbs 				/*unit*/0,
1444434bbf6eSJustin T. Gibbs 				/*max_dev_transactions*/0,
1445434bbf6eSJustin T. Gibbs 				/*max_tagged_dev_transactions*/0,
1446434bbf6eSJustin T. Gibbs 				devq);
14478b8a9b1dSJustin T. Gibbs 	xpt_max_ccbs = 16;
14488b8a9b1dSJustin T. Gibbs 
1449df826980SMatt Jacob 	if ((status = xpt_bus_register(xpt_sim, /*bus #*/0)) != CAM_SUCCESS) {
1450a2821e04SMatt Jacob 		printf("xpt_init: xpt_bus_register failed with status %#x,"
1451df826980SMatt Jacob 		       " failing attach\n", status);
1452df826980SMatt Jacob 		return;
1453df826980SMatt Jacob 	}
14548b8a9b1dSJustin T. Gibbs 
14558b8a9b1dSJustin T. Gibbs 	/*
14568b8a9b1dSJustin T. Gibbs 	 * Looking at the XPT from the SIM layer, the XPT is
14578b8a9b1dSJustin T. Gibbs 	 * the equivelent of a peripheral driver.  Allocate
14588b8a9b1dSJustin T. Gibbs 	 * a peripheral driver entry for us.
14598b8a9b1dSJustin T. Gibbs 	 */
14608b8a9b1dSJustin T. Gibbs 	if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
14618b8a9b1dSJustin T. Gibbs 				      CAM_TARGET_WILDCARD,
14628b8a9b1dSJustin T. Gibbs 				      CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
14638b8a9b1dSJustin T. Gibbs 		printf("xpt_init: xpt_create_path failed with status %#x,"
14648b8a9b1dSJustin T. Gibbs 		       " failing attach\n", status);
14658b8a9b1dSJustin T. Gibbs 		return;
14668b8a9b1dSJustin T. Gibbs 	}
14678b8a9b1dSJustin T. Gibbs 
1468ee9c90c7SKenneth D. Merry 	cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
14698b8a9b1dSJustin T. Gibbs 			 path, NULL, 0, NULL);
14708b8a9b1dSJustin T. Gibbs 	xpt_free_path(path);
14718b8a9b1dSJustin T. Gibbs 
14728b8a9b1dSJustin T. Gibbs 	xpt_sim->softc = xpt_periph;
14738b8a9b1dSJustin T. Gibbs 
14748b8a9b1dSJustin T. Gibbs 	/*
14758b8a9b1dSJustin T. Gibbs 	 * Register a callback for when interrupts are enabled.
14768b8a9b1dSJustin T. Gibbs 	 */
14778b8a9b1dSJustin T. Gibbs 	xpt_config_hook =
14788b8a9b1dSJustin T. Gibbs 	    (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
14795417ec4dSDavid Malone 					      M_TEMP, M_NOWAIT | M_ZERO);
14808b8a9b1dSJustin T. Gibbs 	if (xpt_config_hook == NULL) {
14818b8a9b1dSJustin T. Gibbs 		printf("xpt_init: Cannot malloc config hook "
14828b8a9b1dSJustin T. Gibbs 		       "- failing attach\n");
14838b8a9b1dSJustin T. Gibbs 		return;
14848b8a9b1dSJustin T. Gibbs 	}
14858b8a9b1dSJustin T. Gibbs 
14868b8a9b1dSJustin T. Gibbs 	xpt_config_hook->ich_func = xpt_config;
14878b8a9b1dSJustin T. Gibbs 	if (config_intrhook_establish(xpt_config_hook) != 0) {
14888b8a9b1dSJustin T. Gibbs 		free (xpt_config_hook, M_TEMP);
14898b8a9b1dSJustin T. Gibbs 		printf("xpt_init: config_intrhook_establish failed "
14908b8a9b1dSJustin T. Gibbs 		       "- failing attach\n");
14918b8a9b1dSJustin T. Gibbs 	}
14928b8a9b1dSJustin T. Gibbs 
14938b8a9b1dSJustin T. Gibbs 	/* Install our software interrupt handlers */
1494062d8ff5SJohn Baldwin 	swi_add(NULL, "cambio", camisr, &cam_bioq, SWI_CAMBIO, 0, &cambio_ih);
14958b8a9b1dSJustin T. Gibbs }
14968b8a9b1dSJustin T. Gibbs 
14978b8a9b1dSJustin T. Gibbs static cam_status
14988b8a9b1dSJustin T. Gibbs xptregister(struct cam_periph *periph, void *arg)
14998b8a9b1dSJustin T. Gibbs {
15008b8a9b1dSJustin T. Gibbs 	if (periph == NULL) {
15018b8a9b1dSJustin T. Gibbs 		printf("xptregister: periph was NULL!!\n");
15028b8a9b1dSJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
15038b8a9b1dSJustin T. Gibbs 	}
15048b8a9b1dSJustin T. Gibbs 
15058b8a9b1dSJustin T. Gibbs 	periph->softc = NULL;
15068b8a9b1dSJustin T. Gibbs 
15078b8a9b1dSJustin T. Gibbs 	xpt_periph = periph;
15088b8a9b1dSJustin T. Gibbs 
15098b8a9b1dSJustin T. Gibbs 	return(CAM_REQ_CMP);
15108b8a9b1dSJustin T. Gibbs }
15118b8a9b1dSJustin T. Gibbs 
15128b8a9b1dSJustin T. Gibbs int32_t
15138b8a9b1dSJustin T. Gibbs xpt_add_periph(struct cam_periph *periph)
15148b8a9b1dSJustin T. Gibbs {
15158b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
15168b8a9b1dSJustin T. Gibbs 	int32_t	 status;
15178b8a9b1dSJustin T. Gibbs 	struct periph_list *periph_head;
15188b8a9b1dSJustin T. Gibbs 
151968153f43SScott Long 	GIANT_REQUIRED;
152068153f43SScott Long 
15218b8a9b1dSJustin T. Gibbs 	device = periph->path->device;
15228b8a9b1dSJustin T. Gibbs 
15238b8a9b1dSJustin T. Gibbs 	periph_head = &device->periphs;
15248b8a9b1dSJustin T. Gibbs 
15258b8a9b1dSJustin T. Gibbs 	status = CAM_REQ_CMP;
15268b8a9b1dSJustin T. Gibbs 
15278b8a9b1dSJustin T. Gibbs 	if (device != NULL) {
15288b8a9b1dSJustin T. Gibbs 		int s;
15298b8a9b1dSJustin T. Gibbs 
15308b8a9b1dSJustin T. Gibbs 		/*
15318b8a9b1dSJustin T. Gibbs 		 * Make room for this peripheral
15328b8a9b1dSJustin T. Gibbs 		 * so it will fit in the queue
15338b8a9b1dSJustin T. Gibbs 		 * when it's scheduled to run
15348b8a9b1dSJustin T. Gibbs 		 */
15358b8a9b1dSJustin T. Gibbs 		s = splsoftcam();
15368b8a9b1dSJustin T. Gibbs 		status = camq_resize(&device->drvq,
15378b8a9b1dSJustin T. Gibbs 				     device->drvq.array_size + 1);
15388b8a9b1dSJustin T. Gibbs 
15398b8a9b1dSJustin T. Gibbs 		device->generation++;
15408b8a9b1dSJustin T. Gibbs 
15418b8a9b1dSJustin T. Gibbs 		SLIST_INSERT_HEAD(periph_head, periph, periph_links);
15428b8a9b1dSJustin T. Gibbs 
15438b8a9b1dSJustin T. Gibbs 		splx(s);
15448b8a9b1dSJustin T. Gibbs 	}
15458b8a9b1dSJustin T. Gibbs 
15468b8a9b1dSJustin T. Gibbs 	xsoftc.generation++;
15478b8a9b1dSJustin T. Gibbs 
15488b8a9b1dSJustin T. Gibbs 	return (status);
15498b8a9b1dSJustin T. Gibbs }
15508b8a9b1dSJustin T. Gibbs 
15518b8a9b1dSJustin T. Gibbs void
15528b8a9b1dSJustin T. Gibbs xpt_remove_periph(struct cam_periph *periph)
15538b8a9b1dSJustin T. Gibbs {
15548b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
15558b8a9b1dSJustin T. Gibbs 
155668153f43SScott Long 	GIANT_REQUIRED;
155768153f43SScott Long 
15588b8a9b1dSJustin T. Gibbs 	device = periph->path->device;
15598b8a9b1dSJustin T. Gibbs 
15608b8a9b1dSJustin T. Gibbs 	if (device != NULL) {
15618b8a9b1dSJustin T. Gibbs 		int s;
15628b8a9b1dSJustin T. Gibbs 		struct periph_list *periph_head;
15638b8a9b1dSJustin T. Gibbs 
15648b8a9b1dSJustin T. Gibbs 		periph_head = &device->periphs;
15658b8a9b1dSJustin T. Gibbs 
15668b8a9b1dSJustin T. Gibbs 		/* Release the slot for this peripheral */
15678b8a9b1dSJustin T. Gibbs 		s = splsoftcam();
15688b8a9b1dSJustin T. Gibbs 		camq_resize(&device->drvq, device->drvq.array_size - 1);
15698b8a9b1dSJustin T. Gibbs 
15708b8a9b1dSJustin T. Gibbs 		device->generation++;
15718b8a9b1dSJustin T. Gibbs 
1572e3975643SJake Burkholder 		SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
15738b8a9b1dSJustin T. Gibbs 
15748b8a9b1dSJustin T. Gibbs 		splx(s);
15758b8a9b1dSJustin T. Gibbs 	}
15768b8a9b1dSJustin T. Gibbs 
15778b8a9b1dSJustin T. Gibbs 	xsoftc.generation++;
15788b8a9b1dSJustin T. Gibbs 
15798b8a9b1dSJustin T. Gibbs }
15808b8a9b1dSJustin T. Gibbs 
15813393f8daSKenneth D. Merry 
15823393f8daSKenneth D. Merry void
15833393f8daSKenneth D. Merry xpt_announce_periph(struct cam_periph *periph, char *announce_string)
15843393f8daSKenneth D. Merry {
15853393f8daSKenneth D. Merry 	struct	ccb_pathinq cpi;
15863393f8daSKenneth D. Merry 	struct	ccb_trans_settings cts;
15873393f8daSKenneth D. Merry 	struct	cam_path *path;
15883393f8daSKenneth D. Merry 	u_int	speed;
15893393f8daSKenneth D. Merry 	u_int	freq;
15903393f8daSKenneth D. Merry 	u_int	mb;
15913393f8daSKenneth D. Merry 	int	s;
15923393f8daSKenneth D. Merry 
159368153f43SScott Long 	GIANT_REQUIRED;
159468153f43SScott Long 
15953393f8daSKenneth D. Merry 	path = periph->path;
15963393f8daSKenneth D. Merry 	/*
15973393f8daSKenneth D. Merry 	 * To ensure that this is printed in one piece,
15983393f8daSKenneth D. Merry 	 * mask out CAM interrupts.
15993393f8daSKenneth D. Merry 	 */
16003393f8daSKenneth D. Merry 	s = splsoftcam();
16013393f8daSKenneth D. Merry 	printf("%s%d at %s%d bus %d target %d lun %d\n",
16023393f8daSKenneth D. Merry 	       periph->periph_name, periph->unit_number,
16033393f8daSKenneth D. Merry 	       path->bus->sim->sim_name,
16043393f8daSKenneth D. Merry 	       path->bus->sim->unit_number,
16053393f8daSKenneth D. Merry 	       path->bus->sim->bus_id,
16063393f8daSKenneth D. Merry 	       path->target->target_id,
16073393f8daSKenneth D. Merry 	       path->device->lun_id);
16083393f8daSKenneth D. Merry 	printf("%s%d: ", periph->periph_name, periph->unit_number);
16093393f8daSKenneth D. Merry 	scsi_print_inquiry(&path->device->inq_data);
1610f053d777SMatt Jacob 	if (bootverbose && path->device->serial_num_len > 0) {
16113393f8daSKenneth D. Merry 		/* Don't wrap the screen  - print only the first 60 chars */
16123393f8daSKenneth D. Merry 		printf("%s%d: Serial Number %.60s\n", periph->periph_name,
16133393f8daSKenneth D. Merry 		       periph->unit_number, path->device->serial_num);
16143393f8daSKenneth D. Merry 	}
16153393f8daSKenneth D. Merry 	xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
16163393f8daSKenneth D. Merry 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
16173393f8daSKenneth D. Merry 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
16183393f8daSKenneth D. Merry 	xpt_action((union ccb*)&cts);
16190a480cf0SMatt Jacob 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
16200a480cf0SMatt Jacob 		return;
16210a480cf0SMatt Jacob 	}
16223393f8daSKenneth D. Merry 
16233393f8daSKenneth D. Merry 	/* Ask the SIM for its base transfer speed */
16243393f8daSKenneth D. Merry 	xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
16253393f8daSKenneth D. Merry 	cpi.ccb_h.func_code = XPT_PATH_INQ;
16263393f8daSKenneth D. Merry 	xpt_action((union ccb *)&cpi);
16273393f8daSKenneth D. Merry 
16283393f8daSKenneth D. Merry 	speed = cpi.base_transfer_speed;
16293393f8daSKenneth D. Merry 	freq = 0;
1630f053d777SMatt Jacob 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
16313393f8daSKenneth D. Merry 		struct	ccb_trans_settings_spi *spi;
16323393f8daSKenneth D. Merry 
16333393f8daSKenneth D. Merry 		spi = &cts.xport_specific.spi;
16343393f8daSKenneth D. Merry 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
16353393f8daSKenneth D. Merry 		  && spi->sync_offset != 0) {
16363393f8daSKenneth D. Merry 			freq = scsi_calc_syncsrate(spi->sync_period);
16373393f8daSKenneth D. Merry 			speed = freq;
16383393f8daSKenneth D. Merry 		}
16393393f8daSKenneth D. Merry 
16403393f8daSKenneth D. Merry 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
16413393f8daSKenneth D. Merry 			speed *= (0x01 << spi->bus_width);
16423393f8daSKenneth D. Merry 	}
16432c7d0b8dSMatt Jacob 
1644f053d777SMatt Jacob 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1645f053d777SMatt Jacob 		struct	ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1646f053d777SMatt Jacob 		if (fc->valid & CTS_FC_VALID_SPEED) {
16472c7d0b8dSMatt Jacob 			speed = fc->bitrate;
16482c7d0b8dSMatt Jacob 		}
1649f053d777SMatt Jacob 	}
16503393f8daSKenneth D. Merry 
1651c4270cb6SMatt Jacob 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SAS) {
1652c4270cb6SMatt Jacob 		struct	ccb_trans_settings_sas *sas = &cts.xport_specific.sas;
1653c4270cb6SMatt Jacob 		if (sas->valid & CTS_SAS_VALID_SPEED) {
1654c4270cb6SMatt Jacob 			speed = sas->bitrate;
1655c4270cb6SMatt Jacob 		}
1656c4270cb6SMatt Jacob 	}
1657c4270cb6SMatt Jacob 
16583393f8daSKenneth D. Merry 	mb = speed / 1000;
16593393f8daSKenneth D. Merry 	if (mb > 0)
16603393f8daSKenneth D. Merry 		printf("%s%d: %d.%03dMB/s transfers",
16613393f8daSKenneth D. Merry 		       periph->periph_name, periph->unit_number,
16623393f8daSKenneth D. Merry 		       mb, speed % 1000);
16633393f8daSKenneth D. Merry 	else
16643393f8daSKenneth D. Merry 		printf("%s%d: %dKB/s transfers", periph->periph_name,
16653393f8daSKenneth D. Merry 		       periph->unit_number, speed);
16663393f8daSKenneth D. Merry 	/* Report additional information about SPI connections */
1667f053d777SMatt Jacob 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
16683393f8daSKenneth D. Merry 		struct	ccb_trans_settings_spi *spi;
16693393f8daSKenneth D. Merry 
16703393f8daSKenneth D. Merry 		spi = &cts.xport_specific.spi;
16713393f8daSKenneth D. Merry 		if (freq != 0) {
16723393f8daSKenneth D. Merry 			printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
16733393f8daSKenneth D. Merry 			       freq % 1000,
16743393f8daSKenneth D. Merry 			       (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
16753393f8daSKenneth D. Merry 			     ? " DT" : "",
16763393f8daSKenneth D. Merry 			       spi->sync_offset);
16773393f8daSKenneth D. Merry 		}
16783393f8daSKenneth D. Merry 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
16793393f8daSKenneth D. Merry 		 && spi->bus_width > 0) {
16803393f8daSKenneth D. Merry 			if (freq != 0) {
16813393f8daSKenneth D. Merry 				printf(", ");
16823393f8daSKenneth D. Merry 			} else {
16833393f8daSKenneth D. Merry 				printf(" (");
16843393f8daSKenneth D. Merry 			}
16853393f8daSKenneth D. Merry 			printf("%dbit)", 8 * (0x01 << spi->bus_width));
16863393f8daSKenneth D. Merry 		} else if (freq != 0) {
16873393f8daSKenneth D. Merry 			printf(")");
16883393f8daSKenneth D. Merry 		}
16893393f8daSKenneth D. Merry 	}
1690f053d777SMatt Jacob 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
16912c7d0b8dSMatt Jacob 		struct	ccb_trans_settings_fc *fc;
16922c7d0b8dSMatt Jacob 
16932c7d0b8dSMatt Jacob 		fc = &cts.xport_specific.fc;
1694f053d777SMatt Jacob 		if (fc->valid & CTS_FC_VALID_WWNN)
1695f053d777SMatt Jacob 			printf(" WWNN 0x%llx", (long long) fc->wwnn);
1696f053d777SMatt Jacob 		if (fc->valid & CTS_FC_VALID_WWPN)
1697f053d777SMatt Jacob 			printf(" WWPN 0x%llx", (long long) fc->wwpn);
1698f053d777SMatt Jacob 		if (fc->valid & CTS_FC_VALID_PORT)
1699f053d777SMatt Jacob 			printf(" PortID 0x%x", fc->port);
17002c7d0b8dSMatt Jacob 	}
17013393f8daSKenneth D. Merry 
17023393f8daSKenneth D. Merry 	if (path->device->inq_flags & SID_CmdQue
17033393f8daSKenneth D. Merry 	 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
17047ffbfcd7SMatt Jacob 		printf("\n%s%d: Command Queueing Enabled",
17053393f8daSKenneth D. Merry 		       periph->periph_name, periph->unit_number);
17063393f8daSKenneth D. Merry 	}
17073393f8daSKenneth D. Merry 	printf("\n");
17083393f8daSKenneth D. Merry 
17093393f8daSKenneth D. Merry 	/*
17103393f8daSKenneth D. Merry 	 * We only want to print the caller's announce string if they've
17113393f8daSKenneth D. Merry 	 * passed one in..
17123393f8daSKenneth D. Merry 	 */
17133393f8daSKenneth D. Merry 	if (announce_string != NULL)
17143393f8daSKenneth D. Merry 		printf("%s%d: %s\n", periph->periph_name,
17153393f8daSKenneth D. Merry 		       periph->unit_number, announce_string);
17163393f8daSKenneth D. Merry 	splx(s);
17173393f8daSKenneth D. Merry }
17188b8a9b1dSJustin T. Gibbs 
17198b8a9b1dSJustin T. Gibbs static dev_match_ret
17203393f8daSKenneth D. Merry xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
17218b8a9b1dSJustin T. Gibbs 	    struct cam_eb *bus)
17228b8a9b1dSJustin T. Gibbs {
17238b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
17248b8a9b1dSJustin T. Gibbs 	int i;
17258b8a9b1dSJustin T. Gibbs 
17268b8a9b1dSJustin T. Gibbs 	retval = DM_RET_NONE;
17278b8a9b1dSJustin T. Gibbs 
17288b8a9b1dSJustin T. Gibbs 	/*
17298b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
17308b8a9b1dSJustin T. Gibbs 	 */
17318b8a9b1dSJustin T. Gibbs 	if (bus == NULL)
17328b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
17338b8a9b1dSJustin T. Gibbs 
17348b8a9b1dSJustin T. Gibbs 	/*
17358b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this bus matches no
17368b8a9b1dSJustin T. Gibbs 	 * matter what.
17378b8a9b1dSJustin T. Gibbs 	 */
17388b8a9b1dSJustin T. Gibbs 	if ((patterns == NULL) || (num_patterns == 0))
17398b8a9b1dSJustin T. Gibbs 		return(DM_RET_DESCEND | DM_RET_COPY);
17408b8a9b1dSJustin T. Gibbs 
17418b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
17428b8a9b1dSJustin T. Gibbs 		struct bus_match_pattern *cur_pattern;
17438b8a9b1dSJustin T. Gibbs 
17448b8a9b1dSJustin T. Gibbs 		/*
17458b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a bus node, we
17468b8a9b1dSJustin T. Gibbs 		 * aren't interested.  However, we do indicate to the
17478b8a9b1dSJustin T. Gibbs 		 * calling routine that we should continue descending the
17488b8a9b1dSJustin T. Gibbs 		 * tree, since the user wants to match against lower-level
17498b8a9b1dSJustin T. Gibbs 		 * EDT elements.
17508b8a9b1dSJustin T. Gibbs 		 */
17518b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_BUS) {
17528b8a9b1dSJustin T. Gibbs 			if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
17538b8a9b1dSJustin T. Gibbs 				retval |= DM_RET_DESCEND;
17548b8a9b1dSJustin T. Gibbs 			continue;
17558b8a9b1dSJustin T. Gibbs 		}
17568b8a9b1dSJustin T. Gibbs 
17578b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.bus_pattern;
17588b8a9b1dSJustin T. Gibbs 
17598b8a9b1dSJustin T. Gibbs 		/*
17608b8a9b1dSJustin T. Gibbs 		 * If they want to match any bus node, we give them any
17618b8a9b1dSJustin T. Gibbs 		 * device node.
17628b8a9b1dSJustin T. Gibbs 		 */
17638b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == BUS_MATCH_ANY) {
17648b8a9b1dSJustin T. Gibbs 			/* set the copy flag */
17658b8a9b1dSJustin T. Gibbs 			retval |= DM_RET_COPY;
17668b8a9b1dSJustin T. Gibbs 
17678b8a9b1dSJustin T. Gibbs 			/*
17688b8a9b1dSJustin T. Gibbs 			 * If we've already decided on an action, go ahead
17698b8a9b1dSJustin T. Gibbs 			 * and return.
17708b8a9b1dSJustin T. Gibbs 			 */
17718b8a9b1dSJustin T. Gibbs 			if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
17728b8a9b1dSJustin T. Gibbs 				return(retval);
17738b8a9b1dSJustin T. Gibbs 		}
17748b8a9b1dSJustin T. Gibbs 
17758b8a9b1dSJustin T. Gibbs 		/*
17768b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
17778b8a9b1dSJustin T. Gibbs 		 */
17788b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == BUS_MATCH_NONE)
17798b8a9b1dSJustin T. Gibbs 			continue;
17808b8a9b1dSJustin T. Gibbs 
17818b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
17828b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != bus->path_id))
17838b8a9b1dSJustin T. Gibbs 			continue;
17848b8a9b1dSJustin T. Gibbs 
17858b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
17868b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->bus_id != bus->sim->bus_id))
17878b8a9b1dSJustin T. Gibbs 			continue;
17888b8a9b1dSJustin T. Gibbs 
17898b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
17908b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->unit_number != bus->sim->unit_number))
17918b8a9b1dSJustin T. Gibbs 			continue;
17928b8a9b1dSJustin T. Gibbs 
17938b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
17948b8a9b1dSJustin T. Gibbs 		 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
17958b8a9b1dSJustin T. Gibbs 			     DEV_IDLEN) != 0))
17968b8a9b1dSJustin T. Gibbs 			continue;
17978b8a9b1dSJustin T. Gibbs 
17988b8a9b1dSJustin T. Gibbs 		/*
17998b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
18008b8a9b1dSJustin T. Gibbs 		 * information on this bus.  So tell the caller to copy the
18018b8a9b1dSJustin T. Gibbs 		 * data out.
18028b8a9b1dSJustin T. Gibbs 		 */
18038b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
18048b8a9b1dSJustin T. Gibbs 
18058b8a9b1dSJustin T. Gibbs 		/*
18068b8a9b1dSJustin T. Gibbs 		 * If the return action has been set to descend, then we
18078b8a9b1dSJustin T. Gibbs 		 * know that we've already seen a non-bus matching
18088b8a9b1dSJustin T. Gibbs 		 * expression, therefore we need to further descend the tree.
18098b8a9b1dSJustin T. Gibbs 		 * This won't change by continuing around the loop, so we
18108b8a9b1dSJustin T. Gibbs 		 * go ahead and return.  If we haven't seen a non-bus
18118b8a9b1dSJustin T. Gibbs 		 * matching expression, we keep going around the loop until
18128b8a9b1dSJustin T. Gibbs 		 * we exhaust the matching expressions.  We'll set the stop
18138b8a9b1dSJustin T. Gibbs 		 * flag once we fall out of the loop.
18148b8a9b1dSJustin T. Gibbs 		 */
18158b8a9b1dSJustin T. Gibbs 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
18168b8a9b1dSJustin T. Gibbs 			return(retval);
18178b8a9b1dSJustin T. Gibbs 	}
18188b8a9b1dSJustin T. Gibbs 
18198b8a9b1dSJustin T. Gibbs 	/*
18208b8a9b1dSJustin T. Gibbs 	 * If the return action hasn't been set to descend yet, that means
18218b8a9b1dSJustin T. Gibbs 	 * we haven't seen anything other than bus matching patterns.  So
18228b8a9b1dSJustin T. Gibbs 	 * tell the caller to stop descending the tree -- the user doesn't
18238b8a9b1dSJustin T. Gibbs 	 * want to match against lower level tree elements.
18248b8a9b1dSJustin T. Gibbs 	 */
18258b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
18268b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_STOP;
18278b8a9b1dSJustin T. Gibbs 
18288b8a9b1dSJustin T. Gibbs 	return(retval);
18298b8a9b1dSJustin T. Gibbs }
18308b8a9b1dSJustin T. Gibbs 
18318b8a9b1dSJustin T. Gibbs static dev_match_ret
18323393f8daSKenneth D. Merry xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
18338b8a9b1dSJustin T. Gibbs 	       struct cam_ed *device)
18348b8a9b1dSJustin T. Gibbs {
18358b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
18368b8a9b1dSJustin T. Gibbs 	int i;
18378b8a9b1dSJustin T. Gibbs 
18388b8a9b1dSJustin T. Gibbs 	retval = DM_RET_NONE;
18398b8a9b1dSJustin T. Gibbs 
18408b8a9b1dSJustin T. Gibbs 	/*
18418b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
18428b8a9b1dSJustin T. Gibbs 	 */
18438b8a9b1dSJustin T. Gibbs 	if (device == NULL)
18448b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
18458b8a9b1dSJustin T. Gibbs 
18468b8a9b1dSJustin T. Gibbs 	/*
18478b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this device matches no
18488b8a9b1dSJustin T. Gibbs 	 * matter what.
18498b8a9b1dSJustin T. Gibbs 	 */
185059e75884SColin Percival 	if ((patterns == NULL) || (num_patterns == 0))
18518b8a9b1dSJustin T. Gibbs 		return(DM_RET_DESCEND | DM_RET_COPY);
18528b8a9b1dSJustin T. Gibbs 
18538b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
18548b8a9b1dSJustin T. Gibbs 		struct device_match_pattern *cur_pattern;
18558b8a9b1dSJustin T. Gibbs 
18568b8a9b1dSJustin T. Gibbs 		/*
18578b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a device node, we
18588b8a9b1dSJustin T. Gibbs 		 * aren't interested.
18598b8a9b1dSJustin T. Gibbs 		 */
18608b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_DEVICE) {
18618b8a9b1dSJustin T. Gibbs 			if ((patterns[i].type == DEV_MATCH_PERIPH)
18628b8a9b1dSJustin T. Gibbs 			 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
18638b8a9b1dSJustin T. Gibbs 				retval |= DM_RET_DESCEND;
18648b8a9b1dSJustin T. Gibbs 			continue;
18658b8a9b1dSJustin T. Gibbs 		}
18668b8a9b1dSJustin T. Gibbs 
18678b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.device_pattern;
18688b8a9b1dSJustin T. Gibbs 
18698b8a9b1dSJustin T. Gibbs 		/*
18708b8a9b1dSJustin T. Gibbs 		 * If they want to match any device node, we give them any
18718b8a9b1dSJustin T. Gibbs 		 * device node.
18728b8a9b1dSJustin T. Gibbs 		 */
18738b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == DEV_MATCH_ANY) {
18748b8a9b1dSJustin T. Gibbs 			/* set the copy flag */
18758b8a9b1dSJustin T. Gibbs 			retval |= DM_RET_COPY;
18768b8a9b1dSJustin T. Gibbs 
18778b8a9b1dSJustin T. Gibbs 
18788b8a9b1dSJustin T. Gibbs 			/*
18798b8a9b1dSJustin T. Gibbs 			 * If we've already decided on an action, go ahead
18808b8a9b1dSJustin T. Gibbs 			 * and return.
18818b8a9b1dSJustin T. Gibbs 			 */
18828b8a9b1dSJustin T. Gibbs 			if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
18838b8a9b1dSJustin T. Gibbs 				return(retval);
18848b8a9b1dSJustin T. Gibbs 		}
18858b8a9b1dSJustin T. Gibbs 
18868b8a9b1dSJustin T. Gibbs 		/*
18878b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
18888b8a9b1dSJustin T. Gibbs 		 */
18898b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == DEV_MATCH_NONE)
18908b8a9b1dSJustin T. Gibbs 			continue;
18918b8a9b1dSJustin T. Gibbs 
18928b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
18938b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != device->target->bus->path_id))
18948b8a9b1dSJustin T. Gibbs 			continue;
18958b8a9b1dSJustin T. Gibbs 
18968b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
18978b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->target_id != device->target->target_id))
18988b8a9b1dSJustin T. Gibbs 			continue;
18998b8a9b1dSJustin T. Gibbs 
19008b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
19018b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->target_lun != device->lun_id))
19028b8a9b1dSJustin T. Gibbs 			continue;
19038b8a9b1dSJustin T. Gibbs 
19048b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
19058b8a9b1dSJustin T. Gibbs 		 && (cam_quirkmatch((caddr_t)&device->inq_data,
19068b8a9b1dSJustin T. Gibbs 				    (caddr_t)&cur_pattern->inq_pat,
19078b8a9b1dSJustin T. Gibbs 				    1, sizeof(cur_pattern->inq_pat),
19088b8a9b1dSJustin T. Gibbs 				    scsi_static_inquiry_match) == NULL))
19098b8a9b1dSJustin T. Gibbs 			continue;
19108b8a9b1dSJustin T. Gibbs 
19118b8a9b1dSJustin T. Gibbs 		/*
19128b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
19138b8a9b1dSJustin T. Gibbs 		 * information on this device.  So tell the caller to copy
19148b8a9b1dSJustin T. Gibbs 		 * the data out.
19158b8a9b1dSJustin T. Gibbs 		 */
19168b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
19178b8a9b1dSJustin T. Gibbs 
19188b8a9b1dSJustin T. Gibbs 		/*
19198b8a9b1dSJustin T. Gibbs 		 * If the return action has been set to descend, then we
19208b8a9b1dSJustin T. Gibbs 		 * know that we've already seen a peripheral matching
19218b8a9b1dSJustin T. Gibbs 		 * expression, therefore we need to further descend the tree.
19228b8a9b1dSJustin T. Gibbs 		 * This won't change by continuing around the loop, so we
19238b8a9b1dSJustin T. Gibbs 		 * go ahead and return.  If we haven't seen a peripheral
19248b8a9b1dSJustin T. Gibbs 		 * matching expression, we keep going around the loop until
19258b8a9b1dSJustin T. Gibbs 		 * we exhaust the matching expressions.  We'll set the stop
19268b8a9b1dSJustin T. Gibbs 		 * flag once we fall out of the loop.
19278b8a9b1dSJustin T. Gibbs 		 */
19288b8a9b1dSJustin T. Gibbs 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
19298b8a9b1dSJustin T. Gibbs 			return(retval);
19308b8a9b1dSJustin T. Gibbs 	}
19318b8a9b1dSJustin T. Gibbs 
19328b8a9b1dSJustin T. Gibbs 	/*
19338b8a9b1dSJustin T. Gibbs 	 * If the return action hasn't been set to descend yet, that means
19348b8a9b1dSJustin T. Gibbs 	 * we haven't seen any peripheral matching patterns.  So tell the
19358b8a9b1dSJustin T. Gibbs 	 * caller to stop descending the tree -- the user doesn't want to
19368b8a9b1dSJustin T. Gibbs 	 * match against lower level tree elements.
19378b8a9b1dSJustin T. Gibbs 	 */
19388b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
19398b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_STOP;
19408b8a9b1dSJustin T. Gibbs 
19418b8a9b1dSJustin T. Gibbs 	return(retval);
19428b8a9b1dSJustin T. Gibbs }
19438b8a9b1dSJustin T. Gibbs 
19448b8a9b1dSJustin T. Gibbs /*
19458b8a9b1dSJustin T. Gibbs  * Match a single peripheral against any number of match patterns.
19468b8a9b1dSJustin T. Gibbs  */
19478b8a9b1dSJustin T. Gibbs static dev_match_ret
19483393f8daSKenneth D. Merry xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
19498b8a9b1dSJustin T. Gibbs 	       struct cam_periph *periph)
19508b8a9b1dSJustin T. Gibbs {
19518b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
19528b8a9b1dSJustin T. Gibbs 	int i;
19538b8a9b1dSJustin T. Gibbs 
19548b8a9b1dSJustin T. Gibbs 	/*
19558b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
19568b8a9b1dSJustin T. Gibbs 	 */
19578b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
19588b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
19598b8a9b1dSJustin T. Gibbs 
19608b8a9b1dSJustin T. Gibbs 	/*
19618b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this peripheral matches no
19628b8a9b1dSJustin T. Gibbs 	 * matter what.
19638b8a9b1dSJustin T. Gibbs 	 */
19648b8a9b1dSJustin T. Gibbs 	if ((patterns == NULL) || (num_patterns == 0))
19658b8a9b1dSJustin T. Gibbs 		return(DM_RET_STOP | DM_RET_COPY);
19668b8a9b1dSJustin T. Gibbs 
19678b8a9b1dSJustin T. Gibbs 	/*
19688b8a9b1dSJustin T. Gibbs 	 * There aren't any nodes below a peripheral node, so there's no
19698b8a9b1dSJustin T. Gibbs 	 * reason to descend the tree any further.
19708b8a9b1dSJustin T. Gibbs 	 */
19718b8a9b1dSJustin T. Gibbs 	retval = DM_RET_STOP;
19728b8a9b1dSJustin T. Gibbs 
19738b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
19748b8a9b1dSJustin T. Gibbs 		struct periph_match_pattern *cur_pattern;
19758b8a9b1dSJustin T. Gibbs 
19768b8a9b1dSJustin T. Gibbs 		/*
19778b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a peripheral, we
19788b8a9b1dSJustin T. Gibbs 		 * aren't interested.
19798b8a9b1dSJustin T. Gibbs 		 */
19808b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_PERIPH)
19818b8a9b1dSJustin T. Gibbs 			continue;
19828b8a9b1dSJustin T. Gibbs 
19838b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.periph_pattern;
19848b8a9b1dSJustin T. Gibbs 
19858b8a9b1dSJustin T. Gibbs 		/*
19868b8a9b1dSJustin T. Gibbs 		 * If they want to match on anything, then we will do so.
19878b8a9b1dSJustin T. Gibbs 		 */
19888b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == PERIPH_MATCH_ANY) {
19898b8a9b1dSJustin T. Gibbs 			/* set the copy flag */
19908b8a9b1dSJustin T. Gibbs 			retval |= DM_RET_COPY;
19918b8a9b1dSJustin T. Gibbs 
19928b8a9b1dSJustin T. Gibbs 			/*
19938b8a9b1dSJustin T. Gibbs 			 * We've already set the return action to stop,
19948b8a9b1dSJustin T. Gibbs 			 * since there are no nodes below peripherals in
19958b8a9b1dSJustin T. Gibbs 			 * the tree.
19968b8a9b1dSJustin T. Gibbs 			 */
19978b8a9b1dSJustin T. Gibbs 			return(retval);
19988b8a9b1dSJustin T. Gibbs 		}
19998b8a9b1dSJustin T. Gibbs 
20008b8a9b1dSJustin T. Gibbs 		/*
20018b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
20028b8a9b1dSJustin T. Gibbs 		 */
20038b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == PERIPH_MATCH_NONE)
20048b8a9b1dSJustin T. Gibbs 			continue;
20058b8a9b1dSJustin T. Gibbs 
20068b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
20078b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != periph->path->bus->path_id))
20088b8a9b1dSJustin T. Gibbs 			continue;
20098b8a9b1dSJustin T. Gibbs 
20108b8a9b1dSJustin T. Gibbs 		/*
20118b8a9b1dSJustin T. Gibbs 		 * For the target and lun id's, we have to make sure the
20128b8a9b1dSJustin T. Gibbs 		 * target and lun pointers aren't NULL.  The xpt peripheral
20138b8a9b1dSJustin T. Gibbs 		 * has a wildcard target and device.
20148b8a9b1dSJustin T. Gibbs 		 */
20158b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
20168b8a9b1dSJustin T. Gibbs 		 && ((periph->path->target == NULL)
20178b8a9b1dSJustin T. Gibbs 		 ||(cur_pattern->target_id != periph->path->target->target_id)))
20188b8a9b1dSJustin T. Gibbs 			continue;
20198b8a9b1dSJustin T. Gibbs 
20208b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
20218b8a9b1dSJustin T. Gibbs 		 && ((periph->path->device == NULL)
20228b8a9b1dSJustin T. Gibbs 		 || (cur_pattern->target_lun != periph->path->device->lun_id)))
20238b8a9b1dSJustin T. Gibbs 			continue;
20248b8a9b1dSJustin T. Gibbs 
20258b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
20268b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->unit_number != periph->unit_number))
20278b8a9b1dSJustin T. Gibbs 			continue;
20288b8a9b1dSJustin T. Gibbs 
20298b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
20308b8a9b1dSJustin T. Gibbs 		 && (strncmp(cur_pattern->periph_name, periph->periph_name,
20318b8a9b1dSJustin T. Gibbs 			     DEV_IDLEN) != 0))
20328b8a9b1dSJustin T. Gibbs 			continue;
20338b8a9b1dSJustin T. Gibbs 
20348b8a9b1dSJustin T. Gibbs 		/*
20358b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
20368b8a9b1dSJustin T. Gibbs 		 * information on this peripheral.  So tell the caller to
20378b8a9b1dSJustin T. Gibbs 		 * copy the data out.
20388b8a9b1dSJustin T. Gibbs 		 */
20398b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
20408b8a9b1dSJustin T. Gibbs 
20418b8a9b1dSJustin T. Gibbs 		/*
20428b8a9b1dSJustin T. Gibbs 		 * The return action has already been set to stop, since
20438b8a9b1dSJustin T. Gibbs 		 * peripherals don't have any nodes below them in the EDT.
20448b8a9b1dSJustin T. Gibbs 		 */
20458b8a9b1dSJustin T. Gibbs 		return(retval);
20468b8a9b1dSJustin T. Gibbs 	}
20478b8a9b1dSJustin T. Gibbs 
20488b8a9b1dSJustin T. Gibbs 	/*
20498b8a9b1dSJustin T. Gibbs 	 * If we get to this point, the peripheral that was passed in
20508b8a9b1dSJustin T. Gibbs 	 * doesn't match any of the patterns.
20518b8a9b1dSJustin T. Gibbs 	 */
20528b8a9b1dSJustin T. Gibbs 	return(retval);
20538b8a9b1dSJustin T. Gibbs }
20548b8a9b1dSJustin T. Gibbs 
20558b8a9b1dSJustin T. Gibbs static int
20568b8a9b1dSJustin T. Gibbs xptedtbusfunc(struct cam_eb *bus, void *arg)
20578b8a9b1dSJustin T. Gibbs {
20588b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
20598b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
20608b8a9b1dSJustin T. Gibbs 
20618b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
20628b8a9b1dSJustin T. Gibbs 
20638b8a9b1dSJustin T. Gibbs 	/*
20648b8a9b1dSJustin T. Gibbs 	 * If our position is for something deeper in the tree, that means
20658b8a9b1dSJustin T. Gibbs 	 * that we've already seen this node.  So, we keep going down.
20668b8a9b1dSJustin T. Gibbs 	 */
20678b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
20688b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == bus)
20698b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
20708b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target != NULL))
20718b8a9b1dSJustin T. Gibbs 		retval = DM_RET_DESCEND;
20728b8a9b1dSJustin T. Gibbs 	else
20738b8a9b1dSJustin T. Gibbs 		retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
20748b8a9b1dSJustin T. Gibbs 
20758b8a9b1dSJustin T. Gibbs 	/*
20768b8a9b1dSJustin T. Gibbs 	 * If we got an error, bail out of the search.
20778b8a9b1dSJustin T. Gibbs 	 */
20788b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
20798b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
20808b8a9b1dSJustin T. Gibbs 		return(0);
20818b8a9b1dSJustin T. Gibbs 	}
20828b8a9b1dSJustin T. Gibbs 
20838b8a9b1dSJustin T. Gibbs 	/*
20848b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this bus out.
20858b8a9b1dSJustin T. Gibbs 	 */
20868b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
20878b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
20888b8a9b1dSJustin T. Gibbs 
20898b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
20908b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
20918b8a9b1dSJustin T. Gibbs 
20928b8a9b1dSJustin T. Gibbs 		/*
20938b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
20948b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
20958b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
20968b8a9b1dSJustin T. Gibbs 		 */
20978b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
20988b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
20998b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
21008b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
21018b8a9b1dSJustin T. Gibbs 
21028b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = bus;
21038b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
21048b8a9b1dSJustin T. Gibbs 				bus_generation;
21058b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
21068b8a9b1dSJustin T. Gibbs 			return(0);
21078b8a9b1dSJustin T. Gibbs 		}
21088b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
21098b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
21108b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_BUS;
21118b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.path_id = bus->path_id;
21128b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
21138b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.unit_number =
21148b8a9b1dSJustin T. Gibbs 			bus->sim->unit_number;
21158b8a9b1dSJustin T. Gibbs 		strncpy(cdm->matches[j].result.bus_result.dev_name,
21168b8a9b1dSJustin T. Gibbs 			bus->sim->sim_name, DEV_IDLEN);
21178b8a9b1dSJustin T. Gibbs 	}
21188b8a9b1dSJustin T. Gibbs 
21198b8a9b1dSJustin T. Gibbs 	/*
21208b8a9b1dSJustin T. Gibbs 	 * If the user is only interested in busses, there's no
21218b8a9b1dSJustin T. Gibbs 	 * reason to descend to the next level in the tree.
21228b8a9b1dSJustin T. Gibbs 	 */
21238b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
21248b8a9b1dSJustin T. Gibbs 		return(1);
21258b8a9b1dSJustin T. Gibbs 
21268b8a9b1dSJustin T. Gibbs 	/*
21278b8a9b1dSJustin T. Gibbs 	 * If there is a target generation recorded, check it to
21288b8a9b1dSJustin T. Gibbs 	 * make sure the target list hasn't changed.
21298b8a9b1dSJustin T. Gibbs 	 */
21308b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
21318b8a9b1dSJustin T. Gibbs 	 && (bus == cdm->pos.cookie.bus)
21328b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
21338b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
21348b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
21358b8a9b1dSJustin T. Gibbs 	     bus->generation)) {
21368b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
21378b8a9b1dSJustin T. Gibbs 		return(0);
21388b8a9b1dSJustin T. Gibbs 	}
21398b8a9b1dSJustin T. Gibbs 
21408b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
21418b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == bus)
21428b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
21438b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target != NULL))
21448b8a9b1dSJustin T. Gibbs 		return(xpttargettraverse(bus,
21458b8a9b1dSJustin T. Gibbs 					(struct cam_et *)cdm->pos.cookie.target,
21468b8a9b1dSJustin T. Gibbs 					 xptedttargetfunc, arg));
21478b8a9b1dSJustin T. Gibbs 	else
21488b8a9b1dSJustin T. Gibbs 		return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
21498b8a9b1dSJustin T. Gibbs }
21508b8a9b1dSJustin T. Gibbs 
21518b8a9b1dSJustin T. Gibbs static int
21528b8a9b1dSJustin T. Gibbs xptedttargetfunc(struct cam_et *target, void *arg)
21538b8a9b1dSJustin T. Gibbs {
21548b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
21558b8a9b1dSJustin T. Gibbs 
21568b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
21578b8a9b1dSJustin T. Gibbs 
21588b8a9b1dSJustin T. Gibbs 	/*
21598b8a9b1dSJustin T. Gibbs 	 * If there is a device list generation recorded, check it to
21608b8a9b1dSJustin T. Gibbs 	 * make sure the device list hasn't changed.
21618b8a9b1dSJustin T. Gibbs 	 */
21628b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
21638b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == target->bus)
21648b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
21658b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == target)
21668b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
21678b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
21688b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
21698b8a9b1dSJustin T. Gibbs 	     target->generation)) {
21708b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
21718b8a9b1dSJustin T. Gibbs 		return(0);
21728b8a9b1dSJustin T. Gibbs 	}
21738b8a9b1dSJustin T. Gibbs 
21748b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
21758b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == target->bus)
21768b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
21778b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == target)
21788b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
21798b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device != NULL))
21808b8a9b1dSJustin T. Gibbs 		return(xptdevicetraverse(target,
21818b8a9b1dSJustin T. Gibbs 					(struct cam_ed *)cdm->pos.cookie.device,
21828b8a9b1dSJustin T. Gibbs 					 xptedtdevicefunc, arg));
21838b8a9b1dSJustin T. Gibbs 	else
21848b8a9b1dSJustin T. Gibbs 		return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
21858b8a9b1dSJustin T. Gibbs }
21868b8a9b1dSJustin T. Gibbs 
21878b8a9b1dSJustin T. Gibbs static int
21888b8a9b1dSJustin T. Gibbs xptedtdevicefunc(struct cam_ed *device, void *arg)
21898b8a9b1dSJustin T. Gibbs {
21908b8a9b1dSJustin T. Gibbs 
21918b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
21928b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
21938b8a9b1dSJustin T. Gibbs 
21948b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
21958b8a9b1dSJustin T. Gibbs 
21968b8a9b1dSJustin T. Gibbs 	/*
21978b8a9b1dSJustin T. Gibbs 	 * If our position is for something deeper in the tree, that means
21988b8a9b1dSJustin T. Gibbs 	 * that we've already seen this node.  So, we keep going down.
21998b8a9b1dSJustin T. Gibbs 	 */
22008b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
22018b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device == device)
22028b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
22038b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.periph != NULL))
22048b8a9b1dSJustin T. Gibbs 		retval = DM_RET_DESCEND;
22058b8a9b1dSJustin T. Gibbs 	else
22068b8a9b1dSJustin T. Gibbs 		retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
22078b8a9b1dSJustin T. Gibbs 					device);
22088b8a9b1dSJustin T. Gibbs 
22098b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
22108b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
22118b8a9b1dSJustin T. Gibbs 		return(0);
22128b8a9b1dSJustin T. Gibbs 	}
22138b8a9b1dSJustin T. Gibbs 
22148b8a9b1dSJustin T. Gibbs 	/*
22158b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this device out.
22168b8a9b1dSJustin T. Gibbs 	 */
22178b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
22188b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
22198b8a9b1dSJustin T. Gibbs 
22208b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
22218b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
22228b8a9b1dSJustin T. Gibbs 
22238b8a9b1dSJustin T. Gibbs 		/*
22248b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
22258b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
22268b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
22278b8a9b1dSJustin T. Gibbs 		 */
22288b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
22298b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
22308b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
22318b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
22328b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
22338b8a9b1dSJustin T. Gibbs 
22348b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = device->target->bus;
22358b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
22368b8a9b1dSJustin T. Gibbs 				bus_generation;
22378b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.target = device->target;
22388b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_TARGET_GENERATION] =
22398b8a9b1dSJustin T. Gibbs 				device->target->bus->generation;
22408b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.device = device;
22418b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_DEV_GENERATION] =
22428b8a9b1dSJustin T. Gibbs 				device->target->generation;
22438b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
22448b8a9b1dSJustin T. Gibbs 			return(0);
22458b8a9b1dSJustin T. Gibbs 		}
22468b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
22478b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
22488b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_DEVICE;
22498b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.path_id =
22508b8a9b1dSJustin T. Gibbs 			device->target->bus->path_id;
22518b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.target_id =
22528b8a9b1dSJustin T. Gibbs 			device->target->target_id;
22538b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.target_lun =
22548b8a9b1dSJustin T. Gibbs 			device->lun_id;
22558b8a9b1dSJustin T. Gibbs 		bcopy(&device->inq_data,
22568b8a9b1dSJustin T. Gibbs 		      &cdm->matches[j].result.device_result.inq_data,
22578b8a9b1dSJustin T. Gibbs 		      sizeof(struct scsi_inquiry_data));
22589deea857SKenneth D. Merry 
22599deea857SKenneth D. Merry 		/* Let the user know whether this device is unconfigured */
22609deea857SKenneth D. Merry 		if (device->flags & CAM_DEV_UNCONFIGURED)
22619deea857SKenneth D. Merry 			cdm->matches[j].result.device_result.flags =
22629deea857SKenneth D. Merry 				DEV_RESULT_UNCONFIGURED;
22639deea857SKenneth D. Merry 		else
22649deea857SKenneth D. Merry 			cdm->matches[j].result.device_result.flags =
22659deea857SKenneth D. Merry 				DEV_RESULT_NOFLAG;
22668b8a9b1dSJustin T. Gibbs 	}
22678b8a9b1dSJustin T. Gibbs 
22688b8a9b1dSJustin T. Gibbs 	/*
22698b8a9b1dSJustin T. Gibbs 	 * If the user isn't interested in peripherals, don't descend
22708b8a9b1dSJustin T. Gibbs 	 * the tree any further.
22718b8a9b1dSJustin T. Gibbs 	 */
22728b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
22738b8a9b1dSJustin T. Gibbs 		return(1);
22748b8a9b1dSJustin T. Gibbs 
22758b8a9b1dSJustin T. Gibbs 	/*
22768b8a9b1dSJustin T. Gibbs 	 * If there is a peripheral list generation recorded, make sure
22778b8a9b1dSJustin T. Gibbs 	 * it hasn't changed.
22788b8a9b1dSJustin T. Gibbs 	 */
22798b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
22808b8a9b1dSJustin T. Gibbs 	 && (device->target->bus == cdm->pos.cookie.bus)
22818b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
22828b8a9b1dSJustin T. Gibbs 	 && (device->target == cdm->pos.cookie.target)
22838b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
22848b8a9b1dSJustin T. Gibbs 	 && (device == cdm->pos.cookie.device)
22858b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
22868b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
22878b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
22888b8a9b1dSJustin T. Gibbs 	     device->generation)){
22898b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
22908b8a9b1dSJustin T. Gibbs 		return(0);
22918b8a9b1dSJustin T. Gibbs 	}
22928b8a9b1dSJustin T. Gibbs 
22938b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
22948b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == device->target->bus)
22958b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
22968b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == device->target)
22978b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
22988b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device == device)
22998b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
23008b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.periph != NULL))
23018b8a9b1dSJustin T. Gibbs 		return(xptperiphtraverse(device,
23028b8a9b1dSJustin T. Gibbs 				(struct cam_periph *)cdm->pos.cookie.periph,
23038b8a9b1dSJustin T. Gibbs 				xptedtperiphfunc, arg));
23048b8a9b1dSJustin T. Gibbs 	else
23058b8a9b1dSJustin T. Gibbs 		return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
23068b8a9b1dSJustin T. Gibbs }
23078b8a9b1dSJustin T. Gibbs 
23088b8a9b1dSJustin T. Gibbs static int
23098b8a9b1dSJustin T. Gibbs xptedtperiphfunc(struct cam_periph *periph, void *arg)
23108b8a9b1dSJustin T. Gibbs {
23118b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
23128b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
23138b8a9b1dSJustin T. Gibbs 
23148b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
23158b8a9b1dSJustin T. Gibbs 
23168b8a9b1dSJustin T. Gibbs 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
23178b8a9b1dSJustin T. Gibbs 
23188b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
23198b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
23208b8a9b1dSJustin T. Gibbs 		return(0);
23218b8a9b1dSJustin T. Gibbs 	}
23228b8a9b1dSJustin T. Gibbs 
23238b8a9b1dSJustin T. Gibbs 	/*
23248b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this peripheral out.
23258b8a9b1dSJustin T. Gibbs 	 */
23268b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
23278b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
23288b8a9b1dSJustin T. Gibbs 
23298b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
23308b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
23318b8a9b1dSJustin T. Gibbs 
23328b8a9b1dSJustin T. Gibbs 		/*
23338b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
23348b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
23358b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
23368b8a9b1dSJustin T. Gibbs 		 */
23378b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
23388b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
23398b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
23408b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
23418b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
23428b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PERIPH;
23438b8a9b1dSJustin T. Gibbs 
23448b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = periph->path->bus;
23458b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
23468b8a9b1dSJustin T. Gibbs 				bus_generation;
23478b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.target = periph->path->target;
23488b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_TARGET_GENERATION] =
23498b8a9b1dSJustin T. Gibbs 				periph->path->bus->generation;
23508b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.device = periph->path->device;
23518b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_DEV_GENERATION] =
23528b8a9b1dSJustin T. Gibbs 				periph->path->target->generation;
23538b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.periph = periph;
23548b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
23558b8a9b1dSJustin T. Gibbs 				periph->path->device->generation;
23568b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
23578b8a9b1dSJustin T. Gibbs 			return(0);
23588b8a9b1dSJustin T. Gibbs 		}
23598b8a9b1dSJustin T. Gibbs 
23608b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
23618b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
23628b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_PERIPH;
23638b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.path_id =
23648b8a9b1dSJustin T. Gibbs 			periph->path->bus->path_id;
23658b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.target_id =
23668b8a9b1dSJustin T. Gibbs 			periph->path->target->target_id;
23678b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.target_lun =
23688b8a9b1dSJustin T. Gibbs 			periph->path->device->lun_id;
23698b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.unit_number =
23708b8a9b1dSJustin T. Gibbs 			periph->unit_number;
23718b8a9b1dSJustin T. Gibbs 		strncpy(cdm->matches[j].result.periph_result.periph_name,
23728b8a9b1dSJustin T. Gibbs 			periph->periph_name, DEV_IDLEN);
23738b8a9b1dSJustin T. Gibbs 	}
23748b8a9b1dSJustin T. Gibbs 
23758b8a9b1dSJustin T. Gibbs 	return(1);
23768b8a9b1dSJustin T. Gibbs }
23778b8a9b1dSJustin T. Gibbs 
23788b8a9b1dSJustin T. Gibbs static int
23798b8a9b1dSJustin T. Gibbs xptedtmatch(struct ccb_dev_match *cdm)
23808b8a9b1dSJustin T. Gibbs {
23818b8a9b1dSJustin T. Gibbs 	int ret;
23828b8a9b1dSJustin T. Gibbs 
23838b8a9b1dSJustin T. Gibbs 	cdm->num_matches = 0;
23848b8a9b1dSJustin T. Gibbs 
23858b8a9b1dSJustin T. Gibbs 	/*
23868b8a9b1dSJustin T. Gibbs 	 * Check the bus list generation.  If it has changed, the user
23878b8a9b1dSJustin T. Gibbs 	 * needs to reset everything and start over.
23888b8a9b1dSJustin T. Gibbs 	 */
23898b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
23908b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
23918b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
23928b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
23938b8a9b1dSJustin T. Gibbs 		return(0);
23948b8a9b1dSJustin T. Gibbs 	}
23958b8a9b1dSJustin T. Gibbs 
23968b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
23978b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus != NULL))
23988b8a9b1dSJustin T. Gibbs 		ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
23998b8a9b1dSJustin T. Gibbs 				     xptedtbusfunc, cdm);
24008b8a9b1dSJustin T. Gibbs 	else
24018b8a9b1dSJustin T. Gibbs 		ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
24028b8a9b1dSJustin T. Gibbs 
24038b8a9b1dSJustin T. Gibbs 	/*
24048b8a9b1dSJustin T. Gibbs 	 * If we get back 0, that means that we had to stop before fully
24058b8a9b1dSJustin T. Gibbs 	 * traversing the EDT.  It also means that one of the subroutines
24068b8a9b1dSJustin T. Gibbs 	 * has set the status field to the proper value.  If we get back 1,
24078b8a9b1dSJustin T. Gibbs 	 * we've fully traversed the EDT and copied out any matching entries.
24088b8a9b1dSJustin T. Gibbs 	 */
24098b8a9b1dSJustin T. Gibbs 	if (ret == 1)
24108b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LAST;
24118b8a9b1dSJustin T. Gibbs 
24128b8a9b1dSJustin T. Gibbs 	return(ret);
24138b8a9b1dSJustin T. Gibbs }
24148b8a9b1dSJustin T. Gibbs 
24158b8a9b1dSJustin T. Gibbs static int
24168b8a9b1dSJustin T. Gibbs xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
24178b8a9b1dSJustin T. Gibbs {
24188b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
24198b8a9b1dSJustin T. Gibbs 
24208b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
24218b8a9b1dSJustin T. Gibbs 
24228b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
24238b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv == pdrv)
24248b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
24258b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
24268b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
24278b8a9b1dSJustin T. Gibbs 	     (*pdrv)->generation)) {
24288b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
24298b8a9b1dSJustin T. Gibbs 		return(0);
24308b8a9b1dSJustin T. Gibbs 	}
24318b8a9b1dSJustin T. Gibbs 
24328b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
24338b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv == pdrv)
24348b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
24358b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.periph != NULL))
24368b8a9b1dSJustin T. Gibbs 		return(xptpdperiphtraverse(pdrv,
24378b8a9b1dSJustin T. Gibbs 				(struct cam_periph *)cdm->pos.cookie.periph,
24388b8a9b1dSJustin T. Gibbs 				xptplistperiphfunc, arg));
24398b8a9b1dSJustin T. Gibbs 	else
24408b8a9b1dSJustin T. Gibbs 		return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
24418b8a9b1dSJustin T. Gibbs }
24428b8a9b1dSJustin T. Gibbs 
24438b8a9b1dSJustin T. Gibbs static int
24448b8a9b1dSJustin T. Gibbs xptplistperiphfunc(struct cam_periph *periph, void *arg)
24458b8a9b1dSJustin T. Gibbs {
24468b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
24478b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
24488b8a9b1dSJustin T. Gibbs 
24498b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
24508b8a9b1dSJustin T. Gibbs 
24518b8a9b1dSJustin T. Gibbs 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
24528b8a9b1dSJustin T. Gibbs 
24538b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
24548b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
24558b8a9b1dSJustin T. Gibbs 		return(0);
24568b8a9b1dSJustin T. Gibbs 	}
24578b8a9b1dSJustin T. Gibbs 
24588b8a9b1dSJustin T. Gibbs 	/*
24598b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this peripheral out.
24608b8a9b1dSJustin T. Gibbs 	 */
24618b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
24628b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
24638b8a9b1dSJustin T. Gibbs 
24648b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
24658b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
24668b8a9b1dSJustin T. Gibbs 
24678b8a9b1dSJustin T. Gibbs 		/*
24688b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
24698b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
24708b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
24718b8a9b1dSJustin T. Gibbs 		 */
24728b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
24738b8a9b1dSJustin T. Gibbs 			struct periph_driver **pdrv;
24748b8a9b1dSJustin T. Gibbs 
24758b8a9b1dSJustin T. Gibbs 			pdrv = NULL;
24768b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
24778b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
24788b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
24798b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PERIPH;
24808b8a9b1dSJustin T. Gibbs 
24818b8a9b1dSJustin T. Gibbs 			/*
24828b8a9b1dSJustin T. Gibbs 			 * This may look a bit non-sensical, but it is
24838b8a9b1dSJustin T. Gibbs 			 * actually quite logical.  There are very few
24848b8a9b1dSJustin T. Gibbs 			 * peripheral drivers, and bloating every peripheral
24858b8a9b1dSJustin T. Gibbs 			 * structure with a pointer back to its parent
24868b8a9b1dSJustin T. Gibbs 			 * peripheral driver linker set entry would cost
24878b8a9b1dSJustin T. Gibbs 			 * more in the long run than doing this quick lookup.
24888b8a9b1dSJustin T. Gibbs 			 */
24890b7c27b9SPeter Wemm 			for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
24908b8a9b1dSJustin T. Gibbs 				if (strcmp((*pdrv)->driver_name,
24918b8a9b1dSJustin T. Gibbs 				    periph->periph_name) == 0)
24928b8a9b1dSJustin T. Gibbs 					break;
24938b8a9b1dSJustin T. Gibbs 			}
24948b8a9b1dSJustin T. Gibbs 
249501910babSScott Long 			if (*pdrv == NULL) {
24968b8a9b1dSJustin T. Gibbs 				cdm->status = CAM_DEV_MATCH_ERROR;
24978b8a9b1dSJustin T. Gibbs 				return(0);
24988b8a9b1dSJustin T. Gibbs 			}
24998b8a9b1dSJustin T. Gibbs 
25008b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.pdrv = pdrv;
25018b8a9b1dSJustin T. Gibbs 			/*
25028b8a9b1dSJustin T. Gibbs 			 * The periph generation slot does double duty, as
25038b8a9b1dSJustin T. Gibbs 			 * does the periph pointer slot.  They are used for
25048b8a9b1dSJustin T. Gibbs 			 * both edt and pdrv lookups and positioning.
25058b8a9b1dSJustin T. Gibbs 			 */
25068b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.periph = periph;
25078b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
25088b8a9b1dSJustin T. Gibbs 				(*pdrv)->generation;
25098b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
25108b8a9b1dSJustin T. Gibbs 			return(0);
25118b8a9b1dSJustin T. Gibbs 		}
25128b8a9b1dSJustin T. Gibbs 
25138b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
25148b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
25158b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_PERIPH;
25168b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.path_id =
25178b8a9b1dSJustin T. Gibbs 			periph->path->bus->path_id;
25188b8a9b1dSJustin T. Gibbs 
25198b8a9b1dSJustin T. Gibbs 		/*
25208b8a9b1dSJustin T. Gibbs 		 * The transport layer peripheral doesn't have a target or
25218b8a9b1dSJustin T. Gibbs 		 * lun.
25228b8a9b1dSJustin T. Gibbs 		 */
25238b8a9b1dSJustin T. Gibbs 		if (periph->path->target)
25248b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_id =
25258b8a9b1dSJustin T. Gibbs 				periph->path->target->target_id;
25268b8a9b1dSJustin T. Gibbs 		else
25278b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_id = -1;
25288b8a9b1dSJustin T. Gibbs 
25298b8a9b1dSJustin T. Gibbs 		if (periph->path->device)
25308b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_lun =
25318b8a9b1dSJustin T. Gibbs 				periph->path->device->lun_id;
25328b8a9b1dSJustin T. Gibbs 		else
25338b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_lun = -1;
25348b8a9b1dSJustin T. Gibbs 
25358b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.unit_number =
25368b8a9b1dSJustin T. Gibbs 			periph->unit_number;
25378b8a9b1dSJustin T. Gibbs 		strncpy(cdm->matches[j].result.periph_result.periph_name,
25388b8a9b1dSJustin T. Gibbs 			periph->periph_name, DEV_IDLEN);
25398b8a9b1dSJustin T. Gibbs 	}
25408b8a9b1dSJustin T. Gibbs 
25418b8a9b1dSJustin T. Gibbs 	return(1);
25428b8a9b1dSJustin T. Gibbs }
25438b8a9b1dSJustin T. Gibbs 
25448b8a9b1dSJustin T. Gibbs static int
25458b8a9b1dSJustin T. Gibbs xptperiphlistmatch(struct ccb_dev_match *cdm)
25468b8a9b1dSJustin T. Gibbs {
25478b8a9b1dSJustin T. Gibbs 	int ret;
25488b8a9b1dSJustin T. Gibbs 
25498b8a9b1dSJustin T. Gibbs 	cdm->num_matches = 0;
25508b8a9b1dSJustin T. Gibbs 
25518b8a9b1dSJustin T. Gibbs 	/*
25528b8a9b1dSJustin T. Gibbs 	 * At this point in the edt traversal function, we check the bus
25538b8a9b1dSJustin T. Gibbs 	 * list generation to make sure that no busses have been added or
25548b8a9b1dSJustin T. Gibbs 	 * removed since the user last sent a XPT_DEV_MATCH ccb through.
25558b8a9b1dSJustin T. Gibbs 	 * For the peripheral driver list traversal function, however, we
25568b8a9b1dSJustin T. Gibbs 	 * don't have to worry about new peripheral driver types coming or
25578b8a9b1dSJustin T. Gibbs 	 * going; they're in a linker set, and therefore can't change
25588b8a9b1dSJustin T. Gibbs 	 * without a recompile.
25598b8a9b1dSJustin T. Gibbs 	 */
25608b8a9b1dSJustin T. Gibbs 
25618b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
25628b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv != NULL))
25638b8a9b1dSJustin T. Gibbs 		ret = xptpdrvtraverse(
25648b8a9b1dSJustin T. Gibbs 				(struct periph_driver **)cdm->pos.cookie.pdrv,
25658b8a9b1dSJustin T. Gibbs 				xptplistpdrvfunc, cdm);
25668b8a9b1dSJustin T. Gibbs 	else
25678b8a9b1dSJustin T. Gibbs 		ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
25688b8a9b1dSJustin T. Gibbs 
25698b8a9b1dSJustin T. Gibbs 	/*
25708b8a9b1dSJustin T. Gibbs 	 * If we get back 0, that means that we had to stop before fully
25718b8a9b1dSJustin T. Gibbs 	 * traversing the peripheral driver tree.  It also means that one of
25728b8a9b1dSJustin T. Gibbs 	 * the subroutines has set the status field to the proper value.  If
25738b8a9b1dSJustin T. Gibbs 	 * we get back 1, we've fully traversed the EDT and copied out any
25748b8a9b1dSJustin T. Gibbs 	 * matching entries.
25758b8a9b1dSJustin T. Gibbs 	 */
25768b8a9b1dSJustin T. Gibbs 	if (ret == 1)
25778b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LAST;
25788b8a9b1dSJustin T. Gibbs 
25798b8a9b1dSJustin T. Gibbs 	return(ret);
25808b8a9b1dSJustin T. Gibbs }
25818b8a9b1dSJustin T. Gibbs 
25828b8a9b1dSJustin T. Gibbs static int
25838b8a9b1dSJustin T. Gibbs xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
25848b8a9b1dSJustin T. Gibbs {
25858b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus, *next_bus;
25868b8a9b1dSJustin T. Gibbs 	int retval;
25878b8a9b1dSJustin T. Gibbs 
25888b8a9b1dSJustin T. Gibbs 	retval = 1;
25898b8a9b1dSJustin T. Gibbs 
25908b8a9b1dSJustin T. Gibbs 	for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
25918b8a9b1dSJustin T. Gibbs 	     bus != NULL;
25928b8a9b1dSJustin T. Gibbs 	     bus = next_bus) {
25938b8a9b1dSJustin T. Gibbs 		next_bus = TAILQ_NEXT(bus, links);
25948b8a9b1dSJustin T. Gibbs 
25958b8a9b1dSJustin T. Gibbs 		retval = tr_func(bus, arg);
25968b8a9b1dSJustin T. Gibbs 		if (retval == 0)
25978b8a9b1dSJustin T. Gibbs 			return(retval);
25988b8a9b1dSJustin T. Gibbs 	}
25998b8a9b1dSJustin T. Gibbs 
26008b8a9b1dSJustin T. Gibbs 	return(retval);
26018b8a9b1dSJustin T. Gibbs }
26028b8a9b1dSJustin T. Gibbs 
26038b8a9b1dSJustin T. Gibbs static int
26048b8a9b1dSJustin T. Gibbs xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
26058b8a9b1dSJustin T. Gibbs 		  xpt_targetfunc_t *tr_func, void *arg)
26068b8a9b1dSJustin T. Gibbs {
26078b8a9b1dSJustin T. Gibbs 	struct cam_et *target, *next_target;
26088b8a9b1dSJustin T. Gibbs 	int retval;
26098b8a9b1dSJustin T. Gibbs 
26108b8a9b1dSJustin T. Gibbs 	retval = 1;
26118b8a9b1dSJustin T. Gibbs 	for (target = (start_target ? start_target :
26128b8a9b1dSJustin T. Gibbs 		       TAILQ_FIRST(&bus->et_entries));
26138b8a9b1dSJustin T. Gibbs 	     target != NULL; target = next_target) {
26148b8a9b1dSJustin T. Gibbs 
26158b8a9b1dSJustin T. Gibbs 		next_target = TAILQ_NEXT(target, links);
26168b8a9b1dSJustin T. Gibbs 
26178b8a9b1dSJustin T. Gibbs 		retval = tr_func(target, arg);
26188b8a9b1dSJustin T. Gibbs 
26198b8a9b1dSJustin T. Gibbs 		if (retval == 0)
26208b8a9b1dSJustin T. Gibbs 			return(retval);
26218b8a9b1dSJustin T. Gibbs 	}
26228b8a9b1dSJustin T. Gibbs 
26238b8a9b1dSJustin T. Gibbs 	return(retval);
26248b8a9b1dSJustin T. Gibbs }
26258b8a9b1dSJustin T. Gibbs 
26268b8a9b1dSJustin T. Gibbs static int
26278b8a9b1dSJustin T. Gibbs xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
26288b8a9b1dSJustin T. Gibbs 		  xpt_devicefunc_t *tr_func, void *arg)
26298b8a9b1dSJustin T. Gibbs {
26308b8a9b1dSJustin T. Gibbs 	struct cam_ed *device, *next_device;
26318b8a9b1dSJustin T. Gibbs 	int retval;
26328b8a9b1dSJustin T. Gibbs 
26338b8a9b1dSJustin T. Gibbs 	retval = 1;
26348b8a9b1dSJustin T. Gibbs 	for (device = (start_device ? start_device :
26358b8a9b1dSJustin T. Gibbs 		       TAILQ_FIRST(&target->ed_entries));
26368b8a9b1dSJustin T. Gibbs 	     device != NULL;
26378b8a9b1dSJustin T. Gibbs 	     device = next_device) {
26388b8a9b1dSJustin T. Gibbs 
26398b8a9b1dSJustin T. Gibbs 		next_device = TAILQ_NEXT(device, links);
26408b8a9b1dSJustin T. Gibbs 
26418b8a9b1dSJustin T. Gibbs 		retval = tr_func(device, arg);
26428b8a9b1dSJustin T. Gibbs 
26438b8a9b1dSJustin T. Gibbs 		if (retval == 0)
26448b8a9b1dSJustin T. Gibbs 			return(retval);
26458b8a9b1dSJustin T. Gibbs 	}
26468b8a9b1dSJustin T. Gibbs 
26478b8a9b1dSJustin T. Gibbs 	return(retval);
26488b8a9b1dSJustin T. Gibbs }
26498b8a9b1dSJustin T. Gibbs 
26508b8a9b1dSJustin T. Gibbs static int
26518b8a9b1dSJustin T. Gibbs xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
26528b8a9b1dSJustin T. Gibbs 		  xpt_periphfunc_t *tr_func, void *arg)
26538b8a9b1dSJustin T. Gibbs {
26548b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph, *next_periph;
26558b8a9b1dSJustin T. Gibbs 	int retval;
26568b8a9b1dSJustin T. Gibbs 
26578b8a9b1dSJustin T. Gibbs 	retval = 1;
26588b8a9b1dSJustin T. Gibbs 
26598b8a9b1dSJustin T. Gibbs 	for (periph = (start_periph ? start_periph :
26608b8a9b1dSJustin T. Gibbs 		       SLIST_FIRST(&device->periphs));
26618b8a9b1dSJustin T. Gibbs 	     periph != NULL;
26628b8a9b1dSJustin T. Gibbs 	     periph = next_periph) {
26638b8a9b1dSJustin T. Gibbs 
26648b8a9b1dSJustin T. Gibbs 		next_periph = SLIST_NEXT(periph, periph_links);
26658b8a9b1dSJustin T. Gibbs 
26668b8a9b1dSJustin T. Gibbs 		retval = tr_func(periph, arg);
26678b8a9b1dSJustin T. Gibbs 		if (retval == 0)
26688b8a9b1dSJustin T. Gibbs 			return(retval);
26698b8a9b1dSJustin T. Gibbs 	}
26708b8a9b1dSJustin T. Gibbs 
26718b8a9b1dSJustin T. Gibbs 	return(retval);
26728b8a9b1dSJustin T. Gibbs }
26738b8a9b1dSJustin T. Gibbs 
26748b8a9b1dSJustin T. Gibbs static int
26758b8a9b1dSJustin T. Gibbs xptpdrvtraverse(struct periph_driver **start_pdrv,
26768b8a9b1dSJustin T. Gibbs 		xpt_pdrvfunc_t *tr_func, void *arg)
26778b8a9b1dSJustin T. Gibbs {
26788b8a9b1dSJustin T. Gibbs 	struct periph_driver **pdrv;
26798b8a9b1dSJustin T. Gibbs 	int retval;
26808b8a9b1dSJustin T. Gibbs 
26818b8a9b1dSJustin T. Gibbs 	retval = 1;
26828b8a9b1dSJustin T. Gibbs 
26838b8a9b1dSJustin T. Gibbs 	/*
26848b8a9b1dSJustin T. Gibbs 	 * We don't traverse the peripheral driver list like we do the
26858b8a9b1dSJustin T. Gibbs 	 * other lists, because it is a linker set, and therefore cannot be
26868b8a9b1dSJustin T. Gibbs 	 * changed during runtime.  If the peripheral driver list is ever
26878b8a9b1dSJustin T. Gibbs 	 * re-done to be something other than a linker set (i.e. it can
26888b8a9b1dSJustin T. Gibbs 	 * change while the system is running), the list traversal should
26898b8a9b1dSJustin T. Gibbs 	 * be modified to work like the other traversal functions.
26908b8a9b1dSJustin T. Gibbs 	 */
26910b7c27b9SPeter Wemm 	for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
26928b8a9b1dSJustin T. Gibbs 	     *pdrv != NULL; pdrv++) {
26938b8a9b1dSJustin T. Gibbs 		retval = tr_func(pdrv, arg);
26948b8a9b1dSJustin T. Gibbs 
26958b8a9b1dSJustin T. Gibbs 		if (retval == 0)
26968b8a9b1dSJustin T. Gibbs 			return(retval);
26978b8a9b1dSJustin T. Gibbs 	}
26988b8a9b1dSJustin T. Gibbs 
26998b8a9b1dSJustin T. Gibbs 	return(retval);
27008b8a9b1dSJustin T. Gibbs }
27018b8a9b1dSJustin T. Gibbs 
27028b8a9b1dSJustin T. Gibbs static int
27038b8a9b1dSJustin T. Gibbs xptpdperiphtraverse(struct periph_driver **pdrv,
27048b8a9b1dSJustin T. Gibbs 		    struct cam_periph *start_periph,
27058b8a9b1dSJustin T. Gibbs 		    xpt_periphfunc_t *tr_func, void *arg)
27068b8a9b1dSJustin T. Gibbs {
27078b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph, *next_periph;
27088b8a9b1dSJustin T. Gibbs 	int retval;
27098b8a9b1dSJustin T. Gibbs 
27108b8a9b1dSJustin T. Gibbs 	retval = 1;
27118b8a9b1dSJustin T. Gibbs 
27128b8a9b1dSJustin T. Gibbs 	for (periph = (start_periph ? start_periph :
27138b8a9b1dSJustin T. Gibbs 	     TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
27148b8a9b1dSJustin T. Gibbs 	     periph = next_periph) {
27158b8a9b1dSJustin T. Gibbs 
27168b8a9b1dSJustin T. Gibbs 		next_periph = TAILQ_NEXT(periph, unit_links);
27178b8a9b1dSJustin T. Gibbs 
27188b8a9b1dSJustin T. Gibbs 		retval = tr_func(periph, arg);
27198b8a9b1dSJustin T. Gibbs 		if (retval == 0)
27208b8a9b1dSJustin T. Gibbs 			return(retval);
27218b8a9b1dSJustin T. Gibbs 	}
27228b8a9b1dSJustin T. Gibbs 	return(retval);
27238b8a9b1dSJustin T. Gibbs }
27248b8a9b1dSJustin T. Gibbs 
27258b8a9b1dSJustin T. Gibbs static int
27268b8a9b1dSJustin T. Gibbs xptdefbusfunc(struct cam_eb *bus, void *arg)
27278b8a9b1dSJustin T. Gibbs {
27288b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
27298b8a9b1dSJustin T. Gibbs 
27308b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
27318b8a9b1dSJustin T. Gibbs 
27328b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_BUS) {
27338b8a9b1dSJustin T. Gibbs 		xpt_busfunc_t *tr_func;
27348b8a9b1dSJustin T. Gibbs 
27358b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_busfunc_t *)tr_config->tr_func;
27368b8a9b1dSJustin T. Gibbs 
27378b8a9b1dSJustin T. Gibbs 		return(tr_func(bus, tr_config->tr_arg));
27388b8a9b1dSJustin T. Gibbs 	} else
27398b8a9b1dSJustin T. Gibbs 		return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
27408b8a9b1dSJustin T. Gibbs }
27418b8a9b1dSJustin T. Gibbs 
27428b8a9b1dSJustin T. Gibbs static int
27438b8a9b1dSJustin T. Gibbs xptdeftargetfunc(struct cam_et *target, void *arg)
27448b8a9b1dSJustin T. Gibbs {
27458b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
27468b8a9b1dSJustin T. Gibbs 
27478b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
27488b8a9b1dSJustin T. Gibbs 
27498b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_TARGET) {
27508b8a9b1dSJustin T. Gibbs 		xpt_targetfunc_t *tr_func;
27518b8a9b1dSJustin T. Gibbs 
27528b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
27538b8a9b1dSJustin T. Gibbs 
27548b8a9b1dSJustin T. Gibbs 		return(tr_func(target, tr_config->tr_arg));
27558b8a9b1dSJustin T. Gibbs 	} else
27568b8a9b1dSJustin T. Gibbs 		return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
27578b8a9b1dSJustin T. Gibbs }
27588b8a9b1dSJustin T. Gibbs 
27598b8a9b1dSJustin T. Gibbs static int
27608b8a9b1dSJustin T. Gibbs xptdefdevicefunc(struct cam_ed *device, void *arg)
27618b8a9b1dSJustin T. Gibbs {
27628b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
27638b8a9b1dSJustin T. Gibbs 
27648b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
27658b8a9b1dSJustin T. Gibbs 
27668b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_DEVICE) {
27678b8a9b1dSJustin T. Gibbs 		xpt_devicefunc_t *tr_func;
27688b8a9b1dSJustin T. Gibbs 
27698b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
27708b8a9b1dSJustin T. Gibbs 
27718b8a9b1dSJustin T. Gibbs 		return(tr_func(device, tr_config->tr_arg));
27728b8a9b1dSJustin T. Gibbs 	} else
27738b8a9b1dSJustin T. Gibbs 		return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
27748b8a9b1dSJustin T. Gibbs }
27758b8a9b1dSJustin T. Gibbs 
27768b8a9b1dSJustin T. Gibbs static int
27778b8a9b1dSJustin T. Gibbs xptdefperiphfunc(struct cam_periph *periph, void *arg)
27788b8a9b1dSJustin T. Gibbs {
27798b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
27808b8a9b1dSJustin T. Gibbs 	xpt_periphfunc_t *tr_func;
27818b8a9b1dSJustin T. Gibbs 
27828b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
27838b8a9b1dSJustin T. Gibbs 
27848b8a9b1dSJustin T. Gibbs 	tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
27858b8a9b1dSJustin T. Gibbs 
27868b8a9b1dSJustin T. Gibbs 	/*
27878b8a9b1dSJustin T. Gibbs 	 * Unlike the other default functions, we don't check for depth
27888b8a9b1dSJustin T. Gibbs 	 * here.  The peripheral driver level is the last level in the EDT,
27898b8a9b1dSJustin T. Gibbs 	 * so if we're here, we should execute the function in question.
27908b8a9b1dSJustin T. Gibbs 	 */
27918b8a9b1dSJustin T. Gibbs 	return(tr_func(periph, tr_config->tr_arg));
27928b8a9b1dSJustin T. Gibbs }
27938b8a9b1dSJustin T. Gibbs 
27948b8a9b1dSJustin T. Gibbs /*
27958b8a9b1dSJustin T. Gibbs  * Execute the given function for every bus in the EDT.
27968b8a9b1dSJustin T. Gibbs  */
27978b8a9b1dSJustin T. Gibbs static int
27988b8a9b1dSJustin T. Gibbs xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
27998b8a9b1dSJustin T. Gibbs {
28008b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
28018b8a9b1dSJustin T. Gibbs 
28028b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_BUS;
28038b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
28048b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
28058b8a9b1dSJustin T. Gibbs 
28068b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
28078b8a9b1dSJustin T. Gibbs }
28088b8a9b1dSJustin T. Gibbs 
2809bfc0eb0fSKenneth D. Merry #ifdef notusedyet
28108b8a9b1dSJustin T. Gibbs /*
28118b8a9b1dSJustin T. Gibbs  * Execute the given function for every target in the EDT.
28128b8a9b1dSJustin T. Gibbs  */
28138b8a9b1dSJustin T. Gibbs static int
28148b8a9b1dSJustin T. Gibbs xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
28158b8a9b1dSJustin T. Gibbs {
28168b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
28178b8a9b1dSJustin T. Gibbs 
28188b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_TARGET;
28198b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
28208b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
28218b8a9b1dSJustin T. Gibbs 
28228b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
28238b8a9b1dSJustin T. Gibbs }
2824bfc0eb0fSKenneth D. Merry #endif /* notusedyet */
28258b8a9b1dSJustin T. Gibbs 
28268b8a9b1dSJustin T. Gibbs /*
28278b8a9b1dSJustin T. Gibbs  * Execute the given function for every device in the EDT.
28288b8a9b1dSJustin T. Gibbs  */
28298b8a9b1dSJustin T. Gibbs static int
28308b8a9b1dSJustin T. Gibbs xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
28318b8a9b1dSJustin T. Gibbs {
28328b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
28338b8a9b1dSJustin T. Gibbs 
28348b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_DEVICE;
28358b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
28368b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
28378b8a9b1dSJustin T. Gibbs 
28388b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
28398b8a9b1dSJustin T. Gibbs }
28408b8a9b1dSJustin T. Gibbs 
2841bfc0eb0fSKenneth D. Merry #ifdef notusedyet
28428b8a9b1dSJustin T. Gibbs /*
28438b8a9b1dSJustin T. Gibbs  * Execute the given function for every peripheral in the EDT.
28448b8a9b1dSJustin T. Gibbs  */
28458b8a9b1dSJustin T. Gibbs static int
28468b8a9b1dSJustin T. Gibbs xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
28478b8a9b1dSJustin T. Gibbs {
28488b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
28498b8a9b1dSJustin T. Gibbs 
28508b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_PERIPH;
28518b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
28528b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
28538b8a9b1dSJustin T. Gibbs 
28548b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
28558b8a9b1dSJustin T. Gibbs }
2856bfc0eb0fSKenneth D. Merry #endif /* notusedyet */
28578b8a9b1dSJustin T. Gibbs 
28588b8a9b1dSJustin T. Gibbs static int
28598b8a9b1dSJustin T. Gibbs xptsetasyncfunc(struct cam_ed *device, void *arg)
28608b8a9b1dSJustin T. Gibbs {
28618b8a9b1dSJustin T. Gibbs 	struct cam_path path;
28628b8a9b1dSJustin T. Gibbs 	struct ccb_getdev cgd;
28638b8a9b1dSJustin T. Gibbs 	struct async_node *cur_entry;
28648b8a9b1dSJustin T. Gibbs 
28658b8a9b1dSJustin T. Gibbs 	cur_entry = (struct async_node *)arg;
28668b8a9b1dSJustin T. Gibbs 
2867c8bead2aSJustin T. Gibbs 	/*
2868c8bead2aSJustin T. Gibbs 	 * Don't report unconfigured devices (Wildcard devs,
2869c8bead2aSJustin T. Gibbs 	 * devices only for target mode, device instances
2870c8bead2aSJustin T. Gibbs 	 * that have been invalidated but are waiting for
2871c8bead2aSJustin T. Gibbs 	 * their last reference count to be released).
2872c8bead2aSJustin T. Gibbs 	 */
2873c8bead2aSJustin T. Gibbs 	if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2874c8bead2aSJustin T. Gibbs 		return (1);
2875c8bead2aSJustin T. Gibbs 
28768b8a9b1dSJustin T. Gibbs 	xpt_compile_path(&path,
28778b8a9b1dSJustin T. Gibbs 			 NULL,
28788b8a9b1dSJustin T. Gibbs 			 device->target->bus->path_id,
28798b8a9b1dSJustin T. Gibbs 			 device->target->target_id,
28808b8a9b1dSJustin T. Gibbs 			 device->lun_id);
28818b8a9b1dSJustin T. Gibbs 	xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
28828b8a9b1dSJustin T. Gibbs 	cgd.ccb_h.func_code = XPT_GDEV_TYPE;
28838b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&cgd);
28848b8a9b1dSJustin T. Gibbs 	cur_entry->callback(cur_entry->callback_arg,
28858b8a9b1dSJustin T. Gibbs 			    AC_FOUND_DEVICE,
28868b8a9b1dSJustin T. Gibbs 			    &path, &cgd);
28878b8a9b1dSJustin T. Gibbs 	xpt_release_path(&path);
28888b8a9b1dSJustin T. Gibbs 
28898b8a9b1dSJustin T. Gibbs 	return(1);
28908b8a9b1dSJustin T. Gibbs }
2891c8bead2aSJustin T. Gibbs 
28928b8a9b1dSJustin T. Gibbs static int
28938b8a9b1dSJustin T. Gibbs xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
28948b8a9b1dSJustin T. Gibbs {
28958b8a9b1dSJustin T. Gibbs 	struct cam_path path;
28968b8a9b1dSJustin T. Gibbs 	struct ccb_pathinq cpi;
28978b8a9b1dSJustin T. Gibbs 	struct async_node *cur_entry;
28988b8a9b1dSJustin T. Gibbs 
28998b8a9b1dSJustin T. Gibbs 	cur_entry = (struct async_node *)arg;
29008b8a9b1dSJustin T. Gibbs 
29018b8a9b1dSJustin T. Gibbs 	xpt_compile_path(&path, /*periph*/NULL,
29028b8a9b1dSJustin T. Gibbs 			 bus->sim->path_id,
29038b8a9b1dSJustin T. Gibbs 			 CAM_TARGET_WILDCARD,
29048b8a9b1dSJustin T. Gibbs 			 CAM_LUN_WILDCARD);
29058b8a9b1dSJustin T. Gibbs 	xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
29068b8a9b1dSJustin T. Gibbs 	cpi.ccb_h.func_code = XPT_PATH_INQ;
29078b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&cpi);
29088b8a9b1dSJustin T. Gibbs 	cur_entry->callback(cur_entry->callback_arg,
29098b8a9b1dSJustin T. Gibbs 			    AC_PATH_REGISTERED,
29108b8a9b1dSJustin T. Gibbs 			    &path, &cpi);
29118b8a9b1dSJustin T. Gibbs 	xpt_release_path(&path);
29128b8a9b1dSJustin T. Gibbs 
29138b8a9b1dSJustin T. Gibbs 	return(1);
29148b8a9b1dSJustin T. Gibbs }
29158b8a9b1dSJustin T. Gibbs 
29168b8a9b1dSJustin T. Gibbs void
29178b8a9b1dSJustin T. Gibbs xpt_action(union ccb *start_ccb)
29188b8a9b1dSJustin T. Gibbs {
29199911ecf9SJustin T. Gibbs 	int iopl;
29209911ecf9SJustin T. Gibbs 
292168153f43SScott Long 	GIANT_REQUIRED;
292268153f43SScott Long 
29238b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
29248b8a9b1dSJustin T. Gibbs 
29258b8a9b1dSJustin T. Gibbs 	start_ccb->ccb_h.status = CAM_REQ_INPROG;
29268b8a9b1dSJustin T. Gibbs 
29279911ecf9SJustin T. Gibbs 	iopl = splsoftcam();
29288b8a9b1dSJustin T. Gibbs 	switch (start_ccb->ccb_h.func_code) {
29298b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
2930d05caa00SKenneth D. Merry 	{
29313393f8daSKenneth D. Merry 		struct cam_ed *device;
2932d05caa00SKenneth D. Merry #ifdef CAMDEBUG
2933d05caa00SKenneth D. Merry 		char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2934d05caa00SKenneth D. Merry 		struct cam_path *path;
2935d05caa00SKenneth D. Merry 
2936d05caa00SKenneth D. Merry 		path = start_ccb->ccb_h.path;
2937d05caa00SKenneth D. Merry #endif
2938d05caa00SKenneth D. Merry 
29398b8a9b1dSJustin T. Gibbs 		/*
29408b8a9b1dSJustin T. Gibbs 		 * For the sake of compatibility with SCSI-1
29418b8a9b1dSJustin T. Gibbs 		 * devices that may not understand the identify
29428b8a9b1dSJustin T. Gibbs 		 * message, we include lun information in the
29438b8a9b1dSJustin T. Gibbs 		 * second byte of all commands.  SCSI-1 specifies
29448b8a9b1dSJustin T. Gibbs 		 * that luns are a 3 bit value and reserves only 3
29458b8a9b1dSJustin T. Gibbs 		 * bits for lun information in the CDB.  Later
29468b8a9b1dSJustin T. Gibbs 		 * revisions of the SCSI spec allow for more than 8
29478b8a9b1dSJustin T. Gibbs 		 * luns, but have deprecated lun information in the
29488b8a9b1dSJustin T. Gibbs 		 * CDB.  So, if the lun won't fit, we must omit.
29498b8a9b1dSJustin T. Gibbs 		 *
29508b8a9b1dSJustin T. Gibbs 		 * Also be aware that during initial probing for devices,
29518b8a9b1dSJustin T. Gibbs 		 * the inquiry information is unknown but initialized to 0.
29528b8a9b1dSJustin T. Gibbs 		 * This means that this code will be exercised while probing
29538b8a9b1dSJustin T. Gibbs 		 * devices with an ANSI revision greater than 2.
29548b8a9b1dSJustin T. Gibbs 		 */
29553393f8daSKenneth D. Merry 		device = start_ccb->ccb_h.path->device;
29563393f8daSKenneth D. Merry 		if (device->protocol_version <= SCSI_REV_2
29578b8a9b1dSJustin T. Gibbs 		 && start_ccb->ccb_h.target_lun < 8
29588b8a9b1dSJustin T. Gibbs 		 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
29598b8a9b1dSJustin T. Gibbs 
29608b8a9b1dSJustin T. Gibbs 			start_ccb->csio.cdb_io.cdb_bytes[1] |=
29618b8a9b1dSJustin T. Gibbs 			    start_ccb->ccb_h.target_lun << 5;
29628b8a9b1dSJustin T. Gibbs 		}
29638b8a9b1dSJustin T. Gibbs 		start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2964d05caa00SKenneth D. Merry 		CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2965d05caa00SKenneth D. Merry 			  scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2966d05caa00SKenneth D. Merry 			  	       &path->device->inq_data),
2967d05caa00SKenneth D. Merry 			  scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
296850642f18SKenneth D. Merry 					  cdb_str, sizeof(cdb_str))));
2969d05caa00SKenneth D. Merry 	}
297007c6eac9SPoul-Henning Kamp 	/* FALLTHROUGH */
29718b8a9b1dSJustin T. Gibbs 	case XPT_TARGET_IO:
29728b8a9b1dSJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
29732cefde5fSJustin T. Gibbs 		start_ccb->csio.sense_resid = 0;
29742cefde5fSJustin T. Gibbs 		start_ccb->csio.resid = 0;
29752cefde5fSJustin T. Gibbs 		/* FALLTHROUGH */
297610e1cf63SKenneth D. Merry 	case XPT_RESET_DEV:
29778b8a9b1dSJustin T. Gibbs 	case XPT_ENG_EXEC:
29788b8a9b1dSJustin T. Gibbs 	{
29798b8a9b1dSJustin T. Gibbs 		struct cam_path *path;
2980d3ef3454SIan Dowse 		struct cam_sim *sim;
29818b8a9b1dSJustin T. Gibbs 		int s;
29828b8a9b1dSJustin T. Gibbs 		int runq;
29838b8a9b1dSJustin T. Gibbs 
29848b8a9b1dSJustin T. Gibbs 		path = start_ccb->ccb_h.path;
29858b8a9b1dSJustin T. Gibbs 		s = splsoftcam();
29868b8a9b1dSJustin T. Gibbs 
2987d3ef3454SIan Dowse 		sim = path->bus->sim;
2988d3ef3454SIan Dowse 		if (SIM_DEAD(sim)) {
2989d3ef3454SIan Dowse 			/* The SIM has gone; just execute the CCB directly. */
2990d3ef3454SIan Dowse 			cam_ccbq_send_ccb(&path->device->ccbq, start_ccb);
2991d3ef3454SIan Dowse 			(*(sim->sim_action))(sim, start_ccb);
2992d3ef3454SIan Dowse 			splx(s);
2993d3ef3454SIan Dowse 			break;
2994d3ef3454SIan Dowse 		}
2995d3ef3454SIan Dowse 
29968b8a9b1dSJustin T. Gibbs 		cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
29978b8a9b1dSJustin T. Gibbs 		if (path->device->qfrozen_cnt == 0)
29988b8a9b1dSJustin T. Gibbs 			runq = xpt_schedule_dev_sendq(path->bus, path->device);
29998b8a9b1dSJustin T. Gibbs 		else
30008b8a9b1dSJustin T. Gibbs 			runq = 0;
30018b8a9b1dSJustin T. Gibbs 		splx(s);
30028b8a9b1dSJustin T. Gibbs 		if (runq != 0)
30038b8a9b1dSJustin T. Gibbs 			xpt_run_dev_sendq(path->bus);
30048b8a9b1dSJustin T. Gibbs 		break;
30058b8a9b1dSJustin T. Gibbs 	}
30068b8a9b1dSJustin T. Gibbs 	case XPT_SET_TRAN_SETTINGS:
30078b8a9b1dSJustin T. Gibbs 	{
30088b8a9b1dSJustin T. Gibbs 		xpt_set_transfer_settings(&start_ccb->cts,
300903e3511bSJustin T. Gibbs 					  start_ccb->ccb_h.path->device,
30108b8a9b1dSJustin T. Gibbs 					  /*async_update*/FALSE);
30118b8a9b1dSJustin T. Gibbs 		break;
30128b8a9b1dSJustin T. Gibbs 	}
30138b8a9b1dSJustin T. Gibbs 	case XPT_CALC_GEOMETRY:
30142cefde5fSJustin T. Gibbs 	{
30152cefde5fSJustin T. Gibbs 		struct cam_sim *sim;
30162cefde5fSJustin T. Gibbs 
30178b8a9b1dSJustin T. Gibbs 		/* Filter out garbage */
30188b8a9b1dSJustin T. Gibbs 		if (start_ccb->ccg.block_size == 0
30198b8a9b1dSJustin T. Gibbs 		 || start_ccb->ccg.volume_size == 0) {
30208b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.cylinders = 0;
30218b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.heads = 0;
30228b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.secs_per_track = 0;
30238b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
30248b8a9b1dSJustin T. Gibbs 			break;
30258b8a9b1dSJustin T. Gibbs 		}
30268b8a9b1dSJustin T. Gibbs #ifdef PC98
30278b8a9b1dSJustin T. Gibbs 		/*
30288b8a9b1dSJustin T. Gibbs 		 * In a PC-98 system, geometry translation depens on
30298b8a9b1dSJustin T. Gibbs 		 * the "real" device geometry obtained from mode page 4.
30308b8a9b1dSJustin T. Gibbs 		 * SCSI geometry translation is performed in the
30318b8a9b1dSJustin T. Gibbs 		 * initialization routine of the SCSI BIOS and the result
30328b8a9b1dSJustin T. Gibbs 		 * stored in host memory.  If the translation is available
30338b8a9b1dSJustin T. Gibbs 		 * in host memory, use it.  If not, rely on the default
30348b8a9b1dSJustin T. Gibbs 		 * translation the device driver performs.
30358b8a9b1dSJustin T. Gibbs 		 */
30368b8a9b1dSJustin T. Gibbs 		if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
30378b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
30388b8a9b1dSJustin T. Gibbs 			break;
30398b8a9b1dSJustin T. Gibbs 		}
30408b8a9b1dSJustin T. Gibbs #endif
30412cefde5fSJustin T. Gibbs 		sim = start_ccb->ccb_h.path->bus->sim;
30422cefde5fSJustin T. Gibbs 		(*(sim->sim_action))(sim, start_ccb);
30432cefde5fSJustin T. Gibbs 		break;
30442cefde5fSJustin T. Gibbs 	}
3045bb6087e5SJustin T. Gibbs 	case XPT_ABORT:
30462cefde5fSJustin T. Gibbs 	{
30472cefde5fSJustin T. Gibbs 		union ccb* abort_ccb;
30482cefde5fSJustin T. Gibbs 		int s;
30492cefde5fSJustin T. Gibbs 
30502cefde5fSJustin T. Gibbs 		abort_ccb = start_ccb->cab.abort_ccb;
30512cefde5fSJustin T. Gibbs 		if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
30522cefde5fSJustin T. Gibbs 
30532cefde5fSJustin T. Gibbs 			if (abort_ccb->ccb_h.pinfo.index >= 0) {
30542cefde5fSJustin T. Gibbs 				struct cam_ccbq *ccbq;
30552cefde5fSJustin T. Gibbs 
30562cefde5fSJustin T. Gibbs 				ccbq = &abort_ccb->ccb_h.path->device->ccbq;
30572cefde5fSJustin T. Gibbs 				cam_ccbq_remove_ccb(ccbq, abort_ccb);
30582cefde5fSJustin T. Gibbs 				abort_ccb->ccb_h.status =
30592cefde5fSJustin T. Gibbs 				    CAM_REQ_ABORTED|CAM_DEV_QFRZN;
30602cefde5fSJustin T. Gibbs 				xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
30612cefde5fSJustin T. Gibbs 				s = splcam();
30622cefde5fSJustin T. Gibbs 				xpt_done(abort_ccb);
30632cefde5fSJustin T. Gibbs 				splx(s);
30642cefde5fSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
30652cefde5fSJustin T. Gibbs 				break;
30662cefde5fSJustin T. Gibbs 			}
30672cefde5fSJustin T. Gibbs 			if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
30682cefde5fSJustin T. Gibbs 			 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
30692cefde5fSJustin T. Gibbs 				/*
30702cefde5fSJustin T. Gibbs 				 * We've caught this ccb en route to
30712cefde5fSJustin T. Gibbs 				 * the SIM.  Flag it for abort and the
30722cefde5fSJustin T. Gibbs 				 * SIM will do so just before starting
30732cefde5fSJustin T. Gibbs 				 * real work on the CCB.
30742cefde5fSJustin T. Gibbs 				 */
30752cefde5fSJustin T. Gibbs 				abort_ccb->ccb_h.status =
30762cefde5fSJustin T. Gibbs 				    CAM_REQ_ABORTED|CAM_DEV_QFRZN;
30772cefde5fSJustin T. Gibbs 				xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
30782cefde5fSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
30792cefde5fSJustin T. Gibbs 				break;
30802cefde5fSJustin T. Gibbs 			}
30812cefde5fSJustin T. Gibbs 		}
30822cefde5fSJustin T. Gibbs 		if (XPT_FC_IS_QUEUED(abort_ccb)
30832cefde5fSJustin T. Gibbs 		 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
30842cefde5fSJustin T. Gibbs 			/*
30852cefde5fSJustin T. Gibbs 			 * It's already completed but waiting
30862cefde5fSJustin T. Gibbs 			 * for our SWI to get to it.
30872cefde5fSJustin T. Gibbs 			 */
30882cefde5fSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_UA_ABORT;
30892cefde5fSJustin T. Gibbs 			break;
30902cefde5fSJustin T. Gibbs 		}
30912cefde5fSJustin T. Gibbs 		/*
30922cefde5fSJustin T. Gibbs 		 * If we weren't able to take care of the abort request
30932cefde5fSJustin T. Gibbs 		 * in the XPT, pass the request down to the SIM for processing.
30942cefde5fSJustin T. Gibbs 		 */
30952cefde5fSJustin T. Gibbs 	}
309607c6eac9SPoul-Henning Kamp 	/* FALLTHROUGH */
30978b8a9b1dSJustin T. Gibbs 	case XPT_ACCEPT_TARGET_IO:
30988b8a9b1dSJustin T. Gibbs 	case XPT_EN_LUN:
30998b8a9b1dSJustin T. Gibbs 	case XPT_IMMED_NOTIFY:
31008b8a9b1dSJustin T. Gibbs 	case XPT_NOTIFY_ACK:
31018b8a9b1dSJustin T. Gibbs 	case XPT_GET_TRAN_SETTINGS:
31028b8a9b1dSJustin T. Gibbs 	case XPT_RESET_BUS:
31038b8a9b1dSJustin T. Gibbs 	{
31048b8a9b1dSJustin T. Gibbs 		struct cam_sim *sim;
31058b8a9b1dSJustin T. Gibbs 
31068b8a9b1dSJustin T. Gibbs 		sim = start_ccb->ccb_h.path->bus->sim;
31078b8a9b1dSJustin T. Gibbs 		(*(sim->sim_action))(sim, start_ccb);
31088b8a9b1dSJustin T. Gibbs 		break;
31098b8a9b1dSJustin T. Gibbs 	}
311087cfaf0eSJustin T. Gibbs 	case XPT_PATH_INQ:
311187cfaf0eSJustin T. Gibbs 	{
311287cfaf0eSJustin T. Gibbs 		struct cam_sim *sim;
311387cfaf0eSJustin T. Gibbs 
311487cfaf0eSJustin T. Gibbs 		sim = start_ccb->ccb_h.path->bus->sim;
311587cfaf0eSJustin T. Gibbs 		(*(sim->sim_action))(sim, start_ccb);
311687cfaf0eSJustin T. Gibbs 		break;
311787cfaf0eSJustin T. Gibbs 	}
311887cfaf0eSJustin T. Gibbs 	case XPT_PATH_STATS:
311987cfaf0eSJustin T. Gibbs 		start_ccb->cpis.last_reset =
312087cfaf0eSJustin T. Gibbs 			start_ccb->ccb_h.path->bus->last_reset;
312187cfaf0eSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
312287cfaf0eSJustin T. Gibbs 		break;
31238b8a9b1dSJustin T. Gibbs 	case XPT_GDEV_TYPE:
3124a5479bc5SJustin T. Gibbs 	{
312587cfaf0eSJustin T. Gibbs 		struct cam_ed *dev;
3126a5479bc5SJustin T. Gibbs 		int s;
3127a5479bc5SJustin T. Gibbs 
312887cfaf0eSJustin T. Gibbs 		dev = start_ccb->ccb_h.path->device;
3129a5479bc5SJustin T. Gibbs 		s = splcam();
313087cfaf0eSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
31318b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
31328b8a9b1dSJustin T. Gibbs 		} else {
31338b8a9b1dSJustin T. Gibbs 			struct ccb_getdev *cgd;
313487cfaf0eSJustin T. Gibbs 			struct cam_eb *bus;
31358b8a9b1dSJustin T. Gibbs 			struct cam_et *tar;
31368b8a9b1dSJustin T. Gibbs 
31378b8a9b1dSJustin T. Gibbs 			cgd = &start_ccb->cgd;
313887cfaf0eSJustin T. Gibbs 			bus = cgd->ccb_h.path->bus;
31398b8a9b1dSJustin T. Gibbs 			tar = cgd->ccb_h.path->target;
31408b8a9b1dSJustin T. Gibbs 			cgd->inq_data = dev->inq_data;
31418b8a9b1dSJustin T. Gibbs 			cgd->ccb_h.status = CAM_REQ_CMP;
31428b8a9b1dSJustin T. Gibbs 			cgd->serial_num_len = dev->serial_num_len;
31438b8a9b1dSJustin T. Gibbs 			if ((dev->serial_num_len > 0)
31448b8a9b1dSJustin T. Gibbs 			 && (dev->serial_num != NULL))
31458b8a9b1dSJustin T. Gibbs 				bcopy(dev->serial_num, cgd->serial_num,
31468b8a9b1dSJustin T. Gibbs 				      dev->serial_num_len);
31478b8a9b1dSJustin T. Gibbs 		}
3148a5479bc5SJustin T. Gibbs 		splx(s);
31498b8a9b1dSJustin T. Gibbs 		break;
3150a5479bc5SJustin T. Gibbs 	}
315187cfaf0eSJustin T. Gibbs 	case XPT_GDEV_STATS:
315287cfaf0eSJustin T. Gibbs 	{
315387cfaf0eSJustin T. Gibbs 		struct cam_ed *dev;
315487cfaf0eSJustin T. Gibbs 		int s;
315587cfaf0eSJustin T. Gibbs 
315687cfaf0eSJustin T. Gibbs 		dev = start_ccb->ccb_h.path->device;
315787cfaf0eSJustin T. Gibbs 		s = splcam();
315887cfaf0eSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
315987cfaf0eSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
316087cfaf0eSJustin T. Gibbs 		} else {
316187cfaf0eSJustin T. Gibbs 			struct ccb_getdevstats *cgds;
316287cfaf0eSJustin T. Gibbs 			struct cam_eb *bus;
316387cfaf0eSJustin T. Gibbs 			struct cam_et *tar;
316487cfaf0eSJustin T. Gibbs 
316587cfaf0eSJustin T. Gibbs 			cgds = &start_ccb->cgds;
316687cfaf0eSJustin T. Gibbs 			bus = cgds->ccb_h.path->bus;
316787cfaf0eSJustin T. Gibbs 			tar = cgds->ccb_h.path->target;
316887cfaf0eSJustin T. Gibbs 			cgds->dev_openings = dev->ccbq.dev_openings;
316987cfaf0eSJustin T. Gibbs 			cgds->dev_active = dev->ccbq.dev_active;
317087cfaf0eSJustin T. Gibbs 			cgds->devq_openings = dev->ccbq.devq_openings;
317187cfaf0eSJustin T. Gibbs 			cgds->devq_queued = dev->ccbq.queue.entries;
317287cfaf0eSJustin T. Gibbs 			cgds->held = dev->ccbq.held;
317387cfaf0eSJustin T. Gibbs 			cgds->last_reset = tar->last_reset;
317482815562SJustin T. Gibbs 			cgds->maxtags = dev->quirk->maxtags;
317582815562SJustin T. Gibbs 			cgds->mintags = dev->quirk->mintags;
317687cfaf0eSJustin T. Gibbs 			if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
317787cfaf0eSJustin T. Gibbs 				cgds->last_reset = bus->last_reset;
317887cfaf0eSJustin T. Gibbs 			cgds->ccb_h.status = CAM_REQ_CMP;
317987cfaf0eSJustin T. Gibbs 		}
318087cfaf0eSJustin T. Gibbs 		splx(s);
318187cfaf0eSJustin T. Gibbs 		break;
318287cfaf0eSJustin T. Gibbs 	}
31838b8a9b1dSJustin T. Gibbs 	case XPT_GDEVLIST:
31848b8a9b1dSJustin T. Gibbs 	{
31858b8a9b1dSJustin T. Gibbs 		struct cam_periph	*nperiph;
31868b8a9b1dSJustin T. Gibbs 		struct periph_list	*periph_head;
31878b8a9b1dSJustin T. Gibbs 		struct ccb_getdevlist	*cgdl;
31883393f8daSKenneth D. Merry 		u_int			i;
31898b8a9b1dSJustin T. Gibbs 		int			s;
31908b8a9b1dSJustin T. Gibbs 		struct cam_ed		*device;
31918b8a9b1dSJustin T. Gibbs 		int			found;
31928b8a9b1dSJustin T. Gibbs 
31938b8a9b1dSJustin T. Gibbs 
31948b8a9b1dSJustin T. Gibbs 		found = 0;
31958b8a9b1dSJustin T. Gibbs 
31968b8a9b1dSJustin T. Gibbs 		/*
31978b8a9b1dSJustin T. Gibbs 		 * Don't want anyone mucking with our data.
31988b8a9b1dSJustin T. Gibbs 		 */
3199a5479bc5SJustin T. Gibbs 		s = splcam();
32008b8a9b1dSJustin T. Gibbs 		device = start_ccb->ccb_h.path->device;
32018b8a9b1dSJustin T. Gibbs 		periph_head = &device->periphs;
32028b8a9b1dSJustin T. Gibbs 		cgdl = &start_ccb->cgdl;
32038b8a9b1dSJustin T. Gibbs 
32048b8a9b1dSJustin T. Gibbs 		/*
32058b8a9b1dSJustin T. Gibbs 		 * Check and see if the list has changed since the user
32068b8a9b1dSJustin T. Gibbs 		 * last requested a list member.  If so, tell them that the
32078b8a9b1dSJustin T. Gibbs 		 * list has changed, and therefore they need to start over
32088b8a9b1dSJustin T. Gibbs 		 * from the beginning.
32098b8a9b1dSJustin T. Gibbs 		 */
32108b8a9b1dSJustin T. Gibbs 		if ((cgdl->index != 0) &&
32118b8a9b1dSJustin T. Gibbs 		    (cgdl->generation != device->generation)) {
32128b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
32138b8a9b1dSJustin T. Gibbs 			splx(s);
32148b8a9b1dSJustin T. Gibbs 			break;
32158b8a9b1dSJustin T. Gibbs 		}
32168b8a9b1dSJustin T. Gibbs 
32178b8a9b1dSJustin T. Gibbs 		/*
32188b8a9b1dSJustin T. Gibbs 		 * Traverse the list of peripherals and attempt to find
32198b8a9b1dSJustin T. Gibbs 		 * the requested peripheral.
32208b8a9b1dSJustin T. Gibbs 		 */
3221fc2ffbe6SPoul-Henning Kamp 		for (nperiph = SLIST_FIRST(periph_head), i = 0;
32228b8a9b1dSJustin T. Gibbs 		     (nperiph != NULL) && (i <= cgdl->index);
3223fc2ffbe6SPoul-Henning Kamp 		     nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
32248b8a9b1dSJustin T. Gibbs 			if (i == cgdl->index) {
32258b8a9b1dSJustin T. Gibbs 				strncpy(cgdl->periph_name,
32268b8a9b1dSJustin T. Gibbs 					nperiph->periph_name,
32278b8a9b1dSJustin T. Gibbs 					DEV_IDLEN);
32288b8a9b1dSJustin T. Gibbs 				cgdl->unit_number = nperiph->unit_number;
32298b8a9b1dSJustin T. Gibbs 				found = 1;
32308b8a9b1dSJustin T. Gibbs 			}
32318b8a9b1dSJustin T. Gibbs 		}
32328b8a9b1dSJustin T. Gibbs 		if (found == 0) {
32338b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_ERROR;
32348b8a9b1dSJustin T. Gibbs 			splx(s);
32358b8a9b1dSJustin T. Gibbs 			break;
32368b8a9b1dSJustin T. Gibbs 		}
32378b8a9b1dSJustin T. Gibbs 
32388b8a9b1dSJustin T. Gibbs 		if (nperiph == NULL)
32398b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
32408b8a9b1dSJustin T. Gibbs 		else
32418b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_MORE_DEVS;
32428b8a9b1dSJustin T. Gibbs 
32438b8a9b1dSJustin T. Gibbs 		cgdl->index++;
32448b8a9b1dSJustin T. Gibbs 		cgdl->generation = device->generation;
32458b8a9b1dSJustin T. Gibbs 
32468b8a9b1dSJustin T. Gibbs 		splx(s);
32478b8a9b1dSJustin T. Gibbs 		cgdl->ccb_h.status = CAM_REQ_CMP;
32488b8a9b1dSJustin T. Gibbs 		break;
32498b8a9b1dSJustin T. Gibbs 	}
32508b8a9b1dSJustin T. Gibbs 	case XPT_DEV_MATCH:
32518b8a9b1dSJustin T. Gibbs 	{
32528b8a9b1dSJustin T. Gibbs 		int s;
32538b8a9b1dSJustin T. Gibbs 		dev_pos_type position_type;
32548b8a9b1dSJustin T. Gibbs 		struct ccb_dev_match *cdm;
32558b8a9b1dSJustin T. Gibbs 
32568b8a9b1dSJustin T. Gibbs 		cdm = &start_ccb->cdm;
32578b8a9b1dSJustin T. Gibbs 
32588b8a9b1dSJustin T. Gibbs 		/*
32598b8a9b1dSJustin T. Gibbs 		 * Prevent EDT changes while we traverse it.
32608b8a9b1dSJustin T. Gibbs 		 */
3261a5479bc5SJustin T. Gibbs 		s = splcam();
32628b8a9b1dSJustin T. Gibbs 		/*
32638b8a9b1dSJustin T. Gibbs 		 * There are two ways of getting at information in the EDT.
32648b8a9b1dSJustin T. Gibbs 		 * The first way is via the primary EDT tree.  It starts
32658b8a9b1dSJustin T. Gibbs 		 * with a list of busses, then a list of targets on a bus,
32668b8a9b1dSJustin T. Gibbs 		 * then devices/luns on a target, and then peripherals on a
32678b8a9b1dSJustin T. Gibbs 		 * device/lun.  The "other" way is by the peripheral driver
32688b8a9b1dSJustin T. Gibbs 		 * lists.  The peripheral driver lists are organized by
32698b8a9b1dSJustin T. Gibbs 		 * peripheral driver.  (obviously)  So it makes sense to
32708b8a9b1dSJustin T. Gibbs 		 * use the peripheral driver list if the user is looking
32718b8a9b1dSJustin T. Gibbs 		 * for something like "da1", or all "da" devices.  If the
32728b8a9b1dSJustin T. Gibbs 		 * user is looking for something on a particular bus/target
32738b8a9b1dSJustin T. Gibbs 		 * or lun, it's generally better to go through the EDT tree.
32748b8a9b1dSJustin T. Gibbs 		 */
32758b8a9b1dSJustin T. Gibbs 
32768b8a9b1dSJustin T. Gibbs 		if (cdm->pos.position_type != CAM_DEV_POS_NONE)
32778b8a9b1dSJustin T. Gibbs 			position_type = cdm->pos.position_type;
32788b8a9b1dSJustin T. Gibbs 		else {
32793393f8daSKenneth D. Merry 			u_int i;
32808b8a9b1dSJustin T. Gibbs 
32818b8a9b1dSJustin T. Gibbs 			position_type = CAM_DEV_POS_NONE;
32828b8a9b1dSJustin T. Gibbs 
32838b8a9b1dSJustin T. Gibbs 			for (i = 0; i < cdm->num_patterns; i++) {
32848b8a9b1dSJustin T. Gibbs 				if ((cdm->patterns[i].type == DEV_MATCH_BUS)
32858b8a9b1dSJustin T. Gibbs 				 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
32868b8a9b1dSJustin T. Gibbs 					position_type = CAM_DEV_POS_EDT;
32878b8a9b1dSJustin T. Gibbs 					break;
32888b8a9b1dSJustin T. Gibbs 				}
32898b8a9b1dSJustin T. Gibbs 			}
32908b8a9b1dSJustin T. Gibbs 
32918b8a9b1dSJustin T. Gibbs 			if (cdm->num_patterns == 0)
32928b8a9b1dSJustin T. Gibbs 				position_type = CAM_DEV_POS_EDT;
32938b8a9b1dSJustin T. Gibbs 			else if (position_type == CAM_DEV_POS_NONE)
32948b8a9b1dSJustin T. Gibbs 				position_type = CAM_DEV_POS_PDRV;
32958b8a9b1dSJustin T. Gibbs 		}
32968b8a9b1dSJustin T. Gibbs 
32978b8a9b1dSJustin T. Gibbs 		switch(position_type & CAM_DEV_POS_TYPEMASK) {
32988b8a9b1dSJustin T. Gibbs 		case CAM_DEV_POS_EDT:
329907c6eac9SPoul-Henning Kamp 			xptedtmatch(cdm);
33008b8a9b1dSJustin T. Gibbs 			break;
33018b8a9b1dSJustin T. Gibbs 		case CAM_DEV_POS_PDRV:
330207c6eac9SPoul-Henning Kamp 			xptperiphlistmatch(cdm);
33038b8a9b1dSJustin T. Gibbs 			break;
33048b8a9b1dSJustin T. Gibbs 		default:
33058b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_ERROR;
33068b8a9b1dSJustin T. Gibbs 			break;
33078b8a9b1dSJustin T. Gibbs 		}
33088b8a9b1dSJustin T. Gibbs 
33098b8a9b1dSJustin T. Gibbs 		splx(s);
33108b8a9b1dSJustin T. Gibbs 
33118b8a9b1dSJustin T. Gibbs 		if (cdm->status == CAM_DEV_MATCH_ERROR)
33128b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
33138b8a9b1dSJustin T. Gibbs 		else
33148b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
33158b8a9b1dSJustin T. Gibbs 
33168b8a9b1dSJustin T. Gibbs 		break;
33178b8a9b1dSJustin T. Gibbs 	}
33188b8a9b1dSJustin T. Gibbs 	case XPT_SASYNC_CB:
33198b8a9b1dSJustin T. Gibbs 	{
33208b8a9b1dSJustin T. Gibbs 		struct ccb_setasync *csa;
33218b8a9b1dSJustin T. Gibbs 		struct async_node *cur_entry;
33228b8a9b1dSJustin T. Gibbs 		struct async_list *async_head;
33238b8a9b1dSJustin T. Gibbs 		u_int32_t added;
33248b8a9b1dSJustin T. Gibbs 		int s;
33258b8a9b1dSJustin T. Gibbs 
33268b8a9b1dSJustin T. Gibbs 		csa = &start_ccb->csa;
33278b8a9b1dSJustin T. Gibbs 		added = csa->event_enable;
33288b8a9b1dSJustin T. Gibbs 		async_head = &csa->ccb_h.path->device->asyncs;
33298b8a9b1dSJustin T. Gibbs 
33308b8a9b1dSJustin T. Gibbs 		/*
33318b8a9b1dSJustin T. Gibbs 		 * If there is already an entry for us, simply
33328b8a9b1dSJustin T. Gibbs 		 * update it.
33338b8a9b1dSJustin T. Gibbs 		 */
3334a5479bc5SJustin T. Gibbs 		s = splcam();
33358b8a9b1dSJustin T. Gibbs 		cur_entry = SLIST_FIRST(async_head);
33368b8a9b1dSJustin T. Gibbs 		while (cur_entry != NULL) {
33378b8a9b1dSJustin T. Gibbs 			if ((cur_entry->callback_arg == csa->callback_arg)
33388b8a9b1dSJustin T. Gibbs 			 && (cur_entry->callback == csa->callback))
33398b8a9b1dSJustin T. Gibbs 				break;
33408b8a9b1dSJustin T. Gibbs 			cur_entry = SLIST_NEXT(cur_entry, links);
33418b8a9b1dSJustin T. Gibbs 		}
33428b8a9b1dSJustin T. Gibbs 
33438b8a9b1dSJustin T. Gibbs 		if (cur_entry != NULL) {
33448b8a9b1dSJustin T. Gibbs 		 	/*
33458b8a9b1dSJustin T. Gibbs 			 * If the request has no flags set,
33468b8a9b1dSJustin T. Gibbs 			 * remove the entry.
33478b8a9b1dSJustin T. Gibbs 			 */
33488b8a9b1dSJustin T. Gibbs 			added &= ~cur_entry->event_enable;
33498b8a9b1dSJustin T. Gibbs 			if (csa->event_enable == 0) {
33508b8a9b1dSJustin T. Gibbs 				SLIST_REMOVE(async_head, cur_entry,
3351e3975643SJake Burkholder 					     async_node, links);
3352c8bead2aSJustin T. Gibbs 				csa->ccb_h.path->device->refcount--;
3353362abc44STai-hwa Liang 				free(cur_entry, M_CAMXPT);
33548b8a9b1dSJustin T. Gibbs 			} else {
33558b8a9b1dSJustin T. Gibbs 				cur_entry->event_enable = csa->event_enable;
33568b8a9b1dSJustin T. Gibbs 			}
33578b8a9b1dSJustin T. Gibbs 		} else {
3358362abc44STai-hwa Liang 			cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
33598b8a9b1dSJustin T. Gibbs 					   M_NOWAIT);
33608b8a9b1dSJustin T. Gibbs 			if (cur_entry == NULL) {
33618b8a9b1dSJustin T. Gibbs 				splx(s);
33628b8a9b1dSJustin T. Gibbs 				csa->ccb_h.status = CAM_RESRC_UNAVAIL;
33638b8a9b1dSJustin T. Gibbs 				break;
33648b8a9b1dSJustin T. Gibbs 			}
3365434bbf6eSJustin T. Gibbs 			cur_entry->event_enable = csa->event_enable;
33668b8a9b1dSJustin T. Gibbs 			cur_entry->callback_arg = csa->callback_arg;
33678b8a9b1dSJustin T. Gibbs 			cur_entry->callback = csa->callback;
33688b8a9b1dSJustin T. Gibbs 			SLIST_INSERT_HEAD(async_head, cur_entry, links);
3369c8bead2aSJustin T. Gibbs 			csa->ccb_h.path->device->refcount++;
33708b8a9b1dSJustin T. Gibbs 		}
33718b8a9b1dSJustin T. Gibbs 
33728b8a9b1dSJustin T. Gibbs 		if ((added & AC_FOUND_DEVICE) != 0) {
33738b8a9b1dSJustin T. Gibbs 			/*
33748b8a9b1dSJustin T. Gibbs 			 * Get this peripheral up to date with all
33758b8a9b1dSJustin T. Gibbs 			 * the currently existing devices.
33768b8a9b1dSJustin T. Gibbs 			 */
33778b8a9b1dSJustin T. Gibbs 			xpt_for_all_devices(xptsetasyncfunc, cur_entry);
33788b8a9b1dSJustin T. Gibbs 		}
33798b8a9b1dSJustin T. Gibbs 		if ((added & AC_PATH_REGISTERED) != 0) {
33808b8a9b1dSJustin T. Gibbs 			/*
33818b8a9b1dSJustin T. Gibbs 			 * Get this peripheral up to date with all
33828b8a9b1dSJustin T. Gibbs 			 * the currently existing busses.
33838b8a9b1dSJustin T. Gibbs 			 */
33848b8a9b1dSJustin T. Gibbs 			xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
33858b8a9b1dSJustin T. Gibbs 		}
33868b8a9b1dSJustin T. Gibbs 		splx(s);
33878b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
33888b8a9b1dSJustin T. Gibbs 		break;
33898b8a9b1dSJustin T. Gibbs 	}
33908b8a9b1dSJustin T. Gibbs 	case XPT_REL_SIMQ:
33918b8a9b1dSJustin T. Gibbs 	{
33928b8a9b1dSJustin T. Gibbs 		struct ccb_relsim *crs;
33938b8a9b1dSJustin T. Gibbs 		struct cam_ed *dev;
33948b8a9b1dSJustin T. Gibbs 		int s;
33958b8a9b1dSJustin T. Gibbs 
33968b8a9b1dSJustin T. Gibbs 		crs = &start_ccb->crs;
33978b8a9b1dSJustin T. Gibbs 		dev = crs->ccb_h.path->device;
33988b8a9b1dSJustin T. Gibbs 		if (dev == NULL) {
33998b8a9b1dSJustin T. Gibbs 
34008b8a9b1dSJustin T. Gibbs 			crs->ccb_h.status = CAM_DEV_NOT_THERE;
34018b8a9b1dSJustin T. Gibbs 			break;
34028b8a9b1dSJustin T. Gibbs 		}
34038b8a9b1dSJustin T. Gibbs 
34048b8a9b1dSJustin T. Gibbs 		s = splcam();
34058b8a9b1dSJustin T. Gibbs 
34068b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
34078b8a9b1dSJustin T. Gibbs 
34084e359f5aSMatt Jacob  			if (INQ_DATA_TQ_ENABLED(&dev->inq_data)) {
34098b8a9b1dSJustin T. Gibbs 				/* Don't ever go below one opening */
34108b8a9b1dSJustin T. Gibbs 				if (crs->openings > 0) {
34118b8a9b1dSJustin T. Gibbs 					xpt_dev_ccbq_resize(crs->ccb_h.path,
34128b8a9b1dSJustin T. Gibbs 							    crs->openings);
34138b8a9b1dSJustin T. Gibbs 
341479ccc199SJordan K. Hubbard 					if (bootverbose) {
3415f0d9af51SMatt Jacob 						xpt_print(crs->ccb_h.path,
3416f0d9af51SMatt Jacob 						    "tagged openings now %d\n",
34178b8a9b1dSJustin T. Gibbs 						    crs->openings);
34188b8a9b1dSJustin T. Gibbs 					}
34198b8a9b1dSJustin T. Gibbs 				}
34208b8a9b1dSJustin T. Gibbs 			}
34218b8a9b1dSJustin T. Gibbs 		}
34228b8a9b1dSJustin T. Gibbs 
34238b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
34248b8a9b1dSJustin T. Gibbs 
34258b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
34268b8a9b1dSJustin T. Gibbs 
34278b8a9b1dSJustin T. Gibbs 				/*
34288b8a9b1dSJustin T. Gibbs 				 * Just extend the old timeout and decrement
34298b8a9b1dSJustin T. Gibbs 				 * the freeze count so that a single timeout
34308b8a9b1dSJustin T. Gibbs 				 * is sufficient for releasing the queue.
34318b8a9b1dSJustin T. Gibbs 				 */
34328b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
34338b8a9b1dSJustin T. Gibbs 				untimeout(xpt_release_devq_timeout,
34348b8a9b1dSJustin T. Gibbs 					  dev, dev->c_handle);
34358b8a9b1dSJustin T. Gibbs 			} else {
34368b8a9b1dSJustin T. Gibbs 
34378b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
34388b8a9b1dSJustin T. Gibbs 			}
34398b8a9b1dSJustin T. Gibbs 
34408b8a9b1dSJustin T. Gibbs 			dev->c_handle =
34418b8a9b1dSJustin T. Gibbs 				timeout(xpt_release_devq_timeout,
34428b8a9b1dSJustin T. Gibbs 					dev,
34438b8a9b1dSJustin T. Gibbs 					(crs->release_timeout * hz) / 1000);
34448b8a9b1dSJustin T. Gibbs 
34458b8a9b1dSJustin T. Gibbs 			dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
34468b8a9b1dSJustin T. Gibbs 
34478b8a9b1dSJustin T. Gibbs 		}
34488b8a9b1dSJustin T. Gibbs 
34498b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
34508b8a9b1dSJustin T. Gibbs 
34518b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
34528b8a9b1dSJustin T. Gibbs 				/*
34538b8a9b1dSJustin T. Gibbs 				 * Decrement the freeze count so that a single
34548b8a9b1dSJustin T. Gibbs 				 * completion is still sufficient to unfreeze
34558b8a9b1dSJustin T. Gibbs 				 * the queue.
34568b8a9b1dSJustin T. Gibbs 				 */
34578b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
34588b8a9b1dSJustin T. Gibbs 			} else {
34598b8a9b1dSJustin T. Gibbs 
34608b8a9b1dSJustin T. Gibbs 				dev->flags |= CAM_DEV_REL_ON_COMPLETE;
34618b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
34628b8a9b1dSJustin T. Gibbs 			}
34638b8a9b1dSJustin T. Gibbs 		}
34648b8a9b1dSJustin T. Gibbs 
34658b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
34668b8a9b1dSJustin T. Gibbs 
34678b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
34688b8a9b1dSJustin T. Gibbs 			 || (dev->ccbq.dev_active == 0)) {
34698b8a9b1dSJustin T. Gibbs 
34708b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
34718b8a9b1dSJustin T. Gibbs 			} else {
34728b8a9b1dSJustin T. Gibbs 
34738b8a9b1dSJustin T. Gibbs 				dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
34748b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
34758b8a9b1dSJustin T. Gibbs 			}
34768b8a9b1dSJustin T. Gibbs 		}
34778b8a9b1dSJustin T. Gibbs 		splx(s);
34788b8a9b1dSJustin T. Gibbs 
34798b8a9b1dSJustin T. Gibbs 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
34808b8a9b1dSJustin T. Gibbs 
34812cefde5fSJustin T. Gibbs 			xpt_release_devq(crs->ccb_h.path, /*count*/1,
34828b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
34838b8a9b1dSJustin T. Gibbs 		}
34848b8a9b1dSJustin T. Gibbs 		start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
34858b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
34868b8a9b1dSJustin T. Gibbs 		break;
34878b8a9b1dSJustin T. Gibbs 	}
34888b8a9b1dSJustin T. Gibbs 	case XPT_SCAN_BUS:
34898b8a9b1dSJustin T. Gibbs 		xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
34908b8a9b1dSJustin T. Gibbs 		break;
34918b8a9b1dSJustin T. Gibbs 	case XPT_SCAN_LUN:
34928b8a9b1dSJustin T. Gibbs 		xpt_scan_lun(start_ccb->ccb_h.path->periph,
34938b8a9b1dSJustin T. Gibbs 			     start_ccb->ccb_h.path, start_ccb->crcn.flags,
34948b8a9b1dSJustin T. Gibbs 			     start_ccb);
34958b8a9b1dSJustin T. Gibbs 		break;
34968b8a9b1dSJustin T. Gibbs 	case XPT_DEBUG: {
34978b8a9b1dSJustin T. Gibbs #ifdef CAMDEBUG
34988b8a9b1dSJustin T. Gibbs 		int s;
34998b8a9b1dSJustin T. Gibbs 
35008b8a9b1dSJustin T. Gibbs 		s = splcam();
35012cefde5fSJustin T. Gibbs #ifdef CAM_DEBUG_DELAY
35022cefde5fSJustin T. Gibbs 		cam_debug_delay = CAM_DEBUG_DELAY;
35032cefde5fSJustin T. Gibbs #endif
35048b8a9b1dSJustin T. Gibbs 		cam_dflags = start_ccb->cdbg.flags;
35058b8a9b1dSJustin T. Gibbs 		if (cam_dpath != NULL) {
35068b8a9b1dSJustin T. Gibbs 			xpt_free_path(cam_dpath);
35078b8a9b1dSJustin T. Gibbs 			cam_dpath = NULL;
35088b8a9b1dSJustin T. Gibbs 		}
35098b8a9b1dSJustin T. Gibbs 
35108b8a9b1dSJustin T. Gibbs 		if (cam_dflags != CAM_DEBUG_NONE) {
35118b8a9b1dSJustin T. Gibbs 			if (xpt_create_path(&cam_dpath, xpt_periph,
35128b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.path_id,
35138b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.target_id,
35148b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.target_lun) !=
35158b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP) {
35168b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
35178b8a9b1dSJustin T. Gibbs 				cam_dflags = CAM_DEBUG_NONE;
3518aa872be6SMatt Jacob 			} else {
35198b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
3520f0d9af51SMatt Jacob 				xpt_print(cam_dpath, "debugging flags now %x\n",
3521f0d9af51SMatt Jacob 				    cam_dflags);
3522aa872be6SMatt Jacob 			}
35238b8a9b1dSJustin T. Gibbs 		} else {
35248b8a9b1dSJustin T. Gibbs 			cam_dpath = NULL;
35258b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
35268b8a9b1dSJustin T. Gibbs 		}
35278b8a9b1dSJustin T. Gibbs 		splx(s);
35288b8a9b1dSJustin T. Gibbs #else /* !CAMDEBUG */
35298b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
35308b8a9b1dSJustin T. Gibbs #endif /* CAMDEBUG */
35318b8a9b1dSJustin T. Gibbs 		break;
35328b8a9b1dSJustin T. Gibbs 	}
35338b8a9b1dSJustin T. Gibbs 	case XPT_NOOP:
353487cfaf0eSJustin T. Gibbs 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
353587cfaf0eSJustin T. Gibbs 			xpt_freeze_devq(start_ccb->ccb_h.path, 1);
35368b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
35378b8a9b1dSJustin T. Gibbs 		break;
35388b8a9b1dSJustin T. Gibbs 	default:
35398b8a9b1dSJustin T. Gibbs 	case XPT_SDEV_TYPE:
35408b8a9b1dSJustin T. Gibbs 	case XPT_TERM_IO:
35418b8a9b1dSJustin T. Gibbs 	case XPT_ENG_INQ:
35428b8a9b1dSJustin T. Gibbs 		/* XXX Implement */
35438b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
35448b8a9b1dSJustin T. Gibbs 		break;
35458b8a9b1dSJustin T. Gibbs 	}
35469911ecf9SJustin T. Gibbs 	splx(iopl);
35478b8a9b1dSJustin T. Gibbs }
35488b8a9b1dSJustin T. Gibbs 
35498b8a9b1dSJustin T. Gibbs void
35508b8a9b1dSJustin T. Gibbs xpt_polled_action(union ccb *start_ccb)
35518b8a9b1dSJustin T. Gibbs {
35528b8a9b1dSJustin T. Gibbs 	int	  s;
35538b8a9b1dSJustin T. Gibbs 	u_int32_t timeout;
35548b8a9b1dSJustin T. Gibbs 	struct	  cam_sim *sim;
35558b8a9b1dSJustin T. Gibbs 	struct	  cam_devq *devq;
35568b8a9b1dSJustin T. Gibbs 	struct	  cam_ed *dev;
35578b8a9b1dSJustin T. Gibbs 
355868153f43SScott Long 	GIANT_REQUIRED;
355968153f43SScott Long 
35608b8a9b1dSJustin T. Gibbs 	timeout = start_ccb->ccb_h.timeout;
35618b8a9b1dSJustin T. Gibbs 	sim = start_ccb->ccb_h.path->bus->sim;
35628b8a9b1dSJustin T. Gibbs 	devq = sim->devq;
35638b8a9b1dSJustin T. Gibbs 	dev = start_ccb->ccb_h.path->device;
35648b8a9b1dSJustin T. Gibbs 
35658b8a9b1dSJustin T. Gibbs 	s = splcam();
35668b8a9b1dSJustin T. Gibbs 
35678b8a9b1dSJustin T. Gibbs 	/*
35688b8a9b1dSJustin T. Gibbs 	 * Steal an opening so that no other queued requests
35698b8a9b1dSJustin T. Gibbs 	 * can get it before us while we simulate interrupts.
35708b8a9b1dSJustin T. Gibbs 	 */
35718b8a9b1dSJustin T. Gibbs 	dev->ccbq.devq_openings--;
35728b8a9b1dSJustin T. Gibbs 	dev->ccbq.dev_openings--;
35738b8a9b1dSJustin T. Gibbs 
3574d3ef3454SIan Dowse 	while(((devq != NULL && devq->send_openings <= 0) ||
3575d3ef3454SIan Dowse 	   dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
35768b8a9b1dSJustin T. Gibbs 		DELAY(1000);
35778b8a9b1dSJustin T. Gibbs 		(*(sim->sim_poll))(sim);
35788088699fSJohn Baldwin 		camisr(&cam_bioq);
35798b8a9b1dSJustin T. Gibbs 	}
35808b8a9b1dSJustin T. Gibbs 
35818b8a9b1dSJustin T. Gibbs 	dev->ccbq.devq_openings++;
35828b8a9b1dSJustin T. Gibbs 	dev->ccbq.dev_openings++;
35838b8a9b1dSJustin T. Gibbs 
35848b8a9b1dSJustin T. Gibbs 	if (timeout != 0) {
35858b8a9b1dSJustin T. Gibbs 		xpt_action(start_ccb);
35868b8a9b1dSJustin T. Gibbs 		while(--timeout > 0) {
35878b8a9b1dSJustin T. Gibbs 			(*(sim->sim_poll))(sim);
35888088699fSJohn Baldwin 			camisr(&cam_bioq);
35898b8a9b1dSJustin T. Gibbs 			if ((start_ccb->ccb_h.status  & CAM_STATUS_MASK)
35908b8a9b1dSJustin T. Gibbs 			    != CAM_REQ_INPROG)
35918b8a9b1dSJustin T. Gibbs 				break;
35928b8a9b1dSJustin T. Gibbs 			DELAY(1000);
35938b8a9b1dSJustin T. Gibbs 		}
35948b8a9b1dSJustin T. Gibbs 		if (timeout == 0) {
35958b8a9b1dSJustin T. Gibbs 			/*
35968b8a9b1dSJustin T. Gibbs 			 * XXX Is it worth adding a sim_timeout entry
35978b8a9b1dSJustin T. Gibbs 			 * point so we can attempt recovery?  If
35988b8a9b1dSJustin T. Gibbs 			 * this is only used for dumps, I don't think
35998b8a9b1dSJustin T. Gibbs 			 * it is.
36008b8a9b1dSJustin T. Gibbs 			 */
36018b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
36028b8a9b1dSJustin T. Gibbs 		}
36038b8a9b1dSJustin T. Gibbs 	} else {
36048b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
36058b8a9b1dSJustin T. Gibbs 	}
36068b8a9b1dSJustin T. Gibbs 	splx(s);
36078b8a9b1dSJustin T. Gibbs }
36088b8a9b1dSJustin T. Gibbs 
36098b8a9b1dSJustin T. Gibbs /*
36108b8a9b1dSJustin T. Gibbs  * Schedule a peripheral driver to receive a ccb when it's
36118b8a9b1dSJustin T. Gibbs  * target device has space for more transactions.
36128b8a9b1dSJustin T. Gibbs  */
36138b8a9b1dSJustin T. Gibbs void
36148b8a9b1dSJustin T. Gibbs xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
36158b8a9b1dSJustin T. Gibbs {
36168b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
3617d3ef3454SIan Dowse 	union ccb *work_ccb;
36188b8a9b1dSJustin T. Gibbs 	int s;
36198b8a9b1dSJustin T. Gibbs 	int runq;
36208b8a9b1dSJustin T. Gibbs 
362168153f43SScott Long 	GIANT_REQUIRED;
362268153f43SScott Long 
36238b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
36248b8a9b1dSJustin T. Gibbs 	device = perph->path->device;
36258b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
36268b8a9b1dSJustin T. Gibbs 	if (periph_is_queued(perph)) {
36278b8a9b1dSJustin T. Gibbs 		/* Simply reorder based on new priority */
36288b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
36298b8a9b1dSJustin T. Gibbs 			  ("   change priority to %d\n", new_priority));
36308b8a9b1dSJustin T. Gibbs 		if (new_priority < perph->pinfo.priority) {
36318b8a9b1dSJustin T. Gibbs 			camq_change_priority(&device->drvq,
36328b8a9b1dSJustin T. Gibbs 					     perph->pinfo.index,
36338b8a9b1dSJustin T. Gibbs 					     new_priority);
36348b8a9b1dSJustin T. Gibbs 		}
36358b8a9b1dSJustin T. Gibbs 		runq = 0;
3636d3ef3454SIan Dowse 	} else if (SIM_DEAD(perph->path->bus->sim)) {
3637d3ef3454SIan Dowse 		/* The SIM is gone so just call periph_start directly. */
3638d3ef3454SIan Dowse 		work_ccb = xpt_get_ccb(perph->path->device);
3639d3ef3454SIan Dowse 		splx(s);
3640d3ef3454SIan Dowse 		if (work_ccb == NULL)
3641d3ef3454SIan Dowse 			return; /* XXX */
3642d3ef3454SIan Dowse 		xpt_setup_ccb(&work_ccb->ccb_h, perph->path, new_priority);
3643d3ef3454SIan Dowse 		perph->pinfo.priority = new_priority;
3644d3ef3454SIan Dowse 		perph->periph_start(perph, work_ccb);
3645d3ef3454SIan Dowse 		return;
36468b8a9b1dSJustin T. Gibbs 	} else {
36478b8a9b1dSJustin T. Gibbs 		/* New entry on the queue */
36488b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
36498b8a9b1dSJustin T. Gibbs 			  ("   added periph to queue\n"));
36508b8a9b1dSJustin T. Gibbs 		perph->pinfo.priority = new_priority;
36518bad620dSJustin T. Gibbs 		perph->pinfo.generation = ++device->drvq.generation;
36528b8a9b1dSJustin T. Gibbs 		camq_insert(&device->drvq, &perph->pinfo);
36538b8a9b1dSJustin T. Gibbs 		runq = xpt_schedule_dev_allocq(perph->path->bus, device);
36548b8a9b1dSJustin T. Gibbs 	}
36558b8a9b1dSJustin T. Gibbs 	splx(s);
36568b8a9b1dSJustin T. Gibbs 	if (runq != 0) {
36578b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
36588b8a9b1dSJustin T. Gibbs 			  ("   calling xpt_run_devq\n"));
36598b8a9b1dSJustin T. Gibbs 		xpt_run_dev_allocq(perph->path->bus);
36608b8a9b1dSJustin T. Gibbs 	}
36618b8a9b1dSJustin T. Gibbs }
36628b8a9b1dSJustin T. Gibbs 
36638b8a9b1dSJustin T. Gibbs 
36648b8a9b1dSJustin T. Gibbs /*
36658b8a9b1dSJustin T. Gibbs  * Schedule a device to run on a given queue.
36668b8a9b1dSJustin T. Gibbs  * If the device was inserted as a new entry on the queue,
36678b8a9b1dSJustin T. Gibbs  * return 1 meaning the device queue should be run. If we
36688b8a9b1dSJustin T. Gibbs  * were already queued, implying someone else has already
36698b8a9b1dSJustin T. Gibbs  * started the queue, return 0 so the caller doesn't attempt
36708b8a9b1dSJustin T. Gibbs  * to run the queue.  Must be run at either splsoftcam
36718b8a9b1dSJustin T. Gibbs  * (or splcam since that encompases splsoftcam).
36728b8a9b1dSJustin T. Gibbs  */
36738b8a9b1dSJustin T. Gibbs static int
36748b8a9b1dSJustin T. Gibbs xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
36758b8a9b1dSJustin T. Gibbs 		 u_int32_t new_priority)
36768b8a9b1dSJustin T. Gibbs {
36778b8a9b1dSJustin T. Gibbs 	int retval;
36788b8a9b1dSJustin T. Gibbs 	u_int32_t old_priority;
36798b8a9b1dSJustin T. Gibbs 
3680aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
36818b8a9b1dSJustin T. Gibbs 
36828b8a9b1dSJustin T. Gibbs 	old_priority = pinfo->priority;
36838b8a9b1dSJustin T. Gibbs 
36848b8a9b1dSJustin T. Gibbs 	/*
36858b8a9b1dSJustin T. Gibbs 	 * Are we already queued?
36868b8a9b1dSJustin T. Gibbs 	 */
36878b8a9b1dSJustin T. Gibbs 	if (pinfo->index != CAM_UNQUEUED_INDEX) {
36888b8a9b1dSJustin T. Gibbs 		/* Simply reorder based on new priority */
36898b8a9b1dSJustin T. Gibbs 		if (new_priority < old_priority) {
36908b8a9b1dSJustin T. Gibbs 			camq_change_priority(queue, pinfo->index,
36918b8a9b1dSJustin T. Gibbs 					     new_priority);
3692aa872be6SMatt Jacob 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
36938b8a9b1dSJustin T. Gibbs 					("changed priority to %d\n",
36948b8a9b1dSJustin T. Gibbs 					 new_priority));
36958b8a9b1dSJustin T. Gibbs 		}
36968b8a9b1dSJustin T. Gibbs 		retval = 0;
36978b8a9b1dSJustin T. Gibbs 	} else {
36988b8a9b1dSJustin T. Gibbs 		/* New entry on the queue */
36998b8a9b1dSJustin T. Gibbs 		if (new_priority < old_priority)
37008b8a9b1dSJustin T. Gibbs 			pinfo->priority = new_priority;
37018b8a9b1dSJustin T. Gibbs 
3702aa872be6SMatt Jacob 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
37038b8a9b1dSJustin T. Gibbs 				("Inserting onto queue\n"));
37048bad620dSJustin T. Gibbs 		pinfo->generation = ++queue->generation;
37058b8a9b1dSJustin T. Gibbs 		camq_insert(queue, pinfo);
37068b8a9b1dSJustin T. Gibbs 		retval = 1;
37078b8a9b1dSJustin T. Gibbs 	}
37088b8a9b1dSJustin T. Gibbs 	return (retval);
37098b8a9b1dSJustin T. Gibbs }
37108b8a9b1dSJustin T. Gibbs 
37118b8a9b1dSJustin T. Gibbs static void
37128b8a9b1dSJustin T. Gibbs xpt_run_dev_allocq(struct cam_eb *bus)
37138b8a9b1dSJustin T. Gibbs {
37148b8a9b1dSJustin T. Gibbs 	struct	cam_devq *devq;
37158b8a9b1dSJustin T. Gibbs 	int	s;
37168b8a9b1dSJustin T. Gibbs 
3717aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
37188b8a9b1dSJustin T. Gibbs 	devq = bus->sim->devq;
37198b8a9b1dSJustin T. Gibbs 
3720aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
37218b8a9b1dSJustin T. Gibbs 			("   qfrozen_cnt == 0x%x, entries == %d, "
37228b8a9b1dSJustin T. Gibbs 			 "openings == %d, active == %d\n",
37238b8a9b1dSJustin T. Gibbs 			 devq->alloc_queue.qfrozen_cnt,
37248b8a9b1dSJustin T. Gibbs 			 devq->alloc_queue.entries,
37258b8a9b1dSJustin T. Gibbs 			 devq->alloc_openings,
37268b8a9b1dSJustin T. Gibbs 			 devq->alloc_active));
37278b8a9b1dSJustin T. Gibbs 
37288b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
37298b8a9b1dSJustin T. Gibbs 	devq->alloc_queue.qfrozen_cnt++;
37308b8a9b1dSJustin T. Gibbs 	while ((devq->alloc_queue.entries > 0)
37318b8a9b1dSJustin T. Gibbs 	    && (devq->alloc_openings > 0)
37328b8a9b1dSJustin T. Gibbs 	    && (devq->alloc_queue.qfrozen_cnt <= 1)) {
37338b8a9b1dSJustin T. Gibbs 		struct	cam_ed_qinfo *qinfo;
37348b8a9b1dSJustin T. Gibbs 		struct	cam_ed *device;
37358b8a9b1dSJustin T. Gibbs 		union	ccb *work_ccb;
37368b8a9b1dSJustin T. Gibbs 		struct	cam_periph *drv;
37378b8a9b1dSJustin T. Gibbs 		struct	camq *drvq;
37388b8a9b1dSJustin T. Gibbs 
37398b8a9b1dSJustin T. Gibbs 		qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
37405a526431SJustin T. Gibbs 							   CAMQ_HEAD);
37418b8a9b1dSJustin T. Gibbs 		device = qinfo->device;
37428b8a9b1dSJustin T. Gibbs 
3743aa872be6SMatt Jacob 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
374450642f18SKenneth D. Merry 				("running device %p\n", device));
37458b8a9b1dSJustin T. Gibbs 
37468b8a9b1dSJustin T. Gibbs 		drvq = &device->drvq;
37478b8a9b1dSJustin T. Gibbs 
37488b8a9b1dSJustin T. Gibbs #ifdef CAMDEBUG
37498b8a9b1dSJustin T. Gibbs 		if (drvq->entries <= 0) {
37508b8a9b1dSJustin T. Gibbs 			panic("xpt_run_dev_allocq: "
37518b8a9b1dSJustin T. Gibbs 			      "Device on queue without any work to do");
37528b8a9b1dSJustin T. Gibbs 		}
37538b8a9b1dSJustin T. Gibbs #endif
37548b8a9b1dSJustin T. Gibbs 		if ((work_ccb = xpt_get_ccb(device)) != NULL) {
37558b8a9b1dSJustin T. Gibbs 			devq->alloc_openings--;
37568b8a9b1dSJustin T. Gibbs 			devq->alloc_active++;
37575a526431SJustin T. Gibbs 			drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
37588b8a9b1dSJustin T. Gibbs 			splx(s);
37598b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
37608b8a9b1dSJustin T. Gibbs 				      drv->pinfo.priority);
3761aa872be6SMatt Jacob 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
37628b8a9b1dSJustin T. Gibbs 					("calling periph start\n"));
37638b8a9b1dSJustin T. Gibbs 			drv->periph_start(drv, work_ccb);
37648b8a9b1dSJustin T. Gibbs 		} else {
37658b8a9b1dSJustin T. Gibbs 			/*
37668b8a9b1dSJustin T. Gibbs 			 * Malloc failure in alloc_ccb
37678b8a9b1dSJustin T. Gibbs 			 */
37688b8a9b1dSJustin T. Gibbs 			/*
37698b8a9b1dSJustin T. Gibbs 			 * XXX add us to a list to be run from free_ccb
37708b8a9b1dSJustin T. Gibbs 			 * if we don't have any ccbs active on this
37718b8a9b1dSJustin T. Gibbs 			 * device queue otherwise we may never get run
37728b8a9b1dSJustin T. Gibbs 			 * again.
37738b8a9b1dSJustin T. Gibbs 			 */
37748b8a9b1dSJustin T. Gibbs 			break;
37758b8a9b1dSJustin T. Gibbs 		}
37768b8a9b1dSJustin T. Gibbs 
37778b8a9b1dSJustin T. Gibbs 		/* Raise IPL for possible insertion and test at top of loop */
37788b8a9b1dSJustin T. Gibbs 		s = splsoftcam();
37798b8a9b1dSJustin T. Gibbs 
37808b8a9b1dSJustin T. Gibbs 		if (drvq->entries > 0) {
37818b8a9b1dSJustin T. Gibbs 			/* We have more work.  Attempt to reschedule */
37828b8a9b1dSJustin T. Gibbs 			xpt_schedule_dev_allocq(bus, device);
37838b8a9b1dSJustin T. Gibbs 		}
37848b8a9b1dSJustin T. Gibbs 	}
37858b8a9b1dSJustin T. Gibbs 	devq->alloc_queue.qfrozen_cnt--;
37868b8a9b1dSJustin T. Gibbs 	splx(s);
37878b8a9b1dSJustin T. Gibbs }
37888b8a9b1dSJustin T. Gibbs 
37898b8a9b1dSJustin T. Gibbs static void
37908b8a9b1dSJustin T. Gibbs xpt_run_dev_sendq(struct cam_eb *bus)
37918b8a9b1dSJustin T. Gibbs {
37928b8a9b1dSJustin T. Gibbs 	struct	cam_devq *devq;
37938b8a9b1dSJustin T. Gibbs 	int	s;
37948b8a9b1dSJustin T. Gibbs 
3795aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
37968b8a9b1dSJustin T. Gibbs 
37978b8a9b1dSJustin T. Gibbs 	devq = bus->sim->devq;
37988b8a9b1dSJustin T. Gibbs 
37998b8a9b1dSJustin T. Gibbs 	s = splcam();
38008b8a9b1dSJustin T. Gibbs 	devq->send_queue.qfrozen_cnt++;
38018b8a9b1dSJustin T. Gibbs 	splx(s);
38028b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
38038b8a9b1dSJustin T. Gibbs 	while ((devq->send_queue.entries > 0)
38048b8a9b1dSJustin T. Gibbs 	    && (devq->send_openings > 0)) {
38058b8a9b1dSJustin T. Gibbs 		struct	cam_ed_qinfo *qinfo;
38068b8a9b1dSJustin T. Gibbs 		struct	cam_ed *device;
38078b8a9b1dSJustin T. Gibbs 		union ccb *work_ccb;
38088b8a9b1dSJustin T. Gibbs 		struct	cam_sim *sim;
38098b8a9b1dSJustin T. Gibbs 		int	ospl;
38108b8a9b1dSJustin T. Gibbs 
38118b8a9b1dSJustin T. Gibbs 		ospl = splcam();
38128b8a9b1dSJustin T. Gibbs 	    	if (devq->send_queue.qfrozen_cnt > 1) {
38138b8a9b1dSJustin T. Gibbs 			splx(ospl);
38148b8a9b1dSJustin T. Gibbs 			break;
38158b8a9b1dSJustin T. Gibbs 		}
38168b8a9b1dSJustin T. Gibbs 
38178b8a9b1dSJustin T. Gibbs 		qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
38185a526431SJustin T. Gibbs 							   CAMQ_HEAD);
38198b8a9b1dSJustin T. Gibbs 		device = qinfo->device;
38208b8a9b1dSJustin T. Gibbs 
38218b8a9b1dSJustin T. Gibbs 		/*
38228b8a9b1dSJustin T. Gibbs 		 * If the device has been "frozen", don't attempt
38238b8a9b1dSJustin T. Gibbs 		 * to run it.
38248b8a9b1dSJustin T. Gibbs 		 */
38258b8a9b1dSJustin T. Gibbs 		if (device->qfrozen_cnt > 0) {
38268b8a9b1dSJustin T. Gibbs 			splx(ospl);
38278b8a9b1dSJustin T. Gibbs 			continue;
38288b8a9b1dSJustin T. Gibbs 		}
38298b8a9b1dSJustin T. Gibbs 
3830aa872be6SMatt Jacob 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
383150642f18SKenneth D. Merry 				("running device %p\n", device));
38328b8a9b1dSJustin T. Gibbs 
38335a526431SJustin T. Gibbs 		work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
38348b8a9b1dSJustin T. Gibbs 		if (work_ccb == NULL) {
383557b89bbcSNate Lawson 			printf("device on run queue with no ccbs???\n");
38368b8a9b1dSJustin T. Gibbs 			splx(ospl);
38378b8a9b1dSJustin T. Gibbs 			continue;
38388b8a9b1dSJustin T. Gibbs 		}
38398b8a9b1dSJustin T. Gibbs 
38408b8a9b1dSJustin T. Gibbs 		if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
38418b8a9b1dSJustin T. Gibbs 
38428b8a9b1dSJustin T. Gibbs 		 	if (num_highpower <= 0) {
38438b8a9b1dSJustin T. Gibbs 				/*
38448b8a9b1dSJustin T. Gibbs 				 * We got a high power command, but we
38458b8a9b1dSJustin T. Gibbs 				 * don't have any available slots.  Freeze
38468b8a9b1dSJustin T. Gibbs 				 * the device queue until we have a slot
38478b8a9b1dSJustin T. Gibbs 				 * available.
38488b8a9b1dSJustin T. Gibbs 				 */
38498b8a9b1dSJustin T. Gibbs 				device->qfrozen_cnt++;
38508b8a9b1dSJustin T. Gibbs 				STAILQ_INSERT_TAIL(&highpowerq,
38518b8a9b1dSJustin T. Gibbs 						   &work_ccb->ccb_h,
38528b8a9b1dSJustin T. Gibbs 						   xpt_links.stqe);
38538b8a9b1dSJustin T. Gibbs 
38548b8a9b1dSJustin T. Gibbs 				splx(ospl);
38558b8a9b1dSJustin T. Gibbs 				continue;
38568b8a9b1dSJustin T. Gibbs 			} else {
38578b8a9b1dSJustin T. Gibbs 				/*
38588b8a9b1dSJustin T. Gibbs 				 * Consume a high power slot while
38598b8a9b1dSJustin T. Gibbs 				 * this ccb runs.
38608b8a9b1dSJustin T. Gibbs 				 */
38618b8a9b1dSJustin T. Gibbs 				num_highpower--;
38628b8a9b1dSJustin T. Gibbs 			}
38638b8a9b1dSJustin T. Gibbs 		}
38648b8a9b1dSJustin T. Gibbs 		devq->active_dev = device;
38658b8a9b1dSJustin T. Gibbs 		cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
38668b8a9b1dSJustin T. Gibbs 
38678b8a9b1dSJustin T. Gibbs 		cam_ccbq_send_ccb(&device->ccbq, work_ccb);
38688b8a9b1dSJustin T. Gibbs 		splx(ospl);
38698b8a9b1dSJustin T. Gibbs 
38708b8a9b1dSJustin T. Gibbs 		devq->send_openings--;
38718b8a9b1dSJustin T. Gibbs 		devq->send_active++;
38728b8a9b1dSJustin T. Gibbs 
38735a526431SJustin T. Gibbs 		if (device->ccbq.queue.entries > 0)
38748b8a9b1dSJustin T. Gibbs 			xpt_schedule_dev_sendq(bus, device);
38758b8a9b1dSJustin T. Gibbs 
38768b8a9b1dSJustin T. Gibbs 		if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
38778b8a9b1dSJustin T. Gibbs 			/*
38788b8a9b1dSJustin T. Gibbs 			 * The client wants to freeze the queue
38798b8a9b1dSJustin T. Gibbs 			 * after this CCB is sent.
38808b8a9b1dSJustin T. Gibbs 			 */
38818b8a9b1dSJustin T. Gibbs 			ospl = splcam();
38828b8a9b1dSJustin T. Gibbs 			device->qfrozen_cnt++;
38838b8a9b1dSJustin T. Gibbs 			splx(ospl);
38848b8a9b1dSJustin T. Gibbs 		}
38858b8a9b1dSJustin T. Gibbs 
38868b8a9b1dSJustin T. Gibbs 		splx(s);
38878b8a9b1dSJustin T. Gibbs 
3888a4eb4f16SMatt Jacob 		/* In Target mode, the peripheral driver knows best... */
3889a4eb4f16SMatt Jacob 		if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3890a4eb4f16SMatt Jacob 			if ((device->inq_flags & SID_CmdQue) != 0
3891a4eb4f16SMatt Jacob 			 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
38928b8a9b1dSJustin T. Gibbs 				work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
38938b8a9b1dSJustin T. Gibbs 			else
38948b8a9b1dSJustin T. Gibbs 				/*
3895a4eb4f16SMatt Jacob 				 * Clear this in case of a retried CCB that
3896a4eb4f16SMatt Jacob 				 * failed due to a rejected tag.
38978b8a9b1dSJustin T. Gibbs 				 */
38988b8a9b1dSJustin T. Gibbs 				work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3899a4eb4f16SMatt Jacob 		}
39008b8a9b1dSJustin T. Gibbs 
39018b8a9b1dSJustin T. Gibbs 		/*
39028b8a9b1dSJustin T. Gibbs 		 * Device queues can be shared among multiple sim instances
39038b8a9b1dSJustin T. Gibbs 		 * that reside on different busses.  Use the SIM in the queue
39048b8a9b1dSJustin T. Gibbs 		 * CCB's path, rather than the one in the bus that was passed
39058b8a9b1dSJustin T. Gibbs 		 * into this function.
39068b8a9b1dSJustin T. Gibbs 		 */
39078b8a9b1dSJustin T. Gibbs 		sim = work_ccb->ccb_h.path->bus->sim;
39088b8a9b1dSJustin T. Gibbs 		(*(sim->sim_action))(sim, work_ccb);
39098b8a9b1dSJustin T. Gibbs 
39108b8a9b1dSJustin T. Gibbs 		ospl = splcam();
39118b8a9b1dSJustin T. Gibbs 		devq->active_dev = NULL;
39128b8a9b1dSJustin T. Gibbs 		splx(ospl);
39138b8a9b1dSJustin T. Gibbs 		/* Raise IPL for possible insertion and test at top of loop */
39148b8a9b1dSJustin T. Gibbs 		s = splsoftcam();
39158b8a9b1dSJustin T. Gibbs 	}
39168b8a9b1dSJustin T. Gibbs 	splx(s);
39178b8a9b1dSJustin T. Gibbs 	s = splcam();
39188b8a9b1dSJustin T. Gibbs 	devq->send_queue.qfrozen_cnt--;
39198b8a9b1dSJustin T. Gibbs 	splx(s);
39208b8a9b1dSJustin T. Gibbs }
39218b8a9b1dSJustin T. Gibbs 
39228b8a9b1dSJustin T. Gibbs /*
39238b8a9b1dSJustin T. Gibbs  * This function merges stuff from the slave ccb into the master ccb, while
39248b8a9b1dSJustin T. Gibbs  * keeping important fields in the master ccb constant.
39258b8a9b1dSJustin T. Gibbs  */
39268b8a9b1dSJustin T. Gibbs void
39278b8a9b1dSJustin T. Gibbs xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
39288b8a9b1dSJustin T. Gibbs {
392968153f43SScott Long 	GIANT_REQUIRED;
393068153f43SScott Long 
39318b8a9b1dSJustin T. Gibbs 	/*
39328b8a9b1dSJustin T. Gibbs 	 * Pull fields that are valid for peripheral drivers to set
39338b8a9b1dSJustin T. Gibbs 	 * into the master CCB along with the CCB "payload".
39348b8a9b1dSJustin T. Gibbs 	 */
39358b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
39368b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
39378b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
39388b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
39398b8a9b1dSJustin T. Gibbs 	bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
39408b8a9b1dSJustin T. Gibbs 	      sizeof(union ccb) - sizeof(struct ccb_hdr));
39418b8a9b1dSJustin T. Gibbs }
39428b8a9b1dSJustin T. Gibbs 
39438b8a9b1dSJustin T. Gibbs void
39448b8a9b1dSJustin T. Gibbs xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
39458b8a9b1dSJustin T. Gibbs {
394668153f43SScott Long 	GIANT_REQUIRED;
394768153f43SScott Long 
39488b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
39498b8a9b1dSJustin T. Gibbs 	ccb_h->pinfo.priority = priority;
39508b8a9b1dSJustin T. Gibbs 	ccb_h->path = path;
39518b8a9b1dSJustin T. Gibbs 	ccb_h->path_id = path->bus->path_id;
39528b8a9b1dSJustin T. Gibbs 	if (path->target)
39538b8a9b1dSJustin T. Gibbs 		ccb_h->target_id = path->target->target_id;
39548b8a9b1dSJustin T. Gibbs 	else
39558b8a9b1dSJustin T. Gibbs 		ccb_h->target_id = CAM_TARGET_WILDCARD;
39568b8a9b1dSJustin T. Gibbs 	if (path->device) {
39578b8a9b1dSJustin T. Gibbs 		ccb_h->target_lun = path->device->lun_id;
39588bad620dSJustin T. Gibbs 		ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
39598b8a9b1dSJustin T. Gibbs 	} else {
39608b8a9b1dSJustin T. Gibbs 		ccb_h->target_lun = CAM_TARGET_WILDCARD;
39618b8a9b1dSJustin T. Gibbs 	}
39628b8a9b1dSJustin T. Gibbs 	ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
39638b8a9b1dSJustin T. Gibbs 	ccb_h->flags = 0;
39648b8a9b1dSJustin T. Gibbs }
39658b8a9b1dSJustin T. Gibbs 
39668b8a9b1dSJustin T. Gibbs /* Path manipulation functions */
39678b8a9b1dSJustin T. Gibbs cam_status
39688b8a9b1dSJustin T. Gibbs xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
39698b8a9b1dSJustin T. Gibbs 		path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
39708b8a9b1dSJustin T. Gibbs {
39718b8a9b1dSJustin T. Gibbs 	struct	   cam_path *path;
39728b8a9b1dSJustin T. Gibbs 	cam_status status;
39738b8a9b1dSJustin T. Gibbs 
397468153f43SScott Long 	GIANT_REQUIRED;
397568153f43SScott Long 
3976362abc44STai-hwa Liang 	path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
39778b8a9b1dSJustin T. Gibbs 
39788b8a9b1dSJustin T. Gibbs 	if (path == NULL) {
39798b8a9b1dSJustin T. Gibbs 		status = CAM_RESRC_UNAVAIL;
39808b8a9b1dSJustin T. Gibbs 		return(status);
39818b8a9b1dSJustin T. Gibbs 	}
39828b8a9b1dSJustin T. Gibbs 	status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
39838b8a9b1dSJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
3984362abc44STai-hwa Liang 		free(path, M_CAMXPT);
39858b8a9b1dSJustin T. Gibbs 		path = NULL;
39868b8a9b1dSJustin T. Gibbs 	}
39878b8a9b1dSJustin T. Gibbs 	*new_path_ptr = path;
39888b8a9b1dSJustin T. Gibbs 	return (status);
39898b8a9b1dSJustin T. Gibbs }
39908b8a9b1dSJustin T. Gibbs 
39918b8a9b1dSJustin T. Gibbs static cam_status
39928b8a9b1dSJustin T. Gibbs xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
39938b8a9b1dSJustin T. Gibbs 		 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
39948b8a9b1dSJustin T. Gibbs {
39958b8a9b1dSJustin T. Gibbs 	struct	     cam_eb *bus;
39968b8a9b1dSJustin T. Gibbs 	struct	     cam_et *target;
39978b8a9b1dSJustin T. Gibbs 	struct	     cam_ed *device;
39988b8a9b1dSJustin T. Gibbs 	cam_status   status;
39998b8a9b1dSJustin T. Gibbs 	int	     s;
40008b8a9b1dSJustin T. Gibbs 
40018b8a9b1dSJustin T. Gibbs 	status = CAM_REQ_CMP;	/* Completed without error */
40028b8a9b1dSJustin T. Gibbs 	target = NULL;		/* Wildcarded */
40038b8a9b1dSJustin T. Gibbs 	device = NULL;		/* Wildcarded */
4004a5479bc5SJustin T. Gibbs 
4005a5479bc5SJustin T. Gibbs 	/*
4006a5479bc5SJustin T. Gibbs 	 * We will potentially modify the EDT, so block interrupts
4007a5479bc5SJustin T. Gibbs 	 * that may attempt to create cam paths.
4008a5479bc5SJustin T. Gibbs 	 */
4009a5479bc5SJustin T. Gibbs 	s = splcam();
40108b8a9b1dSJustin T. Gibbs 	bus = xpt_find_bus(path_id);
40118b8a9b1dSJustin T. Gibbs 	if (bus == NULL) {
40128b8a9b1dSJustin T. Gibbs 		status = CAM_PATH_INVALID;
4013c8bead2aSJustin T. Gibbs 	} else {
40148b8a9b1dSJustin T. Gibbs 		target = xpt_find_target(bus, target_id);
40158b8a9b1dSJustin T. Gibbs 		if (target == NULL) {
40168b8a9b1dSJustin T. Gibbs 			/* Create one */
40178b8a9b1dSJustin T. Gibbs 			struct cam_et *new_target;
40188b8a9b1dSJustin T. Gibbs 
40198b8a9b1dSJustin T. Gibbs 			new_target = xpt_alloc_target(bus, target_id);
40208b8a9b1dSJustin T. Gibbs 			if (new_target == NULL) {
40218b8a9b1dSJustin T. Gibbs 				status = CAM_RESRC_UNAVAIL;
40228b8a9b1dSJustin T. Gibbs 			} else {
40238b8a9b1dSJustin T. Gibbs 				target = new_target;
40248b8a9b1dSJustin T. Gibbs 			}
40258b8a9b1dSJustin T. Gibbs 		}
4026c8bead2aSJustin T. Gibbs 		if (target != NULL) {
40278b8a9b1dSJustin T. Gibbs 			device = xpt_find_device(target, lun_id);
40288b8a9b1dSJustin T. Gibbs 			if (device == NULL) {
40298b8a9b1dSJustin T. Gibbs 				/* Create one */
40308b8a9b1dSJustin T. Gibbs 				struct cam_ed *new_device;
40318b8a9b1dSJustin T. Gibbs 
40328b8a9b1dSJustin T. Gibbs 				new_device = xpt_alloc_device(bus,
40338b8a9b1dSJustin T. Gibbs 							      target,
40348b8a9b1dSJustin T. Gibbs 							      lun_id);
40358b8a9b1dSJustin T. Gibbs 				if (new_device == NULL) {
40368b8a9b1dSJustin T. Gibbs 					status = CAM_RESRC_UNAVAIL;
40378b8a9b1dSJustin T. Gibbs 				} else {
40388b8a9b1dSJustin T. Gibbs 					device = new_device;
40398b8a9b1dSJustin T. Gibbs 				}
40408b8a9b1dSJustin T. Gibbs 			}
40418b8a9b1dSJustin T. Gibbs 		}
40428b8a9b1dSJustin T. Gibbs 	}
4043a5479bc5SJustin T. Gibbs 	splx(s);
40448b8a9b1dSJustin T. Gibbs 
40458b8a9b1dSJustin T. Gibbs 	/*
40468b8a9b1dSJustin T. Gibbs 	 * Only touch the user's data if we are successful.
40478b8a9b1dSJustin T. Gibbs 	 */
40488b8a9b1dSJustin T. Gibbs 	if (status == CAM_REQ_CMP) {
40498b8a9b1dSJustin T. Gibbs 		new_path->periph = perph;
40508b8a9b1dSJustin T. Gibbs 		new_path->bus = bus;
40518b8a9b1dSJustin T. Gibbs 		new_path->target = target;
40528b8a9b1dSJustin T. Gibbs 		new_path->device = device;
40538b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
40548b8a9b1dSJustin T. Gibbs 	} else {
40558b8a9b1dSJustin T. Gibbs 		if (device != NULL)
40568b8a9b1dSJustin T. Gibbs 			xpt_release_device(bus, target, device);
40578b8a9b1dSJustin T. Gibbs 		if (target != NULL)
40588b8a9b1dSJustin T. Gibbs 			xpt_release_target(bus, target);
4059a5479bc5SJustin T. Gibbs 		if (bus != NULL)
4060a5479bc5SJustin T. Gibbs 			xpt_release_bus(bus);
40618b8a9b1dSJustin T. Gibbs 	}
40628b8a9b1dSJustin T. Gibbs 	return (status);
40638b8a9b1dSJustin T. Gibbs }
40648b8a9b1dSJustin T. Gibbs 
40658b8a9b1dSJustin T. Gibbs static void
40668b8a9b1dSJustin T. Gibbs xpt_release_path(struct cam_path *path)
40678b8a9b1dSJustin T. Gibbs {
40688b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
40699dd03ecfSJustin T. Gibbs 	if (path->device != NULL) {
40708b8a9b1dSJustin T. Gibbs 		xpt_release_device(path->bus, path->target, path->device);
40719dd03ecfSJustin T. Gibbs 		path->device = NULL;
40729dd03ecfSJustin T. Gibbs 	}
40739dd03ecfSJustin T. Gibbs 	if (path->target != NULL) {
40748b8a9b1dSJustin T. Gibbs 		xpt_release_target(path->bus, path->target);
40759dd03ecfSJustin T. Gibbs 		path->target = NULL;
40769dd03ecfSJustin T. Gibbs 	}
40779dd03ecfSJustin T. Gibbs 	if (path->bus != NULL) {
40789dd03ecfSJustin T. Gibbs 		xpt_release_bus(path->bus);
40799dd03ecfSJustin T. Gibbs 		path->bus = NULL;
40809dd03ecfSJustin T. Gibbs 	}
40818b8a9b1dSJustin T. Gibbs }
40828b8a9b1dSJustin T. Gibbs 
40838b8a9b1dSJustin T. Gibbs void
40848b8a9b1dSJustin T. Gibbs xpt_free_path(struct cam_path *path)
40858b8a9b1dSJustin T. Gibbs {
408668153f43SScott Long 	GIANT_REQUIRED;
408768153f43SScott Long 
40888b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
40898b8a9b1dSJustin T. Gibbs 	xpt_release_path(path);
4090362abc44STai-hwa Liang 	free(path, M_CAMXPT);
40918b8a9b1dSJustin T. Gibbs }
40928b8a9b1dSJustin T. Gibbs 
40938b8a9b1dSJustin T. Gibbs 
40948b8a9b1dSJustin T. Gibbs /*
40952cefde5fSJustin T. Gibbs  * Return -1 for failure, 0 for exact match, 1 for match with wildcards
40962cefde5fSJustin T. Gibbs  * in path1, 2 for match with wildcards in path2.
40978b8a9b1dSJustin T. Gibbs  */
40988b8a9b1dSJustin T. Gibbs int
40998b8a9b1dSJustin T. Gibbs xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
41008b8a9b1dSJustin T. Gibbs {
410168153f43SScott Long 	GIANT_REQUIRED;
410268153f43SScott Long 
41038b8a9b1dSJustin T. Gibbs 	int retval = 0;
41048b8a9b1dSJustin T. Gibbs 
41058b8a9b1dSJustin T. Gibbs 	if (path1->bus != path2->bus) {
41062cefde5fSJustin T. Gibbs 		if (path1->bus->path_id == CAM_BUS_WILDCARD)
41078b8a9b1dSJustin T. Gibbs 			retval = 1;
41082cefde5fSJustin T. Gibbs 		else if (path2->bus->path_id == CAM_BUS_WILDCARD)
41092cefde5fSJustin T. Gibbs 			retval = 2;
41108b8a9b1dSJustin T. Gibbs 		else
41118b8a9b1dSJustin T. Gibbs 			return (-1);
41128b8a9b1dSJustin T. Gibbs 	}
41138b8a9b1dSJustin T. Gibbs 	if (path1->target != path2->target) {
41142cefde5fSJustin T. Gibbs 		if (path1->target->target_id == CAM_TARGET_WILDCARD) {
41152cefde5fSJustin T. Gibbs 			if (retval == 0)
41168b8a9b1dSJustin T. Gibbs 				retval = 1;
41172cefde5fSJustin T. Gibbs 		} else if (path2->target->target_id == CAM_TARGET_WILDCARD)
41182cefde5fSJustin T. Gibbs 			retval = 2;
41198b8a9b1dSJustin T. Gibbs 		else
41208b8a9b1dSJustin T. Gibbs 			return (-1);
41218b8a9b1dSJustin T. Gibbs 	}
41228b8a9b1dSJustin T. Gibbs 	if (path1->device != path2->device) {
41232cefde5fSJustin T. Gibbs 		if (path1->device->lun_id == CAM_LUN_WILDCARD) {
41242cefde5fSJustin T. Gibbs 			if (retval == 0)
41258b8a9b1dSJustin T. Gibbs 				retval = 1;
41262cefde5fSJustin T. Gibbs 		} else if (path2->device->lun_id == CAM_LUN_WILDCARD)
41272cefde5fSJustin T. Gibbs 			retval = 2;
41288b8a9b1dSJustin T. Gibbs 		else
41298b8a9b1dSJustin T. Gibbs 			return (-1);
41308b8a9b1dSJustin T. Gibbs 	}
41318b8a9b1dSJustin T. Gibbs 	return (retval);
41328b8a9b1dSJustin T. Gibbs }
41338b8a9b1dSJustin T. Gibbs 
41348b8a9b1dSJustin T. Gibbs void
41358b8a9b1dSJustin T. Gibbs xpt_print_path(struct cam_path *path)
41368b8a9b1dSJustin T. Gibbs {
413768153f43SScott Long 	GIANT_REQUIRED;
413868153f43SScott Long 
41398b8a9b1dSJustin T. Gibbs 	if (path == NULL)
41408b8a9b1dSJustin T. Gibbs 		printf("(nopath): ");
41418b8a9b1dSJustin T. Gibbs 	else {
41428b8a9b1dSJustin T. Gibbs 		if (path->periph != NULL)
41438b8a9b1dSJustin T. Gibbs 			printf("(%s%d:", path->periph->periph_name,
41448b8a9b1dSJustin T. Gibbs 			       path->periph->unit_number);
41458b8a9b1dSJustin T. Gibbs 		else
41468b8a9b1dSJustin T. Gibbs 			printf("(noperiph:");
41478b8a9b1dSJustin T. Gibbs 
41488b8a9b1dSJustin T. Gibbs 		if (path->bus != NULL)
41498b8a9b1dSJustin T. Gibbs 			printf("%s%d:%d:", path->bus->sim->sim_name,
41508b8a9b1dSJustin T. Gibbs 			       path->bus->sim->unit_number,
41518b8a9b1dSJustin T. Gibbs 			       path->bus->sim->bus_id);
41528b8a9b1dSJustin T. Gibbs 		else
41538b8a9b1dSJustin T. Gibbs 			printf("nobus:");
41548b8a9b1dSJustin T. Gibbs 
41558b8a9b1dSJustin T. Gibbs 		if (path->target != NULL)
41568b8a9b1dSJustin T. Gibbs 			printf("%d:", path->target->target_id);
41578b8a9b1dSJustin T. Gibbs 		else
41588b8a9b1dSJustin T. Gibbs 			printf("X:");
41598b8a9b1dSJustin T. Gibbs 
41608b8a9b1dSJustin T. Gibbs 		if (path->device != NULL)
41618b8a9b1dSJustin T. Gibbs 			printf("%d): ", path->device->lun_id);
41628b8a9b1dSJustin T. Gibbs 		else
41638b8a9b1dSJustin T. Gibbs 			printf("X): ");
41648b8a9b1dSJustin T. Gibbs 	}
41658b8a9b1dSJustin T. Gibbs }
41668b8a9b1dSJustin T. Gibbs 
4167f0d9af51SMatt Jacob void
4168f0d9af51SMatt Jacob xpt_print(struct cam_path *path, const char *fmt, ...)
4169f0d9af51SMatt Jacob {
4170f0d9af51SMatt Jacob 	va_list ap;
4171f0d9af51SMatt Jacob 	xpt_print_path(path);
4172f0d9af51SMatt Jacob 	va_start(ap, fmt);
4173f0d9af51SMatt Jacob 	vprintf(fmt, ap);
4174f0d9af51SMatt Jacob 	va_end(ap);
4175f0d9af51SMatt Jacob }
4176f0d9af51SMatt Jacob 
41773393f8daSKenneth D. Merry int
41783393f8daSKenneth D. Merry xpt_path_string(struct cam_path *path, char *str, size_t str_len)
41793393f8daSKenneth D. Merry {
41803393f8daSKenneth D. Merry 	struct sbuf sb;
41813393f8daSKenneth D. Merry 
418268153f43SScott Long 	GIANT_REQUIRED;
418368153f43SScott Long 
41843393f8daSKenneth D. Merry 	sbuf_new(&sb, str, str_len, 0);
41853393f8daSKenneth D. Merry 
41863393f8daSKenneth D. Merry 	if (path == NULL)
41873393f8daSKenneth D. Merry 		sbuf_printf(&sb, "(nopath): ");
41883393f8daSKenneth D. Merry 	else {
41893393f8daSKenneth D. Merry 		if (path->periph != NULL)
41903393f8daSKenneth D. Merry 			sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
41913393f8daSKenneth D. Merry 				    path->periph->unit_number);
41923393f8daSKenneth D. Merry 		else
41933393f8daSKenneth D. Merry 			sbuf_printf(&sb, "(noperiph:");
41943393f8daSKenneth D. Merry 
41953393f8daSKenneth D. Merry 		if (path->bus != NULL)
41963393f8daSKenneth D. Merry 			sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
41973393f8daSKenneth D. Merry 				    path->bus->sim->unit_number,
41983393f8daSKenneth D. Merry 				    path->bus->sim->bus_id);
41993393f8daSKenneth D. Merry 		else
42003393f8daSKenneth D. Merry 			sbuf_printf(&sb, "nobus:");
42013393f8daSKenneth D. Merry 
42023393f8daSKenneth D. Merry 		if (path->target != NULL)
42033393f8daSKenneth D. Merry 			sbuf_printf(&sb, "%d:", path->target->target_id);
42043393f8daSKenneth D. Merry 		else
42053393f8daSKenneth D. Merry 			sbuf_printf(&sb, "X:");
42063393f8daSKenneth D. Merry 
42073393f8daSKenneth D. Merry 		if (path->device != NULL)
42083393f8daSKenneth D. Merry 			sbuf_printf(&sb, "%d): ", path->device->lun_id);
42093393f8daSKenneth D. Merry 		else
42103393f8daSKenneth D. Merry 			sbuf_printf(&sb, "X): ");
42113393f8daSKenneth D. Merry 	}
42123393f8daSKenneth D. Merry 	sbuf_finish(&sb);
42133393f8daSKenneth D. Merry 
42143393f8daSKenneth D. Merry 	return(sbuf_len(&sb));
42153393f8daSKenneth D. Merry }
42163393f8daSKenneth D. Merry 
42178b8a9b1dSJustin T. Gibbs path_id_t
42188b8a9b1dSJustin T. Gibbs xpt_path_path_id(struct cam_path *path)
42198b8a9b1dSJustin T. Gibbs {
422068153f43SScott Long 	GIANT_REQUIRED;
422168153f43SScott Long 
42228b8a9b1dSJustin T. Gibbs 	return(path->bus->path_id);
42238b8a9b1dSJustin T. Gibbs }
42248b8a9b1dSJustin T. Gibbs 
42258b8a9b1dSJustin T. Gibbs target_id_t
42268b8a9b1dSJustin T. Gibbs xpt_path_target_id(struct cam_path *path)
42278b8a9b1dSJustin T. Gibbs {
422868153f43SScott Long 	GIANT_REQUIRED;
422968153f43SScott Long 
42308b8a9b1dSJustin T. Gibbs 	if (path->target != NULL)
42318b8a9b1dSJustin T. Gibbs 		return (path->target->target_id);
42328b8a9b1dSJustin T. Gibbs 	else
42338b8a9b1dSJustin T. Gibbs 		return (CAM_TARGET_WILDCARD);
42348b8a9b1dSJustin T. Gibbs }
42358b8a9b1dSJustin T. Gibbs 
42368b8a9b1dSJustin T. Gibbs lun_id_t
42378b8a9b1dSJustin T. Gibbs xpt_path_lun_id(struct cam_path *path)
42388b8a9b1dSJustin T. Gibbs {
423968153f43SScott Long 	GIANT_REQUIRED;
424068153f43SScott Long 
42418b8a9b1dSJustin T. Gibbs 	if (path->device != NULL)
42428b8a9b1dSJustin T. Gibbs 		return (path->device->lun_id);
42438b8a9b1dSJustin T. Gibbs 	else
42448b8a9b1dSJustin T. Gibbs 		return (CAM_LUN_WILDCARD);
42458b8a9b1dSJustin T. Gibbs }
42468b8a9b1dSJustin T. Gibbs 
42478b8a9b1dSJustin T. Gibbs struct cam_sim *
42488b8a9b1dSJustin T. Gibbs xpt_path_sim(struct cam_path *path)
42498b8a9b1dSJustin T. Gibbs {
425068153f43SScott Long 	GIANT_REQUIRED;
425168153f43SScott Long 
42528b8a9b1dSJustin T. Gibbs 	return (path->bus->sim);
42538b8a9b1dSJustin T. Gibbs }
42548b8a9b1dSJustin T. Gibbs 
42558b8a9b1dSJustin T. Gibbs struct cam_periph*
42568b8a9b1dSJustin T. Gibbs xpt_path_periph(struct cam_path *path)
42578b8a9b1dSJustin T. Gibbs {
425868153f43SScott Long 	GIANT_REQUIRED;
425968153f43SScott Long 
42608b8a9b1dSJustin T. Gibbs 	return (path->periph);
42618b8a9b1dSJustin T. Gibbs }
42628b8a9b1dSJustin T. Gibbs 
42638b8a9b1dSJustin T. Gibbs /*
42648b8a9b1dSJustin T. Gibbs  * Release a CAM control block for the caller.  Remit the cost of the structure
42658b8a9b1dSJustin T. Gibbs  * to the device referenced by the path.  If the this device had no 'credits'
42668b8a9b1dSJustin T. Gibbs  * and peripheral drivers have registered async callbacks for this notification
42678b8a9b1dSJustin T. Gibbs  * call them now.
42688b8a9b1dSJustin T. Gibbs  */
42698b8a9b1dSJustin T. Gibbs void
42708b8a9b1dSJustin T. Gibbs xpt_release_ccb(union ccb *free_ccb)
42718b8a9b1dSJustin T. Gibbs {
42728b8a9b1dSJustin T. Gibbs 	int	 s;
42738b8a9b1dSJustin T. Gibbs 	struct	 cam_path *path;
42748b8a9b1dSJustin T. Gibbs 	struct	 cam_ed *device;
42758b8a9b1dSJustin T. Gibbs 	struct	 cam_eb *bus;
42768b8a9b1dSJustin T. Gibbs 
427768153f43SScott Long 	GIANT_REQUIRED;
427868153f43SScott Long 
4279aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
42808b8a9b1dSJustin T. Gibbs 	path = free_ccb->ccb_h.path;
42818b8a9b1dSJustin T. Gibbs 	device = path->device;
42828b8a9b1dSJustin T. Gibbs 	bus = path->bus;
42838b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
42848b8a9b1dSJustin T. Gibbs 	cam_ccbq_release_opening(&device->ccbq);
42858b8a9b1dSJustin T. Gibbs 	if (xpt_ccb_count > xpt_max_ccbs) {
42868b8a9b1dSJustin T. Gibbs 		xpt_free_ccb(free_ccb);
42878b8a9b1dSJustin T. Gibbs 		xpt_ccb_count--;
42888b8a9b1dSJustin T. Gibbs 	} else {
42898b8a9b1dSJustin T. Gibbs 		SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
42908b8a9b1dSJustin T. Gibbs 	}
4291d3ef3454SIan Dowse 	if (bus->sim->devq == NULL) {
4292d3ef3454SIan Dowse 		splx(s);
4293d3ef3454SIan Dowse 		return;
4294d3ef3454SIan Dowse 	}
42958b8a9b1dSJustin T. Gibbs 	bus->sim->devq->alloc_openings++;
42968b8a9b1dSJustin T. Gibbs 	bus->sim->devq->alloc_active--;
42978b8a9b1dSJustin T. Gibbs 	/* XXX Turn this into an inline function - xpt_run_device?? */
42988b8a9b1dSJustin T. Gibbs 	if ((device_is_alloc_queued(device) == 0)
42998b8a9b1dSJustin T. Gibbs 	 && (device->drvq.entries > 0)) {
43008b8a9b1dSJustin T. Gibbs 		xpt_schedule_dev_allocq(bus, device);
43018b8a9b1dSJustin T. Gibbs 	}
43028b8a9b1dSJustin T. Gibbs 	splx(s);
43038b8a9b1dSJustin T. Gibbs 	if (dev_allocq_is_runnable(bus->sim->devq))
43048b8a9b1dSJustin T. Gibbs 		xpt_run_dev_allocq(bus);
43058b8a9b1dSJustin T. Gibbs }
43068b8a9b1dSJustin T. Gibbs 
43078b8a9b1dSJustin T. Gibbs /* Functions accessed by SIM drivers */
43088b8a9b1dSJustin T. Gibbs 
43098b8a9b1dSJustin T. Gibbs /*
43108b8a9b1dSJustin T. Gibbs  * A sim structure, listing the SIM entry points and instance
43118b8a9b1dSJustin T. Gibbs  * identification info is passed to xpt_bus_register to hook the SIM
43128b8a9b1dSJustin T. Gibbs  * into the CAM framework.  xpt_bus_register creates a cam_eb entry
43138b8a9b1dSJustin T. Gibbs  * for this new bus and places it in the array of busses and assigns
43148b8a9b1dSJustin T. Gibbs  * it a path_id.  The path_id may be influenced by "hard wiring"
43158b8a9b1dSJustin T. Gibbs  * information specified by the user.  Once interrupt services are
43168b8a9b1dSJustin T. Gibbs  * availible, the bus will be probed.
43178b8a9b1dSJustin T. Gibbs  */
43188b8a9b1dSJustin T. Gibbs int32_t
43198b8a9b1dSJustin T. Gibbs xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
43208b8a9b1dSJustin T. Gibbs {
43218b8a9b1dSJustin T. Gibbs 	struct cam_eb *new_bus;
4322434bbf6eSJustin T. Gibbs 	struct cam_eb *old_bus;
43238b8a9b1dSJustin T. Gibbs 	struct ccb_pathinq cpi;
43248b8a9b1dSJustin T. Gibbs 	int s;
43258b8a9b1dSJustin T. Gibbs 
432668153f43SScott Long 	GIANT_REQUIRED;
432768153f43SScott Long 
43288b8a9b1dSJustin T. Gibbs 	sim->bus_id = bus;
43298b8a9b1dSJustin T. Gibbs 	new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4330362abc44STai-hwa Liang 					  M_CAMXPT, M_NOWAIT);
43318b8a9b1dSJustin T. Gibbs 	if (new_bus == NULL) {
43328b8a9b1dSJustin T. Gibbs 		/* Couldn't satisfy request */
43338b8a9b1dSJustin T. Gibbs 		return (CAM_RESRC_UNAVAIL);
43348b8a9b1dSJustin T. Gibbs 	}
43358b8a9b1dSJustin T. Gibbs 
43368b8a9b1dSJustin T. Gibbs 	if (strcmp(sim->sim_name, "xpt") != 0) {
43378b8a9b1dSJustin T. Gibbs 
4338434bbf6eSJustin T. Gibbs 		sim->path_id =
4339434bbf6eSJustin T. Gibbs 		    xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
43408b8a9b1dSJustin T. Gibbs 	}
43418b8a9b1dSJustin T. Gibbs 
4342434bbf6eSJustin T. Gibbs 	TAILQ_INIT(&new_bus->et_entries);
43438b8a9b1dSJustin T. Gibbs 	new_bus->path_id = sim->path_id;
43448b8a9b1dSJustin T. Gibbs 	new_bus->sim = sim;
434587cfaf0eSJustin T. Gibbs 	timevalclear(&new_bus->last_reset);
4346434bbf6eSJustin T. Gibbs 	new_bus->flags = 0;
4347a5479bc5SJustin T. Gibbs 	new_bus->refcount = 1;	/* Held until a bus_deregister event */
4348434bbf6eSJustin T. Gibbs 	new_bus->generation = 0;
4349a5479bc5SJustin T. Gibbs 	s = splcam();
4350434bbf6eSJustin T. Gibbs 	old_bus = TAILQ_FIRST(&xpt_busses);
4351434bbf6eSJustin T. Gibbs 	while (old_bus != NULL
4352434bbf6eSJustin T. Gibbs 	    && old_bus->path_id < new_bus->path_id)
4353434bbf6eSJustin T. Gibbs 		old_bus = TAILQ_NEXT(old_bus, links);
4354434bbf6eSJustin T. Gibbs 	if (old_bus != NULL)
4355434bbf6eSJustin T. Gibbs 		TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4356434bbf6eSJustin T. Gibbs 	else
43578b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
43588b8a9b1dSJustin T. Gibbs 	bus_generation++;
4359a5479bc5SJustin T. Gibbs 	splx(s);
43608b8a9b1dSJustin T. Gibbs 
43618b8a9b1dSJustin T. Gibbs 	/* Notify interested parties */
43628b8a9b1dSJustin T. Gibbs 	if (sim->path_id != CAM_XPT_PATH_ID) {
43638b8a9b1dSJustin T. Gibbs 		struct cam_path path;
43648b8a9b1dSJustin T. Gibbs 
43658b8a9b1dSJustin T. Gibbs 		xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
43668b8a9b1dSJustin T. Gibbs 			         CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
43678b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
43688b8a9b1dSJustin T. Gibbs 		cpi.ccb_h.func_code = XPT_PATH_INQ;
43698b8a9b1dSJustin T. Gibbs 		xpt_action((union ccb *)&cpi);
43703393f8daSKenneth D. Merry 		xpt_async(AC_PATH_REGISTERED, &path, &cpi);
43718b8a9b1dSJustin T. Gibbs 		xpt_release_path(&path);
43728b8a9b1dSJustin T. Gibbs 	}
43738b8a9b1dSJustin T. Gibbs 	return (CAM_SUCCESS);
43748b8a9b1dSJustin T. Gibbs }
43758b8a9b1dSJustin T. Gibbs 
4376434bbf6eSJustin T. Gibbs int32_t
4377434bbf6eSJustin T. Gibbs xpt_bus_deregister(path_id_t pathid)
43788b8a9b1dSJustin T. Gibbs {
4379434bbf6eSJustin T. Gibbs 	struct cam_path bus_path;
4380d3ef3454SIan Dowse 	struct cam_ed *device;
4381d3ef3454SIan Dowse 	struct cam_ed_qinfo *qinfo;
4382d3ef3454SIan Dowse 	struct cam_devq *devq;
4383d3ef3454SIan Dowse 	struct cam_periph *periph;
4384d3ef3454SIan Dowse 	struct cam_sim *ccbsim;
4385d3ef3454SIan Dowse 	union ccb *work_ccb;
4386434bbf6eSJustin T. Gibbs 	cam_status status;
4387434bbf6eSJustin T. Gibbs 
438868153f43SScott Long 	GIANT_REQUIRED;
438968153f43SScott Long 
4390434bbf6eSJustin T. Gibbs 	status = xpt_compile_path(&bus_path, NULL, pathid,
4391434bbf6eSJustin T. Gibbs 				  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4392434bbf6eSJustin T. Gibbs 	if (status != CAM_REQ_CMP)
4393434bbf6eSJustin T. Gibbs 		return (status);
4394434bbf6eSJustin T. Gibbs 
4395434bbf6eSJustin T. Gibbs 	xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4396434bbf6eSJustin T. Gibbs 	xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4397434bbf6eSJustin T. Gibbs 
4398d3ef3454SIan Dowse 	/* The SIM may be gone, so use a dummy SIM for any stray operations. */
4399d3ef3454SIan Dowse 	devq = bus_path.bus->sim->devq;
4400d3ef3454SIan Dowse 	bus_path.bus->sim = &cam_dead_sim;
4401d3ef3454SIan Dowse 
4402d3ef3454SIan Dowse 	/* Execute any pending operations now. */
4403d3ef3454SIan Dowse 	while ((qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
4404d3ef3454SIan Dowse 	    CAMQ_HEAD)) != NULL ||
4405d3ef3454SIan Dowse 	    (qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
4406d3ef3454SIan Dowse 	    CAMQ_HEAD)) != NULL) {
4407d3ef3454SIan Dowse 		do {
4408d3ef3454SIan Dowse 			device = qinfo->device;
4409d3ef3454SIan Dowse 			work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
4410d3ef3454SIan Dowse 			if (work_ccb != NULL) {
4411d3ef3454SIan Dowse 				devq->active_dev = device;
4412d3ef3454SIan Dowse 				cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
4413d3ef3454SIan Dowse 				cam_ccbq_send_ccb(&device->ccbq, work_ccb);
4414d3ef3454SIan Dowse 				ccbsim = work_ccb->ccb_h.path->bus->sim;
4415d3ef3454SIan Dowse 				(*(ccbsim->sim_action))(ccbsim, work_ccb);
4416d3ef3454SIan Dowse 			}
4417d3ef3454SIan Dowse 
4418d3ef3454SIan Dowse 			periph = (struct cam_periph *)camq_remove(&device->drvq,
4419d3ef3454SIan Dowse 			    CAMQ_HEAD);
4420d3ef3454SIan Dowse 			if (periph != NULL)
4421d3ef3454SIan Dowse 				xpt_schedule(periph, periph->pinfo.priority);
4422d3ef3454SIan Dowse 		} while (work_ccb != NULL || periph != NULL);
4423d3ef3454SIan Dowse 	}
4424d3ef3454SIan Dowse 
4425d3ef3454SIan Dowse 	/* Make sure all completed CCBs are processed. */
4426d3ef3454SIan Dowse 	while (!TAILQ_EMPTY(&cam_bioq)) {
4427d3ef3454SIan Dowse 		camisr(&cam_bioq);
4428d3ef3454SIan Dowse 
4429d3ef3454SIan Dowse 		/* Repeat the async's for the benefit of any new devices. */
4430d3ef3454SIan Dowse 		xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4431d3ef3454SIan Dowse 		xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4432d3ef3454SIan Dowse 	}
4433d3ef3454SIan Dowse 
4434434bbf6eSJustin T. Gibbs 	/* Release the reference count held while registered. */
4435434bbf6eSJustin T. Gibbs 	xpt_release_bus(bus_path.bus);
4436434bbf6eSJustin T. Gibbs 	xpt_release_path(&bus_path);
4437434bbf6eSJustin T. Gibbs 
4438d3ef3454SIan Dowse 	/* Recheck for more completed CCBs. */
4439d3ef3454SIan Dowse 	while (!TAILQ_EMPTY(&cam_bioq))
4440d3ef3454SIan Dowse 		camisr(&cam_bioq);
4441d3ef3454SIan Dowse 
4442434bbf6eSJustin T. Gibbs 	return (CAM_REQ_CMP);
4443434bbf6eSJustin T. Gibbs }
4444434bbf6eSJustin T. Gibbs 
4445434bbf6eSJustin T. Gibbs static path_id_t
4446434bbf6eSJustin T. Gibbs xptnextfreepathid(void)
4447434bbf6eSJustin T. Gibbs {
4448434bbf6eSJustin T. Gibbs 	struct cam_eb *bus;
4449434bbf6eSJustin T. Gibbs 	path_id_t pathid;
44502398f0cdSPeter Wemm 	const char *strval;
44518b8a9b1dSJustin T. Gibbs 
4452434bbf6eSJustin T. Gibbs 	pathid = 0;
4453434bbf6eSJustin T. Gibbs 	bus = TAILQ_FIRST(&xpt_busses);
4454434bbf6eSJustin T. Gibbs retry:
4455434bbf6eSJustin T. Gibbs 	/* Find an unoccupied pathid */
4456434bbf6eSJustin T. Gibbs 	while (bus != NULL
4457434bbf6eSJustin T. Gibbs 	    && bus->path_id <= pathid) {
4458434bbf6eSJustin T. Gibbs 		if (bus->path_id == pathid)
4459434bbf6eSJustin T. Gibbs 			pathid++;
4460434bbf6eSJustin T. Gibbs 		bus = TAILQ_NEXT(bus, links);
4461434bbf6eSJustin T. Gibbs 	}
4462434bbf6eSJustin T. Gibbs 
4463434bbf6eSJustin T. Gibbs 	/*
4464434bbf6eSJustin T. Gibbs 	 * Ensure that this pathid is not reserved for
4465434bbf6eSJustin T. Gibbs 	 * a bus that may be registered in the future.
4466434bbf6eSJustin T. Gibbs 	 */
446775f51904SPeter Wemm 	if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4468434bbf6eSJustin T. Gibbs 		++pathid;
44698b8a9b1dSJustin T. Gibbs 		/* Start the search over */
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 	int s;
45258b8a9b1dSJustin T. Gibbs 
452668153f43SScott Long 	GIANT_REQUIRED;
452768153f43SScott Long 
45288b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
45298b8a9b1dSJustin T. Gibbs 
4530a5479bc5SJustin T. Gibbs 	/*
4531a5479bc5SJustin T. Gibbs 	 * Most async events come from a CAM interrupt context.  In
4532a5479bc5SJustin T. Gibbs 	 * a few cases, the error recovery code at the peripheral layer,
4533a5479bc5SJustin T. Gibbs 	 * which may run from our SWI or a process context, may signal
4534a5479bc5SJustin T. Gibbs 	 * deferred events with a call to xpt_async. Ensure async
4535a5479bc5SJustin T. Gibbs 	 * notifications are serialized by blocking cam interrupts.
4536a5479bc5SJustin T. Gibbs 	 */
4537a5479bc5SJustin T. Gibbs 	s = splcam();
45388b8a9b1dSJustin T. Gibbs 
45398b8a9b1dSJustin T. Gibbs 	bus = path->bus;
45408b8a9b1dSJustin T. Gibbs 
45418b8a9b1dSJustin T. Gibbs 	if (async_code == AC_BUS_RESET) {
454287cfaf0eSJustin T. Gibbs 		int s;
45438b8a9b1dSJustin T. Gibbs 
454487cfaf0eSJustin T. Gibbs 		s = splclock();
454587cfaf0eSJustin T. Gibbs 		/* Update our notion of when the last reset occurred */
454687cfaf0eSJustin T. Gibbs 		microtime(&bus->last_reset);
454787cfaf0eSJustin T. Gibbs 		splx(s);
45488b8a9b1dSJustin T. Gibbs 	}
45498b8a9b1dSJustin T. Gibbs 
45508b8a9b1dSJustin T. Gibbs 	for (target = TAILQ_FIRST(&bus->et_entries);
45518b8a9b1dSJustin T. Gibbs 	     target != NULL;
45528b8a9b1dSJustin T. Gibbs 	     target = next_target) {
45538b8a9b1dSJustin T. Gibbs 
45548b8a9b1dSJustin T. Gibbs 		next_target = TAILQ_NEXT(target, links);
45558b8a9b1dSJustin T. Gibbs 
45568b8a9b1dSJustin T. Gibbs 		if (path->target != target
45572f22d08dSJustin T. Gibbs 		 && path->target->target_id != CAM_TARGET_WILDCARD
45582f22d08dSJustin T. Gibbs 		 && target->target_id != CAM_TARGET_WILDCARD)
45598b8a9b1dSJustin T. Gibbs 			continue;
45608b8a9b1dSJustin T. Gibbs 
456187cfaf0eSJustin T. Gibbs 		if (async_code == AC_SENT_BDR) {
456287cfaf0eSJustin T. Gibbs 			int s;
456387cfaf0eSJustin T. Gibbs 
456487cfaf0eSJustin T. Gibbs 			/* Update our notion of when the last reset occurred */
456587cfaf0eSJustin T. Gibbs 			s = splclock();
456687cfaf0eSJustin T. Gibbs 			microtime(&path->target->last_reset);
456787cfaf0eSJustin T. Gibbs 			splx(s);
456887cfaf0eSJustin T. Gibbs 		}
456987cfaf0eSJustin T. Gibbs 
45708b8a9b1dSJustin T. Gibbs 		for (device = TAILQ_FIRST(&target->ed_entries);
45718b8a9b1dSJustin T. Gibbs 		     device != NULL;
45728b8a9b1dSJustin T. Gibbs 		     device = next_device) {
45738b8a9b1dSJustin T. Gibbs 
45748b8a9b1dSJustin T. Gibbs 			next_device = TAILQ_NEXT(device, links);
45758b8a9b1dSJustin T. Gibbs 
45768b8a9b1dSJustin T. Gibbs 			if (path->device != device
45772f22d08dSJustin T. Gibbs 			 && path->device->lun_id != CAM_LUN_WILDCARD
45782f22d08dSJustin T. Gibbs 			 && device->lun_id != CAM_LUN_WILDCARD)
45798b8a9b1dSJustin T. Gibbs 				continue;
45808b8a9b1dSJustin T. Gibbs 
45812f22d08dSJustin T. Gibbs 			xpt_dev_async(async_code, bus, target,
45822f22d08dSJustin T. Gibbs 				      device, async_arg);
45838b8a9b1dSJustin T. Gibbs 
45842f22d08dSJustin T. Gibbs 			xpt_async_bcast(&device->asyncs, async_code,
45852f22d08dSJustin T. Gibbs 					path, async_arg);
45868b8a9b1dSJustin T. Gibbs 		}
45878b8a9b1dSJustin T. Gibbs 	}
4588c8bead2aSJustin T. Gibbs 
4589c8bead2aSJustin T. Gibbs 	/*
4590c8bead2aSJustin T. Gibbs 	 * If this wasn't a fully wildcarded async, tell all
4591c8bead2aSJustin T. Gibbs 	 * clients that want all async events.
4592c8bead2aSJustin T. Gibbs 	 */
4593c8bead2aSJustin T. Gibbs 	if (bus != xpt_periph->path->bus)
4594c8bead2aSJustin T. Gibbs 		xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
45958b8a9b1dSJustin T. Gibbs 				path, async_arg);
45968b8a9b1dSJustin T. Gibbs 	splx(s);
45978b8a9b1dSJustin T. Gibbs }
45988b8a9b1dSJustin T. Gibbs 
45998b8a9b1dSJustin T. Gibbs static void
46008b8a9b1dSJustin T. Gibbs xpt_async_bcast(struct async_list *async_head,
46018b8a9b1dSJustin T. Gibbs 		u_int32_t async_code,
46028b8a9b1dSJustin T. Gibbs 		struct cam_path *path, void *async_arg)
46038b8a9b1dSJustin T. Gibbs {
46048b8a9b1dSJustin T. Gibbs 	struct async_node *cur_entry;
46058b8a9b1dSJustin T. Gibbs 
46068b8a9b1dSJustin T. Gibbs 	cur_entry = SLIST_FIRST(async_head);
46078b8a9b1dSJustin T. Gibbs 	while (cur_entry != NULL) {
46088b8a9b1dSJustin T. Gibbs 		struct async_node *next_entry;
46098b8a9b1dSJustin T. Gibbs 		/*
46108b8a9b1dSJustin T. Gibbs 		 * Grab the next list entry before we call the current
46118b8a9b1dSJustin T. Gibbs 		 * entry's callback.  This is because the callback function
46128b8a9b1dSJustin T. Gibbs 		 * can delete its async callback entry.
46138b8a9b1dSJustin T. Gibbs 		 */
46148b8a9b1dSJustin T. Gibbs 		next_entry = SLIST_NEXT(cur_entry, links);
46158b8a9b1dSJustin T. Gibbs 		if ((cur_entry->event_enable & async_code) != 0)
46168b8a9b1dSJustin T. Gibbs 			cur_entry->callback(cur_entry->callback_arg,
46178b8a9b1dSJustin T. Gibbs 					    async_code, path,
46188b8a9b1dSJustin T. Gibbs 					    async_arg);
46198b8a9b1dSJustin T. Gibbs 		cur_entry = next_entry;
46208b8a9b1dSJustin T. Gibbs 	}
46218b8a9b1dSJustin T. Gibbs }
46228b8a9b1dSJustin T. Gibbs 
46232f22d08dSJustin T. Gibbs /*
46242f22d08dSJustin T. Gibbs  * Handle any per-device event notifications that require action by the XPT.
46252f22d08dSJustin T. Gibbs  */
46262f22d08dSJustin T. Gibbs static void
46272f22d08dSJustin T. Gibbs xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
46282f22d08dSJustin T. Gibbs 	      struct cam_ed *device, void *async_arg)
46292f22d08dSJustin T. Gibbs {
46302f22d08dSJustin T. Gibbs 	cam_status status;
46312f22d08dSJustin T. Gibbs 	struct cam_path newpath;
46322f22d08dSJustin T. Gibbs 
46332f22d08dSJustin T. Gibbs 	/*
46342f22d08dSJustin T. Gibbs 	 * We only need to handle events for real devices.
46352f22d08dSJustin T. Gibbs 	 */
46362f22d08dSJustin T. Gibbs 	if (target->target_id == CAM_TARGET_WILDCARD
46372f22d08dSJustin T. Gibbs 	 || device->lun_id == CAM_LUN_WILDCARD)
46382f22d08dSJustin T. Gibbs 		return;
46392f22d08dSJustin T. Gibbs 
46402f22d08dSJustin T. Gibbs 	/*
46412f22d08dSJustin T. Gibbs 	 * We need our own path with wildcards expanded to
46422f22d08dSJustin T. Gibbs 	 * handle certain types of events.
46432f22d08dSJustin T. Gibbs 	 */
46442f22d08dSJustin T. Gibbs 	if ((async_code == AC_SENT_BDR)
46452f22d08dSJustin T. Gibbs 	 || (async_code == AC_BUS_RESET)
46462f22d08dSJustin T. Gibbs 	 || (async_code == AC_INQ_CHANGED))
46472f22d08dSJustin T. Gibbs 		status = xpt_compile_path(&newpath, NULL,
46482f22d08dSJustin T. Gibbs 					  bus->path_id,
46492f22d08dSJustin T. Gibbs 					  target->target_id,
46502f22d08dSJustin T. Gibbs 					  device->lun_id);
46512f22d08dSJustin T. Gibbs 	else
46522f22d08dSJustin T. Gibbs 		status = CAM_REQ_CMP_ERR;
46532f22d08dSJustin T. Gibbs 
46542f22d08dSJustin T. Gibbs 	if (status == CAM_REQ_CMP) {
46552f22d08dSJustin T. Gibbs 
46562f22d08dSJustin T. Gibbs 		/*
46572f22d08dSJustin T. Gibbs 		 * Allow transfer negotiation to occur in a
46582f22d08dSJustin T. Gibbs 		 * tag free environment.
46592f22d08dSJustin T. Gibbs 		 */
46602f22d08dSJustin T. Gibbs 		if (async_code == AC_SENT_BDR
46612f22d08dSJustin T. Gibbs 		 || async_code == AC_BUS_RESET)
46622f22d08dSJustin T. Gibbs 			xpt_toggle_tags(&newpath);
46632f22d08dSJustin T. Gibbs 
46642f22d08dSJustin T. Gibbs 		if (async_code == AC_INQ_CHANGED) {
46652f22d08dSJustin T. Gibbs 			/*
46662f22d08dSJustin T. Gibbs 			 * We've sent a start unit command, or
46672f22d08dSJustin T. Gibbs 			 * something similar to a device that
46682f22d08dSJustin T. Gibbs 			 * may have caused its inquiry data to
46692f22d08dSJustin T. Gibbs 			 * change. So we re-scan the device to
46702f22d08dSJustin T. Gibbs 			 * refresh the inquiry data for it.
46712f22d08dSJustin T. Gibbs 			 */
46722f22d08dSJustin T. Gibbs 			xpt_scan_lun(newpath.periph, &newpath,
46732f22d08dSJustin T. Gibbs 				     CAM_EXPECT_INQ_CHANGE, NULL);
46742f22d08dSJustin T. Gibbs 		}
46752f22d08dSJustin T. Gibbs 		xpt_release_path(&newpath);
46762f22d08dSJustin T. Gibbs 	} else if (async_code == AC_LOST_DEVICE) {
46772f22d08dSJustin T. Gibbs 		device->flags |= CAM_DEV_UNCONFIGURED;
46782f22d08dSJustin T. Gibbs 	} else if (async_code == AC_TRANSFER_NEG) {
46792f22d08dSJustin T. Gibbs 		struct ccb_trans_settings *settings;
46802f22d08dSJustin T. Gibbs 
46812f22d08dSJustin T. Gibbs 		settings = (struct ccb_trans_settings *)async_arg;
46822f22d08dSJustin T. Gibbs 		xpt_set_transfer_settings(settings, device,
46832f22d08dSJustin T. Gibbs 					  /*async_update*/TRUE);
46842f22d08dSJustin T. Gibbs 	}
46852f22d08dSJustin T. Gibbs }
46862f22d08dSJustin T. Gibbs 
46878b8a9b1dSJustin T. Gibbs u_int32_t
46888b8a9b1dSJustin T. Gibbs xpt_freeze_devq(struct cam_path *path, u_int count)
46898b8a9b1dSJustin T. Gibbs {
46908b8a9b1dSJustin T. Gibbs 	int s;
46918b8a9b1dSJustin T. Gibbs 	struct ccb_hdr *ccbh;
46928b8a9b1dSJustin T. Gibbs 
469368153f43SScott Long 	GIANT_REQUIRED;
469468153f43SScott Long 
46958b8a9b1dSJustin T. Gibbs 	s = splcam();
46968b8a9b1dSJustin T. Gibbs 	path->device->qfrozen_cnt += count;
46978b8a9b1dSJustin T. Gibbs 
46988b8a9b1dSJustin T. Gibbs 	/*
46998b8a9b1dSJustin T. Gibbs 	 * Mark the last CCB in the queue as needing
47008b8a9b1dSJustin T. Gibbs 	 * to be requeued if the driver hasn't
47018b8a9b1dSJustin T. Gibbs 	 * changed it's state yet.  This fixes a race
47028b8a9b1dSJustin T. Gibbs 	 * where a ccb is just about to be queued to
47038b8a9b1dSJustin T. Gibbs 	 * a controller driver when it's interrupt routine
47048b8a9b1dSJustin T. Gibbs 	 * freezes the queue.  To completly close the
47058b8a9b1dSJustin T. Gibbs 	 * hole, controller drives must check to see
47068b8a9b1dSJustin T. Gibbs 	 * if a ccb's status is still CAM_REQ_INPROG
47078b8a9b1dSJustin T. Gibbs 	 * under spl protection just before they queue
47088b8a9b1dSJustin T. Gibbs 	 * the CCB.  See ahc_action/ahc_freeze_devq for
47098b8a9b1dSJustin T. Gibbs 	 * an example.
47108b8a9b1dSJustin T. Gibbs 	 */
4711bb6087e5SJustin T. Gibbs 	ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
47128b8a9b1dSJustin T. Gibbs 	if (ccbh && ccbh->status == CAM_REQ_INPROG)
47138b8a9b1dSJustin T. Gibbs 		ccbh->status = CAM_REQUEUE_REQ;
47148b8a9b1dSJustin T. Gibbs 	splx(s);
47158b8a9b1dSJustin T. Gibbs 	return (path->device->qfrozen_cnt);
47168b8a9b1dSJustin T. Gibbs }
47178b8a9b1dSJustin T. Gibbs 
47188b8a9b1dSJustin T. Gibbs u_int32_t
47198b8a9b1dSJustin T. Gibbs xpt_freeze_simq(struct cam_sim *sim, u_int count)
47208b8a9b1dSJustin T. Gibbs {
472168153f43SScott Long 	GIANT_REQUIRED;
472268153f43SScott Long 
47238b8a9b1dSJustin T. Gibbs 	sim->devq->send_queue.qfrozen_cnt += count;
47248b8a9b1dSJustin T. Gibbs 	if (sim->devq->active_dev != NULL) {
47258b8a9b1dSJustin T. Gibbs 		struct ccb_hdr *ccbh;
47268b8a9b1dSJustin T. Gibbs 
47278b8a9b1dSJustin T. Gibbs 		ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4728bb6087e5SJustin T. Gibbs 				  ccb_hdr_tailq);
47298b8a9b1dSJustin T. Gibbs 		if (ccbh && ccbh->status == CAM_REQ_INPROG)
47308b8a9b1dSJustin T. Gibbs 			ccbh->status = CAM_REQUEUE_REQ;
47318b8a9b1dSJustin T. Gibbs 	}
47328b8a9b1dSJustin T. Gibbs 	return (sim->devq->send_queue.qfrozen_cnt);
47338b8a9b1dSJustin T. Gibbs }
47348b8a9b1dSJustin T. Gibbs 
47358b8a9b1dSJustin T. Gibbs static void
47368b8a9b1dSJustin T. Gibbs xpt_release_devq_timeout(void *arg)
47378b8a9b1dSJustin T. Gibbs {
47388b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
47398b8a9b1dSJustin T. Gibbs 
47408b8a9b1dSJustin T. Gibbs 	device = (struct cam_ed *)arg;
47418b8a9b1dSJustin T. Gibbs 
47422cefde5fSJustin T. Gibbs 	xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
47438b8a9b1dSJustin T. Gibbs }
47448b8a9b1dSJustin T. Gibbs 
47458b8a9b1dSJustin T. Gibbs void
47462cefde5fSJustin T. Gibbs xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
47472cefde5fSJustin T. Gibbs {
474868153f43SScott Long 	GIANT_REQUIRED;
474968153f43SScott Long 
47502cefde5fSJustin T. Gibbs 	xpt_release_devq_device(path->device, count, run_queue);
47512cefde5fSJustin T. Gibbs }
47522cefde5fSJustin T. Gibbs 
47532cefde5fSJustin T. Gibbs static void
47542cefde5fSJustin T. Gibbs xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
47558b8a9b1dSJustin T. Gibbs {
47568b8a9b1dSJustin T. Gibbs 	int	rundevq;
47570642b69aSMatt Jacob 	int	s0, s1;
47588b8a9b1dSJustin T. Gibbs 
47598b8a9b1dSJustin T. Gibbs 	rundevq = 0;
47600642b69aSMatt Jacob 	s0 = splsoftcam();
47610642b69aSMatt Jacob 	s1 = splcam();
47628b8a9b1dSJustin T. Gibbs 	if (dev->qfrozen_cnt > 0) {
47638b8a9b1dSJustin T. Gibbs 
47642cefde5fSJustin T. Gibbs 		count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
47652cefde5fSJustin T. Gibbs 		dev->qfrozen_cnt -= count;
47668b8a9b1dSJustin T. Gibbs 		if (dev->qfrozen_cnt == 0) {
47678b8a9b1dSJustin T. Gibbs 
47688b8a9b1dSJustin T. Gibbs 			/*
47698b8a9b1dSJustin T. Gibbs 			 * No longer need to wait for a successful
47708b8a9b1dSJustin T. Gibbs 			 * command completion.
47718b8a9b1dSJustin T. Gibbs 			 */
47728b8a9b1dSJustin T. Gibbs 			dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
47738b8a9b1dSJustin T. Gibbs 
47748b8a9b1dSJustin T. Gibbs 			/*
47758b8a9b1dSJustin T. Gibbs 			 * Remove any timeouts that might be scheduled
47768b8a9b1dSJustin T. Gibbs 			 * to release this queue.
47778b8a9b1dSJustin T. Gibbs 			 */
47788b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
47798b8a9b1dSJustin T. Gibbs 				untimeout(xpt_release_devq_timeout, dev,
47808b8a9b1dSJustin T. Gibbs 					  dev->c_handle);
47818b8a9b1dSJustin T. Gibbs 				dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
47828b8a9b1dSJustin T. Gibbs 			}
47838b8a9b1dSJustin T. Gibbs 
47848b8a9b1dSJustin T. Gibbs 			/*
47858b8a9b1dSJustin T. Gibbs 			 * Now that we are unfrozen schedule the
47868b8a9b1dSJustin T. Gibbs 			 * device so any pending transactions are
47878b8a9b1dSJustin T. Gibbs 			 * run.
47888b8a9b1dSJustin T. Gibbs 			 */
47898b8a9b1dSJustin T. Gibbs 			if ((dev->ccbq.queue.entries > 0)
47908b8a9b1dSJustin T. Gibbs 			 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
47918b8a9b1dSJustin T. Gibbs 			 && (run_queue != 0)) {
47928b8a9b1dSJustin T. Gibbs 				rundevq = 1;
47938b8a9b1dSJustin T. Gibbs 			}
47948b8a9b1dSJustin T. Gibbs 		}
47958b8a9b1dSJustin T. Gibbs 	}
47960642b69aSMatt Jacob 	splx(s1);
47978b8a9b1dSJustin T. Gibbs 	if (rundevq != 0)
47988b8a9b1dSJustin T. Gibbs 		xpt_run_dev_sendq(dev->target->bus);
47990642b69aSMatt Jacob 	splx(s0);
48008b8a9b1dSJustin T. Gibbs }
48018b8a9b1dSJustin T. Gibbs 
48028b8a9b1dSJustin T. Gibbs void
48038b8a9b1dSJustin T. Gibbs xpt_release_simq(struct cam_sim *sim, int run_queue)
48048b8a9b1dSJustin T. Gibbs {
48058b8a9b1dSJustin T. Gibbs 	int	s;
48068b8a9b1dSJustin T. Gibbs 	struct	camq *sendq;
48078b8a9b1dSJustin T. Gibbs 
480868153f43SScott Long 	GIANT_REQUIRED;
480968153f43SScott Long 
48108b8a9b1dSJustin T. Gibbs 	sendq = &(sim->devq->send_queue);
48118b8a9b1dSJustin T. Gibbs 	s = splcam();
48128b8a9b1dSJustin T. Gibbs 	if (sendq->qfrozen_cnt > 0) {
48138b8a9b1dSJustin T. Gibbs 
48148b8a9b1dSJustin T. Gibbs 		sendq->qfrozen_cnt--;
48158b8a9b1dSJustin T. Gibbs 		if (sendq->qfrozen_cnt == 0) {
4816a5479bc5SJustin T. Gibbs 			struct cam_eb *bus;
48178b8a9b1dSJustin T. Gibbs 
48188b8a9b1dSJustin T. Gibbs 			/*
48198b8a9b1dSJustin T. Gibbs 			 * If there is a timeout scheduled to release this
48208b8a9b1dSJustin T. Gibbs 			 * sim queue, remove it.  The queue frozen count is
48218b8a9b1dSJustin T. Gibbs 			 * already at 0.
48228b8a9b1dSJustin T. Gibbs 			 */
48238b8a9b1dSJustin T. Gibbs 			if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
48248b8a9b1dSJustin T. Gibbs 				untimeout(xpt_release_simq_timeout, sim,
48258b8a9b1dSJustin T. Gibbs 					  sim->c_handle);
48268b8a9b1dSJustin T. Gibbs 				sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
48278b8a9b1dSJustin T. Gibbs 			}
4828a5479bc5SJustin T. Gibbs 			bus = xpt_find_bus(sim->path_id);
48298b8a9b1dSJustin T. Gibbs 			splx(s);
48308b8a9b1dSJustin T. Gibbs 
48318b8a9b1dSJustin T. Gibbs 			if (run_queue) {
48328b8a9b1dSJustin T. Gibbs 				/*
48338b8a9b1dSJustin T. Gibbs 				 * Now that we are unfrozen run the send queue.
48348b8a9b1dSJustin T. Gibbs 				 */
4835a5479bc5SJustin T. Gibbs 				xpt_run_dev_sendq(bus);
48368b8a9b1dSJustin T. Gibbs 			}
4837a5479bc5SJustin T. Gibbs 			xpt_release_bus(bus);
48388b8a9b1dSJustin T. Gibbs 		} else
48398b8a9b1dSJustin T. Gibbs 			splx(s);
48408b8a9b1dSJustin T. Gibbs 	} else
48418b8a9b1dSJustin T. Gibbs 		splx(s);
48428b8a9b1dSJustin T. Gibbs }
48438b8a9b1dSJustin T. Gibbs 
48448b8a9b1dSJustin T. Gibbs static void
48458b8a9b1dSJustin T. Gibbs xpt_release_simq_timeout(void *arg)
48468b8a9b1dSJustin T. Gibbs {
48478b8a9b1dSJustin T. Gibbs 	struct cam_sim *sim;
48488b8a9b1dSJustin T. Gibbs 
48498b8a9b1dSJustin T. Gibbs 	sim = (struct cam_sim *)arg;
48508b8a9b1dSJustin T. Gibbs 	xpt_release_simq(sim, /* run_queue */ TRUE);
48518b8a9b1dSJustin T. Gibbs }
48528b8a9b1dSJustin T. Gibbs 
48538b8a9b1dSJustin T. Gibbs void
4854a5479bc5SJustin T. Gibbs xpt_done(union ccb *done_ccb)
48558b8a9b1dSJustin T. Gibbs {
48568b8a9b1dSJustin T. Gibbs 	int s;
48578b8a9b1dSJustin T. Gibbs 
48588b8a9b1dSJustin T. Gibbs 	s = splcam();
48598b8a9b1dSJustin T. Gibbs 
48608b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
48619deea857SKenneth D. Merry 	if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
48628b8a9b1dSJustin T. Gibbs 		/*
48638b8a9b1dSJustin T. Gibbs 		 * Queue up the request for handling by our SWI handler
48648b8a9b1dSJustin T. Gibbs 		 * any of the "non-immediate" type of ccbs.
48658b8a9b1dSJustin T. Gibbs 		 */
48668b8a9b1dSJustin T. Gibbs 		switch (done_ccb->ccb_h.path->periph->type) {
48678b8a9b1dSJustin T. Gibbs 		case CAM_PERIPH_BIO:
4868ef3cf714SScott Long 			mtx_lock(&cam_bioq_lock);
48698b8a9b1dSJustin T. Gibbs 			TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
48708b8a9b1dSJustin T. Gibbs 					  sim_links.tqe);
48718b8a9b1dSJustin T. Gibbs 			done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4872ef3cf714SScott Long 			mtx_unlock(&cam_bioq_lock);
4873c86b6ff5SJohn Baldwin 			swi_sched(cambio_ih, 0);
48748b8a9b1dSJustin T. Gibbs 			break;
48752e8f0ae6SScott Long 		default:
48762e8f0ae6SScott Long 			panic("unknown periph type %d",
48772e8f0ae6SScott Long 			    done_ccb->ccb_h.path->periph->type);
48788b8a9b1dSJustin T. Gibbs 		}
48798b8a9b1dSJustin T. Gibbs 	}
48808b8a9b1dSJustin T. Gibbs 	splx(s);
48818b8a9b1dSJustin T. Gibbs }
48828b8a9b1dSJustin T. Gibbs 
48838b8a9b1dSJustin T. Gibbs union ccb *
48848b8a9b1dSJustin T. Gibbs xpt_alloc_ccb()
48858b8a9b1dSJustin T. Gibbs {
48868b8a9b1dSJustin T. Gibbs 	union ccb *new_ccb;
48878b8a9b1dSJustin T. Gibbs 
488868153f43SScott Long 	GIANT_REQUIRED;
488968153f43SScott Long 
4890362abc44STai-hwa Liang 	new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_WAITOK);
4891362abc44STai-hwa Liang 	return (new_ccb);
4892362abc44STai-hwa Liang }
4893362abc44STai-hwa Liang 
4894362abc44STai-hwa Liang union ccb *
4895362abc44STai-hwa Liang xpt_alloc_ccb_nowait()
4896362abc44STai-hwa Liang {
4897362abc44STai-hwa Liang 	union ccb *new_ccb;
4898362abc44STai-hwa Liang 
4899362abc44STai-hwa Liang 	GIANT_REQUIRED;
4900362abc44STai-hwa Liang 
4901362abc44STai-hwa Liang 	new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_NOWAIT);
49028b8a9b1dSJustin T. Gibbs 	return (new_ccb);
49038b8a9b1dSJustin T. Gibbs }
49048b8a9b1dSJustin T. Gibbs 
49058b8a9b1dSJustin T. Gibbs void
49068b8a9b1dSJustin T. Gibbs xpt_free_ccb(union ccb *free_ccb)
49078b8a9b1dSJustin T. Gibbs {
4908362abc44STai-hwa Liang 	free(free_ccb, M_CAMXPT);
49098b8a9b1dSJustin T. Gibbs }
49108b8a9b1dSJustin T. Gibbs 
49118b8a9b1dSJustin T. Gibbs 
49128b8a9b1dSJustin T. Gibbs 
49138b8a9b1dSJustin T. Gibbs /* Private XPT functions */
49148b8a9b1dSJustin T. Gibbs 
49158b8a9b1dSJustin T. Gibbs /*
49168b8a9b1dSJustin T. Gibbs  * Get a CAM control block for the caller. Charge the structure to the device
49178b8a9b1dSJustin T. Gibbs  * referenced by the path.  If the this device has no 'credits' then the
49188b8a9b1dSJustin T. Gibbs  * device already has the maximum number of outstanding operations under way
49198b8a9b1dSJustin T. Gibbs  * and we return NULL. If we don't have sufficient resources to allocate more
49208b8a9b1dSJustin T. Gibbs  * ccbs, we also return NULL.
49218b8a9b1dSJustin T. Gibbs  */
49228b8a9b1dSJustin T. Gibbs static union ccb *
49238b8a9b1dSJustin T. Gibbs xpt_get_ccb(struct cam_ed *device)
49248b8a9b1dSJustin T. Gibbs {
49258b8a9b1dSJustin T. Gibbs 	union ccb *new_ccb;
49268b8a9b1dSJustin T. Gibbs 	int s;
49278b8a9b1dSJustin T. Gibbs 
49288b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
4929fc2ffbe6SPoul-Henning Kamp 	if ((new_ccb = (union ccb *)SLIST_FIRST(&ccb_freeq)) == NULL) {
4930362abc44STai-hwa Liang 		new_ccb = xpt_alloc_ccb_nowait();
49318b8a9b1dSJustin T. Gibbs                 if (new_ccb == NULL) {
49328b8a9b1dSJustin T. Gibbs 			splx(s);
49338b8a9b1dSJustin T. Gibbs 			return (NULL);
49348b8a9b1dSJustin T. Gibbs 		}
49358b8a9b1dSJustin T. Gibbs 		callout_handle_init(&new_ccb->ccb_h.timeout_ch);
49368b8a9b1dSJustin T. Gibbs 		SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
49378b8a9b1dSJustin T. Gibbs 				  xpt_links.sle);
49388b8a9b1dSJustin T. Gibbs 		xpt_ccb_count++;
49398b8a9b1dSJustin T. Gibbs 	}
49408b8a9b1dSJustin T. Gibbs 	cam_ccbq_take_opening(&device->ccbq);
49418b8a9b1dSJustin T. Gibbs 	SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
49428b8a9b1dSJustin T. Gibbs 	splx(s);
49438b8a9b1dSJustin T. Gibbs 	return (new_ccb);
49448b8a9b1dSJustin T. Gibbs }
49458b8a9b1dSJustin T. Gibbs 
4946a5479bc5SJustin T. Gibbs static void
4947a5479bc5SJustin T. Gibbs xpt_release_bus(struct cam_eb *bus)
4948a5479bc5SJustin T. Gibbs {
4949a5479bc5SJustin T. Gibbs 	int s;
4950a5479bc5SJustin T. Gibbs 
4951a5479bc5SJustin T. Gibbs 	s = splcam();
4952a5479bc5SJustin T. Gibbs 	if ((--bus->refcount == 0)
4953a5479bc5SJustin T. Gibbs 	 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4954a5479bc5SJustin T. Gibbs 		TAILQ_REMOVE(&xpt_busses, bus, links);
4955a5479bc5SJustin T. Gibbs 		bus_generation++;
4956a5479bc5SJustin T. Gibbs 		splx(s);
4957362abc44STai-hwa Liang 		free(bus, M_CAMXPT);
4958a5479bc5SJustin T. Gibbs 	} else
4959a5479bc5SJustin T. Gibbs 		splx(s);
4960a5479bc5SJustin T. Gibbs }
49618b8a9b1dSJustin T. Gibbs 
49628b8a9b1dSJustin T. Gibbs static struct cam_et *
49638b8a9b1dSJustin T. Gibbs xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
49648b8a9b1dSJustin T. Gibbs {
49658b8a9b1dSJustin T. Gibbs 	struct cam_et *target;
49668b8a9b1dSJustin T. Gibbs 
4967362abc44STai-hwa Liang 	target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, M_NOWAIT);
49688b8a9b1dSJustin T. Gibbs 	if (target != NULL) {
49698b8a9b1dSJustin T. Gibbs 		struct cam_et *cur_target;
49708b8a9b1dSJustin T. Gibbs 
4971434bbf6eSJustin T. Gibbs 		TAILQ_INIT(&target->ed_entries);
49728b8a9b1dSJustin T. Gibbs 		target->bus = bus;
49738b8a9b1dSJustin T. Gibbs 		target->target_id = target_id;
49748b8a9b1dSJustin T. Gibbs 		target->refcount = 1;
4975434bbf6eSJustin T. Gibbs 		target->generation = 0;
4976434bbf6eSJustin T. Gibbs 		timevalclear(&target->last_reset);
4977a5479bc5SJustin T. Gibbs 		/*
4978a5479bc5SJustin T. Gibbs 		 * Hold a reference to our parent bus so it
4979a5479bc5SJustin T. Gibbs 		 * will not go away before we do.
4980a5479bc5SJustin T. Gibbs 		 */
4981a5479bc5SJustin T. Gibbs 		bus->refcount++;
49828b8a9b1dSJustin T. Gibbs 
49838b8a9b1dSJustin T. Gibbs 		/* Insertion sort into our bus's target list */
49848b8a9b1dSJustin T. Gibbs 		cur_target = TAILQ_FIRST(&bus->et_entries);
49858b8a9b1dSJustin T. Gibbs 		while (cur_target != NULL && cur_target->target_id < target_id)
49868b8a9b1dSJustin T. Gibbs 			cur_target = TAILQ_NEXT(cur_target, links);
49878b8a9b1dSJustin T. Gibbs 
49888b8a9b1dSJustin T. Gibbs 		if (cur_target != NULL) {
49898b8a9b1dSJustin T. Gibbs 			TAILQ_INSERT_BEFORE(cur_target, target, links);
49908b8a9b1dSJustin T. Gibbs 		} else {
49918b8a9b1dSJustin T. Gibbs 			TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
49928b8a9b1dSJustin T. Gibbs 		}
4993a5479bc5SJustin T. Gibbs 		bus->generation++;
49948b8a9b1dSJustin T. Gibbs 	}
49958b8a9b1dSJustin T. Gibbs 	return (target);
49968b8a9b1dSJustin T. Gibbs }
49978b8a9b1dSJustin T. Gibbs 
4998a5479bc5SJustin T. Gibbs static void
49998b8a9b1dSJustin T. Gibbs xpt_release_target(struct cam_eb *bus, struct cam_et *target)
50008b8a9b1dSJustin T. Gibbs {
5001a5479bc5SJustin T. Gibbs 	int s;
5002a5479bc5SJustin T. Gibbs 
5003a5479bc5SJustin T. Gibbs 	s = splcam();
50048b8a9b1dSJustin T. Gibbs 	if ((--target->refcount == 0)
50058b8a9b1dSJustin T. Gibbs 	 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
50068b8a9b1dSJustin T. Gibbs 		TAILQ_REMOVE(&bus->et_entries, target, links);
50078b8a9b1dSJustin T. Gibbs 		bus->generation++;
5008a5479bc5SJustin T. Gibbs 		splx(s);
5009362abc44STai-hwa Liang 		free(target, M_CAMXPT);
5010a5479bc5SJustin T. Gibbs 		xpt_release_bus(bus);
5011a5479bc5SJustin T. Gibbs 	} else
5012a5479bc5SJustin T. Gibbs 		splx(s);
50138b8a9b1dSJustin T. Gibbs }
50148b8a9b1dSJustin T. Gibbs 
50158b8a9b1dSJustin T. Gibbs static struct cam_ed *
50168b8a9b1dSJustin T. Gibbs xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
50178b8a9b1dSJustin T. Gibbs {
50183393f8daSKenneth D. Merry 	struct	   cam_path path;
50198b8a9b1dSJustin T. Gibbs 	struct	   cam_ed *device;
50208b8a9b1dSJustin T. Gibbs 	struct	   cam_devq *devq;
5021a5479bc5SJustin T. Gibbs 	cam_status status;
50228b8a9b1dSJustin T. Gibbs 
5023d3ef3454SIan Dowse 	if (SIM_DEAD(bus->sim))
5024d3ef3454SIan Dowse 		return (NULL);
5025d3ef3454SIan Dowse 
50268b8a9b1dSJustin T. Gibbs 	/* Make space for us in the device queue on our bus */
50278b8a9b1dSJustin T. Gibbs 	devq = bus->sim->devq;
50288b8a9b1dSJustin T. Gibbs 	status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
50298b8a9b1dSJustin T. Gibbs 
50308b8a9b1dSJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
50318b8a9b1dSJustin T. Gibbs 		device = NULL;
50328b8a9b1dSJustin T. Gibbs 	} else {
50338b8a9b1dSJustin T. Gibbs 		device = (struct cam_ed *)malloc(sizeof(*device),
5034362abc44STai-hwa Liang 						 M_CAMXPT, M_NOWAIT);
50358b8a9b1dSJustin T. Gibbs 	}
50368b8a9b1dSJustin T. Gibbs 
50378b8a9b1dSJustin T. Gibbs 	if (device != NULL) {
50388b8a9b1dSJustin T. Gibbs 		struct cam_ed *cur_device;
50398b8a9b1dSJustin T. Gibbs 
50408b8a9b1dSJustin T. Gibbs 		cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
50418b8a9b1dSJustin T. Gibbs 		device->alloc_ccb_entry.device = device;
50428b8a9b1dSJustin T. Gibbs 		cam_init_pinfo(&device->send_ccb_entry.pinfo);
50438b8a9b1dSJustin T. Gibbs 		device->send_ccb_entry.device = device;
50448b8a9b1dSJustin T. Gibbs 		device->target = target;
50458b8a9b1dSJustin T. Gibbs 		device->lun_id = lun_id;
50468b8a9b1dSJustin T. Gibbs 		/* Initialize our queues */
50478b8a9b1dSJustin T. Gibbs 		if (camq_init(&device->drvq, 0) != 0) {
5048362abc44STai-hwa Liang 			free(device, M_CAMXPT);
50498b8a9b1dSJustin T. Gibbs 			return (NULL);
50508b8a9b1dSJustin T. Gibbs 		}
50518b8a9b1dSJustin T. Gibbs 		if (cam_ccbq_init(&device->ccbq,
50528b8a9b1dSJustin T. Gibbs 				  bus->sim->max_dev_openings) != 0) {
50538b8a9b1dSJustin T. Gibbs 			camq_fini(&device->drvq);
5054362abc44STai-hwa Liang 			free(device, M_CAMXPT);
50558b8a9b1dSJustin T. Gibbs 			return (NULL);
50568b8a9b1dSJustin T. Gibbs 		}
5057434bbf6eSJustin T. Gibbs 		SLIST_INIT(&device->asyncs);
5058434bbf6eSJustin T. Gibbs 		SLIST_INIT(&device->periphs);
5059434bbf6eSJustin T. Gibbs 		device->generation = 0;
5060434bbf6eSJustin T. Gibbs 		device->owner = NULL;
5061434bbf6eSJustin T. Gibbs 		/*
5062434bbf6eSJustin T. Gibbs 		 * Take the default quirk entry until we have inquiry
5063434bbf6eSJustin T. Gibbs 		 * data and can determine a better quirk to use.
5064434bbf6eSJustin T. Gibbs 		 */
5065434bbf6eSJustin T. Gibbs 		device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
5066434bbf6eSJustin T. Gibbs 		bzero(&device->inq_data, sizeof(device->inq_data));
5067434bbf6eSJustin T. Gibbs 		device->inq_flags = 0;
5068434bbf6eSJustin T. Gibbs 		device->queue_flags = 0;
5069434bbf6eSJustin T. Gibbs 		device->serial_num = NULL;
5070434bbf6eSJustin T. Gibbs 		device->serial_num_len = 0;
5071434bbf6eSJustin T. Gibbs 		device->qfrozen_cnt = 0;
5072434bbf6eSJustin T. Gibbs 		device->flags = CAM_DEV_UNCONFIGURED;
5073434bbf6eSJustin T. Gibbs 		device->tag_delay_count = 0;
5074df8f9080SJustin T. Gibbs 		device->tag_saved_openings = 0;
5075434bbf6eSJustin T. Gibbs 		device->refcount = 1;
5076434bbf6eSJustin T. Gibbs 		callout_handle_init(&device->c_handle);
5077434bbf6eSJustin T. Gibbs 
5078434bbf6eSJustin T. Gibbs 		/*
5079434bbf6eSJustin T. Gibbs 		 * Hold a reference to our parent target so it
5080434bbf6eSJustin T. Gibbs 		 * will not go away before we do.
5081434bbf6eSJustin T. Gibbs 		 */
5082434bbf6eSJustin T. Gibbs 		target->refcount++;
5083434bbf6eSJustin T. Gibbs 
50848b8a9b1dSJustin T. Gibbs 		/*
50858b8a9b1dSJustin T. Gibbs 		 * XXX should be limited by number of CCBs this bus can
50868b8a9b1dSJustin T. Gibbs 		 * do.
50878b8a9b1dSJustin T. Gibbs 		 */
50888b8a9b1dSJustin T. Gibbs 		xpt_max_ccbs += device->ccbq.devq_openings;
50898b8a9b1dSJustin T. Gibbs 		/* Insertion sort into our target's device list */
50908b8a9b1dSJustin T. Gibbs 		cur_device = TAILQ_FIRST(&target->ed_entries);
50918b8a9b1dSJustin T. Gibbs 		while (cur_device != NULL && cur_device->lun_id < lun_id)
50928b8a9b1dSJustin T. Gibbs 			cur_device = TAILQ_NEXT(cur_device, links);
50938b8a9b1dSJustin T. Gibbs 		if (cur_device != NULL) {
50948b8a9b1dSJustin T. Gibbs 			TAILQ_INSERT_BEFORE(cur_device, device, links);
50958b8a9b1dSJustin T. Gibbs 		} else {
50968b8a9b1dSJustin T. Gibbs 			TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
50978b8a9b1dSJustin T. Gibbs 		}
5098a5479bc5SJustin T. Gibbs 		target->generation++;
50993393f8daSKenneth D. Merry 		if (lun_id != CAM_LUN_WILDCARD) {
51003393f8daSKenneth D. Merry 			xpt_compile_path(&path,
51013393f8daSKenneth D. Merry 					 NULL,
51023393f8daSKenneth D. Merry 					 bus->path_id,
51033393f8daSKenneth D. Merry 					 target->target_id,
51043393f8daSKenneth D. Merry 					 lun_id);
51053393f8daSKenneth D. Merry 			xpt_devise_transport(&path);
51063393f8daSKenneth D. Merry 			xpt_release_path(&path);
51073393f8daSKenneth D. Merry 		}
51088b8a9b1dSJustin T. Gibbs 	}
51098b8a9b1dSJustin T. Gibbs 	return (device);
51108b8a9b1dSJustin T. Gibbs }
51118b8a9b1dSJustin T. Gibbs 
51128b8a9b1dSJustin T. Gibbs static void
51138b8a9b1dSJustin T. Gibbs xpt_release_device(struct cam_eb *bus, struct cam_et *target,
51148b8a9b1dSJustin T. Gibbs 		   struct cam_ed *device)
51158b8a9b1dSJustin T. Gibbs {
51168b8a9b1dSJustin T. Gibbs 	int s;
51178b8a9b1dSJustin T. Gibbs 
5118a5479bc5SJustin T. Gibbs 	s = splcam();
51198b8a9b1dSJustin T. Gibbs 	if ((--device->refcount == 0)
51208b8a9b1dSJustin T. Gibbs 	 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
51218b8a9b1dSJustin T. Gibbs 		struct cam_devq *devq;
51228b8a9b1dSJustin T. Gibbs 
5123a5479bc5SJustin T. Gibbs 		if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
5124a5479bc5SJustin T. Gibbs 		 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
5125a5479bc5SJustin T. Gibbs 			panic("Removing device while still queued for ccbs");
51262cefde5fSJustin T. Gibbs 
51272cefde5fSJustin T. Gibbs 		if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
51282cefde5fSJustin T. Gibbs 				untimeout(xpt_release_devq_timeout, device,
51292cefde5fSJustin T. Gibbs 					  device->c_handle);
51302cefde5fSJustin T. Gibbs 
51318b8a9b1dSJustin T. Gibbs 		TAILQ_REMOVE(&target->ed_entries, device,links);
51328b8a9b1dSJustin T. Gibbs 		target->generation++;
51338b8a9b1dSJustin T. Gibbs 		xpt_max_ccbs -= device->ccbq.devq_openings;
5134d3ef3454SIan Dowse 		if (!SIM_DEAD(bus->sim)) {
51358b8a9b1dSJustin T. Gibbs 			/* Release our slot in the devq */
51368b8a9b1dSJustin T. Gibbs 			devq = bus->sim->devq;
51378b8a9b1dSJustin T. Gibbs 			cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
5138d3ef3454SIan Dowse 		}
51398b8a9b1dSJustin T. Gibbs 		splx(s);
51407c20d5d7STai-hwa Liang 		camq_fini(&device->drvq);
51417c20d5d7STai-hwa Liang 		camq_fini(&device->ccbq.queue);
5142362abc44STai-hwa Liang 		free(device, M_CAMXPT);
5143434bbf6eSJustin T. Gibbs 		xpt_release_target(bus, target);
5144a5479bc5SJustin T. Gibbs 	} else
5145a5479bc5SJustin T. Gibbs 		splx(s);
51468b8a9b1dSJustin T. Gibbs }
51478b8a9b1dSJustin T. Gibbs 
51488b8a9b1dSJustin T. Gibbs static u_int32_t
51498b8a9b1dSJustin T. Gibbs xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
51508b8a9b1dSJustin T. Gibbs {
51518b8a9b1dSJustin T. Gibbs 	int	s;
51528b8a9b1dSJustin T. Gibbs 	int	diff;
51538b8a9b1dSJustin T. Gibbs 	int	result;
51548b8a9b1dSJustin T. Gibbs 	struct	cam_ed *dev;
51558b8a9b1dSJustin T. Gibbs 
51568b8a9b1dSJustin T. Gibbs 	dev = path->device;
51578b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
51588b8a9b1dSJustin T. Gibbs 
51598b8a9b1dSJustin T. Gibbs 	diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
51608b8a9b1dSJustin T. Gibbs 	result = cam_ccbq_resize(&dev->ccbq, newopenings);
51618b8a9b1dSJustin T. Gibbs 	if (result == CAM_REQ_CMP && (diff < 0)) {
51628b8a9b1dSJustin T. Gibbs 		dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
51638b8a9b1dSJustin T. Gibbs 	}
5164df8f9080SJustin T. Gibbs 	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5165df8f9080SJustin T. Gibbs 	 || (dev->inq_flags & SID_CmdQue) != 0)
5166df8f9080SJustin T. Gibbs 		dev->tag_saved_openings = newopenings;
51678b8a9b1dSJustin T. Gibbs 	/* Adjust the global limit */
51688b8a9b1dSJustin T. Gibbs 	xpt_max_ccbs += diff;
51698b8a9b1dSJustin T. Gibbs 	splx(s);
51708b8a9b1dSJustin T. Gibbs 	return (result);
51718b8a9b1dSJustin T. Gibbs }
51728b8a9b1dSJustin T. Gibbs 
51738b8a9b1dSJustin T. Gibbs static struct cam_eb *
51748b8a9b1dSJustin T. Gibbs xpt_find_bus(path_id_t path_id)
51758b8a9b1dSJustin T. Gibbs {
51768b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus;
51778b8a9b1dSJustin T. Gibbs 
51788b8a9b1dSJustin T. Gibbs 	for (bus = TAILQ_FIRST(&xpt_busses);
51798b8a9b1dSJustin T. Gibbs 	     bus != NULL;
51808b8a9b1dSJustin T. Gibbs 	     bus = TAILQ_NEXT(bus, links)) {
5181a5479bc5SJustin T. Gibbs 		if (bus->path_id == path_id) {
5182a5479bc5SJustin T. Gibbs 			bus->refcount++;
51838b8a9b1dSJustin T. Gibbs 			break;
51848b8a9b1dSJustin T. Gibbs 		}
5185a5479bc5SJustin T. Gibbs 	}
51868b8a9b1dSJustin T. Gibbs 	return (bus);
51878b8a9b1dSJustin T. Gibbs }
51888b8a9b1dSJustin T. Gibbs 
51898b8a9b1dSJustin T. Gibbs static struct cam_et *
51908b8a9b1dSJustin T. Gibbs xpt_find_target(struct cam_eb *bus, target_id_t	target_id)
51918b8a9b1dSJustin T. Gibbs {
51928b8a9b1dSJustin T. Gibbs 	struct cam_et *target;
51938b8a9b1dSJustin T. Gibbs 
51948b8a9b1dSJustin T. Gibbs 	for (target = TAILQ_FIRST(&bus->et_entries);
51958b8a9b1dSJustin T. Gibbs 	     target != NULL;
51968b8a9b1dSJustin T. Gibbs 	     target = TAILQ_NEXT(target, links)) {
51978b8a9b1dSJustin T. Gibbs 		if (target->target_id == target_id) {
51988b8a9b1dSJustin T. Gibbs 			target->refcount++;
51998b8a9b1dSJustin T. Gibbs 			break;
52008b8a9b1dSJustin T. Gibbs 		}
52018b8a9b1dSJustin T. Gibbs 	}
52028b8a9b1dSJustin T. Gibbs 	return (target);
52038b8a9b1dSJustin T. Gibbs }
52048b8a9b1dSJustin T. Gibbs 
52058b8a9b1dSJustin T. Gibbs static struct cam_ed *
52068b8a9b1dSJustin T. Gibbs xpt_find_device(struct cam_et *target, lun_id_t lun_id)
52078b8a9b1dSJustin T. Gibbs {
52088b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
52098b8a9b1dSJustin T. Gibbs 
52108b8a9b1dSJustin T. Gibbs 	for (device = TAILQ_FIRST(&target->ed_entries);
52118b8a9b1dSJustin T. Gibbs 	     device != NULL;
52128b8a9b1dSJustin T. Gibbs 	     device = TAILQ_NEXT(device, links)) {
52138b8a9b1dSJustin T. Gibbs 		if (device->lun_id == lun_id) {
52148b8a9b1dSJustin T. Gibbs 			device->refcount++;
52158b8a9b1dSJustin T. Gibbs 			break;
52168b8a9b1dSJustin T. Gibbs 		}
52178b8a9b1dSJustin T. Gibbs 	}
52188b8a9b1dSJustin T. Gibbs 	return (device);
52198b8a9b1dSJustin T. Gibbs }
52208b8a9b1dSJustin T. Gibbs 
52218b8a9b1dSJustin T. Gibbs typedef struct {
52228b8a9b1dSJustin T. Gibbs 	union	ccb *request_ccb;
52238b8a9b1dSJustin T. Gibbs 	struct 	ccb_pathinq *cpi;
5224c1c3139eSMatt Jacob 	int	counter;
52258b8a9b1dSJustin T. Gibbs } xpt_scan_bus_info;
52268b8a9b1dSJustin T. Gibbs 
52278b8a9b1dSJustin T. Gibbs /*
52288b8a9b1dSJustin T. Gibbs  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
52298b8a9b1dSJustin T. Gibbs  * As the scan progresses, xpt_scan_bus is used as the
52308b8a9b1dSJustin T. Gibbs  * callback on completion function.
52318b8a9b1dSJustin T. Gibbs  */
52328b8a9b1dSJustin T. Gibbs static void
52338b8a9b1dSJustin T. Gibbs xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
52348b8a9b1dSJustin T. Gibbs {
52358b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
52368b8a9b1dSJustin T. Gibbs 		  ("xpt_scan_bus\n"));
52378b8a9b1dSJustin T. Gibbs 	switch (request_ccb->ccb_h.func_code) {
52388b8a9b1dSJustin T. Gibbs 	case XPT_SCAN_BUS:
52398b8a9b1dSJustin T. Gibbs 	{
52408b8a9b1dSJustin T. Gibbs 		xpt_scan_bus_info *scan_info;
52418b8a9b1dSJustin T. Gibbs 		union	ccb *work_ccb;
52428b8a9b1dSJustin T. Gibbs 		struct	cam_path *path;
52438b8a9b1dSJustin T. Gibbs 		u_int	i;
52448b8a9b1dSJustin T. Gibbs 		u_int	max_target;
52458b8a9b1dSJustin T. Gibbs 		u_int	initiator_id;
52468b8a9b1dSJustin T. Gibbs 
52478b8a9b1dSJustin T. Gibbs 		/* Find out the characteristics of the bus */
52488b8a9b1dSJustin T. Gibbs 		work_ccb = xpt_alloc_ccb();
52498b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
52508b8a9b1dSJustin T. Gibbs 			      request_ccb->ccb_h.pinfo.priority);
52518b8a9b1dSJustin T. Gibbs 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
52528b8a9b1dSJustin T. Gibbs 		xpt_action(work_ccb);
52538b8a9b1dSJustin T. Gibbs 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
52548b8a9b1dSJustin T. Gibbs 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
52558b8a9b1dSJustin T. Gibbs 			xpt_free_ccb(work_ccb);
52568b8a9b1dSJustin T. Gibbs 			xpt_done(request_ccb);
52578b8a9b1dSJustin T. Gibbs 			return;
52588b8a9b1dSJustin T. Gibbs 		}
52598b8a9b1dSJustin T. Gibbs 
526098192658SJustin T. Gibbs 		if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
526198192658SJustin T. Gibbs 			/*
526298192658SJustin T. Gibbs 			 * Can't scan the bus on an adapter that
526398192658SJustin T. Gibbs 			 * cannot perform the initiator role.
526498192658SJustin T. Gibbs 			 */
526598192658SJustin T. Gibbs 			request_ccb->ccb_h.status = CAM_REQ_CMP;
526698192658SJustin T. Gibbs 			xpt_free_ccb(work_ccb);
526798192658SJustin T. Gibbs 			xpt_done(request_ccb);
526898192658SJustin T. Gibbs 			return;
526998192658SJustin T. Gibbs 		}
527098192658SJustin T. Gibbs 
52718b8a9b1dSJustin T. Gibbs 		/* Save some state for use while we probe for devices */
52728b8a9b1dSJustin T. Gibbs 		scan_info = (xpt_scan_bus_info *)
5273a163d034SWarner Losh 		    malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK);
52748b8a9b1dSJustin T. Gibbs 		scan_info->request_ccb = request_ccb;
52758b8a9b1dSJustin T. Gibbs 		scan_info->cpi = &work_ccb->cpi;
52768b8a9b1dSJustin T. Gibbs 
52778b8a9b1dSJustin T. Gibbs 		/* Cache on our stack so we can work asynchronously */
52788b8a9b1dSJustin T. Gibbs 		max_target = scan_info->cpi->max_target;
52798b8a9b1dSJustin T. Gibbs 		initiator_id = scan_info->cpi->initiator_id;
52808b8a9b1dSJustin T. Gibbs 
5281c1c3139eSMatt Jacob 
52828b8a9b1dSJustin T. Gibbs 		/*
5283c1c3139eSMatt Jacob 		 * We can scan all targets in parallel, or do it sequentially.
52848b8a9b1dSJustin T. Gibbs 		 */
5285c1c3139eSMatt Jacob 		if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
5286c1c3139eSMatt Jacob 			max_target = 0;
5287c1c3139eSMatt Jacob 			scan_info->counter = 0;
5288c1c3139eSMatt Jacob 		} else {
5289c1c3139eSMatt Jacob 			scan_info->counter = scan_info->cpi->max_target + 1;
5290c1c3139eSMatt Jacob 			if (scan_info->cpi->initiator_id < scan_info->counter) {
5291c1c3139eSMatt Jacob 				scan_info->counter--;
5292c1c3139eSMatt Jacob 			}
5293c1c3139eSMatt Jacob 		}
52948b8a9b1dSJustin T. Gibbs 
52958b8a9b1dSJustin T. Gibbs 		for (i = 0; i <= max_target; i++) {
52968b8a9b1dSJustin T. Gibbs 			cam_status status;
52978b8a9b1dSJustin T. Gibbs 			if (i == initiator_id)
52988b8a9b1dSJustin T. Gibbs 				continue;
52998b8a9b1dSJustin T. Gibbs 
53008b8a9b1dSJustin T. Gibbs 			status = xpt_create_path(&path, xpt_periph,
53018b8a9b1dSJustin T. Gibbs 						 request_ccb->ccb_h.path_id,
53028b8a9b1dSJustin T. Gibbs 						 i, 0);
53038b8a9b1dSJustin T. Gibbs 			if (status != CAM_REQ_CMP) {
53048b8a9b1dSJustin T. Gibbs 				printf("xpt_scan_bus: xpt_create_path failed"
53058b8a9b1dSJustin T. Gibbs 				       " with status %#x, bus scan halted\n",
53068b8a9b1dSJustin T. Gibbs 				       status);
5307c1c3139eSMatt Jacob 				free(scan_info, M_TEMP);
5308c1c3139eSMatt Jacob 				request_ccb->ccb_h.status = status;
5309c1c3139eSMatt Jacob 				xpt_free_ccb(work_ccb);
5310c1c3139eSMatt Jacob 				xpt_done(request_ccb);
53118b8a9b1dSJustin T. Gibbs 				break;
53128b8a9b1dSJustin T. Gibbs 			}
53138b8a9b1dSJustin T. Gibbs 			work_ccb = xpt_alloc_ccb();
53148b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&work_ccb->ccb_h, path,
53158b8a9b1dSJustin T. Gibbs 				      request_ccb->ccb_h.pinfo.priority);
53168b8a9b1dSJustin T. Gibbs 			work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
53178b8a9b1dSJustin T. Gibbs 			work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
53188b8a9b1dSJustin T. Gibbs 			work_ccb->ccb_h.ppriv_ptr0 = scan_info;
53198b8a9b1dSJustin T. Gibbs 			work_ccb->crcn.flags = request_ccb->crcn.flags;
53208b8a9b1dSJustin T. Gibbs 			xpt_action(work_ccb);
53218b8a9b1dSJustin T. Gibbs 		}
53228b8a9b1dSJustin T. Gibbs 		break;
53238b8a9b1dSJustin T. Gibbs 	}
53248b8a9b1dSJustin T. Gibbs 	case XPT_SCAN_LUN:
53258b8a9b1dSJustin T. Gibbs 	{
5326c1c3139eSMatt Jacob 		cam_status status;
5327c1c3139eSMatt Jacob 		struct cam_path *path;
53288b8a9b1dSJustin T. Gibbs 		xpt_scan_bus_info *scan_info;
53298b8a9b1dSJustin T. Gibbs 		path_id_t path_id;
53308b8a9b1dSJustin T. Gibbs 		target_id_t target_id;
53318b8a9b1dSJustin T. Gibbs 		lun_id_t lun_id;
53328b8a9b1dSJustin T. Gibbs 
53338b8a9b1dSJustin T. Gibbs 		/* Reuse the same CCB to query if a device was really found */
53348b8a9b1dSJustin T. Gibbs 		scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
53358b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
53368b8a9b1dSJustin T. Gibbs 			      request_ccb->ccb_h.pinfo.priority);
53378b8a9b1dSJustin T. Gibbs 		request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
53388b8a9b1dSJustin T. Gibbs 
53398b8a9b1dSJustin T. Gibbs 		path_id = request_ccb->ccb_h.path_id;
53408b8a9b1dSJustin T. Gibbs 		target_id = request_ccb->ccb_h.target_id;
53418b8a9b1dSJustin T. Gibbs 		lun_id = request_ccb->ccb_h.target_lun;
53428b8a9b1dSJustin T. Gibbs 		xpt_action(request_ccb);
53438b8a9b1dSJustin T. Gibbs 
53448b8a9b1dSJustin T. Gibbs 		if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
53458b8a9b1dSJustin T. Gibbs 			struct cam_ed *device;
53468b8a9b1dSJustin T. Gibbs 			struct cam_et *target;
5347b6a0d1abSMatt Jacob 			int s, phl;
53488b8a9b1dSJustin T. Gibbs 
53498b8a9b1dSJustin T. Gibbs 			/*
53508b8a9b1dSJustin T. Gibbs 			 * If we already probed lun 0 successfully, or
53518b8a9b1dSJustin T. Gibbs 			 * we have additional configured luns on this
53528b8a9b1dSJustin T. Gibbs 			 * target that might have "gone away", go onto
53538b8a9b1dSJustin T. Gibbs 			 * the next lun.
53548b8a9b1dSJustin T. Gibbs 			 */
53558b8a9b1dSJustin T. Gibbs 			target = request_ccb->ccb_h.path->target;
5356b6a0d1abSMatt Jacob 			/*
5357b6a0d1abSMatt Jacob 			 * We may touch devices that we don't
5358b6a0d1abSMatt Jacob 			 * hold references too, so ensure they
5359b6a0d1abSMatt Jacob 			 * don't disappear out from under us.
5360b6a0d1abSMatt Jacob 			 * The target above is referenced by the
5361b6a0d1abSMatt Jacob 			 * path in the request ccb.
5362b6a0d1abSMatt Jacob 			 */
5363b6a0d1abSMatt Jacob 			phl = 0;
5364a5479bc5SJustin T. Gibbs 			s = splcam();
53658b8a9b1dSJustin T. Gibbs 			device = TAILQ_FIRST(&target->ed_entries);
5366b6a0d1abSMatt Jacob 			if (device != NULL) {
53673b87a552SMatt Jacob 				phl = CAN_SRCH_HI_SPARSE(device);
5368b6a0d1abSMatt Jacob 				if (device->lun_id == 0)
53698b8a9b1dSJustin T. Gibbs 					device = TAILQ_NEXT(device, links);
5370b6a0d1abSMatt Jacob 			}
5371a5479bc5SJustin T. Gibbs 			splx(s);
53728b8a9b1dSJustin T. Gibbs 			if ((lun_id != 0) || (device != NULL)) {
5373b6a0d1abSMatt Jacob 				if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
53748b8a9b1dSJustin T. Gibbs 					lun_id++;
53758b8a9b1dSJustin T. Gibbs 			}
53768b8a9b1dSJustin T. Gibbs 		} else {
53778b8a9b1dSJustin T. Gibbs 			struct cam_ed *device;
53788b8a9b1dSJustin T. Gibbs 
53798b8a9b1dSJustin T. Gibbs 			device = request_ccb->ccb_h.path->device;
53808b8a9b1dSJustin T. Gibbs 
53818b8a9b1dSJustin T. Gibbs 			if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
53828b8a9b1dSJustin T. Gibbs 				/* Try the next lun */
5383f4322bc8SMatt Jacob 				if (lun_id < (CAM_SCSI2_MAXLUN-1)
53843b87a552SMatt Jacob 				  || CAN_SRCH_HI_DENSE(device))
53858b8a9b1dSJustin T. Gibbs 					lun_id++;
53868b8a9b1dSJustin T. Gibbs 			}
53878b8a9b1dSJustin T. Gibbs 		}
53888b8a9b1dSJustin T. Gibbs 
5389c1c3139eSMatt Jacob 		/*
5390c1c3139eSMatt Jacob 		 * Free the current request path- we're done with it.
5391c1c3139eSMatt Jacob 		 */
53928b8a9b1dSJustin T. Gibbs 		xpt_free_path(request_ccb->ccb_h.path);
53938b8a9b1dSJustin T. Gibbs 
5394c1c3139eSMatt Jacob 		/*
5395c1c3139eSMatt Jacob 		 * Check to see if we scan any further luns.
5396c1c3139eSMatt Jacob 		 */
5397c1c3139eSMatt Jacob 		if (lun_id == request_ccb->ccb_h.target_lun
53988b8a9b1dSJustin T. Gibbs                  || lun_id > scan_info->cpi->max_lun) {
5399c1c3139eSMatt Jacob 			int done;
54008b8a9b1dSJustin T. Gibbs 
5401c1c3139eSMatt Jacob  hop_again:
5402c1c3139eSMatt Jacob 			done = 0;
5403c1c3139eSMatt Jacob 			if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
5404c1c3139eSMatt Jacob 				scan_info->counter++;
5405c1c3139eSMatt Jacob 				if (scan_info->counter ==
5406c1c3139eSMatt Jacob 				    scan_info->cpi->initiator_id) {
5407c1c3139eSMatt Jacob 					scan_info->counter++;
5408c1c3139eSMatt Jacob 				}
5409c1c3139eSMatt Jacob 				if (scan_info->counter >=
5410c1c3139eSMatt Jacob 				    scan_info->cpi->max_target+1) {
5411c1c3139eSMatt Jacob 					done = 1;
5412c1c3139eSMatt Jacob 				}
5413c1c3139eSMatt Jacob 			} else {
5414c1c3139eSMatt Jacob 				scan_info->counter--;
5415c1c3139eSMatt Jacob 				if (scan_info->counter == 0) {
5416c1c3139eSMatt Jacob 					done = 1;
5417c1c3139eSMatt Jacob 				}
5418c1c3139eSMatt Jacob 			}
5419c1c3139eSMatt Jacob 			if (done) {
54208b8a9b1dSJustin T. Gibbs 				xpt_free_ccb(request_ccb);
54218b8a9b1dSJustin T. Gibbs 				xpt_free_ccb((union ccb *)scan_info->cpi);
54228b8a9b1dSJustin T. Gibbs 				request_ccb = scan_info->request_ccb;
54238b8a9b1dSJustin T. Gibbs 				free(scan_info, M_TEMP);
54248b8a9b1dSJustin T. Gibbs 				request_ccb->ccb_h.status = CAM_REQ_CMP;
54258b8a9b1dSJustin T. Gibbs 				xpt_done(request_ccb);
5426c1c3139eSMatt Jacob 				break;
54278b8a9b1dSJustin T. Gibbs 			}
54288b8a9b1dSJustin T. Gibbs 
5429c1c3139eSMatt Jacob 			if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
5430c1c3139eSMatt Jacob 				break;
5431c1c3139eSMatt Jacob 			}
54328b8a9b1dSJustin T. Gibbs 			status = xpt_create_path(&path, xpt_periph,
5433c1c3139eSMatt Jacob 			    scan_info->request_ccb->ccb_h.path_id,
5434c1c3139eSMatt Jacob 			    scan_info->counter, 0);
54358b8a9b1dSJustin T. Gibbs 			if (status != CAM_REQ_CMP) {
54368b8a9b1dSJustin T. Gibbs 				printf("xpt_scan_bus: xpt_create_path failed"
5437c1c3139eSMatt Jacob 				    " with status %#x, bus scan halted\n",
54388b8a9b1dSJustin T. Gibbs 			       	    status);
54398b8a9b1dSJustin T. Gibbs 				xpt_free_ccb(request_ccb);
5440c1c3139eSMatt Jacob 				xpt_free_ccb((union ccb *)scan_info->cpi);
54418b8a9b1dSJustin T. Gibbs 				request_ccb = scan_info->request_ccb;
54428b8a9b1dSJustin T. Gibbs 				free(scan_info, M_TEMP);
5443c1c3139eSMatt Jacob 				request_ccb->ccb_h.status = status;
54448b8a9b1dSJustin T. Gibbs 				xpt_done(request_ccb);
5445ec04feb2SSam Leffler 				break;
54468b8a9b1dSJustin T. Gibbs 			}
54478b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&request_ccb->ccb_h, path,
54488b8a9b1dSJustin T. Gibbs 			    request_ccb->ccb_h.pinfo.priority);
54498b8a9b1dSJustin T. Gibbs 			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
54508b8a9b1dSJustin T. Gibbs 			request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
54518b8a9b1dSJustin T. Gibbs 			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
54528b8a9b1dSJustin T. Gibbs 			request_ccb->crcn.flags =
54538b8a9b1dSJustin T. Gibbs 			    scan_info->request_ccb->crcn.flags;
5454c1c3139eSMatt Jacob 		} else {
5455c1c3139eSMatt Jacob 			status = xpt_create_path(&path, xpt_periph,
5456c1c3139eSMatt Jacob 						 path_id, target_id, lun_id);
5457c1c3139eSMatt Jacob 			if (status != CAM_REQ_CMP) {
5458c1c3139eSMatt Jacob 				printf("xpt_scan_bus: xpt_create_path failed "
5459c1c3139eSMatt Jacob 				       "with status %#x, halting LUN scan\n",
5460c1c3139eSMatt Jacob 			 	       status);
5461c1c3139eSMatt Jacob 				goto hop_again;
54628b8a9b1dSJustin T. Gibbs 			}
5463c1c3139eSMatt Jacob 			xpt_setup_ccb(&request_ccb->ccb_h, path,
5464c1c3139eSMatt Jacob 				      request_ccb->ccb_h.pinfo.priority);
5465c1c3139eSMatt Jacob 			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5466c1c3139eSMatt Jacob 			request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5467c1c3139eSMatt Jacob 			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5468c1c3139eSMatt Jacob 			request_ccb->crcn.flags =
5469c1c3139eSMatt Jacob 				scan_info->request_ccb->crcn.flags;
5470c1c3139eSMatt Jacob 		}
5471c1c3139eSMatt Jacob 		xpt_action(request_ccb);
54728b8a9b1dSJustin T. Gibbs 		break;
54738b8a9b1dSJustin T. Gibbs 	}
54748b8a9b1dSJustin T. Gibbs 	default:
54758b8a9b1dSJustin T. Gibbs 		break;
54768b8a9b1dSJustin T. Gibbs 	}
54778b8a9b1dSJustin T. Gibbs }
54788b8a9b1dSJustin T. Gibbs 
54798b8a9b1dSJustin T. Gibbs typedef enum {
54808b8a9b1dSJustin T. Gibbs 	PROBE_TUR,
54812afca7acSMatt Jacob 	PROBE_INQUIRY,	/* this counts as DV0 for Basic Domain Validation */
54829ec5d7cdSMatt Jacob 	PROBE_FULL_INQUIRY,
54838b8a9b1dSJustin T. Gibbs 	PROBE_MODE_SENSE,
54848b8a9b1dSJustin T. Gibbs 	PROBE_SERIAL_NUM,
54852afca7acSMatt Jacob 	PROBE_TUR_FOR_NEGOTIATION,
54862afca7acSMatt Jacob 	PROBE_INQUIRY_BASIC_DV1,
54872afca7acSMatt Jacob 	PROBE_INQUIRY_BASIC_DV2,
54882afca7acSMatt Jacob 	PROBE_DV_EXIT
54898b8a9b1dSJustin T. Gibbs } probe_action;
54908b8a9b1dSJustin T. Gibbs 
54918b8a9b1dSJustin T. Gibbs typedef enum {
54928b8a9b1dSJustin T. Gibbs 	PROBE_INQUIRY_CKSUM	= 0x01,
54938b8a9b1dSJustin T. Gibbs 	PROBE_SERIAL_CKSUM	= 0x02,
54948b8a9b1dSJustin T. Gibbs 	PROBE_NO_ANNOUNCE	= 0x04
54958b8a9b1dSJustin T. Gibbs } probe_flags;
54968b8a9b1dSJustin T. Gibbs 
54978b8a9b1dSJustin T. Gibbs typedef struct {
5498e3975643SJake Burkholder 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
54998b8a9b1dSJustin T. Gibbs 	probe_action	action;
55008b8a9b1dSJustin T. Gibbs 	union ccb	saved_ccb;
55018b8a9b1dSJustin T. Gibbs 	probe_flags	flags;
55028b8a9b1dSJustin T. Gibbs 	MD5_CTX		context;
55038b8a9b1dSJustin T. Gibbs 	u_int8_t	digest[16];
55048b8a9b1dSJustin T. Gibbs } probe_softc;
55058b8a9b1dSJustin T. Gibbs 
55068b8a9b1dSJustin T. Gibbs static void
55078b8a9b1dSJustin T. Gibbs xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
55088b8a9b1dSJustin T. Gibbs 	     cam_flags flags, union ccb *request_ccb)
55098b8a9b1dSJustin T. Gibbs {
551098192658SJustin T. Gibbs 	struct ccb_pathinq cpi;
55118b8a9b1dSJustin T. Gibbs 	cam_status status;
55128b8a9b1dSJustin T. Gibbs 	struct cam_path *new_path;
55138b8a9b1dSJustin T. Gibbs 	struct cam_periph *old_periph;
55148b8a9b1dSJustin T. Gibbs 	int s;
55158b8a9b1dSJustin T. Gibbs 
55168b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
55178b8a9b1dSJustin T. Gibbs 		  ("xpt_scan_lun\n"));
55188b8a9b1dSJustin T. Gibbs 
551998192658SJustin T. Gibbs 	xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
552098192658SJustin T. Gibbs 	cpi.ccb_h.func_code = XPT_PATH_INQ;
552198192658SJustin T. Gibbs 	xpt_action((union ccb *)&cpi);
552298192658SJustin T. Gibbs 
552398192658SJustin T. Gibbs 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
552498192658SJustin T. Gibbs 		if (request_ccb != NULL) {
552598192658SJustin T. Gibbs 			request_ccb->ccb_h.status = cpi.ccb_h.status;
552698192658SJustin T. Gibbs 			xpt_done(request_ccb);
552798192658SJustin T. Gibbs 		}
552898192658SJustin T. Gibbs 		return;
552998192658SJustin T. Gibbs 	}
553098192658SJustin T. Gibbs 
553198192658SJustin T. Gibbs 	if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
553298192658SJustin T. Gibbs 		/*
553398192658SJustin T. Gibbs 		 * Can't scan the bus on an adapter that
553498192658SJustin T. Gibbs 		 * cannot perform the initiator role.
553598192658SJustin T. Gibbs 		 */
553698192658SJustin T. Gibbs 		if (request_ccb != NULL) {
553798192658SJustin T. Gibbs 			request_ccb->ccb_h.status = CAM_REQ_CMP;
553898192658SJustin T. Gibbs 			xpt_done(request_ccb);
553998192658SJustin T. Gibbs 		}
554098192658SJustin T. Gibbs 		return;
554198192658SJustin T. Gibbs 	}
554298192658SJustin T. Gibbs 
55438b8a9b1dSJustin T. Gibbs 	if (request_ccb == NULL) {
55448b8a9b1dSJustin T. Gibbs 		request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
55458b8a9b1dSJustin T. Gibbs 		if (request_ccb == NULL) {
5546f0d9af51SMatt Jacob 			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
5547f0d9af51SMatt Jacob 			    "can't continue\n");
55488b8a9b1dSJustin T. Gibbs 			return;
55498b8a9b1dSJustin T. Gibbs 		}
55508b8a9b1dSJustin T. Gibbs 		new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
55518b8a9b1dSJustin T. Gibbs 		if (new_path == NULL) {
5552f0d9af51SMatt Jacob 			xpt_print(path, "xpt_scan_lun: can't allocate path, "
5553f0d9af51SMatt Jacob 			    "can't continue\n");
55548b8a9b1dSJustin T. Gibbs 			free(request_ccb, M_TEMP);
55558b8a9b1dSJustin T. Gibbs 			return;
55568b8a9b1dSJustin T. Gibbs 		}
5557fce84cb4SKenneth D. Merry 		status = xpt_compile_path(new_path, xpt_periph,
5558fce84cb4SKenneth D. Merry 					  path->bus->path_id,
55598b8a9b1dSJustin T. Gibbs 					  path->target->target_id,
55608b8a9b1dSJustin T. Gibbs 					  path->device->lun_id);
55618b8a9b1dSJustin T. Gibbs 
55628b8a9b1dSJustin T. Gibbs 		if (status != CAM_REQ_CMP) {
5563f0d9af51SMatt Jacob 			xpt_print(path, "xpt_scan_lun: can't compile path, "
5564f0d9af51SMatt Jacob 			    "can't continue\n");
55658b8a9b1dSJustin T. Gibbs 			free(request_ccb, M_TEMP);
55668b8a9b1dSJustin T. Gibbs 			free(new_path, M_TEMP);
55678b8a9b1dSJustin T. Gibbs 			return;
55688b8a9b1dSJustin T. Gibbs 		}
55698b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
55708b8a9b1dSJustin T. Gibbs 		request_ccb->ccb_h.cbfcnp = xptscandone;
55718b8a9b1dSJustin T. Gibbs 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
55728b8a9b1dSJustin T. Gibbs 		request_ccb->crcn.flags = flags;
55738b8a9b1dSJustin T. Gibbs 	}
55748b8a9b1dSJustin T. Gibbs 
55758b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
55768b8a9b1dSJustin T. Gibbs 	if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
55778b8a9b1dSJustin T. Gibbs 		probe_softc *softc;
55788b8a9b1dSJustin T. Gibbs 
55798b8a9b1dSJustin T. Gibbs 		softc = (probe_softc *)old_periph->softc;
55808b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
55818b8a9b1dSJustin T. Gibbs 				  periph_links.tqe);
55828b8a9b1dSJustin T. Gibbs 	} else {
5583ee9c90c7SKenneth D. Merry 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
55848b8a9b1dSJustin T. Gibbs 					  probestart, "probe",
55858b8a9b1dSJustin T. Gibbs 					  CAM_PERIPH_BIO,
55868b8a9b1dSJustin T. Gibbs 					  request_ccb->ccb_h.path, NULL, 0,
55878b8a9b1dSJustin T. Gibbs 					  request_ccb);
55888b8a9b1dSJustin T. Gibbs 
55898b8a9b1dSJustin T. Gibbs 		if (status != CAM_REQ_CMP) {
5590f0d9af51SMatt Jacob 			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
5591f0d9af51SMatt Jacob 			    "returned an error, can't continue probe\n");
55928b8a9b1dSJustin T. Gibbs 			request_ccb->ccb_h.status = status;
55938b8a9b1dSJustin T. Gibbs 			xpt_done(request_ccb);
55948b8a9b1dSJustin T. Gibbs 		}
55958b8a9b1dSJustin T. Gibbs 	}
55968b8a9b1dSJustin T. Gibbs 	splx(s);
55978b8a9b1dSJustin T. Gibbs }
55988b8a9b1dSJustin T. Gibbs 
55998b8a9b1dSJustin T. Gibbs static void
56008b8a9b1dSJustin T. Gibbs xptscandone(struct cam_periph *periph, union ccb *done_ccb)
56018b8a9b1dSJustin T. Gibbs {
56028b8a9b1dSJustin T. Gibbs 	xpt_release_path(done_ccb->ccb_h.path);
56038b8a9b1dSJustin T. Gibbs 	free(done_ccb->ccb_h.path, M_TEMP);
56048b8a9b1dSJustin T. Gibbs 	free(done_ccb, M_TEMP);
56058b8a9b1dSJustin T. Gibbs }
56068b8a9b1dSJustin T. Gibbs 
56078b8a9b1dSJustin T. Gibbs static cam_status
56088b8a9b1dSJustin T. Gibbs proberegister(struct cam_periph *periph, void *arg)
56098b8a9b1dSJustin T. Gibbs {
561087cfaf0eSJustin T. Gibbs 	union ccb *request_ccb;	/* CCB representing the probe request */
56118b8a9b1dSJustin T. Gibbs 	probe_softc *softc;
56128b8a9b1dSJustin T. Gibbs 
561387cfaf0eSJustin T. Gibbs 	request_ccb = (union ccb *)arg;
56148b8a9b1dSJustin T. Gibbs 	if (periph == NULL) {
56158b8a9b1dSJustin T. Gibbs 		printf("proberegister: periph was NULL!!\n");
56168b8a9b1dSJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
56178b8a9b1dSJustin T. Gibbs 	}
56188b8a9b1dSJustin T. Gibbs 
561987cfaf0eSJustin T. Gibbs 	if (request_ccb == NULL) {
5620434bbf6eSJustin T. Gibbs 		printf("proberegister: no probe CCB, "
5621434bbf6eSJustin T. Gibbs 		       "can't register device\n");
56228b8a9b1dSJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
56238b8a9b1dSJustin T. Gibbs 	}
56248b8a9b1dSJustin T. Gibbs 
56259bc66b83SJustin T. Gibbs 	softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
56268b8a9b1dSJustin T. Gibbs 
56278b8a9b1dSJustin T. Gibbs 	if (softc == NULL) {
56288b8a9b1dSJustin T. Gibbs 		printf("proberegister: Unable to probe new device. "
56298b8a9b1dSJustin T. Gibbs 		       "Unable to allocate softc\n");
56308b8a9b1dSJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
56318b8a9b1dSJustin T. Gibbs 	}
56328b8a9b1dSJustin T. Gibbs 	TAILQ_INIT(&softc->request_ccbs);
563387cfaf0eSJustin T. Gibbs 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
563487cfaf0eSJustin T. Gibbs 			  periph_links.tqe);
56358b8a9b1dSJustin T. Gibbs 	softc->flags = 0;
56368b8a9b1dSJustin T. Gibbs 	periph->softc = softc;
56378b8a9b1dSJustin T. Gibbs 	cam_periph_acquire(periph);
563887cfaf0eSJustin T. Gibbs 	/*
563987cfaf0eSJustin T. Gibbs 	 * Ensure we've waited at least a bus settle
564087cfaf0eSJustin T. Gibbs 	 * delay before attempting to probe the device.
5641cda9006fSMatt Jacob 	 * For HBAs that don't do bus resets, this won't make a difference.
564287cfaf0eSJustin T. Gibbs 	 */
564387cfaf0eSJustin T. Gibbs 	cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
56443a937198SBrooks Davis 				      scsi_delay);
56458b8a9b1dSJustin T. Gibbs 	probeschedule(periph);
56468b8a9b1dSJustin T. Gibbs 	return(CAM_REQ_CMP);
56478b8a9b1dSJustin T. Gibbs }
56488b8a9b1dSJustin T. Gibbs 
56498b8a9b1dSJustin T. Gibbs static void
56508b8a9b1dSJustin T. Gibbs probeschedule(struct cam_periph *periph)
56518b8a9b1dSJustin T. Gibbs {
565287cfaf0eSJustin T. Gibbs 	struct ccb_pathinq cpi;
56538b8a9b1dSJustin T. Gibbs 	union ccb *ccb;
56548b8a9b1dSJustin T. Gibbs 	probe_softc *softc;
56558b8a9b1dSJustin T. Gibbs 
56568b8a9b1dSJustin T. Gibbs 	softc = (probe_softc *)periph->softc;
56578b8a9b1dSJustin T. Gibbs 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
56588b8a9b1dSJustin T. Gibbs 
565987cfaf0eSJustin T. Gibbs 	xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
566087cfaf0eSJustin T. Gibbs 	cpi.ccb_h.func_code = XPT_PATH_INQ;
566187cfaf0eSJustin T. Gibbs 	xpt_action((union ccb *)&cpi);
566287cfaf0eSJustin T. Gibbs 
56638b8a9b1dSJustin T. Gibbs 	/*
56648b8a9b1dSJustin T. Gibbs 	 * If a device has gone away and another device, or the same one,
56658b8a9b1dSJustin T. Gibbs 	 * is back in the same place, it should have a unit attention
56668b8a9b1dSJustin T. Gibbs 	 * condition pending.  It will not report the unit attention in
56678b8a9b1dSJustin T. Gibbs 	 * response to an inquiry, which may leave invalid transfer
56688b8a9b1dSJustin T. Gibbs 	 * negotiations in effect.  The TUR will reveal the unit attention
56698b8a9b1dSJustin T. Gibbs 	 * condition.  Only send the TUR for lun 0, since some devices
56708b8a9b1dSJustin T. Gibbs 	 * will get confused by commands other than inquiry to non-existent
56718b8a9b1dSJustin T. Gibbs 	 * luns.  If you think a device has gone away start your scan from
56728b8a9b1dSJustin T. Gibbs 	 * lun 0.  This will insure that any bogus transfer settings are
56738b8a9b1dSJustin T. Gibbs 	 * invalidated.
567487cfaf0eSJustin T. Gibbs 	 *
567587cfaf0eSJustin T. Gibbs 	 * If we haven't seen the device before and the controller supports
567687cfaf0eSJustin T. Gibbs 	 * some kind of transfer negotiation, negotiate with the first
567787cfaf0eSJustin T. Gibbs 	 * sent command if no bus reset was performed at startup.  This
567887cfaf0eSJustin T. Gibbs 	 * ensures that the device is not confused by transfer negotiation
567987cfaf0eSJustin T. Gibbs 	 * settings left over by loader or BIOS action.
56808b8a9b1dSJustin T. Gibbs 	 */
56818b8a9b1dSJustin T. Gibbs 	if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
568287cfaf0eSJustin T. Gibbs 	 && (ccb->ccb_h.target_lun == 0)) {
56838b8a9b1dSJustin T. Gibbs 		softc->action = PROBE_TUR;
568487cfaf0eSJustin T. Gibbs 	} else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
568587cfaf0eSJustin T. Gibbs 	      && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
568687cfaf0eSJustin T. Gibbs 		proberequestdefaultnegotiation(periph);
56878b8a9b1dSJustin T. Gibbs 		softc->action = PROBE_INQUIRY;
568887cfaf0eSJustin T. Gibbs 	} else {
568987cfaf0eSJustin T. Gibbs 		softc->action = PROBE_INQUIRY;
569087cfaf0eSJustin T. Gibbs 	}
56918b8a9b1dSJustin T. Gibbs 
56928b8a9b1dSJustin T. Gibbs 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
56938b8a9b1dSJustin T. Gibbs 		softc->flags |= PROBE_NO_ANNOUNCE;
56948b8a9b1dSJustin T. Gibbs 	else
56958b8a9b1dSJustin T. Gibbs 		softc->flags &= ~PROBE_NO_ANNOUNCE;
56968b8a9b1dSJustin T. Gibbs 
56978b8a9b1dSJustin T. Gibbs 	xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
56988b8a9b1dSJustin T. Gibbs }
56998b8a9b1dSJustin T. Gibbs 
57008b8a9b1dSJustin T. Gibbs static void
57018b8a9b1dSJustin T. Gibbs probestart(struct cam_periph *periph, union ccb *start_ccb)
57028b8a9b1dSJustin T. Gibbs {
57038b8a9b1dSJustin T. Gibbs 	/* Probe the device that our peripheral driver points to */
57048b8a9b1dSJustin T. Gibbs 	struct ccb_scsiio *csio;
57058b8a9b1dSJustin T. Gibbs 	probe_softc *softc;
57068b8a9b1dSJustin T. Gibbs 
57078b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
57088b8a9b1dSJustin T. Gibbs 
57098b8a9b1dSJustin T. Gibbs 	softc = (probe_softc *)periph->softc;
57108b8a9b1dSJustin T. Gibbs 	csio = &start_ccb->csio;
57118b8a9b1dSJustin T. Gibbs 
57128b8a9b1dSJustin T. Gibbs 	switch (softc->action) {
57138b8a9b1dSJustin T. Gibbs 	case PROBE_TUR:
57148b8a9b1dSJustin T. Gibbs 	case PROBE_TUR_FOR_NEGOTIATION:
57152afca7acSMatt Jacob 	case PROBE_DV_EXIT:
57168b8a9b1dSJustin T. Gibbs 	{
57178b8a9b1dSJustin T. Gibbs 		scsi_test_unit_ready(csio,
57188b8a9b1dSJustin T. Gibbs 				     /*retries*/4,
57198b8a9b1dSJustin T. Gibbs 				     probedone,
57208b8a9b1dSJustin T. Gibbs 				     MSG_SIMPLE_Q_TAG,
57218b8a9b1dSJustin T. Gibbs 				     SSD_FULL_SIZE,
5722e471e974SJustin T. Gibbs 				     /*timeout*/60000);
57238b8a9b1dSJustin T. Gibbs 		break;
57248b8a9b1dSJustin T. Gibbs 	}
57258b8a9b1dSJustin T. Gibbs 	case PROBE_INQUIRY:
57269ec5d7cdSMatt Jacob 	case PROBE_FULL_INQUIRY:
57272afca7acSMatt Jacob 	case PROBE_INQUIRY_BASIC_DV1:
57282afca7acSMatt Jacob 	case PROBE_INQUIRY_BASIC_DV2:
57298b8a9b1dSJustin T. Gibbs 	{
5730c19cf05dSMatt Jacob 		u_int inquiry_len;
57318b8a9b1dSJustin T. Gibbs 		struct scsi_inquiry_data *inq_buf;
57328b8a9b1dSJustin T. Gibbs 
57338b8a9b1dSJustin T. Gibbs 		inq_buf = &periph->path->device->inq_data;
57342afca7acSMatt Jacob 
57358b8a9b1dSJustin T. Gibbs 		/*
57368b8a9b1dSJustin T. Gibbs 		 * If the device is currently configured, we calculate an
57378b8a9b1dSJustin T. Gibbs 		 * MD5 checksum of the inquiry data, and if the serial number
57388b8a9b1dSJustin T. Gibbs 		 * length is greater than 0, add the serial number data
57398b8a9b1dSJustin T. Gibbs 		 * into the checksum as well.  Once the inquiry and the
57408b8a9b1dSJustin T. Gibbs 		 * serial number check finish, we attempt to figure out
57418b8a9b1dSJustin T. Gibbs 		 * whether we still have the same device.
57428b8a9b1dSJustin T. Gibbs 		 */
57438b8a9b1dSJustin T. Gibbs 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
57448b8a9b1dSJustin T. Gibbs 
57458b8a9b1dSJustin T. Gibbs 			MD5Init(&softc->context);
57468b8a9b1dSJustin T. Gibbs 			MD5Update(&softc->context, (unsigned char *)inq_buf,
57478b8a9b1dSJustin T. Gibbs 				  sizeof(struct scsi_inquiry_data));
57488b8a9b1dSJustin T. Gibbs 			softc->flags |= PROBE_INQUIRY_CKSUM;
57498b8a9b1dSJustin T. Gibbs 			if (periph->path->device->serial_num_len > 0) {
57508b8a9b1dSJustin T. Gibbs 				MD5Update(&softc->context,
57518b8a9b1dSJustin T. Gibbs 					  periph->path->device->serial_num,
57528b8a9b1dSJustin T. Gibbs 					  periph->path->device->serial_num_len);
57538b8a9b1dSJustin T. Gibbs 				softc->flags |= PROBE_SERIAL_CKSUM;
57548b8a9b1dSJustin T. Gibbs 			}
57558b8a9b1dSJustin T. Gibbs 			MD5Final(softc->digest, &softc->context);
57568b8a9b1dSJustin T. Gibbs 		}
57578b8a9b1dSJustin T. Gibbs 
57589ec5d7cdSMatt Jacob 		if (softc->action == PROBE_INQUIRY)
5759c19cf05dSMatt Jacob 			inquiry_len = SHORT_INQUIRY_LENGTH;
5760c19cf05dSMatt Jacob 		else
57612afca7acSMatt Jacob 			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
5762709a936fSJustin T. Gibbs 
5763709a936fSJustin T. Gibbs 		/*
5764709a936fSJustin T. Gibbs 		 * Some parallel SCSI devices fail to send an
5765709a936fSJustin T. Gibbs 		 * ignore wide residue message when dealing with
5766709a936fSJustin T. Gibbs 		 * odd length inquiry requests.  Round up to be
5767709a936fSJustin T. Gibbs 		 * safe.
5768709a936fSJustin T. Gibbs 		 */
5769709a936fSJustin T. Gibbs 		inquiry_len = roundup2(inquiry_len, 2);
57709ec5d7cdSMatt Jacob 
57712afca7acSMatt Jacob 		if (softc->action == PROBE_INQUIRY_BASIC_DV1
57722afca7acSMatt Jacob 		 || softc->action == PROBE_INQUIRY_BASIC_DV2) {
57732afca7acSMatt Jacob 			inq_buf = malloc(inquiry_len, M_TEMP, M_NOWAIT);
57742afca7acSMatt Jacob 		}
57752afca7acSMatt Jacob 		if (inq_buf == NULL) {
5776f0d9af51SMatt Jacob 			xpt_print(periph->path, "malloc failure- skipping Basic"
5777f0d9af51SMatt Jacob 			    "Domain Validation\n");
57782afca7acSMatt Jacob 			softc->action = PROBE_DV_EXIT;
57792afca7acSMatt Jacob 			scsi_test_unit_ready(csio,
57802afca7acSMatt Jacob 					     /*retries*/4,
57812afca7acSMatt Jacob 					     probedone,
57822afca7acSMatt Jacob 					     MSG_SIMPLE_Q_TAG,
57832afca7acSMatt Jacob 					     SSD_FULL_SIZE,
57842afca7acSMatt Jacob 					     /*timeout*/60000);
57852afca7acSMatt Jacob 			break;
57862afca7acSMatt Jacob 		}
57878b8a9b1dSJustin T. Gibbs 		scsi_inquiry(csio,
57888b8a9b1dSJustin T. Gibbs 			     /*retries*/4,
57898b8a9b1dSJustin T. Gibbs 			     probedone,
57908b8a9b1dSJustin T. Gibbs 			     MSG_SIMPLE_Q_TAG,
57918b8a9b1dSJustin T. Gibbs 			     (u_int8_t *)inq_buf,
5792c19cf05dSMatt Jacob 			     inquiry_len,
57938b8a9b1dSJustin T. Gibbs 			     /*evpd*/FALSE,
57948b8a9b1dSJustin T. Gibbs 			     /*page_code*/0,
57958b8a9b1dSJustin T. Gibbs 			     SSD_MIN_SIZE,
5796e471e974SJustin T. Gibbs 			     /*timeout*/60 * 1000);
57978b8a9b1dSJustin T. Gibbs 		break;
57988b8a9b1dSJustin T. Gibbs 	}
57998b8a9b1dSJustin T. Gibbs 	case PROBE_MODE_SENSE:
58008b8a9b1dSJustin T. Gibbs 	{
58018b8a9b1dSJustin T. Gibbs 		void  *mode_buf;
58028b8a9b1dSJustin T. Gibbs 		int    mode_buf_len;
58038b8a9b1dSJustin T. Gibbs 
58048b8a9b1dSJustin T. Gibbs 		mode_buf_len = sizeof(struct scsi_mode_header_6)
58058b8a9b1dSJustin T. Gibbs 			     + sizeof(struct scsi_mode_blk_desc)
58068b8a9b1dSJustin T. Gibbs 			     + sizeof(struct scsi_control_page);
58078b8a9b1dSJustin T. Gibbs 		mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
58088b8a9b1dSJustin T. Gibbs 		if (mode_buf != NULL) {
58098b8a9b1dSJustin T. Gibbs 	                scsi_mode_sense(csio,
58108b8a9b1dSJustin T. Gibbs 					/*retries*/4,
58118b8a9b1dSJustin T. Gibbs 					probedone,
58128b8a9b1dSJustin T. Gibbs 					MSG_SIMPLE_Q_TAG,
58138b8a9b1dSJustin T. Gibbs 					/*dbd*/FALSE,
58148b8a9b1dSJustin T. Gibbs 					SMS_PAGE_CTRL_CURRENT,
58158b8a9b1dSJustin T. Gibbs 					SMS_CONTROL_MODE_PAGE,
58168b8a9b1dSJustin T. Gibbs 					mode_buf,
58178b8a9b1dSJustin T. Gibbs 					mode_buf_len,
58188b8a9b1dSJustin T. Gibbs 					SSD_FULL_SIZE,
5819e471e974SJustin T. Gibbs 					/*timeout*/60000);
58208b8a9b1dSJustin T. Gibbs 			break;
58218b8a9b1dSJustin T. Gibbs 		}
5822f0d9af51SMatt Jacob 		xpt_print(periph->path, "Unable to mode sense control page - "
5823f0d9af51SMatt Jacob 		    "malloc failure\n");
58248b8a9b1dSJustin T. Gibbs 		softc->action = PROBE_SERIAL_NUM;
58258b8a9b1dSJustin T. Gibbs 	}
582607c6eac9SPoul-Henning Kamp 	/* FALLTHROUGH */
58278b8a9b1dSJustin T. Gibbs 	case PROBE_SERIAL_NUM:
58288b8a9b1dSJustin T. Gibbs 	{
58298b8a9b1dSJustin T. Gibbs 		struct scsi_vpd_unit_serial_number *serial_buf;
58308b8a9b1dSJustin T. Gibbs 		struct cam_ed* device;
58318b8a9b1dSJustin T. Gibbs 
58328b8a9b1dSJustin T. Gibbs 		serial_buf = NULL;
58338b8a9b1dSJustin T. Gibbs 		device = periph->path->device;
58348b8a9b1dSJustin T. Gibbs 		device->serial_num = NULL;
58358b8a9b1dSJustin T. Gibbs 		device->serial_num_len = 0;
58368b8a9b1dSJustin T. Gibbs 
58378b8a9b1dSJustin T. Gibbs 		if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
58388b8a9b1dSJustin T. Gibbs 			serial_buf = (struct scsi_vpd_unit_serial_number *)
58395417ec4dSDavid Malone 				malloc(sizeof(*serial_buf), M_TEMP,
58405417ec4dSDavid Malone 					M_NOWAIT | M_ZERO);
58418b8a9b1dSJustin T. Gibbs 
58428b8a9b1dSJustin T. Gibbs 		if (serial_buf != NULL) {
58438b8a9b1dSJustin T. Gibbs 			scsi_inquiry(csio,
58448b8a9b1dSJustin T. Gibbs 				     /*retries*/4,
58458b8a9b1dSJustin T. Gibbs 				     probedone,
58468b8a9b1dSJustin T. Gibbs 				     MSG_SIMPLE_Q_TAG,
58478b8a9b1dSJustin T. Gibbs 				     (u_int8_t *)serial_buf,
58488b8a9b1dSJustin T. Gibbs 				     sizeof(*serial_buf),
58498b8a9b1dSJustin T. Gibbs 				     /*evpd*/TRUE,
58508b8a9b1dSJustin T. Gibbs 				     SVPD_UNIT_SERIAL_NUMBER,
58518b8a9b1dSJustin T. Gibbs 				     SSD_MIN_SIZE,
5852e471e974SJustin T. Gibbs 				     /*timeout*/60 * 1000);
58538b8a9b1dSJustin T. Gibbs 			break;
58548b8a9b1dSJustin T. Gibbs 		}
58558b8a9b1dSJustin T. Gibbs 		/*
58568b8a9b1dSJustin T. Gibbs 		 * We'll have to do without, let our probedone
58578b8a9b1dSJustin T. Gibbs 		 * routine finish up for us.
58588b8a9b1dSJustin T. Gibbs 		 */
58598b8a9b1dSJustin T. Gibbs 		start_ccb->csio.data_ptr = NULL;
58608b8a9b1dSJustin T. Gibbs 		probedone(periph, start_ccb);
58618b8a9b1dSJustin T. Gibbs 		return;
58628b8a9b1dSJustin T. Gibbs 	}
58638b8a9b1dSJustin T. Gibbs 	}
58648b8a9b1dSJustin T. Gibbs 	xpt_action(start_ccb);
58658b8a9b1dSJustin T. Gibbs }
58668b8a9b1dSJustin T. Gibbs 
58678b8a9b1dSJustin T. Gibbs static void
586887cfaf0eSJustin T. Gibbs proberequestdefaultnegotiation(struct cam_periph *periph)
586987cfaf0eSJustin T. Gibbs {
587087cfaf0eSJustin T. Gibbs 	struct ccb_trans_settings cts;
587187cfaf0eSJustin T. Gibbs 
587287cfaf0eSJustin T. Gibbs 	xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
587387cfaf0eSJustin T. Gibbs 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
58743393f8daSKenneth D. Merry 	cts.type = CTS_TYPE_USER_SETTINGS;
587587cfaf0eSJustin T. Gibbs 	xpt_action((union ccb *)&cts);
58760a480cf0SMatt Jacob 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
58770a480cf0SMatt Jacob 		return;
58780a480cf0SMatt Jacob 	}
587987cfaf0eSJustin T. Gibbs 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
58803393f8daSKenneth D. Merry 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
588187cfaf0eSJustin T. Gibbs 	xpt_action((union ccb *)&cts);
588287cfaf0eSJustin T. Gibbs }
588387cfaf0eSJustin T. Gibbs 
58842afca7acSMatt Jacob /*
58852afca7acSMatt Jacob  * Backoff Negotiation Code- only pertinent for SPI devices.
58862afca7acSMatt Jacob  */
58872afca7acSMatt Jacob static int
58882afca7acSMatt Jacob proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
58892afca7acSMatt Jacob {
58902afca7acSMatt Jacob 	struct ccb_trans_settings cts;
58912afca7acSMatt Jacob 	struct ccb_trans_settings_spi *spi;
58922afca7acSMatt Jacob 
58932afca7acSMatt Jacob 	memset(&cts, 0, sizeof (cts));
58942afca7acSMatt Jacob 	xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
58952afca7acSMatt Jacob 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
58962afca7acSMatt Jacob 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
58972afca7acSMatt Jacob 	xpt_action((union ccb *)&cts);
58982afca7acSMatt Jacob 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
58992afca7acSMatt Jacob 		if (bootverbose) {
5900f0d9af51SMatt Jacob 			xpt_print(periph->path,
5901f0d9af51SMatt Jacob 			    "failed to get current device settings\n");
59022afca7acSMatt Jacob 		}
59032afca7acSMatt Jacob 		return (0);
59042afca7acSMatt Jacob 	}
59052afca7acSMatt Jacob 	if (cts.transport != XPORT_SPI) {
59062afca7acSMatt Jacob 		if (bootverbose) {
5907f0d9af51SMatt Jacob 			xpt_print(periph->path, "not SPI transport\n");
59082afca7acSMatt Jacob 		}
59092afca7acSMatt Jacob 		return (0);
59102afca7acSMatt Jacob 	}
59112afca7acSMatt Jacob 	spi = &cts.xport_specific.spi;
59122afca7acSMatt Jacob 
59132afca7acSMatt Jacob 	/*
59142afca7acSMatt Jacob 	 * We cannot renegotiate sync rate if we don't have one.
59152afca7acSMatt Jacob 	 */
59162afca7acSMatt Jacob 	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
59172afca7acSMatt Jacob 		if (bootverbose) {
5918f0d9af51SMatt Jacob 			xpt_print(periph->path, "no sync rate known\n");
59192afca7acSMatt Jacob 		}
59202afca7acSMatt Jacob 		return (0);
59212afca7acSMatt Jacob 	}
59222afca7acSMatt Jacob 
59232afca7acSMatt Jacob 	/*
59242afca7acSMatt Jacob 	 * We'll assert that we don't have to touch PPR options- the
59252afca7acSMatt Jacob 	 * SIM will see what we do with period and offset and adjust
59262afca7acSMatt Jacob 	 * the PPR options as appropriate.
59272afca7acSMatt Jacob 	 */
59282afca7acSMatt Jacob 
59292afca7acSMatt Jacob 	/*
59302afca7acSMatt Jacob 	 * A sync rate with unknown or zero offset is nonsensical.
59312afca7acSMatt Jacob 	 * A sync period of zero means Async.
59322afca7acSMatt Jacob 	 */
59332afca7acSMatt Jacob 	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
59342afca7acSMatt Jacob 	 || spi->sync_offset == 0 || spi->sync_period == 0) {
59352afca7acSMatt Jacob 		if (bootverbose) {
5936f0d9af51SMatt Jacob 			xpt_print(periph->path, "no sync rate available\n");
59372afca7acSMatt Jacob 		}
59382afca7acSMatt Jacob 		return (0);
59392afca7acSMatt Jacob 	}
59402afca7acSMatt Jacob 
59412afca7acSMatt Jacob 	if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
59422afca7acSMatt Jacob 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
59432afca7acSMatt Jacob 		    ("hit async: giving up on DV\n"));
59442afca7acSMatt Jacob 		return (0);
59452afca7acSMatt Jacob 	}
59462afca7acSMatt Jacob 
59472afca7acSMatt Jacob 
59482afca7acSMatt Jacob 	/*
59492afca7acSMatt Jacob 	 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
59502afca7acSMatt Jacob 	 * We don't try to remember 'last' settings to see if the SIM actually
59512afca7acSMatt Jacob 	 * gets into the speed we want to set. We check on the SIM telling
59522afca7acSMatt Jacob 	 * us that a requested speed is bad, but otherwise don't try and
59532afca7acSMatt Jacob 	 * check the speed due to the asynchronous and handshake nature
59542afca7acSMatt Jacob 	 * of speed setting.
59552afca7acSMatt Jacob 	 */
59562afca7acSMatt Jacob 	spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
59572afca7acSMatt Jacob 	for (;;) {
59582afca7acSMatt Jacob 		spi->sync_period++;
59592afca7acSMatt Jacob 		if (spi->sync_period >= 0xf) {
59602afca7acSMatt Jacob 			spi->sync_period = 0;
59612afca7acSMatt Jacob 			spi->sync_offset = 0;
59622afca7acSMatt Jacob 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
59632afca7acSMatt Jacob 			    ("setting to async for DV\n"));
59642afca7acSMatt Jacob 			/*
59652afca7acSMatt Jacob 			 * Once we hit async, we don't want to try
59662afca7acSMatt Jacob 			 * any more settings.
59672afca7acSMatt Jacob 			 */
59682afca7acSMatt Jacob 			device->flags |= CAM_DEV_DV_HIT_BOTTOM;
59692afca7acSMatt Jacob 		} else if (bootverbose) {
59702afca7acSMatt Jacob 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
59712afca7acSMatt Jacob 			    ("DV: period 0x%x\n", spi->sync_period));
59722afca7acSMatt Jacob 			printf("setting period to 0x%x\n", spi->sync_period);
59732afca7acSMatt Jacob 		}
59742afca7acSMatt Jacob 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
59752afca7acSMatt Jacob 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
59762afca7acSMatt Jacob 		xpt_action((union ccb *)&cts);
59772afca7acSMatt Jacob 		if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
59782afca7acSMatt Jacob 			break;
59792afca7acSMatt Jacob 		}
59802afca7acSMatt Jacob 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
59812afca7acSMatt Jacob 		    ("DV: failed to set period 0x%x\n", spi->sync_period));
59822afca7acSMatt Jacob 		if (spi->sync_period == 0) {
59832afca7acSMatt Jacob 			return (0);
59842afca7acSMatt Jacob 		}
59852afca7acSMatt Jacob 	}
59862afca7acSMatt Jacob 	return (1);
59872afca7acSMatt Jacob }
59882afca7acSMatt Jacob 
598987cfaf0eSJustin T. Gibbs static void
59908b8a9b1dSJustin T. Gibbs probedone(struct cam_periph *periph, union ccb *done_ccb)
59918b8a9b1dSJustin T. Gibbs {
59928b8a9b1dSJustin T. Gibbs 	probe_softc *softc;
59938b8a9b1dSJustin T. Gibbs 	struct cam_path *path;
59948b8a9b1dSJustin T. Gibbs 	u_int32_t  priority;
59958b8a9b1dSJustin T. Gibbs 
59968b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
59978b8a9b1dSJustin T. Gibbs 
59988b8a9b1dSJustin T. Gibbs 	softc = (probe_softc *)periph->softc;
59998b8a9b1dSJustin T. Gibbs 	path = done_ccb->ccb_h.path;
60008b8a9b1dSJustin T. Gibbs 	priority = done_ccb->ccb_h.pinfo.priority;
60018b8a9b1dSJustin T. Gibbs 
60028b8a9b1dSJustin T. Gibbs 	switch (softc->action) {
60038b8a9b1dSJustin T. Gibbs 	case PROBE_TUR:
60048b8a9b1dSJustin T. Gibbs 	{
60058b8a9b1dSJustin T. Gibbs 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
60068b8a9b1dSJustin T. Gibbs 
60078b8a9b1dSJustin T. Gibbs 			if (cam_periph_error(done_ccb, 0,
60088b8a9b1dSJustin T. Gibbs 					     SF_NO_PRINT, NULL) == ERESTART)
60098b8a9b1dSJustin T. Gibbs 				return;
60108b8a9b1dSJustin T. Gibbs 			else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
60118b8a9b1dSJustin T. Gibbs 				/* Don't wedge the queue */
60122cefde5fSJustin T. Gibbs 				xpt_release_devq(done_ccb->ccb_h.path,
60132cefde5fSJustin T. Gibbs 						 /*count*/1,
60148b8a9b1dSJustin T. Gibbs 						 /*run_queue*/TRUE);
60158b8a9b1dSJustin T. Gibbs 		}
60168b8a9b1dSJustin T. Gibbs 		softc->action = PROBE_INQUIRY;
60178b8a9b1dSJustin T. Gibbs 		xpt_release_ccb(done_ccb);
60188b8a9b1dSJustin T. Gibbs 		xpt_schedule(periph, priority);
60198b8a9b1dSJustin T. Gibbs 		return;
60208b8a9b1dSJustin T. Gibbs 	}
60218b8a9b1dSJustin T. Gibbs 	case PROBE_INQUIRY:
60229ec5d7cdSMatt Jacob 	case PROBE_FULL_INQUIRY:
60238b8a9b1dSJustin T. Gibbs 	{
60248b8a9b1dSJustin T. Gibbs 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
60258b8a9b1dSJustin T. Gibbs 			struct scsi_inquiry_data *inq_buf;
60268b8a9b1dSJustin T. Gibbs 			u_int8_t periph_qual;
60278b8a9b1dSJustin T. Gibbs 
602887cfaf0eSJustin T. Gibbs 			path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
60298b8a9b1dSJustin T. Gibbs 			inq_buf = &path->device->inq_data;
60308b8a9b1dSJustin T. Gibbs 
60318b8a9b1dSJustin T. Gibbs 			periph_qual = SID_QUAL(inq_buf);
60329ec5d7cdSMatt Jacob 
6033c19cf05dSMatt Jacob 			switch(periph_qual) {
6034c19cf05dSMatt Jacob 			case SID_QUAL_LU_CONNECTED:
6035c19cf05dSMatt Jacob 			{
60369a4f1a4dSJustin T. Gibbs 				u_int8_t len;
6037c19cf05dSMatt Jacob 
60389ec5d7cdSMatt Jacob 				/*
6039c19cf05dSMatt Jacob 				 * We conservatively request only
6040c19cf05dSMatt Jacob 				 * SHORT_INQUIRY_LEN bytes of inquiry
6041c19cf05dSMatt Jacob 				 * information during our first try
6042c19cf05dSMatt Jacob 				 * at sending an INQUIRY. If the device
6043c19cf05dSMatt Jacob 				 * has more information to give,
6044c19cf05dSMatt Jacob 				 * perform a second request specifying
6045c19cf05dSMatt Jacob 				 * the amount of information the device
6046c19cf05dSMatt Jacob 				 * is willing to give.
60479ec5d7cdSMatt Jacob 				 */
60489a4f1a4dSJustin T. Gibbs 				len = inq_buf->additional_length
60499a4f1a4dSJustin T. Gibbs 				    + offsetof(struct scsi_inquiry_data,
60509a4f1a4dSJustin T. Gibbs                                                additional_length) + 1;
6051c19cf05dSMatt Jacob 				if (softc->action == PROBE_INQUIRY
60529a4f1a4dSJustin T. Gibbs 				 && len > SHORT_INQUIRY_LENGTH) {
60535cd818b1SKenneth D. Merry 					softc->action = PROBE_FULL_INQUIRY;
60549ec5d7cdSMatt Jacob 					xpt_release_ccb(done_ccb);
60559ec5d7cdSMatt Jacob 					xpt_schedule(periph, priority);
60569ec5d7cdSMatt Jacob 					return;
60579ec5d7cdSMatt Jacob 				}
60589ec5d7cdSMatt Jacob 
60598b8a9b1dSJustin T. Gibbs 				xpt_find_quirk(path->device);
60608b8a9b1dSJustin T. Gibbs 
60613393f8daSKenneth D. Merry 				xpt_devise_transport(path);
60624e359f5aSMatt Jacob 				if (INQ_DATA_TQ_ENABLED(inq_buf))
60635cd818b1SKenneth D. Merry 					softc->action = PROBE_MODE_SENSE;
60648b8a9b1dSJustin T. Gibbs 				else
60655cd818b1SKenneth D. Merry 					softc->action = PROBE_SERIAL_NUM;
60668b8a9b1dSJustin T. Gibbs 
60675cd818b1SKenneth D. Merry 				path->device->flags &= ~CAM_DEV_UNCONFIGURED;
60688b8a9b1dSJustin T. Gibbs 
60698b8a9b1dSJustin T. Gibbs 				xpt_release_ccb(done_ccb);
60708b8a9b1dSJustin T. Gibbs 				xpt_schedule(periph, priority);
60718b8a9b1dSJustin T. Gibbs 				return;
60728b8a9b1dSJustin T. Gibbs 			}
60738b8a9b1dSJustin T. Gibbs 			default:
60748b8a9b1dSJustin T. Gibbs 				break;
60758b8a9b1dSJustin T. Gibbs 			}
60768b8a9b1dSJustin T. Gibbs 		} else if (cam_periph_error(done_ccb, 0,
60778b8a9b1dSJustin T. Gibbs 					    done_ccb->ccb_h.target_lun > 0
60788b8a9b1dSJustin T. Gibbs 					    ? SF_RETRY_UA|SF_QUIET_IR
60798b8a9b1dSJustin T. Gibbs 					    : SF_RETRY_UA,
60808b8a9b1dSJustin T. Gibbs 					    &softc->saved_ccb) == ERESTART) {
60818b8a9b1dSJustin T. Gibbs 			return;
60828b8a9b1dSJustin T. Gibbs 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
60838b8a9b1dSJustin T. Gibbs 			/* Don't wedge the queue */
60842cefde5fSJustin T. Gibbs 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
60858b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
60868b8a9b1dSJustin T. Gibbs 		}
60878b8a9b1dSJustin T. Gibbs 		/*
60888b8a9b1dSJustin T. Gibbs 		 * If we get to this point, we got an error status back
60898b8a9b1dSJustin T. Gibbs 		 * from the inquiry and the error status doesn't require
60908b8a9b1dSJustin T. Gibbs 		 * automatically retrying the command.  Therefore, the
60918b8a9b1dSJustin T. Gibbs 		 * inquiry failed.  If we had inquiry information before
60928b8a9b1dSJustin T. Gibbs 		 * for this device, but this latest inquiry command failed,
60938b8a9b1dSJustin T. Gibbs 		 * the device has probably gone away.  If this device isn't
60948b8a9b1dSJustin T. Gibbs 		 * already marked unconfigured, notify the peripheral
60958b8a9b1dSJustin T. Gibbs 		 * drivers that this device is no more.
60968b8a9b1dSJustin T. Gibbs 		 */
60978b8a9b1dSJustin T. Gibbs 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
60988b8a9b1dSJustin T. Gibbs 			/* Send the async notification. */
60998b8a9b1dSJustin T. Gibbs 			xpt_async(AC_LOST_DEVICE, path, NULL);
61008b8a9b1dSJustin T. Gibbs 
61018b8a9b1dSJustin T. Gibbs 		xpt_release_ccb(done_ccb);
61028b8a9b1dSJustin T. Gibbs 		break;
61038b8a9b1dSJustin T. Gibbs 	}
61048b8a9b1dSJustin T. Gibbs 	case PROBE_MODE_SENSE:
61058b8a9b1dSJustin T. Gibbs 	{
61068b8a9b1dSJustin T. Gibbs 		struct ccb_scsiio *csio;
61078b8a9b1dSJustin T. Gibbs 		struct scsi_mode_header_6 *mode_hdr;
61088b8a9b1dSJustin T. Gibbs 
61098b8a9b1dSJustin T. Gibbs 		csio = &done_ccb->csio;
61108b8a9b1dSJustin T. Gibbs 		mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
61118b8a9b1dSJustin T. Gibbs 		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
61128b8a9b1dSJustin T. Gibbs 			struct scsi_control_page *page;
61138b8a9b1dSJustin T. Gibbs 			u_int8_t *offset;
61148b8a9b1dSJustin T. Gibbs 
61158b8a9b1dSJustin T. Gibbs 			offset = ((u_int8_t *)&mode_hdr[1])
61168b8a9b1dSJustin T. Gibbs 			    + mode_hdr->blk_desc_len;
61178b8a9b1dSJustin T. Gibbs 			page = (struct scsi_control_page *)offset;
61188b8a9b1dSJustin T. Gibbs 			path->device->queue_flags = page->queue_flags;
61198b8a9b1dSJustin T. Gibbs 		} else if (cam_periph_error(done_ccb, 0,
61208b8a9b1dSJustin T. Gibbs 					    SF_RETRY_UA|SF_NO_PRINT,
61218b8a9b1dSJustin T. Gibbs 					    &softc->saved_ccb) == ERESTART) {
61228b8a9b1dSJustin T. Gibbs 			return;
61238b8a9b1dSJustin T. Gibbs 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
61248b8a9b1dSJustin T. Gibbs 			/* Don't wedge the queue */
61252cefde5fSJustin T. Gibbs 			xpt_release_devq(done_ccb->ccb_h.path,
61262cefde5fSJustin T. Gibbs 					 /*count*/1, /*run_queue*/TRUE);
61278b8a9b1dSJustin T. Gibbs 		}
61288b8a9b1dSJustin T. Gibbs 		xpt_release_ccb(done_ccb);
61298b8a9b1dSJustin T. Gibbs 		free(mode_hdr, M_TEMP);
61308b8a9b1dSJustin T. Gibbs 		softc->action = PROBE_SERIAL_NUM;
61318b8a9b1dSJustin T. Gibbs 		xpt_schedule(periph, priority);
61328b8a9b1dSJustin T. Gibbs 		return;
61338b8a9b1dSJustin T. Gibbs 	}
61348b8a9b1dSJustin T. Gibbs 	case PROBE_SERIAL_NUM:
61358b8a9b1dSJustin T. Gibbs 	{
61368b8a9b1dSJustin T. Gibbs 		struct ccb_scsiio *csio;
61378b8a9b1dSJustin T. Gibbs 		struct scsi_vpd_unit_serial_number *serial_buf;
61388b8a9b1dSJustin T. Gibbs 		u_int32_t  priority;
61398b8a9b1dSJustin T. Gibbs 		int changed;
61408b8a9b1dSJustin T. Gibbs 		int have_serialnum;
61418b8a9b1dSJustin T. Gibbs 
61428b8a9b1dSJustin T. Gibbs 		changed = 1;
61438b8a9b1dSJustin T. Gibbs 		have_serialnum = 0;
61448b8a9b1dSJustin T. Gibbs 		csio = &done_ccb->csio;
61458b8a9b1dSJustin T. Gibbs 		priority = done_ccb->ccb_h.pinfo.priority;
61468b8a9b1dSJustin T. Gibbs 		serial_buf =
61478b8a9b1dSJustin T. Gibbs 		    (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
61488b8a9b1dSJustin T. Gibbs 
61498b8a9b1dSJustin T. Gibbs 		/* Clean up from previous instance of this device */
61508b8a9b1dSJustin T. Gibbs 		if (path->device->serial_num != NULL) {
6151362abc44STai-hwa Liang 			free(path->device->serial_num, M_CAMXPT);
61528b8a9b1dSJustin T. Gibbs 			path->device->serial_num = NULL;
61538b8a9b1dSJustin T. Gibbs 			path->device->serial_num_len = 0;
61548b8a9b1dSJustin T. Gibbs 		}
61558b8a9b1dSJustin T. Gibbs 
61568b8a9b1dSJustin T. Gibbs 		if (serial_buf == NULL) {
61578b8a9b1dSJustin T. Gibbs 			/*
61588b8a9b1dSJustin T. Gibbs 			 * Don't process the command as it was never sent
61598b8a9b1dSJustin T. Gibbs 			 */
61608b8a9b1dSJustin T. Gibbs 		} else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
61618b8a9b1dSJustin T. Gibbs 			&& (serial_buf->length > 0)) {
61628b8a9b1dSJustin T. Gibbs 
61638b8a9b1dSJustin T. Gibbs 			have_serialnum = 1;
61648b8a9b1dSJustin T. Gibbs 			path->device->serial_num =
61658b8a9b1dSJustin T. Gibbs 				(u_int8_t *)malloc((serial_buf->length + 1),
6166362abc44STai-hwa Liang 						   M_CAMXPT, M_NOWAIT);
61678b8a9b1dSJustin T. Gibbs 			if (path->device->serial_num != NULL) {
61688b8a9b1dSJustin T. Gibbs 				bcopy(serial_buf->serial_num,
61698b8a9b1dSJustin T. Gibbs 				      path->device->serial_num,
61708b8a9b1dSJustin T. Gibbs 				      serial_buf->length);
61718b8a9b1dSJustin T. Gibbs 				path->device->serial_num_len =
61728b8a9b1dSJustin T. Gibbs 				    serial_buf->length;
61738b8a9b1dSJustin T. Gibbs 				path->device->serial_num[serial_buf->length]
61748b8a9b1dSJustin T. Gibbs 				    = '\0';
61758b8a9b1dSJustin T. Gibbs 			}
61768b8a9b1dSJustin T. Gibbs 		} else if (cam_periph_error(done_ccb, 0,
61778b8a9b1dSJustin T. Gibbs 					    SF_RETRY_UA|SF_NO_PRINT,
61788b8a9b1dSJustin T. Gibbs 					    &softc->saved_ccb) == ERESTART) {
61798b8a9b1dSJustin T. Gibbs 			return;
61808b8a9b1dSJustin T. Gibbs 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
61818b8a9b1dSJustin T. Gibbs 			/* Don't wedge the queue */
61822cefde5fSJustin T. Gibbs 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
61838b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
61848b8a9b1dSJustin T. Gibbs 		}
61858b8a9b1dSJustin T. Gibbs 
61868b8a9b1dSJustin T. Gibbs 		/*
61878b8a9b1dSJustin T. Gibbs 		 * Let's see if we have seen this device before.
61888b8a9b1dSJustin T. Gibbs 		 */
61898b8a9b1dSJustin T. Gibbs 		if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
61908b8a9b1dSJustin T. Gibbs 			MD5_CTX context;
61918b8a9b1dSJustin T. Gibbs 			u_int8_t digest[16];
61928b8a9b1dSJustin T. Gibbs 
61938b8a9b1dSJustin T. Gibbs 			MD5Init(&context);
61948b8a9b1dSJustin T. Gibbs 
61958b8a9b1dSJustin T. Gibbs 			MD5Update(&context,
61968b8a9b1dSJustin T. Gibbs 				  (unsigned char *)&path->device->inq_data,
61978b8a9b1dSJustin T. Gibbs 				  sizeof(struct scsi_inquiry_data));
61988b8a9b1dSJustin T. Gibbs 
61998b8a9b1dSJustin T. Gibbs 			if (have_serialnum)
62008b8a9b1dSJustin T. Gibbs 				MD5Update(&context, serial_buf->serial_num,
62018b8a9b1dSJustin T. Gibbs 					  serial_buf->length);
62028b8a9b1dSJustin T. Gibbs 
62038b8a9b1dSJustin T. Gibbs 			MD5Final(digest, &context);
62048b8a9b1dSJustin T. Gibbs 			if (bcmp(softc->digest, digest, 16) == 0)
62058b8a9b1dSJustin T. Gibbs 				changed = 0;
62068b8a9b1dSJustin T. Gibbs 
62078b8a9b1dSJustin T. Gibbs 			/*
62088b8a9b1dSJustin T. Gibbs 			 * XXX Do we need to do a TUR in order to ensure
62098b8a9b1dSJustin T. Gibbs 			 *     that the device really hasn't changed???
62108b8a9b1dSJustin T. Gibbs 			 */
62118b8a9b1dSJustin T. Gibbs 			if ((changed != 0)
62128b8a9b1dSJustin T. Gibbs 			 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
62138b8a9b1dSJustin T. Gibbs 				xpt_async(AC_LOST_DEVICE, path, NULL);
62148b8a9b1dSJustin T. Gibbs 		}
62158b8a9b1dSJustin T. Gibbs 		if (serial_buf != NULL)
62168b8a9b1dSJustin T. Gibbs 			free(serial_buf, M_TEMP);
62178b8a9b1dSJustin T. Gibbs 
62188b8a9b1dSJustin T. Gibbs 		if (changed != 0) {
62198b8a9b1dSJustin T. Gibbs 			/*
62208b8a9b1dSJustin T. Gibbs 			 * Now that we have all the necessary
62218b8a9b1dSJustin T. Gibbs 			 * information to safely perform transfer
62228b8a9b1dSJustin T. Gibbs 			 * negotiations... Controllers don't perform
62238b8a9b1dSJustin T. Gibbs 			 * any negotiation or tagged queuing until
62248b8a9b1dSJustin T. Gibbs 			 * after the first XPT_SET_TRAN_SETTINGS ccb is
62252afca7acSMatt Jacob 			 * received.  So, on a new device, just retrieve
62268b8a9b1dSJustin T. Gibbs 			 * the user settings, and set them as the current
62278b8a9b1dSJustin T. Gibbs 			 * settings to set the device up.
62288b8a9b1dSJustin T. Gibbs 			 */
622987cfaf0eSJustin T. Gibbs 			proberequestdefaultnegotiation(periph);
62308b8a9b1dSJustin T. Gibbs 			xpt_release_ccb(done_ccb);
62318b8a9b1dSJustin T. Gibbs 
62328b8a9b1dSJustin T. Gibbs 			/*
62338b8a9b1dSJustin T. Gibbs 			 * Perform a TUR to allow the controller to
62348b8a9b1dSJustin T. Gibbs 			 * perform any necessary transfer negotiation.
62358b8a9b1dSJustin T. Gibbs 			 */
62368b8a9b1dSJustin T. Gibbs 			softc->action = PROBE_TUR_FOR_NEGOTIATION;
62378b8a9b1dSJustin T. Gibbs 			xpt_schedule(periph, priority);
62388b8a9b1dSJustin T. Gibbs 			return;
62398b8a9b1dSJustin T. Gibbs 		}
62408b8a9b1dSJustin T. Gibbs 		xpt_release_ccb(done_ccb);
62418b8a9b1dSJustin T. Gibbs 		break;
62428b8a9b1dSJustin T. Gibbs 	}
62438b8a9b1dSJustin T. Gibbs 	case PROBE_TUR_FOR_NEGOTIATION:
62442afca7acSMatt Jacob 	case PROBE_DV_EXIT:
62458b8a9b1dSJustin T. Gibbs 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
62468b8a9b1dSJustin T. Gibbs 			/* Don't wedge the queue */
62472cefde5fSJustin T. Gibbs 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
62488b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
62498b8a9b1dSJustin T. Gibbs 		}
62502afca7acSMatt Jacob 		/*
62512afca7acSMatt Jacob 		 * Do Domain Validation for lun 0 on devices that claim
62522afca7acSMatt Jacob 		 * to support Synchronous Transfer modes.
62532afca7acSMatt Jacob 		 */
62542afca7acSMatt Jacob 	 	if (softc->action == PROBE_TUR_FOR_NEGOTIATION
62552afca7acSMatt Jacob 		 && done_ccb->ccb_h.target_lun == 0
62562afca7acSMatt Jacob 		 && (path->device->inq_data.flags & SID_Sync) != 0
62572afca7acSMatt Jacob                  && (path->device->flags & CAM_DEV_IN_DV) == 0) {
62582afca7acSMatt Jacob 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
62592afca7acSMatt Jacob 			    ("Begin Domain Validation\n"));
62602afca7acSMatt Jacob 			path->device->flags |= CAM_DEV_IN_DV;
62612afca7acSMatt Jacob 			xpt_release_ccb(done_ccb);
62622afca7acSMatt Jacob 			softc->action = PROBE_INQUIRY_BASIC_DV1;
62632afca7acSMatt Jacob 			xpt_schedule(periph, priority);
62642afca7acSMatt Jacob 			return;
62652afca7acSMatt Jacob 		}
62662afca7acSMatt Jacob 		if (softc->action == PROBE_DV_EXIT) {
62672afca7acSMatt Jacob 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
62682afca7acSMatt Jacob 			    ("Leave Domain Validation\n"));
62692afca7acSMatt Jacob 		}
62702afca7acSMatt Jacob 		path->device->flags &=
62712afca7acSMatt Jacob 		    ~(CAM_DEV_UNCONFIGURED|CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
62728b8a9b1dSJustin T. Gibbs 		if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
62738b8a9b1dSJustin T. Gibbs 			/* Inform the XPT that a new device has been found */
62748b8a9b1dSJustin T. Gibbs 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
62758b8a9b1dSJustin T. Gibbs 			xpt_action(done_ccb);
6276df8f9080SJustin T. Gibbs 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
6277df8f9080SJustin T. Gibbs 				  done_ccb);
62788b8a9b1dSJustin T. Gibbs 		}
62798b8a9b1dSJustin T. Gibbs 		xpt_release_ccb(done_ccb);
62808b8a9b1dSJustin T. Gibbs 		break;
62812afca7acSMatt Jacob 	case PROBE_INQUIRY_BASIC_DV1:
62822afca7acSMatt Jacob 	case PROBE_INQUIRY_BASIC_DV2:
62832afca7acSMatt Jacob 	{
62842afca7acSMatt Jacob 		struct scsi_inquiry_data *nbuf;
62852afca7acSMatt Jacob 		struct ccb_scsiio *csio;
62862afca7acSMatt Jacob 
62872afca7acSMatt Jacob 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
62882afca7acSMatt Jacob 			/* Don't wedge the queue */
62892afca7acSMatt Jacob 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
62902afca7acSMatt Jacob 					 /*run_queue*/TRUE);
62912afca7acSMatt Jacob 		}
62922afca7acSMatt Jacob 		csio = &done_ccb->csio;
62932afca7acSMatt Jacob 		nbuf = (struct scsi_inquiry_data *)csio->data_ptr;
62942afca7acSMatt Jacob 		if (bcmp(nbuf, &path->device->inq_data, SHORT_INQUIRY_LENGTH)) {
6295f0d9af51SMatt Jacob 			xpt_print(path,
6296f0d9af51SMatt Jacob 			    "inquiry data fails comparison at DV%d step\n",
62972afca7acSMatt Jacob 			    softc->action == PROBE_INQUIRY_BASIC_DV1? 1 : 2);
62982afca7acSMatt Jacob 			if (proberequestbackoff(periph, path->device)) {
62992afca7acSMatt Jacob 				path->device->flags &= ~CAM_DEV_IN_DV;
63002afca7acSMatt Jacob 				softc->action = PROBE_TUR_FOR_NEGOTIATION;
63012afca7acSMatt Jacob 			} else {
63022afca7acSMatt Jacob 				/* give up */
63032afca7acSMatt Jacob 				softc->action = PROBE_DV_EXIT;
63042afca7acSMatt Jacob 			}
63052afca7acSMatt Jacob 			free(nbuf, M_TEMP);
63062afca7acSMatt Jacob 			xpt_release_ccb(done_ccb);
63072afca7acSMatt Jacob 			xpt_schedule(periph, priority);
63082afca7acSMatt Jacob 			return;
63092afca7acSMatt Jacob 		}
63102afca7acSMatt Jacob 		free(nbuf, M_TEMP);
63112afca7acSMatt Jacob 		if (softc->action == PROBE_INQUIRY_BASIC_DV1) {
63122afca7acSMatt Jacob 			softc->action = PROBE_INQUIRY_BASIC_DV2;
63132afca7acSMatt Jacob 			xpt_release_ccb(done_ccb);
63142afca7acSMatt Jacob 			xpt_schedule(periph, priority);
63152afca7acSMatt Jacob 			return;
63162afca7acSMatt Jacob 		}
63172afca7acSMatt Jacob 		if (softc->action == PROBE_DV_EXIT) {
63182afca7acSMatt Jacob 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
63192afca7acSMatt Jacob 			    ("Leave Domain Validation Successfully\n"));
63202afca7acSMatt Jacob 		}
63212afca7acSMatt Jacob 		path->device->flags &=
63222afca7acSMatt Jacob 		    ~(CAM_DEV_UNCONFIGURED|CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
63232afca7acSMatt Jacob 		if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
63242afca7acSMatt Jacob 			/* Inform the XPT that a new device has been found */
63252afca7acSMatt Jacob 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
63262afca7acSMatt Jacob 			xpt_action(done_ccb);
63272afca7acSMatt Jacob 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
63282afca7acSMatt Jacob 				  done_ccb);
63292afca7acSMatt Jacob 		}
63302afca7acSMatt Jacob 		xpt_release_ccb(done_ccb);
63312afca7acSMatt Jacob 		break;
63322afca7acSMatt Jacob 	}
63338b8a9b1dSJustin T. Gibbs 	}
63348b8a9b1dSJustin T. Gibbs 	done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
63358b8a9b1dSJustin T. Gibbs 	TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
63368b8a9b1dSJustin T. Gibbs 	done_ccb->ccb_h.status = CAM_REQ_CMP;
63378b8a9b1dSJustin T. Gibbs 	xpt_done(done_ccb);
63388b8a9b1dSJustin T. Gibbs 	if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
63398b8a9b1dSJustin T. Gibbs 		cam_periph_invalidate(periph);
63408b8a9b1dSJustin T. Gibbs 		cam_periph_release(periph);
63418b8a9b1dSJustin T. Gibbs 	} else {
63428b8a9b1dSJustin T. Gibbs 		probeschedule(periph);
63438b8a9b1dSJustin T. Gibbs 	}
63448b8a9b1dSJustin T. Gibbs }
63458b8a9b1dSJustin T. Gibbs 
63468b8a9b1dSJustin T. Gibbs static void
63478b8a9b1dSJustin T. Gibbs probecleanup(struct cam_periph *periph)
63488b8a9b1dSJustin T. Gibbs {
63498b8a9b1dSJustin T. Gibbs 	free(periph->softc, M_TEMP);
63508b8a9b1dSJustin T. Gibbs }
63518b8a9b1dSJustin T. Gibbs 
63528b8a9b1dSJustin T. Gibbs static void
63538b8a9b1dSJustin T. Gibbs xpt_find_quirk(struct cam_ed *device)
63548b8a9b1dSJustin T. Gibbs {
63558b8a9b1dSJustin T. Gibbs 	caddr_t	match;
63568b8a9b1dSJustin T. Gibbs 
63578b8a9b1dSJustin T. Gibbs 	match = cam_quirkmatch((caddr_t)&device->inq_data,
63588b8a9b1dSJustin T. Gibbs 			       (caddr_t)xpt_quirk_table,
63598b8a9b1dSJustin T. Gibbs 			       sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
63608b8a9b1dSJustin T. Gibbs 			       sizeof(*xpt_quirk_table), scsi_inquiry_match);
63618b8a9b1dSJustin T. Gibbs 
63628b8a9b1dSJustin T. Gibbs 	if (match == NULL)
63638b8a9b1dSJustin T. Gibbs 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
63648b8a9b1dSJustin T. Gibbs 
63658b8a9b1dSJustin T. Gibbs 	device->quirk = (struct xpt_quirk_entry *)match;
63668b8a9b1dSJustin T. Gibbs }
63678b8a9b1dSJustin T. Gibbs 
63683b87a552SMatt Jacob static int
63693b87a552SMatt Jacob sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS)
63703b87a552SMatt Jacob {
63713b87a552SMatt Jacob 	int error, bool;
63723b87a552SMatt Jacob 
63733b87a552SMatt Jacob 	bool = cam_srch_hi;
63743b87a552SMatt Jacob 	error = sysctl_handle_int(oidp, &bool, sizeof(bool), req);
63753b87a552SMatt Jacob 	if (error != 0 || req->newptr == NULL)
63763b87a552SMatt Jacob 		return (error);
63773b87a552SMatt Jacob 	if (bool == 0 || bool == 1) {
63783b87a552SMatt Jacob 		cam_srch_hi = bool;
63793b87a552SMatt Jacob 		return (0);
63803b87a552SMatt Jacob 	} else {
63813b87a552SMatt Jacob 		return (EINVAL);
63823b87a552SMatt Jacob 	}
63833b87a552SMatt Jacob }
63843b87a552SMatt Jacob 
63853393f8daSKenneth D. Merry 
63863393f8daSKenneth D. Merry static void
63873393f8daSKenneth D. Merry xpt_devise_transport(struct cam_path *path)
63883393f8daSKenneth D. Merry {
63893393f8daSKenneth D. Merry 	struct ccb_pathinq cpi;
63903393f8daSKenneth D. Merry 	struct ccb_trans_settings cts;
63913393f8daSKenneth D. Merry 	struct scsi_inquiry_data *inq_buf;
63923393f8daSKenneth D. Merry 
63933393f8daSKenneth D. Merry 	/* Get transport information from the SIM */
63943393f8daSKenneth D. Merry 	xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
63953393f8daSKenneth D. Merry 	cpi.ccb_h.func_code = XPT_PATH_INQ;
63963393f8daSKenneth D. Merry 	xpt_action((union ccb *)&cpi);
63973393f8daSKenneth D. Merry 
63983393f8daSKenneth D. Merry 	inq_buf = NULL;
63993393f8daSKenneth D. Merry 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
64003393f8daSKenneth D. Merry 		inq_buf = &path->device->inq_data;
64013393f8daSKenneth D. Merry 	path->device->protocol = PROTO_SCSI;
64023393f8daSKenneth D. Merry 	path->device->protocol_version =
64033393f8daSKenneth D. Merry 	    inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
64043393f8daSKenneth D. Merry 	path->device->transport = cpi.transport;
64053393f8daSKenneth D. Merry 	path->device->transport_version = cpi.transport_version;
64063393f8daSKenneth D. Merry 
64073393f8daSKenneth D. Merry 	/*
64083393f8daSKenneth D. Merry 	 * Any device not using SPI3 features should
64093393f8daSKenneth D. Merry 	 * be considered SPI2 or lower.
64103393f8daSKenneth D. Merry 	 */
64113393f8daSKenneth D. Merry 	if (inq_buf != NULL) {
64123393f8daSKenneth D. Merry 		if (path->device->transport == XPORT_SPI
64133393f8daSKenneth D. Merry 		 && (inq_buf->spi3data & SID_SPI_MASK) == 0
64143393f8daSKenneth D. Merry 		 && path->device->transport_version > 2)
64153393f8daSKenneth D. Merry 			path->device->transport_version = 2;
64163393f8daSKenneth D. Merry 	} else {
64173393f8daSKenneth D. Merry 		struct cam_ed* otherdev;
64183393f8daSKenneth D. Merry 
64193393f8daSKenneth D. Merry 		for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
64203393f8daSKenneth D. Merry 		     otherdev != NULL;
64213393f8daSKenneth D. Merry 		     otherdev = TAILQ_NEXT(otherdev, links)) {
64223393f8daSKenneth D. Merry 			if (otherdev != path->device)
64233393f8daSKenneth D. Merry 				break;
64243393f8daSKenneth D. Merry 		}
64253393f8daSKenneth D. Merry 
64263393f8daSKenneth D. Merry 		if (otherdev != NULL) {
64273393f8daSKenneth D. Merry 			/*
64283393f8daSKenneth D. Merry 			 * Initially assume the same versioning as
64293393f8daSKenneth D. Merry 			 * prior luns for this target.
64303393f8daSKenneth D. Merry 			 */
64313393f8daSKenneth D. Merry 			path->device->protocol_version =
64323393f8daSKenneth D. Merry 			    otherdev->protocol_version;
64333393f8daSKenneth D. Merry 			path->device->transport_version =
64343393f8daSKenneth D. Merry 			    otherdev->transport_version;
64353393f8daSKenneth D. Merry 		} else {
64363393f8daSKenneth D. Merry 			/* Until we know better, opt for safty */
64373393f8daSKenneth D. Merry 			path->device->protocol_version = 2;
64383393f8daSKenneth D. Merry 			if (path->device->transport == XPORT_SPI)
64393393f8daSKenneth D. Merry 				path->device->transport_version = 2;
64403393f8daSKenneth D. Merry 			else
64413393f8daSKenneth D. Merry 				path->device->transport_version = 0;
64423393f8daSKenneth D. Merry 		}
64433393f8daSKenneth D. Merry 	}
64443393f8daSKenneth D. Merry 
64453393f8daSKenneth D. Merry 	/*
64463393f8daSKenneth D. Merry 	 * XXX
64473393f8daSKenneth D. Merry 	 * For a device compliant with SPC-2 we should be able
64483393f8daSKenneth D. Merry 	 * to determine the transport version supported by
64493393f8daSKenneth D. Merry 	 * scrutinizing the version descriptors in the
64503393f8daSKenneth D. Merry 	 * inquiry buffer.
64513393f8daSKenneth D. Merry 	 */
64523393f8daSKenneth D. Merry 
64533393f8daSKenneth D. Merry 	/* Tell the controller what we think */
64543393f8daSKenneth D. Merry 	xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
64553393f8daSKenneth D. Merry 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
64563393f8daSKenneth D. Merry 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
64573393f8daSKenneth D. Merry 	cts.transport = path->device->transport;
64583393f8daSKenneth D. Merry 	cts.transport_version = path->device->transport_version;
64593393f8daSKenneth D. Merry 	cts.protocol = path->device->protocol;
64603393f8daSKenneth D. Merry 	cts.protocol_version = path->device->protocol_version;
64613393f8daSKenneth D. Merry 	cts.proto_specific.valid = 0;
64623393f8daSKenneth D. Merry 	cts.xport_specific.valid = 0;
64633393f8daSKenneth D. Merry 	xpt_action((union ccb *)&cts);
64643393f8daSKenneth D. Merry }
64653393f8daSKenneth D. Merry 
64663393f8daSKenneth D. Merry static void
64673393f8daSKenneth D. Merry xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
64683393f8daSKenneth D. Merry 			  int async_update)
64693393f8daSKenneth D. Merry {
64703393f8daSKenneth D. Merry 	struct	ccb_pathinq cpi;
64713393f8daSKenneth D. Merry 	struct	ccb_trans_settings cur_cts;
64723393f8daSKenneth D. Merry 	struct	ccb_trans_settings_scsi *scsi;
64733393f8daSKenneth D. Merry 	struct	ccb_trans_settings_scsi *cur_scsi;
64743393f8daSKenneth D. Merry 	struct	cam_sim *sim;
64753393f8daSKenneth D. Merry 	struct	scsi_inquiry_data *inq_data;
64763393f8daSKenneth D. Merry 
64773393f8daSKenneth D. Merry 	if (device == NULL) {
64783393f8daSKenneth D. Merry 		cts->ccb_h.status = CAM_PATH_INVALID;
64793393f8daSKenneth D. Merry 		xpt_done((union ccb *)cts);
64803393f8daSKenneth D. Merry 		return;
64813393f8daSKenneth D. Merry 	}
64823393f8daSKenneth D. Merry 
64833393f8daSKenneth D. Merry 	if (cts->protocol == PROTO_UNKNOWN
64843393f8daSKenneth D. Merry 	 || cts->protocol == PROTO_UNSPECIFIED) {
64853393f8daSKenneth D. Merry 		cts->protocol = device->protocol;
64863393f8daSKenneth D. Merry 		cts->protocol_version = device->protocol_version;
64873393f8daSKenneth D. Merry 	}
64883393f8daSKenneth D. Merry 
64893393f8daSKenneth D. Merry 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
64903393f8daSKenneth D. Merry 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
64913393f8daSKenneth D. Merry 		cts->protocol_version = device->protocol_version;
64923393f8daSKenneth D. Merry 
64933393f8daSKenneth D. Merry 	if (cts->protocol != device->protocol) {
6494f0d9af51SMatt Jacob 		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
64953393f8daSKenneth D. Merry 		       cts->protocol, device->protocol);
64963393f8daSKenneth D. Merry 		cts->protocol = device->protocol;
64973393f8daSKenneth D. Merry 	}
64983393f8daSKenneth D. Merry 
64993393f8daSKenneth D. Merry 	if (cts->protocol_version > device->protocol_version) {
65003393f8daSKenneth D. Merry 		if (bootverbose) {
6501f0d9af51SMatt Jacob 			xpt_print(cts->ccb_h.path, "Down reving Protocol "
6502f0d9af51SMatt Jacob 			    "Version from %d to %d?\n", cts->protocol_version,
6503f0d9af51SMatt Jacob 			    device->protocol_version);
65043393f8daSKenneth D. Merry 		}
65053393f8daSKenneth D. Merry 		cts->protocol_version = device->protocol_version;
65063393f8daSKenneth D. Merry 	}
65073393f8daSKenneth D. Merry 
65083393f8daSKenneth D. Merry 	if (cts->transport == XPORT_UNKNOWN
65093393f8daSKenneth D. Merry 	 || cts->transport == XPORT_UNSPECIFIED) {
65103393f8daSKenneth D. Merry 		cts->transport = device->transport;
65113393f8daSKenneth D. Merry 		cts->transport_version = device->transport_version;
65123393f8daSKenneth D. Merry 	}
65133393f8daSKenneth D. Merry 
65143393f8daSKenneth D. Merry 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
65153393f8daSKenneth D. Merry 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
65163393f8daSKenneth D. Merry 		cts->transport_version = device->transport_version;
65173393f8daSKenneth D. Merry 
65183393f8daSKenneth D. Merry 	if (cts->transport != device->transport) {
6519f0d9af51SMatt Jacob 		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
65203393f8daSKenneth D. Merry 		    cts->transport, device->transport);
65213393f8daSKenneth D. Merry 		cts->transport = device->transport;
65223393f8daSKenneth D. Merry 	}
65233393f8daSKenneth D. Merry 
65243393f8daSKenneth D. Merry 	if (cts->transport_version > device->transport_version) {
65253393f8daSKenneth D. Merry 		if (bootverbose) {
6526f0d9af51SMatt Jacob 			xpt_print(cts->ccb_h.path, "Down reving Transport "
6527f0d9af51SMatt Jacob 			    "Version from %d to %d?\n", cts->transport_version,
65283393f8daSKenneth D. Merry 			    device->transport_version);
65293393f8daSKenneth D. Merry 		}
65303393f8daSKenneth D. Merry 		cts->transport_version = device->transport_version;
65313393f8daSKenneth D. Merry 	}
65323393f8daSKenneth D. Merry 
65333393f8daSKenneth D. Merry 	sim = cts->ccb_h.path->bus->sim;
65343393f8daSKenneth D. Merry 
65353393f8daSKenneth D. Merry 	/*
65363393f8daSKenneth D. Merry 	 * Nothing more of interest to do unless
65373393f8daSKenneth D. Merry 	 * this is a device connected via the
65383393f8daSKenneth D. Merry 	 * SCSI protocol.
65393393f8daSKenneth D. Merry 	 */
65403393f8daSKenneth D. Merry 	if (cts->protocol != PROTO_SCSI) {
65413393f8daSKenneth D. Merry 		if (async_update == FALSE)
65423393f8daSKenneth D. Merry 			(*(sim->sim_action))(sim, (union ccb *)cts);
65433393f8daSKenneth D. Merry 		return;
65443393f8daSKenneth D. Merry 	}
65453393f8daSKenneth D. Merry 
65463393f8daSKenneth D. Merry 	inq_data = &device->inq_data;
65473393f8daSKenneth D. Merry 	scsi = &cts->proto_specific.scsi;
65483393f8daSKenneth D. Merry 	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
65493393f8daSKenneth D. Merry 	cpi.ccb_h.func_code = XPT_PATH_INQ;
65503393f8daSKenneth D. Merry 	xpt_action((union ccb *)&cpi);
65513393f8daSKenneth D. Merry 
65523393f8daSKenneth D. Merry 	/* SCSI specific sanity checking */
65533393f8daSKenneth D. Merry 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
65544e359f5aSMatt Jacob 	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
65553393f8daSKenneth D. Merry 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
65563393f8daSKenneth D. Merry 	 || (device->quirk->mintags == 0)) {
65573393f8daSKenneth D. Merry 		/*
65583393f8daSKenneth D. Merry 		 * Can't tag on hardware that doesn't support tags,
65593393f8daSKenneth D. Merry 		 * doesn't have it enabled, or has broken tag support.
65603393f8daSKenneth D. Merry 		 */
65613393f8daSKenneth D. Merry 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
65623393f8daSKenneth D. Merry 	}
65633393f8daSKenneth D. Merry 
65643393f8daSKenneth D. Merry 	if (async_update == FALSE) {
65653393f8daSKenneth D. Merry 		/*
65663393f8daSKenneth D. Merry 		 * Perform sanity checking against what the
65673393f8daSKenneth D. Merry 		 * controller and device can do.
65683393f8daSKenneth D. Merry 		 */
65693393f8daSKenneth D. Merry 		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
65703393f8daSKenneth D. Merry 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
65713393f8daSKenneth D. Merry 		cur_cts.type = cts->type;
65723393f8daSKenneth D. Merry 		xpt_action((union ccb *)&cur_cts);
65730a480cf0SMatt Jacob 		if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
65740a480cf0SMatt Jacob 			return;
65750a480cf0SMatt Jacob 		}
65763393f8daSKenneth D. Merry 		cur_scsi = &cur_cts.proto_specific.scsi;
65773393f8daSKenneth D. Merry 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
65783393f8daSKenneth D. Merry 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
65793393f8daSKenneth D. Merry 			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
65803393f8daSKenneth D. Merry 		}
65813393f8daSKenneth D. Merry 		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
65823393f8daSKenneth D. Merry 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
65833393f8daSKenneth D. Merry 	}
65843393f8daSKenneth D. Merry 
65853393f8daSKenneth D. Merry 	/* SPI specific sanity checking */
6586f053d777SMatt Jacob 	if (cts->transport == XPORT_SPI && async_update == FALSE) {
65873393f8daSKenneth D. Merry 		u_int spi3caps;
65883393f8daSKenneth D. Merry 		struct ccb_trans_settings_spi *spi;
65893393f8daSKenneth D. Merry 		struct ccb_trans_settings_spi *cur_spi;
65903393f8daSKenneth D. Merry 
65913393f8daSKenneth D. Merry 		spi = &cts->xport_specific.spi;
65923393f8daSKenneth D. Merry 
65933393f8daSKenneth D. Merry 		cur_spi = &cur_cts.xport_specific.spi;
65943393f8daSKenneth D. Merry 
65953393f8daSKenneth D. Merry 		/* Fill in any gaps in what the user gave us */
65963393f8daSKenneth D. Merry 		if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
65973393f8daSKenneth D. Merry 			spi->sync_period = cur_spi->sync_period;
65983393f8daSKenneth D. Merry 		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
65993393f8daSKenneth D. Merry 			spi->sync_period = 0;
66003393f8daSKenneth D. Merry 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
66013393f8daSKenneth D. Merry 			spi->sync_offset = cur_spi->sync_offset;
66023393f8daSKenneth D. Merry 		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
66033393f8daSKenneth D. Merry 			spi->sync_offset = 0;
66043393f8daSKenneth D. Merry 		if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
66053393f8daSKenneth D. Merry 			spi->ppr_options = cur_spi->ppr_options;
66063393f8daSKenneth D. Merry 		if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
66073393f8daSKenneth D. Merry 			spi->ppr_options = 0;
66083393f8daSKenneth D. Merry 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
66093393f8daSKenneth D. Merry 			spi->bus_width = cur_spi->bus_width;
66103393f8daSKenneth D. Merry 		if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
66113393f8daSKenneth D. Merry 			spi->bus_width = 0;
66123393f8daSKenneth D. Merry 		if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
66133393f8daSKenneth D. Merry 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
66143393f8daSKenneth D. Merry 			spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
66153393f8daSKenneth D. Merry 		}
66163393f8daSKenneth D. Merry 		if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
66173393f8daSKenneth D. Merry 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
66183393f8daSKenneth D. Merry 		if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
66193393f8daSKenneth D. Merry 		  && (inq_data->flags & SID_Sync) == 0
66203393f8daSKenneth D. Merry 		  && cts->type == CTS_TYPE_CURRENT_SETTINGS)
66213393f8daSKenneth D. Merry 		 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6622c144ae3eSMatt Jacob 		 || (spi->sync_offset == 0)
6623c144ae3eSMatt Jacob 		 || (spi->sync_period == 0)) {
66243393f8daSKenneth D. Merry 			/* Force async */
66253393f8daSKenneth D. Merry 			spi->sync_period = 0;
66263393f8daSKenneth D. Merry 			spi->sync_offset = 0;
66273393f8daSKenneth D. Merry 		}
66283393f8daSKenneth D. Merry 
66293393f8daSKenneth D. Merry 		switch (spi->bus_width) {
66303393f8daSKenneth D. Merry 		case MSG_EXT_WDTR_BUS_32_BIT:
66313393f8daSKenneth D. Merry 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
66323393f8daSKenneth D. Merry 			  || (inq_data->flags & SID_WBus32) != 0
66333393f8daSKenneth D. Merry 			  || cts->type == CTS_TYPE_USER_SETTINGS)
66343393f8daSKenneth D. Merry 			 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
66353393f8daSKenneth D. Merry 				break;
66363393f8daSKenneth D. Merry 			/* Fall Through to 16-bit */
66373393f8daSKenneth D. Merry 		case MSG_EXT_WDTR_BUS_16_BIT:
66383393f8daSKenneth D. Merry 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
66393393f8daSKenneth D. Merry 			  || (inq_data->flags & SID_WBus16) != 0
66403393f8daSKenneth D. Merry 			  || cts->type == CTS_TYPE_USER_SETTINGS)
66413393f8daSKenneth D. Merry 			 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
66423393f8daSKenneth D. Merry 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
66433393f8daSKenneth D. Merry 				break;
66443393f8daSKenneth D. Merry 			}
66453393f8daSKenneth D. Merry 			/* Fall Through to 8-bit */
66463393f8daSKenneth D. Merry 		default: /* New bus width?? */
66473393f8daSKenneth D. Merry 		case MSG_EXT_WDTR_BUS_8_BIT:
66483393f8daSKenneth D. Merry 			/* All targets can do this */
66493393f8daSKenneth D. Merry 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
66503393f8daSKenneth D. Merry 			break;
66513393f8daSKenneth D. Merry 		}
66523393f8daSKenneth D. Merry 
66533393f8daSKenneth D. Merry 		spi3caps = cpi.xport_specific.spi.ppr_options;
66543393f8daSKenneth D. Merry 		if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
66553393f8daSKenneth D. Merry 		 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
66563393f8daSKenneth D. Merry 			spi3caps &= inq_data->spi3data;
66573393f8daSKenneth D. Merry 
66583393f8daSKenneth D. Merry 		if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
66593393f8daSKenneth D. Merry 			spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
66603393f8daSKenneth D. Merry 
66613393f8daSKenneth D. Merry 		if ((spi3caps & SID_SPI_IUS) == 0)
66623393f8daSKenneth D. Merry 			spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
66633393f8daSKenneth D. Merry 
66643393f8daSKenneth D. Merry 		if ((spi3caps & SID_SPI_QAS) == 0)
66653393f8daSKenneth D. Merry 			spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
66663393f8daSKenneth D. Merry 
66673393f8daSKenneth D. Merry 		/* No SPI Transfer settings are allowed unless we are wide */
66683393f8daSKenneth D. Merry 		if (spi->bus_width == 0)
66693393f8daSKenneth D. Merry 			spi->ppr_options = 0;
66703393f8daSKenneth D. Merry 
66713393f8daSKenneth D. Merry 		if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0) {
66723393f8daSKenneth D. Merry 			/*
66733393f8daSKenneth D. Merry 			 * Can't tag queue without disconnection.
66743393f8daSKenneth D. Merry 			 */
66753393f8daSKenneth D. Merry 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
66763393f8daSKenneth D. Merry 			scsi->valid |= CTS_SCSI_VALID_TQ;
66773393f8daSKenneth D. Merry 		}
66783393f8daSKenneth D. Merry 
66793393f8daSKenneth D. Merry 		/*
66803393f8daSKenneth D. Merry 		 * If we are currently performing tagged transactions to
66813393f8daSKenneth D. Merry 		 * this device and want to change its negotiation parameters,
66823393f8daSKenneth D. Merry 		 * go non-tagged for a bit to give the controller a chance to
66833393f8daSKenneth D. Merry 		 * negotiate unhampered by tag messages.
66843393f8daSKenneth D. Merry 		 */
66853393f8daSKenneth D. Merry 		if (cts->type == CTS_TYPE_CURRENT_SETTINGS
66863393f8daSKenneth D. Merry 		 && (device->inq_flags & SID_CmdQue) != 0
66873393f8daSKenneth D. Merry 		 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
66883393f8daSKenneth D. Merry 		 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
66893393f8daSKenneth D. Merry 				   CTS_SPI_VALID_SYNC_OFFSET|
66903393f8daSKenneth D. Merry 				   CTS_SPI_VALID_BUS_WIDTH)) != 0)
66913393f8daSKenneth D. Merry 			xpt_toggle_tags(cts->ccb_h.path);
66923393f8daSKenneth D. Merry 	}
66933393f8daSKenneth D. Merry 
66943393f8daSKenneth D. Merry 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
66953393f8daSKenneth D. Merry 	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
66963393f8daSKenneth D. Merry 		int device_tagenb;
66973393f8daSKenneth D. Merry 
66983393f8daSKenneth D. Merry 		/*
66993393f8daSKenneth D. Merry 		 * If we are transitioning from tags to no-tags or
67003393f8daSKenneth D. Merry 		 * vice-versa, we need to carefully freeze and restart
67013393f8daSKenneth D. Merry 		 * the queue so that we don't overlap tagged and non-tagged
67023393f8daSKenneth D. Merry 		 * commands.  We also temporarily stop tags if there is
67033393f8daSKenneth D. Merry 		 * a change in transfer negotiation settings to allow
67043393f8daSKenneth D. Merry 		 * "tag-less" negotiation.
67053393f8daSKenneth D. Merry 		 */
67063393f8daSKenneth D. Merry 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
67073393f8daSKenneth D. Merry 		 || (device->inq_flags & SID_CmdQue) != 0)
67083393f8daSKenneth D. Merry 			device_tagenb = TRUE;
67093393f8daSKenneth D. Merry 		else
67103393f8daSKenneth D. Merry 			device_tagenb = FALSE;
67113393f8daSKenneth D. Merry 
67123393f8daSKenneth D. Merry 		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
67133393f8daSKenneth D. Merry 		  && device_tagenb == FALSE)
67143393f8daSKenneth D. Merry 		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
67153393f8daSKenneth D. Merry 		  && device_tagenb == TRUE)) {
67163393f8daSKenneth D. Merry 
67173393f8daSKenneth D. Merry 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
67183393f8daSKenneth D. Merry 				/*
67193393f8daSKenneth D. Merry 				 * Delay change to use tags until after a
67203393f8daSKenneth D. Merry 				 * few commands have gone to this device so
67213393f8daSKenneth D. Merry 				 * the controller has time to perform transfer
67223393f8daSKenneth D. Merry 				 * negotiations without tagged messages getting
67233393f8daSKenneth D. Merry 				 * in the way.
67243393f8daSKenneth D. Merry 				 */
67253393f8daSKenneth D. Merry 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
67263393f8daSKenneth D. Merry 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
67273393f8daSKenneth D. Merry 			} else {
67283393f8daSKenneth D. Merry 				struct ccb_relsim crs;
67293393f8daSKenneth D. Merry 
67303393f8daSKenneth D. Merry 				xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
67313393f8daSKenneth D. Merry 		  		device->inq_flags &= ~SID_CmdQue;
67323393f8daSKenneth D. Merry 				xpt_dev_ccbq_resize(cts->ccb_h.path,
67333393f8daSKenneth D. Merry 						    sim->max_dev_openings);
67343393f8daSKenneth D. Merry 				device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
67353393f8daSKenneth D. Merry 				device->tag_delay_count = 0;
67363393f8daSKenneth D. Merry 
67373393f8daSKenneth D. Merry 				xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
67383393f8daSKenneth D. Merry 					      /*priority*/1);
67393393f8daSKenneth D. Merry 				crs.ccb_h.func_code = XPT_REL_SIMQ;
67403393f8daSKenneth D. Merry 				crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
67413393f8daSKenneth D. Merry 				crs.openings
67423393f8daSKenneth D. Merry 				    = crs.release_timeout
67433393f8daSKenneth D. Merry 				    = crs.qfrozen_cnt
67443393f8daSKenneth D. Merry 				    = 0;
67453393f8daSKenneth D. Merry 				xpt_action((union ccb *)&crs);
67463393f8daSKenneth D. Merry 			}
67473393f8daSKenneth D. Merry 		}
67483393f8daSKenneth D. Merry 	}
67493393f8daSKenneth D. Merry 	if (async_update == FALSE)
67503393f8daSKenneth D. Merry 		(*(sim->sim_action))(sim, (union ccb *)cts);
67513393f8daSKenneth D. Merry }
67523393f8daSKenneth D. Merry 
67533393f8daSKenneth D. Merry 
6754fd21cc5eSJustin T. Gibbs static void
6755f0adc790SJustin T. Gibbs xpt_toggle_tags(struct cam_path *path)
6756f0adc790SJustin T. Gibbs {
675787cfaf0eSJustin T. Gibbs 	struct cam_ed *dev;
675887cfaf0eSJustin T. Gibbs 
6759f0adc790SJustin T. Gibbs 	/*
6760f0adc790SJustin T. Gibbs 	 * Give controllers a chance to renegotiate
6761f0adc790SJustin T. Gibbs 	 * before starting tag operations.  We
6762f0adc790SJustin T. Gibbs 	 * "toggle" tagged queuing off then on
6763f0adc790SJustin T. Gibbs 	 * which causes the tag enable command delay
6764f0adc790SJustin T. Gibbs 	 * counter to come into effect.
6765f0adc790SJustin T. Gibbs 	 */
676687cfaf0eSJustin T. Gibbs 	dev = path->device;
676787cfaf0eSJustin T. Gibbs 	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
676887cfaf0eSJustin T. Gibbs 	 || ((dev->inq_flags & SID_CmdQue) != 0
676987cfaf0eSJustin T. Gibbs  	  && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
6770f0adc790SJustin T. Gibbs 		struct ccb_trans_settings cts;
6771f0adc790SJustin T. Gibbs 
6772f0adc790SJustin T. Gibbs 		xpt_setup_ccb(&cts.ccb_h, path, 1);
67733393f8daSKenneth D. Merry 		cts.protocol = PROTO_SCSI;
67743393f8daSKenneth D. Merry 		cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
67753393f8daSKenneth D. Merry 		cts.transport = XPORT_UNSPECIFIED;
67763393f8daSKenneth D. Merry 		cts.transport_version = XPORT_VERSION_UNSPECIFIED;
67773393f8daSKenneth D. Merry 		cts.proto_specific.scsi.flags = 0;
67783393f8daSKenneth D. Merry 		cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
677903e3511bSJustin T. Gibbs 		xpt_set_transfer_settings(&cts, path->device,
678003e3511bSJustin T. Gibbs 					  /*async_update*/TRUE);
67813393f8daSKenneth D. Merry 		cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
678203e3511bSJustin T. Gibbs 		xpt_set_transfer_settings(&cts, path->device,
678303e3511bSJustin T. Gibbs 					  /*async_update*/TRUE);
6784f0adc790SJustin T. Gibbs 	}
6785f0adc790SJustin T. Gibbs }
6786f0adc790SJustin T. Gibbs 
6787f0adc790SJustin T. Gibbs static void
6788f0adc790SJustin T. Gibbs xpt_start_tags(struct cam_path *path)
6789f0adc790SJustin T. Gibbs {
6790fd21cc5eSJustin T. Gibbs 	struct ccb_relsim crs;
6791fd21cc5eSJustin T. Gibbs 	struct cam_ed *device;
6792fd21cc5eSJustin T. Gibbs 	struct cam_sim *sim;
6793fd21cc5eSJustin T. Gibbs 	int    newopenings;
6794fd21cc5eSJustin T. Gibbs 
6795fd21cc5eSJustin T. Gibbs 	device = path->device;
6796fd21cc5eSJustin T. Gibbs 	sim = path->bus->sim;
6797fd21cc5eSJustin T. Gibbs 	device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6798fd21cc5eSJustin T. Gibbs 	xpt_freeze_devq(path, /*count*/1);
6799fd21cc5eSJustin T. Gibbs 	device->inq_flags |= SID_CmdQue;
6800df8f9080SJustin T. Gibbs 	if (device->tag_saved_openings != 0)
6801df8f9080SJustin T. Gibbs 		newopenings = device->tag_saved_openings;
6802df8f9080SJustin T. Gibbs 	else
6803df8f9080SJustin T. Gibbs 		newopenings = min(device->quirk->maxtags,
6804df8f9080SJustin T. Gibbs 				  sim->max_tagged_dev_openings);
6805f0adc790SJustin T. Gibbs 	xpt_dev_ccbq_resize(path, newopenings);
6806fd21cc5eSJustin T. Gibbs 	xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6807fd21cc5eSJustin T. Gibbs 	crs.ccb_h.func_code = XPT_REL_SIMQ;
6808fd21cc5eSJustin T. Gibbs 	crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6809fd21cc5eSJustin T. Gibbs 	crs.openings
6810fd21cc5eSJustin T. Gibbs 	    = crs.release_timeout
6811fd21cc5eSJustin T. Gibbs 	    = crs.qfrozen_cnt
6812fd21cc5eSJustin T. Gibbs 	    = 0;
6813fd21cc5eSJustin T. Gibbs 	xpt_action((union ccb *)&crs);
6814fd21cc5eSJustin T. Gibbs }
6815fd21cc5eSJustin T. Gibbs 
68168b8a9b1dSJustin T. Gibbs static int busses_to_config;
681787cfaf0eSJustin T. Gibbs static int busses_to_reset;
68188b8a9b1dSJustin T. Gibbs 
68198b8a9b1dSJustin T. Gibbs static int
68208b8a9b1dSJustin T. Gibbs xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
68218b8a9b1dSJustin T. Gibbs {
682287cfaf0eSJustin T. Gibbs 	if (bus->path_id != CAM_XPT_PATH_ID) {
682387cfaf0eSJustin T. Gibbs 		struct cam_path path;
682487cfaf0eSJustin T. Gibbs 		struct ccb_pathinq cpi;
682587cfaf0eSJustin T. Gibbs 		int can_negotiate;
682687cfaf0eSJustin T. Gibbs 
68278b8a9b1dSJustin T. Gibbs 		busses_to_config++;
682887cfaf0eSJustin T. Gibbs 		xpt_compile_path(&path, NULL, bus->path_id,
682987cfaf0eSJustin T. Gibbs 				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
683087cfaf0eSJustin T. Gibbs 		xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
683187cfaf0eSJustin T. Gibbs 		cpi.ccb_h.func_code = XPT_PATH_INQ;
683287cfaf0eSJustin T. Gibbs 		xpt_action((union ccb *)&cpi);
683387cfaf0eSJustin T. Gibbs 		can_negotiate = cpi.hba_inquiry;
683487cfaf0eSJustin T. Gibbs 		can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
683587cfaf0eSJustin T. Gibbs 		if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
683687cfaf0eSJustin T. Gibbs 		 && can_negotiate)
683787cfaf0eSJustin T. Gibbs 			busses_to_reset++;
683887cfaf0eSJustin T. Gibbs 		xpt_release_path(&path);
683987cfaf0eSJustin T. Gibbs 	}
68408b8a9b1dSJustin T. Gibbs 
68418b8a9b1dSJustin T. Gibbs 	return(1);
68428b8a9b1dSJustin T. Gibbs }
68438b8a9b1dSJustin T. Gibbs 
68448b8a9b1dSJustin T. Gibbs static int
68458b8a9b1dSJustin T. Gibbs xptconfigfunc(struct cam_eb *bus, void *arg)
68468b8a9b1dSJustin T. Gibbs {
68478b8a9b1dSJustin T. Gibbs 	struct	cam_path *path;
68488b8a9b1dSJustin T. Gibbs 	union	ccb *work_ccb;
68498b8a9b1dSJustin T. Gibbs 
68508b8a9b1dSJustin T. Gibbs 	if (bus->path_id != CAM_XPT_PATH_ID) {
68518b8a9b1dSJustin T. Gibbs 		cam_status status;
685287cfaf0eSJustin T. Gibbs 		int can_negotiate;
68538b8a9b1dSJustin T. Gibbs 
68548b8a9b1dSJustin T. Gibbs 		work_ccb = xpt_alloc_ccb();
68558b8a9b1dSJustin T. Gibbs 		if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
68568b8a9b1dSJustin T. Gibbs 					      CAM_TARGET_WILDCARD,
68578b8a9b1dSJustin T. Gibbs 					      CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
68588b8a9b1dSJustin T. Gibbs 			printf("xptconfigfunc: xpt_create_path failed with "
68598b8a9b1dSJustin T. Gibbs 			       "status %#x for bus %d\n", status, bus->path_id);
68608b8a9b1dSJustin T. Gibbs 			printf("xptconfigfunc: halting bus configuration\n");
68618b8a9b1dSJustin T. Gibbs 			xpt_free_ccb(work_ccb);
686298192658SJustin T. Gibbs 			busses_to_config--;
686398192658SJustin T. Gibbs 			xpt_finishconfig(xpt_periph, NULL);
68648b8a9b1dSJustin T. Gibbs 			return(0);
68658b8a9b1dSJustin T. Gibbs 		}
68668b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
686798192658SJustin T. Gibbs 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
686898192658SJustin T. Gibbs 		xpt_action(work_ccb);
686998192658SJustin T. Gibbs 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
687098192658SJustin T. Gibbs 			printf("xptconfigfunc: CPI failed on bus %d "
687198192658SJustin T. Gibbs 			       "with status %d\n", bus->path_id,
687298192658SJustin T. Gibbs 			       work_ccb->ccb_h.status);
687398192658SJustin T. Gibbs 			xpt_finishconfig(xpt_periph, work_ccb);
687498192658SJustin T. Gibbs 			return(1);
687598192658SJustin T. Gibbs 		}
687698192658SJustin T. Gibbs 
687787cfaf0eSJustin T. Gibbs 		can_negotiate = work_ccb->cpi.hba_inquiry;
687887cfaf0eSJustin T. Gibbs 		can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
687987cfaf0eSJustin T. Gibbs 		if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
688087cfaf0eSJustin T. Gibbs 		 && (can_negotiate != 0)) {
688198192658SJustin T. Gibbs 			xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
68828b8a9b1dSJustin T. Gibbs 			work_ccb->ccb_h.func_code = XPT_RESET_BUS;
68838b8a9b1dSJustin T. Gibbs 			work_ccb->ccb_h.cbfcnp = NULL;
68848b8a9b1dSJustin T. Gibbs 			CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
68858b8a9b1dSJustin T. Gibbs 				  ("Resetting Bus\n"));
68868b8a9b1dSJustin T. Gibbs 			xpt_action(work_ccb);
68878b8a9b1dSJustin T. Gibbs 			xpt_finishconfig(xpt_periph, work_ccb);
688898192658SJustin T. Gibbs 		} else {
688998192658SJustin T. Gibbs 			/* Act as though we performed a successful BUS RESET */
689098192658SJustin T. Gibbs 			work_ccb->ccb_h.func_code = XPT_RESET_BUS;
689198192658SJustin T. Gibbs 			xpt_finishconfig(xpt_periph, work_ccb);
689298192658SJustin T. Gibbs 		}
68938b8a9b1dSJustin T. Gibbs 	}
68948b8a9b1dSJustin T. Gibbs 
68958b8a9b1dSJustin T. Gibbs 	return(1);
68968b8a9b1dSJustin T. Gibbs }
68978b8a9b1dSJustin T. Gibbs 
68988b8a9b1dSJustin T. Gibbs static void
68998b8a9b1dSJustin T. Gibbs xpt_config(void *arg)
69008b8a9b1dSJustin T. Gibbs {
69013393f8daSKenneth D. Merry 	/*
69023393f8daSKenneth D. Merry 	 * Now that interrupts are enabled, go find our devices
69033393f8daSKenneth D. Merry 	 */
69048b8a9b1dSJustin T. Gibbs 
69058b8a9b1dSJustin T. Gibbs #ifdef CAMDEBUG
69068b8a9b1dSJustin T. Gibbs 	/* Setup debugging flags and path */
69078b8a9b1dSJustin T. Gibbs #ifdef CAM_DEBUG_FLAGS
69088b8a9b1dSJustin T. Gibbs 	cam_dflags = CAM_DEBUG_FLAGS;
69098b8a9b1dSJustin T. Gibbs #else /* !CAM_DEBUG_FLAGS */
69108b8a9b1dSJustin T. Gibbs 	cam_dflags = CAM_DEBUG_NONE;
69118b8a9b1dSJustin T. Gibbs #endif /* CAM_DEBUG_FLAGS */
69128b8a9b1dSJustin T. Gibbs #ifdef CAM_DEBUG_BUS
69138b8a9b1dSJustin T. Gibbs 	if (cam_dflags != CAM_DEBUG_NONE) {
69148b8a9b1dSJustin T. Gibbs 		if (xpt_create_path(&cam_dpath, xpt_periph,
69158b8a9b1dSJustin T. Gibbs 				    CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
69168b8a9b1dSJustin T. Gibbs 				    CAM_DEBUG_LUN) != CAM_REQ_CMP) {
69178b8a9b1dSJustin T. Gibbs 			printf("xpt_config: xpt_create_path() failed for debug"
69188b8a9b1dSJustin T. Gibbs 			       " target %d:%d:%d, debugging disabled\n",
69198b8a9b1dSJustin T. Gibbs 			       CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
69208b8a9b1dSJustin T. Gibbs 			cam_dflags = CAM_DEBUG_NONE;
69218b8a9b1dSJustin T. Gibbs 		}
69228b8a9b1dSJustin T. Gibbs 	} else
69238b8a9b1dSJustin T. Gibbs 		cam_dpath = NULL;
69248b8a9b1dSJustin T. Gibbs #else /* !CAM_DEBUG_BUS */
69258b8a9b1dSJustin T. Gibbs 	cam_dpath = NULL;
69268b8a9b1dSJustin T. Gibbs #endif /* CAM_DEBUG_BUS */
69278b8a9b1dSJustin T. Gibbs #endif /* CAMDEBUG */
69288b8a9b1dSJustin T. Gibbs 
6929a5479bc5SJustin T. Gibbs 	/*
6930a5479bc5SJustin T. Gibbs 	 * Scan all installed busses.
6931a5479bc5SJustin T. Gibbs 	 */
69328b8a9b1dSJustin T. Gibbs 	xpt_for_all_busses(xptconfigbuscountfunc, NULL);
69338b8a9b1dSJustin T. Gibbs 
6934ecdf1113SJustin T. Gibbs 	if (busses_to_config == 0) {
6935ecdf1113SJustin T. Gibbs 		/* Call manually because we don't have any busses */
69368b8a9b1dSJustin T. Gibbs 		xpt_finishconfig(xpt_periph, NULL);
69372863f7b1SJustin T. Gibbs 	} else  {
69383a937198SBrooks Davis 		if (busses_to_reset > 0 && scsi_delay >= 2000) {
69392863f7b1SJustin T. Gibbs 			printf("Waiting %d seconds for SCSI "
69403a937198SBrooks Davis 			       "devices to settle\n", scsi_delay/1000);
69412863f7b1SJustin T. Gibbs 		}
6942ecdf1113SJustin T. Gibbs 		xpt_for_all_busses(xptconfigfunc, NULL);
69438b8a9b1dSJustin T. Gibbs 	}
69442863f7b1SJustin T. Gibbs }
69458b8a9b1dSJustin T. Gibbs 
69468b8a9b1dSJustin T. Gibbs /*
69478b8a9b1dSJustin T. Gibbs  * If the given device only has one peripheral attached to it, and if that
69488b8a9b1dSJustin T. Gibbs  * peripheral is the passthrough driver, announce it.  This insures that the
69498b8a9b1dSJustin T. Gibbs  * user sees some sort of announcement for every peripheral in their system.
69508b8a9b1dSJustin T. Gibbs  */
69518b8a9b1dSJustin T. Gibbs static int
69528b8a9b1dSJustin T. Gibbs xptpassannouncefunc(struct cam_ed *device, void *arg)
69538b8a9b1dSJustin T. Gibbs {
69548b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph;
69558b8a9b1dSJustin T. Gibbs 	int i;
69568b8a9b1dSJustin T. Gibbs 
69578b8a9b1dSJustin T. Gibbs 	for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
69588b8a9b1dSJustin T. Gibbs 	     periph = SLIST_NEXT(periph, periph_links), i++);
69598b8a9b1dSJustin T. Gibbs 
69608b8a9b1dSJustin T. Gibbs 	periph = SLIST_FIRST(&device->periphs);
69618b8a9b1dSJustin T. Gibbs 	if ((i == 1)
69628b8a9b1dSJustin T. Gibbs 	 && (strncmp(periph->periph_name, "pass", 4) == 0))
69638b8a9b1dSJustin T. Gibbs 		xpt_announce_periph(periph, NULL);
69648b8a9b1dSJustin T. Gibbs 
69658b8a9b1dSJustin T. Gibbs 	return(1);
69668b8a9b1dSJustin T. Gibbs }
69678b8a9b1dSJustin T. Gibbs 
69688b8a9b1dSJustin T. Gibbs static void
69698b8a9b1dSJustin T. Gibbs xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
69708b8a9b1dSJustin T. Gibbs {
69718b8a9b1dSJustin T. Gibbs 	struct	periph_driver **p_drv;
69728b8a9b1dSJustin T. Gibbs 	int	i;
69738b8a9b1dSJustin T. Gibbs 
69748b8a9b1dSJustin T. Gibbs 	if (done_ccb != NULL) {
69758b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
69768b8a9b1dSJustin T. Gibbs 			  ("xpt_finishconfig\n"));
69778b8a9b1dSJustin T. Gibbs 		switch(done_ccb->ccb_h.func_code) {
69788b8a9b1dSJustin T. Gibbs 		case XPT_RESET_BUS:
69798b8a9b1dSJustin T. Gibbs 			if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
69808b8a9b1dSJustin T. Gibbs 				done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
69818b8a9b1dSJustin T. Gibbs 				done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
6982df8f9080SJustin T. Gibbs 				done_ccb->crcn.flags = 0;
69838b8a9b1dSJustin T. Gibbs 				xpt_action(done_ccb);
69848b8a9b1dSJustin T. Gibbs 				return;
69858b8a9b1dSJustin T. Gibbs 			}
69868b8a9b1dSJustin T. Gibbs 			/* FALLTHROUGH */
69878b8a9b1dSJustin T. Gibbs 		case XPT_SCAN_BUS:
698898192658SJustin T. Gibbs 		default:
69898b8a9b1dSJustin T. Gibbs 			xpt_free_path(done_ccb->ccb_h.path);
69908b8a9b1dSJustin T. Gibbs 			busses_to_config--;
69918b8a9b1dSJustin T. Gibbs 			break;
69928b8a9b1dSJustin T. Gibbs 		}
69938b8a9b1dSJustin T. Gibbs 	}
69948b8a9b1dSJustin T. Gibbs 
69958b8a9b1dSJustin T. Gibbs 	if (busses_to_config == 0) {
69968b8a9b1dSJustin T. Gibbs 		/* Register all the peripheral drivers */
6997e9189611SPeter Wemm 		/* XXX This will have to change when we have loadable modules */
69980b7c27b9SPeter Wemm 		p_drv = periph_drivers;
69998b8a9b1dSJustin T. Gibbs 		for (i = 0; p_drv[i] != NULL; i++) {
70008b8a9b1dSJustin T. Gibbs 			(*p_drv[i]->init)();
70018b8a9b1dSJustin T. Gibbs 		}
70028b8a9b1dSJustin T. Gibbs 
70038b8a9b1dSJustin T. Gibbs 		/*
70048b8a9b1dSJustin T. Gibbs 		 * Check for devices with no "standard" peripheral driver
70058b8a9b1dSJustin T. Gibbs 		 * attached.  For any devices like that, announce the
70068b8a9b1dSJustin T. Gibbs 		 * passthrough driver so the user will see something.
70078b8a9b1dSJustin T. Gibbs 		 */
70088b8a9b1dSJustin T. Gibbs 		xpt_for_all_devices(xptpassannouncefunc, NULL);
70098b8a9b1dSJustin T. Gibbs 
70108b8a9b1dSJustin T. Gibbs 		/* Release our hook so that the boot can continue. */
70118b8a9b1dSJustin T. Gibbs 		config_intrhook_disestablish(xpt_config_hook);
70129dd03ecfSJustin T. Gibbs 		free(xpt_config_hook, M_TEMP);
70139dd03ecfSJustin T. Gibbs 		xpt_config_hook = NULL;
70148b8a9b1dSJustin T. Gibbs 	}
70158b8a9b1dSJustin T. Gibbs 	if (done_ccb != NULL)
70168b8a9b1dSJustin T. Gibbs 		xpt_free_ccb(done_ccb);
70178b8a9b1dSJustin T. Gibbs }
70188b8a9b1dSJustin T. Gibbs 
70198b8a9b1dSJustin T. Gibbs static void
70208b8a9b1dSJustin T. Gibbs xptaction(struct cam_sim *sim, union ccb *work_ccb)
70218b8a9b1dSJustin T. Gibbs {
70228b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
70238b8a9b1dSJustin T. Gibbs 
70248b8a9b1dSJustin T. Gibbs 	switch (work_ccb->ccb_h.func_code) {
70258b8a9b1dSJustin T. Gibbs 	/* Common cases first */
70268b8a9b1dSJustin T. Gibbs 	case XPT_PATH_INQ:		/* Path routing inquiry */
70278b8a9b1dSJustin T. Gibbs 	{
70288b8a9b1dSJustin T. Gibbs 		struct ccb_pathinq *cpi;
70298b8a9b1dSJustin T. Gibbs 
70308b8a9b1dSJustin T. Gibbs 		cpi = &work_ccb->cpi;
70318b8a9b1dSJustin T. Gibbs 		cpi->version_num = 1; /* XXX??? */
70328b8a9b1dSJustin T. Gibbs 		cpi->hba_inquiry = 0;
70338b8a9b1dSJustin T. Gibbs 		cpi->target_sprt = 0;
70348b8a9b1dSJustin T. Gibbs 		cpi->hba_misc = 0;
70358b8a9b1dSJustin T. Gibbs 		cpi->hba_eng_cnt = 0;
70368b8a9b1dSJustin T. Gibbs 		cpi->max_target = 0;
70378b8a9b1dSJustin T. Gibbs 		cpi->max_lun = 0;
70388b8a9b1dSJustin T. Gibbs 		cpi->initiator_id = 0;
70398b8a9b1dSJustin T. Gibbs 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
70408b8a9b1dSJustin T. Gibbs 		strncpy(cpi->hba_vid, "", HBA_IDLEN);
70418b8a9b1dSJustin T. Gibbs 		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
70428b8a9b1dSJustin T. Gibbs 		cpi->unit_number = sim->unit_number;
70438b8a9b1dSJustin T. Gibbs 		cpi->bus_id = sim->bus_id;
70449deea857SKenneth D. Merry 		cpi->base_transfer_speed = 0;
70453393f8daSKenneth D. Merry 		cpi->protocol = PROTO_UNSPECIFIED;
70463393f8daSKenneth D. Merry 		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
70473393f8daSKenneth D. Merry 		cpi->transport = XPORT_UNSPECIFIED;
70483393f8daSKenneth D. Merry 		cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
70498b8a9b1dSJustin T. Gibbs 		cpi->ccb_h.status = CAM_REQ_CMP;
70508b8a9b1dSJustin T. Gibbs 		xpt_done(work_ccb);
70518b8a9b1dSJustin T. Gibbs 		break;
70528b8a9b1dSJustin T. Gibbs 	}
70538b8a9b1dSJustin T. Gibbs 	default:
70548b8a9b1dSJustin T. Gibbs 		work_ccb->ccb_h.status = CAM_REQ_INVALID;
70558b8a9b1dSJustin T. Gibbs 		xpt_done(work_ccb);
70568b8a9b1dSJustin T. Gibbs 		break;
70578b8a9b1dSJustin T. Gibbs 	}
70588b8a9b1dSJustin T. Gibbs }
70598b8a9b1dSJustin T. Gibbs 
70608b8a9b1dSJustin T. Gibbs /*
7061434bbf6eSJustin T. Gibbs  * The xpt as a "controller" has no interrupt sources, so polling
7062434bbf6eSJustin T. Gibbs  * is a no-op.
7063434bbf6eSJustin T. Gibbs  */
7064434bbf6eSJustin T. Gibbs static void
7065434bbf6eSJustin T. Gibbs xptpoll(struct cam_sim *sim)
7066434bbf6eSJustin T. Gibbs {
7067434bbf6eSJustin T. Gibbs }
7068434bbf6eSJustin T. Gibbs 
70695d754af7SMatt Jacob static void
70708088699fSJohn Baldwin camisr(void *V_queue)
70718b8a9b1dSJustin T. Gibbs {
7072ef3cf714SScott Long 	cam_isrq_t *oqueue = V_queue;
7073ef3cf714SScott Long 	cam_isrq_t queue;
70748b8a9b1dSJustin T. Gibbs 	int	s;
70758b8a9b1dSJustin T. Gibbs 	struct	ccb_hdr *ccb_h;
70768b8a9b1dSJustin T. Gibbs 
7077ef3cf714SScott Long 	/*
7078ef3cf714SScott Long 	 * Transfer the ccb_bioq list to a temporary list so we can operate
7079ef3cf714SScott Long 	 * on it without needing to lock/unlock on every loop.  The concat
7080ef3cf714SScott Long 	 * function with re-init the real list for us.
7081ef3cf714SScott Long 	 */
70828b8a9b1dSJustin T. Gibbs 	s = splcam();
7083ef3cf714SScott Long 	mtx_lock(&cam_bioq_lock);
7084ef3cf714SScott Long 	TAILQ_INIT(&queue);
7085ef3cf714SScott Long 	TAILQ_CONCAT(&queue, oqueue, sim_links.tqe);
7086ef3cf714SScott Long 	mtx_unlock(&cam_bioq_lock);
7087ef3cf714SScott Long 
7088ef3cf714SScott Long 	while ((ccb_h = TAILQ_FIRST(&queue)) != NULL) {
70898b8a9b1dSJustin T. Gibbs 		int	runq;
70908b8a9b1dSJustin T. Gibbs 
7091ef3cf714SScott Long 		TAILQ_REMOVE(&queue, ccb_h, sim_links.tqe);
70928b8a9b1dSJustin T. Gibbs 		ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
70938b8a9b1dSJustin T. Gibbs 		splx(s);
70948b8a9b1dSJustin T. Gibbs 
70958b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
709657b89bbcSNate Lawson 			  ("camisr\n"));
70978b8a9b1dSJustin T. Gibbs 
70988b8a9b1dSJustin T. Gibbs 		runq = FALSE;
70998b8a9b1dSJustin T. Gibbs 
71008b8a9b1dSJustin T. Gibbs 		if (ccb_h->flags & CAM_HIGH_POWER) {
71018b8a9b1dSJustin T. Gibbs 			struct highpowerlist	*hphead;
71028b8a9b1dSJustin T. Gibbs 			union ccb		*send_ccb;
71038b8a9b1dSJustin T. Gibbs 
71048b8a9b1dSJustin T. Gibbs 			hphead = &highpowerq;
71058b8a9b1dSJustin T. Gibbs 
71068b8a9b1dSJustin T. Gibbs 			send_ccb = (union ccb *)STAILQ_FIRST(hphead);
71078b8a9b1dSJustin T. Gibbs 
71088b8a9b1dSJustin T. Gibbs 			/*
71098b8a9b1dSJustin T. Gibbs 			 * Increment the count since this command is done.
71108b8a9b1dSJustin T. Gibbs 			 */
71118b8a9b1dSJustin T. Gibbs 			num_highpower++;
71128b8a9b1dSJustin T. Gibbs 
71138b8a9b1dSJustin T. Gibbs 			/*
71148b8a9b1dSJustin T. Gibbs 			 * Any high powered commands queued up?
71158b8a9b1dSJustin T. Gibbs 			 */
71168b8a9b1dSJustin T. Gibbs 			if (send_ccb != NULL) {
71178b8a9b1dSJustin T. Gibbs 
71188b8a9b1dSJustin T. Gibbs 				STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
71198b8a9b1dSJustin T. Gibbs 
71202cefde5fSJustin T. Gibbs 				xpt_release_devq(send_ccb->ccb_h.path,
71212cefde5fSJustin T. Gibbs 						 /*count*/1, /*runqueue*/TRUE);
71228b8a9b1dSJustin T. Gibbs 			}
71238b8a9b1dSJustin T. Gibbs 		}
71249deea857SKenneth D. Merry 		if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
71258b8a9b1dSJustin T. Gibbs 			struct cam_ed *dev;
71268b8a9b1dSJustin T. Gibbs 
71278b8a9b1dSJustin T. Gibbs 			dev = ccb_h->path->device;
71288b8a9b1dSJustin T. Gibbs 
71298b8a9b1dSJustin T. Gibbs 			s = splcam();
71308b8a9b1dSJustin T. Gibbs 			cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
71318b8a9b1dSJustin T. Gibbs 
7132d3ef3454SIan Dowse 			if (!SIM_DEAD(ccb_h->path->bus->sim)) {
71338b8a9b1dSJustin T. Gibbs 				ccb_h->path->bus->sim->devq->send_active--;
71348b8a9b1dSJustin T. Gibbs 				ccb_h->path->bus->sim->devq->send_openings++;
7135d3ef3454SIan Dowse 			}
71368b8a9b1dSJustin T. Gibbs 			splx(s);
71378b8a9b1dSJustin T. Gibbs 
71383393f8daSKenneth D. Merry 			if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
71393393f8daSKenneth D. Merry 			  && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
71408b8a9b1dSJustin T. Gibbs 			 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
71418b8a9b1dSJustin T. Gibbs 			  && (dev->ccbq.dev_active == 0))) {
71428b8a9b1dSJustin T. Gibbs 
71432cefde5fSJustin T. Gibbs 				xpt_release_devq(ccb_h->path, /*count*/1,
71448b8a9b1dSJustin T. Gibbs 						 /*run_queue*/TRUE);
71458b8a9b1dSJustin T. Gibbs 			}
71468b8a9b1dSJustin T. Gibbs 
7147fd21cc5eSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
7148fd21cc5eSJustin T. Gibbs 			 && (--dev->tag_delay_count == 0))
7149fd21cc5eSJustin T. Gibbs 				xpt_start_tags(ccb_h->path);
7150fd21cc5eSJustin T. Gibbs 
71518b8a9b1dSJustin T. Gibbs 			if ((dev->ccbq.queue.entries > 0)
71528b8a9b1dSJustin T. Gibbs 			 && (dev->qfrozen_cnt == 0)
71538b8a9b1dSJustin T. Gibbs 			 && (device_is_send_queued(dev) == 0)) {
71548b8a9b1dSJustin T. Gibbs 				runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
71558b8a9b1dSJustin T. Gibbs 							      dev);
71568b8a9b1dSJustin T. Gibbs 			}
71578b8a9b1dSJustin T. Gibbs 		}
71588b8a9b1dSJustin T. Gibbs 
71598b8a9b1dSJustin T. Gibbs 		if (ccb_h->status & CAM_RELEASE_SIMQ) {
71608b8a9b1dSJustin T. Gibbs 			xpt_release_simq(ccb_h->path->bus->sim,
71618b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
7162434bbf6eSJustin T. Gibbs 			ccb_h->status &= ~CAM_RELEASE_SIMQ;
7163434bbf6eSJustin T. Gibbs 			runq = FALSE;
7164434bbf6eSJustin T. Gibbs 		}
7165434bbf6eSJustin T. Gibbs 
7166434bbf6eSJustin T. Gibbs 		if ((ccb_h->flags & CAM_DEV_QFRZDIS)
71678b8a9b1dSJustin T. Gibbs 		 && (ccb_h->status & CAM_DEV_QFRZN)) {
71682cefde5fSJustin T. Gibbs 			xpt_release_devq(ccb_h->path, /*count*/1,
71698b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
71708b8a9b1dSJustin T. Gibbs 			ccb_h->status &= ~CAM_DEV_QFRZN;
71718b8a9b1dSJustin T. Gibbs 		} else if (runq) {
71728b8a9b1dSJustin T. Gibbs 			xpt_run_dev_sendq(ccb_h->path->bus);
71738b8a9b1dSJustin T. Gibbs 		}
71748b8a9b1dSJustin T. Gibbs 
71758b8a9b1dSJustin T. Gibbs 		/* Call the peripheral driver's callback */
7176434bbf6eSJustin T. Gibbs 		(*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
71778b8a9b1dSJustin T. Gibbs 
71788b8a9b1dSJustin T. Gibbs 		/* Raise IPL for while test */
71798b8a9b1dSJustin T. Gibbs 		s = splcam();
71808b8a9b1dSJustin T. Gibbs 	}
71818b8a9b1dSJustin T. Gibbs 	splx(s);
71828b8a9b1dSJustin T. Gibbs }
7183d3ef3454SIan Dowse 
7184d3ef3454SIan Dowse static void
7185d3ef3454SIan Dowse dead_sim_action(struct cam_sim *sim, union ccb *ccb)
7186d3ef3454SIan Dowse {
7187d3ef3454SIan Dowse 
7188d3ef3454SIan Dowse 	ccb->ccb_h.status = CAM_DEV_NOT_THERE;
7189d3ef3454SIan Dowse 	xpt_done(ccb);
7190d3ef3454SIan Dowse }
7191d3ef3454SIan Dowse 
7192d3ef3454SIan Dowse static void
7193d3ef3454SIan Dowse dead_sim_poll(struct cam_sim *sim)
7194d3ef3454SIan Dowse {
7195d3ef3454SIan Dowse }
7196