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