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