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