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