xref: /freebsd/sys/cam/scsi/scsi_xpt.c (revision 830940567b49bb0c08dfaed40418999e76616909)
1 /*-
2  * Implementation of the SCSI Transport
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_queue.h>
57 #include <cam/cam_periph.h>
58 #include <cam/cam_sim.h>
59 #include <cam/cam_xpt.h>
60 #include <cam/cam_xpt_sim.h>
61 #include <cam/cam_xpt_periph.h>
62 #include <cam/cam_xpt_internal.h>
63 #include <cam/cam_debug.h>
64 
65 #include <cam/scsi/scsi_all.h>
66 #include <cam/scsi/scsi_message.h>
67 #include <cam/scsi/scsi_pass.h>
68 #include <machine/stdarg.h>	/* for xpt_print below */
69 #include "opt_cam.h"
70 
71 struct scsi_quirk_entry {
72 	struct scsi_inquiry_pattern inq_pat;
73 	u_int8_t quirks;
74 #define	CAM_QUIRK_NOLUNS	0x01
75 #define	CAM_QUIRK_NOSERIAL	0x02
76 #define	CAM_QUIRK_HILUNS	0x04
77 #define	CAM_QUIRK_NOHILUNS	0x08
78 	u_int mintags;
79 	u_int maxtags;
80 };
81 #define SCSI_QUIRK(dev)	((struct scsi_quirk_entry *)((dev)->quirk))
82 
83 static int cam_srch_hi = 0;
84 TUNABLE_INT("kern.cam.cam_srch_hi", &cam_srch_hi);
85 static int sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS);
86 SYSCTL_PROC(_kern_cam, OID_AUTO, cam_srch_hi, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
87     sysctl_cam_search_luns, "I",
88     "allow search above LUN 7 for SCSI3 and greater devices");
89 
90 #define	CAM_SCSI2_MAXLUN	8
91 /*
92  * If we're not quirked to search <= the first 8 luns
93  * and we are either quirked to search above lun 8,
94  * or we're > SCSI-2 and we've enabled hilun searching,
95  * or we're > SCSI-2 and the last lun was a success,
96  * we can look for luns above lun 8.
97  */
98 #define	CAN_SRCH_HI_SPARSE(dv)					\
99   (((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_NOHILUNS) == 0) 	\
100   && ((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_HILUNS)		\
101   || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2 && cam_srch_hi)))
102 
103 #define	CAN_SRCH_HI_DENSE(dv)					\
104   (((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_NOHILUNS) == 0) 	\
105   && ((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_HILUNS)		\
106   || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2)))
107 
108 static periph_init_t probe_periph_init;
109 
110 static struct periph_driver probe_driver =
111 {
112 	probe_periph_init, "probe",
113 	TAILQ_HEAD_INITIALIZER(probe_driver.units)
114 };
115 
116 PERIPHDRIVER_DECLARE(probe, probe_driver);
117 
118 typedef enum {
119 	PROBE_TUR,
120 	PROBE_INQUIRY,	/* this counts as DV0 for Basic Domain Validation */
121 	PROBE_FULL_INQUIRY,
122 	PROBE_MODE_SENSE,
123 	PROBE_SERIAL_NUM_0,
124 	PROBE_SERIAL_NUM_1,
125 	PROBE_TUR_FOR_NEGOTIATION,
126 	PROBE_INQUIRY_BASIC_DV1,
127 	PROBE_INQUIRY_BASIC_DV2,
128 	PROBE_DV_EXIT,
129 	PROBE_INVALID
130 } probe_action;
131 
132 static char *probe_action_text[] = {
133 	"PROBE_TUR",
134 	"PROBE_INQUIRY",
135 	"PROBE_FULL_INQUIRY",
136 	"PROBE_MODE_SENSE",
137 	"PROBE_SERIAL_NUM_0",
138 	"PROBE_SERIAL_NUM_1",
139 	"PROBE_TUR_FOR_NEGOTIATION",
140 	"PROBE_INQUIRY_BASIC_DV1",
141 	"PROBE_INQUIRY_BASIC_DV2",
142 	"PROBE_DV_EXIT",
143 	"PROBE_INVALID"
144 };
145 
146 #define PROBE_SET_ACTION(softc, newaction)	\
147 do {									\
148 	char **text;							\
149 	text = probe_action_text;					\
150 	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO,		\
151 	    ("Probe %s to %s\n", text[(softc)->action],			\
152 	    text[(newaction)]));					\
153 	(softc)->action = (newaction);					\
154 } while(0)
155 
156 typedef enum {
157 	PROBE_INQUIRY_CKSUM	= 0x01,
158 	PROBE_SERIAL_CKSUM	= 0x02,
159 	PROBE_NO_ANNOUNCE	= 0x04
160 } probe_flags;
161 
162 typedef struct {
163 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
164 	probe_action	action;
165 	union ccb	saved_ccb;
166 	probe_flags	flags;
167 	MD5_CTX		context;
168 	u_int8_t	digest[16];
169 	struct cam_periph *periph;
170 } probe_softc;
171 
172 static const char quantum[] = "QUANTUM";
173 static const char sony[] = "SONY";
174 static const char west_digital[] = "WDIGTL";
175 static const char samsung[] = "SAMSUNG";
176 static const char seagate[] = "SEAGATE";
177 static const char microp[] = "MICROP";
178 
179 static struct scsi_quirk_entry scsi_quirk_table[] =
180 {
181 	{
182 		/* Reports QUEUE FULL for temporary resource shortages */
183 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
184 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
185 	},
186 	{
187 		/* Reports QUEUE FULL for temporary resource shortages */
188 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
189 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
190 	},
191 	{
192 		/* Reports QUEUE FULL for temporary resource shortages */
193 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
194 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
195 	},
196 	{
197 		/* Broken tagged queuing drive */
198 		{ T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
199 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
200 	},
201 	{
202 		/* Broken tagged queuing drive */
203 		{ T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
204 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
205 	},
206 	{
207 		/* Broken tagged queuing drive */
208 		{ T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
209 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
210 	},
211 	{
212 		/*
213 		 * Unfortunately, the Quantum Atlas III has the same
214 		 * problem as the Atlas II drives above.
215 		 * Reported by: "Johan Granlund" <johan@granlund.nu>
216 		 *
217 		 * For future reference, the drive with the problem was:
218 		 * QUANTUM QM39100TD-SW N1B0
219 		 *
220 		 * It's possible that Quantum will fix the problem in later
221 		 * firmware revisions.  If that happens, the quirk entry
222 		 * will need to be made specific to the firmware revisions
223 		 * with the problem.
224 		 *
225 		 */
226 		/* Reports QUEUE FULL for temporary resource shortages */
227 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
228 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
229 	},
230 	{
231 		/*
232 		 * 18 Gig Atlas III, same problem as the 9G version.
233 		 * Reported by: Andre Albsmeier
234 		 *		<andre.albsmeier@mchp.siemens.de>
235 		 *
236 		 * For future reference, the drive with the problem was:
237 		 * QUANTUM QM318000TD-S N491
238 		 */
239 		/* Reports QUEUE FULL for temporary resource shortages */
240 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
241 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
242 	},
243 	{
244 		/*
245 		 * Broken tagged queuing drive
246 		 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
247 		 *         and: Martin Renters <martin@tdc.on.ca>
248 		 */
249 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
250 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
251 	},
252 		/*
253 		 * The Seagate Medalist Pro drives have very poor write
254 		 * performance with anything more than 2 tags.
255 		 *
256 		 * Reported by:  Paul van der Zwan <paulz@trantor.xs4all.nl>
257 		 * Drive:  <SEAGATE ST36530N 1444>
258 		 *
259 		 * Reported by:  Jeremy Lea <reg@shale.csir.co.za>
260 		 * Drive:  <SEAGATE ST34520W 1281>
261 		 *
262 		 * No one has actually reported that the 9G version
263 		 * (ST39140*) of the Medalist Pro has the same problem, but
264 		 * we're assuming that it does because the 4G and 6.5G
265 		 * versions of the drive are broken.
266 		 */
267 	{
268 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
269 		/*quirks*/0, /*mintags*/2, /*maxtags*/2
270 	},
271 	{
272 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
273 		/*quirks*/0, /*mintags*/2, /*maxtags*/2
274 	},
275 	{
276 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
277 		/*quirks*/0, /*mintags*/2, /*maxtags*/2
278 	},
279 	{
280 		/*
281 		 * Slow when tagged queueing is enabled.  Write performance
282 		 * steadily drops off with more and more concurrent
283 		 * transactions.  Best sequential write performance with
284 		 * tagged queueing turned off and write caching turned on.
285 		 *
286 		 * PR:  kern/10398
287 		 * Submitted by:  Hideaki Okada <hokada@isl.melco.co.jp>
288 		 * Drive:  DCAS-34330 w/ "S65A" firmware.
289 		 *
290 		 * The drive with the problem had the "S65A" firmware
291 		 * revision, and has also been reported (by Stephen J.
292 		 * Roznowski <sjr@home.net>) for a drive with the "S61A"
293 		 * firmware revision.
294 		 *
295 		 * Although no one has reported problems with the 2 gig
296 		 * version of the DCAS drive, the assumption is that it
297 		 * has the same problems as the 4 gig version.  Therefore
298 		 * this quirk entries disables tagged queueing for all
299 		 * DCAS drives.
300 		 */
301 		{ T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
302 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
303 	},
304 	{
305 		/* Broken tagged queuing drive */
306 		{ T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
307 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
308 	},
309 	{
310 		/* Broken tagged queuing drive */
311 		{ T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
312 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
313 	},
314 	{
315 		/* This does not support other than LUN 0 */
316 		{ T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*" },
317 		CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
318 	},
319 	{
320 		/*
321 		 * Broken tagged queuing drive.
322 		 * Submitted by:
323 		 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
324 		 * in PR kern/9535
325 		 */
326 		{ T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
327 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
328 	},
329         {
330 		/*
331 		 * Slow when tagged queueing is enabled. (1.5MB/sec versus
332 		 * 8MB/sec.)
333 		 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
334 		 * Best performance with these drives is achieved with
335 		 * tagged queueing turned off, and write caching turned on.
336 		 */
337 		{ T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
338 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
339         },
340         {
341 		/*
342 		 * Slow when tagged queueing is enabled. (1.5MB/sec versus
343 		 * 8MB/sec.)
344 		 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
345 		 * Best performance with these drives is achieved with
346 		 * tagged queueing turned off, and write caching turned on.
347 		 */
348 		{ T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
349 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
350         },
351 	{
352 		/*
353 		 * Doesn't handle queue full condition correctly,
354 		 * so we need to limit maxtags to what the device
355 		 * can handle instead of determining this automatically.
356 		 */
357 		{ T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
358 		/*quirks*/0, /*mintags*/2, /*maxtags*/32
359 	},
360 	{
361 		/* Really only one LUN */
362 		{ T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
363 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
364 	},
365 	{
366 		/* I can't believe we need a quirk for DPT volumes. */
367 		{ T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
368 		CAM_QUIRK_NOLUNS,
369 		/*mintags*/0, /*maxtags*/255
370 	},
371 	{
372 		/*
373 		 * Many Sony CDROM drives don't like multi-LUN probing.
374 		 */
375 		{ T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
376 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
377 	},
378 	{
379 		/*
380 		 * This drive doesn't like multiple LUN probing.
381 		 * Submitted by:  Parag Patel <parag@cgt.com>
382 		 */
383 		{ T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R   CDU9*", "*" },
384 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
385 	},
386 	{
387 		{ T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
388 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
389 	},
390 	{
391 		/*
392 		 * The 8200 doesn't like multi-lun probing, and probably
393 		 * don't like serial number requests either.
394 		 */
395 		{
396 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
397 			"EXB-8200*", "*"
398 		},
399 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
400 	},
401 	{
402 		/*
403 		 * Let's try the same as above, but for a drive that says
404 		 * it's an IPL-6860 but is actually an EXB 8200.
405 		 */
406 		{
407 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
408 			"IPL-6860*", "*"
409 		},
410 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
411 	},
412 	{
413 		/*
414 		 * These Hitachi drives don't like multi-lun probing.
415 		 * The PR submitter has a DK319H, but says that the Linux
416 		 * kernel has a similar work-around for the DK312 and DK314,
417 		 * so all DK31* drives are quirked here.
418 		 * PR:            misc/18793
419 		 * Submitted by:  Paul Haddad <paul@pth.com>
420 		 */
421 		{ T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
422 		CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
423 	},
424 	{
425 		/*
426 		 * The Hitachi CJ series with J8A8 firmware apparantly has
427 		 * problems with tagged commands.
428 		 * PR: 23536
429 		 * Reported by: amagai@nue.org
430 		 */
431 		{ T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
432 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
433 	},
434 	{
435 		/*
436 		 * These are the large storage arrays.
437 		 * Submitted by:  William Carrel <william.carrel@infospace.com>
438 		 */
439 		{ T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
440 		CAM_QUIRK_HILUNS, 2, 1024
441 	},
442 	{
443 		/*
444 		 * This old revision of the TDC3600 is also SCSI-1, and
445 		 * hangs upon serial number probing.
446 		 */
447 		{
448 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
449 			" TDC 3600", "U07:"
450 		},
451 		CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
452 	},
453 	{
454 		/*
455 		 * Would repond to all LUNs if asked for.
456 		 */
457 		{
458 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
459 			"CP150", "*"
460 		},
461 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
462 	},
463 	{
464 		/*
465 		 * Would repond to all LUNs if asked for.
466 		 */
467 		{
468 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
469 			"96X2*", "*"
470 		},
471 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
472 	},
473 	{
474 		/* Submitted by: Matthew Dodd <winter@jurai.net> */
475 		{ T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
476 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
477 	},
478 	{
479 		/* Submitted by: Matthew Dodd <winter@jurai.net> */
480 		{ T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
481 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
482 	},
483 	{
484 		/* TeraSolutions special settings for TRC-22 RAID */
485 		{ T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
486 		  /*quirks*/0, /*mintags*/55, /*maxtags*/255
487 	},
488 	{
489 		/* Veritas Storage Appliance */
490 		{ T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
491 		  CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
492 	},
493 	{
494 		/*
495 		 * Would respond to all LUNs.  Device type and removable
496 		 * flag are jumper-selectable.
497 		 */
498 		{ T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
499 		  "Tahiti 1", "*"
500 		},
501 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
502 	},
503 	{
504 		/* EasyRAID E5A aka. areca ARC-6010 */
505 		{ T_DIRECT, SIP_MEDIA_FIXED, "easyRAID", "*", "*" },
506 		  CAM_QUIRK_NOHILUNS, /*mintags*/2, /*maxtags*/255
507 	},
508 	{
509 		{ T_ENCLOSURE, SIP_MEDIA_FIXED, "DP", "BACKPLANE", "*" },
510 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
511 	},
512 	{
513 		/* Default tagged queuing parameters for all devices */
514 		{
515 		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
516 		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
517 		},
518 		/*quirks*/0, /*mintags*/2, /*maxtags*/255
519 	},
520 };
521 
522 static const int scsi_quirk_table_size =
523 	sizeof(scsi_quirk_table) / sizeof(*scsi_quirk_table);
524 
525 static cam_status	proberegister(struct cam_periph *periph,
526 				      void *arg);
527 static void	 probeschedule(struct cam_periph *probe_periph);
528 static void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
529 static void	 proberequestdefaultnegotiation(struct cam_periph *periph);
530 static int       proberequestbackoff(struct cam_periph *periph,
531 				     struct cam_ed *device);
532 static void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
533 static void	 probecleanup(struct cam_periph *periph);
534 static void	 scsi_find_quirk(struct cam_ed *device);
535 static void	 scsi_scan_bus(struct cam_periph *periph, union ccb *ccb);
536 static void	 scsi_scan_lun(struct cam_periph *periph,
537 			       struct cam_path *path, cam_flags flags,
538 			       union ccb *ccb);
539 static void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
540 static struct cam_ed *
541 		 scsi_alloc_device(struct cam_eb *bus, struct cam_et *target,
542 				   lun_id_t lun_id);
543 static void	 scsi_devise_transport(struct cam_path *path);
544 static void	 scsi_set_transfer_settings(struct ccb_trans_settings *cts,
545 					    struct cam_ed *device,
546 					    int async_update);
547 static void	 scsi_toggle_tags(struct cam_path *path);
548 static void	 scsi_dev_async(u_int32_t async_code,
549 				struct cam_eb *bus,
550 				struct cam_et *target,
551 				struct cam_ed *device,
552 				void *async_arg);
553 static void	 scsi_action(union ccb *start_ccb);
554 
555 static struct xpt_xport scsi_xport = {
556 	.alloc_device = scsi_alloc_device,
557 	.action = scsi_action,
558 	.async = scsi_dev_async,
559 };
560 
561 struct xpt_xport *
562 scsi_get_xport(void)
563 {
564 	return (&scsi_xport);
565 }
566 
567 static void
568 probe_periph_init()
569 {
570 }
571 
572 static cam_status
573 proberegister(struct cam_periph *periph, void *arg)
574 {
575 	union ccb *request_ccb;	/* CCB representing the probe request */
576 	cam_status status;
577 	probe_softc *softc;
578 
579 	request_ccb = (union ccb *)arg;
580 	if (periph == NULL) {
581 		printf("proberegister: periph was NULL!!\n");
582 		return(CAM_REQ_CMP_ERR);
583 	}
584 
585 	if (request_ccb == NULL) {
586 		printf("proberegister: no probe CCB, "
587 		       "can't register device\n");
588 		return(CAM_REQ_CMP_ERR);
589 	}
590 
591 	softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
592 
593 	if (softc == NULL) {
594 		printf("proberegister: Unable to probe new device. "
595 		       "Unable to allocate softc\n");
596 		return(CAM_REQ_CMP_ERR);
597 	}
598 	TAILQ_INIT(&softc->request_ccbs);
599 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
600 			  periph_links.tqe);
601 	softc->flags = 0;
602 	periph->softc = softc;
603 	softc->periph = periph;
604 	softc->action = PROBE_INVALID;
605 	status = cam_periph_acquire(periph);
606 	if (status != CAM_REQ_CMP) {
607 		return (status);
608 	}
609 
610 
611 	/*
612 	 * Ensure we've waited at least a bus settle
613 	 * delay before attempting to probe the device.
614 	 * For HBAs that don't do bus resets, this won't make a difference.
615 	 */
616 	cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
617 				      scsi_delay);
618 	probeschedule(periph);
619 	return(CAM_REQ_CMP);
620 }
621 
622 static void
623 probeschedule(struct cam_periph *periph)
624 {
625 	struct ccb_pathinq cpi;
626 	union ccb *ccb;
627 	probe_softc *softc;
628 
629 	softc = (probe_softc *)periph->softc;
630 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
631 
632 	xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
633 	cpi.ccb_h.func_code = XPT_PATH_INQ;
634 	xpt_action((union ccb *)&cpi);
635 
636 	/*
637 	 * If a device has gone away and another device, or the same one,
638 	 * is back in the same place, it should have a unit attention
639 	 * condition pending.  It will not report the unit attention in
640 	 * response to an inquiry, which may leave invalid transfer
641 	 * negotiations in effect.  The TUR will reveal the unit attention
642 	 * condition.  Only send the TUR for lun 0, since some devices
643 	 * will get confused by commands other than inquiry to non-existent
644 	 * luns.  If you think a device has gone away start your scan from
645 	 * lun 0.  This will insure that any bogus transfer settings are
646 	 * invalidated.
647 	 *
648 	 * If we haven't seen the device before and the controller supports
649 	 * some kind of transfer negotiation, negotiate with the first
650 	 * sent command if no bus reset was performed at startup.  This
651 	 * ensures that the device is not confused by transfer negotiation
652 	 * settings left over by loader or BIOS action.
653 	 */
654 	if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
655 	 && (ccb->ccb_h.target_lun == 0)) {
656 		PROBE_SET_ACTION(softc, PROBE_TUR);
657 	} else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
658 	      && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
659 		proberequestdefaultnegotiation(periph);
660 		PROBE_SET_ACTION(softc, PROBE_INQUIRY);
661 	} else {
662 		PROBE_SET_ACTION(softc, PROBE_INQUIRY);
663 	}
664 
665 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
666 		softc->flags |= PROBE_NO_ANNOUNCE;
667 	else
668 		softc->flags &= ~PROBE_NO_ANNOUNCE;
669 
670 	xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
671 }
672 
673 static void
674 probestart(struct cam_periph *periph, union ccb *start_ccb)
675 {
676 	/* Probe the device that our peripheral driver points to */
677 	struct ccb_scsiio *csio;
678 	probe_softc *softc;
679 
680 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
681 
682 	softc = (probe_softc *)periph->softc;
683 	csio = &start_ccb->csio;
684 
685 	switch (softc->action) {
686 	case PROBE_TUR:
687 	case PROBE_TUR_FOR_NEGOTIATION:
688 	case PROBE_DV_EXIT:
689 	{
690 		scsi_test_unit_ready(csio,
691 				     /*retries*/10,
692 				     probedone,
693 				     MSG_SIMPLE_Q_TAG,
694 				     SSD_FULL_SIZE,
695 				     /*timeout*/60000);
696 		break;
697 	}
698 	case PROBE_INQUIRY:
699 	case PROBE_FULL_INQUIRY:
700 	case PROBE_INQUIRY_BASIC_DV1:
701 	case PROBE_INQUIRY_BASIC_DV2:
702 	{
703 		u_int inquiry_len;
704 		struct scsi_inquiry_data *inq_buf;
705 
706 		inq_buf = &periph->path->device->inq_data;
707 
708 		/*
709 		 * If the device is currently configured, we calculate an
710 		 * MD5 checksum of the inquiry data, and if the serial number
711 		 * length is greater than 0, add the serial number data
712 		 * into the checksum as well.  Once the inquiry and the
713 		 * serial number check finish, we attempt to figure out
714 		 * whether we still have the same device.
715 		 */
716 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
717 
718 			MD5Init(&softc->context);
719 			MD5Update(&softc->context, (unsigned char *)inq_buf,
720 				  sizeof(struct scsi_inquiry_data));
721 			softc->flags |= PROBE_INQUIRY_CKSUM;
722 			if (periph->path->device->serial_num_len > 0) {
723 				MD5Update(&softc->context,
724 					  periph->path->device->serial_num,
725 					  periph->path->device->serial_num_len);
726 				softc->flags |= PROBE_SERIAL_CKSUM;
727 			}
728 			MD5Final(softc->digest, &softc->context);
729 		}
730 
731 		if (softc->action == PROBE_INQUIRY)
732 			inquiry_len = SHORT_INQUIRY_LENGTH;
733 		else
734 			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
735 
736 		/*
737 		 * Some parallel SCSI devices fail to send an
738 		 * ignore wide residue message when dealing with
739 		 * odd length inquiry requests.  Round up to be
740 		 * safe.
741 		 */
742 		inquiry_len = roundup2(inquiry_len, 2);
743 
744 		if (softc->action == PROBE_INQUIRY_BASIC_DV1
745 		 || softc->action == PROBE_INQUIRY_BASIC_DV2) {
746 			inq_buf = malloc(inquiry_len, M_CAMXPT, M_NOWAIT);
747 		}
748 		if (inq_buf == NULL) {
749 			xpt_print(periph->path, "malloc failure- skipping Basic"
750 			    "Domain Validation\n");
751 			PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
752 			scsi_test_unit_ready(csio,
753 					     /*retries*/4,
754 					     probedone,
755 					     MSG_SIMPLE_Q_TAG,
756 					     SSD_FULL_SIZE,
757 					     /*timeout*/60000);
758 			break;
759 		}
760 		scsi_inquiry(csio,
761 			     /*retries*/4,
762 			     probedone,
763 			     MSG_SIMPLE_Q_TAG,
764 			     (u_int8_t *)inq_buf,
765 			     inquiry_len,
766 			     /*evpd*/FALSE,
767 			     /*page_code*/0,
768 			     SSD_MIN_SIZE,
769 			     /*timeout*/60 * 1000);
770 		break;
771 	}
772 	case PROBE_MODE_SENSE:
773 	{
774 		void  *mode_buf;
775 		int    mode_buf_len;
776 
777 		mode_buf_len = sizeof(struct scsi_mode_header_6)
778 			     + sizeof(struct scsi_mode_blk_desc)
779 			     + sizeof(struct scsi_control_page);
780 		mode_buf = malloc(mode_buf_len, M_CAMXPT, M_NOWAIT);
781 		if (mode_buf != NULL) {
782 	                scsi_mode_sense(csio,
783 					/*retries*/4,
784 					probedone,
785 					MSG_SIMPLE_Q_TAG,
786 					/*dbd*/FALSE,
787 					SMS_PAGE_CTRL_CURRENT,
788 					SMS_CONTROL_MODE_PAGE,
789 					mode_buf,
790 					mode_buf_len,
791 					SSD_FULL_SIZE,
792 					/*timeout*/60000);
793 			break;
794 		}
795 		xpt_print(periph->path, "Unable to mode sense control page - "
796 		    "malloc failure\n");
797 		PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM_0);
798 	}
799 	/* FALLTHROUGH */
800 	case PROBE_SERIAL_NUM_0:
801 	{
802 		struct scsi_vpd_supported_page_list *vpd_list = NULL;
803 		struct cam_ed *device;
804 
805 		device = periph->path->device;
806 		if ((SCSI_QUIRK(device)->quirks & CAM_QUIRK_NOSERIAL) == 0) {
807 			vpd_list = malloc(sizeof(*vpd_list), M_CAMXPT,
808 			    M_NOWAIT | M_ZERO);
809 		}
810 
811 		if (vpd_list != NULL) {
812 			scsi_inquiry(csio,
813 				     /*retries*/4,
814 				     probedone,
815 				     MSG_SIMPLE_Q_TAG,
816 				     (u_int8_t *)vpd_list,
817 				     sizeof(*vpd_list),
818 				     /*evpd*/TRUE,
819 				     SVPD_SUPPORTED_PAGE_LIST,
820 				     SSD_MIN_SIZE,
821 				     /*timeout*/60 * 1000);
822 			break;
823 		}
824 		/*
825 		 * We'll have to do without, let our probedone
826 		 * routine finish up for us.
827 		 */
828 		start_ccb->csio.data_ptr = NULL;
829 		probedone(periph, start_ccb);
830 		return;
831 	}
832 	case PROBE_SERIAL_NUM_1:
833 	{
834 		struct scsi_vpd_unit_serial_number *serial_buf;
835 		struct cam_ed* device;
836 
837 		serial_buf = NULL;
838 		device = periph->path->device;
839 		if (device->serial_num != NULL) {
840 			free(device->serial_num, M_CAMXPT);
841 			device->serial_num = NULL;
842 			device->serial_num_len = 0;
843 		}
844 
845 		serial_buf = (struct scsi_vpd_unit_serial_number *)
846 			malloc(sizeof(*serial_buf), M_CAMXPT, M_NOWAIT|M_ZERO);
847 
848 		if (serial_buf != NULL) {
849 			scsi_inquiry(csio,
850 				     /*retries*/4,
851 				     probedone,
852 				     MSG_SIMPLE_Q_TAG,
853 				     (u_int8_t *)serial_buf,
854 				     sizeof(*serial_buf),
855 				     /*evpd*/TRUE,
856 				     SVPD_UNIT_SERIAL_NUMBER,
857 				     SSD_MIN_SIZE,
858 				     /*timeout*/60 * 1000);
859 			break;
860 		}
861 		/*
862 		 * We'll have to do without, let our probedone
863 		 * routine finish up for us.
864 		 */
865 		start_ccb->csio.data_ptr = NULL;
866 		probedone(periph, start_ccb);
867 		return;
868 	}
869 	case PROBE_INVALID:
870 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
871 		    ("probestart: invalid action state\n"));
872 	default:
873 		break;
874 	}
875 	xpt_action(start_ccb);
876 }
877 
878 static void
879 proberequestdefaultnegotiation(struct cam_periph *periph)
880 {
881 	struct ccb_trans_settings cts;
882 
883 	xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
884 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
885 	cts.type = CTS_TYPE_USER_SETTINGS;
886 	xpt_action((union ccb *)&cts);
887 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
888 		return;
889 	}
890 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
891 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
892 	xpt_action((union ccb *)&cts);
893 }
894 
895 /*
896  * Backoff Negotiation Code- only pertinent for SPI devices.
897  */
898 static int
899 proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
900 {
901 	struct ccb_trans_settings cts;
902 	struct ccb_trans_settings_spi *spi;
903 
904 	memset(&cts, 0, sizeof (cts));
905 	xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
906 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
907 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
908 	xpt_action((union ccb *)&cts);
909 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
910 		if (bootverbose) {
911 			xpt_print(periph->path,
912 			    "failed to get current device settings\n");
913 		}
914 		return (0);
915 	}
916 	if (cts.transport != XPORT_SPI) {
917 		if (bootverbose) {
918 			xpt_print(periph->path, "not SPI transport\n");
919 		}
920 		return (0);
921 	}
922 	spi = &cts.xport_specific.spi;
923 
924 	/*
925 	 * We cannot renegotiate sync rate if we don't have one.
926 	 */
927 	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
928 		if (bootverbose) {
929 			xpt_print(periph->path, "no sync rate known\n");
930 		}
931 		return (0);
932 	}
933 
934 	/*
935 	 * We'll assert that we don't have to touch PPR options- the
936 	 * SIM will see what we do with period and offset and adjust
937 	 * the PPR options as appropriate.
938 	 */
939 
940 	/*
941 	 * A sync rate with unknown or zero offset is nonsensical.
942 	 * A sync period of zero means Async.
943 	 */
944 	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
945 	 || spi->sync_offset == 0 || spi->sync_period == 0) {
946 		if (bootverbose) {
947 			xpt_print(periph->path, "no sync rate available\n");
948 		}
949 		return (0);
950 	}
951 
952 	if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
953 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
954 		    ("hit async: giving up on DV\n"));
955 		return (0);
956 	}
957 
958 
959 	/*
960 	 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
961 	 * We don't try to remember 'last' settings to see if the SIM actually
962 	 * gets into the speed we want to set. We check on the SIM telling
963 	 * us that a requested speed is bad, but otherwise don't try and
964 	 * check the speed due to the asynchronous and handshake nature
965 	 * of speed setting.
966 	 */
967 	spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
968 	for (;;) {
969 		spi->sync_period++;
970 		if (spi->sync_period >= 0xf) {
971 			spi->sync_period = 0;
972 			spi->sync_offset = 0;
973 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
974 			    ("setting to async for DV\n"));
975 			/*
976 			 * Once we hit async, we don't want to try
977 			 * any more settings.
978 			 */
979 			device->flags |= CAM_DEV_DV_HIT_BOTTOM;
980 		} else if (bootverbose) {
981 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
982 			    ("DV: period 0x%x\n", spi->sync_period));
983 			printf("setting period to 0x%x\n", spi->sync_period);
984 		}
985 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
986 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
987 		xpt_action((union ccb *)&cts);
988 		if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
989 			break;
990 		}
991 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
992 		    ("DV: failed to set period 0x%x\n", spi->sync_period));
993 		if (spi->sync_period == 0) {
994 			return (0);
995 		}
996 	}
997 	return (1);
998 }
999 
1000 static void
1001 probedone(struct cam_periph *periph, union ccb *done_ccb)
1002 {
1003 	probe_softc *softc;
1004 	struct cam_path *path;
1005 	u_int32_t  priority;
1006 
1007 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
1008 
1009 	softc = (probe_softc *)periph->softc;
1010 	path = done_ccb->ccb_h.path;
1011 	priority = done_ccb->ccb_h.pinfo.priority;
1012 
1013 	switch (softc->action) {
1014 	case PROBE_TUR:
1015 	{
1016 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1017 
1018 			if (cam_periph_error(done_ccb, 0,
1019 					     SF_NO_PRINT, NULL) == ERESTART)
1020 				return;
1021 			else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1022 				/* Don't wedge the queue */
1023 				xpt_release_devq(done_ccb->ccb_h.path,
1024 						 /*count*/1,
1025 						 /*run_queue*/TRUE);
1026 		}
1027 		PROBE_SET_ACTION(softc, PROBE_INQUIRY);
1028 		xpt_release_ccb(done_ccb);
1029 		xpt_schedule(periph, priority);
1030 		return;
1031 	}
1032 	case PROBE_INQUIRY:
1033 	case PROBE_FULL_INQUIRY:
1034 	{
1035 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1036 			struct scsi_inquiry_data *inq_buf;
1037 			u_int8_t periph_qual;
1038 
1039 			path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1040 			inq_buf = &path->device->inq_data;
1041 
1042 			periph_qual = SID_QUAL(inq_buf);
1043 
1044 			switch(periph_qual) {
1045 			case SID_QUAL_LU_CONNECTED:
1046 			{
1047 				u_int8_t len;
1048 
1049 				/*
1050 				 * We conservatively request only
1051 				 * SHORT_INQUIRY_LEN bytes of inquiry
1052 				 * information during our first try
1053 				 * at sending an INQUIRY. If the device
1054 				 * has more information to give,
1055 				 * perform a second request specifying
1056 				 * the amount of information the device
1057 				 * is willing to give.
1058 				 */
1059 				len = inq_buf->additional_length
1060 				    + offsetof(struct scsi_inquiry_data,
1061                                                additional_length) + 1;
1062 				if (softc->action == PROBE_INQUIRY
1063 				    && len > SHORT_INQUIRY_LENGTH) {
1064 					PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1065 					xpt_release_ccb(done_ccb);
1066 					xpt_schedule(periph, priority);
1067 					return;
1068 				}
1069 
1070 				scsi_find_quirk(path->device);
1071 
1072 				scsi_devise_transport(path);
1073 				if (INQ_DATA_TQ_ENABLED(inq_buf))
1074 					PROBE_SET_ACTION(softc, PROBE_MODE_SENSE);
1075 				else
1076 					PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM_0);
1077 
1078 				path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1079 
1080 				xpt_release_ccb(done_ccb);
1081 				xpt_schedule(periph, priority);
1082 				return;
1083 			}
1084 			default:
1085 				break;
1086 			}
1087 		} else if (cam_periph_error(done_ccb, 0,
1088 					    done_ccb->ccb_h.target_lun > 0
1089 					    ? SF_RETRY_UA|SF_QUIET_IR
1090 					    : SF_RETRY_UA,
1091 					    &softc->saved_ccb) == ERESTART) {
1092 			return;
1093 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1094 			/* Don't wedge the queue */
1095 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1096 					 /*run_queue*/TRUE);
1097 		}
1098 		/*
1099 		 * If we get to this point, we got an error status back
1100 		 * from the inquiry and the error status doesn't require
1101 		 * automatically retrying the command.  Therefore, the
1102 		 * inquiry failed.  If we had inquiry information before
1103 		 * for this device, but this latest inquiry command failed,
1104 		 * the device has probably gone away.  If this device isn't
1105 		 * already marked unconfigured, notify the peripheral
1106 		 * drivers that this device is no more.
1107 		 */
1108 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
1109 			/* Send the async notification. */
1110 			xpt_async(AC_LOST_DEVICE, path, NULL);
1111 
1112 		xpt_release_ccb(done_ccb);
1113 		break;
1114 	}
1115 	case PROBE_MODE_SENSE:
1116 	{
1117 		struct ccb_scsiio *csio;
1118 		struct scsi_mode_header_6 *mode_hdr;
1119 
1120 		csio = &done_ccb->csio;
1121 		mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
1122 		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1123 			struct scsi_control_page *page;
1124 			u_int8_t *offset;
1125 
1126 			offset = ((u_int8_t *)&mode_hdr[1])
1127 			    + mode_hdr->blk_desc_len;
1128 			page = (struct scsi_control_page *)offset;
1129 			path->device->queue_flags = page->queue_flags;
1130 		} else if (cam_periph_error(done_ccb, 0,
1131 					    SF_RETRY_UA|SF_NO_PRINT,
1132 					    &softc->saved_ccb) == ERESTART) {
1133 			return;
1134 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1135 			/* Don't wedge the queue */
1136 			xpt_release_devq(done_ccb->ccb_h.path,
1137 					 /*count*/1, /*run_queue*/TRUE);
1138 		}
1139 		xpt_release_ccb(done_ccb);
1140 		free(mode_hdr, M_CAMXPT);
1141 		PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM_0);
1142 		xpt_schedule(periph, priority);
1143 		return;
1144 	}
1145 	case PROBE_SERIAL_NUM_0:
1146 	{
1147 		struct ccb_scsiio *csio;
1148 		struct scsi_vpd_supported_page_list *page_list;
1149 		int length, serialnum_supported, i;
1150 
1151 		serialnum_supported = 0;
1152 		csio = &done_ccb->csio;
1153 		page_list =
1154 		    (struct scsi_vpd_supported_page_list *)csio->data_ptr;
1155 
1156 		if (page_list == NULL) {
1157 			/*
1158 			 * Don't process the command as it was never sent
1159 			 */
1160 		} else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
1161 		    && (page_list->length > 0)) {
1162 			length = min(page_list->length,
1163 			    SVPD_SUPPORTED_PAGES_SIZE);
1164 			for (i = 0; i < length; i++) {
1165 				if (page_list->list[i] ==
1166 				    SVPD_UNIT_SERIAL_NUMBER) {
1167 					serialnum_supported = 1;
1168 					break;
1169 				}
1170 			}
1171 		} else if (cam_periph_error(done_ccb, 0,
1172 					    SF_RETRY_UA|SF_NO_PRINT,
1173 					    &softc->saved_ccb) == ERESTART) {
1174 			return;
1175 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1176 			/* Don't wedge the queue */
1177 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1178 					 /*run_queue*/TRUE);
1179 		}
1180 
1181 		if (page_list != NULL)
1182 			free(page_list, M_CAMXPT);
1183 
1184 		if (serialnum_supported) {
1185 			xpt_release_ccb(done_ccb);
1186 			PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM_1);
1187 			xpt_schedule(periph, priority);
1188 			return;
1189 		}
1190 
1191 		csio->data_ptr = NULL;
1192 		/* FALLTHROUGH */
1193 	}
1194 
1195 	case PROBE_SERIAL_NUM_1:
1196 	{
1197 		struct ccb_scsiio *csio;
1198 		struct scsi_vpd_unit_serial_number *serial_buf;
1199 		u_int32_t  priority;
1200 		int changed;
1201 		int have_serialnum;
1202 
1203 		changed = 1;
1204 		have_serialnum = 0;
1205 		csio = &done_ccb->csio;
1206 		priority = done_ccb->ccb_h.pinfo.priority;
1207 		serial_buf =
1208 		    (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
1209 
1210 		/* Clean up from previous instance of this device */
1211 		if (path->device->serial_num != NULL) {
1212 			free(path->device->serial_num, M_CAMXPT);
1213 			path->device->serial_num = NULL;
1214 			path->device->serial_num_len = 0;
1215 		}
1216 
1217 		if (serial_buf == NULL) {
1218 			/*
1219 			 * Don't process the command as it was never sent
1220 			 */
1221 		} else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
1222 			&& (serial_buf->length > 0)) {
1223 
1224 			have_serialnum = 1;
1225 			path->device->serial_num =
1226 				(u_int8_t *)malloc((serial_buf->length + 1),
1227 						   M_CAMXPT, M_NOWAIT);
1228 			if (path->device->serial_num != NULL) {
1229 				bcopy(serial_buf->serial_num,
1230 				      path->device->serial_num,
1231 				      serial_buf->length);
1232 				path->device->serial_num_len =
1233 				    serial_buf->length;
1234 				path->device->serial_num[serial_buf->length]
1235 				    = '\0';
1236 			}
1237 		} else if (cam_periph_error(done_ccb, 0,
1238 					    SF_RETRY_UA|SF_NO_PRINT,
1239 					    &softc->saved_ccb) == ERESTART) {
1240 			return;
1241 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1242 			/* Don't wedge the queue */
1243 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1244 					 /*run_queue*/TRUE);
1245 		}
1246 
1247 		/*
1248 		 * Let's see if we have seen this device before.
1249 		 */
1250 		if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
1251 			MD5_CTX context;
1252 			u_int8_t digest[16];
1253 
1254 			MD5Init(&context);
1255 
1256 			MD5Update(&context,
1257 				  (unsigned char *)&path->device->inq_data,
1258 				  sizeof(struct scsi_inquiry_data));
1259 
1260 			if (have_serialnum)
1261 				MD5Update(&context, serial_buf->serial_num,
1262 					  serial_buf->length);
1263 
1264 			MD5Final(digest, &context);
1265 			if (bcmp(softc->digest, digest, 16) == 0)
1266 				changed = 0;
1267 
1268 			/*
1269 			 * XXX Do we need to do a TUR in order to ensure
1270 			 *     that the device really hasn't changed???
1271 			 */
1272 			if ((changed != 0)
1273 			 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
1274 				xpt_async(AC_LOST_DEVICE, path, NULL);
1275 		}
1276 		if (serial_buf != NULL)
1277 			free(serial_buf, M_CAMXPT);
1278 
1279 		if (changed != 0) {
1280 			/*
1281 			 * Now that we have all the necessary
1282 			 * information to safely perform transfer
1283 			 * negotiations... Controllers don't perform
1284 			 * any negotiation or tagged queuing until
1285 			 * after the first XPT_SET_TRAN_SETTINGS ccb is
1286 			 * received.  So, on a new device, just retrieve
1287 			 * the user settings, and set them as the current
1288 			 * settings to set the device up.
1289 			 */
1290 			proberequestdefaultnegotiation(periph);
1291 			xpt_release_ccb(done_ccb);
1292 
1293 			/*
1294 			 * Perform a TUR to allow the controller to
1295 			 * perform any necessary transfer negotiation.
1296 			 */
1297 			PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1298 			xpt_schedule(periph, priority);
1299 			return;
1300 		}
1301 		xpt_release_ccb(done_ccb);
1302 		break;
1303 	}
1304 	case PROBE_TUR_FOR_NEGOTIATION:
1305 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1306 			DELAY(500000);
1307 			if (cam_periph_error(done_ccb, 0, SF_RETRY_UA,
1308 			    NULL) == ERESTART)
1309 				return;
1310 		}
1311 	/* FALLTHROUGH */
1312 	case PROBE_DV_EXIT:
1313 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1314 			/* Don't wedge the queue */
1315 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1316 					 /*run_queue*/TRUE);
1317 		}
1318 		/*
1319 		 * Do Domain Validation for lun 0 on devices that claim
1320 		 * to support Synchronous Transfer modes.
1321 		 */
1322 	 	if (softc->action == PROBE_TUR_FOR_NEGOTIATION
1323 		 && done_ccb->ccb_h.target_lun == 0
1324 		 && (path->device->inq_data.flags & SID_Sync) != 0
1325                  && (path->device->flags & CAM_DEV_IN_DV) == 0) {
1326 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1327 			    ("Begin Domain Validation\n"));
1328 			path->device->flags |= CAM_DEV_IN_DV;
1329 			xpt_release_ccb(done_ccb);
1330 			PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV1);
1331 			xpt_schedule(periph, priority);
1332 			return;
1333 		}
1334 		if (softc->action == PROBE_DV_EXIT) {
1335 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1336 			    ("Leave Domain Validation\n"));
1337 		}
1338 		path->device->flags &=
1339 		    ~(CAM_DEV_UNCONFIGURED|CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1340 		if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1341 			/* Inform the XPT that a new device has been found */
1342 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1343 			xpt_action(done_ccb);
1344 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1345 				  done_ccb);
1346 		}
1347 		xpt_release_ccb(done_ccb);
1348 		break;
1349 	case PROBE_INQUIRY_BASIC_DV1:
1350 	case PROBE_INQUIRY_BASIC_DV2:
1351 	{
1352 		struct scsi_inquiry_data *nbuf;
1353 		struct ccb_scsiio *csio;
1354 
1355 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1356 			/* Don't wedge the queue */
1357 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1358 					 /*run_queue*/TRUE);
1359 		}
1360 		csio = &done_ccb->csio;
1361 		nbuf = (struct scsi_inquiry_data *)csio->data_ptr;
1362 		if (bcmp(nbuf, &path->device->inq_data, SHORT_INQUIRY_LENGTH)) {
1363 			xpt_print(path,
1364 			    "inquiry data fails comparison at DV%d step\n",
1365 			    softc->action == PROBE_INQUIRY_BASIC_DV1 ? 1 : 2);
1366 			if (proberequestbackoff(periph, path->device)) {
1367 				path->device->flags &= ~CAM_DEV_IN_DV;
1368 				PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1369 			} else {
1370 				/* give up */
1371 				PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
1372 			}
1373 			free(nbuf, M_CAMXPT);
1374 			xpt_release_ccb(done_ccb);
1375 			xpt_schedule(periph, priority);
1376 			return;
1377 		}
1378 		free(nbuf, M_CAMXPT);
1379 		if (softc->action == PROBE_INQUIRY_BASIC_DV1) {
1380 			PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV2);
1381 			xpt_release_ccb(done_ccb);
1382 			xpt_schedule(periph, priority);
1383 			return;
1384 		}
1385 		if (softc->action == PROBE_INQUIRY_BASIC_DV2) {
1386 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1387 			    ("Leave Domain Validation Successfully\n"));
1388 		}
1389 		path->device->flags &=
1390 		    ~(CAM_DEV_UNCONFIGURED|CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1391 		if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1392 			/* Inform the XPT that a new device has been found */
1393 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1394 			xpt_action(done_ccb);
1395 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1396 				  done_ccb);
1397 		}
1398 		xpt_release_ccb(done_ccb);
1399 		break;
1400 	}
1401 	case PROBE_INVALID:
1402 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
1403 		    ("probedone: invalid action state\n"));
1404 	default:
1405 		break;
1406 	}
1407 	done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
1408 	TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
1409 	done_ccb->ccb_h.status = CAM_REQ_CMP;
1410 	xpt_done(done_ccb);
1411 	if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
1412 		cam_periph_invalidate(periph);
1413 		cam_periph_release_locked(periph);
1414 	} else {
1415 		probeschedule(periph);
1416 	}
1417 }
1418 
1419 static void
1420 probecleanup(struct cam_periph *periph)
1421 {
1422 	free(periph->softc, M_CAMXPT);
1423 }
1424 
1425 static void
1426 scsi_find_quirk(struct cam_ed *device)
1427 {
1428 	struct scsi_quirk_entry *quirk;
1429 	caddr_t	match;
1430 
1431 	match = cam_quirkmatch((caddr_t)&device->inq_data,
1432 			       (caddr_t)scsi_quirk_table,
1433 			       sizeof(scsi_quirk_table) /
1434 			       sizeof(*scsi_quirk_table),
1435 			       sizeof(*scsi_quirk_table), scsi_inquiry_match);
1436 
1437 	if (match == NULL)
1438 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
1439 
1440 	quirk = (struct scsi_quirk_entry *)match;
1441 	device->quirk = quirk;
1442 	device->mintags = quirk->mintags;
1443 	device->maxtags = quirk->maxtags;
1444 }
1445 
1446 static int
1447 sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS)
1448 {
1449 	int error, bool;
1450 
1451 	bool = cam_srch_hi;
1452 	error = sysctl_handle_int(oidp, &bool, 0, req);
1453 	if (error != 0 || req->newptr == NULL)
1454 		return (error);
1455 	if (bool == 0 || bool == 1) {
1456 		cam_srch_hi = bool;
1457 		return (0);
1458 	} else {
1459 		return (EINVAL);
1460 	}
1461 }
1462 
1463 typedef struct {
1464 	union	ccb *request_ccb;
1465 	struct 	ccb_pathinq *cpi;
1466 	int	counter;
1467 } scsi_scan_bus_info;
1468 
1469 /*
1470  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1471  * As the scan progresses, scsi_scan_bus is used as the
1472  * callback on completion function.
1473  */
1474 static void
1475 scsi_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1476 {
1477 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1478 		  ("scsi_scan_bus\n"));
1479 	switch (request_ccb->ccb_h.func_code) {
1480 	case XPT_SCAN_BUS:
1481 	{
1482 		scsi_scan_bus_info *scan_info;
1483 		union	ccb *work_ccb;
1484 		struct	cam_path *path;
1485 		u_int	i;
1486 		u_int	max_target;
1487 		u_int	initiator_id;
1488 
1489 		/* Find out the characteristics of the bus */
1490 		work_ccb = xpt_alloc_ccb_nowait();
1491 		if (work_ccb == NULL) {
1492 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1493 			xpt_done(request_ccb);
1494 			return;
1495 		}
1496 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1497 			      request_ccb->ccb_h.pinfo.priority);
1498 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1499 		xpt_action(work_ccb);
1500 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1501 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1502 			xpt_free_ccb(work_ccb);
1503 			xpt_done(request_ccb);
1504 			return;
1505 		}
1506 
1507 		if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
1508 			/*
1509 			 * Can't scan the bus on an adapter that
1510 			 * cannot perform the initiator role.
1511 			 */
1512 			request_ccb->ccb_h.status = CAM_REQ_CMP;
1513 			xpt_free_ccb(work_ccb);
1514 			xpt_done(request_ccb);
1515 			return;
1516 		}
1517 
1518 		/* Save some state for use while we probe for devices */
1519 		scan_info = (scsi_scan_bus_info *)
1520 		    malloc(sizeof(scsi_scan_bus_info), M_CAMXPT, M_NOWAIT);
1521 		if (scan_info == NULL) {
1522 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1523 			xpt_done(request_ccb);
1524 			return;
1525 		}
1526 		scan_info->request_ccb = request_ccb;
1527 		scan_info->cpi = &work_ccb->cpi;
1528 
1529 		/* Cache on our stack so we can work asynchronously */
1530 		max_target = scan_info->cpi->max_target;
1531 		initiator_id = scan_info->cpi->initiator_id;
1532 
1533 
1534 		/*
1535 		 * We can scan all targets in parallel, or do it sequentially.
1536 		 */
1537 		if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
1538 			max_target = 0;
1539 			scan_info->counter = 0;
1540 		} else {
1541 			scan_info->counter = scan_info->cpi->max_target + 1;
1542 			if (scan_info->cpi->initiator_id < scan_info->counter) {
1543 				scan_info->counter--;
1544 			}
1545 		}
1546 
1547 		for (i = 0; i <= max_target; i++) {
1548 			cam_status status;
1549 			if (i == initiator_id)
1550 				continue;
1551 
1552 			status = xpt_create_path(&path, xpt_periph,
1553 						 request_ccb->ccb_h.path_id,
1554 						 i, 0);
1555 			if (status != CAM_REQ_CMP) {
1556 				printf("scsi_scan_bus: xpt_create_path failed"
1557 				       " with status %#x, bus scan halted\n",
1558 				       status);
1559 				free(scan_info, M_CAMXPT);
1560 				request_ccb->ccb_h.status = status;
1561 				xpt_free_ccb(work_ccb);
1562 				xpt_done(request_ccb);
1563 				break;
1564 			}
1565 			work_ccb = xpt_alloc_ccb_nowait();
1566 			if (work_ccb == NULL) {
1567 				xpt_free_ccb((union ccb *)scan_info->cpi);
1568 				free(scan_info, M_CAMXPT);
1569 				xpt_free_path(path);
1570 				request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1571 				xpt_done(request_ccb);
1572 				break;
1573 			}
1574 			xpt_setup_ccb(&work_ccb->ccb_h, path,
1575 				      request_ccb->ccb_h.pinfo.priority);
1576 			work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1577 			work_ccb->ccb_h.cbfcnp = scsi_scan_bus;
1578 			work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1579 			work_ccb->crcn.flags = request_ccb->crcn.flags;
1580 			xpt_action(work_ccb);
1581 		}
1582 		break;
1583 	}
1584 	case XPT_SCAN_LUN:
1585 	{
1586 		cam_status status;
1587 		struct cam_path *path;
1588 		scsi_scan_bus_info *scan_info;
1589 		path_id_t path_id;
1590 		target_id_t target_id;
1591 		lun_id_t lun_id;
1592 
1593 		/* Reuse the same CCB to query if a device was really found */
1594 		scan_info = (scsi_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
1595 		xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
1596 			      request_ccb->ccb_h.pinfo.priority);
1597 		request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1598 
1599 		path_id = request_ccb->ccb_h.path_id;
1600 		target_id = request_ccb->ccb_h.target_id;
1601 		lun_id = request_ccb->ccb_h.target_lun;
1602 		xpt_action(request_ccb);
1603 
1604 		if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
1605 			struct cam_ed *device;
1606 			struct cam_et *target;
1607 			int phl;
1608 
1609 			/*
1610 			 * If we already probed lun 0 successfully, or
1611 			 * we have additional configured luns on this
1612 			 * target that might have "gone away", go onto
1613 			 * the next lun.
1614 			 */
1615 			target = request_ccb->ccb_h.path->target;
1616 			/*
1617 			 * We may touch devices that we don't
1618 			 * hold references too, so ensure they
1619 			 * don't disappear out from under us.
1620 			 * The target above is referenced by the
1621 			 * path in the request ccb.
1622 			 */
1623 			phl = 0;
1624 			device = TAILQ_FIRST(&target->ed_entries);
1625 			if (device != NULL) {
1626 				phl = CAN_SRCH_HI_SPARSE(device);
1627 				if (device->lun_id == 0)
1628 					device = TAILQ_NEXT(device, links);
1629 			}
1630 			if ((lun_id != 0) || (device != NULL)) {
1631 				if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
1632 					lun_id++;
1633 			}
1634 		} else {
1635 			struct cam_ed *device;
1636 
1637 			device = request_ccb->ccb_h.path->device;
1638 
1639 			if ((SCSI_QUIRK(device)->quirks &
1640 			    CAM_QUIRK_NOLUNS) == 0) {
1641 				/* Try the next lun */
1642 				if (lun_id < (CAM_SCSI2_MAXLUN-1)
1643 				  || CAN_SRCH_HI_DENSE(device))
1644 					lun_id++;
1645 			}
1646 		}
1647 
1648 		/*
1649 		 * Free the current request path- we're done with it.
1650 		 */
1651 		xpt_free_path(request_ccb->ccb_h.path);
1652 
1653 		/*
1654 		 * Check to see if we scan any further luns.
1655 		 */
1656 		if (lun_id == request_ccb->ccb_h.target_lun
1657                  || lun_id > scan_info->cpi->max_lun) {
1658 			int done;
1659 
1660  hop_again:
1661 			done = 0;
1662 			if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
1663 				scan_info->counter++;
1664 				if (scan_info->counter ==
1665 				    scan_info->cpi->initiator_id) {
1666 					scan_info->counter++;
1667 				}
1668 				if (scan_info->counter >=
1669 				    scan_info->cpi->max_target+1) {
1670 					done = 1;
1671 				}
1672 			} else {
1673 				scan_info->counter--;
1674 				if (scan_info->counter == 0) {
1675 					done = 1;
1676 				}
1677 			}
1678 			if (done) {
1679 				xpt_free_ccb(request_ccb);
1680 				xpt_free_ccb((union ccb *)scan_info->cpi);
1681 				request_ccb = scan_info->request_ccb;
1682 				free(scan_info, M_CAMXPT);
1683 				request_ccb->ccb_h.status = CAM_REQ_CMP;
1684 				xpt_done(request_ccb);
1685 				break;
1686 			}
1687 
1688 			if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
1689 				xpt_free_ccb(request_ccb);
1690 				break;
1691 			}
1692 			status = xpt_create_path(&path, xpt_periph,
1693 			    scan_info->request_ccb->ccb_h.path_id,
1694 			    scan_info->counter, 0);
1695 			if (status != CAM_REQ_CMP) {
1696 				printf("scsi_scan_bus: xpt_create_path failed"
1697 				    " with status %#x, bus scan halted\n",
1698 			       	    status);
1699 				xpt_free_ccb(request_ccb);
1700 				xpt_free_ccb((union ccb *)scan_info->cpi);
1701 				request_ccb = scan_info->request_ccb;
1702 				free(scan_info, M_CAMXPT);
1703 				request_ccb->ccb_h.status = status;
1704 				xpt_done(request_ccb);
1705 				break;
1706 			}
1707 			xpt_setup_ccb(&request_ccb->ccb_h, path,
1708 			    request_ccb->ccb_h.pinfo.priority);
1709 			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1710 			request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
1711 			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
1712 			request_ccb->crcn.flags =
1713 			    scan_info->request_ccb->crcn.flags;
1714 		} else {
1715 			status = xpt_create_path(&path, xpt_periph,
1716 						 path_id, target_id, lun_id);
1717 			if (status != CAM_REQ_CMP) {
1718 				printf("scsi_scan_bus: xpt_create_path failed "
1719 				       "with status %#x, halting LUN scan\n",
1720 			 	       status);
1721 				goto hop_again;
1722 			}
1723 			xpt_setup_ccb(&request_ccb->ccb_h, path,
1724 				      request_ccb->ccb_h.pinfo.priority);
1725 			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1726 			request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
1727 			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
1728 			request_ccb->crcn.flags =
1729 				scan_info->request_ccb->crcn.flags;
1730 		}
1731 		xpt_action(request_ccb);
1732 		break;
1733 	}
1734 	default:
1735 		break;
1736 	}
1737 }
1738 
1739 static void
1740 scsi_scan_lun(struct cam_periph *periph, struct cam_path *path,
1741 	     cam_flags flags, union ccb *request_ccb)
1742 {
1743 	struct ccb_pathinq cpi;
1744 	cam_status status;
1745 	struct cam_path *new_path;
1746 	struct cam_periph *old_periph;
1747 
1748 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1749 		  ("scsi_scan_lun\n"));
1750 
1751 	xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1752 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1753 	xpt_action((union ccb *)&cpi);
1754 
1755 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
1756 		if (request_ccb != NULL) {
1757 			request_ccb->ccb_h.status = cpi.ccb_h.status;
1758 			xpt_done(request_ccb);
1759 		}
1760 		return;
1761 	}
1762 
1763 	if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
1764 		/*
1765 		 * Can't scan the bus on an adapter that
1766 		 * cannot perform the initiator role.
1767 		 */
1768 		if (request_ccb != NULL) {
1769 			request_ccb->ccb_h.status = CAM_REQ_CMP;
1770 			xpt_done(request_ccb);
1771 		}
1772 		return;
1773 	}
1774 
1775 	if (request_ccb == NULL) {
1776 		request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
1777 		if (request_ccb == NULL) {
1778 			xpt_print(path, "scsi_scan_lun: can't allocate CCB, "
1779 			    "can't continue\n");
1780 			return;
1781 		}
1782 		new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
1783 		if (new_path == NULL) {
1784 			xpt_print(path, "scsi_scan_lun: can't allocate path, "
1785 			    "can't continue\n");
1786 			free(request_ccb, M_CAMXPT);
1787 			return;
1788 		}
1789 		status = xpt_compile_path(new_path, xpt_periph,
1790 					  path->bus->path_id,
1791 					  path->target->target_id,
1792 					  path->device->lun_id);
1793 
1794 		if (status != CAM_REQ_CMP) {
1795 			xpt_print(path, "scsi_scan_lun: can't compile path, "
1796 			    "can't continue\n");
1797 			free(request_ccb, M_CAMXPT);
1798 			free(new_path, M_CAMXPT);
1799 			return;
1800 		}
1801 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
1802 		request_ccb->ccb_h.cbfcnp = xptscandone;
1803 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1804 		request_ccb->crcn.flags = flags;
1805 	}
1806 
1807 	if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
1808 		probe_softc *softc;
1809 
1810 		softc = (probe_softc *)old_periph->softc;
1811 		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
1812 				  periph_links.tqe);
1813 	} else {
1814 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1815 					  probestart, "probe",
1816 					  CAM_PERIPH_BIO,
1817 					  request_ccb->ccb_h.path, NULL, 0,
1818 					  request_ccb);
1819 
1820 		if (status != CAM_REQ_CMP) {
1821 			xpt_print(path, "scsi_scan_lun: cam_alloc_periph "
1822 			    "returned an error, can't continue probe\n");
1823 			request_ccb->ccb_h.status = status;
1824 			xpt_done(request_ccb);
1825 		}
1826 	}
1827 }
1828 
1829 static void
1830 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
1831 {
1832 	xpt_release_path(done_ccb->ccb_h.path);
1833 	free(done_ccb->ccb_h.path, M_CAMXPT);
1834 	free(done_ccb, M_CAMXPT);
1835 }
1836 
1837 static struct cam_ed *
1838 scsi_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
1839 {
1840 	struct cam_path path;
1841 	struct scsi_quirk_entry *quirk;
1842 	struct cam_ed *device;
1843 	struct cam_ed *cur_device;
1844 
1845 	device = xpt_alloc_device(bus, target, lun_id);
1846 	if (device == NULL)
1847 		return (NULL);
1848 
1849 	/*
1850 	 * Take the default quirk entry until we have inquiry
1851 	 * data and can determine a better quirk to use.
1852 	 */
1853 	quirk = &scsi_quirk_table[scsi_quirk_table_size - 1];
1854 	device->quirk = (void *)quirk;
1855 	device->mintags = quirk->mintags;
1856 	device->maxtags = quirk->maxtags;
1857 	bzero(&device->inq_data, sizeof(device->inq_data));
1858 	device->inq_flags = 0;
1859 	device->queue_flags = 0;
1860 	device->serial_num = NULL;
1861 	device->serial_num_len = 0;
1862 
1863 	/*
1864 	 * XXX should be limited by number of CCBs this bus can
1865 	 * do.
1866 	 */
1867 	bus->sim->max_ccbs += device->ccbq.devq_openings;
1868 	/* Insertion sort into our target's device list */
1869 	cur_device = TAILQ_FIRST(&target->ed_entries);
1870 	while (cur_device != NULL && cur_device->lun_id < lun_id)
1871 		cur_device = TAILQ_NEXT(cur_device, links);
1872 	if (cur_device != NULL) {
1873 		TAILQ_INSERT_BEFORE(cur_device, device, links);
1874 	} else {
1875 		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
1876 	}
1877 	target->generation++;
1878 	if (lun_id != CAM_LUN_WILDCARD) {
1879 		xpt_compile_path(&path,
1880 				 NULL,
1881 				 bus->path_id,
1882 				 target->target_id,
1883 				 lun_id);
1884 		scsi_devise_transport(&path);
1885 		xpt_release_path(&path);
1886 	}
1887 
1888 	return (device);
1889 }
1890 
1891 static void
1892 scsi_devise_transport(struct cam_path *path)
1893 {
1894 	struct ccb_pathinq cpi;
1895 	struct ccb_trans_settings cts;
1896 	struct scsi_inquiry_data *inq_buf;
1897 
1898 	/* Get transport information from the SIM */
1899 	xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1900 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1901 	xpt_action((union ccb *)&cpi);
1902 
1903 	inq_buf = NULL;
1904 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1905 		inq_buf = &path->device->inq_data;
1906 	path->device->protocol = PROTO_SCSI;
1907 	path->device->protocol_version =
1908 	    inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1909 	path->device->transport = cpi.transport;
1910 	path->device->transport_version = cpi.transport_version;
1911 
1912 	/*
1913 	 * Any device not using SPI3 features should
1914 	 * be considered SPI2 or lower.
1915 	 */
1916 	if (inq_buf != NULL) {
1917 		if (path->device->transport == XPORT_SPI
1918 		 && (inq_buf->spi3data & SID_SPI_MASK) == 0
1919 		 && path->device->transport_version > 2)
1920 			path->device->transport_version = 2;
1921 	} else {
1922 		struct cam_ed* otherdev;
1923 
1924 		for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
1925 		     otherdev != NULL;
1926 		     otherdev = TAILQ_NEXT(otherdev, links)) {
1927 			if (otherdev != path->device)
1928 				break;
1929 		}
1930 
1931 		if (otherdev != NULL) {
1932 			/*
1933 			 * Initially assume the same versioning as
1934 			 * prior luns for this target.
1935 			 */
1936 			path->device->protocol_version =
1937 			    otherdev->protocol_version;
1938 			path->device->transport_version =
1939 			    otherdev->transport_version;
1940 		} else {
1941 			/* Until we know better, opt for safty */
1942 			path->device->protocol_version = 2;
1943 			if (path->device->transport == XPORT_SPI)
1944 				path->device->transport_version = 2;
1945 			else
1946 				path->device->transport_version = 0;
1947 		}
1948 	}
1949 
1950 	/*
1951 	 * XXX
1952 	 * For a device compliant with SPC-2 we should be able
1953 	 * to determine the transport version supported by
1954 	 * scrutinizing the version descriptors in the
1955 	 * inquiry buffer.
1956 	 */
1957 
1958 	/* Tell the controller what we think */
1959 	xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1960 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1961 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1962 	cts.transport = path->device->transport;
1963 	cts.transport_version = path->device->transport_version;
1964 	cts.protocol = path->device->protocol;
1965 	cts.protocol_version = path->device->protocol_version;
1966 	cts.proto_specific.valid = 0;
1967 	cts.xport_specific.valid = 0;
1968 	xpt_action((union ccb *)&cts);
1969 }
1970 
1971 static void
1972 scsi_action(union ccb *start_ccb)
1973 {
1974 
1975 	switch (start_ccb->ccb_h.func_code) {
1976 	case XPT_SET_TRAN_SETTINGS:
1977 	{
1978 		scsi_set_transfer_settings(&start_ccb->cts,
1979 					   start_ccb->ccb_h.path->device,
1980 					   /*async_update*/FALSE);
1981 		break;
1982 	}
1983 	case XPT_SCAN_BUS:
1984 		scsi_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
1985 		break;
1986 	case XPT_SCAN_LUN:
1987 		scsi_scan_lun(start_ccb->ccb_h.path->periph,
1988 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
1989 			      start_ccb);
1990 		break;
1991 	case XPT_GET_TRAN_SETTINGS:
1992 	{
1993 		struct cam_sim *sim;
1994 
1995 		sim = start_ccb->ccb_h.path->bus->sim;
1996 		(*(sim->sim_action))(sim, start_ccb);
1997 		break;
1998 	}
1999 	default:
2000 		xpt_action_default(start_ccb);
2001 		break;
2002 	}
2003 }
2004 
2005 static void
2006 scsi_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
2007 			   int async_update)
2008 {
2009 	struct	ccb_pathinq cpi;
2010 	struct	ccb_trans_settings cur_cts;
2011 	struct	ccb_trans_settings_scsi *scsi;
2012 	struct	ccb_trans_settings_scsi *cur_scsi;
2013 	struct	cam_sim *sim;
2014 	struct	scsi_inquiry_data *inq_data;
2015 
2016 	if (device == NULL) {
2017 		cts->ccb_h.status = CAM_PATH_INVALID;
2018 		xpt_done((union ccb *)cts);
2019 		return;
2020 	}
2021 
2022 	if (cts->protocol == PROTO_UNKNOWN
2023 	 || cts->protocol == PROTO_UNSPECIFIED) {
2024 		cts->protocol = device->protocol;
2025 		cts->protocol_version = device->protocol_version;
2026 	}
2027 
2028 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
2029 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
2030 		cts->protocol_version = device->protocol_version;
2031 
2032 	if (cts->protocol != device->protocol) {
2033 		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
2034 		       cts->protocol, device->protocol);
2035 		cts->protocol = device->protocol;
2036 	}
2037 
2038 	if (cts->protocol_version > device->protocol_version) {
2039 		if (bootverbose) {
2040 			xpt_print(cts->ccb_h.path, "Down reving Protocol "
2041 			    "Version from %d to %d?\n", cts->protocol_version,
2042 			    device->protocol_version);
2043 		}
2044 		cts->protocol_version = device->protocol_version;
2045 	}
2046 
2047 	if (cts->transport == XPORT_UNKNOWN
2048 	 || cts->transport == XPORT_UNSPECIFIED) {
2049 		cts->transport = device->transport;
2050 		cts->transport_version = device->transport_version;
2051 	}
2052 
2053 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
2054 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
2055 		cts->transport_version = device->transport_version;
2056 
2057 	if (cts->transport != device->transport) {
2058 		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
2059 		    cts->transport, device->transport);
2060 		cts->transport = device->transport;
2061 	}
2062 
2063 	if (cts->transport_version > device->transport_version) {
2064 		if (bootverbose) {
2065 			xpt_print(cts->ccb_h.path, "Down reving Transport "
2066 			    "Version from %d to %d?\n", cts->transport_version,
2067 			    device->transport_version);
2068 		}
2069 		cts->transport_version = device->transport_version;
2070 	}
2071 
2072 	sim = cts->ccb_h.path->bus->sim;
2073 
2074 	/*
2075 	 * Nothing more of interest to do unless
2076 	 * this is a device connected via the
2077 	 * SCSI protocol.
2078 	 */
2079 	if (cts->protocol != PROTO_SCSI) {
2080 		if (async_update == FALSE)
2081 			(*(sim->sim_action))(sim, (union ccb *)cts);
2082 		return;
2083 	}
2084 
2085 	inq_data = &device->inq_data;
2086 	scsi = &cts->proto_specific.scsi;
2087 	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
2088 	cpi.ccb_h.func_code = XPT_PATH_INQ;
2089 	xpt_action((union ccb *)&cpi);
2090 
2091 	/* SCSI specific sanity checking */
2092 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
2093 	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
2094 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
2095 	 || (device->mintags == 0)) {
2096 		/*
2097 		 * Can't tag on hardware that doesn't support tags,
2098 		 * doesn't have it enabled, or has broken tag support.
2099 		 */
2100 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2101 	}
2102 
2103 	if (async_update == FALSE) {
2104 		/*
2105 		 * Perform sanity checking against what the
2106 		 * controller and device can do.
2107 		 */
2108 		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
2109 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2110 		cur_cts.type = cts->type;
2111 		xpt_action((union ccb *)&cur_cts);
2112 		if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2113 			return;
2114 		}
2115 		cur_scsi = &cur_cts.proto_specific.scsi;
2116 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
2117 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2118 			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
2119 		}
2120 		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
2121 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2122 	}
2123 
2124 	/* SPI specific sanity checking */
2125 	if (cts->transport == XPORT_SPI && async_update == FALSE) {
2126 		u_int spi3caps;
2127 		struct ccb_trans_settings_spi *spi;
2128 		struct ccb_trans_settings_spi *cur_spi;
2129 
2130 		spi = &cts->xport_specific.spi;
2131 
2132 		cur_spi = &cur_cts.xport_specific.spi;
2133 
2134 		/* Fill in any gaps in what the user gave us */
2135 		if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2136 			spi->sync_period = cur_spi->sync_period;
2137 		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2138 			spi->sync_period = 0;
2139 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2140 			spi->sync_offset = cur_spi->sync_offset;
2141 		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2142 			spi->sync_offset = 0;
2143 		if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2144 			spi->ppr_options = cur_spi->ppr_options;
2145 		if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2146 			spi->ppr_options = 0;
2147 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2148 			spi->bus_width = cur_spi->bus_width;
2149 		if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2150 			spi->bus_width = 0;
2151 		if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
2152 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2153 			spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
2154 		}
2155 		if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
2156 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2157 		if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2158 		  && (inq_data->flags & SID_Sync) == 0
2159 		  && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2160 		 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)) {
2161 			/* Force async */
2162 			spi->sync_period = 0;
2163 			spi->sync_offset = 0;
2164 		}
2165 
2166 		switch (spi->bus_width) {
2167 		case MSG_EXT_WDTR_BUS_32_BIT:
2168 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2169 			  || (inq_data->flags & SID_WBus32) != 0
2170 			  || cts->type == CTS_TYPE_USER_SETTINGS)
2171 			 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
2172 				break;
2173 			/* Fall Through to 16-bit */
2174 		case MSG_EXT_WDTR_BUS_16_BIT:
2175 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2176 			  || (inq_data->flags & SID_WBus16) != 0
2177 			  || cts->type == CTS_TYPE_USER_SETTINGS)
2178 			 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
2179 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2180 				break;
2181 			}
2182 			/* Fall Through to 8-bit */
2183 		default: /* New bus width?? */
2184 		case MSG_EXT_WDTR_BUS_8_BIT:
2185 			/* All targets can do this */
2186 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2187 			break;
2188 		}
2189 
2190 		spi3caps = cpi.xport_specific.spi.ppr_options;
2191 		if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2192 		 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2193 			spi3caps &= inq_data->spi3data;
2194 
2195 		if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
2196 			spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2197 
2198 		if ((spi3caps & SID_SPI_IUS) == 0)
2199 			spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
2200 
2201 		if ((spi3caps & SID_SPI_QAS) == 0)
2202 			spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
2203 
2204 		/* No SPI Transfer settings are allowed unless we are wide */
2205 		if (spi->bus_width == 0)
2206 			spi->ppr_options = 0;
2207 
2208 		if ((spi->valid & CTS_SPI_VALID_DISC)
2209 		 && ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0)) {
2210 			/*
2211 			 * Can't tag queue without disconnection.
2212 			 */
2213 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2214 			scsi->valid |= CTS_SCSI_VALID_TQ;
2215 		}
2216 
2217 		/*
2218 		 * If we are currently performing tagged transactions to
2219 		 * this device and want to change its negotiation parameters,
2220 		 * go non-tagged for a bit to give the controller a chance to
2221 		 * negotiate unhampered by tag messages.
2222 		 */
2223 		if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2224 		 && (device->inq_flags & SID_CmdQue) != 0
2225 		 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2226 		 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
2227 				   CTS_SPI_VALID_SYNC_OFFSET|
2228 				   CTS_SPI_VALID_BUS_WIDTH)) != 0)
2229 			scsi_toggle_tags(cts->ccb_h.path);
2230 	}
2231 
2232 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2233 	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
2234 		int device_tagenb;
2235 
2236 		/*
2237 		 * If we are transitioning from tags to no-tags or
2238 		 * vice-versa, we need to carefully freeze and restart
2239 		 * the queue so that we don't overlap tagged and non-tagged
2240 		 * commands.  We also temporarily stop tags if there is
2241 		 * a change in transfer negotiation settings to allow
2242 		 * "tag-less" negotiation.
2243 		 */
2244 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2245 		 || (device->inq_flags & SID_CmdQue) != 0)
2246 			device_tagenb = TRUE;
2247 		else
2248 			device_tagenb = FALSE;
2249 
2250 		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2251 		  && device_tagenb == FALSE)
2252 		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
2253 		  && device_tagenb == TRUE)) {
2254 
2255 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
2256 				/*
2257 				 * Delay change to use tags until after a
2258 				 * few commands have gone to this device so
2259 				 * the controller has time to perform transfer
2260 				 * negotiations without tagged messages getting
2261 				 * in the way.
2262 				 */
2263 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
2264 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
2265 			} else {
2266 				struct ccb_relsim crs;
2267 
2268 				xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
2269 		  		device->inq_flags &= ~SID_CmdQue;
2270 				xpt_dev_ccbq_resize(cts->ccb_h.path,
2271 						    sim->max_dev_openings);
2272 				device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
2273 				device->tag_delay_count = 0;
2274 
2275 				xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
2276 					      /*priority*/1);
2277 				crs.ccb_h.func_code = XPT_REL_SIMQ;
2278 				crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
2279 				crs.openings
2280 				    = crs.release_timeout
2281 				    = crs.qfrozen_cnt
2282 				    = 0;
2283 				xpt_action((union ccb *)&crs);
2284 			}
2285 		}
2286 	}
2287 	if (async_update == FALSE)
2288 		(*(sim->sim_action))(sim, (union ccb *)cts);
2289 }
2290 
2291 static void
2292 scsi_toggle_tags(struct cam_path *path)
2293 {
2294 	struct cam_ed *dev;
2295 
2296 	/*
2297 	 * Give controllers a chance to renegotiate
2298 	 * before starting tag operations.  We
2299 	 * "toggle" tagged queuing off then on
2300 	 * which causes the tag enable command delay
2301 	 * counter to come into effect.
2302 	 */
2303 	dev = path->device;
2304 	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2305 	 || ((dev->inq_flags & SID_CmdQue) != 0
2306  	  && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
2307 		struct ccb_trans_settings cts;
2308 
2309 		xpt_setup_ccb(&cts.ccb_h, path, 1);
2310 		cts.protocol = PROTO_SCSI;
2311 		cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
2312 		cts.transport = XPORT_UNSPECIFIED;
2313 		cts.transport_version = XPORT_VERSION_UNSPECIFIED;
2314 		cts.proto_specific.scsi.flags = 0;
2315 		cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
2316 		scsi_set_transfer_settings(&cts, path->device,
2317 					  /*async_update*/TRUE);
2318 		cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
2319 		scsi_set_transfer_settings(&cts, path->device,
2320 					  /*async_update*/TRUE);
2321 	}
2322 }
2323 
2324 /*
2325  * Handle any per-device event notifications that require action by the XPT.
2326  */
2327 static void
2328 scsi_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
2329 	      struct cam_ed *device, void *async_arg)
2330 {
2331 	cam_status status;
2332 	struct cam_path newpath;
2333 
2334 	/*
2335 	 * We only need to handle events for real devices.
2336 	 */
2337 	if (target->target_id == CAM_TARGET_WILDCARD
2338 	 || device->lun_id == CAM_LUN_WILDCARD)
2339 		return;
2340 
2341 	/*
2342 	 * We need our own path with wildcards expanded to
2343 	 * handle certain types of events.
2344 	 */
2345 	if ((async_code == AC_SENT_BDR)
2346 	 || (async_code == AC_BUS_RESET)
2347 	 || (async_code == AC_INQ_CHANGED))
2348 		status = xpt_compile_path(&newpath, NULL,
2349 					  bus->path_id,
2350 					  target->target_id,
2351 					  device->lun_id);
2352 	else
2353 		status = CAM_REQ_CMP_ERR;
2354 
2355 	if (status == CAM_REQ_CMP) {
2356 
2357 		/*
2358 		 * Allow transfer negotiation to occur in a
2359 		 * tag free environment.
2360 		 */
2361 		if (async_code == AC_SENT_BDR
2362 		 || async_code == AC_BUS_RESET)
2363 			scsi_toggle_tags(&newpath);
2364 
2365 		if (async_code == AC_INQ_CHANGED) {
2366 			/*
2367 			 * We've sent a start unit command, or
2368 			 * something similar to a device that
2369 			 * may have caused its inquiry data to
2370 			 * change. So we re-scan the device to
2371 			 * refresh the inquiry data for it.
2372 			 */
2373 			scsi_scan_lun(newpath.periph, &newpath,
2374 				     CAM_EXPECT_INQ_CHANGE, NULL);
2375 		}
2376 		xpt_release_path(&newpath);
2377 	} else if (async_code == AC_LOST_DEVICE) {
2378 		device->flags |= CAM_DEV_UNCONFIGURED;
2379 	} else if (async_code == AC_TRANSFER_NEG) {
2380 		struct ccb_trans_settings *settings;
2381 
2382 		settings = (struct ccb_trans_settings *)async_arg;
2383 		scsi_set_transfer_settings(settings, device,
2384 					  /*async_update*/TRUE);
2385 	}
2386 }
2387 
2388