xref: /freebsd/sys/cam/cam_xpt.c (revision b3a1f9373a31b644f8a65de1ba35929af3f6a9fe)
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 (INQ_DATA_TQ_ENABLED(&dev->inq_data)) {
3506 				/* Don't ever go below one opening */
3507 				if (crs->openings > 0) {
3508 					xpt_dev_ccbq_resize(crs->ccb_h.path,
3509 							    crs->openings);
3510 
3511 					if (bootverbose) {
3512 						xpt_print_path(crs->ccb_h.path);
3513 						printf("tagged openings "
3514 						       "now %d\n",
3515 						       crs->openings);
3516 					}
3517 				}
3518 			}
3519 		}
3520 
3521 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3522 
3523 			if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3524 
3525 				/*
3526 				 * Just extend the old timeout and decrement
3527 				 * the freeze count so that a single timeout
3528 				 * is sufficient for releasing the queue.
3529 				 */
3530 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3531 				untimeout(xpt_release_devq_timeout,
3532 					  dev, dev->c_handle);
3533 			} else {
3534 
3535 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3536 			}
3537 
3538 			dev->c_handle =
3539 				timeout(xpt_release_devq_timeout,
3540 					dev,
3541 					(crs->release_timeout * hz) / 1000);
3542 
3543 			dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3544 
3545 		}
3546 
3547 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3548 
3549 			if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3550 				/*
3551 				 * Decrement the freeze count so that a single
3552 				 * completion is still sufficient to unfreeze
3553 				 * the queue.
3554 				 */
3555 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3556 			} else {
3557 
3558 				dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3559 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3560 			}
3561 		}
3562 
3563 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3564 
3565 			if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3566 			 || (dev->ccbq.dev_active == 0)) {
3567 
3568 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3569 			} else {
3570 
3571 				dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3572 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3573 			}
3574 		}
3575 		splx(s);
3576 
3577 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3578 
3579 			xpt_release_devq(crs->ccb_h.path, /*count*/1,
3580 					 /*run_queue*/TRUE);
3581 		}
3582 		start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3583 		start_ccb->ccb_h.status = CAM_REQ_CMP;
3584 		break;
3585 	}
3586 	case XPT_SCAN_BUS:
3587 		xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3588 		break;
3589 	case XPT_SCAN_LUN:
3590 		xpt_scan_lun(start_ccb->ccb_h.path->periph,
3591 			     start_ccb->ccb_h.path, start_ccb->crcn.flags,
3592 			     start_ccb);
3593 		break;
3594 	case XPT_DEBUG: {
3595 #ifdef CAMDEBUG
3596 		int s;
3597 
3598 		s = splcam();
3599 #ifdef CAM_DEBUG_DELAY
3600 		cam_debug_delay = CAM_DEBUG_DELAY;
3601 #endif
3602 		cam_dflags = start_ccb->cdbg.flags;
3603 		if (cam_dpath != NULL) {
3604 			xpt_free_path(cam_dpath);
3605 			cam_dpath = NULL;
3606 		}
3607 
3608 		if (cam_dflags != CAM_DEBUG_NONE) {
3609 			if (xpt_create_path(&cam_dpath, xpt_periph,
3610 					    start_ccb->ccb_h.path_id,
3611 					    start_ccb->ccb_h.target_id,
3612 					    start_ccb->ccb_h.target_lun) !=
3613 					    CAM_REQ_CMP) {
3614 				start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3615 				cam_dflags = CAM_DEBUG_NONE;
3616 			} else {
3617 				start_ccb->ccb_h.status = CAM_REQ_CMP;
3618 				xpt_print_path(cam_dpath);
3619 				printf("debugging flags now %x\n", cam_dflags);
3620 			}
3621 		} else {
3622 			cam_dpath = NULL;
3623 			start_ccb->ccb_h.status = CAM_REQ_CMP;
3624 		}
3625 		splx(s);
3626 #else /* !CAMDEBUG */
3627 		start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3628 #endif /* CAMDEBUG */
3629 		break;
3630 	}
3631 	case XPT_NOOP:
3632 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3633 			xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3634 		start_ccb->ccb_h.status = CAM_REQ_CMP;
3635 		break;
3636 	default:
3637 	case XPT_SDEV_TYPE:
3638 	case XPT_TERM_IO:
3639 	case XPT_ENG_INQ:
3640 		/* XXX Implement */
3641 		start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3642 		break;
3643 	}
3644 	splx(iopl);
3645 }
3646 
3647 void
3648 xpt_polled_action(union ccb *start_ccb)
3649 {
3650 	int	  s;
3651 	u_int32_t timeout;
3652 	struct	  cam_sim *sim;
3653 	struct	  cam_devq *devq;
3654 	struct	  cam_ed *dev;
3655 
3656 	GIANT_REQUIRED;
3657 
3658 	timeout = start_ccb->ccb_h.timeout;
3659 	sim = start_ccb->ccb_h.path->bus->sim;
3660 	devq = sim->devq;
3661 	dev = start_ccb->ccb_h.path->device;
3662 
3663 	s = splcam();
3664 
3665 	/*
3666 	 * Steal an opening so that no other queued requests
3667 	 * can get it before us while we simulate interrupts.
3668 	 */
3669 	dev->ccbq.devq_openings--;
3670 	dev->ccbq.dev_openings--;
3671 
3672 	while(((devq != NULL && devq->send_openings <= 0) ||
3673 	   dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
3674 		DELAY(1000);
3675 		(*(sim->sim_poll))(sim);
3676 		camisr(&cam_bioq);
3677 	}
3678 
3679 	dev->ccbq.devq_openings++;
3680 	dev->ccbq.dev_openings++;
3681 
3682 	if (timeout != 0) {
3683 		xpt_action(start_ccb);
3684 		while(--timeout > 0) {
3685 			(*(sim->sim_poll))(sim);
3686 			camisr(&cam_bioq);
3687 			if ((start_ccb->ccb_h.status  & CAM_STATUS_MASK)
3688 			    != CAM_REQ_INPROG)
3689 				break;
3690 			DELAY(1000);
3691 		}
3692 		if (timeout == 0) {
3693 			/*
3694 			 * XXX Is it worth adding a sim_timeout entry
3695 			 * point so we can attempt recovery?  If
3696 			 * this is only used for dumps, I don't think
3697 			 * it is.
3698 			 */
3699 			start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3700 		}
3701 	} else {
3702 		start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3703 	}
3704 	splx(s);
3705 }
3706 
3707 /*
3708  * Schedule a peripheral driver to receive a ccb when it's
3709  * target device has space for more transactions.
3710  */
3711 void
3712 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3713 {
3714 	struct cam_ed *device;
3715 	union ccb *work_ccb;
3716 	int s;
3717 	int runq;
3718 
3719 	GIANT_REQUIRED;
3720 
3721 	CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3722 	device = perph->path->device;
3723 	s = splsoftcam();
3724 	if (periph_is_queued(perph)) {
3725 		/* Simply reorder based on new priority */
3726 		CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3727 			  ("   change priority to %d\n", new_priority));
3728 		if (new_priority < perph->pinfo.priority) {
3729 			camq_change_priority(&device->drvq,
3730 					     perph->pinfo.index,
3731 					     new_priority);
3732 		}
3733 		runq = 0;
3734 	} else if (SIM_DEAD(perph->path->bus->sim)) {
3735 		/* The SIM is gone so just call periph_start directly. */
3736 		work_ccb = xpt_get_ccb(perph->path->device);
3737 		splx(s);
3738 		if (work_ccb == NULL)
3739 			return; /* XXX */
3740 		xpt_setup_ccb(&work_ccb->ccb_h, perph->path, new_priority);
3741 		perph->pinfo.priority = new_priority;
3742 		perph->periph_start(perph, work_ccb);
3743 		return;
3744 	} else {
3745 		/* New entry on the queue */
3746 		CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3747 			  ("   added periph to queue\n"));
3748 		perph->pinfo.priority = new_priority;
3749 		perph->pinfo.generation = ++device->drvq.generation;
3750 		camq_insert(&device->drvq, &perph->pinfo);
3751 		runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3752 	}
3753 	splx(s);
3754 	if (runq != 0) {
3755 		CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3756 			  ("   calling xpt_run_devq\n"));
3757 		xpt_run_dev_allocq(perph->path->bus);
3758 	}
3759 }
3760 
3761 
3762 /*
3763  * Schedule a device to run on a given queue.
3764  * If the device was inserted as a new entry on the queue,
3765  * return 1 meaning the device queue should be run. If we
3766  * were already queued, implying someone else has already
3767  * started the queue, return 0 so the caller doesn't attempt
3768  * to run the queue.  Must be run at either splsoftcam
3769  * (or splcam since that encompases splsoftcam).
3770  */
3771 static int
3772 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3773 		 u_int32_t new_priority)
3774 {
3775 	int retval;
3776 	u_int32_t old_priority;
3777 
3778 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3779 
3780 	old_priority = pinfo->priority;
3781 
3782 	/*
3783 	 * Are we already queued?
3784 	 */
3785 	if (pinfo->index != CAM_UNQUEUED_INDEX) {
3786 		/* Simply reorder based on new priority */
3787 		if (new_priority < old_priority) {
3788 			camq_change_priority(queue, pinfo->index,
3789 					     new_priority);
3790 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3791 					("changed priority to %d\n",
3792 					 new_priority));
3793 		}
3794 		retval = 0;
3795 	} else {
3796 		/* New entry on the queue */
3797 		if (new_priority < old_priority)
3798 			pinfo->priority = new_priority;
3799 
3800 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3801 				("Inserting onto queue\n"));
3802 		pinfo->generation = ++queue->generation;
3803 		camq_insert(queue, pinfo);
3804 		retval = 1;
3805 	}
3806 	return (retval);
3807 }
3808 
3809 static void
3810 xpt_run_dev_allocq(struct cam_eb *bus)
3811 {
3812 	struct	cam_devq *devq;
3813 	int	s;
3814 
3815 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3816 	devq = bus->sim->devq;
3817 
3818 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3819 			("   qfrozen_cnt == 0x%x, entries == %d, "
3820 			 "openings == %d, active == %d\n",
3821 			 devq->alloc_queue.qfrozen_cnt,
3822 			 devq->alloc_queue.entries,
3823 			 devq->alloc_openings,
3824 			 devq->alloc_active));
3825 
3826 	s = splsoftcam();
3827 	devq->alloc_queue.qfrozen_cnt++;
3828 	while ((devq->alloc_queue.entries > 0)
3829 	    && (devq->alloc_openings > 0)
3830 	    && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3831 		struct	cam_ed_qinfo *qinfo;
3832 		struct	cam_ed *device;
3833 		union	ccb *work_ccb;
3834 		struct	cam_periph *drv;
3835 		struct	camq *drvq;
3836 
3837 		qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3838 							   CAMQ_HEAD);
3839 		device = qinfo->device;
3840 
3841 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3842 				("running device %p\n", device));
3843 
3844 		drvq = &device->drvq;
3845 
3846 #ifdef CAMDEBUG
3847 		if (drvq->entries <= 0) {
3848 			panic("xpt_run_dev_allocq: "
3849 			      "Device on queue without any work to do");
3850 		}
3851 #endif
3852 		if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3853 			devq->alloc_openings--;
3854 			devq->alloc_active++;
3855 			drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3856 			splx(s);
3857 			xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3858 				      drv->pinfo.priority);
3859 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3860 					("calling periph start\n"));
3861 			drv->periph_start(drv, work_ccb);
3862 		} else {
3863 			/*
3864 			 * Malloc failure in alloc_ccb
3865 			 */
3866 			/*
3867 			 * XXX add us to a list to be run from free_ccb
3868 			 * if we don't have any ccbs active on this
3869 			 * device queue otherwise we may never get run
3870 			 * again.
3871 			 */
3872 			break;
3873 		}
3874 
3875 		/* Raise IPL for possible insertion and test at top of loop */
3876 		s = splsoftcam();
3877 
3878 		if (drvq->entries > 0) {
3879 			/* We have more work.  Attempt to reschedule */
3880 			xpt_schedule_dev_allocq(bus, device);
3881 		}
3882 	}
3883 	devq->alloc_queue.qfrozen_cnt--;
3884 	splx(s);
3885 }
3886 
3887 static void
3888 xpt_run_dev_sendq(struct cam_eb *bus)
3889 {
3890 	struct	cam_devq *devq;
3891 	int	s;
3892 
3893 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3894 
3895 	devq = bus->sim->devq;
3896 
3897 	s = splcam();
3898 	devq->send_queue.qfrozen_cnt++;
3899 	splx(s);
3900 	s = splsoftcam();
3901 	while ((devq->send_queue.entries > 0)
3902 	    && (devq->send_openings > 0)) {
3903 		struct	cam_ed_qinfo *qinfo;
3904 		struct	cam_ed *device;
3905 		union ccb *work_ccb;
3906 		struct	cam_sim *sim;
3907 		int	ospl;
3908 
3909 		ospl = splcam();
3910 	    	if (devq->send_queue.qfrozen_cnt > 1) {
3911 			splx(ospl);
3912 			break;
3913 		}
3914 
3915 		qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3916 							   CAMQ_HEAD);
3917 		device = qinfo->device;
3918 
3919 		/*
3920 		 * If the device has been "frozen", don't attempt
3921 		 * to run it.
3922 		 */
3923 		if (device->qfrozen_cnt > 0) {
3924 			splx(ospl);
3925 			continue;
3926 		}
3927 
3928 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3929 				("running device %p\n", device));
3930 
3931 		work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3932 		if (work_ccb == NULL) {
3933 			printf("device on run queue with no ccbs???\n");
3934 			splx(ospl);
3935 			continue;
3936 		}
3937 
3938 		if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3939 
3940 		 	if (num_highpower <= 0) {
3941 				/*
3942 				 * We got a high power command, but we
3943 				 * don't have any available slots.  Freeze
3944 				 * the device queue until we have a slot
3945 				 * available.
3946 				 */
3947 				device->qfrozen_cnt++;
3948 				STAILQ_INSERT_TAIL(&highpowerq,
3949 						   &work_ccb->ccb_h,
3950 						   xpt_links.stqe);
3951 
3952 				splx(ospl);
3953 				continue;
3954 			} else {
3955 				/*
3956 				 * Consume a high power slot while
3957 				 * this ccb runs.
3958 				 */
3959 				num_highpower--;
3960 			}
3961 		}
3962 		devq->active_dev = device;
3963 		cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3964 
3965 		cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3966 		splx(ospl);
3967 
3968 		devq->send_openings--;
3969 		devq->send_active++;
3970 
3971 		if (device->ccbq.queue.entries > 0)
3972 			xpt_schedule_dev_sendq(bus, device);
3973 
3974 		if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3975 			/*
3976 			 * The client wants to freeze the queue
3977 			 * after this CCB is sent.
3978 			 */
3979 			ospl = splcam();
3980 			device->qfrozen_cnt++;
3981 			splx(ospl);
3982 		}
3983 
3984 		splx(s);
3985 
3986 		/* In Target mode, the peripheral driver knows best... */
3987 		if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3988 			if ((device->inq_flags & SID_CmdQue) != 0
3989 			 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3990 				work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3991 			else
3992 				/*
3993 				 * Clear this in case of a retried CCB that
3994 				 * failed due to a rejected tag.
3995 				 */
3996 				work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3997 		}
3998 
3999 		/*
4000 		 * Device queues can be shared among multiple sim instances
4001 		 * that reside on different busses.  Use the SIM in the queue
4002 		 * CCB's path, rather than the one in the bus that was passed
4003 		 * into this function.
4004 		 */
4005 		sim = work_ccb->ccb_h.path->bus->sim;
4006 		(*(sim->sim_action))(sim, work_ccb);
4007 
4008 		ospl = splcam();
4009 		devq->active_dev = NULL;
4010 		splx(ospl);
4011 		/* Raise IPL for possible insertion and test at top of loop */
4012 		s = splsoftcam();
4013 	}
4014 	splx(s);
4015 	s = splcam();
4016 	devq->send_queue.qfrozen_cnt--;
4017 	splx(s);
4018 }
4019 
4020 /*
4021  * This function merges stuff from the slave ccb into the master ccb, while
4022  * keeping important fields in the master ccb constant.
4023  */
4024 void
4025 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
4026 {
4027 	GIANT_REQUIRED;
4028 
4029 	/*
4030 	 * Pull fields that are valid for peripheral drivers to set
4031 	 * into the master CCB along with the CCB "payload".
4032 	 */
4033 	master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
4034 	master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
4035 	master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
4036 	master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
4037 	bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
4038 	      sizeof(union ccb) - sizeof(struct ccb_hdr));
4039 }
4040 
4041 void
4042 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
4043 {
4044 	GIANT_REQUIRED;
4045 
4046 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
4047 	ccb_h->pinfo.priority = priority;
4048 	ccb_h->path = path;
4049 	ccb_h->path_id = path->bus->path_id;
4050 	if (path->target)
4051 		ccb_h->target_id = path->target->target_id;
4052 	else
4053 		ccb_h->target_id = CAM_TARGET_WILDCARD;
4054 	if (path->device) {
4055 		ccb_h->target_lun = path->device->lun_id;
4056 		ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
4057 	} else {
4058 		ccb_h->target_lun = CAM_TARGET_WILDCARD;
4059 	}
4060 	ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
4061 	ccb_h->flags = 0;
4062 }
4063 
4064 /* Path manipulation functions */
4065 cam_status
4066 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
4067 		path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
4068 {
4069 	struct	   cam_path *path;
4070 	cam_status status;
4071 
4072 	GIANT_REQUIRED;
4073 
4074 	path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
4075 
4076 	if (path == NULL) {
4077 		status = CAM_RESRC_UNAVAIL;
4078 		return(status);
4079 	}
4080 	status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
4081 	if (status != CAM_REQ_CMP) {
4082 		free(path, M_CAMXPT);
4083 		path = NULL;
4084 	}
4085 	*new_path_ptr = path;
4086 	return (status);
4087 }
4088 
4089 static cam_status
4090 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
4091 		 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
4092 {
4093 	struct	     cam_eb *bus;
4094 	struct	     cam_et *target;
4095 	struct	     cam_ed *device;
4096 	cam_status   status;
4097 	int	     s;
4098 
4099 	status = CAM_REQ_CMP;	/* Completed without error */
4100 	target = NULL;		/* Wildcarded */
4101 	device = NULL;		/* Wildcarded */
4102 
4103 	/*
4104 	 * We will potentially modify the EDT, so block interrupts
4105 	 * that may attempt to create cam paths.
4106 	 */
4107 	s = splcam();
4108 	bus = xpt_find_bus(path_id);
4109 	if (bus == NULL) {
4110 		status = CAM_PATH_INVALID;
4111 	} else {
4112 		target = xpt_find_target(bus, target_id);
4113 		if (target == NULL) {
4114 			/* Create one */
4115 			struct cam_et *new_target;
4116 
4117 			new_target = xpt_alloc_target(bus, target_id);
4118 			if (new_target == NULL) {
4119 				status = CAM_RESRC_UNAVAIL;
4120 			} else {
4121 				target = new_target;
4122 			}
4123 		}
4124 		if (target != NULL) {
4125 			device = xpt_find_device(target, lun_id);
4126 			if (device == NULL) {
4127 				/* Create one */
4128 				struct cam_ed *new_device;
4129 
4130 				new_device = xpt_alloc_device(bus,
4131 							      target,
4132 							      lun_id);
4133 				if (new_device == NULL) {
4134 					status = CAM_RESRC_UNAVAIL;
4135 				} else {
4136 					device = new_device;
4137 				}
4138 			}
4139 		}
4140 	}
4141 	splx(s);
4142 
4143 	/*
4144 	 * Only touch the user's data if we are successful.
4145 	 */
4146 	if (status == CAM_REQ_CMP) {
4147 		new_path->periph = perph;
4148 		new_path->bus = bus;
4149 		new_path->target = target;
4150 		new_path->device = device;
4151 		CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
4152 	} else {
4153 		if (device != NULL)
4154 			xpt_release_device(bus, target, device);
4155 		if (target != NULL)
4156 			xpt_release_target(bus, target);
4157 		if (bus != NULL)
4158 			xpt_release_bus(bus);
4159 	}
4160 	return (status);
4161 }
4162 
4163 static void
4164 xpt_release_path(struct cam_path *path)
4165 {
4166 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
4167 	if (path->device != NULL) {
4168 		xpt_release_device(path->bus, path->target, path->device);
4169 		path->device = NULL;
4170 	}
4171 	if (path->target != NULL) {
4172 		xpt_release_target(path->bus, path->target);
4173 		path->target = NULL;
4174 	}
4175 	if (path->bus != NULL) {
4176 		xpt_release_bus(path->bus);
4177 		path->bus = NULL;
4178 	}
4179 }
4180 
4181 void
4182 xpt_free_path(struct cam_path *path)
4183 {
4184 	GIANT_REQUIRED;
4185 
4186 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
4187 	xpt_release_path(path);
4188 	free(path, M_CAMXPT);
4189 }
4190 
4191 
4192 /*
4193  * Return -1 for failure, 0 for exact match, 1 for match with wildcards
4194  * in path1, 2 for match with wildcards in path2.
4195  */
4196 int
4197 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
4198 {
4199 	GIANT_REQUIRED;
4200 
4201 	int retval = 0;
4202 
4203 	if (path1->bus != path2->bus) {
4204 		if (path1->bus->path_id == CAM_BUS_WILDCARD)
4205 			retval = 1;
4206 		else if (path2->bus->path_id == CAM_BUS_WILDCARD)
4207 			retval = 2;
4208 		else
4209 			return (-1);
4210 	}
4211 	if (path1->target != path2->target) {
4212 		if (path1->target->target_id == CAM_TARGET_WILDCARD) {
4213 			if (retval == 0)
4214 				retval = 1;
4215 		} else if (path2->target->target_id == CAM_TARGET_WILDCARD)
4216 			retval = 2;
4217 		else
4218 			return (-1);
4219 	}
4220 	if (path1->device != path2->device) {
4221 		if (path1->device->lun_id == CAM_LUN_WILDCARD) {
4222 			if (retval == 0)
4223 				retval = 1;
4224 		} else if (path2->device->lun_id == CAM_LUN_WILDCARD)
4225 			retval = 2;
4226 		else
4227 			return (-1);
4228 	}
4229 	return (retval);
4230 }
4231 
4232 void
4233 xpt_print_path(struct cam_path *path)
4234 {
4235 	GIANT_REQUIRED;
4236 
4237 	if (path == NULL)
4238 		printf("(nopath): ");
4239 	else {
4240 		if (path->periph != NULL)
4241 			printf("(%s%d:", path->periph->periph_name,
4242 			       path->periph->unit_number);
4243 		else
4244 			printf("(noperiph:");
4245 
4246 		if (path->bus != NULL)
4247 			printf("%s%d:%d:", path->bus->sim->sim_name,
4248 			       path->bus->sim->unit_number,
4249 			       path->bus->sim->bus_id);
4250 		else
4251 			printf("nobus:");
4252 
4253 		if (path->target != NULL)
4254 			printf("%d:", path->target->target_id);
4255 		else
4256 			printf("X:");
4257 
4258 		if (path->device != NULL)
4259 			printf("%d): ", path->device->lun_id);
4260 		else
4261 			printf("X): ");
4262 	}
4263 }
4264 
4265 int
4266 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
4267 {
4268 	struct sbuf sb;
4269 
4270 	GIANT_REQUIRED;
4271 
4272 	sbuf_new(&sb, str, str_len, 0);
4273 
4274 	if (path == NULL)
4275 		sbuf_printf(&sb, "(nopath): ");
4276 	else {
4277 		if (path->periph != NULL)
4278 			sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
4279 				    path->periph->unit_number);
4280 		else
4281 			sbuf_printf(&sb, "(noperiph:");
4282 
4283 		if (path->bus != NULL)
4284 			sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
4285 				    path->bus->sim->unit_number,
4286 				    path->bus->sim->bus_id);
4287 		else
4288 			sbuf_printf(&sb, "nobus:");
4289 
4290 		if (path->target != NULL)
4291 			sbuf_printf(&sb, "%d:", path->target->target_id);
4292 		else
4293 			sbuf_printf(&sb, "X:");
4294 
4295 		if (path->device != NULL)
4296 			sbuf_printf(&sb, "%d): ", path->device->lun_id);
4297 		else
4298 			sbuf_printf(&sb, "X): ");
4299 	}
4300 	sbuf_finish(&sb);
4301 
4302 	return(sbuf_len(&sb));
4303 }
4304 
4305 path_id_t
4306 xpt_path_path_id(struct cam_path *path)
4307 {
4308 	GIANT_REQUIRED;
4309 
4310 	return(path->bus->path_id);
4311 }
4312 
4313 target_id_t
4314 xpt_path_target_id(struct cam_path *path)
4315 {
4316 	GIANT_REQUIRED;
4317 
4318 	if (path->target != NULL)
4319 		return (path->target->target_id);
4320 	else
4321 		return (CAM_TARGET_WILDCARD);
4322 }
4323 
4324 lun_id_t
4325 xpt_path_lun_id(struct cam_path *path)
4326 {
4327 	GIANT_REQUIRED;
4328 
4329 	if (path->device != NULL)
4330 		return (path->device->lun_id);
4331 	else
4332 		return (CAM_LUN_WILDCARD);
4333 }
4334 
4335 struct cam_sim *
4336 xpt_path_sim(struct cam_path *path)
4337 {
4338 	GIANT_REQUIRED;
4339 
4340 	return (path->bus->sim);
4341 }
4342 
4343 struct cam_periph*
4344 xpt_path_periph(struct cam_path *path)
4345 {
4346 	GIANT_REQUIRED;
4347 
4348 	return (path->periph);
4349 }
4350 
4351 /*
4352  * Release a CAM control block for the caller.  Remit the cost of the structure
4353  * to the device referenced by the path.  If the this device had no 'credits'
4354  * and peripheral drivers have registered async callbacks for this notification
4355  * call them now.
4356  */
4357 void
4358 xpt_release_ccb(union ccb *free_ccb)
4359 {
4360 	int	 s;
4361 	struct	 cam_path *path;
4362 	struct	 cam_ed *device;
4363 	struct	 cam_eb *bus;
4364 
4365 	GIANT_REQUIRED;
4366 
4367 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4368 	path = free_ccb->ccb_h.path;
4369 	device = path->device;
4370 	bus = path->bus;
4371 	s = splsoftcam();
4372 	cam_ccbq_release_opening(&device->ccbq);
4373 	if (xpt_ccb_count > xpt_max_ccbs) {
4374 		xpt_free_ccb(free_ccb);
4375 		xpt_ccb_count--;
4376 	} else {
4377 		SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
4378 	}
4379 	if (bus->sim->devq == NULL) {
4380 		splx(s);
4381 		return;
4382 	}
4383 	bus->sim->devq->alloc_openings++;
4384 	bus->sim->devq->alloc_active--;
4385 	/* XXX Turn this into an inline function - xpt_run_device?? */
4386 	if ((device_is_alloc_queued(device) == 0)
4387 	 && (device->drvq.entries > 0)) {
4388 		xpt_schedule_dev_allocq(bus, device);
4389 	}
4390 	splx(s);
4391 	if (dev_allocq_is_runnable(bus->sim->devq))
4392 		xpt_run_dev_allocq(bus);
4393 }
4394 
4395 /* Functions accessed by SIM drivers */
4396 
4397 /*
4398  * A sim structure, listing the SIM entry points and instance
4399  * identification info is passed to xpt_bus_register to hook the SIM
4400  * into the CAM framework.  xpt_bus_register creates a cam_eb entry
4401  * for this new bus and places it in the array of busses and assigns
4402  * it a path_id.  The path_id may be influenced by "hard wiring"
4403  * information specified by the user.  Once interrupt services are
4404  * availible, the bus will be probed.
4405  */
4406 int32_t
4407 xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4408 {
4409 	struct cam_eb *new_bus;
4410 	struct cam_eb *old_bus;
4411 	struct ccb_pathinq cpi;
4412 	int s;
4413 
4414 	GIANT_REQUIRED;
4415 
4416 	sim->bus_id = bus;
4417 	new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4418 					  M_CAMXPT, M_NOWAIT);
4419 	if (new_bus == NULL) {
4420 		/* Couldn't satisfy request */
4421 		return (CAM_RESRC_UNAVAIL);
4422 	}
4423 
4424 	if (strcmp(sim->sim_name, "xpt") != 0) {
4425 
4426 		sim->path_id =
4427 		    xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4428 	}
4429 
4430 	TAILQ_INIT(&new_bus->et_entries);
4431 	new_bus->path_id = sim->path_id;
4432 	new_bus->sim = sim;
4433 	timevalclear(&new_bus->last_reset);
4434 	new_bus->flags = 0;
4435 	new_bus->refcount = 1;	/* Held until a bus_deregister event */
4436 	new_bus->generation = 0;
4437 	s = splcam();
4438 	old_bus = TAILQ_FIRST(&xpt_busses);
4439 	while (old_bus != NULL
4440 	    && old_bus->path_id < new_bus->path_id)
4441 		old_bus = TAILQ_NEXT(old_bus, links);
4442 	if (old_bus != NULL)
4443 		TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4444 	else
4445 		TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4446 	bus_generation++;
4447 	splx(s);
4448 
4449 	/* Notify interested parties */
4450 	if (sim->path_id != CAM_XPT_PATH_ID) {
4451 		struct cam_path path;
4452 
4453 		xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4454 			         CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4455 		xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4456 		cpi.ccb_h.func_code = XPT_PATH_INQ;
4457 		xpt_action((union ccb *)&cpi);
4458 		xpt_async(AC_PATH_REGISTERED, &path, &cpi);
4459 		xpt_release_path(&path);
4460 	}
4461 	return (CAM_SUCCESS);
4462 }
4463 
4464 int32_t
4465 xpt_bus_deregister(path_id_t pathid)
4466 {
4467 	struct cam_path bus_path;
4468 	struct cam_ed *device;
4469 	struct cam_ed_qinfo *qinfo;
4470 	struct cam_devq *devq;
4471 	struct cam_periph *periph;
4472 	struct cam_sim *ccbsim;
4473 	union ccb *work_ccb;
4474 	cam_status status;
4475 
4476 	GIANT_REQUIRED;
4477 
4478 	status = xpt_compile_path(&bus_path, NULL, pathid,
4479 				  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4480 	if (status != CAM_REQ_CMP)
4481 		return (status);
4482 
4483 	xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4484 	xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4485 
4486 	/* The SIM may be gone, so use a dummy SIM for any stray operations. */
4487 	devq = bus_path.bus->sim->devq;
4488 	bus_path.bus->sim = &cam_dead_sim;
4489 
4490 	/* Execute any pending operations now. */
4491 	while ((qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
4492 	    CAMQ_HEAD)) != NULL ||
4493 	    (qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
4494 	    CAMQ_HEAD)) != NULL) {
4495 		do {
4496 			device = qinfo->device;
4497 			work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
4498 			if (work_ccb != NULL) {
4499 				devq->active_dev = device;
4500 				cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
4501 				cam_ccbq_send_ccb(&device->ccbq, work_ccb);
4502 				ccbsim = work_ccb->ccb_h.path->bus->sim;
4503 				(*(ccbsim->sim_action))(ccbsim, work_ccb);
4504 			}
4505 
4506 			periph = (struct cam_periph *)camq_remove(&device->drvq,
4507 			    CAMQ_HEAD);
4508 			if (periph != NULL)
4509 				xpt_schedule(periph, periph->pinfo.priority);
4510 		} while (work_ccb != NULL || periph != NULL);
4511 	}
4512 
4513 	/* Make sure all completed CCBs are processed. */
4514 	while (!TAILQ_EMPTY(&cam_bioq)) {
4515 		camisr(&cam_bioq);
4516 
4517 		/* Repeat the async's for the benefit of any new devices. */
4518 		xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4519 		xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4520 	}
4521 
4522 	/* Release the reference count held while registered. */
4523 	xpt_release_bus(bus_path.bus);
4524 	xpt_release_path(&bus_path);
4525 
4526 	/* Recheck for more completed CCBs. */
4527 	while (!TAILQ_EMPTY(&cam_bioq))
4528 		camisr(&cam_bioq);
4529 
4530 	return (CAM_REQ_CMP);
4531 }
4532 
4533 static path_id_t
4534 xptnextfreepathid(void)
4535 {
4536 	struct cam_eb *bus;
4537 	path_id_t pathid;
4538 	const char *strval;
4539 
4540 	pathid = 0;
4541 	bus = TAILQ_FIRST(&xpt_busses);
4542 retry:
4543 	/* Find an unoccupied pathid */
4544 	while (bus != NULL
4545 	    && bus->path_id <= pathid) {
4546 		if (bus->path_id == pathid)
4547 			pathid++;
4548 		bus = TAILQ_NEXT(bus, links);
4549 	}
4550 
4551 	/*
4552 	 * Ensure that this pathid is not reserved for
4553 	 * a bus that may be registered in the future.
4554 	 */
4555 	if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4556 		++pathid;
4557 		/* Start the search over */
4558 		goto retry;
4559 	}
4560 	return (pathid);
4561 }
4562 
4563 static path_id_t
4564 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4565 {
4566 	path_id_t pathid;
4567 	int i, dunit, val;
4568 	char buf[32];
4569 	const char *dname;
4570 
4571 	pathid = CAM_XPT_PATH_ID;
4572 	snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4573 	i = 0;
4574 	while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4575 		if (strcmp(dname, "scbus")) {
4576 			/* Avoid a bit of foot shooting. */
4577 			continue;
4578 		}
4579 		if (dunit < 0)		/* unwired?! */
4580 			continue;
4581 		if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4582 			if (sim_bus == val) {
4583 				pathid = dunit;
4584 				break;
4585 			}
4586 		} else if (sim_bus == 0) {
4587 			/* Unspecified matches bus 0 */
4588 			pathid = dunit;
4589 			break;
4590 		} else {
4591 			printf("Ambiguous scbus configuration for %s%d "
4592 			       "bus %d, cannot wire down.  The kernel "
4593 			       "config entry for scbus%d should "
4594 			       "specify a controller bus.\n"
4595 			       "Scbus will be assigned dynamically.\n",
4596 			       sim_name, sim_unit, sim_bus, dunit);
4597 			break;
4598 		}
4599 	}
4600 
4601 	if (pathid == CAM_XPT_PATH_ID)
4602 		pathid = xptnextfreepathid();
4603 	return (pathid);
4604 }
4605 
4606 void
4607 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4608 {
4609 	struct cam_eb *bus;
4610 	struct cam_et *target, *next_target;
4611 	struct cam_ed *device, *next_device;
4612 	int s;
4613 
4614 	GIANT_REQUIRED;
4615 
4616 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4617 
4618 	/*
4619 	 * Most async events come from a CAM interrupt context.  In
4620 	 * a few cases, the error recovery code at the peripheral layer,
4621 	 * which may run from our SWI or a process context, may signal
4622 	 * deferred events with a call to xpt_async. Ensure async
4623 	 * notifications are serialized by blocking cam interrupts.
4624 	 */
4625 	s = splcam();
4626 
4627 	bus = path->bus;
4628 
4629 	if (async_code == AC_BUS_RESET) {
4630 		int s;
4631 
4632 		s = splclock();
4633 		/* Update our notion of when the last reset occurred */
4634 		microtime(&bus->last_reset);
4635 		splx(s);
4636 	}
4637 
4638 	for (target = TAILQ_FIRST(&bus->et_entries);
4639 	     target != NULL;
4640 	     target = next_target) {
4641 
4642 		next_target = TAILQ_NEXT(target, links);
4643 
4644 		if (path->target != target
4645 		 && path->target->target_id != CAM_TARGET_WILDCARD
4646 		 && target->target_id != CAM_TARGET_WILDCARD)
4647 			continue;
4648 
4649 		if (async_code == AC_SENT_BDR) {
4650 			int s;
4651 
4652 			/* Update our notion of when the last reset occurred */
4653 			s = splclock();
4654 			microtime(&path->target->last_reset);
4655 			splx(s);
4656 		}
4657 
4658 		for (device = TAILQ_FIRST(&target->ed_entries);
4659 		     device != NULL;
4660 		     device = next_device) {
4661 
4662 			next_device = TAILQ_NEXT(device, links);
4663 
4664 			if (path->device != device
4665 			 && path->device->lun_id != CAM_LUN_WILDCARD
4666 			 && device->lun_id != CAM_LUN_WILDCARD)
4667 				continue;
4668 
4669 			xpt_dev_async(async_code, bus, target,
4670 				      device, async_arg);
4671 
4672 			xpt_async_bcast(&device->asyncs, async_code,
4673 					path, async_arg);
4674 		}
4675 	}
4676 
4677 	/*
4678 	 * If this wasn't a fully wildcarded async, tell all
4679 	 * clients that want all async events.
4680 	 */
4681 	if (bus != xpt_periph->path->bus)
4682 		xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4683 				path, async_arg);
4684 	splx(s);
4685 }
4686 
4687 static void
4688 xpt_async_bcast(struct async_list *async_head,
4689 		u_int32_t async_code,
4690 		struct cam_path *path, void *async_arg)
4691 {
4692 	struct async_node *cur_entry;
4693 
4694 	cur_entry = SLIST_FIRST(async_head);
4695 	while (cur_entry != NULL) {
4696 		struct async_node *next_entry;
4697 		/*
4698 		 * Grab the next list entry before we call the current
4699 		 * entry's callback.  This is because the callback function
4700 		 * can delete its async callback entry.
4701 		 */
4702 		next_entry = SLIST_NEXT(cur_entry, links);
4703 		if ((cur_entry->event_enable & async_code) != 0)
4704 			cur_entry->callback(cur_entry->callback_arg,
4705 					    async_code, path,
4706 					    async_arg);
4707 		cur_entry = next_entry;
4708 	}
4709 }
4710 
4711 /*
4712  * Handle any per-device event notifications that require action by the XPT.
4713  */
4714 static void
4715 xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4716 	      struct cam_ed *device, void *async_arg)
4717 {
4718 	cam_status status;
4719 	struct cam_path newpath;
4720 
4721 	/*
4722 	 * We only need to handle events for real devices.
4723 	 */
4724 	if (target->target_id == CAM_TARGET_WILDCARD
4725 	 || device->lun_id == CAM_LUN_WILDCARD)
4726 		return;
4727 
4728 	/*
4729 	 * We need our own path with wildcards expanded to
4730 	 * handle certain types of events.
4731 	 */
4732 	if ((async_code == AC_SENT_BDR)
4733 	 || (async_code == AC_BUS_RESET)
4734 	 || (async_code == AC_INQ_CHANGED))
4735 		status = xpt_compile_path(&newpath, NULL,
4736 					  bus->path_id,
4737 					  target->target_id,
4738 					  device->lun_id);
4739 	else
4740 		status = CAM_REQ_CMP_ERR;
4741 
4742 	if (status == CAM_REQ_CMP) {
4743 
4744 		/*
4745 		 * Allow transfer negotiation to occur in a
4746 		 * tag free environment.
4747 		 */
4748 		if (async_code == AC_SENT_BDR
4749 		 || async_code == AC_BUS_RESET)
4750 			xpt_toggle_tags(&newpath);
4751 
4752 		if (async_code == AC_INQ_CHANGED) {
4753 			/*
4754 			 * We've sent a start unit command, or
4755 			 * something similar to a device that
4756 			 * may have caused its inquiry data to
4757 			 * change. So we re-scan the device to
4758 			 * refresh the inquiry data for it.
4759 			 */
4760 			xpt_scan_lun(newpath.periph, &newpath,
4761 				     CAM_EXPECT_INQ_CHANGE, NULL);
4762 		}
4763 		xpt_release_path(&newpath);
4764 	} else if (async_code == AC_LOST_DEVICE) {
4765 		device->flags |= CAM_DEV_UNCONFIGURED;
4766 	} else if (async_code == AC_TRANSFER_NEG) {
4767 		struct ccb_trans_settings *settings;
4768 
4769 		settings = (struct ccb_trans_settings *)async_arg;
4770 		xpt_set_transfer_settings(settings, device,
4771 					  /*async_update*/TRUE);
4772 	}
4773 }
4774 
4775 u_int32_t
4776 xpt_freeze_devq(struct cam_path *path, u_int count)
4777 {
4778 	int s;
4779 	struct ccb_hdr *ccbh;
4780 
4781 	GIANT_REQUIRED;
4782 
4783 	s = splcam();
4784 	path->device->qfrozen_cnt += count;
4785 
4786 	/*
4787 	 * Mark the last CCB in the queue as needing
4788 	 * to be requeued if the driver hasn't
4789 	 * changed it's state yet.  This fixes a race
4790 	 * where a ccb is just about to be queued to
4791 	 * a controller driver when it's interrupt routine
4792 	 * freezes the queue.  To completly close the
4793 	 * hole, controller drives must check to see
4794 	 * if a ccb's status is still CAM_REQ_INPROG
4795 	 * under spl protection just before they queue
4796 	 * the CCB.  See ahc_action/ahc_freeze_devq for
4797 	 * an example.
4798 	 */
4799 	ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4800 	if (ccbh && ccbh->status == CAM_REQ_INPROG)
4801 		ccbh->status = CAM_REQUEUE_REQ;
4802 	splx(s);
4803 	return (path->device->qfrozen_cnt);
4804 }
4805 
4806 u_int32_t
4807 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4808 {
4809 	GIANT_REQUIRED;
4810 
4811 	sim->devq->send_queue.qfrozen_cnt += count;
4812 	if (sim->devq->active_dev != NULL) {
4813 		struct ccb_hdr *ccbh;
4814 
4815 		ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4816 				  ccb_hdr_tailq);
4817 		if (ccbh && ccbh->status == CAM_REQ_INPROG)
4818 			ccbh->status = CAM_REQUEUE_REQ;
4819 	}
4820 	return (sim->devq->send_queue.qfrozen_cnt);
4821 }
4822 
4823 static void
4824 xpt_release_devq_timeout(void *arg)
4825 {
4826 	struct cam_ed *device;
4827 
4828 	device = (struct cam_ed *)arg;
4829 
4830 	xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4831 }
4832 
4833 void
4834 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4835 {
4836 	GIANT_REQUIRED;
4837 
4838 	xpt_release_devq_device(path->device, count, run_queue);
4839 }
4840 
4841 static void
4842 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4843 {
4844 	int	rundevq;
4845 	int	s0, s1;
4846 
4847 	rundevq = 0;
4848 	s0 = splsoftcam();
4849 	s1 = splcam();
4850 	if (dev->qfrozen_cnt > 0) {
4851 
4852 		count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4853 		dev->qfrozen_cnt -= count;
4854 		if (dev->qfrozen_cnt == 0) {
4855 
4856 			/*
4857 			 * No longer need to wait for a successful
4858 			 * command completion.
4859 			 */
4860 			dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4861 
4862 			/*
4863 			 * Remove any timeouts that might be scheduled
4864 			 * to release this queue.
4865 			 */
4866 			if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4867 				untimeout(xpt_release_devq_timeout, dev,
4868 					  dev->c_handle);
4869 				dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4870 			}
4871 
4872 			/*
4873 			 * Now that we are unfrozen schedule the
4874 			 * device so any pending transactions are
4875 			 * run.
4876 			 */
4877 			if ((dev->ccbq.queue.entries > 0)
4878 			 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4879 			 && (run_queue != 0)) {
4880 				rundevq = 1;
4881 			}
4882 		}
4883 	}
4884 	splx(s1);
4885 	if (rundevq != 0)
4886 		xpt_run_dev_sendq(dev->target->bus);
4887 	splx(s0);
4888 }
4889 
4890 void
4891 xpt_release_simq(struct cam_sim *sim, int run_queue)
4892 {
4893 	int	s;
4894 	struct	camq *sendq;
4895 
4896 	GIANT_REQUIRED;
4897 
4898 	sendq = &(sim->devq->send_queue);
4899 	s = splcam();
4900 	if (sendq->qfrozen_cnt > 0) {
4901 
4902 		sendq->qfrozen_cnt--;
4903 		if (sendq->qfrozen_cnt == 0) {
4904 			struct cam_eb *bus;
4905 
4906 			/*
4907 			 * If there is a timeout scheduled to release this
4908 			 * sim queue, remove it.  The queue frozen count is
4909 			 * already at 0.
4910 			 */
4911 			if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4912 				untimeout(xpt_release_simq_timeout, sim,
4913 					  sim->c_handle);
4914 				sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4915 			}
4916 			bus = xpt_find_bus(sim->path_id);
4917 			splx(s);
4918 
4919 			if (run_queue) {
4920 				/*
4921 				 * Now that we are unfrozen run the send queue.
4922 				 */
4923 				xpt_run_dev_sendq(bus);
4924 			}
4925 			xpt_release_bus(bus);
4926 		} else
4927 			splx(s);
4928 	} else
4929 		splx(s);
4930 }
4931 
4932 static void
4933 xpt_release_simq_timeout(void *arg)
4934 {
4935 	struct cam_sim *sim;
4936 
4937 	sim = (struct cam_sim *)arg;
4938 	xpt_release_simq(sim, /* run_queue */ TRUE);
4939 }
4940 
4941 void
4942 xpt_done(union ccb *done_ccb)
4943 {
4944 	int s;
4945 
4946 	s = splcam();
4947 
4948 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4949 	if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4950 		/*
4951 		 * Queue up the request for handling by our SWI handler
4952 		 * any of the "non-immediate" type of ccbs.
4953 		 */
4954 		switch (done_ccb->ccb_h.path->periph->type) {
4955 		case CAM_PERIPH_BIO:
4956 			mtx_lock(&cam_bioq_lock);
4957 			TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4958 					  sim_links.tqe);
4959 			done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4960 			mtx_unlock(&cam_bioq_lock);
4961 			swi_sched(cambio_ih, 0);
4962 			break;
4963 		default:
4964 			panic("unknown periph type %d",
4965 			    done_ccb->ccb_h.path->periph->type);
4966 		}
4967 	}
4968 	splx(s);
4969 }
4970 
4971 union ccb *
4972 xpt_alloc_ccb()
4973 {
4974 	union ccb *new_ccb;
4975 
4976 	GIANT_REQUIRED;
4977 
4978 	new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_WAITOK);
4979 	return (new_ccb);
4980 }
4981 
4982 union ccb *
4983 xpt_alloc_ccb_nowait()
4984 {
4985 	union ccb *new_ccb;
4986 
4987 	GIANT_REQUIRED;
4988 
4989 	new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_NOWAIT);
4990 	return (new_ccb);
4991 }
4992 
4993 void
4994 xpt_free_ccb(union ccb *free_ccb)
4995 {
4996 	free(free_ccb, M_CAMXPT);
4997 }
4998 
4999 
5000 
5001 /* Private XPT functions */
5002 
5003 /*
5004  * Get a CAM control block for the caller. Charge the structure to the device
5005  * referenced by the path.  If the this device has no 'credits' then the
5006  * device already has the maximum number of outstanding operations under way
5007  * and we return NULL. If we don't have sufficient resources to allocate more
5008  * ccbs, we also return NULL.
5009  */
5010 static union ccb *
5011 xpt_get_ccb(struct cam_ed *device)
5012 {
5013 	union ccb *new_ccb;
5014 	int s;
5015 
5016 	s = splsoftcam();
5017 	if ((new_ccb = (union ccb *)SLIST_FIRST(&ccb_freeq)) == NULL) {
5018 		new_ccb = xpt_alloc_ccb_nowait();
5019                 if (new_ccb == NULL) {
5020 			splx(s);
5021 			return (NULL);
5022 		}
5023 		callout_handle_init(&new_ccb->ccb_h.timeout_ch);
5024 		SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
5025 				  xpt_links.sle);
5026 		xpt_ccb_count++;
5027 	}
5028 	cam_ccbq_take_opening(&device->ccbq);
5029 	SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
5030 	splx(s);
5031 	return (new_ccb);
5032 }
5033 
5034 static void
5035 xpt_release_bus(struct cam_eb *bus)
5036 {
5037 	int s;
5038 
5039 	s = splcam();
5040 	if ((--bus->refcount == 0)
5041 	 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
5042 		TAILQ_REMOVE(&xpt_busses, bus, links);
5043 		bus_generation++;
5044 		splx(s);
5045 		free(bus, M_CAMXPT);
5046 	} else
5047 		splx(s);
5048 }
5049 
5050 static struct cam_et *
5051 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
5052 {
5053 	struct cam_et *target;
5054 
5055 	target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, M_NOWAIT);
5056 	if (target != NULL) {
5057 		struct cam_et *cur_target;
5058 
5059 		TAILQ_INIT(&target->ed_entries);
5060 		target->bus = bus;
5061 		target->target_id = target_id;
5062 		target->refcount = 1;
5063 		target->generation = 0;
5064 		timevalclear(&target->last_reset);
5065 		/*
5066 		 * Hold a reference to our parent bus so it
5067 		 * will not go away before we do.
5068 		 */
5069 		bus->refcount++;
5070 
5071 		/* Insertion sort into our bus's target list */
5072 		cur_target = TAILQ_FIRST(&bus->et_entries);
5073 		while (cur_target != NULL && cur_target->target_id < target_id)
5074 			cur_target = TAILQ_NEXT(cur_target, links);
5075 
5076 		if (cur_target != NULL) {
5077 			TAILQ_INSERT_BEFORE(cur_target, target, links);
5078 		} else {
5079 			TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
5080 		}
5081 		bus->generation++;
5082 	}
5083 	return (target);
5084 }
5085 
5086 static void
5087 xpt_release_target(struct cam_eb *bus, struct cam_et *target)
5088 {
5089 	int s;
5090 
5091 	s = splcam();
5092 	if ((--target->refcount == 0)
5093 	 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
5094 		TAILQ_REMOVE(&bus->et_entries, target, links);
5095 		bus->generation++;
5096 		splx(s);
5097 		free(target, M_CAMXPT);
5098 		xpt_release_bus(bus);
5099 	} else
5100 		splx(s);
5101 }
5102 
5103 static struct cam_ed *
5104 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
5105 {
5106 #ifdef CAM_NEW_TRAN_CODE
5107 	struct	   cam_path path;
5108 #endif /* CAM_NEW_TRAN_CODE */
5109 	struct	   cam_ed *device;
5110 	struct	   cam_devq *devq;
5111 	cam_status status;
5112 
5113 	if (SIM_DEAD(bus->sim))
5114 		return (NULL);
5115 
5116 	/* Make space for us in the device queue on our bus */
5117 	devq = bus->sim->devq;
5118 	status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
5119 
5120 	if (status != CAM_REQ_CMP) {
5121 		device = NULL;
5122 	} else {
5123 		device = (struct cam_ed *)malloc(sizeof(*device),
5124 						 M_CAMXPT, M_NOWAIT);
5125 	}
5126 
5127 	if (device != NULL) {
5128 		struct cam_ed *cur_device;
5129 
5130 		cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
5131 		device->alloc_ccb_entry.device = device;
5132 		cam_init_pinfo(&device->send_ccb_entry.pinfo);
5133 		device->send_ccb_entry.device = device;
5134 		device->target = target;
5135 		device->lun_id = lun_id;
5136 		/* Initialize our queues */
5137 		if (camq_init(&device->drvq, 0) != 0) {
5138 			free(device, M_CAMXPT);
5139 			return (NULL);
5140 		}
5141 		if (cam_ccbq_init(&device->ccbq,
5142 				  bus->sim->max_dev_openings) != 0) {
5143 			camq_fini(&device->drvq);
5144 			free(device, M_CAMXPT);
5145 			return (NULL);
5146 		}
5147 		SLIST_INIT(&device->asyncs);
5148 		SLIST_INIT(&device->periphs);
5149 		device->generation = 0;
5150 		device->owner = NULL;
5151 		/*
5152 		 * Take the default quirk entry until we have inquiry
5153 		 * data and can determine a better quirk to use.
5154 		 */
5155 		device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
5156 		bzero(&device->inq_data, sizeof(device->inq_data));
5157 		device->inq_flags = 0;
5158 		device->queue_flags = 0;
5159 		device->serial_num = NULL;
5160 		device->serial_num_len = 0;
5161 		device->qfrozen_cnt = 0;
5162 		device->flags = CAM_DEV_UNCONFIGURED;
5163 		device->tag_delay_count = 0;
5164 		device->tag_saved_openings = 0;
5165 		device->refcount = 1;
5166 		callout_handle_init(&device->c_handle);
5167 
5168 		/*
5169 		 * Hold a reference to our parent target so it
5170 		 * will not go away before we do.
5171 		 */
5172 		target->refcount++;
5173 
5174 		/*
5175 		 * XXX should be limited by number of CCBs this bus can
5176 		 * do.
5177 		 */
5178 		xpt_max_ccbs += device->ccbq.devq_openings;
5179 		/* Insertion sort into our target's device list */
5180 		cur_device = TAILQ_FIRST(&target->ed_entries);
5181 		while (cur_device != NULL && cur_device->lun_id < lun_id)
5182 			cur_device = TAILQ_NEXT(cur_device, links);
5183 		if (cur_device != NULL) {
5184 			TAILQ_INSERT_BEFORE(cur_device, device, links);
5185 		} else {
5186 			TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
5187 		}
5188 		target->generation++;
5189 #ifdef CAM_NEW_TRAN_CODE
5190 		if (lun_id != CAM_LUN_WILDCARD) {
5191 			xpt_compile_path(&path,
5192 					 NULL,
5193 					 bus->path_id,
5194 					 target->target_id,
5195 					 lun_id);
5196 			xpt_devise_transport(&path);
5197 			xpt_release_path(&path);
5198 		}
5199 #endif /* CAM_NEW_TRAN_CODE */
5200 	}
5201 	return (device);
5202 }
5203 
5204 static void
5205 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
5206 		   struct cam_ed *device)
5207 {
5208 	int s;
5209 
5210 	s = splcam();
5211 	if ((--device->refcount == 0)
5212 	 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
5213 		struct cam_devq *devq;
5214 
5215 		if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
5216 		 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
5217 			panic("Removing device while still queued for ccbs");
5218 
5219 		if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
5220 				untimeout(xpt_release_devq_timeout, device,
5221 					  device->c_handle);
5222 
5223 		TAILQ_REMOVE(&target->ed_entries, device,links);
5224 		target->generation++;
5225 		xpt_max_ccbs -= device->ccbq.devq_openings;
5226 		if (!SIM_DEAD(bus->sim)) {
5227 			/* Release our slot in the devq */
5228 			devq = bus->sim->devq;
5229 			cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
5230 		}
5231 		splx(s);
5232 		camq_fini(&device->drvq);
5233 		camq_fini(&device->ccbq.queue);
5234 		free(device, M_CAMXPT);
5235 		xpt_release_target(bus, target);
5236 	} else
5237 		splx(s);
5238 }
5239 
5240 static u_int32_t
5241 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
5242 {
5243 	int	s;
5244 	int	diff;
5245 	int	result;
5246 	struct	cam_ed *dev;
5247 
5248 	dev = path->device;
5249 	s = splsoftcam();
5250 
5251 	diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
5252 	result = cam_ccbq_resize(&dev->ccbq, newopenings);
5253 	if (result == CAM_REQ_CMP && (diff < 0)) {
5254 		dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
5255 	}
5256 	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5257 	 || (dev->inq_flags & SID_CmdQue) != 0)
5258 		dev->tag_saved_openings = newopenings;
5259 	/* Adjust the global limit */
5260 	xpt_max_ccbs += diff;
5261 	splx(s);
5262 	return (result);
5263 }
5264 
5265 static struct cam_eb *
5266 xpt_find_bus(path_id_t path_id)
5267 {
5268 	struct cam_eb *bus;
5269 
5270 	for (bus = TAILQ_FIRST(&xpt_busses);
5271 	     bus != NULL;
5272 	     bus = TAILQ_NEXT(bus, links)) {
5273 		if (bus->path_id == path_id) {
5274 			bus->refcount++;
5275 			break;
5276 		}
5277 	}
5278 	return (bus);
5279 }
5280 
5281 static struct cam_et *
5282 xpt_find_target(struct cam_eb *bus, target_id_t	target_id)
5283 {
5284 	struct cam_et *target;
5285 
5286 	for (target = TAILQ_FIRST(&bus->et_entries);
5287 	     target != NULL;
5288 	     target = TAILQ_NEXT(target, links)) {
5289 		if (target->target_id == target_id) {
5290 			target->refcount++;
5291 			break;
5292 		}
5293 	}
5294 	return (target);
5295 }
5296 
5297 static struct cam_ed *
5298 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5299 {
5300 	struct cam_ed *device;
5301 
5302 	for (device = TAILQ_FIRST(&target->ed_entries);
5303 	     device != NULL;
5304 	     device = TAILQ_NEXT(device, links)) {
5305 		if (device->lun_id == lun_id) {
5306 			device->refcount++;
5307 			break;
5308 		}
5309 	}
5310 	return (device);
5311 }
5312 
5313 typedef struct {
5314 	union	ccb *request_ccb;
5315 	struct 	ccb_pathinq *cpi;
5316 	int	counter;
5317 } xpt_scan_bus_info;
5318 
5319 /*
5320  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
5321  * As the scan progresses, xpt_scan_bus is used as the
5322  * callback on completion function.
5323  */
5324 static void
5325 xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
5326 {
5327 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5328 		  ("xpt_scan_bus\n"));
5329 	switch (request_ccb->ccb_h.func_code) {
5330 	case XPT_SCAN_BUS:
5331 	{
5332 		xpt_scan_bus_info *scan_info;
5333 		union	ccb *work_ccb;
5334 		struct	cam_path *path;
5335 		u_int	i;
5336 		u_int	max_target;
5337 		u_int	initiator_id;
5338 
5339 		/* Find out the characteristics of the bus */
5340 		work_ccb = xpt_alloc_ccb();
5341 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
5342 			      request_ccb->ccb_h.pinfo.priority);
5343 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
5344 		xpt_action(work_ccb);
5345 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
5346 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
5347 			xpt_free_ccb(work_ccb);
5348 			xpt_done(request_ccb);
5349 			return;
5350 		}
5351 
5352 		if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5353 			/*
5354 			 * Can't scan the bus on an adapter that
5355 			 * cannot perform the initiator role.
5356 			 */
5357 			request_ccb->ccb_h.status = CAM_REQ_CMP;
5358 			xpt_free_ccb(work_ccb);
5359 			xpt_done(request_ccb);
5360 			return;
5361 		}
5362 
5363 		/* Save some state for use while we probe for devices */
5364 		scan_info = (xpt_scan_bus_info *)
5365 		    malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK);
5366 		scan_info->request_ccb = request_ccb;
5367 		scan_info->cpi = &work_ccb->cpi;
5368 
5369 		/* Cache on our stack so we can work asynchronously */
5370 		max_target = scan_info->cpi->max_target;
5371 		initiator_id = scan_info->cpi->initiator_id;
5372 
5373 
5374 		/*
5375 		 * We can scan all targets in parallel, or do it sequentially.
5376 		 */
5377 		if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
5378 			max_target = 0;
5379 			scan_info->counter = 0;
5380 		} else {
5381 			scan_info->counter = scan_info->cpi->max_target + 1;
5382 			if (scan_info->cpi->initiator_id < scan_info->counter) {
5383 				scan_info->counter--;
5384 			}
5385 		}
5386 
5387 		for (i = 0; i <= max_target; i++) {
5388 			cam_status status;
5389 			if (i == initiator_id)
5390 				continue;
5391 
5392 			status = xpt_create_path(&path, xpt_periph,
5393 						 request_ccb->ccb_h.path_id,
5394 						 i, 0);
5395 			if (status != CAM_REQ_CMP) {
5396 				printf("xpt_scan_bus: xpt_create_path failed"
5397 				       " with status %#x, bus scan halted\n",
5398 				       status);
5399 				free(scan_info, M_TEMP);
5400 				request_ccb->ccb_h.status = status;
5401 				xpt_free_ccb(work_ccb);
5402 				xpt_done(request_ccb);
5403 				break;
5404 			}
5405 			work_ccb = xpt_alloc_ccb();
5406 			xpt_setup_ccb(&work_ccb->ccb_h, path,
5407 				      request_ccb->ccb_h.pinfo.priority);
5408 			work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5409 			work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5410 			work_ccb->ccb_h.ppriv_ptr0 = scan_info;
5411 			work_ccb->crcn.flags = request_ccb->crcn.flags;
5412 			xpt_action(work_ccb);
5413 		}
5414 		break;
5415 	}
5416 	case XPT_SCAN_LUN:
5417 	{
5418 		cam_status status;
5419 		struct cam_path *path;
5420 		xpt_scan_bus_info *scan_info;
5421 		path_id_t path_id;
5422 		target_id_t target_id;
5423 		lun_id_t lun_id;
5424 
5425 		/* Reuse the same CCB to query if a device was really found */
5426 		scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
5427 		xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
5428 			      request_ccb->ccb_h.pinfo.priority);
5429 		request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5430 
5431 		path_id = request_ccb->ccb_h.path_id;
5432 		target_id = request_ccb->ccb_h.target_id;
5433 		lun_id = request_ccb->ccb_h.target_lun;
5434 		xpt_action(request_ccb);
5435 
5436 		if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
5437 			struct cam_ed *device;
5438 			struct cam_et *target;
5439 			int s, phl;
5440 
5441 			/*
5442 			 * If we already probed lun 0 successfully, or
5443 			 * we have additional configured luns on this
5444 			 * target that might have "gone away", go onto
5445 			 * the next lun.
5446 			 */
5447 			target = request_ccb->ccb_h.path->target;
5448 			/*
5449 			 * We may touch devices that we don't
5450 			 * hold references too, so ensure they
5451 			 * don't disappear out from under us.
5452 			 * The target above is referenced by the
5453 			 * path in the request ccb.
5454 			 */
5455 			phl = 0;
5456 			s = splcam();
5457 			device = TAILQ_FIRST(&target->ed_entries);
5458 			if (device != NULL) {
5459 				phl = CAN_SRCH_HI_SPARSE(device);
5460 				if (device->lun_id == 0)
5461 					device = TAILQ_NEXT(device, links);
5462 			}
5463 			splx(s);
5464 			if ((lun_id != 0) || (device != NULL)) {
5465 				if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5466 					lun_id++;
5467 			}
5468 		} else {
5469 			struct cam_ed *device;
5470 
5471 			device = request_ccb->ccb_h.path->device;
5472 
5473 			if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5474 				/* Try the next lun */
5475 				if (lun_id < (CAM_SCSI2_MAXLUN-1)
5476 				  || CAN_SRCH_HI_DENSE(device))
5477 					lun_id++;
5478 			}
5479 		}
5480 
5481 		/*
5482 		 * Free the current request path- we're done with it.
5483 		 */
5484 		xpt_free_path(request_ccb->ccb_h.path);
5485 
5486 		/*
5487 		 * Check to see if we scan any further luns.
5488 		 */
5489 		if (lun_id == request_ccb->ccb_h.target_lun
5490                  || lun_id > scan_info->cpi->max_lun) {
5491 			int done;
5492 
5493  hop_again:
5494 			done = 0;
5495 			if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
5496 				scan_info->counter++;
5497 				if (scan_info->counter ==
5498 				    scan_info->cpi->initiator_id) {
5499 					scan_info->counter++;
5500 				}
5501 				if (scan_info->counter >=
5502 				    scan_info->cpi->max_target+1) {
5503 					done = 1;
5504 				}
5505 			} else {
5506 				scan_info->counter--;
5507 				if (scan_info->counter == 0) {
5508 					done = 1;
5509 				}
5510 			}
5511 			if (done) {
5512 				xpt_free_ccb(request_ccb);
5513 				xpt_free_ccb((union ccb *)scan_info->cpi);
5514 				request_ccb = scan_info->request_ccb;
5515 				free(scan_info, M_TEMP);
5516 				request_ccb->ccb_h.status = CAM_REQ_CMP;
5517 				xpt_done(request_ccb);
5518 				break;
5519 			}
5520 
5521 			if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
5522 				break;
5523 			}
5524 			status = xpt_create_path(&path, xpt_periph,
5525 			    scan_info->request_ccb->ccb_h.path_id,
5526 			    scan_info->counter, 0);
5527 			if (status != CAM_REQ_CMP) {
5528 				printf("xpt_scan_bus: xpt_create_path failed"
5529 				    " with status %#x, bus scan halted\n",
5530 			       	    status);
5531 				xpt_free_ccb(request_ccb);
5532 				xpt_free_ccb((union ccb *)scan_info->cpi);
5533 				request_ccb = scan_info->request_ccb;
5534 				free(scan_info, M_TEMP);
5535 				request_ccb->ccb_h.status = status;
5536 				xpt_done(request_ccb);
5537 				break;
5538 			}
5539 			xpt_setup_ccb(&request_ccb->ccb_h, path,
5540 			    request_ccb->ccb_h.pinfo.priority);
5541 			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5542 			request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5543 			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5544 			request_ccb->crcn.flags =
5545 			    scan_info->request_ccb->crcn.flags;
5546 		} else {
5547 			status = xpt_create_path(&path, xpt_periph,
5548 						 path_id, target_id, lun_id);
5549 			if (status != CAM_REQ_CMP) {
5550 				printf("xpt_scan_bus: xpt_create_path failed "
5551 				       "with status %#x, halting LUN scan\n",
5552 			 	       status);
5553 				goto hop_again;
5554 			}
5555 			xpt_setup_ccb(&request_ccb->ccb_h, path,
5556 				      request_ccb->ccb_h.pinfo.priority);
5557 			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5558 			request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5559 			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5560 			request_ccb->crcn.flags =
5561 				scan_info->request_ccb->crcn.flags;
5562 		}
5563 		xpt_action(request_ccb);
5564 		break;
5565 	}
5566 	default:
5567 		break;
5568 	}
5569 }
5570 
5571 typedef enum {
5572 	PROBE_TUR,
5573 	PROBE_INQUIRY,
5574 	PROBE_FULL_INQUIRY,
5575 	PROBE_MODE_SENSE,
5576 	PROBE_SERIAL_NUM,
5577 	PROBE_TUR_FOR_NEGOTIATION
5578 } probe_action;
5579 
5580 typedef enum {
5581 	PROBE_INQUIRY_CKSUM	= 0x01,
5582 	PROBE_SERIAL_CKSUM	= 0x02,
5583 	PROBE_NO_ANNOUNCE	= 0x04
5584 } probe_flags;
5585 
5586 typedef struct {
5587 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
5588 	probe_action	action;
5589 	union ccb	saved_ccb;
5590 	probe_flags	flags;
5591 	MD5_CTX		context;
5592 	u_int8_t	digest[16];
5593 } probe_softc;
5594 
5595 static void
5596 xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5597 	     cam_flags flags, union ccb *request_ccb)
5598 {
5599 	struct ccb_pathinq cpi;
5600 	cam_status status;
5601 	struct cam_path *new_path;
5602 	struct cam_periph *old_periph;
5603 	int s;
5604 
5605 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5606 		  ("xpt_scan_lun\n"));
5607 
5608 	xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5609 	cpi.ccb_h.func_code = XPT_PATH_INQ;
5610 	xpt_action((union ccb *)&cpi);
5611 
5612 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
5613 		if (request_ccb != NULL) {
5614 			request_ccb->ccb_h.status = cpi.ccb_h.status;
5615 			xpt_done(request_ccb);
5616 		}
5617 		return;
5618 	}
5619 
5620 	if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5621 		/*
5622 		 * Can't scan the bus on an adapter that
5623 		 * cannot perform the initiator role.
5624 		 */
5625 		if (request_ccb != NULL) {
5626 			request_ccb->ccb_h.status = CAM_REQ_CMP;
5627 			xpt_done(request_ccb);
5628 		}
5629 		return;
5630 	}
5631 
5632 	if (request_ccb == NULL) {
5633 		request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
5634 		if (request_ccb == NULL) {
5635 			xpt_print_path(path);
5636 			printf("xpt_scan_lun: can't allocate CCB, can't "
5637 			       "continue\n");
5638 			return;
5639 		}
5640 		new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
5641 		if (new_path == NULL) {
5642 			xpt_print_path(path);
5643 			printf("xpt_scan_lun: can't allocate path, can't "
5644 			       "continue\n");
5645 			free(request_ccb, M_TEMP);
5646 			return;
5647 		}
5648 		status = xpt_compile_path(new_path, xpt_periph,
5649 					  path->bus->path_id,
5650 					  path->target->target_id,
5651 					  path->device->lun_id);
5652 
5653 		if (status != CAM_REQ_CMP) {
5654 			xpt_print_path(path);
5655 			printf("xpt_scan_lun: can't compile path, can't "
5656 			       "continue\n");
5657 			free(request_ccb, M_TEMP);
5658 			free(new_path, M_TEMP);
5659 			return;
5660 		}
5661 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5662 		request_ccb->ccb_h.cbfcnp = xptscandone;
5663 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5664 		request_ccb->crcn.flags = flags;
5665 	}
5666 
5667 	s = splsoftcam();
5668 	if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5669 		probe_softc *softc;
5670 
5671 		softc = (probe_softc *)old_periph->softc;
5672 		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5673 				  periph_links.tqe);
5674 	} else {
5675 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
5676 					  probestart, "probe",
5677 					  CAM_PERIPH_BIO,
5678 					  request_ccb->ccb_h.path, NULL, 0,
5679 					  request_ccb);
5680 
5681 		if (status != CAM_REQ_CMP) {
5682 			xpt_print_path(path);
5683 			printf("xpt_scan_lun: cam_alloc_periph returned an "
5684 			       "error, can't continue probe\n");
5685 			request_ccb->ccb_h.status = status;
5686 			xpt_done(request_ccb);
5687 		}
5688 	}
5689 	splx(s);
5690 }
5691 
5692 static void
5693 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5694 {
5695 	xpt_release_path(done_ccb->ccb_h.path);
5696 	free(done_ccb->ccb_h.path, M_TEMP);
5697 	free(done_ccb, M_TEMP);
5698 }
5699 
5700 static cam_status
5701 proberegister(struct cam_periph *periph, void *arg)
5702 {
5703 	union ccb *request_ccb;	/* CCB representing the probe request */
5704 	probe_softc *softc;
5705 
5706 	request_ccb = (union ccb *)arg;
5707 	if (periph == NULL) {
5708 		printf("proberegister: periph was NULL!!\n");
5709 		return(CAM_REQ_CMP_ERR);
5710 	}
5711 
5712 	if (request_ccb == NULL) {
5713 		printf("proberegister: no probe CCB, "
5714 		       "can't register device\n");
5715 		return(CAM_REQ_CMP_ERR);
5716 	}
5717 
5718 	softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
5719 
5720 	if (softc == NULL) {
5721 		printf("proberegister: Unable to probe new device. "
5722 		       "Unable to allocate softc\n");
5723 		return(CAM_REQ_CMP_ERR);
5724 	}
5725 	TAILQ_INIT(&softc->request_ccbs);
5726 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5727 			  periph_links.tqe);
5728 	softc->flags = 0;
5729 	periph->softc = softc;
5730 	cam_periph_acquire(periph);
5731 	/*
5732 	 * Ensure we've waited at least a bus settle
5733 	 * delay before attempting to probe the device.
5734 	 * For HBAs that don't do bus resets, this won't make a difference.
5735 	 */
5736 	cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5737 				      scsi_delay);
5738 	probeschedule(periph);
5739 	return(CAM_REQ_CMP);
5740 }
5741 
5742 static void
5743 probeschedule(struct cam_periph *periph)
5744 {
5745 	struct ccb_pathinq cpi;
5746 	union ccb *ccb;
5747 	probe_softc *softc;
5748 
5749 	softc = (probe_softc *)periph->softc;
5750 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5751 
5752 	xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5753 	cpi.ccb_h.func_code = XPT_PATH_INQ;
5754 	xpt_action((union ccb *)&cpi);
5755 
5756 	/*
5757 	 * If a device has gone away and another device, or the same one,
5758 	 * is back in the same place, it should have a unit attention
5759 	 * condition pending.  It will not report the unit attention in
5760 	 * response to an inquiry, which may leave invalid transfer
5761 	 * negotiations in effect.  The TUR will reveal the unit attention
5762 	 * condition.  Only send the TUR for lun 0, since some devices
5763 	 * will get confused by commands other than inquiry to non-existent
5764 	 * luns.  If you think a device has gone away start your scan from
5765 	 * lun 0.  This will insure that any bogus transfer settings are
5766 	 * invalidated.
5767 	 *
5768 	 * If we haven't seen the device before and the controller supports
5769 	 * some kind of transfer negotiation, negotiate with the first
5770 	 * sent command if no bus reset was performed at startup.  This
5771 	 * ensures that the device is not confused by transfer negotiation
5772 	 * settings left over by loader or BIOS action.
5773 	 */
5774 	if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5775 	 && (ccb->ccb_h.target_lun == 0)) {
5776 		softc->action = PROBE_TUR;
5777 	} else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5778 	      && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5779 		proberequestdefaultnegotiation(periph);
5780 		softc->action = PROBE_INQUIRY;
5781 	} else {
5782 		softc->action = PROBE_INQUIRY;
5783 	}
5784 
5785 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5786 		softc->flags |= PROBE_NO_ANNOUNCE;
5787 	else
5788 		softc->flags &= ~PROBE_NO_ANNOUNCE;
5789 
5790 	xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5791 }
5792 
5793 static void
5794 probestart(struct cam_periph *periph, union ccb *start_ccb)
5795 {
5796 	/* Probe the device that our peripheral driver points to */
5797 	struct ccb_scsiio *csio;
5798 	probe_softc *softc;
5799 
5800 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5801 
5802 	softc = (probe_softc *)periph->softc;
5803 	csio = &start_ccb->csio;
5804 
5805 	switch (softc->action) {
5806 	case PROBE_TUR:
5807 	case PROBE_TUR_FOR_NEGOTIATION:
5808 	{
5809 		scsi_test_unit_ready(csio,
5810 				     /*retries*/4,
5811 				     probedone,
5812 				     MSG_SIMPLE_Q_TAG,
5813 				     SSD_FULL_SIZE,
5814 				     /*timeout*/60000);
5815 		break;
5816 	}
5817 	case PROBE_INQUIRY:
5818 	case PROBE_FULL_INQUIRY:
5819 	{
5820 		u_int inquiry_len;
5821 		struct scsi_inquiry_data *inq_buf;
5822 
5823 		inq_buf = &periph->path->device->inq_data;
5824 		/*
5825 		 * If the device is currently configured, we calculate an
5826 		 * MD5 checksum of the inquiry data, and if the serial number
5827 		 * length is greater than 0, add the serial number data
5828 		 * into the checksum as well.  Once the inquiry and the
5829 		 * serial number check finish, we attempt to figure out
5830 		 * whether we still have the same device.
5831 		 */
5832 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5833 
5834 			MD5Init(&softc->context);
5835 			MD5Update(&softc->context, (unsigned char *)inq_buf,
5836 				  sizeof(struct scsi_inquiry_data));
5837 			softc->flags |= PROBE_INQUIRY_CKSUM;
5838 			if (periph->path->device->serial_num_len > 0) {
5839 				MD5Update(&softc->context,
5840 					  periph->path->device->serial_num,
5841 					  periph->path->device->serial_num_len);
5842 				softc->flags |= PROBE_SERIAL_CKSUM;
5843 			}
5844 			MD5Final(softc->digest, &softc->context);
5845 		}
5846 
5847 		if (softc->action == PROBE_INQUIRY)
5848 			inquiry_len = SHORT_INQUIRY_LENGTH;
5849 		else
5850 			inquiry_len = inq_buf->additional_length
5851 				    + offsetof(struct scsi_inquiry_data,
5852                                                additional_length) + 1;
5853 
5854 		/*
5855 		 * Some parallel SCSI devices fail to send an
5856 		 * ignore wide residue message when dealing with
5857 		 * odd length inquiry requests.  Round up to be
5858 		 * safe.
5859 		 */
5860 		inquiry_len = roundup2(inquiry_len, 2);
5861 
5862 		scsi_inquiry(csio,
5863 			     /*retries*/4,
5864 			     probedone,
5865 			     MSG_SIMPLE_Q_TAG,
5866 			     (u_int8_t *)inq_buf,
5867 			     inquiry_len,
5868 			     /*evpd*/FALSE,
5869 			     /*page_code*/0,
5870 			     SSD_MIN_SIZE,
5871 			     /*timeout*/60 * 1000);
5872 		break;
5873 	}
5874 	case PROBE_MODE_SENSE:
5875 	{
5876 		void  *mode_buf;
5877 		int    mode_buf_len;
5878 
5879 		mode_buf_len = sizeof(struct scsi_mode_header_6)
5880 			     + sizeof(struct scsi_mode_blk_desc)
5881 			     + sizeof(struct scsi_control_page);
5882 		mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
5883 		if (mode_buf != NULL) {
5884 	                scsi_mode_sense(csio,
5885 					/*retries*/4,
5886 					probedone,
5887 					MSG_SIMPLE_Q_TAG,
5888 					/*dbd*/FALSE,
5889 					SMS_PAGE_CTRL_CURRENT,
5890 					SMS_CONTROL_MODE_PAGE,
5891 					mode_buf,
5892 					mode_buf_len,
5893 					SSD_FULL_SIZE,
5894 					/*timeout*/60000);
5895 			break;
5896 		}
5897 		xpt_print_path(periph->path);
5898 		printf("Unable to mode sense control page - malloc failure\n");
5899 		softc->action = PROBE_SERIAL_NUM;
5900 	}
5901 	/* FALLTHROUGH */
5902 	case PROBE_SERIAL_NUM:
5903 	{
5904 		struct scsi_vpd_unit_serial_number *serial_buf;
5905 		struct cam_ed* device;
5906 
5907 		serial_buf = NULL;
5908 		device = periph->path->device;
5909 		device->serial_num = NULL;
5910 		device->serial_num_len = 0;
5911 
5912 		if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
5913 			serial_buf = (struct scsi_vpd_unit_serial_number *)
5914 				malloc(sizeof(*serial_buf), M_TEMP,
5915 					M_NOWAIT | M_ZERO);
5916 
5917 		if (serial_buf != NULL) {
5918 			scsi_inquiry(csio,
5919 				     /*retries*/4,
5920 				     probedone,
5921 				     MSG_SIMPLE_Q_TAG,
5922 				     (u_int8_t *)serial_buf,
5923 				     sizeof(*serial_buf),
5924 				     /*evpd*/TRUE,
5925 				     SVPD_UNIT_SERIAL_NUMBER,
5926 				     SSD_MIN_SIZE,
5927 				     /*timeout*/60 * 1000);
5928 			break;
5929 		}
5930 		/*
5931 		 * We'll have to do without, let our probedone
5932 		 * routine finish up for us.
5933 		 */
5934 		start_ccb->csio.data_ptr = NULL;
5935 		probedone(periph, start_ccb);
5936 		return;
5937 	}
5938 	}
5939 	xpt_action(start_ccb);
5940 }
5941 
5942 static void
5943 proberequestdefaultnegotiation(struct cam_periph *periph)
5944 {
5945 	struct ccb_trans_settings cts;
5946 
5947 	xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5948 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5949 #ifdef CAM_NEW_TRAN_CODE
5950 	cts.type = CTS_TYPE_USER_SETTINGS;
5951 #else /* CAM_NEW_TRAN_CODE */
5952 	cts.flags = CCB_TRANS_USER_SETTINGS;
5953 #endif /* CAM_NEW_TRAN_CODE */
5954 	xpt_action((union ccb *)&cts);
5955 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5956 #ifdef CAM_NEW_TRAN_CODE
5957 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
5958 #else /* CAM_NEW_TRAN_CODE */
5959 	cts.flags &= ~CCB_TRANS_USER_SETTINGS;
5960 	cts.flags |= CCB_TRANS_CURRENT_SETTINGS;
5961 #endif /* CAM_NEW_TRAN_CODE */
5962 	xpt_action((union ccb *)&cts);
5963 }
5964 
5965 static void
5966 probedone(struct cam_periph *periph, union ccb *done_ccb)
5967 {
5968 	probe_softc *softc;
5969 	struct cam_path *path;
5970 	u_int32_t  priority;
5971 
5972 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5973 
5974 	softc = (probe_softc *)periph->softc;
5975 	path = done_ccb->ccb_h.path;
5976 	priority = done_ccb->ccb_h.pinfo.priority;
5977 
5978 	switch (softc->action) {
5979 	case PROBE_TUR:
5980 	{
5981 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5982 
5983 			if (cam_periph_error(done_ccb, 0,
5984 					     SF_NO_PRINT, NULL) == ERESTART)
5985 				return;
5986 			else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5987 				/* Don't wedge the queue */
5988 				xpt_release_devq(done_ccb->ccb_h.path,
5989 						 /*count*/1,
5990 						 /*run_queue*/TRUE);
5991 		}
5992 		softc->action = PROBE_INQUIRY;
5993 		xpt_release_ccb(done_ccb);
5994 		xpt_schedule(periph, priority);
5995 		return;
5996 	}
5997 	case PROBE_INQUIRY:
5998 	case PROBE_FULL_INQUIRY:
5999 	{
6000 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
6001 			struct scsi_inquiry_data *inq_buf;
6002 			u_int8_t periph_qual;
6003 
6004 			path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
6005 			inq_buf = &path->device->inq_data;
6006 
6007 			periph_qual = SID_QUAL(inq_buf);
6008 
6009 			switch(periph_qual) {
6010 			case SID_QUAL_LU_CONNECTED:
6011 			{
6012 				u_int8_t len;
6013 
6014 				/*
6015 				 * We conservatively request only
6016 				 * SHORT_INQUIRY_LEN bytes of inquiry
6017 				 * information during our first try
6018 				 * at sending an INQUIRY. If the device
6019 				 * has more information to give,
6020 				 * perform a second request specifying
6021 				 * the amount of information the device
6022 				 * is willing to give.
6023 				 */
6024 				len = inq_buf->additional_length
6025 				    + offsetof(struct scsi_inquiry_data,
6026                                                additional_length) + 1;
6027 				if (softc->action == PROBE_INQUIRY
6028 				 && len > SHORT_INQUIRY_LENGTH) {
6029 					softc->action = PROBE_FULL_INQUIRY;
6030 					xpt_release_ccb(done_ccb);
6031 					xpt_schedule(periph, priority);
6032 					return;
6033 				}
6034 
6035 				xpt_find_quirk(path->device);
6036 
6037 #ifdef CAM_NEW_TRAN_CODE
6038 				xpt_devise_transport(path);
6039 #endif /* CAM_NEW_TRAN_CODE */
6040 				if (INQ_DATA_TQ_ENABLED(inq_buf))
6041 					softc->action = PROBE_MODE_SENSE;
6042 				else
6043 					softc->action = PROBE_SERIAL_NUM;
6044 
6045 				path->device->flags &= ~CAM_DEV_UNCONFIGURED;
6046 
6047 				xpt_release_ccb(done_ccb);
6048 				xpt_schedule(periph, priority);
6049 				return;
6050 			}
6051 			default:
6052 				break;
6053 			}
6054 		} else if (cam_periph_error(done_ccb, 0,
6055 					    done_ccb->ccb_h.target_lun > 0
6056 					    ? SF_RETRY_UA|SF_QUIET_IR
6057 					    : SF_RETRY_UA,
6058 					    &softc->saved_ccb) == ERESTART) {
6059 			return;
6060 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6061 			/* Don't wedge the queue */
6062 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6063 					 /*run_queue*/TRUE);
6064 		}
6065 		/*
6066 		 * If we get to this point, we got an error status back
6067 		 * from the inquiry and the error status doesn't require
6068 		 * automatically retrying the command.  Therefore, the
6069 		 * inquiry failed.  If we had inquiry information before
6070 		 * for this device, but this latest inquiry command failed,
6071 		 * the device has probably gone away.  If this device isn't
6072 		 * already marked unconfigured, notify the peripheral
6073 		 * drivers that this device is no more.
6074 		 */
6075 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
6076 			/* Send the async notification. */
6077 			xpt_async(AC_LOST_DEVICE, path, NULL);
6078 
6079 		xpt_release_ccb(done_ccb);
6080 		break;
6081 	}
6082 	case PROBE_MODE_SENSE:
6083 	{
6084 		struct ccb_scsiio *csio;
6085 		struct scsi_mode_header_6 *mode_hdr;
6086 
6087 		csio = &done_ccb->csio;
6088 		mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
6089 		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
6090 			struct scsi_control_page *page;
6091 			u_int8_t *offset;
6092 
6093 			offset = ((u_int8_t *)&mode_hdr[1])
6094 			    + mode_hdr->blk_desc_len;
6095 			page = (struct scsi_control_page *)offset;
6096 			path->device->queue_flags = page->queue_flags;
6097 		} else if (cam_periph_error(done_ccb, 0,
6098 					    SF_RETRY_UA|SF_NO_PRINT,
6099 					    &softc->saved_ccb) == ERESTART) {
6100 			return;
6101 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6102 			/* Don't wedge the queue */
6103 			xpt_release_devq(done_ccb->ccb_h.path,
6104 					 /*count*/1, /*run_queue*/TRUE);
6105 		}
6106 		xpt_release_ccb(done_ccb);
6107 		free(mode_hdr, M_TEMP);
6108 		softc->action = PROBE_SERIAL_NUM;
6109 		xpt_schedule(periph, priority);
6110 		return;
6111 	}
6112 	case PROBE_SERIAL_NUM:
6113 	{
6114 		struct ccb_scsiio *csio;
6115 		struct scsi_vpd_unit_serial_number *serial_buf;
6116 		u_int32_t  priority;
6117 		int changed;
6118 		int have_serialnum;
6119 
6120 		changed = 1;
6121 		have_serialnum = 0;
6122 		csio = &done_ccb->csio;
6123 		priority = done_ccb->ccb_h.pinfo.priority;
6124 		serial_buf =
6125 		    (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
6126 
6127 		/* Clean up from previous instance of this device */
6128 		if (path->device->serial_num != NULL) {
6129 			free(path->device->serial_num, M_CAMXPT);
6130 			path->device->serial_num = NULL;
6131 			path->device->serial_num_len = 0;
6132 		}
6133 
6134 		if (serial_buf == NULL) {
6135 			/*
6136 			 * Don't process the command as it was never sent
6137 			 */
6138 		} else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
6139 			&& (serial_buf->length > 0)) {
6140 
6141 			have_serialnum = 1;
6142 			path->device->serial_num =
6143 				(u_int8_t *)malloc((serial_buf->length + 1),
6144 						   M_CAMXPT, M_NOWAIT);
6145 			if (path->device->serial_num != NULL) {
6146 				bcopy(serial_buf->serial_num,
6147 				      path->device->serial_num,
6148 				      serial_buf->length);
6149 				path->device->serial_num_len =
6150 				    serial_buf->length;
6151 				path->device->serial_num[serial_buf->length]
6152 				    = '\0';
6153 			}
6154 		} else if (cam_periph_error(done_ccb, 0,
6155 					    SF_RETRY_UA|SF_NO_PRINT,
6156 					    &softc->saved_ccb) == ERESTART) {
6157 			return;
6158 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6159 			/* Don't wedge the queue */
6160 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6161 					 /*run_queue*/TRUE);
6162 		}
6163 
6164 		/*
6165 		 * Let's see if we have seen this device before.
6166 		 */
6167 		if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
6168 			MD5_CTX context;
6169 			u_int8_t digest[16];
6170 
6171 			MD5Init(&context);
6172 
6173 			MD5Update(&context,
6174 				  (unsigned char *)&path->device->inq_data,
6175 				  sizeof(struct scsi_inquiry_data));
6176 
6177 			if (have_serialnum)
6178 				MD5Update(&context, serial_buf->serial_num,
6179 					  serial_buf->length);
6180 
6181 			MD5Final(digest, &context);
6182 			if (bcmp(softc->digest, digest, 16) == 0)
6183 				changed = 0;
6184 
6185 			/*
6186 			 * XXX Do we need to do a TUR in order to ensure
6187 			 *     that the device really hasn't changed???
6188 			 */
6189 			if ((changed != 0)
6190 			 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
6191 				xpt_async(AC_LOST_DEVICE, path, NULL);
6192 		}
6193 		if (serial_buf != NULL)
6194 			free(serial_buf, M_TEMP);
6195 
6196 		if (changed != 0) {
6197 			/*
6198 			 * Now that we have all the necessary
6199 			 * information to safely perform transfer
6200 			 * negotiations... Controllers don't perform
6201 			 * any negotiation or tagged queuing until
6202 			 * after the first XPT_SET_TRAN_SETTINGS ccb is
6203 			 * received.  So, on a new device, just retreive
6204 			 * the user settings, and set them as the current
6205 			 * settings to set the device up.
6206 			 */
6207 			proberequestdefaultnegotiation(periph);
6208 			xpt_release_ccb(done_ccb);
6209 
6210 			/*
6211 			 * Perform a TUR to allow the controller to
6212 			 * perform any necessary transfer negotiation.
6213 			 */
6214 			softc->action = PROBE_TUR_FOR_NEGOTIATION;
6215 			xpt_schedule(periph, priority);
6216 			return;
6217 		}
6218 		xpt_release_ccb(done_ccb);
6219 		break;
6220 	}
6221 	case PROBE_TUR_FOR_NEGOTIATION:
6222 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6223 			/* Don't wedge the queue */
6224 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6225 					 /*run_queue*/TRUE);
6226 		}
6227 
6228 		path->device->flags &= ~CAM_DEV_UNCONFIGURED;
6229 
6230 		if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
6231 			/* Inform the XPT that a new device has been found */
6232 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
6233 			xpt_action(done_ccb);
6234 
6235 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
6236 				  done_ccb);
6237 		}
6238 		xpt_release_ccb(done_ccb);
6239 		break;
6240 	}
6241 	done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
6242 	TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
6243 	done_ccb->ccb_h.status = CAM_REQ_CMP;
6244 	xpt_done(done_ccb);
6245 	if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
6246 		cam_periph_invalidate(periph);
6247 		cam_periph_release(periph);
6248 	} else {
6249 		probeschedule(periph);
6250 	}
6251 }
6252 
6253 static void
6254 probecleanup(struct cam_periph *periph)
6255 {
6256 	free(periph->softc, M_TEMP);
6257 }
6258 
6259 static void
6260 xpt_find_quirk(struct cam_ed *device)
6261 {
6262 	caddr_t	match;
6263 
6264 	match = cam_quirkmatch((caddr_t)&device->inq_data,
6265 			       (caddr_t)xpt_quirk_table,
6266 			       sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
6267 			       sizeof(*xpt_quirk_table), scsi_inquiry_match);
6268 
6269 	if (match == NULL)
6270 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
6271 
6272 	device->quirk = (struct xpt_quirk_entry *)match;
6273 }
6274 
6275 static int
6276 sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS)
6277 {
6278 	int error, bool;
6279 
6280 	bool = cam_srch_hi;
6281 	error = sysctl_handle_int(oidp, &bool, sizeof(bool), req);
6282 	if (error != 0 || req->newptr == NULL)
6283 		return (error);
6284 	if (bool == 0 || bool == 1) {
6285 		cam_srch_hi = bool;
6286 		return (0);
6287 	} else {
6288 		return (EINVAL);
6289 	}
6290 }
6291 
6292 #ifdef CAM_NEW_TRAN_CODE
6293 
6294 static void
6295 xpt_devise_transport(struct cam_path *path)
6296 {
6297 	struct ccb_pathinq cpi;
6298 	struct ccb_trans_settings cts;
6299 	struct scsi_inquiry_data *inq_buf;
6300 
6301 	/* Get transport information from the SIM */
6302 	xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
6303 	cpi.ccb_h.func_code = XPT_PATH_INQ;
6304 	xpt_action((union ccb *)&cpi);
6305 
6306 	inq_buf = NULL;
6307 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
6308 		inq_buf = &path->device->inq_data;
6309 	path->device->protocol = PROTO_SCSI;
6310 	path->device->protocol_version =
6311 	    inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
6312 	path->device->transport = cpi.transport;
6313 	path->device->transport_version = cpi.transport_version;
6314 
6315 	/*
6316 	 * Any device not using SPI3 features should
6317 	 * be considered SPI2 or lower.
6318 	 */
6319 	if (inq_buf != NULL) {
6320 		if (path->device->transport == XPORT_SPI
6321 		 && (inq_buf->spi3data & SID_SPI_MASK) == 0
6322 		 && path->device->transport_version > 2)
6323 			path->device->transport_version = 2;
6324 	} else {
6325 		struct cam_ed* otherdev;
6326 
6327 		for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
6328 		     otherdev != NULL;
6329 		     otherdev = TAILQ_NEXT(otherdev, links)) {
6330 			if (otherdev != path->device)
6331 				break;
6332 		}
6333 
6334 		if (otherdev != NULL) {
6335 			/*
6336 			 * Initially assume the same versioning as
6337 			 * prior luns for this target.
6338 			 */
6339 			path->device->protocol_version =
6340 			    otherdev->protocol_version;
6341 			path->device->transport_version =
6342 			    otherdev->transport_version;
6343 		} else {
6344 			/* Until we know better, opt for safty */
6345 			path->device->protocol_version = 2;
6346 			if (path->device->transport == XPORT_SPI)
6347 				path->device->transport_version = 2;
6348 			else
6349 				path->device->transport_version = 0;
6350 		}
6351 	}
6352 
6353 	/*
6354 	 * XXX
6355 	 * For a device compliant with SPC-2 we should be able
6356 	 * to determine the transport version supported by
6357 	 * scrutinizing the version descriptors in the
6358 	 * inquiry buffer.
6359 	 */
6360 
6361 	/* Tell the controller what we think */
6362 	xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
6363 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
6364 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
6365 	cts.transport = path->device->transport;
6366 	cts.transport_version = path->device->transport_version;
6367 	cts.protocol = path->device->protocol;
6368 	cts.protocol_version = path->device->protocol_version;
6369 	cts.proto_specific.valid = 0;
6370 	cts.xport_specific.valid = 0;
6371 	xpt_action((union ccb *)&cts);
6372 }
6373 
6374 static void
6375 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6376 			  int async_update)
6377 {
6378 	struct	ccb_pathinq cpi;
6379 	struct	ccb_trans_settings cur_cts;
6380 	struct	ccb_trans_settings_scsi *scsi;
6381 	struct	ccb_trans_settings_scsi *cur_scsi;
6382 	struct	cam_sim *sim;
6383 	struct	scsi_inquiry_data *inq_data;
6384 
6385 	if (device == NULL) {
6386 		cts->ccb_h.status = CAM_PATH_INVALID;
6387 		xpt_done((union ccb *)cts);
6388 		return;
6389 	}
6390 
6391 	if (cts->protocol == PROTO_UNKNOWN
6392 	 || cts->protocol == PROTO_UNSPECIFIED) {
6393 		cts->protocol = device->protocol;
6394 		cts->protocol_version = device->protocol_version;
6395 	}
6396 
6397 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
6398 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
6399 		cts->protocol_version = device->protocol_version;
6400 
6401 	if (cts->protocol != device->protocol) {
6402 		xpt_print_path(cts->ccb_h.path);
6403 		printf("Uninitialized Protocol %x:%x?\n",
6404 		       cts->protocol, device->protocol);
6405 		cts->protocol = device->protocol;
6406 	}
6407 
6408 	if (cts->protocol_version > device->protocol_version) {
6409 		if (bootverbose) {
6410 			xpt_print_path(cts->ccb_h.path);
6411 			printf("Down reving Protocol Version from %d to %d?\n",
6412 			       cts->protocol_version, device->protocol_version);
6413 		}
6414 		cts->protocol_version = device->protocol_version;
6415 	}
6416 
6417 	if (cts->transport == XPORT_UNKNOWN
6418 	 || cts->transport == XPORT_UNSPECIFIED) {
6419 		cts->transport = device->transport;
6420 		cts->transport_version = device->transport_version;
6421 	}
6422 
6423 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
6424 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
6425 		cts->transport_version = device->transport_version;
6426 
6427 	if (cts->transport != device->transport) {
6428 		xpt_print_path(cts->ccb_h.path);
6429 		printf("Uninitialized Transport %x:%x?\n",
6430 		       cts->transport, device->transport);
6431 		cts->transport = device->transport;
6432 	}
6433 
6434 	if (cts->transport_version > device->transport_version) {
6435 		if (bootverbose) {
6436 			xpt_print_path(cts->ccb_h.path);
6437 			printf("Down reving Transport Version from %d to %d?\n",
6438 			       cts->transport_version,
6439 			       device->transport_version);
6440 		}
6441 		cts->transport_version = device->transport_version;
6442 	}
6443 
6444 	sim = cts->ccb_h.path->bus->sim;
6445 
6446 	/*
6447 	 * Nothing more of interest to do unless
6448 	 * this is a device connected via the
6449 	 * SCSI protocol.
6450 	 */
6451 	if (cts->protocol != PROTO_SCSI) {
6452 		if (async_update == FALSE)
6453 			(*(sim->sim_action))(sim, (union ccb *)cts);
6454 		return;
6455 	}
6456 
6457 	inq_data = &device->inq_data;
6458 	scsi = &cts->proto_specific.scsi;
6459 	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6460 	cpi.ccb_h.func_code = XPT_PATH_INQ;
6461 	xpt_action((union ccb *)&cpi);
6462 
6463 	/* SCSI specific sanity checking */
6464 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6465 	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
6466 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6467 	 || (device->quirk->mintags == 0)) {
6468 		/*
6469 		 * Can't tag on hardware that doesn't support tags,
6470 		 * doesn't have it enabled, or has broken tag support.
6471 		 */
6472 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6473 	}
6474 
6475 	if (async_update == FALSE) {
6476 		/*
6477 		 * Perform sanity checking against what the
6478 		 * controller and device can do.
6479 		 */
6480 		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6481 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6482 		cur_cts.type = cts->type;
6483 		xpt_action((union ccb *)&cur_cts);
6484 
6485 		cur_scsi = &cur_cts.proto_specific.scsi;
6486 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
6487 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6488 			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
6489 		}
6490 		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
6491 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6492 	}
6493 
6494 	/* SPI specific sanity checking */
6495 	if (cts->transport == XPORT_SPI && async_update == FALSE) {
6496 		u_int spi3caps;
6497 		struct ccb_trans_settings_spi *spi;
6498 		struct ccb_trans_settings_spi *cur_spi;
6499 
6500 		spi = &cts->xport_specific.spi;
6501 
6502 		cur_spi = &cur_cts.xport_specific.spi;
6503 
6504 		/* Fill in any gaps in what the user gave us */
6505 		if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6506 			spi->sync_period = cur_spi->sync_period;
6507 		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6508 			spi->sync_period = 0;
6509 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6510 			spi->sync_offset = cur_spi->sync_offset;
6511 		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6512 			spi->sync_offset = 0;
6513 		if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6514 			spi->ppr_options = cur_spi->ppr_options;
6515 		if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6516 			spi->ppr_options = 0;
6517 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6518 			spi->bus_width = cur_spi->bus_width;
6519 		if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6520 			spi->bus_width = 0;
6521 		if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
6522 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6523 			spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
6524 		}
6525 		if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
6526 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6527 		if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6528 		  && (inq_data->flags & SID_Sync) == 0
6529 		  && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6530 		 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6531 		 || (cur_spi->sync_offset == 0)
6532 		 || (cur_spi->sync_period == 0)) {
6533 			/* Force async */
6534 			spi->sync_period = 0;
6535 			spi->sync_offset = 0;
6536 		}
6537 
6538 		switch (spi->bus_width) {
6539 		case MSG_EXT_WDTR_BUS_32_BIT:
6540 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6541 			  || (inq_data->flags & SID_WBus32) != 0
6542 			  || cts->type == CTS_TYPE_USER_SETTINGS)
6543 			 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6544 				break;
6545 			/* Fall Through to 16-bit */
6546 		case MSG_EXT_WDTR_BUS_16_BIT:
6547 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6548 			  || (inq_data->flags & SID_WBus16) != 0
6549 			  || cts->type == CTS_TYPE_USER_SETTINGS)
6550 			 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6551 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6552 				break;
6553 			}
6554 			/* Fall Through to 8-bit */
6555 		default: /* New bus width?? */
6556 		case MSG_EXT_WDTR_BUS_8_BIT:
6557 			/* All targets can do this */
6558 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6559 			break;
6560 		}
6561 
6562 		spi3caps = cpi.xport_specific.spi.ppr_options;
6563 		if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6564 		 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6565 			spi3caps &= inq_data->spi3data;
6566 
6567 		if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
6568 			spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
6569 
6570 		if ((spi3caps & SID_SPI_IUS) == 0)
6571 			spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
6572 
6573 		if ((spi3caps & SID_SPI_QAS) == 0)
6574 			spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
6575 
6576 		/* No SPI Transfer settings are allowed unless we are wide */
6577 		if (spi->bus_width == 0)
6578 			spi->ppr_options = 0;
6579 
6580 		if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0) {
6581 			/*
6582 			 * Can't tag queue without disconnection.
6583 			 */
6584 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6585 			scsi->valid |= CTS_SCSI_VALID_TQ;
6586 		}
6587 
6588 		/*
6589 		 * If we are currently performing tagged transactions to
6590 		 * this device and want to change its negotiation parameters,
6591 		 * go non-tagged for a bit to give the controller a chance to
6592 		 * negotiate unhampered by tag messages.
6593 		 */
6594 		if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6595 		 && (device->inq_flags & SID_CmdQue) != 0
6596 		 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6597 		 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
6598 				   CTS_SPI_VALID_SYNC_OFFSET|
6599 				   CTS_SPI_VALID_BUS_WIDTH)) != 0)
6600 			xpt_toggle_tags(cts->ccb_h.path);
6601 	}
6602 
6603 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6604 	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
6605 		int device_tagenb;
6606 
6607 		/*
6608 		 * If we are transitioning from tags to no-tags or
6609 		 * vice-versa, we need to carefully freeze and restart
6610 		 * the queue so that we don't overlap tagged and non-tagged
6611 		 * commands.  We also temporarily stop tags if there is
6612 		 * a change in transfer negotiation settings to allow
6613 		 * "tag-less" negotiation.
6614 		 */
6615 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6616 		 || (device->inq_flags & SID_CmdQue) != 0)
6617 			device_tagenb = TRUE;
6618 		else
6619 			device_tagenb = FALSE;
6620 
6621 		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6622 		  && device_tagenb == FALSE)
6623 		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
6624 		  && device_tagenb == TRUE)) {
6625 
6626 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
6627 				/*
6628 				 * Delay change to use tags until after a
6629 				 * few commands have gone to this device so
6630 				 * the controller has time to perform transfer
6631 				 * negotiations without tagged messages getting
6632 				 * in the way.
6633 				 */
6634 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6635 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6636 			} else {
6637 				struct ccb_relsim crs;
6638 
6639 				xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6640 		  		device->inq_flags &= ~SID_CmdQue;
6641 				xpt_dev_ccbq_resize(cts->ccb_h.path,
6642 						    sim->max_dev_openings);
6643 				device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6644 				device->tag_delay_count = 0;
6645 
6646 				xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6647 					      /*priority*/1);
6648 				crs.ccb_h.func_code = XPT_REL_SIMQ;
6649 				crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6650 				crs.openings
6651 				    = crs.release_timeout
6652 				    = crs.qfrozen_cnt
6653 				    = 0;
6654 				xpt_action((union ccb *)&crs);
6655 			}
6656 		}
6657 	}
6658 	if (async_update == FALSE)
6659 		(*(sim->sim_action))(sim, (union ccb *)cts);
6660 }
6661 
6662 #else /* CAM_NEW_TRAN_CODE */
6663 
6664 static void
6665 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6666 			  int async_update)
6667 {
6668 	struct	cam_sim *sim;
6669 	int	qfrozen;
6670 
6671 	sim = cts->ccb_h.path->bus->sim;
6672 	if (async_update == FALSE) {
6673 		struct	scsi_inquiry_data *inq_data;
6674 		struct	ccb_pathinq cpi;
6675 		struct	ccb_trans_settings cur_cts;
6676 
6677 		if (device == NULL) {
6678 			cts->ccb_h.status = CAM_PATH_INVALID;
6679 			xpt_done((union ccb *)cts);
6680 			return;
6681 		}
6682 
6683 		/*
6684 		 * Perform sanity checking against what the
6685 		 * controller and device can do.
6686 		 */
6687 		xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6688 		cpi.ccb_h.func_code = XPT_PATH_INQ;
6689 		xpt_action((union ccb *)&cpi);
6690 		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6691 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6692 		cur_cts.flags = CCB_TRANS_CURRENT_SETTINGS;
6693 		xpt_action((union ccb *)&cur_cts);
6694 		inq_data = &device->inq_data;
6695 
6696 		/* Fill in any gaps in what the user gave us */
6697 		if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0)
6698 			cts->sync_period = cur_cts.sync_period;
6699 		if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0)
6700 			cts->sync_offset = cur_cts.sync_offset;
6701 		if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) == 0)
6702 			cts->bus_width = cur_cts.bus_width;
6703 		if ((cts->valid & CCB_TRANS_DISC_VALID) == 0) {
6704 			cts->flags &= ~CCB_TRANS_DISC_ENB;
6705 			cts->flags |= cur_cts.flags & CCB_TRANS_DISC_ENB;
6706 		}
6707 		if ((cts->valid & CCB_TRANS_TQ_VALID) == 0) {
6708 			cts->flags &= ~CCB_TRANS_TAG_ENB;
6709 			cts->flags |= cur_cts.flags & CCB_TRANS_TAG_ENB;
6710 		}
6711 
6712 		if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6713 		  && (inq_data->flags & SID_Sync) == 0)
6714 		 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6715 		 || (cts->sync_offset == 0)
6716 		 || (cts->sync_period == 0)) {
6717 			/* Force async */
6718 			cts->sync_period = 0;
6719 			cts->sync_offset = 0;
6720 		} else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6721 			&& (inq_data->spi3data & SID_SPI_CLOCK_DT) == 0
6722 			&& cts->sync_period <= 0x9) {
6723 			/*
6724 			 * Don't allow DT transmission rates if the
6725 			 * device does not support it.
6726 			 */
6727 			cts->sync_period = 0xa;
6728 		}
6729 
6730 		switch (cts->bus_width) {
6731 		case MSG_EXT_WDTR_BUS_32_BIT:
6732 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6733 			  || (inq_data->flags & SID_WBus32) != 0)
6734 			 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6735 				break;
6736 			/* FALLTHROUGH to 16-bit */
6737 		case MSG_EXT_WDTR_BUS_16_BIT:
6738 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6739 			  || (inq_data->flags & SID_WBus16) != 0)
6740 			 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6741 				cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6742 				break;
6743 			}
6744 			/* FALLTHROUGH to 8-bit */
6745 		default: /* New bus width?? */
6746 		case MSG_EXT_WDTR_BUS_8_BIT:
6747 			/* All targets can do this */
6748 			cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6749 			break;
6750 		}
6751 
6752 		if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) {
6753 			/*
6754 			 * Can't tag queue without disconnection.
6755 			 */
6756 			cts->flags &= ~CCB_TRANS_TAG_ENB;
6757 			cts->valid |= CCB_TRANS_TQ_VALID;
6758 		}
6759 
6760 		if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6761 	 	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
6762 		 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6763 		 || (device->quirk->mintags == 0)) {
6764 			/*
6765 			 * Can't tag on hardware that doesn't support,
6766 			 * doesn't have it enabled, or has broken tag support.
6767 			 */
6768 			cts->flags &= ~CCB_TRANS_TAG_ENB;
6769 		}
6770 	}
6771 
6772 	qfrozen = FALSE;
6773 	if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
6774 		int device_tagenb;
6775 
6776 		/*
6777 		 * If we are transitioning from tags to no-tags or
6778 		 * vice-versa, we need to carefully freeze and restart
6779 		 * the queue so that we don't overlap tagged and non-tagged
6780 		 * commands.  We also temporarily stop tags if there is
6781 		 * a change in transfer negotiation settings to allow
6782 		 * "tag-less" negotiation.
6783 		 */
6784 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6785 		 || (device->inq_flags & SID_CmdQue) != 0)
6786 			device_tagenb = TRUE;
6787 		else
6788 			device_tagenb = FALSE;
6789 
6790 		if (((cts->flags & CCB_TRANS_TAG_ENB) != 0
6791 		  && device_tagenb == FALSE)
6792 		 || ((cts->flags & CCB_TRANS_TAG_ENB) == 0
6793 		  && device_tagenb == TRUE)) {
6794 
6795 			if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
6796 				/*
6797 				 * Delay change to use tags until after a
6798 				 * few commands have gone to this device so
6799 				 * the controller has time to perform transfer
6800 				 * negotiations without tagged messages getting
6801 				 * in the way.
6802 				 */
6803 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6804 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6805 			} else {
6806 				xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6807 				qfrozen = TRUE;
6808 		  		device->inq_flags &= ~SID_CmdQue;
6809 				xpt_dev_ccbq_resize(cts->ccb_h.path,
6810 						    sim->max_dev_openings);
6811 				device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6812 				device->tag_delay_count = 0;
6813 			}
6814 		}
6815 	}
6816 
6817 	if (async_update == FALSE) {
6818 		/*
6819 		 * If we are currently performing tagged transactions to
6820 		 * this device and want to change its negotiation parameters,
6821 		 * go non-tagged for a bit to give the controller a chance to
6822 		 * negotiate unhampered by tag messages.
6823 		 */
6824 		if ((device->inq_flags & SID_CmdQue) != 0
6825 		 && (cts->flags & (CCB_TRANS_SYNC_RATE_VALID|
6826 				   CCB_TRANS_SYNC_OFFSET_VALID|
6827 				   CCB_TRANS_BUS_WIDTH_VALID)) != 0)
6828 			xpt_toggle_tags(cts->ccb_h.path);
6829 
6830 		(*(sim->sim_action))(sim, (union ccb *)cts);
6831 	}
6832 
6833 	if (qfrozen) {
6834 		struct ccb_relsim crs;
6835 
6836 		xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6837 			      /*priority*/1);
6838 		crs.ccb_h.func_code = XPT_REL_SIMQ;
6839 		crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6840 		crs.openings
6841 		    = crs.release_timeout
6842 		    = crs.qfrozen_cnt
6843 		    = 0;
6844 		xpt_action((union ccb *)&crs);
6845 	}
6846 }
6847 
6848 
6849 #endif /* CAM_NEW_TRAN_CODE */
6850 
6851 static void
6852 xpt_toggle_tags(struct cam_path *path)
6853 {
6854 	struct cam_ed *dev;
6855 
6856 	/*
6857 	 * Give controllers a chance to renegotiate
6858 	 * before starting tag operations.  We
6859 	 * "toggle" tagged queuing off then on
6860 	 * which causes the tag enable command delay
6861 	 * counter to come into effect.
6862 	 */
6863 	dev = path->device;
6864 	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6865 	 || ((dev->inq_flags & SID_CmdQue) != 0
6866  	  && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
6867 		struct ccb_trans_settings cts;
6868 
6869 		xpt_setup_ccb(&cts.ccb_h, path, 1);
6870 #ifdef CAM_NEW_TRAN_CODE
6871 		cts.protocol = PROTO_SCSI;
6872 		cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
6873 		cts.transport = XPORT_UNSPECIFIED;
6874 		cts.transport_version = XPORT_VERSION_UNSPECIFIED;
6875 		cts.proto_specific.scsi.flags = 0;
6876 		cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
6877 #else /* CAM_NEW_TRAN_CODE */
6878 		cts.flags = 0;
6879 		cts.valid = CCB_TRANS_TQ_VALID;
6880 #endif /* CAM_NEW_TRAN_CODE */
6881 		xpt_set_transfer_settings(&cts, path->device,
6882 					  /*async_update*/TRUE);
6883 #ifdef CAM_NEW_TRAN_CODE
6884 		cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
6885 #else /* CAM_NEW_TRAN_CODE */
6886 		cts.flags = CCB_TRANS_TAG_ENB;
6887 #endif /* CAM_NEW_TRAN_CODE */
6888 		xpt_set_transfer_settings(&cts, path->device,
6889 					  /*async_update*/TRUE);
6890 	}
6891 }
6892 
6893 static void
6894 xpt_start_tags(struct cam_path *path)
6895 {
6896 	struct ccb_relsim crs;
6897 	struct cam_ed *device;
6898 	struct cam_sim *sim;
6899 	int    newopenings;
6900 
6901 	device = path->device;
6902 	sim = path->bus->sim;
6903 	device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6904 	xpt_freeze_devq(path, /*count*/1);
6905 	device->inq_flags |= SID_CmdQue;
6906 	if (device->tag_saved_openings != 0)
6907 		newopenings = device->tag_saved_openings;
6908 	else
6909 		newopenings = min(device->quirk->maxtags,
6910 				  sim->max_tagged_dev_openings);
6911 	xpt_dev_ccbq_resize(path, newopenings);
6912 	xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6913 	crs.ccb_h.func_code = XPT_REL_SIMQ;
6914 	crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6915 	crs.openings
6916 	    = crs.release_timeout
6917 	    = crs.qfrozen_cnt
6918 	    = 0;
6919 	xpt_action((union ccb *)&crs);
6920 }
6921 
6922 static int busses_to_config;
6923 static int busses_to_reset;
6924 
6925 static int
6926 xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
6927 {
6928 	if (bus->path_id != CAM_XPT_PATH_ID) {
6929 		struct cam_path path;
6930 		struct ccb_pathinq cpi;
6931 		int can_negotiate;
6932 
6933 		busses_to_config++;
6934 		xpt_compile_path(&path, NULL, bus->path_id,
6935 				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
6936 		xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
6937 		cpi.ccb_h.func_code = XPT_PATH_INQ;
6938 		xpt_action((union ccb *)&cpi);
6939 		can_negotiate = cpi.hba_inquiry;
6940 		can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6941 		if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
6942 		 && can_negotiate)
6943 			busses_to_reset++;
6944 		xpt_release_path(&path);
6945 	}
6946 
6947 	return(1);
6948 }
6949 
6950 static int
6951 xptconfigfunc(struct cam_eb *bus, void *arg)
6952 {
6953 	struct	cam_path *path;
6954 	union	ccb *work_ccb;
6955 
6956 	if (bus->path_id != CAM_XPT_PATH_ID) {
6957 		cam_status status;
6958 		int can_negotiate;
6959 
6960 		work_ccb = xpt_alloc_ccb();
6961 		if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6962 					      CAM_TARGET_WILDCARD,
6963 					      CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6964 			printf("xptconfigfunc: xpt_create_path failed with "
6965 			       "status %#x for bus %d\n", status, bus->path_id);
6966 			printf("xptconfigfunc: halting bus configuration\n");
6967 			xpt_free_ccb(work_ccb);
6968 			busses_to_config--;
6969 			xpt_finishconfig(xpt_periph, NULL);
6970 			return(0);
6971 		}
6972 		xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6973 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6974 		xpt_action(work_ccb);
6975 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6976 			printf("xptconfigfunc: CPI failed on bus %d "
6977 			       "with status %d\n", bus->path_id,
6978 			       work_ccb->ccb_h.status);
6979 			xpt_finishconfig(xpt_periph, work_ccb);
6980 			return(1);
6981 		}
6982 
6983 		can_negotiate = work_ccb->cpi.hba_inquiry;
6984 		can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6985 		if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6986 		 && (can_negotiate != 0)) {
6987 			xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6988 			work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6989 			work_ccb->ccb_h.cbfcnp = NULL;
6990 			CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
6991 				  ("Resetting Bus\n"));
6992 			xpt_action(work_ccb);
6993 			xpt_finishconfig(xpt_periph, work_ccb);
6994 		} else {
6995 			/* Act as though we performed a successful BUS RESET */
6996 			work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6997 			xpt_finishconfig(xpt_periph, work_ccb);
6998 		}
6999 	}
7000 
7001 	return(1);
7002 }
7003 
7004 static void
7005 xpt_config(void *arg)
7006 {
7007 	/*
7008 	 * Now that interrupts are enabled, go find our devices
7009 	 */
7010 
7011 #ifdef CAMDEBUG
7012 	/* Setup debugging flags and path */
7013 #ifdef CAM_DEBUG_FLAGS
7014 	cam_dflags = CAM_DEBUG_FLAGS;
7015 #else /* !CAM_DEBUG_FLAGS */
7016 	cam_dflags = CAM_DEBUG_NONE;
7017 #endif /* CAM_DEBUG_FLAGS */
7018 #ifdef CAM_DEBUG_BUS
7019 	if (cam_dflags != CAM_DEBUG_NONE) {
7020 		if (xpt_create_path(&cam_dpath, xpt_periph,
7021 				    CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
7022 				    CAM_DEBUG_LUN) != CAM_REQ_CMP) {
7023 			printf("xpt_config: xpt_create_path() failed for debug"
7024 			       " target %d:%d:%d, debugging disabled\n",
7025 			       CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
7026 			cam_dflags = CAM_DEBUG_NONE;
7027 		}
7028 	} else
7029 		cam_dpath = NULL;
7030 #else /* !CAM_DEBUG_BUS */
7031 	cam_dpath = NULL;
7032 #endif /* CAM_DEBUG_BUS */
7033 #endif /* CAMDEBUG */
7034 
7035 	/*
7036 	 * Scan all installed busses.
7037 	 */
7038 	xpt_for_all_busses(xptconfigbuscountfunc, NULL);
7039 
7040 	if (busses_to_config == 0) {
7041 		/* Call manually because we don't have any busses */
7042 		xpt_finishconfig(xpt_periph, NULL);
7043 	} else  {
7044 		if (busses_to_reset > 0 && scsi_delay >= 2000) {
7045 			printf("Waiting %d seconds for SCSI "
7046 			       "devices to settle\n", scsi_delay/1000);
7047 		}
7048 		xpt_for_all_busses(xptconfigfunc, NULL);
7049 	}
7050 }
7051 
7052 /*
7053  * If the given device only has one peripheral attached to it, and if that
7054  * peripheral is the passthrough driver, announce it.  This insures that the
7055  * user sees some sort of announcement for every peripheral in their system.
7056  */
7057 static int
7058 xptpassannouncefunc(struct cam_ed *device, void *arg)
7059 {
7060 	struct cam_periph *periph;
7061 	int i;
7062 
7063 	for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
7064 	     periph = SLIST_NEXT(periph, periph_links), i++);
7065 
7066 	periph = SLIST_FIRST(&device->periphs);
7067 	if ((i == 1)
7068 	 && (strncmp(periph->periph_name, "pass", 4) == 0))
7069 		xpt_announce_periph(periph, NULL);
7070 
7071 	return(1);
7072 }
7073 
7074 static void
7075 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
7076 {
7077 	struct	periph_driver **p_drv;
7078 	int	i;
7079 
7080 	if (done_ccb != NULL) {
7081 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
7082 			  ("xpt_finishconfig\n"));
7083 		switch(done_ccb->ccb_h.func_code) {
7084 		case XPT_RESET_BUS:
7085 			if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
7086 				done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
7087 				done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
7088 				done_ccb->crcn.flags = 0;
7089 				xpt_action(done_ccb);
7090 				return;
7091 			}
7092 			/* FALLTHROUGH */
7093 		case XPT_SCAN_BUS:
7094 		default:
7095 			xpt_free_path(done_ccb->ccb_h.path);
7096 			busses_to_config--;
7097 			break;
7098 		}
7099 	}
7100 
7101 	if (busses_to_config == 0) {
7102 		/* Register all the peripheral drivers */
7103 		/* XXX This will have to change when we have loadable modules */
7104 		p_drv = periph_drivers;
7105 		for (i = 0; p_drv[i] != NULL; i++) {
7106 			(*p_drv[i]->init)();
7107 		}
7108 
7109 		/*
7110 		 * Check for devices with no "standard" peripheral driver
7111 		 * attached.  For any devices like that, announce the
7112 		 * passthrough driver so the user will see something.
7113 		 */
7114 		xpt_for_all_devices(xptpassannouncefunc, NULL);
7115 
7116 		/* Release our hook so that the boot can continue. */
7117 		config_intrhook_disestablish(xpt_config_hook);
7118 		free(xpt_config_hook, M_TEMP);
7119 		xpt_config_hook = NULL;
7120 	}
7121 	if (done_ccb != NULL)
7122 		xpt_free_ccb(done_ccb);
7123 }
7124 
7125 static void
7126 xptaction(struct cam_sim *sim, union ccb *work_ccb)
7127 {
7128 	CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
7129 
7130 	switch (work_ccb->ccb_h.func_code) {
7131 	/* Common cases first */
7132 	case XPT_PATH_INQ:		/* Path routing inquiry */
7133 	{
7134 		struct ccb_pathinq *cpi;
7135 
7136 		cpi = &work_ccb->cpi;
7137 		cpi->version_num = 1; /* XXX??? */
7138 		cpi->hba_inquiry = 0;
7139 		cpi->target_sprt = 0;
7140 		cpi->hba_misc = 0;
7141 		cpi->hba_eng_cnt = 0;
7142 		cpi->max_target = 0;
7143 		cpi->max_lun = 0;
7144 		cpi->initiator_id = 0;
7145 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
7146 		strncpy(cpi->hba_vid, "", HBA_IDLEN);
7147 		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
7148 		cpi->unit_number = sim->unit_number;
7149 		cpi->bus_id = sim->bus_id;
7150 		cpi->base_transfer_speed = 0;
7151 #ifdef CAM_NEW_TRAN_CODE
7152 		cpi->protocol = PROTO_UNSPECIFIED;
7153 		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
7154 		cpi->transport = XPORT_UNSPECIFIED;
7155 		cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
7156 #endif /* CAM_NEW_TRAN_CODE */
7157 		cpi->ccb_h.status = CAM_REQ_CMP;
7158 		xpt_done(work_ccb);
7159 		break;
7160 	}
7161 	default:
7162 		work_ccb->ccb_h.status = CAM_REQ_INVALID;
7163 		xpt_done(work_ccb);
7164 		break;
7165 	}
7166 }
7167 
7168 /*
7169  * The xpt as a "controller" has no interrupt sources, so polling
7170  * is a no-op.
7171  */
7172 static void
7173 xptpoll(struct cam_sim *sim)
7174 {
7175 }
7176 
7177 static void
7178 camisr(void *V_queue)
7179 {
7180 	cam_isrq_t *oqueue = V_queue;
7181 	cam_isrq_t queue;
7182 	int	s;
7183 	struct	ccb_hdr *ccb_h;
7184 
7185 	/*
7186 	 * Transfer the ccb_bioq list to a temporary list so we can operate
7187 	 * on it without needing to lock/unlock on every loop.  The concat
7188 	 * function with re-init the real list for us.
7189 	 */
7190 	s = splcam();
7191 	mtx_lock(&cam_bioq_lock);
7192 	TAILQ_INIT(&queue);
7193 	TAILQ_CONCAT(&queue, oqueue, sim_links.tqe);
7194 	mtx_unlock(&cam_bioq_lock);
7195 
7196 	while ((ccb_h = TAILQ_FIRST(&queue)) != NULL) {
7197 		int	runq;
7198 
7199 		TAILQ_REMOVE(&queue, ccb_h, sim_links.tqe);
7200 		ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
7201 		splx(s);
7202 
7203 		CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
7204 			  ("camisr\n"));
7205 
7206 		runq = FALSE;
7207 
7208 		if (ccb_h->flags & CAM_HIGH_POWER) {
7209 			struct highpowerlist	*hphead;
7210 			union ccb		*send_ccb;
7211 
7212 			hphead = &highpowerq;
7213 
7214 			send_ccb = (union ccb *)STAILQ_FIRST(hphead);
7215 
7216 			/*
7217 			 * Increment the count since this command is done.
7218 			 */
7219 			num_highpower++;
7220 
7221 			/*
7222 			 * Any high powered commands queued up?
7223 			 */
7224 			if (send_ccb != NULL) {
7225 
7226 				STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
7227 
7228 				xpt_release_devq(send_ccb->ccb_h.path,
7229 						 /*count*/1, /*runqueue*/TRUE);
7230 			}
7231 		}
7232 		if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
7233 			struct cam_ed *dev;
7234 
7235 			dev = ccb_h->path->device;
7236 
7237 			s = splcam();
7238 			cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
7239 
7240 			if (!SIM_DEAD(ccb_h->path->bus->sim)) {
7241 				ccb_h->path->bus->sim->devq->send_active--;
7242 				ccb_h->path->bus->sim->devq->send_openings++;
7243 			}
7244 			splx(s);
7245 
7246 			if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
7247 			  && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
7248 			 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
7249 			  && (dev->ccbq.dev_active == 0))) {
7250 
7251 				xpt_release_devq(ccb_h->path, /*count*/1,
7252 						 /*run_queue*/TRUE);
7253 			}
7254 
7255 			if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
7256 			 && (--dev->tag_delay_count == 0))
7257 				xpt_start_tags(ccb_h->path);
7258 
7259 			if ((dev->ccbq.queue.entries > 0)
7260 			 && (dev->qfrozen_cnt == 0)
7261 			 && (device_is_send_queued(dev) == 0)) {
7262 				runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
7263 							      dev);
7264 			}
7265 		}
7266 
7267 		if (ccb_h->status & CAM_RELEASE_SIMQ) {
7268 			xpt_release_simq(ccb_h->path->bus->sim,
7269 					 /*run_queue*/TRUE);
7270 			ccb_h->status &= ~CAM_RELEASE_SIMQ;
7271 			runq = FALSE;
7272 		}
7273 
7274 		if ((ccb_h->flags & CAM_DEV_QFRZDIS)
7275 		 && (ccb_h->status & CAM_DEV_QFRZN)) {
7276 			xpt_release_devq(ccb_h->path, /*count*/1,
7277 					 /*run_queue*/TRUE);
7278 			ccb_h->status &= ~CAM_DEV_QFRZN;
7279 		} else if (runq) {
7280 			xpt_run_dev_sendq(ccb_h->path->bus);
7281 		}
7282 
7283 		/* Call the peripheral driver's callback */
7284 		(*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
7285 
7286 		/* Raise IPL for while test */
7287 		s = splcam();
7288 	}
7289 	splx(s);
7290 }
7291 
7292 static void
7293 dead_sim_action(struct cam_sim *sim, union ccb *ccb)
7294 {
7295 
7296 	ccb->ccb_h.status = CAM_DEV_NOT_THERE;
7297 	xpt_done(ccb);
7298 }
7299 
7300 static void
7301 dead_sim_poll(struct cam_sim *sim)
7302 {
7303 }
7304