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