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