xref: /freebsd/sys/cam/cam_xpt.c (revision ce4946daa5ce852d28008dac492029500ab2ee95)
1 /*
2  * Implementation of the Common Access Method Transport (XPT) layer.
3  *
4  * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5  * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification, immediately at the beginning of the file.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
37 #include <sys/time.h>
38 #include <sys/conf.h>
39 #include <sys/fcntl.h>
40 #include <sys/md5.h>
41 #include <sys/devicestat.h>
42 #include <sys/interrupt.h>
43 #include <sys/sbuf.h>
44 
45 #ifdef PC98
46 #include <pc98/pc98/pc98_machdep.h>	/* geometry translation */
47 #endif
48 
49 #include <sys/ipl.h>
50 
51 #include <cam/cam.h>
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_periph.h>
54 #include <cam/cam_sim.h>
55 #include <cam/cam_xpt.h>
56 #include <cam/cam_xpt_sim.h>
57 #include <cam/cam_xpt_periph.h>
58 #include <cam/cam_debug.h>
59 
60 #include <cam/scsi/scsi_all.h>
61 #include <cam/scsi/scsi_message.h>
62 #include <cam/scsi/scsi_pass.h>
63 #include "opt_cam.h"
64 
65 /* Datastructures internal to the xpt layer */
66 
67 /*
68  * Definition of an async handler callback block.  These are used to add
69  * SIMs and peripherals to the async callback lists.
70  */
71 struct async_node {
72 	SLIST_ENTRY(async_node)	links;
73 	u_int32_t	event_enable;	/* Async Event enables */
74 	void		(*callback)(void *arg, u_int32_t code,
75 				    struct cam_path *path, void *args);
76 	void		*callback_arg;
77 };
78 
79 SLIST_HEAD(async_list, async_node);
80 SLIST_HEAD(periph_list, cam_periph);
81 static STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
82 
83 /*
84  * This is the maximum number of high powered commands (e.g. start unit)
85  * that can be outstanding at a particular time.
86  */
87 #ifndef CAM_MAX_HIGHPOWER
88 #define CAM_MAX_HIGHPOWER  4
89 #endif
90 
91 /* number of high powered commands that can go through right now */
92 static int num_highpower = CAM_MAX_HIGHPOWER;
93 
94 /*
95  * Structure for queueing a device in a run queue.
96  * There is one run queue for allocating new ccbs,
97  * and another for sending ccbs to the controller.
98  */
99 struct cam_ed_qinfo {
100 	cam_pinfo pinfo;
101 	struct	  cam_ed *device;
102 };
103 
104 /*
105  * The CAM EDT (Existing Device Table) contains the device information for
106  * all devices for all busses in the system.  The table contains a
107  * cam_ed structure for each device on the bus.
108  */
109 struct cam_ed {
110 	TAILQ_ENTRY(cam_ed) links;
111 	struct	cam_ed_qinfo alloc_ccb_entry;
112 	struct	cam_ed_qinfo send_ccb_entry;
113 	struct	cam_et	 *target;
114 	lun_id_t	 lun_id;
115 	struct	camq drvq;		/*
116 					 * Queue of type drivers wanting to do
117 					 * work on this device.
118 					 */
119 	struct	cam_ccbq ccbq;		/* Queue of pending ccbs */
120 	struct	async_list asyncs;	/* Async callback info for this B/T/L */
121 	struct	periph_list periphs;	/* All attached devices */
122 	u_int	generation;		/* Generation number */
123 	struct	cam_periph *owner;	/* Peripheral driver's ownership tag */
124 	struct	xpt_quirk_entry *quirk;	/* Oddities about this device */
125 					/* Storage for the inquiry data */
126 #ifdef CAM_NEW_TRAN_CODE
127 	cam_proto	 protocol;
128 	u_int		 protocol_version;
129 	cam_xport	 transport;
130 	u_int		 transport_version;
131 #endif /* CAM_NEW_TRAN_CODE */
132 	struct		 scsi_inquiry_data inq_data;
133 	u_int8_t	 inq_flags;	/*
134 					 * Current settings for inquiry flags.
135 					 * This allows us to override settings
136 					 * like disconnection and tagged
137 					 * queuing for a device.
138 					 */
139 	u_int8_t	 queue_flags;	/* Queue flags from the control page */
140 	u_int8_t	 serial_num_len;
141 	u_int8_t	*serial_num;
142 	u_int32_t	 qfrozen_cnt;
143 	u_int32_t	 flags;
144 #define CAM_DEV_UNCONFIGURED	 	0x01
145 #define CAM_DEV_REL_TIMEOUT_PENDING	0x02
146 #define CAM_DEV_REL_ON_COMPLETE		0x04
147 #define CAM_DEV_REL_ON_QUEUE_EMPTY	0x08
148 #define CAM_DEV_RESIZE_QUEUE_NEEDED	0x10
149 #define CAM_DEV_TAG_AFTER_COUNT		0x20
150 #define CAM_DEV_INQUIRY_DATA_VALID	0x40
151 	u_int32_t	 tag_delay_count;
152 #define	CAM_TAG_DELAY_COUNT		5
153 	u_int32_t	 refcount;
154 	struct		 callout_handle c_handle;
155 };
156 
157 /*
158  * Each target is represented by an ET (Existing Target).  These
159  * entries are created when a target is successfully probed with an
160  * identify, and removed when a device fails to respond after a number
161  * of retries, or a bus rescan finds the device missing.
162  */
163 struct cam_et {
164 	TAILQ_HEAD(, cam_ed) ed_entries;
165 	TAILQ_ENTRY(cam_et) links;
166 	struct	cam_eb	*bus;
167 	target_id_t	target_id;
168 	u_int32_t	refcount;
169 	u_int		generation;
170 	struct		timeval last_reset;
171 };
172 
173 /*
174  * Each bus is represented by an EB (Existing Bus).  These entries
175  * are created by calls to xpt_bus_register and deleted by calls to
176  * xpt_bus_deregister.
177  */
178 struct cam_eb {
179 	TAILQ_HEAD(, cam_et) et_entries;
180 	TAILQ_ENTRY(cam_eb)  links;
181 	path_id_t	     path_id;
182 	struct cam_sim	     *sim;
183 	struct timeval	     last_reset;
184 	u_int32_t	     flags;
185 #define	CAM_EB_RUNQ_SCHEDULED	0x01
186 	u_int32_t	     refcount;
187 	u_int		     generation;
188 };
189 
190 struct cam_path {
191 	struct cam_periph *periph;
192 	struct cam_eb	  *bus;
193 	struct cam_et	  *target;
194 	struct cam_ed	  *device;
195 };
196 
197 struct xpt_quirk_entry {
198 	struct scsi_inquiry_pattern inq_pat;
199 	u_int8_t quirks;
200 #define	CAM_QUIRK_NOLUNS	0x01
201 #define	CAM_QUIRK_NOSERIAL	0x02
202 #define	CAM_QUIRK_HILUNS	0x04
203 	u_int mintags;
204 	u_int maxtags;
205 };
206 #define	CAM_SCSI2_MAXLUN	8
207 
208 typedef enum {
209 	XPT_FLAG_OPEN		= 0x01
210 } xpt_flags;
211 
212 struct xpt_softc {
213 	xpt_flags	flags;
214 	u_int32_t	generation;
215 };
216 
217 static const char quantum[] = "QUANTUM";
218 static const char sony[] = "SONY";
219 static const char west_digital[] = "WDIGTL";
220 static const char samsung[] = "SAMSUNG";
221 static const char seagate[] = "SEAGATE";
222 static const char microp[] = "MICROP";
223 
224 static struct xpt_quirk_entry xpt_quirk_table[] =
225 {
226 	{
227 		/* Reports QUEUE FULL for temporary resource shortages */
228 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
229 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
230 	},
231 	{
232 		/* Reports QUEUE FULL for temporary resource shortages */
233 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
234 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
235 	},
236 	{
237 		/* Reports QUEUE FULL for temporary resource shortages */
238 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
239 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
240 	},
241 	{
242 		/* Broken tagged queuing drive */
243 		{ T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
244 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
245 	},
246 	{
247 		/* Broken tagged queuing drive */
248 		{ T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
249 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
250 	},
251 	{
252 		/* Broken tagged queuing drive */
253 		{ T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
254 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
255 	},
256 	{
257 		/*
258 		 * Unfortunately, the Quantum Atlas III has the same
259 		 * problem as the Atlas II drives above.
260 		 * Reported by: "Johan Granlund" <johan@granlund.nu>
261 		 *
262 		 * For future reference, the drive with the problem was:
263 		 * QUANTUM QM39100TD-SW N1B0
264 		 *
265 		 * It's possible that Quantum will fix the problem in later
266 		 * firmware revisions.  If that happens, the quirk entry
267 		 * will need to be made specific to the firmware revisions
268 		 * with the problem.
269 		 *
270 		 */
271 		/* Reports QUEUE FULL for temporary resource shortages */
272 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
273 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
274 	},
275 	{
276 		/*
277 		 * 18 Gig Atlas III, same problem as the 9G version.
278 		 * Reported by: Andre Albsmeier
279 		 *		<andre.albsmeier@mchp.siemens.de>
280 		 *
281 		 * For future reference, the drive with the problem was:
282 		 * QUANTUM QM318000TD-S N491
283 		 */
284 		/* Reports QUEUE FULL for temporary resource shortages */
285 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
286 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
287 	},
288 	{
289 		/*
290 		 * Broken tagged queuing drive
291 		 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
292 		 *         and: Martin Renters <martin@tdc.on.ca>
293 		 */
294 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
295 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
296 	},
297 		/*
298 		 * The Seagate Medalist Pro drives have very poor write
299 		 * performance with anything more than 2 tags.
300 		 *
301 		 * Reported by:  Paul van der Zwan <paulz@trantor.xs4all.nl>
302 		 * Drive:  <SEAGATE ST36530N 1444>
303 		 *
304 		 * Reported by:  Jeremy Lea <reg@shale.csir.co.za>
305 		 * Drive:  <SEAGATE ST34520W 1281>
306 		 *
307 		 * No one has actually reported that the 9G version
308 		 * (ST39140*) of the Medalist Pro has the same problem, but
309 		 * we're assuming that it does because the 4G and 6.5G
310 		 * versions of the drive are broken.
311 		 */
312 	{
313 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
314 		/*quirks*/0, /*mintags*/2, /*maxtags*/2
315 	},
316 	{
317 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
318 		/*quirks*/0, /*mintags*/2, /*maxtags*/2
319 	},
320 	{
321 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
322 		/*quirks*/0, /*mintags*/2, /*maxtags*/2
323 	},
324 	{
325 		/*
326 		 * Slow when tagged queueing is enabled.  Write performance
327 		 * steadily drops off with more and more concurrent
328 		 * transactions.  Best sequential write performance with
329 		 * tagged queueing turned off and write caching turned on.
330 		 *
331 		 * PR:  kern/10398
332 		 * Submitted by:  Hideaki Okada <hokada@isl.melco.co.jp>
333 		 * Drive:  DCAS-34330 w/ "S65A" firmware.
334 		 *
335 		 * The drive with the problem had the "S65A" firmware
336 		 * revision, and has also been reported (by Stephen J.
337 		 * Roznowski <sjr@home.net>) for a drive with the "S61A"
338 		 * firmware revision.
339 		 *
340 		 * Although no one has reported problems with the 2 gig
341 		 * version of the DCAS drive, the assumption is that it
342 		 * has the same problems as the 4 gig version.  Therefore
343 		 * this quirk entries disables tagged queueing for all
344 		 * DCAS drives.
345 		 */
346 		{ T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
347 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
348 	},
349 	{
350 		/* Broken tagged queuing drive */
351 		{ T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
352 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
353 	},
354 	{
355 		/* Broken tagged queuing drive */
356 		{ T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
357 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
358 	},
359 	{
360 		/*
361 		 * Broken tagged queuing drive.
362 		 * Submitted by:
363 		 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
364 		 * in PR kern/9535
365 		 */
366 		{ T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
367 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
368 	},
369         {
370 		/*
371 		 * Slow when tagged queueing is enabled. (1.5MB/sec versus
372 		 * 8MB/sec.)
373 		 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
374 		 * Best performance with these drives is achieved with
375 		 * tagged queueing turned off, and write caching turned on.
376 		 */
377 		{ T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
378 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
379         },
380         {
381 		/*
382 		 * Slow when tagged queueing is enabled. (1.5MB/sec versus
383 		 * 8MB/sec.)
384 		 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
385 		 * Best performance with these drives is achieved with
386 		 * tagged queueing turned off, and write caching turned on.
387 		 */
388 		{ T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
389 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
390         },
391 	{
392 		/*
393 		 * Doesn't handle queue full condition correctly,
394 		 * so we need to limit maxtags to what the device
395 		 * can handle instead of determining this automatically.
396 		 */
397 		{ T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
398 		/*quirks*/0, /*mintags*/2, /*maxtags*/32
399 	},
400 	{
401 		/* Really only one LUN */
402 		{ T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA*", "*" },
403 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
404 	},
405 	{
406 		/* I can't believe we need a quirk for DPT volumes. */
407 		{ T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
408 		CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
409 		/*mintags*/0, /*maxtags*/255
410 	},
411 	{
412 		/*
413 		 * Many Sony CDROM drives don't like multi-LUN probing.
414 		 */
415 		{ T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
416 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
417 	},
418 	{
419 		/*
420 		 * This drive doesn't like multiple LUN probing.
421 		 * Submitted by:  Parag Patel <parag@cgt.com>
422 		 */
423 		{ T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R   CDU9*", "*" },
424 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
425 	},
426 	{
427 		{ T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
428 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
429 	},
430 	{
431 		/*
432 		 * The 8200 doesn't like multi-lun probing, and probably
433 		 * don't like serial number requests either.
434 		 */
435 		{
436 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
437 			"EXB-8200*", "*"
438 		},
439 		CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
440 	},
441 	{
442 		/*
443 		 * These Hitachi drives don't like multi-lun probing.
444 		 * The PR submitter has a DK319H, but says that the Linux
445 		 * kernel has a similar work-around for the DK312 and DK314,
446 		 * so all DK31* drives are quirked here.
447 		 * PR:            misc/18793
448 		 * Submitted by:  Paul Haddad <paul@pth.com>
449 		 */
450 		{ T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
451 		CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
452 	},
453 	{
454 		/*
455 		 * This old revision of the TDC3600 is also SCSI-1, and
456 		 * hangs upon serial number probing.
457 		 */
458 		{
459 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
460 			" TDC 3600", "U07:"
461 		},
462 		CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
463 	},
464 	{
465 		/*
466 		 * Would repond to all LUNs if asked for.
467 		 */
468 		{
469 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
470 			"CP150", "*"
471 		},
472 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
473 	},
474 	{
475 		/*
476 		 * Would repond to all LUNs if asked for.
477 		 */
478 		{
479 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
480 			"96X2*", "*"
481 		},
482 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
483 	},
484 	{
485 		/* Submitted by: Matthew Dodd <winter@jurai.net> */
486 		{ T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
487 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
488 	},
489 	{
490 		/* Submitted by: Matthew Dodd <winter@jurai.net> */
491 		{ T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
492 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
493 	},
494 	{
495 		/* TeraSolutions special settings for TRC-22 RAID */
496 		{ T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
497 		  /*quirks*/0, /*mintags*/55, /*maxtags*/255
498 	},
499 	{
500 		/*
501 		 * Would respond to all LUNs.  Device type and removable
502 		 * flag are jumper-selectable.
503 		 */
504 		{ T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
505 		  "Tahiti 1", "*"
506 		},
507 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
508 	},
509 	{
510 		/* Default tagged queuing parameters for all devices */
511 		{
512 		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
513 		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
514 		},
515 		/*quirks*/0, /*mintags*/2, /*maxtags*/255
516 	},
517 };
518 
519 static const int xpt_quirk_table_size =
520 	sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
521 
522 typedef enum {
523 	DM_RET_COPY		= 0x01,
524 	DM_RET_FLAG_MASK	= 0x0f,
525 	DM_RET_NONE		= 0x00,
526 	DM_RET_STOP		= 0x10,
527 	DM_RET_DESCEND		= 0x20,
528 	DM_RET_ERROR		= 0x30,
529 	DM_RET_ACTION_MASK	= 0xf0
530 } dev_match_ret;
531 
532 typedef enum {
533 	XPT_DEPTH_BUS,
534 	XPT_DEPTH_TARGET,
535 	XPT_DEPTH_DEVICE,
536 	XPT_DEPTH_PERIPH
537 } xpt_traverse_depth;
538 
539 struct xpt_traverse_config {
540 	xpt_traverse_depth	depth;
541 	void			*tr_func;
542 	void			*tr_arg;
543 };
544 
545 typedef	int	xpt_busfunc_t (struct cam_eb *bus, void *arg);
546 typedef	int	xpt_targetfunc_t (struct cam_et *target, void *arg);
547 typedef	int	xpt_devicefunc_t (struct cam_ed *device, void *arg);
548 typedef	int	xpt_periphfunc_t (struct cam_periph *periph, void *arg);
549 typedef int	xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
550 
551 /* Transport layer configuration information */
552 static struct xpt_softc xsoftc;
553 
554 /* Queues for our software interrupt handler */
555 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
556 static cam_isrq_t cam_bioq;
557 static cam_isrq_t cam_netq;
558 
559 /* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
560 static SLIST_HEAD(,ccb_hdr) ccb_freeq;
561 static u_int xpt_max_ccbs;	/*
562 				 * Maximum size of ccb pool.  Modified as
563 				 * devices are added/removed or have their
564 				 * opening counts changed.
565 				 */
566 static u_int xpt_ccb_count;	/* Current count of allocated ccbs */
567 
568 struct cam_periph *xpt_periph;
569 
570 static periph_init_t xpt_periph_init;
571 
572 static periph_init_t probe_periph_init;
573 
574 static struct periph_driver xpt_driver =
575 {
576 	xpt_periph_init, "xpt",
577 	TAILQ_HEAD_INITIALIZER(xpt_driver.units)
578 };
579 
580 static struct periph_driver probe_driver =
581 {
582 	probe_periph_init, "probe",
583 	TAILQ_HEAD_INITIALIZER(probe_driver.units)
584 };
585 
586 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
587 PERIPHDRIVER_DECLARE(probe, probe_driver);
588 
589 #define XPT_CDEV_MAJOR 104
590 
591 static d_open_t xptopen;
592 static d_close_t xptclose;
593 static d_ioctl_t xptioctl;
594 
595 static struct cdevsw xpt_cdevsw = {
596 	/* open */	xptopen,
597 	/* close */	xptclose,
598 	/* read */	noread,
599 	/* write */	nowrite,
600 	/* ioctl */	xptioctl,
601 	/* poll */	nopoll,
602 	/* mmap */	nommap,
603 	/* strategy */	nostrategy,
604 	/* name */	"xpt",
605 	/* maj */	XPT_CDEV_MAJOR,
606 	/* dump */	nodump,
607 	/* psize */	nopsize,
608 	/* flags */	0,
609 };
610 
611 static struct intr_config_hook *xpt_config_hook;
612 
613 /* Registered busses */
614 static TAILQ_HEAD(,cam_eb) xpt_busses;
615 static u_int bus_generation;
616 
617 /* Storage for debugging datastructures */
618 #ifdef	CAMDEBUG
619 struct cam_path *cam_dpath;
620 u_int32_t cam_dflags;
621 u_int32_t cam_debug_delay;
622 #endif
623 
624 /* Pointers to software interrupt handlers */
625 void *camnet_ih;
626 void *cambio_ih;
627 
628 #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
629 #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
630 #endif
631 
632 /*
633  * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
634  * enabled.  Also, the user must have either none, or all of CAM_DEBUG_BUS,
635  * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
636  */
637 #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
638     || defined(CAM_DEBUG_LUN)
639 #ifdef CAMDEBUG
640 #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
641     || !defined(CAM_DEBUG_LUN)
642 #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
643         and CAM_DEBUG_LUN"
644 #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
645 #else /* !CAMDEBUG */
646 #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
647 #endif /* CAMDEBUG */
648 #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
649 
650 /* Our boot-time initialization hook */
651 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
652 
653 static moduledata_t cam_moduledata = {
654 	"cam",
655 	cam_module_event_handler,
656 	NULL
657 };
658 
659 static void	xpt_init(void *);
660 
661 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
662 MODULE_VERSION(cam, 1);
663 
664 
665 static cam_status	xpt_compile_path(struct cam_path *new_path,
666 					 struct cam_periph *perph,
667 					 path_id_t path_id,
668 					 target_id_t target_id,
669 					 lun_id_t lun_id);
670 
671 static void		xpt_release_path(struct cam_path *path);
672 
673 static void		xpt_async_bcast(struct async_list *async_head,
674 					u_int32_t async_code,
675 					struct cam_path *path,
676 					void *async_arg);
677 static void		xpt_dev_async(u_int32_t async_code,
678 				      struct cam_eb *bus,
679 				      struct cam_et *target,
680 				      struct cam_ed *device,
681 				      void *async_arg);
682 static path_id_t xptnextfreepathid(void);
683 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
684 static union ccb *xpt_get_ccb(struct cam_ed *device);
685 static int	 xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
686 				  u_int32_t new_priority);
687 static void	 xpt_run_dev_allocq(struct cam_eb *bus);
688 static void	 xpt_run_dev_sendq(struct cam_eb *bus);
689 static timeout_t xpt_release_devq_timeout;
690 static timeout_t xpt_release_simq_timeout;
691 static void	 xpt_release_bus(struct cam_eb *bus);
692 static void	 xpt_release_devq_device(struct cam_ed *dev, u_int count,
693 					 int run_queue);
694 static struct cam_et*
695 		 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
696 static void	 xpt_release_target(struct cam_eb *bus, struct cam_et *target);
697 static struct cam_ed*
698 		 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
699 				  lun_id_t lun_id);
700 static void	 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
701 				    struct cam_ed *device);
702 static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
703 static struct cam_eb*
704 		 xpt_find_bus(path_id_t path_id);
705 static struct cam_et*
706 		 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
707 static struct cam_ed*
708 		 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
709 static void	 xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
710 static void	 xpt_scan_lun(struct cam_periph *periph,
711 			      struct cam_path *path, cam_flags flags,
712 			      union ccb *ccb);
713 static void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
714 static xpt_busfunc_t	xptconfigbuscountfunc;
715 static xpt_busfunc_t	xptconfigfunc;
716 static void	 xpt_config(void *arg);
717 static xpt_devicefunc_t xptpassannouncefunc;
718 static void	 xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
719 static void	 xptaction(struct cam_sim *sim, union ccb *work_ccb);
720 static void	 xptpoll(struct cam_sim *sim);
721 static void	 camisr(void *);
722 #if 0
723 static void	 xptstart(struct cam_periph *periph, union ccb *work_ccb);
724 static void	 xptasync(struct cam_periph *periph,
725 			  u_int32_t code, cam_path *path);
726 #endif
727 static dev_match_ret	xptbusmatch(struct dev_match_pattern *patterns,
728 				    u_int num_patterns, struct cam_eb *bus);
729 static dev_match_ret	xptdevicematch(struct dev_match_pattern *patterns,
730 				       u_int num_patterns,
731 				       struct cam_ed *device);
732 static dev_match_ret	xptperiphmatch(struct dev_match_pattern *patterns,
733 				       u_int num_patterns,
734 				       struct cam_periph *periph);
735 static xpt_busfunc_t	xptedtbusfunc;
736 static xpt_targetfunc_t	xptedttargetfunc;
737 static xpt_devicefunc_t	xptedtdevicefunc;
738 static xpt_periphfunc_t	xptedtperiphfunc;
739 static xpt_pdrvfunc_t	xptplistpdrvfunc;
740 static xpt_periphfunc_t	xptplistperiphfunc;
741 static int		xptedtmatch(struct ccb_dev_match *cdm);
742 static int		xptperiphlistmatch(struct ccb_dev_match *cdm);
743 static int		xptbustraverse(struct cam_eb *start_bus,
744 				       xpt_busfunc_t *tr_func, void *arg);
745 static int		xpttargettraverse(struct cam_eb *bus,
746 					  struct cam_et *start_target,
747 					  xpt_targetfunc_t *tr_func, void *arg);
748 static int		xptdevicetraverse(struct cam_et *target,
749 					  struct cam_ed *start_device,
750 					  xpt_devicefunc_t *tr_func, void *arg);
751 static int		xptperiphtraverse(struct cam_ed *device,
752 					  struct cam_periph *start_periph,
753 					  xpt_periphfunc_t *tr_func, void *arg);
754 static int		xptpdrvtraverse(struct periph_driver **start_pdrv,
755 					xpt_pdrvfunc_t *tr_func, void *arg);
756 static int		xptpdperiphtraverse(struct periph_driver **pdrv,
757 					    struct cam_periph *start_periph,
758 					    xpt_periphfunc_t *tr_func,
759 					    void *arg);
760 static xpt_busfunc_t	xptdefbusfunc;
761 static xpt_targetfunc_t	xptdeftargetfunc;
762 static xpt_devicefunc_t	xptdefdevicefunc;
763 static xpt_periphfunc_t	xptdefperiphfunc;
764 static int		xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
765 #ifdef notusedyet
766 static int		xpt_for_all_targets(xpt_targetfunc_t *tr_func,
767 					    void *arg);
768 #endif
769 static int		xpt_for_all_devices(xpt_devicefunc_t *tr_func,
770 					    void *arg);
771 #ifdef notusedyet
772 static int		xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
773 					    void *arg);
774 #endif
775 static xpt_devicefunc_t	xptsetasyncfunc;
776 static xpt_busfunc_t	xptsetasyncbusfunc;
777 static cam_status	xptregister(struct cam_periph *periph,
778 				    void *arg);
779 static cam_status	proberegister(struct cam_periph *periph,
780 				      void *arg);
781 static void	 probeschedule(struct cam_periph *probe_periph);
782 static void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
783 static void	 proberequestdefaultnegotiation(struct cam_periph *periph);
784 static void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
785 static void	 probecleanup(struct cam_periph *periph);
786 static void	 xpt_find_quirk(struct cam_ed *device);
787 #ifdef CAM_NEW_TRAN_CODE
788 static void	 xpt_devise_transport(struct cam_path *path);
789 #endif /* CAM_NEW_TRAN_CODE */
790 static void	 xpt_set_transfer_settings(struct ccb_trans_settings *cts,
791 					   struct cam_ed *device,
792 					   int async_update);
793 static void	 xpt_toggle_tags(struct cam_path *path);
794 static void	 xpt_start_tags(struct cam_path *path);
795 static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
796 					    struct cam_ed *dev);
797 static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
798 					   struct cam_ed *dev);
799 static __inline int periph_is_queued(struct cam_periph *periph);
800 static __inline int device_is_alloc_queued(struct cam_ed *device);
801 static __inline int device_is_send_queued(struct cam_ed *device);
802 static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
803 
804 static __inline int
805 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
806 {
807 	int retval;
808 
809 	if (dev->ccbq.devq_openings > 0) {
810 		if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
811 			cam_ccbq_resize(&dev->ccbq,
812 					dev->ccbq.dev_openings
813 					+ dev->ccbq.dev_active);
814 			dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
815 		}
816 		/*
817 		 * The priority of a device waiting for CCB resources
818 		 * is that of the the highest priority peripheral driver
819 		 * enqueued.
820 		 */
821 		retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
822 					  &dev->alloc_ccb_entry.pinfo,
823 					  CAMQ_GET_HEAD(&dev->drvq)->priority);
824 	} else {
825 		retval = 0;
826 	}
827 
828 	return (retval);
829 }
830 
831 static __inline int
832 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
833 {
834 	int	retval;
835 
836 	if (dev->ccbq.dev_openings > 0) {
837 		/*
838 		 * The priority of a device waiting for controller
839 		 * resources is that of the the highest priority CCB
840 		 * enqueued.
841 		 */
842 		retval =
843 		    xpt_schedule_dev(&bus->sim->devq->send_queue,
844 				     &dev->send_ccb_entry.pinfo,
845 				     CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
846 	} else {
847 		retval = 0;
848 	}
849 	return (retval);
850 }
851 
852 static __inline int
853 periph_is_queued(struct cam_periph *periph)
854 {
855 	return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
856 }
857 
858 static __inline int
859 device_is_alloc_queued(struct cam_ed *device)
860 {
861 	return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
862 }
863 
864 static __inline int
865 device_is_send_queued(struct cam_ed *device)
866 {
867 	return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
868 }
869 
870 static __inline int
871 dev_allocq_is_runnable(struct cam_devq *devq)
872 {
873 	/*
874 	 * Have work to do.
875 	 * Have space to do more work.
876 	 * Allowed to do work.
877 	 */
878 	return ((devq->alloc_queue.qfrozen_cnt == 0)
879 	     && (devq->alloc_queue.entries > 0)
880 	     && (devq->alloc_openings > 0));
881 }
882 
883 static void
884 xpt_periph_init()
885 {
886 	make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
887 }
888 
889 static void
890 probe_periph_init()
891 {
892 }
893 
894 
895 static void
896 xptdone(struct cam_periph *periph, union ccb *done_ccb)
897 {
898 	/* Caller will release the CCB */
899 	wakeup(&done_ccb->ccb_h.cbfcnp);
900 }
901 
902 static int
903 xptopen(dev_t dev, int flags, int fmt, struct proc *p)
904 {
905 	int unit;
906 
907 	unit = minor(dev) & 0xff;
908 
909 	/*
910 	 * Only allow read-write access.
911 	 */
912 	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
913 		return(EPERM);
914 
915 	/*
916 	 * We don't allow nonblocking access.
917 	 */
918 	if ((flags & O_NONBLOCK) != 0) {
919 		printf("xpt%d: can't do nonblocking accesss\n", unit);
920 		return(ENODEV);
921 	}
922 
923 	/*
924 	 * We only have one transport layer right now.  If someone accesses
925 	 * us via something other than minor number 1, point out their
926 	 * mistake.
927 	 */
928 	if (unit != 0) {
929 		printf("xptopen: got invalid xpt unit %d\n", unit);
930 		return(ENXIO);
931 	}
932 
933 	/* Mark ourselves open */
934 	xsoftc.flags |= XPT_FLAG_OPEN;
935 
936 	return(0);
937 }
938 
939 static int
940 xptclose(dev_t dev, int flag, int fmt, struct proc *p)
941 {
942 	int unit;
943 
944 	unit = minor(dev) & 0xff;
945 
946 	/*
947 	 * We only have one transport layer right now.  If someone accesses
948 	 * us via something other than minor number 1, point out their
949 	 * mistake.
950 	 */
951 	if (unit != 0) {
952 		printf("xptclose: got invalid xpt unit %d\n", unit);
953 		return(ENXIO);
954 	}
955 
956 	/* Mark ourselves closed */
957 	xsoftc.flags &= ~XPT_FLAG_OPEN;
958 
959 	return(0);
960 }
961 
962 static int
963 xptioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
964 {
965 	int unit, error;
966 
967 	error = 0;
968 	unit = minor(dev) & 0xff;
969 
970 	/*
971 	 * We only have one transport layer right now.  If someone accesses
972 	 * us via something other than minor number 1, point out their
973 	 * mistake.
974 	 */
975 	if (unit != 0) {
976 		printf("xptioctl: got invalid xpt unit %d\n", unit);
977 		return(ENXIO);
978 	}
979 
980 	switch(cmd) {
981 	/*
982 	 * For the transport layer CAMIOCOMMAND ioctl, we really only want
983 	 * to accept CCB types that don't quite make sense to send through a
984 	 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
985 	 * in the CAM spec.
986 	 */
987 	case CAMIOCOMMAND: {
988 		union ccb *ccb;
989 		union ccb *inccb;
990 
991 		inccb = (union ccb *)addr;
992 
993 		switch(inccb->ccb_h.func_code) {
994 		case XPT_SCAN_BUS:
995 		case XPT_RESET_BUS:
996 			if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
997 			 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
998 				error = EINVAL;
999 				break;
1000 			}
1001 			/* FALLTHROUGH */
1002 		case XPT_PATH_INQ:
1003 		case XPT_ENG_INQ:
1004 		case XPT_SCAN_LUN:
1005 
1006 			ccb = xpt_alloc_ccb();
1007 
1008 			/*
1009 			 * Create a path using the bus, target, and lun the
1010 			 * user passed in.
1011 			 */
1012 			if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1013 					    inccb->ccb_h.path_id,
1014 					    inccb->ccb_h.target_id,
1015 					    inccb->ccb_h.target_lun) !=
1016 					    CAM_REQ_CMP){
1017 				error = EINVAL;
1018 				xpt_free_ccb(ccb);
1019 				break;
1020 			}
1021 			/* Ensure all of our fields are correct */
1022 			xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
1023 				      inccb->ccb_h.pinfo.priority);
1024 			xpt_merge_ccb(ccb, inccb);
1025 			ccb->ccb_h.cbfcnp = xptdone;
1026 			cam_periph_runccb(ccb, NULL, 0, 0, NULL);
1027 			bcopy(ccb, inccb, sizeof(union ccb));
1028 			xpt_free_path(ccb->ccb_h.path);
1029 			xpt_free_ccb(ccb);
1030 			break;
1031 
1032 		case XPT_DEBUG: {
1033 			union ccb ccb;
1034 
1035 			/*
1036 			 * This is an immediate CCB, so it's okay to
1037 			 * allocate it on the stack.
1038 			 */
1039 
1040 			/*
1041 			 * Create a path using the bus, target, and lun the
1042 			 * user passed in.
1043 			 */
1044 			if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
1045 					    inccb->ccb_h.path_id,
1046 					    inccb->ccb_h.target_id,
1047 					    inccb->ccb_h.target_lun) !=
1048 					    CAM_REQ_CMP){
1049 				error = EINVAL;
1050 				break;
1051 			}
1052 			/* Ensure all of our fields are correct */
1053 			xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1054 				      inccb->ccb_h.pinfo.priority);
1055 			xpt_merge_ccb(&ccb, inccb);
1056 			ccb.ccb_h.cbfcnp = xptdone;
1057 			xpt_action(&ccb);
1058 			bcopy(&ccb, inccb, sizeof(union ccb));
1059 			xpt_free_path(ccb.ccb_h.path);
1060 			break;
1061 
1062 		}
1063 		case XPT_DEV_MATCH: {
1064 			struct cam_periph_map_info mapinfo;
1065 			struct cam_path *old_path;
1066 
1067 			/*
1068 			 * We can't deal with physical addresses for this
1069 			 * type of transaction.
1070 			 */
1071 			if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1072 				error = EINVAL;
1073 				break;
1074 			}
1075 
1076 			/*
1077 			 * Save this in case the caller had it set to
1078 			 * something in particular.
1079 			 */
1080 			old_path = inccb->ccb_h.path;
1081 
1082 			/*
1083 			 * We really don't need a path for the matching
1084 			 * code.  The path is needed because of the
1085 			 * debugging statements in xpt_action().  They
1086 			 * assume that the CCB has a valid path.
1087 			 */
1088 			inccb->ccb_h.path = xpt_periph->path;
1089 
1090 			bzero(&mapinfo, sizeof(mapinfo));
1091 
1092 			/*
1093 			 * Map the pattern and match buffers into kernel
1094 			 * virtual address space.
1095 			 */
1096 			error = cam_periph_mapmem(inccb, &mapinfo);
1097 
1098 			if (error) {
1099 				inccb->ccb_h.path = old_path;
1100 				break;
1101 			}
1102 
1103 			/*
1104 			 * This is an immediate CCB, we can send it on directly.
1105 			 */
1106 			xpt_action(inccb);
1107 
1108 			/*
1109 			 * Map the buffers back into user space.
1110 			 */
1111 			cam_periph_unmapmem(inccb, &mapinfo);
1112 
1113 			inccb->ccb_h.path = old_path;
1114 
1115 			error = 0;
1116 			break;
1117 		}
1118 		default:
1119 			error = ENOTSUP;
1120 			break;
1121 		}
1122 		break;
1123 	}
1124 	/*
1125 	 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1126 	 * with the periphal driver name and unit name filled in.  The other
1127 	 * fields don't really matter as input.  The passthrough driver name
1128 	 * ("pass"), and unit number are passed back in the ccb.  The current
1129 	 * device generation number, and the index into the device peripheral
1130 	 * driver list, and the status are also passed back.  Note that
1131 	 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1132 	 * we never return a status of CAM_GDEVLIST_LIST_CHANGED.  It is
1133 	 * (or rather should be) impossible for the device peripheral driver
1134 	 * list to change since we look at the whole thing in one pass, and
1135 	 * we do it with splcam protection.
1136 	 *
1137 	 */
1138 	case CAMGETPASSTHRU: {
1139 		union ccb *ccb;
1140 		struct cam_periph *periph;
1141 		struct periph_driver **p_drv;
1142 		char   *name;
1143 		u_int unit;
1144 		u_int cur_generation;
1145 		int base_periph_found;
1146 		int splbreaknum;
1147 		int s;
1148 
1149 		ccb = (union ccb *)addr;
1150 		unit = ccb->cgdl.unit_number;
1151 		name = ccb->cgdl.periph_name;
1152 		/*
1153 		 * Every 100 devices, we want to drop our spl protection to
1154 		 * give the software interrupt handler a chance to run.
1155 		 * Most systems won't run into this check, but this should
1156 		 * avoid starvation in the software interrupt handler in
1157 		 * large systems.
1158 		 */
1159 		splbreaknum = 100;
1160 
1161 		ccb = (union ccb *)addr;
1162 
1163 		base_periph_found = 0;
1164 
1165 		/*
1166 		 * Sanity check -- make sure we don't get a null peripheral
1167 		 * driver name.
1168 		 */
1169 		if (*ccb->cgdl.periph_name == '\0') {
1170 			error = EINVAL;
1171 			break;
1172 		}
1173 
1174 		/* Keep the list from changing while we traverse it */
1175 		s = splcam();
1176 ptstartover:
1177 		cur_generation = xsoftc.generation;
1178 
1179 		/* first find our driver in the list of drivers */
1180 		for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
1181 			if (strcmp((*p_drv)->driver_name, name) == 0)
1182 				break;
1183 
1184 		if (*p_drv == NULL) {
1185 			splx(s);
1186 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1187 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1188 			*ccb->cgdl.periph_name = '\0';
1189 			ccb->cgdl.unit_number = 0;
1190 			error = ENOENT;
1191 			break;
1192 		}
1193 
1194 		/*
1195 		 * Run through every peripheral instance of this driver
1196 		 * and check to see whether it matches the unit passed
1197 		 * in by the user.  If it does, get out of the loops and
1198 		 * find the passthrough driver associated with that
1199 		 * peripheral driver.
1200 		 */
1201 		for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
1202 		     periph = TAILQ_NEXT(periph, unit_links)) {
1203 
1204 			if (periph->unit_number == unit) {
1205 				break;
1206 			} else if (--splbreaknum == 0) {
1207 				splx(s);
1208 				s = splcam();
1209 				splbreaknum = 100;
1210 				if (cur_generation != xsoftc.generation)
1211 				       goto ptstartover;
1212 			}
1213 		}
1214 		/*
1215 		 * If we found the peripheral driver that the user passed
1216 		 * in, go through all of the peripheral drivers for that
1217 		 * particular device and look for a passthrough driver.
1218 		 */
1219 		if (periph != NULL) {
1220 			struct cam_ed *device;
1221 			int i;
1222 
1223 			base_periph_found = 1;
1224 			device = periph->path->device;
1225 			for (i = 0, periph = SLIST_FIRST(&device->periphs);
1226 			     periph != NULL;
1227 			     periph = SLIST_NEXT(periph, periph_links), i++) {
1228 				/*
1229 				 * Check to see whether we have a
1230 				 * passthrough device or not.
1231 				 */
1232 				if (strcmp(periph->periph_name, "pass") == 0) {
1233 					/*
1234 					 * Fill in the getdevlist fields.
1235 					 */
1236 					strcpy(ccb->cgdl.periph_name,
1237 					       periph->periph_name);
1238 					ccb->cgdl.unit_number =
1239 						periph->unit_number;
1240 					if (SLIST_NEXT(periph, periph_links))
1241 						ccb->cgdl.status =
1242 							CAM_GDEVLIST_MORE_DEVS;
1243 					else
1244 						ccb->cgdl.status =
1245 						       CAM_GDEVLIST_LAST_DEVICE;
1246 					ccb->cgdl.generation =
1247 						device->generation;
1248 					ccb->cgdl.index = i;
1249 					/*
1250 					 * Fill in some CCB header fields
1251 					 * that the user may want.
1252 					 */
1253 					ccb->ccb_h.path_id =
1254 						periph->path->bus->path_id;
1255 					ccb->ccb_h.target_id =
1256 						periph->path->target->target_id;
1257 					ccb->ccb_h.target_lun =
1258 						periph->path->device->lun_id;
1259 					ccb->ccb_h.status = CAM_REQ_CMP;
1260 					break;
1261 				}
1262 			}
1263 		}
1264 
1265 		/*
1266 		 * If the periph is null here, one of two things has
1267 		 * happened.  The first possibility is that we couldn't
1268 		 * find the unit number of the particular peripheral driver
1269 		 * that the user is asking about.  e.g. the user asks for
1270 		 * the passthrough driver for "da11".  We find the list of
1271 		 * "da" peripherals all right, but there is no unit 11.
1272 		 * The other possibility is that we went through the list
1273 		 * of peripheral drivers attached to the device structure,
1274 		 * but didn't find one with the name "pass".  Either way,
1275 		 * we return ENOENT, since we couldn't find something.
1276 		 */
1277 		if (periph == NULL) {
1278 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1279 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1280 			*ccb->cgdl.periph_name = '\0';
1281 			ccb->cgdl.unit_number = 0;
1282 			error = ENOENT;
1283 			/*
1284 			 * It is unfortunate that this is even necessary,
1285 			 * but there are many, many clueless users out there.
1286 			 * If this is true, the user is looking for the
1287 			 * passthrough driver, but doesn't have one in his
1288 			 * kernel.
1289 			 */
1290 			if (base_periph_found == 1) {
1291 				printf("xptioctl: pass driver is not in the "
1292 				       "kernel\n");
1293 				printf("xptioctl: put \"device pass0\" in "
1294 				       "your kernel config file\n");
1295 			}
1296 		}
1297 		splx(s);
1298 		break;
1299 		}
1300 	default:
1301 		error = ENOTTY;
1302 		break;
1303 	}
1304 
1305 	return(error);
1306 }
1307 
1308 static int
1309 cam_module_event_handler(module_t mod, int what, void *arg)
1310 {
1311 	if (what == MOD_LOAD) {
1312 		xpt_init(NULL);
1313 	} else if (what == MOD_UNLOAD) {
1314 		return EBUSY;
1315 	}
1316 
1317 	return 0;
1318 }
1319 
1320 /* Functions accessed by the peripheral drivers */
1321 static void
1322 xpt_init(dummy)
1323 	void *dummy;
1324 {
1325 	struct cam_sim *xpt_sim;
1326 	struct cam_path *path;
1327 	struct cam_devq *devq;
1328 	cam_status status;
1329 
1330 	TAILQ_INIT(&xpt_busses);
1331 	TAILQ_INIT(&cam_bioq);
1332 	TAILQ_INIT(&cam_netq);
1333 	SLIST_INIT(&ccb_freeq);
1334 	STAILQ_INIT(&highpowerq);
1335 
1336 	/*
1337 	 * The xpt layer is, itself, the equivelent of a SIM.
1338 	 * Allow 16 ccbs in the ccb pool for it.  This should
1339 	 * give decent parallelism when we probe busses and
1340 	 * perform other XPT functions.
1341 	 */
1342 	devq = cam_simq_alloc(16);
1343 	xpt_sim = cam_sim_alloc(xptaction,
1344 				xptpoll,
1345 				"xpt",
1346 				/*softc*/NULL,
1347 				/*unit*/0,
1348 				/*max_dev_transactions*/0,
1349 				/*max_tagged_dev_transactions*/0,
1350 				devq);
1351 	xpt_max_ccbs = 16;
1352 
1353 	xpt_bus_register(xpt_sim, /*bus #*/0);
1354 
1355 	/*
1356 	 * Looking at the XPT from the SIM layer, the XPT is
1357 	 * the equivelent of a peripheral driver.  Allocate
1358 	 * a peripheral driver entry for us.
1359 	 */
1360 	if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1361 				      CAM_TARGET_WILDCARD,
1362 				      CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1363 		printf("xpt_init: xpt_create_path failed with status %#x,"
1364 		       " failing attach\n", status);
1365 		return;
1366 	}
1367 
1368 	cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1369 			 path, NULL, 0, NULL);
1370 	xpt_free_path(path);
1371 
1372 	xpt_sim->softc = xpt_periph;
1373 
1374 	/*
1375 	 * Register a callback for when interrupts are enabled.
1376 	 */
1377 	xpt_config_hook =
1378 	    (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
1379 					      M_TEMP, M_NOWAIT | M_ZERO);
1380 	if (xpt_config_hook == NULL) {
1381 		printf("xpt_init: Cannot malloc config hook "
1382 		       "- failing attach\n");
1383 		return;
1384 	}
1385 
1386 	xpt_config_hook->ich_func = xpt_config;
1387 	if (config_intrhook_establish(xpt_config_hook) != 0) {
1388 		free (xpt_config_hook, M_TEMP);
1389 		printf("xpt_init: config_intrhook_establish failed "
1390 		       "- failing attach\n");
1391 	}
1392 
1393 	/* Install our software interrupt handlers */
1394 	swi_add(NULL, "camnet", camisr, &cam_netq, SWI_CAMNET, 0, &camnet_ih);
1395 	swi_add(NULL, "cambio", camisr, &cam_bioq, SWI_CAMBIO, 0, &cambio_ih);
1396 }
1397 
1398 static cam_status
1399 xptregister(struct cam_periph *periph, void *arg)
1400 {
1401 	if (periph == NULL) {
1402 		printf("xptregister: periph was NULL!!\n");
1403 		return(CAM_REQ_CMP_ERR);
1404 	}
1405 
1406 	periph->softc = NULL;
1407 
1408 	xpt_periph = periph;
1409 
1410 	return(CAM_REQ_CMP);
1411 }
1412 
1413 int32_t
1414 xpt_add_periph(struct cam_periph *periph)
1415 {
1416 	struct cam_ed *device;
1417 	int32_t	 status;
1418 	struct periph_list *periph_head;
1419 
1420 	device = periph->path->device;
1421 
1422 	periph_head = &device->periphs;
1423 
1424 	status = CAM_REQ_CMP;
1425 
1426 	if (device != NULL) {
1427 		int s;
1428 
1429 		/*
1430 		 * Make room for this peripheral
1431 		 * so it will fit in the queue
1432 		 * when it's scheduled to run
1433 		 */
1434 		s = splsoftcam();
1435 		status = camq_resize(&device->drvq,
1436 				     device->drvq.array_size + 1);
1437 
1438 		device->generation++;
1439 
1440 		SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1441 
1442 		splx(s);
1443 	}
1444 
1445 	xsoftc.generation++;
1446 
1447 	return (status);
1448 }
1449 
1450 void
1451 xpt_remove_periph(struct cam_periph *periph)
1452 {
1453 	struct cam_ed *device;
1454 
1455 	device = periph->path->device;
1456 
1457 	if (device != NULL) {
1458 		int s;
1459 		struct periph_list *periph_head;
1460 
1461 		periph_head = &device->periphs;
1462 
1463 		/* Release the slot for this peripheral */
1464 		s = splsoftcam();
1465 		camq_resize(&device->drvq, device->drvq.array_size - 1);
1466 
1467 		device->generation++;
1468 
1469 		SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1470 
1471 		splx(s);
1472 	}
1473 
1474 	xsoftc.generation++;
1475 
1476 }
1477 
1478 #ifdef CAM_NEW_TRAN_CODE
1479 
1480 void
1481 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1482 {
1483 	struct	ccb_pathinq cpi;
1484 	struct	ccb_trans_settings cts;
1485 	struct	cam_path *path;
1486 	u_int	speed;
1487 	u_int	freq;
1488 	u_int	mb;
1489 	int	s;
1490 
1491 	path = periph->path;
1492 	/*
1493 	 * To ensure that this is printed in one piece,
1494 	 * mask out CAM interrupts.
1495 	 */
1496 	s = splsoftcam();
1497 	printf("%s%d at %s%d bus %d target %d lun %d\n",
1498 	       periph->periph_name, periph->unit_number,
1499 	       path->bus->sim->sim_name,
1500 	       path->bus->sim->unit_number,
1501 	       path->bus->sim->bus_id,
1502 	       path->target->target_id,
1503 	       path->device->lun_id);
1504 	printf("%s%d: ", periph->periph_name, periph->unit_number);
1505 	scsi_print_inquiry(&path->device->inq_data);
1506 	if ((bootverbose)
1507 	 && (path->device->serial_num_len > 0)) {
1508 		/* Don't wrap the screen  - print only the first 60 chars */
1509 		printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1510 		       periph->unit_number, path->device->serial_num);
1511 	}
1512 	xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1513 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1514 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1515 	xpt_action((union ccb*)&cts);
1516 
1517 	/* Ask the SIM for its base transfer speed */
1518 	xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1519 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1520 	xpt_action((union ccb *)&cpi);
1521 
1522 	speed = cpi.base_transfer_speed;
1523 	freq = 0;
1524 	if (cts.ccb_h.status == CAM_REQ_CMP
1525 	 && cts.transport == XPORT_SPI) {
1526 		struct	ccb_trans_settings_spi *spi;
1527 
1528 		spi = &cts.xport_specific.spi;
1529 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
1530 		  && spi->sync_offset != 0) {
1531 			freq = scsi_calc_syncsrate(spi->sync_period);
1532 			speed = freq;
1533 		}
1534 
1535 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
1536 			speed *= (0x01 << spi->bus_width);
1537 	}
1538 
1539 	mb = speed / 1000;
1540 	if (mb > 0)
1541 		printf("%s%d: %d.%03dMB/s transfers",
1542 		       periph->periph_name, periph->unit_number,
1543 		       mb, speed % 1000);
1544 	else
1545 		printf("%s%d: %dKB/s transfers", periph->periph_name,
1546 		       periph->unit_number, speed);
1547 	/* Report additional information about SPI connections */
1548 	if (cts.ccb_h.status == CAM_REQ_CMP
1549 	 && cts.transport == XPORT_SPI) {
1550 		struct	ccb_trans_settings_spi *spi;
1551 
1552 		spi = &cts.xport_specific.spi;
1553 		if (freq != 0) {
1554 			printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
1555 			       freq % 1000,
1556 			       (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
1557 			     ? " DT" : "",
1558 			       spi->sync_offset);
1559 		}
1560 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
1561 		 && spi->bus_width > 0) {
1562 			if (freq != 0) {
1563 				printf(", ");
1564 			} else {
1565 				printf(" (");
1566 			}
1567 			printf("%dbit)", 8 * (0x01 << spi->bus_width));
1568 		} else if (freq != 0) {
1569 			printf(")");
1570 		}
1571 	}
1572 
1573 	if (path->device->inq_flags & SID_CmdQue
1574 	 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1575 		printf("\n%s%d: Tagged Queueing Enabled",
1576 		       periph->periph_name, periph->unit_number);
1577 	}
1578 	printf("\n");
1579 
1580 	/*
1581 	 * We only want to print the caller's announce string if they've
1582 	 * passed one in..
1583 	 */
1584 	if (announce_string != NULL)
1585 		printf("%s%d: %s\n", periph->periph_name,
1586 		       periph->unit_number, announce_string);
1587 	splx(s);
1588 }
1589 #else /* CAM_NEW_TRAN_CODE */
1590 void
1591 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1592 {
1593 	int s;
1594 	u_int mb;
1595 	struct cam_path *path;
1596 	struct ccb_trans_settings cts;
1597 
1598 	path = periph->path;
1599 	/*
1600 	 * To ensure that this is printed in one piece,
1601 	 * mask out CAM interrupts.
1602 	 */
1603 	s = splsoftcam();
1604 	printf("%s%d at %s%d bus %d target %d lun %d\n",
1605 	       periph->periph_name, periph->unit_number,
1606 	       path->bus->sim->sim_name,
1607 	       path->bus->sim->unit_number,
1608 	       path->bus->sim->bus_id,
1609 	       path->target->target_id,
1610 	       path->device->lun_id);
1611 	printf("%s%d: ", periph->periph_name, periph->unit_number);
1612 	scsi_print_inquiry(&path->device->inq_data);
1613 	if ((bootverbose)
1614 	 && (path->device->serial_num_len > 0)) {
1615 		/* Don't wrap the screen  - print only the first 60 chars */
1616 		printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1617 		       periph->unit_number, path->device->serial_num);
1618 	}
1619 	xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1620 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1621 	cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1622 	xpt_action((union ccb*)&cts);
1623 	if (cts.ccb_h.status == CAM_REQ_CMP) {
1624 		u_int speed;
1625 		u_int freq;
1626 
1627 		if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1628 		  && cts.sync_offset != 0) {
1629 			freq = scsi_calc_syncsrate(cts.sync_period);
1630 			speed = freq;
1631 		} else {
1632 			struct ccb_pathinq cpi;
1633 
1634 			/* Ask the SIM for its base transfer speed */
1635 			xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1636 			cpi.ccb_h.func_code = XPT_PATH_INQ;
1637 			xpt_action((union ccb *)&cpi);
1638 
1639 			speed = cpi.base_transfer_speed;
1640 			freq = 0;
1641 		}
1642 		if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
1643 			speed *= (0x01 << cts.bus_width);
1644 		mb = speed / 1000;
1645 		if (mb > 0)
1646 			printf("%s%d: %d.%03dMB/s transfers",
1647 			       periph->periph_name, periph->unit_number,
1648 			       mb, speed % 1000);
1649 		else
1650 			printf("%s%d: %dKB/s transfers", periph->periph_name,
1651 			       periph->unit_number, speed);
1652 		if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1653 		 && cts.sync_offset != 0) {
1654 			printf(" (%d.%03dMHz, offset %d", freq / 1000,
1655 			       freq % 1000, cts.sync_offset);
1656 		}
1657 		if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0
1658 		 && cts.bus_width > 0) {
1659 			if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1660 			 && cts.sync_offset != 0) {
1661 				printf(", ");
1662 			} else {
1663 				printf(" (");
1664 			}
1665 			printf("%dbit)", 8 * (0x01 << cts.bus_width));
1666 		} else if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1667 			&& cts.sync_offset != 0) {
1668 			printf(")");
1669 		}
1670 
1671 		if (path->device->inq_flags & SID_CmdQue
1672 		 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1673 			printf(", Tagged Queueing Enabled");
1674 		}
1675 
1676 		printf("\n");
1677 	} else if (path->device->inq_flags & SID_CmdQue
1678    		|| path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1679 		printf("%s%d: Tagged Queueing Enabled\n",
1680 		       periph->periph_name, periph->unit_number);
1681 	}
1682 
1683 	/*
1684 	 * We only want to print the caller's announce string if they've
1685 	 * passed one in..
1686 	 */
1687 	if (announce_string != NULL)
1688 		printf("%s%d: %s\n", periph->periph_name,
1689 		       periph->unit_number, announce_string);
1690 	splx(s);
1691 }
1692 
1693 #endif /* CAM_NEW_TRAN_CODE */
1694 
1695 static dev_match_ret
1696 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1697 	    struct cam_eb *bus)
1698 {
1699 	dev_match_ret retval;
1700 	int i;
1701 
1702 	retval = DM_RET_NONE;
1703 
1704 	/*
1705 	 * If we aren't given something to match against, that's an error.
1706 	 */
1707 	if (bus == NULL)
1708 		return(DM_RET_ERROR);
1709 
1710 	/*
1711 	 * If there are no match entries, then this bus matches no
1712 	 * matter what.
1713 	 */
1714 	if ((patterns == NULL) || (num_patterns == 0))
1715 		return(DM_RET_DESCEND | DM_RET_COPY);
1716 
1717 	for (i = 0; i < num_patterns; i++) {
1718 		struct bus_match_pattern *cur_pattern;
1719 
1720 		/*
1721 		 * If the pattern in question isn't for a bus node, we
1722 		 * aren't interested.  However, we do indicate to the
1723 		 * calling routine that we should continue descending the
1724 		 * tree, since the user wants to match against lower-level
1725 		 * EDT elements.
1726 		 */
1727 		if (patterns[i].type != DEV_MATCH_BUS) {
1728 			if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1729 				retval |= DM_RET_DESCEND;
1730 			continue;
1731 		}
1732 
1733 		cur_pattern = &patterns[i].pattern.bus_pattern;
1734 
1735 		/*
1736 		 * If they want to match any bus node, we give them any
1737 		 * device node.
1738 		 */
1739 		if (cur_pattern->flags == BUS_MATCH_ANY) {
1740 			/* set the copy flag */
1741 			retval |= DM_RET_COPY;
1742 
1743 			/*
1744 			 * If we've already decided on an action, go ahead
1745 			 * and return.
1746 			 */
1747 			if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1748 				return(retval);
1749 		}
1750 
1751 		/*
1752 		 * Not sure why someone would do this...
1753 		 */
1754 		if (cur_pattern->flags == BUS_MATCH_NONE)
1755 			continue;
1756 
1757 		if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1758 		 && (cur_pattern->path_id != bus->path_id))
1759 			continue;
1760 
1761 		if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1762 		 && (cur_pattern->bus_id != bus->sim->bus_id))
1763 			continue;
1764 
1765 		if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1766 		 && (cur_pattern->unit_number != bus->sim->unit_number))
1767 			continue;
1768 
1769 		if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1770 		 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1771 			     DEV_IDLEN) != 0))
1772 			continue;
1773 
1774 		/*
1775 		 * If we get to this point, the user definitely wants
1776 		 * information on this bus.  So tell the caller to copy the
1777 		 * data out.
1778 		 */
1779 		retval |= DM_RET_COPY;
1780 
1781 		/*
1782 		 * If the return action has been set to descend, then we
1783 		 * know that we've already seen a non-bus matching
1784 		 * expression, therefore we need to further descend the tree.
1785 		 * This won't change by continuing around the loop, so we
1786 		 * go ahead and return.  If we haven't seen a non-bus
1787 		 * matching expression, we keep going around the loop until
1788 		 * we exhaust the matching expressions.  We'll set the stop
1789 		 * flag once we fall out of the loop.
1790 		 */
1791 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1792 			return(retval);
1793 	}
1794 
1795 	/*
1796 	 * If the return action hasn't been set to descend yet, that means
1797 	 * we haven't seen anything other than bus matching patterns.  So
1798 	 * tell the caller to stop descending the tree -- the user doesn't
1799 	 * want to match against lower level tree elements.
1800 	 */
1801 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1802 		retval |= DM_RET_STOP;
1803 
1804 	return(retval);
1805 }
1806 
1807 static dev_match_ret
1808 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1809 	       struct cam_ed *device)
1810 {
1811 	dev_match_ret retval;
1812 	int i;
1813 
1814 	retval = DM_RET_NONE;
1815 
1816 	/*
1817 	 * If we aren't given something to match against, that's an error.
1818 	 */
1819 	if (device == NULL)
1820 		return(DM_RET_ERROR);
1821 
1822 	/*
1823 	 * If there are no match entries, then this device matches no
1824 	 * matter what.
1825 	 */
1826 	if ((patterns == NULL) || (patterns == 0))
1827 		return(DM_RET_DESCEND | DM_RET_COPY);
1828 
1829 	for (i = 0; i < num_patterns; i++) {
1830 		struct device_match_pattern *cur_pattern;
1831 
1832 		/*
1833 		 * If the pattern in question isn't for a device node, we
1834 		 * aren't interested.
1835 		 */
1836 		if (patterns[i].type != DEV_MATCH_DEVICE) {
1837 			if ((patterns[i].type == DEV_MATCH_PERIPH)
1838 			 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1839 				retval |= DM_RET_DESCEND;
1840 			continue;
1841 		}
1842 
1843 		cur_pattern = &patterns[i].pattern.device_pattern;
1844 
1845 		/*
1846 		 * If they want to match any device node, we give them any
1847 		 * device node.
1848 		 */
1849 		if (cur_pattern->flags == DEV_MATCH_ANY) {
1850 			/* set the copy flag */
1851 			retval |= DM_RET_COPY;
1852 
1853 
1854 			/*
1855 			 * If we've already decided on an action, go ahead
1856 			 * and return.
1857 			 */
1858 			if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1859 				return(retval);
1860 		}
1861 
1862 		/*
1863 		 * Not sure why someone would do this...
1864 		 */
1865 		if (cur_pattern->flags == DEV_MATCH_NONE)
1866 			continue;
1867 
1868 		if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1869 		 && (cur_pattern->path_id != device->target->bus->path_id))
1870 			continue;
1871 
1872 		if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1873 		 && (cur_pattern->target_id != device->target->target_id))
1874 			continue;
1875 
1876 		if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1877 		 && (cur_pattern->target_lun != device->lun_id))
1878 			continue;
1879 
1880 		if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1881 		 && (cam_quirkmatch((caddr_t)&device->inq_data,
1882 				    (caddr_t)&cur_pattern->inq_pat,
1883 				    1, sizeof(cur_pattern->inq_pat),
1884 				    scsi_static_inquiry_match) == NULL))
1885 			continue;
1886 
1887 		/*
1888 		 * If we get to this point, the user definitely wants
1889 		 * information on this device.  So tell the caller to copy
1890 		 * the data out.
1891 		 */
1892 		retval |= DM_RET_COPY;
1893 
1894 		/*
1895 		 * If the return action has been set to descend, then we
1896 		 * know that we've already seen a peripheral matching
1897 		 * expression, therefore we need to further descend the tree.
1898 		 * This won't change by continuing around the loop, so we
1899 		 * go ahead and return.  If we haven't seen a peripheral
1900 		 * matching expression, we keep going around the loop until
1901 		 * we exhaust the matching expressions.  We'll set the stop
1902 		 * flag once we fall out of the loop.
1903 		 */
1904 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1905 			return(retval);
1906 	}
1907 
1908 	/*
1909 	 * If the return action hasn't been set to descend yet, that means
1910 	 * we haven't seen any peripheral matching patterns.  So tell the
1911 	 * caller to stop descending the tree -- the user doesn't want to
1912 	 * match against lower level tree elements.
1913 	 */
1914 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1915 		retval |= DM_RET_STOP;
1916 
1917 	return(retval);
1918 }
1919 
1920 /*
1921  * Match a single peripheral against any number of match patterns.
1922  */
1923 static dev_match_ret
1924 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1925 	       struct cam_periph *periph)
1926 {
1927 	dev_match_ret retval;
1928 	int i;
1929 
1930 	/*
1931 	 * If we aren't given something to match against, that's an error.
1932 	 */
1933 	if (periph == NULL)
1934 		return(DM_RET_ERROR);
1935 
1936 	/*
1937 	 * If there are no match entries, then this peripheral matches no
1938 	 * matter what.
1939 	 */
1940 	if ((patterns == NULL) || (num_patterns == 0))
1941 		return(DM_RET_STOP | DM_RET_COPY);
1942 
1943 	/*
1944 	 * There aren't any nodes below a peripheral node, so there's no
1945 	 * reason to descend the tree any further.
1946 	 */
1947 	retval = DM_RET_STOP;
1948 
1949 	for (i = 0; i < num_patterns; i++) {
1950 		struct periph_match_pattern *cur_pattern;
1951 
1952 		/*
1953 		 * If the pattern in question isn't for a peripheral, we
1954 		 * aren't interested.
1955 		 */
1956 		if (patterns[i].type != DEV_MATCH_PERIPH)
1957 			continue;
1958 
1959 		cur_pattern = &patterns[i].pattern.periph_pattern;
1960 
1961 		/*
1962 		 * If they want to match on anything, then we will do so.
1963 		 */
1964 		if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1965 			/* set the copy flag */
1966 			retval |= DM_RET_COPY;
1967 
1968 			/*
1969 			 * We've already set the return action to stop,
1970 			 * since there are no nodes below peripherals in
1971 			 * the tree.
1972 			 */
1973 			return(retval);
1974 		}
1975 
1976 		/*
1977 		 * Not sure why someone would do this...
1978 		 */
1979 		if (cur_pattern->flags == PERIPH_MATCH_NONE)
1980 			continue;
1981 
1982 		if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1983 		 && (cur_pattern->path_id != periph->path->bus->path_id))
1984 			continue;
1985 
1986 		/*
1987 		 * For the target and lun id's, we have to make sure the
1988 		 * target and lun pointers aren't NULL.  The xpt peripheral
1989 		 * has a wildcard target and device.
1990 		 */
1991 		if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1992 		 && ((periph->path->target == NULL)
1993 		 ||(cur_pattern->target_id != periph->path->target->target_id)))
1994 			continue;
1995 
1996 		if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1997 		 && ((periph->path->device == NULL)
1998 		 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1999 			continue;
2000 
2001 		if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
2002 		 && (cur_pattern->unit_number != periph->unit_number))
2003 			continue;
2004 
2005 		if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
2006 		 && (strncmp(cur_pattern->periph_name, periph->periph_name,
2007 			     DEV_IDLEN) != 0))
2008 			continue;
2009 
2010 		/*
2011 		 * If we get to this point, the user definitely wants
2012 		 * information on this peripheral.  So tell the caller to
2013 		 * copy the data out.
2014 		 */
2015 		retval |= DM_RET_COPY;
2016 
2017 		/*
2018 		 * The return action has already been set to stop, since
2019 		 * peripherals don't have any nodes below them in the EDT.
2020 		 */
2021 		return(retval);
2022 	}
2023 
2024 	/*
2025 	 * If we get to this point, the peripheral that was passed in
2026 	 * doesn't match any of the patterns.
2027 	 */
2028 	return(retval);
2029 }
2030 
2031 static int
2032 xptedtbusfunc(struct cam_eb *bus, void *arg)
2033 {
2034 	struct ccb_dev_match *cdm;
2035 	dev_match_ret retval;
2036 
2037 	cdm = (struct ccb_dev_match *)arg;
2038 
2039 	/*
2040 	 * If our position is for something deeper in the tree, that means
2041 	 * that we've already seen this node.  So, we keep going down.
2042 	 */
2043 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2044 	 && (cdm->pos.cookie.bus == bus)
2045 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2046 	 && (cdm->pos.cookie.target != NULL))
2047 		retval = DM_RET_DESCEND;
2048 	else
2049 		retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
2050 
2051 	/*
2052 	 * If we got an error, bail out of the search.
2053 	 */
2054 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2055 		cdm->status = CAM_DEV_MATCH_ERROR;
2056 		return(0);
2057 	}
2058 
2059 	/*
2060 	 * If the copy flag is set, copy this bus out.
2061 	 */
2062 	if (retval & DM_RET_COPY) {
2063 		int spaceleft, j;
2064 
2065 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
2066 			sizeof(struct dev_match_result));
2067 
2068 		/*
2069 		 * If we don't have enough space to put in another
2070 		 * match result, save our position and tell the
2071 		 * user there are more devices to check.
2072 		 */
2073 		if (spaceleft < sizeof(struct dev_match_result)) {
2074 			bzero(&cdm->pos, sizeof(cdm->pos));
2075 			cdm->pos.position_type =
2076 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
2077 
2078 			cdm->pos.cookie.bus = bus;
2079 			cdm->pos.generations[CAM_BUS_GENERATION]=
2080 				bus_generation;
2081 			cdm->status = CAM_DEV_MATCH_MORE;
2082 			return(0);
2083 		}
2084 		j = cdm->num_matches;
2085 		cdm->num_matches++;
2086 		cdm->matches[j].type = DEV_MATCH_BUS;
2087 		cdm->matches[j].result.bus_result.path_id = bus->path_id;
2088 		cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
2089 		cdm->matches[j].result.bus_result.unit_number =
2090 			bus->sim->unit_number;
2091 		strncpy(cdm->matches[j].result.bus_result.dev_name,
2092 			bus->sim->sim_name, DEV_IDLEN);
2093 	}
2094 
2095 	/*
2096 	 * If the user is only interested in busses, there's no
2097 	 * reason to descend to the next level in the tree.
2098 	 */
2099 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2100 		return(1);
2101 
2102 	/*
2103 	 * If there is a target generation recorded, check it to
2104 	 * make sure the target list hasn't changed.
2105 	 */
2106 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2107 	 && (bus == cdm->pos.cookie.bus)
2108 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2109 	 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
2110 	 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
2111 	     bus->generation)) {
2112 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2113 		return(0);
2114 	}
2115 
2116 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2117 	 && (cdm->pos.cookie.bus == bus)
2118 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2119 	 && (cdm->pos.cookie.target != NULL))
2120 		return(xpttargettraverse(bus,
2121 					(struct cam_et *)cdm->pos.cookie.target,
2122 					 xptedttargetfunc, arg));
2123 	else
2124 		return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
2125 }
2126 
2127 static int
2128 xptedttargetfunc(struct cam_et *target, void *arg)
2129 {
2130 	struct ccb_dev_match *cdm;
2131 
2132 	cdm = (struct ccb_dev_match *)arg;
2133 
2134 	/*
2135 	 * If there is a device list generation recorded, check it to
2136 	 * make sure the device list hasn't changed.
2137 	 */
2138 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2139 	 && (cdm->pos.cookie.bus == target->bus)
2140 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2141 	 && (cdm->pos.cookie.target == target)
2142 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2143 	 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
2144 	 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
2145 	     target->generation)) {
2146 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2147 		return(0);
2148 	}
2149 
2150 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2151 	 && (cdm->pos.cookie.bus == target->bus)
2152 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2153 	 && (cdm->pos.cookie.target == target)
2154 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2155 	 && (cdm->pos.cookie.device != NULL))
2156 		return(xptdevicetraverse(target,
2157 					(struct cam_ed *)cdm->pos.cookie.device,
2158 					 xptedtdevicefunc, arg));
2159 	else
2160 		return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2161 }
2162 
2163 static int
2164 xptedtdevicefunc(struct cam_ed *device, void *arg)
2165 {
2166 
2167 	struct ccb_dev_match *cdm;
2168 	dev_match_ret retval;
2169 
2170 	cdm = (struct ccb_dev_match *)arg;
2171 
2172 	/*
2173 	 * If our position is for something deeper in the tree, that means
2174 	 * that we've already seen this node.  So, we keep going down.
2175 	 */
2176 	if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2177 	 && (cdm->pos.cookie.device == device)
2178 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2179 	 && (cdm->pos.cookie.periph != NULL))
2180 		retval = DM_RET_DESCEND;
2181 	else
2182 		retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2183 					device);
2184 
2185 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2186 		cdm->status = CAM_DEV_MATCH_ERROR;
2187 		return(0);
2188 	}
2189 
2190 	/*
2191 	 * If the copy flag is set, copy this device out.
2192 	 */
2193 	if (retval & DM_RET_COPY) {
2194 		int spaceleft, j;
2195 
2196 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
2197 			sizeof(struct dev_match_result));
2198 
2199 		/*
2200 		 * If we don't have enough space to put in another
2201 		 * match result, save our position and tell the
2202 		 * user there are more devices to check.
2203 		 */
2204 		if (spaceleft < sizeof(struct dev_match_result)) {
2205 			bzero(&cdm->pos, sizeof(cdm->pos));
2206 			cdm->pos.position_type =
2207 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2208 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2209 
2210 			cdm->pos.cookie.bus = device->target->bus;
2211 			cdm->pos.generations[CAM_BUS_GENERATION]=
2212 				bus_generation;
2213 			cdm->pos.cookie.target = device->target;
2214 			cdm->pos.generations[CAM_TARGET_GENERATION] =
2215 				device->target->bus->generation;
2216 			cdm->pos.cookie.device = device;
2217 			cdm->pos.generations[CAM_DEV_GENERATION] =
2218 				device->target->generation;
2219 			cdm->status = CAM_DEV_MATCH_MORE;
2220 			return(0);
2221 		}
2222 		j = cdm->num_matches;
2223 		cdm->num_matches++;
2224 		cdm->matches[j].type = DEV_MATCH_DEVICE;
2225 		cdm->matches[j].result.device_result.path_id =
2226 			device->target->bus->path_id;
2227 		cdm->matches[j].result.device_result.target_id =
2228 			device->target->target_id;
2229 		cdm->matches[j].result.device_result.target_lun =
2230 			device->lun_id;
2231 		bcopy(&device->inq_data,
2232 		      &cdm->matches[j].result.device_result.inq_data,
2233 		      sizeof(struct scsi_inquiry_data));
2234 
2235 		/* Let the user know whether this device is unconfigured */
2236 		if (device->flags & CAM_DEV_UNCONFIGURED)
2237 			cdm->matches[j].result.device_result.flags =
2238 				DEV_RESULT_UNCONFIGURED;
2239 		else
2240 			cdm->matches[j].result.device_result.flags =
2241 				DEV_RESULT_NOFLAG;
2242 	}
2243 
2244 	/*
2245 	 * If the user isn't interested in peripherals, don't descend
2246 	 * the tree any further.
2247 	 */
2248 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2249 		return(1);
2250 
2251 	/*
2252 	 * If there is a peripheral list generation recorded, make sure
2253 	 * it hasn't changed.
2254 	 */
2255 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2256 	 && (device->target->bus == cdm->pos.cookie.bus)
2257 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2258 	 && (device->target == cdm->pos.cookie.target)
2259 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2260 	 && (device == cdm->pos.cookie.device)
2261 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2262 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2263 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2264 	     device->generation)){
2265 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2266 		return(0);
2267 	}
2268 
2269 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2270 	 && (cdm->pos.cookie.bus == device->target->bus)
2271 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2272 	 && (cdm->pos.cookie.target == device->target)
2273 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2274 	 && (cdm->pos.cookie.device == device)
2275 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2276 	 && (cdm->pos.cookie.periph != NULL))
2277 		return(xptperiphtraverse(device,
2278 				(struct cam_periph *)cdm->pos.cookie.periph,
2279 				xptedtperiphfunc, arg));
2280 	else
2281 		return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2282 }
2283 
2284 static int
2285 xptedtperiphfunc(struct cam_periph *periph, void *arg)
2286 {
2287 	struct ccb_dev_match *cdm;
2288 	dev_match_ret retval;
2289 
2290 	cdm = (struct ccb_dev_match *)arg;
2291 
2292 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2293 
2294 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2295 		cdm->status = CAM_DEV_MATCH_ERROR;
2296 		return(0);
2297 	}
2298 
2299 	/*
2300 	 * If the copy flag is set, copy this peripheral out.
2301 	 */
2302 	if (retval & DM_RET_COPY) {
2303 		int spaceleft, j;
2304 
2305 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
2306 			sizeof(struct dev_match_result));
2307 
2308 		/*
2309 		 * If we don't have enough space to put in another
2310 		 * match result, save our position and tell the
2311 		 * user there are more devices to check.
2312 		 */
2313 		if (spaceleft < sizeof(struct dev_match_result)) {
2314 			bzero(&cdm->pos, sizeof(cdm->pos));
2315 			cdm->pos.position_type =
2316 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2317 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2318 				CAM_DEV_POS_PERIPH;
2319 
2320 			cdm->pos.cookie.bus = periph->path->bus;
2321 			cdm->pos.generations[CAM_BUS_GENERATION]=
2322 				bus_generation;
2323 			cdm->pos.cookie.target = periph->path->target;
2324 			cdm->pos.generations[CAM_TARGET_GENERATION] =
2325 				periph->path->bus->generation;
2326 			cdm->pos.cookie.device = periph->path->device;
2327 			cdm->pos.generations[CAM_DEV_GENERATION] =
2328 				periph->path->target->generation;
2329 			cdm->pos.cookie.periph = periph;
2330 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
2331 				periph->path->device->generation;
2332 			cdm->status = CAM_DEV_MATCH_MORE;
2333 			return(0);
2334 		}
2335 
2336 		j = cdm->num_matches;
2337 		cdm->num_matches++;
2338 		cdm->matches[j].type = DEV_MATCH_PERIPH;
2339 		cdm->matches[j].result.periph_result.path_id =
2340 			periph->path->bus->path_id;
2341 		cdm->matches[j].result.periph_result.target_id =
2342 			periph->path->target->target_id;
2343 		cdm->matches[j].result.periph_result.target_lun =
2344 			periph->path->device->lun_id;
2345 		cdm->matches[j].result.periph_result.unit_number =
2346 			periph->unit_number;
2347 		strncpy(cdm->matches[j].result.periph_result.periph_name,
2348 			periph->periph_name, DEV_IDLEN);
2349 	}
2350 
2351 	return(1);
2352 }
2353 
2354 static int
2355 xptedtmatch(struct ccb_dev_match *cdm)
2356 {
2357 	int ret;
2358 
2359 	cdm->num_matches = 0;
2360 
2361 	/*
2362 	 * Check the bus list generation.  If it has changed, the user
2363 	 * needs to reset everything and start over.
2364 	 */
2365 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2366 	 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2367 	 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
2368 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2369 		return(0);
2370 	}
2371 
2372 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2373 	 && (cdm->pos.cookie.bus != NULL))
2374 		ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2375 				     xptedtbusfunc, cdm);
2376 	else
2377 		ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2378 
2379 	/*
2380 	 * If we get back 0, that means that we had to stop before fully
2381 	 * traversing the EDT.  It also means that one of the subroutines
2382 	 * has set the status field to the proper value.  If we get back 1,
2383 	 * we've fully traversed the EDT and copied out any matching entries.
2384 	 */
2385 	if (ret == 1)
2386 		cdm->status = CAM_DEV_MATCH_LAST;
2387 
2388 	return(ret);
2389 }
2390 
2391 static int
2392 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2393 {
2394 	struct ccb_dev_match *cdm;
2395 
2396 	cdm = (struct ccb_dev_match *)arg;
2397 
2398 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2399 	 && (cdm->pos.cookie.pdrv == pdrv)
2400 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2401 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2402 	 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2403 	     (*pdrv)->generation)) {
2404 		cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2405 		return(0);
2406 	}
2407 
2408 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2409 	 && (cdm->pos.cookie.pdrv == pdrv)
2410 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2411 	 && (cdm->pos.cookie.periph != NULL))
2412 		return(xptpdperiphtraverse(pdrv,
2413 				(struct cam_periph *)cdm->pos.cookie.periph,
2414 				xptplistperiphfunc, arg));
2415 	else
2416 		return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2417 }
2418 
2419 static int
2420 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2421 {
2422 	struct ccb_dev_match *cdm;
2423 	dev_match_ret retval;
2424 
2425 	cdm = (struct ccb_dev_match *)arg;
2426 
2427 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2428 
2429 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2430 		cdm->status = CAM_DEV_MATCH_ERROR;
2431 		return(0);
2432 	}
2433 
2434 	/*
2435 	 * If the copy flag is set, copy this peripheral out.
2436 	 */
2437 	if (retval & DM_RET_COPY) {
2438 		int spaceleft, j;
2439 
2440 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
2441 			sizeof(struct dev_match_result));
2442 
2443 		/*
2444 		 * If we don't have enough space to put in another
2445 		 * match result, save our position and tell the
2446 		 * user there are more devices to check.
2447 		 */
2448 		if (spaceleft < sizeof(struct dev_match_result)) {
2449 			struct periph_driver **pdrv;
2450 
2451 			pdrv = NULL;
2452 			bzero(&cdm->pos, sizeof(cdm->pos));
2453 			cdm->pos.position_type =
2454 				CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2455 				CAM_DEV_POS_PERIPH;
2456 
2457 			/*
2458 			 * This may look a bit non-sensical, but it is
2459 			 * actually quite logical.  There are very few
2460 			 * peripheral drivers, and bloating every peripheral
2461 			 * structure with a pointer back to its parent
2462 			 * peripheral driver linker set entry would cost
2463 			 * more in the long run than doing this quick lookup.
2464 			 */
2465 			for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2466 				if (strcmp((*pdrv)->driver_name,
2467 				    periph->periph_name) == 0)
2468 					break;
2469 			}
2470 
2471 			if (pdrv == NULL) {
2472 				cdm->status = CAM_DEV_MATCH_ERROR;
2473 				return(0);
2474 			}
2475 
2476 			cdm->pos.cookie.pdrv = pdrv;
2477 			/*
2478 			 * The periph generation slot does double duty, as
2479 			 * does the periph pointer slot.  They are used for
2480 			 * both edt and pdrv lookups and positioning.
2481 			 */
2482 			cdm->pos.cookie.periph = periph;
2483 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
2484 				(*pdrv)->generation;
2485 			cdm->status = CAM_DEV_MATCH_MORE;
2486 			return(0);
2487 		}
2488 
2489 		j = cdm->num_matches;
2490 		cdm->num_matches++;
2491 		cdm->matches[j].type = DEV_MATCH_PERIPH;
2492 		cdm->matches[j].result.periph_result.path_id =
2493 			periph->path->bus->path_id;
2494 
2495 		/*
2496 		 * The transport layer peripheral doesn't have a target or
2497 		 * lun.
2498 		 */
2499 		if (periph->path->target)
2500 			cdm->matches[j].result.periph_result.target_id =
2501 				periph->path->target->target_id;
2502 		else
2503 			cdm->matches[j].result.periph_result.target_id = -1;
2504 
2505 		if (periph->path->device)
2506 			cdm->matches[j].result.periph_result.target_lun =
2507 				periph->path->device->lun_id;
2508 		else
2509 			cdm->matches[j].result.periph_result.target_lun = -1;
2510 
2511 		cdm->matches[j].result.periph_result.unit_number =
2512 			periph->unit_number;
2513 		strncpy(cdm->matches[j].result.periph_result.periph_name,
2514 			periph->periph_name, DEV_IDLEN);
2515 	}
2516 
2517 	return(1);
2518 }
2519 
2520 static int
2521 xptperiphlistmatch(struct ccb_dev_match *cdm)
2522 {
2523 	int ret;
2524 
2525 	cdm->num_matches = 0;
2526 
2527 	/*
2528 	 * At this point in the edt traversal function, we check the bus
2529 	 * list generation to make sure that no busses have been added or
2530 	 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2531 	 * For the peripheral driver list traversal function, however, we
2532 	 * don't have to worry about new peripheral driver types coming or
2533 	 * going; they're in a linker set, and therefore can't change
2534 	 * without a recompile.
2535 	 */
2536 
2537 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2538 	 && (cdm->pos.cookie.pdrv != NULL))
2539 		ret = xptpdrvtraverse(
2540 				(struct periph_driver **)cdm->pos.cookie.pdrv,
2541 				xptplistpdrvfunc, cdm);
2542 	else
2543 		ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2544 
2545 	/*
2546 	 * If we get back 0, that means that we had to stop before fully
2547 	 * traversing the peripheral driver tree.  It also means that one of
2548 	 * the subroutines has set the status field to the proper value.  If
2549 	 * we get back 1, we've fully traversed the EDT and copied out any
2550 	 * matching entries.
2551 	 */
2552 	if (ret == 1)
2553 		cdm->status = CAM_DEV_MATCH_LAST;
2554 
2555 	return(ret);
2556 }
2557 
2558 static int
2559 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2560 {
2561 	struct cam_eb *bus, *next_bus;
2562 	int retval;
2563 
2564 	retval = 1;
2565 
2566 	for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
2567 	     bus != NULL;
2568 	     bus = next_bus) {
2569 		next_bus = TAILQ_NEXT(bus, links);
2570 
2571 		retval = tr_func(bus, arg);
2572 		if (retval == 0)
2573 			return(retval);
2574 	}
2575 
2576 	return(retval);
2577 }
2578 
2579 static int
2580 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2581 		  xpt_targetfunc_t *tr_func, void *arg)
2582 {
2583 	struct cam_et *target, *next_target;
2584 	int retval;
2585 
2586 	retval = 1;
2587 	for (target = (start_target ? start_target :
2588 		       TAILQ_FIRST(&bus->et_entries));
2589 	     target != NULL; target = next_target) {
2590 
2591 		next_target = TAILQ_NEXT(target, links);
2592 
2593 		retval = tr_func(target, arg);
2594 
2595 		if (retval == 0)
2596 			return(retval);
2597 	}
2598 
2599 	return(retval);
2600 }
2601 
2602 static int
2603 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2604 		  xpt_devicefunc_t *tr_func, void *arg)
2605 {
2606 	struct cam_ed *device, *next_device;
2607 	int retval;
2608 
2609 	retval = 1;
2610 	for (device = (start_device ? start_device :
2611 		       TAILQ_FIRST(&target->ed_entries));
2612 	     device != NULL;
2613 	     device = next_device) {
2614 
2615 		next_device = TAILQ_NEXT(device, links);
2616 
2617 		retval = tr_func(device, arg);
2618 
2619 		if (retval == 0)
2620 			return(retval);
2621 	}
2622 
2623 	return(retval);
2624 }
2625 
2626 static int
2627 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2628 		  xpt_periphfunc_t *tr_func, void *arg)
2629 {
2630 	struct cam_periph *periph, *next_periph;
2631 	int retval;
2632 
2633 	retval = 1;
2634 
2635 	for (periph = (start_periph ? start_periph :
2636 		       SLIST_FIRST(&device->periphs));
2637 	     periph != NULL;
2638 	     periph = next_periph) {
2639 
2640 		next_periph = SLIST_NEXT(periph, periph_links);
2641 
2642 		retval = tr_func(periph, arg);
2643 		if (retval == 0)
2644 			return(retval);
2645 	}
2646 
2647 	return(retval);
2648 }
2649 
2650 static int
2651 xptpdrvtraverse(struct periph_driver **start_pdrv,
2652 		xpt_pdrvfunc_t *tr_func, void *arg)
2653 {
2654 	struct periph_driver **pdrv;
2655 	int retval;
2656 
2657 	retval = 1;
2658 
2659 	/*
2660 	 * We don't traverse the peripheral driver list like we do the
2661 	 * other lists, because it is a linker set, and therefore cannot be
2662 	 * changed during runtime.  If the peripheral driver list is ever
2663 	 * re-done to be something other than a linker set (i.e. it can
2664 	 * change while the system is running), the list traversal should
2665 	 * be modified to work like the other traversal functions.
2666 	 */
2667 	for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2668 	     *pdrv != NULL; pdrv++) {
2669 		retval = tr_func(pdrv, arg);
2670 
2671 		if (retval == 0)
2672 			return(retval);
2673 	}
2674 
2675 	return(retval);
2676 }
2677 
2678 static int
2679 xptpdperiphtraverse(struct periph_driver **pdrv,
2680 		    struct cam_periph *start_periph,
2681 		    xpt_periphfunc_t *tr_func, void *arg)
2682 {
2683 	struct cam_periph *periph, *next_periph;
2684 	int retval;
2685 
2686 	retval = 1;
2687 
2688 	for (periph = (start_periph ? start_periph :
2689 	     TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2690 	     periph = next_periph) {
2691 
2692 		next_periph = TAILQ_NEXT(periph, unit_links);
2693 
2694 		retval = tr_func(periph, arg);
2695 		if (retval == 0)
2696 			return(retval);
2697 	}
2698 	return(retval);
2699 }
2700 
2701 static int
2702 xptdefbusfunc(struct cam_eb *bus, void *arg)
2703 {
2704 	struct xpt_traverse_config *tr_config;
2705 
2706 	tr_config = (struct xpt_traverse_config *)arg;
2707 
2708 	if (tr_config->depth == XPT_DEPTH_BUS) {
2709 		xpt_busfunc_t *tr_func;
2710 
2711 		tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2712 
2713 		return(tr_func(bus, tr_config->tr_arg));
2714 	} else
2715 		return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2716 }
2717 
2718 static int
2719 xptdeftargetfunc(struct cam_et *target, void *arg)
2720 {
2721 	struct xpt_traverse_config *tr_config;
2722 
2723 	tr_config = (struct xpt_traverse_config *)arg;
2724 
2725 	if (tr_config->depth == XPT_DEPTH_TARGET) {
2726 		xpt_targetfunc_t *tr_func;
2727 
2728 		tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2729 
2730 		return(tr_func(target, tr_config->tr_arg));
2731 	} else
2732 		return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2733 }
2734 
2735 static int
2736 xptdefdevicefunc(struct cam_ed *device, void *arg)
2737 {
2738 	struct xpt_traverse_config *tr_config;
2739 
2740 	tr_config = (struct xpt_traverse_config *)arg;
2741 
2742 	if (tr_config->depth == XPT_DEPTH_DEVICE) {
2743 		xpt_devicefunc_t *tr_func;
2744 
2745 		tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2746 
2747 		return(tr_func(device, tr_config->tr_arg));
2748 	} else
2749 		return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2750 }
2751 
2752 static int
2753 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2754 {
2755 	struct xpt_traverse_config *tr_config;
2756 	xpt_periphfunc_t *tr_func;
2757 
2758 	tr_config = (struct xpt_traverse_config *)arg;
2759 
2760 	tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2761 
2762 	/*
2763 	 * Unlike the other default functions, we don't check for depth
2764 	 * here.  The peripheral driver level is the last level in the EDT,
2765 	 * so if we're here, we should execute the function in question.
2766 	 */
2767 	return(tr_func(periph, tr_config->tr_arg));
2768 }
2769 
2770 /*
2771  * Execute the given function for every bus in the EDT.
2772  */
2773 static int
2774 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2775 {
2776 	struct xpt_traverse_config tr_config;
2777 
2778 	tr_config.depth = XPT_DEPTH_BUS;
2779 	tr_config.tr_func = tr_func;
2780 	tr_config.tr_arg = arg;
2781 
2782 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2783 }
2784 
2785 #ifdef notusedyet
2786 /*
2787  * Execute the given function for every target in the EDT.
2788  */
2789 static int
2790 xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
2791 {
2792 	struct xpt_traverse_config tr_config;
2793 
2794 	tr_config.depth = XPT_DEPTH_TARGET;
2795 	tr_config.tr_func = tr_func;
2796 	tr_config.tr_arg = arg;
2797 
2798 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2799 }
2800 #endif /* notusedyet */
2801 
2802 /*
2803  * Execute the given function for every device in the EDT.
2804  */
2805 static int
2806 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2807 {
2808 	struct xpt_traverse_config tr_config;
2809 
2810 	tr_config.depth = XPT_DEPTH_DEVICE;
2811 	tr_config.tr_func = tr_func;
2812 	tr_config.tr_arg = arg;
2813 
2814 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2815 }
2816 
2817 #ifdef notusedyet
2818 /*
2819  * Execute the given function for every peripheral in the EDT.
2820  */
2821 static int
2822 xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
2823 {
2824 	struct xpt_traverse_config tr_config;
2825 
2826 	tr_config.depth = XPT_DEPTH_PERIPH;
2827 	tr_config.tr_func = tr_func;
2828 	tr_config.tr_arg = arg;
2829 
2830 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2831 }
2832 #endif /* notusedyet */
2833 
2834 static int
2835 xptsetasyncfunc(struct cam_ed *device, void *arg)
2836 {
2837 	struct cam_path path;
2838 	struct ccb_getdev cgd;
2839 	struct async_node *cur_entry;
2840 
2841 	cur_entry = (struct async_node *)arg;
2842 
2843 	/*
2844 	 * Don't report unconfigured devices (Wildcard devs,
2845 	 * devices only for target mode, device instances
2846 	 * that have been invalidated but are waiting for
2847 	 * their last reference count to be released).
2848 	 */
2849 	if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2850 		return (1);
2851 
2852 	xpt_compile_path(&path,
2853 			 NULL,
2854 			 device->target->bus->path_id,
2855 			 device->target->target_id,
2856 			 device->lun_id);
2857 	xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2858 	cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2859 	xpt_action((union ccb *)&cgd);
2860 	cur_entry->callback(cur_entry->callback_arg,
2861 			    AC_FOUND_DEVICE,
2862 			    &path, &cgd);
2863 	xpt_release_path(&path);
2864 
2865 	return(1);
2866 }
2867 
2868 static int
2869 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2870 {
2871 	struct cam_path path;
2872 	struct ccb_pathinq cpi;
2873 	struct async_node *cur_entry;
2874 
2875 	cur_entry = (struct async_node *)arg;
2876 
2877 	xpt_compile_path(&path, /*periph*/NULL,
2878 			 bus->sim->path_id,
2879 			 CAM_TARGET_WILDCARD,
2880 			 CAM_LUN_WILDCARD);
2881 	xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2882 	cpi.ccb_h.func_code = XPT_PATH_INQ;
2883 	xpt_action((union ccb *)&cpi);
2884 	cur_entry->callback(cur_entry->callback_arg,
2885 			    AC_PATH_REGISTERED,
2886 			    &path, &cpi);
2887 	xpt_release_path(&path);
2888 
2889 	return(1);
2890 }
2891 
2892 void
2893 xpt_action(union ccb *start_ccb)
2894 {
2895 	int iopl;
2896 
2897 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2898 
2899 	start_ccb->ccb_h.status = CAM_REQ_INPROG;
2900 
2901 	iopl = splsoftcam();
2902 	switch (start_ccb->ccb_h.func_code) {
2903 	case XPT_SCSI_IO:
2904 	{
2905 #ifdef CAM_NEW_TRAN_CODE
2906 		struct cam_ed *device;
2907 #endif /* CAM_NEW_TRAN_CODE */
2908 #ifdef CAMDEBUG
2909 		char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2910 		struct cam_path *path;
2911 
2912 		path = start_ccb->ccb_h.path;
2913 #endif
2914 
2915 		/*
2916 		 * For the sake of compatibility with SCSI-1
2917 		 * devices that may not understand the identify
2918 		 * message, we include lun information in the
2919 		 * second byte of all commands.  SCSI-1 specifies
2920 		 * that luns are a 3 bit value and reserves only 3
2921 		 * bits for lun information in the CDB.  Later
2922 		 * revisions of the SCSI spec allow for more than 8
2923 		 * luns, but have deprecated lun information in the
2924 		 * CDB.  So, if the lun won't fit, we must omit.
2925 		 *
2926 		 * Also be aware that during initial probing for devices,
2927 		 * the inquiry information is unknown but initialized to 0.
2928 		 * This means that this code will be exercised while probing
2929 		 * devices with an ANSI revision greater than 2.
2930 		 */
2931 #ifdef CAM_NEW_TRAN_CODE
2932 		device = start_ccb->ccb_h.path->device;
2933 		if (device->protocol_version <= SCSI_REV_2
2934 #else /* CAM_NEW_TRAN_CODE */
2935 		if (SID_ANSI_REV(&start_ccb->ccb_h.path->device->inq_data) <= 2
2936 #endif /* CAM_NEW_TRAN_CODE */
2937 		 && start_ccb->ccb_h.target_lun < 8
2938 		 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2939 
2940 			start_ccb->csio.cdb_io.cdb_bytes[1] |=
2941 			    start_ccb->ccb_h.target_lun << 5;
2942 		}
2943 		start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2944 		CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2945 			  scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2946 			  	       &path->device->inq_data),
2947 			  scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2948 					  cdb_str, sizeof(cdb_str))));
2949 		/* FALLTHROUGH */
2950 	}
2951 	case XPT_TARGET_IO:
2952 	case XPT_CONT_TARGET_IO:
2953 		start_ccb->csio.sense_resid = 0;
2954 		start_ccb->csio.resid = 0;
2955 		/* FALLTHROUGH */
2956 	case XPT_RESET_DEV:
2957 	case XPT_ENG_EXEC:
2958 	{
2959 		struct cam_path *path;
2960 		int s;
2961 		int runq;
2962 
2963 		path = start_ccb->ccb_h.path;
2964 		s = splsoftcam();
2965 
2966 		cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2967 		if (path->device->qfrozen_cnt == 0)
2968 			runq = xpt_schedule_dev_sendq(path->bus, path->device);
2969 		else
2970 			runq = 0;
2971 		splx(s);
2972 		if (runq != 0)
2973 			xpt_run_dev_sendq(path->bus);
2974 		break;
2975 	}
2976 	case XPT_SET_TRAN_SETTINGS:
2977 	{
2978 		xpt_set_transfer_settings(&start_ccb->cts,
2979 					  start_ccb->ccb_h.path->device,
2980 					  /*async_update*/FALSE);
2981 		break;
2982 	}
2983 	case XPT_CALC_GEOMETRY:
2984 	{
2985 		struct cam_sim *sim;
2986 
2987 		/* Filter out garbage */
2988 		if (start_ccb->ccg.block_size == 0
2989 		 || start_ccb->ccg.volume_size == 0) {
2990 			start_ccb->ccg.cylinders = 0;
2991 			start_ccb->ccg.heads = 0;
2992 			start_ccb->ccg.secs_per_track = 0;
2993 			start_ccb->ccb_h.status = CAM_REQ_CMP;
2994 			break;
2995 		}
2996 #ifdef PC98
2997 		/*
2998 		 * In a PC-98 system, geometry translation depens on
2999 		 * the "real" device geometry obtained from mode page 4.
3000 		 * SCSI geometry translation is performed in the
3001 		 * initialization routine of the SCSI BIOS and the result
3002 		 * stored in host memory.  If the translation is available
3003 		 * in host memory, use it.  If not, rely on the default
3004 		 * translation the device driver performs.
3005 		 */
3006 		if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
3007 			start_ccb->ccb_h.status = CAM_REQ_CMP;
3008 			break;
3009 		}
3010 #endif
3011 		sim = start_ccb->ccb_h.path->bus->sim;
3012 		(*(sim->sim_action))(sim, start_ccb);
3013 		break;
3014 	}
3015 	case XPT_ABORT:
3016 	{
3017 		union ccb* abort_ccb;
3018 		int s;
3019 
3020 		abort_ccb = start_ccb->cab.abort_ccb;
3021 		if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
3022 
3023 			if (abort_ccb->ccb_h.pinfo.index >= 0) {
3024 				struct cam_ccbq *ccbq;
3025 
3026 				ccbq = &abort_ccb->ccb_h.path->device->ccbq;
3027 				cam_ccbq_remove_ccb(ccbq, abort_ccb);
3028 				abort_ccb->ccb_h.status =
3029 				    CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3030 				xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3031 				s = splcam();
3032 				xpt_done(abort_ccb);
3033 				splx(s);
3034 				start_ccb->ccb_h.status = CAM_REQ_CMP;
3035 				break;
3036 			}
3037 			if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
3038 			 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
3039 				/*
3040 				 * We've caught this ccb en route to
3041 				 * the SIM.  Flag it for abort and the
3042 				 * SIM will do so just before starting
3043 				 * real work on the CCB.
3044 				 */
3045 				abort_ccb->ccb_h.status =
3046 				    CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3047 				xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3048 				start_ccb->ccb_h.status = CAM_REQ_CMP;
3049 				break;
3050 			}
3051 		}
3052 		if (XPT_FC_IS_QUEUED(abort_ccb)
3053 		 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
3054 			/*
3055 			 * It's already completed but waiting
3056 			 * for our SWI to get to it.
3057 			 */
3058 			start_ccb->ccb_h.status = CAM_UA_ABORT;
3059 			break;
3060 		}
3061 		/*
3062 		 * If we weren't able to take care of the abort request
3063 		 * in the XPT, pass the request down to the SIM for processing.
3064 		 */
3065 		/* FALLTHROUGH */
3066 	}
3067 	case XPT_ACCEPT_TARGET_IO:
3068 	case XPT_EN_LUN:
3069 	case XPT_IMMED_NOTIFY:
3070 	case XPT_NOTIFY_ACK:
3071 	case XPT_GET_TRAN_SETTINGS:
3072 	case XPT_RESET_BUS:
3073 	{
3074 		struct cam_sim *sim;
3075 
3076 		sim = start_ccb->ccb_h.path->bus->sim;
3077 		(*(sim->sim_action))(sim, start_ccb);
3078 		break;
3079 	}
3080 	case XPT_PATH_INQ:
3081 	{
3082 		struct cam_sim *sim;
3083 
3084 		sim = start_ccb->ccb_h.path->bus->sim;
3085 		(*(sim->sim_action))(sim, start_ccb);
3086 		break;
3087 	}
3088 	case XPT_PATH_STATS:
3089 		start_ccb->cpis.last_reset =
3090 			start_ccb->ccb_h.path->bus->last_reset;
3091 		start_ccb->ccb_h.status = CAM_REQ_CMP;
3092 		break;
3093 	case XPT_GDEV_TYPE:
3094 	{
3095 		struct cam_ed *dev;
3096 		int s;
3097 
3098 		dev = start_ccb->ccb_h.path->device;
3099 		s = splcam();
3100 		if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3101 			start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3102 		} else {
3103 			struct ccb_getdev *cgd;
3104 			struct cam_eb *bus;
3105 			struct cam_et *tar;
3106 
3107 			cgd = &start_ccb->cgd;
3108 			bus = cgd->ccb_h.path->bus;
3109 			tar = cgd->ccb_h.path->target;
3110 			cgd->inq_data = dev->inq_data;
3111 			cgd->ccb_h.status = CAM_REQ_CMP;
3112 			cgd->serial_num_len = dev->serial_num_len;
3113 			if ((dev->serial_num_len > 0)
3114 			 && (dev->serial_num != NULL))
3115 				bcopy(dev->serial_num, cgd->serial_num,
3116 				      dev->serial_num_len);
3117 		}
3118 		splx(s);
3119 		break;
3120 	}
3121 	case XPT_GDEV_STATS:
3122 	{
3123 		struct cam_ed *dev;
3124 		int s;
3125 
3126 		dev = start_ccb->ccb_h.path->device;
3127 		s = splcam();
3128 		if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3129 			start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3130 		} else {
3131 			struct ccb_getdevstats *cgds;
3132 			struct cam_eb *bus;
3133 			struct cam_et *tar;
3134 
3135 			cgds = &start_ccb->cgds;
3136 			bus = cgds->ccb_h.path->bus;
3137 			tar = cgds->ccb_h.path->target;
3138 			cgds->dev_openings = dev->ccbq.dev_openings;
3139 			cgds->dev_active = dev->ccbq.dev_active;
3140 			cgds->devq_openings = dev->ccbq.devq_openings;
3141 			cgds->devq_queued = dev->ccbq.queue.entries;
3142 			cgds->held = dev->ccbq.held;
3143 			cgds->last_reset = tar->last_reset;
3144 			cgds->maxtags = dev->quirk->maxtags;
3145 			cgds->mintags = dev->quirk->mintags;
3146 			if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
3147 				cgds->last_reset = bus->last_reset;
3148 			cgds->ccb_h.status = CAM_REQ_CMP;
3149 		}
3150 		splx(s);
3151 		break;
3152 	}
3153 	case XPT_GDEVLIST:
3154 	{
3155 		struct cam_periph	*nperiph;
3156 		struct periph_list	*periph_head;
3157 		struct ccb_getdevlist	*cgdl;
3158 		u_int			i;
3159 		int			s;
3160 		struct cam_ed		*device;
3161 		int			found;
3162 
3163 
3164 		found = 0;
3165 
3166 		/*
3167 		 * Don't want anyone mucking with our data.
3168 		 */
3169 		s = splcam();
3170 		device = start_ccb->ccb_h.path->device;
3171 		periph_head = &device->periphs;
3172 		cgdl = &start_ccb->cgdl;
3173 
3174 		/*
3175 		 * Check and see if the list has changed since the user
3176 		 * last requested a list member.  If so, tell them that the
3177 		 * list has changed, and therefore they need to start over
3178 		 * from the beginning.
3179 		 */
3180 		if ((cgdl->index != 0) &&
3181 		    (cgdl->generation != device->generation)) {
3182 			cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3183 			splx(s);
3184 			break;
3185 		}
3186 
3187 		/*
3188 		 * Traverse the list of peripherals and attempt to find
3189 		 * the requested peripheral.
3190 		 */
3191 		for (nperiph = SLIST_FIRST(periph_head), i = 0;
3192 		     (nperiph != NULL) && (i <= cgdl->index);
3193 		     nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
3194 			if (i == cgdl->index) {
3195 				strncpy(cgdl->periph_name,
3196 					nperiph->periph_name,
3197 					DEV_IDLEN);
3198 				cgdl->unit_number = nperiph->unit_number;
3199 				found = 1;
3200 			}
3201 		}
3202 		if (found == 0) {
3203 			cgdl->status = CAM_GDEVLIST_ERROR;
3204 			splx(s);
3205 			break;
3206 		}
3207 
3208 		if (nperiph == NULL)
3209 			cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3210 		else
3211 			cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3212 
3213 		cgdl->index++;
3214 		cgdl->generation = device->generation;
3215 
3216 		splx(s);
3217 		cgdl->ccb_h.status = CAM_REQ_CMP;
3218 		break;
3219 	}
3220 	case XPT_DEV_MATCH:
3221 	{
3222 		int s;
3223 		dev_pos_type position_type;
3224 		struct ccb_dev_match *cdm;
3225 		int ret;
3226 
3227 		cdm = &start_ccb->cdm;
3228 
3229 		/*
3230 		 * Prevent EDT changes while we traverse it.
3231 		 */
3232 		s = splcam();
3233 		/*
3234 		 * There are two ways of getting at information in the EDT.
3235 		 * The first way is via the primary EDT tree.  It starts
3236 		 * with a list of busses, then a list of targets on a bus,
3237 		 * then devices/luns on a target, and then peripherals on a
3238 		 * device/lun.  The "other" way is by the peripheral driver
3239 		 * lists.  The peripheral driver lists are organized by
3240 		 * peripheral driver.  (obviously)  So it makes sense to
3241 		 * use the peripheral driver list if the user is looking
3242 		 * for something like "da1", or all "da" devices.  If the
3243 		 * user is looking for something on a particular bus/target
3244 		 * or lun, it's generally better to go through the EDT tree.
3245 		 */
3246 
3247 		if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3248 			position_type = cdm->pos.position_type;
3249 		else {
3250 			u_int i;
3251 
3252 			position_type = CAM_DEV_POS_NONE;
3253 
3254 			for (i = 0; i < cdm->num_patterns; i++) {
3255 				if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3256 				 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3257 					position_type = CAM_DEV_POS_EDT;
3258 					break;
3259 				}
3260 			}
3261 
3262 			if (cdm->num_patterns == 0)
3263 				position_type = CAM_DEV_POS_EDT;
3264 			else if (position_type == CAM_DEV_POS_NONE)
3265 				position_type = CAM_DEV_POS_PDRV;
3266 		}
3267 
3268 		switch(position_type & CAM_DEV_POS_TYPEMASK) {
3269 		case CAM_DEV_POS_EDT:
3270 			ret = xptedtmatch(cdm);
3271 			break;
3272 		case CAM_DEV_POS_PDRV:
3273 			ret = xptperiphlistmatch(cdm);
3274 			break;
3275 		default:
3276 			cdm->status = CAM_DEV_MATCH_ERROR;
3277 			break;
3278 		}
3279 
3280 		splx(s);
3281 
3282 		if (cdm->status == CAM_DEV_MATCH_ERROR)
3283 			start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3284 		else
3285 			start_ccb->ccb_h.status = CAM_REQ_CMP;
3286 
3287 		break;
3288 	}
3289 	case XPT_SASYNC_CB:
3290 	{
3291 		struct ccb_setasync *csa;
3292 		struct async_node *cur_entry;
3293 		struct async_list *async_head;
3294 		u_int32_t added;
3295 		int s;
3296 
3297 		csa = &start_ccb->csa;
3298 		added = csa->event_enable;
3299 		async_head = &csa->ccb_h.path->device->asyncs;
3300 
3301 		/*
3302 		 * If there is already an entry for us, simply
3303 		 * update it.
3304 		 */
3305 		s = splcam();
3306 		cur_entry = SLIST_FIRST(async_head);
3307 		while (cur_entry != NULL) {
3308 			if ((cur_entry->callback_arg == csa->callback_arg)
3309 			 && (cur_entry->callback == csa->callback))
3310 				break;
3311 			cur_entry = SLIST_NEXT(cur_entry, links);
3312 		}
3313 
3314 		if (cur_entry != NULL) {
3315 		 	/*
3316 			 * If the request has no flags set,
3317 			 * remove the entry.
3318 			 */
3319 			added &= ~cur_entry->event_enable;
3320 			if (csa->event_enable == 0) {
3321 				SLIST_REMOVE(async_head, cur_entry,
3322 					     async_node, links);
3323 				csa->ccb_h.path->device->refcount--;
3324 				free(cur_entry, M_DEVBUF);
3325 			} else {
3326 				cur_entry->event_enable = csa->event_enable;
3327 			}
3328 		} else {
3329 			cur_entry = malloc(sizeof(*cur_entry), M_DEVBUF,
3330 					   M_NOWAIT);
3331 			if (cur_entry == NULL) {
3332 				splx(s);
3333 				csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3334 				break;
3335 			}
3336 			cur_entry->event_enable = csa->event_enable;
3337 			cur_entry->callback_arg = csa->callback_arg;
3338 			cur_entry->callback = csa->callback;
3339 			SLIST_INSERT_HEAD(async_head, cur_entry, links);
3340 			csa->ccb_h.path->device->refcount++;
3341 		}
3342 
3343 		if ((added & AC_FOUND_DEVICE) != 0) {
3344 			/*
3345 			 * Get this peripheral up to date with all
3346 			 * the currently existing devices.
3347 			 */
3348 			xpt_for_all_devices(xptsetasyncfunc, cur_entry);
3349 		}
3350 		if ((added & AC_PATH_REGISTERED) != 0) {
3351 			/*
3352 			 * Get this peripheral up to date with all
3353 			 * the currently existing busses.
3354 			 */
3355 			xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
3356 		}
3357 		splx(s);
3358 		start_ccb->ccb_h.status = CAM_REQ_CMP;
3359 		break;
3360 	}
3361 	case XPT_REL_SIMQ:
3362 	{
3363 		struct ccb_relsim *crs;
3364 		struct cam_ed *dev;
3365 		int s;
3366 
3367 		crs = &start_ccb->crs;
3368 		dev = crs->ccb_h.path->device;
3369 		if (dev == NULL) {
3370 
3371 			crs->ccb_h.status = CAM_DEV_NOT_THERE;
3372 			break;
3373 		}
3374 
3375 		s = splcam();
3376 
3377 		if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3378 
3379  			if ((dev->inq_data.flags & SID_CmdQue) != 0) {
3380 
3381 				/* Don't ever go below one opening */
3382 				if (crs->openings > 0) {
3383 					xpt_dev_ccbq_resize(crs->ccb_h.path,
3384 							    crs->openings);
3385 
3386 					if (bootverbose) {
3387 						xpt_print_path(crs->ccb_h.path);
3388 						printf("tagged openings "
3389 						       "now %d\n",
3390 						       crs->openings);
3391 					}
3392 				}
3393 			}
3394 		}
3395 
3396 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3397 
3398 			if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3399 
3400 				/*
3401 				 * Just extend the old timeout and decrement
3402 				 * the freeze count so that a single timeout
3403 				 * is sufficient for releasing the queue.
3404 				 */
3405 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3406 				untimeout(xpt_release_devq_timeout,
3407 					  dev, dev->c_handle);
3408 			} else {
3409 
3410 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3411 			}
3412 
3413 			dev->c_handle =
3414 				timeout(xpt_release_devq_timeout,
3415 					dev,
3416 					(crs->release_timeout * hz) / 1000);
3417 
3418 			dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3419 
3420 		}
3421 
3422 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3423 
3424 			if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3425 				/*
3426 				 * Decrement the freeze count so that a single
3427 				 * completion is still sufficient to unfreeze
3428 				 * the queue.
3429 				 */
3430 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3431 			} else {
3432 
3433 				dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3434 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3435 			}
3436 		}
3437 
3438 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3439 
3440 			if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3441 			 || (dev->ccbq.dev_active == 0)) {
3442 
3443 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3444 			} else {
3445 
3446 				dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3447 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3448 			}
3449 		}
3450 		splx(s);
3451 
3452 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3453 
3454 			xpt_release_devq(crs->ccb_h.path, /*count*/1,
3455 					 /*run_queue*/TRUE);
3456 		}
3457 		start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3458 		start_ccb->ccb_h.status = CAM_REQ_CMP;
3459 		break;
3460 	}
3461 	case XPT_SCAN_BUS:
3462 		xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3463 		break;
3464 	case XPT_SCAN_LUN:
3465 		xpt_scan_lun(start_ccb->ccb_h.path->periph,
3466 			     start_ccb->ccb_h.path, start_ccb->crcn.flags,
3467 			     start_ccb);
3468 		break;
3469 	case XPT_DEBUG: {
3470 #ifdef CAMDEBUG
3471 		int s;
3472 
3473 		s = splcam();
3474 #ifdef CAM_DEBUG_DELAY
3475 		cam_debug_delay = CAM_DEBUG_DELAY;
3476 #endif
3477 		cam_dflags = start_ccb->cdbg.flags;
3478 		if (cam_dpath != NULL) {
3479 			xpt_free_path(cam_dpath);
3480 			cam_dpath = NULL;
3481 		}
3482 
3483 		if (cam_dflags != CAM_DEBUG_NONE) {
3484 			if (xpt_create_path(&cam_dpath, xpt_periph,
3485 					    start_ccb->ccb_h.path_id,
3486 					    start_ccb->ccb_h.target_id,
3487 					    start_ccb->ccb_h.target_lun) !=
3488 					    CAM_REQ_CMP) {
3489 				start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3490 				cam_dflags = CAM_DEBUG_NONE;
3491 			} else {
3492 				start_ccb->ccb_h.status = CAM_REQ_CMP;
3493 				xpt_print_path(cam_dpath);
3494 				printf("debugging flags now %x\n", cam_dflags);
3495 			}
3496 		} else {
3497 			cam_dpath = NULL;
3498 			start_ccb->ccb_h.status = CAM_REQ_CMP;
3499 		}
3500 		splx(s);
3501 #else /* !CAMDEBUG */
3502 		start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3503 #endif /* CAMDEBUG */
3504 		break;
3505 	}
3506 	case XPT_NOOP:
3507 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3508 			xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3509 		start_ccb->ccb_h.status = CAM_REQ_CMP;
3510 		break;
3511 	default:
3512 	case XPT_SDEV_TYPE:
3513 	case XPT_TERM_IO:
3514 	case XPT_ENG_INQ:
3515 		/* XXX Implement */
3516 		start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3517 		break;
3518 	}
3519 	splx(iopl);
3520 }
3521 
3522 void
3523 xpt_polled_action(union ccb *start_ccb)
3524 {
3525 	int	  s;
3526 	u_int32_t timeout;
3527 	struct	  cam_sim *sim;
3528 	struct	  cam_devq *devq;
3529 	struct	  cam_ed *dev;
3530 
3531 	timeout = start_ccb->ccb_h.timeout;
3532 	sim = start_ccb->ccb_h.path->bus->sim;
3533 	devq = sim->devq;
3534 	dev = start_ccb->ccb_h.path->device;
3535 
3536 	s = splcam();
3537 
3538 	/*
3539 	 * Steal an opening so that no other queued requests
3540 	 * can get it before us while we simulate interrupts.
3541 	 */
3542 	dev->ccbq.devq_openings--;
3543 	dev->ccbq.dev_openings--;
3544 
3545 	while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0)
3546 	   && (--timeout > 0)) {
3547 		DELAY(1000);
3548 		(*(sim->sim_poll))(sim);
3549 		camisr(&cam_netq);
3550 		camisr(&cam_bioq);
3551 	}
3552 
3553 	dev->ccbq.devq_openings++;
3554 	dev->ccbq.dev_openings++;
3555 
3556 	if (timeout != 0) {
3557 		xpt_action(start_ccb);
3558 		while(--timeout > 0) {
3559 			(*(sim->sim_poll))(sim);
3560 			camisr(&cam_netq);
3561 			camisr(&cam_bioq);
3562 			if ((start_ccb->ccb_h.status  & CAM_STATUS_MASK)
3563 			    != CAM_REQ_INPROG)
3564 				break;
3565 			DELAY(1000);
3566 		}
3567 		if (timeout == 0) {
3568 			/*
3569 			 * XXX Is it worth adding a sim_timeout entry
3570 			 * point so we can attempt recovery?  If
3571 			 * this is only used for dumps, I don't think
3572 			 * it is.
3573 			 */
3574 			start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3575 		}
3576 	} else {
3577 		start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3578 	}
3579 	splx(s);
3580 }
3581 
3582 /*
3583  * Schedule a peripheral driver to receive a ccb when it's
3584  * target device has space for more transactions.
3585  */
3586 void
3587 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3588 {
3589 	struct cam_ed *device;
3590 	int s;
3591 	int runq;
3592 
3593 	CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3594 	device = perph->path->device;
3595 	s = splsoftcam();
3596 	if (periph_is_queued(perph)) {
3597 		/* Simply reorder based on new priority */
3598 		CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3599 			  ("   change priority to %d\n", new_priority));
3600 		if (new_priority < perph->pinfo.priority) {
3601 			camq_change_priority(&device->drvq,
3602 					     perph->pinfo.index,
3603 					     new_priority);
3604 		}
3605 		runq = 0;
3606 	} else {
3607 		/* New entry on the queue */
3608 		CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3609 			  ("   added periph to queue\n"));
3610 		perph->pinfo.priority = new_priority;
3611 		perph->pinfo.generation = ++device->drvq.generation;
3612 		camq_insert(&device->drvq, &perph->pinfo);
3613 		runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3614 	}
3615 	splx(s);
3616 	if (runq != 0) {
3617 		CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3618 			  ("   calling xpt_run_devq\n"));
3619 		xpt_run_dev_allocq(perph->path->bus);
3620 	}
3621 }
3622 
3623 
3624 /*
3625  * Schedule a device to run on a given queue.
3626  * If the device was inserted as a new entry on the queue,
3627  * return 1 meaning the device queue should be run. If we
3628  * were already queued, implying someone else has already
3629  * started the queue, return 0 so the caller doesn't attempt
3630  * to run the queue.  Must be run at either splsoftcam
3631  * (or splcam since that encompases splsoftcam).
3632  */
3633 static int
3634 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3635 		 u_int32_t new_priority)
3636 {
3637 	int retval;
3638 	u_int32_t old_priority;
3639 
3640 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3641 
3642 	old_priority = pinfo->priority;
3643 
3644 	/*
3645 	 * Are we already queued?
3646 	 */
3647 	if (pinfo->index != CAM_UNQUEUED_INDEX) {
3648 		/* Simply reorder based on new priority */
3649 		if (new_priority < old_priority) {
3650 			camq_change_priority(queue, pinfo->index,
3651 					     new_priority);
3652 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3653 					("changed priority to %d\n",
3654 					 new_priority));
3655 		}
3656 		retval = 0;
3657 	} else {
3658 		/* New entry on the queue */
3659 		if (new_priority < old_priority)
3660 			pinfo->priority = new_priority;
3661 
3662 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3663 				("Inserting onto queue\n"));
3664 		pinfo->generation = ++queue->generation;
3665 		camq_insert(queue, pinfo);
3666 		retval = 1;
3667 	}
3668 	return (retval);
3669 }
3670 
3671 static void
3672 xpt_run_dev_allocq(struct cam_eb *bus)
3673 {
3674 	struct	cam_devq *devq;
3675 	int	s;
3676 
3677 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3678 	devq = bus->sim->devq;
3679 
3680 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3681 			("   qfrozen_cnt == 0x%x, entries == %d, "
3682 			 "openings == %d, active == %d\n",
3683 			 devq->alloc_queue.qfrozen_cnt,
3684 			 devq->alloc_queue.entries,
3685 			 devq->alloc_openings,
3686 			 devq->alloc_active));
3687 
3688 	s = splsoftcam();
3689 	devq->alloc_queue.qfrozen_cnt++;
3690 	while ((devq->alloc_queue.entries > 0)
3691 	    && (devq->alloc_openings > 0)
3692 	    && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3693 		struct	cam_ed_qinfo *qinfo;
3694 		struct	cam_ed *device;
3695 		union	ccb *work_ccb;
3696 		struct	cam_periph *drv;
3697 		struct	camq *drvq;
3698 
3699 		qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3700 							   CAMQ_HEAD);
3701 		device = qinfo->device;
3702 
3703 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3704 				("running device %p\n", device));
3705 
3706 		drvq = &device->drvq;
3707 
3708 #ifdef CAMDEBUG
3709 		if (drvq->entries <= 0) {
3710 			panic("xpt_run_dev_allocq: "
3711 			      "Device on queue without any work to do");
3712 		}
3713 #endif
3714 		if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3715 			devq->alloc_openings--;
3716 			devq->alloc_active++;
3717 			drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3718 			splx(s);
3719 			xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3720 				      drv->pinfo.priority);
3721 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3722 					("calling periph start\n"));
3723 			drv->periph_start(drv, work_ccb);
3724 		} else {
3725 			/*
3726 			 * Malloc failure in alloc_ccb
3727 			 */
3728 			/*
3729 			 * XXX add us to a list to be run from free_ccb
3730 			 * if we don't have any ccbs active on this
3731 			 * device queue otherwise we may never get run
3732 			 * again.
3733 			 */
3734 			break;
3735 		}
3736 
3737 		/* Raise IPL for possible insertion and test at top of loop */
3738 		s = splsoftcam();
3739 
3740 		if (drvq->entries > 0) {
3741 			/* We have more work.  Attempt to reschedule */
3742 			xpt_schedule_dev_allocq(bus, device);
3743 		}
3744 	}
3745 	devq->alloc_queue.qfrozen_cnt--;
3746 	splx(s);
3747 }
3748 
3749 static void
3750 xpt_run_dev_sendq(struct cam_eb *bus)
3751 {
3752 	struct	cam_devq *devq;
3753 	int	s;
3754 
3755 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3756 
3757 	devq = bus->sim->devq;
3758 
3759 	s = splcam();
3760 	devq->send_queue.qfrozen_cnt++;
3761 	splx(s);
3762 	s = splsoftcam();
3763 	while ((devq->send_queue.entries > 0)
3764 	    && (devq->send_openings > 0)) {
3765 		struct	cam_ed_qinfo *qinfo;
3766 		struct	cam_ed *device;
3767 		union ccb *work_ccb;
3768 		struct	cam_sim *sim;
3769 		int	ospl;
3770 
3771 		ospl = splcam();
3772 	    	if (devq->send_queue.qfrozen_cnt > 1) {
3773 			splx(ospl);
3774 			break;
3775 		}
3776 
3777 		qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3778 							   CAMQ_HEAD);
3779 		device = qinfo->device;
3780 
3781 		/*
3782 		 * If the device has been "frozen", don't attempt
3783 		 * to run it.
3784 		 */
3785 		if (device->qfrozen_cnt > 0) {
3786 			splx(ospl);
3787 			continue;
3788 		}
3789 
3790 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3791 				("running device %p\n", device));
3792 
3793 		work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3794 		if (work_ccb == NULL) {
3795 			printf("device on run queue with no ccbs???");
3796 			splx(ospl);
3797 			continue;
3798 		}
3799 
3800 		if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3801 
3802 		 	if (num_highpower <= 0) {
3803 				/*
3804 				 * We got a high power command, but we
3805 				 * don't have any available slots.  Freeze
3806 				 * the device queue until we have a slot
3807 				 * available.
3808 				 */
3809 				device->qfrozen_cnt++;
3810 				STAILQ_INSERT_TAIL(&highpowerq,
3811 						   &work_ccb->ccb_h,
3812 						   xpt_links.stqe);
3813 
3814 				splx(ospl);
3815 				continue;
3816 			} else {
3817 				/*
3818 				 * Consume a high power slot while
3819 				 * this ccb runs.
3820 				 */
3821 				num_highpower--;
3822 			}
3823 		}
3824 		devq->active_dev = device;
3825 		cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3826 
3827 		cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3828 		splx(ospl);
3829 
3830 		devq->send_openings--;
3831 		devq->send_active++;
3832 
3833 		if (device->ccbq.queue.entries > 0)
3834 			xpt_schedule_dev_sendq(bus, device);
3835 
3836 		if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3837 			/*
3838 			 * The client wants to freeze the queue
3839 			 * after this CCB is sent.
3840 			 */
3841 			ospl = splcam();
3842 			device->qfrozen_cnt++;
3843 			splx(ospl);
3844 		}
3845 
3846 		splx(s);
3847 
3848 		/* In Target mode, the peripheral driver knows best... */
3849 		if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3850 			if ((device->inq_flags & SID_CmdQue) != 0
3851 			 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3852 				work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3853 			else
3854 				/*
3855 				 * Clear this in case of a retried CCB that
3856 				 * failed due to a rejected tag.
3857 				 */
3858 				work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3859 		}
3860 
3861 		/*
3862 		 * Device queues can be shared among multiple sim instances
3863 		 * that reside on different busses.  Use the SIM in the queue
3864 		 * CCB's path, rather than the one in the bus that was passed
3865 		 * into this function.
3866 		 */
3867 		sim = work_ccb->ccb_h.path->bus->sim;
3868 		(*(sim->sim_action))(sim, work_ccb);
3869 
3870 		ospl = splcam();
3871 		devq->active_dev = NULL;
3872 		splx(ospl);
3873 		/* Raise IPL for possible insertion and test at top of loop */
3874 		s = splsoftcam();
3875 	}
3876 	splx(s);
3877 	s = splcam();
3878 	devq->send_queue.qfrozen_cnt--;
3879 	splx(s);
3880 }
3881 
3882 /*
3883  * This function merges stuff from the slave ccb into the master ccb, while
3884  * keeping important fields in the master ccb constant.
3885  */
3886 void
3887 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3888 {
3889 	/*
3890 	 * Pull fields that are valid for peripheral drivers to set
3891 	 * into the master CCB along with the CCB "payload".
3892 	 */
3893 	master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3894 	master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3895 	master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3896 	master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3897 	bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3898 	      sizeof(union ccb) - sizeof(struct ccb_hdr));
3899 }
3900 
3901 void
3902 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3903 {
3904 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3905 	ccb_h->pinfo.priority = priority;
3906 	ccb_h->path = path;
3907 	ccb_h->path_id = path->bus->path_id;
3908 	if (path->target)
3909 		ccb_h->target_id = path->target->target_id;
3910 	else
3911 		ccb_h->target_id = CAM_TARGET_WILDCARD;
3912 	if (path->device) {
3913 		ccb_h->target_lun = path->device->lun_id;
3914 		ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3915 	} else {
3916 		ccb_h->target_lun = CAM_TARGET_WILDCARD;
3917 	}
3918 	ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3919 	ccb_h->flags = 0;
3920 }
3921 
3922 /* Path manipulation functions */
3923 cam_status
3924 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3925 		path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3926 {
3927 	struct	   cam_path *path;
3928 	cam_status status;
3929 
3930 	path = (struct cam_path *)malloc(sizeof(*path), M_DEVBUF, M_NOWAIT);
3931 
3932 	if (path == NULL) {
3933 		status = CAM_RESRC_UNAVAIL;
3934 		return(status);
3935 	}
3936 	status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3937 	if (status != CAM_REQ_CMP) {
3938 		free(path, M_DEVBUF);
3939 		path = NULL;
3940 	}
3941 	*new_path_ptr = path;
3942 	return (status);
3943 }
3944 
3945 static cam_status
3946 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3947 		 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3948 {
3949 	struct	     cam_eb *bus;
3950 	struct	     cam_et *target;
3951 	struct	     cam_ed *device;
3952 	cam_status   status;
3953 	int	     s;
3954 
3955 	status = CAM_REQ_CMP;	/* Completed without error */
3956 	target = NULL;		/* Wildcarded */
3957 	device = NULL;		/* Wildcarded */
3958 
3959 	/*
3960 	 * We will potentially modify the EDT, so block interrupts
3961 	 * that may attempt to create cam paths.
3962 	 */
3963 	s = splcam();
3964 	bus = xpt_find_bus(path_id);
3965 	if (bus == NULL) {
3966 		status = CAM_PATH_INVALID;
3967 	} else {
3968 		target = xpt_find_target(bus, target_id);
3969 		if (target == NULL) {
3970 			/* Create one */
3971 			struct cam_et *new_target;
3972 
3973 			new_target = xpt_alloc_target(bus, target_id);
3974 			if (new_target == NULL) {
3975 				status = CAM_RESRC_UNAVAIL;
3976 			} else {
3977 				target = new_target;
3978 			}
3979 		}
3980 		if (target != NULL) {
3981 			device = xpt_find_device(target, lun_id);
3982 			if (device == NULL) {
3983 				/* Create one */
3984 				struct cam_ed *new_device;
3985 
3986 				new_device = xpt_alloc_device(bus,
3987 							      target,
3988 							      lun_id);
3989 				if (new_device == NULL) {
3990 					status = CAM_RESRC_UNAVAIL;
3991 				} else {
3992 					device = new_device;
3993 				}
3994 			}
3995 		}
3996 	}
3997 	splx(s);
3998 
3999 	/*
4000 	 * Only touch the user's data if we are successful.
4001 	 */
4002 	if (status == CAM_REQ_CMP) {
4003 		new_path->periph = perph;
4004 		new_path->bus = bus;
4005 		new_path->target = target;
4006 		new_path->device = device;
4007 		CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
4008 	} else {
4009 		if (device != NULL)
4010 			xpt_release_device(bus, target, device);
4011 		if (target != NULL)
4012 			xpt_release_target(bus, target);
4013 		if (bus != NULL)
4014 			xpt_release_bus(bus);
4015 	}
4016 	return (status);
4017 }
4018 
4019 static void
4020 xpt_release_path(struct cam_path *path)
4021 {
4022 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
4023 	if (path->device != NULL) {
4024 		xpt_release_device(path->bus, path->target, path->device);
4025 		path->device = NULL;
4026 	}
4027 	if (path->target != NULL) {
4028 		xpt_release_target(path->bus, path->target);
4029 		path->target = NULL;
4030 	}
4031 	if (path->bus != NULL) {
4032 		xpt_release_bus(path->bus);
4033 		path->bus = NULL;
4034 	}
4035 }
4036 
4037 void
4038 xpt_free_path(struct cam_path *path)
4039 {
4040 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
4041 	xpt_release_path(path);
4042 	free(path, M_DEVBUF);
4043 }
4044 
4045 
4046 /*
4047  * Return -1 for failure, 0 for exact match, 1 for match with wildcards
4048  * in path1, 2 for match with wildcards in path2.
4049  */
4050 int
4051 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
4052 {
4053 	int retval = 0;
4054 
4055 	if (path1->bus != path2->bus) {
4056 		if (path1->bus->path_id == CAM_BUS_WILDCARD)
4057 			retval = 1;
4058 		else if (path2->bus->path_id == CAM_BUS_WILDCARD)
4059 			retval = 2;
4060 		else
4061 			return (-1);
4062 	}
4063 	if (path1->target != path2->target) {
4064 		if (path1->target->target_id == CAM_TARGET_WILDCARD) {
4065 			if (retval == 0)
4066 				retval = 1;
4067 		} else if (path2->target->target_id == CAM_TARGET_WILDCARD)
4068 			retval = 2;
4069 		else
4070 			return (-1);
4071 	}
4072 	if (path1->device != path2->device) {
4073 		if (path1->device->lun_id == CAM_LUN_WILDCARD) {
4074 			if (retval == 0)
4075 				retval = 1;
4076 		} else if (path2->device->lun_id == CAM_LUN_WILDCARD)
4077 			retval = 2;
4078 		else
4079 			return (-1);
4080 	}
4081 	return (retval);
4082 }
4083 
4084 void
4085 xpt_print_path(struct cam_path *path)
4086 {
4087 	if (path == NULL)
4088 		printf("(nopath): ");
4089 	else {
4090 		if (path->periph != NULL)
4091 			printf("(%s%d:", path->periph->periph_name,
4092 			       path->periph->unit_number);
4093 		else
4094 			printf("(noperiph:");
4095 
4096 		if (path->bus != NULL)
4097 			printf("%s%d:%d:", path->bus->sim->sim_name,
4098 			       path->bus->sim->unit_number,
4099 			       path->bus->sim->bus_id);
4100 		else
4101 			printf("nobus:");
4102 
4103 		if (path->target != NULL)
4104 			printf("%d:", path->target->target_id);
4105 		else
4106 			printf("X:");
4107 
4108 		if (path->device != NULL)
4109 			printf("%d): ", path->device->lun_id);
4110 		else
4111 			printf("X): ");
4112 	}
4113 }
4114 
4115 int
4116 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
4117 {
4118 	struct sbuf sb;
4119 
4120 	sbuf_new(&sb, str, str_len, 0);
4121 
4122 	if (path == NULL)
4123 		sbuf_printf(&sb, "(nopath): ");
4124 	else {
4125 		if (path->periph != NULL)
4126 			sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
4127 				    path->periph->unit_number);
4128 		else
4129 			sbuf_printf(&sb, "(noperiph:");
4130 
4131 		if (path->bus != NULL)
4132 			sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
4133 				    path->bus->sim->unit_number,
4134 				    path->bus->sim->bus_id);
4135 		else
4136 			sbuf_printf(&sb, "nobus:");
4137 
4138 		if (path->target != NULL)
4139 			sbuf_printf(&sb, "%d:", path->target->target_id);
4140 		else
4141 			sbuf_printf(&sb, "X:");
4142 
4143 		if (path->device != NULL)
4144 			sbuf_printf(&sb, "%d): ", path->device->lun_id);
4145 		else
4146 			sbuf_printf(&sb, "X): ");
4147 	}
4148 	sbuf_finish(&sb);
4149 
4150 	return(sbuf_len(&sb));
4151 }
4152 
4153 path_id_t
4154 xpt_path_path_id(struct cam_path *path)
4155 {
4156 	return(path->bus->path_id);
4157 }
4158 
4159 target_id_t
4160 xpt_path_target_id(struct cam_path *path)
4161 {
4162 	if (path->target != NULL)
4163 		return (path->target->target_id);
4164 	else
4165 		return (CAM_TARGET_WILDCARD);
4166 }
4167 
4168 lun_id_t
4169 xpt_path_lun_id(struct cam_path *path)
4170 {
4171 	if (path->device != NULL)
4172 		return (path->device->lun_id);
4173 	else
4174 		return (CAM_LUN_WILDCARD);
4175 }
4176 
4177 struct cam_sim *
4178 xpt_path_sim(struct cam_path *path)
4179 {
4180 	return (path->bus->sim);
4181 }
4182 
4183 struct cam_periph*
4184 xpt_path_periph(struct cam_path *path)
4185 {
4186 	return (path->periph);
4187 }
4188 
4189 /*
4190  * Release a CAM control block for the caller.  Remit the cost of the structure
4191  * to the device referenced by the path.  If the this device had no 'credits'
4192  * and peripheral drivers have registered async callbacks for this notification
4193  * call them now.
4194  */
4195 void
4196 xpt_release_ccb(union ccb *free_ccb)
4197 {
4198 	int	 s;
4199 	struct	 cam_path *path;
4200 	struct	 cam_ed *device;
4201 	struct	 cam_eb *bus;
4202 
4203 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4204 	path = free_ccb->ccb_h.path;
4205 	device = path->device;
4206 	bus = path->bus;
4207 	s = splsoftcam();
4208 	cam_ccbq_release_opening(&device->ccbq);
4209 	if (xpt_ccb_count > xpt_max_ccbs) {
4210 		xpt_free_ccb(free_ccb);
4211 		xpt_ccb_count--;
4212 	} else {
4213 		SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
4214 	}
4215 	bus->sim->devq->alloc_openings++;
4216 	bus->sim->devq->alloc_active--;
4217 	/* XXX Turn this into an inline function - xpt_run_device?? */
4218 	if ((device_is_alloc_queued(device) == 0)
4219 	 && (device->drvq.entries > 0)) {
4220 		xpt_schedule_dev_allocq(bus, device);
4221 	}
4222 	splx(s);
4223 	if (dev_allocq_is_runnable(bus->sim->devq))
4224 		xpt_run_dev_allocq(bus);
4225 }
4226 
4227 /* Functions accessed by SIM drivers */
4228 
4229 /*
4230  * A sim structure, listing the SIM entry points and instance
4231  * identification info is passed to xpt_bus_register to hook the SIM
4232  * into the CAM framework.  xpt_bus_register creates a cam_eb entry
4233  * for this new bus and places it in the array of busses and assigns
4234  * it a path_id.  The path_id may be influenced by "hard wiring"
4235  * information specified by the user.  Once interrupt services are
4236  * availible, the bus will be probed.
4237  */
4238 int32_t
4239 xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4240 {
4241 	struct cam_eb *new_bus;
4242 	struct cam_eb *old_bus;
4243 	struct ccb_pathinq cpi;
4244 	int s;
4245 
4246 	sim->bus_id = bus;
4247 	new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4248 					  M_DEVBUF, M_NOWAIT);
4249 	if (new_bus == NULL) {
4250 		/* Couldn't satisfy request */
4251 		return (CAM_RESRC_UNAVAIL);
4252 	}
4253 
4254 	if (strcmp(sim->sim_name, "xpt") != 0) {
4255 
4256 		sim->path_id =
4257 		    xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4258 	}
4259 
4260 	TAILQ_INIT(&new_bus->et_entries);
4261 	new_bus->path_id = sim->path_id;
4262 	new_bus->sim = sim;
4263 	timevalclear(&new_bus->last_reset);
4264 	new_bus->flags = 0;
4265 	new_bus->refcount = 1;	/* Held until a bus_deregister event */
4266 	new_bus->generation = 0;
4267 	s = splcam();
4268 	old_bus = TAILQ_FIRST(&xpt_busses);
4269 	while (old_bus != NULL
4270 	    && old_bus->path_id < new_bus->path_id)
4271 		old_bus = TAILQ_NEXT(old_bus, links);
4272 	if (old_bus != NULL)
4273 		TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4274 	else
4275 		TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4276 	bus_generation++;
4277 	splx(s);
4278 
4279 	/* Notify interested parties */
4280 	if (sim->path_id != CAM_XPT_PATH_ID) {
4281 		struct cam_path path;
4282 
4283 		xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4284 			         CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4285 		xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4286 		cpi.ccb_h.func_code = XPT_PATH_INQ;
4287 		xpt_action((union ccb *)&cpi);
4288 		xpt_async(AC_PATH_REGISTERED, &path, &cpi);
4289 		xpt_release_path(&path);
4290 	}
4291 	return (CAM_SUCCESS);
4292 }
4293 
4294 int32_t
4295 xpt_bus_deregister(path_id_t pathid)
4296 {
4297 	struct cam_path bus_path;
4298 	cam_status status;
4299 
4300 	status = xpt_compile_path(&bus_path, NULL, pathid,
4301 				  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4302 	if (status != CAM_REQ_CMP)
4303 		return (status);
4304 
4305 	xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4306 	xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4307 
4308 	/* Release the reference count held while registered. */
4309 	xpt_release_bus(bus_path.bus);
4310 	xpt_release_path(&bus_path);
4311 
4312 	return (CAM_REQ_CMP);
4313 }
4314 
4315 static path_id_t
4316 xptnextfreepathid(void)
4317 {
4318 	struct cam_eb *bus;
4319 	path_id_t pathid;
4320 	char *strval;
4321 
4322 	pathid = 0;
4323 	bus = TAILQ_FIRST(&xpt_busses);
4324 retry:
4325 	/* Find an unoccupied pathid */
4326 	while (bus != NULL
4327 	    && bus->path_id <= pathid) {
4328 		if (bus->path_id == pathid)
4329 			pathid++;
4330 		bus = TAILQ_NEXT(bus, links);
4331 	}
4332 
4333 	/*
4334 	 * Ensure that this pathid is not reserved for
4335 	 * a bus that may be registered in the future.
4336 	 */
4337 	if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4338 		++pathid;
4339 		/* Start the search over */
4340 		goto retry;
4341 	}
4342 	return (pathid);
4343 }
4344 
4345 static path_id_t
4346 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4347 {
4348 	path_id_t pathid;
4349 	int i, dunit, val;
4350 	char buf[32];
4351 
4352 	pathid = CAM_XPT_PATH_ID;
4353 	snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4354 	i = -1;
4355 	while ((i = resource_query_string(i, "at", buf)) != -1) {
4356 		if (strcmp(resource_query_name(i), "scbus")) {
4357 			/* Avoid a bit of foot shooting. */
4358 			continue;
4359 		}
4360 		dunit = resource_query_unit(i);
4361 		if (dunit < 0)		/* unwired?! */
4362 			continue;
4363 		if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4364 			if (sim_bus == val) {
4365 				pathid = dunit;
4366 				break;
4367 			}
4368 		} else if (sim_bus == 0) {
4369 			/* Unspecified matches bus 0 */
4370 			pathid = dunit;
4371 			break;
4372 		} else {
4373 			printf("Ambiguous scbus configuration for %s%d "
4374 			       "bus %d, cannot wire down.  The kernel "
4375 			       "config entry for scbus%d should "
4376 			       "specify a controller bus.\n"
4377 			       "Scbus will be assigned dynamically.\n",
4378 			       sim_name, sim_unit, sim_bus, dunit);
4379 			break;
4380 		}
4381 	}
4382 
4383 	if (pathid == CAM_XPT_PATH_ID)
4384 		pathid = xptnextfreepathid();
4385 	return (pathid);
4386 }
4387 
4388 void
4389 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4390 {
4391 	struct cam_eb *bus;
4392 	struct cam_et *target, *next_target;
4393 	struct cam_ed *device, *next_device;
4394 	int s;
4395 
4396 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4397 
4398 	/*
4399 	 * Most async events come from a CAM interrupt context.  In
4400 	 * a few cases, the error recovery code at the peripheral layer,
4401 	 * which may run from our SWI or a process context, may signal
4402 	 * deferred events with a call to xpt_async. Ensure async
4403 	 * notifications are serialized by blocking cam interrupts.
4404 	 */
4405 	s = splcam();
4406 
4407 	bus = path->bus;
4408 
4409 	if (async_code == AC_BUS_RESET) {
4410 		int s;
4411 
4412 		s = splclock();
4413 		/* Update our notion of when the last reset occurred */
4414 		microtime(&bus->last_reset);
4415 		splx(s);
4416 	}
4417 
4418 	for (target = TAILQ_FIRST(&bus->et_entries);
4419 	     target != NULL;
4420 	     target = next_target) {
4421 
4422 		next_target = TAILQ_NEXT(target, links);
4423 
4424 		if (path->target != target
4425 		 && path->target->target_id != CAM_TARGET_WILDCARD
4426 		 && target->target_id != CAM_TARGET_WILDCARD)
4427 			continue;
4428 
4429 		if (async_code == AC_SENT_BDR) {
4430 			int s;
4431 
4432 			/* Update our notion of when the last reset occurred */
4433 			s = splclock();
4434 			microtime(&path->target->last_reset);
4435 			splx(s);
4436 		}
4437 
4438 		for (device = TAILQ_FIRST(&target->ed_entries);
4439 		     device != NULL;
4440 		     device = next_device) {
4441 
4442 			next_device = TAILQ_NEXT(device, links);
4443 
4444 			if (path->device != device
4445 			 && path->device->lun_id != CAM_LUN_WILDCARD
4446 			 && device->lun_id != CAM_LUN_WILDCARD)
4447 				continue;
4448 
4449 			xpt_dev_async(async_code, bus, target,
4450 				      device, async_arg);
4451 
4452 			xpt_async_bcast(&device->asyncs, async_code,
4453 					path, async_arg);
4454 		}
4455 	}
4456 
4457 	/*
4458 	 * If this wasn't a fully wildcarded async, tell all
4459 	 * clients that want all async events.
4460 	 */
4461 	if (bus != xpt_periph->path->bus)
4462 		xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4463 				path, async_arg);
4464 	splx(s);
4465 }
4466 
4467 static void
4468 xpt_async_bcast(struct async_list *async_head,
4469 		u_int32_t async_code,
4470 		struct cam_path *path, void *async_arg)
4471 {
4472 	struct async_node *cur_entry;
4473 
4474 	cur_entry = SLIST_FIRST(async_head);
4475 	while (cur_entry != NULL) {
4476 		struct async_node *next_entry;
4477 		/*
4478 		 * Grab the next list entry before we call the current
4479 		 * entry's callback.  This is because the callback function
4480 		 * can delete its async callback entry.
4481 		 */
4482 		next_entry = SLIST_NEXT(cur_entry, links);
4483 		if ((cur_entry->event_enable & async_code) != 0)
4484 			cur_entry->callback(cur_entry->callback_arg,
4485 					    async_code, path,
4486 					    async_arg);
4487 		cur_entry = next_entry;
4488 	}
4489 }
4490 
4491 /*
4492  * Handle any per-device event notifications that require action by the XPT.
4493  */
4494 static void
4495 xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4496 	      struct cam_ed *device, void *async_arg)
4497 {
4498 	cam_status status;
4499 	struct cam_path newpath;
4500 
4501 	/*
4502 	 * We only need to handle events for real devices.
4503 	 */
4504 	if (target->target_id == CAM_TARGET_WILDCARD
4505 	 || device->lun_id == CAM_LUN_WILDCARD)
4506 		return;
4507 
4508 	/*
4509 	 * We need our own path with wildcards expanded to
4510 	 * handle certain types of events.
4511 	 */
4512 	if ((async_code == AC_SENT_BDR)
4513 	 || (async_code == AC_BUS_RESET)
4514 	 || (async_code == AC_INQ_CHANGED))
4515 		status = xpt_compile_path(&newpath, NULL,
4516 					  bus->path_id,
4517 					  target->target_id,
4518 					  device->lun_id);
4519 	else
4520 		status = CAM_REQ_CMP_ERR;
4521 
4522 	if (status == CAM_REQ_CMP) {
4523 
4524 		/*
4525 		 * Allow transfer negotiation to occur in a
4526 		 * tag free environment.
4527 		 */
4528 		if (async_code == AC_SENT_BDR
4529 		 || async_code == AC_BUS_RESET)
4530 			xpt_toggle_tags(&newpath);
4531 
4532 		if (async_code == AC_INQ_CHANGED) {
4533 			/*
4534 			 * We've sent a start unit command, or
4535 			 * something similar to a device that
4536 			 * may have caused its inquiry data to
4537 			 * change. So we re-scan the device to
4538 			 * refresh the inquiry data for it.
4539 			 */
4540 			xpt_scan_lun(newpath.periph, &newpath,
4541 				     CAM_EXPECT_INQ_CHANGE, NULL);
4542 		}
4543 		xpt_release_path(&newpath);
4544 	} else if (async_code == AC_LOST_DEVICE) {
4545 		device->flags |= CAM_DEV_UNCONFIGURED;
4546 	} else if (async_code == AC_TRANSFER_NEG) {
4547 		struct ccb_trans_settings *settings;
4548 
4549 		settings = (struct ccb_trans_settings *)async_arg;
4550 		xpt_set_transfer_settings(settings, device,
4551 					  /*async_update*/TRUE);
4552 	}
4553 }
4554 
4555 u_int32_t
4556 xpt_freeze_devq(struct cam_path *path, u_int count)
4557 {
4558 	int s;
4559 	struct ccb_hdr *ccbh;
4560 
4561 	s = splcam();
4562 	path->device->qfrozen_cnt += count;
4563 
4564 	/*
4565 	 * Mark the last CCB in the queue as needing
4566 	 * to be requeued if the driver hasn't
4567 	 * changed it's state yet.  This fixes a race
4568 	 * where a ccb is just about to be queued to
4569 	 * a controller driver when it's interrupt routine
4570 	 * freezes the queue.  To completly close the
4571 	 * hole, controller drives must check to see
4572 	 * if a ccb's status is still CAM_REQ_INPROG
4573 	 * under spl protection just before they queue
4574 	 * the CCB.  See ahc_action/ahc_freeze_devq for
4575 	 * an example.
4576 	 */
4577 	ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4578 	if (ccbh && ccbh->status == CAM_REQ_INPROG)
4579 		ccbh->status = CAM_REQUEUE_REQ;
4580 	splx(s);
4581 	return (path->device->qfrozen_cnt);
4582 }
4583 
4584 u_int32_t
4585 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4586 {
4587 	sim->devq->send_queue.qfrozen_cnt += count;
4588 	if (sim->devq->active_dev != NULL) {
4589 		struct ccb_hdr *ccbh;
4590 
4591 		ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4592 				  ccb_hdr_tailq);
4593 		if (ccbh && ccbh->status == CAM_REQ_INPROG)
4594 			ccbh->status = CAM_REQUEUE_REQ;
4595 	}
4596 	return (sim->devq->send_queue.qfrozen_cnt);
4597 }
4598 
4599 static void
4600 xpt_release_devq_timeout(void *arg)
4601 {
4602 	struct cam_ed *device;
4603 
4604 	device = (struct cam_ed *)arg;
4605 
4606 	xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4607 }
4608 
4609 void
4610 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4611 {
4612 	xpt_release_devq_device(path->device, count, run_queue);
4613 }
4614 
4615 static void
4616 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4617 {
4618 	int	rundevq;
4619 	int	s0, s1;
4620 
4621 	rundevq = 0;
4622 	s0 = splsoftcam();
4623 	s1 = splcam();
4624 	if (dev->qfrozen_cnt > 0) {
4625 
4626 		count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4627 		dev->qfrozen_cnt -= count;
4628 		if (dev->qfrozen_cnt == 0) {
4629 
4630 			/*
4631 			 * No longer need to wait for a successful
4632 			 * command completion.
4633 			 */
4634 			dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4635 
4636 			/*
4637 			 * Remove any timeouts that might be scheduled
4638 			 * to release this queue.
4639 			 */
4640 			if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4641 				untimeout(xpt_release_devq_timeout, dev,
4642 					  dev->c_handle);
4643 				dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4644 			}
4645 
4646 			/*
4647 			 * Now that we are unfrozen schedule the
4648 			 * device so any pending transactions are
4649 			 * run.
4650 			 */
4651 			if ((dev->ccbq.queue.entries > 0)
4652 			 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4653 			 && (run_queue != 0)) {
4654 				rundevq = 1;
4655 			}
4656 		}
4657 	}
4658 	splx(s1);
4659 	if (rundevq != 0)
4660 		xpt_run_dev_sendq(dev->target->bus);
4661 	splx(s0);
4662 }
4663 
4664 void
4665 xpt_release_simq(struct cam_sim *sim, int run_queue)
4666 {
4667 	int	s;
4668 	struct	camq *sendq;
4669 
4670 	sendq = &(sim->devq->send_queue);
4671 	s = splcam();
4672 	if (sendq->qfrozen_cnt > 0) {
4673 
4674 		sendq->qfrozen_cnt--;
4675 		if (sendq->qfrozen_cnt == 0) {
4676 			struct cam_eb *bus;
4677 
4678 			/*
4679 			 * If there is a timeout scheduled to release this
4680 			 * sim queue, remove it.  The queue frozen count is
4681 			 * already at 0.
4682 			 */
4683 			if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4684 				untimeout(xpt_release_simq_timeout, sim,
4685 					  sim->c_handle);
4686 				sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4687 			}
4688 			bus = xpt_find_bus(sim->path_id);
4689 			splx(s);
4690 
4691 			if (run_queue) {
4692 				/*
4693 				 * Now that we are unfrozen run the send queue.
4694 				 */
4695 				xpt_run_dev_sendq(bus);
4696 			}
4697 			xpt_release_bus(bus);
4698 		} else
4699 			splx(s);
4700 	} else
4701 		splx(s);
4702 }
4703 
4704 static void
4705 xpt_release_simq_timeout(void *arg)
4706 {
4707 	struct cam_sim *sim;
4708 
4709 	sim = (struct cam_sim *)arg;
4710 	xpt_release_simq(sim, /* run_queue */ TRUE);
4711 }
4712 
4713 void
4714 xpt_done(union ccb *done_ccb)
4715 {
4716 	int s;
4717 
4718 	s = splcam();
4719 
4720 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4721 	if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4722 		/*
4723 		 * Queue up the request for handling by our SWI handler
4724 		 * any of the "non-immediate" type of ccbs.
4725 		 */
4726 		switch (done_ccb->ccb_h.path->periph->type) {
4727 		case CAM_PERIPH_BIO:
4728 			TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4729 					  sim_links.tqe);
4730 			done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4731 			swi_sched(cambio_ih, SWI_NOSWITCH);
4732 			break;
4733 		case CAM_PERIPH_NET:
4734 			TAILQ_INSERT_TAIL(&cam_netq, &done_ccb->ccb_h,
4735 					  sim_links.tqe);
4736 			done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4737 			swi_sched(camnet_ih, SWI_NOSWITCH);
4738 			break;
4739 		}
4740 	}
4741 	splx(s);
4742 }
4743 
4744 union ccb *
4745 xpt_alloc_ccb()
4746 {
4747 	union ccb *new_ccb;
4748 
4749 	new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_WAITOK);
4750 	return (new_ccb);
4751 }
4752 
4753 void
4754 xpt_free_ccb(union ccb *free_ccb)
4755 {
4756 	free(free_ccb, M_DEVBUF);
4757 }
4758 
4759 
4760 
4761 /* Private XPT functions */
4762 
4763 /*
4764  * Get a CAM control block for the caller. Charge the structure to the device
4765  * referenced by the path.  If the this device has no 'credits' then the
4766  * device already has the maximum number of outstanding operations under way
4767  * and we return NULL. If we don't have sufficient resources to allocate more
4768  * ccbs, we also return NULL.
4769  */
4770 static union ccb *
4771 xpt_get_ccb(struct cam_ed *device)
4772 {
4773 	union ccb *new_ccb;
4774 	int s;
4775 
4776 	s = splsoftcam();
4777 	if ((new_ccb = (union ccb *)SLIST_FIRST(&ccb_freeq)) == NULL) {
4778 		new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_NOWAIT);
4779                 if (new_ccb == NULL) {
4780 			splx(s);
4781 			return (NULL);
4782 		}
4783 		callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4784 		SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
4785 				  xpt_links.sle);
4786 		xpt_ccb_count++;
4787 	}
4788 	cam_ccbq_take_opening(&device->ccbq);
4789 	SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
4790 	splx(s);
4791 	return (new_ccb);
4792 }
4793 
4794 static void
4795 xpt_release_bus(struct cam_eb *bus)
4796 {
4797 	int s;
4798 
4799 	s = splcam();
4800 	if ((--bus->refcount == 0)
4801 	 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4802 		TAILQ_REMOVE(&xpt_busses, bus, links);
4803 		bus_generation++;
4804 		splx(s);
4805 		free(bus, M_DEVBUF);
4806 	} else
4807 		splx(s);
4808 }
4809 
4810 static struct cam_et *
4811 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4812 {
4813 	struct cam_et *target;
4814 
4815 	target = (struct cam_et *)malloc(sizeof(*target), M_DEVBUF, M_NOWAIT);
4816 	if (target != NULL) {
4817 		struct cam_et *cur_target;
4818 
4819 		TAILQ_INIT(&target->ed_entries);
4820 		target->bus = bus;
4821 		target->target_id = target_id;
4822 		target->refcount = 1;
4823 		target->generation = 0;
4824 		timevalclear(&target->last_reset);
4825 		/*
4826 		 * Hold a reference to our parent bus so it
4827 		 * will not go away before we do.
4828 		 */
4829 		bus->refcount++;
4830 
4831 		/* Insertion sort into our bus's target list */
4832 		cur_target = TAILQ_FIRST(&bus->et_entries);
4833 		while (cur_target != NULL && cur_target->target_id < target_id)
4834 			cur_target = TAILQ_NEXT(cur_target, links);
4835 
4836 		if (cur_target != NULL) {
4837 			TAILQ_INSERT_BEFORE(cur_target, target, links);
4838 		} else {
4839 			TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4840 		}
4841 		bus->generation++;
4842 	}
4843 	return (target);
4844 }
4845 
4846 static void
4847 xpt_release_target(struct cam_eb *bus, struct cam_et *target)
4848 {
4849 	int s;
4850 
4851 	s = splcam();
4852 	if ((--target->refcount == 0)
4853 	 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
4854 		TAILQ_REMOVE(&bus->et_entries, target, links);
4855 		bus->generation++;
4856 		splx(s);
4857 		free(target, M_DEVBUF);
4858 		xpt_release_bus(bus);
4859 	} else
4860 		splx(s);
4861 }
4862 
4863 static struct cam_ed *
4864 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4865 {
4866 #ifdef CAM_NEW_TRAN_CODE
4867 	struct	   cam_path path;
4868 #endif /* CAM_NEW_TRAN_CODE */
4869 	struct	   cam_ed *device;
4870 	struct	   cam_devq *devq;
4871 	cam_status status;
4872 
4873 	/* Make space for us in the device queue on our bus */
4874 	devq = bus->sim->devq;
4875 	status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4876 
4877 	if (status != CAM_REQ_CMP) {
4878 		device = NULL;
4879 	} else {
4880 		device = (struct cam_ed *)malloc(sizeof(*device),
4881 						 M_DEVBUF, M_NOWAIT);
4882 	}
4883 
4884 	if (device != NULL) {
4885 		struct cam_ed *cur_device;
4886 
4887 		cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4888 		device->alloc_ccb_entry.device = device;
4889 		cam_init_pinfo(&device->send_ccb_entry.pinfo);
4890 		device->send_ccb_entry.device = device;
4891 		device->target = target;
4892 		device->lun_id = lun_id;
4893 		/* Initialize our queues */
4894 		if (camq_init(&device->drvq, 0) != 0) {
4895 			free(device, M_DEVBUF);
4896 			return (NULL);
4897 		}
4898 		if (cam_ccbq_init(&device->ccbq,
4899 				  bus->sim->max_dev_openings) != 0) {
4900 			camq_fini(&device->drvq);
4901 			free(device, M_DEVBUF);
4902 			return (NULL);
4903 		}
4904 		SLIST_INIT(&device->asyncs);
4905 		SLIST_INIT(&device->periphs);
4906 		device->generation = 0;
4907 		device->owner = NULL;
4908 		/*
4909 		 * Take the default quirk entry until we have inquiry
4910 		 * data and can determine a better quirk to use.
4911 		 */
4912 		device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
4913 		bzero(&device->inq_data, sizeof(device->inq_data));
4914 		device->inq_flags = 0;
4915 		device->queue_flags = 0;
4916 		device->serial_num = NULL;
4917 		device->serial_num_len = 0;
4918 		device->qfrozen_cnt = 0;
4919 		device->flags = CAM_DEV_UNCONFIGURED;
4920 		device->tag_delay_count = 0;
4921 		device->refcount = 1;
4922 		callout_handle_init(&device->c_handle);
4923 
4924 		/*
4925 		 * Hold a reference to our parent target so it
4926 		 * will not go away before we do.
4927 		 */
4928 		target->refcount++;
4929 
4930 		/*
4931 		 * XXX should be limited by number of CCBs this bus can
4932 		 * do.
4933 		 */
4934 		xpt_max_ccbs += device->ccbq.devq_openings;
4935 		/* Insertion sort into our target's device list */
4936 		cur_device = TAILQ_FIRST(&target->ed_entries);
4937 		while (cur_device != NULL && cur_device->lun_id < lun_id)
4938 			cur_device = TAILQ_NEXT(cur_device, links);
4939 		if (cur_device != NULL) {
4940 			TAILQ_INSERT_BEFORE(cur_device, device, links);
4941 		} else {
4942 			TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4943 		}
4944 		target->generation++;
4945 #ifdef CAM_NEW_TRAN_CODE
4946 		if (lun_id != CAM_LUN_WILDCARD) {
4947 			xpt_compile_path(&path,
4948 					 NULL,
4949 					 bus->path_id,
4950 					 target->target_id,
4951 					 lun_id);
4952 			xpt_devise_transport(&path);
4953 			xpt_release_path(&path);
4954 		}
4955 #endif /* CAM_NEW_TRAN_CODE */
4956 	}
4957 	return (device);
4958 }
4959 
4960 static void
4961 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
4962 		   struct cam_ed *device)
4963 {
4964 	int s;
4965 
4966 	s = splcam();
4967 	if ((--device->refcount == 0)
4968 	 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
4969 		struct cam_devq *devq;
4970 
4971 		if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4972 		 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4973 			panic("Removing device while still queued for ccbs");
4974 
4975 		if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4976 				untimeout(xpt_release_devq_timeout, device,
4977 					  device->c_handle);
4978 
4979 		TAILQ_REMOVE(&target->ed_entries, device,links);
4980 		target->generation++;
4981 		xpt_max_ccbs -= device->ccbq.devq_openings;
4982 		/* Release our slot in the devq */
4983 		devq = bus->sim->devq;
4984 		cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4985 		splx(s);
4986 		free(device, M_DEVBUF);
4987 		xpt_release_target(bus, target);
4988 	} else
4989 		splx(s);
4990 }
4991 
4992 static u_int32_t
4993 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4994 {
4995 	int	s;
4996 	int	diff;
4997 	int	result;
4998 	struct	cam_ed *dev;
4999 
5000 	dev = path->device;
5001 	s = splsoftcam();
5002 
5003 	diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
5004 	result = cam_ccbq_resize(&dev->ccbq, newopenings);
5005 	if (result == CAM_REQ_CMP && (diff < 0)) {
5006 		dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
5007 	}
5008 	/* Adjust the global limit */
5009 	xpt_max_ccbs += diff;
5010 	splx(s);
5011 	return (result);
5012 }
5013 
5014 static struct cam_eb *
5015 xpt_find_bus(path_id_t path_id)
5016 {
5017 	struct cam_eb *bus;
5018 
5019 	for (bus = TAILQ_FIRST(&xpt_busses);
5020 	     bus != NULL;
5021 	     bus = TAILQ_NEXT(bus, links)) {
5022 		if (bus->path_id == path_id) {
5023 			bus->refcount++;
5024 			break;
5025 		}
5026 	}
5027 	return (bus);
5028 }
5029 
5030 static struct cam_et *
5031 xpt_find_target(struct cam_eb *bus, target_id_t	target_id)
5032 {
5033 	struct cam_et *target;
5034 
5035 	for (target = TAILQ_FIRST(&bus->et_entries);
5036 	     target != NULL;
5037 	     target = TAILQ_NEXT(target, links)) {
5038 		if (target->target_id == target_id) {
5039 			target->refcount++;
5040 			break;
5041 		}
5042 	}
5043 	return (target);
5044 }
5045 
5046 static struct cam_ed *
5047 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5048 {
5049 	struct cam_ed *device;
5050 
5051 	for (device = TAILQ_FIRST(&target->ed_entries);
5052 	     device != NULL;
5053 	     device = TAILQ_NEXT(device, links)) {
5054 		if (device->lun_id == lun_id) {
5055 			device->refcount++;
5056 			break;
5057 		}
5058 	}
5059 	return (device);
5060 }
5061 
5062 typedef struct {
5063 	union	ccb *request_ccb;
5064 	struct 	ccb_pathinq *cpi;
5065 	int	pending_count;
5066 } xpt_scan_bus_info;
5067 
5068 /*
5069  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
5070  * As the scan progresses, xpt_scan_bus is used as the
5071  * callback on completion function.
5072  */
5073 static void
5074 xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
5075 {
5076 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5077 		  ("xpt_scan_bus\n"));
5078 	switch (request_ccb->ccb_h.func_code) {
5079 	case XPT_SCAN_BUS:
5080 	{
5081 		xpt_scan_bus_info *scan_info;
5082 		union	ccb *work_ccb;
5083 		struct	cam_path *path;
5084 		u_int	i;
5085 		u_int	max_target;
5086 		u_int	initiator_id;
5087 
5088 		/* Find out the characteristics of the bus */
5089 		work_ccb = xpt_alloc_ccb();
5090 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
5091 			      request_ccb->ccb_h.pinfo.priority);
5092 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
5093 		xpt_action(work_ccb);
5094 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
5095 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
5096 			xpt_free_ccb(work_ccb);
5097 			xpt_done(request_ccb);
5098 			return;
5099 		}
5100 
5101 		if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5102 			/*
5103 			 * Can't scan the bus on an adapter that
5104 			 * cannot perform the initiator role.
5105 			 */
5106 			request_ccb->ccb_h.status = CAM_REQ_CMP;
5107 			xpt_free_ccb(work_ccb);
5108 			xpt_done(request_ccb);
5109 			return;
5110 		}
5111 
5112 		/* Save some state for use while we probe for devices */
5113 		scan_info = (xpt_scan_bus_info *)
5114 		    malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK);
5115 		scan_info->request_ccb = request_ccb;
5116 		scan_info->cpi = &work_ccb->cpi;
5117 
5118 		/* Cache on our stack so we can work asynchronously */
5119 		max_target = scan_info->cpi->max_target;
5120 		initiator_id = scan_info->cpi->initiator_id;
5121 
5122 		/*
5123 		 * Don't count the initiator if the
5124 		 * initiator is addressable.
5125 		 */
5126 		scan_info->pending_count = max_target + 1;
5127 		if (initiator_id <= max_target)
5128 			scan_info->pending_count--;
5129 
5130 		for (i = 0; i <= max_target; i++) {
5131 			cam_status status;
5132 		 	if (i == initiator_id)
5133 				continue;
5134 
5135 			status = xpt_create_path(&path, xpt_periph,
5136 						 request_ccb->ccb_h.path_id,
5137 						 i, 0);
5138 			if (status != CAM_REQ_CMP) {
5139 				printf("xpt_scan_bus: xpt_create_path failed"
5140 				       " with status %#x, bus scan halted\n",
5141 				       status);
5142 				break;
5143 			}
5144 			work_ccb = xpt_alloc_ccb();
5145 			xpt_setup_ccb(&work_ccb->ccb_h, path,
5146 				      request_ccb->ccb_h.pinfo.priority);
5147 			work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5148 			work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5149 			work_ccb->ccb_h.ppriv_ptr0 = scan_info;
5150 			work_ccb->crcn.flags = request_ccb->crcn.flags;
5151 			xpt_action(work_ccb);
5152 		}
5153 		break;
5154 	}
5155 	case XPT_SCAN_LUN:
5156 	{
5157 		xpt_scan_bus_info *scan_info;
5158 		path_id_t path_id;
5159 		target_id_t target_id;
5160 		lun_id_t lun_id;
5161 
5162 		/* Reuse the same CCB to query if a device was really found */
5163 		scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
5164 		xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
5165 			      request_ccb->ccb_h.pinfo.priority);
5166 		request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5167 
5168 		path_id = request_ccb->ccb_h.path_id;
5169 		target_id = request_ccb->ccb_h.target_id;
5170 		lun_id = request_ccb->ccb_h.target_lun;
5171 		xpt_action(request_ccb);
5172 
5173 		if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
5174 			struct cam_ed *device;
5175 			struct cam_et *target;
5176 			int s, phl;
5177 
5178 			/*
5179 			 * If we already probed lun 0 successfully, or
5180 			 * we have additional configured luns on this
5181 			 * target that might have "gone away", go onto
5182 			 * the next lun.
5183 			 */
5184 			target = request_ccb->ccb_h.path->target;
5185 			/*
5186 			 * We may touch devices that we don't
5187 			 * hold references too, so ensure they
5188 			 * don't disappear out from under us.
5189 			 * The target above is referenced by the
5190 			 * path in the request ccb.
5191 			 */
5192 			phl = 0;
5193 			s = splcam();
5194 			device = TAILQ_FIRST(&target->ed_entries);
5195 			if (device != NULL) {
5196 				phl = device->quirk->quirks & CAM_QUIRK_HILUNS;
5197 				if (device->lun_id == 0)
5198 					device = TAILQ_NEXT(device, links);
5199 			}
5200 			splx(s);
5201 			if ((lun_id != 0) || (device != NULL)) {
5202 				if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5203 					lun_id++;
5204 			}
5205 		} else {
5206 			struct cam_ed *device;
5207 
5208 			device = request_ccb->ccb_h.path->device;
5209 
5210 			if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5211 				/* Try the next lun */
5212 				if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
5213 				    (device->quirk->quirks & CAM_QUIRK_HILUNS))
5214 					lun_id++;
5215 			}
5216 		}
5217 
5218 		xpt_free_path(request_ccb->ccb_h.path);
5219 
5220 		/* Check Bounds */
5221 		if ((lun_id == request_ccb->ccb_h.target_lun)
5222 		 || lun_id > scan_info->cpi->max_lun) {
5223 			/* We're done */
5224 
5225 			xpt_free_ccb(request_ccb);
5226 			scan_info->pending_count--;
5227 			if (scan_info->pending_count == 0) {
5228 				xpt_free_ccb((union ccb *)scan_info->cpi);
5229 				request_ccb = scan_info->request_ccb;
5230 				free(scan_info, M_TEMP);
5231 				request_ccb->ccb_h.status = CAM_REQ_CMP;
5232 				xpt_done(request_ccb);
5233 			}
5234 		} else {
5235 			/* Try the next device */
5236 			struct cam_path *path;
5237 			cam_status status;
5238 
5239 			path = request_ccb->ccb_h.path;
5240 			status = xpt_create_path(&path, xpt_periph,
5241 						 path_id, target_id, lun_id);
5242 			if (status != CAM_REQ_CMP) {
5243 				printf("xpt_scan_bus: xpt_create_path failed "
5244 				       "with status %#x, halting LUN scan\n",
5245 			 	       status);
5246 				xpt_free_ccb(request_ccb);
5247 				scan_info->pending_count--;
5248 				if (scan_info->pending_count == 0) {
5249 					xpt_free_ccb(
5250 						(union ccb *)scan_info->cpi);
5251 					request_ccb = scan_info->request_ccb;
5252 					free(scan_info, M_TEMP);
5253 					request_ccb->ccb_h.status = CAM_REQ_CMP;
5254 					xpt_done(request_ccb);
5255 					break;
5256 				}
5257 			}
5258 			xpt_setup_ccb(&request_ccb->ccb_h, path,
5259 				      request_ccb->ccb_h.pinfo.priority);
5260 			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5261 			request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5262 			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5263 			request_ccb->crcn.flags =
5264 				scan_info->request_ccb->crcn.flags;
5265 			xpt_action(request_ccb);
5266 		}
5267 		break;
5268 	}
5269 	default:
5270 		break;
5271 	}
5272 }
5273 
5274 typedef enum {
5275 	PROBE_TUR,
5276 	PROBE_INQUIRY,
5277 	PROBE_FULL_INQUIRY,
5278 	PROBE_MODE_SENSE,
5279 	PROBE_SERIAL_NUM,
5280 	PROBE_TUR_FOR_NEGOTIATION
5281 } probe_action;
5282 
5283 typedef enum {
5284 	PROBE_INQUIRY_CKSUM	= 0x01,
5285 	PROBE_SERIAL_CKSUM	= 0x02,
5286 	PROBE_NO_ANNOUNCE	= 0x04
5287 } probe_flags;
5288 
5289 typedef struct {
5290 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
5291 	probe_action	action;
5292 	union ccb	saved_ccb;
5293 	probe_flags	flags;
5294 	MD5_CTX		context;
5295 	u_int8_t	digest[16];
5296 } probe_softc;
5297 
5298 static void
5299 xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5300 	     cam_flags flags, union ccb *request_ccb)
5301 {
5302 	struct ccb_pathinq cpi;
5303 	cam_status status;
5304 	struct cam_path *new_path;
5305 	struct cam_periph *old_periph;
5306 	int s;
5307 
5308 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5309 		  ("xpt_scan_lun\n"));
5310 
5311 	xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5312 	cpi.ccb_h.func_code = XPT_PATH_INQ;
5313 	xpt_action((union ccb *)&cpi);
5314 
5315 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
5316 		if (request_ccb != NULL) {
5317 			request_ccb->ccb_h.status = cpi.ccb_h.status;
5318 			xpt_done(request_ccb);
5319 		}
5320 		return;
5321 	}
5322 
5323 	if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5324 		/*
5325 		 * Can't scan the bus on an adapter that
5326 		 * cannot perform the initiator role.
5327 		 */
5328 		if (request_ccb != NULL) {
5329 			request_ccb->ccb_h.status = CAM_REQ_CMP;
5330 			xpt_done(request_ccb);
5331 		}
5332 		return;
5333 	}
5334 
5335 	if (request_ccb == NULL) {
5336 		request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
5337 		if (request_ccb == NULL) {
5338 			xpt_print_path(path);
5339 			printf("xpt_scan_lun: can't allocate CCB, can't "
5340 			       "continue\n");
5341 			return;
5342 		}
5343 		new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
5344 		if (new_path == NULL) {
5345 			xpt_print_path(path);
5346 			printf("xpt_scan_lun: can't allocate path, can't "
5347 			       "continue\n");
5348 			free(request_ccb, M_TEMP);
5349 			return;
5350 		}
5351 		status = xpt_compile_path(new_path, xpt_periph,
5352 					  path->bus->path_id,
5353 					  path->target->target_id,
5354 					  path->device->lun_id);
5355 
5356 		if (status != CAM_REQ_CMP) {
5357 			xpt_print_path(path);
5358 			printf("xpt_scan_lun: can't compile path, can't "
5359 			       "continue\n");
5360 			free(request_ccb, M_TEMP);
5361 			free(new_path, M_TEMP);
5362 			return;
5363 		}
5364 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5365 		request_ccb->ccb_h.cbfcnp = xptscandone;
5366 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5367 		request_ccb->crcn.flags = flags;
5368 	}
5369 
5370 	s = splsoftcam();
5371 	if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5372 		probe_softc *softc;
5373 
5374 		softc = (probe_softc *)old_periph->softc;
5375 		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5376 				  periph_links.tqe);
5377 	} else {
5378 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
5379 					  probestart, "probe",
5380 					  CAM_PERIPH_BIO,
5381 					  request_ccb->ccb_h.path, NULL, 0,
5382 					  request_ccb);
5383 
5384 		if (status != CAM_REQ_CMP) {
5385 			xpt_print_path(path);
5386 			printf("xpt_scan_lun: cam_alloc_periph returned an "
5387 			       "error, can't continue probe\n");
5388 			request_ccb->ccb_h.status = status;
5389 			xpt_done(request_ccb);
5390 		}
5391 	}
5392 	splx(s);
5393 }
5394 
5395 static void
5396 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5397 {
5398 	xpt_release_path(done_ccb->ccb_h.path);
5399 	free(done_ccb->ccb_h.path, M_TEMP);
5400 	free(done_ccb, M_TEMP);
5401 }
5402 
5403 static cam_status
5404 proberegister(struct cam_periph *periph, void *arg)
5405 {
5406 	union ccb *request_ccb;	/* CCB representing the probe request */
5407 	probe_softc *softc;
5408 
5409 	request_ccb = (union ccb *)arg;
5410 	if (periph == NULL) {
5411 		printf("proberegister: periph was NULL!!\n");
5412 		return(CAM_REQ_CMP_ERR);
5413 	}
5414 
5415 	if (request_ccb == NULL) {
5416 		printf("proberegister: no probe CCB, "
5417 		       "can't register device\n");
5418 		return(CAM_REQ_CMP_ERR);
5419 	}
5420 
5421 	softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
5422 
5423 	if (softc == NULL) {
5424 		printf("proberegister: Unable to probe new device. "
5425 		       "Unable to allocate softc\n");
5426 		return(CAM_REQ_CMP_ERR);
5427 	}
5428 	TAILQ_INIT(&softc->request_ccbs);
5429 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5430 			  periph_links.tqe);
5431 	softc->flags = 0;
5432 	periph->softc = softc;
5433 	cam_periph_acquire(periph);
5434 	/*
5435 	 * Ensure we've waited at least a bus settle
5436 	 * delay before attempting to probe the device.
5437 	 * For HBAs that don't do bus resets, this won't make a difference.
5438 	 */
5439 	cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5440 				      SCSI_DELAY);
5441 	probeschedule(periph);
5442 	return(CAM_REQ_CMP);
5443 }
5444 
5445 static void
5446 probeschedule(struct cam_periph *periph)
5447 {
5448 	struct ccb_pathinq cpi;
5449 	union ccb *ccb;
5450 	probe_softc *softc;
5451 
5452 	softc = (probe_softc *)periph->softc;
5453 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5454 
5455 	xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5456 	cpi.ccb_h.func_code = XPT_PATH_INQ;
5457 	xpt_action((union ccb *)&cpi);
5458 
5459 	/*
5460 	 * If a device has gone away and another device, or the same one,
5461 	 * is back in the same place, it should have a unit attention
5462 	 * condition pending.  It will not report the unit attention in
5463 	 * response to an inquiry, which may leave invalid transfer
5464 	 * negotiations in effect.  The TUR will reveal the unit attention
5465 	 * condition.  Only send the TUR for lun 0, since some devices
5466 	 * will get confused by commands other than inquiry to non-existent
5467 	 * luns.  If you think a device has gone away start your scan from
5468 	 * lun 0.  This will insure that any bogus transfer settings are
5469 	 * invalidated.
5470 	 *
5471 	 * If we haven't seen the device before and the controller supports
5472 	 * some kind of transfer negotiation, negotiate with the first
5473 	 * sent command if no bus reset was performed at startup.  This
5474 	 * ensures that the device is not confused by transfer negotiation
5475 	 * settings left over by loader or BIOS action.
5476 	 */
5477 	if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5478 	 && (ccb->ccb_h.target_lun == 0)) {
5479 		softc->action = PROBE_TUR;
5480 	} else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5481 	      && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5482 		proberequestdefaultnegotiation(periph);
5483 		softc->action = PROBE_INQUIRY;
5484 	} else {
5485 		softc->action = PROBE_INQUIRY;
5486 	}
5487 
5488 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5489 		softc->flags |= PROBE_NO_ANNOUNCE;
5490 	else
5491 		softc->flags &= ~PROBE_NO_ANNOUNCE;
5492 
5493 	xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5494 }
5495 
5496 static void
5497 probestart(struct cam_periph *periph, union ccb *start_ccb)
5498 {
5499 	/* Probe the device that our peripheral driver points to */
5500 	struct ccb_scsiio *csio;
5501 	probe_softc *softc;
5502 
5503 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5504 
5505 	softc = (probe_softc *)periph->softc;
5506 	csio = &start_ccb->csio;
5507 
5508 	switch (softc->action) {
5509 	case PROBE_TUR:
5510 	case PROBE_TUR_FOR_NEGOTIATION:
5511 	{
5512 		scsi_test_unit_ready(csio,
5513 				     /*retries*/4,
5514 				     probedone,
5515 				     MSG_SIMPLE_Q_TAG,
5516 				     SSD_FULL_SIZE,
5517 				     /*timeout*/60000);
5518 		break;
5519 	}
5520 	case PROBE_INQUIRY:
5521 	case PROBE_FULL_INQUIRY:
5522 	{
5523 		u_int inquiry_len;
5524 		struct scsi_inquiry_data *inq_buf;
5525 
5526 		inq_buf = &periph->path->device->inq_data;
5527 		/*
5528 		 * If the device is currently configured, we calculate an
5529 		 * MD5 checksum of the inquiry data, and if the serial number
5530 		 * length is greater than 0, add the serial number data
5531 		 * into the checksum as well.  Once the inquiry and the
5532 		 * serial number check finish, we attempt to figure out
5533 		 * whether we still have the same device.
5534 		 */
5535 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5536 
5537 			MD5Init(&softc->context);
5538 			MD5Update(&softc->context, (unsigned char *)inq_buf,
5539 				  sizeof(struct scsi_inquiry_data));
5540 			softc->flags |= PROBE_INQUIRY_CKSUM;
5541 			if (periph->path->device->serial_num_len > 0) {
5542 				MD5Update(&softc->context,
5543 					  periph->path->device->serial_num,
5544 					  periph->path->device->serial_num_len);
5545 				softc->flags |= PROBE_SERIAL_CKSUM;
5546 			}
5547 			MD5Final(softc->digest, &softc->context);
5548 		}
5549 
5550 		if (softc->action == PROBE_INQUIRY)
5551 			inquiry_len = SHORT_INQUIRY_LENGTH;
5552 		else
5553 			inquiry_len = inq_buf->additional_length + 4;
5554 
5555 		scsi_inquiry(csio,
5556 			     /*retries*/4,
5557 			     probedone,
5558 			     MSG_SIMPLE_Q_TAG,
5559 			     (u_int8_t *)inq_buf,
5560 			     inquiry_len,
5561 			     /*evpd*/FALSE,
5562 			     /*page_code*/0,
5563 			     SSD_MIN_SIZE,
5564 			     /*timeout*/60 * 1000);
5565 		break;
5566 	}
5567 	case PROBE_MODE_SENSE:
5568 	{
5569 		void  *mode_buf;
5570 		int    mode_buf_len;
5571 
5572 		mode_buf_len = sizeof(struct scsi_mode_header_6)
5573 			     + sizeof(struct scsi_mode_blk_desc)
5574 			     + sizeof(struct scsi_control_page);
5575 		mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
5576 		if (mode_buf != NULL) {
5577 	                scsi_mode_sense(csio,
5578 					/*retries*/4,
5579 					probedone,
5580 					MSG_SIMPLE_Q_TAG,
5581 					/*dbd*/FALSE,
5582 					SMS_PAGE_CTRL_CURRENT,
5583 					SMS_CONTROL_MODE_PAGE,
5584 					mode_buf,
5585 					mode_buf_len,
5586 					SSD_FULL_SIZE,
5587 					/*timeout*/60000);
5588 			break;
5589 		}
5590 		xpt_print_path(periph->path);
5591 		printf("Unable to mode sense control page - malloc failure\n");
5592 		softc->action = PROBE_SERIAL_NUM;
5593 		/* FALLTHROUGH */
5594 	}
5595 	case PROBE_SERIAL_NUM:
5596 	{
5597 		struct scsi_vpd_unit_serial_number *serial_buf;
5598 		struct cam_ed* device;
5599 
5600 		serial_buf = NULL;
5601 		device = periph->path->device;
5602 		device->serial_num = NULL;
5603 		device->serial_num_len = 0;
5604 
5605 		if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
5606 			serial_buf = (struct scsi_vpd_unit_serial_number *)
5607 				malloc(sizeof(*serial_buf), M_TEMP,
5608 					M_NOWAIT | M_ZERO);
5609 
5610 		if (serial_buf != NULL) {
5611 			scsi_inquiry(csio,
5612 				     /*retries*/4,
5613 				     probedone,
5614 				     MSG_SIMPLE_Q_TAG,
5615 				     (u_int8_t *)serial_buf,
5616 				     sizeof(*serial_buf),
5617 				     /*evpd*/TRUE,
5618 				     SVPD_UNIT_SERIAL_NUMBER,
5619 				     SSD_MIN_SIZE,
5620 				     /*timeout*/60 * 1000);
5621 			break;
5622 		}
5623 		/*
5624 		 * We'll have to do without, let our probedone
5625 		 * routine finish up for us.
5626 		 */
5627 		start_ccb->csio.data_ptr = NULL;
5628 		probedone(periph, start_ccb);
5629 		return;
5630 	}
5631 	}
5632 	xpt_action(start_ccb);
5633 }
5634 
5635 static void
5636 proberequestdefaultnegotiation(struct cam_periph *periph)
5637 {
5638 	struct ccb_trans_settings cts;
5639 
5640 	xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5641 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5642 #ifdef CAM_NEW_TRAN_CODE
5643 	cts.type = CTS_TYPE_USER_SETTINGS;
5644 #else /* CAM_NEW_TRAN_CODE */
5645 	cts.flags = CCB_TRANS_USER_SETTINGS;
5646 #endif /* CAM_NEW_TRAN_CODE */
5647 	xpt_action((union ccb *)&cts);
5648 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5649 #ifdef CAM_NEW_TRAN_CODE
5650 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
5651 #else /* CAM_NEW_TRAN_CODE */
5652 	cts.flags &= ~CCB_TRANS_USER_SETTINGS;
5653 	cts.flags |= CCB_TRANS_CURRENT_SETTINGS;
5654 #endif /* CAM_NEW_TRAN_CODE */
5655 	xpt_action((union ccb *)&cts);
5656 }
5657 
5658 static void
5659 probedone(struct cam_periph *periph, union ccb *done_ccb)
5660 {
5661 	probe_softc *softc;
5662 	struct cam_path *path;
5663 	u_int32_t  priority;
5664 
5665 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5666 
5667 	softc = (probe_softc *)periph->softc;
5668 	path = done_ccb->ccb_h.path;
5669 	priority = done_ccb->ccb_h.pinfo.priority;
5670 
5671 	switch (softc->action) {
5672 	case PROBE_TUR:
5673 	{
5674 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5675 
5676 			if (cam_periph_error(done_ccb, 0,
5677 					     SF_NO_PRINT, NULL) == ERESTART)
5678 				return;
5679 			else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5680 				/* Don't wedge the queue */
5681 				xpt_release_devq(done_ccb->ccb_h.path,
5682 						 /*count*/1,
5683 						 /*run_queue*/TRUE);
5684 		}
5685 		softc->action = PROBE_INQUIRY;
5686 		xpt_release_ccb(done_ccb);
5687 		xpt_schedule(periph, priority);
5688 		return;
5689 	}
5690 	case PROBE_INQUIRY:
5691 	case PROBE_FULL_INQUIRY:
5692 	{
5693 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5694 			struct scsi_inquiry_data *inq_buf;
5695 			u_int8_t periph_qual;
5696 			u_int8_t periph_dtype;
5697 
5698 			path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
5699 			inq_buf = &path->device->inq_data;
5700 
5701 			periph_qual = SID_QUAL(inq_buf);
5702 			periph_dtype = SID_TYPE(inq_buf);
5703 
5704 			if (periph_dtype != T_NODEVICE) {
5705 				switch(periph_qual) {
5706 				case SID_QUAL_LU_CONNECTED:
5707 				{
5708 					u_int8_t alen;
5709 
5710 					/*
5711 					 * We conservatively request only
5712 					 * SHORT_INQUIRY_LEN bytes of inquiry
5713 					 * information during our first try
5714 					 * at sending an INQUIRY. If the device
5715 					 * has more information to give,
5716 					 * perform a second request specifying
5717 					 * the amount of information the device
5718 					 * is willing to give.
5719 					 */
5720 					alen = inq_buf->additional_length;
5721 					if (softc->action == PROBE_INQUIRY
5722 					 && alen > (SHORT_INQUIRY_LENGTH - 4)) {
5723 						softc->action =
5724 						    PROBE_FULL_INQUIRY;
5725 						xpt_release_ccb(done_ccb);
5726 						xpt_schedule(periph, priority);
5727 						return;
5728 					}
5729 
5730 					xpt_find_quirk(path->device);
5731 
5732 #ifdef CAM_NEW_TRAN_CODE
5733 					xpt_devise_transport(path);
5734 #endif /* CAM_NEW_TRAN_CODE */
5735 					if ((inq_buf->flags & SID_CmdQue) != 0)
5736 						softc->action =
5737 						    PROBE_MODE_SENSE;
5738 					else
5739 						softc->action =
5740 						    PROBE_SERIAL_NUM;
5741 
5742 					path->device->flags &=
5743 						~CAM_DEV_UNCONFIGURED;
5744 
5745 					xpt_release_ccb(done_ccb);
5746 					xpt_schedule(periph, priority);
5747 					return;
5748 				}
5749 				default:
5750 					break;
5751 				}
5752 			}
5753 		} else if (cam_periph_error(done_ccb, 0,
5754 					    done_ccb->ccb_h.target_lun > 0
5755 					    ? SF_RETRY_UA|SF_QUIET_IR
5756 					    : SF_RETRY_UA,
5757 					    &softc->saved_ccb) == ERESTART) {
5758 			return;
5759 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5760 			/* Don't wedge the queue */
5761 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5762 					 /*run_queue*/TRUE);
5763 		}
5764 		/*
5765 		 * If we get to this point, we got an error status back
5766 		 * from the inquiry and the error status doesn't require
5767 		 * automatically retrying the command.  Therefore, the
5768 		 * inquiry failed.  If we had inquiry information before
5769 		 * for this device, but this latest inquiry command failed,
5770 		 * the device has probably gone away.  If this device isn't
5771 		 * already marked unconfigured, notify the peripheral
5772 		 * drivers that this device is no more.
5773 		 */
5774 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5775 			/* Send the async notification. */
5776 			xpt_async(AC_LOST_DEVICE, path, NULL);
5777 
5778 		xpt_release_ccb(done_ccb);
5779 		break;
5780 	}
5781 	case PROBE_MODE_SENSE:
5782 	{
5783 		struct ccb_scsiio *csio;
5784 		struct scsi_mode_header_6 *mode_hdr;
5785 
5786 		csio = &done_ccb->csio;
5787 		mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
5788 		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5789 			struct scsi_control_page *page;
5790 			u_int8_t *offset;
5791 
5792 			offset = ((u_int8_t *)&mode_hdr[1])
5793 			    + mode_hdr->blk_desc_len;
5794 			page = (struct scsi_control_page *)offset;
5795 			path->device->queue_flags = page->queue_flags;
5796 		} else if (cam_periph_error(done_ccb, 0,
5797 					    SF_RETRY_UA|SF_NO_PRINT,
5798 					    &softc->saved_ccb) == ERESTART) {
5799 			return;
5800 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5801 			/* Don't wedge the queue */
5802 			xpt_release_devq(done_ccb->ccb_h.path,
5803 					 /*count*/1, /*run_queue*/TRUE);
5804 		}
5805 		xpt_release_ccb(done_ccb);
5806 		free(mode_hdr, M_TEMP);
5807 		softc->action = PROBE_SERIAL_NUM;
5808 		xpt_schedule(periph, priority);
5809 		return;
5810 	}
5811 	case PROBE_SERIAL_NUM:
5812 	{
5813 		struct ccb_scsiio *csio;
5814 		struct scsi_vpd_unit_serial_number *serial_buf;
5815 		u_int32_t  priority;
5816 		int changed;
5817 		int have_serialnum;
5818 
5819 		changed = 1;
5820 		have_serialnum = 0;
5821 		csio = &done_ccb->csio;
5822 		priority = done_ccb->ccb_h.pinfo.priority;
5823 		serial_buf =
5824 		    (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
5825 
5826 		/* Clean up from previous instance of this device */
5827 		if (path->device->serial_num != NULL) {
5828 			free(path->device->serial_num, M_DEVBUF);
5829 			path->device->serial_num = NULL;
5830 			path->device->serial_num_len = 0;
5831 		}
5832 
5833 		if (serial_buf == NULL) {
5834 			/*
5835 			 * Don't process the command as it was never sent
5836 			 */
5837 		} else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
5838 			&& (serial_buf->length > 0)) {
5839 
5840 			have_serialnum = 1;
5841 			path->device->serial_num =
5842 				(u_int8_t *)malloc((serial_buf->length + 1),
5843 						   M_DEVBUF, M_NOWAIT);
5844 			if (path->device->serial_num != NULL) {
5845 				bcopy(serial_buf->serial_num,
5846 				      path->device->serial_num,
5847 				      serial_buf->length);
5848 				path->device->serial_num_len =
5849 				    serial_buf->length;
5850 				path->device->serial_num[serial_buf->length]
5851 				    = '\0';
5852 			}
5853 		} else if (cam_periph_error(done_ccb, 0,
5854 					    SF_RETRY_UA|SF_NO_PRINT,
5855 					    &softc->saved_ccb) == ERESTART) {
5856 			return;
5857 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5858 			/* Don't wedge the queue */
5859 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5860 					 /*run_queue*/TRUE);
5861 		}
5862 
5863 		/*
5864 		 * Let's see if we have seen this device before.
5865 		 */
5866 		if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
5867 			MD5_CTX context;
5868 			u_int8_t digest[16];
5869 
5870 			MD5Init(&context);
5871 
5872 			MD5Update(&context,
5873 				  (unsigned char *)&path->device->inq_data,
5874 				  sizeof(struct scsi_inquiry_data));
5875 
5876 			if (have_serialnum)
5877 				MD5Update(&context, serial_buf->serial_num,
5878 					  serial_buf->length);
5879 
5880 			MD5Final(digest, &context);
5881 			if (bcmp(softc->digest, digest, 16) == 0)
5882 				changed = 0;
5883 
5884 			/*
5885 			 * XXX Do we need to do a TUR in order to ensure
5886 			 *     that the device really hasn't changed???
5887 			 */
5888 			if ((changed != 0)
5889 			 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
5890 				xpt_async(AC_LOST_DEVICE, path, NULL);
5891 		}
5892 		if (serial_buf != NULL)
5893 			free(serial_buf, M_TEMP);
5894 
5895 		if (changed != 0) {
5896 			/*
5897 			 * Now that we have all the necessary
5898 			 * information to safely perform transfer
5899 			 * negotiations... Controllers don't perform
5900 			 * any negotiation or tagged queuing until
5901 			 * after the first XPT_SET_TRAN_SETTINGS ccb is
5902 			 * received.  So, on a new device, just retreive
5903 			 * the user settings, and set them as the current
5904 			 * settings to set the device up.
5905 			 */
5906 			proberequestdefaultnegotiation(periph);
5907 			xpt_release_ccb(done_ccb);
5908 
5909 			/*
5910 			 * Perform a TUR to allow the controller to
5911 			 * perform any necessary transfer negotiation.
5912 			 */
5913 			softc->action = PROBE_TUR_FOR_NEGOTIATION;
5914 			xpt_schedule(periph, priority);
5915 			return;
5916 		}
5917 		xpt_release_ccb(done_ccb);
5918 		break;
5919 	}
5920 	case PROBE_TUR_FOR_NEGOTIATION:
5921 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5922 			/* Don't wedge the queue */
5923 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5924 					 /*run_queue*/TRUE);
5925 		}
5926 
5927 		path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5928 
5929 		if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
5930 			/* Inform the XPT that a new device has been found */
5931 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5932 			xpt_action(done_ccb);
5933 
5934 			xpt_async(AC_FOUND_DEVICE, xpt_periph->path, done_ccb);
5935 		}
5936 		xpt_release_ccb(done_ccb);
5937 		break;
5938 	}
5939 	done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5940 	TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
5941 	done_ccb->ccb_h.status = CAM_REQ_CMP;
5942 	xpt_done(done_ccb);
5943 	if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
5944 		cam_periph_invalidate(periph);
5945 		cam_periph_release(periph);
5946 	} else {
5947 		probeschedule(periph);
5948 	}
5949 }
5950 
5951 static void
5952 probecleanup(struct cam_periph *periph)
5953 {
5954 	free(periph->softc, M_TEMP);
5955 }
5956 
5957 static void
5958 xpt_find_quirk(struct cam_ed *device)
5959 {
5960 	caddr_t	match;
5961 
5962 	match = cam_quirkmatch((caddr_t)&device->inq_data,
5963 			       (caddr_t)xpt_quirk_table,
5964 			       sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
5965 			       sizeof(*xpt_quirk_table), scsi_inquiry_match);
5966 
5967 	if (match == NULL)
5968 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
5969 
5970 	device->quirk = (struct xpt_quirk_entry *)match;
5971 }
5972 
5973 #ifdef CAM_NEW_TRAN_CODE
5974 
5975 static void
5976 xpt_devise_transport(struct cam_path *path)
5977 {
5978 	struct ccb_pathinq cpi;
5979 	struct ccb_trans_settings cts;
5980 	struct scsi_inquiry_data *inq_buf;
5981 
5982 	/* Get transport information from the SIM */
5983 	xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5984 	cpi.ccb_h.func_code = XPT_PATH_INQ;
5985 	xpt_action((union ccb *)&cpi);
5986 
5987 	inq_buf = NULL;
5988 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
5989 		inq_buf = &path->device->inq_data;
5990 	path->device->protocol = PROTO_SCSI;
5991 	path->device->protocol_version =
5992 	    inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
5993 	path->device->transport = cpi.transport;
5994 	path->device->transport_version = cpi.transport_version;
5995 
5996 	/*
5997 	 * Any device not using SPI3 features should
5998 	 * be considered SPI2 or lower.
5999 	 */
6000 	if (inq_buf != NULL) {
6001 		if (path->device->transport == XPORT_SPI
6002 		 && (inq_buf->spi3data & SID_SPI_MASK) == 0
6003 		 && path->device->transport_version > 2)
6004 			path->device->transport_version = 2;
6005 	} else {
6006 		struct cam_ed* otherdev;
6007 
6008 		for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
6009 		     otherdev != NULL;
6010 		     otherdev = TAILQ_NEXT(otherdev, links)) {
6011 			if (otherdev != path->device)
6012 				break;
6013 		}
6014 
6015 		if (otherdev != NULL) {
6016 			/*
6017 			 * Initially assume the same versioning as
6018 			 * prior luns for this target.
6019 			 */
6020 			path->device->protocol_version =
6021 			    otherdev->protocol_version;
6022 			path->device->transport_version =
6023 			    otherdev->transport_version;
6024 		} else {
6025 			/* Until we know better, opt for safty */
6026 			path->device->protocol_version = 2;
6027 			if (path->device->transport == XPORT_SPI)
6028 				path->device->transport_version = 2;
6029 			else
6030 				path->device->transport_version = 0;
6031 		}
6032 	}
6033 
6034 	/*
6035 	 * XXX
6036 	 * For a device compliant with SPC-2 we should be able
6037 	 * to determine the transport version supported by
6038 	 * scrutinizing the version descriptors in the
6039 	 * inquiry buffer.
6040 	 */
6041 
6042 	/* Tell the controller what we think */
6043 	xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
6044 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
6045 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
6046 	cts.transport = path->device->transport;
6047 	cts.transport_version = path->device->transport_version;
6048 	cts.protocol = path->device->protocol;
6049 	cts.protocol_version = path->device->protocol_version;
6050 	cts.proto_specific.valid = 0;
6051 	cts.xport_specific.valid = 0;
6052 	xpt_action((union ccb *)&cts);
6053 }
6054 
6055 static void
6056 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6057 			  int async_update)
6058 {
6059 	struct	ccb_pathinq cpi;
6060 	struct	ccb_trans_settings cur_cts;
6061 	struct	ccb_trans_settings_scsi *scsi;
6062 	struct	ccb_trans_settings_scsi *cur_scsi;
6063 	struct	cam_sim *sim;
6064 	struct	scsi_inquiry_data *inq_data;
6065 
6066 	if (device == NULL) {
6067 		cts->ccb_h.status = CAM_PATH_INVALID;
6068 		xpt_done((union ccb *)cts);
6069 		return;
6070 	}
6071 
6072 	if (cts->protocol == PROTO_UNKNOWN
6073 	 || cts->protocol == PROTO_UNSPECIFIED) {
6074 		cts->protocol = device->protocol;
6075 		cts->protocol_version = device->protocol_version;
6076 	}
6077 
6078 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
6079 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
6080 		cts->protocol_version = device->protocol_version;
6081 
6082 	if (cts->protocol != device->protocol) {
6083 		xpt_print_path(cts->ccb_h.path);
6084 		printf("Uninitialized Protocol %x:%x?\n",
6085 		       cts->protocol, device->protocol);
6086 		cts->protocol = device->protocol;
6087 	}
6088 
6089 	if (cts->protocol_version > device->protocol_version) {
6090 		if (bootverbose) {
6091 			xpt_print_path(cts->ccb_h.path);
6092 			printf("Down reving Protocol Version from %d to %d?\n",
6093 			       cts->protocol_version, device->protocol_version);
6094 		}
6095 		cts->protocol_version = device->protocol_version;
6096 	}
6097 
6098 	if (cts->transport == XPORT_UNKNOWN
6099 	 || cts->transport == XPORT_UNSPECIFIED) {
6100 		cts->transport = device->transport;
6101 		cts->transport_version = device->transport_version;
6102 	}
6103 
6104 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
6105 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
6106 		cts->transport_version = device->transport_version;
6107 
6108 	if (cts->transport != device->transport) {
6109 		xpt_print_path(cts->ccb_h.path);
6110 		printf("Uninitialized Transport %x:%x?\n",
6111 		       cts->transport, device->transport);
6112 		cts->transport = device->transport;
6113 	}
6114 
6115 	if (cts->transport_version > device->transport_version) {
6116 		if (bootverbose) {
6117 			xpt_print_path(cts->ccb_h.path);
6118 			printf("Down reving Transport Version from %d to %d?\n",
6119 			       cts->transport_version,
6120 			       device->transport_version);
6121 		}
6122 		cts->transport_version = device->transport_version;
6123 	}
6124 
6125 	sim = cts->ccb_h.path->bus->sim;
6126 
6127 	/*
6128 	 * Nothing more of interest to do unless
6129 	 * this is a device connected via the
6130 	 * SCSI protocol.
6131 	 */
6132 	if (cts->protocol != PROTO_SCSI) {
6133 		if (async_update == FALSE)
6134 			(*(sim->sim_action))(sim, (union ccb *)cts);
6135 		return;
6136 	}
6137 
6138 	inq_data = &device->inq_data;
6139 	scsi = &cts->proto_specific.scsi;
6140 	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6141 	cpi.ccb_h.func_code = XPT_PATH_INQ;
6142 	xpt_action((union ccb *)&cpi);
6143 
6144 	/* SCSI specific sanity checking */
6145 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6146 	 || (inq_data->flags & SID_CmdQue) == 0
6147 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6148 	 || (device->quirk->mintags == 0)) {
6149 		/*
6150 		 * Can't tag on hardware that doesn't support tags,
6151 		 * doesn't have it enabled, or has broken tag support.
6152 		 */
6153 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6154 	}
6155 
6156 	if (async_update == FALSE) {
6157 		/*
6158 		 * Perform sanity checking against what the
6159 		 * controller and device can do.
6160 		 */
6161 		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6162 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6163 		cur_cts.type = cts->type;
6164 		xpt_action((union ccb *)&cur_cts);
6165 
6166 		cur_scsi = &cur_cts.proto_specific.scsi;
6167 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
6168 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6169 			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
6170 		}
6171 		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
6172 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6173 	}
6174 
6175 	/* SPI specific sanity checking */
6176 	if (cts->transport == XPORT_SPI
6177 	 && async_update == FALSE) {
6178 		u_int spi3caps;
6179 		struct ccb_trans_settings_spi *spi;
6180 		struct ccb_trans_settings_spi *cur_spi;
6181 
6182 		spi = &cts->xport_specific.spi;
6183 
6184 		cur_spi = &cur_cts.xport_specific.spi;
6185 
6186 		/* Fill in any gaps in what the user gave us */
6187 		if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6188 			spi->sync_period = cur_spi->sync_period;
6189 		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6190 			spi->sync_period = 0;
6191 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6192 			spi->sync_offset = cur_spi->sync_offset;
6193 		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6194 			spi->sync_offset = 0;
6195 		if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6196 			spi->ppr_options = cur_spi->ppr_options;
6197 		if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6198 			spi->ppr_options = 0;
6199 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6200 			spi->bus_width = cur_spi->bus_width;
6201 		if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6202 			spi->bus_width = 0;
6203 		if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
6204 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6205 			spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
6206 		}
6207 		if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
6208 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6209 		if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6210 		  && (inq_data->flags & SID_Sync) == 0
6211 		  && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6212 		 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6213 		 || (cts->sync_offset == 0)
6214 		 || (cts->sync_period == 0)) {
6215 			/* Force async */
6216 			spi->sync_period = 0;
6217 			spi->sync_offset = 0;
6218 		}
6219 
6220 		switch (spi->bus_width) {
6221 		case MSG_EXT_WDTR_BUS_32_BIT:
6222 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6223 			  || (inq_data->flags & SID_WBus32) != 0
6224 			  || cts->type == CTS_TYPE_USER_SETTINGS)
6225 			 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6226 				break;
6227 			/* Fall Through to 16-bit */
6228 		case MSG_EXT_WDTR_BUS_16_BIT:
6229 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6230 			  || (inq_data->flags & SID_WBus16) != 0
6231 			  || cts->type == CTS_TYPE_USER_SETTINGS)
6232 			 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6233 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6234 				break;
6235 			}
6236 			/* Fall Through to 8-bit */
6237 		default: /* New bus width?? */
6238 		case MSG_EXT_WDTR_BUS_8_BIT:
6239 			/* All targets can do this */
6240 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6241 			break;
6242 		}
6243 
6244 		spi3caps = cpi.xport_specific.spi.ppr_options;
6245 		if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6246 		 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6247 			spi3caps &= inq_data->spi3data;
6248 
6249 		if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
6250 			spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
6251 
6252 		if ((spi3caps & SID_SPI_IUS) == 0)
6253 			spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
6254 
6255 		if ((spi3caps & SID_SPI_QAS) == 0)
6256 			spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
6257 
6258 		/* No SPI Transfer settings are allowed unless we are wide */
6259 		if (spi->bus_width == 0)
6260 			spi->ppr_options = 0;
6261 
6262 		if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0) {
6263 			/*
6264 			 * Can't tag queue without disconnection.
6265 			 */
6266 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6267 			scsi->valid |= CTS_SCSI_VALID_TQ;
6268 		}
6269 
6270 		/*
6271 		 * If we are currently performing tagged transactions to
6272 		 * this device and want to change its negotiation parameters,
6273 		 * go non-tagged for a bit to give the controller a chance to
6274 		 * negotiate unhampered by tag messages.
6275 		 */
6276 		if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6277 		 && (device->inq_flags & SID_CmdQue) != 0
6278 		 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6279 		 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
6280 				   CTS_SPI_VALID_SYNC_OFFSET|
6281 				   CTS_SPI_VALID_BUS_WIDTH)) != 0)
6282 			xpt_toggle_tags(cts->ccb_h.path);
6283 	}
6284 
6285 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6286 	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
6287 		int device_tagenb;
6288 
6289 		/*
6290 		 * If we are transitioning from tags to no-tags or
6291 		 * vice-versa, we need to carefully freeze and restart
6292 		 * the queue so that we don't overlap tagged and non-tagged
6293 		 * commands.  We also temporarily stop tags if there is
6294 		 * a change in transfer negotiation settings to allow
6295 		 * "tag-less" negotiation.
6296 		 */
6297 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6298 		 || (device->inq_flags & SID_CmdQue) != 0)
6299 			device_tagenb = TRUE;
6300 		else
6301 			device_tagenb = FALSE;
6302 
6303 		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6304 		  && device_tagenb == FALSE)
6305 		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
6306 		  && device_tagenb == TRUE)) {
6307 
6308 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
6309 				/*
6310 				 * Delay change to use tags until after a
6311 				 * few commands have gone to this device so
6312 				 * the controller has time to perform transfer
6313 				 * negotiations without tagged messages getting
6314 				 * in the way.
6315 				 */
6316 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6317 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6318 			} else {
6319 				struct ccb_relsim crs;
6320 
6321 				xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6322 		  		device->inq_flags &= ~SID_CmdQue;
6323 				xpt_dev_ccbq_resize(cts->ccb_h.path,
6324 						    sim->max_dev_openings);
6325 				device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6326 				device->tag_delay_count = 0;
6327 
6328 				xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6329 					      /*priority*/1);
6330 				crs.ccb_h.func_code = XPT_REL_SIMQ;
6331 				crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6332 				crs.openings
6333 				    = crs.release_timeout
6334 				    = crs.qfrozen_cnt
6335 				    = 0;
6336 				xpt_action((union ccb *)&crs);
6337 			}
6338 		}
6339 	}
6340 	if (async_update == FALSE)
6341 		(*(sim->sim_action))(sim, (union ccb *)cts);
6342 }
6343 
6344 #else /* CAM_NEW_TRAN_CODE */
6345 
6346 static void
6347 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6348 			  int async_update)
6349 {
6350 	struct	cam_sim *sim;
6351 	int	qfrozen;
6352 
6353 	sim = cts->ccb_h.path->bus->sim;
6354 	if (async_update == FALSE) {
6355 		struct	scsi_inquiry_data *inq_data;
6356 		struct	ccb_pathinq cpi;
6357 		struct	ccb_trans_settings cur_cts;
6358 
6359 		if (device == NULL) {
6360 			cts->ccb_h.status = CAM_PATH_INVALID;
6361 			xpt_done((union ccb *)cts);
6362 			return;
6363 		}
6364 
6365 		/*
6366 		 * Perform sanity checking against what the
6367 		 * controller and device can do.
6368 		 */
6369 		xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6370 		cpi.ccb_h.func_code = XPT_PATH_INQ;
6371 		xpt_action((union ccb *)&cpi);
6372 		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6373 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6374 		cur_cts.flags = CCB_TRANS_CURRENT_SETTINGS;
6375 		xpt_action((union ccb *)&cur_cts);
6376 		inq_data = &device->inq_data;
6377 
6378 		/* Fill in any gaps in what the user gave us */
6379 		if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0)
6380 			cts->sync_period = cur_cts.sync_period;
6381 		if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0)
6382 			cts->sync_offset = cur_cts.sync_offset;
6383 		if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) == 0)
6384 			cts->bus_width = cur_cts.bus_width;
6385 		if ((cts->valid & CCB_TRANS_DISC_VALID) == 0) {
6386 			cts->flags &= ~CCB_TRANS_DISC_ENB;
6387 			cts->flags |= cur_cts.flags & CCB_TRANS_DISC_ENB;
6388 		}
6389 		if ((cts->valid & CCB_TRANS_TQ_VALID) == 0) {
6390 			cts->flags &= ~CCB_TRANS_TAG_ENB;
6391 			cts->flags |= cur_cts.flags & CCB_TRANS_TAG_ENB;
6392 		}
6393 
6394 		if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6395 		  && (inq_data->flags & SID_Sync) == 0)
6396 		 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6397 		 || (cts->sync_offset == 0)
6398 		 || (cts->sync_period == 0)) {
6399 			/* Force async */
6400 			cts->sync_period = 0;
6401 			cts->sync_offset = 0;
6402 		} else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6403 			&& (inq_data->spi3data & SID_SPI_CLOCK_DT) == 0
6404 			&& cts->sync_period <= 0x9) {
6405 			/*
6406 			 * Don't allow DT transmission rates if the
6407 			 * device does not support it.
6408 			 */
6409 			cts->sync_period = 0xa;
6410 		}
6411 
6412 		switch (cts->bus_width) {
6413 		case MSG_EXT_WDTR_BUS_32_BIT:
6414 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6415 			  || (inq_data->flags & SID_WBus32) != 0)
6416 			 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6417 				break;
6418 			/* Fall Through to 16-bit */
6419 		case MSG_EXT_WDTR_BUS_16_BIT:
6420 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6421 			  || (inq_data->flags & SID_WBus16) != 0)
6422 			 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6423 				cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6424 				break;
6425 			}
6426 			/* Fall Through to 8-bit */
6427 		default: /* New bus width?? */
6428 		case MSG_EXT_WDTR_BUS_8_BIT:
6429 			/* All targets can do this */
6430 			cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6431 			break;
6432 		}
6433 
6434 		if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) {
6435 			/*
6436 			 * Can't tag queue without disconnection.
6437 			 */
6438 			cts->flags &= ~CCB_TRANS_TAG_ENB;
6439 			cts->valid |= CCB_TRANS_TQ_VALID;
6440 		}
6441 
6442 		if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6443 		 || (inq_data->flags & SID_CmdQue) == 0
6444 		 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6445 		 || (device->quirk->mintags == 0)) {
6446 			/*
6447 			 * Can't tag on hardware that doesn't support,
6448 			 * doesn't have it enabled, or has broken tag support.
6449 			 */
6450 			cts->flags &= ~CCB_TRANS_TAG_ENB;
6451 		}
6452 	}
6453 
6454 	qfrozen = FALSE;
6455 	if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
6456 		int device_tagenb;
6457 
6458 		/*
6459 		 * If we are transitioning from tags to no-tags or
6460 		 * vice-versa, we need to carefully freeze and restart
6461 		 * the queue so that we don't overlap tagged and non-tagged
6462 		 * commands.  We also temporarily stop tags if there is
6463 		 * a change in transfer negotiation settings to allow
6464 		 * "tag-less" negotiation.
6465 		 */
6466 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6467 		 || (device->inq_flags & SID_CmdQue) != 0)
6468 			device_tagenb = TRUE;
6469 		else
6470 			device_tagenb = FALSE;
6471 
6472 		if (((cts->flags & CCB_TRANS_TAG_ENB) != 0
6473 		  && device_tagenb == FALSE)
6474 		 || ((cts->flags & CCB_TRANS_TAG_ENB) == 0
6475 		  && device_tagenb == TRUE)) {
6476 
6477 			if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
6478 				/*
6479 				 * Delay change to use tags until after a
6480 				 * few commands have gone to this device so
6481 				 * the controller has time to perform transfer
6482 				 * negotiations without tagged messages getting
6483 				 * in the way.
6484 				 */
6485 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6486 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6487 			} else {
6488 				xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6489 				qfrozen = TRUE;
6490 		  		device->inq_flags &= ~SID_CmdQue;
6491 				xpt_dev_ccbq_resize(cts->ccb_h.path,
6492 						    sim->max_dev_openings);
6493 				device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6494 				device->tag_delay_count = 0;
6495 			}
6496 		}
6497 	}
6498 
6499 	if (async_update == FALSE) {
6500 		/*
6501 		 * If we are currently performing tagged transactions to
6502 		 * this device and want to change its negotiation parameters,
6503 		 * go non-tagged for a bit to give the controller a chance to
6504 		 * negotiate unhampered by tag messages.
6505 		 */
6506 		if ((device->inq_flags & SID_CmdQue) != 0
6507 		 && (cts->flags & (CCB_TRANS_SYNC_RATE_VALID|
6508 				   CCB_TRANS_SYNC_OFFSET_VALID|
6509 				   CCB_TRANS_BUS_WIDTH_VALID)) != 0)
6510 			xpt_toggle_tags(cts->ccb_h.path);
6511 
6512 		(*(sim->sim_action))(sim, (union ccb *)cts);
6513 	}
6514 
6515 	if (qfrozen) {
6516 		struct ccb_relsim crs;
6517 
6518 		xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6519 			      /*priority*/1);
6520 		crs.ccb_h.func_code = XPT_REL_SIMQ;
6521 		crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6522 		crs.openings
6523 		    = crs.release_timeout
6524 		    = crs.qfrozen_cnt
6525 		    = 0;
6526 		xpt_action((union ccb *)&crs);
6527 	}
6528 }
6529 
6530 
6531 #endif /* CAM_NEW_TRAN_CODE */
6532 
6533 static void
6534 xpt_toggle_tags(struct cam_path *path)
6535 {
6536 	struct cam_ed *dev;
6537 
6538 	/*
6539 	 * Give controllers a chance to renegotiate
6540 	 * before starting tag operations.  We
6541 	 * "toggle" tagged queuing off then on
6542 	 * which causes the tag enable command delay
6543 	 * counter to come into effect.
6544 	 */
6545 	dev = path->device;
6546 	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6547 	 || ((dev->inq_flags & SID_CmdQue) != 0
6548  	  && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
6549 		struct ccb_trans_settings cts;
6550 
6551 		xpt_setup_ccb(&cts.ccb_h, path, 1);
6552 #ifdef CAM_NEW_TRAN_CODE
6553 		cts.protocol = PROTO_SCSI;
6554 		cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
6555 		cts.transport = XPORT_UNSPECIFIED;
6556 		cts.transport_version = XPORT_VERSION_UNSPECIFIED;
6557 		cts.proto_specific.scsi.flags = 0;
6558 		cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
6559 #else /* CAM_NEW_TRAN_CODE */
6560 		cts.flags = 0;
6561 		cts.valid = CCB_TRANS_TQ_VALID;
6562 #endif /* CAM_NEW_TRAN_CODE */
6563 		xpt_set_transfer_settings(&cts, path->device,
6564 					  /*async_update*/TRUE);
6565 #ifdef CAM_NEW_TRAN_CODE
6566 		cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
6567 #else /* CAM_NEW_TRAN_CODE */
6568 		cts.flags = CCB_TRANS_TAG_ENB;
6569 #endif /* CAM_NEW_TRAN_CODE */
6570 		xpt_set_transfer_settings(&cts, path->device,
6571 					  /*async_update*/TRUE);
6572 	}
6573 }
6574 
6575 static void
6576 xpt_start_tags(struct cam_path *path)
6577 {
6578 	struct ccb_relsim crs;
6579 	struct cam_ed *device;
6580 	struct cam_sim *sim;
6581 	int    newopenings;
6582 
6583 	device = path->device;
6584 	sim = path->bus->sim;
6585 	device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6586 	xpt_freeze_devq(path, /*count*/1);
6587 	device->inq_flags |= SID_CmdQue;
6588 	newopenings = min(device->quirk->maxtags, sim->max_tagged_dev_openings);
6589 	xpt_dev_ccbq_resize(path, newopenings);
6590 	xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6591 	crs.ccb_h.func_code = XPT_REL_SIMQ;
6592 	crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6593 	crs.openings
6594 	    = crs.release_timeout
6595 	    = crs.qfrozen_cnt
6596 	    = 0;
6597 	xpt_action((union ccb *)&crs);
6598 }
6599 
6600 static int busses_to_config;
6601 static int busses_to_reset;
6602 
6603 static int
6604 xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
6605 {
6606 	if (bus->path_id != CAM_XPT_PATH_ID) {
6607 		struct cam_path path;
6608 		struct ccb_pathinq cpi;
6609 		int can_negotiate;
6610 
6611 		busses_to_config++;
6612 		xpt_compile_path(&path, NULL, bus->path_id,
6613 				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
6614 		xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
6615 		cpi.ccb_h.func_code = XPT_PATH_INQ;
6616 		xpt_action((union ccb *)&cpi);
6617 		can_negotiate = cpi.hba_inquiry;
6618 		can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6619 		if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
6620 		 && can_negotiate)
6621 			busses_to_reset++;
6622 		xpt_release_path(&path);
6623 	}
6624 
6625 	return(1);
6626 }
6627 
6628 static int
6629 xptconfigfunc(struct cam_eb *bus, void *arg)
6630 {
6631 	struct	cam_path *path;
6632 	union	ccb *work_ccb;
6633 
6634 	if (bus->path_id != CAM_XPT_PATH_ID) {
6635 		cam_status status;
6636 		int can_negotiate;
6637 
6638 		work_ccb = xpt_alloc_ccb();
6639 		if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6640 					      CAM_TARGET_WILDCARD,
6641 					      CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6642 			printf("xptconfigfunc: xpt_create_path failed with "
6643 			       "status %#x for bus %d\n", status, bus->path_id);
6644 			printf("xptconfigfunc: halting bus configuration\n");
6645 			xpt_free_ccb(work_ccb);
6646 			busses_to_config--;
6647 			xpt_finishconfig(xpt_periph, NULL);
6648 			return(0);
6649 		}
6650 		xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6651 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6652 		xpt_action(work_ccb);
6653 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6654 			printf("xptconfigfunc: CPI failed on bus %d "
6655 			       "with status %d\n", bus->path_id,
6656 			       work_ccb->ccb_h.status);
6657 			xpt_finishconfig(xpt_periph, work_ccb);
6658 			return(1);
6659 		}
6660 
6661 		can_negotiate = work_ccb->cpi.hba_inquiry;
6662 		can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6663 		if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6664 		 && (can_negotiate != 0)) {
6665 			xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6666 			work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6667 			work_ccb->ccb_h.cbfcnp = NULL;
6668 			CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
6669 				  ("Resetting Bus\n"));
6670 			xpt_action(work_ccb);
6671 			xpt_finishconfig(xpt_periph, work_ccb);
6672 		} else {
6673 			/* Act as though we performed a successful BUS RESET */
6674 			work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6675 			xpt_finishconfig(xpt_periph, work_ccb);
6676 		}
6677 	}
6678 
6679 	return(1);
6680 }
6681 
6682 static void
6683 xpt_config(void *arg)
6684 {
6685 	/*
6686 	 * Now that interrupts are enabled, go find our devices
6687 	 */
6688 
6689 #ifdef CAMDEBUG
6690 	/* Setup debugging flags and path */
6691 #ifdef CAM_DEBUG_FLAGS
6692 	cam_dflags = CAM_DEBUG_FLAGS;
6693 #else /* !CAM_DEBUG_FLAGS */
6694 	cam_dflags = CAM_DEBUG_NONE;
6695 #endif /* CAM_DEBUG_FLAGS */
6696 #ifdef CAM_DEBUG_BUS
6697 	if (cam_dflags != CAM_DEBUG_NONE) {
6698 		if (xpt_create_path(&cam_dpath, xpt_periph,
6699 				    CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
6700 				    CAM_DEBUG_LUN) != CAM_REQ_CMP) {
6701 			printf("xpt_config: xpt_create_path() failed for debug"
6702 			       " target %d:%d:%d, debugging disabled\n",
6703 			       CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
6704 			cam_dflags = CAM_DEBUG_NONE;
6705 		}
6706 	} else
6707 		cam_dpath = NULL;
6708 #else /* !CAM_DEBUG_BUS */
6709 	cam_dpath = NULL;
6710 #endif /* CAM_DEBUG_BUS */
6711 #endif /* CAMDEBUG */
6712 
6713 	/*
6714 	 * Scan all installed busses.
6715 	 */
6716 	xpt_for_all_busses(xptconfigbuscountfunc, NULL);
6717 
6718 	if (busses_to_config == 0) {
6719 		/* Call manually because we don't have any busses */
6720 		xpt_finishconfig(xpt_periph, NULL);
6721 	} else  {
6722 		if (busses_to_reset > 0 && SCSI_DELAY >= 2000) {
6723 			printf("Waiting %d seconds for SCSI "
6724 			       "devices to settle\n", SCSI_DELAY/1000);
6725 		}
6726 		xpt_for_all_busses(xptconfigfunc, NULL);
6727 	}
6728 }
6729 
6730 /*
6731  * If the given device only has one peripheral attached to it, and if that
6732  * peripheral is the passthrough driver, announce it.  This insures that the
6733  * user sees some sort of announcement for every peripheral in their system.
6734  */
6735 static int
6736 xptpassannouncefunc(struct cam_ed *device, void *arg)
6737 {
6738 	struct cam_periph *periph;
6739 	int i;
6740 
6741 	for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
6742 	     periph = SLIST_NEXT(periph, periph_links), i++);
6743 
6744 	periph = SLIST_FIRST(&device->periphs);
6745 	if ((i == 1)
6746 	 && (strncmp(periph->periph_name, "pass", 4) == 0))
6747 		xpt_announce_periph(periph, NULL);
6748 
6749 	return(1);
6750 }
6751 
6752 static void
6753 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
6754 {
6755 	struct	periph_driver **p_drv;
6756 	int	i;
6757 
6758 	if (done_ccb != NULL) {
6759 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
6760 			  ("xpt_finishconfig\n"));
6761 		switch(done_ccb->ccb_h.func_code) {
6762 		case XPT_RESET_BUS:
6763 			if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
6764 				done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
6765 				done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
6766 				xpt_action(done_ccb);
6767 				return;
6768 			}
6769 			/* FALLTHROUGH */
6770 		case XPT_SCAN_BUS:
6771 		default:
6772 			xpt_free_path(done_ccb->ccb_h.path);
6773 			busses_to_config--;
6774 			break;
6775 		}
6776 	}
6777 
6778 	if (busses_to_config == 0) {
6779 		/* Register all the peripheral drivers */
6780 		/* XXX This will have to change when we have loadable modules */
6781 		p_drv = periph_drivers;
6782 		for (i = 0; p_drv[i] != NULL; i++) {
6783 			(*p_drv[i]->init)();
6784 		}
6785 
6786 		/*
6787 		 * Check for devices with no "standard" peripheral driver
6788 		 * attached.  For any devices like that, announce the
6789 		 * passthrough driver so the user will see something.
6790 		 */
6791 		xpt_for_all_devices(xptpassannouncefunc, NULL);
6792 
6793 		/* Release our hook so that the boot can continue. */
6794 		config_intrhook_disestablish(xpt_config_hook);
6795 		free(xpt_config_hook, M_TEMP);
6796 		xpt_config_hook = NULL;
6797 	}
6798 	if (done_ccb != NULL)
6799 		xpt_free_ccb(done_ccb);
6800 }
6801 
6802 static void
6803 xptaction(struct cam_sim *sim, union ccb *work_ccb)
6804 {
6805 	CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
6806 
6807 	switch (work_ccb->ccb_h.func_code) {
6808 	/* Common cases first */
6809 	case XPT_PATH_INQ:		/* Path routing inquiry */
6810 	{
6811 		struct ccb_pathinq *cpi;
6812 
6813 		cpi = &work_ccb->cpi;
6814 		cpi->version_num = 1; /* XXX??? */
6815 		cpi->hba_inquiry = 0;
6816 		cpi->target_sprt = 0;
6817 		cpi->hba_misc = 0;
6818 		cpi->hba_eng_cnt = 0;
6819 		cpi->max_target = 0;
6820 		cpi->max_lun = 0;
6821 		cpi->initiator_id = 0;
6822 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
6823 		strncpy(cpi->hba_vid, "", HBA_IDLEN);
6824 		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
6825 		cpi->unit_number = sim->unit_number;
6826 		cpi->bus_id = sim->bus_id;
6827 		cpi->base_transfer_speed = 0;
6828 #ifdef CAM_NEW_TRAN_CODE
6829 		cpi->protocol = PROTO_UNSPECIFIED;
6830 		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
6831 		cpi->transport = XPORT_UNSPECIFIED;
6832 		cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
6833 #endif /* CAM_NEW_TRAN_CODE */
6834 		cpi->ccb_h.status = CAM_REQ_CMP;
6835 		xpt_done(work_ccb);
6836 		break;
6837 	}
6838 	default:
6839 		work_ccb->ccb_h.status = CAM_REQ_INVALID;
6840 		xpt_done(work_ccb);
6841 		break;
6842 	}
6843 }
6844 
6845 /*
6846  * The xpt as a "controller" has no interrupt sources, so polling
6847  * is a no-op.
6848  */
6849 static void
6850 xptpoll(struct cam_sim *sim)
6851 {
6852 }
6853 
6854 static void
6855 camisr(void *V_queue)
6856 {
6857 	cam_isrq_t *queue = V_queue;
6858 	int	s;
6859 	struct	ccb_hdr *ccb_h;
6860 
6861 	s = splcam();
6862 	while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
6863 		int	runq;
6864 
6865 		TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
6866 		ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
6867 		splx(s);
6868 
6869 		CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
6870 			  ("camisr"));
6871 
6872 		runq = FALSE;
6873 
6874 		if (ccb_h->flags & CAM_HIGH_POWER) {
6875 			struct highpowerlist	*hphead;
6876 			struct cam_ed		*device;
6877 			union ccb		*send_ccb;
6878 
6879 			hphead = &highpowerq;
6880 
6881 			send_ccb = (union ccb *)STAILQ_FIRST(hphead);
6882 
6883 			/*
6884 			 * Increment the count since this command is done.
6885 			 */
6886 			num_highpower++;
6887 
6888 			/*
6889 			 * Any high powered commands queued up?
6890 			 */
6891 			if (send_ccb != NULL) {
6892 				device = send_ccb->ccb_h.path->device;
6893 
6894 				STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
6895 
6896 				xpt_release_devq(send_ccb->ccb_h.path,
6897 						 /*count*/1, /*runqueue*/TRUE);
6898 			}
6899 		}
6900 		if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
6901 			struct cam_ed *dev;
6902 
6903 			dev = ccb_h->path->device;
6904 
6905 			s = splcam();
6906 			cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
6907 
6908 			ccb_h->path->bus->sim->devq->send_active--;
6909 			ccb_h->path->bus->sim->devq->send_openings++;
6910 			splx(s);
6911 
6912 			if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
6913 			  && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
6914 			 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
6915 			  && (dev->ccbq.dev_active == 0))) {
6916 
6917 				xpt_release_devq(ccb_h->path, /*count*/1,
6918 						 /*run_queue*/TRUE);
6919 			}
6920 
6921 			if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6922 			 && (--dev->tag_delay_count == 0))
6923 				xpt_start_tags(ccb_h->path);
6924 
6925 			if ((dev->ccbq.queue.entries > 0)
6926 			 && (dev->qfrozen_cnt == 0)
6927 			 && (device_is_send_queued(dev) == 0)) {
6928 				runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
6929 							      dev);
6930 			}
6931 		}
6932 
6933 		if (ccb_h->status & CAM_RELEASE_SIMQ) {
6934 			xpt_release_simq(ccb_h->path->bus->sim,
6935 					 /*run_queue*/TRUE);
6936 			ccb_h->status &= ~CAM_RELEASE_SIMQ;
6937 			runq = FALSE;
6938 		}
6939 
6940 		if ((ccb_h->flags & CAM_DEV_QFRZDIS)
6941 		 && (ccb_h->status & CAM_DEV_QFRZN)) {
6942 			xpt_release_devq(ccb_h->path, /*count*/1,
6943 					 /*run_queue*/TRUE);
6944 			ccb_h->status &= ~CAM_DEV_QFRZN;
6945 		} else if (runq) {
6946 			xpt_run_dev_sendq(ccb_h->path->bus);
6947 		}
6948 
6949 		/* Call the peripheral driver's callback */
6950 		(*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
6951 
6952 		/* Raise IPL for while test */
6953 		s = splcam();
6954 	}
6955 	splx(s);
6956 }
6957