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