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