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