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