xref: /freebsd/sys/cam/cam_xpt.c (revision c8bead2a1225d947c4d1719cb324f23596aff697)
18b8a9b1dSJustin T. Gibbs /*
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.
58b8a9b1dSJustin T. Gibbs  * Copyright (c) 1997, 1998 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  *
29c8bead2aSJustin T. Gibbs  *      $Id: cam_xpt.c,v 1.35 1999/01/07 01:11:24 ken Exp $
308b8a9b1dSJustin T. Gibbs  */
318b8a9b1dSJustin T. Gibbs #include <sys/param.h>
328b8a9b1dSJustin T. Gibbs #include <sys/systm.h>
338b8a9b1dSJustin T. Gibbs #include <sys/types.h>
348b8a9b1dSJustin T. Gibbs #include <sys/malloc.h>
358b8a9b1dSJustin T. Gibbs #include <sys/device.h>
368b8a9b1dSJustin T. Gibbs #include <sys/kernel.h>
378b8a9b1dSJustin T. Gibbs #include <sys/conf.h>
388b8a9b1dSJustin T. Gibbs #include <sys/fcntl.h>
398b8a9b1dSJustin T. Gibbs #include <sys/md5.h>
408b8a9b1dSJustin T. Gibbs #include <sys/devicestat.h>
415d754af7SMatt Jacob #include <sys/interrupt.h>
428b8a9b1dSJustin T. Gibbs 
438b8a9b1dSJustin T. Gibbs #ifdef PC98
448b8a9b1dSJustin T. Gibbs #include <pc98/pc98/pc98_machdep.h>	/* geometry translation */
458b8a9b1dSJustin T. Gibbs #endif
468b8a9b1dSJustin T. Gibbs 
478b8a9b1dSJustin T. Gibbs #include <machine/clock.h>
488b8a9b1dSJustin T. Gibbs #include <machine/ipl.h>
498b8a9b1dSJustin T. Gibbs 
508b8a9b1dSJustin T. Gibbs #include <cam/cam.h>
518b8a9b1dSJustin T. Gibbs #include <cam/cam_conf.h>
528b8a9b1dSJustin T. Gibbs #include <cam/cam_ccb.h>
538b8a9b1dSJustin T. Gibbs #include <cam/cam_periph.h>
548b8a9b1dSJustin T. Gibbs #include <cam/cam_sim.h>
558b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt.h>
568b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_sim.h>
578b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_periph.h>
588b8a9b1dSJustin T. Gibbs #include <cam/cam_debug.h>
598b8a9b1dSJustin T. Gibbs 
608b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_all.h>
618b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_message.h>
628b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_pass.h>
638b8a9b1dSJustin T. Gibbs #include "opt_cam.h"
648b8a9b1dSJustin T. Gibbs #include "opt_scsi.h"
658b8a9b1dSJustin T. Gibbs 
668b8a9b1dSJustin T. Gibbs extern	void	(*ihandlers[32]) __P((void));
678b8a9b1dSJustin T. Gibbs 
688b8a9b1dSJustin T. Gibbs /* Datastructures internal to the xpt layer */
698b8a9b1dSJustin T. Gibbs 
708b8a9b1dSJustin T. Gibbs /*
718b8a9b1dSJustin T. Gibbs  * Definition of an async handler callback block.  These are used to add
728b8a9b1dSJustin T. Gibbs  * SIMs and peripherals to the async callback lists.
738b8a9b1dSJustin T. Gibbs  */
748b8a9b1dSJustin T. Gibbs struct async_node {
758b8a9b1dSJustin T. Gibbs 	SLIST_ENTRY(async_node)	links;
768b8a9b1dSJustin T. Gibbs 	u_int32_t	event_enable;	/* Async Event enables */
778b8a9b1dSJustin T. Gibbs 	void		(*callback)(void *arg, u_int32_t code,
788b8a9b1dSJustin T. Gibbs 				    struct cam_path *path, void *args);
798b8a9b1dSJustin T. Gibbs 	void		*callback_arg;
808b8a9b1dSJustin T. Gibbs };
818b8a9b1dSJustin T. Gibbs 
828b8a9b1dSJustin T. Gibbs SLIST_HEAD(async_list, async_node);
838b8a9b1dSJustin T. Gibbs SLIST_HEAD(periph_list, cam_periph);
843c68dd1aSEivind Eklund static STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
858b8a9b1dSJustin T. Gibbs 
868b8a9b1dSJustin T. Gibbs /*
878b8a9b1dSJustin T. Gibbs  * This is the maximum number of high powered commands (e.g. start unit)
888b8a9b1dSJustin T. Gibbs  * that can be outstanding at a particular time.
898b8a9b1dSJustin T. Gibbs  */
908b8a9b1dSJustin T. Gibbs #ifndef CAM_MAX_HIGHPOWER
918b8a9b1dSJustin T. Gibbs #define CAM_MAX_HIGHPOWER  4
928b8a9b1dSJustin T. Gibbs #endif
938b8a9b1dSJustin T. Gibbs 
948b8a9b1dSJustin T. Gibbs /*
958b8a9b1dSJustin T. Gibbs  * This is the number of seconds we wait for devices to settle after a SCSI
968b8a9b1dSJustin T. Gibbs  * bus reset.
978b8a9b1dSJustin T. Gibbs  */
988b8a9b1dSJustin T. Gibbs #ifndef SCSI_DELAY
998b8a9b1dSJustin T. Gibbs #define SCSI_DELAY 2000
1008b8a9b1dSJustin T. Gibbs #endif
101a822eb2aSKenneth D. Merry /*
102a822eb2aSKenneth D. Merry  * If someone sets this to 0, we assume that they want the minimum
103a822eb2aSKenneth D. Merry  * allowable bus settle delay.  All devices need _some_ sort of bus settle
104a822eb2aSKenneth D. Merry  * delay, so we'll set it to a minimum value of 100ms.
105a822eb2aSKenneth D. Merry  */
106a822eb2aSKenneth D. Merry #if (SCSI_DELAY == 0)
107a822eb2aSKenneth D. Merry #undef SCSI_DELAY
108a822eb2aSKenneth D. Merry #define SCSI_DELAY 100
109a822eb2aSKenneth D. Merry #endif
110a822eb2aSKenneth D. Merry 
111a822eb2aSKenneth D. Merry /*
112a822eb2aSKenneth D. Merry  * Make sure the user isn't using seconds instead of milliseconds.
113a822eb2aSKenneth D. Merry  */
1148b8a9b1dSJustin T. Gibbs #if (SCSI_DELAY < 100)
1158b8a9b1dSJustin T. Gibbs #error "SCSI_DELAY is in milliseconds, not seconds!  Please use a larger value"
1168b8a9b1dSJustin T. Gibbs #endif
1178b8a9b1dSJustin T. Gibbs 
1188b8a9b1dSJustin T. Gibbs /* number of high powered commands that can go through right now */
1198b8a9b1dSJustin T. Gibbs static int num_highpower = CAM_MAX_HIGHPOWER;
1208b8a9b1dSJustin T. Gibbs 
1218b8a9b1dSJustin T. Gibbs /*
1228b8a9b1dSJustin T. Gibbs  * Structure for queueing a device in a run queue.
1238b8a9b1dSJustin T. Gibbs  * There is one run queue for allocating new ccbs,
1248b8a9b1dSJustin T. Gibbs  * and another for sending ccbs to the controller.
1258b8a9b1dSJustin T. Gibbs  */
1268b8a9b1dSJustin T. Gibbs struct cam_ed_qinfo {
1278b8a9b1dSJustin T. Gibbs 	cam_pinfo pinfo;
1288b8a9b1dSJustin T. Gibbs 	struct	  cam_ed *device;
1298b8a9b1dSJustin T. Gibbs };
1308b8a9b1dSJustin T. Gibbs 
1318b8a9b1dSJustin T. Gibbs /*
1328b8a9b1dSJustin T. Gibbs  * The CAM EDT (Existing Device Table) contains the device information for
1338b8a9b1dSJustin T. Gibbs  * all devices for all busses in the system.  The table contains a
1348b8a9b1dSJustin T. Gibbs  * cam_ed structure for each device on the bus.
1358b8a9b1dSJustin T. Gibbs  */
1368b8a9b1dSJustin T. Gibbs struct cam_ed {
1378b8a9b1dSJustin T. Gibbs 	TAILQ_ENTRY(cam_ed) links;
1388b8a9b1dSJustin T. Gibbs 	struct	cam_ed_qinfo alloc_ccb_entry;
1398b8a9b1dSJustin T. Gibbs 	struct	cam_ed_qinfo send_ccb_entry;
1408b8a9b1dSJustin T. Gibbs 	struct	cam_et	 *target;
1418b8a9b1dSJustin T. Gibbs 	lun_id_t	 lun_id;
1428b8a9b1dSJustin T. Gibbs 	struct	camq drvq;		/*
1438b8a9b1dSJustin T. Gibbs 					 * Queue of type drivers wanting to do
1448b8a9b1dSJustin T. Gibbs 					 * work on this device.
1458b8a9b1dSJustin T. Gibbs 					 */
1468b8a9b1dSJustin T. Gibbs 	struct	cam_ccbq ccbq;		/* Queue of pending ccbs */
1478b8a9b1dSJustin T. Gibbs 	struct	async_list asyncs;	/* Async callback info for this B/T/L */
1488b8a9b1dSJustin T. Gibbs 	struct	periph_list periphs;	/* All attached devices */
1498b8a9b1dSJustin T. Gibbs 	u_int	generation;		/* Generation number */
1508b8a9b1dSJustin T. Gibbs 	struct	cam_periph *owner;	/* Peripheral driver's ownership tag */
1518b8a9b1dSJustin T. Gibbs 	struct	xpt_quirk_entry *quirk;	/* Oddities about this device */
1528b8a9b1dSJustin T. Gibbs 					/* Storage for the inquiry data */
1538b8a9b1dSJustin T. Gibbs 	struct	scsi_inquiry_data inq_data;
1548b8a9b1dSJustin T. Gibbs 	u_int8_t	 inq_flags;	/*
1558b8a9b1dSJustin T. Gibbs 					 * Current settings for inquiry flags.
1568b8a9b1dSJustin T. Gibbs 					 * This allows us to override settings
1578b8a9b1dSJustin T. Gibbs 					 * like disconnection and tagged
1588b8a9b1dSJustin T. Gibbs 					 * queuing for a device.
1598b8a9b1dSJustin T. Gibbs 					 */
1608b8a9b1dSJustin T. Gibbs 	u_int8_t	 queue_flags;	/* Queue flags from the control page */
1618b8a9b1dSJustin T. Gibbs 	u_int8_t	 *serial_num;
1628b8a9b1dSJustin T. Gibbs 	u_int8_t	 serial_num_len;
1638b8a9b1dSJustin T. Gibbs 	u_int32_t	 qfrozen_cnt;
1648b8a9b1dSJustin T. Gibbs 	u_int32_t	 flags;
1658b8a9b1dSJustin T. Gibbs #define CAM_DEV_UNCONFIGURED	 	0x01
1668b8a9b1dSJustin T. Gibbs #define CAM_DEV_REL_TIMEOUT_PENDING	0x02
1678b8a9b1dSJustin T. Gibbs #define CAM_DEV_REL_ON_COMPLETE		0x04
1688b8a9b1dSJustin T. Gibbs #define CAM_DEV_REL_ON_QUEUE_EMPTY	0x08
1698b8a9b1dSJustin T. Gibbs #define CAM_DEV_RESIZE_QUEUE_NEEDED	0x10
170fd21cc5eSJustin T. Gibbs #define CAM_DEV_TAG_AFTER_COUNT		0x20
171fd21cc5eSJustin T. Gibbs 	u_int32_t	 tag_delay_count;
172fd21cc5eSJustin T. Gibbs #define	CAM_TAG_DELAY_COUNT		5
1738b8a9b1dSJustin T. Gibbs 	u_int32_t	 refcount;
1748b8a9b1dSJustin T. Gibbs 	struct		 callout_handle c_handle;
1758b8a9b1dSJustin T. Gibbs };
1768b8a9b1dSJustin T. Gibbs 
1778b8a9b1dSJustin T. Gibbs /*
1788b8a9b1dSJustin T. Gibbs  * Each target is represented by an ET (Existing Target).  These
1798b8a9b1dSJustin T. Gibbs  * entries are created when a target is successfully probed with an
1808b8a9b1dSJustin T. Gibbs  * identify, and removed when a device fails to respond after a number
1818b8a9b1dSJustin T. Gibbs  * of retries, or a bus rescan finds the device missing.
1828b8a9b1dSJustin T. Gibbs  */
1838b8a9b1dSJustin T. Gibbs struct cam_et {
1848b8a9b1dSJustin T. Gibbs 	TAILQ_HEAD(, cam_ed) ed_entries;
1858b8a9b1dSJustin T. Gibbs 	TAILQ_ENTRY(cam_et) links;
1868b8a9b1dSJustin T. Gibbs 	struct	cam_eb	*bus;
1878b8a9b1dSJustin T. Gibbs 	target_id_t	target_id;
1888b8a9b1dSJustin T. Gibbs 	u_int32_t	refcount;
1898b8a9b1dSJustin T. Gibbs 	u_int		generation;
1908b8a9b1dSJustin T. Gibbs };
1918b8a9b1dSJustin T. Gibbs 
1928b8a9b1dSJustin T. Gibbs /*
1938b8a9b1dSJustin T. Gibbs  * Each bus is represented by an EB (Existing Bus).  These entries
1948b8a9b1dSJustin T. Gibbs  * are created by calls to xpt_bus_register and deleted by calls to
1958b8a9b1dSJustin T. Gibbs  * xpt_bus_deregister.
1968b8a9b1dSJustin T. Gibbs  */
1978b8a9b1dSJustin T. Gibbs struct cam_eb {
1988b8a9b1dSJustin T. Gibbs 	TAILQ_HEAD(, cam_et) et_entries;
1998b8a9b1dSJustin T. Gibbs 	TAILQ_ENTRY(cam_eb)  links;
2008b8a9b1dSJustin T. Gibbs 	path_id_t	     path_id;
2018b8a9b1dSJustin T. Gibbs 	struct cam_sim	     *sim;
2028b8a9b1dSJustin T. Gibbs 	u_int32_t	     flags;
2038b8a9b1dSJustin T. Gibbs #define	CAM_EB_RUNQ_SCHEDULED	0x01
2048b8a9b1dSJustin T. Gibbs 	u_int		     generation;
2058b8a9b1dSJustin T. Gibbs };
2068b8a9b1dSJustin T. Gibbs 
2078b8a9b1dSJustin T. Gibbs struct cam_path {
2088b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph;
2098b8a9b1dSJustin T. Gibbs 	struct cam_eb	  *bus;
2108b8a9b1dSJustin T. Gibbs 	struct cam_et	  *target;
2118b8a9b1dSJustin T. Gibbs 	struct cam_ed	  *device;
2128b8a9b1dSJustin T. Gibbs };
2138b8a9b1dSJustin T. Gibbs 
2148b8a9b1dSJustin T. Gibbs struct xpt_quirk_entry {
2158b8a9b1dSJustin T. Gibbs 	struct scsi_inquiry_pattern inq_pat;
2168b8a9b1dSJustin T. Gibbs 	u_int8_t quirks;
2178b8a9b1dSJustin T. Gibbs #define	CAM_QUIRK_NOLUNS	0x01
2188b8a9b1dSJustin T. Gibbs #define	CAM_QUIRK_NOSERIAL	0x02
2198b8a9b1dSJustin T. Gibbs 	u_int8_t mintags;
2208b8a9b1dSJustin T. Gibbs 	u_int8_t maxtags;
2218b8a9b1dSJustin T. Gibbs };
2228b8a9b1dSJustin T. Gibbs 
2238b8a9b1dSJustin T. Gibbs typedef enum {
2248b8a9b1dSJustin T. Gibbs 	XPT_FLAG_OPEN		= 0x01
2258b8a9b1dSJustin T. Gibbs } xpt_flags;
2268b8a9b1dSJustin T. Gibbs 
2278b8a9b1dSJustin T. Gibbs struct xpt_softc {
2288b8a9b1dSJustin T. Gibbs 	xpt_flags	flags;
2298b8a9b1dSJustin T. Gibbs 	u_int32_t	generation;
2308b8a9b1dSJustin T. Gibbs #ifdef DEVFS
2318b8a9b1dSJustin T. Gibbs 	void		*xpt_devfs_token;
2328b8a9b1dSJustin T. Gibbs 	void		*ctl_devfs_token;
2338b8a9b1dSJustin T. Gibbs #endif
2348b8a9b1dSJustin T. Gibbs };
2358b8a9b1dSJustin T. Gibbs 
2368b8a9b1dSJustin T. Gibbs static const char quantum[] = "QUANTUM";
237f24c39c7SKenneth D. Merry static const char sony[] = "SONY";
238f24c39c7SKenneth D. Merry static const char west_digital[] = "WDIGTL";
2398b8a9b1dSJustin T. Gibbs 
2408b8a9b1dSJustin T. Gibbs static struct xpt_quirk_entry xpt_quirk_table[] =
2418b8a9b1dSJustin T. Gibbs {
2428b8a9b1dSJustin T. Gibbs 	{
2438b8a9b1dSJustin T. Gibbs 		/* Reports QUEUE FULL for temporary resource shortages */
2446747168dSKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
2458b8a9b1dSJustin T. Gibbs 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
2468b8a9b1dSJustin T. Gibbs 	},
2478b8a9b1dSJustin T. Gibbs 	{
2488b8a9b1dSJustin T. Gibbs 		/* Reports QUEUE FULL for temporary resource shortages */
2496747168dSKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
2506747168dSKenneth D. Merry 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
2516747168dSKenneth D. Merry 	},
2526747168dSKenneth D. Merry 	{
2536747168dSKenneth D. Merry 		/* Reports QUEUE FULL for temporary resource shortages */
2546747168dSKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
2558b8a9b1dSJustin T. Gibbs 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
2568b8a9b1dSJustin T. Gibbs 	},
2578b8a9b1dSJustin T. Gibbs 	{
2588b8a9b1dSJustin T. Gibbs 		/* Broken tagged queuing drive */
2598b8a9b1dSJustin T. Gibbs 		{ T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
2608b8a9b1dSJustin T. Gibbs 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
2618b8a9b1dSJustin T. Gibbs 	},
2628b8a9b1dSJustin T. Gibbs 	{
2638b8a9b1dSJustin T. Gibbs 		/* Broken tagged queuing drive */
2648b8a9b1dSJustin T. Gibbs 		{ T_DIRECT, SIP_MEDIA_FIXED, "MICROP", "3391*", "x43h" },
2658b8a9b1dSJustin T. Gibbs 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
2668b8a9b1dSJustin T. Gibbs 	},
2678b8a9b1dSJustin T. Gibbs 	{
2688597eafeSKenneth D. Merry 		/*
2694cdd0221SKenneth D. Merry 		 * Unfortunately, the Quantum Atlas III has the same
2704cdd0221SKenneth D. Merry 		 * problem as the Atlas II drives above.
2714cdd0221SKenneth D. Merry 		 * Reported by: "Johan Granlund" <johan@granlund.nu>
2724cdd0221SKenneth D. Merry 		 *
2734cdd0221SKenneth D. Merry 		 * For future reference, the drive with the problem was:
2744cdd0221SKenneth D. Merry 		 * QUANTUM QM39100TD-SW N1B0
2754cdd0221SKenneth D. Merry 		 *
2764cdd0221SKenneth D. Merry 		 * It's possible that Quantum will fix the problem in later
2774cdd0221SKenneth D. Merry 		 * firmware revisions.  If that happens, the quirk entry
2784cdd0221SKenneth D. Merry 		 * will need to be made specific to the firmware revisions
2794cdd0221SKenneth D. Merry 		 * with the problem.
2804cdd0221SKenneth D. Merry 		 *
2814cdd0221SKenneth D. Merry 		 * XXX need to add a quirk for the 18G version of this
2824cdd0221SKenneth D. Merry 		 * drive, once inquiry information is known.
2834cdd0221SKenneth D. Merry 		 */
2844cdd0221SKenneth D. Merry 		/* Reports QUEUE FULL for temporary resource shortages */
2854cdd0221SKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
2864cdd0221SKenneth D. Merry 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
2874cdd0221SKenneth D. Merry 	},
2884cdd0221SKenneth D. Merry 	{
2894cdd0221SKenneth D. Merry 		/*
2908597eafeSKenneth D. Merry 		 * Broken tagged queuing drive
2918597eafeSKenneth D. Merry 		 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
2928597eafeSKenneth D. Merry 		 *         and: Martin Renters <martin@tdc.on.ca>
2938597eafeSKenneth D. Merry 		 */
2941b6833dbSKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, "SEAGATE", "ST410800*", "71*" },
2958597eafeSKenneth D. Merry 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
2968597eafeSKenneth D. Merry 	},
2978597eafeSKenneth D. Merry         {
2988b8a9b1dSJustin T. Gibbs 		/* Broken tagged queuing drive */
2998b8a9b1dSJustin T. Gibbs                 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
3008b8a9b1dSJustin T. Gibbs 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
3018b8a9b1dSJustin T. Gibbs 	},
3028b8a9b1dSJustin T. Gibbs 	{
3032ba01e6fSJustin T. Gibbs 		/* Broken tagged queuing drive */
304bfc0eb0fSKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
3052ba01e6fSJustin T. Gibbs 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
3062ba01e6fSJustin T. Gibbs 	},
3072ba01e6fSJustin T. Gibbs         {
308e471e974SJustin T. Gibbs 		/*
309f24c39c7SKenneth D. Merry 		 * Slow when tagged queueing is enabled. (1.5MB/sec versus
310f24c39c7SKenneth D. Merry 		 * 8MB/sec.)
311f24c39c7SKenneth D. Merry 		 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
312f24c39c7SKenneth D. Merry 		 */
313f24c39c7SKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
314f24c39c7SKenneth D. Merry 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
315f24c39c7SKenneth D. Merry         },
316f24c39c7SKenneth D. Merry         {
317f24c39c7SKenneth D. Merry 		/*
318f24c39c7SKenneth D. Merry 		 * Slow when tagged queueing is enabled. (1.5MB/sec versus
319f24c39c7SKenneth D. Merry 		 * 8MB/sec.)
320f24c39c7SKenneth D. Merry 		 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
321f24c39c7SKenneth D. Merry 		 */
322f24c39c7SKenneth D. Merry 		{ T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
323f24c39c7SKenneth D. Merry 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
324f24c39c7SKenneth D. Merry         },
325f24c39c7SKenneth D. Merry 	{
326f24c39c7SKenneth D. Merry 		/*
327e471e974SJustin T. Gibbs 		 * Doesn't handle queue full condition correctly,
328e471e974SJustin T. Gibbs 		 * so we need to limit maxtags to what the device
329e471e974SJustin T. Gibbs 		 * can handle instead of determining this automatically.
330e471e974SJustin T. Gibbs 		 */
331e471e974SJustin T. Gibbs 		{ T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "WN321010S*", "*" },
332fd21cc5eSJustin T. Gibbs 		/*quirks*/0, /*mintags*/2, /*maxtags*/32
333e471e974SJustin T. Gibbs 	},
334e471e974SJustin T. Gibbs 	{
3358b8a9b1dSJustin T. Gibbs 		/* Really only one LUN */
3368b8a9b1dSJustin T. Gibbs 		{
337a822eb2aSKenneth D. Merry 			T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA*", "*"
338a822eb2aSKenneth D. Merry 		},
3398b8a9b1dSJustin T. Gibbs 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
3408b8a9b1dSJustin T. Gibbs 	},
3418b8a9b1dSJustin T. Gibbs 	{
342fd21cc5eSJustin T. Gibbs 		/* I can't believe we need a quirk for DPT volumes. */
343fd21cc5eSJustin T. Gibbs 		{
344fd21cc5eSJustin T. Gibbs 			T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE,
345fd21cc5eSJustin T. Gibbs 			"DPT", "*", "*"
346fd21cc5eSJustin T. Gibbs 		},
347fd21cc5eSJustin T. Gibbs 		CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
348fd21cc5eSJustin T. Gibbs 		/*mintags*/0, /*maxtags*/255
349fd21cc5eSJustin T. Gibbs 	},
350fd21cc5eSJustin T. Gibbs 	{
351f24c39c7SKenneth D. Merry 		/*
35225e5ca27SKenneth D. Merry 		 * Many Sony CDROM drives don't like multi-LUN probing.
353f24c39c7SKenneth D. Merry 		 */
354f24c39c7SKenneth D. Merry 		{
355f24c39c7SKenneth D. Merry 			T_CDROM, SIP_MEDIA_REMOVABLE, sony,
35625e5ca27SKenneth D. Merry 			"CD-ROM CDU*", "*"
357f24c39c7SKenneth D. Merry 		},
358f24c39c7SKenneth D. Merry 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
359f24c39c7SKenneth D. Merry 	},
360f24c39c7SKenneth D. Merry 	{
361f24c39c7SKenneth D. Merry 		/*
362f24c39c7SKenneth D. Merry 		 * This drive doesn't like multiple LUN probing.
363f24c39c7SKenneth D. Merry 		 * Submitted by:  Parag Patel <parag@cgt.com>
364f24c39c7SKenneth D. Merry 		 */
365f24c39c7SKenneth D. Merry 		{
366f24c39c7SKenneth D. Merry 			T_WORM, SIP_MEDIA_REMOVABLE, sony,
367f24c39c7SKenneth D. Merry 			"CD-R   CDU9*", "*"
368f24c39c7SKenneth D. Merry 		},
369f24c39c7SKenneth D. Merry 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
370f24c39c7SKenneth D. Merry 	},
371f24c39c7SKenneth D. Merry 	{
372b1d26455SKenneth D. Merry 		/*
373b1d26455SKenneth D. Merry 		 * The 8200 doesn't like multi-lun probing, and probably
374b1d26455SKenneth D. Merry 		 * don't like serial number requests either.
375b1d26455SKenneth D. Merry 		 */
376b1d26455SKenneth D. Merry 		{
377b1d26455SKenneth D. Merry 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
378b1d26455SKenneth D. Merry 			"EXB-8200*", "*"
379b1d26455SKenneth D. Merry 		},
380b1d26455SKenneth D. Merry 		CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
381b1d26455SKenneth D. Merry 	},
382b1d26455SKenneth D. Merry 	{
38336230d67SJoerg Wunsch 		/*
38436230d67SJoerg Wunsch 		 * This old revision of the TDC3600 is also SCSI-1, and
38536230d67SJoerg Wunsch 		 * hangs upon serial number probing.
38636230d67SJoerg Wunsch 		 */
38736230d67SJoerg Wunsch 		{
38836230d67SJoerg Wunsch 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
38936230d67SJoerg Wunsch 			" TDC 3600", "U07:"
39036230d67SJoerg Wunsch 		},
39136230d67SJoerg Wunsch 		CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
39236230d67SJoerg Wunsch 	},
39336230d67SJoerg Wunsch 	{
3948b8a9b1dSJustin T. Gibbs 		/* Default tagged queuing parameters for all devices */
3958b8a9b1dSJustin T. Gibbs 		{
3968b8a9b1dSJustin T. Gibbs 		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
3978b8a9b1dSJustin T. Gibbs 		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
3988b8a9b1dSJustin T. Gibbs 		},
399fd21cc5eSJustin T. Gibbs 		/*quirks*/0, /*mintags*/2, /*maxtags*/255
4008b8a9b1dSJustin T. Gibbs 	},
4018b8a9b1dSJustin T. Gibbs };
402fd21cc5eSJustin T. Gibbs 
4038b8a9b1dSJustin T. Gibbs typedef enum {
4048b8a9b1dSJustin T. Gibbs 	DM_RET_COPY		= 0x01,
4058b8a9b1dSJustin T. Gibbs 	DM_RET_FLAG_MASK	= 0x0f,
4068b8a9b1dSJustin T. Gibbs 	DM_RET_NONE		= 0x00,
4078b8a9b1dSJustin T. Gibbs 	DM_RET_STOP		= 0x10,
4088b8a9b1dSJustin T. Gibbs 	DM_RET_DESCEND		= 0x20,
4098b8a9b1dSJustin T. Gibbs 	DM_RET_ERROR		= 0x30,
4108b8a9b1dSJustin T. Gibbs 	DM_RET_ACTION_MASK	= 0xf0
4118b8a9b1dSJustin T. Gibbs } dev_match_ret;
4128b8a9b1dSJustin T. Gibbs 
4138b8a9b1dSJustin T. Gibbs typedef enum {
4148b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_BUS,
4158b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_TARGET,
4168b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_DEVICE,
4178b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_PERIPH
4188b8a9b1dSJustin T. Gibbs } xpt_traverse_depth;
4198b8a9b1dSJustin T. Gibbs 
4208b8a9b1dSJustin T. Gibbs struct xpt_traverse_config {
4218b8a9b1dSJustin T. Gibbs 	xpt_traverse_depth	depth;
4228b8a9b1dSJustin T. Gibbs 	void			*tr_func;
4238b8a9b1dSJustin T. Gibbs 	void			*tr_arg;
4248b8a9b1dSJustin T. Gibbs };
4258b8a9b1dSJustin T. Gibbs 
4268b8a9b1dSJustin T. Gibbs typedef	int	xpt_busfunc_t (struct cam_eb *bus, void *arg);
4278b8a9b1dSJustin T. Gibbs typedef	int	xpt_targetfunc_t (struct cam_et *target, void *arg);
4288b8a9b1dSJustin T. Gibbs typedef	int	xpt_devicefunc_t (struct cam_ed *device, void *arg);
4298b8a9b1dSJustin T. Gibbs typedef	int	xpt_periphfunc_t (struct cam_periph *periph, void *arg);
4308b8a9b1dSJustin T. Gibbs typedef int	xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
4318b8a9b1dSJustin T. Gibbs 
4328b8a9b1dSJustin T. Gibbs /* Transport layer configuration information */
4338b8a9b1dSJustin T. Gibbs static struct xpt_softc xsoftc;
4348b8a9b1dSJustin T. Gibbs 
4358b8a9b1dSJustin T. Gibbs /* Queues for our software interrupt handler */
4368b8a9b1dSJustin T. Gibbs typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
4378b8a9b1dSJustin T. Gibbs static cam_isrq_t cam_bioq;
4388b8a9b1dSJustin T. Gibbs static cam_isrq_t cam_netq;
4398b8a9b1dSJustin T. Gibbs 
4408b8a9b1dSJustin T. Gibbs /* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
4413c68dd1aSEivind Eklund static SLIST_HEAD(,ccb_hdr) ccb_freeq;
4428b8a9b1dSJustin T. Gibbs static u_int xpt_max_ccbs;	/*
4438b8a9b1dSJustin T. Gibbs 				 * Maximum size of ccb pool.  Modified as
4448b8a9b1dSJustin T. Gibbs 				 * devices are added/removed or have their
4458b8a9b1dSJustin T. Gibbs 				 * opening counts changed.
4468b8a9b1dSJustin T. Gibbs 				 */
4478b8a9b1dSJustin T. Gibbs static u_int xpt_ccb_count;	/* Current count of allocated ccbs */
4488b8a9b1dSJustin T. Gibbs 
4498b8a9b1dSJustin T. Gibbs static struct cam_periph *xpt_periph;
4508b8a9b1dSJustin T. Gibbs 
4518b8a9b1dSJustin T. Gibbs static periph_init_t xpt_periph_init;
4528b8a9b1dSJustin T. Gibbs 
4538b8a9b1dSJustin T. Gibbs static periph_init_t probe_periph_init;
4548b8a9b1dSJustin T. Gibbs 
4558b8a9b1dSJustin T. Gibbs static struct periph_driver xpt_driver =
4568b8a9b1dSJustin T. Gibbs {
4578b8a9b1dSJustin T. Gibbs 	xpt_periph_init, "xpt",
4588b8a9b1dSJustin T. Gibbs 	TAILQ_HEAD_INITIALIZER(xpt_driver.units)
4598b8a9b1dSJustin T. Gibbs };
4608b8a9b1dSJustin T. Gibbs 
4618b8a9b1dSJustin T. Gibbs static struct periph_driver probe_driver =
4628b8a9b1dSJustin T. Gibbs {
4638b8a9b1dSJustin T. Gibbs 	probe_periph_init, "probe",
4648b8a9b1dSJustin T. Gibbs 	TAILQ_HEAD_INITIALIZER(probe_driver.units)
4658b8a9b1dSJustin T. Gibbs };
4668b8a9b1dSJustin T. Gibbs 
4678b8a9b1dSJustin T. Gibbs DATA_SET(periphdriver_set, xpt_driver);
4688b8a9b1dSJustin T. Gibbs DATA_SET(periphdriver_set, probe_driver);
4698b8a9b1dSJustin T. Gibbs 
4708b8a9b1dSJustin T. Gibbs #define XPT_CDEV_MAJOR 104
4718b8a9b1dSJustin T. Gibbs 
4728b8a9b1dSJustin T. Gibbs static d_open_t xptopen;
4738b8a9b1dSJustin T. Gibbs static d_close_t xptclose;
4748b8a9b1dSJustin T. Gibbs static d_ioctl_t xptioctl;
4758b8a9b1dSJustin T. Gibbs 
4768b8a9b1dSJustin T. Gibbs static struct cdevsw xpt_cdevsw =
4778b8a9b1dSJustin T. Gibbs {
4788b8a9b1dSJustin T. Gibbs 	/*d_open*/	xptopen,
4798b8a9b1dSJustin T. Gibbs 	/*d_close*/	xptclose,
4808b8a9b1dSJustin T. Gibbs 	/*d_read*/	noread,
4818b8a9b1dSJustin T. Gibbs 	/*d_write*/	nowrite,
4828b8a9b1dSJustin T. Gibbs 	/*d_ioctl*/	xptioctl,
4838b8a9b1dSJustin T. Gibbs 	/*d_stop*/	nostop,
4848b8a9b1dSJustin T. Gibbs 	/*d_reset*/	noreset,
4858b8a9b1dSJustin T. Gibbs 	/*d_devtotty*/	nodevtotty,
4868b8a9b1dSJustin T. Gibbs 	/*d_poll*/	NULL,
4878b8a9b1dSJustin T. Gibbs 	/*d_mmap*/	nommap,
4888b8a9b1dSJustin T. Gibbs 	/*d_strategy*/	nostrategy,
4898b8a9b1dSJustin T. Gibbs 	/*d_name*/	"xpt",
4908b8a9b1dSJustin T. Gibbs 	/*d_spare*/	NULL,
4918b8a9b1dSJustin T. Gibbs 	/*d_maj*/	-1,
4928b8a9b1dSJustin T. Gibbs 	/*d_dump*/	nodump,
4938b8a9b1dSJustin T. Gibbs 	/*d_psize*/	nopsize,
4948b8a9b1dSJustin T. Gibbs 	/*d_flags*/	0,
4958b8a9b1dSJustin T. Gibbs 	/*d_maxio*/	0,
4968b8a9b1dSJustin T. Gibbs 	/*b_maj*/	-1
4978b8a9b1dSJustin T. Gibbs };
4988b8a9b1dSJustin T. Gibbs 
4998b8a9b1dSJustin T. Gibbs static struct intr_config_hook *xpt_config_hook;
5008b8a9b1dSJustin T. Gibbs 
5018b8a9b1dSJustin T. Gibbs /* Registered busses */
5023c68dd1aSEivind Eklund static TAILQ_HEAD(,cam_eb) xpt_busses;
5038b8a9b1dSJustin T. Gibbs static u_int bus_generation;
5048b8a9b1dSJustin T. Gibbs 
5058b8a9b1dSJustin T. Gibbs /* Storage for debugging datastructures */
5068b8a9b1dSJustin T. Gibbs #ifdef	CAMDEBUG
5078b8a9b1dSJustin T. Gibbs struct cam_path *cam_dpath;
5088b8a9b1dSJustin T. Gibbs u_int32_t cam_dflags;
5098b8a9b1dSJustin T. Gibbs #endif
5108b8a9b1dSJustin T. Gibbs 
5118b8a9b1dSJustin T. Gibbs #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
5128b8a9b1dSJustin T. Gibbs #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
5138b8a9b1dSJustin T. Gibbs #endif
5148b8a9b1dSJustin T. Gibbs 
5158b8a9b1dSJustin T. Gibbs /*
5168b8a9b1dSJustin T. Gibbs  * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
5178b8a9b1dSJustin T. Gibbs  * enabled.  Also, the user must have either none, or all of CAM_DEBUG_BUS,
5188b8a9b1dSJustin T. Gibbs  * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
5198b8a9b1dSJustin T. Gibbs  */
5208b8a9b1dSJustin T. Gibbs #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
5218b8a9b1dSJustin T. Gibbs     || defined(CAM_DEBUG_LUN)
5228b8a9b1dSJustin T. Gibbs #ifdef CAMDEBUG
5238b8a9b1dSJustin T. Gibbs #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
5248b8a9b1dSJustin T. Gibbs     || !defined(CAM_DEBUG_LUN)
5258b8a9b1dSJustin T. Gibbs #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
5268b8a9b1dSJustin T. Gibbs         and CAM_DEBUG_LUN"
5278b8a9b1dSJustin T. Gibbs #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
5288b8a9b1dSJustin T. Gibbs #else /* !CAMDEBUG */
5298b8a9b1dSJustin T. Gibbs #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
5308b8a9b1dSJustin T. Gibbs #endif /* CAMDEBUG */
5318b8a9b1dSJustin T. Gibbs #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
5328b8a9b1dSJustin T. Gibbs 
5338b8a9b1dSJustin T. Gibbs /* Forward declarations for private functions */
5348b8a9b1dSJustin T. Gibbs void	xpt_init(void);
5358b8a9b1dSJustin T. Gibbs 
5368b8a9b1dSJustin T. Gibbs static cam_status	xpt_compile_path(struct cam_path *new_path,
5378b8a9b1dSJustin T. Gibbs 					 struct cam_periph *perph,
5388b8a9b1dSJustin T. Gibbs 					 path_id_t path_id,
5398b8a9b1dSJustin T. Gibbs 					 target_id_t target_id,
5408b8a9b1dSJustin T. Gibbs 					 lun_id_t lun_id);
5418b8a9b1dSJustin T. Gibbs 
5428b8a9b1dSJustin T. Gibbs static void		xpt_release_path(struct cam_path *path);
5438b8a9b1dSJustin T. Gibbs 
5448b8a9b1dSJustin T. Gibbs static void		xpt_async_bcast(struct async_list *async_head,
5458b8a9b1dSJustin T. Gibbs 					u_int32_t async_code,
5468b8a9b1dSJustin T. Gibbs 					struct cam_path *path,
5478b8a9b1dSJustin T. Gibbs 					void *async_arg);
5488b8a9b1dSJustin T. Gibbs static int 	 xptnextfreebus(path_id_t startbus);
5498b8a9b1dSJustin T. Gibbs static int	 xptpathid(const char *sim_name, int sim_unit, int sim_bus,
5508b8a9b1dSJustin T. Gibbs 			   path_id_t *nextpath);
5518b8a9b1dSJustin T. Gibbs static union ccb *xpt_get_ccb(struct cam_ed *device);
5528b8a9b1dSJustin T. Gibbs static int	 xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
5538b8a9b1dSJustin T. Gibbs 				  u_int32_t new_priority);
5548b8a9b1dSJustin T. Gibbs static void	 xpt_run_dev_allocq(struct cam_eb *bus);
5558b8a9b1dSJustin T. Gibbs static void	 xpt_run_dev_sendq(struct cam_eb *bus);
5568b8a9b1dSJustin T. Gibbs static timeout_t xpt_release_devq_timeout;
5578b8a9b1dSJustin T. Gibbs static timeout_t xpt_release_simq_timeout;
5588b8a9b1dSJustin T. Gibbs static struct cam_et*
5598b8a9b1dSJustin T. Gibbs 		 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
5608b8a9b1dSJustin T. Gibbs static void	 xpt_release_target(struct cam_eb *bus, struct cam_et *target);
5618b8a9b1dSJustin T. Gibbs static struct cam_ed*
5628b8a9b1dSJustin T. Gibbs 		 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
5638b8a9b1dSJustin T. Gibbs 				  lun_id_t lun_id);
5648b8a9b1dSJustin T. Gibbs static void	 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
5658b8a9b1dSJustin T. Gibbs 				    struct cam_ed *device);
5668b8a9b1dSJustin T. Gibbs static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
5678b8a9b1dSJustin T. Gibbs static struct cam_eb*
5688b8a9b1dSJustin T. Gibbs 		 xpt_find_bus(path_id_t path_id);
5698b8a9b1dSJustin T. Gibbs static struct cam_et*
5708b8a9b1dSJustin T. Gibbs 		 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
5718b8a9b1dSJustin T. Gibbs static struct cam_ed*
5728b8a9b1dSJustin T. Gibbs 		 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
5738b8a9b1dSJustin T. Gibbs static void	 xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
5748b8a9b1dSJustin T. Gibbs static void	 xpt_scan_lun(struct cam_periph *periph,
5758b8a9b1dSJustin T. Gibbs 			      struct cam_path *path, cam_flags flags,
5768b8a9b1dSJustin T. Gibbs 			      union ccb *ccb);
5778b8a9b1dSJustin T. Gibbs static void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
5788b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptconfigbuscountfunc;
5798b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptconfigfunc;
5808b8a9b1dSJustin T. Gibbs static void	 xpt_config(void *arg);
5818b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t xptpassannouncefunc;
5828b8a9b1dSJustin T. Gibbs static void	 xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
5838b8a9b1dSJustin T. Gibbs static void	 xptaction(struct cam_sim *sim, union ccb *work_ccb);
5845d754af7SMatt Jacob static swihand_t swi_camnet;
5855d754af7SMatt Jacob static swihand_t swi_cambio;
5868b8a9b1dSJustin T. Gibbs static void	 camisr(cam_isrq_t *queue);
5878b8a9b1dSJustin T. Gibbs #if 0
5888b8a9b1dSJustin T. Gibbs static void	 xptstart(struct cam_periph *periph, union ccb *work_ccb);
5898b8a9b1dSJustin T. Gibbs static void	 xptasync(struct cam_periph *periph,
5908b8a9b1dSJustin T. Gibbs 			  u_int32_t code, cam_path *path);
5918b8a9b1dSJustin T. Gibbs #endif
5928b8a9b1dSJustin T. Gibbs static dev_match_ret	xptbusmatch(struct dev_match_pattern *patterns,
5938b8a9b1dSJustin T. Gibbs 				    int num_patterns, struct cam_eb *bus);
5948b8a9b1dSJustin T. Gibbs static dev_match_ret	xptdevicematch(struct dev_match_pattern *patterns,
5958b8a9b1dSJustin T. Gibbs 				       int num_patterns, struct cam_ed *device);
5968b8a9b1dSJustin T. Gibbs static dev_match_ret	xptperiphmatch(struct dev_match_pattern *patterns,
5978b8a9b1dSJustin T. Gibbs 				       int num_patterns,
5988b8a9b1dSJustin T. Gibbs 				       struct cam_periph *periph);
5998b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptedtbusfunc;
6008b8a9b1dSJustin T. Gibbs static xpt_targetfunc_t	xptedttargetfunc;
6018b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptedtdevicefunc;
6028b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptedtperiphfunc;
6038b8a9b1dSJustin T. Gibbs static xpt_pdrvfunc_t	xptplistpdrvfunc;
6048b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptplistperiphfunc;
6058b8a9b1dSJustin T. Gibbs static int		xptedtmatch(struct ccb_dev_match *cdm);
6068b8a9b1dSJustin T. Gibbs static int		xptperiphlistmatch(struct ccb_dev_match *cdm);
6078b8a9b1dSJustin T. Gibbs static int		xptbustraverse(struct cam_eb *start_bus,
6088b8a9b1dSJustin T. Gibbs 				       xpt_busfunc_t *tr_func, void *arg);
6098b8a9b1dSJustin T. Gibbs static int		xpttargettraverse(struct cam_eb *bus,
6108b8a9b1dSJustin T. Gibbs 					  struct cam_et *start_target,
6118b8a9b1dSJustin T. Gibbs 					  xpt_targetfunc_t *tr_func, void *arg);
6128b8a9b1dSJustin T. Gibbs static int		xptdevicetraverse(struct cam_et *target,
6138b8a9b1dSJustin T. Gibbs 					  struct cam_ed *start_device,
6148b8a9b1dSJustin T. Gibbs 					  xpt_devicefunc_t *tr_func, void *arg);
6158b8a9b1dSJustin T. Gibbs static int		xptperiphtraverse(struct cam_ed *device,
6168b8a9b1dSJustin T. Gibbs 					  struct cam_periph *start_periph,
6178b8a9b1dSJustin T. Gibbs 					  xpt_periphfunc_t *tr_func, void *arg);
6188b8a9b1dSJustin T. Gibbs static int		xptpdrvtraverse(struct periph_driver **start_pdrv,
6198b8a9b1dSJustin T. Gibbs 					xpt_pdrvfunc_t *tr_func, void *arg);
6208b8a9b1dSJustin T. Gibbs static int		xptpdperiphtraverse(struct periph_driver **pdrv,
6218b8a9b1dSJustin T. Gibbs 					    struct cam_periph *start_periph,
6228b8a9b1dSJustin T. Gibbs 					    xpt_periphfunc_t *tr_func,
6238b8a9b1dSJustin T. Gibbs 					    void *arg);
6248b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptdefbusfunc;
6258b8a9b1dSJustin T. Gibbs static xpt_targetfunc_t	xptdeftargetfunc;
6268b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptdefdevicefunc;
6278b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptdefperiphfunc;
6288b8a9b1dSJustin T. Gibbs static int		xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
629bfc0eb0fSKenneth D. Merry #ifdef notusedyet
6308b8a9b1dSJustin T. Gibbs static int		xpt_for_all_targets(xpt_targetfunc_t *tr_func,
6318b8a9b1dSJustin T. Gibbs 					    void *arg);
632bfc0eb0fSKenneth D. Merry #endif
6338b8a9b1dSJustin T. Gibbs static int		xpt_for_all_devices(xpt_devicefunc_t *tr_func,
6348b8a9b1dSJustin T. Gibbs 					    void *arg);
635bfc0eb0fSKenneth D. Merry #ifdef notusedyet
6368b8a9b1dSJustin T. Gibbs static int		xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
6378b8a9b1dSJustin T. Gibbs 					    void *arg);
638bfc0eb0fSKenneth D. Merry #endif
6398b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptsetasyncfunc;
6408b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptsetasyncbusfunc;
6418b8a9b1dSJustin T. Gibbs static cam_status	xptregister(struct cam_periph *periph,
6428b8a9b1dSJustin T. Gibbs 				    void *arg);
6438b8a9b1dSJustin T. Gibbs static cam_status	proberegister(struct cam_periph *periph,
6448b8a9b1dSJustin T. Gibbs 				      void *arg);
6458b8a9b1dSJustin T. Gibbs static void	 probeschedule(struct cam_periph *probe_periph);
6468b8a9b1dSJustin T. Gibbs static void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
6478b8a9b1dSJustin T. Gibbs static void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
6488b8a9b1dSJustin T. Gibbs static void	 probecleanup(struct cam_periph *periph);
6498b8a9b1dSJustin T. Gibbs static void	 xpt_find_quirk(struct cam_ed *device);
6508b8a9b1dSJustin T. Gibbs static void	 xpt_set_transfer_settings(struct ccb_trans_settings *cts,
65103e3511bSJustin T. Gibbs 					   struct cam_ed *device,
6528b8a9b1dSJustin T. Gibbs 					   int async_update);
653f0adc790SJustin T. Gibbs static void	 xpt_toggle_tags(struct cam_path *path);
654fd21cc5eSJustin T. Gibbs static void	 xpt_start_tags(struct cam_path *path);
6558b8a9b1dSJustin T. Gibbs static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
6568b8a9b1dSJustin T. Gibbs 					    struct cam_ed *dev);
6578b8a9b1dSJustin T. Gibbs static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
6588b8a9b1dSJustin T. Gibbs 					   struct cam_ed *dev);
6598b8a9b1dSJustin T. Gibbs static __inline int periph_is_queued(struct cam_periph *periph);
6608b8a9b1dSJustin T. Gibbs static __inline int device_is_alloc_queued(struct cam_ed *device);
6618b8a9b1dSJustin T. Gibbs static __inline int device_is_send_queued(struct cam_ed *device);
6628b8a9b1dSJustin T. Gibbs static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
6638b8a9b1dSJustin T. Gibbs 
6648b8a9b1dSJustin T. Gibbs static __inline int
6658b8a9b1dSJustin T. Gibbs xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
6668b8a9b1dSJustin T. Gibbs {
6678b8a9b1dSJustin T. Gibbs 	int retval;
6688b8a9b1dSJustin T. Gibbs 
6698b8a9b1dSJustin T. Gibbs 	if (dev->ccbq.devq_openings > 0) {
6708b8a9b1dSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
6718b8a9b1dSJustin T. Gibbs 			cam_ccbq_resize(&dev->ccbq,
6728b8a9b1dSJustin T. Gibbs 					dev->ccbq.dev_openings
6738b8a9b1dSJustin T. Gibbs 					+ dev->ccbq.dev_active);
6748b8a9b1dSJustin T. Gibbs 			dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
6758b8a9b1dSJustin T. Gibbs 		}
6768b8a9b1dSJustin T. Gibbs 		retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
6778b8a9b1dSJustin T. Gibbs 					  &dev->alloc_ccb_entry.pinfo,
6788b8a9b1dSJustin T. Gibbs 					  dev->drvq.queue_array[0]->priority);
6798b8a9b1dSJustin T. Gibbs 	} else {
6808b8a9b1dSJustin T. Gibbs 		retval = 0;
6818b8a9b1dSJustin T. Gibbs 	}
6828b8a9b1dSJustin T. Gibbs 
6838b8a9b1dSJustin T. Gibbs 	return (retval);
6848b8a9b1dSJustin T. Gibbs }
6858b8a9b1dSJustin T. Gibbs 
6868b8a9b1dSJustin T. Gibbs static __inline int
6878b8a9b1dSJustin T. Gibbs xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
6888b8a9b1dSJustin T. Gibbs {
6898b8a9b1dSJustin T. Gibbs 	int	retval;
6908b8a9b1dSJustin T. Gibbs 
6918b8a9b1dSJustin T. Gibbs 	if (dev->ccbq.dev_openings > 0) {
6928b8a9b1dSJustin T. Gibbs 		retval = xpt_schedule_dev(&bus->sim->devq->send_queue,
6938b8a9b1dSJustin T. Gibbs 					  &dev->send_ccb_entry.pinfo,
6948b8a9b1dSJustin T. Gibbs 					  dev->ccbq.queue.queue_array[0]->priority);
6958b8a9b1dSJustin T. Gibbs 	} else {
6968b8a9b1dSJustin T. Gibbs 		retval = 0;
6978b8a9b1dSJustin T. Gibbs 	}
6988b8a9b1dSJustin T. Gibbs 	return (retval);
6998b8a9b1dSJustin T. Gibbs }
7008b8a9b1dSJustin T. Gibbs 
7018b8a9b1dSJustin T. Gibbs static __inline int
7028b8a9b1dSJustin T. Gibbs periph_is_queued(struct cam_periph *periph)
7038b8a9b1dSJustin T. Gibbs {
7048b8a9b1dSJustin T. Gibbs 	return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
7058b8a9b1dSJustin T. Gibbs }
7068b8a9b1dSJustin T. Gibbs 
7078b8a9b1dSJustin T. Gibbs static __inline int
7088b8a9b1dSJustin T. Gibbs device_is_alloc_queued(struct cam_ed *device)
7098b8a9b1dSJustin T. Gibbs {
7108b8a9b1dSJustin T. Gibbs 	return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
7118b8a9b1dSJustin T. Gibbs }
7128b8a9b1dSJustin T. Gibbs 
7138b8a9b1dSJustin T. Gibbs static __inline int
7148b8a9b1dSJustin T. Gibbs device_is_send_queued(struct cam_ed *device)
7158b8a9b1dSJustin T. Gibbs {
7168b8a9b1dSJustin T. Gibbs 	return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
7178b8a9b1dSJustin T. Gibbs }
7188b8a9b1dSJustin T. Gibbs 
7198b8a9b1dSJustin T. Gibbs static __inline int
7208b8a9b1dSJustin T. Gibbs dev_allocq_is_runnable(struct cam_devq *devq)
7218b8a9b1dSJustin T. Gibbs {
7228b8a9b1dSJustin T. Gibbs 	/*
7238b8a9b1dSJustin T. Gibbs 	 * Have work to do.
7248b8a9b1dSJustin T. Gibbs 	 * Have space to do more work.
7258b8a9b1dSJustin T. Gibbs 	 * Allowed to do work.
7268b8a9b1dSJustin T. Gibbs 	 */
7278b8a9b1dSJustin T. Gibbs 	return ((devq->alloc_queue.qfrozen_cnt == 0)
7288b8a9b1dSJustin T. Gibbs 	     && (devq->alloc_queue.entries > 0)
7298b8a9b1dSJustin T. Gibbs 	     && (devq->alloc_openings > 0));
7308b8a9b1dSJustin T. Gibbs }
7318b8a9b1dSJustin T. Gibbs 
7328b8a9b1dSJustin T. Gibbs static void
7338b8a9b1dSJustin T. Gibbs xpt_periph_init()
7348b8a9b1dSJustin T. Gibbs {
7358b8a9b1dSJustin T. Gibbs 	dev_t dev;
7368b8a9b1dSJustin T. Gibbs 
7378b8a9b1dSJustin T. Gibbs 	dev = makedev(XPT_CDEV_MAJOR, 0);
7388b8a9b1dSJustin T. Gibbs 	cdevsw_add(&dev, &xpt_cdevsw, NULL);
7398b8a9b1dSJustin T. Gibbs }
7408b8a9b1dSJustin T. Gibbs 
7418b8a9b1dSJustin T. Gibbs static void
7428b8a9b1dSJustin T. Gibbs probe_periph_init()
7438b8a9b1dSJustin T. Gibbs {
7448b8a9b1dSJustin T. Gibbs }
7458b8a9b1dSJustin T. Gibbs 
7468b8a9b1dSJustin T. Gibbs 
7478b8a9b1dSJustin T. Gibbs static void
7488b8a9b1dSJustin T. Gibbs xptdone(struct cam_periph *periph, union ccb *done_ccb)
7498b8a9b1dSJustin T. Gibbs {
7508b8a9b1dSJustin T. Gibbs 	/* Caller will release the CCB */
7518b8a9b1dSJustin T. Gibbs 	wakeup(&done_ccb->ccb_h.cbfcnp);
7528b8a9b1dSJustin T. Gibbs }
7538b8a9b1dSJustin T. Gibbs 
7548b8a9b1dSJustin T. Gibbs static int
7558b8a9b1dSJustin T. Gibbs xptopen(dev_t dev, int flags, int fmt, struct proc *p)
7568b8a9b1dSJustin T. Gibbs {
7578b8a9b1dSJustin T. Gibbs 	int unit;
7588b8a9b1dSJustin T. Gibbs 
7598b8a9b1dSJustin T. Gibbs 	unit = minor(dev) & 0xff;
7608b8a9b1dSJustin T. Gibbs 
7618b8a9b1dSJustin T. Gibbs 	/*
76266a0780eSKenneth D. Merry 	 * Only allow read-write access.
76366a0780eSKenneth D. Merry 	 */
76466a0780eSKenneth D. Merry 	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
76566a0780eSKenneth D. Merry 		return(EPERM);
76666a0780eSKenneth D. Merry 
76766a0780eSKenneth D. Merry 	/*
7688b8a9b1dSJustin T. Gibbs 	 * We don't allow nonblocking access.
7698b8a9b1dSJustin T. Gibbs 	 */
7708b8a9b1dSJustin T. Gibbs 	if ((flags & O_NONBLOCK) != 0) {
7718b8a9b1dSJustin T. Gibbs 		printf("xpt%d: can't do nonblocking accesss\n", unit);
7728b8a9b1dSJustin T. Gibbs 		return(ENODEV);
7738b8a9b1dSJustin T. Gibbs 	}
7748b8a9b1dSJustin T. Gibbs 
7758b8a9b1dSJustin T. Gibbs 	/*
7768b8a9b1dSJustin T. Gibbs 	 * We only have one transport layer right now.  If someone accesses
7778b8a9b1dSJustin T. Gibbs 	 * us via something other than minor number 1, point out their
7788b8a9b1dSJustin T. Gibbs 	 * mistake.
7798b8a9b1dSJustin T. Gibbs 	 */
7808b8a9b1dSJustin T. Gibbs 	if (unit != 0) {
7818b8a9b1dSJustin T. Gibbs 		printf("xptopen: got invalid xpt unit %d\n", unit);
7828b8a9b1dSJustin T. Gibbs 		return(ENXIO);
7838b8a9b1dSJustin T. Gibbs 	}
7848b8a9b1dSJustin T. Gibbs 
7858b8a9b1dSJustin T. Gibbs 	/* Mark ourselves open */
7868b8a9b1dSJustin T. Gibbs 	xsoftc.flags |= XPT_FLAG_OPEN;
7878b8a9b1dSJustin T. Gibbs 
7888b8a9b1dSJustin T. Gibbs 	return(0);
7898b8a9b1dSJustin T. Gibbs }
7908b8a9b1dSJustin T. Gibbs 
7918b8a9b1dSJustin T. Gibbs static int
7928b8a9b1dSJustin T. Gibbs xptclose(dev_t dev, int flag, int fmt, struct proc *p)
7938b8a9b1dSJustin T. Gibbs {
7948b8a9b1dSJustin T. Gibbs 	int unit;
7958b8a9b1dSJustin T. Gibbs 
7968b8a9b1dSJustin T. Gibbs 	unit = minor(dev) & 0xff;
7978b8a9b1dSJustin T. Gibbs 
7988b8a9b1dSJustin T. Gibbs 	/*
7998b8a9b1dSJustin T. Gibbs 	 * We only have one transport layer right now.  If someone accesses
8008b8a9b1dSJustin T. Gibbs 	 * us via something other than minor number 1, point out their
8018b8a9b1dSJustin T. Gibbs 	 * mistake.
8028b8a9b1dSJustin T. Gibbs 	 */
8038b8a9b1dSJustin T. Gibbs 	if (unit != 0) {
8048b8a9b1dSJustin T. Gibbs 		printf("xptclose: got invalid xpt unit %d\n", unit);
8058b8a9b1dSJustin T. Gibbs 		return(ENXIO);
8068b8a9b1dSJustin T. Gibbs 	}
8078b8a9b1dSJustin T. Gibbs 
8088b8a9b1dSJustin T. Gibbs 	/* Mark ourselves closed */
8098b8a9b1dSJustin T. Gibbs 	xsoftc.flags &= ~XPT_FLAG_OPEN;
8108b8a9b1dSJustin T. Gibbs 
8118b8a9b1dSJustin T. Gibbs 	return(0);
8128b8a9b1dSJustin T. Gibbs }
8138b8a9b1dSJustin T. Gibbs 
8148b8a9b1dSJustin T. Gibbs static int
8158b8a9b1dSJustin T. Gibbs xptioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
8168b8a9b1dSJustin T. Gibbs {
8178b8a9b1dSJustin T. Gibbs 	int unit, error;
8188b8a9b1dSJustin T. Gibbs 
8198b8a9b1dSJustin T. Gibbs 	error = 0;
8208b8a9b1dSJustin T. Gibbs 	unit = minor(dev) & 0xff;
8218b8a9b1dSJustin T. Gibbs 
8228b8a9b1dSJustin T. Gibbs 	/*
8238b8a9b1dSJustin T. Gibbs 	 * We only have one transport layer right now.  If someone accesses
8248b8a9b1dSJustin T. Gibbs 	 * us via something other than minor number 1, point out their
8258b8a9b1dSJustin T. Gibbs 	 * mistake.
8268b8a9b1dSJustin T. Gibbs 	 */
8278b8a9b1dSJustin T. Gibbs 	if (unit != 0) {
8288b8a9b1dSJustin T. Gibbs 		printf("xptioctl: got invalid xpt unit %d\n", unit);
8298b8a9b1dSJustin T. Gibbs 		return(ENXIO);
8308b8a9b1dSJustin T. Gibbs 	}
8318b8a9b1dSJustin T. Gibbs 
8328b8a9b1dSJustin T. Gibbs 	switch(cmd) {
8338b8a9b1dSJustin T. Gibbs 	/*
8348b8a9b1dSJustin T. Gibbs 	 * For the transport layer CAMIOCOMMAND ioctl, we really only want
8358b8a9b1dSJustin T. Gibbs 	 * to accept CCB types that don't quite make sense to send through a
8368b8a9b1dSJustin T. Gibbs 	 * passthrough driver.
8378b8a9b1dSJustin T. Gibbs 	 */
8388b8a9b1dSJustin T. Gibbs 	case CAMIOCOMMAND: {
8398b8a9b1dSJustin T. Gibbs 		union ccb *ccb;
8408b8a9b1dSJustin T. Gibbs 		union ccb *inccb;
8418b8a9b1dSJustin T. Gibbs 
8428b8a9b1dSJustin T. Gibbs 		inccb = (union ccb *)addr;
8438b8a9b1dSJustin T. Gibbs 
8448b8a9b1dSJustin T. Gibbs 		switch(inccb->ccb_h.func_code) {
8458b8a9b1dSJustin T. Gibbs 		case XPT_SCAN_BUS:
8468b8a9b1dSJustin T. Gibbs 		case XPT_RESET_BUS:
8478b8a9b1dSJustin T. Gibbs 			if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
8488b8a9b1dSJustin T. Gibbs 			 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
8498b8a9b1dSJustin T. Gibbs 				error = EINVAL;
8508b8a9b1dSJustin T. Gibbs 				break;
8518b8a9b1dSJustin T. Gibbs 			}
8528b8a9b1dSJustin T. Gibbs 			/* FALLTHROUGH */
8538b8a9b1dSJustin T. Gibbs 		case XPT_SCAN_LUN:
8548b8a9b1dSJustin T. Gibbs 		case XPT_ENG_INQ:  /* XXX not implemented yet */
8558b8a9b1dSJustin T. Gibbs 		case XPT_ENG_EXEC:
8568b8a9b1dSJustin T. Gibbs 
8578b8a9b1dSJustin T. Gibbs 			ccb = xpt_alloc_ccb();
8588b8a9b1dSJustin T. Gibbs 
8598b8a9b1dSJustin T. Gibbs 			/*
8608b8a9b1dSJustin T. Gibbs 			 * Create a path using the bus, target, and lun the
8618b8a9b1dSJustin T. Gibbs 			 * user passed in.
8628b8a9b1dSJustin T. Gibbs 			 */
8638b8a9b1dSJustin T. Gibbs 			if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
8648b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.path_id,
8658b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_id,
8668b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_lun) !=
8678b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP){
8688b8a9b1dSJustin T. Gibbs 				error = EINVAL;
8698b8a9b1dSJustin T. Gibbs 				xpt_free_ccb(ccb);
8708b8a9b1dSJustin T. Gibbs 				break;
8718b8a9b1dSJustin T. Gibbs 			}
8728b8a9b1dSJustin T. Gibbs 			/* Ensure all of our fields are correct */
8738b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
8748b8a9b1dSJustin T. Gibbs 				      inccb->ccb_h.pinfo.priority);
8758b8a9b1dSJustin T. Gibbs 			xpt_merge_ccb(ccb, inccb);
8768b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.cbfcnp = xptdone;
8778b8a9b1dSJustin T. Gibbs 			cam_periph_runccb(ccb, NULL, 0, 0, NULL);
8788b8a9b1dSJustin T. Gibbs 			bcopy(ccb, inccb, sizeof(union ccb));
8798b8a9b1dSJustin T. Gibbs 			xpt_free_path(ccb->ccb_h.path);
8808b8a9b1dSJustin T. Gibbs 			xpt_free_ccb(ccb);
8818b8a9b1dSJustin T. Gibbs 			break;
8828b8a9b1dSJustin T. Gibbs 
8838b8a9b1dSJustin T. Gibbs 		case XPT_DEBUG: {
8848b8a9b1dSJustin T. Gibbs 			union ccb ccb;
8858b8a9b1dSJustin T. Gibbs 
8868b8a9b1dSJustin T. Gibbs 			/*
887aa872be6SMatt Jacob 			 * This is an immediate CCB, so it's okay to
8888b8a9b1dSJustin T. Gibbs 			 * allocate it on the stack.
8898b8a9b1dSJustin T. Gibbs 			 */
8908b8a9b1dSJustin T. Gibbs 
8918b8a9b1dSJustin T. Gibbs 			/*
8928b8a9b1dSJustin T. Gibbs 			 * Create a path using the bus, target, and lun the
8938b8a9b1dSJustin T. Gibbs 			 * user passed in.
8948b8a9b1dSJustin T. Gibbs 			 */
8958b8a9b1dSJustin T. Gibbs 			if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
8968b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.path_id,
8978b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_id,
8988b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_lun) !=
8998b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP){
9008b8a9b1dSJustin T. Gibbs 				error = EINVAL;
9018b8a9b1dSJustin T. Gibbs 				break;
9028b8a9b1dSJustin T. Gibbs 			}
9038b8a9b1dSJustin T. Gibbs 			/* Ensure all of our fields are correct */
9048b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
9058b8a9b1dSJustin T. Gibbs 				      inccb->ccb_h.pinfo.priority);
9068b8a9b1dSJustin T. Gibbs 			xpt_merge_ccb(&ccb, inccb);
9078b8a9b1dSJustin T. Gibbs 			ccb.ccb_h.cbfcnp = xptdone;
9088b8a9b1dSJustin T. Gibbs 			xpt_action(&ccb);
9098b8a9b1dSJustin T. Gibbs 			bcopy(&ccb, inccb, sizeof(union ccb));
9108b8a9b1dSJustin T. Gibbs 			xpt_free_path(ccb.ccb_h.path);
9118b8a9b1dSJustin T. Gibbs 			break;
9128b8a9b1dSJustin T. Gibbs 
9138b8a9b1dSJustin T. Gibbs 		}
9148b8a9b1dSJustin T. Gibbs 		case XPT_DEV_MATCH: {
9158b8a9b1dSJustin T. Gibbs 			struct cam_periph_map_info mapinfo;
9168b8a9b1dSJustin T. Gibbs 
9178b8a9b1dSJustin T. Gibbs 			/*
9188b8a9b1dSJustin T. Gibbs 			 * We can't deal with physical addresses for this
9198b8a9b1dSJustin T. Gibbs 			 * type of transaction.
9208b8a9b1dSJustin T. Gibbs 			 */
9218b8a9b1dSJustin T. Gibbs 			if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
9228b8a9b1dSJustin T. Gibbs 				error = EINVAL;
9238b8a9b1dSJustin T. Gibbs 				break;
9248b8a9b1dSJustin T. Gibbs 			}
9258b8a9b1dSJustin T. Gibbs 			bzero(&mapinfo, sizeof(mapinfo));
9268b8a9b1dSJustin T. Gibbs 
9278b8a9b1dSJustin T. Gibbs 			/*
9288b8a9b1dSJustin T. Gibbs 			 * Map the pattern and match buffers into kernel
9298b8a9b1dSJustin T. Gibbs 			 * virtual address space.
9308b8a9b1dSJustin T. Gibbs 			 */
9318b8a9b1dSJustin T. Gibbs 			error = cam_periph_mapmem(inccb, &mapinfo);
9328b8a9b1dSJustin T. Gibbs 
9338b8a9b1dSJustin T. Gibbs 			if (error)
9348b8a9b1dSJustin T. Gibbs 				break;
9358b8a9b1dSJustin T. Gibbs 
9368b8a9b1dSJustin T. Gibbs 			/*
9378b8a9b1dSJustin T. Gibbs 			 * This is an immediate CCB, we can send it on directly.
9388b8a9b1dSJustin T. Gibbs 			 */
9398b8a9b1dSJustin T. Gibbs 			xpt_action(inccb);
9408b8a9b1dSJustin T. Gibbs 
9418b8a9b1dSJustin T. Gibbs 			/*
9428b8a9b1dSJustin T. Gibbs 			 * Map the buffers back into user space.
9438b8a9b1dSJustin T. Gibbs 			 */
9448b8a9b1dSJustin T. Gibbs 			cam_periph_unmapmem(inccb, &mapinfo);
9458b8a9b1dSJustin T. Gibbs 
9468b8a9b1dSJustin T. Gibbs 			error = 0;
9478b8a9b1dSJustin T. Gibbs 			break;
9488b8a9b1dSJustin T. Gibbs 		}
9498b8a9b1dSJustin T. Gibbs 		default:
9508b8a9b1dSJustin T. Gibbs 			error = EINVAL;
9518b8a9b1dSJustin T. Gibbs 			break;
9528b8a9b1dSJustin T. Gibbs 		}
9538b8a9b1dSJustin T. Gibbs 		break;
9548b8a9b1dSJustin T. Gibbs 	}
9558b8a9b1dSJustin T. Gibbs 	/*
9568b8a9b1dSJustin T. Gibbs 	 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
9578b8a9b1dSJustin T. Gibbs 	 * with the periphal driver name and unit name filled in.  The other
9588b8a9b1dSJustin T. Gibbs 	 * fields don't really matter as input.  The passthrough driver name
9598b8a9b1dSJustin T. Gibbs 	 * ("pass"), and unit number are passed back in the ccb.  The current
9608b8a9b1dSJustin T. Gibbs 	 * device generation number, and the index into the device peripheral
9618b8a9b1dSJustin T. Gibbs 	 * driver list, and the status are also passed back.  Note that
9628b8a9b1dSJustin T. Gibbs 	 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
9638b8a9b1dSJustin T. Gibbs 	 * we never return a status of CAM_GDEVLIST_LIST_CHANGED.  It is
9648b8a9b1dSJustin T. Gibbs 	 * (or rather should be) impossible for the device peripheral driver
9658b8a9b1dSJustin T. Gibbs 	 * list to change since we look at the whole thing in one pass, and
9668b8a9b1dSJustin T. Gibbs 	 * we do it with splsoftcam protection.
9678b8a9b1dSJustin T. Gibbs 	 *
9688b8a9b1dSJustin T. Gibbs 	 */
9698b8a9b1dSJustin T. Gibbs 	case CAMGETPASSTHRU: {
9708b8a9b1dSJustin T. Gibbs 		union ccb *ccb;
9718b8a9b1dSJustin T. Gibbs 		struct cam_periph *periph;
9728b8a9b1dSJustin T. Gibbs 		struct periph_driver **p_drv;
9738b8a9b1dSJustin T. Gibbs 		char   *name;
9748b8a9b1dSJustin T. Gibbs 		int unit;
9758b8a9b1dSJustin T. Gibbs 		int cur_generation;
976621a60d4SKenneth D. Merry 		int base_periph_found;
9778b8a9b1dSJustin T. Gibbs 		int splbreaknum;
9788b8a9b1dSJustin T. Gibbs 		int s;
9798b8a9b1dSJustin T. Gibbs 
9808b8a9b1dSJustin T. Gibbs 		ccb = (union ccb *)addr;
9818b8a9b1dSJustin T. Gibbs 		unit = ccb->cgdl.unit_number;
9828b8a9b1dSJustin T. Gibbs 		name = ccb->cgdl.periph_name;
9838b8a9b1dSJustin T. Gibbs 		/*
9848b8a9b1dSJustin T. Gibbs 		 * Every 100 devices, we want to drop our spl protection to
9858b8a9b1dSJustin T. Gibbs 		 * give the software interrupt handler a chance to run.
9868b8a9b1dSJustin T. Gibbs 		 * Most systems won't run into this check, but this should
9878b8a9b1dSJustin T. Gibbs 		 * avoid starvation in the software interrupt handler in
9888b8a9b1dSJustin T. Gibbs 		 * large systems.
9898b8a9b1dSJustin T. Gibbs 		 */
9908b8a9b1dSJustin T. Gibbs 		splbreaknum = 100;
9918b8a9b1dSJustin T. Gibbs 
9928b8a9b1dSJustin T. Gibbs 		ccb = (union ccb *)addr;
9938b8a9b1dSJustin T. Gibbs 
994621a60d4SKenneth D. Merry 		base_periph_found = 0;
995621a60d4SKenneth D. Merry 
9968b8a9b1dSJustin T. Gibbs 		/*
9978b8a9b1dSJustin T. Gibbs 		 * Sanity check -- make sure we don't get a null peripheral
9988b8a9b1dSJustin T. Gibbs 		 * driver name.
9998b8a9b1dSJustin T. Gibbs 		 */
10008b8a9b1dSJustin T. Gibbs 		if (*ccb->cgdl.periph_name == '\0') {
10018b8a9b1dSJustin T. Gibbs 			error = EINVAL;
10028b8a9b1dSJustin T. Gibbs 			break;
10038b8a9b1dSJustin T. Gibbs 		}
10048b8a9b1dSJustin T. Gibbs 
10058b8a9b1dSJustin T. Gibbs 		/* Keep the list from changing while we traverse it */
10068b8a9b1dSJustin T. Gibbs 		s = splsoftcam();
10078b8a9b1dSJustin T. Gibbs ptstartover:
10088b8a9b1dSJustin T. Gibbs 		cur_generation = xsoftc.generation;
10098b8a9b1dSJustin T. Gibbs 
10108b8a9b1dSJustin T. Gibbs 		/* first find our driver in the list of drivers */
10118b8a9b1dSJustin T. Gibbs 		for (p_drv = (struct periph_driver **)periphdriver_set.ls_items;
10128b8a9b1dSJustin T. Gibbs 		     *p_drv != NULL; p_drv++)
10138b8a9b1dSJustin T. Gibbs 			if (strcmp((*p_drv)->driver_name, name) == 0)
10148b8a9b1dSJustin T. Gibbs 				break;
10158b8a9b1dSJustin T. Gibbs 
10168b8a9b1dSJustin T. Gibbs 		if (*p_drv == NULL) {
10178b8a9b1dSJustin T. Gibbs 			splx(s);
10188b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
10198b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
10208b8a9b1dSJustin T. Gibbs 			*ccb->cgdl.periph_name = '\0';
10218b8a9b1dSJustin T. Gibbs 			ccb->cgdl.unit_number = 0;
10228b8a9b1dSJustin T. Gibbs 			error = ENOENT;
10238b8a9b1dSJustin T. Gibbs 			break;
10248b8a9b1dSJustin T. Gibbs 		}
10258b8a9b1dSJustin T. Gibbs 
10268b8a9b1dSJustin T. Gibbs 		/*
10278b8a9b1dSJustin T. Gibbs 		 * Run through every peripheral instance of this driver
10288b8a9b1dSJustin T. Gibbs 		 * and check to see whether it matches the unit passed
10298b8a9b1dSJustin T. Gibbs 		 * in by the user.  If it does, get out of the loops and
10308b8a9b1dSJustin T. Gibbs 		 * find the passthrough driver associated with that
10318b8a9b1dSJustin T. Gibbs 		 * peripheral driver.
10328b8a9b1dSJustin T. Gibbs 		 */
10338b8a9b1dSJustin T. Gibbs 		for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
10348b8a9b1dSJustin T. Gibbs 		     periph = TAILQ_NEXT(periph, unit_links)) {
10358b8a9b1dSJustin T. Gibbs 
10368b8a9b1dSJustin T. Gibbs 			if (periph->unit_number == unit) {
10378b8a9b1dSJustin T. Gibbs 				break;
10388b8a9b1dSJustin T. Gibbs 			} else if (--splbreaknum == 0) {
10398b8a9b1dSJustin T. Gibbs 				splx(s);
10408b8a9b1dSJustin T. Gibbs 				s = splsoftcam();
10418b8a9b1dSJustin T. Gibbs 				splbreaknum = 100;
10428b8a9b1dSJustin T. Gibbs 				if (cur_generation != xsoftc.generation)
10438b8a9b1dSJustin T. Gibbs 				       goto ptstartover;
10448b8a9b1dSJustin T. Gibbs 			}
10458b8a9b1dSJustin T. Gibbs 		}
10468b8a9b1dSJustin T. Gibbs 		/*
10478b8a9b1dSJustin T. Gibbs 		 * If we found the peripheral driver that the user passed
10488b8a9b1dSJustin T. Gibbs 		 * in, go through all of the peripheral drivers for that
10498b8a9b1dSJustin T. Gibbs 		 * particular device and look for a passthrough driver.
10508b8a9b1dSJustin T. Gibbs 		 */
10518b8a9b1dSJustin T. Gibbs 		if (periph != NULL) {
10528b8a9b1dSJustin T. Gibbs 			struct cam_ed *device;
10538b8a9b1dSJustin T. Gibbs 			int i;
10548b8a9b1dSJustin T. Gibbs 
1055621a60d4SKenneth D. Merry 			base_periph_found = 1;
10568b8a9b1dSJustin T. Gibbs 			device = periph->path->device;
10578b8a9b1dSJustin T. Gibbs 			for (i = 0, periph = device->periphs.slh_first;
10588b8a9b1dSJustin T. Gibbs 			     periph != NULL;
10598b8a9b1dSJustin T. Gibbs 			     periph = periph->periph_links.sle_next, i++) {
10608b8a9b1dSJustin T. Gibbs 				/*
10618b8a9b1dSJustin T. Gibbs 				 * Check to see whether we have a
10628b8a9b1dSJustin T. Gibbs 				 * passthrough device or not.
10638b8a9b1dSJustin T. Gibbs 				 */
10648b8a9b1dSJustin T. Gibbs 				if (strcmp(periph->periph_name, "pass") == 0) {
10658b8a9b1dSJustin T. Gibbs 					/*
10668b8a9b1dSJustin T. Gibbs 					 * Fill in the getdevlist fields.
10678b8a9b1dSJustin T. Gibbs 					 */
10688b8a9b1dSJustin T. Gibbs 					strcpy(ccb->cgdl.periph_name,
10698b8a9b1dSJustin T. Gibbs 					       periph->periph_name);
10708b8a9b1dSJustin T. Gibbs 					ccb->cgdl.unit_number =
10718b8a9b1dSJustin T. Gibbs 						periph->unit_number;
10728b8a9b1dSJustin T. Gibbs 					if (periph->periph_links.sle_next)
10738b8a9b1dSJustin T. Gibbs 						ccb->cgdl.status =
10748b8a9b1dSJustin T. Gibbs 							CAM_GDEVLIST_MORE_DEVS;
10758b8a9b1dSJustin T. Gibbs 					else
10768b8a9b1dSJustin T. Gibbs 						ccb->cgdl.status =
10778b8a9b1dSJustin T. Gibbs 						       CAM_GDEVLIST_LAST_DEVICE;
10788b8a9b1dSJustin T. Gibbs 					ccb->cgdl.generation =
10798b8a9b1dSJustin T. Gibbs 						device->generation;
10808b8a9b1dSJustin T. Gibbs 					ccb->cgdl.index = i;
10818b8a9b1dSJustin T. Gibbs 					/*
10828b8a9b1dSJustin T. Gibbs 					 * Fill in some CCB header fields
10838b8a9b1dSJustin T. Gibbs 					 * that the user may want.
10848b8a9b1dSJustin T. Gibbs 					 */
10858b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.path_id =
10868b8a9b1dSJustin T. Gibbs 						periph->path->bus->path_id;
10878b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.target_id =
10888b8a9b1dSJustin T. Gibbs 						periph->path->target->target_id;
10898b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.target_lun =
10908b8a9b1dSJustin T. Gibbs 						periph->path->device->lun_id;
10918b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.status = CAM_REQ_CMP;
10928b8a9b1dSJustin T. Gibbs 					break;
10938b8a9b1dSJustin T. Gibbs 				}
10948b8a9b1dSJustin T. Gibbs 			}
10958b8a9b1dSJustin T. Gibbs 		}
10968b8a9b1dSJustin T. Gibbs 
10978b8a9b1dSJustin T. Gibbs 		/*
10988b8a9b1dSJustin T. Gibbs 		 * If the periph is null here, one of two things has
10998b8a9b1dSJustin T. Gibbs 		 * happened.  The first possibility is that we couldn't
11008b8a9b1dSJustin T. Gibbs 		 * find the unit number of the particular peripheral driver
11018b8a9b1dSJustin T. Gibbs 		 * that the user is asking about.  e.g. the user asks for
11028b8a9b1dSJustin T. Gibbs 		 * the passthrough driver for "da11".  We find the list of
11038b8a9b1dSJustin T. Gibbs 		 * "da" peripherals all right, but there is no unit 11.
11048b8a9b1dSJustin T. Gibbs 		 * The other possibility is that we went through the list
11058b8a9b1dSJustin T. Gibbs 		 * of peripheral drivers attached to the device structure,
11068b8a9b1dSJustin T. Gibbs 		 * but didn't find one with the name "pass".  Either way,
11078b8a9b1dSJustin T. Gibbs 		 * we return ENOENT, since we couldn't find something.
11088b8a9b1dSJustin T. Gibbs 		 */
11098b8a9b1dSJustin T. Gibbs 		if (periph == NULL) {
11108b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
11118b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
11128b8a9b1dSJustin T. Gibbs 			*ccb->cgdl.periph_name = '\0';
11138b8a9b1dSJustin T. Gibbs 			ccb->cgdl.unit_number = 0;
11148b8a9b1dSJustin T. Gibbs 			error = ENOENT;
1115621a60d4SKenneth D. Merry 			/*
1116621a60d4SKenneth D. Merry 			 * It is unfortunate that this is even necessary,
1117621a60d4SKenneth D. Merry 			 * but there are many, many clueless users out there.
1118621a60d4SKenneth D. Merry 			 * If this is true, the user is looking for the
1119621a60d4SKenneth D. Merry 			 * passthrough driver, but doesn't have one in his
1120621a60d4SKenneth D. Merry 			 * kernel.
1121621a60d4SKenneth D. Merry 			 */
1122621a60d4SKenneth D. Merry 			if (base_periph_found == 1) {
1123621a60d4SKenneth D. Merry 				printf("xptioctl: pass driver is not in the "
1124621a60d4SKenneth D. Merry 				       "kernel\n");
1125621a60d4SKenneth D. Merry 				printf("xptioctl: put \"device pass0\" in "
1126621a60d4SKenneth D. Merry 				       "your kernel config file\n");
1127621a60d4SKenneth D. Merry 			}
11288b8a9b1dSJustin T. Gibbs 		}
11298b8a9b1dSJustin T. Gibbs 		splx(s);
11308b8a9b1dSJustin T. Gibbs 		break;
11318b8a9b1dSJustin T. Gibbs 		}
11328b8a9b1dSJustin T. Gibbs 	default:
11338b8a9b1dSJustin T. Gibbs 		error = ENOTTY;
11348b8a9b1dSJustin T. Gibbs 		break;
11358b8a9b1dSJustin T. Gibbs 	}
11368b8a9b1dSJustin T. Gibbs 
11378b8a9b1dSJustin T. Gibbs 	return(error);
11388b8a9b1dSJustin T. Gibbs }
11398b8a9b1dSJustin T. Gibbs 
11408b8a9b1dSJustin T. Gibbs /* Functions accessed by the peripheral drivers */
11418b8a9b1dSJustin T. Gibbs void
11428b8a9b1dSJustin T. Gibbs xpt_init()
11438b8a9b1dSJustin T. Gibbs {
11448b8a9b1dSJustin T. Gibbs 	struct cam_sim *xpt_sim;
11458b8a9b1dSJustin T. Gibbs 	struct cam_path *path;
11468b8a9b1dSJustin T. Gibbs 	struct cam_devq;
11478b8a9b1dSJustin T. Gibbs 	cam_status status;
11488b8a9b1dSJustin T. Gibbs 
11498b8a9b1dSJustin T. Gibbs 	TAILQ_INIT(&xpt_busses);
11508b8a9b1dSJustin T. Gibbs 	TAILQ_INIT(&cam_bioq);
11518b8a9b1dSJustin T. Gibbs 	TAILQ_INIT(&cam_netq);
11528b8a9b1dSJustin T. Gibbs 	SLIST_INIT(&ccb_freeq);
11538b8a9b1dSJustin T. Gibbs 	STAILQ_INIT(&highpowerq);
11548b8a9b1dSJustin T. Gibbs 
11558b8a9b1dSJustin T. Gibbs 	/*
11568b8a9b1dSJustin T. Gibbs 	 * The xpt layer is, itself, the equivelent of a SIM.
11578b8a9b1dSJustin T. Gibbs 	 * Allow 16 ccbs in the ccb pool for it.  This should
11588b8a9b1dSJustin T. Gibbs 	 * give decent parallelism when we probe busses and
11598b8a9b1dSJustin T. Gibbs 	 * perform other XPT functions.
11608b8a9b1dSJustin T. Gibbs 	 */
11618b8a9b1dSJustin T. Gibbs 	xpt_sim = (struct cam_sim *)malloc(sizeof(*xpt_sim),
11628b8a9b1dSJustin T. Gibbs 					   M_DEVBUF, M_WAITOK);
11638b8a9b1dSJustin T. Gibbs 	xpt_sim->sim_action = xptaction;
11648b8a9b1dSJustin T. Gibbs 	xpt_sim->sim_name = "xpt";
11658b8a9b1dSJustin T. Gibbs 	xpt_sim->path_id = CAM_XPT_PATH_ID;
11668b8a9b1dSJustin T. Gibbs 	xpt_sim->bus_id = 0;
11678b8a9b1dSJustin T. Gibbs 	xpt_sim->max_tagged_dev_openings = 0;
11688b8a9b1dSJustin T. Gibbs 	xpt_sim->max_dev_openings = 0;
11698b8a9b1dSJustin T. Gibbs 	xpt_sim->devq = cam_simq_alloc(16);
11708b8a9b1dSJustin T. Gibbs 	xpt_max_ccbs = 16;
11718b8a9b1dSJustin T. Gibbs 
11728b8a9b1dSJustin T. Gibbs 	xpt_bus_register(xpt_sim, 0);
11738b8a9b1dSJustin T. Gibbs 
11748b8a9b1dSJustin T. Gibbs 	/*
11758b8a9b1dSJustin T. Gibbs 	 * Looking at the XPT from the SIM layer, the XPT is
11768b8a9b1dSJustin T. Gibbs 	 * the equivelent of a peripheral driver.  Allocate
11778b8a9b1dSJustin T. Gibbs 	 * a peripheral driver entry for us.
11788b8a9b1dSJustin T. Gibbs 	 */
11798b8a9b1dSJustin T. Gibbs 	if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
11808b8a9b1dSJustin T. Gibbs 				      CAM_TARGET_WILDCARD,
11818b8a9b1dSJustin T. Gibbs 				      CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
11828b8a9b1dSJustin T. Gibbs 		printf("xpt_init: xpt_create_path failed with status %#x,"
11838b8a9b1dSJustin T. Gibbs 		       " failing attach\n", status);
11848b8a9b1dSJustin T. Gibbs 		return;
11858b8a9b1dSJustin T. Gibbs 	}
11868b8a9b1dSJustin T. Gibbs 
1187ee9c90c7SKenneth D. Merry 	cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
11888b8a9b1dSJustin T. Gibbs 			 path, NULL, 0, NULL);
11898b8a9b1dSJustin T. Gibbs 	xpt_free_path(path);
11908b8a9b1dSJustin T. Gibbs 
11918b8a9b1dSJustin T. Gibbs 	xpt_sim->softc = xpt_periph;
11928b8a9b1dSJustin T. Gibbs 
11938b8a9b1dSJustin T. Gibbs 	/*
11948b8a9b1dSJustin T. Gibbs 	 * Register a callback for when interrupts are enabled.
11958b8a9b1dSJustin T. Gibbs 	 */
11968b8a9b1dSJustin T. Gibbs 	xpt_config_hook =
11978b8a9b1dSJustin T. Gibbs 	    (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
11988b8a9b1dSJustin T. Gibbs 					      M_TEMP, M_NOWAIT);
11998b8a9b1dSJustin T. Gibbs 	if (xpt_config_hook == NULL) {
12008b8a9b1dSJustin T. Gibbs 		printf("xpt_init: Cannot malloc config hook "
12018b8a9b1dSJustin T. Gibbs 		       "- failing attach\n");
12028b8a9b1dSJustin T. Gibbs 		return;
12038b8a9b1dSJustin T. Gibbs 	}
12048b8a9b1dSJustin T. Gibbs 	bzero(xpt_config_hook, sizeof(*xpt_config_hook));
12058b8a9b1dSJustin T. Gibbs 
12068b8a9b1dSJustin T. Gibbs 	xpt_config_hook->ich_func = xpt_config;
12078b8a9b1dSJustin T. Gibbs 	if (config_intrhook_establish(xpt_config_hook) != 0) {
12088b8a9b1dSJustin T. Gibbs 		free (xpt_config_hook, M_TEMP);
12098b8a9b1dSJustin T. Gibbs 		printf("xpt_init: config_intrhook_establish failed "
12108b8a9b1dSJustin T. Gibbs 		       "- failing attach\n");
12118b8a9b1dSJustin T. Gibbs 	}
12128b8a9b1dSJustin T. Gibbs 
12138b8a9b1dSJustin T. Gibbs 	/* Install our software interrupt handlers */
12145d754af7SMatt Jacob 	register_swi(SWI_CAMNET, swi_camnet);
12155d754af7SMatt Jacob 	register_swi(SWI_CAMBIO, swi_cambio);
12168b8a9b1dSJustin T. Gibbs }
12178b8a9b1dSJustin T. Gibbs 
12188b8a9b1dSJustin T. Gibbs static cam_status
12198b8a9b1dSJustin T. Gibbs xptregister(struct cam_periph *periph, void *arg)
12208b8a9b1dSJustin T. Gibbs {
12218b8a9b1dSJustin T. Gibbs 	if (periph == NULL) {
12228b8a9b1dSJustin T. Gibbs 		printf("xptregister: periph was NULL!!\n");
12238b8a9b1dSJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
12248b8a9b1dSJustin T. Gibbs 	}
12258b8a9b1dSJustin T. Gibbs 
12268b8a9b1dSJustin T. Gibbs 	periph->softc = NULL;
12278b8a9b1dSJustin T. Gibbs 
12288b8a9b1dSJustin T. Gibbs 	xpt_periph = periph;
12298b8a9b1dSJustin T. Gibbs 
12308b8a9b1dSJustin T. Gibbs 	return(CAM_REQ_CMP);
12318b8a9b1dSJustin T. Gibbs }
12328b8a9b1dSJustin T. Gibbs 
12338b8a9b1dSJustin T. Gibbs int32_t
12348b8a9b1dSJustin T. Gibbs xpt_add_periph(struct cam_periph *periph)
12358b8a9b1dSJustin T. Gibbs {
12368b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
12378b8a9b1dSJustin T. Gibbs 	int32_t	 status;
12388b8a9b1dSJustin T. Gibbs 	struct periph_list *periph_head;
12398b8a9b1dSJustin T. Gibbs 
12408b8a9b1dSJustin T. Gibbs 	device = periph->path->device;
12418b8a9b1dSJustin T. Gibbs 
12428b8a9b1dSJustin T. Gibbs 	periph_head = &device->periphs;
12438b8a9b1dSJustin T. Gibbs 
12448b8a9b1dSJustin T. Gibbs 	status = CAM_REQ_CMP;
12458b8a9b1dSJustin T. Gibbs 
12468b8a9b1dSJustin T. Gibbs 	if (device != NULL) {
12478b8a9b1dSJustin T. Gibbs 		int s;
12488b8a9b1dSJustin T. Gibbs 
12498b8a9b1dSJustin T. Gibbs 		/*
12508b8a9b1dSJustin T. Gibbs 		 * Make room for this peripheral
12518b8a9b1dSJustin T. Gibbs 		 * so it will fit in the queue
12528b8a9b1dSJustin T. Gibbs 		 * when it's scheduled to run
12538b8a9b1dSJustin T. Gibbs 		 */
12548b8a9b1dSJustin T. Gibbs 		s = splsoftcam();
12558b8a9b1dSJustin T. Gibbs 		status = camq_resize(&device->drvq,
12568b8a9b1dSJustin T. Gibbs 				     device->drvq.array_size + 1);
12578b8a9b1dSJustin T. Gibbs 
12588b8a9b1dSJustin T. Gibbs 		device->generation++;
12598b8a9b1dSJustin T. Gibbs 
12608b8a9b1dSJustin T. Gibbs 		SLIST_INSERT_HEAD(periph_head, periph, periph_links);
12618b8a9b1dSJustin T. Gibbs 
12628b8a9b1dSJustin T. Gibbs 		splx(s);
12638b8a9b1dSJustin T. Gibbs 	}
12648b8a9b1dSJustin T. Gibbs 
12658b8a9b1dSJustin T. Gibbs 	xsoftc.generation++;
12668b8a9b1dSJustin T. Gibbs 
12678b8a9b1dSJustin T. Gibbs 	return (status);
12688b8a9b1dSJustin T. Gibbs }
12698b8a9b1dSJustin T. Gibbs 
12708b8a9b1dSJustin T. Gibbs void
12718b8a9b1dSJustin T. Gibbs xpt_remove_periph(struct cam_periph *periph)
12728b8a9b1dSJustin T. Gibbs {
12738b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
12748b8a9b1dSJustin T. Gibbs 
12758b8a9b1dSJustin T. Gibbs 	device = periph->path->device;
12768b8a9b1dSJustin T. Gibbs 
12778b8a9b1dSJustin T. Gibbs 	if (device != NULL) {
12788b8a9b1dSJustin T. Gibbs 		int s;
12798b8a9b1dSJustin T. Gibbs 		struct periph_list *periph_head;
12808b8a9b1dSJustin T. Gibbs 
12818b8a9b1dSJustin T. Gibbs 		periph_head = &device->periphs;
12828b8a9b1dSJustin T. Gibbs 
12838b8a9b1dSJustin T. Gibbs 		/* Release the slot for this peripheral */
12848b8a9b1dSJustin T. Gibbs 		s = splsoftcam();
12858b8a9b1dSJustin T. Gibbs 		camq_resize(&device->drvq, device->drvq.array_size - 1);
12868b8a9b1dSJustin T. Gibbs 
12878b8a9b1dSJustin T. Gibbs 		device->generation++;
12888b8a9b1dSJustin T. Gibbs 
12898b8a9b1dSJustin T. Gibbs 		SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
12908b8a9b1dSJustin T. Gibbs 
12918b8a9b1dSJustin T. Gibbs 		splx(s);
12928b8a9b1dSJustin T. Gibbs 	}
12938b8a9b1dSJustin T. Gibbs 
12948b8a9b1dSJustin T. Gibbs 	xsoftc.generation++;
12958b8a9b1dSJustin T. Gibbs 
12968b8a9b1dSJustin T. Gibbs }
12978b8a9b1dSJustin T. Gibbs 
12988b8a9b1dSJustin T. Gibbs void
12998b8a9b1dSJustin T. Gibbs xpt_announce_periph(struct cam_periph *periph, char *announce_string)
13008b8a9b1dSJustin T. Gibbs {
13018b8a9b1dSJustin T. Gibbs 	int s;
13028b8a9b1dSJustin T. Gibbs 	u_int mb;
13038b8a9b1dSJustin T. Gibbs 	struct cam_path *path;
13048b8a9b1dSJustin T. Gibbs 	struct ccb_trans_settings cts;
13058b8a9b1dSJustin T. Gibbs 
13068b8a9b1dSJustin T. Gibbs 	path = periph->path;
13078b8a9b1dSJustin T. Gibbs 	/*
13088b8a9b1dSJustin T. Gibbs 	 * To ensure that this is printed in one piece,
13098b8a9b1dSJustin T. Gibbs 	 * mask out CAM interrupts.
13108b8a9b1dSJustin T. Gibbs 	 */
13118b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
13128b8a9b1dSJustin T. Gibbs 	printf("%s%d at %s%d bus %d target %d lun %d\n",
13138b8a9b1dSJustin T. Gibbs 	       periph->periph_name, periph->unit_number,
13148b8a9b1dSJustin T. Gibbs 	       path->bus->sim->sim_name,
13158b8a9b1dSJustin T. Gibbs 	       path->bus->sim->unit_number,
13168b8a9b1dSJustin T. Gibbs 	       path->bus->sim->bus_id,
13178b8a9b1dSJustin T. Gibbs 	       path->target->target_id,
13188b8a9b1dSJustin T. Gibbs 	       path->device->lun_id);
13198b8a9b1dSJustin T. Gibbs 	printf("%s%d: ", periph->periph_name, periph->unit_number);
13208b8a9b1dSJustin T. Gibbs 	scsi_print_inquiry(&path->device->inq_data);
13218b8a9b1dSJustin T. Gibbs 	if ((bootverbose)
13228b8a9b1dSJustin T. Gibbs 	 && (path->device->serial_num_len > 0)) {
13238b8a9b1dSJustin T. Gibbs 		/* Don't wrap the screen  - print only the first 60 chars */
13248b8a9b1dSJustin T. Gibbs 		printf("%s%d: Serial Number %.60s\n", periph->periph_name,
13258b8a9b1dSJustin T. Gibbs 		       periph->unit_number, path->device->serial_num);
13268b8a9b1dSJustin T. Gibbs 	}
13278b8a9b1dSJustin T. Gibbs 	xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
13288b8a9b1dSJustin T. Gibbs 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
13298b8a9b1dSJustin T. Gibbs 	cts.flags = CCB_TRANS_CURRENT_SETTINGS;
13308b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb*)&cts);
13318b8a9b1dSJustin T. Gibbs 	if (cts.ccb_h.status == CAM_REQ_CMP) {
13328b8a9b1dSJustin T. Gibbs 		u_int speed;
13338b8a9b1dSJustin T. Gibbs 		u_int freq;
13348b8a9b1dSJustin T. Gibbs 
13358b8a9b1dSJustin T. Gibbs 		if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
13368b8a9b1dSJustin T. Gibbs 		  && cts.sync_offset != 0) {
13378b8a9b1dSJustin T. Gibbs 			freq = scsi_calc_syncsrate(cts.sync_period);
13388b8a9b1dSJustin T. Gibbs 			speed = freq;
13398b8a9b1dSJustin T. Gibbs 		} else {
13408b8a9b1dSJustin T. Gibbs 			freq = 0;
13418b8a9b1dSJustin T. Gibbs 			speed = path->bus->sim->base_transfer_speed;
13428b8a9b1dSJustin T. Gibbs 		}
13438b8a9b1dSJustin T. Gibbs 		if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
13448b8a9b1dSJustin T. Gibbs 			speed *= (0x01 << cts.bus_width);
13458b8a9b1dSJustin T. Gibbs 		mb = speed / 1000;
13468b8a9b1dSJustin T. Gibbs 		if (mb > 0)
13478b8a9b1dSJustin T. Gibbs 			printf("%s%d: %d.%dMB/s transfers", periph->periph_name,
13488b8a9b1dSJustin T. Gibbs 			       periph->unit_number, mb, speed % 1000);
13498b8a9b1dSJustin T. Gibbs 		else
13508b8a9b1dSJustin T. Gibbs 			printf("%s%d: %dKB/s transfers", periph->periph_name,
13518b8a9b1dSJustin T. Gibbs 			       periph->unit_number, (speed % 1000) * 1000);
13528b8a9b1dSJustin T. Gibbs 		if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
13538b8a9b1dSJustin T. Gibbs 		 && cts.sync_offset != 0) {
13548b8a9b1dSJustin T. Gibbs 			printf(" (%d.%dMHz, offset %d", freq / 1000,
13558b8a9b1dSJustin T. Gibbs 			       freq % 1000, cts.sync_offset);
13568b8a9b1dSJustin T. Gibbs 		}
13578b8a9b1dSJustin T. Gibbs 		if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0
13588b8a9b1dSJustin T. Gibbs 		 && cts.bus_width > 0) {
13598b8a9b1dSJustin T. Gibbs 			if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
13608b8a9b1dSJustin T. Gibbs 			 && cts.sync_offset != 0) {
13618b8a9b1dSJustin T. Gibbs 				printf(", ");
13628b8a9b1dSJustin T. Gibbs 			} else {
13638b8a9b1dSJustin T. Gibbs 				printf(" (");
13648b8a9b1dSJustin T. Gibbs 			}
13658b8a9b1dSJustin T. Gibbs 			printf("%dbit)", 8 * (0x01 << cts.bus_width));
13668b8a9b1dSJustin T. Gibbs 		} else if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
13678b8a9b1dSJustin T. Gibbs 			&& cts.sync_offset != 0) {
13688b8a9b1dSJustin T. Gibbs 			printf(")");
13698b8a9b1dSJustin T. Gibbs 		}
1370fd21cc5eSJustin T. Gibbs 
1371fd21cc5eSJustin T. Gibbs 		if (path->device->inq_flags & SID_CmdQue
1372fd21cc5eSJustin T. Gibbs 		 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
13738b8a9b1dSJustin T. Gibbs 			printf(", Tagged Queueing Enabled");
13748b8a9b1dSJustin T. Gibbs 		}
13758b8a9b1dSJustin T. Gibbs 
13768b8a9b1dSJustin T. Gibbs 		printf("\n");
13772ba01e6fSJustin T. Gibbs 	} else if (path->device->inq_flags & SID_CmdQue
13782ba01e6fSJustin T. Gibbs    		|| path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
13798b8a9b1dSJustin T. Gibbs 		printf("%s%d: Tagged Queueing Enabled\n",
13808b8a9b1dSJustin T. Gibbs 		       periph->periph_name, periph->unit_number);
13818b8a9b1dSJustin T. Gibbs 	}
13828b8a9b1dSJustin T. Gibbs 
13838b8a9b1dSJustin T. Gibbs 	/*
13848b8a9b1dSJustin T. Gibbs 	 * We only want to print the caller's announce string if they've
13858b8a9b1dSJustin T. Gibbs 	 * passed one in..
13868b8a9b1dSJustin T. Gibbs 	 */
13878b8a9b1dSJustin T. Gibbs 	if (announce_string != NULL)
13888b8a9b1dSJustin T. Gibbs 		printf("%s%d: %s\n", periph->periph_name,
13898b8a9b1dSJustin T. Gibbs 		       periph->unit_number, announce_string);
13908b8a9b1dSJustin T. Gibbs 	splx(s);
13918b8a9b1dSJustin T. Gibbs }
13928b8a9b1dSJustin T. Gibbs 
13938b8a9b1dSJustin T. Gibbs 
13948b8a9b1dSJustin T. Gibbs static dev_match_ret
13958b8a9b1dSJustin T. Gibbs xptbusmatch(struct dev_match_pattern *patterns, int num_patterns,
13968b8a9b1dSJustin T. Gibbs 	    struct cam_eb *bus)
13978b8a9b1dSJustin T. Gibbs {
13988b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
13998b8a9b1dSJustin T. Gibbs 	int i;
14008b8a9b1dSJustin T. Gibbs 
14018b8a9b1dSJustin T. Gibbs 	retval = DM_RET_NONE;
14028b8a9b1dSJustin T. Gibbs 
14038b8a9b1dSJustin T. Gibbs 	/*
14048b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
14058b8a9b1dSJustin T. Gibbs 	 */
14068b8a9b1dSJustin T. Gibbs 	if (bus == NULL)
14078b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
14088b8a9b1dSJustin T. Gibbs 
14098b8a9b1dSJustin T. Gibbs 	/*
14108b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this bus matches no
14118b8a9b1dSJustin T. Gibbs 	 * matter what.
14128b8a9b1dSJustin T. Gibbs 	 */
14138b8a9b1dSJustin T. Gibbs 	if ((patterns == NULL) || (num_patterns == 0))
14148b8a9b1dSJustin T. Gibbs 		return(DM_RET_DESCEND | DM_RET_COPY);
14158b8a9b1dSJustin T. Gibbs 
14168b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
14178b8a9b1dSJustin T. Gibbs 		struct bus_match_pattern *cur_pattern;
14188b8a9b1dSJustin T. Gibbs 
14198b8a9b1dSJustin T. Gibbs 		/*
14208b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a bus node, we
14218b8a9b1dSJustin T. Gibbs 		 * aren't interested.  However, we do indicate to the
14228b8a9b1dSJustin T. Gibbs 		 * calling routine that we should continue descending the
14238b8a9b1dSJustin T. Gibbs 		 * tree, since the user wants to match against lower-level
14248b8a9b1dSJustin T. Gibbs 		 * EDT elements.
14258b8a9b1dSJustin T. Gibbs 		 */
14268b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_BUS) {
14278b8a9b1dSJustin T. Gibbs 			if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
14288b8a9b1dSJustin T. Gibbs 				retval |= DM_RET_DESCEND;
14298b8a9b1dSJustin T. Gibbs 			continue;
14308b8a9b1dSJustin T. Gibbs 		}
14318b8a9b1dSJustin T. Gibbs 
14328b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.bus_pattern;
14338b8a9b1dSJustin T. Gibbs 
14348b8a9b1dSJustin T. Gibbs 		/*
14358b8a9b1dSJustin T. Gibbs 		 * If they want to match any bus node, we give them any
14368b8a9b1dSJustin T. Gibbs 		 * device node.
14378b8a9b1dSJustin T. Gibbs 		 */
14388b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == BUS_MATCH_ANY) {
14398b8a9b1dSJustin T. Gibbs 			/* set the copy flag */
14408b8a9b1dSJustin T. Gibbs 			retval |= DM_RET_COPY;
14418b8a9b1dSJustin T. Gibbs 
14428b8a9b1dSJustin T. Gibbs 			/*
14438b8a9b1dSJustin T. Gibbs 			 * If we've already decided on an action, go ahead
14448b8a9b1dSJustin T. Gibbs 			 * and return.
14458b8a9b1dSJustin T. Gibbs 			 */
14468b8a9b1dSJustin T. Gibbs 			if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
14478b8a9b1dSJustin T. Gibbs 				return(retval);
14488b8a9b1dSJustin T. Gibbs 		}
14498b8a9b1dSJustin T. Gibbs 
14508b8a9b1dSJustin T. Gibbs 		/*
14518b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
14528b8a9b1dSJustin T. Gibbs 		 */
14538b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == BUS_MATCH_NONE)
14548b8a9b1dSJustin T. Gibbs 			continue;
14558b8a9b1dSJustin T. Gibbs 
14568b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
14578b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != bus->path_id))
14588b8a9b1dSJustin T. Gibbs 			continue;
14598b8a9b1dSJustin T. Gibbs 
14608b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
14618b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->bus_id != bus->sim->bus_id))
14628b8a9b1dSJustin T. Gibbs 			continue;
14638b8a9b1dSJustin T. Gibbs 
14648b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
14658b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->unit_number != bus->sim->unit_number))
14668b8a9b1dSJustin T. Gibbs 			continue;
14678b8a9b1dSJustin T. Gibbs 
14688b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
14698b8a9b1dSJustin T. Gibbs 		 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
14708b8a9b1dSJustin T. Gibbs 			     DEV_IDLEN) != 0))
14718b8a9b1dSJustin T. Gibbs 			continue;
14728b8a9b1dSJustin T. Gibbs 
14738b8a9b1dSJustin T. Gibbs 		/*
14748b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
14758b8a9b1dSJustin T. Gibbs 		 * information on this bus.  So tell the caller to copy the
14768b8a9b1dSJustin T. Gibbs 		 * data out.
14778b8a9b1dSJustin T. Gibbs 		 */
14788b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
14798b8a9b1dSJustin T. Gibbs 
14808b8a9b1dSJustin T. Gibbs 		/*
14818b8a9b1dSJustin T. Gibbs 		 * If the return action has been set to descend, then we
14828b8a9b1dSJustin T. Gibbs 		 * know that we've already seen a non-bus matching
14838b8a9b1dSJustin T. Gibbs 		 * expression, therefore we need to further descend the tree.
14848b8a9b1dSJustin T. Gibbs 		 * This won't change by continuing around the loop, so we
14858b8a9b1dSJustin T. Gibbs 		 * go ahead and return.  If we haven't seen a non-bus
14868b8a9b1dSJustin T. Gibbs 		 * matching expression, we keep going around the loop until
14878b8a9b1dSJustin T. Gibbs 		 * we exhaust the matching expressions.  We'll set the stop
14888b8a9b1dSJustin T. Gibbs 		 * flag once we fall out of the loop.
14898b8a9b1dSJustin T. Gibbs 		 */
14908b8a9b1dSJustin T. Gibbs 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
14918b8a9b1dSJustin T. Gibbs 			return(retval);
14928b8a9b1dSJustin T. Gibbs 	}
14938b8a9b1dSJustin T. Gibbs 
14948b8a9b1dSJustin T. Gibbs 	/*
14958b8a9b1dSJustin T. Gibbs 	 * If the return action hasn't been set to descend yet, that means
14968b8a9b1dSJustin T. Gibbs 	 * we haven't seen anything other than bus matching patterns.  So
14978b8a9b1dSJustin T. Gibbs 	 * tell the caller to stop descending the tree -- the user doesn't
14988b8a9b1dSJustin T. Gibbs 	 * want to match against lower level tree elements.
14998b8a9b1dSJustin T. Gibbs 	 */
15008b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
15018b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_STOP;
15028b8a9b1dSJustin T. Gibbs 
15038b8a9b1dSJustin T. Gibbs 	return(retval);
15048b8a9b1dSJustin T. Gibbs }
15058b8a9b1dSJustin T. Gibbs 
15068b8a9b1dSJustin T. Gibbs static dev_match_ret
15078b8a9b1dSJustin T. Gibbs xptdevicematch(struct dev_match_pattern *patterns, int num_patterns,
15088b8a9b1dSJustin T. Gibbs 	       struct cam_ed *device)
15098b8a9b1dSJustin T. Gibbs {
15108b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
15118b8a9b1dSJustin T. Gibbs 	int i;
15128b8a9b1dSJustin T. Gibbs 
15138b8a9b1dSJustin T. Gibbs 	retval = DM_RET_NONE;
15148b8a9b1dSJustin T. Gibbs 
15158b8a9b1dSJustin T. Gibbs 	/*
15168b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
15178b8a9b1dSJustin T. Gibbs 	 */
15188b8a9b1dSJustin T. Gibbs 	if (device == NULL)
15198b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
15208b8a9b1dSJustin T. Gibbs 
15218b8a9b1dSJustin T. Gibbs 	/*
15228b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this device matches no
15238b8a9b1dSJustin T. Gibbs 	 * matter what.
15248b8a9b1dSJustin T. Gibbs 	 */
15258b8a9b1dSJustin T. Gibbs 	if ((patterns == NULL) || (patterns == 0))
15268b8a9b1dSJustin T. Gibbs 		return(DM_RET_DESCEND | DM_RET_COPY);
15278b8a9b1dSJustin T. Gibbs 
15288b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
15298b8a9b1dSJustin T. Gibbs 		struct device_match_pattern *cur_pattern;
15308b8a9b1dSJustin T. Gibbs 
15318b8a9b1dSJustin T. Gibbs 		/*
15328b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a device node, we
15338b8a9b1dSJustin T. Gibbs 		 * aren't interested.
15348b8a9b1dSJustin T. Gibbs 		 */
15358b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_DEVICE) {
15368b8a9b1dSJustin T. Gibbs 			if ((patterns[i].type == DEV_MATCH_PERIPH)
15378b8a9b1dSJustin T. Gibbs 			 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
15388b8a9b1dSJustin T. Gibbs 				retval |= DM_RET_DESCEND;
15398b8a9b1dSJustin T. Gibbs 			continue;
15408b8a9b1dSJustin T. Gibbs 		}
15418b8a9b1dSJustin T. Gibbs 
15428b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.device_pattern;
15438b8a9b1dSJustin T. Gibbs 
15448b8a9b1dSJustin T. Gibbs 		/*
15458b8a9b1dSJustin T. Gibbs 		 * If they want to match any device node, we give them any
15468b8a9b1dSJustin T. Gibbs 		 * device node.
15478b8a9b1dSJustin T. Gibbs 		 */
15488b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == DEV_MATCH_ANY) {
15498b8a9b1dSJustin T. Gibbs 			/* set the copy flag */
15508b8a9b1dSJustin T. Gibbs 			retval |= DM_RET_COPY;
15518b8a9b1dSJustin T. Gibbs 
15528b8a9b1dSJustin T. Gibbs 
15538b8a9b1dSJustin T. Gibbs 			/*
15548b8a9b1dSJustin T. Gibbs 			 * If we've already decided on an action, go ahead
15558b8a9b1dSJustin T. Gibbs 			 * and return.
15568b8a9b1dSJustin T. Gibbs 			 */
15578b8a9b1dSJustin T. Gibbs 			if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
15588b8a9b1dSJustin T. Gibbs 				return(retval);
15598b8a9b1dSJustin T. Gibbs 		}
15608b8a9b1dSJustin T. Gibbs 
15618b8a9b1dSJustin T. Gibbs 		/*
15628b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
15638b8a9b1dSJustin T. Gibbs 		 */
15648b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == DEV_MATCH_NONE)
15658b8a9b1dSJustin T. Gibbs 			continue;
15668b8a9b1dSJustin T. Gibbs 
15678b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
15688b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != device->target->bus->path_id))
15698b8a9b1dSJustin T. Gibbs 			continue;
15708b8a9b1dSJustin T. Gibbs 
15718b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
15728b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->target_id != device->target->target_id))
15738b8a9b1dSJustin T. Gibbs 			continue;
15748b8a9b1dSJustin T. Gibbs 
15758b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
15768b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->target_lun != device->lun_id))
15778b8a9b1dSJustin T. Gibbs 			continue;
15788b8a9b1dSJustin T. Gibbs 
15798b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
15808b8a9b1dSJustin T. Gibbs 		 && (cam_quirkmatch((caddr_t)&device->inq_data,
15818b8a9b1dSJustin T. Gibbs 				    (caddr_t)&cur_pattern->inq_pat,
15828b8a9b1dSJustin T. Gibbs 				    1, sizeof(cur_pattern->inq_pat),
15838b8a9b1dSJustin T. Gibbs 				    scsi_static_inquiry_match) == NULL))
15848b8a9b1dSJustin T. Gibbs 			continue;
15858b8a9b1dSJustin T. Gibbs 
15868b8a9b1dSJustin T. Gibbs 		/*
15878b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
15888b8a9b1dSJustin T. Gibbs 		 * information on this device.  So tell the caller to copy
15898b8a9b1dSJustin T. Gibbs 		 * the data out.
15908b8a9b1dSJustin T. Gibbs 		 */
15918b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
15928b8a9b1dSJustin T. Gibbs 
15938b8a9b1dSJustin T. Gibbs 		/*
15948b8a9b1dSJustin T. Gibbs 		 * If the return action has been set to descend, then we
15958b8a9b1dSJustin T. Gibbs 		 * know that we've already seen a peripheral matching
15968b8a9b1dSJustin T. Gibbs 		 * expression, therefore we need to further descend the tree.
15978b8a9b1dSJustin T. Gibbs 		 * This won't change by continuing around the loop, so we
15988b8a9b1dSJustin T. Gibbs 		 * go ahead and return.  If we haven't seen a peripheral
15998b8a9b1dSJustin T. Gibbs 		 * matching expression, we keep going around the loop until
16008b8a9b1dSJustin T. Gibbs 		 * we exhaust the matching expressions.  We'll set the stop
16018b8a9b1dSJustin T. Gibbs 		 * flag once we fall out of the loop.
16028b8a9b1dSJustin T. Gibbs 		 */
16038b8a9b1dSJustin T. Gibbs 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
16048b8a9b1dSJustin T. Gibbs 			return(retval);
16058b8a9b1dSJustin T. Gibbs 	}
16068b8a9b1dSJustin T. Gibbs 
16078b8a9b1dSJustin T. Gibbs 	/*
16088b8a9b1dSJustin T. Gibbs 	 * If the return action hasn't been set to descend yet, that means
16098b8a9b1dSJustin T. Gibbs 	 * we haven't seen any peripheral matching patterns.  So tell the
16108b8a9b1dSJustin T. Gibbs 	 * caller to stop descending the tree -- the user doesn't want to
16118b8a9b1dSJustin T. Gibbs 	 * match against lower level tree elements.
16128b8a9b1dSJustin T. Gibbs 	 */
16138b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
16148b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_STOP;
16158b8a9b1dSJustin T. Gibbs 
16168b8a9b1dSJustin T. Gibbs 	return(retval);
16178b8a9b1dSJustin T. Gibbs }
16188b8a9b1dSJustin T. Gibbs 
16198b8a9b1dSJustin T. Gibbs /*
16208b8a9b1dSJustin T. Gibbs  * Match a single peripheral against any number of match patterns.
16218b8a9b1dSJustin T. Gibbs  */
16228b8a9b1dSJustin T. Gibbs static dev_match_ret
16238b8a9b1dSJustin T. Gibbs xptperiphmatch(struct dev_match_pattern *patterns, int num_patterns,
16248b8a9b1dSJustin T. Gibbs 	       struct cam_periph *periph)
16258b8a9b1dSJustin T. Gibbs {
16268b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
16278b8a9b1dSJustin T. Gibbs 	int i;
16288b8a9b1dSJustin T. Gibbs 
16298b8a9b1dSJustin T. Gibbs 	/*
16308b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
16318b8a9b1dSJustin T. Gibbs 	 */
16328b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
16338b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
16348b8a9b1dSJustin T. Gibbs 
16358b8a9b1dSJustin T. Gibbs 	/*
16368b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this peripheral matches no
16378b8a9b1dSJustin T. Gibbs 	 * matter what.
16388b8a9b1dSJustin T. Gibbs 	 */
16398b8a9b1dSJustin T. Gibbs 	if ((patterns == NULL) || (num_patterns == 0))
16408b8a9b1dSJustin T. Gibbs 		return(DM_RET_STOP | DM_RET_COPY);
16418b8a9b1dSJustin T. Gibbs 
16428b8a9b1dSJustin T. Gibbs 	/*
16438b8a9b1dSJustin T. Gibbs 	 * There aren't any nodes below a peripheral node, so there's no
16448b8a9b1dSJustin T. Gibbs 	 * reason to descend the tree any further.
16458b8a9b1dSJustin T. Gibbs 	 */
16468b8a9b1dSJustin T. Gibbs 	retval = DM_RET_STOP;
16478b8a9b1dSJustin T. Gibbs 
16488b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
16498b8a9b1dSJustin T. Gibbs 		struct periph_match_pattern *cur_pattern;
16508b8a9b1dSJustin T. Gibbs 
16518b8a9b1dSJustin T. Gibbs 		/*
16528b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a peripheral, we
16538b8a9b1dSJustin T. Gibbs 		 * aren't interested.
16548b8a9b1dSJustin T. Gibbs 		 */
16558b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_PERIPH)
16568b8a9b1dSJustin T. Gibbs 			continue;
16578b8a9b1dSJustin T. Gibbs 
16588b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.periph_pattern;
16598b8a9b1dSJustin T. Gibbs 
16608b8a9b1dSJustin T. Gibbs 		/*
16618b8a9b1dSJustin T. Gibbs 		 * If they want to match on anything, then we will do so.
16628b8a9b1dSJustin T. Gibbs 		 */
16638b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == PERIPH_MATCH_ANY) {
16648b8a9b1dSJustin T. Gibbs 			/* set the copy flag */
16658b8a9b1dSJustin T. Gibbs 			retval |= DM_RET_COPY;
16668b8a9b1dSJustin T. Gibbs 
16678b8a9b1dSJustin T. Gibbs 			/*
16688b8a9b1dSJustin T. Gibbs 			 * We've already set the return action to stop,
16698b8a9b1dSJustin T. Gibbs 			 * since there are no nodes below peripherals in
16708b8a9b1dSJustin T. Gibbs 			 * the tree.
16718b8a9b1dSJustin T. Gibbs 			 */
16728b8a9b1dSJustin T. Gibbs 			return(retval);
16738b8a9b1dSJustin T. Gibbs 		}
16748b8a9b1dSJustin T. Gibbs 
16758b8a9b1dSJustin T. Gibbs 		/*
16768b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
16778b8a9b1dSJustin T. Gibbs 		 */
16788b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == PERIPH_MATCH_NONE)
16798b8a9b1dSJustin T. Gibbs 			continue;
16808b8a9b1dSJustin T. Gibbs 
16818b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
16828b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != periph->path->bus->path_id))
16838b8a9b1dSJustin T. Gibbs 			continue;
16848b8a9b1dSJustin T. Gibbs 
16858b8a9b1dSJustin T. Gibbs 		/*
16868b8a9b1dSJustin T. Gibbs 		 * For the target and lun id's, we have to make sure the
16878b8a9b1dSJustin T. Gibbs 		 * target and lun pointers aren't NULL.  The xpt peripheral
16888b8a9b1dSJustin T. Gibbs 		 * has a wildcard target and device.
16898b8a9b1dSJustin T. Gibbs 		 */
16908b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
16918b8a9b1dSJustin T. Gibbs 		 && ((periph->path->target == NULL)
16928b8a9b1dSJustin T. Gibbs 		 ||(cur_pattern->target_id != periph->path->target->target_id)))
16938b8a9b1dSJustin T. Gibbs 			continue;
16948b8a9b1dSJustin T. Gibbs 
16958b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
16968b8a9b1dSJustin T. Gibbs 		 && ((periph->path->device == NULL)
16978b8a9b1dSJustin T. Gibbs 		 || (cur_pattern->target_lun != periph->path->device->lun_id)))
16988b8a9b1dSJustin T. Gibbs 			continue;
16998b8a9b1dSJustin T. Gibbs 
17008b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
17018b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->unit_number != periph->unit_number))
17028b8a9b1dSJustin T. Gibbs 			continue;
17038b8a9b1dSJustin T. Gibbs 
17048b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
17058b8a9b1dSJustin T. Gibbs 		 && (strncmp(cur_pattern->periph_name, periph->periph_name,
17068b8a9b1dSJustin T. Gibbs 			     DEV_IDLEN) != 0))
17078b8a9b1dSJustin T. Gibbs 			continue;
17088b8a9b1dSJustin T. Gibbs 
17098b8a9b1dSJustin T. Gibbs 		/*
17108b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
17118b8a9b1dSJustin T. Gibbs 		 * information on this peripheral.  So tell the caller to
17128b8a9b1dSJustin T. Gibbs 		 * copy the data out.
17138b8a9b1dSJustin T. Gibbs 		 */
17148b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
17158b8a9b1dSJustin T. Gibbs 
17168b8a9b1dSJustin T. Gibbs 		/*
17178b8a9b1dSJustin T. Gibbs 		 * The return action has already been set to stop, since
17188b8a9b1dSJustin T. Gibbs 		 * peripherals don't have any nodes below them in the EDT.
17198b8a9b1dSJustin T. Gibbs 		 */
17208b8a9b1dSJustin T. Gibbs 		return(retval);
17218b8a9b1dSJustin T. Gibbs 	}
17228b8a9b1dSJustin T. Gibbs 
17238b8a9b1dSJustin T. Gibbs 	/*
17248b8a9b1dSJustin T. Gibbs 	 * If we get to this point, the peripheral that was passed in
17258b8a9b1dSJustin T. Gibbs 	 * doesn't match any of the patterns.
17268b8a9b1dSJustin T. Gibbs 	 */
17278b8a9b1dSJustin T. Gibbs 	return(retval);
17288b8a9b1dSJustin T. Gibbs }
17298b8a9b1dSJustin T. Gibbs 
17308b8a9b1dSJustin T. Gibbs static int
17318b8a9b1dSJustin T. Gibbs xptedtbusfunc(struct cam_eb *bus, void *arg)
17328b8a9b1dSJustin T. Gibbs {
17338b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
17348b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
17358b8a9b1dSJustin T. Gibbs 
17368b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
17378b8a9b1dSJustin T. Gibbs 
17388b8a9b1dSJustin T. Gibbs 	/*
17398b8a9b1dSJustin T. Gibbs 	 * If our position is for something deeper in the tree, that means
17408b8a9b1dSJustin T. Gibbs 	 * that we've already seen this node.  So, we keep going down.
17418b8a9b1dSJustin T. Gibbs 	 */
17428b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
17438b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == bus)
17448b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
17458b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target != NULL))
17468b8a9b1dSJustin T. Gibbs 		retval = DM_RET_DESCEND;
17478b8a9b1dSJustin T. Gibbs 	else
17488b8a9b1dSJustin T. Gibbs 		retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
17498b8a9b1dSJustin T. Gibbs 
17508b8a9b1dSJustin T. Gibbs 	/*
17518b8a9b1dSJustin T. Gibbs 	 * If we got an error, bail out of the search.
17528b8a9b1dSJustin T. Gibbs 	 */
17538b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
17548b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
17558b8a9b1dSJustin T. Gibbs 		return(0);
17568b8a9b1dSJustin T. Gibbs 	}
17578b8a9b1dSJustin T. Gibbs 
17588b8a9b1dSJustin T. Gibbs 	/*
17598b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this bus out.
17608b8a9b1dSJustin T. Gibbs 	 */
17618b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
17628b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
17638b8a9b1dSJustin T. Gibbs 
17648b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
17658b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
17668b8a9b1dSJustin T. Gibbs 
17678b8a9b1dSJustin T. Gibbs 		/*
17688b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
17698b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
17708b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
17718b8a9b1dSJustin T. Gibbs 		 */
17728b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
17738b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
17748b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
17758b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
17768b8a9b1dSJustin T. Gibbs 
17778b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = bus;
17788b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
17798b8a9b1dSJustin T. Gibbs 				bus_generation;
17808b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
17818b8a9b1dSJustin T. Gibbs 			return(0);
17828b8a9b1dSJustin T. Gibbs 		}
17838b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
17848b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
17858b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_BUS;
17868b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.path_id = bus->path_id;
17878b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
17888b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.unit_number =
17898b8a9b1dSJustin T. Gibbs 			bus->sim->unit_number;
17908b8a9b1dSJustin T. Gibbs 		strncpy(cdm->matches[j].result.bus_result.dev_name,
17918b8a9b1dSJustin T. Gibbs 			bus->sim->sim_name, DEV_IDLEN);
17928b8a9b1dSJustin T. Gibbs 	}
17938b8a9b1dSJustin T. Gibbs 
17948b8a9b1dSJustin T. Gibbs 	/*
17958b8a9b1dSJustin T. Gibbs 	 * If the user is only interested in busses, there's no
17968b8a9b1dSJustin T. Gibbs 	 * reason to descend to the next level in the tree.
17978b8a9b1dSJustin T. Gibbs 	 */
17988b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
17998b8a9b1dSJustin T. Gibbs 		return(1);
18008b8a9b1dSJustin T. Gibbs 
18018b8a9b1dSJustin T. Gibbs 	/*
18028b8a9b1dSJustin T. Gibbs 	 * If there is a target generation recorded, check it to
18038b8a9b1dSJustin T. Gibbs 	 * make sure the target list hasn't changed.
18048b8a9b1dSJustin T. Gibbs 	 */
18058b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
18068b8a9b1dSJustin T. Gibbs 	 && (bus == cdm->pos.cookie.bus)
18078b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
18088b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
18098b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
18108b8a9b1dSJustin T. Gibbs 	     bus->generation)) {
18118b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
18128b8a9b1dSJustin T. Gibbs 		return(0);
18138b8a9b1dSJustin T. Gibbs 	}
18148b8a9b1dSJustin T. Gibbs 
18158b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
18168b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == bus)
18178b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
18188b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target != NULL))
18198b8a9b1dSJustin T. Gibbs 		return(xpttargettraverse(bus,
18208b8a9b1dSJustin T. Gibbs 					(struct cam_et *)cdm->pos.cookie.target,
18218b8a9b1dSJustin T. Gibbs 					 xptedttargetfunc, arg));
18228b8a9b1dSJustin T. Gibbs 	else
18238b8a9b1dSJustin T. Gibbs 		return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
18248b8a9b1dSJustin T. Gibbs }
18258b8a9b1dSJustin T. Gibbs 
18268b8a9b1dSJustin T. Gibbs static int
18278b8a9b1dSJustin T. Gibbs xptedttargetfunc(struct cam_et *target, void *arg)
18288b8a9b1dSJustin T. Gibbs {
18298b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
18308b8a9b1dSJustin T. Gibbs 
18318b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
18328b8a9b1dSJustin T. Gibbs 
18338b8a9b1dSJustin T. Gibbs 	/*
18348b8a9b1dSJustin T. Gibbs 	 * If there is a device list generation recorded, check it to
18358b8a9b1dSJustin T. Gibbs 	 * make sure the device list hasn't changed.
18368b8a9b1dSJustin T. Gibbs 	 */
18378b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
18388b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == target->bus)
18398b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
18408b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == target)
18418b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
18428b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
18438b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
18448b8a9b1dSJustin T. Gibbs 	     target->generation)) {
18458b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
18468b8a9b1dSJustin T. Gibbs 		return(0);
18478b8a9b1dSJustin T. Gibbs 	}
18488b8a9b1dSJustin T. Gibbs 
18498b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
18508b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == target->bus)
18518b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
18528b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == target)
18538b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
18548b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device != NULL))
18558b8a9b1dSJustin T. Gibbs 		return(xptdevicetraverse(target,
18568b8a9b1dSJustin T. Gibbs 					(struct cam_ed *)cdm->pos.cookie.device,
18578b8a9b1dSJustin T. Gibbs 					 xptedtdevicefunc, arg));
18588b8a9b1dSJustin T. Gibbs 	else
18598b8a9b1dSJustin T. Gibbs 		return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
18608b8a9b1dSJustin T. Gibbs }
18618b8a9b1dSJustin T. Gibbs 
18628b8a9b1dSJustin T. Gibbs static int
18638b8a9b1dSJustin T. Gibbs xptedtdevicefunc(struct cam_ed *device, void *arg)
18648b8a9b1dSJustin T. Gibbs {
18658b8a9b1dSJustin T. Gibbs 
18668b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
18678b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
18688b8a9b1dSJustin T. Gibbs 
18698b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
18708b8a9b1dSJustin T. Gibbs 
18718b8a9b1dSJustin T. Gibbs 	/*
18728b8a9b1dSJustin T. Gibbs 	 * If our position is for something deeper in the tree, that means
18738b8a9b1dSJustin T. Gibbs 	 * that we've already seen this node.  So, we keep going down.
18748b8a9b1dSJustin T. Gibbs 	 */
18758b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
18768b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device == device)
18778b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
18788b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.periph != NULL))
18798b8a9b1dSJustin T. Gibbs 		retval = DM_RET_DESCEND;
18808b8a9b1dSJustin T. Gibbs 	else
18818b8a9b1dSJustin T. Gibbs 		retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
18828b8a9b1dSJustin T. Gibbs 					device);
18838b8a9b1dSJustin T. Gibbs 
18848b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
18858b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
18868b8a9b1dSJustin T. Gibbs 		return(0);
18878b8a9b1dSJustin T. Gibbs 	}
18888b8a9b1dSJustin T. Gibbs 
18898b8a9b1dSJustin T. Gibbs 	/*
18908b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this device out.
18918b8a9b1dSJustin T. Gibbs 	 */
18928b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
18938b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
18948b8a9b1dSJustin T. Gibbs 
18958b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
18968b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
18978b8a9b1dSJustin T. Gibbs 
18988b8a9b1dSJustin T. Gibbs 		/*
18998b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
19008b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
19018b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
19028b8a9b1dSJustin T. Gibbs 		 */
19038b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
19048b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
19058b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
19068b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
19078b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
19088b8a9b1dSJustin T. Gibbs 
19098b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = device->target->bus;
19108b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
19118b8a9b1dSJustin T. Gibbs 				bus_generation;
19128b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.target = device->target;
19138b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_TARGET_GENERATION] =
19148b8a9b1dSJustin T. Gibbs 				device->target->bus->generation;
19158b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.device = device;
19168b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_DEV_GENERATION] =
19178b8a9b1dSJustin T. Gibbs 				device->target->generation;
19188b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
19198b8a9b1dSJustin T. Gibbs 			return(0);
19208b8a9b1dSJustin T. Gibbs 		}
19218b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
19228b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
19238b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_DEVICE;
19248b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.path_id =
19258b8a9b1dSJustin T. Gibbs 			device->target->bus->path_id;
19268b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.target_id =
19278b8a9b1dSJustin T. Gibbs 			device->target->target_id;
19288b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.target_lun =
19298b8a9b1dSJustin T. Gibbs 			device->lun_id;
19308b8a9b1dSJustin T. Gibbs 		bcopy(&device->inq_data,
19318b8a9b1dSJustin T. Gibbs 		      &cdm->matches[j].result.device_result.inq_data,
19328b8a9b1dSJustin T. Gibbs 		      sizeof(struct scsi_inquiry_data));
19338b8a9b1dSJustin T. Gibbs 	}
19348b8a9b1dSJustin T. Gibbs 
19358b8a9b1dSJustin T. Gibbs 	/*
19368b8a9b1dSJustin T. Gibbs 	 * If the user isn't interested in peripherals, don't descend
19378b8a9b1dSJustin T. Gibbs 	 * the tree any further.
19388b8a9b1dSJustin T. Gibbs 	 */
19398b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
19408b8a9b1dSJustin T. Gibbs 		return(1);
19418b8a9b1dSJustin T. Gibbs 
19428b8a9b1dSJustin T. Gibbs 	/*
19438b8a9b1dSJustin T. Gibbs 	 * If there is a peripheral list generation recorded, make sure
19448b8a9b1dSJustin T. Gibbs 	 * it hasn't changed.
19458b8a9b1dSJustin T. Gibbs 	 */
19468b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
19478b8a9b1dSJustin T. Gibbs 	 && (device->target->bus == cdm->pos.cookie.bus)
19488b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
19498b8a9b1dSJustin T. Gibbs 	 && (device->target == cdm->pos.cookie.target)
19508b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
19518b8a9b1dSJustin T. Gibbs 	 && (device == cdm->pos.cookie.device)
19528b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
19538b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
19548b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
19558b8a9b1dSJustin T. Gibbs 	     device->generation)){
19568b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
19578b8a9b1dSJustin T. Gibbs 		return(0);
19588b8a9b1dSJustin T. Gibbs 	}
19598b8a9b1dSJustin T. Gibbs 
19608b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
19618b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == device->target->bus)
19628b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
19638b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == device->target)
19648b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
19658b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device == device)
19668b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
19678b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.periph != NULL))
19688b8a9b1dSJustin T. Gibbs 		return(xptperiphtraverse(device,
19698b8a9b1dSJustin T. Gibbs 				(struct cam_periph *)cdm->pos.cookie.periph,
19708b8a9b1dSJustin T. Gibbs 				xptedtperiphfunc, arg));
19718b8a9b1dSJustin T. Gibbs 	else
19728b8a9b1dSJustin T. Gibbs 		return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
19738b8a9b1dSJustin T. Gibbs }
19748b8a9b1dSJustin T. Gibbs 
19758b8a9b1dSJustin T. Gibbs static int
19768b8a9b1dSJustin T. Gibbs xptedtperiphfunc(struct cam_periph *periph, void *arg)
19778b8a9b1dSJustin T. Gibbs {
19788b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
19798b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
19808b8a9b1dSJustin T. Gibbs 
19818b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
19828b8a9b1dSJustin T. Gibbs 
19838b8a9b1dSJustin T. Gibbs 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
19848b8a9b1dSJustin T. Gibbs 
19858b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
19868b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
19878b8a9b1dSJustin T. Gibbs 		return(0);
19888b8a9b1dSJustin T. Gibbs 	}
19898b8a9b1dSJustin T. Gibbs 
19908b8a9b1dSJustin T. Gibbs 	/*
19918b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this peripheral out.
19928b8a9b1dSJustin T. Gibbs 	 */
19938b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
19948b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
19958b8a9b1dSJustin T. Gibbs 
19968b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
19978b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
19988b8a9b1dSJustin T. Gibbs 
19998b8a9b1dSJustin T. Gibbs 		/*
20008b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
20018b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
20028b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
20038b8a9b1dSJustin T. Gibbs 		 */
20048b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
20058b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
20068b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
20078b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
20088b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
20098b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PERIPH;
20108b8a9b1dSJustin T. Gibbs 
20118b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = periph->path->bus;
20128b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
20138b8a9b1dSJustin T. Gibbs 				bus_generation;
20148b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.target = periph->path->target;
20158b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_TARGET_GENERATION] =
20168b8a9b1dSJustin T. Gibbs 				periph->path->bus->generation;
20178b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.device = periph->path->device;
20188b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_DEV_GENERATION] =
20198b8a9b1dSJustin T. Gibbs 				periph->path->target->generation;
20208b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.periph = periph;
20218b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
20228b8a9b1dSJustin T. Gibbs 				periph->path->device->generation;
20238b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
20248b8a9b1dSJustin T. Gibbs 			return(0);
20258b8a9b1dSJustin T. Gibbs 		}
20268b8a9b1dSJustin T. Gibbs 
20278b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
20288b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
20298b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_PERIPH;
20308b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.path_id =
20318b8a9b1dSJustin T. Gibbs 			periph->path->bus->path_id;
20328b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.target_id =
20338b8a9b1dSJustin T. Gibbs 			periph->path->target->target_id;
20348b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.target_lun =
20358b8a9b1dSJustin T. Gibbs 			periph->path->device->lun_id;
20368b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.unit_number =
20378b8a9b1dSJustin T. Gibbs 			periph->unit_number;
20388b8a9b1dSJustin T. Gibbs 		strncpy(cdm->matches[j].result.periph_result.periph_name,
20398b8a9b1dSJustin T. Gibbs 			periph->periph_name, DEV_IDLEN);
20408b8a9b1dSJustin T. Gibbs 	}
20418b8a9b1dSJustin T. Gibbs 
20428b8a9b1dSJustin T. Gibbs 	return(1);
20438b8a9b1dSJustin T. Gibbs }
20448b8a9b1dSJustin T. Gibbs 
20458b8a9b1dSJustin T. Gibbs static int
20468b8a9b1dSJustin T. Gibbs xptedtmatch(struct ccb_dev_match *cdm)
20478b8a9b1dSJustin T. Gibbs {
20488b8a9b1dSJustin T. Gibbs 	int ret;
20498b8a9b1dSJustin T. Gibbs 
20508b8a9b1dSJustin T. Gibbs 	cdm->num_matches = 0;
20518b8a9b1dSJustin T. Gibbs 
20528b8a9b1dSJustin T. Gibbs 	/*
20538b8a9b1dSJustin T. Gibbs 	 * Check the bus list generation.  If it has changed, the user
20548b8a9b1dSJustin T. Gibbs 	 * needs to reset everything and start over.
20558b8a9b1dSJustin T. Gibbs 	 */
20568b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
20578b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
20588b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
20598b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
20608b8a9b1dSJustin T. Gibbs 		return(0);
20618b8a9b1dSJustin T. Gibbs 	}
20628b8a9b1dSJustin T. Gibbs 
20638b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
20648b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus != NULL))
20658b8a9b1dSJustin T. Gibbs 		ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
20668b8a9b1dSJustin T. Gibbs 				     xptedtbusfunc, cdm);
20678b8a9b1dSJustin T. Gibbs 	else
20688b8a9b1dSJustin T. Gibbs 		ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
20698b8a9b1dSJustin T. Gibbs 
20708b8a9b1dSJustin T. Gibbs 	/*
20718b8a9b1dSJustin T. Gibbs 	 * If we get back 0, that means that we had to stop before fully
20728b8a9b1dSJustin T. Gibbs 	 * traversing the EDT.  It also means that one of the subroutines
20738b8a9b1dSJustin T. Gibbs 	 * has set the status field to the proper value.  If we get back 1,
20748b8a9b1dSJustin T. Gibbs 	 * we've fully traversed the EDT and copied out any matching entries.
20758b8a9b1dSJustin T. Gibbs 	 */
20768b8a9b1dSJustin T. Gibbs 	if (ret == 1)
20778b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LAST;
20788b8a9b1dSJustin T. Gibbs 
20798b8a9b1dSJustin T. Gibbs 	return(ret);
20808b8a9b1dSJustin T. Gibbs }
20818b8a9b1dSJustin T. Gibbs 
20828b8a9b1dSJustin T. Gibbs static int
20838b8a9b1dSJustin T. Gibbs xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
20848b8a9b1dSJustin T. Gibbs {
20858b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
20868b8a9b1dSJustin T. Gibbs 
20878b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
20888b8a9b1dSJustin T. Gibbs 
20898b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
20908b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv == pdrv)
20918b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
20928b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
20938b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
20948b8a9b1dSJustin T. Gibbs 	     (*pdrv)->generation)) {
20958b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
20968b8a9b1dSJustin T. Gibbs 		return(0);
20978b8a9b1dSJustin T. Gibbs 	}
20988b8a9b1dSJustin T. Gibbs 
20998b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
21008b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv == pdrv)
21018b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
21028b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.periph != NULL))
21038b8a9b1dSJustin T. Gibbs 		return(xptpdperiphtraverse(pdrv,
21048b8a9b1dSJustin T. Gibbs 				(struct cam_periph *)cdm->pos.cookie.periph,
21058b8a9b1dSJustin T. Gibbs 				xptplistperiphfunc, arg));
21068b8a9b1dSJustin T. Gibbs 	else
21078b8a9b1dSJustin T. Gibbs 		return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
21088b8a9b1dSJustin T. Gibbs }
21098b8a9b1dSJustin T. Gibbs 
21108b8a9b1dSJustin T. Gibbs static int
21118b8a9b1dSJustin T. Gibbs xptplistperiphfunc(struct cam_periph *periph, void *arg)
21128b8a9b1dSJustin T. Gibbs {
21138b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
21148b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
21158b8a9b1dSJustin T. Gibbs 
21168b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
21178b8a9b1dSJustin T. Gibbs 
21188b8a9b1dSJustin T. Gibbs 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
21198b8a9b1dSJustin T. Gibbs 
21208b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
21218b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
21228b8a9b1dSJustin T. Gibbs 		return(0);
21238b8a9b1dSJustin T. Gibbs 	}
21248b8a9b1dSJustin T. Gibbs 
21258b8a9b1dSJustin T. Gibbs 	/*
21268b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this peripheral out.
21278b8a9b1dSJustin T. Gibbs 	 */
21288b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
21298b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
21308b8a9b1dSJustin T. Gibbs 
21318b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
21328b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
21338b8a9b1dSJustin T. Gibbs 
21348b8a9b1dSJustin T. Gibbs 		/*
21358b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
21368b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
21378b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
21388b8a9b1dSJustin T. Gibbs 		 */
21398b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
21408b8a9b1dSJustin T. Gibbs 			struct periph_driver **pdrv;
21418b8a9b1dSJustin T. Gibbs 
21428b8a9b1dSJustin T. Gibbs 			pdrv = NULL;
21438b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
21448b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
21458b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
21468b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PERIPH;
21478b8a9b1dSJustin T. Gibbs 
21488b8a9b1dSJustin T. Gibbs 			/*
21498b8a9b1dSJustin T. Gibbs 			 * This may look a bit non-sensical, but it is
21508b8a9b1dSJustin T. Gibbs 			 * actually quite logical.  There are very few
21518b8a9b1dSJustin T. Gibbs 			 * peripheral drivers, and bloating every peripheral
21528b8a9b1dSJustin T. Gibbs 			 * structure with a pointer back to its parent
21538b8a9b1dSJustin T. Gibbs 			 * peripheral driver linker set entry would cost
21548b8a9b1dSJustin T. Gibbs 			 * more in the long run than doing this quick lookup.
21558b8a9b1dSJustin T. Gibbs 			 */
21568b8a9b1dSJustin T. Gibbs 			for (pdrv =
21578b8a9b1dSJustin T. Gibbs 			     (struct periph_driver **)periphdriver_set.ls_items;
21588b8a9b1dSJustin T. Gibbs 			     *pdrv != NULL; pdrv++) {
21598b8a9b1dSJustin T. Gibbs 				if (strcmp((*pdrv)->driver_name,
21608b8a9b1dSJustin T. Gibbs 				    periph->periph_name) == 0)
21618b8a9b1dSJustin T. Gibbs 					break;
21628b8a9b1dSJustin T. Gibbs 			}
21638b8a9b1dSJustin T. Gibbs 
21648b8a9b1dSJustin T. Gibbs 			if (pdrv == NULL) {
21658b8a9b1dSJustin T. Gibbs 				cdm->status = CAM_DEV_MATCH_ERROR;
21668b8a9b1dSJustin T. Gibbs 				return(0);
21678b8a9b1dSJustin T. Gibbs 			}
21688b8a9b1dSJustin T. Gibbs 
21698b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.pdrv = pdrv;
21708b8a9b1dSJustin T. Gibbs 			/*
21718b8a9b1dSJustin T. Gibbs 			 * The periph generation slot does double duty, as
21728b8a9b1dSJustin T. Gibbs 			 * does the periph pointer slot.  They are used for
21738b8a9b1dSJustin T. Gibbs 			 * both edt and pdrv lookups and positioning.
21748b8a9b1dSJustin T. Gibbs 			 */
21758b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.periph = periph;
21768b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
21778b8a9b1dSJustin T. Gibbs 				(*pdrv)->generation;
21788b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
21798b8a9b1dSJustin T. Gibbs 			return(0);
21808b8a9b1dSJustin T. Gibbs 		}
21818b8a9b1dSJustin T. Gibbs 
21828b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
21838b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
21848b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_PERIPH;
21858b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.path_id =
21868b8a9b1dSJustin T. Gibbs 			periph->path->bus->path_id;
21878b8a9b1dSJustin T. Gibbs 
21888b8a9b1dSJustin T. Gibbs 		/*
21898b8a9b1dSJustin T. Gibbs 		 * The transport layer peripheral doesn't have a target or
21908b8a9b1dSJustin T. Gibbs 		 * lun.
21918b8a9b1dSJustin T. Gibbs 		 */
21928b8a9b1dSJustin T. Gibbs 		if (periph->path->target)
21938b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_id =
21948b8a9b1dSJustin T. Gibbs 				periph->path->target->target_id;
21958b8a9b1dSJustin T. Gibbs 		else
21968b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_id = -1;
21978b8a9b1dSJustin T. Gibbs 
21988b8a9b1dSJustin T. Gibbs 		if (periph->path->device)
21998b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_lun =
22008b8a9b1dSJustin T. Gibbs 				periph->path->device->lun_id;
22018b8a9b1dSJustin T. Gibbs 		else
22028b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_lun = -1;
22038b8a9b1dSJustin T. Gibbs 
22048b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.unit_number =
22058b8a9b1dSJustin T. Gibbs 			periph->unit_number;
22068b8a9b1dSJustin T. Gibbs 		strncpy(cdm->matches[j].result.periph_result.periph_name,
22078b8a9b1dSJustin T. Gibbs 			periph->periph_name, DEV_IDLEN);
22088b8a9b1dSJustin T. Gibbs 	}
22098b8a9b1dSJustin T. Gibbs 
22108b8a9b1dSJustin T. Gibbs 	return(1);
22118b8a9b1dSJustin T. Gibbs }
22128b8a9b1dSJustin T. Gibbs 
22138b8a9b1dSJustin T. Gibbs static int
22148b8a9b1dSJustin T. Gibbs xptperiphlistmatch(struct ccb_dev_match *cdm)
22158b8a9b1dSJustin T. Gibbs {
22168b8a9b1dSJustin T. Gibbs 	int ret;
22178b8a9b1dSJustin T. Gibbs 
22188b8a9b1dSJustin T. Gibbs 	cdm->num_matches = 0;
22198b8a9b1dSJustin T. Gibbs 
22208b8a9b1dSJustin T. Gibbs 	/*
22218b8a9b1dSJustin T. Gibbs 	 * At this point in the edt traversal function, we check the bus
22228b8a9b1dSJustin T. Gibbs 	 * list generation to make sure that no busses have been added or
22238b8a9b1dSJustin T. Gibbs 	 * removed since the user last sent a XPT_DEV_MATCH ccb through.
22248b8a9b1dSJustin T. Gibbs 	 * For the peripheral driver list traversal function, however, we
22258b8a9b1dSJustin T. Gibbs 	 * don't have to worry about new peripheral driver types coming or
22268b8a9b1dSJustin T. Gibbs 	 * going; they're in a linker set, and therefore can't change
22278b8a9b1dSJustin T. Gibbs 	 * without a recompile.
22288b8a9b1dSJustin T. Gibbs 	 */
22298b8a9b1dSJustin T. Gibbs 
22308b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
22318b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv != NULL))
22328b8a9b1dSJustin T. Gibbs 		ret = xptpdrvtraverse(
22338b8a9b1dSJustin T. Gibbs 			        (struct periph_driver **)cdm->pos.cookie.pdrv,
22348b8a9b1dSJustin T. Gibbs 				xptplistpdrvfunc, cdm);
22358b8a9b1dSJustin T. Gibbs 	else
22368b8a9b1dSJustin T. Gibbs 		ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
22378b8a9b1dSJustin T. Gibbs 
22388b8a9b1dSJustin T. Gibbs 	/*
22398b8a9b1dSJustin T. Gibbs 	 * If we get back 0, that means that we had to stop before fully
22408b8a9b1dSJustin T. Gibbs 	 * traversing the peripheral driver tree.  It also means that one of
22418b8a9b1dSJustin T. Gibbs 	 * the subroutines has set the status field to the proper value.  If
22428b8a9b1dSJustin T. Gibbs 	 * we get back 1, we've fully traversed the EDT and copied out any
22438b8a9b1dSJustin T. Gibbs 	 * matching entries.
22448b8a9b1dSJustin T. Gibbs 	 */
22458b8a9b1dSJustin T. Gibbs 	if (ret == 1)
22468b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LAST;
22478b8a9b1dSJustin T. Gibbs 
22488b8a9b1dSJustin T. Gibbs 	return(ret);
22498b8a9b1dSJustin T. Gibbs }
22508b8a9b1dSJustin T. Gibbs 
22518b8a9b1dSJustin T. Gibbs static int
22528b8a9b1dSJustin T. Gibbs xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
22538b8a9b1dSJustin T. Gibbs {
22548b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus, *next_bus;
22558b8a9b1dSJustin T. Gibbs 	int retval;
22568b8a9b1dSJustin T. Gibbs 
22578b8a9b1dSJustin T. Gibbs 	retval = 1;
22588b8a9b1dSJustin T. Gibbs 
22598b8a9b1dSJustin T. Gibbs 	for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
22608b8a9b1dSJustin T. Gibbs 	     bus != NULL;
22618b8a9b1dSJustin T. Gibbs 	     bus = next_bus) {
22628b8a9b1dSJustin T. Gibbs 		next_bus = TAILQ_NEXT(bus, links);
22638b8a9b1dSJustin T. Gibbs 
22648b8a9b1dSJustin T. Gibbs 		retval = tr_func(bus, arg);
22658b8a9b1dSJustin T. Gibbs 		if (retval == 0)
22668b8a9b1dSJustin T. Gibbs 			return(retval);
22678b8a9b1dSJustin T. Gibbs 	}
22688b8a9b1dSJustin T. Gibbs 
22698b8a9b1dSJustin T. Gibbs 	return(retval);
22708b8a9b1dSJustin T. Gibbs }
22718b8a9b1dSJustin T. Gibbs 
22728b8a9b1dSJustin T. Gibbs static int
22738b8a9b1dSJustin T. Gibbs xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
22748b8a9b1dSJustin T. Gibbs 		  xpt_targetfunc_t *tr_func, void *arg)
22758b8a9b1dSJustin T. Gibbs {
22768b8a9b1dSJustin T. Gibbs 	struct cam_et *target, *next_target;
22778b8a9b1dSJustin T. Gibbs 	int retval;
22788b8a9b1dSJustin T. Gibbs 
22798b8a9b1dSJustin T. Gibbs 	retval = 1;
22808b8a9b1dSJustin T. Gibbs 	for (target = (start_target ? start_target :
22818b8a9b1dSJustin T. Gibbs 		       TAILQ_FIRST(&bus->et_entries));
22828b8a9b1dSJustin T. Gibbs 	     target != NULL; target = next_target) {
22838b8a9b1dSJustin T. Gibbs 
22848b8a9b1dSJustin T. Gibbs 		next_target = TAILQ_NEXT(target, links);
22858b8a9b1dSJustin T. Gibbs 
22868b8a9b1dSJustin T. Gibbs 		retval = tr_func(target, arg);
22878b8a9b1dSJustin T. Gibbs 
22888b8a9b1dSJustin T. Gibbs 		if (retval == 0)
22898b8a9b1dSJustin T. Gibbs 			return(retval);
22908b8a9b1dSJustin T. Gibbs 	}
22918b8a9b1dSJustin T. Gibbs 
22928b8a9b1dSJustin T. Gibbs 	return(retval);
22938b8a9b1dSJustin T. Gibbs }
22948b8a9b1dSJustin T. Gibbs 
22958b8a9b1dSJustin T. Gibbs static int
22968b8a9b1dSJustin T. Gibbs xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
22978b8a9b1dSJustin T. Gibbs 		  xpt_devicefunc_t *tr_func, void *arg)
22988b8a9b1dSJustin T. Gibbs {
22998b8a9b1dSJustin T. Gibbs 	struct cam_ed *device, *next_device;
23008b8a9b1dSJustin T. Gibbs 	int retval;
23018b8a9b1dSJustin T. Gibbs 
23028b8a9b1dSJustin T. Gibbs 	retval = 1;
23038b8a9b1dSJustin T. Gibbs 	for (device = (start_device ? start_device :
23048b8a9b1dSJustin T. Gibbs 		       TAILQ_FIRST(&target->ed_entries));
23058b8a9b1dSJustin T. Gibbs 	     device != NULL;
23068b8a9b1dSJustin T. Gibbs 	     device = next_device) {
23078b8a9b1dSJustin T. Gibbs 
23088b8a9b1dSJustin T. Gibbs 		next_device = TAILQ_NEXT(device, links);
23098b8a9b1dSJustin T. Gibbs 
23108b8a9b1dSJustin T. Gibbs 		retval = tr_func(device, arg);
23118b8a9b1dSJustin T. Gibbs 
23128b8a9b1dSJustin T. Gibbs 		if (retval == 0)
23138b8a9b1dSJustin T. Gibbs 			return(retval);
23148b8a9b1dSJustin T. Gibbs 	}
23158b8a9b1dSJustin T. Gibbs 
23168b8a9b1dSJustin T. Gibbs 	return(retval);
23178b8a9b1dSJustin T. Gibbs }
23188b8a9b1dSJustin T. Gibbs 
23198b8a9b1dSJustin T. Gibbs static int
23208b8a9b1dSJustin T. Gibbs xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
23218b8a9b1dSJustin T. Gibbs 		  xpt_periphfunc_t *tr_func, void *arg)
23228b8a9b1dSJustin T. Gibbs {
23238b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph, *next_periph;
23248b8a9b1dSJustin T. Gibbs 	int retval;
23258b8a9b1dSJustin T. Gibbs 
23268b8a9b1dSJustin T. Gibbs 	retval = 1;
23278b8a9b1dSJustin T. Gibbs 
23288b8a9b1dSJustin T. Gibbs 	for (periph = (start_periph ? start_periph :
23298b8a9b1dSJustin T. Gibbs 		       SLIST_FIRST(&device->periphs));
23308b8a9b1dSJustin T. Gibbs 	     periph != NULL;
23318b8a9b1dSJustin T. Gibbs 	     periph = next_periph) {
23328b8a9b1dSJustin T. Gibbs 
23338b8a9b1dSJustin T. Gibbs 		next_periph = SLIST_NEXT(periph, periph_links);
23348b8a9b1dSJustin T. Gibbs 
23358b8a9b1dSJustin T. Gibbs 		retval = tr_func(periph, arg);
23368b8a9b1dSJustin T. Gibbs 		if (retval == 0)
23378b8a9b1dSJustin T. Gibbs 			return(retval);
23388b8a9b1dSJustin T. Gibbs 	}
23398b8a9b1dSJustin T. Gibbs 
23408b8a9b1dSJustin T. Gibbs 	return(retval);
23418b8a9b1dSJustin T. Gibbs }
23428b8a9b1dSJustin T. Gibbs 
23438b8a9b1dSJustin T. Gibbs static int
23448b8a9b1dSJustin T. Gibbs xptpdrvtraverse(struct periph_driver **start_pdrv,
23458b8a9b1dSJustin T. Gibbs 		xpt_pdrvfunc_t *tr_func, void *arg)
23468b8a9b1dSJustin T. Gibbs {
23478b8a9b1dSJustin T. Gibbs 	struct periph_driver **pdrv;
23488b8a9b1dSJustin T. Gibbs 	int retval;
23498b8a9b1dSJustin T. Gibbs 
23508b8a9b1dSJustin T. Gibbs 	retval = 1;
23518b8a9b1dSJustin T. Gibbs 
23528b8a9b1dSJustin T. Gibbs 	/*
23538b8a9b1dSJustin T. Gibbs 	 * We don't traverse the peripheral driver list like we do the
23548b8a9b1dSJustin T. Gibbs 	 * other lists, because it is a linker set, and therefore cannot be
23558b8a9b1dSJustin T. Gibbs 	 * changed during runtime.  If the peripheral driver list is ever
23568b8a9b1dSJustin T. Gibbs 	 * re-done to be something other than a linker set (i.e. it can
23578b8a9b1dSJustin T. Gibbs 	 * change while the system is running), the list traversal should
23588b8a9b1dSJustin T. Gibbs 	 * be modified to work like the other traversal functions.
23598b8a9b1dSJustin T. Gibbs 	 */
23608b8a9b1dSJustin T. Gibbs 	for (pdrv = (start_pdrv ? start_pdrv :
23618b8a9b1dSJustin T. Gibbs 	     (struct periph_driver **)periphdriver_set.ls_items);
23628b8a9b1dSJustin T. Gibbs 	     *pdrv != NULL; pdrv++) {
23638b8a9b1dSJustin T. Gibbs 		retval = tr_func(pdrv, arg);
23648b8a9b1dSJustin T. Gibbs 
23658b8a9b1dSJustin T. Gibbs 		if (retval == 0)
23668b8a9b1dSJustin T. Gibbs 			return(retval);
23678b8a9b1dSJustin T. Gibbs 	}
23688b8a9b1dSJustin T. Gibbs 
23698b8a9b1dSJustin T. Gibbs 	return(retval);
23708b8a9b1dSJustin T. Gibbs }
23718b8a9b1dSJustin T. Gibbs 
23728b8a9b1dSJustin T. Gibbs static int
23738b8a9b1dSJustin T. Gibbs xptpdperiphtraverse(struct periph_driver **pdrv,
23748b8a9b1dSJustin T. Gibbs 		    struct cam_periph *start_periph,
23758b8a9b1dSJustin T. Gibbs 		    xpt_periphfunc_t *tr_func, void *arg)
23768b8a9b1dSJustin T. Gibbs {
23778b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph, *next_periph;
23788b8a9b1dSJustin T. Gibbs 	int retval;
23798b8a9b1dSJustin T. Gibbs 
23808b8a9b1dSJustin T. Gibbs 	retval = 1;
23818b8a9b1dSJustin T. Gibbs 
23828b8a9b1dSJustin T. Gibbs 	for (periph = (start_periph ? start_periph :
23838b8a9b1dSJustin T. Gibbs 	     TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
23848b8a9b1dSJustin T. Gibbs 	     periph = next_periph) {
23858b8a9b1dSJustin T. Gibbs 
23868b8a9b1dSJustin T. Gibbs 		next_periph = TAILQ_NEXT(periph, unit_links);
23878b8a9b1dSJustin T. Gibbs 
23888b8a9b1dSJustin T. Gibbs 		retval = tr_func(periph, arg);
23898b8a9b1dSJustin T. Gibbs 		if (retval == 0)
23908b8a9b1dSJustin T. Gibbs 			return(retval);
23918b8a9b1dSJustin T. Gibbs 	}
23928b8a9b1dSJustin T. Gibbs 	return(retval);
23938b8a9b1dSJustin T. Gibbs }
23948b8a9b1dSJustin T. Gibbs 
23958b8a9b1dSJustin T. Gibbs static int
23968b8a9b1dSJustin T. Gibbs xptdefbusfunc(struct cam_eb *bus, void *arg)
23978b8a9b1dSJustin T. Gibbs {
23988b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
23998b8a9b1dSJustin T. Gibbs 
24008b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
24018b8a9b1dSJustin T. Gibbs 
24028b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_BUS) {
24038b8a9b1dSJustin T. Gibbs 		xpt_busfunc_t *tr_func;
24048b8a9b1dSJustin T. Gibbs 
24058b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_busfunc_t *)tr_config->tr_func;
24068b8a9b1dSJustin T. Gibbs 
24078b8a9b1dSJustin T. Gibbs 		return(tr_func(bus, tr_config->tr_arg));
24088b8a9b1dSJustin T. Gibbs 	} else
24098b8a9b1dSJustin T. Gibbs 		return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
24108b8a9b1dSJustin T. Gibbs }
24118b8a9b1dSJustin T. Gibbs 
24128b8a9b1dSJustin T. Gibbs static int
24138b8a9b1dSJustin T. Gibbs xptdeftargetfunc(struct cam_et *target, void *arg)
24148b8a9b1dSJustin T. Gibbs {
24158b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
24168b8a9b1dSJustin T. Gibbs 
24178b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
24188b8a9b1dSJustin T. Gibbs 
24198b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_TARGET) {
24208b8a9b1dSJustin T. Gibbs 		xpt_targetfunc_t *tr_func;
24218b8a9b1dSJustin T. Gibbs 
24228b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
24238b8a9b1dSJustin T. Gibbs 
24248b8a9b1dSJustin T. Gibbs 		return(tr_func(target, tr_config->tr_arg));
24258b8a9b1dSJustin T. Gibbs 	} else
24268b8a9b1dSJustin T. Gibbs 		return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
24278b8a9b1dSJustin T. Gibbs }
24288b8a9b1dSJustin T. Gibbs 
24298b8a9b1dSJustin T. Gibbs static int
24308b8a9b1dSJustin T. Gibbs xptdefdevicefunc(struct cam_ed *device, void *arg)
24318b8a9b1dSJustin T. Gibbs {
24328b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
24338b8a9b1dSJustin T. Gibbs 
24348b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
24358b8a9b1dSJustin T. Gibbs 
24368b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_DEVICE) {
24378b8a9b1dSJustin T. Gibbs 		xpt_devicefunc_t *tr_func;
24388b8a9b1dSJustin T. Gibbs 
24398b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
24408b8a9b1dSJustin T. Gibbs 
24418b8a9b1dSJustin T. Gibbs 		return(tr_func(device, tr_config->tr_arg));
24428b8a9b1dSJustin T. Gibbs 	} else
24438b8a9b1dSJustin T. Gibbs 		return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
24448b8a9b1dSJustin T. Gibbs }
24458b8a9b1dSJustin T. Gibbs 
24468b8a9b1dSJustin T. Gibbs static int
24478b8a9b1dSJustin T. Gibbs xptdefperiphfunc(struct cam_periph *periph, void *arg)
24488b8a9b1dSJustin T. Gibbs {
24498b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
24508b8a9b1dSJustin T. Gibbs 	xpt_periphfunc_t *tr_func;
24518b8a9b1dSJustin T. Gibbs 
24528b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
24538b8a9b1dSJustin T. Gibbs 
24548b8a9b1dSJustin T. Gibbs 	tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
24558b8a9b1dSJustin T. Gibbs 
24568b8a9b1dSJustin T. Gibbs 	/*
24578b8a9b1dSJustin T. Gibbs 	 * Unlike the other default functions, we don't check for depth
24588b8a9b1dSJustin T. Gibbs 	 * here.  The peripheral driver level is the last level in the EDT,
24598b8a9b1dSJustin T. Gibbs 	 * so if we're here, we should execute the function in question.
24608b8a9b1dSJustin T. Gibbs 	 */
24618b8a9b1dSJustin T. Gibbs 	return(tr_func(periph, tr_config->tr_arg));
24628b8a9b1dSJustin T. Gibbs }
24638b8a9b1dSJustin T. Gibbs 
24648b8a9b1dSJustin T. Gibbs /*
24658b8a9b1dSJustin T. Gibbs  * Execute the given function for every bus in the EDT.
24668b8a9b1dSJustin T. Gibbs  */
24678b8a9b1dSJustin T. Gibbs static int
24688b8a9b1dSJustin T. Gibbs xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
24698b8a9b1dSJustin T. Gibbs {
24708b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
24718b8a9b1dSJustin T. Gibbs 
24728b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_BUS;
24738b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
24748b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
24758b8a9b1dSJustin T. Gibbs 
24768b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
24778b8a9b1dSJustin T. Gibbs }
24788b8a9b1dSJustin T. Gibbs 
2479bfc0eb0fSKenneth D. Merry #ifdef notusedyet
24808b8a9b1dSJustin T. Gibbs /*
24818b8a9b1dSJustin T. Gibbs  * Execute the given function for every target in the EDT.
24828b8a9b1dSJustin T. Gibbs  */
24838b8a9b1dSJustin T. Gibbs static int
24848b8a9b1dSJustin T. Gibbs xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
24858b8a9b1dSJustin T. Gibbs {
24868b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
24878b8a9b1dSJustin T. Gibbs 
24888b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_TARGET;
24898b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
24908b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
24918b8a9b1dSJustin T. Gibbs 
24928b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
24938b8a9b1dSJustin T. Gibbs }
2494bfc0eb0fSKenneth D. Merry #endif /* notusedyet */
24958b8a9b1dSJustin T. Gibbs 
24968b8a9b1dSJustin T. Gibbs /*
24978b8a9b1dSJustin T. Gibbs  * Execute the given function for every device in the EDT.
24988b8a9b1dSJustin T. Gibbs  */
24998b8a9b1dSJustin T. Gibbs static int
25008b8a9b1dSJustin T. Gibbs xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
25018b8a9b1dSJustin T. Gibbs {
25028b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
25038b8a9b1dSJustin T. Gibbs 
25048b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_DEVICE;
25058b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
25068b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
25078b8a9b1dSJustin T. Gibbs 
25088b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
25098b8a9b1dSJustin T. Gibbs }
25108b8a9b1dSJustin T. Gibbs 
2511bfc0eb0fSKenneth D. Merry #ifdef notusedyet
25128b8a9b1dSJustin T. Gibbs /*
25138b8a9b1dSJustin T. Gibbs  * Execute the given function for every peripheral in the EDT.
25148b8a9b1dSJustin T. Gibbs  */
25158b8a9b1dSJustin T. Gibbs static int
25168b8a9b1dSJustin T. Gibbs xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
25178b8a9b1dSJustin T. Gibbs {
25188b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
25198b8a9b1dSJustin T. Gibbs 
25208b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_PERIPH;
25218b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
25228b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
25238b8a9b1dSJustin T. Gibbs 
25248b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
25258b8a9b1dSJustin T. Gibbs }
2526bfc0eb0fSKenneth D. Merry #endif /* notusedyet */
25278b8a9b1dSJustin T. Gibbs 
25288b8a9b1dSJustin T. Gibbs static int
25298b8a9b1dSJustin T. Gibbs xptsetasyncfunc(struct cam_ed *device, void *arg)
25308b8a9b1dSJustin T. Gibbs {
25318b8a9b1dSJustin T. Gibbs 	struct cam_path path;
25328b8a9b1dSJustin T. Gibbs 	struct ccb_getdev cgd;
25338b8a9b1dSJustin T. Gibbs 	struct async_node *cur_entry;
25348b8a9b1dSJustin T. Gibbs 
25358b8a9b1dSJustin T. Gibbs 	cur_entry = (struct async_node *)arg;
25368b8a9b1dSJustin T. Gibbs 
2537c8bead2aSJustin T. Gibbs 	/*
2538c8bead2aSJustin T. Gibbs 	 * Don't report unconfigured devices (Wildcard devs,
2539c8bead2aSJustin T. Gibbs 	 * devices only for target mode, device instances
2540c8bead2aSJustin T. Gibbs 	 * that have been invalidated but are waiting for
2541c8bead2aSJustin T. Gibbs 	 * their last reference count to be released).
2542c8bead2aSJustin T. Gibbs 	 */
2543c8bead2aSJustin T. Gibbs 	if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2544c8bead2aSJustin T. Gibbs 		return (1);
2545c8bead2aSJustin T. Gibbs 
25468b8a9b1dSJustin T. Gibbs 	xpt_compile_path(&path,
25478b8a9b1dSJustin T. Gibbs 			 NULL,
25488b8a9b1dSJustin T. Gibbs 			 device->target->bus->path_id,
25498b8a9b1dSJustin T. Gibbs 			 device->target->target_id,
25508b8a9b1dSJustin T. Gibbs 			 device->lun_id);
25518b8a9b1dSJustin T. Gibbs 	xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
25528b8a9b1dSJustin T. Gibbs 	cgd.ccb_h.func_code = XPT_GDEV_TYPE;
25538b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&cgd);
25548b8a9b1dSJustin T. Gibbs 	cur_entry->callback(cur_entry->callback_arg,
25558b8a9b1dSJustin T. Gibbs 			    AC_FOUND_DEVICE,
25568b8a9b1dSJustin T. Gibbs 			    &path, &cgd);
25578b8a9b1dSJustin T. Gibbs 	xpt_release_path(&path);
25588b8a9b1dSJustin T. Gibbs 
25598b8a9b1dSJustin T. Gibbs 	return(1);
25608b8a9b1dSJustin T. Gibbs }
2561c8bead2aSJustin T. Gibbs 
25628b8a9b1dSJustin T. Gibbs static int
25638b8a9b1dSJustin T. Gibbs xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
25648b8a9b1dSJustin T. Gibbs {
25658b8a9b1dSJustin T. Gibbs 	struct cam_path path;
25668b8a9b1dSJustin T. Gibbs 	struct ccb_pathinq cpi;
25678b8a9b1dSJustin T. Gibbs 	struct async_node *cur_entry;
25688b8a9b1dSJustin T. Gibbs 
25698b8a9b1dSJustin T. Gibbs 	cur_entry = (struct async_node *)arg;
25708b8a9b1dSJustin T. Gibbs 
25718b8a9b1dSJustin T. Gibbs 	xpt_compile_path(&path, /*periph*/NULL,
25728b8a9b1dSJustin T. Gibbs 			 bus->sim->path_id,
25738b8a9b1dSJustin T. Gibbs 			 CAM_TARGET_WILDCARD,
25748b8a9b1dSJustin T. Gibbs 			 CAM_LUN_WILDCARD);
25758b8a9b1dSJustin T. Gibbs 	xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
25768b8a9b1dSJustin T. Gibbs 	cpi.ccb_h.func_code = XPT_PATH_INQ;
25778b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&cpi);
25788b8a9b1dSJustin T. Gibbs 	cur_entry->callback(cur_entry->callback_arg,
25798b8a9b1dSJustin T. Gibbs 			    AC_PATH_REGISTERED,
25808b8a9b1dSJustin T. Gibbs 			    &path, &cpi);
25818b8a9b1dSJustin T. Gibbs 	xpt_release_path(&path);
25828b8a9b1dSJustin T. Gibbs 
25838b8a9b1dSJustin T. Gibbs 	return(1);
25848b8a9b1dSJustin T. Gibbs }
25858b8a9b1dSJustin T. Gibbs 
25868b8a9b1dSJustin T. Gibbs void
25878b8a9b1dSJustin T. Gibbs xpt_action(union ccb *start_ccb)
25888b8a9b1dSJustin T. Gibbs {
25898b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
25908b8a9b1dSJustin T. Gibbs 
25918b8a9b1dSJustin T. Gibbs 	start_ccb->ccb_h.status = CAM_REQ_INPROG;
25928b8a9b1dSJustin T. Gibbs 
25938b8a9b1dSJustin T. Gibbs 	switch (start_ccb->ccb_h.func_code) {
25948b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
2595d05caa00SKenneth D. Merry 	{
2596d05caa00SKenneth D. Merry #ifdef CAMDEBUG
2597d05caa00SKenneth D. Merry 		char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2598d05caa00SKenneth D. Merry 		struct cam_path *path;
2599d05caa00SKenneth D. Merry 
2600d05caa00SKenneth D. Merry 		path = start_ccb->ccb_h.path;
2601d05caa00SKenneth D. Merry #endif
2602d05caa00SKenneth D. Merry 
26038b8a9b1dSJustin T. Gibbs 		/*
26048b8a9b1dSJustin T. Gibbs 		 * For the sake of compatibility with SCSI-1
26058b8a9b1dSJustin T. Gibbs 		 * devices that may not understand the identify
26068b8a9b1dSJustin T. Gibbs 		 * message, we include lun information in the
26078b8a9b1dSJustin T. Gibbs 		 * second byte of all commands.  SCSI-1 specifies
26088b8a9b1dSJustin T. Gibbs 		 * that luns are a 3 bit value and reserves only 3
26098b8a9b1dSJustin T. Gibbs 		 * bits for lun information in the CDB.  Later
26108b8a9b1dSJustin T. Gibbs 		 * revisions of the SCSI spec allow for more than 8
26118b8a9b1dSJustin T. Gibbs 		 * luns, but have deprecated lun information in the
26128b8a9b1dSJustin T. Gibbs 		 * CDB.  So, if the lun won't fit, we must omit.
26138b8a9b1dSJustin T. Gibbs 		 *
26148b8a9b1dSJustin T. Gibbs 		 * Also be aware that during initial probing for devices,
26158b8a9b1dSJustin T. Gibbs 		 * the inquiry information is unknown but initialized to 0.
26168b8a9b1dSJustin T. Gibbs 		 * This means that this code will be exercised while probing
26178b8a9b1dSJustin T. Gibbs 		 * devices with an ANSI revision greater than 2.
26188b8a9b1dSJustin T. Gibbs 		 */
26198b8a9b1dSJustin T. Gibbs 		if (SID_ANSI_REV(&start_ccb->ccb_h.path->device->inq_data) <= 2
26208b8a9b1dSJustin T. Gibbs 		 && start_ccb->ccb_h.target_lun < 8
26218b8a9b1dSJustin T. Gibbs 		 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
26228b8a9b1dSJustin T. Gibbs 
26238b8a9b1dSJustin T. Gibbs 			start_ccb->csio.cdb_io.cdb_bytes[1] |=
26248b8a9b1dSJustin T. Gibbs 			    start_ccb->ccb_h.target_lun << 5;
26258b8a9b1dSJustin T. Gibbs 		}
26268b8a9b1dSJustin T. Gibbs 		start_ccb->csio.scsi_status = SCSI_STATUS_OK;
26278b8a9b1dSJustin T. Gibbs 		start_ccb->csio.sense_resid = 0;
26288b8a9b1dSJustin T. Gibbs 		start_ccb->csio.resid = 0;
2629d05caa00SKenneth D. Merry 		CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2630d05caa00SKenneth D. Merry 			  scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2631d05caa00SKenneth D. Merry 			  	       &path->device->inq_data),
2632d05caa00SKenneth D. Merry 			  scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
263350642f18SKenneth D. Merry 					  cdb_str, sizeof(cdb_str))));
26348b8a9b1dSJustin T. Gibbs 		/* FALLTRHOUGH */
2635d05caa00SKenneth D. Merry 	}
26368b8a9b1dSJustin T. Gibbs 	case XPT_TARGET_IO:
26378b8a9b1dSJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
26388b8a9b1dSJustin T. Gibbs 	case XPT_ENG_EXEC:
26398b8a9b1dSJustin T. Gibbs 	{
26408b8a9b1dSJustin T. Gibbs 		struct cam_path *path;
26418b8a9b1dSJustin T. Gibbs 		int s;
26428b8a9b1dSJustin T. Gibbs 		int runq;
26438b8a9b1dSJustin T. Gibbs 
26448b8a9b1dSJustin T. Gibbs 		path = start_ccb->ccb_h.path;
26458b8a9b1dSJustin T. Gibbs 		s = splsoftcam();
26468b8a9b1dSJustin T. Gibbs 
26478b8a9b1dSJustin T. Gibbs 		cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
26488b8a9b1dSJustin T. Gibbs 		if (path->device->qfrozen_cnt == 0)
26498b8a9b1dSJustin T. Gibbs 			runq = xpt_schedule_dev_sendq(path->bus, path->device);
26508b8a9b1dSJustin T. Gibbs 		else
26518b8a9b1dSJustin T. Gibbs 			runq = 0;
26528b8a9b1dSJustin T. Gibbs 		splx(s);
26538b8a9b1dSJustin T. Gibbs 		if (runq != 0)
26548b8a9b1dSJustin T. Gibbs 			xpt_run_dev_sendq(path->bus);
26558b8a9b1dSJustin T. Gibbs 		break;
26568b8a9b1dSJustin T. Gibbs 	}
26578b8a9b1dSJustin T. Gibbs 	case XPT_SET_TRAN_SETTINGS:
26588b8a9b1dSJustin T. Gibbs 	{
26598b8a9b1dSJustin T. Gibbs 		xpt_set_transfer_settings(&start_ccb->cts,
266003e3511bSJustin T. Gibbs 					  start_ccb->ccb_h.path->device,
26618b8a9b1dSJustin T. Gibbs 					  /*async_update*/FALSE);
26628b8a9b1dSJustin T. Gibbs 		break;
26638b8a9b1dSJustin T. Gibbs 	}
26648b8a9b1dSJustin T. Gibbs 	case XPT_CALC_GEOMETRY:
26658b8a9b1dSJustin T. Gibbs 		/* Filter out garbage */
26668b8a9b1dSJustin T. Gibbs 		if (start_ccb->ccg.block_size == 0
26678b8a9b1dSJustin T. Gibbs 		 || start_ccb->ccg.volume_size == 0) {
26688b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.cylinders = 0;
26698b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.heads = 0;
26708b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.secs_per_track = 0;
26718b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
26728b8a9b1dSJustin T. Gibbs 			break;
26738b8a9b1dSJustin T. Gibbs 		}
26748b8a9b1dSJustin T. Gibbs #ifdef PC98
26758b8a9b1dSJustin T. Gibbs 		/*
26768b8a9b1dSJustin T. Gibbs 		 * In a PC-98 system, geometry translation depens on
26778b8a9b1dSJustin T. Gibbs 		 * the "real" device geometry obtained from mode page 4.
26788b8a9b1dSJustin T. Gibbs 		 * SCSI geometry translation is performed in the
26798b8a9b1dSJustin T. Gibbs 		 * initialization routine of the SCSI BIOS and the result
26808b8a9b1dSJustin T. Gibbs 		 * stored in host memory.  If the translation is available
26818b8a9b1dSJustin T. Gibbs 		 * in host memory, use it.  If not, rely on the default
26828b8a9b1dSJustin T. Gibbs 		 * translation the device driver performs.
26838b8a9b1dSJustin T. Gibbs 		 */
26848b8a9b1dSJustin T. Gibbs 		if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
26858b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
26868b8a9b1dSJustin T. Gibbs 			break;
26878b8a9b1dSJustin T. Gibbs 		}
26888b8a9b1dSJustin T. Gibbs 		/* FALLTHROUGH */
26898b8a9b1dSJustin T. Gibbs #endif
2690bb6087e5SJustin T. Gibbs 	case XPT_ABORT:
2691bb6087e5SJustin T. Gibbs 	case XPT_RESET_DEV:
26928b8a9b1dSJustin T. Gibbs 	case XPT_ACCEPT_TARGET_IO:
26938b8a9b1dSJustin T. Gibbs 	case XPT_EN_LUN:
26948b8a9b1dSJustin T. Gibbs 	case XPT_IMMED_NOTIFY:
26958b8a9b1dSJustin T. Gibbs 	case XPT_NOTIFY_ACK:
26968b8a9b1dSJustin T. Gibbs 	case XPT_GET_TRAN_SETTINGS:
26978b8a9b1dSJustin T. Gibbs 	case XPT_PATH_INQ:
26988b8a9b1dSJustin T. Gibbs 	case XPT_RESET_BUS:
26998b8a9b1dSJustin T. Gibbs 	{
27008b8a9b1dSJustin T. Gibbs 		struct cam_sim *sim;
27018b8a9b1dSJustin T. Gibbs 
27028b8a9b1dSJustin T. Gibbs 		sim = start_ccb->ccb_h.path->bus->sim;
27038b8a9b1dSJustin T. Gibbs 		(*(sim->sim_action))(sim, start_ccb);
27048b8a9b1dSJustin T. Gibbs 		break;
27058b8a9b1dSJustin T. Gibbs 	}
27068b8a9b1dSJustin T. Gibbs 	case XPT_GDEV_TYPE:
27078b8a9b1dSJustin T. Gibbs 		if ((start_ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) != 0) {
27088b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
27098b8a9b1dSJustin T. Gibbs 		} else {
27108b8a9b1dSJustin T. Gibbs 			struct ccb_getdev *cgd;
27118b8a9b1dSJustin T. Gibbs 			struct cam_et *tar;
27128b8a9b1dSJustin T. Gibbs 			struct cam_ed *dev;
27138b8a9b1dSJustin T. Gibbs 			int s;
27148b8a9b1dSJustin T. Gibbs 
27158b8a9b1dSJustin T. Gibbs 			s = splsoftcam();
27168b8a9b1dSJustin T. Gibbs 			cgd = &start_ccb->cgd;
27178b8a9b1dSJustin T. Gibbs 			tar = cgd->ccb_h.path->target;
27188b8a9b1dSJustin T. Gibbs 			dev = cgd->ccb_h.path->device;
27198b8a9b1dSJustin T. Gibbs 			cgd->inq_data = dev->inq_data;
27208b8a9b1dSJustin T. Gibbs 			cgd->pd_type = SID_TYPE(&dev->inq_data);
27218b8a9b1dSJustin T. Gibbs 			cgd->dev_openings = dev->ccbq.dev_openings;
27228b8a9b1dSJustin T. Gibbs 			cgd->dev_active = dev->ccbq.dev_active;
27238b8a9b1dSJustin T. Gibbs 			cgd->devq_openings = dev->ccbq.devq_openings;
27248b8a9b1dSJustin T. Gibbs 			cgd->devq_queued = dev->ccbq.queue.entries;
27258b8a9b1dSJustin T. Gibbs 			cgd->held = dev->ccbq.held;
27268b8a9b1dSJustin T. Gibbs 			cgd->maxtags = dev->quirk->maxtags;
27278b8a9b1dSJustin T. Gibbs 			cgd->mintags = dev->quirk->mintags;
27288b8a9b1dSJustin T. Gibbs 			cgd->ccb_h.status = CAM_REQ_CMP;
27298b8a9b1dSJustin T. Gibbs 			cgd->serial_num_len = dev->serial_num_len;
27308b8a9b1dSJustin T. Gibbs 			if ((dev->serial_num_len > 0)
27318b8a9b1dSJustin T. Gibbs 			 && (dev->serial_num != NULL))
27328b8a9b1dSJustin T. Gibbs 				bcopy(dev->serial_num, cgd->serial_num,
27338b8a9b1dSJustin T. Gibbs 				      dev->serial_num_len);
27348b8a9b1dSJustin T. Gibbs 			splx(s);
27358b8a9b1dSJustin T. Gibbs 		}
27368b8a9b1dSJustin T. Gibbs 		break;
27378b8a9b1dSJustin T. Gibbs 	case XPT_GDEVLIST:
27388b8a9b1dSJustin T. Gibbs 	{
27398b8a9b1dSJustin T. Gibbs 		struct cam_periph	*nperiph;
27408b8a9b1dSJustin T. Gibbs 		struct periph_list	*periph_head;
27418b8a9b1dSJustin T. Gibbs 		struct ccb_getdevlist	*cgdl;
27428b8a9b1dSJustin T. Gibbs 		int			i;
27438b8a9b1dSJustin T. Gibbs 		int			s;
27448b8a9b1dSJustin T. Gibbs 		struct cam_ed		*device;
27458b8a9b1dSJustin T. Gibbs 		int			found;
27468b8a9b1dSJustin T. Gibbs 
27478b8a9b1dSJustin T. Gibbs 
27488b8a9b1dSJustin T. Gibbs 		found = 0;
27498b8a9b1dSJustin T. Gibbs 
27508b8a9b1dSJustin T. Gibbs 		/*
27518b8a9b1dSJustin T. Gibbs 		 * Don't want anyone mucking with our data.
27528b8a9b1dSJustin T. Gibbs 		 */
27538b8a9b1dSJustin T. Gibbs 		s = splsoftcam();
27548b8a9b1dSJustin T. Gibbs 		device = start_ccb->ccb_h.path->device;
27558b8a9b1dSJustin T. Gibbs 		periph_head = &device->periphs;
27568b8a9b1dSJustin T. Gibbs 		cgdl = &start_ccb->cgdl;
27578b8a9b1dSJustin T. Gibbs 
27588b8a9b1dSJustin T. Gibbs 		/*
27598b8a9b1dSJustin T. Gibbs 		 * Check and see if the list has changed since the user
27608b8a9b1dSJustin T. Gibbs 		 * last requested a list member.  If so, tell them that the
27618b8a9b1dSJustin T. Gibbs 		 * list has changed, and therefore they need to start over
27628b8a9b1dSJustin T. Gibbs 		 * from the beginning.
27638b8a9b1dSJustin T. Gibbs 		 */
27648b8a9b1dSJustin T. Gibbs 		if ((cgdl->index != 0) &&
27658b8a9b1dSJustin T. Gibbs 		    (cgdl->generation != device->generation)) {
27668b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
27678b8a9b1dSJustin T. Gibbs 			splx(s);
27688b8a9b1dSJustin T. Gibbs 			break;
27698b8a9b1dSJustin T. Gibbs 		}
27708b8a9b1dSJustin T. Gibbs 
27718b8a9b1dSJustin T. Gibbs 		/*
27728b8a9b1dSJustin T. Gibbs 		 * Traverse the list of peripherals and attempt to find
27738b8a9b1dSJustin T. Gibbs 		 * the requested peripheral.
27748b8a9b1dSJustin T. Gibbs 		 */
27758b8a9b1dSJustin T. Gibbs 		for (nperiph = periph_head->slh_first, i = 0;
27768b8a9b1dSJustin T. Gibbs 		     (nperiph != NULL) && (i <= cgdl->index);
27778b8a9b1dSJustin T. Gibbs 		     nperiph = nperiph->periph_links.sle_next, i++) {
27788b8a9b1dSJustin T. Gibbs 			if (i == cgdl->index) {
27798b8a9b1dSJustin T. Gibbs 				strncpy(cgdl->periph_name,
27808b8a9b1dSJustin T. Gibbs 					nperiph->periph_name,
27818b8a9b1dSJustin T. Gibbs 					DEV_IDLEN);
27828b8a9b1dSJustin T. Gibbs 				cgdl->unit_number = nperiph->unit_number;
27838b8a9b1dSJustin T. Gibbs 				found = 1;
27848b8a9b1dSJustin T. Gibbs 			}
27858b8a9b1dSJustin T. Gibbs 		}
27868b8a9b1dSJustin T. Gibbs 		if (found == 0) {
27878b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_ERROR;
27888b8a9b1dSJustin T. Gibbs 			splx(s);
27898b8a9b1dSJustin T. Gibbs 			break;
27908b8a9b1dSJustin T. Gibbs 		}
27918b8a9b1dSJustin T. Gibbs 
27928b8a9b1dSJustin T. Gibbs 		if (nperiph == NULL)
27938b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
27948b8a9b1dSJustin T. Gibbs 		else
27958b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_MORE_DEVS;
27968b8a9b1dSJustin T. Gibbs 
27978b8a9b1dSJustin T. Gibbs 		cgdl->index++;
27988b8a9b1dSJustin T. Gibbs 		cgdl->generation = device->generation;
27998b8a9b1dSJustin T. Gibbs 
28008b8a9b1dSJustin T. Gibbs 		splx(s);
28018b8a9b1dSJustin T. Gibbs 		cgdl->ccb_h.status = CAM_REQ_CMP;
28028b8a9b1dSJustin T. Gibbs 		break;
28038b8a9b1dSJustin T. Gibbs 	}
28048b8a9b1dSJustin T. Gibbs 	case XPT_DEV_MATCH:
28058b8a9b1dSJustin T. Gibbs 	{
28068b8a9b1dSJustin T. Gibbs 		int s;
28078b8a9b1dSJustin T. Gibbs 		dev_pos_type position_type;
28088b8a9b1dSJustin T. Gibbs 		struct ccb_dev_match *cdm;
28098b8a9b1dSJustin T. Gibbs 		int ret;
28108b8a9b1dSJustin T. Gibbs 
28118b8a9b1dSJustin T. Gibbs 		cdm = &start_ccb->cdm;
28128b8a9b1dSJustin T. Gibbs 
28138b8a9b1dSJustin T. Gibbs 		/*
28148b8a9b1dSJustin T. Gibbs 		 * Prevent EDT changes while we traverse it.
28158b8a9b1dSJustin T. Gibbs 		 */
28168b8a9b1dSJustin T. Gibbs 		s = splsoftcam();
28178b8a9b1dSJustin T. Gibbs 		/*
28188b8a9b1dSJustin T. Gibbs 		 * There are two ways of getting at information in the EDT.
28198b8a9b1dSJustin T. Gibbs 		 * The first way is via the primary EDT tree.  It starts
28208b8a9b1dSJustin T. Gibbs 		 * with a list of busses, then a list of targets on a bus,
28218b8a9b1dSJustin T. Gibbs 		 * then devices/luns on a target, and then peripherals on a
28228b8a9b1dSJustin T. Gibbs 		 * device/lun.  The "other" way is by the peripheral driver
28238b8a9b1dSJustin T. Gibbs 		 * lists.  The peripheral driver lists are organized by
28248b8a9b1dSJustin T. Gibbs 		 * peripheral driver.  (obviously)  So it makes sense to
28258b8a9b1dSJustin T. Gibbs 		 * use the peripheral driver list if the user is looking
28268b8a9b1dSJustin T. Gibbs 		 * for something like "da1", or all "da" devices.  If the
28278b8a9b1dSJustin T. Gibbs 		 * user is looking for something on a particular bus/target
28288b8a9b1dSJustin T. Gibbs 		 * or lun, it's generally better to go through the EDT tree.
28298b8a9b1dSJustin T. Gibbs 		 */
28308b8a9b1dSJustin T. Gibbs 
28318b8a9b1dSJustin T. Gibbs 		if (cdm->pos.position_type != CAM_DEV_POS_NONE)
28328b8a9b1dSJustin T. Gibbs 			position_type = cdm->pos.position_type;
28338b8a9b1dSJustin T. Gibbs 		else {
28348b8a9b1dSJustin T. Gibbs 			int i;
28358b8a9b1dSJustin T. Gibbs 
28368b8a9b1dSJustin T. Gibbs 			position_type = CAM_DEV_POS_NONE;
28378b8a9b1dSJustin T. Gibbs 
28388b8a9b1dSJustin T. Gibbs 			for (i = 0; i < cdm->num_patterns; i++) {
28398b8a9b1dSJustin T. Gibbs 				if ((cdm->patterns[i].type == DEV_MATCH_BUS)
28408b8a9b1dSJustin T. Gibbs 				 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
28418b8a9b1dSJustin T. Gibbs 					position_type = CAM_DEV_POS_EDT;
28428b8a9b1dSJustin T. Gibbs 					break;
28438b8a9b1dSJustin T. Gibbs 				}
28448b8a9b1dSJustin T. Gibbs 			}
28458b8a9b1dSJustin T. Gibbs 
28468b8a9b1dSJustin T. Gibbs 			if (cdm->num_patterns == 0)
28478b8a9b1dSJustin T. Gibbs 				position_type = CAM_DEV_POS_EDT;
28488b8a9b1dSJustin T. Gibbs 			else if (position_type == CAM_DEV_POS_NONE)
28498b8a9b1dSJustin T. Gibbs 				position_type = CAM_DEV_POS_PDRV;
28508b8a9b1dSJustin T. Gibbs 		}
28518b8a9b1dSJustin T. Gibbs 
28528b8a9b1dSJustin T. Gibbs 		switch(position_type & CAM_DEV_POS_TYPEMASK) {
28538b8a9b1dSJustin T. Gibbs 		case CAM_DEV_POS_EDT:
28548b8a9b1dSJustin T. Gibbs 			ret = xptedtmatch(cdm);
28558b8a9b1dSJustin T. Gibbs 			break;
28568b8a9b1dSJustin T. Gibbs 		case CAM_DEV_POS_PDRV:
28578b8a9b1dSJustin T. Gibbs 			ret = xptperiphlistmatch(cdm);
28588b8a9b1dSJustin T. Gibbs 			break;
28598b8a9b1dSJustin T. Gibbs 		default:
28608b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_ERROR;
28618b8a9b1dSJustin T. Gibbs 			break;
28628b8a9b1dSJustin T. Gibbs 		}
28638b8a9b1dSJustin T. Gibbs 
28648b8a9b1dSJustin T. Gibbs 		splx(s);
28658b8a9b1dSJustin T. Gibbs 
28668b8a9b1dSJustin T. Gibbs 		if (cdm->status == CAM_DEV_MATCH_ERROR)
28678b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
28688b8a9b1dSJustin T. Gibbs 		else
28698b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
28708b8a9b1dSJustin T. Gibbs 
28718b8a9b1dSJustin T. Gibbs 		break;
28728b8a9b1dSJustin T. Gibbs 	}
28738b8a9b1dSJustin T. Gibbs 	case XPT_SASYNC_CB:
28748b8a9b1dSJustin T. Gibbs 	{
28758b8a9b1dSJustin T. Gibbs 		struct ccb_setasync *csa;
28768b8a9b1dSJustin T. Gibbs 		struct async_node *cur_entry;
28778b8a9b1dSJustin T. Gibbs 		struct async_list *async_head;
28788b8a9b1dSJustin T. Gibbs 		u_int32_t added;
28798b8a9b1dSJustin T. Gibbs 		int s;
28808b8a9b1dSJustin T. Gibbs 
28818b8a9b1dSJustin T. Gibbs 		csa = &start_ccb->csa;
28828b8a9b1dSJustin T. Gibbs 		added = csa->event_enable;
28838b8a9b1dSJustin T. Gibbs 		async_head = &csa->ccb_h.path->device->asyncs;
28848b8a9b1dSJustin T. Gibbs 
28858b8a9b1dSJustin T. Gibbs 		/*
28868b8a9b1dSJustin T. Gibbs 		 * If there is already an entry for us, simply
28878b8a9b1dSJustin T. Gibbs 		 * update it.
28888b8a9b1dSJustin T. Gibbs 		 */
28898b8a9b1dSJustin T. Gibbs 		s = splsoftcam();
28908b8a9b1dSJustin T. Gibbs 		cur_entry = SLIST_FIRST(async_head);
28918b8a9b1dSJustin T. Gibbs 		while (cur_entry != NULL) {
28928b8a9b1dSJustin T. Gibbs 			if ((cur_entry->callback_arg == csa->callback_arg)
28938b8a9b1dSJustin T. Gibbs 			 && (cur_entry->callback == csa->callback))
28948b8a9b1dSJustin T. Gibbs 				break;
28958b8a9b1dSJustin T. Gibbs 			cur_entry = SLIST_NEXT(cur_entry, links);
28968b8a9b1dSJustin T. Gibbs 		}
28978b8a9b1dSJustin T. Gibbs 
28988b8a9b1dSJustin T. Gibbs 		if (cur_entry != NULL) {
28998b8a9b1dSJustin T. Gibbs 		 	/*
29008b8a9b1dSJustin T. Gibbs 			 * If the request has no flags set,
29018b8a9b1dSJustin T. Gibbs 			 * remove the entry.
29028b8a9b1dSJustin T. Gibbs 			 */
29038b8a9b1dSJustin T. Gibbs 			added &= ~cur_entry->event_enable;
29048b8a9b1dSJustin T. Gibbs 			if (csa->event_enable == 0) {
29058b8a9b1dSJustin T. Gibbs 				SLIST_REMOVE(async_head, cur_entry,
29068b8a9b1dSJustin T. Gibbs 					     async_node, links);
2907c8bead2aSJustin T. Gibbs 				csa->ccb_h.path->device->refcount--;
29088b8a9b1dSJustin T. Gibbs 				free(cur_entry, M_DEVBUF);
29098b8a9b1dSJustin T. Gibbs 			} else {
29108b8a9b1dSJustin T. Gibbs 				cur_entry->event_enable = csa->event_enable;
29118b8a9b1dSJustin T. Gibbs 			}
29128b8a9b1dSJustin T. Gibbs 		} else {
29138b8a9b1dSJustin T. Gibbs 			cur_entry = malloc(sizeof(*cur_entry), M_DEVBUF,
29148b8a9b1dSJustin T. Gibbs 					   M_NOWAIT);
29158b8a9b1dSJustin T. Gibbs 			if (cur_entry == NULL) {
29168b8a9b1dSJustin T. Gibbs 				splx(s);
29178b8a9b1dSJustin T. Gibbs 				csa->ccb_h.status = CAM_RESRC_UNAVAIL;
29188b8a9b1dSJustin T. Gibbs 				break;
29198b8a9b1dSJustin T. Gibbs 			}
29208b8a9b1dSJustin T. Gibbs 			cur_entry->callback_arg = csa->callback_arg;
29218b8a9b1dSJustin T. Gibbs 			cur_entry->callback = csa->callback;
29228b8a9b1dSJustin T. Gibbs 			cur_entry->event_enable = csa->event_enable;
29238b8a9b1dSJustin T. Gibbs 			SLIST_INSERT_HEAD(async_head, cur_entry, links);
2924c8bead2aSJustin T. Gibbs 			csa->ccb_h.path->device->refcount++;
29258b8a9b1dSJustin T. Gibbs 		}
29268b8a9b1dSJustin T. Gibbs 
29278b8a9b1dSJustin T. Gibbs 		if ((added & AC_FOUND_DEVICE) != 0) {
29288b8a9b1dSJustin T. Gibbs 			/*
29298b8a9b1dSJustin T. Gibbs 			 * Get this peripheral up to date with all
29308b8a9b1dSJustin T. Gibbs 			 * the currently existing devices.
29318b8a9b1dSJustin T. Gibbs 			 */
29328b8a9b1dSJustin T. Gibbs 			xpt_for_all_devices(xptsetasyncfunc, cur_entry);
29338b8a9b1dSJustin T. Gibbs 		}
29348b8a9b1dSJustin T. Gibbs 		if ((added & AC_PATH_REGISTERED) != 0) {
29358b8a9b1dSJustin T. Gibbs 			/*
29368b8a9b1dSJustin T. Gibbs 			 * Get this peripheral up to date with all
29378b8a9b1dSJustin T. Gibbs 			 * the currently existing busses.
29388b8a9b1dSJustin T. Gibbs 			 */
29398b8a9b1dSJustin T. Gibbs 			xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
29408b8a9b1dSJustin T. Gibbs 		}
29418b8a9b1dSJustin T. Gibbs 		splx(s);
29428b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
29438b8a9b1dSJustin T. Gibbs 		break;
29448b8a9b1dSJustin T. Gibbs 	}
29458b8a9b1dSJustin T. Gibbs 	case XPT_REL_SIMQ:
29468b8a9b1dSJustin T. Gibbs 	{
29478b8a9b1dSJustin T. Gibbs 		struct ccb_relsim *crs;
29488b8a9b1dSJustin T. Gibbs 		struct cam_ed *dev;
29498b8a9b1dSJustin T. Gibbs 		int s;
29508b8a9b1dSJustin T. Gibbs 
29518b8a9b1dSJustin T. Gibbs 		crs = &start_ccb->crs;
29528b8a9b1dSJustin T. Gibbs 		dev = crs->ccb_h.path->device;
29538b8a9b1dSJustin T. Gibbs 		if (dev == NULL) {
29548b8a9b1dSJustin T. Gibbs 
29558b8a9b1dSJustin T. Gibbs 			crs->ccb_h.status = CAM_DEV_NOT_THERE;
29568b8a9b1dSJustin T. Gibbs 			break;
29578b8a9b1dSJustin T. Gibbs 		}
29588b8a9b1dSJustin T. Gibbs 
29598b8a9b1dSJustin T. Gibbs 		s = splcam();
29608b8a9b1dSJustin T. Gibbs 
29618b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
29628b8a9b1dSJustin T. Gibbs 
29638b8a9b1dSJustin T. Gibbs  			if ((dev->inq_data.flags & SID_CmdQue) != 0) {
29648b8a9b1dSJustin T. Gibbs 
29658b8a9b1dSJustin T. Gibbs 				/* Don't ever go below one opening */
29668b8a9b1dSJustin T. Gibbs 				if (crs->openings > 0) {
29678b8a9b1dSJustin T. Gibbs 					xpt_dev_ccbq_resize(crs->ccb_h.path,
29688b8a9b1dSJustin T. Gibbs 							    crs->openings);
29698b8a9b1dSJustin T. Gibbs 
29708b8a9b1dSJustin T. Gibbs 					if (bootverbose || 1) {
29718b8a9b1dSJustin T. Gibbs 						xpt_print_path(crs->ccb_h.path);
29728b8a9b1dSJustin T. Gibbs 						printf("tagged openings "
29738b8a9b1dSJustin T. Gibbs 						       "now %d\n",
29748b8a9b1dSJustin T. Gibbs 						       crs->openings);
29758b8a9b1dSJustin T. Gibbs 					}
29768b8a9b1dSJustin T. Gibbs 				}
29778b8a9b1dSJustin T. Gibbs 			}
29788b8a9b1dSJustin T. Gibbs 		}
29798b8a9b1dSJustin T. Gibbs 
29808b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
29818b8a9b1dSJustin T. Gibbs 
29828b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
29838b8a9b1dSJustin T. Gibbs 
29848b8a9b1dSJustin T. Gibbs 				/*
29858b8a9b1dSJustin T. Gibbs 				 * Just extend the old timeout and decrement
29868b8a9b1dSJustin T. Gibbs 				 * the freeze count so that a single timeout
29878b8a9b1dSJustin T. Gibbs 				 * is sufficient for releasing the queue.
29888b8a9b1dSJustin T. Gibbs 				 */
29898b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
29908b8a9b1dSJustin T. Gibbs 				untimeout(xpt_release_devq_timeout,
29918b8a9b1dSJustin T. Gibbs 					  dev, dev->c_handle);
29928b8a9b1dSJustin T. Gibbs 			} else {
29938b8a9b1dSJustin T. Gibbs 
29948b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
29958b8a9b1dSJustin T. Gibbs 			}
29968b8a9b1dSJustin T. Gibbs 
29978b8a9b1dSJustin T. Gibbs 			dev->c_handle =
29988b8a9b1dSJustin T. Gibbs 				timeout(xpt_release_devq_timeout,
29998b8a9b1dSJustin T. Gibbs 					dev,
30008b8a9b1dSJustin T. Gibbs 					(crs->release_timeout * hz) / 1000);
30018b8a9b1dSJustin T. Gibbs 
30028b8a9b1dSJustin T. Gibbs 			dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
30038b8a9b1dSJustin T. Gibbs 
30048b8a9b1dSJustin T. Gibbs 		}
30058b8a9b1dSJustin T. Gibbs 
30068b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
30078b8a9b1dSJustin T. Gibbs 
30088b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
30098b8a9b1dSJustin T. Gibbs 				/*
30108b8a9b1dSJustin T. Gibbs 				 * Decrement the freeze count so that a single
30118b8a9b1dSJustin T. Gibbs 				 * completion is still sufficient to unfreeze
30128b8a9b1dSJustin T. Gibbs 				 * the queue.
30138b8a9b1dSJustin T. Gibbs 				 */
30148b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
30158b8a9b1dSJustin T. Gibbs 			} else {
30168b8a9b1dSJustin T. Gibbs 
30178b8a9b1dSJustin T. Gibbs 				dev->flags |= CAM_DEV_REL_ON_COMPLETE;
30188b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
30198b8a9b1dSJustin T. Gibbs 			}
30208b8a9b1dSJustin T. Gibbs 		}
30218b8a9b1dSJustin T. Gibbs 
30228b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
30238b8a9b1dSJustin T. Gibbs 
30248b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
30258b8a9b1dSJustin T. Gibbs 			 || (dev->ccbq.dev_active == 0)) {
30268b8a9b1dSJustin T. Gibbs 
30278b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
30288b8a9b1dSJustin T. Gibbs 			} else {
30298b8a9b1dSJustin T. Gibbs 
30308b8a9b1dSJustin T. Gibbs 				dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
30318b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
30328b8a9b1dSJustin T. Gibbs 			}
30338b8a9b1dSJustin T. Gibbs 		}
30348b8a9b1dSJustin T. Gibbs 		splx(s);
30358b8a9b1dSJustin T. Gibbs 
30368b8a9b1dSJustin T. Gibbs 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
30378b8a9b1dSJustin T. Gibbs 
30388b8a9b1dSJustin T. Gibbs 			xpt_release_devq(crs->ccb_h.path->device,
30398b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
30408b8a9b1dSJustin T. Gibbs 		}
30418b8a9b1dSJustin T. Gibbs 		start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
30428b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
30438b8a9b1dSJustin T. Gibbs 		break;
30448b8a9b1dSJustin T. Gibbs 	}
30458b8a9b1dSJustin T. Gibbs 	case XPT_SCAN_BUS:
30468b8a9b1dSJustin T. Gibbs 		xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
30478b8a9b1dSJustin T. Gibbs 		break;
30488b8a9b1dSJustin T. Gibbs 	case XPT_SCAN_LUN:
30498b8a9b1dSJustin T. Gibbs 		xpt_scan_lun(start_ccb->ccb_h.path->periph,
30508b8a9b1dSJustin T. Gibbs 			     start_ccb->ccb_h.path, start_ccb->crcn.flags,
30518b8a9b1dSJustin T. Gibbs 			     start_ccb);
30528b8a9b1dSJustin T. Gibbs 		break;
30538b8a9b1dSJustin T. Gibbs 	case XPT_DEBUG: {
30548b8a9b1dSJustin T. Gibbs #ifdef CAMDEBUG
30558b8a9b1dSJustin T. Gibbs 		int s;
30568b8a9b1dSJustin T. Gibbs 
30578b8a9b1dSJustin T. Gibbs 		s = splcam();
30588b8a9b1dSJustin T. Gibbs 		cam_dflags = start_ccb->cdbg.flags;
30598b8a9b1dSJustin T. Gibbs 		if (cam_dpath != NULL) {
30608b8a9b1dSJustin T. Gibbs 			xpt_free_path(cam_dpath);
30618b8a9b1dSJustin T. Gibbs 			cam_dpath = NULL;
30628b8a9b1dSJustin T. Gibbs 		}
30638b8a9b1dSJustin T. Gibbs 
30648b8a9b1dSJustin T. Gibbs 		if (cam_dflags != CAM_DEBUG_NONE) {
30658b8a9b1dSJustin T. Gibbs 			if (xpt_create_path(&cam_dpath, xpt_periph,
30668b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.path_id,
30678b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.target_id,
30688b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.target_lun) !=
30698b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP) {
30708b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
30718b8a9b1dSJustin T. Gibbs 				cam_dflags = CAM_DEBUG_NONE;
3072aa872be6SMatt Jacob 			} else {
30738b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
3074aa872be6SMatt Jacob 				xpt_print_path(cam_dpath);
3075aa872be6SMatt Jacob 				printf("debugging flags now %x\n", cam_dflags);
3076aa872be6SMatt Jacob 			}
30778b8a9b1dSJustin T. Gibbs 		} else {
30788b8a9b1dSJustin T. Gibbs 			cam_dpath = NULL;
30798b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
30808b8a9b1dSJustin T. Gibbs 		}
30818b8a9b1dSJustin T. Gibbs 		splx(s);
30828b8a9b1dSJustin T. Gibbs #else /* !CAMDEBUG */
30838b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
30848b8a9b1dSJustin T. Gibbs #endif /* CAMDEBUG */
30858b8a9b1dSJustin T. Gibbs 		break;
30868b8a9b1dSJustin T. Gibbs 	}
30878b8a9b1dSJustin T. Gibbs 	case XPT_NOOP:
30888b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
30898b8a9b1dSJustin T. Gibbs 		break;
30908b8a9b1dSJustin T. Gibbs 	default:
30918b8a9b1dSJustin T. Gibbs 	case XPT_SDEV_TYPE:
30928b8a9b1dSJustin T. Gibbs 	case XPT_TERM_IO:
30938b8a9b1dSJustin T. Gibbs 	case XPT_ENG_INQ:
30948b8a9b1dSJustin T. Gibbs 		/* XXX Implement */
30958b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
30968b8a9b1dSJustin T. Gibbs 		break;
30978b8a9b1dSJustin T. Gibbs 	}
30988b8a9b1dSJustin T. Gibbs }
30998b8a9b1dSJustin T. Gibbs 
31008b8a9b1dSJustin T. Gibbs void
31018b8a9b1dSJustin T. Gibbs xpt_polled_action(union ccb *start_ccb)
31028b8a9b1dSJustin T. Gibbs {
31038b8a9b1dSJustin T. Gibbs 	int	  s;
31048b8a9b1dSJustin T. Gibbs 	u_int32_t timeout;
31058b8a9b1dSJustin T. Gibbs 	struct	  cam_sim *sim;
31068b8a9b1dSJustin T. Gibbs 	struct	  cam_devq *devq;
31078b8a9b1dSJustin T. Gibbs 	struct	  cam_ed *dev;
31088b8a9b1dSJustin T. Gibbs 
31098b8a9b1dSJustin T. Gibbs 	timeout = start_ccb->ccb_h.timeout;
31108b8a9b1dSJustin T. Gibbs 	sim = start_ccb->ccb_h.path->bus->sim;
31118b8a9b1dSJustin T. Gibbs 	devq = sim->devq;
31128b8a9b1dSJustin T. Gibbs 	dev = start_ccb->ccb_h.path->device;
31138b8a9b1dSJustin T. Gibbs 
31148b8a9b1dSJustin T. Gibbs 	s = splcam();
31158b8a9b1dSJustin T. Gibbs 
31168b8a9b1dSJustin T. Gibbs 	/*
31178b8a9b1dSJustin T. Gibbs 	 * Steal an opening so that no other queued requests
31188b8a9b1dSJustin T. Gibbs 	 * can get it before us while we simulate interrupts.
31198b8a9b1dSJustin T. Gibbs 	 */
31208b8a9b1dSJustin T. Gibbs 	dev->ccbq.devq_openings--;
31218b8a9b1dSJustin T. Gibbs 	dev->ccbq.dev_openings--;
31228b8a9b1dSJustin T. Gibbs 
31238b8a9b1dSJustin T. Gibbs 	while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0)
31248b8a9b1dSJustin T. Gibbs 	   && (--timeout > 0)) {
31258b8a9b1dSJustin T. Gibbs 		DELAY(1000);
31268b8a9b1dSJustin T. Gibbs 		(*(sim->sim_poll))(sim);
31278b8a9b1dSJustin T. Gibbs 		swi_camnet();
31288b8a9b1dSJustin T. Gibbs 		swi_cambio();
31298b8a9b1dSJustin T. Gibbs 	}
31308b8a9b1dSJustin T. Gibbs 
31318b8a9b1dSJustin T. Gibbs 	dev->ccbq.devq_openings++;
31328b8a9b1dSJustin T. Gibbs 	dev->ccbq.dev_openings++;
31338b8a9b1dSJustin T. Gibbs 
31348b8a9b1dSJustin T. Gibbs 	if (timeout != 0) {
31358b8a9b1dSJustin T. Gibbs 		xpt_action(start_ccb);
31368b8a9b1dSJustin T. Gibbs 		while(--timeout > 0) {
31378b8a9b1dSJustin T. Gibbs 			(*(sim->sim_poll))(sim);
31388b8a9b1dSJustin T. Gibbs 			swi_camnet();
31398b8a9b1dSJustin T. Gibbs 			swi_cambio();
31408b8a9b1dSJustin T. Gibbs 			if ((start_ccb->ccb_h.status  & CAM_STATUS_MASK)
31418b8a9b1dSJustin T. Gibbs 			    != CAM_REQ_INPROG)
31428b8a9b1dSJustin T. Gibbs 				break;
31438b8a9b1dSJustin T. Gibbs 			DELAY(1000);
31448b8a9b1dSJustin T. Gibbs 		}
31458b8a9b1dSJustin T. Gibbs 		if (timeout == 0) {
31468b8a9b1dSJustin T. Gibbs 			/*
31478b8a9b1dSJustin T. Gibbs 			 * XXX Is it worth adding a sim_timeout entry
31488b8a9b1dSJustin T. Gibbs 			 * point so we can attempt recovery?  If
31498b8a9b1dSJustin T. Gibbs 			 * this is only used for dumps, I don't think
31508b8a9b1dSJustin T. Gibbs 			 * it is.
31518b8a9b1dSJustin T. Gibbs 			 */
31528b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
31538b8a9b1dSJustin T. Gibbs 		}
31548b8a9b1dSJustin T. Gibbs 	} else {
31558b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
31568b8a9b1dSJustin T. Gibbs 	}
31578b8a9b1dSJustin T. Gibbs 	splx(s);
31588b8a9b1dSJustin T. Gibbs }
31598b8a9b1dSJustin T. Gibbs 
31608b8a9b1dSJustin T. Gibbs /*
31618b8a9b1dSJustin T. Gibbs  * Schedule a peripheral driver to receive a ccb when it's
31628b8a9b1dSJustin T. Gibbs  * target device has space for more transactions.
31638b8a9b1dSJustin T. Gibbs  */
31648b8a9b1dSJustin T. Gibbs void
31658b8a9b1dSJustin T. Gibbs xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
31668b8a9b1dSJustin T. Gibbs {
31678b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
31688b8a9b1dSJustin T. Gibbs 	int s;
31698b8a9b1dSJustin T. Gibbs 	int runq;
31708b8a9b1dSJustin T. Gibbs 
31718b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
31728b8a9b1dSJustin T. Gibbs 	device = perph->path->device;
31738b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
31748b8a9b1dSJustin T. Gibbs 	if (periph_is_queued(perph)) {
31758b8a9b1dSJustin T. Gibbs 		/* Simply reorder based on new priority */
31768b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
31778b8a9b1dSJustin T. Gibbs 			  ("   change priority to %d\n", new_priority));
31788b8a9b1dSJustin T. Gibbs 		if (new_priority < perph->pinfo.priority) {
31798b8a9b1dSJustin T. Gibbs 			camq_change_priority(&device->drvq,
31808b8a9b1dSJustin T. Gibbs 					     perph->pinfo.index,
31818b8a9b1dSJustin T. Gibbs 					     new_priority);
31828b8a9b1dSJustin T. Gibbs 		}
31838b8a9b1dSJustin T. Gibbs 		runq = 0;
31848b8a9b1dSJustin T. Gibbs 	} else {
31858b8a9b1dSJustin T. Gibbs 		/* New entry on the queue */
31868b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
31878b8a9b1dSJustin T. Gibbs 			  ("   added periph to queue\n"));
31888b8a9b1dSJustin T. Gibbs 		if (device->drvq.generation++ == 0) {
31898b8a9b1dSJustin T. Gibbs 			/* Generation wrap, regen all entries */
31908b8a9b1dSJustin T. Gibbs 			camq_regen(&device->drvq);
31918b8a9b1dSJustin T. Gibbs 		}
31928b8a9b1dSJustin T. Gibbs 		perph->pinfo.priority = new_priority;
31938b8a9b1dSJustin T. Gibbs 		perph->pinfo.generation = device->drvq.generation;
31948b8a9b1dSJustin T. Gibbs 		camq_insert(&device->drvq, &perph->pinfo);
31958b8a9b1dSJustin T. Gibbs 		runq = xpt_schedule_dev_allocq(perph->path->bus, device);
31968b8a9b1dSJustin T. Gibbs 	}
31978b8a9b1dSJustin T. Gibbs 	splx(s);
31988b8a9b1dSJustin T. Gibbs 	if (runq != 0) {
31998b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
32008b8a9b1dSJustin T. Gibbs 			  ("   calling xpt_run_devq\n"));
32018b8a9b1dSJustin T. Gibbs 		xpt_run_dev_allocq(perph->path->bus);
32028b8a9b1dSJustin T. Gibbs 	}
32038b8a9b1dSJustin T. Gibbs }
32048b8a9b1dSJustin T. Gibbs 
32058b8a9b1dSJustin T. Gibbs 
32068b8a9b1dSJustin T. Gibbs /*
32078b8a9b1dSJustin T. Gibbs  * Schedule a device to run on a given queue.
32088b8a9b1dSJustin T. Gibbs  * If the device was inserted as a new entry on the queue,
32098b8a9b1dSJustin T. Gibbs  * return 1 meaning the device queue should be run. If we
32108b8a9b1dSJustin T. Gibbs  * were already queued, implying someone else has already
32118b8a9b1dSJustin T. Gibbs  * started the queue, return 0 so the caller doesn't attempt
32128b8a9b1dSJustin T. Gibbs  * to run the queue.  Must be run at either splsoftcam
32138b8a9b1dSJustin T. Gibbs  * (or splcam since that encompases splsoftcam).
32148b8a9b1dSJustin T. Gibbs  */
32158b8a9b1dSJustin T. Gibbs static int
32168b8a9b1dSJustin T. Gibbs xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
32178b8a9b1dSJustin T. Gibbs 		 u_int32_t new_priority)
32188b8a9b1dSJustin T. Gibbs {
32198b8a9b1dSJustin T. Gibbs 	int retval;
32208b8a9b1dSJustin T. Gibbs 	u_int32_t old_priority;
32218b8a9b1dSJustin T. Gibbs 
3222aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
32238b8a9b1dSJustin T. Gibbs 
32248b8a9b1dSJustin T. Gibbs 	old_priority = pinfo->priority;
32258b8a9b1dSJustin T. Gibbs 
32268b8a9b1dSJustin T. Gibbs 	/*
32278b8a9b1dSJustin T. Gibbs 	 * Are we already queued?
32288b8a9b1dSJustin T. Gibbs 	 */
32298b8a9b1dSJustin T. Gibbs 	if (pinfo->index != CAM_UNQUEUED_INDEX) {
32308b8a9b1dSJustin T. Gibbs 		/* Simply reorder based on new priority */
32318b8a9b1dSJustin T. Gibbs 		if (new_priority < old_priority) {
32328b8a9b1dSJustin T. Gibbs 			camq_change_priority(queue, pinfo->index,
32338b8a9b1dSJustin T. Gibbs 					     new_priority);
3234aa872be6SMatt Jacob 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
32358b8a9b1dSJustin T. Gibbs 					("changed priority to %d\n",
32368b8a9b1dSJustin T. Gibbs 					 new_priority));
32378b8a9b1dSJustin T. Gibbs 		}
32388b8a9b1dSJustin T. Gibbs 		retval = 0;
32398b8a9b1dSJustin T. Gibbs 	} else {
32408b8a9b1dSJustin T. Gibbs 		/* New entry on the queue */
32418b8a9b1dSJustin T. Gibbs 		if (new_priority < old_priority)
32428b8a9b1dSJustin T. Gibbs 			pinfo->priority = new_priority;
32438b8a9b1dSJustin T. Gibbs 
3244aa872be6SMatt Jacob 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
32458b8a9b1dSJustin T. Gibbs 				("Inserting onto queue\n"));
32468b8a9b1dSJustin T. Gibbs 		if (queue->generation++ == 0) {
32478b8a9b1dSJustin T. Gibbs 			/* Generation wrap, regen all entries */
32488b8a9b1dSJustin T. Gibbs 			camq_regen(queue);
32498b8a9b1dSJustin T. Gibbs 		}
32508b8a9b1dSJustin T. Gibbs 		pinfo->generation = queue->generation;
32518b8a9b1dSJustin T. Gibbs 		camq_insert(queue, pinfo);
32528b8a9b1dSJustin T. Gibbs 		retval = 1;
32538b8a9b1dSJustin T. Gibbs 	}
32548b8a9b1dSJustin T. Gibbs 	return (retval);
32558b8a9b1dSJustin T. Gibbs }
32568b8a9b1dSJustin T. Gibbs 
32578b8a9b1dSJustin T. Gibbs static void
32588b8a9b1dSJustin T. Gibbs xpt_run_dev_allocq(struct cam_eb *bus)
32598b8a9b1dSJustin T. Gibbs {
32608b8a9b1dSJustin T. Gibbs 	struct	cam_devq *devq;
32618b8a9b1dSJustin T. Gibbs 	int	s;
32628b8a9b1dSJustin T. Gibbs 
3263aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
32648b8a9b1dSJustin T. Gibbs 	devq = bus->sim->devq;
32658b8a9b1dSJustin T. Gibbs 
3266aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
32678b8a9b1dSJustin T. Gibbs 			("   qfrozen_cnt == 0x%x, entries == %d, "
32688b8a9b1dSJustin T. Gibbs 			 "openings == %d, active == %d\n",
32698b8a9b1dSJustin T. Gibbs 			 devq->alloc_queue.qfrozen_cnt,
32708b8a9b1dSJustin T. Gibbs 			 devq->alloc_queue.entries,
32718b8a9b1dSJustin T. Gibbs 			 devq->alloc_openings,
32728b8a9b1dSJustin T. Gibbs 			 devq->alloc_active));
32738b8a9b1dSJustin T. Gibbs 
32748b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
32758b8a9b1dSJustin T. Gibbs 	devq->alloc_queue.qfrozen_cnt++;
32768b8a9b1dSJustin T. Gibbs 	while ((devq->alloc_queue.entries > 0)
32778b8a9b1dSJustin T. Gibbs 	    && (devq->alloc_openings > 0)
32788b8a9b1dSJustin T. Gibbs 	    && (devq->alloc_queue.qfrozen_cnt <= 1)) {
32798b8a9b1dSJustin T. Gibbs 		struct	cam_ed_qinfo *qinfo;
32808b8a9b1dSJustin T. Gibbs 		struct	cam_ed *device;
32818b8a9b1dSJustin T. Gibbs 		union	ccb *work_ccb;
32828b8a9b1dSJustin T. Gibbs 		struct	cam_periph *drv;
32838b8a9b1dSJustin T. Gibbs 		struct	camq *drvq;
32848b8a9b1dSJustin T. Gibbs 
32858b8a9b1dSJustin T. Gibbs 		qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
32868b8a9b1dSJustin T. Gibbs 							   /*position*/0);
32878b8a9b1dSJustin T. Gibbs 		device = qinfo->device;
32888b8a9b1dSJustin T. Gibbs 
3289aa872be6SMatt Jacob 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
329050642f18SKenneth D. Merry 				("running device %p\n", device));
32918b8a9b1dSJustin T. Gibbs 
32928b8a9b1dSJustin T. Gibbs 		drvq = &device->drvq;
32938b8a9b1dSJustin T. Gibbs 
32948b8a9b1dSJustin T. Gibbs #ifdef CAMDEBUG
32958b8a9b1dSJustin T. Gibbs 		if (drvq->entries <= 0) {
32968b8a9b1dSJustin T. Gibbs 			panic("xpt_run_dev_allocq: "
32978b8a9b1dSJustin T. Gibbs 			      "Device on queue without any work to do");
32988b8a9b1dSJustin T. Gibbs 		}
32998b8a9b1dSJustin T. Gibbs #endif
33008b8a9b1dSJustin T. Gibbs 		if ((work_ccb = xpt_get_ccb(device)) != NULL) {
33018b8a9b1dSJustin T. Gibbs 			devq->alloc_openings--;
33028b8a9b1dSJustin T. Gibbs 			devq->alloc_active++;
33038b8a9b1dSJustin T. Gibbs 			drv = (struct cam_periph*)camq_remove(drvq,
33048b8a9b1dSJustin T. Gibbs 							      /*pos*/0);
33058b8a9b1dSJustin T. Gibbs 			/* Update priority */
33068b8a9b1dSJustin T. Gibbs 			if (drvq->entries > 0) {
33078b8a9b1dSJustin T. Gibbs 				qinfo->pinfo.priority = drvq->queue_array[0]->priority;
33088b8a9b1dSJustin T. Gibbs 			} else {
33098b8a9b1dSJustin T. Gibbs 				qinfo->pinfo.priority = CAM_PRIORITY_NONE;
33108b8a9b1dSJustin T. Gibbs 			}
33118b8a9b1dSJustin T. Gibbs 			splx(s);
33128b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
33138b8a9b1dSJustin T. Gibbs 				      drv->pinfo.priority);
3314aa872be6SMatt Jacob 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
33158b8a9b1dSJustin T. Gibbs 					("calling periph start\n"));
33168b8a9b1dSJustin T. Gibbs 			drv->periph_start(drv, work_ccb);
33178b8a9b1dSJustin T. Gibbs 		} else {
33188b8a9b1dSJustin T. Gibbs 			/*
33198b8a9b1dSJustin T. Gibbs 			 * Malloc failure in alloc_ccb
33208b8a9b1dSJustin T. Gibbs 			 */
33218b8a9b1dSJustin T. Gibbs 			/*
33228b8a9b1dSJustin T. Gibbs 			 * XXX add us to a list to be run from free_ccb
33238b8a9b1dSJustin T. Gibbs 			 * if we don't have any ccbs active on this
33248b8a9b1dSJustin T. Gibbs 			 * device queue otherwise we may never get run
33258b8a9b1dSJustin T. Gibbs 			 * again.
33268b8a9b1dSJustin T. Gibbs 			 */
33278b8a9b1dSJustin T. Gibbs 			break;
33288b8a9b1dSJustin T. Gibbs 		}
33298b8a9b1dSJustin T. Gibbs 
33308b8a9b1dSJustin T. Gibbs 		/* Raise IPL for possible insertion and test at top of loop */
33318b8a9b1dSJustin T. Gibbs 		s = splsoftcam();
33328b8a9b1dSJustin T. Gibbs 
33338b8a9b1dSJustin T. Gibbs 		if (drvq->entries > 0) {
33348b8a9b1dSJustin T. Gibbs 			/* We have more work.  Attempt to reschedule */
33358b8a9b1dSJustin T. Gibbs 			xpt_schedule_dev_allocq(bus, device);
33368b8a9b1dSJustin T. Gibbs 		}
33378b8a9b1dSJustin T. Gibbs 	}
33388b8a9b1dSJustin T. Gibbs 	devq->alloc_queue.qfrozen_cnt--;
33398b8a9b1dSJustin T. Gibbs 	splx(s);
33408b8a9b1dSJustin T. Gibbs }
33418b8a9b1dSJustin T. Gibbs 
33428b8a9b1dSJustin T. Gibbs static void
33438b8a9b1dSJustin T. Gibbs xpt_run_dev_sendq(struct cam_eb *bus)
33448b8a9b1dSJustin T. Gibbs {
33458b8a9b1dSJustin T. Gibbs 	struct	cam_devq *devq;
33468b8a9b1dSJustin T. Gibbs 	int	s;
33478b8a9b1dSJustin T. Gibbs 
3348aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
33498b8a9b1dSJustin T. Gibbs 
33508b8a9b1dSJustin T. Gibbs 	devq = bus->sim->devq;
33518b8a9b1dSJustin T. Gibbs 
33528b8a9b1dSJustin T. Gibbs 	s = splcam();
33538b8a9b1dSJustin T. Gibbs 	devq->send_queue.qfrozen_cnt++;
33548b8a9b1dSJustin T. Gibbs 	splx(s);
33558b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
33568b8a9b1dSJustin T. Gibbs 	while ((devq->send_queue.entries > 0)
33578b8a9b1dSJustin T. Gibbs 	    && (devq->send_openings > 0)) {
33588b8a9b1dSJustin T. Gibbs 		struct	cam_ed_qinfo *qinfo;
33598b8a9b1dSJustin T. Gibbs 		struct	cam_ed *device;
33608b8a9b1dSJustin T. Gibbs 		union ccb *work_ccb;
33618b8a9b1dSJustin T. Gibbs 		struct	cam_sim *sim;
33628b8a9b1dSJustin T. Gibbs 		int	ospl;
33638b8a9b1dSJustin T. Gibbs 
33648b8a9b1dSJustin T. Gibbs 		ospl = splcam();
33658b8a9b1dSJustin T. Gibbs 	    	if (devq->send_queue.qfrozen_cnt > 1) {
33668b8a9b1dSJustin T. Gibbs 			splx(ospl);
33678b8a9b1dSJustin T. Gibbs 			break;
33688b8a9b1dSJustin T. Gibbs 		}
33698b8a9b1dSJustin T. Gibbs 
33708b8a9b1dSJustin T. Gibbs 		qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
33718b8a9b1dSJustin T. Gibbs 							   /*position*/0);
33728b8a9b1dSJustin T. Gibbs 		device = qinfo->device;
33738b8a9b1dSJustin T. Gibbs 
33748b8a9b1dSJustin T. Gibbs 		/*
33758b8a9b1dSJustin T. Gibbs 		 * If the device has been "frozen", don't attempt
33768b8a9b1dSJustin T. Gibbs 		 * to run it.
33778b8a9b1dSJustin T. Gibbs 		 */
33788b8a9b1dSJustin T. Gibbs 		if (device->qfrozen_cnt > 0) {
33798b8a9b1dSJustin T. Gibbs 			splx(ospl);
33808b8a9b1dSJustin T. Gibbs 			continue;
33818b8a9b1dSJustin T. Gibbs 		}
33828b8a9b1dSJustin T. Gibbs 
3383aa872be6SMatt Jacob 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
338450642f18SKenneth D. Merry 				("running device %p\n", device));
33858b8a9b1dSJustin T. Gibbs 
33868b8a9b1dSJustin T. Gibbs 		work_ccb = cam_ccbq_peek_ccb(&device->ccbq, 0);
33878b8a9b1dSJustin T. Gibbs 		if (work_ccb == NULL) {
33888b8a9b1dSJustin T. Gibbs 			printf("device on run queue with no ccbs???");
33898b8a9b1dSJustin T. Gibbs 			splx(ospl);
33908b8a9b1dSJustin T. Gibbs 			continue;
33918b8a9b1dSJustin T. Gibbs 		}
33928b8a9b1dSJustin T. Gibbs 
33938b8a9b1dSJustin T. Gibbs 		if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
33948b8a9b1dSJustin T. Gibbs 
33958b8a9b1dSJustin T. Gibbs 		 	if (num_highpower <= 0) {
33968b8a9b1dSJustin T. Gibbs 				/*
33978b8a9b1dSJustin T. Gibbs 				 * We got a high power command, but we
33988b8a9b1dSJustin T. Gibbs 				 * don't have any available slots.  Freeze
33998b8a9b1dSJustin T. Gibbs 				 * the device queue until we have a slot
34008b8a9b1dSJustin T. Gibbs 				 * available.
34018b8a9b1dSJustin T. Gibbs 				 */
34028b8a9b1dSJustin T. Gibbs 				device->qfrozen_cnt++;
34038b8a9b1dSJustin T. Gibbs 				STAILQ_INSERT_TAIL(&highpowerq,
34048b8a9b1dSJustin T. Gibbs 						   &work_ccb->ccb_h,
34058b8a9b1dSJustin T. Gibbs 						   xpt_links.stqe);
34068b8a9b1dSJustin T. Gibbs 
34078b8a9b1dSJustin T. Gibbs 				splx(ospl);
34088b8a9b1dSJustin T. Gibbs 				continue;
34098b8a9b1dSJustin T. Gibbs 			} else {
34108b8a9b1dSJustin T. Gibbs 				/*
34118b8a9b1dSJustin T. Gibbs 				 * Consume a high power slot while
34128b8a9b1dSJustin T. Gibbs 				 * this ccb runs.
34138b8a9b1dSJustin T. Gibbs 				 */
34148b8a9b1dSJustin T. Gibbs 				num_highpower--;
34158b8a9b1dSJustin T. Gibbs 			}
34168b8a9b1dSJustin T. Gibbs 		}
34178b8a9b1dSJustin T. Gibbs 		devq->active_dev = device;
34188b8a9b1dSJustin T. Gibbs 		cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
34198b8a9b1dSJustin T. Gibbs 
34208b8a9b1dSJustin T. Gibbs 		cam_ccbq_send_ccb(&device->ccbq, work_ccb);
34218b8a9b1dSJustin T. Gibbs 		splx(ospl);
34228b8a9b1dSJustin T. Gibbs 
34238b8a9b1dSJustin T. Gibbs 		devq->send_openings--;
34248b8a9b1dSJustin T. Gibbs 		devq->send_active++;
34258b8a9b1dSJustin T. Gibbs 
34268b8a9b1dSJustin T. Gibbs 		if (device->ccbq.queue.entries > 0) {
34278b8a9b1dSJustin T. Gibbs 			qinfo->pinfo.priority =
34288b8a9b1dSJustin T. Gibbs 			    device->ccbq.queue.queue_array[0]->priority;
34298b8a9b1dSJustin T. Gibbs 			xpt_schedule_dev_sendq(bus, device);
34308b8a9b1dSJustin T. Gibbs 		} else {
34318b8a9b1dSJustin T. Gibbs 			qinfo->pinfo.priority = CAM_PRIORITY_NONE;
34328b8a9b1dSJustin T. Gibbs 		}
34338b8a9b1dSJustin T. Gibbs 
34348b8a9b1dSJustin T. Gibbs 		if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
34358b8a9b1dSJustin T. Gibbs 			/*
34368b8a9b1dSJustin T. Gibbs 			 * The client wants to freeze the queue
34378b8a9b1dSJustin T. Gibbs 			 * after this CCB is sent.
34388b8a9b1dSJustin T. Gibbs 			 */
34398b8a9b1dSJustin T. Gibbs 			ospl = splcam();
34408b8a9b1dSJustin T. Gibbs 			device->qfrozen_cnt++;
34418b8a9b1dSJustin T. Gibbs 			splx(ospl);
34428b8a9b1dSJustin T. Gibbs 		}
34438b8a9b1dSJustin T. Gibbs 
34448b8a9b1dSJustin T. Gibbs 		splx(s);
34458b8a9b1dSJustin T. Gibbs 
34468b8a9b1dSJustin T. Gibbs 		if ((device->inq_flags & SID_CmdQue) != 0)
34478b8a9b1dSJustin T. Gibbs 			work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
34488b8a9b1dSJustin T. Gibbs 		else
34498b8a9b1dSJustin T. Gibbs 			/*
34508b8a9b1dSJustin T. Gibbs 			 * Clear this in case of a retried CCB that failed
34518b8a9b1dSJustin T. Gibbs 			 * due to a rejected tag.
34528b8a9b1dSJustin T. Gibbs 			 */
34538b8a9b1dSJustin T. Gibbs 			work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
34548b8a9b1dSJustin T. Gibbs 
34558b8a9b1dSJustin T. Gibbs 		/*
34568b8a9b1dSJustin T. Gibbs 		 * Device queues can be shared among multiple sim instances
34578b8a9b1dSJustin T. Gibbs 		 * that reside on different busses.  Use the SIM in the queue
34588b8a9b1dSJustin T. Gibbs 		 * CCB's path, rather than the one in the bus that was passed
34598b8a9b1dSJustin T. Gibbs 		 * into this function.
34608b8a9b1dSJustin T. Gibbs 		 */
34618b8a9b1dSJustin T. Gibbs 		sim = work_ccb->ccb_h.path->bus->sim;
34628b8a9b1dSJustin T. Gibbs 		(*(sim->sim_action))(sim, work_ccb);
34638b8a9b1dSJustin T. Gibbs 
34648b8a9b1dSJustin T. Gibbs 		ospl = splcam();
34658b8a9b1dSJustin T. Gibbs 		devq->active_dev = NULL;
34668b8a9b1dSJustin T. Gibbs 		splx(ospl);
34678b8a9b1dSJustin T. Gibbs 		/* Raise IPL for possible insertion and test at top of loop */
34688b8a9b1dSJustin T. Gibbs 		s = splsoftcam();
34698b8a9b1dSJustin T. Gibbs 	}
34708b8a9b1dSJustin T. Gibbs 	splx(s);
34718b8a9b1dSJustin T. Gibbs 	s = splcam();
34728b8a9b1dSJustin T. Gibbs 	devq->send_queue.qfrozen_cnt--;
34738b8a9b1dSJustin T. Gibbs 	splx(s);
34748b8a9b1dSJustin T. Gibbs }
34758b8a9b1dSJustin T. Gibbs 
34768b8a9b1dSJustin T. Gibbs /*
34778b8a9b1dSJustin T. Gibbs  * This function merges stuff from the slave ccb into the master ccb, while
34788b8a9b1dSJustin T. Gibbs  * keeping important fields in the master ccb constant.
34798b8a9b1dSJustin T. Gibbs  */
34808b8a9b1dSJustin T. Gibbs void
34818b8a9b1dSJustin T. Gibbs xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
34828b8a9b1dSJustin T. Gibbs {
34838b8a9b1dSJustin T. Gibbs 	/*
34848b8a9b1dSJustin T. Gibbs 	 * Pull fields that are valid for peripheral drivers to set
34858b8a9b1dSJustin T. Gibbs 	 * into the master CCB along with the CCB "payload".
34868b8a9b1dSJustin T. Gibbs 	 */
34878b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
34888b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
34898b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
34908b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
34918b8a9b1dSJustin T. Gibbs 	bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
34928b8a9b1dSJustin T. Gibbs 	      sizeof(union ccb) - sizeof(struct ccb_hdr));
34938b8a9b1dSJustin T. Gibbs }
34948b8a9b1dSJustin T. Gibbs 
34958b8a9b1dSJustin T. Gibbs void
34968b8a9b1dSJustin T. Gibbs xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
34978b8a9b1dSJustin T. Gibbs {
34988b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
34998b8a9b1dSJustin T. Gibbs 	ccb_h->pinfo.priority = priority;
35008b8a9b1dSJustin T. Gibbs 	ccb_h->path = path;
35018b8a9b1dSJustin T. Gibbs 	ccb_h->path_id = path->bus->path_id;
35028b8a9b1dSJustin T. Gibbs 	if (path->target)
35038b8a9b1dSJustin T. Gibbs 		ccb_h->target_id = path->target->target_id;
35048b8a9b1dSJustin T. Gibbs 	else
35058b8a9b1dSJustin T. Gibbs 		ccb_h->target_id = CAM_TARGET_WILDCARD;
35068b8a9b1dSJustin T. Gibbs 	if (path->device) {
35078b8a9b1dSJustin T. Gibbs 		if (path->device->ccbq.queue.generation++ == 0) {
35088b8a9b1dSJustin T. Gibbs 			/* Generation wrap, regen all entries */
35098b8a9b1dSJustin T. Gibbs 			cam_ccbq_regen(&path->device->ccbq);
35108b8a9b1dSJustin T. Gibbs 		}
35118b8a9b1dSJustin T. Gibbs 		ccb_h->target_lun = path->device->lun_id;
35128b8a9b1dSJustin T. Gibbs 		ccb_h->pinfo.generation = path->device->ccbq.queue.generation;
35138b8a9b1dSJustin T. Gibbs 	} else {
35148b8a9b1dSJustin T. Gibbs 		ccb_h->target_lun = CAM_TARGET_WILDCARD;
35158b8a9b1dSJustin T. Gibbs 	}
35168b8a9b1dSJustin T. Gibbs 	ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
35178b8a9b1dSJustin T. Gibbs 	ccb_h->flags = 0;
35188b8a9b1dSJustin T. Gibbs }
35198b8a9b1dSJustin T. Gibbs 
35208b8a9b1dSJustin T. Gibbs /* Path manipulation functions */
35218b8a9b1dSJustin T. Gibbs cam_status
35228b8a9b1dSJustin T. Gibbs xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
35238b8a9b1dSJustin T. Gibbs 		path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
35248b8a9b1dSJustin T. Gibbs {
35258b8a9b1dSJustin T. Gibbs 	struct	   cam_path *path;
35268b8a9b1dSJustin T. Gibbs 	cam_status status;
35278b8a9b1dSJustin T. Gibbs 
35288b8a9b1dSJustin T. Gibbs 	path = (struct cam_path *)malloc(sizeof(*path), M_DEVBUF, M_NOWAIT);
35298b8a9b1dSJustin T. Gibbs 
35308b8a9b1dSJustin T. Gibbs 	if (path == NULL) {
35318b8a9b1dSJustin T. Gibbs 		status = CAM_RESRC_UNAVAIL;
35328b8a9b1dSJustin T. Gibbs 		return(status);
35338b8a9b1dSJustin T. Gibbs 	}
35348b8a9b1dSJustin T. Gibbs 	status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
35358b8a9b1dSJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
35368b8a9b1dSJustin T. Gibbs 		free(path, M_DEVBUF);
35378b8a9b1dSJustin T. Gibbs 		path = NULL;
35388b8a9b1dSJustin T. Gibbs 	}
35398b8a9b1dSJustin T. Gibbs 	*new_path_ptr = path;
35408b8a9b1dSJustin T. Gibbs 	return (status);
35418b8a9b1dSJustin T. Gibbs }
35428b8a9b1dSJustin T. Gibbs 
35438b8a9b1dSJustin T. Gibbs static cam_status
35448b8a9b1dSJustin T. Gibbs xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
35458b8a9b1dSJustin T. Gibbs 		 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
35468b8a9b1dSJustin T. Gibbs {
35478b8a9b1dSJustin T. Gibbs 	struct	     cam_eb *bus;
35488b8a9b1dSJustin T. Gibbs 	struct	     cam_et *target;
35498b8a9b1dSJustin T. Gibbs 	struct	     cam_ed *device;
35508b8a9b1dSJustin T. Gibbs 	cam_status   status;
35518b8a9b1dSJustin T. Gibbs 	int	     s;
35528b8a9b1dSJustin T. Gibbs 
35538b8a9b1dSJustin T. Gibbs 	status = CAM_REQ_CMP;	/* Completed without error */
35548b8a9b1dSJustin T. Gibbs 	target = NULL;		/* Wildcarded */
35558b8a9b1dSJustin T. Gibbs 	device = NULL;		/* Wildcarded */
35568b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
35578b8a9b1dSJustin T. Gibbs 	bus = xpt_find_bus(path_id);
35588b8a9b1dSJustin T. Gibbs 	if (bus == NULL) {
35598b8a9b1dSJustin T. Gibbs 		status = CAM_PATH_INVALID;
3560c8bead2aSJustin T. Gibbs 	} else {
35618b8a9b1dSJustin T. Gibbs 		target = xpt_find_target(bus, target_id);
35628b8a9b1dSJustin T. Gibbs 		if (target == NULL) {
35638b8a9b1dSJustin T. Gibbs 			/* Create one */
35648b8a9b1dSJustin T. Gibbs 			struct cam_et *new_target;
35658b8a9b1dSJustin T. Gibbs 
35668b8a9b1dSJustin T. Gibbs 			new_target = xpt_alloc_target(bus, target_id);
35678b8a9b1dSJustin T. Gibbs 			if (new_target == NULL) {
35688b8a9b1dSJustin T. Gibbs 				status = CAM_RESRC_UNAVAIL;
35698b8a9b1dSJustin T. Gibbs 			} else {
35708b8a9b1dSJustin T. Gibbs 				target = new_target;
35718b8a9b1dSJustin T. Gibbs 			}
35728b8a9b1dSJustin T. Gibbs 		}
3573c8bead2aSJustin T. Gibbs 		if (target != NULL) {
35748b8a9b1dSJustin T. Gibbs 			device = xpt_find_device(target, lun_id);
35758b8a9b1dSJustin T. Gibbs 			if (device == NULL) {
35768b8a9b1dSJustin T. Gibbs 				/* Create one */
35778b8a9b1dSJustin T. Gibbs 				struct cam_ed *new_device;
35788b8a9b1dSJustin T. Gibbs 
35798b8a9b1dSJustin T. Gibbs 				new_device = xpt_alloc_device(bus,
35808b8a9b1dSJustin T. Gibbs 							      target,
35818b8a9b1dSJustin T. Gibbs 							      lun_id);
35828b8a9b1dSJustin T. Gibbs 				if (new_device == NULL) {
35838b8a9b1dSJustin T. Gibbs 					status = CAM_RESRC_UNAVAIL;
35848b8a9b1dSJustin T. Gibbs 				} else {
35858b8a9b1dSJustin T. Gibbs 					device = new_device;
35868b8a9b1dSJustin T. Gibbs 				}
35878b8a9b1dSJustin T. Gibbs 			}
35888b8a9b1dSJustin T. Gibbs 		}
35898b8a9b1dSJustin T. Gibbs 	}
35908b8a9b1dSJustin T. Gibbs 
35918b8a9b1dSJustin T. Gibbs 	/*
35928b8a9b1dSJustin T. Gibbs 	 * Only touch the user's data if we are successful.
35938b8a9b1dSJustin T. Gibbs 	 */
35948b8a9b1dSJustin T. Gibbs 	if (status == CAM_REQ_CMP) {
35958b8a9b1dSJustin T. Gibbs 		new_path->periph = perph;
35968b8a9b1dSJustin T. Gibbs 		new_path->bus = bus;
35978b8a9b1dSJustin T. Gibbs 		new_path->target = target;
35988b8a9b1dSJustin T. Gibbs 		new_path->device = device;
35998b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
36008b8a9b1dSJustin T. Gibbs 	} else {
36018b8a9b1dSJustin T. Gibbs 		if (device != NULL)
36028b8a9b1dSJustin T. Gibbs 			xpt_release_device(bus, target, device);
36038b8a9b1dSJustin T. Gibbs 		if (target != NULL)
36048b8a9b1dSJustin T. Gibbs 			xpt_release_target(bus, target);
36058b8a9b1dSJustin T. Gibbs 	}
36068b8a9b1dSJustin T. Gibbs 	splx(s);
36078b8a9b1dSJustin T. Gibbs 	return (status);
36088b8a9b1dSJustin T. Gibbs }
36098b8a9b1dSJustin T. Gibbs 
36108b8a9b1dSJustin T. Gibbs static void
36118b8a9b1dSJustin T. Gibbs xpt_release_path(struct cam_path *path)
36128b8a9b1dSJustin T. Gibbs {
36138b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
36148b8a9b1dSJustin T. Gibbs 	if (path->device != NULL)
36158b8a9b1dSJustin T. Gibbs 		xpt_release_device(path->bus, path->target, path->device);
36168b8a9b1dSJustin T. Gibbs 	if (path->target != NULL)
36178b8a9b1dSJustin T. Gibbs 		xpt_release_target(path->bus, path->target);
36188b8a9b1dSJustin T. Gibbs }
36198b8a9b1dSJustin T. Gibbs 
36208b8a9b1dSJustin T. Gibbs void
36218b8a9b1dSJustin T. Gibbs xpt_free_path(struct cam_path *path)
36228b8a9b1dSJustin T. Gibbs {
36238b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
36248b8a9b1dSJustin T. Gibbs 	xpt_release_path(path);
36258b8a9b1dSJustin T. Gibbs 	free(path, M_DEVBUF);
36268b8a9b1dSJustin T. Gibbs }
36278b8a9b1dSJustin T. Gibbs 
36288b8a9b1dSJustin T. Gibbs 
36298b8a9b1dSJustin T. Gibbs /*
36308b8a9b1dSJustin T. Gibbs  * Return -1 for failure, 0 for exact match, 1 for match with wildcards.
36318b8a9b1dSJustin T. Gibbs  */
36328b8a9b1dSJustin T. Gibbs int
36338b8a9b1dSJustin T. Gibbs xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
36348b8a9b1dSJustin T. Gibbs {
36358b8a9b1dSJustin T. Gibbs 	int retval = 0;
36368b8a9b1dSJustin T. Gibbs 
36378b8a9b1dSJustin T. Gibbs 	if (path1->bus != path2->bus) {
36388b8a9b1dSJustin T. Gibbs 		if ((path1->bus == NULL)
36398b8a9b1dSJustin T. Gibbs 		 || (path2->bus == NULL))
36408b8a9b1dSJustin T. Gibbs 			retval = 1;
36418b8a9b1dSJustin T. Gibbs 		else
36428b8a9b1dSJustin T. Gibbs 			return (-1);
36438b8a9b1dSJustin T. Gibbs 	}
36448b8a9b1dSJustin T. Gibbs 	if (path1->target != path2->target) {
36458b8a9b1dSJustin T. Gibbs 		if ((path1->target == NULL)
36468b8a9b1dSJustin T. Gibbs 		 || (path2->target == NULL))
36478b8a9b1dSJustin T. Gibbs 			retval = 1;
36488b8a9b1dSJustin T. Gibbs 		else
36498b8a9b1dSJustin T. Gibbs 			return (-1);
36508b8a9b1dSJustin T. Gibbs 	}
36518b8a9b1dSJustin T. Gibbs 	if (path1->device != path2->device) {
36528b8a9b1dSJustin T. Gibbs 		if ((path1->device == NULL)
36538b8a9b1dSJustin T. Gibbs 		 || (path2->device == NULL))
36548b8a9b1dSJustin T. Gibbs 			retval = 1;
36558b8a9b1dSJustin T. Gibbs 		else
36568b8a9b1dSJustin T. Gibbs 			return (-1);
36578b8a9b1dSJustin T. Gibbs 	}
36588b8a9b1dSJustin T. Gibbs 	return (retval);
36598b8a9b1dSJustin T. Gibbs }
36608b8a9b1dSJustin T. Gibbs 
36618b8a9b1dSJustin T. Gibbs void
36628b8a9b1dSJustin T. Gibbs xpt_print_path(struct cam_path *path)
36638b8a9b1dSJustin T. Gibbs {
36648b8a9b1dSJustin T. Gibbs 	if (path == NULL)
36658b8a9b1dSJustin T. Gibbs 		printf("(nopath): ");
36668b8a9b1dSJustin T. Gibbs 	else {
36678b8a9b1dSJustin T. Gibbs 		if (path->periph != NULL)
36688b8a9b1dSJustin T. Gibbs 			printf("(%s%d:", path->periph->periph_name,
36698b8a9b1dSJustin T. Gibbs 			       path->periph->unit_number);
36708b8a9b1dSJustin T. Gibbs 		else
36718b8a9b1dSJustin T. Gibbs 			printf("(noperiph:");
36728b8a9b1dSJustin T. Gibbs 
36738b8a9b1dSJustin T. Gibbs 		if (path->bus != NULL)
36748b8a9b1dSJustin T. Gibbs 			printf("%s%d:%d:", path->bus->sim->sim_name,
36758b8a9b1dSJustin T. Gibbs 			       path->bus->sim->unit_number,
36768b8a9b1dSJustin T. Gibbs 			       path->bus->sim->bus_id);
36778b8a9b1dSJustin T. Gibbs 		else
36788b8a9b1dSJustin T. Gibbs 			printf("nobus:");
36798b8a9b1dSJustin T. Gibbs 
36808b8a9b1dSJustin T. Gibbs 		if (path->target != NULL)
36818b8a9b1dSJustin T. Gibbs 			printf("%d:", path->target->target_id);
36828b8a9b1dSJustin T. Gibbs 		else
36838b8a9b1dSJustin T. Gibbs 			printf("X:");
36848b8a9b1dSJustin T. Gibbs 
36858b8a9b1dSJustin T. Gibbs 		if (path->device != NULL)
36868b8a9b1dSJustin T. Gibbs 			printf("%d): ", path->device->lun_id);
36878b8a9b1dSJustin T. Gibbs 		else
36888b8a9b1dSJustin T. Gibbs 			printf("X): ");
36898b8a9b1dSJustin T. Gibbs 	}
36908b8a9b1dSJustin T. Gibbs }
36918b8a9b1dSJustin T. Gibbs 
36928b8a9b1dSJustin T. Gibbs path_id_t
36938b8a9b1dSJustin T. Gibbs xpt_path_path_id(struct cam_path *path)
36948b8a9b1dSJustin T. Gibbs {
36958b8a9b1dSJustin T. Gibbs 	return(path->bus->path_id);
36968b8a9b1dSJustin T. Gibbs }
36978b8a9b1dSJustin T. Gibbs 
36988b8a9b1dSJustin T. Gibbs target_id_t
36998b8a9b1dSJustin T. Gibbs xpt_path_target_id(struct cam_path *path)
37008b8a9b1dSJustin T. Gibbs {
37018b8a9b1dSJustin T. Gibbs 	if (path->target != NULL)
37028b8a9b1dSJustin T. Gibbs 		return (path->target->target_id);
37038b8a9b1dSJustin T. Gibbs 	else
37048b8a9b1dSJustin T. Gibbs 		return (CAM_TARGET_WILDCARD);
37058b8a9b1dSJustin T. Gibbs }
37068b8a9b1dSJustin T. Gibbs 
37078b8a9b1dSJustin T. Gibbs lun_id_t
37088b8a9b1dSJustin T. Gibbs xpt_path_lun_id(struct cam_path *path)
37098b8a9b1dSJustin T. Gibbs {
37108b8a9b1dSJustin T. Gibbs 	if (path->device != NULL)
37118b8a9b1dSJustin T. Gibbs 		return (path->device->lun_id);
37128b8a9b1dSJustin T. Gibbs 	else
37138b8a9b1dSJustin T. Gibbs 		return (CAM_LUN_WILDCARD);
37148b8a9b1dSJustin T. Gibbs }
37158b8a9b1dSJustin T. Gibbs 
37168b8a9b1dSJustin T. Gibbs struct cam_sim *
37178b8a9b1dSJustin T. Gibbs xpt_path_sim(struct cam_path *path)
37188b8a9b1dSJustin T. Gibbs {
37198b8a9b1dSJustin T. Gibbs 	return (path->bus->sim);
37208b8a9b1dSJustin T. Gibbs }
37218b8a9b1dSJustin T. Gibbs 
37228b8a9b1dSJustin T. Gibbs struct cam_periph*
37238b8a9b1dSJustin T. Gibbs xpt_path_periph(struct cam_path *path)
37248b8a9b1dSJustin T. Gibbs {
37258b8a9b1dSJustin T. Gibbs 	return (path->periph);
37268b8a9b1dSJustin T. Gibbs }
37278b8a9b1dSJustin T. Gibbs 
37288b8a9b1dSJustin T. Gibbs /*
37298b8a9b1dSJustin T. Gibbs  * Release a CAM control block for the caller.  Remit the cost of the structure
37308b8a9b1dSJustin T. Gibbs  * to the device referenced by the path.  If the this device had no 'credits'
37318b8a9b1dSJustin T. Gibbs  * and peripheral drivers have registered async callbacks for this notification
37328b8a9b1dSJustin T. Gibbs  * call them now.
37338b8a9b1dSJustin T. Gibbs  */
37348b8a9b1dSJustin T. Gibbs void
37358b8a9b1dSJustin T. Gibbs xpt_release_ccb(union ccb *free_ccb)
37368b8a9b1dSJustin T. Gibbs {
37378b8a9b1dSJustin T. Gibbs 	int	 s;
37388b8a9b1dSJustin T. Gibbs 	struct	 cam_path *path;
37398b8a9b1dSJustin T. Gibbs 	struct	 cam_ed *device;
37408b8a9b1dSJustin T. Gibbs 	struct	 cam_eb *bus;
37418b8a9b1dSJustin T. Gibbs 
3742aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
37438b8a9b1dSJustin T. Gibbs 	path = free_ccb->ccb_h.path;
37448b8a9b1dSJustin T. Gibbs 	device = path->device;
37458b8a9b1dSJustin T. Gibbs 	bus = path->bus;
37468b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
37478b8a9b1dSJustin T. Gibbs 	cam_ccbq_release_opening(&device->ccbq);
37488b8a9b1dSJustin T. Gibbs 	if (xpt_ccb_count > xpt_max_ccbs) {
37498b8a9b1dSJustin T. Gibbs 		xpt_free_ccb(free_ccb);
37508b8a9b1dSJustin T. Gibbs 		xpt_ccb_count--;
37518b8a9b1dSJustin T. Gibbs 	} else {
37528b8a9b1dSJustin T. Gibbs 		SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
37538b8a9b1dSJustin T. Gibbs 	}
37548b8a9b1dSJustin T. Gibbs 	bus->sim->devq->alloc_openings++;
37558b8a9b1dSJustin T. Gibbs 	bus->sim->devq->alloc_active--;
37568b8a9b1dSJustin T. Gibbs 	/* XXX Turn this into an inline function - xpt_run_device?? */
37578b8a9b1dSJustin T. Gibbs 	if ((device_is_alloc_queued(device) == 0)
37588b8a9b1dSJustin T. Gibbs 	 && (device->drvq.entries > 0)) {
37598b8a9b1dSJustin T. Gibbs 		xpt_schedule_dev_allocq(bus, device);
37608b8a9b1dSJustin T. Gibbs 	}
37618b8a9b1dSJustin T. Gibbs 	splx(s);
37628b8a9b1dSJustin T. Gibbs 	if (dev_allocq_is_runnable(bus->sim->devq))
37638b8a9b1dSJustin T. Gibbs 		xpt_run_dev_allocq(bus);
37648b8a9b1dSJustin T. Gibbs }
37658b8a9b1dSJustin T. Gibbs 
37668b8a9b1dSJustin T. Gibbs /* Functions accessed by SIM drivers */
37678b8a9b1dSJustin T. Gibbs 
37688b8a9b1dSJustin T. Gibbs /*
37698b8a9b1dSJustin T. Gibbs  * A sim structure, listing the SIM entry points and instance
37708b8a9b1dSJustin T. Gibbs  * identification info is passed to xpt_bus_register to hook the SIM
37718b8a9b1dSJustin T. Gibbs  * into the CAM framework.  xpt_bus_register creates a cam_eb entry
37728b8a9b1dSJustin T. Gibbs  * for this new bus and places it in the array of busses and assigns
37738b8a9b1dSJustin T. Gibbs  * it a path_id.  The path_id may be influenced by "hard wiring"
37748b8a9b1dSJustin T. Gibbs  * information specified by the user.  Once interrupt services are
37758b8a9b1dSJustin T. Gibbs  * availible, the bus will be probed.
37768b8a9b1dSJustin T. Gibbs  */
37778b8a9b1dSJustin T. Gibbs int32_t
37788b8a9b1dSJustin T. Gibbs xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
37798b8a9b1dSJustin T. Gibbs {
37808b8a9b1dSJustin T. Gibbs 	static path_id_t buscount;
37818b8a9b1dSJustin T. Gibbs 	struct cam_eb *new_bus;
37828b8a9b1dSJustin T. Gibbs 	struct ccb_pathinq cpi;
37838b8a9b1dSJustin T. Gibbs 	int s;
37848b8a9b1dSJustin T. Gibbs 
37858b8a9b1dSJustin T. Gibbs 	sim->bus_id = bus;
37868b8a9b1dSJustin T. Gibbs 	new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
37878b8a9b1dSJustin T. Gibbs 					  M_DEVBUF, M_NOWAIT);
37888b8a9b1dSJustin T. Gibbs 	if (new_bus == NULL) {
37898b8a9b1dSJustin T. Gibbs 		/* Couldn't satisfy request */
37908b8a9b1dSJustin T. Gibbs 		return (CAM_RESRC_UNAVAIL);
37918b8a9b1dSJustin T. Gibbs 	}
37928b8a9b1dSJustin T. Gibbs 
37938b8a9b1dSJustin T. Gibbs 	bzero(new_bus, sizeof(*new_bus));
37948b8a9b1dSJustin T. Gibbs 
37958b8a9b1dSJustin T. Gibbs 	if (strcmp(sim->sim_name, "xpt") != 0) {
37968b8a9b1dSJustin T. Gibbs 
37978b8a9b1dSJustin T. Gibbs 		sim->path_id = xptpathid(sim->sim_name, sim->unit_number,
37988b8a9b1dSJustin T. Gibbs 					 sim->bus_id, &buscount);
37998b8a9b1dSJustin T. Gibbs 	}
38008b8a9b1dSJustin T. Gibbs 
38018b8a9b1dSJustin T. Gibbs 	new_bus->path_id = sim->path_id;
38028b8a9b1dSJustin T. Gibbs 	new_bus->sim = sim;
38038b8a9b1dSJustin T. Gibbs 	TAILQ_INIT(&new_bus->et_entries);
38048b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
38058b8a9b1dSJustin T. Gibbs 	TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
38068b8a9b1dSJustin T. Gibbs 	bus_generation++;
38078b8a9b1dSJustin T. Gibbs 
38088b8a9b1dSJustin T. Gibbs 	/* Notify interested parties */
38098b8a9b1dSJustin T. Gibbs 	if (sim->path_id != CAM_XPT_PATH_ID) {
38108b8a9b1dSJustin T. Gibbs 		struct cam_path path;
38118b8a9b1dSJustin T. Gibbs 
38128b8a9b1dSJustin T. Gibbs 		xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
38138b8a9b1dSJustin T. Gibbs 			         CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
38148b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
38158b8a9b1dSJustin T. Gibbs 		cpi.ccb_h.func_code = XPT_PATH_INQ;
38168b8a9b1dSJustin T. Gibbs 		xpt_action((union ccb *)&cpi);
38178b8a9b1dSJustin T. Gibbs 		xpt_async(AC_PATH_REGISTERED, xpt_periph->path, &cpi);
38188b8a9b1dSJustin T. Gibbs 		xpt_release_path(&path);
38198b8a9b1dSJustin T. Gibbs 	}
38208b8a9b1dSJustin T. Gibbs 	splx(s);
38218b8a9b1dSJustin T. Gibbs 	return (CAM_SUCCESS);
38228b8a9b1dSJustin T. Gibbs }
38238b8a9b1dSJustin T. Gibbs 
38248b8a9b1dSJustin T. Gibbs static int
38258b8a9b1dSJustin T. Gibbs xptnextfreebus(path_id_t startbus)
38268b8a9b1dSJustin T. Gibbs {
38278b8a9b1dSJustin T. Gibbs 	struct cam_sim_config *sim_conf;
38288b8a9b1dSJustin T. Gibbs 
38298b8a9b1dSJustin T. Gibbs 	sim_conf = cam_sinit;
38308b8a9b1dSJustin T. Gibbs 	while (sim_conf->sim_name != NULL) {
38318b8a9b1dSJustin T. Gibbs 
38328b8a9b1dSJustin T. Gibbs 		if (IS_SPECIFIED(sim_conf->pathid)
38338b8a9b1dSJustin T. Gibbs 		 && (startbus == sim_conf->pathid)) {
38348b8a9b1dSJustin T. Gibbs 			++startbus;
38358b8a9b1dSJustin T. Gibbs 			/* Start the search over */
38368b8a9b1dSJustin T. Gibbs 			sim_conf = cam_sinit;
38378b8a9b1dSJustin T. Gibbs 		} else {
38388b8a9b1dSJustin T. Gibbs 			sim_conf++;
38398b8a9b1dSJustin T. Gibbs 		}
38408b8a9b1dSJustin T. Gibbs 	}
38418b8a9b1dSJustin T. Gibbs 	return (startbus);
38428b8a9b1dSJustin T. Gibbs }
38438b8a9b1dSJustin T. Gibbs 
38448b8a9b1dSJustin T. Gibbs static int
38458b8a9b1dSJustin T. Gibbs xptpathid(const char *sim_name, int sim_unit,
38468b8a9b1dSJustin T. Gibbs 	  int sim_bus, path_id_t *nextpath)
38478b8a9b1dSJustin T. Gibbs {
38488b8a9b1dSJustin T. Gibbs 	struct cam_sim_config *sim_conf;
38498b8a9b1dSJustin T. Gibbs 	path_id_t pathid;
38508b8a9b1dSJustin T. Gibbs 
38518b8a9b1dSJustin T. Gibbs 	pathid = CAM_XPT_PATH_ID;
38528b8a9b1dSJustin T. Gibbs 	for (sim_conf = cam_sinit; sim_conf->sim_name != NULL; sim_conf++) {
38538b8a9b1dSJustin T. Gibbs 
38548b8a9b1dSJustin T. Gibbs 		if (!IS_SPECIFIED(sim_conf->pathid))
38558b8a9b1dSJustin T. Gibbs 			continue;
38568b8a9b1dSJustin T. Gibbs 
38578b8a9b1dSJustin T. Gibbs 		if (!strcmp(sim_name, sim_conf->sim_name)
38588b8a9b1dSJustin T. Gibbs 		 && (sim_unit == sim_conf->sim_unit)) {
38598b8a9b1dSJustin T. Gibbs 
38608b8a9b1dSJustin T. Gibbs 			if (IS_SPECIFIED(sim_conf->sim_bus)) {
38618b8a9b1dSJustin T. Gibbs 				if (sim_bus == sim_conf->sim_bus) {
38628b8a9b1dSJustin T. Gibbs 					pathid = sim_conf->pathid;
38638b8a9b1dSJustin T. Gibbs 					break;
38648b8a9b1dSJustin T. Gibbs 				}
38658b8a9b1dSJustin T. Gibbs 			} else if (sim_bus == 0) {
38668b8a9b1dSJustin T. Gibbs 				/* Unspecified matches bus 0 */
38678b8a9b1dSJustin T. Gibbs 				pathid = sim_conf->pathid;
38688b8a9b1dSJustin T. Gibbs 				break;
38698b8a9b1dSJustin T. Gibbs 			} else {
38708b8a9b1dSJustin T. Gibbs 				printf("Ambiguous scbus configuration for %s%d "
38718b8a9b1dSJustin T. Gibbs 				       "bus %d, cannot wire down.  The kernel "
38728b8a9b1dSJustin T. Gibbs 				       "config entry for scbus%d should "
38738b8a9b1dSJustin T. Gibbs 				       "specify a controller bus.\n"
38748b8a9b1dSJustin T. Gibbs 				       "Scbus will be assigned dynamically.\n",
38758b8a9b1dSJustin T. Gibbs 				       sim_name, sim_unit, sim_bus,
38768b8a9b1dSJustin T. Gibbs 				       sim_conf->pathid);
38778b8a9b1dSJustin T. Gibbs                              break;
38788b8a9b1dSJustin T. Gibbs 			}
38798b8a9b1dSJustin T. Gibbs 		}
38808b8a9b1dSJustin T. Gibbs 	}
38818b8a9b1dSJustin T. Gibbs 
38828b8a9b1dSJustin T. Gibbs 	if (pathid == CAM_XPT_PATH_ID) {
38838b8a9b1dSJustin T. Gibbs 		pathid = xptnextfreebus(*nextpath);
38848b8a9b1dSJustin T. Gibbs 		*nextpath = pathid + 1;
38858b8a9b1dSJustin T. Gibbs 	}
38868b8a9b1dSJustin T. Gibbs 	return (pathid);
38878b8a9b1dSJustin T. Gibbs }
38888b8a9b1dSJustin T. Gibbs 
38898b8a9b1dSJustin T. Gibbs int32_t
38908b8a9b1dSJustin T. Gibbs xpt_bus_deregister(path_id)
38918b8a9b1dSJustin T. Gibbs 	u_int8_t path_id;
38928b8a9b1dSJustin T. Gibbs {
38938b8a9b1dSJustin T. Gibbs 	/* XXX */
38948b8a9b1dSJustin T. Gibbs 	return (CAM_SUCCESS);
38958b8a9b1dSJustin T. Gibbs }
38968b8a9b1dSJustin T. Gibbs 
38978b8a9b1dSJustin T. Gibbs void
38988b8a9b1dSJustin T. Gibbs xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
38998b8a9b1dSJustin T. Gibbs {
39008b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus;
39018b8a9b1dSJustin T. Gibbs 	struct cam_et *target, *next_target;
39028b8a9b1dSJustin T. Gibbs 	struct cam_ed *device, *next_device;
39038b8a9b1dSJustin T. Gibbs 	int s;
39048b8a9b1dSJustin T. Gibbs 
39058b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
39068b8a9b1dSJustin T. Gibbs 
39078b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
39088b8a9b1dSJustin T. Gibbs 
39098b8a9b1dSJustin T. Gibbs 	bus = path->bus;
39108b8a9b1dSJustin T. Gibbs 
39118b8a9b1dSJustin T. Gibbs 	/*
39128b8a9b1dSJustin T. Gibbs 	 * Freeze the SIM queue for SCSI_DELAY ms to
39138b8a9b1dSJustin T. Gibbs 	 * allow the bus to settle.
39148b8a9b1dSJustin T. Gibbs 	 */
39158b8a9b1dSJustin T. Gibbs 	if (async_code == AC_BUS_RESET) {
39168b8a9b1dSJustin T. Gibbs 		struct cam_sim *sim;
39178b8a9b1dSJustin T. Gibbs 
39188b8a9b1dSJustin T. Gibbs 		sim = bus->sim;
39198b8a9b1dSJustin T. Gibbs 
39208b8a9b1dSJustin T. Gibbs 		/*
39218b8a9b1dSJustin T. Gibbs 		 * If there isn't already another timeout pending, go ahead
39228b8a9b1dSJustin T. Gibbs 		 * and freeze the simq and set the timeout flag.  If there
39238b8a9b1dSJustin T. Gibbs 		 * is another timeout pending, replace it with this
39248b8a9b1dSJustin T. Gibbs 		 * timeout.  There could be two bus reset async broadcasts
39258b8a9b1dSJustin T. Gibbs 		 * sent for some dual-channel controllers.
39268b8a9b1dSJustin T. Gibbs 		 */
39278b8a9b1dSJustin T. Gibbs 		if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) == 0) {
39288b8a9b1dSJustin T. Gibbs 			xpt_freeze_simq(sim, 1);
39298b8a9b1dSJustin T. Gibbs 			sim->flags |= CAM_SIM_REL_TIMEOUT_PENDING;
39308b8a9b1dSJustin T. Gibbs 		} else
39318b8a9b1dSJustin T. Gibbs 			untimeout(xpt_release_simq_timeout, sim, sim->c_handle);
39328b8a9b1dSJustin T. Gibbs 
39338b8a9b1dSJustin T. Gibbs 		sim->c_handle = timeout(xpt_release_simq_timeout,
39348b8a9b1dSJustin T. Gibbs 					sim, (SCSI_DELAY * hz) / 1000);
39358b8a9b1dSJustin T. Gibbs 	}
39368b8a9b1dSJustin T. Gibbs 
39378b8a9b1dSJustin T. Gibbs 	for (target = TAILQ_FIRST(&bus->et_entries);
39388b8a9b1dSJustin T. Gibbs 	     target != NULL;
39398b8a9b1dSJustin T. Gibbs 	     target = next_target) {
39408b8a9b1dSJustin T. Gibbs 
39418b8a9b1dSJustin T. Gibbs 		next_target = TAILQ_NEXT(target, links);
39428b8a9b1dSJustin T. Gibbs 
39438b8a9b1dSJustin T. Gibbs 		if (path->target != target
39448b8a9b1dSJustin T. Gibbs 		 && path->target != NULL)
39458b8a9b1dSJustin T. Gibbs 			continue;
39468b8a9b1dSJustin T. Gibbs 
39478b8a9b1dSJustin T. Gibbs 		for (device = TAILQ_FIRST(&target->ed_entries);
39488b8a9b1dSJustin T. Gibbs 		     device != NULL;
39498b8a9b1dSJustin T. Gibbs 		     device = next_device) {
39508b8a9b1dSJustin T. Gibbs 			cam_status status;
39518b8a9b1dSJustin T. Gibbs 			struct cam_path newpath;
39528b8a9b1dSJustin T. Gibbs 
39538b8a9b1dSJustin T. Gibbs 			next_device = TAILQ_NEXT(device, links);
39548b8a9b1dSJustin T. Gibbs 
39558b8a9b1dSJustin T. Gibbs 			if (path->device != device
39568b8a9b1dSJustin T. Gibbs 			 && path->device != NULL)
39578b8a9b1dSJustin T. Gibbs 				continue;
39588b8a9b1dSJustin T. Gibbs 
39598b8a9b1dSJustin T. Gibbs 			/*
3960fd21cc5eSJustin T. Gibbs 			 * We need our own path with wildcards expanded to
3961fd21cc5eSJustin T. Gibbs 			 * handle certain types of events.
39628b8a9b1dSJustin T. Gibbs 			 */
39638b8a9b1dSJustin T. Gibbs 			if ((async_code == AC_SENT_BDR)
3964fd21cc5eSJustin T. Gibbs 			 || (async_code == AC_BUS_RESET)
39658b8a9b1dSJustin T. Gibbs 			 || (async_code == AC_INQ_CHANGED))
39668b8a9b1dSJustin T. Gibbs 				status = xpt_compile_path(&newpath, NULL,
39678b8a9b1dSJustin T. Gibbs 							  bus->path_id,
39688b8a9b1dSJustin T. Gibbs 							  target->target_id,
39698b8a9b1dSJustin T. Gibbs 							  device->lun_id);
39708b8a9b1dSJustin T. Gibbs 			else
3971f0adc790SJustin T. Gibbs 				status = CAM_REQ_CMP_ERR;
39728b8a9b1dSJustin T. Gibbs 
3973f0adc790SJustin T. Gibbs 			if (status == CAM_REQ_CMP) {
3974fd21cc5eSJustin T. Gibbs 
3975fd21cc5eSJustin T. Gibbs 				/*
3976f0adc790SJustin T. Gibbs 				 * Allow transfer negotiation to occur in a
3977f0adc790SJustin T. Gibbs 				 * tag free environment.
3978fd21cc5eSJustin T. Gibbs 				 */
3979f0adc790SJustin T. Gibbs 				if (async_code == AC_SENT_BDR
3980f0adc790SJustin T. Gibbs 				  || async_code == AC_BUS_RESET)
3981f0adc790SJustin T. Gibbs 					xpt_toggle_tags(&newpath);
3982fd21cc5eSJustin T. Gibbs 
39838b8a9b1dSJustin T. Gibbs 				/*
3984f0adc790SJustin T. Gibbs 				 * If we send a BDR, freeze the device queue
3985f0adc790SJustin T. Gibbs 				 * for SCSI_DELAY ms to allow it to settle
3986f0adc790SJustin T. Gibbs 				 * down.
39878b8a9b1dSJustin T. Gibbs 				 */
39888b8a9b1dSJustin T. Gibbs 				if (async_code == AC_SENT_BDR) {
39898b8a9b1dSJustin T. Gibbs 					xpt_freeze_devq(&newpath, 1);
39908b8a9b1dSJustin T. Gibbs 					/*
39918b8a9b1dSJustin T. Gibbs 					 * Although this looks bad, it
39928b8a9b1dSJustin T. Gibbs 					 * isn't as bad as it seems.  We're
39938b8a9b1dSJustin T. Gibbs 					 * passing in a stack-allocated path
39948b8a9b1dSJustin T. Gibbs 					 * that we then immediately release
39958b8a9b1dSJustin T. Gibbs 					 * after scheduling a timeout to
39968b8a9b1dSJustin T. Gibbs 					 * release the device queue.  So
39978b8a9b1dSJustin T. Gibbs 					 * the path won't be around when
39988b8a9b1dSJustin T. Gibbs 					 * the timeout fires, right?  Right.
39998b8a9b1dSJustin T. Gibbs 					 * But it doesn't matter, since
40008b8a9b1dSJustin T. Gibbs 					 * xpt_release_devq and its timeout
40018b8a9b1dSJustin T. Gibbs 					 * function both take the device as
40028b8a9b1dSJustin T. Gibbs 					 * an argument.  Theoretically, the
40038b8a9b1dSJustin T. Gibbs 					 * device will still be there when
40048b8a9b1dSJustin T. Gibbs 					 * the timeout fires, even though
40058b8a9b1dSJustin T. Gibbs 					 * the path will be gone.
40068b8a9b1dSJustin T. Gibbs 					 */
40078b8a9b1dSJustin T. Gibbs 					cam_release_devq(
40088b8a9b1dSJustin T. Gibbs 						   &newpath,
40098b8a9b1dSJustin T. Gibbs 						   /*relsim_flags*/
40108b8a9b1dSJustin T. Gibbs 						   RELSIM_RELEASE_AFTER_TIMEOUT,
40118b8a9b1dSJustin T. Gibbs 						   /*reduction*/0,
40128b8a9b1dSJustin T. Gibbs 						   /*timeout*/SCSI_DELAY,
40138b8a9b1dSJustin T. Gibbs 						   /*getcount_only*/0);
40148b8a9b1dSJustin T. Gibbs 				} else if (async_code == AC_INQ_CHANGED) {
40158b8a9b1dSJustin T. Gibbs 					/*
40168b8a9b1dSJustin T. Gibbs 					 * We've sent a start unit command, or
4017f0adc790SJustin T. Gibbs 					 * something similar to a device that
4018f0adc790SJustin T. Gibbs 					 * may have caused its inquiry data to
4019f0adc790SJustin T. Gibbs 					 * change. So we re-scan the device to
4020f0adc790SJustin T. Gibbs 					 * refresh the inquiry data for it.
40218b8a9b1dSJustin T. Gibbs 					 */
4022f0adc790SJustin T. Gibbs 					xpt_scan_lun(newpath.periph, &newpath,
40238b8a9b1dSJustin T. Gibbs 						     CAM_EXPECT_INQ_CHANGE,
40248b8a9b1dSJustin T. Gibbs 						     NULL);
40258b8a9b1dSJustin T. Gibbs 				}
4026f0adc790SJustin T. Gibbs 				xpt_release_path(&newpath);
402703e3511bSJustin T. Gibbs 			} else if (async_code == AC_LOST_DEVICE) {
40288b8a9b1dSJustin T. Gibbs 				device->flags |= CAM_DEV_UNCONFIGURED;
402903e3511bSJustin T. Gibbs 			} else if (async_code == AC_TRANSFER_NEG) {
403003e3511bSJustin T. Gibbs 				struct ccb_trans_settings *settings;
403103e3511bSJustin T. Gibbs 
403203e3511bSJustin T. Gibbs 				settings =
403303e3511bSJustin T. Gibbs 				    (struct ccb_trans_settings *)async_arg;
403403e3511bSJustin T. Gibbs 				xpt_set_transfer_settings(settings, device,
403503e3511bSJustin T. Gibbs 							  /*async_update*/TRUE);
403603e3511bSJustin T. Gibbs 			}
403703e3511bSJustin T. Gibbs 
40388b8a9b1dSJustin T. Gibbs 			xpt_async_bcast(&device->asyncs,
40398b8a9b1dSJustin T. Gibbs 					async_code,
40408b8a9b1dSJustin T. Gibbs 					path,
40418b8a9b1dSJustin T. Gibbs 					async_arg);
40428b8a9b1dSJustin T. Gibbs 		}
40438b8a9b1dSJustin T. Gibbs 	}
4044c8bead2aSJustin T. Gibbs 
4045c8bead2aSJustin T. Gibbs 	/*
4046c8bead2aSJustin T. Gibbs 	 * If this wasn't a fully wildcarded async, tell all
4047c8bead2aSJustin T. Gibbs 	 * clients that want all async events.
4048c8bead2aSJustin T. Gibbs 	 */
4049c8bead2aSJustin T. Gibbs 	if (bus != xpt_periph->path->bus)
4050c8bead2aSJustin T. Gibbs 		xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
40518b8a9b1dSJustin T. Gibbs 				path, async_arg);
40528b8a9b1dSJustin T. Gibbs 	splx(s);
40538b8a9b1dSJustin T. Gibbs }
40548b8a9b1dSJustin T. Gibbs 
40558b8a9b1dSJustin T. Gibbs static void
40568b8a9b1dSJustin T. Gibbs xpt_async_bcast(struct async_list *async_head,
40578b8a9b1dSJustin T. Gibbs 		u_int32_t async_code,
40588b8a9b1dSJustin T. Gibbs 		struct cam_path *path, void *async_arg)
40598b8a9b1dSJustin T. Gibbs {
40608b8a9b1dSJustin T. Gibbs 	struct async_node *cur_entry;
40618b8a9b1dSJustin T. Gibbs 
40628b8a9b1dSJustin T. Gibbs 	cur_entry = SLIST_FIRST(async_head);
40638b8a9b1dSJustin T. Gibbs 	while (cur_entry != NULL) {
40648b8a9b1dSJustin T. Gibbs 		struct async_node *next_entry;
40658b8a9b1dSJustin T. Gibbs 		/*
40668b8a9b1dSJustin T. Gibbs 		 * Grab the next list entry before we call the current
40678b8a9b1dSJustin T. Gibbs 		 * entry's callback.  This is because the callback function
40688b8a9b1dSJustin T. Gibbs 		 * can delete its async callback entry.
40698b8a9b1dSJustin T. Gibbs 		 */
40708b8a9b1dSJustin T. Gibbs 		next_entry = SLIST_NEXT(cur_entry, links);
40718b8a9b1dSJustin T. Gibbs 		if ((cur_entry->event_enable & async_code) != 0)
40728b8a9b1dSJustin T. Gibbs 			cur_entry->callback(cur_entry->callback_arg,
40738b8a9b1dSJustin T. Gibbs 					    async_code, path,
40748b8a9b1dSJustin T. Gibbs 					    async_arg);
40758b8a9b1dSJustin T. Gibbs 		cur_entry = next_entry;
40768b8a9b1dSJustin T. Gibbs 	}
40778b8a9b1dSJustin T. Gibbs }
40788b8a9b1dSJustin T. Gibbs 
40798b8a9b1dSJustin T. Gibbs u_int32_t
40808b8a9b1dSJustin T. Gibbs xpt_freeze_devq(struct cam_path *path, u_int count)
40818b8a9b1dSJustin T. Gibbs {
40828b8a9b1dSJustin T. Gibbs 	int s;
40838b8a9b1dSJustin T. Gibbs 	struct ccb_hdr *ccbh;
40848b8a9b1dSJustin T. Gibbs 
40858b8a9b1dSJustin T. Gibbs 	s = splcam();
40868b8a9b1dSJustin T. Gibbs 	path->device->qfrozen_cnt += count;
40878b8a9b1dSJustin T. Gibbs 
40888b8a9b1dSJustin T. Gibbs 	/*
40898b8a9b1dSJustin T. Gibbs 	 * Mark the last CCB in the queue as needing
40908b8a9b1dSJustin T. Gibbs 	 * to be requeued if the driver hasn't
40918b8a9b1dSJustin T. Gibbs 	 * changed it's state yet.  This fixes a race
40928b8a9b1dSJustin T. Gibbs 	 * where a ccb is just about to be queued to
40938b8a9b1dSJustin T. Gibbs 	 * a controller driver when it's interrupt routine
40948b8a9b1dSJustin T. Gibbs 	 * freezes the queue.  To completly close the
40958b8a9b1dSJustin T. Gibbs 	 * hole, controller drives must check to see
40968b8a9b1dSJustin T. Gibbs 	 * if a ccb's status is still CAM_REQ_INPROG
40978b8a9b1dSJustin T. Gibbs 	 * under spl protection just before they queue
40988b8a9b1dSJustin T. Gibbs 	 * the CCB.  See ahc_action/ahc_freeze_devq for
40998b8a9b1dSJustin T. Gibbs 	 * an example.
41008b8a9b1dSJustin T. Gibbs 	 */
4101bb6087e5SJustin T. Gibbs 	ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
41028b8a9b1dSJustin T. Gibbs 	if (ccbh && ccbh->status == CAM_REQ_INPROG)
41038b8a9b1dSJustin T. Gibbs 		ccbh->status = CAM_REQUEUE_REQ;
41048b8a9b1dSJustin T. Gibbs 	splx(s);
41058b8a9b1dSJustin T. Gibbs 	return (path->device->qfrozen_cnt);
41068b8a9b1dSJustin T. Gibbs }
41078b8a9b1dSJustin T. Gibbs 
41088b8a9b1dSJustin T. Gibbs u_int32_t
41098b8a9b1dSJustin T. Gibbs xpt_freeze_simq(struct cam_sim *sim, u_int count)
41108b8a9b1dSJustin T. Gibbs {
41118b8a9b1dSJustin T. Gibbs 	sim->devq->send_queue.qfrozen_cnt += count;
41128b8a9b1dSJustin T. Gibbs 	if (sim->devq->active_dev != NULL) {
41138b8a9b1dSJustin T. Gibbs 		struct ccb_hdr *ccbh;
41148b8a9b1dSJustin T. Gibbs 
41158b8a9b1dSJustin T. Gibbs 		ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4116bb6087e5SJustin T. Gibbs 				  ccb_hdr_tailq);
41178b8a9b1dSJustin T. Gibbs 		if (ccbh && ccbh->status == CAM_REQ_INPROG)
41188b8a9b1dSJustin T. Gibbs 			ccbh->status = CAM_REQUEUE_REQ;
41198b8a9b1dSJustin T. Gibbs 	}
41208b8a9b1dSJustin T. Gibbs 	return (sim->devq->send_queue.qfrozen_cnt);
41218b8a9b1dSJustin T. Gibbs }
41228b8a9b1dSJustin T. Gibbs 
41238b8a9b1dSJustin T. Gibbs static void
41248b8a9b1dSJustin T. Gibbs xpt_release_devq_timeout(void *arg)
41258b8a9b1dSJustin T. Gibbs {
41268b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
41278b8a9b1dSJustin T. Gibbs 
41288b8a9b1dSJustin T. Gibbs 	device = (struct cam_ed *)arg;
41298b8a9b1dSJustin T. Gibbs 
41308b8a9b1dSJustin T. Gibbs 	xpt_release_devq(device, /*run_queue*/TRUE);
41318b8a9b1dSJustin T. Gibbs }
41328b8a9b1dSJustin T. Gibbs 
41338b8a9b1dSJustin T. Gibbs void
41348b8a9b1dSJustin T. Gibbs xpt_release_devq(struct cam_ed *dev, int run_queue)
41358b8a9b1dSJustin T. Gibbs {
41368b8a9b1dSJustin T. Gibbs 	int	rundevq;
41378b8a9b1dSJustin T. Gibbs 	int	s;
41388b8a9b1dSJustin T. Gibbs 
41398b8a9b1dSJustin T. Gibbs 	rundevq = 0;
41408b8a9b1dSJustin T. Gibbs 	s = splcam();
41418b8a9b1dSJustin T. Gibbs 	if (dev->qfrozen_cnt > 0) {
41428b8a9b1dSJustin T. Gibbs 
41438b8a9b1dSJustin T. Gibbs 		dev->qfrozen_cnt--;
41448b8a9b1dSJustin T. Gibbs 		if (dev->qfrozen_cnt == 0) {
41458b8a9b1dSJustin T. Gibbs 
41468b8a9b1dSJustin T. Gibbs 			/*
41478b8a9b1dSJustin T. Gibbs 			 * No longer need to wait for a successful
41488b8a9b1dSJustin T. Gibbs 			 * command completion.
41498b8a9b1dSJustin T. Gibbs 			 */
41508b8a9b1dSJustin T. Gibbs 			dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
41518b8a9b1dSJustin T. Gibbs 
41528b8a9b1dSJustin T. Gibbs 			/*
41538b8a9b1dSJustin T. Gibbs 			 * Remove any timeouts that might be scheduled
41548b8a9b1dSJustin T. Gibbs 			 * to release this queue.
41558b8a9b1dSJustin T. Gibbs 			 */
41568b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
41578b8a9b1dSJustin T. Gibbs 				untimeout(xpt_release_devq_timeout, dev,
41588b8a9b1dSJustin T. Gibbs 					  dev->c_handle);
41598b8a9b1dSJustin T. Gibbs 				dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
41608b8a9b1dSJustin T. Gibbs 			}
41618b8a9b1dSJustin T. Gibbs 
41628b8a9b1dSJustin T. Gibbs 			/*
41638b8a9b1dSJustin T. Gibbs 			 * Now that we are unfrozen schedule the
41648b8a9b1dSJustin T. Gibbs 			 * device so any pending transactions are
41658b8a9b1dSJustin T. Gibbs 			 * run.
41668b8a9b1dSJustin T. Gibbs 			 */
41678b8a9b1dSJustin T. Gibbs 			if ((dev->ccbq.queue.entries > 0)
41688b8a9b1dSJustin T. Gibbs 			 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
41698b8a9b1dSJustin T. Gibbs 			 && (run_queue != 0)) {
41708b8a9b1dSJustin T. Gibbs 				rundevq = 1;
41718b8a9b1dSJustin T. Gibbs 			}
41728b8a9b1dSJustin T. Gibbs 		}
41738b8a9b1dSJustin T. Gibbs 	}
41748b8a9b1dSJustin T. Gibbs 	splx(s);
41758b8a9b1dSJustin T. Gibbs 	if (rundevq != 0)
41768b8a9b1dSJustin T. Gibbs 		xpt_run_dev_sendq(dev->target->bus);
41778b8a9b1dSJustin T. Gibbs }
41788b8a9b1dSJustin T. Gibbs 
41798b8a9b1dSJustin T. Gibbs void
41808b8a9b1dSJustin T. Gibbs xpt_release_simq(struct cam_sim *sim, int run_queue)
41818b8a9b1dSJustin T. Gibbs {
41828b8a9b1dSJustin T. Gibbs 	int	s;
41838b8a9b1dSJustin T. Gibbs 	struct	camq *sendq;
41848b8a9b1dSJustin T. Gibbs 
41858b8a9b1dSJustin T. Gibbs 	sendq = &(sim->devq->send_queue);
41868b8a9b1dSJustin T. Gibbs 	s = splcam();
41878b8a9b1dSJustin T. Gibbs 	if (sendq->qfrozen_cnt > 0) {
41888b8a9b1dSJustin T. Gibbs 
41898b8a9b1dSJustin T. Gibbs 		sendq->qfrozen_cnt--;
41908b8a9b1dSJustin T. Gibbs 		if (sendq->qfrozen_cnt == 0) {
41918b8a9b1dSJustin T. Gibbs 
41928b8a9b1dSJustin T. Gibbs 			/*
41938b8a9b1dSJustin T. Gibbs 			 * If there is a timeout scheduled to release this
41948b8a9b1dSJustin T. Gibbs 			 * sim queue, remove it.  The queue frozen count is
41958b8a9b1dSJustin T. Gibbs 			 * already at 0.
41968b8a9b1dSJustin T. Gibbs 			 */
41978b8a9b1dSJustin T. Gibbs 			if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
41988b8a9b1dSJustin T. Gibbs 				untimeout(xpt_release_simq_timeout, sim,
41998b8a9b1dSJustin T. Gibbs 					  sim->c_handle);
42008b8a9b1dSJustin T. Gibbs 				sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
42018b8a9b1dSJustin T. Gibbs 			}
42028b8a9b1dSJustin T. Gibbs 
42038b8a9b1dSJustin T. Gibbs 			splx(s);
42048b8a9b1dSJustin T. Gibbs 
42058b8a9b1dSJustin T. Gibbs 			if (run_queue) {
42068b8a9b1dSJustin T. Gibbs 				/*
42078b8a9b1dSJustin T. Gibbs 				 * Now that we are unfrozen run the send queue.
42088b8a9b1dSJustin T. Gibbs 				 */
42098b8a9b1dSJustin T. Gibbs 				xpt_run_dev_sendq(xpt_find_bus(sim->path_id));
42108b8a9b1dSJustin T. Gibbs 			}
42118b8a9b1dSJustin T. Gibbs 		} else
42128b8a9b1dSJustin T. Gibbs 			splx(s);
42138b8a9b1dSJustin T. Gibbs 	} else
42148b8a9b1dSJustin T. Gibbs 		splx(s);
42158b8a9b1dSJustin T. Gibbs }
42168b8a9b1dSJustin T. Gibbs 
42178b8a9b1dSJustin T. Gibbs static void
42188b8a9b1dSJustin T. Gibbs xpt_release_simq_timeout(void *arg)
42198b8a9b1dSJustin T. Gibbs {
42208b8a9b1dSJustin T. Gibbs 	struct cam_sim *sim;
42218b8a9b1dSJustin T. Gibbs 
42228b8a9b1dSJustin T. Gibbs 	sim = (struct cam_sim *)arg;
42238b8a9b1dSJustin T. Gibbs 	xpt_release_simq(sim, /* run_queue */ TRUE);
42248b8a9b1dSJustin T. Gibbs }
42258b8a9b1dSJustin T. Gibbs 
42268b8a9b1dSJustin T. Gibbs void
42278b8a9b1dSJustin T. Gibbs xpt_done(union ccb *done_ccb)
42288b8a9b1dSJustin T. Gibbs {
42298b8a9b1dSJustin T. Gibbs 	int s;
42308b8a9b1dSJustin T. Gibbs 
42318b8a9b1dSJustin T. Gibbs 	s = splcam();
42328b8a9b1dSJustin T. Gibbs 
42338b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
42348b8a9b1dSJustin T. Gibbs 	switch (done_ccb->ccb_h.func_code) {
42358b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
42368b8a9b1dSJustin T. Gibbs 	case XPT_ENG_EXEC:
42378b8a9b1dSJustin T. Gibbs 	case XPT_TARGET_IO:
42388b8a9b1dSJustin T. Gibbs 	case XPT_ACCEPT_TARGET_IO:
42398b8a9b1dSJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
42408b8a9b1dSJustin T. Gibbs 	case XPT_SCAN_BUS:
42418b8a9b1dSJustin T. Gibbs 	case XPT_SCAN_LUN:
42428b8a9b1dSJustin T. Gibbs 	{
42438b8a9b1dSJustin T. Gibbs 		/*
42448b8a9b1dSJustin T. Gibbs 		 * Queue up the request for handling by our SWI handler
42458b8a9b1dSJustin T. Gibbs 		 * any of the "non-immediate" type of ccbs.
42468b8a9b1dSJustin T. Gibbs 		 */
42478b8a9b1dSJustin T. Gibbs 		switch (done_ccb->ccb_h.path->periph->type) {
42488b8a9b1dSJustin T. Gibbs 		case CAM_PERIPH_BIO:
42498b8a9b1dSJustin T. Gibbs 			TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
42508b8a9b1dSJustin T. Gibbs 					  sim_links.tqe);
42518b8a9b1dSJustin T. Gibbs 			done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
42528b8a9b1dSJustin T. Gibbs 			setsoftcambio();
42538b8a9b1dSJustin T. Gibbs 			break;
42548b8a9b1dSJustin T. Gibbs 		case CAM_PERIPH_NET:
42558b8a9b1dSJustin T. Gibbs 			TAILQ_INSERT_TAIL(&cam_netq, &done_ccb->ccb_h,
42568b8a9b1dSJustin T. Gibbs 					  sim_links.tqe);
42578b8a9b1dSJustin T. Gibbs 			done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
42588b8a9b1dSJustin T. Gibbs 			setsoftcamnet();
42598b8a9b1dSJustin T. Gibbs 			break;
42608b8a9b1dSJustin T. Gibbs 		}
42618b8a9b1dSJustin T. Gibbs 		break;
42628b8a9b1dSJustin T. Gibbs 	}
42638b8a9b1dSJustin T. Gibbs 	default:
42648b8a9b1dSJustin T. Gibbs 		break;
42658b8a9b1dSJustin T. Gibbs 	}
42668b8a9b1dSJustin T. Gibbs 	splx(s);
42678b8a9b1dSJustin T. Gibbs }
42688b8a9b1dSJustin T. Gibbs 
42698b8a9b1dSJustin T. Gibbs union ccb *
42708b8a9b1dSJustin T. Gibbs xpt_alloc_ccb()
42718b8a9b1dSJustin T. Gibbs {
42728b8a9b1dSJustin T. Gibbs 	union ccb *new_ccb;
42738b8a9b1dSJustin T. Gibbs 
42748b8a9b1dSJustin T. Gibbs 	new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_WAITOK);
42758b8a9b1dSJustin T. Gibbs 	return (new_ccb);
42768b8a9b1dSJustin T. Gibbs }
42778b8a9b1dSJustin T. Gibbs 
42788b8a9b1dSJustin T. Gibbs void
42798b8a9b1dSJustin T. Gibbs xpt_free_ccb(union ccb *free_ccb)
42808b8a9b1dSJustin T. Gibbs {
42818b8a9b1dSJustin T. Gibbs 	free(free_ccb, M_DEVBUF);
42828b8a9b1dSJustin T. Gibbs }
42838b8a9b1dSJustin T. Gibbs 
42848b8a9b1dSJustin T. Gibbs 
42858b8a9b1dSJustin T. Gibbs 
42868b8a9b1dSJustin T. Gibbs /* Private XPT functions */
42878b8a9b1dSJustin T. Gibbs 
42888b8a9b1dSJustin T. Gibbs /*
42898b8a9b1dSJustin T. Gibbs  * Get a CAM control block for the caller. Charge the structure to the device
42908b8a9b1dSJustin T. Gibbs  * referenced by the path.  If the this device has no 'credits' then the
42918b8a9b1dSJustin T. Gibbs  * device already has the maximum number of outstanding operations under way
42928b8a9b1dSJustin T. Gibbs  * and we return NULL. If we don't have sufficient resources to allocate more
42938b8a9b1dSJustin T. Gibbs  * ccbs, we also return NULL.
42948b8a9b1dSJustin T. Gibbs  */
42958b8a9b1dSJustin T. Gibbs static union ccb *
42968b8a9b1dSJustin T. Gibbs xpt_get_ccb(struct cam_ed *device)
42978b8a9b1dSJustin T. Gibbs {
42988b8a9b1dSJustin T. Gibbs 	union ccb *new_ccb;
42998b8a9b1dSJustin T. Gibbs 	int s;
43008b8a9b1dSJustin T. Gibbs 
43018b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
43028b8a9b1dSJustin T. Gibbs 	if ((new_ccb = (union ccb *)ccb_freeq.slh_first) == NULL) {
43038b8a9b1dSJustin T. Gibbs 		new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_NOWAIT);
43048b8a9b1dSJustin T. Gibbs                 if (new_ccb == NULL) {
43058b8a9b1dSJustin T. Gibbs 			splx(s);
43068b8a9b1dSJustin T. Gibbs 			return (NULL);
43078b8a9b1dSJustin T. Gibbs 		}
43088b8a9b1dSJustin T. Gibbs 		callout_handle_init(&new_ccb->ccb_h.timeout_ch);
43098b8a9b1dSJustin T. Gibbs 		SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
43108b8a9b1dSJustin T. Gibbs 				  xpt_links.sle);
43118b8a9b1dSJustin T. Gibbs 		xpt_ccb_count++;
43128b8a9b1dSJustin T. Gibbs 	}
43138b8a9b1dSJustin T. Gibbs 	cam_ccbq_take_opening(&device->ccbq);
43148b8a9b1dSJustin T. Gibbs 	SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
43158b8a9b1dSJustin T. Gibbs 	splx(s);
43168b8a9b1dSJustin T. Gibbs 	return (new_ccb);
43178b8a9b1dSJustin T. Gibbs }
43188b8a9b1dSJustin T. Gibbs 
43198b8a9b1dSJustin T. Gibbs 
43208b8a9b1dSJustin T. Gibbs static struct cam_et *
43218b8a9b1dSJustin T. Gibbs xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
43228b8a9b1dSJustin T. Gibbs {
43238b8a9b1dSJustin T. Gibbs 	struct cam_et *target;
43248b8a9b1dSJustin T. Gibbs 
43258b8a9b1dSJustin T. Gibbs 	target = (struct cam_et *)malloc(sizeof(*target), M_DEVBUF, M_NOWAIT);
43268b8a9b1dSJustin T. Gibbs 	if (target != NULL) {
43278b8a9b1dSJustin T. Gibbs 		struct cam_et *cur_target;
43288b8a9b1dSJustin T. Gibbs 
43298b8a9b1dSJustin T. Gibbs 		target->bus = bus;
43308b8a9b1dSJustin T. Gibbs 		target->target_id = target_id;
43318b8a9b1dSJustin T. Gibbs 		target->refcount = 1;
43328b8a9b1dSJustin T. Gibbs 		TAILQ_INIT(&target->ed_entries);
43338b8a9b1dSJustin T. Gibbs 
43348b8a9b1dSJustin T. Gibbs 		/* Insertion sort into our bus's target list */
43358b8a9b1dSJustin T. Gibbs 		cur_target = TAILQ_FIRST(&bus->et_entries);
43368b8a9b1dSJustin T. Gibbs 		while (cur_target != NULL && cur_target->target_id < target_id)
43378b8a9b1dSJustin T. Gibbs 			cur_target = TAILQ_NEXT(cur_target, links);
43388b8a9b1dSJustin T. Gibbs 
43398b8a9b1dSJustin T. Gibbs 		if (cur_target != NULL) {
43408b8a9b1dSJustin T. Gibbs 			TAILQ_INSERT_BEFORE(cur_target, target, links);
43418b8a9b1dSJustin T. Gibbs 		} else {
43428b8a9b1dSJustin T. Gibbs 			TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
43438b8a9b1dSJustin T. Gibbs 			bus->generation++;
43448b8a9b1dSJustin T. Gibbs 		}
43458b8a9b1dSJustin T. Gibbs 	}
43468b8a9b1dSJustin T. Gibbs 	return (target);
43478b8a9b1dSJustin T. Gibbs }
43488b8a9b1dSJustin T. Gibbs 
43498b8a9b1dSJustin T. Gibbs void
43508b8a9b1dSJustin T. Gibbs xpt_release_target(struct cam_eb *bus, struct cam_et *target)
43518b8a9b1dSJustin T. Gibbs {
43528b8a9b1dSJustin T. Gibbs 	if ((--target->refcount == 0)
43538b8a9b1dSJustin T. Gibbs 	 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
43548b8a9b1dSJustin T. Gibbs 		TAILQ_REMOVE(&bus->et_entries, target, links);
43558b8a9b1dSJustin T. Gibbs 		bus->generation++;
43568b8a9b1dSJustin T. Gibbs 		free(target, M_DEVBUF);
43578b8a9b1dSJustin T. Gibbs 	}
43588b8a9b1dSJustin T. Gibbs }
43598b8a9b1dSJustin T. Gibbs 
43608b8a9b1dSJustin T. Gibbs static struct cam_ed *
43618b8a9b1dSJustin T. Gibbs xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
43628b8a9b1dSJustin T. Gibbs {
43638b8a9b1dSJustin T. Gibbs 	struct	 cam_ed *device;
43648b8a9b1dSJustin T. Gibbs 	struct 	 cam_devq *devq;
43658b8a9b1dSJustin T. Gibbs 	int32_t	 status;
43668b8a9b1dSJustin T. Gibbs 	int	 s;
43678b8a9b1dSJustin T. Gibbs 
43688b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
43698b8a9b1dSJustin T. Gibbs 	/* Make space for us in the device queue on our bus */
43708b8a9b1dSJustin T. Gibbs 	devq = bus->sim->devq;
43718b8a9b1dSJustin T. Gibbs 	status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
43728b8a9b1dSJustin T. Gibbs 	splx(s);
43738b8a9b1dSJustin T. Gibbs 
43748b8a9b1dSJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
43758b8a9b1dSJustin T. Gibbs 		device = NULL;
43768b8a9b1dSJustin T. Gibbs 	} else {
43778b8a9b1dSJustin T. Gibbs 		device = (struct cam_ed *)malloc(sizeof(*device),
43788b8a9b1dSJustin T. Gibbs 						 M_DEVBUF, M_NOWAIT);
43798b8a9b1dSJustin T. Gibbs 	}
43808b8a9b1dSJustin T. Gibbs 
43818b8a9b1dSJustin T. Gibbs 	if (device != NULL) {
43828b8a9b1dSJustin T. Gibbs 		struct cam_ed *cur_device;
43838b8a9b1dSJustin T. Gibbs 
43848b8a9b1dSJustin T. Gibbs 		bzero(device, sizeof(*device));
43858b8a9b1dSJustin T. Gibbs 
43868b8a9b1dSJustin T. Gibbs 		SLIST_INIT(&device->asyncs);
43878b8a9b1dSJustin T. Gibbs 		SLIST_INIT(&device->periphs);
43888b8a9b1dSJustin T. Gibbs 		callout_handle_init(&device->c_handle);
43898b8a9b1dSJustin T. Gibbs 		device->refcount = 1;
43908b8a9b1dSJustin T. Gibbs 		device->flags |= CAM_DEV_UNCONFIGURED;
43918b8a9b1dSJustin T. Gibbs 
43928b8a9b1dSJustin T. Gibbs 		cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
43938b8a9b1dSJustin T. Gibbs 		device->alloc_ccb_entry.device = device;
43948b8a9b1dSJustin T. Gibbs 		cam_init_pinfo(&device->send_ccb_entry.pinfo);
43958b8a9b1dSJustin T. Gibbs 		device->send_ccb_entry.device = device;
43968b8a9b1dSJustin T. Gibbs 
43978b8a9b1dSJustin T. Gibbs 		device->target = target;
43988b8a9b1dSJustin T. Gibbs 
43998b8a9b1dSJustin T. Gibbs 		device->lun_id = lun_id;
44008b8a9b1dSJustin T. Gibbs 
44018b8a9b1dSJustin T. Gibbs 		/* Initialize our queues */
44028b8a9b1dSJustin T. Gibbs 		if (camq_init(&device->drvq, 0) != 0) {
44038b8a9b1dSJustin T. Gibbs 			free(device, M_DEVBUF);
44048b8a9b1dSJustin T. Gibbs 			return (NULL);
44058b8a9b1dSJustin T. Gibbs 		}
44068b8a9b1dSJustin T. Gibbs 
44078b8a9b1dSJustin T. Gibbs 		if (cam_ccbq_init(&device->ccbq,
44088b8a9b1dSJustin T. Gibbs 				  bus->sim->max_dev_openings) != 0) {
44098b8a9b1dSJustin T. Gibbs 			camq_fini(&device->drvq);
44108b8a9b1dSJustin T. Gibbs 			free(device, M_DEVBUF);
44118b8a9b1dSJustin T. Gibbs 			return (NULL);
44128b8a9b1dSJustin T. Gibbs 		}
44138b8a9b1dSJustin T. Gibbs 		s = splsoftcam();
44148b8a9b1dSJustin T. Gibbs 		/*
44158b8a9b1dSJustin T. Gibbs 		 * XXX should be limited by number of CCBs this bus can
44168b8a9b1dSJustin T. Gibbs 		 * do.
44178b8a9b1dSJustin T. Gibbs 		 */
44188b8a9b1dSJustin T. Gibbs 		xpt_max_ccbs += device->ccbq.devq_openings;
44198b8a9b1dSJustin T. Gibbs 		/* Insertion sort into our target's device list */
44208b8a9b1dSJustin T. Gibbs 		cur_device = TAILQ_FIRST(&target->ed_entries);
44218b8a9b1dSJustin T. Gibbs 		while (cur_device != NULL && cur_device->lun_id < lun_id)
44228b8a9b1dSJustin T. Gibbs 			cur_device = TAILQ_NEXT(cur_device, links);
44238b8a9b1dSJustin T. Gibbs 		if (cur_device != NULL) {
44248b8a9b1dSJustin T. Gibbs 			TAILQ_INSERT_BEFORE(cur_device, device, links);
44258b8a9b1dSJustin T. Gibbs 		} else {
44268b8a9b1dSJustin T. Gibbs 			TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
44278b8a9b1dSJustin T. Gibbs 			target->generation++;
44288b8a9b1dSJustin T. Gibbs 		}
44298b8a9b1dSJustin T. Gibbs 		splx(s);
44308b8a9b1dSJustin T. Gibbs 	}
44318b8a9b1dSJustin T. Gibbs 	return (device);
44328b8a9b1dSJustin T. Gibbs }
44338b8a9b1dSJustin T. Gibbs 
44348b8a9b1dSJustin T. Gibbs static void
44358b8a9b1dSJustin T. Gibbs xpt_release_device(struct cam_eb *bus, struct cam_et *target,
44368b8a9b1dSJustin T. Gibbs 		   struct cam_ed *device)
44378b8a9b1dSJustin T. Gibbs {
44388b8a9b1dSJustin T. Gibbs 	int s;
44398b8a9b1dSJustin T. Gibbs 
44408b8a9b1dSJustin T. Gibbs 	if ((--device->refcount == 0)
44418b8a9b1dSJustin T. Gibbs 	 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
44428b8a9b1dSJustin T. Gibbs 		struct cam_devq *devq;
44438b8a9b1dSJustin T. Gibbs 
44448b8a9b1dSJustin T. Gibbs 		s = splsoftcam();
44458b8a9b1dSJustin T. Gibbs 		TAILQ_REMOVE(&target->ed_entries, device,links);
44468b8a9b1dSJustin T. Gibbs 		target->generation++;
44478b8a9b1dSJustin T. Gibbs 		xpt_max_ccbs -= device->ccbq.devq_openings;
44488b8a9b1dSJustin T. Gibbs 		free(device, M_DEVBUF);
44498b8a9b1dSJustin T. Gibbs 		/* Release our slot in the devq */
44508b8a9b1dSJustin T. Gibbs 		devq = bus->sim->devq;
44518b8a9b1dSJustin T. Gibbs 		cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
44528b8a9b1dSJustin T. Gibbs 		splx(s);
44538b8a9b1dSJustin T. Gibbs 	}
44548b8a9b1dSJustin T. Gibbs }
44558b8a9b1dSJustin T. Gibbs 
44568b8a9b1dSJustin T. Gibbs static u_int32_t
44578b8a9b1dSJustin T. Gibbs xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
44588b8a9b1dSJustin T. Gibbs {
44598b8a9b1dSJustin T. Gibbs 	int	s;
44608b8a9b1dSJustin T. Gibbs 	int	diff;
44618b8a9b1dSJustin T. Gibbs 	int	result;
44628b8a9b1dSJustin T. Gibbs 	struct	cam_ed *dev;
44638b8a9b1dSJustin T. Gibbs 
44648b8a9b1dSJustin T. Gibbs 	dev = path->device;
44658b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
44668b8a9b1dSJustin T. Gibbs 
44678b8a9b1dSJustin T. Gibbs 	diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
44688b8a9b1dSJustin T. Gibbs 	result = cam_ccbq_resize(&dev->ccbq, newopenings);
44698b8a9b1dSJustin T. Gibbs 	if (result == CAM_REQ_CMP && (diff < 0)) {
44708b8a9b1dSJustin T. Gibbs 		dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
44718b8a9b1dSJustin T. Gibbs 	}
44728b8a9b1dSJustin T. Gibbs 	/* Adjust the global limit */
44738b8a9b1dSJustin T. Gibbs 	xpt_max_ccbs += diff;
44748b8a9b1dSJustin T. Gibbs 	splx(s);
44758b8a9b1dSJustin T. Gibbs 	return (result);
44768b8a9b1dSJustin T. Gibbs }
44778b8a9b1dSJustin T. Gibbs 
44788b8a9b1dSJustin T. Gibbs static struct cam_eb *
44798b8a9b1dSJustin T. Gibbs xpt_find_bus(path_id_t path_id)
44808b8a9b1dSJustin T. Gibbs {
44818b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus;
44828b8a9b1dSJustin T. Gibbs 
44838b8a9b1dSJustin T. Gibbs 	for (bus = TAILQ_FIRST(&xpt_busses);
44848b8a9b1dSJustin T. Gibbs 	     bus != NULL;
44858b8a9b1dSJustin T. Gibbs 	     bus = TAILQ_NEXT(bus, links)) {
44868b8a9b1dSJustin T. Gibbs 		if (bus->path_id == path_id)
44878b8a9b1dSJustin T. Gibbs 			break;
44888b8a9b1dSJustin T. Gibbs 	}
44898b8a9b1dSJustin T. Gibbs 	return (bus);
44908b8a9b1dSJustin T. Gibbs }
44918b8a9b1dSJustin T. Gibbs 
44928b8a9b1dSJustin T. Gibbs static struct cam_et *
44938b8a9b1dSJustin T. Gibbs xpt_find_target(struct cam_eb *bus, target_id_t	target_id)
44948b8a9b1dSJustin T. Gibbs {
44958b8a9b1dSJustin T. Gibbs 	struct cam_et *target;
44968b8a9b1dSJustin T. Gibbs 
44978b8a9b1dSJustin T. Gibbs 	for (target = TAILQ_FIRST(&bus->et_entries);
44988b8a9b1dSJustin T. Gibbs 	     target != NULL;
44998b8a9b1dSJustin T. Gibbs 	     target = TAILQ_NEXT(target, links)) {
45008b8a9b1dSJustin T. Gibbs 		if (target->target_id == target_id) {
45018b8a9b1dSJustin T. Gibbs 			target->refcount++;
45028b8a9b1dSJustin T. Gibbs 			break;
45038b8a9b1dSJustin T. Gibbs 		}
45048b8a9b1dSJustin T. Gibbs 	}
45058b8a9b1dSJustin T. Gibbs 	return (target);
45068b8a9b1dSJustin T. Gibbs }
45078b8a9b1dSJustin T. Gibbs 
45088b8a9b1dSJustin T. Gibbs static struct cam_ed *
45098b8a9b1dSJustin T. Gibbs xpt_find_device(struct cam_et *target, lun_id_t lun_id)
45108b8a9b1dSJustin T. Gibbs {
45118b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
45128b8a9b1dSJustin T. Gibbs 
45138b8a9b1dSJustin T. Gibbs 	for (device = TAILQ_FIRST(&target->ed_entries);
45148b8a9b1dSJustin T. Gibbs 	     device != NULL;
45158b8a9b1dSJustin T. Gibbs 	     device = TAILQ_NEXT(device, links)) {
45168b8a9b1dSJustin T. Gibbs 		if (device->lun_id == lun_id) {
45178b8a9b1dSJustin T. Gibbs 			device->refcount++;
45188b8a9b1dSJustin T. Gibbs 			break;
45198b8a9b1dSJustin T. Gibbs 		}
45208b8a9b1dSJustin T. Gibbs 	}
45218b8a9b1dSJustin T. Gibbs 	return (device);
45228b8a9b1dSJustin T. Gibbs }
45238b8a9b1dSJustin T. Gibbs 
45248b8a9b1dSJustin T. Gibbs typedef struct {
45258b8a9b1dSJustin T. Gibbs 	union	ccb *request_ccb;
45268b8a9b1dSJustin T. Gibbs 	struct 	ccb_pathinq *cpi;
45278b8a9b1dSJustin T. Gibbs 	int	pending_count;
45288b8a9b1dSJustin T. Gibbs } xpt_scan_bus_info;
45298b8a9b1dSJustin T. Gibbs 
45308b8a9b1dSJustin T. Gibbs /*
45318b8a9b1dSJustin T. Gibbs  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
45328b8a9b1dSJustin T. Gibbs  * As the scan progresses, xpt_scan_bus is used as the
45338b8a9b1dSJustin T. Gibbs  * callback on completion function.
45348b8a9b1dSJustin T. Gibbs  */
45358b8a9b1dSJustin T. Gibbs static void
45368b8a9b1dSJustin T. Gibbs xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
45378b8a9b1dSJustin T. Gibbs {
45388b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
45398b8a9b1dSJustin T. Gibbs 		  ("xpt_scan_bus\n"));
45408b8a9b1dSJustin T. Gibbs 	switch (request_ccb->ccb_h.func_code) {
45418b8a9b1dSJustin T. Gibbs 	case XPT_SCAN_BUS:
45428b8a9b1dSJustin T. Gibbs 	{
45438b8a9b1dSJustin T. Gibbs 		xpt_scan_bus_info *scan_info;
45448b8a9b1dSJustin T. Gibbs 		union	ccb *work_ccb;
45458b8a9b1dSJustin T. Gibbs 		struct	cam_path *path;
45468b8a9b1dSJustin T. Gibbs 		u_int	i;
45478b8a9b1dSJustin T. Gibbs 		u_int	max_target;
45488b8a9b1dSJustin T. Gibbs 		u_int	initiator_id;
45498b8a9b1dSJustin T. Gibbs 
45508b8a9b1dSJustin T. Gibbs 		/* Find out the characteristics of the bus */
45518b8a9b1dSJustin T. Gibbs 		work_ccb = xpt_alloc_ccb();
45528b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
45538b8a9b1dSJustin T. Gibbs 			      request_ccb->ccb_h.pinfo.priority);
45548b8a9b1dSJustin T. Gibbs 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
45558b8a9b1dSJustin T. Gibbs 		xpt_action(work_ccb);
45568b8a9b1dSJustin T. Gibbs 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
45578b8a9b1dSJustin T. Gibbs 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
45588b8a9b1dSJustin T. Gibbs 			xpt_free_ccb(work_ccb);
45598b8a9b1dSJustin T. Gibbs 			xpt_done(request_ccb);
45608b8a9b1dSJustin T. Gibbs 			return;
45618b8a9b1dSJustin T. Gibbs 		}
45628b8a9b1dSJustin T. Gibbs 
456398192658SJustin T. Gibbs 		if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
456498192658SJustin T. Gibbs 			/*
456598192658SJustin T. Gibbs 			 * Can't scan the bus on an adapter that
456698192658SJustin T. Gibbs 			 * cannot perform the initiator role.
456798192658SJustin T. Gibbs 			 */
456898192658SJustin T. Gibbs 			request_ccb->ccb_h.status = CAM_REQ_CMP;
456998192658SJustin T. Gibbs 			xpt_free_ccb(work_ccb);
457098192658SJustin T. Gibbs 			xpt_done(request_ccb);
457198192658SJustin T. Gibbs 			return;
457298192658SJustin T. Gibbs 		}
457398192658SJustin T. Gibbs 
45748b8a9b1dSJustin T. Gibbs 		/* Save some state for use while we probe for devices */
45758b8a9b1dSJustin T. Gibbs 		scan_info = (xpt_scan_bus_info *)
45768b8a9b1dSJustin T. Gibbs 		    malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK);
45778b8a9b1dSJustin T. Gibbs 		scan_info->request_ccb = request_ccb;
45788b8a9b1dSJustin T. Gibbs 		scan_info->cpi = &work_ccb->cpi;
45798b8a9b1dSJustin T. Gibbs 
45808b8a9b1dSJustin T. Gibbs 		/* Cache on our stack so we can work asynchronously */
45818b8a9b1dSJustin T. Gibbs 		max_target = scan_info->cpi->max_target;
45828b8a9b1dSJustin T. Gibbs 		initiator_id = scan_info->cpi->initiator_id;
45838b8a9b1dSJustin T. Gibbs 
45848b8a9b1dSJustin T. Gibbs 		/*
45858b8a9b1dSJustin T. Gibbs 		 * Don't count the initiator if the
45868b8a9b1dSJustin T. Gibbs 		 * initiator is addressable.
45878b8a9b1dSJustin T. Gibbs 		 */
45888b8a9b1dSJustin T. Gibbs 		scan_info->pending_count = max_target + 1;
45898b8a9b1dSJustin T. Gibbs 		if (initiator_id <= max_target)
45908b8a9b1dSJustin T. Gibbs 			scan_info->pending_count--;
45918b8a9b1dSJustin T. Gibbs 
45928b8a9b1dSJustin T. Gibbs 		for (i = 0; i <= max_target; i++) {
45938b8a9b1dSJustin T. Gibbs 			cam_status status;
45948b8a9b1dSJustin T. Gibbs 		 	if (i == initiator_id)
45958b8a9b1dSJustin T. Gibbs 				continue;
45968b8a9b1dSJustin T. Gibbs 
45978b8a9b1dSJustin T. Gibbs 			status = xpt_create_path(&path, xpt_periph,
45988b8a9b1dSJustin T. Gibbs 						 request_ccb->ccb_h.path_id,
45998b8a9b1dSJustin T. Gibbs 						 i, 0);
46008b8a9b1dSJustin T. Gibbs 			if (status != CAM_REQ_CMP) {
46018b8a9b1dSJustin T. Gibbs 				printf("xpt_scan_bus: xpt_create_path failed"
46028b8a9b1dSJustin T. Gibbs 				       " with status %#x, bus scan halted\n",
46038b8a9b1dSJustin T. Gibbs 				       status);
46048b8a9b1dSJustin T. Gibbs 				break;
46058b8a9b1dSJustin T. Gibbs 			}
46068b8a9b1dSJustin T. Gibbs 			work_ccb = xpt_alloc_ccb();
46078b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&work_ccb->ccb_h, path,
46088b8a9b1dSJustin T. Gibbs 				      request_ccb->ccb_h.pinfo.priority);
46098b8a9b1dSJustin T. Gibbs 			work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
46108b8a9b1dSJustin T. Gibbs 			work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
46118b8a9b1dSJustin T. Gibbs 			work_ccb->ccb_h.ppriv_ptr0 = scan_info;
46128b8a9b1dSJustin T. Gibbs 			work_ccb->crcn.flags = request_ccb->crcn.flags;
46138b8a9b1dSJustin T. Gibbs #if 0
46148b8a9b1dSJustin T. Gibbs 			printf("xpt_scan_bus: probing %d:%d:%d\n",
46158b8a9b1dSJustin T. Gibbs 				request_ccb->ccb_h.path_id, i, 0);
46168b8a9b1dSJustin T. Gibbs #endif
46178b8a9b1dSJustin T. Gibbs 			xpt_action(work_ccb);
46188b8a9b1dSJustin T. Gibbs 		}
46198b8a9b1dSJustin T. Gibbs 		break;
46208b8a9b1dSJustin T. Gibbs 	}
46218b8a9b1dSJustin T. Gibbs 	case XPT_SCAN_LUN:
46228b8a9b1dSJustin T. Gibbs 	{
46238b8a9b1dSJustin T. Gibbs 		xpt_scan_bus_info *scan_info;
46248b8a9b1dSJustin T. Gibbs 		path_id_t path_id;
46258b8a9b1dSJustin T. Gibbs 		target_id_t target_id;
46268b8a9b1dSJustin T. Gibbs 		lun_id_t lun_id;
46278b8a9b1dSJustin T. Gibbs 
46288b8a9b1dSJustin T. Gibbs 		/* Reuse the same CCB to query if a device was really found */
46298b8a9b1dSJustin T. Gibbs 		scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
46308b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
46318b8a9b1dSJustin T. Gibbs 			      request_ccb->ccb_h.pinfo.priority);
46328b8a9b1dSJustin T. Gibbs 		request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
46338b8a9b1dSJustin T. Gibbs 
46348b8a9b1dSJustin T. Gibbs 		path_id = request_ccb->ccb_h.path_id;
46358b8a9b1dSJustin T. Gibbs 		target_id = request_ccb->ccb_h.target_id;
46368b8a9b1dSJustin T. Gibbs 		lun_id = request_ccb->ccb_h.target_lun;
46378b8a9b1dSJustin T. Gibbs 		xpt_action(request_ccb);
46388b8a9b1dSJustin T. Gibbs 
46398b8a9b1dSJustin T. Gibbs #if 0
46408b8a9b1dSJustin T. Gibbs 		printf("xpt_scan_bus: got back probe from %d:%d:%d\n",
46418b8a9b1dSJustin T. Gibbs 			path_id, target_id, lun_id);
46428b8a9b1dSJustin T. Gibbs #endif
46438b8a9b1dSJustin T. Gibbs 
46448b8a9b1dSJustin T. Gibbs 		if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
46458b8a9b1dSJustin T. Gibbs 			struct cam_ed *device;
46468b8a9b1dSJustin T. Gibbs 			struct cam_et *target;
46478b8a9b1dSJustin T. Gibbs 
46488b8a9b1dSJustin T. Gibbs 			/*
46498b8a9b1dSJustin T. Gibbs 			 * If we already probed lun 0 successfully, or
46508b8a9b1dSJustin T. Gibbs 			 * we have additional configured luns on this
46518b8a9b1dSJustin T. Gibbs 			 * target that might have "gone away", go onto
46528b8a9b1dSJustin T. Gibbs 			 * the next lun.
46538b8a9b1dSJustin T. Gibbs 			 */
46548b8a9b1dSJustin T. Gibbs 			target = request_ccb->ccb_h.path->target;
46558b8a9b1dSJustin T. Gibbs 			device = TAILQ_FIRST(&target->ed_entries);
46568b8a9b1dSJustin T. Gibbs 			if (device != NULL)
46578b8a9b1dSJustin T. Gibbs 				device = TAILQ_NEXT(device, links);
46588b8a9b1dSJustin T. Gibbs 
46598b8a9b1dSJustin T. Gibbs 			if ((lun_id != 0) || (device != NULL)) {
46608b8a9b1dSJustin T. Gibbs 				/* Try the next lun */
46618b8a9b1dSJustin T. Gibbs 				lun_id++;
46628b8a9b1dSJustin T. Gibbs 			}
46638b8a9b1dSJustin T. Gibbs 		} else {
46648b8a9b1dSJustin T. Gibbs 			struct cam_ed *device;
46658b8a9b1dSJustin T. Gibbs 
46668b8a9b1dSJustin T. Gibbs 			device = request_ccb->ccb_h.path->device;
46678b8a9b1dSJustin T. Gibbs 
46688b8a9b1dSJustin T. Gibbs 			if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
46698b8a9b1dSJustin T. Gibbs 				/* Try the next lun */
46708b8a9b1dSJustin T. Gibbs 				lun_id++;
46718b8a9b1dSJustin T. Gibbs 			}
46728b8a9b1dSJustin T. Gibbs 		}
46738b8a9b1dSJustin T. Gibbs 
46748b8a9b1dSJustin T. Gibbs 		xpt_free_path(request_ccb->ccb_h.path);
46758b8a9b1dSJustin T. Gibbs 
46768b8a9b1dSJustin T. Gibbs 		/* Check Bounds */
46778b8a9b1dSJustin T. Gibbs 		if ((lun_id == request_ccb->ccb_h.target_lun)
46788b8a9b1dSJustin T. Gibbs 		 || lun_id > scan_info->cpi->max_lun) {
46798b8a9b1dSJustin T. Gibbs 			/* We're done */
46808b8a9b1dSJustin T. Gibbs 
46818b8a9b1dSJustin T. Gibbs 			xpt_free_ccb(request_ccb);
46828b8a9b1dSJustin T. Gibbs 			scan_info->pending_count--;
46838b8a9b1dSJustin T. Gibbs 			if (scan_info->pending_count == 0) {
46848b8a9b1dSJustin T. Gibbs 				xpt_free_ccb((union ccb *)scan_info->cpi);
46858b8a9b1dSJustin T. Gibbs 				request_ccb = scan_info->request_ccb;
46868b8a9b1dSJustin T. Gibbs 				free(scan_info, M_TEMP);
46878b8a9b1dSJustin T. Gibbs 				request_ccb->ccb_h.status = CAM_REQ_CMP;
46888b8a9b1dSJustin T. Gibbs 				xpt_done(request_ccb);
46898b8a9b1dSJustin T. Gibbs 			}
46908b8a9b1dSJustin T. Gibbs 		} else {
46918b8a9b1dSJustin T. Gibbs 			/* Try the next device */
46928b8a9b1dSJustin T. Gibbs 			struct cam_path *path;
46938b8a9b1dSJustin T. Gibbs 			cam_status status;
46948b8a9b1dSJustin T. Gibbs 
46958b8a9b1dSJustin T. Gibbs 			path = request_ccb->ccb_h.path;
46968b8a9b1dSJustin T. Gibbs 			status = xpt_create_path(&path, xpt_periph,
46978b8a9b1dSJustin T. Gibbs 						 path_id, target_id, lun_id);
46988b8a9b1dSJustin T. Gibbs 			if (status != CAM_REQ_CMP) {
46998b8a9b1dSJustin T. Gibbs 				printf("xpt_scan_bus: xpt_create_path failed "
47008b8a9b1dSJustin T. Gibbs 				       "with status %#x, halting LUN scan\n",
47018b8a9b1dSJustin T. Gibbs 			 	       status);
47028b8a9b1dSJustin T. Gibbs 				xpt_free_ccb(request_ccb);
47038b8a9b1dSJustin T. Gibbs 				scan_info->pending_count--;
47048b8a9b1dSJustin T. Gibbs 				if (scan_info->pending_count == 0) {
47058b8a9b1dSJustin T. Gibbs 					xpt_free_ccb(
47068b8a9b1dSJustin T. Gibbs 						(union ccb *)scan_info->cpi);
47078b8a9b1dSJustin T. Gibbs 					request_ccb = scan_info->request_ccb;
47088b8a9b1dSJustin T. Gibbs 					free(scan_info, M_TEMP);
47098b8a9b1dSJustin T. Gibbs 					request_ccb->ccb_h.status = CAM_REQ_CMP;
47108b8a9b1dSJustin T. Gibbs 					xpt_done(request_ccb);
47118b8a9b1dSJustin T. Gibbs 					break;
47128b8a9b1dSJustin T. Gibbs 				}
47138b8a9b1dSJustin T. Gibbs 			}
47148b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&request_ccb->ccb_h, path,
47158b8a9b1dSJustin T. Gibbs 				      request_ccb->ccb_h.pinfo.priority);
47168b8a9b1dSJustin T. Gibbs 			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
47178b8a9b1dSJustin T. Gibbs 			request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
47188b8a9b1dSJustin T. Gibbs 			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
47198b8a9b1dSJustin T. Gibbs 			request_ccb->crcn.flags =
47208b8a9b1dSJustin T. Gibbs 				scan_info->request_ccb->crcn.flags;
47218b8a9b1dSJustin T. Gibbs #if 0
47228b8a9b1dSJustin T. Gibbs 			xpt_print_path(path);
47238b8a9b1dSJustin T. Gibbs 			printf("xpt_scan bus probing\n");
47248b8a9b1dSJustin T. Gibbs #endif
47258b8a9b1dSJustin T. Gibbs 			xpt_action(request_ccb);
47268b8a9b1dSJustin T. Gibbs 		}
47278b8a9b1dSJustin T. Gibbs 		break;
47288b8a9b1dSJustin T. Gibbs 	}
47298b8a9b1dSJustin T. Gibbs 	default:
47308b8a9b1dSJustin T. Gibbs 		break;
47318b8a9b1dSJustin T. Gibbs 	}
47328b8a9b1dSJustin T. Gibbs }
47338b8a9b1dSJustin T. Gibbs 
47348b8a9b1dSJustin T. Gibbs typedef enum {
47358b8a9b1dSJustin T. Gibbs 	PROBE_TUR,
47368b8a9b1dSJustin T. Gibbs 	PROBE_INQUIRY,
47378b8a9b1dSJustin T. Gibbs 	PROBE_MODE_SENSE,
47388b8a9b1dSJustin T. Gibbs 	PROBE_SERIAL_NUM,
47398b8a9b1dSJustin T. Gibbs 	PROBE_TUR_FOR_NEGOTIATION
47408b8a9b1dSJustin T. Gibbs } probe_action;
47418b8a9b1dSJustin T. Gibbs 
47428b8a9b1dSJustin T. Gibbs typedef enum {
47438b8a9b1dSJustin T. Gibbs 	PROBE_INQUIRY_CKSUM	= 0x01,
47448b8a9b1dSJustin T. Gibbs 	PROBE_SERIAL_CKSUM	= 0x02,
47458b8a9b1dSJustin T. Gibbs 	PROBE_NO_ANNOUNCE	= 0x04
47468b8a9b1dSJustin T. Gibbs } probe_flags;
47478b8a9b1dSJustin T. Gibbs 
47488b8a9b1dSJustin T. Gibbs typedef struct {
47498b8a9b1dSJustin T. Gibbs 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
47508b8a9b1dSJustin T. Gibbs 	probe_action	action;
47518b8a9b1dSJustin T. Gibbs 	union ccb	saved_ccb;
47528b8a9b1dSJustin T. Gibbs 	probe_flags	flags;
47538b8a9b1dSJustin T. Gibbs 	MD5_CTX		context;
47548b8a9b1dSJustin T. Gibbs 	u_int8_t	digest[16];
47558b8a9b1dSJustin T. Gibbs } probe_softc;
47568b8a9b1dSJustin T. Gibbs 
47578b8a9b1dSJustin T. Gibbs static void
47588b8a9b1dSJustin T. Gibbs xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
47598b8a9b1dSJustin T. Gibbs 	     cam_flags flags, union ccb *request_ccb)
47608b8a9b1dSJustin T. Gibbs {
476198192658SJustin T. Gibbs 	struct ccb_pathinq cpi;
47628b8a9b1dSJustin T. Gibbs 	cam_status status;
47638b8a9b1dSJustin T. Gibbs 	struct cam_path *new_path;
47648b8a9b1dSJustin T. Gibbs 	struct cam_periph *old_periph;
47658b8a9b1dSJustin T. Gibbs 	int s;
47668b8a9b1dSJustin T. Gibbs 
47678b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
47688b8a9b1dSJustin T. Gibbs 		  ("xpt_scan_lun\n"));
47698b8a9b1dSJustin T. Gibbs 
477098192658SJustin T. Gibbs 	xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
477198192658SJustin T. Gibbs 	cpi.ccb_h.func_code = XPT_PATH_INQ;
477298192658SJustin T. Gibbs 	xpt_action((union ccb *)&cpi);
477398192658SJustin T. Gibbs 
477498192658SJustin T. Gibbs 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
477598192658SJustin T. Gibbs 		if (request_ccb != NULL) {
477698192658SJustin T. Gibbs 			request_ccb->ccb_h.status = cpi.ccb_h.status;
477798192658SJustin T. Gibbs 			xpt_done(request_ccb);
477898192658SJustin T. Gibbs 		}
477998192658SJustin T. Gibbs 		return;
478098192658SJustin T. Gibbs 	}
478198192658SJustin T. Gibbs 
478298192658SJustin T. Gibbs 	if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
478398192658SJustin T. Gibbs 		/*
478498192658SJustin T. Gibbs 		 * Can't scan the bus on an adapter that
478598192658SJustin T. Gibbs 		 * cannot perform the initiator role.
478698192658SJustin T. Gibbs 		 */
478798192658SJustin T. Gibbs 		if (request_ccb != NULL) {
478898192658SJustin T. Gibbs 			request_ccb->ccb_h.status = CAM_REQ_CMP;
478998192658SJustin T. Gibbs 			xpt_done(request_ccb);
479098192658SJustin T. Gibbs 		}
479198192658SJustin T. Gibbs 		return;
479298192658SJustin T. Gibbs 	}
479398192658SJustin T. Gibbs 
47948b8a9b1dSJustin T. Gibbs 	if (request_ccb == NULL) {
47958b8a9b1dSJustin T. Gibbs 		request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
47968b8a9b1dSJustin T. Gibbs 		if (request_ccb == NULL) {
47978b8a9b1dSJustin T. Gibbs 			xpt_print_path(path);
47988b8a9b1dSJustin T. Gibbs 			printf("xpt_scan_lun: can't allocate CCB, can't "
47998b8a9b1dSJustin T. Gibbs 			       "continue\n");
48008b8a9b1dSJustin T. Gibbs 			return;
48018b8a9b1dSJustin T. Gibbs 		}
48028b8a9b1dSJustin T. Gibbs 		new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
48038b8a9b1dSJustin T. Gibbs 		if (new_path == NULL) {
48048b8a9b1dSJustin T. Gibbs 			xpt_print_path(path);
48058b8a9b1dSJustin T. Gibbs 			printf("xpt_scan_lun: can't allocate path, can't "
48068b8a9b1dSJustin T. Gibbs 			       "continue\n");
48078b8a9b1dSJustin T. Gibbs 			free(request_ccb, M_TEMP);
48088b8a9b1dSJustin T. Gibbs 			return;
48098b8a9b1dSJustin T. Gibbs 		}
4810fce84cb4SKenneth D. Merry 		status = xpt_compile_path(new_path, xpt_periph,
4811fce84cb4SKenneth D. Merry 					  path->bus->path_id,
48128b8a9b1dSJustin T. Gibbs 					  path->target->target_id,
48138b8a9b1dSJustin T. Gibbs 					  path->device->lun_id);
48148b8a9b1dSJustin T. Gibbs 
48158b8a9b1dSJustin T. Gibbs 		if (status != CAM_REQ_CMP) {
48168b8a9b1dSJustin T. Gibbs 			xpt_print_path(path);
48178b8a9b1dSJustin T. Gibbs 			printf("xpt_scan_lun: can't compile path, can't "
48188b8a9b1dSJustin T. Gibbs 			       "continue\n");
48198b8a9b1dSJustin T. Gibbs 			free(request_ccb, M_TEMP);
48208b8a9b1dSJustin T. Gibbs 			free(new_path, M_TEMP);
48218b8a9b1dSJustin T. Gibbs 			return;
48228b8a9b1dSJustin T. Gibbs 		}
48238b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
48248b8a9b1dSJustin T. Gibbs 		request_ccb->ccb_h.cbfcnp = xptscandone;
48258b8a9b1dSJustin T. Gibbs 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
48268b8a9b1dSJustin T. Gibbs 		request_ccb->crcn.flags = flags;
48278b8a9b1dSJustin T. Gibbs 	}
48288b8a9b1dSJustin T. Gibbs 
48298b8a9b1dSJustin T. Gibbs 	s = splsoftcam();
48308b8a9b1dSJustin T. Gibbs 	if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
48318b8a9b1dSJustin T. Gibbs 		probe_softc *softc;
48328b8a9b1dSJustin T. Gibbs 
48338b8a9b1dSJustin T. Gibbs 		softc = (probe_softc *)old_periph->softc;
48348b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
48358b8a9b1dSJustin T. Gibbs 				  periph_links.tqe);
48368b8a9b1dSJustin T. Gibbs 	} else {
4837ee9c90c7SKenneth D. Merry 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
48388b8a9b1dSJustin T. Gibbs 					  probestart, "probe",
48398b8a9b1dSJustin T. Gibbs 					  CAM_PERIPH_BIO,
48408b8a9b1dSJustin T. Gibbs 					  request_ccb->ccb_h.path, NULL, 0,
48418b8a9b1dSJustin T. Gibbs 					  request_ccb);
48428b8a9b1dSJustin T. Gibbs 
48438b8a9b1dSJustin T. Gibbs 		if (status != CAM_REQ_CMP) {
48448b8a9b1dSJustin T. Gibbs 			xpt_print_path(path);
48458b8a9b1dSJustin T. Gibbs 			printf("xpt_scan_lun: cam_alloc_periph returned an "
48468b8a9b1dSJustin T. Gibbs 			       "error, can't continue probe\n");
48478b8a9b1dSJustin T. Gibbs 			request_ccb->ccb_h.status = status;
48488b8a9b1dSJustin T. Gibbs 			xpt_done(request_ccb);
48498b8a9b1dSJustin T. Gibbs 		}
48508b8a9b1dSJustin T. Gibbs 	}
48518b8a9b1dSJustin T. Gibbs 	splx(s);
48528b8a9b1dSJustin T. Gibbs }
48538b8a9b1dSJustin T. Gibbs 
48548b8a9b1dSJustin T. Gibbs static void
48558b8a9b1dSJustin T. Gibbs xptscandone(struct cam_periph *periph, union ccb *done_ccb)
48568b8a9b1dSJustin T. Gibbs {
48578b8a9b1dSJustin T. Gibbs 	xpt_release_path(done_ccb->ccb_h.path);
48588b8a9b1dSJustin T. Gibbs 	free(done_ccb->ccb_h.path, M_TEMP);
48598b8a9b1dSJustin T. Gibbs 	free(done_ccb, M_TEMP);
48608b8a9b1dSJustin T. Gibbs }
48618b8a9b1dSJustin T. Gibbs 
48628b8a9b1dSJustin T. Gibbs static cam_status
48638b8a9b1dSJustin T. Gibbs proberegister(struct cam_periph *periph, void *arg)
48648b8a9b1dSJustin T. Gibbs {
48658b8a9b1dSJustin T. Gibbs 	struct ccb_getdev *cgd;
48668b8a9b1dSJustin T. Gibbs 	probe_softc *softc;
48678b8a9b1dSJustin T. Gibbs 	union ccb *ccb;
48688b8a9b1dSJustin T. Gibbs 
48698b8a9b1dSJustin T. Gibbs 	cgd = (struct ccb_getdev *)arg;
48708b8a9b1dSJustin T. Gibbs 	if (periph == NULL) {
48718b8a9b1dSJustin T. Gibbs 		printf("proberegister: periph was NULL!!\n");
48728b8a9b1dSJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
48738b8a9b1dSJustin T. Gibbs 	}
48748b8a9b1dSJustin T. Gibbs 
48758b8a9b1dSJustin T. Gibbs 	if (cgd == NULL) {
48768b8a9b1dSJustin T. Gibbs 		printf("proberegister: no getdev CCB, can't register device\n");
48778b8a9b1dSJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
48788b8a9b1dSJustin T. Gibbs 	}
48798b8a9b1dSJustin T. Gibbs 
48809bc66b83SJustin T. Gibbs 	softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
48818b8a9b1dSJustin T. Gibbs 
48828b8a9b1dSJustin T. Gibbs 	if (softc == NULL) {
48838b8a9b1dSJustin T. Gibbs 		printf("proberegister: Unable to probe new device. "
48848b8a9b1dSJustin T. Gibbs 		       "Unable to allocate softc\n");
48858b8a9b1dSJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
48868b8a9b1dSJustin T. Gibbs 	}
48878b8a9b1dSJustin T. Gibbs 	ccb = (union ccb *)cgd;
48888b8a9b1dSJustin T. Gibbs 	TAILQ_INIT(&softc->request_ccbs);
48898b8a9b1dSJustin T. Gibbs 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &ccb->ccb_h, periph_links.tqe);
48908b8a9b1dSJustin T. Gibbs 	softc->flags = 0;
48918b8a9b1dSJustin T. Gibbs 	periph->softc = softc;
48928b8a9b1dSJustin T. Gibbs 	cam_periph_acquire(periph);
48938b8a9b1dSJustin T. Gibbs 	probeschedule(periph);
48948b8a9b1dSJustin T. Gibbs 	return(CAM_REQ_CMP);
48958b8a9b1dSJustin T. Gibbs }
48968b8a9b1dSJustin T. Gibbs 
48978b8a9b1dSJustin T. Gibbs static void
48988b8a9b1dSJustin T. Gibbs probeschedule(struct cam_periph *periph)
48998b8a9b1dSJustin T. Gibbs {
49008b8a9b1dSJustin T. Gibbs 	union ccb *ccb;
49018b8a9b1dSJustin T. Gibbs 	probe_softc *softc;
49028b8a9b1dSJustin T. Gibbs 
49038b8a9b1dSJustin T. Gibbs 	softc = (probe_softc *)periph->softc;
49048b8a9b1dSJustin T. Gibbs 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
49058b8a9b1dSJustin T. Gibbs 
49068b8a9b1dSJustin T. Gibbs 	/*
49078b8a9b1dSJustin T. Gibbs 	 * If a device has gone away and another device, or the same one,
49088b8a9b1dSJustin T. Gibbs 	 * is back in the same place, it should have a unit attention
49098b8a9b1dSJustin T. Gibbs 	 * condition pending.  It will not report the unit attention in
49108b8a9b1dSJustin T. Gibbs 	 * response to an inquiry, which may leave invalid transfer
49118b8a9b1dSJustin T. Gibbs 	 * negotiations in effect.  The TUR will reveal the unit attention
49128b8a9b1dSJustin T. Gibbs 	 * condition.  Only send the TUR for lun 0, since some devices
49138b8a9b1dSJustin T. Gibbs 	 * will get confused by commands other than inquiry to non-existent
49148b8a9b1dSJustin T. Gibbs 	 * luns.  If you think a device has gone away start your scan from
49158b8a9b1dSJustin T. Gibbs 	 * lun 0.  This will insure that any bogus transfer settings are
49168b8a9b1dSJustin T. Gibbs 	 * invalidated.
49178b8a9b1dSJustin T. Gibbs 	 */
49188b8a9b1dSJustin T. Gibbs 	if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
49198b8a9b1dSJustin T. Gibbs 	 && (ccb->ccb_h.target_lun == 0))
49208b8a9b1dSJustin T. Gibbs 		softc->action = PROBE_TUR;
49218b8a9b1dSJustin T. Gibbs 	else
49228b8a9b1dSJustin T. Gibbs 		softc->action = PROBE_INQUIRY;
49238b8a9b1dSJustin T. Gibbs 
49248b8a9b1dSJustin T. Gibbs 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
49258b8a9b1dSJustin T. Gibbs 		softc->flags |= PROBE_NO_ANNOUNCE;
49268b8a9b1dSJustin T. Gibbs 	else
49278b8a9b1dSJustin T. Gibbs 		softc->flags &= ~PROBE_NO_ANNOUNCE;
49288b8a9b1dSJustin T. Gibbs 
49298b8a9b1dSJustin T. Gibbs 	xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
49308b8a9b1dSJustin T. Gibbs }
49318b8a9b1dSJustin T. Gibbs 
49328b8a9b1dSJustin T. Gibbs static void
49338b8a9b1dSJustin T. Gibbs probestart(struct cam_periph *periph, union ccb *start_ccb)
49348b8a9b1dSJustin T. Gibbs {
49358b8a9b1dSJustin T. Gibbs 	/* Probe the device that our peripheral driver points to */
49368b8a9b1dSJustin T. Gibbs 	struct ccb_scsiio *csio;
49378b8a9b1dSJustin T. Gibbs 	probe_softc *softc;
49388b8a9b1dSJustin T. Gibbs 
49398b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
49408b8a9b1dSJustin T. Gibbs 
49418b8a9b1dSJustin T. Gibbs 	softc = (probe_softc *)periph->softc;
49428b8a9b1dSJustin T. Gibbs 	csio = &start_ccb->csio;
49438b8a9b1dSJustin T. Gibbs 
49448b8a9b1dSJustin T. Gibbs 	switch (softc->action) {
49458b8a9b1dSJustin T. Gibbs 	case PROBE_TUR:
49468b8a9b1dSJustin T. Gibbs 	case PROBE_TUR_FOR_NEGOTIATION:
49478b8a9b1dSJustin T. Gibbs 	{
49488b8a9b1dSJustin T. Gibbs 		scsi_test_unit_ready(csio,
49498b8a9b1dSJustin T. Gibbs 				     /*retries*/4,
49508b8a9b1dSJustin T. Gibbs 				     probedone,
49518b8a9b1dSJustin T. Gibbs 				     MSG_SIMPLE_Q_TAG,
49528b8a9b1dSJustin T. Gibbs 				     SSD_FULL_SIZE,
4953e471e974SJustin T. Gibbs 				     /*timeout*/60000);
49548b8a9b1dSJustin T. Gibbs 		break;
49558b8a9b1dSJustin T. Gibbs 	}
49568b8a9b1dSJustin T. Gibbs 	case PROBE_INQUIRY:
49578b8a9b1dSJustin T. Gibbs 	{
49588b8a9b1dSJustin T. Gibbs 		struct scsi_inquiry_data *inq_buf;
49598b8a9b1dSJustin T. Gibbs 
49608b8a9b1dSJustin T. Gibbs 		inq_buf = &periph->path->device->inq_data;
49618b8a9b1dSJustin T. Gibbs 		/*
49628b8a9b1dSJustin T. Gibbs 		 * If the device is currently configured, we calculate an
49638b8a9b1dSJustin T. Gibbs 		 * MD5 checksum of the inquiry data, and if the serial number
49648b8a9b1dSJustin T. Gibbs 		 * length is greater than 0, add the serial number data
49658b8a9b1dSJustin T. Gibbs 		 * into the checksum as well.  Once the inquiry and the
49668b8a9b1dSJustin T. Gibbs 		 * serial number check finish, we attempt to figure out
49678b8a9b1dSJustin T. Gibbs 		 * whether we still have the same device.
49688b8a9b1dSJustin T. Gibbs 		 */
49698b8a9b1dSJustin T. Gibbs 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
49708b8a9b1dSJustin T. Gibbs 
49718b8a9b1dSJustin T. Gibbs 			MD5Init(&softc->context);
49728b8a9b1dSJustin T. Gibbs 			MD5Update(&softc->context, (unsigned char *)inq_buf,
49738b8a9b1dSJustin T. Gibbs 				  sizeof(struct scsi_inquiry_data));
49748b8a9b1dSJustin T. Gibbs 			softc->flags |= PROBE_INQUIRY_CKSUM;
49758b8a9b1dSJustin T. Gibbs 			if (periph->path->device->serial_num_len > 0) {
49768b8a9b1dSJustin T. Gibbs 				MD5Update(&softc->context,
49778b8a9b1dSJustin T. Gibbs 					  periph->path->device->serial_num,
49788b8a9b1dSJustin T. Gibbs 					  periph->path->device->serial_num_len);
49798b8a9b1dSJustin T. Gibbs 				softc->flags |= PROBE_SERIAL_CKSUM;
49808b8a9b1dSJustin T. Gibbs 			}
49818b8a9b1dSJustin T. Gibbs 			MD5Final(softc->digest, &softc->context);
49828b8a9b1dSJustin T. Gibbs 		}
49838b8a9b1dSJustin T. Gibbs 
49848b8a9b1dSJustin T. Gibbs 		scsi_inquiry(csio,
49858b8a9b1dSJustin T. Gibbs 			     /*retries*/4,
49868b8a9b1dSJustin T. Gibbs 			     probedone,
49878b8a9b1dSJustin T. Gibbs 			     MSG_SIMPLE_Q_TAG,
49888b8a9b1dSJustin T. Gibbs 			     (u_int8_t *)inq_buf,
49898b8a9b1dSJustin T. Gibbs 			     sizeof(*inq_buf),
49908b8a9b1dSJustin T. Gibbs 			     /*evpd*/FALSE,
49918b8a9b1dSJustin T. Gibbs 			     /*page_code*/0,
49928b8a9b1dSJustin T. Gibbs 			     SSD_MIN_SIZE,
4993e471e974SJustin T. Gibbs 			     /*timeout*/60 * 1000);
49948b8a9b1dSJustin T. Gibbs 		break;
49958b8a9b1dSJustin T. Gibbs 	}
49968b8a9b1dSJustin T. Gibbs 	case PROBE_MODE_SENSE:
49978b8a9b1dSJustin T. Gibbs 	{
49988b8a9b1dSJustin T. Gibbs 		void  *mode_buf;
49998b8a9b1dSJustin T. Gibbs 		int    mode_buf_len;
50008b8a9b1dSJustin T. Gibbs 
50018b8a9b1dSJustin T. Gibbs 		mode_buf_len = sizeof(struct scsi_mode_header_6)
50028b8a9b1dSJustin T. Gibbs 			     + sizeof(struct scsi_mode_blk_desc)
50038b8a9b1dSJustin T. Gibbs 			     + sizeof(struct scsi_control_page);
50048b8a9b1dSJustin T. Gibbs 		mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
50058b8a9b1dSJustin T. Gibbs 		if (mode_buf != NULL) {
50068b8a9b1dSJustin T. Gibbs 	                scsi_mode_sense(csio,
50078b8a9b1dSJustin T. Gibbs 					/*retries*/4,
50088b8a9b1dSJustin T. Gibbs 					probedone,
50098b8a9b1dSJustin T. Gibbs 					MSG_SIMPLE_Q_TAG,
50108b8a9b1dSJustin T. Gibbs 					/*dbd*/FALSE,
50118b8a9b1dSJustin T. Gibbs 					SMS_PAGE_CTRL_CURRENT,
50128b8a9b1dSJustin T. Gibbs 					SMS_CONTROL_MODE_PAGE,
50138b8a9b1dSJustin T. Gibbs 					mode_buf,
50148b8a9b1dSJustin T. Gibbs 					mode_buf_len,
50158b8a9b1dSJustin T. Gibbs 					SSD_FULL_SIZE,
5016e471e974SJustin T. Gibbs 					/*timeout*/60000);
50178b8a9b1dSJustin T. Gibbs 			break;
50188b8a9b1dSJustin T. Gibbs 		}
50198b8a9b1dSJustin T. Gibbs 		xpt_print_path(periph->path);
50208b8a9b1dSJustin T. Gibbs 		printf("Unable to mode sense control page - malloc failure\n");
50218b8a9b1dSJustin T. Gibbs 		softc->action = PROBE_SERIAL_NUM;
50228b8a9b1dSJustin T. Gibbs 		/* FALLTHROUGH */
50238b8a9b1dSJustin T. Gibbs 	}
50248b8a9b1dSJustin T. Gibbs 	case PROBE_SERIAL_NUM:
50258b8a9b1dSJustin T. Gibbs 	{
50268b8a9b1dSJustin T. Gibbs 		struct scsi_vpd_unit_serial_number *serial_buf;
50278b8a9b1dSJustin T. Gibbs 		struct cam_ed* device;
50288b8a9b1dSJustin T. Gibbs 
50298b8a9b1dSJustin T. Gibbs 		serial_buf = NULL;
50308b8a9b1dSJustin T. Gibbs 		device = periph->path->device;
50318b8a9b1dSJustin T. Gibbs 		device->serial_num = NULL;
50328b8a9b1dSJustin T. Gibbs 		device->serial_num_len = 0;
50338b8a9b1dSJustin T. Gibbs 
50348b8a9b1dSJustin T. Gibbs 		if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
50358b8a9b1dSJustin T. Gibbs 			serial_buf = (struct scsi_vpd_unit_serial_number *)
50368b8a9b1dSJustin T. Gibbs 				malloc(sizeof(*serial_buf), M_TEMP, M_NOWAIT);
50378b8a9b1dSJustin T. Gibbs 
50388b8a9b1dSJustin T. Gibbs 		if (serial_buf != NULL) {
50398b8a9b1dSJustin T. Gibbs 			bzero(serial_buf, sizeof(*serial_buf));
50408b8a9b1dSJustin T. Gibbs 			scsi_inquiry(csio,
50418b8a9b1dSJustin T. Gibbs 				     /*retries*/4,
50428b8a9b1dSJustin T. Gibbs 				     probedone,
50438b8a9b1dSJustin T. Gibbs 				     MSG_SIMPLE_Q_TAG,
50448b8a9b1dSJustin T. Gibbs 				     (u_int8_t *)serial_buf,
50458b8a9b1dSJustin T. Gibbs 				     sizeof(*serial_buf),
50468b8a9b1dSJustin T. Gibbs 				     /*evpd*/TRUE,
50478b8a9b1dSJustin T. Gibbs 				     SVPD_UNIT_SERIAL_NUMBER,
50488b8a9b1dSJustin T. Gibbs 				     SSD_MIN_SIZE,
5049e471e974SJustin T. Gibbs 				     /*timeout*/60 * 1000);
50508b8a9b1dSJustin T. Gibbs 			break;
50518b8a9b1dSJustin T. Gibbs 		}
50528b8a9b1dSJustin T. Gibbs 		/*
50538b8a9b1dSJustin T. Gibbs 		 * We'll have to do without, let our probedone
50548b8a9b1dSJustin T. Gibbs 		 * routine finish up for us.
50558b8a9b1dSJustin T. Gibbs 		 */
50568b8a9b1dSJustin T. Gibbs 		start_ccb->csio.data_ptr = NULL;
50578b8a9b1dSJustin T. Gibbs 		probedone(periph, start_ccb);
50588b8a9b1dSJustin T. Gibbs 		return;
50598b8a9b1dSJustin T. Gibbs 	}
50608b8a9b1dSJustin T. Gibbs 	}
50618b8a9b1dSJustin T. Gibbs 	xpt_action(start_ccb);
50628b8a9b1dSJustin T. Gibbs }
50638b8a9b1dSJustin T. Gibbs 
50648b8a9b1dSJustin T. Gibbs static void
50658b8a9b1dSJustin T. Gibbs probedone(struct cam_periph *periph, union ccb *done_ccb)
50668b8a9b1dSJustin T. Gibbs {
50678b8a9b1dSJustin T. Gibbs 	probe_softc *softc;
50688b8a9b1dSJustin T. Gibbs 	struct cam_path *path;
50698b8a9b1dSJustin T. Gibbs 	u_int32_t  priority;
50708b8a9b1dSJustin T. Gibbs 
50718b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
50728b8a9b1dSJustin T. Gibbs 
50738b8a9b1dSJustin T. Gibbs 	softc = (probe_softc *)periph->softc;
50748b8a9b1dSJustin T. Gibbs 	path = done_ccb->ccb_h.path;
50758b8a9b1dSJustin T. Gibbs 	priority = done_ccb->ccb_h.pinfo.priority;
50768b8a9b1dSJustin T. Gibbs 
50778b8a9b1dSJustin T. Gibbs 	switch (softc->action) {
50788b8a9b1dSJustin T. Gibbs 	case PROBE_TUR:
50798b8a9b1dSJustin T. Gibbs 	{
50808b8a9b1dSJustin T. Gibbs 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
50818b8a9b1dSJustin T. Gibbs 
50828b8a9b1dSJustin T. Gibbs 			if (cam_periph_error(done_ccb, 0,
50838b8a9b1dSJustin T. Gibbs 					     SF_NO_PRINT, NULL) == ERESTART)
50848b8a9b1dSJustin T. Gibbs 				return;
50858b8a9b1dSJustin T. Gibbs 			else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
50868b8a9b1dSJustin T. Gibbs 				/* Don't wedge the queue */
50878b8a9b1dSJustin T. Gibbs 				xpt_release_devq(done_ccb->ccb_h.path->device,
50888b8a9b1dSJustin T. Gibbs 						 /*run_queue*/TRUE);
50898b8a9b1dSJustin T. Gibbs 		}
50908b8a9b1dSJustin T. Gibbs 		softc->action = PROBE_INQUIRY;
50918b8a9b1dSJustin T. Gibbs 		xpt_release_ccb(done_ccb);
50928b8a9b1dSJustin T. Gibbs 		xpt_schedule(periph, priority);
50938b8a9b1dSJustin T. Gibbs 		return;
50948b8a9b1dSJustin T. Gibbs 	}
50958b8a9b1dSJustin T. Gibbs 	case PROBE_INQUIRY:
50968b8a9b1dSJustin T. Gibbs 	{
50978b8a9b1dSJustin T. Gibbs 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
50988b8a9b1dSJustin T. Gibbs 			struct scsi_inquiry_data *inq_buf;
50998b8a9b1dSJustin T. Gibbs 			u_int8_t periph_qual;
51008b8a9b1dSJustin T. Gibbs 			u_int8_t periph_dtype;
51018b8a9b1dSJustin T. Gibbs 
51028b8a9b1dSJustin T. Gibbs 			inq_buf = &path->device->inq_data;
51038b8a9b1dSJustin T. Gibbs 
51048b8a9b1dSJustin T. Gibbs 			periph_qual = SID_QUAL(inq_buf);
51058b8a9b1dSJustin T. Gibbs 			periph_dtype = SID_TYPE(inq_buf);
51068b8a9b1dSJustin T. Gibbs 			if (periph_dtype != T_NODEVICE) {
51078b8a9b1dSJustin T. Gibbs 				switch(periph_qual) {
51088b8a9b1dSJustin T. Gibbs 				case SID_QUAL_LU_CONNECTED:
51098b8a9b1dSJustin T. Gibbs 				{
51108b8a9b1dSJustin T. Gibbs 					xpt_find_quirk(path->device);
51118b8a9b1dSJustin T. Gibbs 
51128b8a9b1dSJustin T. Gibbs 					if ((inq_buf->flags & SID_CmdQue) != 0)
51138b8a9b1dSJustin T. Gibbs 						softc->action =
51148b8a9b1dSJustin T. Gibbs 						    PROBE_MODE_SENSE;
51158b8a9b1dSJustin T. Gibbs 					else
51168b8a9b1dSJustin T. Gibbs 						softc->action =
51178b8a9b1dSJustin T. Gibbs 						    PROBE_SERIAL_NUM;
51188b8a9b1dSJustin T. Gibbs 
51198b8a9b1dSJustin T. Gibbs 					path->device->flags &=
51208b8a9b1dSJustin T. Gibbs 						~CAM_DEV_UNCONFIGURED;
51218b8a9b1dSJustin T. Gibbs 
51228b8a9b1dSJustin T. Gibbs 					xpt_release_ccb(done_ccb);
51238b8a9b1dSJustin T. Gibbs 					xpt_schedule(periph, priority);
51248b8a9b1dSJustin T. Gibbs 					return;
51258b8a9b1dSJustin T. Gibbs 				}
51268b8a9b1dSJustin T. Gibbs 				default:
51278b8a9b1dSJustin T. Gibbs 					break;
51288b8a9b1dSJustin T. Gibbs 				}
51298b8a9b1dSJustin T. Gibbs 			}
51308b8a9b1dSJustin T. Gibbs 		} else if (cam_periph_error(done_ccb, 0,
51318b8a9b1dSJustin T. Gibbs 					    done_ccb->ccb_h.target_lun > 0
51328b8a9b1dSJustin T. Gibbs 					    ? SF_RETRY_UA|SF_QUIET_IR
51338b8a9b1dSJustin T. Gibbs 					    : SF_RETRY_UA,
51348b8a9b1dSJustin T. Gibbs 					    &softc->saved_ccb) == ERESTART) {
51358b8a9b1dSJustin T. Gibbs 			return;
51368b8a9b1dSJustin T. Gibbs 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
51378b8a9b1dSJustin T. Gibbs 			/* Don't wedge the queue */
51388b8a9b1dSJustin T. Gibbs 			xpt_release_devq(done_ccb->ccb_h.path->device,
51398b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
51408b8a9b1dSJustin T. Gibbs 		}
51418b8a9b1dSJustin T. Gibbs 		/*
51428b8a9b1dSJustin T. Gibbs 		 * If we get to this point, we got an error status back
51438b8a9b1dSJustin T. Gibbs 		 * from the inquiry and the error status doesn't require
51448b8a9b1dSJustin T. Gibbs 		 * automatically retrying the command.  Therefore, the
51458b8a9b1dSJustin T. Gibbs 		 * inquiry failed.  If we had inquiry information before
51468b8a9b1dSJustin T. Gibbs 		 * for this device, but this latest inquiry command failed,
51478b8a9b1dSJustin T. Gibbs 		 * the device has probably gone away.  If this device isn't
51488b8a9b1dSJustin T. Gibbs 		 * already marked unconfigured, notify the peripheral
51498b8a9b1dSJustin T. Gibbs 		 * drivers that this device is no more.
51508b8a9b1dSJustin T. Gibbs 		 */
51518b8a9b1dSJustin T. Gibbs 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
51528b8a9b1dSJustin T. Gibbs 			/* Send the async notification. */
51538b8a9b1dSJustin T. Gibbs 			xpt_async(AC_LOST_DEVICE, path, NULL);
51548b8a9b1dSJustin T. Gibbs 
51558b8a9b1dSJustin T. Gibbs 		xpt_release_ccb(done_ccb);
51568b8a9b1dSJustin T. Gibbs 		break;
51578b8a9b1dSJustin T. Gibbs 	}
51588b8a9b1dSJustin T. Gibbs 	case PROBE_MODE_SENSE:
51598b8a9b1dSJustin T. Gibbs 	{
51608b8a9b1dSJustin T. Gibbs 		struct ccb_scsiio *csio;
51618b8a9b1dSJustin T. Gibbs 		struct scsi_mode_header_6 *mode_hdr;
51628b8a9b1dSJustin T. Gibbs 
51638b8a9b1dSJustin T. Gibbs 		csio = &done_ccb->csio;
51648b8a9b1dSJustin T. Gibbs 		mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
51658b8a9b1dSJustin T. Gibbs 		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
51668b8a9b1dSJustin T. Gibbs 			struct scsi_control_page *page;
51678b8a9b1dSJustin T. Gibbs 			u_int8_t *offset;
51688b8a9b1dSJustin T. Gibbs 
51698b8a9b1dSJustin T. Gibbs 			offset = ((u_int8_t *)&mode_hdr[1])
51708b8a9b1dSJustin T. Gibbs 			    + mode_hdr->blk_desc_len;
51718b8a9b1dSJustin T. Gibbs 			page = (struct scsi_control_page *)offset;
51728b8a9b1dSJustin T. Gibbs 			path->device->queue_flags = page->queue_flags;
51738b8a9b1dSJustin T. Gibbs 		} else if (cam_periph_error(done_ccb, 0,
51748b8a9b1dSJustin T. Gibbs 					    SF_RETRY_UA|SF_NO_PRINT,
51758b8a9b1dSJustin T. Gibbs 					    &softc->saved_ccb) == ERESTART) {
51768b8a9b1dSJustin T. Gibbs 			return;
51778b8a9b1dSJustin T. Gibbs 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
51788b8a9b1dSJustin T. Gibbs 			/* Don't wedge the queue */
51798b8a9b1dSJustin T. Gibbs 			xpt_release_devq(done_ccb->ccb_h.path->device,
51808b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
51818b8a9b1dSJustin T. Gibbs 		}
51828b8a9b1dSJustin T. Gibbs 		xpt_release_ccb(done_ccb);
51838b8a9b1dSJustin T. Gibbs 		free(mode_hdr, M_TEMP);
51848b8a9b1dSJustin T. Gibbs 		softc->action = PROBE_SERIAL_NUM;
51858b8a9b1dSJustin T. Gibbs 		xpt_schedule(periph, priority);
51868b8a9b1dSJustin T. Gibbs 		return;
51878b8a9b1dSJustin T. Gibbs 	}
51888b8a9b1dSJustin T. Gibbs 	case PROBE_SERIAL_NUM:
51898b8a9b1dSJustin T. Gibbs 	{
51908b8a9b1dSJustin T. Gibbs 		struct ccb_scsiio *csio;
51918b8a9b1dSJustin T. Gibbs 		struct scsi_vpd_unit_serial_number *serial_buf;
51928b8a9b1dSJustin T. Gibbs 		u_int32_t  priority;
51938b8a9b1dSJustin T. Gibbs 		int changed;
51948b8a9b1dSJustin T. Gibbs 		int have_serialnum;
51958b8a9b1dSJustin T. Gibbs 
51968b8a9b1dSJustin T. Gibbs 		changed = 1;
51978b8a9b1dSJustin T. Gibbs 		have_serialnum = 0;
51988b8a9b1dSJustin T. Gibbs 		csio = &done_ccb->csio;
51998b8a9b1dSJustin T. Gibbs 		priority = done_ccb->ccb_h.pinfo.priority;
52008b8a9b1dSJustin T. Gibbs 		serial_buf =
52018b8a9b1dSJustin T. Gibbs 		    (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
52028b8a9b1dSJustin T. Gibbs 
52038b8a9b1dSJustin T. Gibbs 		/* Clean up from previous instance of this device */
52048b8a9b1dSJustin T. Gibbs 		if (path->device->serial_num != NULL) {
52058b8a9b1dSJustin T. Gibbs 			free(path->device->serial_num, M_DEVBUF);
52068b8a9b1dSJustin T. Gibbs 			path->device->serial_num = NULL;
52078b8a9b1dSJustin T. Gibbs 			path->device->serial_num_len = 0;
52088b8a9b1dSJustin T. Gibbs 		}
52098b8a9b1dSJustin T. Gibbs 
52108b8a9b1dSJustin T. Gibbs 		if (serial_buf == NULL) {
52118b8a9b1dSJustin T. Gibbs 			/*
52128b8a9b1dSJustin T. Gibbs 			 * Don't process the command as it was never sent
52138b8a9b1dSJustin T. Gibbs 			 */
52148b8a9b1dSJustin T. Gibbs 		} else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
52158b8a9b1dSJustin T. Gibbs 		      && (serial_buf->length > 0)) {
52168b8a9b1dSJustin T. Gibbs 
52178b8a9b1dSJustin T. Gibbs 			have_serialnum = 1;
52188b8a9b1dSJustin T. Gibbs 			path->device->serial_num =
52198b8a9b1dSJustin T. Gibbs 				(u_int8_t *)malloc((serial_buf->length + 1),
52208b8a9b1dSJustin T. Gibbs 						   M_DEVBUF, M_NOWAIT);
52218b8a9b1dSJustin T. Gibbs 			if (path->device->serial_num != NULL) {
52228b8a9b1dSJustin T. Gibbs 				bcopy(serial_buf->serial_num,
52238b8a9b1dSJustin T. Gibbs 				      path->device->serial_num,
52248b8a9b1dSJustin T. Gibbs 				      serial_buf->length);
52258b8a9b1dSJustin T. Gibbs 				path->device->serial_num_len =
52268b8a9b1dSJustin T. Gibbs 				    serial_buf->length;
52278b8a9b1dSJustin T. Gibbs 				path->device->serial_num[serial_buf->length]
52288b8a9b1dSJustin T. Gibbs 				    = '\0';
52298b8a9b1dSJustin T. Gibbs 			}
52308b8a9b1dSJustin T. Gibbs 		} else if (cam_periph_error(done_ccb, 0,
52318b8a9b1dSJustin T. Gibbs 					    SF_RETRY_UA|SF_NO_PRINT,
52328b8a9b1dSJustin T. Gibbs 					    &softc->saved_ccb) == ERESTART) {
52338b8a9b1dSJustin T. Gibbs 			return;
52348b8a9b1dSJustin T. Gibbs 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
52358b8a9b1dSJustin T. Gibbs 			/* Don't wedge the queue */
52368b8a9b1dSJustin T. Gibbs 			xpt_release_devq(done_ccb->ccb_h.path->device,
52378b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
52388b8a9b1dSJustin T. Gibbs 		}
52398b8a9b1dSJustin T. Gibbs 
52408b8a9b1dSJustin T. Gibbs 		/*
52418b8a9b1dSJustin T. Gibbs 		 * Let's see if we have seen this device before.
52428b8a9b1dSJustin T. Gibbs 		 */
52438b8a9b1dSJustin T. Gibbs 		if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
52448b8a9b1dSJustin T. Gibbs 			MD5_CTX context;
52458b8a9b1dSJustin T. Gibbs 			u_int8_t digest[16];
52468b8a9b1dSJustin T. Gibbs 
52478b8a9b1dSJustin T. Gibbs 			MD5Init(&context);
52488b8a9b1dSJustin T. Gibbs 
52498b8a9b1dSJustin T. Gibbs 			MD5Update(&context,
52508b8a9b1dSJustin T. Gibbs 				  (unsigned char *)&path->device->inq_data,
52518b8a9b1dSJustin T. Gibbs 				  sizeof(struct scsi_inquiry_data));
52528b8a9b1dSJustin T. Gibbs 
52538b8a9b1dSJustin T. Gibbs 			if (have_serialnum)
52548b8a9b1dSJustin T. Gibbs 				MD5Update(&context, serial_buf->serial_num,
52558b8a9b1dSJustin T. Gibbs 					  serial_buf->length);
52568b8a9b1dSJustin T. Gibbs 
52578b8a9b1dSJustin T. Gibbs 			MD5Final(digest, &context);
52588b8a9b1dSJustin T. Gibbs 			if (bcmp(softc->digest, digest, 16) == 0)
52598b8a9b1dSJustin T. Gibbs 				changed = 0;
52608b8a9b1dSJustin T. Gibbs 
52618b8a9b1dSJustin T. Gibbs 			/*
52628b8a9b1dSJustin T. Gibbs 			 * XXX Do we need to do a TUR in order to ensure
52638b8a9b1dSJustin T. Gibbs 			 *     that the device really hasn't changed???
52648b8a9b1dSJustin T. Gibbs 			 */
52658b8a9b1dSJustin T. Gibbs 			if ((changed != 0)
52668b8a9b1dSJustin T. Gibbs 			 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
52678b8a9b1dSJustin T. Gibbs 				xpt_async(AC_LOST_DEVICE, path, NULL);
52688b8a9b1dSJustin T. Gibbs 		}
52698b8a9b1dSJustin T. Gibbs 		if (serial_buf != NULL)
52708b8a9b1dSJustin T. Gibbs 			free(serial_buf, M_TEMP);
52718b8a9b1dSJustin T. Gibbs 
52728b8a9b1dSJustin T. Gibbs 		if (changed != 0) {
52738b8a9b1dSJustin T. Gibbs 			/*
52748b8a9b1dSJustin T. Gibbs 			 * Now that we have all the necessary
52758b8a9b1dSJustin T. Gibbs 			 * information to safely perform transfer
52768b8a9b1dSJustin T. Gibbs 			 * negotiations... Controllers don't perform
52778b8a9b1dSJustin T. Gibbs 			 * any negotiation or tagged queuing until
52788b8a9b1dSJustin T. Gibbs 			 * after the first XPT_SET_TRAN_SETTINGS ccb is
52798b8a9b1dSJustin T. Gibbs 			 * received.  So, on a new device, just retreive
52808b8a9b1dSJustin T. Gibbs 			 * the user settings, and set them as the current
52818b8a9b1dSJustin T. Gibbs 			 * settings to set the device up.
52828b8a9b1dSJustin T. Gibbs 			 */
52838b8a9b1dSJustin T. Gibbs 			done_ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
52848b8a9b1dSJustin T. Gibbs 			done_ccb->cts.flags = CCB_TRANS_USER_SETTINGS;
52858b8a9b1dSJustin T. Gibbs 			xpt_action(done_ccb);
52868b8a9b1dSJustin T. Gibbs 			done_ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
52878b8a9b1dSJustin T. Gibbs 			done_ccb->cts.flags &= ~CCB_TRANS_USER_SETTINGS;
52888b8a9b1dSJustin T. Gibbs 			done_ccb->cts.flags |= CCB_TRANS_CURRENT_SETTINGS;
52898b8a9b1dSJustin T. Gibbs 			xpt_action(done_ccb);
52908b8a9b1dSJustin T. Gibbs 			xpt_release_ccb(done_ccb);
52918b8a9b1dSJustin T. Gibbs 
52928b8a9b1dSJustin T. Gibbs 			/*
52938b8a9b1dSJustin T. Gibbs 			 * Perform a TUR to allow the controller to
52948b8a9b1dSJustin T. Gibbs 			 * perform any necessary transfer negotiation.
52958b8a9b1dSJustin T. Gibbs 			 */
52968b8a9b1dSJustin T. Gibbs 			softc->action = PROBE_TUR_FOR_NEGOTIATION;
52978b8a9b1dSJustin T. Gibbs 			xpt_schedule(periph, priority);
52988b8a9b1dSJustin T. Gibbs 			return;
52998b8a9b1dSJustin T. Gibbs 		}
53008b8a9b1dSJustin T. Gibbs 		xpt_release_ccb(done_ccb);
53018b8a9b1dSJustin T. Gibbs 		break;
53028b8a9b1dSJustin T. Gibbs 	}
53038b8a9b1dSJustin T. Gibbs 	case PROBE_TUR_FOR_NEGOTIATION:
53048b8a9b1dSJustin T. Gibbs 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
53058b8a9b1dSJustin T. Gibbs 			/* Don't wedge the queue */
53068b8a9b1dSJustin T. Gibbs 			xpt_release_devq(done_ccb->ccb_h.path->device,
53078b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
53088b8a9b1dSJustin T. Gibbs 		}
53098b8a9b1dSJustin T. Gibbs 
53108b8a9b1dSJustin T. Gibbs 		path->device->flags &= ~CAM_DEV_UNCONFIGURED;
53118b8a9b1dSJustin T. Gibbs 
53128b8a9b1dSJustin T. Gibbs 		if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
53138b8a9b1dSJustin T. Gibbs 			/* Inform the XPT that a new device has been found */
53148b8a9b1dSJustin T. Gibbs 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
53158b8a9b1dSJustin T. Gibbs 			xpt_action(done_ccb);
53168b8a9b1dSJustin T. Gibbs 
53178b8a9b1dSJustin T. Gibbs 			xpt_async(AC_FOUND_DEVICE, xpt_periph->path, done_ccb);
53188b8a9b1dSJustin T. Gibbs 		}
53198b8a9b1dSJustin T. Gibbs 		xpt_release_ccb(done_ccb);
53208b8a9b1dSJustin T. Gibbs 		break;
53218b8a9b1dSJustin T. Gibbs 	}
53228b8a9b1dSJustin T. Gibbs 	done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
53238b8a9b1dSJustin T. Gibbs 	TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
53248b8a9b1dSJustin T. Gibbs 	done_ccb->ccb_h.status = CAM_REQ_CMP;
53258b8a9b1dSJustin T. Gibbs 	xpt_done(done_ccb);
53268b8a9b1dSJustin T. Gibbs 	if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
53278b8a9b1dSJustin T. Gibbs 		cam_periph_invalidate(periph);
53288b8a9b1dSJustin T. Gibbs 		cam_periph_release(periph);
53298b8a9b1dSJustin T. Gibbs 	} else {
53308b8a9b1dSJustin T. Gibbs 		probeschedule(periph);
53318b8a9b1dSJustin T. Gibbs 	}
53328b8a9b1dSJustin T. Gibbs }
53338b8a9b1dSJustin T. Gibbs 
53348b8a9b1dSJustin T. Gibbs static void
53358b8a9b1dSJustin T. Gibbs probecleanup(struct cam_periph *periph)
53368b8a9b1dSJustin T. Gibbs {
53378b8a9b1dSJustin T. Gibbs 	free(periph->softc, M_TEMP);
53388b8a9b1dSJustin T. Gibbs }
53398b8a9b1dSJustin T. Gibbs 
53408b8a9b1dSJustin T. Gibbs static void
53418b8a9b1dSJustin T. Gibbs xpt_find_quirk(struct cam_ed *device)
53428b8a9b1dSJustin T. Gibbs {
53438b8a9b1dSJustin T. Gibbs 	caddr_t	match;
53448b8a9b1dSJustin T. Gibbs 
53458b8a9b1dSJustin T. Gibbs 	match = cam_quirkmatch((caddr_t)&device->inq_data,
53468b8a9b1dSJustin T. Gibbs 			       (caddr_t)xpt_quirk_table,
53478b8a9b1dSJustin T. Gibbs 			       sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
53488b8a9b1dSJustin T. Gibbs 			       sizeof(*xpt_quirk_table), scsi_inquiry_match);
53498b8a9b1dSJustin T. Gibbs 
53508b8a9b1dSJustin T. Gibbs 	if (match == NULL)
53518b8a9b1dSJustin T. Gibbs 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
53528b8a9b1dSJustin T. Gibbs 
53538b8a9b1dSJustin T. Gibbs 	device->quirk = (struct xpt_quirk_entry *)match;
53548b8a9b1dSJustin T. Gibbs }
53558b8a9b1dSJustin T. Gibbs 
53568b8a9b1dSJustin T. Gibbs static void
535703e3511bSJustin T. Gibbs xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
535803e3511bSJustin T. Gibbs 			  int async_update)
53598b8a9b1dSJustin T. Gibbs {
53608b8a9b1dSJustin T. Gibbs 	struct	cam_sim *sim;
53618b8a9b1dSJustin T. Gibbs 	int	qfrozen;
53628b8a9b1dSJustin T. Gibbs 
53638b8a9b1dSJustin T. Gibbs 	sim = cts->ccb_h.path->bus->sim;
53648b8a9b1dSJustin T. Gibbs 	if (async_update == FALSE) {
53658b8a9b1dSJustin T. Gibbs 		struct	scsi_inquiry_data *inq_data;
53668b8a9b1dSJustin T. Gibbs 		struct	ccb_pathinq cpi;
53678b8a9b1dSJustin T. Gibbs 
536803e3511bSJustin T. Gibbs 		if (device == NULL) {
536903e3511bSJustin T. Gibbs 			cts->ccb_h.status = CAM_PATH_INVALID;
537003e3511bSJustin T. Gibbs 			xpt_done((union ccb *)cts);
537103e3511bSJustin T. Gibbs 			return;
537203e3511bSJustin T. Gibbs 		}
537303e3511bSJustin T. Gibbs 
53748b8a9b1dSJustin T. Gibbs 		/*
53758b8a9b1dSJustin T. Gibbs 		 * Perform sanity checking against what the
53768b8a9b1dSJustin T. Gibbs 		 * controller and device can do.
53778b8a9b1dSJustin T. Gibbs 		 */
53788b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
53798b8a9b1dSJustin T. Gibbs 		cpi.ccb_h.func_code = XPT_PATH_INQ;
53808b8a9b1dSJustin T. Gibbs 		xpt_action((union ccb *)&cpi);
53818b8a9b1dSJustin T. Gibbs 
53828b8a9b1dSJustin T. Gibbs 		inq_data = &device->inq_data;
53838b8a9b1dSJustin T. Gibbs 		if ((inq_data->flags & SID_Sync) == 0
53848b8a9b1dSJustin T. Gibbs 		 || (cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
53858b8a9b1dSJustin T. Gibbs 			/* Force async */
53868b8a9b1dSJustin T. Gibbs 			cts->sync_period = 0;
53878b8a9b1dSJustin T. Gibbs 			cts->sync_offset = 0;
53888b8a9b1dSJustin T. Gibbs 		}
53898b8a9b1dSJustin T. Gibbs 
53908b8a9b1dSJustin T. Gibbs 		switch (cts->bus_width) {
53918b8a9b1dSJustin T. Gibbs 		case MSG_EXT_WDTR_BUS_32_BIT:
53928b8a9b1dSJustin T. Gibbs 			if ((inq_data->flags & SID_WBus32) != 0
53938b8a9b1dSJustin T. Gibbs 			 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
53948b8a9b1dSJustin T. Gibbs 				break;
53958b8a9b1dSJustin T. Gibbs 			/* Fall Through to 16-bit */
53968b8a9b1dSJustin T. Gibbs 		case MSG_EXT_WDTR_BUS_16_BIT:
53978b8a9b1dSJustin T. Gibbs 			if ((inq_data->flags & SID_WBus16) != 0
53988b8a9b1dSJustin T. Gibbs 			 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
53998b8a9b1dSJustin T. Gibbs 				cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
54008b8a9b1dSJustin T. Gibbs 				break;
54018b8a9b1dSJustin T. Gibbs 			}
54028b8a9b1dSJustin T. Gibbs 			/* Fall Through to 8-bit */
54038b8a9b1dSJustin T. Gibbs 		default: /* New bus width?? */
54048b8a9b1dSJustin T. Gibbs 		case MSG_EXT_WDTR_BUS_8_BIT:
54058b8a9b1dSJustin T. Gibbs 			/* All targets can do this */
54068b8a9b1dSJustin T. Gibbs 			cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
54078b8a9b1dSJustin T. Gibbs 			break;
54088b8a9b1dSJustin T. Gibbs 		}
54098b8a9b1dSJustin T. Gibbs 
54108b8a9b1dSJustin T. Gibbs 		if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) {
54118b8a9b1dSJustin T. Gibbs 			/*
54128b8a9b1dSJustin T. Gibbs 			 * Can't tag queue without disconnection.
54138b8a9b1dSJustin T. Gibbs 			 */
54148b8a9b1dSJustin T. Gibbs 			cts->flags &= ~CCB_TRANS_TAG_ENB;
54152ba01e6fSJustin T. Gibbs 			cts->valid |= CCB_TRANS_TQ_VALID;
54168b8a9b1dSJustin T. Gibbs 		}
54178b8a9b1dSJustin T. Gibbs 
54188b8a9b1dSJustin T. Gibbs 		if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
54198b8a9b1dSJustin T. Gibbs 		 || (inq_data->flags & SID_CmdQue) == 0
54208b8a9b1dSJustin T. Gibbs 		 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
54218b8a9b1dSJustin T. Gibbs 		 || (device->quirk->mintags == 0)) {
54228b8a9b1dSJustin T. Gibbs 			/*
54238b8a9b1dSJustin T. Gibbs 			 * Can't tag on hardware that doesn't support,
54248b8a9b1dSJustin T. Gibbs 			 * doesn't have it enabled, or has broken tag support.
54258b8a9b1dSJustin T. Gibbs 			 */
54268b8a9b1dSJustin T. Gibbs 			cts->flags &= ~CCB_TRANS_TAG_ENB;
54278b8a9b1dSJustin T. Gibbs 		}
54288b8a9b1dSJustin T. Gibbs 	}
54298b8a9b1dSJustin T. Gibbs 
543003e3511bSJustin T. Gibbs 	qfrozen = FALSE;
543103e3511bSJustin T. Gibbs 	if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
543203e3511bSJustin T. Gibbs 		int device_tagenb;
543303e3511bSJustin T. Gibbs 
54348b8a9b1dSJustin T. Gibbs 		/*
54358b8a9b1dSJustin T. Gibbs 		 * If we are transitioning from tags to no-tags or
54368b8a9b1dSJustin T. Gibbs 		 * vice-versa, we need to carefully freeze and restart
54378b8a9b1dSJustin T. Gibbs 		 * the queue so that we don't overlap tagged and non-tagged
5438f0adc790SJustin T. Gibbs 		 * commands.  We also temporarily stop tags if there is
5439f0adc790SJustin T. Gibbs 		 * a change in transfer negotiation settings to allow
5440f0adc790SJustin T. Gibbs 		 * "tag-less" negotiation.
54418b8a9b1dSJustin T. Gibbs 		 */
5442f0adc790SJustin T. Gibbs 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5443f0adc790SJustin T. Gibbs 		 || (device->inq_flags & SID_CmdQue) != 0)
5444f0adc790SJustin T. Gibbs 			device_tagenb = TRUE;
5445f0adc790SJustin T. Gibbs 		else
5446f0adc790SJustin T. Gibbs 			device_tagenb = FALSE;
5447f0adc790SJustin T. Gibbs 
544803e3511bSJustin T. Gibbs 		if (((cts->flags & CCB_TRANS_TAG_ENB) != 0
5449f0adc790SJustin T. Gibbs 		  && device_tagenb == FALSE)
54508b8a9b1dSJustin T. Gibbs 		 || ((cts->flags & CCB_TRANS_TAG_ENB) == 0
545103e3511bSJustin T. Gibbs 		  && device_tagenb == TRUE)) {
54528b8a9b1dSJustin T. Gibbs 
54538b8a9b1dSJustin T. Gibbs 			if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
5454fd21cc5eSJustin T. Gibbs 				/*
5455fd21cc5eSJustin T. Gibbs 				 * Delay change to use tags until after a
5456fd21cc5eSJustin T. Gibbs 				 * few commands have gone to this device so
5457fd21cc5eSJustin T. Gibbs 				 * the controller has time to perform transfer
5458fd21cc5eSJustin T. Gibbs 				 * negotiations without tagged messages getting
5459fd21cc5eSJustin T. Gibbs 				 * in the way.
5460fd21cc5eSJustin T. Gibbs 				 */
5461fd21cc5eSJustin T. Gibbs 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
5462fd21cc5eSJustin T. Gibbs 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
54638b8a9b1dSJustin T. Gibbs 			} else {
5464fd21cc5eSJustin T. Gibbs 				xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
5465fd21cc5eSJustin T. Gibbs 				qfrozen = TRUE;
54668b8a9b1dSJustin T. Gibbs 		  		device->inq_flags &= ~SID_CmdQue;
5467fd21cc5eSJustin T. Gibbs 				xpt_dev_ccbq_resize(cts->ccb_h.path,
5468fd21cc5eSJustin T. Gibbs 						    sim->max_dev_openings);
5469fd21cc5eSJustin T. Gibbs 				device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5470fd21cc5eSJustin T. Gibbs 				device->tag_delay_count = 0;
54718b8a9b1dSJustin T. Gibbs 			}
5472f0adc790SJustin T. Gibbs 		} else if ((cts->flags & (CCB_TRANS_SYNC_RATE_VALID|
5473f0adc790SJustin T. Gibbs 					  CCB_TRANS_SYNC_OFFSET_VALID|
5474f0adc790SJustin T. Gibbs 					  CCB_TRANS_BUS_WIDTH_VALID)) != 0) {
5475f0adc790SJustin T. Gibbs 			xpt_toggle_tags(cts->ccb_h.path);
54768b8a9b1dSJustin T. Gibbs 		}
547703e3511bSJustin T. Gibbs 	}
54788b8a9b1dSJustin T. Gibbs 
54798b8a9b1dSJustin T. Gibbs 	if (async_update == FALSE)
54808b8a9b1dSJustin T. Gibbs 		(*(sim->sim_action))(sim, (union ccb *)cts);
54818b8a9b1dSJustin T. Gibbs 
54828b8a9b1dSJustin T. Gibbs 	if (qfrozen) {
54838b8a9b1dSJustin T. Gibbs 		struct ccb_relsim crs;
54848b8a9b1dSJustin T. Gibbs 
54858b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
54868b8a9b1dSJustin T. Gibbs 			      /*priority*/1);
54878b8a9b1dSJustin T. Gibbs 		crs.ccb_h.func_code = XPT_REL_SIMQ;
54888b8a9b1dSJustin T. Gibbs 		crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
54898b8a9b1dSJustin T. Gibbs 		crs.openings
54908b8a9b1dSJustin T. Gibbs 		    = crs.release_timeout
54918b8a9b1dSJustin T. Gibbs 		    = crs.qfrozen_cnt
54928b8a9b1dSJustin T. Gibbs 		    = 0;
54938b8a9b1dSJustin T. Gibbs 		xpt_action((union ccb *)&crs);
54948b8a9b1dSJustin T. Gibbs 	}
54958b8a9b1dSJustin T. Gibbs }
54968b8a9b1dSJustin T. Gibbs 
5497fd21cc5eSJustin T. Gibbs static void
5498f0adc790SJustin T. Gibbs xpt_toggle_tags(struct cam_path *path)
5499f0adc790SJustin T. Gibbs {
5500f0adc790SJustin T. Gibbs 	/*
5501f0adc790SJustin T. Gibbs 	 * Give controllers a chance to renegotiate
5502f0adc790SJustin T. Gibbs 	 * before starting tag operations.  We
5503f0adc790SJustin T. Gibbs 	 * "toggle" tagged queuing off then on
5504f0adc790SJustin T. Gibbs 	 * which causes the tag enable command delay
5505f0adc790SJustin T. Gibbs 	 * counter to come into effect.
5506f0adc790SJustin T. Gibbs 	 */
5507f0adc790SJustin T. Gibbs 	if ((path->device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5508f0adc790SJustin T. Gibbs 	 || (path->device->inq_flags & SID_CmdQue) != 0) {
5509f0adc790SJustin T. Gibbs 		struct ccb_trans_settings cts;
5510f0adc790SJustin T. Gibbs 
5511f0adc790SJustin T. Gibbs 		xpt_setup_ccb(&cts.ccb_h, path, 1);
5512f0adc790SJustin T. Gibbs 		cts.flags = 0;
5513f0adc790SJustin T. Gibbs 		cts.valid = CCB_TRANS_TQ_VALID;
551403e3511bSJustin T. Gibbs 		xpt_set_transfer_settings(&cts, path->device,
551503e3511bSJustin T. Gibbs 					  /*async_update*/TRUE);
5516f0adc790SJustin T. Gibbs 		cts.flags = CCB_TRANS_TAG_ENB;
551703e3511bSJustin T. Gibbs 		xpt_set_transfer_settings(&cts, path->device,
551803e3511bSJustin T. Gibbs 					  /*async_update*/TRUE);
5519f0adc790SJustin T. Gibbs 	}
5520f0adc790SJustin T. Gibbs }
5521f0adc790SJustin T. Gibbs 
5522f0adc790SJustin T. Gibbs static void
5523f0adc790SJustin T. Gibbs xpt_start_tags(struct cam_path *path)
5524f0adc790SJustin T. Gibbs {
5525fd21cc5eSJustin T. Gibbs 	struct ccb_relsim crs;
5526fd21cc5eSJustin T. Gibbs 	struct cam_ed *device;
5527fd21cc5eSJustin T. Gibbs 	struct cam_sim *sim;
5528fd21cc5eSJustin T. Gibbs 	int    newopenings;
5529fd21cc5eSJustin T. Gibbs 
5530fd21cc5eSJustin T. Gibbs 	device = path->device;
5531fd21cc5eSJustin T. Gibbs 	sim = path->bus->sim;
5532fd21cc5eSJustin T. Gibbs 	device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5533fd21cc5eSJustin T. Gibbs 	xpt_freeze_devq(path, /*count*/1);
5534fd21cc5eSJustin T. Gibbs 	device->inq_flags |= SID_CmdQue;
5535fd21cc5eSJustin T. Gibbs 	newopenings = min(device->quirk->maxtags, sim->max_tagged_dev_openings);
5536f0adc790SJustin T. Gibbs 	xpt_dev_ccbq_resize(path, newopenings);
5537fd21cc5eSJustin T. Gibbs 	xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
5538fd21cc5eSJustin T. Gibbs 	crs.ccb_h.func_code = XPT_REL_SIMQ;
5539fd21cc5eSJustin T. Gibbs 	crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5540fd21cc5eSJustin T. Gibbs 	crs.openings
5541fd21cc5eSJustin T. Gibbs 	    = crs.release_timeout
5542fd21cc5eSJustin T. Gibbs 	    = crs.qfrozen_cnt
5543fd21cc5eSJustin T. Gibbs 	    = 0;
5544fd21cc5eSJustin T. Gibbs 	xpt_action((union ccb *)&crs);
5545fd21cc5eSJustin T. Gibbs }
5546fd21cc5eSJustin T. Gibbs 
55478b8a9b1dSJustin T. Gibbs static int busses_to_config;
55488b8a9b1dSJustin T. Gibbs 
55498b8a9b1dSJustin T. Gibbs static int
55508b8a9b1dSJustin T. Gibbs xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
55518b8a9b1dSJustin T. Gibbs {
55528b8a9b1dSJustin T. Gibbs 	if (bus->path_id != CAM_XPT_PATH_ID)
55538b8a9b1dSJustin T. Gibbs 		busses_to_config++;
55548b8a9b1dSJustin T. Gibbs 
55558b8a9b1dSJustin T. Gibbs 	return(1);
55568b8a9b1dSJustin T. Gibbs }
55578b8a9b1dSJustin T. Gibbs 
55588b8a9b1dSJustin T. Gibbs static int
55598b8a9b1dSJustin T. Gibbs xptconfigfunc(struct cam_eb *bus, void *arg)
55608b8a9b1dSJustin T. Gibbs {
55618b8a9b1dSJustin T. Gibbs 	struct	cam_path *path;
55628b8a9b1dSJustin T. Gibbs 	union	ccb *work_ccb;
55638b8a9b1dSJustin T. Gibbs 
55648b8a9b1dSJustin T. Gibbs 	if (bus->path_id != CAM_XPT_PATH_ID) {
55658b8a9b1dSJustin T. Gibbs 		cam_status status;
55668b8a9b1dSJustin T. Gibbs 
55678b8a9b1dSJustin T. Gibbs 		work_ccb = xpt_alloc_ccb();
55688b8a9b1dSJustin T. Gibbs 		if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
55698b8a9b1dSJustin T. Gibbs 					      CAM_TARGET_WILDCARD,
55708b8a9b1dSJustin T. Gibbs 					      CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
55718b8a9b1dSJustin T. Gibbs 			printf("xptconfigfunc: xpt_create_path failed with "
55728b8a9b1dSJustin T. Gibbs 			       "status %#x for bus %d\n", status, bus->path_id);
55738b8a9b1dSJustin T. Gibbs 			printf("xptconfigfunc: halting bus configuration\n");
55748b8a9b1dSJustin T. Gibbs 			xpt_free_ccb(work_ccb);
557598192658SJustin T. Gibbs 			busses_to_config--;
557698192658SJustin T. Gibbs 			xpt_finishconfig(xpt_periph, NULL);
55778b8a9b1dSJustin T. Gibbs 			return(0);
55788b8a9b1dSJustin T. Gibbs 		}
55798b8a9b1dSJustin T. Gibbs 		xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
558098192658SJustin T. Gibbs 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
558198192658SJustin T. Gibbs 		xpt_action(work_ccb);
558298192658SJustin T. Gibbs 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
558398192658SJustin T. Gibbs 			printf("xptconfigfunc: CPI failed on bus %d "
558498192658SJustin T. Gibbs 			       "with status %d\n", bus->path_id,
558598192658SJustin T. Gibbs 			       work_ccb->ccb_h.status);
558698192658SJustin T. Gibbs 			xpt_finishconfig(xpt_periph, work_ccb);
558798192658SJustin T. Gibbs 			return(1);
558898192658SJustin T. Gibbs 		}
558998192658SJustin T. Gibbs 
559098192658SJustin T. Gibbs 		if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0) {
559198192658SJustin T. Gibbs 			xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
55928b8a9b1dSJustin T. Gibbs 			work_ccb->ccb_h.func_code = XPT_RESET_BUS;
55938b8a9b1dSJustin T. Gibbs 			work_ccb->ccb_h.cbfcnp = NULL;
55948b8a9b1dSJustin T. Gibbs 			CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
55958b8a9b1dSJustin T. Gibbs 				  ("Resetting Bus\n"));
55968b8a9b1dSJustin T. Gibbs 			xpt_action(work_ccb);
55978b8a9b1dSJustin T. Gibbs 			xpt_finishconfig(xpt_periph, work_ccb);
559898192658SJustin T. Gibbs 		} else {
559998192658SJustin T. Gibbs 			/* Act as though we performed a successful BUS RESET */
560098192658SJustin T. Gibbs 			work_ccb->ccb_h.func_code = XPT_RESET_BUS;
560198192658SJustin T. Gibbs 			xpt_finishconfig(xpt_periph, work_ccb);
560298192658SJustin T. Gibbs 		}
56038b8a9b1dSJustin T. Gibbs 	}
56048b8a9b1dSJustin T. Gibbs 
56058b8a9b1dSJustin T. Gibbs 	return(1);
56068b8a9b1dSJustin T. Gibbs }
56078b8a9b1dSJustin T. Gibbs 
56088b8a9b1dSJustin T. Gibbs static void
56098b8a9b1dSJustin T. Gibbs xpt_config(void *arg)
56108b8a9b1dSJustin T. Gibbs {
56118b8a9b1dSJustin T. Gibbs 	/* Now that interrupts are enabled, go find our devices */
56128b8a9b1dSJustin T. Gibbs 
56138b8a9b1dSJustin T. Gibbs #ifdef CAMDEBUG
56148b8a9b1dSJustin T. Gibbs 	/* Setup debugging flags and path */
56158b8a9b1dSJustin T. Gibbs #ifdef CAM_DEBUG_FLAGS
56168b8a9b1dSJustin T. Gibbs 	cam_dflags = CAM_DEBUG_FLAGS;
56178b8a9b1dSJustin T. Gibbs #else /* !CAM_DEBUG_FLAGS */
56188b8a9b1dSJustin T. Gibbs 	cam_dflags = CAM_DEBUG_NONE;
56198b8a9b1dSJustin T. Gibbs #endif /* CAM_DEBUG_FLAGS */
56208b8a9b1dSJustin T. Gibbs #ifdef CAM_DEBUG_BUS
56218b8a9b1dSJustin T. Gibbs 	if (cam_dflags != CAM_DEBUG_NONE) {
56228b8a9b1dSJustin T. Gibbs 		if (xpt_create_path(&cam_dpath, xpt_periph,
56238b8a9b1dSJustin T. Gibbs 				    CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
56248b8a9b1dSJustin T. Gibbs 				    CAM_DEBUG_LUN) != CAM_REQ_CMP) {
56258b8a9b1dSJustin T. Gibbs 			printf("xpt_config: xpt_create_path() failed for debug"
56268b8a9b1dSJustin T. Gibbs 			       " target %d:%d:%d, debugging disabled\n",
56278b8a9b1dSJustin T. Gibbs 			       CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
56288b8a9b1dSJustin T. Gibbs 			cam_dflags = CAM_DEBUG_NONE;
56298b8a9b1dSJustin T. Gibbs 		}
56308b8a9b1dSJustin T. Gibbs 	} else
56318b8a9b1dSJustin T. Gibbs 		cam_dpath = NULL;
56328b8a9b1dSJustin T. Gibbs #else /* !CAM_DEBUG_BUS */
56338b8a9b1dSJustin T. Gibbs 	cam_dpath = NULL;
56348b8a9b1dSJustin T. Gibbs #endif /* CAM_DEBUG_BUS */
56358b8a9b1dSJustin T. Gibbs #endif /* CAMDEBUG */
56368b8a9b1dSJustin T. Gibbs 
56378b8a9b1dSJustin T. Gibbs 	/* Scan all installed busses */
56388b8a9b1dSJustin T. Gibbs 	xpt_for_all_busses(xptconfigbuscountfunc, NULL);
56398b8a9b1dSJustin T. Gibbs 
5640ecdf1113SJustin T. Gibbs 	if (busses_to_config == 0) {
5641ecdf1113SJustin T. Gibbs 		/* Call manually because we don't have any busses */
56428b8a9b1dSJustin T. Gibbs 		xpt_finishconfig(xpt_periph, NULL);
56432863f7b1SJustin T. Gibbs 	} else  {
56442863f7b1SJustin T. Gibbs 		if (SCSI_DELAY >= 2000) {
56452863f7b1SJustin T. Gibbs 			printf("Waiting %d seconds for SCSI "
56462863f7b1SJustin T. Gibbs 			       "devices to settle\n", SCSI_DELAY/1000);
56472863f7b1SJustin T. Gibbs 		}
5648ecdf1113SJustin T. Gibbs 		xpt_for_all_busses(xptconfigfunc, NULL);
56498b8a9b1dSJustin T. Gibbs 	}
56502863f7b1SJustin T. Gibbs }
56518b8a9b1dSJustin T. Gibbs 
56528b8a9b1dSJustin T. Gibbs /*
56538b8a9b1dSJustin T. Gibbs  * If the given device only has one peripheral attached to it, and if that
56548b8a9b1dSJustin T. Gibbs  * peripheral is the passthrough driver, announce it.  This insures that the
56558b8a9b1dSJustin T. Gibbs  * user sees some sort of announcement for every peripheral in their system.
56568b8a9b1dSJustin T. Gibbs  */
56578b8a9b1dSJustin T. Gibbs static int
56588b8a9b1dSJustin T. Gibbs xptpassannouncefunc(struct cam_ed *device, void *arg)
56598b8a9b1dSJustin T. Gibbs {
56608b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph;
56618b8a9b1dSJustin T. Gibbs 	int i;
56628b8a9b1dSJustin T. Gibbs 
56638b8a9b1dSJustin T. Gibbs 	for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
56648b8a9b1dSJustin T. Gibbs 	     periph = SLIST_NEXT(periph, periph_links), i++);
56658b8a9b1dSJustin T. Gibbs 
56668b8a9b1dSJustin T. Gibbs 	periph = SLIST_FIRST(&device->periphs);
56678b8a9b1dSJustin T. Gibbs 	if ((i == 1)
56688b8a9b1dSJustin T. Gibbs 	 && (strncmp(periph->periph_name, "pass", 4) == 0))
56698b8a9b1dSJustin T. Gibbs 		xpt_announce_periph(periph, NULL);
56708b8a9b1dSJustin T. Gibbs 
56718b8a9b1dSJustin T. Gibbs 	return(1);
56728b8a9b1dSJustin T. Gibbs }
56738b8a9b1dSJustin T. Gibbs 
56748b8a9b1dSJustin T. Gibbs static void
56758b8a9b1dSJustin T. Gibbs xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
56768b8a9b1dSJustin T. Gibbs {
56778b8a9b1dSJustin T. Gibbs 	struct	periph_driver **p_drv;
56788b8a9b1dSJustin T. Gibbs 	int	i;
56798b8a9b1dSJustin T. Gibbs 
56808b8a9b1dSJustin T. Gibbs 	if (done_ccb != NULL) {
56818b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
56828b8a9b1dSJustin T. Gibbs 			  ("xpt_finishconfig\n"));
56838b8a9b1dSJustin T. Gibbs 		switch(done_ccb->ccb_h.func_code) {
56848b8a9b1dSJustin T. Gibbs 		case XPT_RESET_BUS:
56858b8a9b1dSJustin T. Gibbs 			if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
56868b8a9b1dSJustin T. Gibbs 				done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
56878b8a9b1dSJustin T. Gibbs 				done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
56888b8a9b1dSJustin T. Gibbs 				xpt_action(done_ccb);
56898b8a9b1dSJustin T. Gibbs 				return;
56908b8a9b1dSJustin T. Gibbs 			}
56918b8a9b1dSJustin T. Gibbs 			/* FALLTHROUGH */
56928b8a9b1dSJustin T. Gibbs 		case XPT_SCAN_BUS:
569398192658SJustin T. Gibbs 		default:
56948b8a9b1dSJustin T. Gibbs 			xpt_free_path(done_ccb->ccb_h.path);
56958b8a9b1dSJustin T. Gibbs 			busses_to_config--;
56968b8a9b1dSJustin T. Gibbs 			break;
56978b8a9b1dSJustin T. Gibbs 		}
56988b8a9b1dSJustin T. Gibbs 	}
56998b8a9b1dSJustin T. Gibbs 
57008b8a9b1dSJustin T. Gibbs 	if (busses_to_config == 0) {
57018b8a9b1dSJustin T. Gibbs 		/* Register all the peripheral drivers */
57028b8a9b1dSJustin T. Gibbs 		/* XXX This will have to change when we have LKMs */
57038b8a9b1dSJustin T. Gibbs 		p_drv = (struct periph_driver **)periphdriver_set.ls_items;
57048b8a9b1dSJustin T. Gibbs 		for (i = 0; p_drv[i] != NULL; i++) {
57058b8a9b1dSJustin T. Gibbs 			(*p_drv[i]->init)();
57068b8a9b1dSJustin T. Gibbs 		}
57078b8a9b1dSJustin T. Gibbs 
57088b8a9b1dSJustin T. Gibbs 		/*
57098b8a9b1dSJustin T. Gibbs 		 * Check for devices with no "standard" peripheral driver
57108b8a9b1dSJustin T. Gibbs 		 * attached.  For any devices like that, announce the
57118b8a9b1dSJustin T. Gibbs 		 * passthrough driver so the user will see something.
57128b8a9b1dSJustin T. Gibbs 		 */
57138b8a9b1dSJustin T. Gibbs 		xpt_for_all_devices(xptpassannouncefunc, NULL);
57148b8a9b1dSJustin T. Gibbs 
57158b8a9b1dSJustin T. Gibbs 		/* Release our hook so that the boot can continue. */
57168b8a9b1dSJustin T. Gibbs 		config_intrhook_disestablish(xpt_config_hook);
57178b8a9b1dSJustin T. Gibbs 	}
57188b8a9b1dSJustin T. Gibbs 	if (done_ccb != NULL)
57198b8a9b1dSJustin T. Gibbs 		xpt_free_ccb(done_ccb);
57208b8a9b1dSJustin T. Gibbs }
57218b8a9b1dSJustin T. Gibbs 
57228b8a9b1dSJustin T. Gibbs static void
57238b8a9b1dSJustin T. Gibbs xptaction(struct cam_sim *sim, union ccb *work_ccb)
57248b8a9b1dSJustin T. Gibbs {
57258b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
57268b8a9b1dSJustin T. Gibbs 
57278b8a9b1dSJustin T. Gibbs 	switch (work_ccb->ccb_h.func_code) {
57288b8a9b1dSJustin T. Gibbs 	/* Common cases first */
57298b8a9b1dSJustin T. Gibbs 	case XPT_PATH_INQ:		/* Path routing inquiry */
57308b8a9b1dSJustin T. Gibbs 	{
57318b8a9b1dSJustin T. Gibbs 		struct ccb_pathinq *cpi;
57328b8a9b1dSJustin T. Gibbs 
57338b8a9b1dSJustin T. Gibbs 		cpi = &work_ccb->cpi;
57348b8a9b1dSJustin T. Gibbs 		cpi->version_num = 1; /* XXX??? */
57358b8a9b1dSJustin T. Gibbs 		cpi->hba_inquiry = 0;
57368b8a9b1dSJustin T. Gibbs 		cpi->target_sprt = 0;
57378b8a9b1dSJustin T. Gibbs 		cpi->hba_misc = 0;
57388b8a9b1dSJustin T. Gibbs 		cpi->hba_eng_cnt = 0;
57398b8a9b1dSJustin T. Gibbs 		cpi->max_target = 0;
57408b8a9b1dSJustin T. Gibbs 		cpi->max_lun = 0;
57418b8a9b1dSJustin T. Gibbs 		cpi->initiator_id = 0;
57428b8a9b1dSJustin T. Gibbs 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
57438b8a9b1dSJustin T. Gibbs 		strncpy(cpi->hba_vid, "", HBA_IDLEN);
57448b8a9b1dSJustin T. Gibbs 		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
57458b8a9b1dSJustin T. Gibbs 		cpi->unit_number = sim->unit_number;
57468b8a9b1dSJustin T. Gibbs 		cpi->bus_id = sim->bus_id;
57478b8a9b1dSJustin T. Gibbs 		cpi->ccb_h.status = CAM_REQ_CMP;
57488b8a9b1dSJustin T. Gibbs 		xpt_done(work_ccb);
57498b8a9b1dSJustin T. Gibbs 		break;
57508b8a9b1dSJustin T. Gibbs 	}
57518b8a9b1dSJustin T. Gibbs 	default:
57528b8a9b1dSJustin T. Gibbs 		work_ccb->ccb_h.status = CAM_REQ_INVALID;
57538b8a9b1dSJustin T. Gibbs 		xpt_done(work_ccb);
57548b8a9b1dSJustin T. Gibbs 		break;
57558b8a9b1dSJustin T. Gibbs 	}
57568b8a9b1dSJustin T. Gibbs }
57578b8a9b1dSJustin T. Gibbs 
57588b8a9b1dSJustin T. Gibbs /*
57598b8a9b1dSJustin T. Gibbs  * Should only be called by the machine interrupt dispatch routines,
57608b8a9b1dSJustin T. Gibbs  * so put these prototypes here instead of in the header.
57618b8a9b1dSJustin T. Gibbs  */
57628b8a9b1dSJustin T. Gibbs 
57635d754af7SMatt Jacob static void
57645d754af7SMatt Jacob swi_camnet(void)
57658b8a9b1dSJustin T. Gibbs {
57668b8a9b1dSJustin T. Gibbs 	camisr(&cam_netq);
57678b8a9b1dSJustin T. Gibbs }
57688b8a9b1dSJustin T. Gibbs 
57695d754af7SMatt Jacob static void
57705d754af7SMatt Jacob swi_cambio(void)
57718b8a9b1dSJustin T. Gibbs {
57728b8a9b1dSJustin T. Gibbs 	camisr(&cam_bioq);
57738b8a9b1dSJustin T. Gibbs }
57748b8a9b1dSJustin T. Gibbs 
57758b8a9b1dSJustin T. Gibbs static void
57768b8a9b1dSJustin T. Gibbs camisr(cam_isrq_t *queue)
57778b8a9b1dSJustin T. Gibbs {
57788b8a9b1dSJustin T. Gibbs 	int	s;
57798b8a9b1dSJustin T. Gibbs 	struct	ccb_hdr *ccb_h;
57808b8a9b1dSJustin T. Gibbs 
57818b8a9b1dSJustin T. Gibbs 	s = splcam();
57828b8a9b1dSJustin T. Gibbs 	while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
57838b8a9b1dSJustin T. Gibbs 		int	runq;
57848b8a9b1dSJustin T. Gibbs 
57858b8a9b1dSJustin T. Gibbs 		TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
57868b8a9b1dSJustin T. Gibbs 		ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
57878b8a9b1dSJustin T. Gibbs 		splx(s);
57888b8a9b1dSJustin T. Gibbs 
57898b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
57908b8a9b1dSJustin T. Gibbs 			  ("camisr"));
57918b8a9b1dSJustin T. Gibbs 
57928b8a9b1dSJustin T. Gibbs 		runq = FALSE;
57938b8a9b1dSJustin T. Gibbs 
57948b8a9b1dSJustin T. Gibbs 		if (ccb_h->flags & CAM_HIGH_POWER) {
57958b8a9b1dSJustin T. Gibbs 			struct highpowerlist	*hphead;
57968b8a9b1dSJustin T. Gibbs 			struct cam_ed		*device;
57978b8a9b1dSJustin T. Gibbs 			union ccb		*send_ccb;
57988b8a9b1dSJustin T. Gibbs 
57998b8a9b1dSJustin T. Gibbs 			hphead = &highpowerq;
58008b8a9b1dSJustin T. Gibbs 
58018b8a9b1dSJustin T. Gibbs 			send_ccb = (union ccb *)STAILQ_FIRST(hphead);
58028b8a9b1dSJustin T. Gibbs 
58038b8a9b1dSJustin T. Gibbs 			/*
58048b8a9b1dSJustin T. Gibbs 			 * Increment the count since this command is done.
58058b8a9b1dSJustin T. Gibbs 			 */
58068b8a9b1dSJustin T. Gibbs 			num_highpower++;
58078b8a9b1dSJustin T. Gibbs 
58088b8a9b1dSJustin T. Gibbs 			/*
58098b8a9b1dSJustin T. Gibbs 			 * Any high powered commands queued up?
58108b8a9b1dSJustin T. Gibbs 			 */
58118b8a9b1dSJustin T. Gibbs 			if (send_ccb != NULL) {
58128b8a9b1dSJustin T. Gibbs 				device = send_ccb->ccb_h.path->device;
58138b8a9b1dSJustin T. Gibbs 
58148b8a9b1dSJustin T. Gibbs 				STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
58158b8a9b1dSJustin T. Gibbs 
58168b8a9b1dSJustin T. Gibbs 				xpt_release_devq(send_ccb->ccb_h.path->device,
58178b8a9b1dSJustin T. Gibbs 						 TRUE);
58188b8a9b1dSJustin T. Gibbs 			}
58198b8a9b1dSJustin T. Gibbs 		}
58208b8a9b1dSJustin T. Gibbs 		if ((ccb_h->func_code != XPT_ACCEPT_TARGET_IO)
58218b8a9b1dSJustin T. Gibbs 		 && (ccb_h->func_code != XPT_SCAN_LUN)
58228b8a9b1dSJustin T. Gibbs 		 && (ccb_h->func_code != XPT_SCAN_BUS)) {
58238b8a9b1dSJustin T. Gibbs 			struct cam_ed *dev;
58248b8a9b1dSJustin T. Gibbs 
58258b8a9b1dSJustin T. Gibbs 			dev = ccb_h->path->device;
58268b8a9b1dSJustin T. Gibbs 
58278b8a9b1dSJustin T. Gibbs 			s = splcam();
58288b8a9b1dSJustin T. Gibbs 			cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
58298b8a9b1dSJustin T. Gibbs 
58308b8a9b1dSJustin T. Gibbs 			ccb_h->path->bus->sim->devq->send_active--;
58318b8a9b1dSJustin T. Gibbs 			ccb_h->path->bus->sim->devq->send_openings++;
58328b8a9b1dSJustin T. Gibbs 			splx(s);
58338b8a9b1dSJustin T. Gibbs 
58348b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
58358b8a9b1dSJustin T. Gibbs 			 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
58368b8a9b1dSJustin T. Gibbs 			  && (dev->ccbq.dev_active == 0))) {
58378b8a9b1dSJustin T. Gibbs 
58388b8a9b1dSJustin T. Gibbs 				xpt_release_devq(ccb_h->path->device,
58398b8a9b1dSJustin T. Gibbs 						 /*run_queue*/TRUE);
58408b8a9b1dSJustin T. Gibbs 			}
58418b8a9b1dSJustin T. Gibbs 
5842fd21cc5eSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5843fd21cc5eSJustin T. Gibbs 			 && (--dev->tag_delay_count == 0))
5844fd21cc5eSJustin T. Gibbs 				xpt_start_tags(ccb_h->path);
5845fd21cc5eSJustin T. Gibbs 
58468b8a9b1dSJustin T. Gibbs 			if ((dev->ccbq.queue.entries > 0)
58478b8a9b1dSJustin T. Gibbs 			 && (dev->qfrozen_cnt == 0)
58488b8a9b1dSJustin T. Gibbs 			 && (device_is_send_queued(dev) == 0)) {
58498b8a9b1dSJustin T. Gibbs 				runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
58508b8a9b1dSJustin T. Gibbs 							      dev);
58518b8a9b1dSJustin T. Gibbs 			}
58528b8a9b1dSJustin T. Gibbs 		}
58538b8a9b1dSJustin T. Gibbs 
58548b8a9b1dSJustin T. Gibbs 		if (ccb_h->status & CAM_RELEASE_SIMQ) {
58558b8a9b1dSJustin T. Gibbs 			xpt_release_simq(ccb_h->path->bus->sim,
58568b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
58578b8a9b1dSJustin T. Gibbs 		} else if ((ccb_h->flags & CAM_DEV_QFRZDIS)
58588b8a9b1dSJustin T. Gibbs 			&& (ccb_h->status & CAM_DEV_QFRZN)) {
58598b8a9b1dSJustin T. Gibbs 			xpt_release_devq(ccb_h->path->device,
58608b8a9b1dSJustin T. Gibbs 					 /*run_queue*/TRUE);
58618b8a9b1dSJustin T. Gibbs 			ccb_h->status &= ~CAM_DEV_QFRZN;
58628b8a9b1dSJustin T. Gibbs 		} else if (runq) {
58638b8a9b1dSJustin T. Gibbs 			xpt_run_dev_sendq(ccb_h->path->bus);
58648b8a9b1dSJustin T. Gibbs 		}
58658b8a9b1dSJustin T. Gibbs 
58668b8a9b1dSJustin T. Gibbs 		/* Call the peripheral driver's callback */
58678b8a9b1dSJustin T. Gibbs 		(*ccb_h->cbfcnp)(ccb_h->path->periph,
58688b8a9b1dSJustin T. Gibbs 				 (union ccb *)ccb_h);
58698b8a9b1dSJustin T. Gibbs 
58708b8a9b1dSJustin T. Gibbs 		/* Raise IPL for while test */
58718b8a9b1dSJustin T. Gibbs 		s = splcam();
58728b8a9b1dSJustin T. Gibbs 	}
58738b8a9b1dSJustin T. Gibbs 	splx(s);
58748b8a9b1dSJustin T. Gibbs }
5875