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