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