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