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