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