xref: /freebsd/sys/cam/scsi/scsi_xpt.c (revision e4f6a1bfa31a2299612bbf9dae402bb38d38b1df)
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/interrupt.h>
46 #include <sys/sbuf.h>
47 
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 #include <sys/sysctl.h>
51 
52 #include <cam/cam.h>
53 #include <cam/cam_ccb.h>
54 #include <cam/cam_queue.h>
55 #include <cam/cam_periph.h>
56 #include <cam/cam_sim.h>
57 #include <cam/cam_xpt.h>
58 #include <cam/cam_xpt_sim.h>
59 #include <cam/cam_xpt_periph.h>
60 #include <cam/cam_xpt_internal.h>
61 #include <cam/cam_debug.h>
62 
63 #include <cam/scsi/scsi_all.h>
64 #include <cam/scsi/scsi_message.h>
65 #include <cam/scsi/scsi_pass.h>
66 #include <machine/stdarg.h>	/* for xpt_print below */
67 #include "opt_cam.h"
68 
69 struct scsi_quirk_entry {
70 	struct scsi_inquiry_pattern inq_pat;
71 	u_int8_t quirks;
72 #define	CAM_QUIRK_NOLUNS	0x01
73 #define	CAM_QUIRK_NOVPDS	0x02
74 #define	CAM_QUIRK_HILUNS	0x04
75 #define	CAM_QUIRK_NOHILUNS	0x08
76 #define	CAM_QUIRK_NORPTLUNS	0x10
77 	u_int mintags;
78 	u_int maxtags;
79 };
80 #define SCSI_QUIRK(dev)	((struct scsi_quirk_entry *)((dev)->quirk))
81 
82 static int cam_srch_hi = 0;
83 static int sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS);
84 SYSCTL_PROC(_kern_cam, OID_AUTO, cam_srch_hi, CTLTYPE_INT | CTLFLAG_RWTUN, 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()
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 	cam_status status;
661 	probe_softc *softc;
662 
663 	request_ccb = (union ccb *)arg;
664 	if (request_ccb == NULL) {
665 		printf("proberegister: no probe CCB, "
666 		       "can't register device\n");
667 		return(CAM_REQ_CMP_ERR);
668 	}
669 
670 	softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
671 
672 	if (softc == NULL) {
673 		printf("proberegister: Unable to probe new device. "
674 		       "Unable to allocate softc\n");
675 		return(CAM_REQ_CMP_ERR);
676 	}
677 	TAILQ_INIT(&softc->request_ccbs);
678 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
679 			  periph_links.tqe);
680 	softc->flags = 0;
681 	periph->softc = softc;
682 	softc->periph = periph;
683 	softc->action = PROBE_INVALID;
684 	status = cam_periph_acquire(periph);
685 	if (status != CAM_REQ_CMP) {
686 		return (status);
687 	}
688 	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
689 	scsi_devise_transport(periph->path);
690 
691 	/*
692 	 * Ensure we've waited at least a bus settle
693 	 * delay before attempting to probe the device.
694 	 * For HBAs that don't do bus resets, this won't make a difference.
695 	 */
696 	cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
697 				      scsi_delay);
698 	probeschedule(periph);
699 	return(CAM_REQ_CMP);
700 }
701 
702 static void
703 probeschedule(struct cam_periph *periph)
704 {
705 	struct ccb_pathinq cpi;
706 	union ccb *ccb;
707 	probe_softc *softc;
708 
709 	softc = (probe_softc *)periph->softc;
710 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
711 
712 	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
713 	cpi.ccb_h.func_code = XPT_PATH_INQ;
714 	xpt_action((union ccb *)&cpi);
715 
716 	/*
717 	 * If a device has gone away and another device, or the same one,
718 	 * is back in the same place, it should have a unit attention
719 	 * condition pending.  It will not report the unit attention in
720 	 * response to an inquiry, which may leave invalid transfer
721 	 * negotiations in effect.  The TUR will reveal the unit attention
722 	 * condition.  Only send the TUR for lun 0, since some devices
723 	 * will get confused by commands other than inquiry to non-existent
724 	 * luns.  If you think a device has gone away start your scan from
725 	 * lun 0.  This will insure that any bogus transfer settings are
726 	 * invalidated.
727 	 *
728 	 * If we haven't seen the device before and the controller supports
729 	 * some kind of transfer negotiation, negotiate with the first
730 	 * sent command if no bus reset was performed at startup.  This
731 	 * ensures that the device is not confused by transfer negotiation
732 	 * settings left over by loader or BIOS action.
733 	 */
734 	if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
735 	 && (ccb->ccb_h.target_lun == 0)) {
736 		PROBE_SET_ACTION(softc, PROBE_TUR);
737 	} else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
738 	      && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
739 		proberequestdefaultnegotiation(periph);
740 		PROBE_SET_ACTION(softc, PROBE_INQUIRY);
741 	} else {
742 		PROBE_SET_ACTION(softc, PROBE_INQUIRY);
743 	}
744 
745 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
746 		softc->flags |= PROBE_NO_ANNOUNCE;
747 	else
748 		softc->flags &= ~PROBE_NO_ANNOUNCE;
749 
750 	if (cpi.hba_misc & PIM_EXTLUNS)
751 		softc->flags |= PROBE_EXTLUN;
752 	else
753 		softc->flags &= ~PROBE_EXTLUN;
754 
755 	xpt_schedule(periph, CAM_PRIORITY_XPT);
756 }
757 
758 static void
759 probestart(struct cam_periph *periph, union ccb *start_ccb)
760 {
761 	/* Probe the device that our peripheral driver points to */
762 	struct ccb_scsiio *csio;
763 	probe_softc *softc;
764 
765 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
766 
767 	softc = (probe_softc *)periph->softc;
768 	csio = &start_ccb->csio;
769 again:
770 
771 	switch (softc->action) {
772 	case PROBE_TUR:
773 	case PROBE_TUR_FOR_NEGOTIATION:
774 	case PROBE_DV_EXIT:
775 	{
776 		scsi_test_unit_ready(csio,
777 				     /*retries*/4,
778 				     probedone,
779 				     MSG_SIMPLE_Q_TAG,
780 				     SSD_FULL_SIZE,
781 				     /*timeout*/60000);
782 		break;
783 	}
784 	case PROBE_INQUIRY:
785 	case PROBE_FULL_INQUIRY:
786 	case PROBE_INQUIRY_BASIC_DV1:
787 	case PROBE_INQUIRY_BASIC_DV2:
788 	{
789 		u_int inquiry_len;
790 		struct scsi_inquiry_data *inq_buf;
791 
792 		inq_buf = &periph->path->device->inq_data;
793 
794 		/*
795 		 * If the device is currently configured, we calculate an
796 		 * MD5 checksum of the inquiry data, and if the serial number
797 		 * length is greater than 0, add the serial number data
798 		 * into the checksum as well.  Once the inquiry and the
799 		 * serial number check finish, we attempt to figure out
800 		 * whether we still have the same device.
801 		 */
802 		if (((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
803 		 && ((softc->flags & PROBE_INQUIRY_CKSUM) == 0)) {
804 
805 			MD5Init(&softc->context);
806 			MD5Update(&softc->context, (unsigned char *)inq_buf,
807 				  sizeof(struct scsi_inquiry_data));
808 			softc->flags |= PROBE_INQUIRY_CKSUM;
809 			if (periph->path->device->serial_num_len > 0) {
810 				MD5Update(&softc->context,
811 					  periph->path->device->serial_num,
812 					  periph->path->device->serial_num_len);
813 				softc->flags |= PROBE_SERIAL_CKSUM;
814 			}
815 			MD5Final(softc->digest, &softc->context);
816 		}
817 
818 		if (softc->action == PROBE_INQUIRY)
819 			inquiry_len = SHORT_INQUIRY_LENGTH;
820 		else
821 			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
822 
823 		/*
824 		 * Some parallel SCSI devices fail to send an
825 		 * ignore wide residue message when dealing with
826 		 * odd length inquiry requests.  Round up to be
827 		 * safe.
828 		 */
829 		inquiry_len = roundup2(inquiry_len, 2);
830 
831 		if (softc->action == PROBE_INQUIRY_BASIC_DV1
832 		 || softc->action == PROBE_INQUIRY_BASIC_DV2) {
833 			inq_buf = malloc(inquiry_len, M_CAMXPT, M_NOWAIT);
834 		}
835 		if (inq_buf == NULL) {
836 			xpt_print(periph->path, "malloc failure- skipping Basic"
837 			    "Domain Validation\n");
838 			PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
839 			scsi_test_unit_ready(csio,
840 					     /*retries*/4,
841 					     probedone,
842 					     MSG_SIMPLE_Q_TAG,
843 					     SSD_FULL_SIZE,
844 					     /*timeout*/60000);
845 			break;
846 		}
847 		scsi_inquiry(csio,
848 			     /*retries*/4,
849 			     probedone,
850 			     MSG_SIMPLE_Q_TAG,
851 			     (u_int8_t *)inq_buf,
852 			     inquiry_len,
853 			     /*evpd*/FALSE,
854 			     /*page_code*/0,
855 			     SSD_MIN_SIZE,
856 			     /*timeout*/60 * 1000);
857 		break;
858 	}
859 	case PROBE_REPORT_LUNS:
860 	{
861 		void *rp;
862 
863 		rp = malloc(periph->path->target->rpl_size,
864 		    M_CAMXPT, M_NOWAIT | M_ZERO);
865 		if (rp == NULL) {
866 			struct scsi_inquiry_data *inq_buf;
867 			inq_buf = &periph->path->device->inq_data;
868 			xpt_print(periph->path,
869 			    "Unable to alloc report luns storage\n");
870 			if (INQ_DATA_TQ_ENABLED(inq_buf))
871 				PROBE_SET_ACTION(softc, PROBE_MODE_SENSE);
872 			else
873 				PROBE_SET_ACTION(softc,
874 				    PROBE_SUPPORTED_VPD_LIST);
875 			goto again;
876 		}
877 		scsi_report_luns(csio, 5, probedone, MSG_SIMPLE_Q_TAG,
878 		    RPL_REPORT_DEFAULT, rp, periph->path->target->rpl_size,
879 		    SSD_FULL_SIZE, 60000); break;
880 		break;
881 	}
882 	case PROBE_MODE_SENSE:
883 	{
884 		void  *mode_buf;
885 		int    mode_buf_len;
886 
887 		mode_buf_len = sizeof(struct scsi_mode_header_6)
888 			     + sizeof(struct scsi_mode_blk_desc)
889 			     + sizeof(struct scsi_control_page);
890 		mode_buf = malloc(mode_buf_len, M_CAMXPT, M_NOWAIT);
891 		if (mode_buf != NULL) {
892 	                scsi_mode_sense(csio,
893 					/*retries*/4,
894 					probedone,
895 					MSG_SIMPLE_Q_TAG,
896 					/*dbd*/FALSE,
897 					SMS_PAGE_CTRL_CURRENT,
898 					SMS_CONTROL_MODE_PAGE,
899 					mode_buf,
900 					mode_buf_len,
901 					SSD_FULL_SIZE,
902 					/*timeout*/60000);
903 			break;
904 		}
905 		xpt_print(periph->path, "Unable to mode sense control page - "
906 		    "malloc failure\n");
907 		PROBE_SET_ACTION(softc, PROBE_SUPPORTED_VPD_LIST);
908 	}
909 	/* FALLTHROUGH */
910 	case PROBE_SUPPORTED_VPD_LIST:
911 	{
912 		struct scsi_vpd_supported_page_list *vpd_list;
913 		struct cam_ed *device;
914 
915 		vpd_list = NULL;
916 		device = periph->path->device;
917 
918 		if ((SCSI_QUIRK(device)->quirks & CAM_QUIRK_NOVPDS) == 0)
919 			vpd_list = malloc(sizeof(*vpd_list), M_CAMXPT,
920 			    M_NOWAIT | M_ZERO);
921 
922 		if (vpd_list != NULL) {
923 			scsi_inquiry(csio,
924 				     /*retries*/4,
925 				     probedone,
926 				     MSG_SIMPLE_Q_TAG,
927 				     (u_int8_t *)vpd_list,
928 				     sizeof(*vpd_list),
929 				     /*evpd*/TRUE,
930 				     SVPD_SUPPORTED_PAGE_LIST,
931 				     SSD_MIN_SIZE,
932 				     /*timeout*/60 * 1000);
933 			break;
934 		}
935 done:
936 		/*
937 		 * We'll have to do without, let our probedone
938 		 * routine finish up for us.
939 		 */
940 		start_ccb->csio.data_ptr = NULL;
941 		cam_freeze_devq(periph->path);
942 		cam_periph_doacquire(periph);
943 		probedone(periph, start_ccb);
944 		return;
945 	}
946 	case PROBE_DEVICE_ID:
947 	{
948 		struct scsi_vpd_device_id *devid;
949 
950 		devid = NULL;
951 		if (scsi_vpd_supported_page(periph, SVPD_DEVICE_ID))
952 			devid = malloc(SVPD_DEVICE_ID_MAX_SIZE, M_CAMXPT,
953 			    M_NOWAIT | M_ZERO);
954 
955 		if (devid != NULL) {
956 			scsi_inquiry(csio,
957 				     /*retries*/4,
958 				     probedone,
959 				     MSG_SIMPLE_Q_TAG,
960 				     (uint8_t *)devid,
961 				     SVPD_DEVICE_ID_MAX_SIZE,
962 				     /*evpd*/TRUE,
963 				     SVPD_DEVICE_ID,
964 				     SSD_MIN_SIZE,
965 				     /*timeout*/60 * 1000);
966 			break;
967 		}
968 		goto done;
969 	}
970 	case PROBE_EXTENDED_INQUIRY:
971 	{
972 		struct scsi_vpd_extended_inquiry_data *ext_inq;
973 
974 		ext_inq = NULL;
975 		if (scsi_vpd_supported_page(periph, SVPD_EXTENDED_INQUIRY_DATA))
976 			ext_inq = malloc(sizeof(*ext_inq), M_CAMXPT,
977 			    M_NOWAIT | M_ZERO);
978 
979 		if (ext_inq != NULL) {
980 			scsi_inquiry(csio,
981 				     /*retries*/4,
982 				     probedone,
983 				     MSG_SIMPLE_Q_TAG,
984 				     (uint8_t *)ext_inq,
985 				     sizeof(*ext_inq),
986 				     /*evpd*/TRUE,
987 				     SVPD_EXTENDED_INQUIRY_DATA,
988 				     SSD_MIN_SIZE,
989 				     /*timeout*/60 * 1000);
990 			break;
991 		}
992 		/*
993 		 * We'll have to do without, let our probedone
994 		 * routine finish up for us.
995 		 */
996 		goto done;
997 	}
998 	case PROBE_SERIAL_NUM:
999 	{
1000 		struct scsi_vpd_unit_serial_number *serial_buf;
1001 		struct cam_ed* device;
1002 
1003 		serial_buf = NULL;
1004 		device = periph->path->device;
1005 		if (device->serial_num != NULL) {
1006 			free(device->serial_num, M_CAMXPT);
1007 			device->serial_num = NULL;
1008 			device->serial_num_len = 0;
1009 		}
1010 
1011 		if (scsi_vpd_supported_page(periph, SVPD_UNIT_SERIAL_NUMBER))
1012 			serial_buf = (struct scsi_vpd_unit_serial_number *)
1013 				malloc(sizeof(*serial_buf), M_CAMXPT,
1014 				    M_NOWAIT|M_ZERO);
1015 
1016 		if (serial_buf != NULL) {
1017 			scsi_inquiry(csio,
1018 				     /*retries*/4,
1019 				     probedone,
1020 				     MSG_SIMPLE_Q_TAG,
1021 				     (u_int8_t *)serial_buf,
1022 				     sizeof(*serial_buf),
1023 				     /*evpd*/TRUE,
1024 				     SVPD_UNIT_SERIAL_NUMBER,
1025 				     SSD_MIN_SIZE,
1026 				     /*timeout*/60 * 1000);
1027 			break;
1028 		}
1029 		goto done;
1030 	}
1031 	default:
1032 		panic("probestart: invalid action state 0x%x\n", softc->action);
1033 	}
1034 	start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1035 	cam_periph_doacquire(periph);
1036 	xpt_action(start_ccb);
1037 }
1038 
1039 static void
1040 proberequestdefaultnegotiation(struct cam_periph *periph)
1041 {
1042 	struct ccb_trans_settings cts;
1043 
1044 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1045 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1046 	cts.type = CTS_TYPE_USER_SETTINGS;
1047 	xpt_action((union ccb *)&cts);
1048 	if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP) {
1049 		return;
1050 	}
1051 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1052 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1053 	xpt_action((union ccb *)&cts);
1054 }
1055 
1056 /*
1057  * Backoff Negotiation Code- only pertinent for SPI devices.
1058  */
1059 static int
1060 proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
1061 {
1062 	struct ccb_trans_settings cts;
1063 	struct ccb_trans_settings_spi *spi;
1064 
1065 	memset(&cts, 0, sizeof (cts));
1066 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1067 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1068 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1069 	xpt_action((union ccb *)&cts);
1070 	if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP) {
1071 		if (bootverbose) {
1072 			xpt_print(periph->path,
1073 			    "failed to get current device settings\n");
1074 		}
1075 		return (0);
1076 	}
1077 	if (cts.transport != XPORT_SPI) {
1078 		if (bootverbose) {
1079 			xpt_print(periph->path, "not SPI transport\n");
1080 		}
1081 		return (0);
1082 	}
1083 	spi = &cts.xport_specific.spi;
1084 
1085 	/*
1086 	 * We cannot renegotiate sync rate if we don't have one.
1087 	 */
1088 	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
1089 		if (bootverbose) {
1090 			xpt_print(periph->path, "no sync rate known\n");
1091 		}
1092 		return (0);
1093 	}
1094 
1095 	/*
1096 	 * We'll assert that we don't have to touch PPR options- the
1097 	 * SIM will see what we do with period and offset and adjust
1098 	 * the PPR options as appropriate.
1099 	 */
1100 
1101 	/*
1102 	 * A sync rate with unknown or zero offset is nonsensical.
1103 	 * A sync period of zero means Async.
1104 	 */
1105 	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
1106 	 || spi->sync_offset == 0 || spi->sync_period == 0) {
1107 		if (bootverbose) {
1108 			xpt_print(periph->path, "no sync rate available\n");
1109 		}
1110 		return (0);
1111 	}
1112 
1113 	if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
1114 		CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1115 		    ("hit async: giving up on DV\n"));
1116 		return (0);
1117 	}
1118 
1119 
1120 	/*
1121 	 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
1122 	 * We don't try to remember 'last' settings to see if the SIM actually
1123 	 * gets into the speed we want to set. We check on the SIM telling
1124 	 * us that a requested speed is bad, but otherwise don't try and
1125 	 * check the speed due to the asynchronous and handshake nature
1126 	 * of speed setting.
1127 	 */
1128 	spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
1129 	for (;;) {
1130 		spi->sync_period++;
1131 		if (spi->sync_period >= 0xf) {
1132 			spi->sync_period = 0;
1133 			spi->sync_offset = 0;
1134 			CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1135 			    ("setting to async for DV\n"));
1136 			/*
1137 			 * Once we hit async, we don't want to try
1138 			 * any more settings.
1139 			 */
1140 			device->flags |= CAM_DEV_DV_HIT_BOTTOM;
1141 		} else if (bootverbose) {
1142 			CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1143 			    ("DV: period 0x%x\n", spi->sync_period));
1144 			printf("setting period to 0x%x\n", spi->sync_period);
1145 		}
1146 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1147 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1148 		xpt_action((union ccb *)&cts);
1149 		if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP) {
1150 			break;
1151 		}
1152 		CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1153 		    ("DV: failed to set period 0x%x\n", spi->sync_period));
1154 		if (spi->sync_period == 0) {
1155 			return (0);
1156 		}
1157 	}
1158 	return (1);
1159 }
1160 
1161 #define CCB_COMPLETED_OK(ccb) (((ccb).status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1162 
1163 static void
1164 probedone(struct cam_periph *periph, union ccb *done_ccb)
1165 {
1166 	probe_softc *softc;
1167 	struct cam_path *path;
1168 	struct scsi_inquiry_data *inq_buf;
1169 	u_int32_t  priority;
1170 
1171 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
1172 
1173 	softc = (probe_softc *)periph->softc;
1174 	path = done_ccb->ccb_h.path;
1175 	priority = done_ccb->ccb_h.pinfo.priority;
1176 	cam_periph_assert(periph, MA_OWNED);
1177 
1178 	switch (softc->action) {
1179 	case PROBE_TUR:
1180 	{
1181 		if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1182 
1183 			if (cam_periph_error(done_ccb, 0,
1184 					     SF_NO_PRINT, NULL) == ERESTART) {
1185 outr:
1186 				/* Drop freeze taken due to CAM_DEV_QFREEZE */
1187 				cam_release_devq(path, 0, 0, 0, FALSE);
1188 				return;
1189 			}
1190 			else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1191 				/* Don't wedge the queue */
1192 				xpt_release_devq(done_ccb->ccb_h.path,
1193 						 /*count*/1,
1194 						 /*run_queue*/TRUE);
1195 		}
1196 		PROBE_SET_ACTION(softc, PROBE_INQUIRY);
1197 		xpt_release_ccb(done_ccb);
1198 		xpt_schedule(periph, priority);
1199 out:
1200 		/* Drop freeze taken due to CAM_DEV_QFREEZE and release. */
1201 		cam_release_devq(path, 0, 0, 0, FALSE);
1202 		cam_periph_release_locked(periph);
1203 		return;
1204 	}
1205 	case PROBE_INQUIRY:
1206 	case PROBE_FULL_INQUIRY:
1207 	{
1208 		if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
1209 			u_int8_t periph_qual;
1210 
1211 			path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1212 			scsi_find_quirk(path->device);
1213 			inq_buf = &path->device->inq_data;
1214 
1215 			periph_qual = SID_QUAL(inq_buf);
1216 
1217 			if (periph_qual == SID_QUAL_LU_CONNECTED ||
1218 			    periph_qual == SID_QUAL_LU_OFFLINE) {
1219 				u_int8_t len;
1220 
1221 				/*
1222 				 * We conservatively request only
1223 				 * SHORT_INQUIRY_LEN bytes of inquiry
1224 				 * information during our first try
1225 				 * at sending an INQUIRY. If the device
1226 				 * has more information to give,
1227 				 * perform a second request specifying
1228 				 * the amount of information the device
1229 				 * is willing to give.
1230 				 */
1231 				len = inq_buf->additional_length
1232 				    + offsetof(struct scsi_inquiry_data,
1233                                                additional_length) + 1;
1234 				if (softc->action == PROBE_INQUIRY
1235 				    && len > SHORT_INQUIRY_LENGTH) {
1236 					PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1237 					xpt_release_ccb(done_ccb);
1238 					xpt_schedule(periph, priority);
1239 					goto out;
1240 				}
1241 
1242 				scsi_devise_transport(path);
1243 
1244 				if (path->device->lun_id == 0 &&
1245 				    SID_ANSI_REV(inq_buf) > SCSI_REV_SPC2 &&
1246 				    (SCSI_QUIRK(path->device)->quirks &
1247 				     CAM_QUIRK_NORPTLUNS) == 0) {
1248 					PROBE_SET_ACTION(softc,
1249 					    PROBE_REPORT_LUNS);
1250 					/*
1251 					 * Start with room for *one* lun.
1252 					 */
1253 					periph->path->target->rpl_size = 16;
1254 				} else if (INQ_DATA_TQ_ENABLED(inq_buf))
1255 					PROBE_SET_ACTION(softc,
1256 					    PROBE_MODE_SENSE);
1257 				else
1258 					PROBE_SET_ACTION(softc,
1259 					    PROBE_SUPPORTED_VPD_LIST);
1260 
1261 				if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1262 					path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1263 					xpt_acquire_device(path->device);
1264 				}
1265 				xpt_release_ccb(done_ccb);
1266 				xpt_schedule(periph, priority);
1267 				goto out;
1268 			} else if (path->device->lun_id == 0 &&
1269 			    SID_ANSI_REV(inq_buf) >= SCSI_REV_SPC2 &&
1270 			    (SCSI_QUIRK(path->device)->quirks &
1271 			     CAM_QUIRK_NORPTLUNS) == 0) {
1272 				PROBE_SET_ACTION(softc, PROBE_REPORT_LUNS);
1273 				periph->path->target->rpl_size = 16;
1274 				xpt_release_ccb(done_ccb);
1275 				xpt_schedule(periph, priority);
1276 				goto out;
1277 			}
1278 		} else if (cam_periph_error(done_ccb, 0,
1279 					    done_ccb->ccb_h.target_lun > 0
1280 					    ? SF_RETRY_UA|SF_QUIET_IR
1281 					    : SF_RETRY_UA,
1282 					    &softc->saved_ccb) == ERESTART) {
1283 			goto outr;
1284 		} else {
1285 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1286 				/* Don't wedge the queue */
1287 				xpt_release_devq(done_ccb->ccb_h.path,
1288 				    /*count*/1, /*run_queue*/TRUE);
1289 			}
1290 			path->device->flags &= ~CAM_DEV_INQUIRY_DATA_VALID;
1291 		}
1292 		/*
1293 		 * If we get to this point, we got an error status back
1294 		 * from the inquiry and the error status doesn't require
1295 		 * automatically retrying the command.  Therefore, the
1296 		 * inquiry failed.  If we had inquiry information before
1297 		 * for this device, but this latest inquiry command failed,
1298 		 * the device has probably gone away.  If this device isn't
1299 		 * already marked unconfigured, notify the peripheral
1300 		 * drivers that this device is no more.
1301 		 */
1302 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
1303 			/* Send the async notification. */
1304 			xpt_async(AC_LOST_DEVICE, path, NULL);
1305 		PROBE_SET_ACTION(softc, PROBE_INVALID);
1306 
1307 		xpt_release_ccb(done_ccb);
1308 		break;
1309 	}
1310 	case PROBE_REPORT_LUNS:
1311 	{
1312 		struct ccb_scsiio *csio;
1313 		struct scsi_report_luns_data *lp;
1314 		u_int nlun, maxlun;
1315 
1316 		csio = &done_ccb->csio;
1317 
1318 		lp = (struct scsi_report_luns_data *)csio->data_ptr;
1319 		nlun = scsi_4btoul(lp->length) / 8;
1320 		maxlun = (csio->dxfer_len / 8) - 1;
1321 
1322 		if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1323 			if (cam_periph_error(done_ccb, 0,
1324 			    done_ccb->ccb_h.target_lun > 0 ?
1325 			    SF_RETRY_UA|SF_QUIET_IR : SF_RETRY_UA,
1326 			    &softc->saved_ccb) == ERESTART) {
1327 				goto outr;
1328 			}
1329 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1330 				xpt_release_devq(done_ccb->ccb_h.path, 1,
1331 				    TRUE);
1332 			}
1333 			free(lp, M_CAMXPT);
1334 			lp = NULL;
1335 		} else if (nlun > maxlun) {
1336 			/*
1337 			 * Reallocate and retry to cover all luns
1338 			 */
1339 			CAM_DEBUG(path, CAM_DEBUG_PROBE,
1340 			    ("Probe: reallocating REPORT_LUNS for %u luns\n",
1341 			     nlun));
1342 			free(lp, M_CAMXPT);
1343 			path->target->rpl_size = (nlun << 3) + 8;
1344 			xpt_release_ccb(done_ccb);
1345 			xpt_schedule(periph, priority);
1346 			goto out;
1347 		} else if (nlun == 0) {
1348 			/*
1349 			 * If there don't appear to be any luns, bail.
1350 			 */
1351 			free(lp, M_CAMXPT);
1352 			lp = NULL;
1353 		} else {
1354 			lun_id_t lun;
1355 			int idx;
1356 
1357 			CAM_DEBUG(path, CAM_DEBUG_PROBE,
1358 			   ("Probe: %u lun(s) reported\n", nlun));
1359 
1360 			CAM_GET_LUN(lp, 0, lun);
1361 			/*
1362 			 * If the first lun is not lun 0, then either there
1363 			 * is no lun 0 in the list, or the list is unsorted.
1364 			 */
1365 			if (lun != 0) {
1366 				for (idx = 0; idx < nlun; idx++) {
1367 					CAM_GET_LUN(lp, idx, lun);
1368 					if (lun == 0) {
1369 						break;
1370 					}
1371 				}
1372 				if (idx != nlun) {
1373 					uint8_t tlun[8];
1374 					memcpy(tlun,
1375 					    lp->luns[0].lundata, 8);
1376 					memcpy(lp->luns[0].lundata,
1377 					    lp->luns[idx].lundata, 8);
1378 					memcpy(lp->luns[idx].lundata,
1379 					    tlun, 8);
1380 					CAM_DEBUG(path, CAM_DEBUG_PROBE,
1381 					    ("lun 0 in position %u\n", idx));
1382 				}
1383 			}
1384 			/*
1385 			 * If we have an old lun list, We can either
1386 			 * retest luns that appear to have been dropped,
1387 			 * or just nuke them.  We'll opt for the latter.
1388 			 * This function will also install the new list
1389 			 * in the target structure.
1390 			 */
1391 			probe_purge_old(path, lp, softc->flags);
1392 			lp = NULL;
1393 		}
1394 		inq_buf = &path->device->inq_data;
1395 		if (path->device->flags & CAM_DEV_INQUIRY_DATA_VALID &&
1396 		    (SID_QUAL(inq_buf) == SID_QUAL_LU_CONNECTED ||
1397 		    SID_QUAL(inq_buf) == SID_QUAL_LU_OFFLINE)) {
1398 			if (INQ_DATA_TQ_ENABLED(inq_buf))
1399 				PROBE_SET_ACTION(softc, PROBE_MODE_SENSE);
1400 			else
1401 				PROBE_SET_ACTION(softc,
1402 				    PROBE_SUPPORTED_VPD_LIST);
1403 			xpt_release_ccb(done_ccb);
1404 			xpt_schedule(periph, priority);
1405 			goto out;
1406 		}
1407 		if (lp) {
1408 			free(lp, M_CAMXPT);
1409 		}
1410 		PROBE_SET_ACTION(softc, PROBE_INVALID);
1411 		xpt_release_ccb(done_ccb);
1412 		break;
1413 	}
1414 	case PROBE_MODE_SENSE:
1415 	{
1416 		struct ccb_scsiio *csio;
1417 		struct scsi_mode_header_6 *mode_hdr;
1418 
1419 		csio = &done_ccb->csio;
1420 		mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
1421 		if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
1422 			struct scsi_control_page *page;
1423 			u_int8_t *offset;
1424 
1425 			offset = ((u_int8_t *)&mode_hdr[1])
1426 			    + mode_hdr->blk_desc_len;
1427 			page = (struct scsi_control_page *)offset;
1428 			path->device->queue_flags = page->queue_flags;
1429 		} else if (cam_periph_error(done_ccb, 0,
1430 					    SF_RETRY_UA|SF_NO_PRINT,
1431 					    &softc->saved_ccb) == ERESTART) {
1432 			goto outr;
1433 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1434 			/* Don't wedge the queue */
1435 			xpt_release_devq(done_ccb->ccb_h.path,
1436 					 /*count*/1, /*run_queue*/TRUE);
1437 		}
1438 		xpt_release_ccb(done_ccb);
1439 		free(mode_hdr, M_CAMXPT);
1440 		PROBE_SET_ACTION(softc, PROBE_SUPPORTED_VPD_LIST);
1441 		xpt_schedule(periph, priority);
1442 		goto out;
1443 	}
1444 	case PROBE_SUPPORTED_VPD_LIST:
1445 	{
1446 		struct ccb_scsiio *csio;
1447 		struct scsi_vpd_supported_page_list *page_list;
1448 
1449 		csio = &done_ccb->csio;
1450 		page_list =
1451 		    (struct scsi_vpd_supported_page_list *)csio->data_ptr;
1452 
1453 		if (path->device->supported_vpds != NULL) {
1454 			free(path->device->supported_vpds, M_CAMXPT);
1455 			path->device->supported_vpds = NULL;
1456 			path->device->supported_vpds_len = 0;
1457 		}
1458 
1459 		if (page_list == NULL) {
1460 			/*
1461 			 * Don't process the command as it was never sent
1462 			 */
1463 		} else if (CCB_COMPLETED_OK(csio->ccb_h)) {
1464 			/* Got vpd list */
1465 			path->device->supported_vpds_len = page_list->length +
1466 			    SVPD_SUPPORTED_PAGES_HDR_LEN;
1467 			path->device->supported_vpds = (uint8_t *)page_list;
1468 			xpt_release_ccb(done_ccb);
1469 			PROBE_SET_ACTION(softc, PROBE_DEVICE_ID);
1470 			xpt_schedule(periph, priority);
1471 			goto out;
1472 		} else if (cam_periph_error(done_ccb, 0,
1473 					    SF_RETRY_UA|SF_NO_PRINT,
1474 					    &softc->saved_ccb) == ERESTART) {
1475 			goto outr;
1476 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1477 			/* Don't wedge the queue */
1478 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1479 					 /*run_queue*/TRUE);
1480 		}
1481 
1482 		if (page_list)
1483 			free(page_list, M_CAMXPT);
1484 		/* No VPDs available, skip to device check. */
1485 		csio->data_ptr = NULL;
1486 		goto probe_device_check;
1487 	}
1488 	case PROBE_DEVICE_ID:
1489 	{
1490 		struct scsi_vpd_device_id *devid;
1491 		struct ccb_scsiio *csio;
1492 		uint32_t length = 0;
1493 
1494 		csio = &done_ccb->csio;
1495 		devid = (struct scsi_vpd_device_id *)csio->data_ptr;
1496 
1497 		/* Clean up from previous instance of this device */
1498 		if (path->device->device_id != NULL) {
1499 			path->device->device_id_len = 0;
1500 			free(path->device->device_id, M_CAMXPT);
1501 			path->device->device_id = NULL;
1502 		}
1503 
1504 		if (devid == NULL) {
1505 			/* Don't process the command as it was never sent */
1506 		} else if (CCB_COMPLETED_OK(csio->ccb_h)) {
1507 			length = scsi_2btoul(devid->length);
1508 			if (length != 0) {
1509 				/*
1510 				 * NB: device_id_len is actual response
1511 				 * size, not buffer size.
1512 				 */
1513 				path->device->device_id_len = length +
1514 				    SVPD_DEVICE_ID_HDR_LEN;
1515 				path->device->device_id = (uint8_t *)devid;
1516 			}
1517 		} else if (cam_periph_error(done_ccb, 0,
1518 					    SF_RETRY_UA,
1519 					    &softc->saved_ccb) == ERESTART) {
1520 			goto outr;
1521 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1522 			/* Don't wedge the queue */
1523 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1524 					 /*run_queue*/TRUE);
1525 		}
1526 
1527 		/* Free the device id space if we don't use it */
1528 		if (devid && length == 0)
1529 			free(devid, M_CAMXPT);
1530 		xpt_release_ccb(done_ccb);
1531 		PROBE_SET_ACTION(softc, PROBE_EXTENDED_INQUIRY);
1532 		xpt_schedule(periph, priority);
1533 		goto out;
1534 	}
1535 	case PROBE_EXTENDED_INQUIRY: {
1536 		struct scsi_vpd_extended_inquiry_data *ext_inq;
1537 		struct ccb_scsiio *csio;
1538 		int32_t length = 0;
1539 
1540 		csio = &done_ccb->csio;
1541 		ext_inq = (struct scsi_vpd_extended_inquiry_data *)
1542 		    csio->data_ptr;
1543 		if (path->device->ext_inq != NULL) {
1544 			path->device->ext_inq_len = 0;
1545 			free(path->device->ext_inq, M_CAMXPT);
1546 			path->device->ext_inq = NULL;
1547 		}
1548 
1549 		if (ext_inq == NULL) {
1550 			/* Don't process the command as it was never sent */
1551 		} else if (CCB_COMPLETED_OK(csio->ccb_h)) {
1552 			length = scsi_2btoul(ext_inq->page_length) +
1553 			    __offsetof(struct scsi_vpd_extended_inquiry_data,
1554 			    flags1);
1555 			length = min(length, sizeof(*ext_inq));
1556 			length -= csio->resid;
1557 			if (length > 0) {
1558 				path->device->ext_inq_len = length;
1559 				path->device->ext_inq = (uint8_t *)ext_inq;
1560 			}
1561 		} else if (cam_periph_error(done_ccb, 0,
1562 					    SF_RETRY_UA,
1563 					    &softc->saved_ccb) == ERESTART) {
1564 			goto outr;
1565 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1566 			/* Don't wedge the queue */
1567 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1568 					 /*run_queue*/TRUE);
1569 		}
1570 
1571 		/* Free the device id space if we don't use it */
1572 		if (ext_inq && length <= 0)
1573 			free(ext_inq, M_CAMXPT);
1574 		xpt_release_ccb(done_ccb);
1575 		PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM);
1576 		xpt_schedule(periph, priority);
1577 		goto out;
1578 	}
1579 
1580 probe_device_check:
1581 	case PROBE_SERIAL_NUM:
1582 	{
1583 		struct ccb_scsiio *csio;
1584 		struct scsi_vpd_unit_serial_number *serial_buf;
1585 		u_int32_t  priority;
1586 		int changed;
1587 		int have_serialnum;
1588 
1589 		changed = 1;
1590 		have_serialnum = 0;
1591 		csio = &done_ccb->csio;
1592 		priority = done_ccb->ccb_h.pinfo.priority;
1593 		serial_buf =
1594 		    (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
1595 
1596 		if (serial_buf == NULL) {
1597 			/*
1598 			 * Don't process the command as it was never sent
1599 			 */
1600 		} else if (cam_ccb_status(done_ccb) == CAM_REQ_CMP
1601 			&& (serial_buf->length > 0)) {
1602 
1603 			have_serialnum = 1;
1604 			path->device->serial_num =
1605 				(u_int8_t *)malloc((serial_buf->length + 1),
1606 						   M_CAMXPT, M_NOWAIT);
1607 			if (path->device->serial_num != NULL) {
1608 				int start, slen;
1609 
1610 				start = strspn(serial_buf->serial_num, " ");
1611 				slen = serial_buf->length - start;
1612 				if (slen <= 0) {
1613 					/*
1614 					 * SPC5r05 says that an all-space serial
1615 					 * number means no product serial number
1616 					 * is available
1617 					 */
1618 					slen = 0;
1619 				}
1620 				memcpy(path->device->serial_num,
1621 				       &serial_buf->serial_num[start], slen);
1622 				path->device->serial_num_len = slen;
1623 				path->device->serial_num[slen] = '\0';
1624 			}
1625 		} else if (cam_periph_error(done_ccb, 0,
1626 					    SF_RETRY_UA|SF_NO_PRINT,
1627 					    &softc->saved_ccb) == ERESTART) {
1628 			goto outr;
1629 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1630 			/* Don't wedge the queue */
1631 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1632 					 /*run_queue*/TRUE);
1633 		}
1634 
1635 		/*
1636 		 * Let's see if we have seen this device before.
1637 		 */
1638 		if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
1639 			MD5_CTX context;
1640 			u_int8_t digest[16];
1641 
1642 			MD5Init(&context);
1643 
1644 			MD5Update(&context,
1645 				  (unsigned char *)&path->device->inq_data,
1646 				  sizeof(struct scsi_inquiry_data));
1647 
1648 			if (have_serialnum)
1649 				MD5Update(&context, path->device->serial_num,
1650 					  path->device->serial_num_len);
1651 
1652 			MD5Final(digest, &context);
1653 			if (bcmp(softc->digest, digest, 16) == 0)
1654 				changed = 0;
1655 
1656 			/*
1657 			 * XXX Do we need to do a TUR in order to ensure
1658 			 *     that the device really hasn't changed???
1659 			 */
1660 			if ((changed != 0)
1661 			 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
1662 				xpt_async(AC_LOST_DEVICE, path, NULL);
1663 		}
1664 		if (serial_buf != NULL)
1665 			free(serial_buf, M_CAMXPT);
1666 
1667 		if (changed != 0) {
1668 			/*
1669 			 * Now that we have all the necessary
1670 			 * information to safely perform transfer
1671 			 * negotiations... Controllers don't perform
1672 			 * any negotiation or tagged queuing until
1673 			 * after the first XPT_SET_TRAN_SETTINGS ccb is
1674 			 * received.  So, on a new device, just retrieve
1675 			 * the user settings, and set them as the current
1676 			 * settings to set the device up.
1677 			 */
1678 			proberequestdefaultnegotiation(periph);
1679 			xpt_release_ccb(done_ccb);
1680 
1681 			/*
1682 			 * Perform a TUR to allow the controller to
1683 			 * perform any necessary transfer negotiation.
1684 			 */
1685 			PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1686 			xpt_schedule(periph, priority);
1687 			goto out;
1688 		}
1689 		xpt_release_ccb(done_ccb);
1690 		break;
1691 	}
1692 	case PROBE_TUR_FOR_NEGOTIATION:
1693 	case PROBE_DV_EXIT:
1694 		if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1695 			cam_periph_error(done_ccb, 0,
1696 			    SF_NO_PRINT | SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1697 		}
1698 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1699 			/* Don't wedge the queue */
1700 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1701 					 /*run_queue*/TRUE);
1702 		}
1703 		/*
1704 		 * Do Domain Validation for lun 0 on devices that claim
1705 		 * to support Synchronous Transfer modes.
1706 		 */
1707 	 	if (softc->action == PROBE_TUR_FOR_NEGOTIATION
1708 		 && done_ccb->ccb_h.target_lun == 0
1709 		 && (path->device->inq_data.flags & SID_Sync) != 0
1710                  && (path->device->flags & CAM_DEV_IN_DV) == 0) {
1711 			CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1712 			    ("Begin Domain Validation\n"));
1713 			path->device->flags |= CAM_DEV_IN_DV;
1714 			xpt_release_ccb(done_ccb);
1715 			PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV1);
1716 			xpt_schedule(periph, priority);
1717 			goto out;
1718 		}
1719 		if (softc->action == PROBE_DV_EXIT) {
1720 			CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1721 			    ("Leave Domain Validation\n"));
1722 		}
1723 		if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1724 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1725 			xpt_acquire_device(path->device);
1726 		}
1727 		path->device->flags &=
1728 		    ~(CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1729 		if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1730 			/* Inform the XPT that a new device has been found */
1731 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1732 			xpt_action(done_ccb);
1733 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1734 				  done_ccb);
1735 		}
1736 		PROBE_SET_ACTION(softc, PROBE_DONE);
1737 		xpt_release_ccb(done_ccb);
1738 		break;
1739 	case PROBE_INQUIRY_BASIC_DV1:
1740 	case PROBE_INQUIRY_BASIC_DV2:
1741 	{
1742 		struct scsi_inquiry_data *nbuf;
1743 		struct ccb_scsiio *csio;
1744 
1745 		if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1746 			cam_periph_error(done_ccb, 0,
1747 			    SF_NO_PRINT | SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1748 		}
1749 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1750 			/* Don't wedge the queue */
1751 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1752 					 /*run_queue*/TRUE);
1753 		}
1754 		csio = &done_ccb->csio;
1755 		nbuf = (struct scsi_inquiry_data *)csio->data_ptr;
1756 		if (bcmp(nbuf, &path->device->inq_data, SHORT_INQUIRY_LENGTH)) {
1757 			xpt_print(path,
1758 			    "inquiry data fails comparison at DV%d step\n",
1759 			    softc->action == PROBE_INQUIRY_BASIC_DV1 ? 1 : 2);
1760 			if (proberequestbackoff(periph, path->device)) {
1761 				path->device->flags &= ~CAM_DEV_IN_DV;
1762 				PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1763 			} else {
1764 				/* give up */
1765 				PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
1766 			}
1767 			free(nbuf, M_CAMXPT);
1768 			xpt_release_ccb(done_ccb);
1769 			xpt_schedule(periph, priority);
1770 			goto out;
1771 		}
1772 		free(nbuf, M_CAMXPT);
1773 		if (softc->action == PROBE_INQUIRY_BASIC_DV1) {
1774 			PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV2);
1775 			xpt_release_ccb(done_ccb);
1776 			xpt_schedule(periph, priority);
1777 			goto out;
1778 		}
1779 		if (softc->action == PROBE_INQUIRY_BASIC_DV2) {
1780 			CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1781 			    ("Leave Domain Validation Successfully\n"));
1782 		}
1783 		if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1784 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1785 			xpt_acquire_device(path->device);
1786 		}
1787 		path->device->flags &=
1788 		    ~(CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1789 		if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1790 			/* Inform the XPT that a new device has been found */
1791 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1792 			xpt_action(done_ccb);
1793 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1794 				  done_ccb);
1795 		}
1796 		PROBE_SET_ACTION(softc, PROBE_DONE);
1797 		xpt_release_ccb(done_ccb);
1798 		break;
1799 	}
1800 	default:
1801 		panic("probedone: invalid action state 0x%x\n", softc->action);
1802 	}
1803 	done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
1804 	TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
1805 	done_ccb->ccb_h.status = CAM_REQ_CMP;
1806 	xpt_done(done_ccb);
1807 	if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
1808 		CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
1809 		/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1810 		cam_release_devq(path, 0, 0, 0, FALSE);
1811 		cam_periph_release_locked(periph);
1812 		cam_periph_invalidate(periph);
1813 		cam_periph_release_locked(periph);
1814 	} else {
1815 		probeschedule(periph);
1816 		goto out;
1817 	}
1818 }
1819 
1820 static void
1821 probe_purge_old(struct cam_path *path, struct scsi_report_luns_data *new,
1822     probe_flags flags)
1823 {
1824 	struct cam_path *tp;
1825 	struct scsi_report_luns_data *old;
1826 	u_int idx1, idx2, nlun_old, nlun_new;
1827 	lun_id_t this_lun;
1828 	u_int8_t *ol, *nl;
1829 
1830 	if (path->target == NULL) {
1831 		return;
1832 	}
1833 	mtx_lock(&path->target->luns_mtx);
1834 	old = path->target->luns;
1835 	path->target->luns = new;
1836 	mtx_unlock(&path->target->luns_mtx);
1837 	if (old == NULL)
1838 		return;
1839 	nlun_old = scsi_4btoul(old->length) / 8;
1840 	nlun_new = scsi_4btoul(new->length) / 8;
1841 
1842 	/*
1843 	 * We are not going to assume sorted lists. Deal.
1844 	 */
1845 	for (idx1 = 0; idx1 < nlun_old; idx1++) {
1846 		ol = old->luns[idx1].lundata;
1847 		for (idx2 = 0; idx2 < nlun_new; idx2++) {
1848 			nl = new->luns[idx2].lundata;
1849 			if (memcmp(nl, ol, 8) == 0) {
1850 				break;
1851 			}
1852 		}
1853 		if (idx2 < nlun_new) {
1854 			continue;
1855 		}
1856 		/*
1857 		 * An 'old' item not in the 'new' list.
1858 		 * Nuke it. Except that if it is lun 0,
1859 		 * that would be what the probe state
1860 		 * machine is currently working on,
1861 		 * so we won't do that.
1862 		 */
1863 		CAM_GET_LUN(old, idx1, this_lun);
1864 		if (this_lun == 0) {
1865 			continue;
1866 		}
1867 
1868 		/*
1869 		 * We also cannot nuke it if it is
1870 		 * not in a lun format we understand
1871 		 * and replace the LUN with a "simple" LUN
1872 		 * if that is all the HBA supports.
1873 		 */
1874 		if (!(flags & PROBE_EXTLUN)) {
1875 			if (!CAM_CAN_GET_SIMPLE_LUN(old, idx1))
1876 				continue;
1877 			CAM_GET_SIMPLE_LUN(old, idx1, this_lun);
1878 		}
1879 
1880 		if (xpt_create_path(&tp, NULL, xpt_path_path_id(path),
1881 		    xpt_path_target_id(path), this_lun) == CAM_REQ_CMP) {
1882 			xpt_async(AC_LOST_DEVICE, tp, NULL);
1883 			xpt_free_path(tp);
1884 		}
1885 	}
1886 	free(old, M_CAMXPT);
1887 }
1888 
1889 static void
1890 probecleanup(struct cam_periph *periph)
1891 {
1892 	free(periph->softc, M_CAMXPT);
1893 }
1894 
1895 static void
1896 scsi_find_quirk(struct cam_ed *device)
1897 {
1898 	struct scsi_quirk_entry *quirk;
1899 	caddr_t	match;
1900 
1901 	match = cam_quirkmatch((caddr_t)&device->inq_data,
1902 			       (caddr_t)scsi_quirk_table,
1903 			       nitems(scsi_quirk_table),
1904 			       sizeof(*scsi_quirk_table), scsi_inquiry_match);
1905 
1906 	if (match == NULL)
1907 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
1908 
1909 	quirk = (struct scsi_quirk_entry *)match;
1910 	device->quirk = quirk;
1911 	device->mintags = quirk->mintags;
1912 	device->maxtags = quirk->maxtags;
1913 }
1914 
1915 static int
1916 sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS)
1917 {
1918 	int error, val;
1919 
1920 	val = cam_srch_hi;
1921 	error = sysctl_handle_int(oidp, &val, 0, req);
1922 	if (error != 0 || req->newptr == NULL)
1923 		return (error);
1924 	if (val == 0 || val == 1) {
1925 		cam_srch_hi = val;
1926 		return (0);
1927 	} else {
1928 		return (EINVAL);
1929 	}
1930 }
1931 
1932 typedef struct {
1933 	union	ccb *request_ccb;
1934 	struct 	ccb_pathinq *cpi;
1935 	int	counter;
1936 	int	lunindex[0];
1937 } scsi_scan_bus_info;
1938 
1939 /*
1940  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1941  * As the scan progresses, scsi_scan_bus is used as the
1942  * callback on completion function.
1943  */
1944 static void
1945 scsi_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1946 {
1947 	struct mtx *mtx;
1948 
1949 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1950 		  ("scsi_scan_bus\n"));
1951 	switch (request_ccb->ccb_h.func_code) {
1952 	case XPT_SCAN_BUS:
1953 	case XPT_SCAN_TGT:
1954 	{
1955 		scsi_scan_bus_info *scan_info;
1956 		union	ccb *work_ccb, *reset_ccb;
1957 		struct	cam_path *path;
1958 		u_int	i;
1959 		u_int	low_target, max_target;
1960 		u_int	initiator_id;
1961 
1962 		/* Find out the characteristics of the bus */
1963 		work_ccb = xpt_alloc_ccb_nowait();
1964 		if (work_ccb == NULL) {
1965 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1966 			xpt_done(request_ccb);
1967 			return;
1968 		}
1969 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1970 			      request_ccb->ccb_h.pinfo.priority);
1971 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1972 		xpt_action(work_ccb);
1973 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1974 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1975 			xpt_free_ccb(work_ccb);
1976 			xpt_done(request_ccb);
1977 			return;
1978 		}
1979 
1980 		if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
1981 			/*
1982 			 * Can't scan the bus on an adapter that
1983 			 * cannot perform the initiator role.
1984 			 */
1985 			request_ccb->ccb_h.status = CAM_REQ_CMP;
1986 			xpt_free_ccb(work_ccb);
1987 			xpt_done(request_ccb);
1988 			return;
1989 		}
1990 
1991 		/* We may need to reset bus first, if we haven't done it yet. */
1992 		if ((work_ccb->cpi.hba_inquiry &
1993 		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1994 		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1995 		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset) &&
1996 		    (reset_ccb = xpt_alloc_ccb_nowait()) != NULL) {
1997 			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1998 			      CAM_PRIORITY_NONE);
1999 			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
2000 			xpt_action(reset_ccb);
2001 			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
2002 				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
2003 				xpt_free_ccb(reset_ccb);
2004 				xpt_free_ccb(work_ccb);
2005 				xpt_done(request_ccb);
2006 				return;
2007 			}
2008 			xpt_free_ccb(reset_ccb);
2009 		}
2010 
2011 		/* Save some state for use while we probe for devices */
2012 		scan_info = (scsi_scan_bus_info *) malloc(sizeof(scsi_scan_bus_info) +
2013 		    (work_ccb->cpi.max_target * sizeof (u_int)), M_CAMXPT, M_ZERO|M_NOWAIT);
2014 		if (scan_info == NULL) {
2015 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2016 			xpt_free_ccb(work_ccb);
2017 			xpt_done(request_ccb);
2018 			return;
2019 		}
2020 		CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
2021 		   ("SCAN start for %p\n", scan_info));
2022 		scan_info->request_ccb = request_ccb;
2023 		scan_info->cpi = &work_ccb->cpi;
2024 
2025 		/* Cache on our stack so we can work asynchronously */
2026 		max_target = scan_info->cpi->max_target;
2027 		low_target = 0;
2028 		initiator_id = scan_info->cpi->initiator_id;
2029 
2030 
2031 		/*
2032 		 * We can scan all targets in parallel, or do it sequentially.
2033 		 */
2034 
2035 		if (request_ccb->ccb_h.func_code == XPT_SCAN_TGT) {
2036 			max_target = low_target = request_ccb->ccb_h.target_id;
2037 			scan_info->counter = 0;
2038 		} else if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
2039 			max_target = 0;
2040 			scan_info->counter = 0;
2041 		} else {
2042 			scan_info->counter = scan_info->cpi->max_target + 1;
2043 			if (scan_info->cpi->initiator_id < scan_info->counter) {
2044 				scan_info->counter--;
2045 			}
2046 		}
2047 		mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
2048 		mtx_unlock(mtx);
2049 
2050 		for (i = low_target; i <= max_target; i++) {
2051 			cam_status status;
2052 			if (i == initiator_id)
2053 				continue;
2054 
2055 			status = xpt_create_path(&path, NULL,
2056 						 request_ccb->ccb_h.path_id,
2057 						 i, 0);
2058 			if (status != CAM_REQ_CMP) {
2059 				printf("scsi_scan_bus: xpt_create_path failed"
2060 				       " with status %#x, bus scan halted\n",
2061 				       status);
2062 				free(scan_info, M_CAMXPT);
2063 				request_ccb->ccb_h.status = status;
2064 				xpt_free_ccb(work_ccb);
2065 				xpt_done(request_ccb);
2066 				break;
2067 			}
2068 			work_ccb = xpt_alloc_ccb_nowait();
2069 			if (work_ccb == NULL) {
2070 				xpt_free_ccb((union ccb *)scan_info->cpi);
2071 				free(scan_info, M_CAMXPT);
2072 				xpt_free_path(path);
2073 				request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2074 				xpt_done(request_ccb);
2075 				break;
2076 			}
2077 			xpt_setup_ccb(&work_ccb->ccb_h, path,
2078 				      request_ccb->ccb_h.pinfo.priority);
2079 			work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2080 			work_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2081 			work_ccb->ccb_h.flags |= CAM_UNLOCKED;
2082 			work_ccb->ccb_h.ppriv_ptr0 = scan_info;
2083 			work_ccb->crcn.flags = request_ccb->crcn.flags;
2084 			xpt_action(work_ccb);
2085 		}
2086 
2087 		mtx_lock(mtx);
2088 		break;
2089 	}
2090 	case XPT_SCAN_LUN:
2091 	{
2092 		cam_status status;
2093 		struct cam_path *path, *oldpath;
2094 		scsi_scan_bus_info *scan_info;
2095 		struct cam_et *target;
2096 		struct cam_ed *device, *nextdev;
2097 		int next_target;
2098 		path_id_t path_id;
2099 		target_id_t target_id;
2100 		lun_id_t lun_id;
2101 
2102 		oldpath = request_ccb->ccb_h.path;
2103 
2104 		status = cam_ccb_status(request_ccb);
2105 		scan_info = (scsi_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
2106 		path_id = request_ccb->ccb_h.path_id;
2107 		target_id = request_ccb->ccb_h.target_id;
2108 		lun_id = request_ccb->ccb_h.target_lun;
2109 		target = request_ccb->ccb_h.path->target;
2110 		next_target = 1;
2111 
2112 		mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
2113 		mtx_lock(mtx);
2114 		mtx_lock(&target->luns_mtx);
2115 		if (target->luns) {
2116 			lun_id_t first;
2117 			u_int nluns = scsi_4btoul(target->luns->length) / 8;
2118 
2119 			/*
2120 			 * Make sure we skip over lun 0 if it's the first member
2121 			 * of the list as we've actually just finished probing
2122 			 * it.
2123 			 */
2124 			CAM_GET_LUN(target->luns, 0, first);
2125 			if (first == 0 && scan_info->lunindex[target_id] == 0) {
2126 				scan_info->lunindex[target_id]++;
2127 			}
2128 
2129 			/*
2130 			 * Skip any LUNs that the HBA can't deal with.
2131 			 */
2132 			while (scan_info->lunindex[target_id] < nluns) {
2133 				if (scan_info->cpi->hba_misc & PIM_EXTLUNS) {
2134 					CAM_GET_LUN(target->luns,
2135 					    scan_info->lunindex[target_id],
2136 					    lun_id);
2137 					break;
2138 				}
2139 
2140 				if (CAM_CAN_GET_SIMPLE_LUN(target->luns,
2141 				    scan_info->lunindex[target_id])) {
2142 					CAM_GET_SIMPLE_LUN(target->luns,
2143 					    scan_info->lunindex[target_id],
2144 					    lun_id);
2145 					break;
2146 				}
2147 
2148 				scan_info->lunindex[target_id]++;
2149 			}
2150 
2151 			if (scan_info->lunindex[target_id] < nluns) {
2152 				mtx_unlock(&target->luns_mtx);
2153 				next_target = 0;
2154 				CAM_DEBUG(request_ccb->ccb_h.path,
2155 				    CAM_DEBUG_PROBE,
2156 				   ("next lun to try at index %u is %jx\n",
2157 				   scan_info->lunindex[target_id],
2158 				   (uintmax_t)lun_id));
2159 				scan_info->lunindex[target_id]++;
2160 			} else {
2161 				mtx_unlock(&target->luns_mtx);
2162 				/* We're done with scanning all luns. */
2163 			}
2164 		} else {
2165 			mtx_unlock(&target->luns_mtx);
2166 			device = request_ccb->ccb_h.path->device;
2167 			/* Continue sequential LUN scan if: */
2168 			/*  -- we have more LUNs that need recheck */
2169 			mtx_lock(&target->bus->eb_mtx);
2170 			nextdev = device;
2171 			while ((nextdev = TAILQ_NEXT(nextdev, links)) != NULL)
2172 				if ((nextdev->flags & CAM_DEV_UNCONFIGURED) == 0)
2173 					break;
2174 			mtx_unlock(&target->bus->eb_mtx);
2175 			if (nextdev != NULL) {
2176 				next_target = 0;
2177 			/*  -- stop if CAM_QUIRK_NOLUNS is set. */
2178 			} else if (SCSI_QUIRK(device)->quirks & CAM_QUIRK_NOLUNS) {
2179 				next_target = 1;
2180 			/*  -- this LUN is connected and its SCSI version
2181 			 *     allows more LUNs. */
2182 			} else if ((device->flags & CAM_DEV_UNCONFIGURED) == 0) {
2183 				if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
2184 				    CAN_SRCH_HI_DENSE(device))
2185 					next_target = 0;
2186 			/*  -- this LUN is disconnected, its SCSI version
2187 			 *     allows more LUNs and we guess they may be. */
2188 			} else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0) {
2189 				if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
2190 				    CAN_SRCH_HI_SPARSE(device))
2191 					next_target = 0;
2192 			}
2193 			if (next_target == 0) {
2194 				lun_id++;
2195 				if (lun_id > scan_info->cpi->max_lun)
2196 					next_target = 1;
2197 			}
2198 		}
2199 
2200 		/*
2201 		 * Check to see if we scan any further luns.
2202 		 */
2203 		if (next_target) {
2204 			int done;
2205 
2206 			/*
2207 			 * Free the current request path- we're done with it.
2208 			 */
2209 			xpt_free_path(oldpath);
2210  hop_again:
2211 			done = 0;
2212 			if (scan_info->request_ccb->ccb_h.func_code == XPT_SCAN_TGT) {
2213 				done = 1;
2214 			} else if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
2215 				scan_info->counter++;
2216 				if (scan_info->counter ==
2217 				    scan_info->cpi->initiator_id) {
2218 					scan_info->counter++;
2219 				}
2220 				if (scan_info->counter >=
2221 				    scan_info->cpi->max_target+1) {
2222 					done = 1;
2223 				}
2224 			} else {
2225 				scan_info->counter--;
2226 				if (scan_info->counter == 0) {
2227 					done = 1;
2228 				}
2229 			}
2230 			if (done) {
2231 				mtx_unlock(mtx);
2232 				xpt_free_ccb(request_ccb);
2233 				xpt_free_ccb((union ccb *)scan_info->cpi);
2234 				request_ccb = scan_info->request_ccb;
2235 				CAM_DEBUG(request_ccb->ccb_h.path,
2236 				    CAM_DEBUG_TRACE,
2237 				   ("SCAN done for %p\n", scan_info));
2238 				free(scan_info, M_CAMXPT);
2239 				request_ccb->ccb_h.status = CAM_REQ_CMP;
2240 				xpt_done(request_ccb);
2241 				break;
2242 			}
2243 
2244 			if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
2245 				mtx_unlock(mtx);
2246 				xpt_free_ccb(request_ccb);
2247 				break;
2248 			}
2249 			status = xpt_create_path(&path, NULL,
2250 			    scan_info->request_ccb->ccb_h.path_id,
2251 			    scan_info->counter, 0);
2252 			if (status != CAM_REQ_CMP) {
2253 				mtx_unlock(mtx);
2254 				printf("scsi_scan_bus: xpt_create_path failed"
2255 				    " with status %#x, bus scan halted\n",
2256 			       	    status);
2257 				xpt_free_ccb(request_ccb);
2258 				xpt_free_ccb((union ccb *)scan_info->cpi);
2259 				request_ccb = scan_info->request_ccb;
2260 				free(scan_info, M_CAMXPT);
2261 				request_ccb->ccb_h.status = status;
2262 				xpt_done(request_ccb);
2263 				break;
2264 			}
2265 			xpt_setup_ccb(&request_ccb->ccb_h, path,
2266 			    request_ccb->ccb_h.pinfo.priority);
2267 			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2268 			request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2269 			request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2270 			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
2271 			request_ccb->crcn.flags =
2272 			    scan_info->request_ccb->crcn.flags;
2273 		} else {
2274 			status = xpt_create_path(&path, NULL,
2275 						 path_id, target_id, lun_id);
2276 			/*
2277 			 * Free the old request path- we're done with it. We
2278 			 * do this *after* creating the new path so that
2279 			 * we don't remove a target that has our lun list
2280 			 * in the case that lun 0 is not present.
2281 			 */
2282 			xpt_free_path(oldpath);
2283 			if (status != CAM_REQ_CMP) {
2284 				printf("scsi_scan_bus: xpt_create_path failed "
2285 				       "with status %#x, halting LUN scan\n",
2286 			 	       status);
2287 				goto hop_again;
2288 			}
2289 			xpt_setup_ccb(&request_ccb->ccb_h, path,
2290 				      request_ccb->ccb_h.pinfo.priority);
2291 			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2292 			request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2293 			request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2294 			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
2295 			request_ccb->crcn.flags =
2296 				scan_info->request_ccb->crcn.flags;
2297 		}
2298 		mtx_unlock(mtx);
2299 		xpt_action(request_ccb);
2300 		break;
2301 	}
2302 	default:
2303 		break;
2304 	}
2305 }
2306 
2307 static void
2308 scsi_scan_lun(struct cam_periph *periph, struct cam_path *path,
2309 	     cam_flags flags, union ccb *request_ccb)
2310 {
2311 	struct ccb_pathinq cpi;
2312 	cam_status status;
2313 	struct cam_path *new_path;
2314 	struct cam_periph *old_periph;
2315 	int lock;
2316 
2317 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("scsi_scan_lun\n"));
2318 
2319 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2320 	cpi.ccb_h.func_code = XPT_PATH_INQ;
2321 	xpt_action((union ccb *)&cpi);
2322 
2323 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
2324 		if (request_ccb != NULL) {
2325 			request_ccb->ccb_h.status = cpi.ccb_h.status;
2326 			xpt_done(request_ccb);
2327 		}
2328 		return;
2329 	}
2330 
2331 	if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
2332 		/*
2333 		 * Can't scan the bus on an adapter that
2334 		 * cannot perform the initiator role.
2335 		 */
2336 		if (request_ccb != NULL) {
2337 			request_ccb->ccb_h.status = CAM_REQ_CMP;
2338 			xpt_done(request_ccb);
2339 		}
2340 		return;
2341 	}
2342 
2343 	if (request_ccb == NULL) {
2344 		request_ccb = xpt_alloc_ccb_nowait();
2345 		if (request_ccb == NULL) {
2346 			xpt_print(path, "scsi_scan_lun: can't allocate CCB, "
2347 			    "can't continue\n");
2348 			return;
2349 		}
2350 		status = xpt_create_path(&new_path, NULL,
2351 					  path->bus->path_id,
2352 					  path->target->target_id,
2353 					  path->device->lun_id);
2354 		if (status != CAM_REQ_CMP) {
2355 			xpt_print(path, "scsi_scan_lun: can't create path, "
2356 			    "can't continue\n");
2357 			xpt_free_ccb(request_ccb);
2358 			return;
2359 		}
2360 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
2361 		request_ccb->ccb_h.cbfcnp = xptscandone;
2362 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2363 		request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2364 		request_ccb->crcn.flags = flags;
2365 	}
2366 
2367 	lock = (xpt_path_owned(path) == 0);
2368 	if (lock)
2369 		xpt_path_lock(path);
2370 	if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
2371 		if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
2372 			probe_softc *softc;
2373 
2374 			softc = (probe_softc *)old_periph->softc;
2375 			TAILQ_INSERT_TAIL(&softc->request_ccbs,
2376 			    &request_ccb->ccb_h, periph_links.tqe);
2377 		} else {
2378 			request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2379 			xpt_done(request_ccb);
2380 		}
2381 	} else {
2382 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
2383 					  probestart, "probe",
2384 					  CAM_PERIPH_BIO,
2385 					  request_ccb->ccb_h.path, NULL, 0,
2386 					  request_ccb);
2387 
2388 		if (status != CAM_REQ_CMP) {
2389 			xpt_print(path, "scsi_scan_lun: cam_alloc_periph "
2390 			    "returned an error, can't continue probe\n");
2391 			request_ccb->ccb_h.status = status;
2392 			xpt_done(request_ccb);
2393 		}
2394 	}
2395 	if (lock)
2396 		xpt_path_unlock(path);
2397 }
2398 
2399 static void
2400 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
2401 {
2402 
2403 	xpt_free_path(done_ccb->ccb_h.path);
2404 	xpt_free_ccb(done_ccb);
2405 }
2406 
2407 static struct cam_ed *
2408 scsi_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
2409 {
2410 	struct scsi_quirk_entry *quirk;
2411 	struct cam_ed *device;
2412 
2413 	device = xpt_alloc_device(bus, target, lun_id);
2414 	if (device == NULL)
2415 		return (NULL);
2416 
2417 	/*
2418 	 * Take the default quirk entry until we have inquiry
2419 	 * data and can determine a better quirk to use.
2420 	 */
2421 	quirk = &scsi_quirk_table[nitems(scsi_quirk_table) - 1];
2422 	device->quirk = (void *)quirk;
2423 	device->mintags = quirk->mintags;
2424 	device->maxtags = quirk->maxtags;
2425 	bzero(&device->inq_data, sizeof(device->inq_data));
2426 	device->inq_flags = 0;
2427 	device->queue_flags = 0;
2428 	device->serial_num = NULL;
2429 	device->serial_num_len = 0;
2430 	device->device_id = NULL;
2431 	device->device_id_len = 0;
2432 	device->supported_vpds = NULL;
2433 	device->supported_vpds_len = 0;
2434 	return (device);
2435 }
2436 
2437 static void
2438 scsi_devise_transport(struct cam_path *path)
2439 {
2440 	struct ccb_pathinq cpi;
2441 	struct ccb_trans_settings cts;
2442 	struct scsi_inquiry_data *inq_buf;
2443 
2444 	/* Get transport information from the SIM */
2445 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2446 	cpi.ccb_h.func_code = XPT_PATH_INQ;
2447 	xpt_action((union ccb *)&cpi);
2448 
2449 	inq_buf = NULL;
2450 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
2451 		inq_buf = &path->device->inq_data;
2452 	path->device->protocol = PROTO_SCSI;
2453 	path->device->protocol_version =
2454 	    inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
2455 	path->device->transport = cpi.transport;
2456 	path->device->transport_version = cpi.transport_version;
2457 
2458 	/*
2459 	 * Any device not using SPI3 features should
2460 	 * be considered SPI2 or lower.
2461 	 */
2462 	if (inq_buf != NULL) {
2463 		if (path->device->transport == XPORT_SPI
2464 		 && (inq_buf->spi3data & SID_SPI_MASK) == 0
2465 		 && path->device->transport_version > 2)
2466 			path->device->transport_version = 2;
2467 	} else {
2468 		struct cam_ed* otherdev;
2469 
2470 		for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
2471 		     otherdev != NULL;
2472 		     otherdev = TAILQ_NEXT(otherdev, links)) {
2473 			if (otherdev != path->device)
2474 				break;
2475 		}
2476 
2477 		if (otherdev != NULL) {
2478 			/*
2479 			 * Initially assume the same versioning as
2480 			 * prior luns for this target.
2481 			 */
2482 			path->device->protocol_version =
2483 			    otherdev->protocol_version;
2484 			path->device->transport_version =
2485 			    otherdev->transport_version;
2486 		} else {
2487 			/* Until we know better, opt for safety */
2488 			path->device->protocol_version = 2;
2489 			if (path->device->transport == XPORT_SPI)
2490 				path->device->transport_version = 2;
2491 			else
2492 				path->device->transport_version = 0;
2493 		}
2494 	}
2495 
2496 	/*
2497 	 * XXX
2498 	 * For a device compliant with SPC-2 we should be able
2499 	 * to determine the transport version supported by
2500 	 * scrutinizing the version descriptors in the
2501 	 * inquiry buffer.
2502 	 */
2503 
2504 	/* Tell the controller what we think */
2505 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2506 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
2507 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
2508 	cts.transport = path->device->transport;
2509 	cts.transport_version = path->device->transport_version;
2510 	cts.protocol = path->device->protocol;
2511 	cts.protocol_version = path->device->protocol_version;
2512 	cts.proto_specific.valid = 0;
2513 	cts.xport_specific.valid = 0;
2514 	xpt_action((union ccb *)&cts);
2515 }
2516 
2517 static void
2518 scsi_dev_advinfo(union ccb *start_ccb)
2519 {
2520 	struct cam_ed *device;
2521 	struct ccb_dev_advinfo *cdai;
2522 	off_t amt;
2523 
2524 	start_ccb->ccb_h.status = CAM_REQ_INVALID;
2525 	device = start_ccb->ccb_h.path->device;
2526 	cdai = &start_ccb->cdai;
2527 	switch(cdai->buftype) {
2528 	case CDAI_TYPE_SCSI_DEVID:
2529 		if (cdai->flags & CDAI_FLAG_STORE)
2530 			return;
2531 		cdai->provsiz = device->device_id_len;
2532 		if (device->device_id_len == 0)
2533 			break;
2534 		amt = device->device_id_len;
2535 		if (cdai->provsiz > cdai->bufsiz)
2536 			amt = cdai->bufsiz;
2537 		memcpy(cdai->buf, device->device_id, amt);
2538 		break;
2539 	case CDAI_TYPE_SERIAL_NUM:
2540 		if (cdai->flags & CDAI_FLAG_STORE)
2541 			return;
2542 		cdai->provsiz = device->serial_num_len;
2543 		if (device->serial_num_len == 0)
2544 			break;
2545 		amt = device->serial_num_len;
2546 		if (cdai->provsiz > cdai->bufsiz)
2547 			amt = cdai->bufsiz;
2548 		memcpy(cdai->buf, device->serial_num, amt);
2549 		break;
2550 	case CDAI_TYPE_PHYS_PATH:
2551 		if (cdai->flags & CDAI_FLAG_STORE) {
2552 			if (device->physpath != NULL) {
2553 				free(device->physpath, M_CAMXPT);
2554 				device->physpath = NULL;
2555 				device->physpath_len = 0;
2556 			}
2557 			/* Clear existing buffer if zero length */
2558 			if (cdai->bufsiz == 0)
2559 				break;
2560 			device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
2561 			if (device->physpath == NULL) {
2562 				start_ccb->ccb_h.status = CAM_REQ_ABORTED;
2563 				return;
2564 			}
2565 			device->physpath_len = cdai->bufsiz;
2566 			memcpy(device->physpath, cdai->buf, cdai->bufsiz);
2567 		} else {
2568 			cdai->provsiz = device->physpath_len;
2569 			if (device->physpath_len == 0)
2570 				break;
2571 			amt = device->physpath_len;
2572 			if (cdai->provsiz > cdai->bufsiz)
2573 				amt = cdai->bufsiz;
2574 			memcpy(cdai->buf, device->physpath, amt);
2575 		}
2576 		break;
2577 	case CDAI_TYPE_RCAPLONG:
2578 		if (cdai->flags & CDAI_FLAG_STORE) {
2579 			if (device->rcap_buf != NULL) {
2580 				free(device->rcap_buf, M_CAMXPT);
2581 				device->rcap_buf = NULL;
2582 			}
2583 
2584 			device->rcap_len = cdai->bufsiz;
2585 			/* Clear existing buffer if zero length */
2586 			if (cdai->bufsiz == 0)
2587 				break;
2588 
2589 			device->rcap_buf = malloc(cdai->bufsiz, M_CAMXPT,
2590 						  M_NOWAIT);
2591 			if (device->rcap_buf == NULL) {
2592 				start_ccb->ccb_h.status = CAM_REQ_ABORTED;
2593 				return;
2594 			}
2595 
2596 			memcpy(device->rcap_buf, cdai->buf, cdai->bufsiz);
2597 		} else {
2598 			cdai->provsiz = device->rcap_len;
2599 			if (device->rcap_len == 0)
2600 				break;
2601 			amt = device->rcap_len;
2602 			if (cdai->provsiz > cdai->bufsiz)
2603 				amt = cdai->bufsiz;
2604 			memcpy(cdai->buf, device->rcap_buf, amt);
2605 		}
2606 		break;
2607 	case CDAI_TYPE_EXT_INQ:
2608 		/*
2609 		 * We fetch extended inquiry data during probe, if
2610 		 * available.  We don't allow changing it.
2611 		 */
2612 		if (cdai->flags & CDAI_FLAG_STORE)
2613 			return;
2614 		cdai->provsiz = device->ext_inq_len;
2615 		if (device->ext_inq_len == 0)
2616 			break;
2617 		amt = device->ext_inq_len;
2618 		if (cdai->provsiz > cdai->bufsiz)
2619 			amt = cdai->bufsiz;
2620 		memcpy(cdai->buf, device->ext_inq, amt);
2621 		break;
2622 	default:
2623 		return;
2624 	}
2625 	start_ccb->ccb_h.status = CAM_REQ_CMP;
2626 
2627 	if (cdai->flags & CDAI_FLAG_STORE) {
2628 		xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
2629 			  (void *)(uintptr_t)cdai->buftype);
2630 	}
2631 }
2632 
2633 static void
2634 scsi_action(union ccb *start_ccb)
2635 {
2636 
2637 	switch (start_ccb->ccb_h.func_code) {
2638 	case XPT_SET_TRAN_SETTINGS:
2639 	{
2640 		scsi_set_transfer_settings(&start_ccb->cts,
2641 					   start_ccb->ccb_h.path,
2642 					   /*async_update*/FALSE);
2643 		break;
2644 	}
2645 	case XPT_SCAN_BUS:
2646 	case XPT_SCAN_TGT:
2647 		scsi_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
2648 		break;
2649 	case XPT_SCAN_LUN:
2650 		scsi_scan_lun(start_ccb->ccb_h.path->periph,
2651 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
2652 			      start_ccb);
2653 		break;
2654 	case XPT_DEV_ADVINFO:
2655 	{
2656 		scsi_dev_advinfo(start_ccb);
2657 		break;
2658 	}
2659 	default:
2660 		xpt_action_default(start_ccb);
2661 		break;
2662 	}
2663 }
2664 
2665 static void
2666 scsi_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_path *path,
2667 			   int async_update)
2668 {
2669 	struct	ccb_pathinq cpi;
2670 	struct	ccb_trans_settings cur_cts;
2671 	struct	ccb_trans_settings_scsi *scsi;
2672 	struct	ccb_trans_settings_scsi *cur_scsi;
2673 	struct	scsi_inquiry_data *inq_data;
2674 	struct	cam_ed *device;
2675 
2676 	if (path == NULL || (device = path->device) == NULL) {
2677 		cts->ccb_h.status = CAM_PATH_INVALID;
2678 		xpt_done((union ccb *)cts);
2679 		return;
2680 	}
2681 
2682 	if (cts->protocol == PROTO_UNKNOWN
2683 	 || cts->protocol == PROTO_UNSPECIFIED) {
2684 		cts->protocol = device->protocol;
2685 		cts->protocol_version = device->protocol_version;
2686 	}
2687 
2688 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
2689 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
2690 		cts->protocol_version = device->protocol_version;
2691 
2692 	if (cts->protocol != device->protocol) {
2693 		xpt_print(path, "Uninitialized Protocol %x:%x?\n",
2694 		       cts->protocol, device->protocol);
2695 		cts->protocol = device->protocol;
2696 	}
2697 
2698 	if (cts->protocol_version > device->protocol_version) {
2699 		if (bootverbose) {
2700 			xpt_print(path, "Down reving Protocol "
2701 			    "Version from %d to %d?\n", cts->protocol_version,
2702 			    device->protocol_version);
2703 		}
2704 		cts->protocol_version = device->protocol_version;
2705 	}
2706 
2707 	if (cts->transport == XPORT_UNKNOWN
2708 	 || cts->transport == XPORT_UNSPECIFIED) {
2709 		cts->transport = device->transport;
2710 		cts->transport_version = device->transport_version;
2711 	}
2712 
2713 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
2714 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
2715 		cts->transport_version = device->transport_version;
2716 
2717 	if (cts->transport != device->transport) {
2718 		xpt_print(path, "Uninitialized Transport %x:%x?\n",
2719 		    cts->transport, device->transport);
2720 		cts->transport = device->transport;
2721 	}
2722 
2723 	if (cts->transport_version > device->transport_version) {
2724 		if (bootverbose) {
2725 			xpt_print(path, "Down reving Transport "
2726 			    "Version from %d to %d?\n", cts->transport_version,
2727 			    device->transport_version);
2728 		}
2729 		cts->transport_version = device->transport_version;
2730 	}
2731 
2732 	/*
2733 	 * Nothing more of interest to do unless
2734 	 * this is a device connected via the
2735 	 * SCSI protocol.
2736 	 */
2737 	if (cts->protocol != PROTO_SCSI) {
2738 		if (async_update == FALSE)
2739 			xpt_action_default((union ccb *)cts);
2740 		return;
2741 	}
2742 
2743 	inq_data = &device->inq_data;
2744 	scsi = &cts->proto_specific.scsi;
2745 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2746 	cpi.ccb_h.func_code = XPT_PATH_INQ;
2747 	xpt_action((union ccb *)&cpi);
2748 
2749 	/* SCSI specific sanity checking */
2750 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
2751 	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
2752 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
2753 	 || (device->mintags == 0)) {
2754 		/*
2755 		 * Can't tag on hardware that doesn't support tags,
2756 		 * doesn't have it enabled, or has broken tag support.
2757 		 */
2758 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2759 	}
2760 
2761 	if (async_update == FALSE) {
2762 		/*
2763 		 * Perform sanity checking against what the
2764 		 * controller and device can do.
2765 		 */
2766 		xpt_setup_ccb(&cur_cts.ccb_h, path, CAM_PRIORITY_NONE);
2767 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2768 		cur_cts.type = cts->type;
2769 		xpt_action((union ccb *)&cur_cts);
2770 		if (cam_ccb_status((union ccb *)&cur_cts) != CAM_REQ_CMP) {
2771 			return;
2772 		}
2773 		cur_scsi = &cur_cts.proto_specific.scsi;
2774 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
2775 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2776 			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
2777 		}
2778 		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
2779 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2780 	}
2781 
2782 	/* SPI specific sanity checking */
2783 	if (cts->transport == XPORT_SPI && async_update == FALSE) {
2784 		u_int spi3caps;
2785 		struct ccb_trans_settings_spi *spi;
2786 		struct ccb_trans_settings_spi *cur_spi;
2787 
2788 		spi = &cts->xport_specific.spi;
2789 
2790 		cur_spi = &cur_cts.xport_specific.spi;
2791 
2792 		/* Fill in any gaps in what the user gave us */
2793 		if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2794 			spi->sync_period = cur_spi->sync_period;
2795 		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2796 			spi->sync_period = 0;
2797 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2798 			spi->sync_offset = cur_spi->sync_offset;
2799 		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2800 			spi->sync_offset = 0;
2801 		if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2802 			spi->ppr_options = cur_spi->ppr_options;
2803 		if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2804 			spi->ppr_options = 0;
2805 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2806 			spi->bus_width = cur_spi->bus_width;
2807 		if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2808 			spi->bus_width = 0;
2809 		if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
2810 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2811 			spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
2812 		}
2813 		if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
2814 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2815 		if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2816 		  && (inq_data->flags & SID_Sync) == 0
2817 		  && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2818 		 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)) {
2819 			/* Force async */
2820 			spi->sync_period = 0;
2821 			spi->sync_offset = 0;
2822 		}
2823 
2824 		switch (spi->bus_width) {
2825 		case MSG_EXT_WDTR_BUS_32_BIT:
2826 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2827 			  || (inq_data->flags & SID_WBus32) != 0
2828 			  || cts->type == CTS_TYPE_USER_SETTINGS)
2829 			 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
2830 				break;
2831 			/* Fall Through to 16-bit */
2832 		case MSG_EXT_WDTR_BUS_16_BIT:
2833 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2834 			  || (inq_data->flags & SID_WBus16) != 0
2835 			  || cts->type == CTS_TYPE_USER_SETTINGS)
2836 			 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
2837 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2838 				break;
2839 			}
2840 			/* Fall Through to 8-bit */
2841 		default: /* New bus width?? */
2842 		case MSG_EXT_WDTR_BUS_8_BIT:
2843 			/* All targets can do this */
2844 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2845 			break;
2846 		}
2847 
2848 		spi3caps = cpi.xport_specific.spi.ppr_options;
2849 		if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2850 		 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2851 			spi3caps &= inq_data->spi3data;
2852 
2853 		if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
2854 			spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2855 
2856 		if ((spi3caps & SID_SPI_IUS) == 0)
2857 			spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
2858 
2859 		if ((spi3caps & SID_SPI_QAS) == 0)
2860 			spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
2861 
2862 		/* No SPI Transfer settings are allowed unless we are wide */
2863 		if (spi->bus_width == 0)
2864 			spi->ppr_options = 0;
2865 
2866 		if ((spi->valid & CTS_SPI_VALID_DISC)
2867 		 && ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0)) {
2868 			/*
2869 			 * Can't tag queue without disconnection.
2870 			 */
2871 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2872 			scsi->valid |= CTS_SCSI_VALID_TQ;
2873 		}
2874 
2875 		/*
2876 		 * If we are currently performing tagged transactions to
2877 		 * this device and want to change its negotiation parameters,
2878 		 * go non-tagged for a bit to give the controller a chance to
2879 		 * negotiate unhampered by tag messages.
2880 		 */
2881 		if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2882 		 && (device->inq_flags & SID_CmdQue) != 0
2883 		 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2884 		 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
2885 				   CTS_SPI_VALID_SYNC_OFFSET|
2886 				   CTS_SPI_VALID_BUS_WIDTH)) != 0)
2887 			scsi_toggle_tags(path);
2888 	}
2889 
2890 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2891 	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
2892 		int device_tagenb;
2893 
2894 		/*
2895 		 * If we are transitioning from tags to no-tags or
2896 		 * vice-versa, we need to carefully freeze and restart
2897 		 * the queue so that we don't overlap tagged and non-tagged
2898 		 * commands.  We also temporarily stop tags if there is
2899 		 * a change in transfer negotiation settings to allow
2900 		 * "tag-less" negotiation.
2901 		 */
2902 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2903 		 || (device->inq_flags & SID_CmdQue) != 0)
2904 			device_tagenb = TRUE;
2905 		else
2906 			device_tagenb = FALSE;
2907 
2908 		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2909 		  && device_tagenb == FALSE)
2910 		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
2911 		  && device_tagenb == TRUE)) {
2912 
2913 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
2914 				/*
2915 				 * Delay change to use tags until after a
2916 				 * few commands have gone to this device so
2917 				 * the controller has time to perform transfer
2918 				 * negotiations without tagged messages getting
2919 				 * in the way.
2920 				 */
2921 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
2922 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
2923 			} else {
2924 				xpt_stop_tags(path);
2925 			}
2926 		}
2927 	}
2928 	if (async_update == FALSE)
2929 		xpt_action_default((union ccb *)cts);
2930 }
2931 
2932 static void
2933 scsi_toggle_tags(struct cam_path *path)
2934 {
2935 	struct cam_ed *dev;
2936 
2937 	/*
2938 	 * Give controllers a chance to renegotiate
2939 	 * before starting tag operations.  We
2940 	 * "toggle" tagged queuing off then on
2941 	 * which causes the tag enable command delay
2942 	 * counter to come into effect.
2943 	 */
2944 	dev = path->device;
2945 	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2946 	 || ((dev->inq_flags & SID_CmdQue) != 0
2947  	  && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
2948 		struct ccb_trans_settings cts;
2949 
2950 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2951 		cts.protocol = PROTO_SCSI;
2952 		cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
2953 		cts.transport = XPORT_UNSPECIFIED;
2954 		cts.transport_version = XPORT_VERSION_UNSPECIFIED;
2955 		cts.proto_specific.scsi.flags = 0;
2956 		cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
2957 		scsi_set_transfer_settings(&cts, path,
2958 					  /*async_update*/TRUE);
2959 		cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
2960 		scsi_set_transfer_settings(&cts, path,
2961 					  /*async_update*/TRUE);
2962 	}
2963 }
2964 
2965 /*
2966  * Handle any per-device event notifications that require action by the XPT.
2967  */
2968 static void
2969 scsi_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
2970 	      struct cam_ed *device, void *async_arg)
2971 {
2972 	cam_status status;
2973 	struct cam_path newpath;
2974 
2975 	/*
2976 	 * We only need to handle events for real devices.
2977 	 */
2978 	if (target->target_id == CAM_TARGET_WILDCARD
2979 	 || device->lun_id == CAM_LUN_WILDCARD)
2980 		return;
2981 
2982 	/*
2983 	 * We need our own path with wildcards expanded to
2984 	 * handle certain types of events.
2985 	 */
2986 	if ((async_code == AC_SENT_BDR)
2987 	 || (async_code == AC_BUS_RESET)
2988 	 || (async_code == AC_INQ_CHANGED))
2989 		status = xpt_compile_path(&newpath, NULL,
2990 					  bus->path_id,
2991 					  target->target_id,
2992 					  device->lun_id);
2993 	else
2994 		status = CAM_REQ_CMP_ERR;
2995 
2996 	if (status == CAM_REQ_CMP) {
2997 
2998 		/*
2999 		 * Allow transfer negotiation to occur in a
3000 		 * tag free environment and after settle delay.
3001 		 */
3002 		if (async_code == AC_SENT_BDR
3003 		 || async_code == AC_BUS_RESET) {
3004 			cam_freeze_devq(&newpath);
3005 			cam_release_devq(&newpath,
3006 				RELSIM_RELEASE_AFTER_TIMEOUT,
3007 				/*reduction*/0,
3008 				/*timeout*/scsi_delay,
3009 				/*getcount_only*/0);
3010 			scsi_toggle_tags(&newpath);
3011 		}
3012 
3013 		if (async_code == AC_INQ_CHANGED) {
3014 			/*
3015 			 * We've sent a start unit command, or
3016 			 * something similar to a device that
3017 			 * may have caused its inquiry data to
3018 			 * change. So we re-scan the device to
3019 			 * refresh the inquiry data for it.
3020 			 */
3021 			scsi_scan_lun(newpath.periph, &newpath,
3022 				     CAM_EXPECT_INQ_CHANGE, NULL);
3023 		}
3024 		xpt_release_path(&newpath);
3025 	} else if (async_code == AC_LOST_DEVICE &&
3026 	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
3027 		device->flags |= CAM_DEV_UNCONFIGURED;
3028 		xpt_release_device(device);
3029 	} else if (async_code == AC_TRANSFER_NEG) {
3030 		struct ccb_trans_settings *settings;
3031 		struct cam_path path;
3032 
3033 		settings = (struct ccb_trans_settings *)async_arg;
3034 		xpt_compile_path(&path, NULL, bus->path_id, target->target_id,
3035 				 device->lun_id);
3036 		scsi_set_transfer_settings(settings, &path,
3037 					  /*async_update*/TRUE);
3038 		xpt_release_path(&path);
3039 	}
3040 }
3041 
3042 static void
3043 _scsi_announce_periph(struct cam_periph *periph, u_int *speed, u_int *freq, struct ccb_trans_settings *cts)
3044 {
3045 	struct	ccb_pathinq cpi;
3046 	struct	cam_path *path = periph->path;
3047 
3048 	cam_periph_assert(periph, MA_OWNED);
3049 
3050 	xpt_setup_ccb(&cts->ccb_h, path, CAM_PRIORITY_NORMAL);
3051 	cts->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
3052 	cts->type = CTS_TYPE_CURRENT_SETTINGS;
3053 	xpt_action((union ccb*)cts);
3054 	if (cam_ccb_status((union ccb *)cts) != CAM_REQ_CMP)
3055 		return;
3056 
3057 	/* Ask the SIM for its base transfer speed */
3058 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3059 	cpi.ccb_h.func_code = XPT_PATH_INQ;
3060 	xpt_action((union ccb *)&cpi);
3061 
3062 	/* Report connection speed */
3063 	*speed = cpi.base_transfer_speed;
3064 	*freq = 0;
3065 
3066 	if (cts->ccb_h.status == CAM_REQ_CMP && cts->transport == XPORT_SPI) {
3067 		struct	ccb_trans_settings_spi *spi =
3068 		    &cts->xport_specific.spi;
3069 
3070 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
3071 		  && spi->sync_offset != 0) {
3072 			*freq = scsi_calc_syncsrate(spi->sync_period);
3073 			*speed = *freq;
3074 		}
3075 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
3076 			*speed *= (0x01 << spi->bus_width);
3077 	}
3078 	if (cts->ccb_h.status == CAM_REQ_CMP && cts->transport == XPORT_FC) {
3079 		struct	ccb_trans_settings_fc *fc =
3080 		    &cts->xport_specific.fc;
3081 
3082 		if (fc->valid & CTS_FC_VALID_SPEED)
3083 			*speed = fc->bitrate;
3084 	}
3085 	if (cts->ccb_h.status == CAM_REQ_CMP && cts->transport == XPORT_SAS) {
3086 		struct	ccb_trans_settings_sas *sas =
3087 		    &cts->xport_specific.sas;
3088 
3089 		if (sas->valid & CTS_SAS_VALID_SPEED)
3090 			*speed = sas->bitrate;
3091 	}
3092 }
3093 
3094 static void
3095 scsi_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb)
3096 {
3097 	struct	ccb_trans_settings cts;
3098 	u_int speed, freq, mb;
3099 
3100 	_scsi_announce_periph(periph, &speed, &freq, &cts);
3101 	if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP)
3102 		return;
3103 
3104 	mb = speed / 1000;
3105 	if (mb > 0)
3106 		sbuf_printf(sb, "%s%d: %d.%03dMB/s transfers",
3107 		       periph->periph_name, periph->unit_number,
3108 		       mb, speed % 1000);
3109 	else
3110 		sbuf_printf(sb, "%s%d: %dKB/s transfers", periph->periph_name,
3111 		       periph->unit_number, speed);
3112 	/* Report additional information about SPI connections */
3113 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
3114 		struct	ccb_trans_settings_spi *spi;
3115 
3116 		spi = &cts.xport_specific.spi;
3117 		if (freq != 0) {
3118 			sbuf_printf(sb, " (%d.%03dMHz%s, offset %d", freq / 1000,
3119 			       freq % 1000,
3120 			       (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
3121 			     ? " DT" : "",
3122 			       spi->sync_offset);
3123 		}
3124 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
3125 		 && spi->bus_width > 0) {
3126 			if (freq != 0) {
3127 				sbuf_printf(sb, ", ");
3128 			} else {
3129 				sbuf_printf(sb, " (");
3130 			}
3131 			sbuf_printf(sb, "%dbit)", 8 * (0x01 << spi->bus_width));
3132 		} else if (freq != 0) {
3133 			sbuf_printf(sb, ")");
3134 		}
3135 	}
3136 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
3137 		struct	ccb_trans_settings_fc *fc;
3138 
3139 		fc = &cts.xport_specific.fc;
3140 		if (fc->valid & CTS_FC_VALID_WWNN)
3141 			sbuf_printf(sb, " WWNN 0x%llx", (long long) fc->wwnn);
3142 		if (fc->valid & CTS_FC_VALID_WWPN)
3143 			sbuf_printf(sb, " WWPN 0x%llx", (long long) fc->wwpn);
3144 		if (fc->valid & CTS_FC_VALID_PORT)
3145 			sbuf_printf(sb, " PortID 0x%x", fc->port);
3146 	}
3147 	sbuf_printf(sb, "\n");
3148 }
3149 
3150 static void
3151 scsi_announce_periph(struct cam_periph *periph)
3152 {
3153 	struct	ccb_trans_settings cts;
3154 	u_int speed, freq, mb;
3155 
3156 	_scsi_announce_periph(periph, &speed, &freq, &cts);
3157 	if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP)
3158 		return;
3159 
3160 	mb = speed / 1000;
3161 	if (mb > 0)
3162 		printf("%s%d: %d.%03dMB/s transfers",
3163 		       periph->periph_name, periph->unit_number,
3164 		       mb, speed % 1000);
3165 	else
3166 		printf("%s%d: %dKB/s transfers", periph->periph_name,
3167 		       periph->unit_number, speed);
3168 	/* Report additional information about SPI connections */
3169 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
3170 		struct	ccb_trans_settings_spi *spi;
3171 
3172 		spi = &cts.xport_specific.spi;
3173 		if (freq != 0) {
3174 			printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
3175 			       freq % 1000,
3176 			       (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
3177 			     ? " DT" : "",
3178 			       spi->sync_offset);
3179 		}
3180 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
3181 		 && spi->bus_width > 0) {
3182 			if (freq != 0) {
3183 				printf(", ");
3184 			} else {
3185 				printf(" (");
3186 			}
3187 			printf("%dbit)", 8 * (0x01 << spi->bus_width));
3188 		} else if (freq != 0) {
3189 			printf(")");
3190 		}
3191 	}
3192 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
3193 		struct	ccb_trans_settings_fc *fc;
3194 
3195 		fc = &cts.xport_specific.fc;
3196 		if (fc->valid & CTS_FC_VALID_WWNN)
3197 			printf(" WWNN 0x%llx", (long long) fc->wwnn);
3198 		if (fc->valid & CTS_FC_VALID_WWPN)
3199 			printf(" WWPN 0x%llx", (long long) fc->wwpn);
3200 		if (fc->valid & CTS_FC_VALID_PORT)
3201 			printf(" PortID 0x%x", fc->port);
3202 	}
3203 	printf("\n");
3204 }
3205 
3206 static void
3207 scsi_proto_announce_sbuf(struct cam_ed *device, struct sbuf *sb)
3208 {
3209 	scsi_print_inquiry_sbuf(sb, &device->inq_data);
3210 }
3211 
3212 static void
3213 scsi_proto_announce(struct cam_ed *device)
3214 {
3215 	scsi_print_inquiry(&device->inq_data);
3216 }
3217 
3218 static void
3219 scsi_proto_denounce_sbuf(struct cam_ed *device, struct sbuf *sb)
3220 {
3221 	scsi_print_inquiry_short_sbuf(sb, &device->inq_data);
3222 }
3223 
3224 static void
3225 scsi_proto_denounce(struct cam_ed *device)
3226 {
3227 	scsi_print_inquiry_short(&device->inq_data);
3228 }
3229 
3230 static void
3231 scsi_proto_debug_out(union ccb *ccb)
3232 {
3233 	char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
3234 	struct cam_ed *device;
3235 
3236 	if (ccb->ccb_h.func_code != XPT_SCSI_IO)
3237 		return;
3238 
3239 	device = ccb->ccb_h.path->device;
3240 	CAM_DEBUG(ccb->ccb_h.path,
3241 	    CAM_DEBUG_CDB,("%s. CDB: %s\n",
3242 		scsi_op_desc(scsiio_cdb_ptr(&ccb->csio)[0], &device->inq_data),
3243 		scsi_cdb_string(scsiio_cdb_ptr(&ccb->csio), cdb_str, sizeof(cdb_str))));
3244 }
3245