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