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