xref: /freebsd/sys/cam/scsi/scsi_xpt.c (revision 4ed925457ab06e83238a5db33e89ccc94b99a713)
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 #ifdef PC98
51 #include <pc98/pc98/pc98_machdep.h>	/* geometry translation */
52 #endif
53 
54 #include <cam/cam.h>
55 #include <cam/cam_ccb.h>
56 #include <cam/cam_queue.h>
57 #include <cam/cam_periph.h>
58 #include <cam/cam_sim.h>
59 #include <cam/cam_xpt.h>
60 #include <cam/cam_xpt_sim.h>
61 #include <cam/cam_xpt_periph.h>
62 #include <cam/cam_xpt_internal.h>
63 #include <cam/cam_debug.h>
64 
65 #include <cam/scsi/scsi_all.h>
66 #include <cam/scsi/scsi_message.h>
67 #include <cam/scsi/scsi_pass.h>
68 #include <machine/stdarg.h>	/* for xpt_print below */
69 #include "opt_cam.h"
70 
71 struct scsi_quirk_entry {
72 	struct scsi_inquiry_pattern inq_pat;
73 	u_int8_t quirks;
74 #define	CAM_QUIRK_NOLUNS	0x01
75 #define	CAM_QUIRK_NOSERIAL	0x02
76 #define	CAM_QUIRK_HILUNS	0x04
77 #define	CAM_QUIRK_NOHILUNS	0x08
78 	u_int mintags;
79 	u_int maxtags;
80 };
81 #define SCSI_QUIRK(dev)	((struct scsi_quirk_entry *)((dev)->quirk))
82 
83 static int cam_srch_hi = 0;
84 TUNABLE_INT("kern.cam.cam_srch_hi", &cam_srch_hi);
85 static int sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS);
86 SYSCTL_PROC(_kern_cam, OID_AUTO, cam_srch_hi, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
87     sysctl_cam_search_luns, "I",
88     "allow search above LUN 7 for SCSI3 and greater devices");
89 
90 #define	CAM_SCSI2_MAXLUN	8
91 /*
92  * If we're not quirked to search <= the first 8 luns
93  * and we are either quirked to search above lun 8,
94  * or we're > SCSI-2 and we've enabled hilun searching,
95  * or we're > SCSI-2 and the last lun was a success,
96  * we can look for luns above lun 8.
97  */
98 #define	CAN_SRCH_HI_SPARSE(dv)					\
99   (((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_NOHILUNS) == 0) 	\
100   && ((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_HILUNS)		\
101   || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2 && cam_srch_hi)))
102 
103 #define	CAN_SRCH_HI_DENSE(dv)					\
104   (((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_NOHILUNS) == 0) 	\
105   && ((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_HILUNS)		\
106   || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2)))
107 
108 static periph_init_t probe_periph_init;
109 
110 static struct periph_driver probe_driver =
111 {
112 	probe_periph_init, "probe",
113 	TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
114 	CAM_PERIPH_DRV_EARLY
115 };
116 
117 PERIPHDRIVER_DECLARE(probe, probe_driver);
118 
119 typedef enum {
120 	PROBE_TUR,
121 	PROBE_INQUIRY,	/* this counts as DV0 for Basic Domain Validation */
122 	PROBE_FULL_INQUIRY,
123 	PROBE_MODE_SENSE,
124 	PROBE_SERIAL_NUM_0,
125 	PROBE_SERIAL_NUM_1,
126 	PROBE_TUR_FOR_NEGOTIATION,
127 	PROBE_INQUIRY_BASIC_DV1,
128 	PROBE_INQUIRY_BASIC_DV2,
129 	PROBE_DV_EXIT,
130 	PROBE_INVALID
131 } probe_action;
132 
133 static char *probe_action_text[] = {
134 	"PROBE_TUR",
135 	"PROBE_INQUIRY",
136 	"PROBE_FULL_INQUIRY",
137 	"PROBE_MODE_SENSE",
138 	"PROBE_SERIAL_NUM_0",
139 	"PROBE_SERIAL_NUM_1",
140 	"PROBE_TUR_FOR_NEGOTIATION",
141 	"PROBE_INQUIRY_BASIC_DV1",
142 	"PROBE_INQUIRY_BASIC_DV2",
143 	"PROBE_DV_EXIT",
144 	"PROBE_INVALID"
145 };
146 
147 #define PROBE_SET_ACTION(softc, newaction)	\
148 do {									\
149 	char **text;							\
150 	text = probe_action_text;					\
151 	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO,		\
152 	    ("Probe %s to %s\n", text[(softc)->action],			\
153 	    text[(newaction)]));					\
154 	(softc)->action = (newaction);					\
155 } while(0)
156 
157 typedef enum {
158 	PROBE_INQUIRY_CKSUM	= 0x01,
159 	PROBE_SERIAL_CKSUM	= 0x02,
160 	PROBE_NO_ANNOUNCE	= 0x04
161 } probe_flags;
162 
163 typedef struct {
164 	TAILQ_HEAD(, ccb_hdr) request_ccbs;
165 	probe_action	action;
166 	union ccb	saved_ccb;
167 	probe_flags	flags;
168 	MD5_CTX		context;
169 	u_int8_t	digest[16];
170 	struct cam_periph *periph;
171 } probe_softc;
172 
173 static const char quantum[] = "QUANTUM";
174 static const char sony[] = "SONY";
175 static const char west_digital[] = "WDIGTL";
176 static const char samsung[] = "SAMSUNG";
177 static const char seagate[] = "SEAGATE";
178 static const char microp[] = "MICROP";
179 
180 static struct scsi_quirk_entry scsi_quirk_table[] =
181 {
182 	{
183 		/* Reports QUEUE FULL for temporary resource shortages */
184 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
185 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
186 	},
187 	{
188 		/* Reports QUEUE FULL for temporary resource shortages */
189 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
190 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
191 	},
192 	{
193 		/* Reports QUEUE FULL for temporary resource shortages */
194 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
195 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
196 	},
197 	{
198 		/* Broken tagged queuing drive */
199 		{ T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
200 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
201 	},
202 	{
203 		/* Broken tagged queuing drive */
204 		{ T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
205 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
206 	},
207 	{
208 		/* Broken tagged queuing drive */
209 		{ T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
210 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
211 	},
212 	{
213 		/*
214 		 * Unfortunately, the Quantum Atlas III has the same
215 		 * problem as the Atlas II drives above.
216 		 * Reported by: "Johan Granlund" <johan@granlund.nu>
217 		 *
218 		 * For future reference, the drive with the problem was:
219 		 * QUANTUM QM39100TD-SW N1B0
220 		 *
221 		 * It's possible that Quantum will fix the problem in later
222 		 * firmware revisions.  If that happens, the quirk entry
223 		 * will need to be made specific to the firmware revisions
224 		 * with the problem.
225 		 *
226 		 */
227 		/* Reports QUEUE FULL for temporary resource shortages */
228 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
229 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
230 	},
231 	{
232 		/*
233 		 * 18 Gig Atlas III, same problem as the 9G version.
234 		 * Reported by: Andre Albsmeier
235 		 *		<andre.albsmeier@mchp.siemens.de>
236 		 *
237 		 * For future reference, the drive with the problem was:
238 		 * QUANTUM QM318000TD-S N491
239 		 */
240 		/* Reports QUEUE FULL for temporary resource shortages */
241 		{ T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
242 		/*quirks*/0, /*mintags*/24, /*maxtags*/32
243 	},
244 	{
245 		/*
246 		 * Broken tagged queuing drive
247 		 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
248 		 *         and: Martin Renters <martin@tdc.on.ca>
249 		 */
250 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
251 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
252 	},
253 		/*
254 		 * The Seagate Medalist Pro drives have very poor write
255 		 * performance with anything more than 2 tags.
256 		 *
257 		 * Reported by:  Paul van der Zwan <paulz@trantor.xs4all.nl>
258 		 * Drive:  <SEAGATE ST36530N 1444>
259 		 *
260 		 * Reported by:  Jeremy Lea <reg@shale.csir.co.za>
261 		 * Drive:  <SEAGATE ST34520W 1281>
262 		 *
263 		 * No one has actually reported that the 9G version
264 		 * (ST39140*) of the Medalist Pro has the same problem, but
265 		 * we're assuming that it does because the 4G and 6.5G
266 		 * versions of the drive are broken.
267 		 */
268 	{
269 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
270 		/*quirks*/0, /*mintags*/2, /*maxtags*/2
271 	},
272 	{
273 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
274 		/*quirks*/0, /*mintags*/2, /*maxtags*/2
275 	},
276 	{
277 		{ T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
278 		/*quirks*/0, /*mintags*/2, /*maxtags*/2
279 	},
280 	{
281 		/*
282 		 * Slow when tagged queueing is enabled.  Write performance
283 		 * steadily drops off with more and more concurrent
284 		 * transactions.  Best sequential write performance with
285 		 * tagged queueing turned off and write caching turned on.
286 		 *
287 		 * PR:  kern/10398
288 		 * Submitted by:  Hideaki Okada <hokada@isl.melco.co.jp>
289 		 * Drive:  DCAS-34330 w/ "S65A" firmware.
290 		 *
291 		 * The drive with the problem had the "S65A" firmware
292 		 * revision, and has also been reported (by Stephen J.
293 		 * Roznowski <sjr@home.net>) for a drive with the "S61A"
294 		 * firmware revision.
295 		 *
296 		 * Although no one has reported problems with the 2 gig
297 		 * version of the DCAS drive, the assumption is that it
298 		 * has the same problems as the 4 gig version.  Therefore
299 		 * this quirk entries disables tagged queueing for all
300 		 * DCAS drives.
301 		 */
302 		{ T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
303 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
304 	},
305 	{
306 		/* Broken tagged queuing drive */
307 		{ T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
308 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
309 	},
310 	{
311 		/* Broken tagged queuing drive */
312 		{ T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
313 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
314 	},
315 	{
316 		/* This does not support other than LUN 0 */
317 		{ T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*" },
318 		CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
319 	},
320 	{
321 		/*
322 		 * Broken tagged queuing drive.
323 		 * Submitted by:
324 		 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
325 		 * in PR kern/9535
326 		 */
327 		{ T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
328 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
329 	},
330         {
331 		/*
332 		 * Slow when tagged queueing is enabled. (1.5MB/sec versus
333 		 * 8MB/sec.)
334 		 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
335 		 * Best performance with these drives is achieved with
336 		 * tagged queueing turned off, and write caching turned on.
337 		 */
338 		{ T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
339 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
340         },
341         {
342 		/*
343 		 * Slow when tagged queueing is enabled. (1.5MB/sec versus
344 		 * 8MB/sec.)
345 		 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
346 		 * Best performance with these drives is achieved with
347 		 * tagged queueing turned off, and write caching turned on.
348 		 */
349 		{ T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
350 		/*quirks*/0, /*mintags*/0, /*maxtags*/0
351         },
352 	{
353 		/*
354 		 * Doesn't handle queue full condition correctly,
355 		 * so we need to limit maxtags to what the device
356 		 * can handle instead of determining this automatically.
357 		 */
358 		{ T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
359 		/*quirks*/0, /*mintags*/2, /*maxtags*/32
360 	},
361 	{
362 		/* Really only one LUN */
363 		{ T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
364 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
365 	},
366 	{
367 		/* I can't believe we need a quirk for DPT volumes. */
368 		{ T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
369 		CAM_QUIRK_NOLUNS,
370 		/*mintags*/0, /*maxtags*/255
371 	},
372 	{
373 		/*
374 		 * Many Sony CDROM drives don't like multi-LUN probing.
375 		 */
376 		{ T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
377 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
378 	},
379 	{
380 		/*
381 		 * This drive doesn't like multiple LUN probing.
382 		 * Submitted by:  Parag Patel <parag@cgt.com>
383 		 */
384 		{ T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R   CDU9*", "*" },
385 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
386 	},
387 	{
388 		{ T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
389 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
390 	},
391 	{
392 		/*
393 		 * The 8200 doesn't like multi-lun probing, and probably
394 		 * don't like serial number requests either.
395 		 */
396 		{
397 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
398 			"EXB-8200*", "*"
399 		},
400 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
401 	},
402 	{
403 		/*
404 		 * Let's try the same as above, but for a drive that says
405 		 * it's an IPL-6860 but is actually an EXB 8200.
406 		 */
407 		{
408 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
409 			"IPL-6860*", "*"
410 		},
411 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
412 	},
413 	{
414 		/*
415 		 * These Hitachi drives don't like multi-lun probing.
416 		 * The PR submitter has a DK319H, but says that the Linux
417 		 * kernel has a similar work-around for the DK312 and DK314,
418 		 * so all DK31* drives are quirked here.
419 		 * PR:            misc/18793
420 		 * Submitted by:  Paul Haddad <paul@pth.com>
421 		 */
422 		{ T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
423 		CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
424 	},
425 	{
426 		/*
427 		 * The Hitachi CJ series with J8A8 firmware apparantly has
428 		 * problems with tagged commands.
429 		 * PR: 23536
430 		 * Reported by: amagai@nue.org
431 		 */
432 		{ T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
433 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
434 	},
435 	{
436 		/*
437 		 * These are the large storage arrays.
438 		 * Submitted by:  William Carrel <william.carrel@infospace.com>
439 		 */
440 		{ T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
441 		CAM_QUIRK_HILUNS, 2, 1024
442 	},
443 	{
444 		/*
445 		 * This old revision of the TDC3600 is also SCSI-1, and
446 		 * hangs upon serial number probing.
447 		 */
448 		{
449 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
450 			" TDC 3600", "U07:"
451 		},
452 		CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
453 	},
454 	{
455 		/*
456 		 * Would repond to all LUNs if asked for.
457 		 */
458 		{
459 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
460 			"CP150", "*"
461 		},
462 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
463 	},
464 	{
465 		/*
466 		 * Would repond to all LUNs if asked for.
467 		 */
468 		{
469 			T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
470 			"96X2*", "*"
471 		},
472 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
473 	},
474 	{
475 		/* Submitted by: Matthew Dodd <winter@jurai.net> */
476 		{ T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
477 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
478 	},
479 	{
480 		/* Submitted by: Matthew Dodd <winter@jurai.net> */
481 		{ T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
482 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
483 	},
484 	{
485 		/* TeraSolutions special settings for TRC-22 RAID */
486 		{ T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
487 		  /*quirks*/0, /*mintags*/55, /*maxtags*/255
488 	},
489 	{
490 		/* Veritas Storage Appliance */
491 		{ T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
492 		  CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
493 	},
494 	{
495 		/*
496 		 * Would respond to all LUNs.  Device type and removable
497 		 * flag are jumper-selectable.
498 		 */
499 		{ T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
500 		  "Tahiti 1", "*"
501 		},
502 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
503 	},
504 	{
505 		/* EasyRAID E5A aka. areca ARC-6010 */
506 		{ T_DIRECT, SIP_MEDIA_FIXED, "easyRAID", "*", "*" },
507 		  CAM_QUIRK_NOHILUNS, /*mintags*/2, /*maxtags*/255
508 	},
509 	{
510 		{ T_ENCLOSURE, SIP_MEDIA_FIXED, "DP", "BACKPLANE", "*" },
511 		CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
512 	},
513 	{
514 		/* Default tagged queuing parameters for all devices */
515 		{
516 		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
517 		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
518 		},
519 		/*quirks*/0, /*mintags*/2, /*maxtags*/255
520 	},
521 };
522 
523 static const int scsi_quirk_table_size =
524 	sizeof(scsi_quirk_table) / sizeof(*scsi_quirk_table);
525 
526 static cam_status	proberegister(struct cam_periph *periph,
527 				      void *arg);
528 static void	 probeschedule(struct cam_periph *probe_periph);
529 static void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
530 static void	 proberequestdefaultnegotiation(struct cam_periph *periph);
531 static int       proberequestbackoff(struct cam_periph *periph,
532 				     struct cam_ed *device);
533 static void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
534 static void	 probecleanup(struct cam_periph *periph);
535 static void	 scsi_find_quirk(struct cam_ed *device);
536 static void	 scsi_scan_bus(struct cam_periph *periph, union ccb *ccb);
537 static void	 scsi_scan_lun(struct cam_periph *periph,
538 			       struct cam_path *path, cam_flags flags,
539 			       union ccb *ccb);
540 static void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
541 static struct cam_ed *
542 		 scsi_alloc_device(struct cam_eb *bus, struct cam_et *target,
543 				   lun_id_t lun_id);
544 static void	 scsi_devise_transport(struct cam_path *path);
545 static void	 scsi_set_transfer_settings(struct ccb_trans_settings *cts,
546 					    struct cam_ed *device,
547 					    int async_update);
548 static void	 scsi_toggle_tags(struct cam_path *path);
549 static void	 scsi_dev_async(u_int32_t async_code,
550 				struct cam_eb *bus,
551 				struct cam_et *target,
552 				struct cam_ed *device,
553 				void *async_arg);
554 static void	 scsi_action(union ccb *start_ccb);
555 
556 static struct xpt_xport scsi_xport = {
557 	.alloc_device = scsi_alloc_device,
558 	.action = scsi_action,
559 	.async = scsi_dev_async,
560 };
561 
562 struct xpt_xport *
563 scsi_get_xport(void)
564 {
565 	return (&scsi_xport);
566 }
567 
568 static void
569 probe_periph_init()
570 {
571 }
572 
573 static cam_status
574 proberegister(struct cam_periph *periph, void *arg)
575 {
576 	union ccb *request_ccb;	/* CCB representing the probe request */
577 	cam_status status;
578 	probe_softc *softc;
579 
580 	request_ccb = (union ccb *)arg;
581 	if (periph == NULL) {
582 		printf("proberegister: periph was NULL!!\n");
583 		return(CAM_REQ_CMP_ERR);
584 	}
585 
586 	if (request_ccb == NULL) {
587 		printf("proberegister: no probe CCB, "
588 		       "can't register device\n");
589 		return(CAM_REQ_CMP_ERR);
590 	}
591 
592 	softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
593 
594 	if (softc == NULL) {
595 		printf("proberegister: Unable to probe new device. "
596 		       "Unable to allocate softc\n");
597 		return(CAM_REQ_CMP_ERR);
598 	}
599 	TAILQ_INIT(&softc->request_ccbs);
600 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
601 			  periph_links.tqe);
602 	softc->flags = 0;
603 	periph->softc = softc;
604 	softc->periph = periph;
605 	softc->action = PROBE_INVALID;
606 	status = cam_periph_acquire(periph);
607 	if (status != CAM_REQ_CMP) {
608 		return (status);
609 	}
610 
611 
612 	/*
613 	 * Ensure we've waited at least a bus settle
614 	 * delay before attempting to probe the device.
615 	 * For HBAs that don't do bus resets, this won't make a difference.
616 	 */
617 	cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
618 				      scsi_delay);
619 	/*
620 	 * Ensure nobody slip in until probe finish.
621 	 */
622 	cam_freeze_devq_arg(periph->path,
623 	    RELSIM_RELEASE_RUNLEVEL, CAM_RL_XPT + 1);
624 	probeschedule(periph);
625 	return(CAM_REQ_CMP);
626 }
627 
628 static void
629 probeschedule(struct cam_periph *periph)
630 {
631 	struct ccb_pathinq cpi;
632 	union ccb *ccb;
633 	probe_softc *softc;
634 
635 	softc = (probe_softc *)periph->softc;
636 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
637 
638 	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
639 	cpi.ccb_h.func_code = XPT_PATH_INQ;
640 	xpt_action((union ccb *)&cpi);
641 
642 	/*
643 	 * If a device has gone away and another device, or the same one,
644 	 * is back in the same place, it should have a unit attention
645 	 * condition pending.  It will not report the unit attention in
646 	 * response to an inquiry, which may leave invalid transfer
647 	 * negotiations in effect.  The TUR will reveal the unit attention
648 	 * condition.  Only send the TUR for lun 0, since some devices
649 	 * will get confused by commands other than inquiry to non-existent
650 	 * luns.  If you think a device has gone away start your scan from
651 	 * lun 0.  This will insure that any bogus transfer settings are
652 	 * invalidated.
653 	 *
654 	 * If we haven't seen the device before and the controller supports
655 	 * some kind of transfer negotiation, negotiate with the first
656 	 * sent command if no bus reset was performed at startup.  This
657 	 * ensures that the device is not confused by transfer negotiation
658 	 * settings left over by loader or BIOS action.
659 	 */
660 	if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
661 	 && (ccb->ccb_h.target_lun == 0)) {
662 		PROBE_SET_ACTION(softc, PROBE_TUR);
663 	} else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
664 	      && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
665 		proberequestdefaultnegotiation(periph);
666 		PROBE_SET_ACTION(softc, PROBE_INQUIRY);
667 	} else {
668 		PROBE_SET_ACTION(softc, PROBE_INQUIRY);
669 	}
670 
671 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
672 		softc->flags |= PROBE_NO_ANNOUNCE;
673 	else
674 		softc->flags &= ~PROBE_NO_ANNOUNCE;
675 
676 	xpt_schedule(periph, CAM_PRIORITY_XPT);
677 }
678 
679 static void
680 probestart(struct cam_periph *periph, union ccb *start_ccb)
681 {
682 	/* Probe the device that our peripheral driver points to */
683 	struct ccb_scsiio *csio;
684 	probe_softc *softc;
685 
686 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
687 
688 	softc = (probe_softc *)periph->softc;
689 	csio = &start_ccb->csio;
690 
691 	switch (softc->action) {
692 	case PROBE_TUR:
693 	case PROBE_TUR_FOR_NEGOTIATION:
694 	case PROBE_DV_EXIT:
695 	{
696 		scsi_test_unit_ready(csio,
697 				     /*retries*/10,
698 				     probedone,
699 				     MSG_SIMPLE_Q_TAG,
700 				     SSD_FULL_SIZE,
701 				     /*timeout*/60000);
702 		break;
703 	}
704 	case PROBE_INQUIRY:
705 	case PROBE_FULL_INQUIRY:
706 	case PROBE_INQUIRY_BASIC_DV1:
707 	case PROBE_INQUIRY_BASIC_DV2:
708 	{
709 		u_int inquiry_len;
710 		struct scsi_inquiry_data *inq_buf;
711 
712 		inq_buf = &periph->path->device->inq_data;
713 
714 		/*
715 		 * If the device is currently configured, we calculate an
716 		 * MD5 checksum of the inquiry data, and if the serial number
717 		 * length is greater than 0, add the serial number data
718 		 * into the checksum as well.  Once the inquiry and the
719 		 * serial number check finish, we attempt to figure out
720 		 * whether we still have the same device.
721 		 */
722 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
723 
724 			MD5Init(&softc->context);
725 			MD5Update(&softc->context, (unsigned char *)inq_buf,
726 				  sizeof(struct scsi_inquiry_data));
727 			softc->flags |= PROBE_INQUIRY_CKSUM;
728 			if (periph->path->device->serial_num_len > 0) {
729 				MD5Update(&softc->context,
730 					  periph->path->device->serial_num,
731 					  periph->path->device->serial_num_len);
732 				softc->flags |= PROBE_SERIAL_CKSUM;
733 			}
734 			MD5Final(softc->digest, &softc->context);
735 		}
736 
737 		if (softc->action == PROBE_INQUIRY)
738 			inquiry_len = SHORT_INQUIRY_LENGTH;
739 		else
740 			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
741 
742 		/*
743 		 * Some parallel SCSI devices fail to send an
744 		 * ignore wide residue message when dealing with
745 		 * odd length inquiry requests.  Round up to be
746 		 * safe.
747 		 */
748 		inquiry_len = roundup2(inquiry_len, 2);
749 
750 		if (softc->action == PROBE_INQUIRY_BASIC_DV1
751 		 || softc->action == PROBE_INQUIRY_BASIC_DV2) {
752 			inq_buf = malloc(inquiry_len, M_CAMXPT, M_NOWAIT);
753 		}
754 		if (inq_buf == NULL) {
755 			xpt_print(periph->path, "malloc failure- skipping Basic"
756 			    "Domain Validation\n");
757 			PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
758 			scsi_test_unit_ready(csio,
759 					     /*retries*/4,
760 					     probedone,
761 					     MSG_SIMPLE_Q_TAG,
762 					     SSD_FULL_SIZE,
763 					     /*timeout*/60000);
764 			break;
765 		}
766 		scsi_inquiry(csio,
767 			     /*retries*/4,
768 			     probedone,
769 			     MSG_SIMPLE_Q_TAG,
770 			     (u_int8_t *)inq_buf,
771 			     inquiry_len,
772 			     /*evpd*/FALSE,
773 			     /*page_code*/0,
774 			     SSD_MIN_SIZE,
775 			     /*timeout*/60 * 1000);
776 		break;
777 	}
778 	case PROBE_MODE_SENSE:
779 	{
780 		void  *mode_buf;
781 		int    mode_buf_len;
782 
783 		mode_buf_len = sizeof(struct scsi_mode_header_6)
784 			     + sizeof(struct scsi_mode_blk_desc)
785 			     + sizeof(struct scsi_control_page);
786 		mode_buf = malloc(mode_buf_len, M_CAMXPT, M_NOWAIT);
787 		if (mode_buf != NULL) {
788 	                scsi_mode_sense(csio,
789 					/*retries*/4,
790 					probedone,
791 					MSG_SIMPLE_Q_TAG,
792 					/*dbd*/FALSE,
793 					SMS_PAGE_CTRL_CURRENT,
794 					SMS_CONTROL_MODE_PAGE,
795 					mode_buf,
796 					mode_buf_len,
797 					SSD_FULL_SIZE,
798 					/*timeout*/60000);
799 			break;
800 		}
801 		xpt_print(periph->path, "Unable to mode sense control page - "
802 		    "malloc failure\n");
803 		PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM_0);
804 	}
805 	/* FALLTHROUGH */
806 	case PROBE_SERIAL_NUM_0:
807 	{
808 		struct scsi_vpd_supported_page_list *vpd_list = NULL;
809 		struct cam_ed *device;
810 
811 		device = periph->path->device;
812 		if ((SCSI_QUIRK(device)->quirks & CAM_QUIRK_NOSERIAL) == 0) {
813 			vpd_list = malloc(sizeof(*vpd_list), M_CAMXPT,
814 			    M_NOWAIT | M_ZERO);
815 		}
816 
817 		if (vpd_list != NULL) {
818 			scsi_inquiry(csio,
819 				     /*retries*/4,
820 				     probedone,
821 				     MSG_SIMPLE_Q_TAG,
822 				     (u_int8_t *)vpd_list,
823 				     sizeof(*vpd_list),
824 				     /*evpd*/TRUE,
825 				     SVPD_SUPPORTED_PAGE_LIST,
826 				     SSD_MIN_SIZE,
827 				     /*timeout*/60 * 1000);
828 			break;
829 		}
830 		/*
831 		 * We'll have to do without, let our probedone
832 		 * routine finish up for us.
833 		 */
834 		start_ccb->csio.data_ptr = NULL;
835 		probedone(periph, start_ccb);
836 		return;
837 	}
838 	case PROBE_SERIAL_NUM_1:
839 	{
840 		struct scsi_vpd_unit_serial_number *serial_buf;
841 		struct cam_ed* device;
842 
843 		serial_buf = NULL;
844 		device = periph->path->device;
845 		if (device->serial_num != NULL) {
846 			free(device->serial_num, M_CAMXPT);
847 			device->serial_num = NULL;
848 			device->serial_num_len = 0;
849 		}
850 
851 		serial_buf = (struct scsi_vpd_unit_serial_number *)
852 			malloc(sizeof(*serial_buf), M_CAMXPT, M_NOWAIT|M_ZERO);
853 
854 		if (serial_buf != NULL) {
855 			scsi_inquiry(csio,
856 				     /*retries*/4,
857 				     probedone,
858 				     MSG_SIMPLE_Q_TAG,
859 				     (u_int8_t *)serial_buf,
860 				     sizeof(*serial_buf),
861 				     /*evpd*/TRUE,
862 				     SVPD_UNIT_SERIAL_NUMBER,
863 				     SSD_MIN_SIZE,
864 				     /*timeout*/60 * 1000);
865 			break;
866 		}
867 		/*
868 		 * We'll have to do without, let our probedone
869 		 * routine finish up for us.
870 		 */
871 		start_ccb->csio.data_ptr = NULL;
872 		probedone(periph, start_ccb);
873 		return;
874 	}
875 	case PROBE_INVALID:
876 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
877 		    ("probestart: invalid action state\n"));
878 	default:
879 		break;
880 	}
881 	xpt_action(start_ccb);
882 }
883 
884 static void
885 proberequestdefaultnegotiation(struct cam_periph *periph)
886 {
887 	struct ccb_trans_settings cts;
888 
889 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
890 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
891 	cts.type = CTS_TYPE_USER_SETTINGS;
892 	xpt_action((union ccb *)&cts);
893 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
894 		return;
895 	}
896 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
897 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
898 	xpt_action((union ccb *)&cts);
899 }
900 
901 /*
902  * Backoff Negotiation Code- only pertinent for SPI devices.
903  */
904 static int
905 proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
906 {
907 	struct ccb_trans_settings cts;
908 	struct ccb_trans_settings_spi *spi;
909 
910 	memset(&cts, 0, sizeof (cts));
911 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
912 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
913 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
914 	xpt_action((union ccb *)&cts);
915 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
916 		if (bootverbose) {
917 			xpt_print(periph->path,
918 			    "failed to get current device settings\n");
919 		}
920 		return (0);
921 	}
922 	if (cts.transport != XPORT_SPI) {
923 		if (bootverbose) {
924 			xpt_print(periph->path, "not SPI transport\n");
925 		}
926 		return (0);
927 	}
928 	spi = &cts.xport_specific.spi;
929 
930 	/*
931 	 * We cannot renegotiate sync rate if we don't have one.
932 	 */
933 	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
934 		if (bootverbose) {
935 			xpt_print(periph->path, "no sync rate known\n");
936 		}
937 		return (0);
938 	}
939 
940 	/*
941 	 * We'll assert that we don't have to touch PPR options- the
942 	 * SIM will see what we do with period and offset and adjust
943 	 * the PPR options as appropriate.
944 	 */
945 
946 	/*
947 	 * A sync rate with unknown or zero offset is nonsensical.
948 	 * A sync period of zero means Async.
949 	 */
950 	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
951 	 || spi->sync_offset == 0 || spi->sync_period == 0) {
952 		if (bootverbose) {
953 			xpt_print(periph->path, "no sync rate available\n");
954 		}
955 		return (0);
956 	}
957 
958 	if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
959 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
960 		    ("hit async: giving up on DV\n"));
961 		return (0);
962 	}
963 
964 
965 	/*
966 	 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
967 	 * We don't try to remember 'last' settings to see if the SIM actually
968 	 * gets into the speed we want to set. We check on the SIM telling
969 	 * us that a requested speed is bad, but otherwise don't try and
970 	 * check the speed due to the asynchronous and handshake nature
971 	 * of speed setting.
972 	 */
973 	spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
974 	for (;;) {
975 		spi->sync_period++;
976 		if (spi->sync_period >= 0xf) {
977 			spi->sync_period = 0;
978 			spi->sync_offset = 0;
979 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
980 			    ("setting to async for DV\n"));
981 			/*
982 			 * Once we hit async, we don't want to try
983 			 * any more settings.
984 			 */
985 			device->flags |= CAM_DEV_DV_HIT_BOTTOM;
986 		} else if (bootverbose) {
987 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
988 			    ("DV: period 0x%x\n", spi->sync_period));
989 			printf("setting period to 0x%x\n", spi->sync_period);
990 		}
991 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
992 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
993 		xpt_action((union ccb *)&cts);
994 		if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
995 			break;
996 		}
997 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
998 		    ("DV: failed to set period 0x%x\n", spi->sync_period));
999 		if (spi->sync_period == 0) {
1000 			return (0);
1001 		}
1002 	}
1003 	return (1);
1004 }
1005 
1006 static void
1007 probedone(struct cam_periph *periph, union ccb *done_ccb)
1008 {
1009 	probe_softc *softc;
1010 	struct cam_path *path;
1011 	u_int32_t  priority;
1012 
1013 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
1014 
1015 	softc = (probe_softc *)periph->softc;
1016 	path = done_ccb->ccb_h.path;
1017 	priority = done_ccb->ccb_h.pinfo.priority;
1018 
1019 	switch (softc->action) {
1020 	case PROBE_TUR:
1021 	{
1022 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1023 
1024 			if (cam_periph_error(done_ccb, 0,
1025 					     SF_NO_PRINT, NULL) == ERESTART)
1026 				return;
1027 			else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1028 				/* Don't wedge the queue */
1029 				xpt_release_devq(done_ccb->ccb_h.path,
1030 						 /*count*/1,
1031 						 /*run_queue*/TRUE);
1032 		}
1033 		PROBE_SET_ACTION(softc, PROBE_INQUIRY);
1034 		xpt_release_ccb(done_ccb);
1035 		xpt_schedule(periph, priority);
1036 		return;
1037 	}
1038 	case PROBE_INQUIRY:
1039 	case PROBE_FULL_INQUIRY:
1040 	{
1041 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1042 			struct scsi_inquiry_data *inq_buf;
1043 			u_int8_t periph_qual;
1044 
1045 			path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1046 			inq_buf = &path->device->inq_data;
1047 
1048 			periph_qual = SID_QUAL(inq_buf);
1049 
1050 			switch(periph_qual) {
1051 			case SID_QUAL_LU_CONNECTED:
1052 			{
1053 				u_int8_t len;
1054 
1055 				/*
1056 				 * We conservatively request only
1057 				 * SHORT_INQUIRY_LEN bytes of inquiry
1058 				 * information during our first try
1059 				 * at sending an INQUIRY. If the device
1060 				 * has more information to give,
1061 				 * perform a second request specifying
1062 				 * the amount of information the device
1063 				 * is willing to give.
1064 				 */
1065 				len = inq_buf->additional_length
1066 				    + offsetof(struct scsi_inquiry_data,
1067                                                additional_length) + 1;
1068 				if (softc->action == PROBE_INQUIRY
1069 				    && len > SHORT_INQUIRY_LENGTH) {
1070 					PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1071 					xpt_release_ccb(done_ccb);
1072 					xpt_schedule(periph, priority);
1073 					return;
1074 				}
1075 
1076 				scsi_find_quirk(path->device);
1077 
1078 				scsi_devise_transport(path);
1079 				if (INQ_DATA_TQ_ENABLED(inq_buf))
1080 					PROBE_SET_ACTION(softc, PROBE_MODE_SENSE);
1081 				else
1082 					PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM_0);
1083 
1084 				if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1085 					path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1086 					xpt_acquire_device(path->device);
1087 				}
1088 				xpt_release_ccb(done_ccb);
1089 				xpt_schedule(periph, priority);
1090 				return;
1091 			}
1092 			default:
1093 				break;
1094 			}
1095 		} else if (cam_periph_error(done_ccb, 0,
1096 					    done_ccb->ccb_h.target_lun > 0
1097 					    ? SF_RETRY_UA|SF_QUIET_IR
1098 					    : SF_RETRY_UA,
1099 					    &softc->saved_ccb) == ERESTART) {
1100 			return;
1101 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1102 			/* Don't wedge the queue */
1103 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1104 					 /*run_queue*/TRUE);
1105 		}
1106 		/*
1107 		 * If we get to this point, we got an error status back
1108 		 * from the inquiry and the error status doesn't require
1109 		 * automatically retrying the command.  Therefore, the
1110 		 * inquiry failed.  If we had inquiry information before
1111 		 * for this device, but this latest inquiry command failed,
1112 		 * the device has probably gone away.  If this device isn't
1113 		 * already marked unconfigured, notify the peripheral
1114 		 * drivers that this device is no more.
1115 		 */
1116 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
1117 			/* Send the async notification. */
1118 			xpt_async(AC_LOST_DEVICE, path, NULL);
1119 
1120 		xpt_release_ccb(done_ccb);
1121 		break;
1122 	}
1123 	case PROBE_MODE_SENSE:
1124 	{
1125 		struct ccb_scsiio *csio;
1126 		struct scsi_mode_header_6 *mode_hdr;
1127 
1128 		csio = &done_ccb->csio;
1129 		mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
1130 		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1131 			struct scsi_control_page *page;
1132 			u_int8_t *offset;
1133 
1134 			offset = ((u_int8_t *)&mode_hdr[1])
1135 			    + mode_hdr->blk_desc_len;
1136 			page = (struct scsi_control_page *)offset;
1137 			path->device->queue_flags = page->queue_flags;
1138 		} else if (cam_periph_error(done_ccb, 0,
1139 					    SF_RETRY_UA|SF_NO_PRINT,
1140 					    &softc->saved_ccb) == ERESTART) {
1141 			return;
1142 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1143 			/* Don't wedge the queue */
1144 			xpt_release_devq(done_ccb->ccb_h.path,
1145 					 /*count*/1, /*run_queue*/TRUE);
1146 		}
1147 		xpt_release_ccb(done_ccb);
1148 		free(mode_hdr, M_CAMXPT);
1149 		PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM_0);
1150 		xpt_schedule(periph, priority);
1151 		return;
1152 	}
1153 	case PROBE_SERIAL_NUM_0:
1154 	{
1155 		struct ccb_scsiio *csio;
1156 		struct scsi_vpd_supported_page_list *page_list;
1157 		int length, serialnum_supported, i;
1158 
1159 		serialnum_supported = 0;
1160 		csio = &done_ccb->csio;
1161 		page_list =
1162 		    (struct scsi_vpd_supported_page_list *)csio->data_ptr;
1163 
1164 		if (page_list == NULL) {
1165 			/*
1166 			 * Don't process the command as it was never sent
1167 			 */
1168 		} else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
1169 		    && (page_list->length > 0)) {
1170 			length = min(page_list->length,
1171 			    SVPD_SUPPORTED_PAGES_SIZE);
1172 			for (i = 0; i < length; i++) {
1173 				if (page_list->list[i] ==
1174 				    SVPD_UNIT_SERIAL_NUMBER) {
1175 					serialnum_supported = 1;
1176 					break;
1177 				}
1178 			}
1179 		} else if (cam_periph_error(done_ccb, 0,
1180 					    SF_RETRY_UA|SF_NO_PRINT,
1181 					    &softc->saved_ccb) == ERESTART) {
1182 			return;
1183 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1184 			/* Don't wedge the queue */
1185 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1186 					 /*run_queue*/TRUE);
1187 		}
1188 
1189 		if (page_list != NULL)
1190 			free(page_list, M_CAMXPT);
1191 
1192 		if (serialnum_supported) {
1193 			xpt_release_ccb(done_ccb);
1194 			PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM_1);
1195 			xpt_schedule(periph, priority);
1196 			return;
1197 		}
1198 
1199 		csio->data_ptr = NULL;
1200 		/* FALLTHROUGH */
1201 	}
1202 
1203 	case PROBE_SERIAL_NUM_1:
1204 	{
1205 		struct ccb_scsiio *csio;
1206 		struct scsi_vpd_unit_serial_number *serial_buf;
1207 		u_int32_t  priority;
1208 		int changed;
1209 		int have_serialnum;
1210 
1211 		changed = 1;
1212 		have_serialnum = 0;
1213 		csio = &done_ccb->csio;
1214 		priority = done_ccb->ccb_h.pinfo.priority;
1215 		serial_buf =
1216 		    (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
1217 
1218 		/* Clean up from previous instance of this device */
1219 		if (path->device->serial_num != NULL) {
1220 			free(path->device->serial_num, M_CAMXPT);
1221 			path->device->serial_num = NULL;
1222 			path->device->serial_num_len = 0;
1223 		}
1224 
1225 		if (serial_buf == NULL) {
1226 			/*
1227 			 * Don't process the command as it was never sent
1228 			 */
1229 		} else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
1230 			&& (serial_buf->length > 0)) {
1231 
1232 			have_serialnum = 1;
1233 			path->device->serial_num =
1234 				(u_int8_t *)malloc((serial_buf->length + 1),
1235 						   M_CAMXPT, M_NOWAIT);
1236 			if (path->device->serial_num != NULL) {
1237 				bcopy(serial_buf->serial_num,
1238 				      path->device->serial_num,
1239 				      serial_buf->length);
1240 				path->device->serial_num_len =
1241 				    serial_buf->length;
1242 				path->device->serial_num[serial_buf->length]
1243 				    = '\0';
1244 			}
1245 		} else if (cam_periph_error(done_ccb, 0,
1246 					    SF_RETRY_UA|SF_NO_PRINT,
1247 					    &softc->saved_ccb) == ERESTART) {
1248 			return;
1249 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1250 			/* Don't wedge the queue */
1251 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1252 					 /*run_queue*/TRUE);
1253 		}
1254 
1255 		/*
1256 		 * Let's see if we have seen this device before.
1257 		 */
1258 		if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
1259 			MD5_CTX context;
1260 			u_int8_t digest[16];
1261 
1262 			MD5Init(&context);
1263 
1264 			MD5Update(&context,
1265 				  (unsigned char *)&path->device->inq_data,
1266 				  sizeof(struct scsi_inquiry_data));
1267 
1268 			if (have_serialnum)
1269 				MD5Update(&context, serial_buf->serial_num,
1270 					  serial_buf->length);
1271 
1272 			MD5Final(digest, &context);
1273 			if (bcmp(softc->digest, digest, 16) == 0)
1274 				changed = 0;
1275 
1276 			/*
1277 			 * XXX Do we need to do a TUR in order to ensure
1278 			 *     that the device really hasn't changed???
1279 			 */
1280 			if ((changed != 0)
1281 			 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
1282 				xpt_async(AC_LOST_DEVICE, path, NULL);
1283 		}
1284 		if (serial_buf != NULL)
1285 			free(serial_buf, M_CAMXPT);
1286 
1287 		if (changed != 0) {
1288 			/*
1289 			 * Now that we have all the necessary
1290 			 * information to safely perform transfer
1291 			 * negotiations... Controllers don't perform
1292 			 * any negotiation or tagged queuing until
1293 			 * after the first XPT_SET_TRAN_SETTINGS ccb is
1294 			 * received.  So, on a new device, just retrieve
1295 			 * the user settings, and set them as the current
1296 			 * settings to set the device up.
1297 			 */
1298 			proberequestdefaultnegotiation(periph);
1299 			xpt_release_ccb(done_ccb);
1300 
1301 			/*
1302 			 * Perform a TUR to allow the controller to
1303 			 * perform any necessary transfer negotiation.
1304 			 */
1305 			PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1306 			xpt_schedule(periph, priority);
1307 			return;
1308 		}
1309 		xpt_release_ccb(done_ccb);
1310 		break;
1311 	}
1312 	case PROBE_TUR_FOR_NEGOTIATION:
1313 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1314 			DELAY(500000);
1315 			if (cam_periph_error(done_ccb, 0, SF_RETRY_UA,
1316 			    NULL) == ERESTART)
1317 				return;
1318 		}
1319 	/* FALLTHROUGH */
1320 	case PROBE_DV_EXIT:
1321 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1322 			/* Don't wedge the queue */
1323 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1324 					 /*run_queue*/TRUE);
1325 		}
1326 		/*
1327 		 * Do Domain Validation for lun 0 on devices that claim
1328 		 * to support Synchronous Transfer modes.
1329 		 */
1330 	 	if (softc->action == PROBE_TUR_FOR_NEGOTIATION
1331 		 && done_ccb->ccb_h.target_lun == 0
1332 		 && (path->device->inq_data.flags & SID_Sync) != 0
1333                  && (path->device->flags & CAM_DEV_IN_DV) == 0) {
1334 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1335 			    ("Begin Domain Validation\n"));
1336 			path->device->flags |= CAM_DEV_IN_DV;
1337 			xpt_release_ccb(done_ccb);
1338 			PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV1);
1339 			xpt_schedule(periph, priority);
1340 			return;
1341 		}
1342 		if (softc->action == PROBE_DV_EXIT) {
1343 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1344 			    ("Leave Domain Validation\n"));
1345 		}
1346 		if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1347 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1348 			xpt_acquire_device(path->device);
1349 		}
1350 		path->device->flags &=
1351 		    ~(CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1352 		if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1353 			/* Inform the XPT that a new device has been found */
1354 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1355 			xpt_action(done_ccb);
1356 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1357 				  done_ccb);
1358 		}
1359 		xpt_release_ccb(done_ccb);
1360 		break;
1361 	case PROBE_INQUIRY_BASIC_DV1:
1362 	case PROBE_INQUIRY_BASIC_DV2:
1363 	{
1364 		struct scsi_inquiry_data *nbuf;
1365 		struct ccb_scsiio *csio;
1366 
1367 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1368 			/* Don't wedge the queue */
1369 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1370 					 /*run_queue*/TRUE);
1371 		}
1372 		csio = &done_ccb->csio;
1373 		nbuf = (struct scsi_inquiry_data *)csio->data_ptr;
1374 		if (bcmp(nbuf, &path->device->inq_data, SHORT_INQUIRY_LENGTH)) {
1375 			xpt_print(path,
1376 			    "inquiry data fails comparison at DV%d step\n",
1377 			    softc->action == PROBE_INQUIRY_BASIC_DV1 ? 1 : 2);
1378 			if (proberequestbackoff(periph, path->device)) {
1379 				path->device->flags &= ~CAM_DEV_IN_DV;
1380 				PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1381 			} else {
1382 				/* give up */
1383 				PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
1384 			}
1385 			free(nbuf, M_CAMXPT);
1386 			xpt_release_ccb(done_ccb);
1387 			xpt_schedule(periph, priority);
1388 			return;
1389 		}
1390 		free(nbuf, M_CAMXPT);
1391 		if (softc->action == PROBE_INQUIRY_BASIC_DV1) {
1392 			PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV2);
1393 			xpt_release_ccb(done_ccb);
1394 			xpt_schedule(periph, priority);
1395 			return;
1396 		}
1397 		if (softc->action == PROBE_INQUIRY_BASIC_DV2) {
1398 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1399 			    ("Leave Domain Validation Successfully\n"));
1400 		}
1401 		if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1402 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1403 			xpt_acquire_device(path->device);
1404 		}
1405 		path->device->flags &=
1406 		    ~(CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1407 		if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1408 			/* Inform the XPT that a new device has been found */
1409 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1410 			xpt_action(done_ccb);
1411 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1412 				  done_ccb);
1413 		}
1414 		xpt_release_ccb(done_ccb);
1415 		break;
1416 	}
1417 	case PROBE_INVALID:
1418 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
1419 		    ("probedone: invalid action state\n"));
1420 	default:
1421 		break;
1422 	}
1423 	done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
1424 	TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
1425 	done_ccb->ccb_h.status = CAM_REQ_CMP;
1426 	xpt_done(done_ccb);
1427 	if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
1428 		cam_release_devq(periph->path,
1429 		    RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_XPT + 1, FALSE);
1430 		cam_periph_invalidate(periph);
1431 		cam_periph_release_locked(periph);
1432 	} else {
1433 		probeschedule(periph);
1434 	}
1435 }
1436 
1437 static void
1438 probecleanup(struct cam_periph *periph)
1439 {
1440 	free(periph->softc, M_CAMXPT);
1441 }
1442 
1443 static void
1444 scsi_find_quirk(struct cam_ed *device)
1445 {
1446 	struct scsi_quirk_entry *quirk;
1447 	caddr_t	match;
1448 
1449 	match = cam_quirkmatch((caddr_t)&device->inq_data,
1450 			       (caddr_t)scsi_quirk_table,
1451 			       sizeof(scsi_quirk_table) /
1452 			       sizeof(*scsi_quirk_table),
1453 			       sizeof(*scsi_quirk_table), scsi_inquiry_match);
1454 
1455 	if (match == NULL)
1456 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
1457 
1458 	quirk = (struct scsi_quirk_entry *)match;
1459 	device->quirk = quirk;
1460 	device->mintags = quirk->mintags;
1461 	device->maxtags = quirk->maxtags;
1462 }
1463 
1464 static int
1465 sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS)
1466 {
1467 	int error, bool;
1468 
1469 	bool = cam_srch_hi;
1470 	error = sysctl_handle_int(oidp, &bool, 0, req);
1471 	if (error != 0 || req->newptr == NULL)
1472 		return (error);
1473 	if (bool == 0 || bool == 1) {
1474 		cam_srch_hi = bool;
1475 		return (0);
1476 	} else {
1477 		return (EINVAL);
1478 	}
1479 }
1480 
1481 typedef struct {
1482 	union	ccb *request_ccb;
1483 	struct 	ccb_pathinq *cpi;
1484 	int	counter;
1485 } scsi_scan_bus_info;
1486 
1487 /*
1488  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1489  * As the scan progresses, scsi_scan_bus is used as the
1490  * callback on completion function.
1491  */
1492 static void
1493 scsi_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1494 {
1495 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1496 		  ("scsi_scan_bus\n"));
1497 	switch (request_ccb->ccb_h.func_code) {
1498 	case XPT_SCAN_BUS:
1499 	{
1500 		scsi_scan_bus_info *scan_info;
1501 		union	ccb *work_ccb, *reset_ccb;
1502 		struct	cam_path *path;
1503 		u_int	i;
1504 		u_int	max_target;
1505 		u_int	initiator_id;
1506 
1507 		/* Find out the characteristics of the bus */
1508 		work_ccb = xpt_alloc_ccb_nowait();
1509 		if (work_ccb == NULL) {
1510 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1511 			xpt_done(request_ccb);
1512 			return;
1513 		}
1514 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1515 			      request_ccb->ccb_h.pinfo.priority);
1516 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1517 		xpt_action(work_ccb);
1518 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1519 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1520 			xpt_free_ccb(work_ccb);
1521 			xpt_done(request_ccb);
1522 			return;
1523 		}
1524 
1525 		if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
1526 			/*
1527 			 * Can't scan the bus on an adapter that
1528 			 * cannot perform the initiator role.
1529 			 */
1530 			request_ccb->ccb_h.status = CAM_REQ_CMP;
1531 			xpt_free_ccb(work_ccb);
1532 			xpt_done(request_ccb);
1533 			return;
1534 		}
1535 
1536 		/* We may need to reset bus first, if we haven't done it yet. */
1537 		if ((work_ccb->cpi.hba_inquiry &
1538 		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1539 		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1540 		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
1541 			reset_ccb = xpt_alloc_ccb_nowait();
1542 			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1543 			      CAM_PRIORITY_NONE);
1544 			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1545 			xpt_action(reset_ccb);
1546 			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1547 				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1548 				xpt_free_ccb(reset_ccb);
1549 				xpt_free_ccb(work_ccb);
1550 				xpt_done(request_ccb);
1551 				return;
1552 			}
1553 			xpt_free_ccb(reset_ccb);
1554 		}
1555 
1556 		/* Save some state for use while we probe for devices */
1557 		scan_info = (scsi_scan_bus_info *)
1558 		    malloc(sizeof(scsi_scan_bus_info), M_CAMXPT, M_NOWAIT);
1559 		if (scan_info == NULL) {
1560 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1561 			xpt_done(request_ccb);
1562 			return;
1563 		}
1564 		scan_info->request_ccb = request_ccb;
1565 		scan_info->cpi = &work_ccb->cpi;
1566 
1567 		/* Cache on our stack so we can work asynchronously */
1568 		max_target = scan_info->cpi->max_target;
1569 		initiator_id = scan_info->cpi->initiator_id;
1570 
1571 
1572 		/*
1573 		 * We can scan all targets in parallel, or do it sequentially.
1574 		 */
1575 		if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
1576 			max_target = 0;
1577 			scan_info->counter = 0;
1578 		} else {
1579 			scan_info->counter = scan_info->cpi->max_target + 1;
1580 			if (scan_info->cpi->initiator_id < scan_info->counter) {
1581 				scan_info->counter--;
1582 			}
1583 		}
1584 
1585 		for (i = 0; i <= max_target; i++) {
1586 			cam_status status;
1587 			if (i == initiator_id)
1588 				continue;
1589 
1590 			status = xpt_create_path(&path, xpt_periph,
1591 						 request_ccb->ccb_h.path_id,
1592 						 i, 0);
1593 			if (status != CAM_REQ_CMP) {
1594 				printf("scsi_scan_bus: xpt_create_path failed"
1595 				       " with status %#x, bus scan halted\n",
1596 				       status);
1597 				free(scan_info, M_CAMXPT);
1598 				request_ccb->ccb_h.status = status;
1599 				xpt_free_ccb(work_ccb);
1600 				xpt_done(request_ccb);
1601 				break;
1602 			}
1603 			work_ccb = xpt_alloc_ccb_nowait();
1604 			if (work_ccb == NULL) {
1605 				xpt_free_ccb((union ccb *)scan_info->cpi);
1606 				free(scan_info, M_CAMXPT);
1607 				xpt_free_path(path);
1608 				request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1609 				xpt_done(request_ccb);
1610 				break;
1611 			}
1612 			xpt_setup_ccb(&work_ccb->ccb_h, path,
1613 				      request_ccb->ccb_h.pinfo.priority);
1614 			work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1615 			work_ccb->ccb_h.cbfcnp = scsi_scan_bus;
1616 			work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1617 			work_ccb->crcn.flags = request_ccb->crcn.flags;
1618 			xpt_action(work_ccb);
1619 		}
1620 		break;
1621 	}
1622 	case XPT_SCAN_LUN:
1623 	{
1624 		cam_status status;
1625 		struct cam_path *path;
1626 		scsi_scan_bus_info *scan_info;
1627 		path_id_t path_id;
1628 		target_id_t target_id;
1629 		lun_id_t lun_id;
1630 
1631 		/* Reuse the same CCB to query if a device was really found */
1632 		scan_info = (scsi_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
1633 		xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
1634 			      request_ccb->ccb_h.pinfo.priority);
1635 		request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1636 
1637 		path_id = request_ccb->ccb_h.path_id;
1638 		target_id = request_ccb->ccb_h.target_id;
1639 		lun_id = request_ccb->ccb_h.target_lun;
1640 		xpt_action(request_ccb);
1641 
1642 		if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
1643 			struct cam_ed *device;
1644 			struct cam_et *target;
1645 			int phl;
1646 
1647 			/*
1648 			 * If we already probed lun 0 successfully, or
1649 			 * we have additional configured luns on this
1650 			 * target that might have "gone away", go onto
1651 			 * the next lun.
1652 			 */
1653 			target = request_ccb->ccb_h.path->target;
1654 			/*
1655 			 * We may touch devices that we don't
1656 			 * hold references too, so ensure they
1657 			 * don't disappear out from under us.
1658 			 * The target above is referenced by the
1659 			 * path in the request ccb.
1660 			 */
1661 			phl = 0;
1662 			device = TAILQ_FIRST(&target->ed_entries);
1663 			if (device != NULL) {
1664 				phl = CAN_SRCH_HI_SPARSE(device);
1665 				if (device->lun_id == 0)
1666 					device = TAILQ_NEXT(device, links);
1667 			}
1668 			if ((lun_id != 0) || (device != NULL)) {
1669 				if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
1670 					lun_id++;
1671 			}
1672 		} else {
1673 			struct cam_ed *device;
1674 
1675 			device = request_ccb->ccb_h.path->device;
1676 
1677 			if ((SCSI_QUIRK(device)->quirks &
1678 			    CAM_QUIRK_NOLUNS) == 0) {
1679 				/* Try the next lun */
1680 				if (lun_id < (CAM_SCSI2_MAXLUN-1)
1681 				  || CAN_SRCH_HI_DENSE(device))
1682 					lun_id++;
1683 			}
1684 		}
1685 
1686 		/*
1687 		 * Free the current request path- we're done with it.
1688 		 */
1689 		xpt_free_path(request_ccb->ccb_h.path);
1690 
1691 		/*
1692 		 * Check to see if we scan any further luns.
1693 		 */
1694 		if (lun_id == request_ccb->ccb_h.target_lun
1695                  || lun_id > scan_info->cpi->max_lun) {
1696 			int done;
1697 
1698  hop_again:
1699 			done = 0;
1700 			if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
1701 				scan_info->counter++;
1702 				if (scan_info->counter ==
1703 				    scan_info->cpi->initiator_id) {
1704 					scan_info->counter++;
1705 				}
1706 				if (scan_info->counter >=
1707 				    scan_info->cpi->max_target+1) {
1708 					done = 1;
1709 				}
1710 			} else {
1711 				scan_info->counter--;
1712 				if (scan_info->counter == 0) {
1713 					done = 1;
1714 				}
1715 			}
1716 			if (done) {
1717 				xpt_free_ccb(request_ccb);
1718 				xpt_free_ccb((union ccb *)scan_info->cpi);
1719 				request_ccb = scan_info->request_ccb;
1720 				free(scan_info, M_CAMXPT);
1721 				request_ccb->ccb_h.status = CAM_REQ_CMP;
1722 				xpt_done(request_ccb);
1723 				break;
1724 			}
1725 
1726 			if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
1727 				xpt_free_ccb(request_ccb);
1728 				break;
1729 			}
1730 			status = xpt_create_path(&path, xpt_periph,
1731 			    scan_info->request_ccb->ccb_h.path_id,
1732 			    scan_info->counter, 0);
1733 			if (status != CAM_REQ_CMP) {
1734 				printf("scsi_scan_bus: xpt_create_path failed"
1735 				    " with status %#x, bus scan halted\n",
1736 			       	    status);
1737 				xpt_free_ccb(request_ccb);
1738 				xpt_free_ccb((union ccb *)scan_info->cpi);
1739 				request_ccb = scan_info->request_ccb;
1740 				free(scan_info, M_CAMXPT);
1741 				request_ccb->ccb_h.status = status;
1742 				xpt_done(request_ccb);
1743 				break;
1744 			}
1745 			xpt_setup_ccb(&request_ccb->ccb_h, path,
1746 			    request_ccb->ccb_h.pinfo.priority);
1747 			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1748 			request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
1749 			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
1750 			request_ccb->crcn.flags =
1751 			    scan_info->request_ccb->crcn.flags;
1752 		} else {
1753 			status = xpt_create_path(&path, xpt_periph,
1754 						 path_id, target_id, lun_id);
1755 			if (status != CAM_REQ_CMP) {
1756 				printf("scsi_scan_bus: xpt_create_path failed "
1757 				       "with status %#x, halting LUN scan\n",
1758 			 	       status);
1759 				goto hop_again;
1760 			}
1761 			xpt_setup_ccb(&request_ccb->ccb_h, path,
1762 				      request_ccb->ccb_h.pinfo.priority);
1763 			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1764 			request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
1765 			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
1766 			request_ccb->crcn.flags =
1767 				scan_info->request_ccb->crcn.flags;
1768 		}
1769 		xpt_action(request_ccb);
1770 		break;
1771 	}
1772 	default:
1773 		break;
1774 	}
1775 }
1776 
1777 static void
1778 scsi_scan_lun(struct cam_periph *periph, struct cam_path *path,
1779 	     cam_flags flags, union ccb *request_ccb)
1780 {
1781 	struct ccb_pathinq cpi;
1782 	cam_status status;
1783 	struct cam_path *new_path;
1784 	struct cam_periph *old_periph;
1785 
1786 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("scsi_scan_lun\n"));
1787 
1788 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1789 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1790 	xpt_action((union ccb *)&cpi);
1791 
1792 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
1793 		if (request_ccb != NULL) {
1794 			request_ccb->ccb_h.status = cpi.ccb_h.status;
1795 			xpt_done(request_ccb);
1796 		}
1797 		return;
1798 	}
1799 
1800 	if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
1801 		/*
1802 		 * Can't scan the bus on an adapter that
1803 		 * cannot perform the initiator role.
1804 		 */
1805 		if (request_ccb != NULL) {
1806 			request_ccb->ccb_h.status = CAM_REQ_CMP;
1807 			xpt_done(request_ccb);
1808 		}
1809 		return;
1810 	}
1811 
1812 	if (request_ccb == NULL) {
1813 		request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
1814 		if (request_ccb == NULL) {
1815 			xpt_print(path, "scsi_scan_lun: can't allocate CCB, "
1816 			    "can't continue\n");
1817 			return;
1818 		}
1819 		new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
1820 		if (new_path == NULL) {
1821 			xpt_print(path, "scsi_scan_lun: can't allocate path, "
1822 			    "can't continue\n");
1823 			free(request_ccb, M_CAMXPT);
1824 			return;
1825 		}
1826 		status = xpt_compile_path(new_path, xpt_periph,
1827 					  path->bus->path_id,
1828 					  path->target->target_id,
1829 					  path->device->lun_id);
1830 
1831 		if (status != CAM_REQ_CMP) {
1832 			xpt_print(path, "scsi_scan_lun: can't compile path, "
1833 			    "can't continue\n");
1834 			free(request_ccb, M_CAMXPT);
1835 			free(new_path, M_CAMXPT);
1836 			return;
1837 		}
1838 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
1839 		request_ccb->ccb_h.cbfcnp = xptscandone;
1840 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1841 		request_ccb->crcn.flags = flags;
1842 	}
1843 
1844 	if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
1845 		probe_softc *softc;
1846 
1847 		softc = (probe_softc *)old_periph->softc;
1848 		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
1849 				  periph_links.tqe);
1850 	} else {
1851 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1852 					  probestart, "probe",
1853 					  CAM_PERIPH_BIO,
1854 					  request_ccb->ccb_h.path, NULL, 0,
1855 					  request_ccb);
1856 
1857 		if (status != CAM_REQ_CMP) {
1858 			xpt_print(path, "scsi_scan_lun: cam_alloc_periph "
1859 			    "returned an error, can't continue probe\n");
1860 			request_ccb->ccb_h.status = status;
1861 			xpt_done(request_ccb);
1862 		}
1863 	}
1864 }
1865 
1866 static void
1867 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
1868 {
1869 	xpt_release_path(done_ccb->ccb_h.path);
1870 	free(done_ccb->ccb_h.path, M_CAMXPT);
1871 	free(done_ccb, M_CAMXPT);
1872 }
1873 
1874 static struct cam_ed *
1875 scsi_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
1876 {
1877 	struct cam_path path;
1878 	struct scsi_quirk_entry *quirk;
1879 	struct cam_ed *device;
1880 	struct cam_ed *cur_device;
1881 
1882 	device = xpt_alloc_device(bus, target, lun_id);
1883 	if (device == NULL)
1884 		return (NULL);
1885 
1886 	/*
1887 	 * Take the default quirk entry until we have inquiry
1888 	 * data and can determine a better quirk to use.
1889 	 */
1890 	quirk = &scsi_quirk_table[scsi_quirk_table_size - 1];
1891 	device->quirk = (void *)quirk;
1892 	device->mintags = quirk->mintags;
1893 	device->maxtags = quirk->maxtags;
1894 	bzero(&device->inq_data, sizeof(device->inq_data));
1895 	device->inq_flags = 0;
1896 	device->queue_flags = 0;
1897 	device->serial_num = NULL;
1898 	device->serial_num_len = 0;
1899 
1900 	/*
1901 	 * XXX should be limited by number of CCBs this bus can
1902 	 * do.
1903 	 */
1904 	bus->sim->max_ccbs += device->ccbq.devq_openings;
1905 	/* Insertion sort into our target's device list */
1906 	cur_device = TAILQ_FIRST(&target->ed_entries);
1907 	while (cur_device != NULL && cur_device->lun_id < lun_id)
1908 		cur_device = TAILQ_NEXT(cur_device, links);
1909 	if (cur_device != NULL) {
1910 		TAILQ_INSERT_BEFORE(cur_device, device, links);
1911 	} else {
1912 		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
1913 	}
1914 	target->generation++;
1915 	if (lun_id != CAM_LUN_WILDCARD) {
1916 		xpt_compile_path(&path,
1917 				 NULL,
1918 				 bus->path_id,
1919 				 target->target_id,
1920 				 lun_id);
1921 		scsi_devise_transport(&path);
1922 		xpt_release_path(&path);
1923 	}
1924 
1925 	return (device);
1926 }
1927 
1928 static void
1929 scsi_devise_transport(struct cam_path *path)
1930 {
1931 	struct ccb_pathinq cpi;
1932 	struct ccb_trans_settings cts;
1933 	struct scsi_inquiry_data *inq_buf;
1934 
1935 	/* Get transport information from the SIM */
1936 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1937 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1938 	xpt_action((union ccb *)&cpi);
1939 
1940 	inq_buf = NULL;
1941 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1942 		inq_buf = &path->device->inq_data;
1943 	path->device->protocol = PROTO_SCSI;
1944 	path->device->protocol_version =
1945 	    inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1946 	path->device->transport = cpi.transport;
1947 	path->device->transport_version = cpi.transport_version;
1948 
1949 	/*
1950 	 * Any device not using SPI3 features should
1951 	 * be considered SPI2 or lower.
1952 	 */
1953 	if (inq_buf != NULL) {
1954 		if (path->device->transport == XPORT_SPI
1955 		 && (inq_buf->spi3data & SID_SPI_MASK) == 0
1956 		 && path->device->transport_version > 2)
1957 			path->device->transport_version = 2;
1958 	} else {
1959 		struct cam_ed* otherdev;
1960 
1961 		for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
1962 		     otherdev != NULL;
1963 		     otherdev = TAILQ_NEXT(otherdev, links)) {
1964 			if (otherdev != path->device)
1965 				break;
1966 		}
1967 
1968 		if (otherdev != NULL) {
1969 			/*
1970 			 * Initially assume the same versioning as
1971 			 * prior luns for this target.
1972 			 */
1973 			path->device->protocol_version =
1974 			    otherdev->protocol_version;
1975 			path->device->transport_version =
1976 			    otherdev->transport_version;
1977 		} else {
1978 			/* Until we know better, opt for safty */
1979 			path->device->protocol_version = 2;
1980 			if (path->device->transport == XPORT_SPI)
1981 				path->device->transport_version = 2;
1982 			else
1983 				path->device->transport_version = 0;
1984 		}
1985 	}
1986 
1987 	/*
1988 	 * XXX
1989 	 * For a device compliant with SPC-2 we should be able
1990 	 * to determine the transport version supported by
1991 	 * scrutinizing the version descriptors in the
1992 	 * inquiry buffer.
1993 	 */
1994 
1995 	/* Tell the controller what we think */
1996 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1997 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1998 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1999 	cts.transport = path->device->transport;
2000 	cts.transport_version = path->device->transport_version;
2001 	cts.protocol = path->device->protocol;
2002 	cts.protocol_version = path->device->protocol_version;
2003 	cts.proto_specific.valid = 0;
2004 	cts.xport_specific.valid = 0;
2005 	xpt_action((union ccb *)&cts);
2006 }
2007 
2008 static void
2009 scsi_action(union ccb *start_ccb)
2010 {
2011 
2012 	switch (start_ccb->ccb_h.func_code) {
2013 	case XPT_SET_TRAN_SETTINGS:
2014 	{
2015 		scsi_set_transfer_settings(&start_ccb->cts,
2016 					   start_ccb->ccb_h.path->device,
2017 					   /*async_update*/FALSE);
2018 		break;
2019 	}
2020 	case XPT_SCAN_BUS:
2021 		scsi_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
2022 		break;
2023 	case XPT_SCAN_LUN:
2024 		scsi_scan_lun(start_ccb->ccb_h.path->periph,
2025 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
2026 			      start_ccb);
2027 		break;
2028 	case XPT_GET_TRAN_SETTINGS:
2029 	{
2030 		struct cam_sim *sim;
2031 
2032 		sim = start_ccb->ccb_h.path->bus->sim;
2033 		(*(sim->sim_action))(sim, start_ccb);
2034 		break;
2035 	}
2036 	default:
2037 		xpt_action_default(start_ccb);
2038 		break;
2039 	}
2040 }
2041 
2042 static void
2043 scsi_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
2044 			   int async_update)
2045 {
2046 	struct	ccb_pathinq cpi;
2047 	struct	ccb_trans_settings cur_cts;
2048 	struct	ccb_trans_settings_scsi *scsi;
2049 	struct	ccb_trans_settings_scsi *cur_scsi;
2050 	struct	cam_sim *sim;
2051 	struct	scsi_inquiry_data *inq_data;
2052 
2053 	if (device == NULL) {
2054 		cts->ccb_h.status = CAM_PATH_INVALID;
2055 		xpt_done((union ccb *)cts);
2056 		return;
2057 	}
2058 
2059 	if (cts->protocol == PROTO_UNKNOWN
2060 	 || cts->protocol == PROTO_UNSPECIFIED) {
2061 		cts->protocol = device->protocol;
2062 		cts->protocol_version = device->protocol_version;
2063 	}
2064 
2065 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
2066 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
2067 		cts->protocol_version = device->protocol_version;
2068 
2069 	if (cts->protocol != device->protocol) {
2070 		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
2071 		       cts->protocol, device->protocol);
2072 		cts->protocol = device->protocol;
2073 	}
2074 
2075 	if (cts->protocol_version > device->protocol_version) {
2076 		if (bootverbose) {
2077 			xpt_print(cts->ccb_h.path, "Down reving Protocol "
2078 			    "Version from %d to %d?\n", cts->protocol_version,
2079 			    device->protocol_version);
2080 		}
2081 		cts->protocol_version = device->protocol_version;
2082 	}
2083 
2084 	if (cts->transport == XPORT_UNKNOWN
2085 	 || cts->transport == XPORT_UNSPECIFIED) {
2086 		cts->transport = device->transport;
2087 		cts->transport_version = device->transport_version;
2088 	}
2089 
2090 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
2091 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
2092 		cts->transport_version = device->transport_version;
2093 
2094 	if (cts->transport != device->transport) {
2095 		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
2096 		    cts->transport, device->transport);
2097 		cts->transport = device->transport;
2098 	}
2099 
2100 	if (cts->transport_version > device->transport_version) {
2101 		if (bootverbose) {
2102 			xpt_print(cts->ccb_h.path, "Down reving Transport "
2103 			    "Version from %d to %d?\n", cts->transport_version,
2104 			    device->transport_version);
2105 		}
2106 		cts->transport_version = device->transport_version;
2107 	}
2108 
2109 	sim = cts->ccb_h.path->bus->sim;
2110 
2111 	/*
2112 	 * Nothing more of interest to do unless
2113 	 * this is a device connected via the
2114 	 * SCSI protocol.
2115 	 */
2116 	if (cts->protocol != PROTO_SCSI) {
2117 		if (async_update == FALSE)
2118 			(*(sim->sim_action))(sim, (union ccb *)cts);
2119 		return;
2120 	}
2121 
2122 	inq_data = &device->inq_data;
2123 	scsi = &cts->proto_specific.scsi;
2124 	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
2125 	cpi.ccb_h.func_code = XPT_PATH_INQ;
2126 	xpt_action((union ccb *)&cpi);
2127 
2128 	/* SCSI specific sanity checking */
2129 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
2130 	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
2131 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
2132 	 || (device->mintags == 0)) {
2133 		/*
2134 		 * Can't tag on hardware that doesn't support tags,
2135 		 * doesn't have it enabled, or has broken tag support.
2136 		 */
2137 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2138 	}
2139 
2140 	if (async_update == FALSE) {
2141 		/*
2142 		 * Perform sanity checking against what the
2143 		 * controller and device can do.
2144 		 */
2145 		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
2146 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2147 		cur_cts.type = cts->type;
2148 		xpt_action((union ccb *)&cur_cts);
2149 		if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2150 			return;
2151 		}
2152 		cur_scsi = &cur_cts.proto_specific.scsi;
2153 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
2154 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2155 			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
2156 		}
2157 		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
2158 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2159 	}
2160 
2161 	/* SPI specific sanity checking */
2162 	if (cts->transport == XPORT_SPI && async_update == FALSE) {
2163 		u_int spi3caps;
2164 		struct ccb_trans_settings_spi *spi;
2165 		struct ccb_trans_settings_spi *cur_spi;
2166 
2167 		spi = &cts->xport_specific.spi;
2168 
2169 		cur_spi = &cur_cts.xport_specific.spi;
2170 
2171 		/* Fill in any gaps in what the user gave us */
2172 		if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2173 			spi->sync_period = cur_spi->sync_period;
2174 		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2175 			spi->sync_period = 0;
2176 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2177 			spi->sync_offset = cur_spi->sync_offset;
2178 		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2179 			spi->sync_offset = 0;
2180 		if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2181 			spi->ppr_options = cur_spi->ppr_options;
2182 		if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2183 			spi->ppr_options = 0;
2184 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2185 			spi->bus_width = cur_spi->bus_width;
2186 		if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2187 			spi->bus_width = 0;
2188 		if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
2189 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2190 			spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
2191 		}
2192 		if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
2193 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2194 		if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2195 		  && (inq_data->flags & SID_Sync) == 0
2196 		  && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2197 		 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)) {
2198 			/* Force async */
2199 			spi->sync_period = 0;
2200 			spi->sync_offset = 0;
2201 		}
2202 
2203 		switch (spi->bus_width) {
2204 		case MSG_EXT_WDTR_BUS_32_BIT:
2205 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2206 			  || (inq_data->flags & SID_WBus32) != 0
2207 			  || cts->type == CTS_TYPE_USER_SETTINGS)
2208 			 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
2209 				break;
2210 			/* Fall Through to 16-bit */
2211 		case MSG_EXT_WDTR_BUS_16_BIT:
2212 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2213 			  || (inq_data->flags & SID_WBus16) != 0
2214 			  || cts->type == CTS_TYPE_USER_SETTINGS)
2215 			 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
2216 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2217 				break;
2218 			}
2219 			/* Fall Through to 8-bit */
2220 		default: /* New bus width?? */
2221 		case MSG_EXT_WDTR_BUS_8_BIT:
2222 			/* All targets can do this */
2223 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2224 			break;
2225 		}
2226 
2227 		spi3caps = cpi.xport_specific.spi.ppr_options;
2228 		if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2229 		 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2230 			spi3caps &= inq_data->spi3data;
2231 
2232 		if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
2233 			spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2234 
2235 		if ((spi3caps & SID_SPI_IUS) == 0)
2236 			spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
2237 
2238 		if ((spi3caps & SID_SPI_QAS) == 0)
2239 			spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
2240 
2241 		/* No SPI Transfer settings are allowed unless we are wide */
2242 		if (spi->bus_width == 0)
2243 			spi->ppr_options = 0;
2244 
2245 		if ((spi->valid & CTS_SPI_VALID_DISC)
2246 		 && ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0)) {
2247 			/*
2248 			 * Can't tag queue without disconnection.
2249 			 */
2250 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2251 			scsi->valid |= CTS_SCSI_VALID_TQ;
2252 		}
2253 
2254 		/*
2255 		 * If we are currently performing tagged transactions to
2256 		 * this device and want to change its negotiation parameters,
2257 		 * go non-tagged for a bit to give the controller a chance to
2258 		 * negotiate unhampered by tag messages.
2259 		 */
2260 		if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2261 		 && (device->inq_flags & SID_CmdQue) != 0
2262 		 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2263 		 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
2264 				   CTS_SPI_VALID_SYNC_OFFSET|
2265 				   CTS_SPI_VALID_BUS_WIDTH)) != 0)
2266 			scsi_toggle_tags(cts->ccb_h.path);
2267 	}
2268 
2269 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2270 	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
2271 		int device_tagenb;
2272 
2273 		/*
2274 		 * If we are transitioning from tags to no-tags or
2275 		 * vice-versa, we need to carefully freeze and restart
2276 		 * the queue so that we don't overlap tagged and non-tagged
2277 		 * commands.  We also temporarily stop tags if there is
2278 		 * a change in transfer negotiation settings to allow
2279 		 * "tag-less" negotiation.
2280 		 */
2281 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2282 		 || (device->inq_flags & SID_CmdQue) != 0)
2283 			device_tagenb = TRUE;
2284 		else
2285 			device_tagenb = FALSE;
2286 
2287 		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2288 		  && device_tagenb == FALSE)
2289 		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
2290 		  && device_tagenb == TRUE)) {
2291 
2292 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
2293 				/*
2294 				 * Delay change to use tags until after a
2295 				 * few commands have gone to this device so
2296 				 * the controller has time to perform transfer
2297 				 * negotiations without tagged messages getting
2298 				 * in the way.
2299 				 */
2300 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
2301 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
2302 			} else {
2303 				xpt_stop_tags(cts->ccb_h.path);
2304 			}
2305 		}
2306 	}
2307 	if (async_update == FALSE)
2308 		(*(sim->sim_action))(sim, (union ccb *)cts);
2309 }
2310 
2311 static void
2312 scsi_toggle_tags(struct cam_path *path)
2313 {
2314 	struct cam_ed *dev;
2315 
2316 	/*
2317 	 * Give controllers a chance to renegotiate
2318 	 * before starting tag operations.  We
2319 	 * "toggle" tagged queuing off then on
2320 	 * which causes the tag enable command delay
2321 	 * counter to come into effect.
2322 	 */
2323 	dev = path->device;
2324 	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2325 	 || ((dev->inq_flags & SID_CmdQue) != 0
2326  	  && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
2327 		struct ccb_trans_settings cts;
2328 
2329 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2330 		cts.protocol = PROTO_SCSI;
2331 		cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
2332 		cts.transport = XPORT_UNSPECIFIED;
2333 		cts.transport_version = XPORT_VERSION_UNSPECIFIED;
2334 		cts.proto_specific.scsi.flags = 0;
2335 		cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
2336 		scsi_set_transfer_settings(&cts, path->device,
2337 					  /*async_update*/TRUE);
2338 		cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
2339 		scsi_set_transfer_settings(&cts, path->device,
2340 					  /*async_update*/TRUE);
2341 	}
2342 }
2343 
2344 /*
2345  * Handle any per-device event notifications that require action by the XPT.
2346  */
2347 static void
2348 scsi_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
2349 	      struct cam_ed *device, void *async_arg)
2350 {
2351 	cam_status status;
2352 	struct cam_path newpath;
2353 
2354 	/*
2355 	 * We only need to handle events for real devices.
2356 	 */
2357 	if (target->target_id == CAM_TARGET_WILDCARD
2358 	 || device->lun_id == CAM_LUN_WILDCARD)
2359 		return;
2360 
2361 	/*
2362 	 * We need our own path with wildcards expanded to
2363 	 * handle certain types of events.
2364 	 */
2365 	if ((async_code == AC_SENT_BDR)
2366 	 || (async_code == AC_BUS_RESET)
2367 	 || (async_code == AC_INQ_CHANGED))
2368 		status = xpt_compile_path(&newpath, NULL,
2369 					  bus->path_id,
2370 					  target->target_id,
2371 					  device->lun_id);
2372 	else
2373 		status = CAM_REQ_CMP_ERR;
2374 
2375 	if (status == CAM_REQ_CMP) {
2376 
2377 		/*
2378 		 * Allow transfer negotiation to occur in a
2379 		 * tag free environment and after settle delay.
2380 		 */
2381 		if (async_code == AC_SENT_BDR
2382 		 || async_code == AC_BUS_RESET) {
2383 			cam_freeze_devq(&newpath);
2384 			cam_release_devq(&newpath,
2385 				RELSIM_RELEASE_AFTER_TIMEOUT,
2386 				/*reduction*/0,
2387 				/*timeout*/scsi_delay,
2388 				/*getcount_only*/0);
2389 			scsi_toggle_tags(&newpath);
2390 		}
2391 
2392 		if (async_code == AC_INQ_CHANGED) {
2393 			/*
2394 			 * We've sent a start unit command, or
2395 			 * something similar to a device that
2396 			 * may have caused its inquiry data to
2397 			 * change. So we re-scan the device to
2398 			 * refresh the inquiry data for it.
2399 			 */
2400 			scsi_scan_lun(newpath.periph, &newpath,
2401 				     CAM_EXPECT_INQ_CHANGE, NULL);
2402 		}
2403 		xpt_release_path(&newpath);
2404 	} else if (async_code == AC_LOST_DEVICE &&
2405 	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
2406 		device->flags |= CAM_DEV_UNCONFIGURED;
2407 		xpt_release_device(device);
2408 	} else if (async_code == AC_TRANSFER_NEG) {
2409 		struct ccb_trans_settings *settings;
2410 
2411 		settings = (struct ccb_trans_settings *)async_arg;
2412 		scsi_set_transfer_settings(settings, device,
2413 					  /*async_update*/TRUE);
2414 	}
2415 }
2416 
2417