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