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