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