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