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