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