xref: /freebsd/sys/cam/scsi/scsi_xpt.c (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
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 #include <cam/cam.h>
51 #include <cam/cam_ccb.h>
52 #include <cam/cam_queue.h>
53 #include <cam/cam_periph.h>
54 #include <cam/cam_sim.h>
55 #include <cam/cam_xpt.h>
56 #include <cam/cam_xpt_sim.h>
57 #include <cam/cam_xpt_periph.h>
58 #include <cam/cam_xpt_internal.h>
59 #include <cam/cam_debug.h>
60 
61 #include <cam/scsi/scsi_all.h>
62 #include <cam/scsi/scsi_message.h>
63 #include <cam/scsi/scsi_pass.h>
64 #include <machine/stdarg.h>	/* for xpt_print below */
65 #include "opt_cam.h"
66 
67 struct scsi_quirk_entry {
68 	struct scsi_inquiry_pattern inq_pat;
69 	u_int8_t quirks;
70 #define	CAM_QUIRK_NOLUNS	0x01
71 #define	CAM_QUIRK_NOVPDS	0x02
72 #define	CAM_QUIRK_HILUNS	0x04
73 #define	CAM_QUIRK_NOHILUNS	0x08
74 #define	CAM_QUIRK_NORPTLUNS	0x10
75 	u_int mintags;
76 	u_int maxtags;
77 };
78 #define SCSI_QUIRK(dev)	((struct scsi_quirk_entry *)((dev)->quirk))
79 
80 static int cam_srch_hi = 0;
81 static int sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS);
82 SYSCTL_PROC(_kern_cam, OID_AUTO, cam_srch_hi, CTLTYPE_INT | CTLFLAG_RWTUN, 0, 0,
83     sysctl_cam_search_luns, "I",
84     "allow search above LUN 7 for SCSI3 and greater devices");
85 
86 #define	CAM_SCSI2_MAXLUN	8
87 #define	CAM_CAN_GET_SIMPLE_LUN(x, i)				\
88 	((((x)->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) ==	\
89 	RPL_LUNDATA_ATYP_PERIPH) ||				\
90 	(((x)->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) ==	\
91 	RPL_LUNDATA_ATYP_FLAT))
92 #define	CAM_GET_SIMPLE_LUN(lp, i, lval)					\
93 	if (((lp)->luns[(i)].lundata[0] & RPL_LUNDATA_ATYP_MASK) == 	\
94 	    RPL_LUNDATA_ATYP_PERIPH) {					\
95 		(lval) = (lp)->luns[(i)].lundata[1];			\
96 	} else {							\
97 		(lval) = (lp)->luns[(i)].lundata[0];			\
98 		(lval) &= RPL_LUNDATA_FLAT_LUN_MASK;			\
99 		(lval) <<= 8;						\
100 		(lval) |=  (lp)->luns[(i)].lundata[1];			\
101 	}
102 #define	CAM_GET_LUN(lp, i, lval)					\
103 	(lval) = scsi_8btou64((lp)->luns[(i)].lundata);			\
104 	(lval) = CAM_EXTLUN_BYTE_SWIZZLE(lval);
105 
106 /*
107  * If we're not quirked to search <= the first 8 luns
108  * and we are either quirked to search above lun 8,
109  * or we're > SCSI-2 and we've enabled hilun searching,
110  * or we're > SCSI-2 and the last lun was a success,
111  * we can look for luns above lun 8.
112  */
113 #define	CAN_SRCH_HI_SPARSE(dv)					\
114   (((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_NOHILUNS) == 0) 	\
115   && ((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_HILUNS)		\
116   || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2 && cam_srch_hi)))
117 
118 #define	CAN_SRCH_HI_DENSE(dv)					\
119   (((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_NOHILUNS) == 0) 	\
120   && ((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_HILUNS)		\
121   || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2)))
122 
123 static periph_init_t probe_periph_init;
124 
125 static struct periph_driver probe_driver =
126 {
127 	probe_periph_init, "probe",
128 	TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
129 	CAM_PERIPH_DRV_EARLY
130 };
131 
132 PERIPHDRIVER_DECLARE(probe, probe_driver);
133 
134 typedef enum {
135 	PROBE_TUR,
136 	PROBE_INQUIRY,	/* this counts as DV0 for Basic Domain Validation */
137 	PROBE_FULL_INQUIRY,
138 	PROBE_REPORT_LUNS,
139 	PROBE_MODE_SENSE,
140 	PROBE_SUPPORTED_VPD_LIST,
141 	PROBE_DEVICE_ID,
142 	PROBE_EXTENDED_INQUIRY,
143 	PROBE_SERIAL_NUM,
144 	PROBE_TUR_FOR_NEGOTIATION,
145 	PROBE_INQUIRY_BASIC_DV1,
146 	PROBE_INQUIRY_BASIC_DV2,
147 	PROBE_DV_EXIT,
148 	PROBE_DONE,
149 	PROBE_INVALID
150 } probe_action;
151 
152 static char *probe_action_text[] = {
153 	"PROBE_TUR",
154 	"PROBE_INQUIRY",
155 	"PROBE_FULL_INQUIRY",
156 	"PROBE_REPORT_LUNS",
157 	"PROBE_MODE_SENSE",
158 	"PROBE_SUPPORTED_VPD_LIST",
159 	"PROBE_DEVICE_ID",
160 	"PROBE_EXTENDED_INQUIRY",
161 	"PROBE_SERIAL_NUM",
162 	"PROBE_TUR_FOR_NEGOTIATION",
163 	"PROBE_INQUIRY_BASIC_DV1",
164 	"PROBE_INQUIRY_BASIC_DV2",
165 	"PROBE_DV_EXIT",
166 	"PROBE_DONE",
167 	"PROBE_INVALID"
168 };
169 
170 #define PROBE_SET_ACTION(softc, newaction)	\
171 do {									\
172 	char **text;							\
173 	text = probe_action_text;					\
174 	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE,		\
175 	    ("Probe %s to %s\n", text[(softc)->action],			\
176 	    text[(newaction)]));					\
177 	(softc)->action = (newaction);					\
178 } while(0)
179 
180 typedef enum {
181 	PROBE_INQUIRY_CKSUM	= 0x01,
182 	PROBE_SERIAL_CKSUM	= 0x02,
183 	PROBE_NO_ANNOUNCE	= 0x04,
184 	PROBE_EXTLUN		= 0x08
185 } probe_flags;
186 
187 typedef struct {
188 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
189 	probe_action	action;
190 	union ccb	saved_ccb;
191 	probe_flags	flags;
192 	MD5_CTX		context;
193 	u_int8_t	digest[16];
194 	struct cam_periph *periph;
195 } probe_softc;
196 
197 static const char quantum[] = "QUANTUM";
198 static const char sony[] = "SONY";
199 static const char west_digital[] = "WDIGTL";
200 static const char samsung[] = "SAMSUNG";
201 static const char seagate[] = "SEAGATE";
202 static const char microp[] = "MICROP";
203 
204 static struct scsi_quirk_entry scsi_quirk_table[] =
205 {
206 	{
207 		/* Reports QUEUE FULL for temporary resource shortages */
208 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
209 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
210 	},
211 	{
212 		/* Reports QUEUE FULL for temporary resource shortages */
213 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
214 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
215 	},
216 	{
217 		/* Reports QUEUE FULL for temporary resource shortages */
218 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
219 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
220 	},
221 	{
222 		/* Broken tagged queuing drive */
223 		{ T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
224 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
225 	},
226 	{
227 		/* Broken tagged queuing drive */
228 		{ T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
229 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
230 	},
231 	{
232 		/* Broken tagged queuing drive */
233 		{ T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
234 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
235 	},
236 	{
237 		/*
238 		 * Unfortunately, the Quantum Atlas III has the same
239 		 * problem as the Atlas II drives above.
240 		 * Reported by: "Johan Granlund" <johan@granlund.nu>
241 		 *
242 		 * For future reference, the drive with the problem was:
243 		 * QUANTUM QM39100TD-SW N1B0
244 		 *
245 		 * It's possible that Quantum will fix the problem in later
246 		 * firmware revisions.  If that happens, the quirk entry
247 		 * will need to be made specific to the firmware revisions
248 		 * with the problem.
249 		 *
250 		 */
251 		/* Reports QUEUE FULL for temporary resource shortages */
252 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
253 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
254 	},
255 	{
256 		/*
257 		 * 18 Gig Atlas III, same problem as the 9G version.
258 		 * Reported by: Andre Albsmeier
259 		 *		<andre.albsmeier@mchp.siemens.de>
260 		 *
261 		 * For future reference, the drive with the problem was:
262 		 * QUANTUM QM318000TD-S N491
263 		 */
264 		/* Reports QUEUE FULL for temporary resource shortages */
265 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
266 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
267 	},
268 	{
269 		/*
270 		 * Broken tagged queuing drive
271 		 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
272 		 *         and: Martin Renters <martin@tdc.on.ca>
273 		 */
274 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
275 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
276 	},
277 		/*
278 		 * The Seagate Medalist Pro drives have very poor write
279 		 * performance with anything more than 2 tags.
280 		 *
281 		 * Reported by:  Paul van der Zwan <paulz@trantor.xs4all.nl>
282 		 * Drive:  <SEAGATE ST36530N 1444>
283 		 *
284 		 * Reported by:  Jeremy Lea <reg@shale.csir.co.za>
285 		 * Drive:  <SEAGATE ST34520W 1281>
286 		 *
287 		 * No one has actually reported that the 9G version
288 		 * (ST39140*) of the Medalist Pro has the same problem, but
289 		 * we're assuming that it does because the 4G and 6.5G
290 		 * versions of the drive are broken.
291 		 */
292 	{
293 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
294 		/*quirks*/0, /*mintags*/2, /*maxtags*/2
295 	},
296 	{
297 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
298 		/*quirks*/0, /*mintags*/2, /*maxtags*/2
299 	},
300 	{
301 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
302 		/*quirks*/0, /*mintags*/2, /*maxtags*/2
303 	},
304 	{
305 		/*
306 		 * Experiences command timeouts under load with a
307 		 * tag count higher than 55.
308 		 */
309 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST3146855LW", "*"},
310 		/*quirks*/0, /*mintags*/2, /*maxtags*/55
311 	},
312 	{
313 		/*
314 		 * Slow when tagged queueing is enabled.  Write performance
315 		 * steadily drops off with more and more concurrent
316 		 * transactions.  Best sequential write performance with
317 		 * tagged queueing turned off and write caching turned on.
318 		 *
319 		 * PR:  kern/10398
320 		 * Submitted by:  Hideaki Okada <hokada@isl.melco.co.jp>
321 		 * Drive:  DCAS-34330 w/ "S65A" firmware.
322 		 *
323 		 * The drive with the problem had the "S65A" firmware
324 		 * revision, and has also been reported (by Stephen J.
325 		 * Roznowski <sjr@home.net>) for a drive with the "S61A"
326 		 * firmware revision.
327 		 *
328 		 * Although no one has reported problems with the 2 gig
329 		 * version of the DCAS drive, the assumption is that it
330 		 * has the same problems as the 4 gig version.  Therefore
331 		 * this quirk entries disables tagged queueing for all
332 		 * DCAS drives.
333 		 */
334 		{ T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
335 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
336 	},
337 	{
338 		/* Broken tagged queuing drive */
339 		{ T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
340 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
341 	},
342 	{
343 		/* Broken tagged queuing drive */
344 		{ T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
345 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
346 	},
347 	{
348 		/* This does not support other than LUN 0 */
349 		{ T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*" },
350 		CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
351 	},
352 	{
353 		/*
354 		 * Broken tagged queuing drive.
355 		 * Submitted by:
356 		 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
357 		 * in PR kern/9535
358 		 */
359 		{ T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
360 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
361 	},
362         {
363 		/*
364 		 * Slow when tagged queueing is enabled. (1.5MB/sec versus
365 		 * 8MB/sec.)
366 		 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
367 		 * Best performance with these drives is achieved with
368 		 * tagged queueing turned off, and write caching turned on.
369 		 */
370 		{ T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
371 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
372         },
373         {
374 		/*
375 		 * Slow when tagged queueing is enabled. (1.5MB/sec versus
376 		 * 8MB/sec.)
377 		 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
378 		 * Best performance with these drives is achieved with
379 		 * tagged queueing turned off, and write caching turned on.
380 		 */
381 		{ T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
382 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
383         },
384 	{
385 		/*
386 		 * Doesn't handle queue full condition correctly,
387 		 * so we need to limit maxtags to what the device
388 		 * can handle instead of determining this automatically.
389 		 */
390 		{ T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
391 		/*quirks*/0, /*mintags*/2, /*maxtags*/32
392 	},
393 	{
394 		/* Really only one LUN */
395 		{ T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
396 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
397 	},
398 	{
399 		/* I can't believe we need a quirk for DPT volumes. */
400 		{ T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
401 		CAM_QUIRK_NOLUNS,
402 		/*mintags*/0, /*maxtags*/255
403 	},
404 	{
405 		/*
406 		 * Many Sony CDROM drives don't like multi-LUN probing.
407 		 */
408 		{ T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
409 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
410 	},
411 	{
412 		/*
413 		 * This drive doesn't like multiple LUN probing.
414 		 * Submitted by:  Parag Patel <parag@cgt.com>
415 		 */
416 		{ T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R   CDU9*", "*" },
417 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
418 	},
419 	{
420 		{ T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
421 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
422 	},
423 	{
424 		/*
425 		 * The 8200 doesn't like multi-lun probing, and probably
426 		 * don't like serial number requests either.
427 		 */
428 		{
429 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
430 			"EXB-8200*", "*"
431 		},
432 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
433 	},
434 	{
435 		/*
436 		 * Let's try the same as above, but for a drive that says
437 		 * it's an IPL-6860 but is actually an EXB 8200.
438 		 */
439 		{
440 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
441 			"IPL-6860*", "*"
442 		},
443 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
444 	},
445 	{
446 		/*
447 		 * These Hitachi drives don't like multi-lun probing.
448 		 * The PR submitter has a DK319H, but says that the Linux
449 		 * kernel has a similar work-around for the DK312 and DK314,
450 		 * so all DK31* drives are quirked here.
451 		 * PR:            misc/18793
452 		 * Submitted by:  Paul Haddad <paul@pth.com>
453 		 */
454 		{ T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
455 		CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
456 	},
457 	{
458 		/*
459 		 * The Hitachi CJ series with J8A8 firmware apparantly has
460 		 * problems with tagged commands.
461 		 * PR: 23536
462 		 * Reported by: amagai@nue.org
463 		 */
464 		{ T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
465 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
466 	},
467 	{
468 		/*
469 		 * These are the large storage arrays.
470 		 * Submitted by:  William Carrel <william.carrel@infospace.com>
471 		 */
472 		{ T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
473 		CAM_QUIRK_HILUNS, 2, 1024
474 	},
475 	{
476 		/*
477 		 * This old revision of the TDC3600 is also SCSI-1, and
478 		 * hangs upon serial number probing.
479 		 */
480 		{
481 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
482 			" TDC 3600", "U07:"
483 		},
484 		CAM_QUIRK_NOVPDS, /*mintags*/0, /*maxtags*/0
485 	},
486 	{
487 		/*
488 		 * Would repond to all LUNs if asked for.
489 		 */
490 		{
491 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
492 			"CP150", "*"
493 		},
494 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
495 	},
496 	{
497 		/*
498 		 * Would repond to all LUNs if asked for.
499 		 */
500 		{
501 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
502 			"96X2*", "*"
503 		},
504 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
505 	},
506 	{
507 		/* Submitted by: Matthew Dodd <winter@jurai.net> */
508 		{ T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
509 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
510 	},
511 	{
512 		/* Submitted by: Matthew Dodd <winter@jurai.net> */
513 		{ T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
514 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
515 	},
516 	{
517 		/* TeraSolutions special settings for TRC-22 RAID */
518 		{ T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
519 		  /*quirks*/0, /*mintags*/55, /*maxtags*/255
520 	},
521 	{
522 		/* Veritas Storage Appliance */
523 		{ T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
524 		  CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
525 	},
526 	{
527 		/*
528 		 * Would respond to all LUNs.  Device type and removable
529 		 * flag are jumper-selectable.
530 		 */
531 		{ T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
532 		  "Tahiti 1", "*"
533 		},
534 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
535 	},
536 	{
537 		/* EasyRAID E5A aka. areca ARC-6010 */
538 		{ T_DIRECT, SIP_MEDIA_FIXED, "easyRAID", "*", "*" },
539 		  CAM_QUIRK_NOHILUNS, /*mintags*/2, /*maxtags*/255
540 	},
541 	{
542 		{ T_ENCLOSURE, SIP_MEDIA_FIXED, "DP", "BACKPLANE", "*" },
543 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
544 	},
545 	{
546 		{ T_DIRECT, SIP_MEDIA_REMOVABLE, "Garmin", "*", "*" },
547 		CAM_QUIRK_NORPTLUNS, /*mintags*/2, /*maxtags*/255
548 	},
549 	{
550 		/* Default tagged queuing parameters for all devices */
551 		{
552 		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
553 		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
554 		},
555 		/*quirks*/0, /*mintags*/2, /*maxtags*/255
556 	},
557 };
558 
559 static const int scsi_quirk_table_size =
560 	sizeof(scsi_quirk_table) / sizeof(*scsi_quirk_table);
561 
562 static cam_status	proberegister(struct cam_periph *periph,
563 				      void *arg);
564 static void	 probeschedule(struct cam_periph *probe_periph);
565 static void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
566 static void	 proberequestdefaultnegotiation(struct cam_periph *periph);
567 static int       proberequestbackoff(struct cam_periph *periph,
568 				     struct cam_ed *device);
569 static void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
570 static void	 probe_purge_old(struct cam_path *path,
571 				 struct scsi_report_luns_data *new,
572 				 probe_flags flags);
573 static void	 probecleanup(struct cam_periph *periph);
574 static void	 scsi_find_quirk(struct cam_ed *device);
575 static void	 scsi_scan_bus(struct cam_periph *periph, union ccb *ccb);
576 static void	 scsi_scan_lun(struct cam_periph *periph,
577 			       struct cam_path *path, cam_flags flags,
578 			       union ccb *ccb);
579 static void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
580 static struct cam_ed *
581 		 scsi_alloc_device(struct cam_eb *bus, struct cam_et *target,
582 				   lun_id_t lun_id);
583 static void	 scsi_devise_transport(struct cam_path *path);
584 static void	 scsi_set_transfer_settings(struct ccb_trans_settings *cts,
585 					    struct cam_path *path,
586 					    int async_update);
587 static void	 scsi_toggle_tags(struct cam_path *path);
588 static void	 scsi_dev_async(u_int32_t async_code,
589 				struct cam_eb *bus,
590 				struct cam_et *target,
591 				struct cam_ed *device,
592 				void *async_arg);
593 static void	 scsi_action(union ccb *start_ccb);
594 static void	 scsi_announce_periph(struct cam_periph *periph);
595 
596 static struct xpt_xport scsi_xport = {
597 	.alloc_device = scsi_alloc_device,
598 	.action = scsi_action,
599 	.async = scsi_dev_async,
600 	.announce = scsi_announce_periph,
601 };
602 
603 struct xpt_xport *
604 scsi_get_xport(void)
605 {
606 	return (&scsi_xport);
607 }
608 
609 static void
610 probe_periph_init()
611 {
612 }
613 
614 static cam_status
615 proberegister(struct cam_periph *periph, void *arg)
616 {
617 	union ccb *request_ccb;	/* CCB representing the probe request */
618 	cam_status status;
619 	probe_softc *softc;
620 
621 	request_ccb = (union ccb *)arg;
622 	if (request_ccb == NULL) {
623 		printf("proberegister: no probe CCB, "
624 		       "can't register device\n");
625 		return(CAM_REQ_CMP_ERR);
626 	}
627 
628 	softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
629 
630 	if (softc == NULL) {
631 		printf("proberegister: Unable to probe new device. "
632 		       "Unable to allocate softc\n");
633 		return(CAM_REQ_CMP_ERR);
634 	}
635 	TAILQ_INIT(&softc->request_ccbs);
636 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
637 			  periph_links.tqe);
638 	softc->flags = 0;
639 	periph->softc = softc;
640 	softc->periph = periph;
641 	softc->action = PROBE_INVALID;
642 	status = cam_periph_acquire(periph);
643 	if (status != CAM_REQ_CMP) {
644 		return (status);
645 	}
646 	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
647 	scsi_devise_transport(periph->path);
648 
649 	/*
650 	 * Ensure we've waited at least a bus settle
651 	 * delay before attempting to probe the device.
652 	 * For HBAs that don't do bus resets, this won't make a difference.
653 	 */
654 	cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
655 				      scsi_delay);
656 	probeschedule(periph);
657 	return(CAM_REQ_CMP);
658 }
659 
660 static void
661 probeschedule(struct cam_periph *periph)
662 {
663 	struct ccb_pathinq cpi;
664 	union ccb *ccb;
665 	probe_softc *softc;
666 
667 	softc = (probe_softc *)periph->softc;
668 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
669 
670 	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
671 	cpi.ccb_h.func_code = XPT_PATH_INQ;
672 	xpt_action((union ccb *)&cpi);
673 
674 	/*
675 	 * If a device has gone away and another device, or the same one,
676 	 * is back in the same place, it should have a unit attention
677 	 * condition pending.  It will not report the unit attention in
678 	 * response to an inquiry, which may leave invalid transfer
679 	 * negotiations in effect.  The TUR will reveal the unit attention
680 	 * condition.  Only send the TUR for lun 0, since some devices
681 	 * will get confused by commands other than inquiry to non-existent
682 	 * luns.  If you think a device has gone away start your scan from
683 	 * lun 0.  This will insure that any bogus transfer settings are
684 	 * invalidated.
685 	 *
686 	 * If we haven't seen the device before and the controller supports
687 	 * some kind of transfer negotiation, negotiate with the first
688 	 * sent command if no bus reset was performed at startup.  This
689 	 * ensures that the device is not confused by transfer negotiation
690 	 * settings left over by loader or BIOS action.
691 	 */
692 	if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
693 	 && (ccb->ccb_h.target_lun == 0)) {
694 		PROBE_SET_ACTION(softc, PROBE_TUR);
695 	} else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
696 	      && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
697 		proberequestdefaultnegotiation(periph);
698 		PROBE_SET_ACTION(softc, PROBE_INQUIRY);
699 	} else {
700 		PROBE_SET_ACTION(softc, PROBE_INQUIRY);
701 	}
702 
703 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
704 		softc->flags |= PROBE_NO_ANNOUNCE;
705 	else
706 		softc->flags &= ~PROBE_NO_ANNOUNCE;
707 
708 	if (cpi.hba_misc & PIM_EXTLUNS)
709 		softc->flags |= PROBE_EXTLUN;
710 	else
711 		softc->flags &= ~PROBE_EXTLUN;
712 
713 	xpt_schedule(periph, CAM_PRIORITY_XPT);
714 }
715 
716 static void
717 probestart(struct cam_periph *periph, union ccb *start_ccb)
718 {
719 	/* Probe the device that our peripheral driver points to */
720 	struct ccb_scsiio *csio;
721 	probe_softc *softc;
722 
723 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
724 
725 	softc = (probe_softc *)periph->softc;
726 	csio = &start_ccb->csio;
727 again:
728 
729 	switch (softc->action) {
730 	case PROBE_TUR:
731 	case PROBE_TUR_FOR_NEGOTIATION:
732 	case PROBE_DV_EXIT:
733 	{
734 		scsi_test_unit_ready(csio,
735 				     /*retries*/4,
736 				     probedone,
737 				     MSG_SIMPLE_Q_TAG,
738 				     SSD_FULL_SIZE,
739 				     /*timeout*/60000);
740 		break;
741 	}
742 	case PROBE_INQUIRY:
743 	case PROBE_FULL_INQUIRY:
744 	case PROBE_INQUIRY_BASIC_DV1:
745 	case PROBE_INQUIRY_BASIC_DV2:
746 	{
747 		u_int inquiry_len;
748 		struct scsi_inquiry_data *inq_buf;
749 
750 		inq_buf = &periph->path->device->inq_data;
751 
752 		/*
753 		 * If the device is currently configured, we calculate an
754 		 * MD5 checksum of the inquiry data, and if the serial number
755 		 * length is greater than 0, add the serial number data
756 		 * into the checksum as well.  Once the inquiry and the
757 		 * serial number check finish, we attempt to figure out
758 		 * whether we still have the same device.
759 		 */
760 		if (((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
761 		 && ((softc->flags & PROBE_INQUIRY_CKSUM) == 0)) {
762 
763 			MD5Init(&softc->context);
764 			MD5Update(&softc->context, (unsigned char *)inq_buf,
765 				  sizeof(struct scsi_inquiry_data));
766 			softc->flags |= PROBE_INQUIRY_CKSUM;
767 			if (periph->path->device->serial_num_len > 0) {
768 				MD5Update(&softc->context,
769 					  periph->path->device->serial_num,
770 					  periph->path->device->serial_num_len);
771 				softc->flags |= PROBE_SERIAL_CKSUM;
772 			}
773 			MD5Final(softc->digest, &softc->context);
774 		}
775 
776 		if (softc->action == PROBE_INQUIRY)
777 			inquiry_len = SHORT_INQUIRY_LENGTH;
778 		else
779 			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
780 
781 		/*
782 		 * Some parallel SCSI devices fail to send an
783 		 * ignore wide residue message when dealing with
784 		 * odd length inquiry requests.  Round up to be
785 		 * safe.
786 		 */
787 		inquiry_len = roundup2(inquiry_len, 2);
788 
789 		if (softc->action == PROBE_INQUIRY_BASIC_DV1
790 		 || softc->action == PROBE_INQUIRY_BASIC_DV2) {
791 			inq_buf = malloc(inquiry_len, M_CAMXPT, M_NOWAIT);
792 		}
793 		if (inq_buf == NULL) {
794 			xpt_print(periph->path, "malloc failure- skipping Basic"
795 			    "Domain Validation\n");
796 			PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
797 			scsi_test_unit_ready(csio,
798 					     /*retries*/4,
799 					     probedone,
800 					     MSG_SIMPLE_Q_TAG,
801 					     SSD_FULL_SIZE,
802 					     /*timeout*/60000);
803 			break;
804 		}
805 		scsi_inquiry(csio,
806 			     /*retries*/4,
807 			     probedone,
808 			     MSG_SIMPLE_Q_TAG,
809 			     (u_int8_t *)inq_buf,
810 			     inquiry_len,
811 			     /*evpd*/FALSE,
812 			     /*page_code*/0,
813 			     SSD_MIN_SIZE,
814 			     /*timeout*/60 * 1000);
815 		break;
816 	}
817 	case PROBE_REPORT_LUNS:
818 	{
819 		void *rp;
820 
821 		rp = malloc(periph->path->target->rpl_size,
822 		    M_CAMXPT, M_NOWAIT | M_ZERO);
823 		if (rp == NULL) {
824 			struct scsi_inquiry_data *inq_buf;
825 			inq_buf = &periph->path->device->inq_data;
826 			xpt_print(periph->path,
827 			    "Unable to alloc report luns storage\n");
828 			if (INQ_DATA_TQ_ENABLED(inq_buf))
829 				PROBE_SET_ACTION(softc, PROBE_MODE_SENSE);
830 			else
831 				PROBE_SET_ACTION(softc,
832 				    PROBE_SUPPORTED_VPD_LIST);
833 			goto again;
834 		}
835 		scsi_report_luns(csio, 5, probedone, MSG_SIMPLE_Q_TAG,
836 		    RPL_REPORT_DEFAULT, rp, periph->path->target->rpl_size,
837 		    SSD_FULL_SIZE, 60000); break;
838 		break;
839 	}
840 	case PROBE_MODE_SENSE:
841 	{
842 		void  *mode_buf;
843 		int    mode_buf_len;
844 
845 		mode_buf_len = sizeof(struct scsi_mode_header_6)
846 			     + sizeof(struct scsi_mode_blk_desc)
847 			     + sizeof(struct scsi_control_page);
848 		mode_buf = malloc(mode_buf_len, M_CAMXPT, M_NOWAIT);
849 		if (mode_buf != NULL) {
850 	                scsi_mode_sense(csio,
851 					/*retries*/4,
852 					probedone,
853 					MSG_SIMPLE_Q_TAG,
854 					/*dbd*/FALSE,
855 					SMS_PAGE_CTRL_CURRENT,
856 					SMS_CONTROL_MODE_PAGE,
857 					mode_buf,
858 					mode_buf_len,
859 					SSD_FULL_SIZE,
860 					/*timeout*/60000);
861 			break;
862 		}
863 		xpt_print(periph->path, "Unable to mode sense control page - "
864 		    "malloc failure\n");
865 		PROBE_SET_ACTION(softc, PROBE_SUPPORTED_VPD_LIST);
866 	}
867 	/* FALLTHROUGH */
868 	case PROBE_SUPPORTED_VPD_LIST:
869 	{
870 		struct scsi_vpd_supported_page_list *vpd_list;
871 		struct cam_ed *device;
872 
873 		vpd_list = NULL;
874 		device = periph->path->device;
875 
876 		if ((SCSI_QUIRK(device)->quirks & CAM_QUIRK_NOVPDS) == 0)
877 			vpd_list = malloc(sizeof(*vpd_list), M_CAMXPT,
878 			    M_NOWAIT | M_ZERO);
879 
880 		if (vpd_list != NULL) {
881 			scsi_inquiry(csio,
882 				     /*retries*/4,
883 				     probedone,
884 				     MSG_SIMPLE_Q_TAG,
885 				     (u_int8_t *)vpd_list,
886 				     sizeof(*vpd_list),
887 				     /*evpd*/TRUE,
888 				     SVPD_SUPPORTED_PAGE_LIST,
889 				     SSD_MIN_SIZE,
890 				     /*timeout*/60 * 1000);
891 			break;
892 		}
893 done:
894 		/*
895 		 * We'll have to do without, let our probedone
896 		 * routine finish up for us.
897 		 */
898 		start_ccb->csio.data_ptr = NULL;
899 		cam_freeze_devq(periph->path);
900 		cam_periph_doacquire(periph);
901 		probedone(periph, start_ccb);
902 		return;
903 	}
904 	case PROBE_DEVICE_ID:
905 	{
906 		struct scsi_vpd_device_id *devid;
907 
908 		devid = NULL;
909 		if (scsi_vpd_supported_page(periph, SVPD_DEVICE_ID))
910 			devid = malloc(SVPD_DEVICE_ID_MAX_SIZE, M_CAMXPT,
911 			    M_NOWAIT | M_ZERO);
912 
913 		if (devid != NULL) {
914 			scsi_inquiry(csio,
915 				     /*retries*/4,
916 				     probedone,
917 				     MSG_SIMPLE_Q_TAG,
918 				     (uint8_t *)devid,
919 				     SVPD_DEVICE_ID_MAX_SIZE,
920 				     /*evpd*/TRUE,
921 				     SVPD_DEVICE_ID,
922 				     SSD_MIN_SIZE,
923 				     /*timeout*/60 * 1000);
924 			break;
925 		}
926 		goto done;
927 	}
928 	case PROBE_EXTENDED_INQUIRY:
929 	{
930 		struct scsi_vpd_extended_inquiry_data *ext_inq;
931 
932 		ext_inq = NULL;
933 		if (scsi_vpd_supported_page(periph, SVPD_EXTENDED_INQUIRY_DATA))
934 			ext_inq = malloc(sizeof(*ext_inq), M_CAMXPT,
935 			    M_NOWAIT | M_ZERO);
936 
937 		if (ext_inq != NULL) {
938 			scsi_inquiry(csio,
939 				     /*retries*/4,
940 				     probedone,
941 				     MSG_SIMPLE_Q_TAG,
942 				     (uint8_t *)ext_inq,
943 				     sizeof(*ext_inq),
944 				     /*evpd*/TRUE,
945 				     SVPD_EXTENDED_INQUIRY_DATA,
946 				     SSD_MIN_SIZE,
947 				     /*timeout*/60 * 1000);
948 			break;
949 		}
950 		/*
951 		 * We'll have to do without, let our probedone
952 		 * routine finish up for us.
953 		 */
954 		goto done;
955 	}
956 	case PROBE_SERIAL_NUM:
957 	{
958 		struct scsi_vpd_unit_serial_number *serial_buf;
959 		struct cam_ed* device;
960 
961 		serial_buf = NULL;
962 		device = periph->path->device;
963 		if (device->serial_num != NULL) {
964 			free(device->serial_num, M_CAMXPT);
965 			device->serial_num = NULL;
966 			device->serial_num_len = 0;
967 		}
968 
969 		if (scsi_vpd_supported_page(periph, SVPD_UNIT_SERIAL_NUMBER))
970 			serial_buf = (struct scsi_vpd_unit_serial_number *)
971 				malloc(sizeof(*serial_buf), M_CAMXPT,
972 				    M_NOWAIT|M_ZERO);
973 
974 		if (serial_buf != NULL) {
975 			scsi_inquiry(csio,
976 				     /*retries*/4,
977 				     probedone,
978 				     MSG_SIMPLE_Q_TAG,
979 				     (u_int8_t *)serial_buf,
980 				     sizeof(*serial_buf),
981 				     /*evpd*/TRUE,
982 				     SVPD_UNIT_SERIAL_NUMBER,
983 				     SSD_MIN_SIZE,
984 				     /*timeout*/60 * 1000);
985 			break;
986 		}
987 		goto done;
988 	}
989 	default:
990 		panic("probestart: invalid action state 0x%x\n", softc->action);
991 	}
992 	start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
993 	cam_periph_doacquire(periph);
994 	xpt_action(start_ccb);
995 }
996 
997 static void
998 proberequestdefaultnegotiation(struct cam_periph *periph)
999 {
1000 	struct ccb_trans_settings cts;
1001 
1002 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1003 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1004 	cts.type = CTS_TYPE_USER_SETTINGS;
1005 	xpt_action((union ccb *)&cts);
1006 	if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP) {
1007 		return;
1008 	}
1009 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1010 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1011 	xpt_action((union ccb *)&cts);
1012 }
1013 
1014 /*
1015  * Backoff Negotiation Code- only pertinent for SPI devices.
1016  */
1017 static int
1018 proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
1019 {
1020 	struct ccb_trans_settings cts;
1021 	struct ccb_trans_settings_spi *spi;
1022 
1023 	memset(&cts, 0, sizeof (cts));
1024 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1025 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1026 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1027 	xpt_action((union ccb *)&cts);
1028 	if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP) {
1029 		if (bootverbose) {
1030 			xpt_print(periph->path,
1031 			    "failed to get current device settings\n");
1032 		}
1033 		return (0);
1034 	}
1035 	if (cts.transport != XPORT_SPI) {
1036 		if (bootverbose) {
1037 			xpt_print(periph->path, "not SPI transport\n");
1038 		}
1039 		return (0);
1040 	}
1041 	spi = &cts.xport_specific.spi;
1042 
1043 	/*
1044 	 * We cannot renegotiate sync rate if we don't have one.
1045 	 */
1046 	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
1047 		if (bootverbose) {
1048 			xpt_print(periph->path, "no sync rate known\n");
1049 		}
1050 		return (0);
1051 	}
1052 
1053 	/*
1054 	 * We'll assert that we don't have to touch PPR options- the
1055 	 * SIM will see what we do with period and offset and adjust
1056 	 * the PPR options as appropriate.
1057 	 */
1058 
1059 	/*
1060 	 * A sync rate with unknown or zero offset is nonsensical.
1061 	 * A sync period of zero means Async.
1062 	 */
1063 	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
1064 	 || spi->sync_offset == 0 || spi->sync_period == 0) {
1065 		if (bootverbose) {
1066 			xpt_print(periph->path, "no sync rate available\n");
1067 		}
1068 		return (0);
1069 	}
1070 
1071 	if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
1072 		CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1073 		    ("hit async: giving up on DV\n"));
1074 		return (0);
1075 	}
1076 
1077 
1078 	/*
1079 	 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
1080 	 * We don't try to remember 'last' settings to see if the SIM actually
1081 	 * gets into the speed we want to set. We check on the SIM telling
1082 	 * us that a requested speed is bad, but otherwise don't try and
1083 	 * check the speed due to the asynchronous and handshake nature
1084 	 * of speed setting.
1085 	 */
1086 	spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
1087 	for (;;) {
1088 		spi->sync_period++;
1089 		if (spi->sync_period >= 0xf) {
1090 			spi->sync_period = 0;
1091 			spi->sync_offset = 0;
1092 			CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1093 			    ("setting to async for DV\n"));
1094 			/*
1095 			 * Once we hit async, we don't want to try
1096 			 * any more settings.
1097 			 */
1098 			device->flags |= CAM_DEV_DV_HIT_BOTTOM;
1099 		} else if (bootverbose) {
1100 			CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1101 			    ("DV: period 0x%x\n", spi->sync_period));
1102 			printf("setting period to 0x%x\n", spi->sync_period);
1103 		}
1104 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1105 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1106 		xpt_action((union ccb *)&cts);
1107 		if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP) {
1108 			break;
1109 		}
1110 		CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1111 		    ("DV: failed to set period 0x%x\n", spi->sync_period));
1112 		if (spi->sync_period == 0) {
1113 			return (0);
1114 		}
1115 	}
1116 	return (1);
1117 }
1118 
1119 #define CCB_COMPLETED_OK(ccb) (((ccb).status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1120 
1121 static void
1122 probedone(struct cam_periph *periph, union ccb *done_ccb)
1123 {
1124 	probe_softc *softc;
1125 	struct cam_path *path;
1126 	u_int32_t  priority;
1127 
1128 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
1129 
1130 	softc = (probe_softc *)periph->softc;
1131 	path = done_ccb->ccb_h.path;
1132 	priority = done_ccb->ccb_h.pinfo.priority;
1133 
1134 	switch (softc->action) {
1135 	case PROBE_TUR:
1136 	{
1137 		if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1138 
1139 			if (cam_periph_error(done_ccb, 0,
1140 					     SF_NO_PRINT, NULL) == ERESTART) {
1141 outr:
1142 				/* Drop freeze taken due to CAM_DEV_QFREEZE */
1143 				cam_release_devq(path, 0, 0, 0, FALSE);
1144 				return;
1145 			}
1146 			else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1147 				/* Don't wedge the queue */
1148 				xpt_release_devq(done_ccb->ccb_h.path,
1149 						 /*count*/1,
1150 						 /*run_queue*/TRUE);
1151 		}
1152 		PROBE_SET_ACTION(softc, PROBE_INQUIRY);
1153 		xpt_release_ccb(done_ccb);
1154 		xpt_schedule(periph, priority);
1155 out:
1156 		/* Drop freeze taken due to CAM_DEV_QFREEZE and release. */
1157 		cam_release_devq(path, 0, 0, 0, FALSE);
1158 		cam_periph_release_locked(periph);
1159 		return;
1160 	}
1161 	case PROBE_INQUIRY:
1162 	case PROBE_FULL_INQUIRY:
1163 	{
1164 		if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
1165 			struct scsi_inquiry_data *inq_buf;
1166 			u_int8_t periph_qual;
1167 
1168 			path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1169 			scsi_find_quirk(path->device);
1170 			inq_buf = &path->device->inq_data;
1171 
1172 			periph_qual = SID_QUAL(inq_buf);
1173 
1174 			if (periph_qual == SID_QUAL_LU_CONNECTED) {
1175 				u_int8_t len;
1176 
1177 				/*
1178 				 * We conservatively request only
1179 				 * SHORT_INQUIRY_LEN bytes of inquiry
1180 				 * information during our first try
1181 				 * at sending an INQUIRY. If the device
1182 				 * has more information to give,
1183 				 * perform a second request specifying
1184 				 * the amount of information the device
1185 				 * is willing to give.
1186 				 */
1187 				len = inq_buf->additional_length
1188 				    + offsetof(struct scsi_inquiry_data,
1189                                                additional_length) + 1;
1190 				if (softc->action == PROBE_INQUIRY
1191 				    && len > SHORT_INQUIRY_LENGTH) {
1192 					PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1193 					xpt_release_ccb(done_ccb);
1194 					xpt_schedule(periph, priority);
1195 					goto out;
1196 				}
1197 
1198 				scsi_devise_transport(path);
1199 
1200 				if (path->device->lun_id == 0 &&
1201 				    SID_ANSI_REV(inq_buf) > SCSI_REV_SPC2 &&
1202 				    (SCSI_QUIRK(path->device)->quirks &
1203 				     CAM_QUIRK_NORPTLUNS) == 0) {
1204 					PROBE_SET_ACTION(softc,
1205 					    PROBE_REPORT_LUNS);
1206 					/*
1207 					 * Start with room for *one* lun.
1208 					 */
1209 					periph->path->target->rpl_size = 16;
1210 				} else if (INQ_DATA_TQ_ENABLED(inq_buf))
1211 					PROBE_SET_ACTION(softc,
1212 					    PROBE_MODE_SENSE);
1213 				else
1214 					PROBE_SET_ACTION(softc,
1215 					    PROBE_SUPPORTED_VPD_LIST);
1216 
1217 				if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1218 					path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1219 					xpt_acquire_device(path->device);
1220 				}
1221 				xpt_release_ccb(done_ccb);
1222 				xpt_schedule(periph, priority);
1223 				goto out;
1224 			} else if (path->device->lun_id == 0 &&
1225 			    SID_ANSI_REV(inq_buf) >= SCSI_REV_SPC2 &&
1226 			    (SCSI_QUIRK(path->device)->quirks &
1227 			     CAM_QUIRK_NORPTLUNS) == 0) {
1228 				PROBE_SET_ACTION(softc, PROBE_REPORT_LUNS);
1229 				periph->path->target->rpl_size = 16;
1230 				xpt_release_ccb(done_ccb);
1231 				xpt_schedule(periph, priority);
1232 				goto out;
1233 			}
1234 		} else if (cam_periph_error(done_ccb, 0,
1235 					    done_ccb->ccb_h.target_lun > 0
1236 					    ? SF_RETRY_UA|SF_QUIET_IR
1237 					    : SF_RETRY_UA,
1238 					    &softc->saved_ccb) == ERESTART) {
1239 			goto outr;
1240 		} else {
1241 			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,
1244 				    /*count*/1, /*run_queue*/TRUE);
1245 			}
1246 			path->device->flags &= ~CAM_DEV_INQUIRY_DATA_VALID;
1247 		}
1248 		/*
1249 		 * If we get to this point, we got an error status back
1250 		 * from the inquiry and the error status doesn't require
1251 		 * automatically retrying the command.  Therefore, the
1252 		 * inquiry failed.  If we had inquiry information before
1253 		 * for this device, but this latest inquiry command failed,
1254 		 * the device has probably gone away.  If this device isn't
1255 		 * already marked unconfigured, notify the peripheral
1256 		 * drivers that this device is no more.
1257 		 */
1258 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
1259 			/* Send the async notification. */
1260 			xpt_async(AC_LOST_DEVICE, path, NULL);
1261 		PROBE_SET_ACTION(softc, PROBE_INVALID);
1262 
1263 		xpt_release_ccb(done_ccb);
1264 		break;
1265 	}
1266 	case PROBE_REPORT_LUNS:
1267 	{
1268 		struct ccb_scsiio *csio;
1269 		struct scsi_report_luns_data *lp;
1270 		u_int nlun, maxlun;
1271 
1272 		csio = &done_ccb->csio;
1273 
1274 		lp = (struct scsi_report_luns_data *)csio->data_ptr;
1275 		nlun = scsi_4btoul(lp->length) / 8;
1276 		maxlun = (csio->dxfer_len / 8) - 1;
1277 
1278 		if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1279 			if (cam_periph_error(done_ccb, 0,
1280 			    done_ccb->ccb_h.target_lun > 0 ?
1281 			    SF_RETRY_UA|SF_QUIET_IR : SF_RETRY_UA,
1282 			    &softc->saved_ccb) == ERESTART) {
1283 				goto outr;
1284 			}
1285 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1286 				xpt_release_devq(done_ccb->ccb_h.path, 1,
1287 				    TRUE);
1288 			}
1289 			free(lp, M_CAMXPT);
1290 			lp = NULL;
1291 		} else if (nlun > maxlun) {
1292 			/*
1293 			 * Reallocate and retry to cover all luns
1294 			 */
1295 			CAM_DEBUG(path, CAM_DEBUG_PROBE,
1296 			    ("Probe: reallocating REPORT_LUNS for %u luns\n",
1297 			     nlun));
1298 			free(lp, M_CAMXPT);
1299 			path->target->rpl_size = (nlun << 3) + 8;
1300 			xpt_release_ccb(done_ccb);
1301 			xpt_schedule(periph, priority);
1302 			goto out;
1303 		} else if (nlun == 0) {
1304 			/*
1305 			 * If there don't appear to be any luns, bail.
1306 			 */
1307 			free(lp, M_CAMXPT);
1308 			lp = NULL;
1309 		} else {
1310 			lun_id_t lun;
1311 			int idx;
1312 
1313 			CAM_DEBUG(path, CAM_DEBUG_PROBE,
1314 			   ("Probe: %u lun(s) reported\n", nlun));
1315 
1316 			CAM_GET_LUN(lp, 0, lun);
1317 			/*
1318 			 * If the first lun is not lun 0, then either there
1319 			 * is no lun 0 in the list, or the list is unsorted.
1320 			 */
1321 			if (lun != 0) {
1322 				for (idx = 0; idx < nlun; idx++) {
1323 					CAM_GET_LUN(lp, idx, lun);
1324 					if (lun == 0) {
1325 						break;
1326 					}
1327 				}
1328 				if (idx != nlun) {
1329 					uint8_t tlun[8];
1330 					memcpy(tlun,
1331 					    lp->luns[0].lundata, 8);
1332 					memcpy(lp->luns[0].lundata,
1333 					    lp->luns[idx].lundata, 8);
1334 					memcpy(lp->luns[idx].lundata,
1335 					    tlun, 8);
1336 					CAM_DEBUG(path, CAM_DEBUG_PROBE,
1337 					    ("lun 0 in position %u\n", idx));
1338 				}
1339 			}
1340 			/*
1341 			 * If we have an old lun list, We can either
1342 			 * retest luns that appear to have been dropped,
1343 			 * or just nuke them.  We'll opt for the latter.
1344 			 * This function will also install the new list
1345 			 * in the target structure.
1346 			 */
1347 			probe_purge_old(path, lp, softc->flags);
1348 			lp = NULL;
1349 		}
1350 		if (path->device->flags & CAM_DEV_INQUIRY_DATA_VALID &&
1351 		    SID_QUAL(&path->device->inq_data) == SID_QUAL_LU_CONNECTED) {
1352 			struct scsi_inquiry_data *inq_buf;
1353 			inq_buf = &path->device->inq_data;
1354 			if (INQ_DATA_TQ_ENABLED(inq_buf))
1355 				PROBE_SET_ACTION(softc, PROBE_MODE_SENSE);
1356 			else
1357 				PROBE_SET_ACTION(softc,
1358 				    PROBE_SUPPORTED_VPD_LIST);
1359 			xpt_release_ccb(done_ccb);
1360 			xpt_schedule(periph, priority);
1361 			goto out;
1362 		}
1363 		if (lp) {
1364 			free(lp, M_CAMXPT);
1365 		}
1366 		PROBE_SET_ACTION(softc, PROBE_INVALID);
1367 		xpt_release_ccb(done_ccb);
1368 		break;
1369 	}
1370 	case PROBE_MODE_SENSE:
1371 	{
1372 		struct ccb_scsiio *csio;
1373 		struct scsi_mode_header_6 *mode_hdr;
1374 
1375 		csio = &done_ccb->csio;
1376 		mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
1377 		if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
1378 			struct scsi_control_page *page;
1379 			u_int8_t *offset;
1380 
1381 			offset = ((u_int8_t *)&mode_hdr[1])
1382 			    + mode_hdr->blk_desc_len;
1383 			page = (struct scsi_control_page *)offset;
1384 			path->device->queue_flags = page->queue_flags;
1385 		} else if (cam_periph_error(done_ccb, 0,
1386 					    SF_RETRY_UA|SF_NO_PRINT,
1387 					    &softc->saved_ccb) == ERESTART) {
1388 			goto outr;
1389 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1390 			/* Don't wedge the queue */
1391 			xpt_release_devq(done_ccb->ccb_h.path,
1392 					 /*count*/1, /*run_queue*/TRUE);
1393 		}
1394 		xpt_release_ccb(done_ccb);
1395 		free(mode_hdr, M_CAMXPT);
1396 		PROBE_SET_ACTION(softc, PROBE_SUPPORTED_VPD_LIST);
1397 		xpt_schedule(periph, priority);
1398 		goto out;
1399 	}
1400 	case PROBE_SUPPORTED_VPD_LIST:
1401 	{
1402 		struct ccb_scsiio *csio;
1403 		struct scsi_vpd_supported_page_list *page_list;
1404 
1405 		csio = &done_ccb->csio;
1406 		page_list =
1407 		    (struct scsi_vpd_supported_page_list *)csio->data_ptr;
1408 
1409 		if (path->device->supported_vpds != NULL) {
1410 			free(path->device->supported_vpds, M_CAMXPT);
1411 			path->device->supported_vpds = NULL;
1412 			path->device->supported_vpds_len = 0;
1413 		}
1414 
1415 		if (page_list == NULL) {
1416 			/*
1417 			 * Don't process the command as it was never sent
1418 			 */
1419 		} else if (CCB_COMPLETED_OK(csio->ccb_h)) {
1420 			/* Got vpd list */
1421 			path->device->supported_vpds_len = page_list->length +
1422 			    SVPD_SUPPORTED_PAGES_HDR_LEN;
1423 			path->device->supported_vpds = (uint8_t *)page_list;
1424 			xpt_release_ccb(done_ccb);
1425 			PROBE_SET_ACTION(softc, PROBE_DEVICE_ID);
1426 			xpt_schedule(periph, priority);
1427 			goto out;
1428 		} else if (cam_periph_error(done_ccb, 0,
1429 					    SF_RETRY_UA|SF_NO_PRINT,
1430 					    &softc->saved_ccb) == ERESTART) {
1431 			goto outr;
1432 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1433 			/* Don't wedge the queue */
1434 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1435 					 /*run_queue*/TRUE);
1436 		}
1437 
1438 		if (page_list)
1439 			free(page_list, M_CAMXPT);
1440 		/* No VPDs available, skip to device check. */
1441 		csio->data_ptr = NULL;
1442 		goto probe_device_check;
1443 	}
1444 	case PROBE_DEVICE_ID:
1445 	{
1446 		struct scsi_vpd_device_id *devid;
1447 		struct ccb_scsiio *csio;
1448 		uint32_t length = 0;
1449 
1450 		csio = &done_ccb->csio;
1451 		devid = (struct scsi_vpd_device_id *)csio->data_ptr;
1452 
1453 		/* Clean up from previous instance of this device */
1454 		if (path->device->device_id != NULL) {
1455 			path->device->device_id_len = 0;
1456 			free(path->device->device_id, M_CAMXPT);
1457 			path->device->device_id = NULL;
1458 		}
1459 
1460 		if (devid == NULL) {
1461 			/* Don't process the command as it was never sent */
1462 		} else if (CCB_COMPLETED_OK(csio->ccb_h)) {
1463 			length = scsi_2btoul(devid->length);
1464 			if (length != 0) {
1465 				/*
1466 				 * NB: device_id_len is actual response
1467 				 * size, not buffer size.
1468 				 */
1469 				path->device->device_id_len = length +
1470 				    SVPD_DEVICE_ID_HDR_LEN;
1471 				path->device->device_id = (uint8_t *)devid;
1472 			}
1473 		} else if (cam_periph_error(done_ccb, 0,
1474 					    SF_RETRY_UA,
1475 					    &softc->saved_ccb) == ERESTART) {
1476 			goto outr;
1477 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1478 			/* Don't wedge the queue */
1479 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1480 					 /*run_queue*/TRUE);
1481 		}
1482 
1483 		/* Free the device id space if we don't use it */
1484 		if (devid && length == 0)
1485 			free(devid, M_CAMXPT);
1486 		xpt_release_ccb(done_ccb);
1487 		PROBE_SET_ACTION(softc, PROBE_EXTENDED_INQUIRY);
1488 		xpt_schedule(periph, priority);
1489 		goto out;
1490 	}
1491 	case PROBE_EXTENDED_INQUIRY: {
1492 		struct scsi_vpd_extended_inquiry_data *ext_inq;
1493 		struct ccb_scsiio *csio;
1494 		int32_t length = 0;
1495 
1496 		csio = &done_ccb->csio;
1497 		ext_inq = (struct scsi_vpd_extended_inquiry_data *)
1498 		    csio->data_ptr;
1499 		if (path->device->ext_inq != NULL) {
1500 			path->device->ext_inq_len = 0;
1501 			free(path->device->ext_inq, M_CAMXPT);
1502 			path->device->ext_inq = NULL;
1503 		}
1504 
1505 		if (ext_inq == NULL) {
1506 			/* Don't process the command as it was never sent */
1507 		} else if (CCB_COMPLETED_OK(csio->ccb_h)) {
1508 			length = scsi_2btoul(ext_inq->page_length) +
1509 			    __offsetof(struct scsi_vpd_extended_inquiry_data,
1510 			    flags1);
1511 			length = min(length, sizeof(*ext_inq));
1512 			length -= csio->resid;
1513 			if (length > 0) {
1514 				path->device->ext_inq_len = length;
1515 				path->device->ext_inq = (uint8_t *)ext_inq;
1516 			}
1517 		} else if (cam_periph_error(done_ccb, 0,
1518 					    SF_RETRY_UA,
1519 					    &softc->saved_ccb) == ERESTART) {
1520 			return;
1521 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1522 			/* Don't wedge the queue */
1523 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1524 					 /*run_queue*/TRUE);
1525 		}
1526 
1527 		/* Free the device id space if we don't use it */
1528 		if (ext_inq && length <= 0)
1529 			free(ext_inq, M_CAMXPT);
1530 		xpt_release_ccb(done_ccb);
1531 		PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM);
1532 		xpt_schedule(periph, priority);
1533 		goto out;
1534 	}
1535 
1536 probe_device_check:
1537 	case PROBE_SERIAL_NUM:
1538 	{
1539 		struct ccb_scsiio *csio;
1540 		struct scsi_vpd_unit_serial_number *serial_buf;
1541 		u_int32_t  priority;
1542 		int changed;
1543 		int have_serialnum;
1544 
1545 		changed = 1;
1546 		have_serialnum = 0;
1547 		csio = &done_ccb->csio;
1548 		priority = done_ccb->ccb_h.pinfo.priority;
1549 		serial_buf =
1550 		    (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
1551 
1552 		if (serial_buf == NULL) {
1553 			/*
1554 			 * Don't process the command as it was never sent
1555 			 */
1556 		} else if (cam_ccb_status(done_ccb) == CAM_REQ_CMP
1557 			&& (serial_buf->length > 0)) {
1558 
1559 			have_serialnum = 1;
1560 			path->device->serial_num =
1561 				(u_int8_t *)malloc((serial_buf->length + 1),
1562 						   M_CAMXPT, M_NOWAIT);
1563 			if (path->device->serial_num != NULL) {
1564 				memcpy(path->device->serial_num,
1565 				       serial_buf->serial_num,
1566 				       serial_buf->length);
1567 				path->device->serial_num_len =
1568 				    serial_buf->length;
1569 				path->device->serial_num[serial_buf->length]
1570 				    = '\0';
1571 			}
1572 		} else if (cam_periph_error(done_ccb, 0,
1573 					    SF_RETRY_UA|SF_NO_PRINT,
1574 					    &softc->saved_ccb) == ERESTART) {
1575 			goto outr;
1576 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1577 			/* Don't wedge the queue */
1578 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1579 					 /*run_queue*/TRUE);
1580 		}
1581 
1582 		/*
1583 		 * Let's see if we have seen this device before.
1584 		 */
1585 		if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
1586 			MD5_CTX context;
1587 			u_int8_t digest[16];
1588 
1589 			MD5Init(&context);
1590 
1591 			MD5Update(&context,
1592 				  (unsigned char *)&path->device->inq_data,
1593 				  sizeof(struct scsi_inquiry_data));
1594 
1595 			if (have_serialnum)
1596 				MD5Update(&context, serial_buf->serial_num,
1597 					  serial_buf->length);
1598 
1599 			MD5Final(digest, &context);
1600 			if (bcmp(softc->digest, digest, 16) == 0)
1601 				changed = 0;
1602 
1603 			/*
1604 			 * XXX Do we need to do a TUR in order to ensure
1605 			 *     that the device really hasn't changed???
1606 			 */
1607 			if ((changed != 0)
1608 			 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
1609 				xpt_async(AC_LOST_DEVICE, path, NULL);
1610 		}
1611 		if (serial_buf != NULL)
1612 			free(serial_buf, M_CAMXPT);
1613 
1614 		if (changed != 0) {
1615 			/*
1616 			 * Now that we have all the necessary
1617 			 * information to safely perform transfer
1618 			 * negotiations... Controllers don't perform
1619 			 * any negotiation or tagged queuing until
1620 			 * after the first XPT_SET_TRAN_SETTINGS ccb is
1621 			 * received.  So, on a new device, just retrieve
1622 			 * the user settings, and set them as the current
1623 			 * settings to set the device up.
1624 			 */
1625 			proberequestdefaultnegotiation(periph);
1626 			xpt_release_ccb(done_ccb);
1627 
1628 			/*
1629 			 * Perform a TUR to allow the controller to
1630 			 * perform any necessary transfer negotiation.
1631 			 */
1632 			PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1633 			xpt_schedule(periph, priority);
1634 			goto out;
1635 		}
1636 		xpt_release_ccb(done_ccb);
1637 		break;
1638 	}
1639 	case PROBE_TUR_FOR_NEGOTIATION:
1640 	case PROBE_DV_EXIT:
1641 		if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1642 			cam_periph_error(done_ccb, 0,
1643 			    SF_NO_PRINT | SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1644 		}
1645 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1646 			/* Don't wedge the queue */
1647 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1648 					 /*run_queue*/TRUE);
1649 		}
1650 		/*
1651 		 * Do Domain Validation for lun 0 on devices that claim
1652 		 * to support Synchronous Transfer modes.
1653 		 */
1654 	 	if (softc->action == PROBE_TUR_FOR_NEGOTIATION
1655 		 && done_ccb->ccb_h.target_lun == 0
1656 		 && (path->device->inq_data.flags & SID_Sync) != 0
1657                  && (path->device->flags & CAM_DEV_IN_DV) == 0) {
1658 			CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1659 			    ("Begin Domain Validation\n"));
1660 			path->device->flags |= CAM_DEV_IN_DV;
1661 			xpt_release_ccb(done_ccb);
1662 			PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV1);
1663 			xpt_schedule(periph, priority);
1664 			goto out;
1665 		}
1666 		if (softc->action == PROBE_DV_EXIT) {
1667 			CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1668 			    ("Leave Domain Validation\n"));
1669 		}
1670 		if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1671 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1672 			xpt_acquire_device(path->device);
1673 		}
1674 		path->device->flags &=
1675 		    ~(CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1676 		if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1677 			/* Inform the XPT that a new device has been found */
1678 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1679 			xpt_action(done_ccb);
1680 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1681 				  done_ccb);
1682 		}
1683 		PROBE_SET_ACTION(softc, PROBE_DONE);
1684 		xpt_release_ccb(done_ccb);
1685 		break;
1686 	case PROBE_INQUIRY_BASIC_DV1:
1687 	case PROBE_INQUIRY_BASIC_DV2:
1688 	{
1689 		struct scsi_inquiry_data *nbuf;
1690 		struct ccb_scsiio *csio;
1691 
1692 		if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1693 			cam_periph_error(done_ccb, 0,
1694 			    SF_NO_PRINT | SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1695 		}
1696 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1697 			/* Don't wedge the queue */
1698 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1699 					 /*run_queue*/TRUE);
1700 		}
1701 		csio = &done_ccb->csio;
1702 		nbuf = (struct scsi_inquiry_data *)csio->data_ptr;
1703 		if (bcmp(nbuf, &path->device->inq_data, SHORT_INQUIRY_LENGTH)) {
1704 			xpt_print(path,
1705 			    "inquiry data fails comparison at DV%d step\n",
1706 			    softc->action == PROBE_INQUIRY_BASIC_DV1 ? 1 : 2);
1707 			if (proberequestbackoff(periph, path->device)) {
1708 				path->device->flags &= ~CAM_DEV_IN_DV;
1709 				PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1710 			} else {
1711 				/* give up */
1712 				PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
1713 			}
1714 			free(nbuf, M_CAMXPT);
1715 			xpt_release_ccb(done_ccb);
1716 			xpt_schedule(periph, priority);
1717 			goto out;
1718 		}
1719 		free(nbuf, M_CAMXPT);
1720 		if (softc->action == PROBE_INQUIRY_BASIC_DV1) {
1721 			PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV2);
1722 			xpt_release_ccb(done_ccb);
1723 			xpt_schedule(periph, priority);
1724 			goto out;
1725 		}
1726 		if (softc->action == PROBE_INQUIRY_BASIC_DV2) {
1727 			CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1728 			    ("Leave Domain Validation Successfully\n"));
1729 		}
1730 		if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1731 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1732 			xpt_acquire_device(path->device);
1733 		}
1734 		path->device->flags &=
1735 		    ~(CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1736 		if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1737 			/* Inform the XPT that a new device has been found */
1738 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1739 			xpt_action(done_ccb);
1740 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1741 				  done_ccb);
1742 		}
1743 		PROBE_SET_ACTION(softc, PROBE_DONE);
1744 		xpt_release_ccb(done_ccb);
1745 		break;
1746 	}
1747 	default:
1748 		panic("probedone: invalid action state 0x%x\n", softc->action);
1749 	}
1750 	done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
1751 	TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
1752 	done_ccb->ccb_h.status = CAM_REQ_CMP;
1753 	xpt_done(done_ccb);
1754 	if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
1755 		CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
1756 		/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1757 		cam_release_devq(path, 0, 0, 0, FALSE);
1758 		cam_periph_release_locked(periph);
1759 		cam_periph_invalidate(periph);
1760 		cam_periph_release_locked(periph);
1761 	} else {
1762 		probeschedule(periph);
1763 		goto out;
1764 	}
1765 }
1766 
1767 static void
1768 probe_purge_old(struct cam_path *path, struct scsi_report_luns_data *new,
1769     probe_flags flags)
1770 {
1771 	struct cam_path *tp;
1772 	struct scsi_report_luns_data *old;
1773 	u_int idx1, idx2, nlun_old, nlun_new;
1774 	lun_id_t this_lun;
1775 	u_int8_t *ol, *nl;
1776 
1777 	if (path->target == NULL) {
1778 		return;
1779 	}
1780 	mtx_lock(&path->target->luns_mtx);
1781 	old = path->target->luns;
1782 	path->target->luns = new;
1783 	mtx_unlock(&path->target->luns_mtx);
1784 	if (old == NULL)
1785 		return;
1786 	nlun_old = scsi_4btoul(old->length) / 8;
1787 	nlun_new = scsi_4btoul(new->length) / 8;
1788 
1789 	/*
1790 	 * We are not going to assume sorted lists. Deal.
1791 	 */
1792 	for (idx1 = 0; idx1 < nlun_old; idx1++) {
1793 		ol = old->luns[idx1].lundata;
1794 		for (idx2 = 0; idx2 < nlun_new; idx2++) {
1795 			nl = new->luns[idx2].lundata;
1796 			if (memcmp(nl, ol, 8) == 0) {
1797 				break;
1798 			}
1799 		}
1800 		if (idx2 < nlun_new) {
1801 			continue;
1802 		}
1803 		/*
1804 		 * An 'old' item not in the 'new' list.
1805 		 * Nuke it. Except that if it is lun 0,
1806 		 * that would be what the probe state
1807 		 * machine is currently working on,
1808 		 * so we won't do that.
1809 		 */
1810 		CAM_GET_LUN(old, idx1, this_lun);
1811 		if (this_lun == 0) {
1812 			continue;
1813 		}
1814 
1815 		/*
1816 		 * We also cannot nuke it if it is
1817 		 * not in a lun format we understand
1818 		 * and replace the LUN with a "simple" LUN
1819 		 * if that is all the HBA supports.
1820 		 */
1821 		if (!(flags & PROBE_EXTLUN)) {
1822 			if (!CAM_CAN_GET_SIMPLE_LUN(old, idx1))
1823 				continue;
1824 			CAM_GET_SIMPLE_LUN(old, idx1, this_lun);
1825 		}
1826 
1827 		if (xpt_create_path(&tp, NULL, xpt_path_path_id(path),
1828 		    xpt_path_target_id(path), this_lun) == CAM_REQ_CMP) {
1829 			xpt_async(AC_LOST_DEVICE, tp, NULL);
1830 			xpt_free_path(tp);
1831 		}
1832 	}
1833 	free(old, M_CAMXPT);
1834 }
1835 
1836 static void
1837 probecleanup(struct cam_periph *periph)
1838 {
1839 	free(periph->softc, M_CAMXPT);
1840 }
1841 
1842 static void
1843 scsi_find_quirk(struct cam_ed *device)
1844 {
1845 	struct scsi_quirk_entry *quirk;
1846 	caddr_t	match;
1847 
1848 	match = cam_quirkmatch((caddr_t)&device->inq_data,
1849 			       (caddr_t)scsi_quirk_table,
1850 			       sizeof(scsi_quirk_table) /
1851 			       sizeof(*scsi_quirk_table),
1852 			       sizeof(*scsi_quirk_table), scsi_inquiry_match);
1853 
1854 	if (match == NULL)
1855 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
1856 
1857 	quirk = (struct scsi_quirk_entry *)match;
1858 	device->quirk = quirk;
1859 	device->mintags = quirk->mintags;
1860 	device->maxtags = quirk->maxtags;
1861 }
1862 
1863 static int
1864 sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS)
1865 {
1866 	int error, val;
1867 
1868 	val = cam_srch_hi;
1869 	error = sysctl_handle_int(oidp, &val, 0, req);
1870 	if (error != 0 || req->newptr == NULL)
1871 		return (error);
1872 	if (val == 0 || val == 1) {
1873 		cam_srch_hi = val;
1874 		return (0);
1875 	} else {
1876 		return (EINVAL);
1877 	}
1878 }
1879 
1880 typedef struct {
1881 	union	ccb *request_ccb;
1882 	struct 	ccb_pathinq *cpi;
1883 	int	counter;
1884 	int	lunindex[0];
1885 } scsi_scan_bus_info;
1886 
1887 /*
1888  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1889  * As the scan progresses, scsi_scan_bus is used as the
1890  * callback on completion function.
1891  */
1892 static void
1893 scsi_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1894 {
1895 	struct mtx *mtx;
1896 
1897 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1898 		  ("scsi_scan_bus\n"));
1899 	switch (request_ccb->ccb_h.func_code) {
1900 	case XPT_SCAN_BUS:
1901 	case XPT_SCAN_TGT:
1902 	{
1903 		scsi_scan_bus_info *scan_info;
1904 		union	ccb *work_ccb, *reset_ccb;
1905 		struct	cam_path *path;
1906 		u_int	i;
1907 		u_int	low_target, max_target;
1908 		u_int	initiator_id;
1909 
1910 		/* Find out the characteristics of the bus */
1911 		work_ccb = xpt_alloc_ccb_nowait();
1912 		if (work_ccb == NULL) {
1913 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1914 			xpt_done(request_ccb);
1915 			return;
1916 		}
1917 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1918 			      request_ccb->ccb_h.pinfo.priority);
1919 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1920 		xpt_action(work_ccb);
1921 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1922 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1923 			xpt_free_ccb(work_ccb);
1924 			xpt_done(request_ccb);
1925 			return;
1926 		}
1927 
1928 		if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
1929 			/*
1930 			 * Can't scan the bus on an adapter that
1931 			 * cannot perform the initiator role.
1932 			 */
1933 			request_ccb->ccb_h.status = CAM_REQ_CMP;
1934 			xpt_free_ccb(work_ccb);
1935 			xpt_done(request_ccb);
1936 			return;
1937 		}
1938 
1939 		/* We may need to reset bus first, if we haven't done it yet. */
1940 		if ((work_ccb->cpi.hba_inquiry &
1941 		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1942 		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1943 		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset) &&
1944 		    (reset_ccb = xpt_alloc_ccb_nowait()) != NULL) {
1945 			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1946 			      CAM_PRIORITY_NONE);
1947 			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1948 			xpt_action(reset_ccb);
1949 			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1950 				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1951 				xpt_free_ccb(reset_ccb);
1952 				xpt_free_ccb(work_ccb);
1953 				xpt_done(request_ccb);
1954 				return;
1955 			}
1956 			xpt_free_ccb(reset_ccb);
1957 		}
1958 
1959 		/* Save some state for use while we probe for devices */
1960 		scan_info = (scsi_scan_bus_info *) malloc(sizeof(scsi_scan_bus_info) +
1961 		    (work_ccb->cpi.max_target * sizeof (u_int)), M_CAMXPT, M_ZERO|M_NOWAIT);
1962 		if (scan_info == NULL) {
1963 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1964 			xpt_free_ccb(work_ccb);
1965 			xpt_done(request_ccb);
1966 			return;
1967 		}
1968 		CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1969 		   ("SCAN start for %p\n", scan_info));
1970 		scan_info->request_ccb = request_ccb;
1971 		scan_info->cpi = &work_ccb->cpi;
1972 
1973 		/* Cache on our stack so we can work asynchronously */
1974 		max_target = scan_info->cpi->max_target;
1975 		low_target = 0;
1976 		initiator_id = scan_info->cpi->initiator_id;
1977 
1978 
1979 		/*
1980 		 * We can scan all targets in parallel, or do it sequentially.
1981 		 */
1982 
1983 		if (request_ccb->ccb_h.func_code == XPT_SCAN_TGT) {
1984 			max_target = low_target = request_ccb->ccb_h.target_id;
1985 			scan_info->counter = 0;
1986 		} else if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
1987 			max_target = 0;
1988 			scan_info->counter = 0;
1989 		} else {
1990 			scan_info->counter = scan_info->cpi->max_target + 1;
1991 			if (scan_info->cpi->initiator_id < scan_info->counter) {
1992 				scan_info->counter--;
1993 			}
1994 		}
1995 		mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
1996 		mtx_unlock(mtx);
1997 
1998 		for (i = low_target; i <= max_target; i++) {
1999 			cam_status status;
2000 			if (i == initiator_id)
2001 				continue;
2002 
2003 			status = xpt_create_path(&path, NULL,
2004 						 request_ccb->ccb_h.path_id,
2005 						 i, 0);
2006 			if (status != CAM_REQ_CMP) {
2007 				printf("scsi_scan_bus: xpt_create_path failed"
2008 				       " with status %#x, bus scan halted\n",
2009 				       status);
2010 				free(scan_info, M_CAMXPT);
2011 				request_ccb->ccb_h.status = status;
2012 				xpt_free_ccb(work_ccb);
2013 				xpt_done(request_ccb);
2014 				break;
2015 			}
2016 			work_ccb = xpt_alloc_ccb_nowait();
2017 			if (work_ccb == NULL) {
2018 				xpt_free_ccb((union ccb *)scan_info->cpi);
2019 				free(scan_info, M_CAMXPT);
2020 				xpt_free_path(path);
2021 				request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2022 				xpt_done(request_ccb);
2023 				break;
2024 			}
2025 			xpt_setup_ccb(&work_ccb->ccb_h, path,
2026 				      request_ccb->ccb_h.pinfo.priority);
2027 			work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2028 			work_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2029 			work_ccb->ccb_h.flags |= CAM_UNLOCKED;
2030 			work_ccb->ccb_h.ppriv_ptr0 = scan_info;
2031 			work_ccb->crcn.flags = request_ccb->crcn.flags;
2032 			xpt_action(work_ccb);
2033 		}
2034 
2035 		mtx_lock(mtx);
2036 		break;
2037 	}
2038 	case XPT_SCAN_LUN:
2039 	{
2040 		cam_status status;
2041 		struct cam_path *path, *oldpath;
2042 		scsi_scan_bus_info *scan_info;
2043 		struct cam_et *target;
2044 		struct cam_ed *device, *nextdev;
2045 		int next_target;
2046 		path_id_t path_id;
2047 		target_id_t target_id;
2048 		lun_id_t lun_id;
2049 
2050 		oldpath = request_ccb->ccb_h.path;
2051 
2052 		status = cam_ccb_status(request_ccb);
2053 		scan_info = (scsi_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
2054 		path_id = request_ccb->ccb_h.path_id;
2055 		target_id = request_ccb->ccb_h.target_id;
2056 		lun_id = request_ccb->ccb_h.target_lun;
2057 		target = request_ccb->ccb_h.path->target;
2058 		next_target = 1;
2059 
2060 		mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
2061 		mtx_lock(mtx);
2062 		mtx_lock(&target->luns_mtx);
2063 		if (target->luns) {
2064 			lun_id_t first;
2065 			u_int nluns = scsi_4btoul(target->luns->length) / 8;
2066 
2067 			/*
2068 			 * Make sure we skip over lun 0 if it's the first member
2069 			 * of the list as we've actually just finished probing
2070 			 * it.
2071 			 */
2072 			CAM_GET_LUN(target->luns, 0, first);
2073 			if (first == 0 && scan_info->lunindex[target_id] == 0) {
2074 				scan_info->lunindex[target_id]++;
2075 			}
2076 
2077 			/*
2078 			 * Skip any LUNs that the HBA can't deal with.
2079 			 */
2080 			while (scan_info->lunindex[target_id] < nluns) {
2081 				if (scan_info->cpi->hba_misc & PIM_EXTLUNS) {
2082 					CAM_GET_LUN(target->luns,
2083 					    scan_info->lunindex[target_id],
2084 					    lun_id);
2085 					break;
2086 				}
2087 
2088 				if (CAM_CAN_GET_SIMPLE_LUN(target->luns,
2089 				    scan_info->lunindex[target_id])) {
2090 					CAM_GET_SIMPLE_LUN(target->luns,
2091 					    scan_info->lunindex[target_id],
2092 					    lun_id);
2093 					break;
2094 				}
2095 
2096 				scan_info->lunindex[target_id]++;
2097 			}
2098 
2099 			if (scan_info->lunindex[target_id] < nluns) {
2100 				mtx_unlock(&target->luns_mtx);
2101 				next_target = 0;
2102 				CAM_DEBUG(request_ccb->ccb_h.path,
2103 				    CAM_DEBUG_PROBE,
2104 				   ("next lun to try at index %u is %jx\n",
2105 				   scan_info->lunindex[target_id],
2106 				   (uintmax_t)lun_id));
2107 				scan_info->lunindex[target_id]++;
2108 			} else {
2109 				mtx_unlock(&target->luns_mtx);
2110 				/* We're done with scanning all luns. */
2111 			}
2112 		} else {
2113 			mtx_unlock(&target->luns_mtx);
2114 			device = request_ccb->ccb_h.path->device;
2115 			/* Continue sequential LUN scan if: */
2116 			/*  -- we have more LUNs that need recheck */
2117 			mtx_lock(&target->bus->eb_mtx);
2118 			nextdev = device;
2119 			while ((nextdev = TAILQ_NEXT(nextdev, links)) != NULL)
2120 				if ((nextdev->flags & CAM_DEV_UNCONFIGURED) == 0)
2121 					break;
2122 			mtx_unlock(&target->bus->eb_mtx);
2123 			if (nextdev != NULL) {
2124 				next_target = 0;
2125 			/*  -- stop if CAM_QUIRK_NOLUNS is set. */
2126 			} else if (SCSI_QUIRK(device)->quirks & CAM_QUIRK_NOLUNS) {
2127 				next_target = 1;
2128 			/*  -- this LUN is connected and its SCSI version
2129 			 *     allows more LUNs. */
2130 			} else if ((device->flags & CAM_DEV_UNCONFIGURED) == 0) {
2131 				if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
2132 				    CAN_SRCH_HI_DENSE(device))
2133 					next_target = 0;
2134 			/*  -- this LUN is disconnected, its SCSI version
2135 			 *     allows more LUNs and we guess they may be. */
2136 			} else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0) {
2137 				if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
2138 				    CAN_SRCH_HI_SPARSE(device))
2139 					next_target = 0;
2140 			}
2141 			if (next_target == 0) {
2142 				lun_id++;
2143 				if (lun_id > scan_info->cpi->max_lun)
2144 					next_target = 1;
2145 			}
2146 		}
2147 
2148 		/*
2149 		 * Check to see if we scan any further luns.
2150 		 */
2151 		if (next_target) {
2152 			int done;
2153 
2154 			/*
2155 			 * Free the current request path- we're done with it.
2156 			 */
2157 			xpt_free_path(oldpath);
2158  hop_again:
2159 			done = 0;
2160 			if (scan_info->request_ccb->ccb_h.func_code == XPT_SCAN_TGT) {
2161 				done = 1;
2162 			} else if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
2163 				scan_info->counter++;
2164 				if (scan_info->counter ==
2165 				    scan_info->cpi->initiator_id) {
2166 					scan_info->counter++;
2167 				}
2168 				if (scan_info->counter >=
2169 				    scan_info->cpi->max_target+1) {
2170 					done = 1;
2171 				}
2172 			} else {
2173 				scan_info->counter--;
2174 				if (scan_info->counter == 0) {
2175 					done = 1;
2176 				}
2177 			}
2178 			if (done) {
2179 				mtx_unlock(mtx);
2180 				xpt_free_ccb(request_ccb);
2181 				xpt_free_ccb((union ccb *)scan_info->cpi);
2182 				request_ccb = scan_info->request_ccb;
2183 				CAM_DEBUG(request_ccb->ccb_h.path,
2184 				    CAM_DEBUG_TRACE,
2185 				   ("SCAN done for %p\n", scan_info));
2186 				free(scan_info, M_CAMXPT);
2187 				request_ccb->ccb_h.status = CAM_REQ_CMP;
2188 				xpt_done(request_ccb);
2189 				break;
2190 			}
2191 
2192 			if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
2193 				mtx_unlock(mtx);
2194 				xpt_free_ccb(request_ccb);
2195 				break;
2196 			}
2197 			status = xpt_create_path(&path, NULL,
2198 			    scan_info->request_ccb->ccb_h.path_id,
2199 			    scan_info->counter, 0);
2200 			if (status != CAM_REQ_CMP) {
2201 				mtx_unlock(mtx);
2202 				printf("scsi_scan_bus: xpt_create_path failed"
2203 				    " with status %#x, bus scan halted\n",
2204 			       	    status);
2205 				xpt_free_ccb(request_ccb);
2206 				xpt_free_ccb((union ccb *)scan_info->cpi);
2207 				request_ccb = scan_info->request_ccb;
2208 				free(scan_info, M_CAMXPT);
2209 				request_ccb->ccb_h.status = status;
2210 				xpt_done(request_ccb);
2211 				break;
2212 			}
2213 			xpt_setup_ccb(&request_ccb->ccb_h, path,
2214 			    request_ccb->ccb_h.pinfo.priority);
2215 			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2216 			request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2217 			request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2218 			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
2219 			request_ccb->crcn.flags =
2220 			    scan_info->request_ccb->crcn.flags;
2221 		} else {
2222 			status = xpt_create_path(&path, NULL,
2223 						 path_id, target_id, lun_id);
2224 			/*
2225 			 * Free the old request path- we're done with it. We
2226 			 * do this *after* creating the new path so that
2227 			 * we don't remove a target that has our lun list
2228 			 * in the case that lun 0 is not present.
2229 			 */
2230 			xpt_free_path(oldpath);
2231 			if (status != CAM_REQ_CMP) {
2232 				printf("scsi_scan_bus: xpt_create_path failed "
2233 				       "with status %#x, halting LUN scan\n",
2234 			 	       status);
2235 				goto hop_again;
2236 			}
2237 			xpt_setup_ccb(&request_ccb->ccb_h, path,
2238 				      request_ccb->ccb_h.pinfo.priority);
2239 			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2240 			request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2241 			request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2242 			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
2243 			request_ccb->crcn.flags =
2244 				scan_info->request_ccb->crcn.flags;
2245 		}
2246 		mtx_unlock(mtx);
2247 		xpt_action(request_ccb);
2248 		break;
2249 	}
2250 	default:
2251 		break;
2252 	}
2253 }
2254 
2255 static void
2256 scsi_scan_lun(struct cam_periph *periph, struct cam_path *path,
2257 	     cam_flags flags, union ccb *request_ccb)
2258 {
2259 	struct ccb_pathinq cpi;
2260 	cam_status status;
2261 	struct cam_path *new_path;
2262 	struct cam_periph *old_periph;
2263 	int lock;
2264 
2265 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("scsi_scan_lun\n"));
2266 
2267 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2268 	cpi.ccb_h.func_code = XPT_PATH_INQ;
2269 	xpt_action((union ccb *)&cpi);
2270 
2271 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
2272 		if (request_ccb != NULL) {
2273 			request_ccb->ccb_h.status = cpi.ccb_h.status;
2274 			xpt_done(request_ccb);
2275 		}
2276 		return;
2277 	}
2278 
2279 	if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
2280 		/*
2281 		 * Can't scan the bus on an adapter that
2282 		 * cannot perform the initiator role.
2283 		 */
2284 		if (request_ccb != NULL) {
2285 			request_ccb->ccb_h.status = CAM_REQ_CMP;
2286 			xpt_done(request_ccb);
2287 		}
2288 		return;
2289 	}
2290 
2291 	if (request_ccb == NULL) {
2292 		request_ccb = xpt_alloc_ccb_nowait();
2293 		if (request_ccb == NULL) {
2294 			xpt_print(path, "scsi_scan_lun: can't allocate CCB, "
2295 			    "can't continue\n");
2296 			return;
2297 		}
2298 		status = xpt_create_path(&new_path, NULL,
2299 					  path->bus->path_id,
2300 					  path->target->target_id,
2301 					  path->device->lun_id);
2302 		if (status != CAM_REQ_CMP) {
2303 			xpt_print(path, "scsi_scan_lun: can't create path, "
2304 			    "can't continue\n");
2305 			xpt_free_ccb(request_ccb);
2306 			return;
2307 		}
2308 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
2309 		request_ccb->ccb_h.cbfcnp = xptscandone;
2310 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2311 		request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2312 		request_ccb->crcn.flags = flags;
2313 	}
2314 
2315 	lock = (xpt_path_owned(path) == 0);
2316 	if (lock)
2317 		xpt_path_lock(path);
2318 	if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
2319 		if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
2320 			probe_softc *softc;
2321 
2322 			softc = (probe_softc *)old_periph->softc;
2323 			TAILQ_INSERT_TAIL(&softc->request_ccbs,
2324 			    &request_ccb->ccb_h, periph_links.tqe);
2325 		} else {
2326 			request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2327 			xpt_done(request_ccb);
2328 		}
2329 	} else {
2330 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
2331 					  probestart, "probe",
2332 					  CAM_PERIPH_BIO,
2333 					  request_ccb->ccb_h.path, NULL, 0,
2334 					  request_ccb);
2335 
2336 		if (status != CAM_REQ_CMP) {
2337 			xpt_print(path, "scsi_scan_lun: cam_alloc_periph "
2338 			    "returned an error, can't continue probe\n");
2339 			request_ccb->ccb_h.status = status;
2340 			xpt_done(request_ccb);
2341 		}
2342 	}
2343 	if (lock)
2344 		xpt_path_unlock(path);
2345 }
2346 
2347 static void
2348 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
2349 {
2350 
2351 	xpt_free_path(done_ccb->ccb_h.path);
2352 	xpt_free_ccb(done_ccb);
2353 }
2354 
2355 static struct cam_ed *
2356 scsi_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
2357 {
2358 	struct scsi_quirk_entry *quirk;
2359 	struct cam_ed *device;
2360 
2361 	device = xpt_alloc_device(bus, target, lun_id);
2362 	if (device == NULL)
2363 		return (NULL);
2364 
2365 	/*
2366 	 * Take the default quirk entry until we have inquiry
2367 	 * data and can determine a better quirk to use.
2368 	 */
2369 	quirk = &scsi_quirk_table[scsi_quirk_table_size - 1];
2370 	device->quirk = (void *)quirk;
2371 	device->mintags = quirk->mintags;
2372 	device->maxtags = quirk->maxtags;
2373 	bzero(&device->inq_data, sizeof(device->inq_data));
2374 	device->inq_flags = 0;
2375 	device->queue_flags = 0;
2376 	device->serial_num = NULL;
2377 	device->serial_num_len = 0;
2378 	device->device_id = NULL;
2379 	device->device_id_len = 0;
2380 	device->supported_vpds = NULL;
2381 	device->supported_vpds_len = 0;
2382 	return (device);
2383 }
2384 
2385 static void
2386 scsi_devise_transport(struct cam_path *path)
2387 {
2388 	struct ccb_pathinq cpi;
2389 	struct ccb_trans_settings cts;
2390 	struct scsi_inquiry_data *inq_buf;
2391 
2392 	/* Get transport information from the SIM */
2393 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2394 	cpi.ccb_h.func_code = XPT_PATH_INQ;
2395 	xpt_action((union ccb *)&cpi);
2396 
2397 	inq_buf = NULL;
2398 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
2399 		inq_buf = &path->device->inq_data;
2400 	path->device->protocol = PROTO_SCSI;
2401 	path->device->protocol_version =
2402 	    inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
2403 	path->device->transport = cpi.transport;
2404 	path->device->transport_version = cpi.transport_version;
2405 
2406 	/*
2407 	 * Any device not using SPI3 features should
2408 	 * be considered SPI2 or lower.
2409 	 */
2410 	if (inq_buf != NULL) {
2411 		if (path->device->transport == XPORT_SPI
2412 		 && (inq_buf->spi3data & SID_SPI_MASK) == 0
2413 		 && path->device->transport_version > 2)
2414 			path->device->transport_version = 2;
2415 	} else {
2416 		struct cam_ed* otherdev;
2417 
2418 		for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
2419 		     otherdev != NULL;
2420 		     otherdev = TAILQ_NEXT(otherdev, links)) {
2421 			if (otherdev != path->device)
2422 				break;
2423 		}
2424 
2425 		if (otherdev != NULL) {
2426 			/*
2427 			 * Initially assume the same versioning as
2428 			 * prior luns for this target.
2429 			 */
2430 			path->device->protocol_version =
2431 			    otherdev->protocol_version;
2432 			path->device->transport_version =
2433 			    otherdev->transport_version;
2434 		} else {
2435 			/* Until we know better, opt for safty */
2436 			path->device->protocol_version = 2;
2437 			if (path->device->transport == XPORT_SPI)
2438 				path->device->transport_version = 2;
2439 			else
2440 				path->device->transport_version = 0;
2441 		}
2442 	}
2443 
2444 	/*
2445 	 * XXX
2446 	 * For a device compliant with SPC-2 we should be able
2447 	 * to determine the transport version supported by
2448 	 * scrutinizing the version descriptors in the
2449 	 * inquiry buffer.
2450 	 */
2451 
2452 	/* Tell the controller what we think */
2453 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2454 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
2455 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
2456 	cts.transport = path->device->transport;
2457 	cts.transport_version = path->device->transport_version;
2458 	cts.protocol = path->device->protocol;
2459 	cts.protocol_version = path->device->protocol_version;
2460 	cts.proto_specific.valid = 0;
2461 	cts.xport_specific.valid = 0;
2462 	xpt_action((union ccb *)&cts);
2463 }
2464 
2465 static void
2466 scsi_dev_advinfo(union ccb *start_ccb)
2467 {
2468 	struct cam_ed *device;
2469 	struct ccb_dev_advinfo *cdai;
2470 	off_t amt;
2471 
2472 	start_ccb->ccb_h.status = CAM_REQ_INVALID;
2473 	device = start_ccb->ccb_h.path->device;
2474 	cdai = &start_ccb->cdai;
2475 	switch(cdai->buftype) {
2476 	case CDAI_TYPE_SCSI_DEVID:
2477 		if (cdai->flags & CDAI_FLAG_STORE)
2478 			return;
2479 		cdai->provsiz = device->device_id_len;
2480 		if (device->device_id_len == 0)
2481 			break;
2482 		amt = device->device_id_len;
2483 		if (cdai->provsiz > cdai->bufsiz)
2484 			amt = cdai->bufsiz;
2485 		memcpy(cdai->buf, device->device_id, amt);
2486 		break;
2487 	case CDAI_TYPE_SERIAL_NUM:
2488 		if (cdai->flags & CDAI_FLAG_STORE)
2489 			return;
2490 		cdai->provsiz = device->serial_num_len;
2491 		if (device->serial_num_len == 0)
2492 			break;
2493 		amt = device->serial_num_len;
2494 		if (cdai->provsiz > cdai->bufsiz)
2495 			amt = cdai->bufsiz;
2496 		memcpy(cdai->buf, device->serial_num, amt);
2497 		break;
2498 	case CDAI_TYPE_PHYS_PATH:
2499 		if (cdai->flags & CDAI_FLAG_STORE) {
2500 			if (device->physpath != NULL) {
2501 				free(device->physpath, M_CAMXPT);
2502 				device->physpath = NULL;
2503 			}
2504 			device->physpath_len = cdai->bufsiz;
2505 			/* Clear existing buffer if zero length */
2506 			if (cdai->bufsiz == 0)
2507 				break;
2508 			device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
2509 			if (device->physpath == NULL) {
2510 				start_ccb->ccb_h.status = CAM_REQ_ABORTED;
2511 				return;
2512 			}
2513 			memcpy(device->physpath, cdai->buf, cdai->bufsiz);
2514 		} else {
2515 			cdai->provsiz = device->physpath_len;
2516 			if (device->physpath_len == 0)
2517 				break;
2518 			amt = device->physpath_len;
2519 			if (cdai->provsiz > cdai->bufsiz)
2520 				amt = cdai->bufsiz;
2521 			memcpy(cdai->buf, device->physpath, amt);
2522 		}
2523 		break;
2524 	case CDAI_TYPE_RCAPLONG:
2525 		if (cdai->flags & CDAI_FLAG_STORE) {
2526 			if (device->rcap_buf != NULL) {
2527 				free(device->rcap_buf, M_CAMXPT);
2528 				device->rcap_buf = NULL;
2529 			}
2530 
2531 			device->rcap_len = cdai->bufsiz;
2532 			/* Clear existing buffer if zero length */
2533 			if (cdai->bufsiz == 0)
2534 				break;
2535 
2536 			device->rcap_buf = malloc(cdai->bufsiz, M_CAMXPT,
2537 						  M_NOWAIT);
2538 			if (device->rcap_buf == NULL) {
2539 				start_ccb->ccb_h.status = CAM_REQ_ABORTED;
2540 				return;
2541 			}
2542 
2543 			memcpy(device->rcap_buf, cdai->buf, cdai->bufsiz);
2544 		} else {
2545 			cdai->provsiz = device->rcap_len;
2546 			if (device->rcap_len == 0)
2547 				break;
2548 			amt = device->rcap_len;
2549 			if (cdai->provsiz > cdai->bufsiz)
2550 				amt = cdai->bufsiz;
2551 			memcpy(cdai->buf, device->rcap_buf, amt);
2552 		}
2553 		break;
2554 	case CDAI_TYPE_EXT_INQ:
2555 		/*
2556 		 * We fetch extended inquiry data during probe, if
2557 		 * available.  We don't allow changing it.
2558 		 */
2559 		if (cdai->flags & CDAI_FLAG_STORE)
2560 			return;
2561 		cdai->provsiz = device->ext_inq_len;
2562 		if (device->ext_inq_len == 0)
2563 			break;
2564 		amt = device->ext_inq_len;
2565 		if (cdai->provsiz > cdai->bufsiz)
2566 			amt = cdai->bufsiz;
2567 		memcpy(cdai->buf, device->ext_inq, amt);
2568 		break;
2569 	default:
2570 		return;
2571 	}
2572 	start_ccb->ccb_h.status = CAM_REQ_CMP;
2573 
2574 	if (cdai->flags & CDAI_FLAG_STORE) {
2575 		xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
2576 			  (void *)(uintptr_t)cdai->buftype);
2577 	}
2578 }
2579 
2580 static void
2581 scsi_action(union ccb *start_ccb)
2582 {
2583 
2584 	switch (start_ccb->ccb_h.func_code) {
2585 	case XPT_SET_TRAN_SETTINGS:
2586 	{
2587 		scsi_set_transfer_settings(&start_ccb->cts,
2588 					   start_ccb->ccb_h.path,
2589 					   /*async_update*/FALSE);
2590 		break;
2591 	}
2592 	case XPT_SCAN_BUS:
2593 	case XPT_SCAN_TGT:
2594 		scsi_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
2595 		break;
2596 	case XPT_SCAN_LUN:
2597 		scsi_scan_lun(start_ccb->ccb_h.path->periph,
2598 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
2599 			      start_ccb);
2600 		break;
2601 	case XPT_DEV_ADVINFO:
2602 	{
2603 		scsi_dev_advinfo(start_ccb);
2604 		break;
2605 	}
2606 	default:
2607 		xpt_action_default(start_ccb);
2608 		break;
2609 	}
2610 }
2611 
2612 static void
2613 scsi_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_path *path,
2614 			   int async_update)
2615 {
2616 	struct	ccb_pathinq cpi;
2617 	struct	ccb_trans_settings cur_cts;
2618 	struct	ccb_trans_settings_scsi *scsi;
2619 	struct	ccb_trans_settings_scsi *cur_scsi;
2620 	struct	scsi_inquiry_data *inq_data;
2621 	struct	cam_ed *device;
2622 
2623 	if (path == NULL || (device = path->device) == NULL) {
2624 		cts->ccb_h.status = CAM_PATH_INVALID;
2625 		xpt_done((union ccb *)cts);
2626 		return;
2627 	}
2628 
2629 	if (cts->protocol == PROTO_UNKNOWN
2630 	 || cts->protocol == PROTO_UNSPECIFIED) {
2631 		cts->protocol = device->protocol;
2632 		cts->protocol_version = device->protocol_version;
2633 	}
2634 
2635 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
2636 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
2637 		cts->protocol_version = device->protocol_version;
2638 
2639 	if (cts->protocol != device->protocol) {
2640 		xpt_print(path, "Uninitialized Protocol %x:%x?\n",
2641 		       cts->protocol, device->protocol);
2642 		cts->protocol = device->protocol;
2643 	}
2644 
2645 	if (cts->protocol_version > device->protocol_version) {
2646 		if (bootverbose) {
2647 			xpt_print(path, "Down reving Protocol "
2648 			    "Version from %d to %d?\n", cts->protocol_version,
2649 			    device->protocol_version);
2650 		}
2651 		cts->protocol_version = device->protocol_version;
2652 	}
2653 
2654 	if (cts->transport == XPORT_UNKNOWN
2655 	 || cts->transport == XPORT_UNSPECIFIED) {
2656 		cts->transport = device->transport;
2657 		cts->transport_version = device->transport_version;
2658 	}
2659 
2660 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
2661 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
2662 		cts->transport_version = device->transport_version;
2663 
2664 	if (cts->transport != device->transport) {
2665 		xpt_print(path, "Uninitialized Transport %x:%x?\n",
2666 		    cts->transport, device->transport);
2667 		cts->transport = device->transport;
2668 	}
2669 
2670 	if (cts->transport_version > device->transport_version) {
2671 		if (bootverbose) {
2672 			xpt_print(path, "Down reving Transport "
2673 			    "Version from %d to %d?\n", cts->transport_version,
2674 			    device->transport_version);
2675 		}
2676 		cts->transport_version = device->transport_version;
2677 	}
2678 
2679 	/*
2680 	 * Nothing more of interest to do unless
2681 	 * this is a device connected via the
2682 	 * SCSI protocol.
2683 	 */
2684 	if (cts->protocol != PROTO_SCSI) {
2685 		if (async_update == FALSE)
2686 			xpt_action_default((union ccb *)cts);
2687 		return;
2688 	}
2689 
2690 	inq_data = &device->inq_data;
2691 	scsi = &cts->proto_specific.scsi;
2692 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2693 	cpi.ccb_h.func_code = XPT_PATH_INQ;
2694 	xpt_action((union ccb *)&cpi);
2695 
2696 	/* SCSI specific sanity checking */
2697 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
2698 	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
2699 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
2700 	 || (device->mintags == 0)) {
2701 		/*
2702 		 * Can't tag on hardware that doesn't support tags,
2703 		 * doesn't have it enabled, or has broken tag support.
2704 		 */
2705 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2706 	}
2707 
2708 	if (async_update == FALSE) {
2709 		/*
2710 		 * Perform sanity checking against what the
2711 		 * controller and device can do.
2712 		 */
2713 		xpt_setup_ccb(&cur_cts.ccb_h, path, CAM_PRIORITY_NONE);
2714 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2715 		cur_cts.type = cts->type;
2716 		xpt_action((union ccb *)&cur_cts);
2717 		if (cam_ccb_status((union ccb *)&cur_cts) != CAM_REQ_CMP) {
2718 			return;
2719 		}
2720 		cur_scsi = &cur_cts.proto_specific.scsi;
2721 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
2722 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2723 			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
2724 		}
2725 		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
2726 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2727 	}
2728 
2729 	/* SPI specific sanity checking */
2730 	if (cts->transport == XPORT_SPI && async_update == FALSE) {
2731 		u_int spi3caps;
2732 		struct ccb_trans_settings_spi *spi;
2733 		struct ccb_trans_settings_spi *cur_spi;
2734 
2735 		spi = &cts->xport_specific.spi;
2736 
2737 		cur_spi = &cur_cts.xport_specific.spi;
2738 
2739 		/* Fill in any gaps in what the user gave us */
2740 		if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2741 			spi->sync_period = cur_spi->sync_period;
2742 		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2743 			spi->sync_period = 0;
2744 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2745 			spi->sync_offset = cur_spi->sync_offset;
2746 		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2747 			spi->sync_offset = 0;
2748 		if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2749 			spi->ppr_options = cur_spi->ppr_options;
2750 		if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2751 			spi->ppr_options = 0;
2752 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2753 			spi->bus_width = cur_spi->bus_width;
2754 		if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2755 			spi->bus_width = 0;
2756 		if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
2757 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2758 			spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
2759 		}
2760 		if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
2761 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2762 		if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2763 		  && (inq_data->flags & SID_Sync) == 0
2764 		  && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2765 		 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)) {
2766 			/* Force async */
2767 			spi->sync_period = 0;
2768 			spi->sync_offset = 0;
2769 		}
2770 
2771 		switch (spi->bus_width) {
2772 		case MSG_EXT_WDTR_BUS_32_BIT:
2773 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2774 			  || (inq_data->flags & SID_WBus32) != 0
2775 			  || cts->type == CTS_TYPE_USER_SETTINGS)
2776 			 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
2777 				break;
2778 			/* Fall Through to 16-bit */
2779 		case MSG_EXT_WDTR_BUS_16_BIT:
2780 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2781 			  || (inq_data->flags & SID_WBus16) != 0
2782 			  || cts->type == CTS_TYPE_USER_SETTINGS)
2783 			 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
2784 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2785 				break;
2786 			}
2787 			/* Fall Through to 8-bit */
2788 		default: /* New bus width?? */
2789 		case MSG_EXT_WDTR_BUS_8_BIT:
2790 			/* All targets can do this */
2791 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2792 			break;
2793 		}
2794 
2795 		spi3caps = cpi.xport_specific.spi.ppr_options;
2796 		if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2797 		 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2798 			spi3caps &= inq_data->spi3data;
2799 
2800 		if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
2801 			spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2802 
2803 		if ((spi3caps & SID_SPI_IUS) == 0)
2804 			spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
2805 
2806 		if ((spi3caps & SID_SPI_QAS) == 0)
2807 			spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
2808 
2809 		/* No SPI Transfer settings are allowed unless we are wide */
2810 		if (spi->bus_width == 0)
2811 			spi->ppr_options = 0;
2812 
2813 		if ((spi->valid & CTS_SPI_VALID_DISC)
2814 		 && ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0)) {
2815 			/*
2816 			 * Can't tag queue without disconnection.
2817 			 */
2818 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2819 			scsi->valid |= CTS_SCSI_VALID_TQ;
2820 		}
2821 
2822 		/*
2823 		 * If we are currently performing tagged transactions to
2824 		 * this device and want to change its negotiation parameters,
2825 		 * go non-tagged for a bit to give the controller a chance to
2826 		 * negotiate unhampered by tag messages.
2827 		 */
2828 		if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2829 		 && (device->inq_flags & SID_CmdQue) != 0
2830 		 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2831 		 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
2832 				   CTS_SPI_VALID_SYNC_OFFSET|
2833 				   CTS_SPI_VALID_BUS_WIDTH)) != 0)
2834 			scsi_toggle_tags(path);
2835 	}
2836 
2837 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2838 	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
2839 		int device_tagenb;
2840 
2841 		/*
2842 		 * If we are transitioning from tags to no-tags or
2843 		 * vice-versa, we need to carefully freeze and restart
2844 		 * the queue so that we don't overlap tagged and non-tagged
2845 		 * commands.  We also temporarily stop tags if there is
2846 		 * a change in transfer negotiation settings to allow
2847 		 * "tag-less" negotiation.
2848 		 */
2849 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2850 		 || (device->inq_flags & SID_CmdQue) != 0)
2851 			device_tagenb = TRUE;
2852 		else
2853 			device_tagenb = FALSE;
2854 
2855 		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2856 		  && device_tagenb == FALSE)
2857 		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
2858 		  && device_tagenb == TRUE)) {
2859 
2860 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
2861 				/*
2862 				 * Delay change to use tags until after a
2863 				 * few commands have gone to this device so
2864 				 * the controller has time to perform transfer
2865 				 * negotiations without tagged messages getting
2866 				 * in the way.
2867 				 */
2868 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
2869 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
2870 			} else {
2871 				xpt_stop_tags(path);
2872 			}
2873 		}
2874 	}
2875 	if (async_update == FALSE)
2876 		xpt_action_default((union ccb *)cts);
2877 }
2878 
2879 static void
2880 scsi_toggle_tags(struct cam_path *path)
2881 {
2882 	struct cam_ed *dev;
2883 
2884 	/*
2885 	 * Give controllers a chance to renegotiate
2886 	 * before starting tag operations.  We
2887 	 * "toggle" tagged queuing off then on
2888 	 * which causes the tag enable command delay
2889 	 * counter to come into effect.
2890 	 */
2891 	dev = path->device;
2892 	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2893 	 || ((dev->inq_flags & SID_CmdQue) != 0
2894  	  && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
2895 		struct ccb_trans_settings cts;
2896 
2897 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2898 		cts.protocol = PROTO_SCSI;
2899 		cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
2900 		cts.transport = XPORT_UNSPECIFIED;
2901 		cts.transport_version = XPORT_VERSION_UNSPECIFIED;
2902 		cts.proto_specific.scsi.flags = 0;
2903 		cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
2904 		scsi_set_transfer_settings(&cts, path,
2905 					  /*async_update*/TRUE);
2906 		cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
2907 		scsi_set_transfer_settings(&cts, path,
2908 					  /*async_update*/TRUE);
2909 	}
2910 }
2911 
2912 /*
2913  * Handle any per-device event notifications that require action by the XPT.
2914  */
2915 static void
2916 scsi_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
2917 	      struct cam_ed *device, void *async_arg)
2918 {
2919 	cam_status status;
2920 	struct cam_path newpath;
2921 
2922 	/*
2923 	 * We only need to handle events for real devices.
2924 	 */
2925 	if (target->target_id == CAM_TARGET_WILDCARD
2926 	 || device->lun_id == CAM_LUN_WILDCARD)
2927 		return;
2928 
2929 	/*
2930 	 * We need our own path with wildcards expanded to
2931 	 * handle certain types of events.
2932 	 */
2933 	if ((async_code == AC_SENT_BDR)
2934 	 || (async_code == AC_BUS_RESET)
2935 	 || (async_code == AC_INQ_CHANGED))
2936 		status = xpt_compile_path(&newpath, NULL,
2937 					  bus->path_id,
2938 					  target->target_id,
2939 					  device->lun_id);
2940 	else
2941 		status = CAM_REQ_CMP_ERR;
2942 
2943 	if (status == CAM_REQ_CMP) {
2944 
2945 		/*
2946 		 * Allow transfer negotiation to occur in a
2947 		 * tag free environment and after settle delay.
2948 		 */
2949 		if (async_code == AC_SENT_BDR
2950 		 || async_code == AC_BUS_RESET) {
2951 			cam_freeze_devq(&newpath);
2952 			cam_release_devq(&newpath,
2953 				RELSIM_RELEASE_AFTER_TIMEOUT,
2954 				/*reduction*/0,
2955 				/*timeout*/scsi_delay,
2956 				/*getcount_only*/0);
2957 			scsi_toggle_tags(&newpath);
2958 		}
2959 
2960 		if (async_code == AC_INQ_CHANGED) {
2961 			/*
2962 			 * We've sent a start unit command, or
2963 			 * something similar to a device that
2964 			 * may have caused its inquiry data to
2965 			 * change. So we re-scan the device to
2966 			 * refresh the inquiry data for it.
2967 			 */
2968 			scsi_scan_lun(newpath.periph, &newpath,
2969 				     CAM_EXPECT_INQ_CHANGE, NULL);
2970 		}
2971 		xpt_release_path(&newpath);
2972 	} else if (async_code == AC_LOST_DEVICE &&
2973 	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
2974 		device->flags |= CAM_DEV_UNCONFIGURED;
2975 		xpt_release_device(device);
2976 	} else if (async_code == AC_TRANSFER_NEG) {
2977 		struct ccb_trans_settings *settings;
2978 		struct cam_path path;
2979 
2980 		settings = (struct ccb_trans_settings *)async_arg;
2981 		xpt_compile_path(&path, NULL, bus->path_id, target->target_id,
2982 				 device->lun_id);
2983 		scsi_set_transfer_settings(settings, &path,
2984 					  /*async_update*/TRUE);
2985 		xpt_release_path(&path);
2986 	}
2987 }
2988 
2989 static void
2990 scsi_announce_periph(struct cam_periph *periph)
2991 {
2992 	struct	ccb_pathinq cpi;
2993 	struct	ccb_trans_settings cts;
2994 	struct	cam_path *path = periph->path;
2995 	u_int	speed;
2996 	u_int	freq;
2997 	u_int	mb;
2998 
2999 	cam_periph_assert(periph, MA_OWNED);
3000 
3001 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
3002 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
3003 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
3004 	xpt_action((union ccb*)&cts);
3005 	if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP)
3006 		return;
3007 	/* Ask the SIM for its base transfer speed */
3008 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3009 	cpi.ccb_h.func_code = XPT_PATH_INQ;
3010 	xpt_action((union ccb *)&cpi);
3011 	/* Report connection speed */
3012 	speed = cpi.base_transfer_speed;
3013 	freq = 0;
3014 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
3015 		struct	ccb_trans_settings_spi *spi =
3016 		    &cts.xport_specific.spi;
3017 
3018 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
3019 		  && spi->sync_offset != 0) {
3020 			freq = scsi_calc_syncsrate(spi->sync_period);
3021 			speed = freq;
3022 		}
3023 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
3024 			speed *= (0x01 << spi->bus_width);
3025 	}
3026 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
3027 		struct	ccb_trans_settings_fc *fc =
3028 		    &cts.xport_specific.fc;
3029 
3030 		if (fc->valid & CTS_FC_VALID_SPEED)
3031 			speed = fc->bitrate;
3032 	}
3033 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SAS) {
3034 		struct	ccb_trans_settings_sas *sas =
3035 		    &cts.xport_specific.sas;
3036 
3037 		if (sas->valid & CTS_SAS_VALID_SPEED)
3038 			speed = sas->bitrate;
3039 	}
3040 	mb = speed / 1000;
3041 	if (mb > 0)
3042 		printf("%s%d: %d.%03dMB/s transfers",
3043 		       periph->periph_name, periph->unit_number,
3044 		       mb, speed % 1000);
3045 	else
3046 		printf("%s%d: %dKB/s transfers", periph->periph_name,
3047 		       periph->unit_number, speed);
3048 	/* Report additional information about SPI connections */
3049 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
3050 		struct	ccb_trans_settings_spi *spi;
3051 
3052 		spi = &cts.xport_specific.spi;
3053 		if (freq != 0) {
3054 			printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
3055 			       freq % 1000,
3056 			       (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
3057 			     ? " DT" : "",
3058 			       spi->sync_offset);
3059 		}
3060 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
3061 		 && spi->bus_width > 0) {
3062 			if (freq != 0) {
3063 				printf(", ");
3064 			} else {
3065 				printf(" (");
3066 			}
3067 			printf("%dbit)", 8 * (0x01 << spi->bus_width));
3068 		} else if (freq != 0) {
3069 			printf(")");
3070 		}
3071 	}
3072 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
3073 		struct	ccb_trans_settings_fc *fc;
3074 
3075 		fc = &cts.xport_specific.fc;
3076 		if (fc->valid & CTS_FC_VALID_WWNN)
3077 			printf(" WWNN 0x%llx", (long long) fc->wwnn);
3078 		if (fc->valid & CTS_FC_VALID_WWPN)
3079 			printf(" WWPN 0x%llx", (long long) fc->wwpn);
3080 		if (fc->valid & CTS_FC_VALID_PORT)
3081 			printf(" PortID 0x%x", fc->port);
3082 	}
3083 	printf("\n");
3084 }
3085 
3086