xref: /freebsd/sys/cam/scsi/scsi_xpt.c (revision d8b878873e7aa8df1972cc6a642804b17eb61087)
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 static void	 scsi_announce_periph(struct cam_periph *periph);
556 
557 static struct xpt_xport scsi_xport = {
558 	.alloc_device = scsi_alloc_device,
559 	.action = scsi_action,
560 	.async = scsi_dev_async,
561 	.announce = scsi_announce_periph,
562 };
563 
564 struct xpt_xport *
565 scsi_get_xport(void)
566 {
567 	return (&scsi_xport);
568 }
569 
570 static void
571 probe_periph_init()
572 {
573 }
574 
575 static cam_status
576 proberegister(struct cam_periph *periph, void *arg)
577 {
578 	union ccb *request_ccb;	/* CCB representing the probe request */
579 	cam_status status;
580 	probe_softc *softc;
581 
582 	request_ccb = (union ccb *)arg;
583 	if (periph == NULL) {
584 		printf("proberegister: periph was NULL!!\n");
585 		return(CAM_REQ_CMP_ERR);
586 	}
587 
588 	if (request_ccb == NULL) {
589 		printf("proberegister: no probe CCB, "
590 		       "can't register device\n");
591 		return(CAM_REQ_CMP_ERR);
592 	}
593 
594 	softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
595 
596 	if (softc == NULL) {
597 		printf("proberegister: Unable to probe new device. "
598 		       "Unable to allocate softc\n");
599 		return(CAM_REQ_CMP_ERR);
600 	}
601 	TAILQ_INIT(&softc->request_ccbs);
602 	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
603 			  periph_links.tqe);
604 	softc->flags = 0;
605 	periph->softc = softc;
606 	softc->periph = periph;
607 	softc->action = PROBE_INVALID;
608 	status = cam_periph_acquire(periph);
609 	if (status != CAM_REQ_CMP) {
610 		return (status);
611 	}
612 
613 
614 	/*
615 	 * Ensure we've waited at least a bus settle
616 	 * delay before attempting to probe the device.
617 	 * For HBAs that don't do bus resets, this won't make a difference.
618 	 */
619 	cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
620 				      scsi_delay);
621 	/*
622 	 * Ensure nobody slip in until probe finish.
623 	 */
624 	cam_freeze_devq_arg(periph->path,
625 	    RELSIM_RELEASE_RUNLEVEL, CAM_RL_XPT + 1);
626 	probeschedule(periph);
627 	return(CAM_REQ_CMP);
628 }
629 
630 static void
631 probeschedule(struct cam_periph *periph)
632 {
633 	struct ccb_pathinq cpi;
634 	union ccb *ccb;
635 	probe_softc *softc;
636 
637 	softc = (probe_softc *)periph->softc;
638 	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
639 
640 	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
641 	cpi.ccb_h.func_code = XPT_PATH_INQ;
642 	xpt_action((union ccb *)&cpi);
643 
644 	/*
645 	 * If a device has gone away and another device, or the same one,
646 	 * is back in the same place, it should have a unit attention
647 	 * condition pending.  It will not report the unit attention in
648 	 * response to an inquiry, which may leave invalid transfer
649 	 * negotiations in effect.  The TUR will reveal the unit attention
650 	 * condition.  Only send the TUR for lun 0, since some devices
651 	 * will get confused by commands other than inquiry to non-existent
652 	 * luns.  If you think a device has gone away start your scan from
653 	 * lun 0.  This will insure that any bogus transfer settings are
654 	 * invalidated.
655 	 *
656 	 * If we haven't seen the device before and the controller supports
657 	 * some kind of transfer negotiation, negotiate with the first
658 	 * sent command if no bus reset was performed at startup.  This
659 	 * ensures that the device is not confused by transfer negotiation
660 	 * settings left over by loader or BIOS action.
661 	 */
662 	if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
663 	 && (ccb->ccb_h.target_lun == 0)) {
664 		PROBE_SET_ACTION(softc, PROBE_TUR);
665 	} else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
666 	      && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
667 		proberequestdefaultnegotiation(periph);
668 		PROBE_SET_ACTION(softc, PROBE_INQUIRY);
669 	} else {
670 		PROBE_SET_ACTION(softc, PROBE_INQUIRY);
671 	}
672 
673 	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
674 		softc->flags |= PROBE_NO_ANNOUNCE;
675 	else
676 		softc->flags &= ~PROBE_NO_ANNOUNCE;
677 
678 	xpt_schedule(periph, CAM_PRIORITY_XPT);
679 }
680 
681 static void
682 probestart(struct cam_periph *periph, union ccb *start_ccb)
683 {
684 	/* Probe the device that our peripheral driver points to */
685 	struct ccb_scsiio *csio;
686 	probe_softc *softc;
687 
688 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
689 
690 	softc = (probe_softc *)periph->softc;
691 	csio = &start_ccb->csio;
692 
693 	switch (softc->action) {
694 	case PROBE_TUR:
695 	case PROBE_TUR_FOR_NEGOTIATION:
696 	case PROBE_DV_EXIT:
697 	{
698 		scsi_test_unit_ready(csio,
699 				     /*retries*/10,
700 				     probedone,
701 				     MSG_SIMPLE_Q_TAG,
702 				     SSD_FULL_SIZE,
703 				     /*timeout*/60000);
704 		break;
705 	}
706 	case PROBE_INQUIRY:
707 	case PROBE_FULL_INQUIRY:
708 	case PROBE_INQUIRY_BASIC_DV1:
709 	case PROBE_INQUIRY_BASIC_DV2:
710 	{
711 		u_int inquiry_len;
712 		struct scsi_inquiry_data *inq_buf;
713 
714 		inq_buf = &periph->path->device->inq_data;
715 
716 		/*
717 		 * If the device is currently configured, we calculate an
718 		 * MD5 checksum of the inquiry data, and if the serial number
719 		 * length is greater than 0, add the serial number data
720 		 * into the checksum as well.  Once the inquiry and the
721 		 * serial number check finish, we attempt to figure out
722 		 * whether we still have the same device.
723 		 */
724 		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
725 
726 			MD5Init(&softc->context);
727 			MD5Update(&softc->context, (unsigned char *)inq_buf,
728 				  sizeof(struct scsi_inquiry_data));
729 			softc->flags |= PROBE_INQUIRY_CKSUM;
730 			if (periph->path->device->serial_num_len > 0) {
731 				MD5Update(&softc->context,
732 					  periph->path->device->serial_num,
733 					  periph->path->device->serial_num_len);
734 				softc->flags |= PROBE_SERIAL_CKSUM;
735 			}
736 			MD5Final(softc->digest, &softc->context);
737 		}
738 
739 		if (softc->action == PROBE_INQUIRY)
740 			inquiry_len = SHORT_INQUIRY_LENGTH;
741 		else
742 			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
743 
744 		/*
745 		 * Some parallel SCSI devices fail to send an
746 		 * ignore wide residue message when dealing with
747 		 * odd length inquiry requests.  Round up to be
748 		 * safe.
749 		 */
750 		inquiry_len = roundup2(inquiry_len, 2);
751 
752 		if (softc->action == PROBE_INQUIRY_BASIC_DV1
753 		 || softc->action == PROBE_INQUIRY_BASIC_DV2) {
754 			inq_buf = malloc(inquiry_len, M_CAMXPT, M_NOWAIT);
755 		}
756 		if (inq_buf == NULL) {
757 			xpt_print(periph->path, "malloc failure- skipping Basic"
758 			    "Domain Validation\n");
759 			PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
760 			scsi_test_unit_ready(csio,
761 					     /*retries*/4,
762 					     probedone,
763 					     MSG_SIMPLE_Q_TAG,
764 					     SSD_FULL_SIZE,
765 					     /*timeout*/60000);
766 			break;
767 		}
768 		scsi_inquiry(csio,
769 			     /*retries*/4,
770 			     probedone,
771 			     MSG_SIMPLE_Q_TAG,
772 			     (u_int8_t *)inq_buf,
773 			     inquiry_len,
774 			     /*evpd*/FALSE,
775 			     /*page_code*/0,
776 			     SSD_MIN_SIZE,
777 			     /*timeout*/60 * 1000);
778 		break;
779 	}
780 	case PROBE_MODE_SENSE:
781 	{
782 		void  *mode_buf;
783 		int    mode_buf_len;
784 
785 		mode_buf_len = sizeof(struct scsi_mode_header_6)
786 			     + sizeof(struct scsi_mode_blk_desc)
787 			     + sizeof(struct scsi_control_page);
788 		mode_buf = malloc(mode_buf_len, M_CAMXPT, M_NOWAIT);
789 		if (mode_buf != NULL) {
790 	                scsi_mode_sense(csio,
791 					/*retries*/4,
792 					probedone,
793 					MSG_SIMPLE_Q_TAG,
794 					/*dbd*/FALSE,
795 					SMS_PAGE_CTRL_CURRENT,
796 					SMS_CONTROL_MODE_PAGE,
797 					mode_buf,
798 					mode_buf_len,
799 					SSD_FULL_SIZE,
800 					/*timeout*/60000);
801 			break;
802 		}
803 		xpt_print(periph->path, "Unable to mode sense control page - "
804 		    "malloc failure\n");
805 		PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM_0);
806 	}
807 	/* FALLTHROUGH */
808 	case PROBE_SERIAL_NUM_0:
809 	{
810 		struct scsi_vpd_supported_page_list *vpd_list = NULL;
811 		struct cam_ed *device;
812 
813 		device = periph->path->device;
814 		if ((SCSI_QUIRK(device)->quirks & CAM_QUIRK_NOSERIAL) == 0) {
815 			vpd_list = malloc(sizeof(*vpd_list), M_CAMXPT,
816 			    M_NOWAIT | M_ZERO);
817 		}
818 
819 		if (vpd_list != NULL) {
820 			scsi_inquiry(csio,
821 				     /*retries*/4,
822 				     probedone,
823 				     MSG_SIMPLE_Q_TAG,
824 				     (u_int8_t *)vpd_list,
825 				     sizeof(*vpd_list),
826 				     /*evpd*/TRUE,
827 				     SVPD_SUPPORTED_PAGE_LIST,
828 				     SSD_MIN_SIZE,
829 				     /*timeout*/60 * 1000);
830 			break;
831 		}
832 		/*
833 		 * We'll have to do without, let our probedone
834 		 * routine finish up for us.
835 		 */
836 		start_ccb->csio.data_ptr = NULL;
837 		probedone(periph, start_ccb);
838 		return;
839 	}
840 	case PROBE_SERIAL_NUM_1:
841 	{
842 		struct scsi_vpd_unit_serial_number *serial_buf;
843 		struct cam_ed* device;
844 
845 		serial_buf = NULL;
846 		device = periph->path->device;
847 		if (device->serial_num != NULL) {
848 			free(device->serial_num, M_CAMXPT);
849 			device->serial_num = NULL;
850 			device->serial_num_len = 0;
851 		}
852 
853 		serial_buf = (struct scsi_vpd_unit_serial_number *)
854 			malloc(sizeof(*serial_buf), M_CAMXPT, M_NOWAIT|M_ZERO);
855 
856 		if (serial_buf != NULL) {
857 			scsi_inquiry(csio,
858 				     /*retries*/4,
859 				     probedone,
860 				     MSG_SIMPLE_Q_TAG,
861 				     (u_int8_t *)serial_buf,
862 				     sizeof(*serial_buf),
863 				     /*evpd*/TRUE,
864 				     SVPD_UNIT_SERIAL_NUMBER,
865 				     SSD_MIN_SIZE,
866 				     /*timeout*/60 * 1000);
867 			break;
868 		}
869 		/*
870 		 * We'll have to do without, let our probedone
871 		 * routine finish up for us.
872 		 */
873 		start_ccb->csio.data_ptr = NULL;
874 		probedone(periph, start_ccb);
875 		return;
876 	}
877 	case PROBE_INVALID:
878 		CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
879 		    ("probestart: invalid action state\n"));
880 	default:
881 		break;
882 	}
883 	xpt_action(start_ccb);
884 }
885 
886 static void
887 proberequestdefaultnegotiation(struct cam_periph *periph)
888 {
889 	struct ccb_trans_settings cts;
890 
891 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
892 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
893 	cts.type = CTS_TYPE_USER_SETTINGS;
894 	xpt_action((union ccb *)&cts);
895 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
896 		return;
897 	}
898 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
899 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
900 	xpt_action((union ccb *)&cts);
901 }
902 
903 /*
904  * Backoff Negotiation Code- only pertinent for SPI devices.
905  */
906 static int
907 proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
908 {
909 	struct ccb_trans_settings cts;
910 	struct ccb_trans_settings_spi *spi;
911 
912 	memset(&cts, 0, sizeof (cts));
913 	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
914 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
915 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
916 	xpt_action((union ccb *)&cts);
917 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
918 		if (bootverbose) {
919 			xpt_print(periph->path,
920 			    "failed to get current device settings\n");
921 		}
922 		return (0);
923 	}
924 	if (cts.transport != XPORT_SPI) {
925 		if (bootverbose) {
926 			xpt_print(periph->path, "not SPI transport\n");
927 		}
928 		return (0);
929 	}
930 	spi = &cts.xport_specific.spi;
931 
932 	/*
933 	 * We cannot renegotiate sync rate if we don't have one.
934 	 */
935 	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
936 		if (bootverbose) {
937 			xpt_print(periph->path, "no sync rate known\n");
938 		}
939 		return (0);
940 	}
941 
942 	/*
943 	 * We'll assert that we don't have to touch PPR options- the
944 	 * SIM will see what we do with period and offset and adjust
945 	 * the PPR options as appropriate.
946 	 */
947 
948 	/*
949 	 * A sync rate with unknown or zero offset is nonsensical.
950 	 * A sync period of zero means Async.
951 	 */
952 	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
953 	 || spi->sync_offset == 0 || spi->sync_period == 0) {
954 		if (bootverbose) {
955 			xpt_print(periph->path, "no sync rate available\n");
956 		}
957 		return (0);
958 	}
959 
960 	if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
961 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
962 		    ("hit async: giving up on DV\n"));
963 		return (0);
964 	}
965 
966 
967 	/*
968 	 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
969 	 * We don't try to remember 'last' settings to see if the SIM actually
970 	 * gets into the speed we want to set. We check on the SIM telling
971 	 * us that a requested speed is bad, but otherwise don't try and
972 	 * check the speed due to the asynchronous and handshake nature
973 	 * of speed setting.
974 	 */
975 	spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
976 	for (;;) {
977 		spi->sync_period++;
978 		if (spi->sync_period >= 0xf) {
979 			spi->sync_period = 0;
980 			spi->sync_offset = 0;
981 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
982 			    ("setting to async for DV\n"));
983 			/*
984 			 * Once we hit async, we don't want to try
985 			 * any more settings.
986 			 */
987 			device->flags |= CAM_DEV_DV_HIT_BOTTOM;
988 		} else if (bootverbose) {
989 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
990 			    ("DV: period 0x%x\n", spi->sync_period));
991 			printf("setting period to 0x%x\n", spi->sync_period);
992 		}
993 		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
994 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
995 		xpt_action((union ccb *)&cts);
996 		if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
997 			break;
998 		}
999 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1000 		    ("DV: failed to set period 0x%x\n", spi->sync_period));
1001 		if (spi->sync_period == 0) {
1002 			return (0);
1003 		}
1004 	}
1005 	return (1);
1006 }
1007 
1008 static void
1009 probedone(struct cam_periph *periph, union ccb *done_ccb)
1010 {
1011 	probe_softc *softc;
1012 	struct cam_path *path;
1013 	u_int32_t  priority;
1014 
1015 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
1016 
1017 	softc = (probe_softc *)periph->softc;
1018 	path = done_ccb->ccb_h.path;
1019 	priority = done_ccb->ccb_h.pinfo.priority;
1020 
1021 	switch (softc->action) {
1022 	case PROBE_TUR:
1023 	{
1024 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1025 
1026 			if (cam_periph_error(done_ccb, 0,
1027 					     SF_NO_PRINT, NULL) == ERESTART)
1028 				return;
1029 			else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1030 				/* Don't wedge the queue */
1031 				xpt_release_devq(done_ccb->ccb_h.path,
1032 						 /*count*/1,
1033 						 /*run_queue*/TRUE);
1034 		}
1035 		PROBE_SET_ACTION(softc, PROBE_INQUIRY);
1036 		xpt_release_ccb(done_ccb);
1037 		xpt_schedule(periph, priority);
1038 		return;
1039 	}
1040 	case PROBE_INQUIRY:
1041 	case PROBE_FULL_INQUIRY:
1042 	{
1043 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1044 			struct scsi_inquiry_data *inq_buf;
1045 			u_int8_t periph_qual;
1046 
1047 			path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1048 			inq_buf = &path->device->inq_data;
1049 
1050 			periph_qual = SID_QUAL(inq_buf);
1051 
1052 			switch(periph_qual) {
1053 			case SID_QUAL_LU_CONNECTED:
1054 			{
1055 				u_int8_t len;
1056 
1057 				/*
1058 				 * We conservatively request only
1059 				 * SHORT_INQUIRY_LEN bytes of inquiry
1060 				 * information during our first try
1061 				 * at sending an INQUIRY. If the device
1062 				 * has more information to give,
1063 				 * perform a second request specifying
1064 				 * the amount of information the device
1065 				 * is willing to give.
1066 				 */
1067 				len = inq_buf->additional_length
1068 				    + offsetof(struct scsi_inquiry_data,
1069                                                additional_length) + 1;
1070 				if (softc->action == PROBE_INQUIRY
1071 				    && len > SHORT_INQUIRY_LENGTH) {
1072 					PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1073 					xpt_release_ccb(done_ccb);
1074 					xpt_schedule(periph, priority);
1075 					return;
1076 				}
1077 
1078 				scsi_find_quirk(path->device);
1079 
1080 				scsi_devise_transport(path);
1081 				if (INQ_DATA_TQ_ENABLED(inq_buf))
1082 					PROBE_SET_ACTION(softc, PROBE_MODE_SENSE);
1083 				else
1084 					PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM_0);
1085 
1086 				if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1087 					path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1088 					xpt_acquire_device(path->device);
1089 				}
1090 				xpt_release_ccb(done_ccb);
1091 				xpt_schedule(periph, priority);
1092 				return;
1093 			}
1094 			default:
1095 				break;
1096 			}
1097 		} else if (cam_periph_error(done_ccb, 0,
1098 					    done_ccb->ccb_h.target_lun > 0
1099 					    ? SF_RETRY_UA|SF_QUIET_IR
1100 					    : SF_RETRY_UA,
1101 					    &softc->saved_ccb) == ERESTART) {
1102 			return;
1103 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1104 			/* Don't wedge the queue */
1105 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1106 					 /*run_queue*/TRUE);
1107 		}
1108 		/*
1109 		 * If we get to this point, we got an error status back
1110 		 * from the inquiry and the error status doesn't require
1111 		 * automatically retrying the command.  Therefore, the
1112 		 * inquiry failed.  If we had inquiry information before
1113 		 * for this device, but this latest inquiry command failed,
1114 		 * the device has probably gone away.  If this device isn't
1115 		 * already marked unconfigured, notify the peripheral
1116 		 * drivers that this device is no more.
1117 		 */
1118 		if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
1119 			/* Send the async notification. */
1120 			xpt_async(AC_LOST_DEVICE, path, NULL);
1121 
1122 		xpt_release_ccb(done_ccb);
1123 		break;
1124 	}
1125 	case PROBE_MODE_SENSE:
1126 	{
1127 		struct ccb_scsiio *csio;
1128 		struct scsi_mode_header_6 *mode_hdr;
1129 
1130 		csio = &done_ccb->csio;
1131 		mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
1132 		if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1133 			struct scsi_control_page *page;
1134 			u_int8_t *offset;
1135 
1136 			offset = ((u_int8_t *)&mode_hdr[1])
1137 			    + mode_hdr->blk_desc_len;
1138 			page = (struct scsi_control_page *)offset;
1139 			path->device->queue_flags = page->queue_flags;
1140 		} else if (cam_periph_error(done_ccb, 0,
1141 					    SF_RETRY_UA|SF_NO_PRINT,
1142 					    &softc->saved_ccb) == ERESTART) {
1143 			return;
1144 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1145 			/* Don't wedge the queue */
1146 			xpt_release_devq(done_ccb->ccb_h.path,
1147 					 /*count*/1, /*run_queue*/TRUE);
1148 		}
1149 		xpt_release_ccb(done_ccb);
1150 		free(mode_hdr, M_CAMXPT);
1151 		PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM_0);
1152 		xpt_schedule(periph, priority);
1153 		return;
1154 	}
1155 	case PROBE_SERIAL_NUM_0:
1156 	{
1157 		struct ccb_scsiio *csio;
1158 		struct scsi_vpd_supported_page_list *page_list;
1159 		int length, serialnum_supported, i;
1160 
1161 		serialnum_supported = 0;
1162 		csio = &done_ccb->csio;
1163 		page_list =
1164 		    (struct scsi_vpd_supported_page_list *)csio->data_ptr;
1165 
1166 		if (page_list == NULL) {
1167 			/*
1168 			 * Don't process the command as it was never sent
1169 			 */
1170 		} else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
1171 		    && (page_list->length > 0)) {
1172 			length = min(page_list->length,
1173 			    SVPD_SUPPORTED_PAGES_SIZE);
1174 			for (i = 0; i < length; i++) {
1175 				if (page_list->list[i] ==
1176 				    SVPD_UNIT_SERIAL_NUMBER) {
1177 					serialnum_supported = 1;
1178 					break;
1179 				}
1180 			}
1181 		} else if (cam_periph_error(done_ccb, 0,
1182 					    SF_RETRY_UA|SF_NO_PRINT,
1183 					    &softc->saved_ccb) == ERESTART) {
1184 			return;
1185 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1186 			/* Don't wedge the queue */
1187 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1188 					 /*run_queue*/TRUE);
1189 		}
1190 
1191 		if (page_list != NULL)
1192 			free(page_list, M_CAMXPT);
1193 
1194 		if (serialnum_supported) {
1195 			xpt_release_ccb(done_ccb);
1196 			PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM_1);
1197 			xpt_schedule(periph, priority);
1198 			return;
1199 		}
1200 
1201 		csio->data_ptr = NULL;
1202 		/* FALLTHROUGH */
1203 	}
1204 
1205 	case PROBE_SERIAL_NUM_1:
1206 	{
1207 		struct ccb_scsiio *csio;
1208 		struct scsi_vpd_unit_serial_number *serial_buf;
1209 		u_int32_t  priority;
1210 		int changed;
1211 		int have_serialnum;
1212 
1213 		changed = 1;
1214 		have_serialnum = 0;
1215 		csio = &done_ccb->csio;
1216 		priority = done_ccb->ccb_h.pinfo.priority;
1217 		serial_buf =
1218 		    (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
1219 
1220 		/* Clean up from previous instance of this device */
1221 		if (path->device->serial_num != NULL) {
1222 			free(path->device->serial_num, M_CAMXPT);
1223 			path->device->serial_num = NULL;
1224 			path->device->serial_num_len = 0;
1225 		}
1226 
1227 		if (serial_buf == NULL) {
1228 			/*
1229 			 * Don't process the command as it was never sent
1230 			 */
1231 		} else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
1232 			&& (serial_buf->length > 0)) {
1233 
1234 			have_serialnum = 1;
1235 			path->device->serial_num =
1236 				(u_int8_t *)malloc((serial_buf->length + 1),
1237 						   M_CAMXPT, M_NOWAIT);
1238 			if (path->device->serial_num != NULL) {
1239 				bcopy(serial_buf->serial_num,
1240 				      path->device->serial_num,
1241 				      serial_buf->length);
1242 				path->device->serial_num_len =
1243 				    serial_buf->length;
1244 				path->device->serial_num[serial_buf->length]
1245 				    = '\0';
1246 			}
1247 		} else if (cam_periph_error(done_ccb, 0,
1248 					    SF_RETRY_UA|SF_NO_PRINT,
1249 					    &softc->saved_ccb) == ERESTART) {
1250 			return;
1251 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1252 			/* Don't wedge the queue */
1253 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1254 					 /*run_queue*/TRUE);
1255 		}
1256 
1257 		/*
1258 		 * Let's see if we have seen this device before.
1259 		 */
1260 		if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
1261 			MD5_CTX context;
1262 			u_int8_t digest[16];
1263 
1264 			MD5Init(&context);
1265 
1266 			MD5Update(&context,
1267 				  (unsigned char *)&path->device->inq_data,
1268 				  sizeof(struct scsi_inquiry_data));
1269 
1270 			if (have_serialnum)
1271 				MD5Update(&context, serial_buf->serial_num,
1272 					  serial_buf->length);
1273 
1274 			MD5Final(digest, &context);
1275 			if (bcmp(softc->digest, digest, 16) == 0)
1276 				changed = 0;
1277 
1278 			/*
1279 			 * XXX Do we need to do a TUR in order to ensure
1280 			 *     that the device really hasn't changed???
1281 			 */
1282 			if ((changed != 0)
1283 			 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
1284 				xpt_async(AC_LOST_DEVICE, path, NULL);
1285 		}
1286 		if (serial_buf != NULL)
1287 			free(serial_buf, M_CAMXPT);
1288 
1289 		if (changed != 0) {
1290 			/*
1291 			 * Now that we have all the necessary
1292 			 * information to safely perform transfer
1293 			 * negotiations... Controllers don't perform
1294 			 * any negotiation or tagged queuing until
1295 			 * after the first XPT_SET_TRAN_SETTINGS ccb is
1296 			 * received.  So, on a new device, just retrieve
1297 			 * the user settings, and set them as the current
1298 			 * settings to set the device up.
1299 			 */
1300 			proberequestdefaultnegotiation(periph);
1301 			xpt_release_ccb(done_ccb);
1302 
1303 			/*
1304 			 * Perform a TUR to allow the controller to
1305 			 * perform any necessary transfer negotiation.
1306 			 */
1307 			PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1308 			xpt_schedule(periph, priority);
1309 			return;
1310 		}
1311 		xpt_release_ccb(done_ccb);
1312 		break;
1313 	}
1314 	case PROBE_TUR_FOR_NEGOTIATION:
1315 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1316 			DELAY(500000);
1317 			if (cam_periph_error(done_ccb, 0, SF_RETRY_UA,
1318 			    NULL) == ERESTART)
1319 				return;
1320 		}
1321 	/* FALLTHROUGH */
1322 	case PROBE_DV_EXIT:
1323 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1324 			/* Don't wedge the queue */
1325 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1326 					 /*run_queue*/TRUE);
1327 		}
1328 		/*
1329 		 * Do Domain Validation for lun 0 on devices that claim
1330 		 * to support Synchronous Transfer modes.
1331 		 */
1332 	 	if (softc->action == PROBE_TUR_FOR_NEGOTIATION
1333 		 && done_ccb->ccb_h.target_lun == 0
1334 		 && (path->device->inq_data.flags & SID_Sync) != 0
1335                  && (path->device->flags & CAM_DEV_IN_DV) == 0) {
1336 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1337 			    ("Begin Domain Validation\n"));
1338 			path->device->flags |= CAM_DEV_IN_DV;
1339 			xpt_release_ccb(done_ccb);
1340 			PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV1);
1341 			xpt_schedule(periph, priority);
1342 			return;
1343 		}
1344 		if (softc->action == PROBE_DV_EXIT) {
1345 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1346 			    ("Leave Domain Validation\n"));
1347 		}
1348 		if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1349 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1350 			xpt_acquire_device(path->device);
1351 		}
1352 		path->device->flags &=
1353 		    ~(CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1354 		if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1355 			/* Inform the XPT that a new device has been found */
1356 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1357 			xpt_action(done_ccb);
1358 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1359 				  done_ccb);
1360 		}
1361 		xpt_release_ccb(done_ccb);
1362 		break;
1363 	case PROBE_INQUIRY_BASIC_DV1:
1364 	case PROBE_INQUIRY_BASIC_DV2:
1365 	{
1366 		struct scsi_inquiry_data *nbuf;
1367 		struct ccb_scsiio *csio;
1368 
1369 		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1370 			/* Don't wedge the queue */
1371 			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1372 					 /*run_queue*/TRUE);
1373 		}
1374 		csio = &done_ccb->csio;
1375 		nbuf = (struct scsi_inquiry_data *)csio->data_ptr;
1376 		if (bcmp(nbuf, &path->device->inq_data, SHORT_INQUIRY_LENGTH)) {
1377 			xpt_print(path,
1378 			    "inquiry data fails comparison at DV%d step\n",
1379 			    softc->action == PROBE_INQUIRY_BASIC_DV1 ? 1 : 2);
1380 			if (proberequestbackoff(periph, path->device)) {
1381 				path->device->flags &= ~CAM_DEV_IN_DV;
1382 				PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1383 			} else {
1384 				/* give up */
1385 				PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
1386 			}
1387 			free(nbuf, M_CAMXPT);
1388 			xpt_release_ccb(done_ccb);
1389 			xpt_schedule(periph, priority);
1390 			return;
1391 		}
1392 		free(nbuf, M_CAMXPT);
1393 		if (softc->action == PROBE_INQUIRY_BASIC_DV1) {
1394 			PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV2);
1395 			xpt_release_ccb(done_ccb);
1396 			xpt_schedule(periph, priority);
1397 			return;
1398 		}
1399 		if (softc->action == PROBE_INQUIRY_BASIC_DV2) {
1400 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1401 			    ("Leave Domain Validation Successfully\n"));
1402 		}
1403 		if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1404 			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1405 			xpt_acquire_device(path->device);
1406 		}
1407 		path->device->flags &=
1408 		    ~(CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1409 		if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1410 			/* Inform the XPT that a new device has been found */
1411 			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1412 			xpt_action(done_ccb);
1413 			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1414 				  done_ccb);
1415 		}
1416 		xpt_release_ccb(done_ccb);
1417 		break;
1418 	}
1419 	case PROBE_INVALID:
1420 		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
1421 		    ("probedone: invalid action state\n"));
1422 	default:
1423 		break;
1424 	}
1425 	done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
1426 	TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
1427 	done_ccb->ccb_h.status = CAM_REQ_CMP;
1428 	xpt_done(done_ccb);
1429 	if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
1430 		cam_release_devq(periph->path,
1431 		    RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_XPT + 1, FALSE);
1432 		cam_periph_invalidate(periph);
1433 		cam_periph_release_locked(periph);
1434 	} else {
1435 		probeschedule(periph);
1436 	}
1437 }
1438 
1439 static void
1440 probecleanup(struct cam_periph *periph)
1441 {
1442 	free(periph->softc, M_CAMXPT);
1443 }
1444 
1445 static void
1446 scsi_find_quirk(struct cam_ed *device)
1447 {
1448 	struct scsi_quirk_entry *quirk;
1449 	caddr_t	match;
1450 
1451 	match = cam_quirkmatch((caddr_t)&device->inq_data,
1452 			       (caddr_t)scsi_quirk_table,
1453 			       sizeof(scsi_quirk_table) /
1454 			       sizeof(*scsi_quirk_table),
1455 			       sizeof(*scsi_quirk_table), scsi_inquiry_match);
1456 
1457 	if (match == NULL)
1458 		panic("xpt_find_quirk: device didn't match wildcard entry!!");
1459 
1460 	quirk = (struct scsi_quirk_entry *)match;
1461 	device->quirk = quirk;
1462 	device->mintags = quirk->mintags;
1463 	device->maxtags = quirk->maxtags;
1464 }
1465 
1466 static int
1467 sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS)
1468 {
1469 	int error, bool;
1470 
1471 	bool = cam_srch_hi;
1472 	error = sysctl_handle_int(oidp, &bool, 0, req);
1473 	if (error != 0 || req->newptr == NULL)
1474 		return (error);
1475 	if (bool == 0 || bool == 1) {
1476 		cam_srch_hi = bool;
1477 		return (0);
1478 	} else {
1479 		return (EINVAL);
1480 	}
1481 }
1482 
1483 typedef struct {
1484 	union	ccb *request_ccb;
1485 	struct 	ccb_pathinq *cpi;
1486 	int	counter;
1487 } scsi_scan_bus_info;
1488 
1489 /*
1490  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1491  * As the scan progresses, scsi_scan_bus is used as the
1492  * callback on completion function.
1493  */
1494 static void
1495 scsi_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1496 {
1497 	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1498 		  ("scsi_scan_bus\n"));
1499 	switch (request_ccb->ccb_h.func_code) {
1500 	case XPT_SCAN_BUS:
1501 	{
1502 		scsi_scan_bus_info *scan_info;
1503 		union	ccb *work_ccb, *reset_ccb;
1504 		struct	cam_path *path;
1505 		u_int	i;
1506 		u_int	max_target;
1507 		u_int	initiator_id;
1508 
1509 		/* Find out the characteristics of the bus */
1510 		work_ccb = xpt_alloc_ccb_nowait();
1511 		if (work_ccb == NULL) {
1512 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1513 			xpt_done(request_ccb);
1514 			return;
1515 		}
1516 		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1517 			      request_ccb->ccb_h.pinfo.priority);
1518 		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1519 		xpt_action(work_ccb);
1520 		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1521 			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1522 			xpt_free_ccb(work_ccb);
1523 			xpt_done(request_ccb);
1524 			return;
1525 		}
1526 
1527 		if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
1528 			/*
1529 			 * Can't scan the bus on an adapter that
1530 			 * cannot perform the initiator role.
1531 			 */
1532 			request_ccb->ccb_h.status = CAM_REQ_CMP;
1533 			xpt_free_ccb(work_ccb);
1534 			xpt_done(request_ccb);
1535 			return;
1536 		}
1537 
1538 		/* We may need to reset bus first, if we haven't done it yet. */
1539 		if ((work_ccb->cpi.hba_inquiry &
1540 		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1541 		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1542 		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
1543 			reset_ccb = xpt_alloc_ccb_nowait();
1544 			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1545 			      CAM_PRIORITY_NONE);
1546 			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1547 			xpt_action(reset_ccb);
1548 			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1549 				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1550 				xpt_free_ccb(reset_ccb);
1551 				xpt_free_ccb(work_ccb);
1552 				xpt_done(request_ccb);
1553 				return;
1554 			}
1555 			xpt_free_ccb(reset_ccb);
1556 		}
1557 
1558 		/* Save some state for use while we probe for devices */
1559 		scan_info = (scsi_scan_bus_info *)
1560 		    malloc(sizeof(scsi_scan_bus_info), M_CAMXPT, M_NOWAIT);
1561 		if (scan_info == NULL) {
1562 			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1563 			xpt_done(request_ccb);
1564 			return;
1565 		}
1566 		scan_info->request_ccb = request_ccb;
1567 		scan_info->cpi = &work_ccb->cpi;
1568 
1569 		/* Cache on our stack so we can work asynchronously */
1570 		max_target = scan_info->cpi->max_target;
1571 		initiator_id = scan_info->cpi->initiator_id;
1572 
1573 
1574 		/*
1575 		 * We can scan all targets in parallel, or do it sequentially.
1576 		 */
1577 		if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
1578 			max_target = 0;
1579 			scan_info->counter = 0;
1580 		} else {
1581 			scan_info->counter = scan_info->cpi->max_target + 1;
1582 			if (scan_info->cpi->initiator_id < scan_info->counter) {
1583 				scan_info->counter--;
1584 			}
1585 		}
1586 
1587 		for (i = 0; i <= max_target; i++) {
1588 			cam_status status;
1589 			if (i == initiator_id)
1590 				continue;
1591 
1592 			status = xpt_create_path(&path, xpt_periph,
1593 						 request_ccb->ccb_h.path_id,
1594 						 i, 0);
1595 			if (status != CAM_REQ_CMP) {
1596 				printf("scsi_scan_bus: xpt_create_path failed"
1597 				       " with status %#x, bus scan halted\n",
1598 				       status);
1599 				free(scan_info, M_CAMXPT);
1600 				request_ccb->ccb_h.status = status;
1601 				xpt_free_ccb(work_ccb);
1602 				xpt_done(request_ccb);
1603 				break;
1604 			}
1605 			work_ccb = xpt_alloc_ccb_nowait();
1606 			if (work_ccb == NULL) {
1607 				xpt_free_ccb((union ccb *)scan_info->cpi);
1608 				free(scan_info, M_CAMXPT);
1609 				xpt_free_path(path);
1610 				request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1611 				xpt_done(request_ccb);
1612 				break;
1613 			}
1614 			xpt_setup_ccb(&work_ccb->ccb_h, path,
1615 				      request_ccb->ccb_h.pinfo.priority);
1616 			work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1617 			work_ccb->ccb_h.cbfcnp = scsi_scan_bus;
1618 			work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1619 			work_ccb->crcn.flags = request_ccb->crcn.flags;
1620 			xpt_action(work_ccb);
1621 		}
1622 		break;
1623 	}
1624 	case XPT_SCAN_LUN:
1625 	{
1626 		cam_status status;
1627 		struct cam_path *path;
1628 		scsi_scan_bus_info *scan_info;
1629 		path_id_t path_id;
1630 		target_id_t target_id;
1631 		lun_id_t lun_id;
1632 
1633 		/* Reuse the same CCB to query if a device was really found */
1634 		scan_info = (scsi_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
1635 		xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
1636 			      request_ccb->ccb_h.pinfo.priority);
1637 		request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1638 
1639 		path_id = request_ccb->ccb_h.path_id;
1640 		target_id = request_ccb->ccb_h.target_id;
1641 		lun_id = request_ccb->ccb_h.target_lun;
1642 		xpt_action(request_ccb);
1643 
1644 		if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
1645 			struct cam_ed *device;
1646 			struct cam_et *target;
1647 			int phl;
1648 
1649 			/*
1650 			 * If we already probed lun 0 successfully, or
1651 			 * we have additional configured luns on this
1652 			 * target that might have "gone away", go onto
1653 			 * the next lun.
1654 			 */
1655 			target = request_ccb->ccb_h.path->target;
1656 			/*
1657 			 * We may touch devices that we don't
1658 			 * hold references too, so ensure they
1659 			 * don't disappear out from under us.
1660 			 * The target above is referenced by the
1661 			 * path in the request ccb.
1662 			 */
1663 			phl = 0;
1664 			device = TAILQ_FIRST(&target->ed_entries);
1665 			if (device != NULL) {
1666 				phl = CAN_SRCH_HI_SPARSE(device);
1667 				if (device->lun_id == 0)
1668 					device = TAILQ_NEXT(device, links);
1669 			}
1670 			if ((lun_id != 0) || (device != NULL)) {
1671 				if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
1672 					lun_id++;
1673 			}
1674 		} else {
1675 			struct cam_ed *device;
1676 
1677 			device = request_ccb->ccb_h.path->device;
1678 
1679 			if ((SCSI_QUIRK(device)->quirks &
1680 			    CAM_QUIRK_NOLUNS) == 0) {
1681 				/* Try the next lun */
1682 				if (lun_id < (CAM_SCSI2_MAXLUN-1)
1683 				  || CAN_SRCH_HI_DENSE(device))
1684 					lun_id++;
1685 			}
1686 		}
1687 
1688 		/*
1689 		 * Free the current request path- we're done with it.
1690 		 */
1691 		xpt_free_path(request_ccb->ccb_h.path);
1692 
1693 		/*
1694 		 * Check to see if we scan any further luns.
1695 		 */
1696 		if (lun_id == request_ccb->ccb_h.target_lun
1697                  || lun_id > scan_info->cpi->max_lun) {
1698 			int done;
1699 
1700  hop_again:
1701 			done = 0;
1702 			if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
1703 				scan_info->counter++;
1704 				if (scan_info->counter ==
1705 				    scan_info->cpi->initiator_id) {
1706 					scan_info->counter++;
1707 				}
1708 				if (scan_info->counter >=
1709 				    scan_info->cpi->max_target+1) {
1710 					done = 1;
1711 				}
1712 			} else {
1713 				scan_info->counter--;
1714 				if (scan_info->counter == 0) {
1715 					done = 1;
1716 				}
1717 			}
1718 			if (done) {
1719 				xpt_free_ccb(request_ccb);
1720 				xpt_free_ccb((union ccb *)scan_info->cpi);
1721 				request_ccb = scan_info->request_ccb;
1722 				free(scan_info, M_CAMXPT);
1723 				request_ccb->ccb_h.status = CAM_REQ_CMP;
1724 				xpt_done(request_ccb);
1725 				break;
1726 			}
1727 
1728 			if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
1729 				xpt_free_ccb(request_ccb);
1730 				break;
1731 			}
1732 			status = xpt_create_path(&path, xpt_periph,
1733 			    scan_info->request_ccb->ccb_h.path_id,
1734 			    scan_info->counter, 0);
1735 			if (status != CAM_REQ_CMP) {
1736 				printf("scsi_scan_bus: xpt_create_path failed"
1737 				    " with status %#x, bus scan halted\n",
1738 			       	    status);
1739 				xpt_free_ccb(request_ccb);
1740 				xpt_free_ccb((union ccb *)scan_info->cpi);
1741 				request_ccb = scan_info->request_ccb;
1742 				free(scan_info, M_CAMXPT);
1743 				request_ccb->ccb_h.status = status;
1744 				xpt_done(request_ccb);
1745 				break;
1746 			}
1747 			xpt_setup_ccb(&request_ccb->ccb_h, path,
1748 			    request_ccb->ccb_h.pinfo.priority);
1749 			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1750 			request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
1751 			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
1752 			request_ccb->crcn.flags =
1753 			    scan_info->request_ccb->crcn.flags;
1754 		} else {
1755 			status = xpt_create_path(&path, xpt_periph,
1756 						 path_id, target_id, lun_id);
1757 			if (status != CAM_REQ_CMP) {
1758 				printf("scsi_scan_bus: xpt_create_path failed "
1759 				       "with status %#x, halting LUN scan\n",
1760 			 	       status);
1761 				goto hop_again;
1762 			}
1763 			xpt_setup_ccb(&request_ccb->ccb_h, path,
1764 				      request_ccb->ccb_h.pinfo.priority);
1765 			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1766 			request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
1767 			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
1768 			request_ccb->crcn.flags =
1769 				scan_info->request_ccb->crcn.flags;
1770 		}
1771 		xpt_action(request_ccb);
1772 		break;
1773 	}
1774 	default:
1775 		break;
1776 	}
1777 }
1778 
1779 static void
1780 scsi_scan_lun(struct cam_periph *periph, struct cam_path *path,
1781 	     cam_flags flags, union ccb *request_ccb)
1782 {
1783 	struct ccb_pathinq cpi;
1784 	cam_status status;
1785 	struct cam_path *new_path;
1786 	struct cam_periph *old_periph;
1787 
1788 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("scsi_scan_lun\n"));
1789 
1790 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1791 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1792 	xpt_action((union ccb *)&cpi);
1793 
1794 	if (cpi.ccb_h.status != CAM_REQ_CMP) {
1795 		if (request_ccb != NULL) {
1796 			request_ccb->ccb_h.status = cpi.ccb_h.status;
1797 			xpt_done(request_ccb);
1798 		}
1799 		return;
1800 	}
1801 
1802 	if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
1803 		/*
1804 		 * Can't scan the bus on an adapter that
1805 		 * cannot perform the initiator role.
1806 		 */
1807 		if (request_ccb != NULL) {
1808 			request_ccb->ccb_h.status = CAM_REQ_CMP;
1809 			xpt_done(request_ccb);
1810 		}
1811 		return;
1812 	}
1813 
1814 	if (request_ccb == NULL) {
1815 		request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
1816 		if (request_ccb == NULL) {
1817 			xpt_print(path, "scsi_scan_lun: can't allocate CCB, "
1818 			    "can't continue\n");
1819 			return;
1820 		}
1821 		new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
1822 		if (new_path == NULL) {
1823 			xpt_print(path, "scsi_scan_lun: can't allocate path, "
1824 			    "can't continue\n");
1825 			free(request_ccb, M_CAMXPT);
1826 			return;
1827 		}
1828 		status = xpt_compile_path(new_path, xpt_periph,
1829 					  path->bus->path_id,
1830 					  path->target->target_id,
1831 					  path->device->lun_id);
1832 
1833 		if (status != CAM_REQ_CMP) {
1834 			xpt_print(path, "scsi_scan_lun: can't compile path, "
1835 			    "can't continue\n");
1836 			free(request_ccb, M_CAMXPT);
1837 			free(new_path, M_CAMXPT);
1838 			return;
1839 		}
1840 		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
1841 		request_ccb->ccb_h.cbfcnp = xptscandone;
1842 		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1843 		request_ccb->crcn.flags = flags;
1844 	}
1845 
1846 	if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
1847 		probe_softc *softc;
1848 
1849 		softc = (probe_softc *)old_periph->softc;
1850 		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
1851 				  periph_links.tqe);
1852 	} else {
1853 		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1854 					  probestart, "probe",
1855 					  CAM_PERIPH_BIO,
1856 					  request_ccb->ccb_h.path, NULL, 0,
1857 					  request_ccb);
1858 
1859 		if (status != CAM_REQ_CMP) {
1860 			xpt_print(path, "scsi_scan_lun: cam_alloc_periph "
1861 			    "returned an error, can't continue probe\n");
1862 			request_ccb->ccb_h.status = status;
1863 			xpt_done(request_ccb);
1864 		}
1865 	}
1866 }
1867 
1868 static void
1869 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
1870 {
1871 	xpt_release_path(done_ccb->ccb_h.path);
1872 	free(done_ccb->ccb_h.path, M_CAMXPT);
1873 	free(done_ccb, M_CAMXPT);
1874 }
1875 
1876 static struct cam_ed *
1877 scsi_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
1878 {
1879 	struct cam_path path;
1880 	struct scsi_quirk_entry *quirk;
1881 	struct cam_ed *device;
1882 	struct cam_ed *cur_device;
1883 
1884 	device = xpt_alloc_device(bus, target, lun_id);
1885 	if (device == NULL)
1886 		return (NULL);
1887 
1888 	/*
1889 	 * Take the default quirk entry until we have inquiry
1890 	 * data and can determine a better quirk to use.
1891 	 */
1892 	quirk = &scsi_quirk_table[scsi_quirk_table_size - 1];
1893 	device->quirk = (void *)quirk;
1894 	device->mintags = quirk->mintags;
1895 	device->maxtags = quirk->maxtags;
1896 	bzero(&device->inq_data, sizeof(device->inq_data));
1897 	device->inq_flags = 0;
1898 	device->queue_flags = 0;
1899 	device->serial_num = NULL;
1900 	device->serial_num_len = 0;
1901 
1902 	/*
1903 	 * XXX should be limited by number of CCBs this bus can
1904 	 * do.
1905 	 */
1906 	bus->sim->max_ccbs += device->ccbq.devq_openings;
1907 	/* Insertion sort into our target's device list */
1908 	cur_device = TAILQ_FIRST(&target->ed_entries);
1909 	while (cur_device != NULL && cur_device->lun_id < lun_id)
1910 		cur_device = TAILQ_NEXT(cur_device, links);
1911 	if (cur_device != NULL) {
1912 		TAILQ_INSERT_BEFORE(cur_device, device, links);
1913 	} else {
1914 		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
1915 	}
1916 	target->generation++;
1917 	if (lun_id != CAM_LUN_WILDCARD) {
1918 		xpt_compile_path(&path,
1919 				 NULL,
1920 				 bus->path_id,
1921 				 target->target_id,
1922 				 lun_id);
1923 		scsi_devise_transport(&path);
1924 		xpt_release_path(&path);
1925 	}
1926 
1927 	return (device);
1928 }
1929 
1930 static void
1931 scsi_devise_transport(struct cam_path *path)
1932 {
1933 	struct ccb_pathinq cpi;
1934 	struct ccb_trans_settings cts;
1935 	struct scsi_inquiry_data *inq_buf;
1936 
1937 	/* Get transport information from the SIM */
1938 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1939 	cpi.ccb_h.func_code = XPT_PATH_INQ;
1940 	xpt_action((union ccb *)&cpi);
1941 
1942 	inq_buf = NULL;
1943 	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1944 		inq_buf = &path->device->inq_data;
1945 	path->device->protocol = PROTO_SCSI;
1946 	path->device->protocol_version =
1947 	    inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1948 	path->device->transport = cpi.transport;
1949 	path->device->transport_version = cpi.transport_version;
1950 
1951 	/*
1952 	 * Any device not using SPI3 features should
1953 	 * be considered SPI2 or lower.
1954 	 */
1955 	if (inq_buf != NULL) {
1956 		if (path->device->transport == XPORT_SPI
1957 		 && (inq_buf->spi3data & SID_SPI_MASK) == 0
1958 		 && path->device->transport_version > 2)
1959 			path->device->transport_version = 2;
1960 	} else {
1961 		struct cam_ed* otherdev;
1962 
1963 		for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
1964 		     otherdev != NULL;
1965 		     otherdev = TAILQ_NEXT(otherdev, links)) {
1966 			if (otherdev != path->device)
1967 				break;
1968 		}
1969 
1970 		if (otherdev != NULL) {
1971 			/*
1972 			 * Initially assume the same versioning as
1973 			 * prior luns for this target.
1974 			 */
1975 			path->device->protocol_version =
1976 			    otherdev->protocol_version;
1977 			path->device->transport_version =
1978 			    otherdev->transport_version;
1979 		} else {
1980 			/* Until we know better, opt for safty */
1981 			path->device->protocol_version = 2;
1982 			if (path->device->transport == XPORT_SPI)
1983 				path->device->transport_version = 2;
1984 			else
1985 				path->device->transport_version = 0;
1986 		}
1987 	}
1988 
1989 	/*
1990 	 * XXX
1991 	 * For a device compliant with SPC-2 we should be able
1992 	 * to determine the transport version supported by
1993 	 * scrutinizing the version descriptors in the
1994 	 * inquiry buffer.
1995 	 */
1996 
1997 	/* Tell the controller what we think */
1998 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1999 	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
2000 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
2001 	cts.transport = path->device->transport;
2002 	cts.transport_version = path->device->transport_version;
2003 	cts.protocol = path->device->protocol;
2004 	cts.protocol_version = path->device->protocol_version;
2005 	cts.proto_specific.valid = 0;
2006 	cts.xport_specific.valid = 0;
2007 	xpt_action((union ccb *)&cts);
2008 }
2009 
2010 static void
2011 scsi_action(union ccb *start_ccb)
2012 {
2013 
2014 	switch (start_ccb->ccb_h.func_code) {
2015 	case XPT_SET_TRAN_SETTINGS:
2016 	{
2017 		scsi_set_transfer_settings(&start_ccb->cts,
2018 					   start_ccb->ccb_h.path->device,
2019 					   /*async_update*/FALSE);
2020 		break;
2021 	}
2022 	case XPT_SCAN_BUS:
2023 		scsi_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
2024 		break;
2025 	case XPT_SCAN_LUN:
2026 		scsi_scan_lun(start_ccb->ccb_h.path->periph,
2027 			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
2028 			      start_ccb);
2029 		break;
2030 	case XPT_GET_TRAN_SETTINGS:
2031 	{
2032 		struct cam_sim *sim;
2033 
2034 		sim = start_ccb->ccb_h.path->bus->sim;
2035 		(*(sim->sim_action))(sim, start_ccb);
2036 		break;
2037 	}
2038 	default:
2039 		xpt_action_default(start_ccb);
2040 		break;
2041 	}
2042 }
2043 
2044 static void
2045 scsi_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
2046 			   int async_update)
2047 {
2048 	struct	ccb_pathinq cpi;
2049 	struct	ccb_trans_settings cur_cts;
2050 	struct	ccb_trans_settings_scsi *scsi;
2051 	struct	ccb_trans_settings_scsi *cur_scsi;
2052 	struct	cam_sim *sim;
2053 	struct	scsi_inquiry_data *inq_data;
2054 
2055 	if (device == NULL) {
2056 		cts->ccb_h.status = CAM_PATH_INVALID;
2057 		xpt_done((union ccb *)cts);
2058 		return;
2059 	}
2060 
2061 	if (cts->protocol == PROTO_UNKNOWN
2062 	 || cts->protocol == PROTO_UNSPECIFIED) {
2063 		cts->protocol = device->protocol;
2064 		cts->protocol_version = device->protocol_version;
2065 	}
2066 
2067 	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
2068 	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
2069 		cts->protocol_version = device->protocol_version;
2070 
2071 	if (cts->protocol != device->protocol) {
2072 		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
2073 		       cts->protocol, device->protocol);
2074 		cts->protocol = device->protocol;
2075 	}
2076 
2077 	if (cts->protocol_version > device->protocol_version) {
2078 		if (bootverbose) {
2079 			xpt_print(cts->ccb_h.path, "Down reving Protocol "
2080 			    "Version from %d to %d?\n", cts->protocol_version,
2081 			    device->protocol_version);
2082 		}
2083 		cts->protocol_version = device->protocol_version;
2084 	}
2085 
2086 	if (cts->transport == XPORT_UNKNOWN
2087 	 || cts->transport == XPORT_UNSPECIFIED) {
2088 		cts->transport = device->transport;
2089 		cts->transport_version = device->transport_version;
2090 	}
2091 
2092 	if (cts->transport_version == XPORT_VERSION_UNKNOWN
2093 	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
2094 		cts->transport_version = device->transport_version;
2095 
2096 	if (cts->transport != device->transport) {
2097 		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
2098 		    cts->transport, device->transport);
2099 		cts->transport = device->transport;
2100 	}
2101 
2102 	if (cts->transport_version > device->transport_version) {
2103 		if (bootverbose) {
2104 			xpt_print(cts->ccb_h.path, "Down reving Transport "
2105 			    "Version from %d to %d?\n", cts->transport_version,
2106 			    device->transport_version);
2107 		}
2108 		cts->transport_version = device->transport_version;
2109 	}
2110 
2111 	sim = cts->ccb_h.path->bus->sim;
2112 
2113 	/*
2114 	 * Nothing more of interest to do unless
2115 	 * this is a device connected via the
2116 	 * SCSI protocol.
2117 	 */
2118 	if (cts->protocol != PROTO_SCSI) {
2119 		if (async_update == FALSE)
2120 			(*(sim->sim_action))(sim, (union ccb *)cts);
2121 		return;
2122 	}
2123 
2124 	inq_data = &device->inq_data;
2125 	scsi = &cts->proto_specific.scsi;
2126 	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
2127 	cpi.ccb_h.func_code = XPT_PATH_INQ;
2128 	xpt_action((union ccb *)&cpi);
2129 
2130 	/* SCSI specific sanity checking */
2131 	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
2132 	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
2133 	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
2134 	 || (device->mintags == 0)) {
2135 		/*
2136 		 * Can't tag on hardware that doesn't support tags,
2137 		 * doesn't have it enabled, or has broken tag support.
2138 		 */
2139 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2140 	}
2141 
2142 	if (async_update == FALSE) {
2143 		/*
2144 		 * Perform sanity checking against what the
2145 		 * controller and device can do.
2146 		 */
2147 		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
2148 		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2149 		cur_cts.type = cts->type;
2150 		xpt_action((union ccb *)&cur_cts);
2151 		if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2152 			return;
2153 		}
2154 		cur_scsi = &cur_cts.proto_specific.scsi;
2155 		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
2156 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2157 			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
2158 		}
2159 		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
2160 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2161 	}
2162 
2163 	/* SPI specific sanity checking */
2164 	if (cts->transport == XPORT_SPI && async_update == FALSE) {
2165 		u_int spi3caps;
2166 		struct ccb_trans_settings_spi *spi;
2167 		struct ccb_trans_settings_spi *cur_spi;
2168 
2169 		spi = &cts->xport_specific.spi;
2170 
2171 		cur_spi = &cur_cts.xport_specific.spi;
2172 
2173 		/* Fill in any gaps in what the user gave us */
2174 		if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2175 			spi->sync_period = cur_spi->sync_period;
2176 		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2177 			spi->sync_period = 0;
2178 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2179 			spi->sync_offset = cur_spi->sync_offset;
2180 		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2181 			spi->sync_offset = 0;
2182 		if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2183 			spi->ppr_options = cur_spi->ppr_options;
2184 		if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2185 			spi->ppr_options = 0;
2186 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2187 			spi->bus_width = cur_spi->bus_width;
2188 		if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2189 			spi->bus_width = 0;
2190 		if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
2191 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2192 			spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
2193 		}
2194 		if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
2195 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2196 		if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2197 		  && (inq_data->flags & SID_Sync) == 0
2198 		  && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2199 		 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)) {
2200 			/* Force async */
2201 			spi->sync_period = 0;
2202 			spi->sync_offset = 0;
2203 		}
2204 
2205 		switch (spi->bus_width) {
2206 		case MSG_EXT_WDTR_BUS_32_BIT:
2207 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2208 			  || (inq_data->flags & SID_WBus32) != 0
2209 			  || cts->type == CTS_TYPE_USER_SETTINGS)
2210 			 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
2211 				break;
2212 			/* Fall Through to 16-bit */
2213 		case MSG_EXT_WDTR_BUS_16_BIT:
2214 			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2215 			  || (inq_data->flags & SID_WBus16) != 0
2216 			  || cts->type == CTS_TYPE_USER_SETTINGS)
2217 			 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
2218 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2219 				break;
2220 			}
2221 			/* Fall Through to 8-bit */
2222 		default: /* New bus width?? */
2223 		case MSG_EXT_WDTR_BUS_8_BIT:
2224 			/* All targets can do this */
2225 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2226 			break;
2227 		}
2228 
2229 		spi3caps = cpi.xport_specific.spi.ppr_options;
2230 		if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2231 		 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2232 			spi3caps &= inq_data->spi3data;
2233 
2234 		if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
2235 			spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2236 
2237 		if ((spi3caps & SID_SPI_IUS) == 0)
2238 			spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
2239 
2240 		if ((spi3caps & SID_SPI_QAS) == 0)
2241 			spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
2242 
2243 		/* No SPI Transfer settings are allowed unless we are wide */
2244 		if (spi->bus_width == 0)
2245 			spi->ppr_options = 0;
2246 
2247 		if ((spi->valid & CTS_SPI_VALID_DISC)
2248 		 && ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0)) {
2249 			/*
2250 			 * Can't tag queue without disconnection.
2251 			 */
2252 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2253 			scsi->valid |= CTS_SCSI_VALID_TQ;
2254 		}
2255 
2256 		/*
2257 		 * If we are currently performing tagged transactions to
2258 		 * this device and want to change its negotiation parameters,
2259 		 * go non-tagged for a bit to give the controller a chance to
2260 		 * negotiate unhampered by tag messages.
2261 		 */
2262 		if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2263 		 && (device->inq_flags & SID_CmdQue) != 0
2264 		 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2265 		 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
2266 				   CTS_SPI_VALID_SYNC_OFFSET|
2267 				   CTS_SPI_VALID_BUS_WIDTH)) != 0)
2268 			scsi_toggle_tags(cts->ccb_h.path);
2269 	}
2270 
2271 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2272 	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
2273 		int device_tagenb;
2274 
2275 		/*
2276 		 * If we are transitioning from tags to no-tags or
2277 		 * vice-versa, we need to carefully freeze and restart
2278 		 * the queue so that we don't overlap tagged and non-tagged
2279 		 * commands.  We also temporarily stop tags if there is
2280 		 * a change in transfer negotiation settings to allow
2281 		 * "tag-less" negotiation.
2282 		 */
2283 		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2284 		 || (device->inq_flags & SID_CmdQue) != 0)
2285 			device_tagenb = TRUE;
2286 		else
2287 			device_tagenb = FALSE;
2288 
2289 		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2290 		  && device_tagenb == FALSE)
2291 		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
2292 		  && device_tagenb == TRUE)) {
2293 
2294 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
2295 				/*
2296 				 * Delay change to use tags until after a
2297 				 * few commands have gone to this device so
2298 				 * the controller has time to perform transfer
2299 				 * negotiations without tagged messages getting
2300 				 * in the way.
2301 				 */
2302 				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
2303 				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
2304 			} else {
2305 				xpt_stop_tags(cts->ccb_h.path);
2306 			}
2307 		}
2308 	}
2309 	if (async_update == FALSE)
2310 		(*(sim->sim_action))(sim, (union ccb *)cts);
2311 }
2312 
2313 static void
2314 scsi_toggle_tags(struct cam_path *path)
2315 {
2316 	struct cam_ed *dev;
2317 
2318 	/*
2319 	 * Give controllers a chance to renegotiate
2320 	 * before starting tag operations.  We
2321 	 * "toggle" tagged queuing off then on
2322 	 * which causes the tag enable command delay
2323 	 * counter to come into effect.
2324 	 */
2325 	dev = path->device;
2326 	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2327 	 || ((dev->inq_flags & SID_CmdQue) != 0
2328  	  && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
2329 		struct ccb_trans_settings cts;
2330 
2331 		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2332 		cts.protocol = PROTO_SCSI;
2333 		cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
2334 		cts.transport = XPORT_UNSPECIFIED;
2335 		cts.transport_version = XPORT_VERSION_UNSPECIFIED;
2336 		cts.proto_specific.scsi.flags = 0;
2337 		cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
2338 		scsi_set_transfer_settings(&cts, path->device,
2339 					  /*async_update*/TRUE);
2340 		cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
2341 		scsi_set_transfer_settings(&cts, path->device,
2342 					  /*async_update*/TRUE);
2343 	}
2344 }
2345 
2346 /*
2347  * Handle any per-device event notifications that require action by the XPT.
2348  */
2349 static void
2350 scsi_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
2351 	      struct cam_ed *device, void *async_arg)
2352 {
2353 	cam_status status;
2354 	struct cam_path newpath;
2355 
2356 	/*
2357 	 * We only need to handle events for real devices.
2358 	 */
2359 	if (target->target_id == CAM_TARGET_WILDCARD
2360 	 || device->lun_id == CAM_LUN_WILDCARD)
2361 		return;
2362 
2363 	/*
2364 	 * We need our own path with wildcards expanded to
2365 	 * handle certain types of events.
2366 	 */
2367 	if ((async_code == AC_SENT_BDR)
2368 	 || (async_code == AC_BUS_RESET)
2369 	 || (async_code == AC_INQ_CHANGED))
2370 		status = xpt_compile_path(&newpath, NULL,
2371 					  bus->path_id,
2372 					  target->target_id,
2373 					  device->lun_id);
2374 	else
2375 		status = CAM_REQ_CMP_ERR;
2376 
2377 	if (status == CAM_REQ_CMP) {
2378 
2379 		/*
2380 		 * Allow transfer negotiation to occur in a
2381 		 * tag free environment and after settle delay.
2382 		 */
2383 		if (async_code == AC_SENT_BDR
2384 		 || async_code == AC_BUS_RESET) {
2385 			cam_freeze_devq(&newpath);
2386 			cam_release_devq(&newpath,
2387 				RELSIM_RELEASE_AFTER_TIMEOUT,
2388 				/*reduction*/0,
2389 				/*timeout*/scsi_delay,
2390 				/*getcount_only*/0);
2391 			scsi_toggle_tags(&newpath);
2392 		}
2393 
2394 		if (async_code == AC_INQ_CHANGED) {
2395 			/*
2396 			 * We've sent a start unit command, or
2397 			 * something similar to a device that
2398 			 * may have caused its inquiry data to
2399 			 * change. So we re-scan the device to
2400 			 * refresh the inquiry data for it.
2401 			 */
2402 			scsi_scan_lun(newpath.periph, &newpath,
2403 				     CAM_EXPECT_INQ_CHANGE, NULL);
2404 		}
2405 		xpt_release_path(&newpath);
2406 	} else if (async_code == AC_LOST_DEVICE &&
2407 	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
2408 		device->flags |= CAM_DEV_UNCONFIGURED;
2409 		xpt_release_device(device);
2410 	} else if (async_code == AC_TRANSFER_NEG) {
2411 		struct ccb_trans_settings *settings;
2412 
2413 		settings = (struct ccb_trans_settings *)async_arg;
2414 		scsi_set_transfer_settings(settings, device,
2415 					  /*async_update*/TRUE);
2416 	}
2417 }
2418 
2419 static void
2420 scsi_announce_periph(struct cam_periph *periph)
2421 {
2422 	struct	ccb_pathinq cpi;
2423 	struct	ccb_trans_settings cts;
2424 	struct	cam_path *path = periph->path;
2425 	u_int	speed;
2426 	u_int	freq;
2427 	u_int	mb;
2428 
2429 	mtx_assert(periph->sim->mtx, MA_OWNED);
2430 
2431 	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
2432 	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2433 	cts.type = CTS_TYPE_CURRENT_SETTINGS;
2434 	xpt_action((union ccb*)&cts);
2435 	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
2436 		return;
2437 	/* Ask the SIM for its base transfer speed */
2438 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
2439 	cpi.ccb_h.func_code = XPT_PATH_INQ;
2440 	xpt_action((union ccb *)&cpi);
2441 	/* Report connection speed */
2442 	speed = cpi.base_transfer_speed;
2443 	freq = 0;
2444 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
2445 		struct	ccb_trans_settings_spi *spi =
2446 		    &cts.xport_specific.spi;
2447 
2448 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
2449 		  && spi->sync_offset != 0) {
2450 			freq = scsi_calc_syncsrate(spi->sync_period);
2451 			speed = freq;
2452 		}
2453 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
2454 			speed *= (0x01 << spi->bus_width);
2455 	}
2456 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
2457 		struct	ccb_trans_settings_fc *fc =
2458 		    &cts.xport_specific.fc;
2459 
2460 		if (fc->valid & CTS_FC_VALID_SPEED)
2461 			speed = fc->bitrate;
2462 	}
2463 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SAS) {
2464 		struct	ccb_trans_settings_sas *sas =
2465 		    &cts.xport_specific.sas;
2466 
2467 		if (sas->valid & CTS_SAS_VALID_SPEED)
2468 			speed = sas->bitrate;
2469 	}
2470 	mb = speed / 1000;
2471 	if (mb > 0)
2472 		printf("%s%d: %d.%03dMB/s transfers",
2473 		       periph->periph_name, periph->unit_number,
2474 		       mb, speed % 1000);
2475 	else
2476 		printf("%s%d: %dKB/s transfers", periph->periph_name,
2477 		       periph->unit_number, speed);
2478 	/* Report additional information about SPI connections */
2479 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
2480 		struct	ccb_trans_settings_spi *spi;
2481 
2482 		spi = &cts.xport_specific.spi;
2483 		if (freq != 0) {
2484 			printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
2485 			       freq % 1000,
2486 			       (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
2487 			     ? " DT" : "",
2488 			       spi->sync_offset);
2489 		}
2490 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
2491 		 && spi->bus_width > 0) {
2492 			if (freq != 0) {
2493 				printf(", ");
2494 			} else {
2495 				printf(" (");
2496 			}
2497 			printf("%dbit)", 8 * (0x01 << spi->bus_width));
2498 		} else if (freq != 0) {
2499 			printf(")");
2500 		}
2501 	}
2502 	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
2503 		struct	ccb_trans_settings_fc *fc;
2504 
2505 		fc = &cts.xport_specific.fc;
2506 		if (fc->valid & CTS_FC_VALID_WWNN)
2507 			printf(" WWNN 0x%llx", (long long) fc->wwnn);
2508 		if (fc->valid & CTS_FC_VALID_WWPN)
2509 			printf(" WWPN 0x%llx", (long long) fc->wwpn);
2510 		if (fc->valid & CTS_FC_VALID_PORT)
2511 			printf(" PortID 0x%x", fc->port);
2512 	}
2513 	printf("\n");
2514 }
2515 
2516