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