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