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