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