xref: /freebsd/sys/cam/scsi/scsi_xpt.c (revision 5608fd23c27fa1e8ee595d7b678cbfd35d657fbe)
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 			inq_buf = &path->device->inq_data;
1139 
1140 			periph_qual = SID_QUAL(inq_buf);
1141 
1142 			if (periph_qual == SID_QUAL_LU_CONNECTED) {
1143 				u_int8_t len;
1144 
1145 				/*
1146 				 * We conservatively request only
1147 				 * SHORT_INQUIRY_LEN bytes of inquiry
1148 				 * information during our first try
1149 				 * at sending an INQUIRY. If the device
1150 				 * has more information to give,
1151 				 * perform a second request specifying
1152 				 * the amount of information the device
1153 				 * is willing to give.
1154 				 */
1155 				len = inq_buf->additional_length
1156 				    + offsetof(struct scsi_inquiry_data,
1157                                                additional_length) + 1;
1158 				if (softc->action == PROBE_INQUIRY
1159 				    && len > SHORT_INQUIRY_LENGTH) {
1160 					PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1161 					xpt_release_ccb(done_ccb);
1162 					xpt_schedule(periph, priority);
1163 					goto out;
1164 				}
1165 
1166 				scsi_find_quirk(path->device);
1167 
1168 				scsi_devise_transport(path);
1169 
1170 				if (path->device->lun_id == 0 &&
1171 				    SID_ANSI_REV(inq_buf) > SCSI_REV_SPC2 &&
1172 				    (SCSI_QUIRK(path->device)->quirks &
1173 				     CAM_QUIRK_NORPTLUNS) == 0) {
1174 					PROBE_SET_ACTION(softc,
1175 					    PROBE_REPORT_LUNS);
1176 					/*
1177 					 * Start with room for *one* lun.
1178 					 */
1179 					periph->path->target->rpl_size = 16;
1180 				} else if (INQ_DATA_TQ_ENABLED(inq_buf))
1181 					PROBE_SET_ACTION(softc,
1182 					    PROBE_MODE_SENSE);
1183 				else
1184 					PROBE_SET_ACTION(softc,
1185 					    PROBE_SUPPORTED_VPD_LIST);
1186 
1187 				if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1188 					path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1189 					xpt_acquire_device(path->device);
1190 				}
1191 				xpt_release_ccb(done_ccb);
1192 				xpt_schedule(periph, priority);
1193 				goto out;
1194 			} else if (path->device->lun_id == 0 &&
1195 			    SID_ANSI_REV(inq_buf) > SCSI_REV_SPC2 &&
1196 			    (SCSI_QUIRK(path->device)->quirks &
1197 			     CAM_QUIRK_NORPTLUNS) == 0) {
1198 				if (path->device->flags &
1199 				    CAM_DEV_UNCONFIGURED) {
1200 					path->device->flags &=
1201 					    ~CAM_DEV_UNCONFIGURED;
1202 					xpt_acquire_device(path->device);
1203 				}
1204 				PROBE_SET_ACTION(softc, PROBE_REPORT_LUNS);
1205 				periph->path->target->rpl_size = 16;
1206 				xpt_release_ccb(done_ccb);
1207 				xpt_schedule(periph, priority);
1208 				goto out;
1209 			}
1210 		} else if (cam_periph_error(done_ccb, 0,
1211 					    done_ccb->ccb_h.target_lun > 0
1212 					    ? SF_RETRY_UA|SF_QUIET_IR
1213 					    : SF_RETRY_UA,
1214 					    &softc->saved_ccb) == ERESTART) {
1215 			goto outr;
1216 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1217 			/* Don't wedge the queue */
1218 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1219 					 /*run_queue*/TRUE);
1220 		}
1221 		/*
1222 		 * If we get to this point, we got an error status back
1223 		 * from the inquiry and the error status doesn't require
1224 		 * automatically retrying the command.  Therefore, the
1225 		 * inquiry failed.  If we had inquiry information before
1226 		 * for this device, but this latest inquiry command failed,
1227 		 * the device has probably gone away.  If this device isn't
1228 		 * already marked unconfigured, notify the peripheral
1229 		 * drivers that this device is no more.
1230 		 */
1231 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
1232 			/* Send the async notification. */
1233 			xpt_async(AC_LOST_DEVICE, path, NULL);
1234 		PROBE_SET_ACTION(softc, PROBE_INVALID);
1235 
1236 		xpt_release_ccb(done_ccb);
1237 		break;
1238 	}
1239 	case PROBE_REPORT_LUNS:
1240 	{
1241 		struct ccb_scsiio *csio;
1242 		struct scsi_report_luns_data *lp;
1243 		u_int nlun, maxlun;
1244 
1245 		csio = &done_ccb->csio;
1246 
1247 		lp = (struct scsi_report_luns_data *)csio->data_ptr;
1248 		nlun = scsi_4btoul(lp->length) / 8;
1249 		maxlun = (csio->dxfer_len / 8) - 1;
1250 
1251 		if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1252 			if (cam_periph_error(done_ccb, 0,
1253 			    done_ccb->ccb_h.target_lun > 0 ?
1254 			    SF_RETRY_UA|SF_QUIET_IR : SF_RETRY_UA,
1255 			    &softc->saved_ccb) == ERESTART) {
1256 				goto outr;
1257 			}
1258 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1259 				xpt_release_devq(done_ccb->ccb_h.path, 1,
1260 				    TRUE);
1261 			}
1262 			free(lp, M_CAMXPT);
1263 			lp = NULL;
1264 		} else if (nlun > maxlun) {
1265 			/*
1266 			 * Reallocate and retry to cover all luns
1267 			 */
1268 			CAM_DEBUG(path, CAM_DEBUG_PROBE,
1269 			    ("Probe: reallocating REPORT_LUNS for %u luns\n",
1270 			     nlun));
1271 			free(lp, M_CAMXPT);
1272 			path->target->rpl_size = (nlun << 3) + 8;
1273 			xpt_release_ccb(done_ccb);
1274 			xpt_schedule(periph, priority);
1275 			goto out;
1276 		} else if (nlun == 0) {
1277 			/*
1278 			 * If there don't appear to be any luns, bail.
1279 			 */
1280 			free(lp, M_CAMXPT);
1281 			lp = NULL;
1282 		} else {
1283 			lun_id_t lun;
1284 			int idx;
1285 
1286 			CAM_DEBUG(path, CAM_DEBUG_PROBE,
1287 			   ("Probe: %u lun(s) reported\n", nlun));
1288 
1289 			CAM_GET_LUN(lp, 0, lun);
1290 			/*
1291 			 * If the first lun is not lun 0, then either there
1292 			 * is no lun 0 in the list, or the list is unsorted.
1293 			 */
1294 			if (lun != 0) {
1295 				for (idx = 0; idx < nlun; idx++) {
1296 					CAM_GET_LUN(lp, idx, lun);
1297 					if (lun == 0) {
1298 						break;
1299 					}
1300 				}
1301 				if (idx != nlun) {
1302 					uint8_t tlun[8];
1303 					memcpy(tlun,
1304 					    lp->luns[0].lundata, 8);
1305 					memcpy(lp->luns[0].lundata,
1306 					    lp->luns[idx].lundata, 8);
1307 					memcpy(lp->luns[idx].lundata,
1308 					    tlun, 8);
1309 					CAM_DEBUG(path, CAM_DEBUG_PROBE,
1310 					    ("lun 0 in position %u\n", idx));
1311 				} else {
1312 					/*
1313 					 * There is no lun 0 in our list. Destroy
1314 					 * the validity of the inquiry data so we
1315 					 * bail here and now.
1316 					 */
1317 					path->device->flags &=
1318 					    ~CAM_DEV_INQUIRY_DATA_VALID;
1319 				}
1320 			}
1321 			/*
1322 			 * If we have an old lun list, We can either
1323 			 * retest luns that appear to have been dropped,
1324 			 * or just nuke them.  We'll opt for the latter.
1325 			 * This function will also install the new list
1326 			 * in the target structure.
1327 			 */
1328 			probe_purge_old(path, lp, softc->flags);
1329 			lp = NULL;
1330 		}
1331 		if (path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) {
1332 			struct scsi_inquiry_data *inq_buf;
1333 			inq_buf = &path->device->inq_data;
1334 			if (INQ_DATA_TQ_ENABLED(inq_buf))
1335 				PROBE_SET_ACTION(softc, PROBE_MODE_SENSE);
1336 			else
1337 				PROBE_SET_ACTION(softc,
1338 				    PROBE_SUPPORTED_VPD_LIST);
1339 			xpt_release_ccb(done_ccb);
1340 			xpt_schedule(periph, priority);
1341 			goto out;
1342 		}
1343 		if (lp) {
1344 			free(lp, M_CAMXPT);
1345 		}
1346 		break;
1347 	}
1348 	case PROBE_MODE_SENSE:
1349 	{
1350 		struct ccb_scsiio *csio;
1351 		struct scsi_mode_header_6 *mode_hdr;
1352 
1353 		csio = &done_ccb->csio;
1354 		mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
1355 		if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
1356 			struct scsi_control_page *page;
1357 			u_int8_t *offset;
1358 
1359 			offset = ((u_int8_t *)&mode_hdr[1])
1360 			    + mode_hdr->blk_desc_len;
1361 			page = (struct scsi_control_page *)offset;
1362 			path->device->queue_flags = page->queue_flags;
1363 		} else if (cam_periph_error(done_ccb, 0,
1364 					    SF_RETRY_UA|SF_NO_PRINT,
1365 					    &softc->saved_ccb) == ERESTART) {
1366 			goto outr;
1367 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1368 			/* Don't wedge the queue */
1369 			xpt_release_devq(done_ccb->ccb_h.path,
1370 					 /*count*/1, /*run_queue*/TRUE);
1371 		}
1372 		xpt_release_ccb(done_ccb);
1373 		free(mode_hdr, M_CAMXPT);
1374 		PROBE_SET_ACTION(softc, PROBE_SUPPORTED_VPD_LIST);
1375 		xpt_schedule(periph, priority);
1376 		goto out;
1377 	}
1378 	case PROBE_SUPPORTED_VPD_LIST:
1379 	{
1380 		struct ccb_scsiio *csio;
1381 		struct scsi_vpd_supported_page_list *page_list;
1382 
1383 		csio = &done_ccb->csio;
1384 		page_list =
1385 		    (struct scsi_vpd_supported_page_list *)csio->data_ptr;
1386 
1387 		if (path->device->supported_vpds != NULL) {
1388 			free(path->device->supported_vpds, M_CAMXPT);
1389 			path->device->supported_vpds = NULL;
1390 			path->device->supported_vpds_len = 0;
1391 		}
1392 
1393 		if (page_list == NULL) {
1394 			/*
1395 			 * Don't process the command as it was never sent
1396 			 */
1397 		} else if (CCB_COMPLETED_OK(csio->ccb_h)) {
1398 			/* Got vpd list */
1399 			path->device->supported_vpds_len = page_list->length +
1400 			    SVPD_SUPPORTED_PAGES_HDR_LEN;
1401 			path->device->supported_vpds = (uint8_t *)page_list;
1402 			xpt_release_ccb(done_ccb);
1403 			PROBE_SET_ACTION(softc, PROBE_DEVICE_ID);
1404 			xpt_schedule(periph, priority);
1405 			goto out;
1406 		} else if (cam_periph_error(done_ccb, 0,
1407 					    SF_RETRY_UA|SF_NO_PRINT,
1408 					    &softc->saved_ccb) == ERESTART) {
1409 			goto outr;
1410 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1411 			/* Don't wedge the queue */
1412 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1413 					 /*run_queue*/TRUE);
1414 		}
1415 
1416 		if (page_list)
1417 			free(page_list, M_CAMXPT);
1418 		/* No VPDs available, skip to device check. */
1419 		csio->data_ptr = NULL;
1420 		goto probe_device_check;
1421 	}
1422 	case PROBE_DEVICE_ID:
1423 	{
1424 		struct scsi_vpd_device_id *devid;
1425 		struct ccb_scsiio *csio;
1426 		uint32_t length = 0;
1427 
1428 		csio = &done_ccb->csio;
1429 		devid = (struct scsi_vpd_device_id *)csio->data_ptr;
1430 
1431 		/* Clean up from previous instance of this device */
1432 		if (path->device->device_id != NULL) {
1433 			path->device->device_id_len = 0;
1434 			free(path->device->device_id, M_CAMXPT);
1435 			path->device->device_id = NULL;
1436 		}
1437 
1438 		if (devid == NULL) {
1439 			/* Don't process the command as it was never sent */
1440 		} else if (CCB_COMPLETED_OK(csio->ccb_h)) {
1441 			length = scsi_2btoul(devid->length);
1442 			if (length != 0) {
1443 				/*
1444 				 * NB: device_id_len is actual response
1445 				 * size, not buffer size.
1446 				 */
1447 				path->device->device_id_len = length +
1448 				    SVPD_DEVICE_ID_HDR_LEN;
1449 				path->device->device_id = (uint8_t *)devid;
1450 			}
1451 		} else if (cam_periph_error(done_ccb, 0,
1452 					    SF_RETRY_UA,
1453 					    &softc->saved_ccb) == ERESTART) {
1454 			goto outr;
1455 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1456 			/* Don't wedge the queue */
1457 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1458 					 /*run_queue*/TRUE);
1459 		}
1460 
1461 		/* Free the device id space if we don't use it */
1462 		if (devid && length == 0)
1463 			free(devid, M_CAMXPT);
1464 		xpt_release_ccb(done_ccb);
1465 		PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM);
1466 		xpt_schedule(periph, priority);
1467 		goto out;
1468 	}
1469 
1470 probe_device_check:
1471 	case PROBE_SERIAL_NUM:
1472 	{
1473 		struct ccb_scsiio *csio;
1474 		struct scsi_vpd_unit_serial_number *serial_buf;
1475 		u_int32_t  priority;
1476 		int changed;
1477 		int have_serialnum;
1478 
1479 		changed = 1;
1480 		have_serialnum = 0;
1481 		csio = &done_ccb->csio;
1482 		priority = done_ccb->ccb_h.pinfo.priority;
1483 		serial_buf =
1484 		    (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
1485 
1486 		if (serial_buf == NULL) {
1487 			/*
1488 			 * Don't process the command as it was never sent
1489 			 */
1490 		} else if (cam_ccb_status(done_ccb) == CAM_REQ_CMP
1491 			&& (serial_buf->length > 0)) {
1492 
1493 			have_serialnum = 1;
1494 			path->device->serial_num =
1495 				(u_int8_t *)malloc((serial_buf->length + 1),
1496 						   M_CAMXPT, M_NOWAIT);
1497 			if (path->device->serial_num != NULL) {
1498 				memcpy(path->device->serial_num,
1499 				       serial_buf->serial_num,
1500 				       serial_buf->length);
1501 				path->device->serial_num_len =
1502 				    serial_buf->length;
1503 				path->device->serial_num[serial_buf->length]
1504 				    = '\0';
1505 			}
1506 		} else if (cam_periph_error(done_ccb, 0,
1507 					    SF_RETRY_UA|SF_NO_PRINT,
1508 					    &softc->saved_ccb) == ERESTART) {
1509 			goto outr;
1510 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1511 			/* Don't wedge the queue */
1512 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1513 					 /*run_queue*/TRUE);
1514 		}
1515 
1516 		/*
1517 		 * Let's see if we have seen this device before.
1518 		 */
1519 		if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
1520 			MD5_CTX context;
1521 			u_int8_t digest[16];
1522 
1523 			MD5Init(&context);
1524 
1525 			MD5Update(&context,
1526 				  (unsigned char *)&path->device->inq_data,
1527 				  sizeof(struct scsi_inquiry_data));
1528 
1529 			if (have_serialnum)
1530 				MD5Update(&context, serial_buf->serial_num,
1531 					  serial_buf->length);
1532 
1533 			MD5Final(digest, &context);
1534 			if (bcmp(softc->digest, digest, 16) == 0)
1535 				changed = 0;
1536 
1537 			/*
1538 			 * XXX Do we need to do a TUR in order to ensure
1539 			 *     that the device really hasn't changed???
1540 			 */
1541 			if ((changed != 0)
1542 			 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
1543 				xpt_async(AC_LOST_DEVICE, path, NULL);
1544 		}
1545 		if (serial_buf != NULL)
1546 			free(serial_buf, M_CAMXPT);
1547 
1548 		if (changed != 0) {
1549 			/*
1550 			 * Now that we have all the necessary
1551 			 * information to safely perform transfer
1552 			 * negotiations... Controllers don't perform
1553 			 * any negotiation or tagged queuing until
1554 			 * after the first XPT_SET_TRAN_SETTINGS ccb is
1555 			 * received.  So, on a new device, just retrieve
1556 			 * the user settings, and set them as the current
1557 			 * settings to set the device up.
1558 			 */
1559 			proberequestdefaultnegotiation(periph);
1560 			xpt_release_ccb(done_ccb);
1561 
1562 			/*
1563 			 * Perform a TUR to allow the controller to
1564 			 * perform any necessary transfer negotiation.
1565 			 */
1566 			PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1567 			xpt_schedule(periph, priority);
1568 			goto out;
1569 		}
1570 		xpt_release_ccb(done_ccb);
1571 		break;
1572 	}
1573 	case PROBE_TUR_FOR_NEGOTIATION:
1574 	case PROBE_DV_EXIT:
1575 		if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1576 			cam_periph_error(done_ccb, 0,
1577 			    SF_NO_PRINT | SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1578 		}
1579 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1580 			/* Don't wedge the queue */
1581 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1582 					 /*run_queue*/TRUE);
1583 		}
1584 		/*
1585 		 * Do Domain Validation for lun 0 on devices that claim
1586 		 * to support Synchronous Transfer modes.
1587 		 */
1588 	 	if (softc->action == PROBE_TUR_FOR_NEGOTIATION
1589 		 && done_ccb->ccb_h.target_lun == 0
1590 		 && (path->device->inq_data.flags & SID_Sync) != 0
1591                  && (path->device->flags & CAM_DEV_IN_DV) == 0) {
1592 			CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1593 			    ("Begin Domain Validation\n"));
1594 			path->device->flags |= CAM_DEV_IN_DV;
1595 			xpt_release_ccb(done_ccb);
1596 			PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV1);
1597 			xpt_schedule(periph, priority);
1598 			goto out;
1599 		}
1600 		if (softc->action == PROBE_DV_EXIT) {
1601 			CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1602 			    ("Leave Domain Validation\n"));
1603 		}
1604 		if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1605 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1606 			xpt_acquire_device(path->device);
1607 		}
1608 		path->device->flags &=
1609 		    ~(CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1610 		if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1611 			/* Inform the XPT that a new device has been found */
1612 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1613 			xpt_action(done_ccb);
1614 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1615 				  done_ccb);
1616 		}
1617 		PROBE_SET_ACTION(softc, PROBE_DONE);
1618 		xpt_release_ccb(done_ccb);
1619 		break;
1620 	case PROBE_INQUIRY_BASIC_DV1:
1621 	case PROBE_INQUIRY_BASIC_DV2:
1622 	{
1623 		struct scsi_inquiry_data *nbuf;
1624 		struct ccb_scsiio *csio;
1625 
1626 		if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1627 			cam_periph_error(done_ccb, 0,
1628 			    SF_NO_PRINT | SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1629 		}
1630 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1631 			/* Don't wedge the queue */
1632 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1633 					 /*run_queue*/TRUE);
1634 		}
1635 		csio = &done_ccb->csio;
1636 		nbuf = (struct scsi_inquiry_data *)csio->data_ptr;
1637 		if (bcmp(nbuf, &path->device->inq_data, SHORT_INQUIRY_LENGTH)) {
1638 			xpt_print(path,
1639 			    "inquiry data fails comparison at DV%d step\n",
1640 			    softc->action == PROBE_INQUIRY_BASIC_DV1 ? 1 : 2);
1641 			if (proberequestbackoff(periph, path->device)) {
1642 				path->device->flags &= ~CAM_DEV_IN_DV;
1643 				PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1644 			} else {
1645 				/* give up */
1646 				PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
1647 			}
1648 			free(nbuf, M_CAMXPT);
1649 			xpt_release_ccb(done_ccb);
1650 			xpt_schedule(periph, priority);
1651 			goto out;
1652 		}
1653 		free(nbuf, M_CAMXPT);
1654 		if (softc->action == PROBE_INQUIRY_BASIC_DV1) {
1655 			PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV2);
1656 			xpt_release_ccb(done_ccb);
1657 			xpt_schedule(periph, priority);
1658 			goto out;
1659 		}
1660 		if (softc->action == PROBE_INQUIRY_BASIC_DV2) {
1661 			CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1662 			    ("Leave Domain Validation Successfully\n"));
1663 		}
1664 		if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1665 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1666 			xpt_acquire_device(path->device);
1667 		}
1668 		path->device->flags &=
1669 		    ~(CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1670 		if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1671 			/* Inform the XPT that a new device has been found */
1672 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1673 			xpt_action(done_ccb);
1674 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1675 				  done_ccb);
1676 		}
1677 		PROBE_SET_ACTION(softc, PROBE_DONE);
1678 		xpt_release_ccb(done_ccb);
1679 		break;
1680 	}
1681 	default:
1682 		panic("probedone: invalid action state 0x%x\n", softc->action);
1683 	}
1684 	done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
1685 	TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
1686 	done_ccb->ccb_h.status = CAM_REQ_CMP;
1687 	xpt_done(done_ccb);
1688 	if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
1689 		CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
1690 		/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1691 		cam_release_devq(path, 0, 0, 0, FALSE);
1692 		cam_periph_release_locked(periph);
1693 		cam_periph_invalidate(periph);
1694 		cam_periph_release_locked(periph);
1695 	} else {
1696 		probeschedule(periph);
1697 		goto out;
1698 	}
1699 }
1700 
1701 static void
1702 probe_purge_old(struct cam_path *path, struct scsi_report_luns_data *new,
1703     probe_flags flags)
1704 {
1705 	struct cam_path *tp;
1706 	struct scsi_report_luns_data *old;
1707 	u_int idx1, idx2, nlun_old, nlun_new;
1708 	lun_id_t this_lun;
1709 	u_int8_t *ol, *nl;
1710 
1711 	if (path->target == NULL) {
1712 		return;
1713 	}
1714 	mtx_lock(&path->target->luns_mtx);
1715 	old = path->target->luns;
1716 	path->target->luns = new;
1717 	mtx_unlock(&path->target->luns_mtx);
1718 	if (old == NULL)
1719 		return;
1720 	nlun_old = scsi_4btoul(old->length) / 8;
1721 	nlun_new = scsi_4btoul(new->length) / 8;
1722 
1723 	/*
1724 	 * We are not going to assume sorted lists. Deal.
1725 	 */
1726 	for (idx1 = 0; idx1 < nlun_old; idx1++) {
1727 		ol = old->luns[idx1].lundata;
1728 		for (idx2 = 0; idx2 < nlun_new; idx2++) {
1729 			nl = new->luns[idx2].lundata;
1730 			if (memcmp(nl, ol, 8) == 0) {
1731 				break;
1732 			}
1733 		}
1734 		if (idx2 < nlun_new) {
1735 			continue;
1736 		}
1737 		/*
1738 		 * An 'old' item not in the 'new' list.
1739 		 * Nuke it. Except that if it is lun 0,
1740 		 * that would be what the probe state
1741 		 * machine is currently working on,
1742 		 * so we won't do that.
1743 		 */
1744 		CAM_GET_LUN(old, idx1, this_lun);
1745 		if (this_lun == 0) {
1746 			continue;
1747 		}
1748 
1749 		/*
1750 		 * We also cannot nuke it if it is
1751 		 * not in a lun format we understand
1752 		 * and replace the LUN with a "simple" LUN
1753 		 * if that is all the HBA supports.
1754 		 */
1755 		if (!(flags & PROBE_EXTLUN)) {
1756 			if (!CAM_CAN_GET_SIMPLE_LUN(old, idx1))
1757 				continue;
1758 			CAM_GET_SIMPLE_LUN(old, idx1, this_lun);
1759 		}
1760 
1761 		if (xpt_create_path(&tp, NULL, xpt_path_path_id(path),
1762 		    xpt_path_target_id(path), this_lun) == CAM_REQ_CMP) {
1763 			xpt_async(AC_LOST_DEVICE, tp, NULL);
1764 			xpt_free_path(tp);
1765 		}
1766 	}
1767 	free(old, M_CAMXPT);
1768 }
1769 
1770 static void
1771 probecleanup(struct cam_periph *periph)
1772 {
1773 	free(periph->softc, M_CAMXPT);
1774 }
1775 
1776 static void
1777 scsi_find_quirk(struct cam_ed *device)
1778 {
1779 	struct scsi_quirk_entry *quirk;
1780 	caddr_t	match;
1781 
1782 	match = cam_quirkmatch((caddr_t)&device->inq_data,
1783 			       (caddr_t)scsi_quirk_table,
1784 			       sizeof(scsi_quirk_table) /
1785 			       sizeof(*scsi_quirk_table),
1786 			       sizeof(*scsi_quirk_table), scsi_inquiry_match);
1787 
1788 	if (match == NULL)
1789 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
1790 
1791 	quirk = (struct scsi_quirk_entry *)match;
1792 	device->quirk = quirk;
1793 	device->mintags = quirk->mintags;
1794 	device->maxtags = quirk->maxtags;
1795 }
1796 
1797 static int
1798 sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS)
1799 {
1800 	int error, val;
1801 
1802 	val = cam_srch_hi;
1803 	error = sysctl_handle_int(oidp, &val, 0, req);
1804 	if (error != 0 || req->newptr == NULL)
1805 		return (error);
1806 	if (val == 0 || val == 1) {
1807 		cam_srch_hi = val;
1808 		return (0);
1809 	} else {
1810 		return (EINVAL);
1811 	}
1812 }
1813 
1814 typedef struct {
1815 	union	ccb *request_ccb;
1816 	struct 	ccb_pathinq *cpi;
1817 	int	counter;
1818 	int	lunindex[0];
1819 } scsi_scan_bus_info;
1820 
1821 /*
1822  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1823  * As the scan progresses, scsi_scan_bus is used as the
1824  * callback on completion function.
1825  */
1826 static void
1827 scsi_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1828 {
1829 	struct mtx *mtx;
1830 
1831 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1832 		  ("scsi_scan_bus\n"));
1833 	switch (request_ccb->ccb_h.func_code) {
1834 	case XPT_SCAN_BUS:
1835 	case XPT_SCAN_TGT:
1836 	{
1837 		scsi_scan_bus_info *scan_info;
1838 		union	ccb *work_ccb, *reset_ccb;
1839 		struct	cam_path *path;
1840 		u_int	i;
1841 		u_int	low_target, max_target;
1842 		u_int	initiator_id;
1843 
1844 		/* Find out the characteristics of the bus */
1845 		work_ccb = xpt_alloc_ccb_nowait();
1846 		if (work_ccb == NULL) {
1847 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1848 			xpt_done(request_ccb);
1849 			return;
1850 		}
1851 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1852 			      request_ccb->ccb_h.pinfo.priority);
1853 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1854 		xpt_action(work_ccb);
1855 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1856 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1857 			xpt_free_ccb(work_ccb);
1858 			xpt_done(request_ccb);
1859 			return;
1860 		}
1861 
1862 		if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
1863 			/*
1864 			 * Can't scan the bus on an adapter that
1865 			 * cannot perform the initiator role.
1866 			 */
1867 			request_ccb->ccb_h.status = CAM_REQ_CMP;
1868 			xpt_free_ccb(work_ccb);
1869 			xpt_done(request_ccb);
1870 			return;
1871 		}
1872 
1873 		/* We may need to reset bus first, if we haven't done it yet. */
1874 		if ((work_ccb->cpi.hba_inquiry &
1875 		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1876 		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1877 		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset) &&
1878 		    (reset_ccb = xpt_alloc_ccb_nowait()) != NULL) {
1879 			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1880 			      CAM_PRIORITY_NONE);
1881 			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1882 			xpt_action(reset_ccb);
1883 			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1884 				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1885 				xpt_free_ccb(reset_ccb);
1886 				xpt_free_ccb(work_ccb);
1887 				xpt_done(request_ccb);
1888 				return;
1889 			}
1890 			xpt_free_ccb(reset_ccb);
1891 		}
1892 
1893 		/* Save some state for use while we probe for devices */
1894 		scan_info = (scsi_scan_bus_info *) malloc(sizeof(scsi_scan_bus_info) +
1895 		    (work_ccb->cpi.max_target * sizeof (u_int)), M_CAMXPT, M_ZERO|M_NOWAIT);
1896 		if (scan_info == NULL) {
1897 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1898 			xpt_free_ccb(work_ccb);
1899 			xpt_done(request_ccb);
1900 			return;
1901 		}
1902 		CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1903 		   ("SCAN start for %p\n", scan_info));
1904 		scan_info->request_ccb = request_ccb;
1905 		scan_info->cpi = &work_ccb->cpi;
1906 
1907 		/* Cache on our stack so we can work asynchronously */
1908 		max_target = scan_info->cpi->max_target;
1909 		low_target = 0;
1910 		initiator_id = scan_info->cpi->initiator_id;
1911 
1912 
1913 		/*
1914 		 * We can scan all targets in parallel, or do it sequentially.
1915 		 */
1916 
1917 		if (request_ccb->ccb_h.func_code == XPT_SCAN_TGT) {
1918 			max_target = low_target = request_ccb->ccb_h.target_id;
1919 			scan_info->counter = 0;
1920 		} else if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
1921 			max_target = 0;
1922 			scan_info->counter = 0;
1923 		} else {
1924 			scan_info->counter = scan_info->cpi->max_target + 1;
1925 			if (scan_info->cpi->initiator_id < scan_info->counter) {
1926 				scan_info->counter--;
1927 			}
1928 		}
1929 		mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
1930 		mtx_unlock(mtx);
1931 
1932 		for (i = low_target; i <= max_target; i++) {
1933 			cam_status status;
1934 			if (i == initiator_id)
1935 				continue;
1936 
1937 			status = xpt_create_path(&path, NULL,
1938 						 request_ccb->ccb_h.path_id,
1939 						 i, 0);
1940 			if (status != CAM_REQ_CMP) {
1941 				printf("scsi_scan_bus: xpt_create_path failed"
1942 				       " with status %#x, bus scan halted\n",
1943 				       status);
1944 				free(scan_info, M_CAMXPT);
1945 				request_ccb->ccb_h.status = status;
1946 				xpt_free_ccb(work_ccb);
1947 				xpt_done(request_ccb);
1948 				break;
1949 			}
1950 			work_ccb = xpt_alloc_ccb_nowait();
1951 			if (work_ccb == NULL) {
1952 				xpt_free_ccb((union ccb *)scan_info->cpi);
1953 				free(scan_info, M_CAMXPT);
1954 				xpt_free_path(path);
1955 				request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1956 				xpt_done(request_ccb);
1957 				break;
1958 			}
1959 			xpt_setup_ccb(&work_ccb->ccb_h, path,
1960 				      request_ccb->ccb_h.pinfo.priority);
1961 			work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1962 			work_ccb->ccb_h.cbfcnp = scsi_scan_bus;
1963 			work_ccb->ccb_h.flags |= CAM_UNLOCKED;
1964 			work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1965 			work_ccb->crcn.flags = request_ccb->crcn.flags;
1966 			xpt_action(work_ccb);
1967 		}
1968 
1969 		mtx_lock(mtx);
1970 		break;
1971 	}
1972 	case XPT_SCAN_LUN:
1973 	{
1974 		cam_status status;
1975 		struct cam_path *path, *oldpath;
1976 		scsi_scan_bus_info *scan_info;
1977 		struct cam_et *target;
1978 		struct cam_ed *device;
1979 		int next_target;
1980 		path_id_t path_id;
1981 		target_id_t target_id;
1982 		lun_id_t lun_id;
1983 
1984 		oldpath = request_ccb->ccb_h.path;
1985 
1986 		status = cam_ccb_status(request_ccb);
1987 		/* Reuse the same CCB to query if a device was really found */
1988 		scan_info = (scsi_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
1989 		xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
1990 			      request_ccb->ccb_h.pinfo.priority);
1991 		request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1992 
1993 
1994 		path_id = request_ccb->ccb_h.path_id;
1995 		target_id = request_ccb->ccb_h.target_id;
1996 		lun_id = request_ccb->ccb_h.target_lun;
1997 		xpt_action(request_ccb);
1998 
1999 		target = request_ccb->ccb_h.path->target;
2000 		next_target = 1;
2001 
2002 		mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
2003 		mtx_lock(mtx);
2004 		mtx_lock(&target->luns_mtx);
2005 		if (target->luns) {
2006 			lun_id_t first;
2007 			u_int nluns = scsi_4btoul(target->luns->length) / 8;
2008 
2009 			/*
2010 			 * Make sure we skip over lun 0 if it's the first member
2011 			 * of the list as we've actually just finished probing
2012 			 * it.
2013 			 */
2014 			CAM_GET_LUN(target->luns, 0, first);
2015 			if (first == 0 && scan_info->lunindex[target_id] == 0) {
2016 				scan_info->lunindex[target_id]++;
2017 			}
2018 
2019 			/*
2020 			 * Skip any LUNs that the HBA can't deal with.
2021 			 */
2022 			while (scan_info->lunindex[target_id] < nluns) {
2023 				if (scan_info->cpi->hba_misc & PIM_EXTLUNS) {
2024 					CAM_GET_LUN(target->luns,
2025 					    scan_info->lunindex[target_id],
2026 					    lun_id);
2027 					break;
2028 				}
2029 
2030 				if (CAM_CAN_GET_SIMPLE_LUN(target->luns,
2031 				    scan_info->lunindex[target_id])) {
2032 					CAM_GET_SIMPLE_LUN(target->luns,
2033 					    scan_info->lunindex[target_id],
2034 					    lun_id);
2035 					break;
2036 				}
2037 
2038 				scan_info->lunindex[target_id]++;
2039 			}
2040 
2041 			if (scan_info->lunindex[target_id] < nluns) {
2042 				mtx_unlock(&target->luns_mtx);
2043 				next_target = 0;
2044 				CAM_DEBUG(request_ccb->ccb_h.path,
2045 				    CAM_DEBUG_PROBE,
2046 				   ("next lun to try at index %u is %jx\n",
2047 				   scan_info->lunindex[target_id],
2048 				   (uintmax_t)lun_id));
2049 				scan_info->lunindex[target_id]++;
2050 			} else {
2051 				mtx_unlock(&target->luns_mtx);
2052 				/*
2053 				 * We're done with scanning all luns.
2054 				 *
2055 				 * Nuke the bogus device for lun 0 if lun 0
2056 				 * wasn't on the list.
2057 				 */
2058 				if (first != 0) {
2059 					TAILQ_FOREACH(device,
2060 					    &target->ed_entries, links) {
2061 						if (device->lun_id == 0) {
2062 							break;
2063 						}
2064 					}
2065 					if (device) {
2066 						xpt_release_device(device);
2067 					}
2068 				}
2069 			}
2070 		} else {
2071 		    mtx_unlock(&target->luns_mtx);
2072 		    if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
2073 			int phl;
2074 
2075 			/*
2076 			 * If we already probed lun 0 successfully, or
2077 			 * we have additional configured luns on this
2078 			 * target that might have "gone away", go onto
2079 			 * the next lun.
2080 			 */
2081 			/*
2082 			 * We may touch devices that we don't
2083 			 * hold references too, so ensure they
2084 			 * don't disappear out from under us.
2085 			 * The target above is referenced by the
2086 			 * path in the request ccb.
2087 			 */
2088 			phl = 0;
2089 			device = TAILQ_FIRST(&target->ed_entries);
2090 			if (device != NULL) {
2091 				phl = CAN_SRCH_HI_SPARSE(device);
2092 				if (device->lun_id == 0)
2093 					device = TAILQ_NEXT(device, links);
2094 			}
2095 			if ((lun_id != 0) || (device != NULL)) {
2096 				if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl) {
2097 					lun_id++;
2098 					next_target = 0;
2099 				}
2100 			}
2101 			if (lun_id == request_ccb->ccb_h.target_lun
2102 			    || lun_id > scan_info->cpi->max_lun)
2103 				next_target = 1;
2104 		    } else {
2105 
2106 			device = request_ccb->ccb_h.path->device;
2107 
2108 			if ((SCSI_QUIRK(device)->quirks &
2109 			    CAM_QUIRK_NOLUNS) == 0) {
2110 				/* Try the next lun */
2111 				if (lun_id < (CAM_SCSI2_MAXLUN-1)
2112 				  || CAN_SRCH_HI_DENSE(device)) {
2113 					lun_id++;
2114 					next_target = 0;
2115 				}
2116 			}
2117 			if (lun_id == request_ccb->ccb_h.target_lun
2118 			    || lun_id > scan_info->cpi->max_lun)
2119 				next_target = 1;
2120 		    }
2121 		}
2122 
2123 		/*
2124 		 * Check to see if we scan any further luns.
2125 		 */
2126 		if (next_target) {
2127 			int done;
2128 
2129 			/*
2130 			 * Free the current request path- we're done with it.
2131 			 */
2132 			xpt_free_path(oldpath);
2133  hop_again:
2134 			done = 0;
2135 			if (scan_info->request_ccb->ccb_h.func_code == XPT_SCAN_TGT) {
2136 				done = 1;
2137 			} else if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
2138 				scan_info->counter++;
2139 				if (scan_info->counter ==
2140 				    scan_info->cpi->initiator_id) {
2141 					scan_info->counter++;
2142 				}
2143 				if (scan_info->counter >=
2144 				    scan_info->cpi->max_target+1) {
2145 					done = 1;
2146 				}
2147 			} else {
2148 				scan_info->counter--;
2149 				if (scan_info->counter == 0) {
2150 					done = 1;
2151 				}
2152 			}
2153 			if (done) {
2154 				mtx_unlock(mtx);
2155 				xpt_free_ccb(request_ccb);
2156 				xpt_free_ccb((union ccb *)scan_info->cpi);
2157 				request_ccb = scan_info->request_ccb;
2158 				CAM_DEBUG(request_ccb->ccb_h.path,
2159 				    CAM_DEBUG_TRACE,
2160 				   ("SCAN done for %p\n", scan_info));
2161 				free(scan_info, M_CAMXPT);
2162 				request_ccb->ccb_h.status = CAM_REQ_CMP;
2163 				xpt_done(request_ccb);
2164 				break;
2165 			}
2166 
2167 			if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
2168 				mtx_unlock(mtx);
2169 				xpt_free_ccb(request_ccb);
2170 				break;
2171 			}
2172 			status = xpt_create_path(&path, NULL,
2173 			    scan_info->request_ccb->ccb_h.path_id,
2174 			    scan_info->counter, 0);
2175 			if (status != CAM_REQ_CMP) {
2176 				mtx_unlock(mtx);
2177 				printf("scsi_scan_bus: xpt_create_path failed"
2178 				    " with status %#x, bus scan halted\n",
2179 			       	    status);
2180 				xpt_free_ccb(request_ccb);
2181 				xpt_free_ccb((union ccb *)scan_info->cpi);
2182 				request_ccb = scan_info->request_ccb;
2183 				free(scan_info, M_CAMXPT);
2184 				request_ccb->ccb_h.status = status;
2185 				xpt_done(request_ccb);
2186 				break;
2187 			}
2188 			xpt_setup_ccb(&request_ccb->ccb_h, path,
2189 			    request_ccb->ccb_h.pinfo.priority);
2190 			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2191 			request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2192 			request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2193 			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
2194 			request_ccb->crcn.flags =
2195 			    scan_info->request_ccb->crcn.flags;
2196 		} else {
2197 			status = xpt_create_path(&path, NULL,
2198 						 path_id, target_id, lun_id);
2199 			/*
2200 			 * Free the old request path- we're done with it. We
2201 			 * do this *after* creating the new path so that
2202 			 * we don't remove a target that has our lun list
2203 			 * in the case that lun 0 is not present.
2204 			 */
2205 			xpt_free_path(oldpath);
2206 			if (status != CAM_REQ_CMP) {
2207 				printf("scsi_scan_bus: xpt_create_path failed "
2208 				       "with status %#x, halting LUN scan\n",
2209 			 	       status);
2210 				goto hop_again;
2211 			}
2212 			xpt_setup_ccb(&request_ccb->ccb_h, path,
2213 				      request_ccb->ccb_h.pinfo.priority);
2214 			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2215 			request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2216 			request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2217 			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
2218 			request_ccb->crcn.flags =
2219 				scan_info->request_ccb->crcn.flags;
2220 		}
2221 		mtx_unlock(mtx);
2222 		xpt_action(request_ccb);
2223 		break;
2224 	}
2225 	default:
2226 		break;
2227 	}
2228 }
2229 
2230 static void
2231 scsi_scan_lun(struct cam_periph *periph, struct cam_path *path,
2232 	     cam_flags flags, union ccb *request_ccb)
2233 {
2234 	struct ccb_pathinq cpi;
2235 	cam_status status;
2236 	struct cam_path *new_path;
2237 	struct cam_periph *old_periph;
2238 	int lock;
2239 
2240 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("scsi_scan_lun\n"));
2241 
2242 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2243 	cpi.ccb_h.func_code = XPT_PATH_INQ;
2244 	xpt_action((union ccb *)&cpi);
2245 
2246 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
2247 		if (request_ccb != NULL) {
2248 			request_ccb->ccb_h.status = cpi.ccb_h.status;
2249 			xpt_done(request_ccb);
2250 		}
2251 		return;
2252 	}
2253 
2254 	if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
2255 		/*
2256 		 * Can't scan the bus on an adapter that
2257 		 * cannot perform the initiator role.
2258 		 */
2259 		if (request_ccb != NULL) {
2260 			request_ccb->ccb_h.status = CAM_REQ_CMP;
2261 			xpt_done(request_ccb);
2262 		}
2263 		return;
2264 	}
2265 
2266 	if (request_ccb == NULL) {
2267 		request_ccb = xpt_alloc_ccb_nowait();
2268 		if (request_ccb == NULL) {
2269 			xpt_print(path, "scsi_scan_lun: can't allocate CCB, "
2270 			    "can't continue\n");
2271 			return;
2272 		}
2273 		status = xpt_create_path(&new_path, NULL,
2274 					  path->bus->path_id,
2275 					  path->target->target_id,
2276 					  path->device->lun_id);
2277 		if (status != CAM_REQ_CMP) {
2278 			xpt_print(path, "scsi_scan_lun: can't create path, "
2279 			    "can't continue\n");
2280 			xpt_free_ccb(request_ccb);
2281 			return;
2282 		}
2283 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
2284 		request_ccb->ccb_h.cbfcnp = xptscandone;
2285 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2286 		request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2287 		request_ccb->crcn.flags = flags;
2288 	}
2289 
2290 	lock = (xpt_path_owned(path) == 0);
2291 	if (lock)
2292 		xpt_path_lock(path);
2293 	if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
2294 		if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
2295 			probe_softc *softc;
2296 
2297 			softc = (probe_softc *)old_periph->softc;
2298 			TAILQ_INSERT_TAIL(&softc->request_ccbs,
2299 			    &request_ccb->ccb_h, periph_links.tqe);
2300 		} else {
2301 			request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2302 			xpt_done(request_ccb);
2303 		}
2304 	} else {
2305 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
2306 					  probestart, "probe",
2307 					  CAM_PERIPH_BIO,
2308 					  request_ccb->ccb_h.path, NULL, 0,
2309 					  request_ccb);
2310 
2311 		if (status != CAM_REQ_CMP) {
2312 			xpt_print(path, "scsi_scan_lun: cam_alloc_periph "
2313 			    "returned an error, can't continue probe\n");
2314 			request_ccb->ccb_h.status = status;
2315 			xpt_done(request_ccb);
2316 		}
2317 	}
2318 	if (lock)
2319 		xpt_path_unlock(path);
2320 }
2321 
2322 static void
2323 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
2324 {
2325 
2326 	xpt_free_path(done_ccb->ccb_h.path);
2327 	xpt_free_ccb(done_ccb);
2328 }
2329 
2330 static struct cam_ed *
2331 scsi_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
2332 {
2333 	struct scsi_quirk_entry *quirk;
2334 	struct cam_ed *device;
2335 
2336 	device = xpt_alloc_device(bus, target, lun_id);
2337 	if (device == NULL)
2338 		return (NULL);
2339 
2340 	/*
2341 	 * Take the default quirk entry until we have inquiry
2342 	 * data and can determine a better quirk to use.
2343 	 */
2344 	quirk = &scsi_quirk_table[scsi_quirk_table_size - 1];
2345 	device->quirk = (void *)quirk;
2346 	device->mintags = quirk->mintags;
2347 	device->maxtags = quirk->maxtags;
2348 	bzero(&device->inq_data, sizeof(device->inq_data));
2349 	device->inq_flags = 0;
2350 	device->queue_flags = 0;
2351 	device->serial_num = NULL;
2352 	device->serial_num_len = 0;
2353 	device->device_id = NULL;
2354 	device->device_id_len = 0;
2355 	device->supported_vpds = NULL;
2356 	device->supported_vpds_len = 0;
2357 	return (device);
2358 }
2359 
2360 static void
2361 scsi_devise_transport(struct cam_path *path)
2362 {
2363 	struct ccb_pathinq cpi;
2364 	struct ccb_trans_settings cts;
2365 	struct scsi_inquiry_data *inq_buf;
2366 
2367 	/* Get transport information from the SIM */
2368 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2369 	cpi.ccb_h.func_code = XPT_PATH_INQ;
2370 	xpt_action((union ccb *)&cpi);
2371 
2372 	inq_buf = NULL;
2373 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
2374 		inq_buf = &path->device->inq_data;
2375 	path->device->protocol = PROTO_SCSI;
2376 	path->device->protocol_version =
2377 	    inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
2378 	path->device->transport = cpi.transport;
2379 	path->device->transport_version = cpi.transport_version;
2380 
2381 	/*
2382 	 * Any device not using SPI3 features should
2383 	 * be considered SPI2 or lower.
2384 	 */
2385 	if (inq_buf != NULL) {
2386 		if (path->device->transport == XPORT_SPI
2387 		 && (inq_buf->spi3data & SID_SPI_MASK) == 0
2388 		 && path->device->transport_version > 2)
2389 			path->device->transport_version = 2;
2390 	} else {
2391 		struct cam_ed* otherdev;
2392 
2393 		for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
2394 		     otherdev != NULL;
2395 		     otherdev = TAILQ_NEXT(otherdev, links)) {
2396 			if (otherdev != path->device)
2397 				break;
2398 		}
2399 
2400 		if (otherdev != NULL) {
2401 			/*
2402 			 * Initially assume the same versioning as
2403 			 * prior luns for this target.
2404 			 */
2405 			path->device->protocol_version =
2406 			    otherdev->protocol_version;
2407 			path->device->transport_version =
2408 			    otherdev->transport_version;
2409 		} else {
2410 			/* Until we know better, opt for safty */
2411 			path->device->protocol_version = 2;
2412 			if (path->device->transport == XPORT_SPI)
2413 				path->device->transport_version = 2;
2414 			else
2415 				path->device->transport_version = 0;
2416 		}
2417 	}
2418 
2419 	/*
2420 	 * XXX
2421 	 * For a device compliant with SPC-2 we should be able
2422 	 * to determine the transport version supported by
2423 	 * scrutinizing the version descriptors in the
2424 	 * inquiry buffer.
2425 	 */
2426 
2427 	/* Tell the controller what we think */
2428 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2429 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
2430 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
2431 	cts.transport = path->device->transport;
2432 	cts.transport_version = path->device->transport_version;
2433 	cts.protocol = path->device->protocol;
2434 	cts.protocol_version = path->device->protocol_version;
2435 	cts.proto_specific.valid = 0;
2436 	cts.xport_specific.valid = 0;
2437 	xpt_action((union ccb *)&cts);
2438 }
2439 
2440 static void
2441 scsi_dev_advinfo(union ccb *start_ccb)
2442 {
2443 	struct cam_ed *device;
2444 	struct ccb_dev_advinfo *cdai;
2445 	off_t amt;
2446 
2447 	start_ccb->ccb_h.status = CAM_REQ_INVALID;
2448 	device = start_ccb->ccb_h.path->device;
2449 	cdai = &start_ccb->cdai;
2450 	switch(cdai->buftype) {
2451 	case CDAI_TYPE_SCSI_DEVID:
2452 		if (cdai->flags & CDAI_FLAG_STORE)
2453 			return;
2454 		cdai->provsiz = device->device_id_len;
2455 		if (device->device_id_len == 0)
2456 			break;
2457 		amt = device->device_id_len;
2458 		if (cdai->provsiz > cdai->bufsiz)
2459 			amt = cdai->bufsiz;
2460 		memcpy(cdai->buf, device->device_id, amt);
2461 		break;
2462 	case CDAI_TYPE_SERIAL_NUM:
2463 		if (cdai->flags & CDAI_FLAG_STORE)
2464 			return;
2465 		cdai->provsiz = device->serial_num_len;
2466 		if (device->serial_num_len == 0)
2467 			break;
2468 		amt = device->serial_num_len;
2469 		if (cdai->provsiz > cdai->bufsiz)
2470 			amt = cdai->bufsiz;
2471 		memcpy(cdai->buf, device->serial_num, amt);
2472 		break;
2473 	case CDAI_TYPE_PHYS_PATH:
2474 		if (cdai->flags & CDAI_FLAG_STORE) {
2475 			if (device->physpath != NULL) {
2476 				free(device->physpath, M_CAMXPT);
2477 				device->physpath = NULL;
2478 			}
2479 			device->physpath_len = cdai->bufsiz;
2480 			/* Clear existing buffer if zero length */
2481 			if (cdai->bufsiz == 0)
2482 				break;
2483 			device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
2484 			if (device->physpath == NULL) {
2485 				start_ccb->ccb_h.status = CAM_REQ_ABORTED;
2486 				return;
2487 			}
2488 			memcpy(device->physpath, cdai->buf, cdai->bufsiz);
2489 		} else {
2490 			cdai->provsiz = device->physpath_len;
2491 			if (device->physpath_len == 0)
2492 				break;
2493 			amt = device->physpath_len;
2494 			if (cdai->provsiz > cdai->bufsiz)
2495 				amt = cdai->bufsiz;
2496 			memcpy(cdai->buf, device->physpath, amt);
2497 		}
2498 		break;
2499 	case CDAI_TYPE_RCAPLONG:
2500 		if (cdai->flags & CDAI_FLAG_STORE) {
2501 			if (device->rcap_buf != NULL) {
2502 				free(device->rcap_buf, M_CAMXPT);
2503 				device->rcap_buf = NULL;
2504 			}
2505 
2506 			device->rcap_len = cdai->bufsiz;
2507 			/* Clear existing buffer if zero length */
2508 			if (cdai->bufsiz == 0)
2509 				break;
2510 
2511 			device->rcap_buf = malloc(cdai->bufsiz, M_CAMXPT,
2512 						  M_NOWAIT);
2513 			if (device->rcap_buf == NULL) {
2514 				start_ccb->ccb_h.status = CAM_REQ_ABORTED;
2515 				return;
2516 			}
2517 
2518 			memcpy(device->rcap_buf, cdai->buf, cdai->bufsiz);
2519 		} else {
2520 			cdai->provsiz = device->rcap_len;
2521 			if (device->rcap_len == 0)
2522 				break;
2523 			amt = device->rcap_len;
2524 			if (cdai->provsiz > cdai->bufsiz)
2525 				amt = cdai->bufsiz;
2526 			memcpy(cdai->buf, device->rcap_buf, amt);
2527 		}
2528 		break;
2529 	default:
2530 		return;
2531 	}
2532 	start_ccb->ccb_h.status = CAM_REQ_CMP;
2533 
2534 	if (cdai->flags & CDAI_FLAG_STORE) {
2535 		xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
2536 			  (void *)(uintptr_t)cdai->buftype);
2537 	}
2538 }
2539 
2540 static void
2541 scsi_action(union ccb *start_ccb)
2542 {
2543 
2544 	switch (start_ccb->ccb_h.func_code) {
2545 	case XPT_SET_TRAN_SETTINGS:
2546 	{
2547 		scsi_set_transfer_settings(&start_ccb->cts,
2548 					   start_ccb->ccb_h.path,
2549 					   /*async_update*/FALSE);
2550 		break;
2551 	}
2552 	case XPT_SCAN_BUS:
2553 	case XPT_SCAN_TGT:
2554 		scsi_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
2555 		break;
2556 	case XPT_SCAN_LUN:
2557 		scsi_scan_lun(start_ccb->ccb_h.path->periph,
2558 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
2559 			      start_ccb);
2560 		break;
2561 	case XPT_DEV_ADVINFO:
2562 	{
2563 		scsi_dev_advinfo(start_ccb);
2564 		break;
2565 	}
2566 	default:
2567 		xpt_action_default(start_ccb);
2568 		break;
2569 	}
2570 }
2571 
2572 static void
2573 scsi_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_path *path,
2574 			   int async_update)
2575 {
2576 	struct	ccb_pathinq cpi;
2577 	struct	ccb_trans_settings cur_cts;
2578 	struct	ccb_trans_settings_scsi *scsi;
2579 	struct	ccb_trans_settings_scsi *cur_scsi;
2580 	struct	scsi_inquiry_data *inq_data;
2581 	struct	cam_ed *device;
2582 
2583 	if (path == NULL || (device = path->device) == NULL) {
2584 		cts->ccb_h.status = CAM_PATH_INVALID;
2585 		xpt_done((union ccb *)cts);
2586 		return;
2587 	}
2588 
2589 	if (cts->protocol == PROTO_UNKNOWN
2590 	 || cts->protocol == PROTO_UNSPECIFIED) {
2591 		cts->protocol = device->protocol;
2592 		cts->protocol_version = device->protocol_version;
2593 	}
2594 
2595 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
2596 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
2597 		cts->protocol_version = device->protocol_version;
2598 
2599 	if (cts->protocol != device->protocol) {
2600 		xpt_print(path, "Uninitialized Protocol %x:%x?\n",
2601 		       cts->protocol, device->protocol);
2602 		cts->protocol = device->protocol;
2603 	}
2604 
2605 	if (cts->protocol_version > device->protocol_version) {
2606 		if (bootverbose) {
2607 			xpt_print(path, "Down reving Protocol "
2608 			    "Version from %d to %d?\n", cts->protocol_version,
2609 			    device->protocol_version);
2610 		}
2611 		cts->protocol_version = device->protocol_version;
2612 	}
2613 
2614 	if (cts->transport == XPORT_UNKNOWN
2615 	 || cts->transport == XPORT_UNSPECIFIED) {
2616 		cts->transport = device->transport;
2617 		cts->transport_version = device->transport_version;
2618 	}
2619 
2620 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
2621 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
2622 		cts->transport_version = device->transport_version;
2623 
2624 	if (cts->transport != device->transport) {
2625 		xpt_print(path, "Uninitialized Transport %x:%x?\n",
2626 		    cts->transport, device->transport);
2627 		cts->transport = device->transport;
2628 	}
2629 
2630 	if (cts->transport_version > device->transport_version) {
2631 		if (bootverbose) {
2632 			xpt_print(path, "Down reving Transport "
2633 			    "Version from %d to %d?\n", cts->transport_version,
2634 			    device->transport_version);
2635 		}
2636 		cts->transport_version = device->transport_version;
2637 	}
2638 
2639 	/*
2640 	 * Nothing more of interest to do unless
2641 	 * this is a device connected via the
2642 	 * SCSI protocol.
2643 	 */
2644 	if (cts->protocol != PROTO_SCSI) {
2645 		if (async_update == FALSE)
2646 			xpt_action_default((union ccb *)cts);
2647 		return;
2648 	}
2649 
2650 	inq_data = &device->inq_data;
2651 	scsi = &cts->proto_specific.scsi;
2652 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2653 	cpi.ccb_h.func_code = XPT_PATH_INQ;
2654 	xpt_action((union ccb *)&cpi);
2655 
2656 	/* SCSI specific sanity checking */
2657 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
2658 	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
2659 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
2660 	 || (device->mintags == 0)) {
2661 		/*
2662 		 * Can't tag on hardware that doesn't support tags,
2663 		 * doesn't have it enabled, or has broken tag support.
2664 		 */
2665 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2666 	}
2667 
2668 	if (async_update == FALSE) {
2669 		/*
2670 		 * Perform sanity checking against what the
2671 		 * controller and device can do.
2672 		 */
2673 		xpt_setup_ccb(&cur_cts.ccb_h, path, CAM_PRIORITY_NONE);
2674 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2675 		cur_cts.type = cts->type;
2676 		xpt_action((union ccb *)&cur_cts);
2677 		if (cam_ccb_status((union ccb *)&cur_cts) != CAM_REQ_CMP) {
2678 			return;
2679 		}
2680 		cur_scsi = &cur_cts.proto_specific.scsi;
2681 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
2682 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2683 			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
2684 		}
2685 		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
2686 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2687 	}
2688 
2689 	/* SPI specific sanity checking */
2690 	if (cts->transport == XPORT_SPI && async_update == FALSE) {
2691 		u_int spi3caps;
2692 		struct ccb_trans_settings_spi *spi;
2693 		struct ccb_trans_settings_spi *cur_spi;
2694 
2695 		spi = &cts->xport_specific.spi;
2696 
2697 		cur_spi = &cur_cts.xport_specific.spi;
2698 
2699 		/* Fill in any gaps in what the user gave us */
2700 		if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2701 			spi->sync_period = cur_spi->sync_period;
2702 		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2703 			spi->sync_period = 0;
2704 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2705 			spi->sync_offset = cur_spi->sync_offset;
2706 		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2707 			spi->sync_offset = 0;
2708 		if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2709 			spi->ppr_options = cur_spi->ppr_options;
2710 		if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2711 			spi->ppr_options = 0;
2712 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2713 			spi->bus_width = cur_spi->bus_width;
2714 		if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2715 			spi->bus_width = 0;
2716 		if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
2717 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2718 			spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
2719 		}
2720 		if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
2721 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2722 		if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2723 		  && (inq_data->flags & SID_Sync) == 0
2724 		  && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2725 		 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)) {
2726 			/* Force async */
2727 			spi->sync_period = 0;
2728 			spi->sync_offset = 0;
2729 		}
2730 
2731 		switch (spi->bus_width) {
2732 		case MSG_EXT_WDTR_BUS_32_BIT:
2733 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2734 			  || (inq_data->flags & SID_WBus32) != 0
2735 			  || cts->type == CTS_TYPE_USER_SETTINGS)
2736 			 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
2737 				break;
2738 			/* Fall Through to 16-bit */
2739 		case MSG_EXT_WDTR_BUS_16_BIT:
2740 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2741 			  || (inq_data->flags & SID_WBus16) != 0
2742 			  || cts->type == CTS_TYPE_USER_SETTINGS)
2743 			 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
2744 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2745 				break;
2746 			}
2747 			/* Fall Through to 8-bit */
2748 		default: /* New bus width?? */
2749 		case MSG_EXT_WDTR_BUS_8_BIT:
2750 			/* All targets can do this */
2751 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2752 			break;
2753 		}
2754 
2755 		spi3caps = cpi.xport_specific.spi.ppr_options;
2756 		if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2757 		 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2758 			spi3caps &= inq_data->spi3data;
2759 
2760 		if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
2761 			spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2762 
2763 		if ((spi3caps & SID_SPI_IUS) == 0)
2764 			spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
2765 
2766 		if ((spi3caps & SID_SPI_QAS) == 0)
2767 			spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
2768 
2769 		/* No SPI Transfer settings are allowed unless we are wide */
2770 		if (spi->bus_width == 0)
2771 			spi->ppr_options = 0;
2772 
2773 		if ((spi->valid & CTS_SPI_VALID_DISC)
2774 		 && ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0)) {
2775 			/*
2776 			 * Can't tag queue without disconnection.
2777 			 */
2778 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2779 			scsi->valid |= CTS_SCSI_VALID_TQ;
2780 		}
2781 
2782 		/*
2783 		 * If we are currently performing tagged transactions to
2784 		 * this device and want to change its negotiation parameters,
2785 		 * go non-tagged for a bit to give the controller a chance to
2786 		 * negotiate unhampered by tag messages.
2787 		 */
2788 		if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2789 		 && (device->inq_flags & SID_CmdQue) != 0
2790 		 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2791 		 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
2792 				   CTS_SPI_VALID_SYNC_OFFSET|
2793 				   CTS_SPI_VALID_BUS_WIDTH)) != 0)
2794 			scsi_toggle_tags(path);
2795 	}
2796 
2797 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2798 	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
2799 		int device_tagenb;
2800 
2801 		/*
2802 		 * If we are transitioning from tags to no-tags or
2803 		 * vice-versa, we need to carefully freeze and restart
2804 		 * the queue so that we don't overlap tagged and non-tagged
2805 		 * commands.  We also temporarily stop tags if there is
2806 		 * a change in transfer negotiation settings to allow
2807 		 * "tag-less" negotiation.
2808 		 */
2809 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2810 		 || (device->inq_flags & SID_CmdQue) != 0)
2811 			device_tagenb = TRUE;
2812 		else
2813 			device_tagenb = FALSE;
2814 
2815 		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2816 		  && device_tagenb == FALSE)
2817 		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
2818 		  && device_tagenb == TRUE)) {
2819 
2820 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
2821 				/*
2822 				 * Delay change to use tags until after a
2823 				 * few commands have gone to this device so
2824 				 * the controller has time to perform transfer
2825 				 * negotiations without tagged messages getting
2826 				 * in the way.
2827 				 */
2828 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
2829 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
2830 			} else {
2831 				xpt_stop_tags(path);
2832 			}
2833 		}
2834 	}
2835 	if (async_update == FALSE)
2836 		xpt_action_default((union ccb *)cts);
2837 }
2838 
2839 static void
2840 scsi_toggle_tags(struct cam_path *path)
2841 {
2842 	struct cam_ed *dev;
2843 
2844 	/*
2845 	 * Give controllers a chance to renegotiate
2846 	 * before starting tag operations.  We
2847 	 * "toggle" tagged queuing off then on
2848 	 * which causes the tag enable command delay
2849 	 * counter to come into effect.
2850 	 */
2851 	dev = path->device;
2852 	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2853 	 || ((dev->inq_flags & SID_CmdQue) != 0
2854  	  && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
2855 		struct ccb_trans_settings cts;
2856 
2857 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2858 		cts.protocol = PROTO_SCSI;
2859 		cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
2860 		cts.transport = XPORT_UNSPECIFIED;
2861 		cts.transport_version = XPORT_VERSION_UNSPECIFIED;
2862 		cts.proto_specific.scsi.flags = 0;
2863 		cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
2864 		scsi_set_transfer_settings(&cts, path,
2865 					  /*async_update*/TRUE);
2866 		cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
2867 		scsi_set_transfer_settings(&cts, path,
2868 					  /*async_update*/TRUE);
2869 	}
2870 }
2871 
2872 /*
2873  * Handle any per-device event notifications that require action by the XPT.
2874  */
2875 static void
2876 scsi_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
2877 	      struct cam_ed *device, void *async_arg)
2878 {
2879 	cam_status status;
2880 	struct cam_path newpath;
2881 
2882 	/*
2883 	 * We only need to handle events for real devices.
2884 	 */
2885 	if (target->target_id == CAM_TARGET_WILDCARD
2886 	 || device->lun_id == CAM_LUN_WILDCARD)
2887 		return;
2888 
2889 	/*
2890 	 * We need our own path with wildcards expanded to
2891 	 * handle certain types of events.
2892 	 */
2893 	if ((async_code == AC_SENT_BDR)
2894 	 || (async_code == AC_BUS_RESET)
2895 	 || (async_code == AC_INQ_CHANGED))
2896 		status = xpt_compile_path(&newpath, NULL,
2897 					  bus->path_id,
2898 					  target->target_id,
2899 					  device->lun_id);
2900 	else
2901 		status = CAM_REQ_CMP_ERR;
2902 
2903 	if (status == CAM_REQ_CMP) {
2904 
2905 		/*
2906 		 * Allow transfer negotiation to occur in a
2907 		 * tag free environment and after settle delay.
2908 		 */
2909 		if (async_code == AC_SENT_BDR
2910 		 || async_code == AC_BUS_RESET) {
2911 			cam_freeze_devq(&newpath);
2912 			cam_release_devq(&newpath,
2913 				RELSIM_RELEASE_AFTER_TIMEOUT,
2914 				/*reduction*/0,
2915 				/*timeout*/scsi_delay,
2916 				/*getcount_only*/0);
2917 			scsi_toggle_tags(&newpath);
2918 		}
2919 
2920 		if (async_code == AC_INQ_CHANGED) {
2921 			/*
2922 			 * We've sent a start unit command, or
2923 			 * something similar to a device that
2924 			 * may have caused its inquiry data to
2925 			 * change. So we re-scan the device to
2926 			 * refresh the inquiry data for it.
2927 			 */
2928 			scsi_scan_lun(newpath.periph, &newpath,
2929 				     CAM_EXPECT_INQ_CHANGE, NULL);
2930 		}
2931 		xpt_release_path(&newpath);
2932 	} else if (async_code == AC_LOST_DEVICE &&
2933 	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
2934 		device->flags |= CAM_DEV_UNCONFIGURED;
2935 		xpt_release_device(device);
2936 	} else if (async_code == AC_TRANSFER_NEG) {
2937 		struct ccb_trans_settings *settings;
2938 		struct cam_path path;
2939 
2940 		settings = (struct ccb_trans_settings *)async_arg;
2941 		xpt_compile_path(&path, NULL, bus->path_id, target->target_id,
2942 				 device->lun_id);
2943 		scsi_set_transfer_settings(settings, &path,
2944 					  /*async_update*/TRUE);
2945 		xpt_release_path(&path);
2946 	}
2947 }
2948 
2949 static void
2950 scsi_announce_periph(struct cam_periph *periph)
2951 {
2952 	struct	ccb_pathinq cpi;
2953 	struct	ccb_trans_settings cts;
2954 	struct	cam_path *path = periph->path;
2955 	u_int	speed;
2956 	u_int	freq;
2957 	u_int	mb;
2958 
2959 	cam_periph_assert(periph, MA_OWNED);
2960 
2961 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
2962 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2963 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
2964 	xpt_action((union ccb*)&cts);
2965 	if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP)
2966 		return;
2967 	/* Ask the SIM for its base transfer speed */
2968 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
2969 	cpi.ccb_h.func_code = XPT_PATH_INQ;
2970 	xpt_action((union ccb *)&cpi);
2971 	/* Report connection speed */
2972 	speed = cpi.base_transfer_speed;
2973 	freq = 0;
2974 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
2975 		struct	ccb_trans_settings_spi *spi =
2976 		    &cts.xport_specific.spi;
2977 
2978 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
2979 		  && spi->sync_offset != 0) {
2980 			freq = scsi_calc_syncsrate(spi->sync_period);
2981 			speed = freq;
2982 		}
2983 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
2984 			speed *= (0x01 << spi->bus_width);
2985 	}
2986 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
2987 		struct	ccb_trans_settings_fc *fc =
2988 		    &cts.xport_specific.fc;
2989 
2990 		if (fc->valid & CTS_FC_VALID_SPEED)
2991 			speed = fc->bitrate;
2992 	}
2993 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SAS) {
2994 		struct	ccb_trans_settings_sas *sas =
2995 		    &cts.xport_specific.sas;
2996 
2997 		if (sas->valid & CTS_SAS_VALID_SPEED)
2998 			speed = sas->bitrate;
2999 	}
3000 	mb = speed / 1000;
3001 	if (mb > 0)
3002 		printf("%s%d: %d.%03dMB/s transfers",
3003 		       periph->periph_name, periph->unit_number,
3004 		       mb, speed % 1000);
3005 	else
3006 		printf("%s%d: %dKB/s transfers", periph->periph_name,
3007 		       periph->unit_number, speed);
3008 	/* Report additional information about SPI connections */
3009 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
3010 		struct	ccb_trans_settings_spi *spi;
3011 
3012 		spi = &cts.xport_specific.spi;
3013 		if (freq != 0) {
3014 			printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
3015 			       freq % 1000,
3016 			       (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
3017 			     ? " DT" : "",
3018 			       spi->sync_offset);
3019 		}
3020 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
3021 		 && spi->bus_width > 0) {
3022 			if (freq != 0) {
3023 				printf(", ");
3024 			} else {
3025 				printf(" (");
3026 			}
3027 			printf("%dbit)", 8 * (0x01 << spi->bus_width));
3028 		} else if (freq != 0) {
3029 			printf(")");
3030 		}
3031 	}
3032 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
3033 		struct	ccb_trans_settings_fc *fc;
3034 
3035 		fc = &cts.xport_specific.fc;
3036 		if (fc->valid & CTS_FC_VALID_WWNN)
3037 			printf(" WWNN 0x%llx", (long long) fc->wwnn);
3038 		if (fc->valid & CTS_FC_VALID_WWPN)
3039 			printf(" WWPN 0x%llx", (long long) fc->wwpn);
3040 		if (fc->valid & CTS_FC_VALID_PORT)
3041 			printf(" PortID 0x%x", fc->port);
3042 	}
3043 	printf("\n");
3044 }
3045 
3046