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