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