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