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