scsi_xpt.c revision 273976
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 273976 2014-11-02 17:26:13Z 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				/*
2045				 * We're done with scanning all luns.
2046				 *
2047				 * Nuke the bogus device for lun 0 if lun 0
2048				 * wasn't on the list.
2049				 */
2050				if (first != 0) {
2051					TAILQ_FOREACH(device,
2052					    &target->ed_entries, links) {
2053						if (device->lun_id == 0) {
2054							break;
2055						}
2056					}
2057					if (device) {
2058						xpt_release_device(device);
2059					}
2060				}
2061			}
2062		} else {
2063			mtx_unlock(&target->luns_mtx);
2064			device = request_ccb->ccb_h.path->device;
2065			/* Continue sequential LUN scan if: */
2066			/*  -- we have more LUNs that need recheck */
2067			mtx_lock(&target->bus->eb_mtx);
2068			nextdev = device;
2069			while ((nextdev = TAILQ_NEXT(nextdev, links)) != NULL)
2070				if ((nextdev->flags & CAM_DEV_UNCONFIGURED) == 0)
2071					break;
2072			mtx_unlock(&target->bus->eb_mtx);
2073			if (nextdev != NULL) {
2074				next_target = 0;
2075			/*  -- stop if CAM_QUIRK_NOLUNS is set. */
2076			} else if (SCSI_QUIRK(device)->quirks & CAM_QUIRK_NOLUNS) {
2077				next_target = 1;
2078			/*  -- this LUN is connected and its SCSI version
2079			 *     allows more LUNs. */
2080			} else if ((device->flags & CAM_DEV_UNCONFIGURED) == 0) {
2081				if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
2082				    CAN_SRCH_HI_DENSE(device))
2083					next_target = 0;
2084			/*  -- this LUN is disconnected, its SCSI version
2085			 *     allows more LUNs and we guess they may be. */
2086			} else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0) {
2087				if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
2088				    CAN_SRCH_HI_SPARSE(device))
2089					next_target = 0;
2090			}
2091			if (next_target == 0) {
2092				lun_id++;
2093				if (lun_id > scan_info->cpi->max_lun)
2094					next_target = 1;
2095			}
2096		}
2097
2098		/*
2099		 * Check to see if we scan any further luns.
2100		 */
2101		if (next_target) {
2102			int done;
2103
2104			/*
2105			 * Free the current request path- we're done with it.
2106			 */
2107			xpt_free_path(oldpath);
2108 hop_again:
2109			done = 0;
2110			if (scan_info->request_ccb->ccb_h.func_code == XPT_SCAN_TGT) {
2111				done = 1;
2112			} else if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
2113				scan_info->counter++;
2114				if (scan_info->counter ==
2115				    scan_info->cpi->initiator_id) {
2116					scan_info->counter++;
2117				}
2118				if (scan_info->counter >=
2119				    scan_info->cpi->max_target+1) {
2120					done = 1;
2121				}
2122			} else {
2123				scan_info->counter--;
2124				if (scan_info->counter == 0) {
2125					done = 1;
2126				}
2127			}
2128			if (done) {
2129				mtx_unlock(mtx);
2130				xpt_free_ccb(request_ccb);
2131				xpt_free_ccb((union ccb *)scan_info->cpi);
2132				request_ccb = scan_info->request_ccb;
2133				CAM_DEBUG(request_ccb->ccb_h.path,
2134				    CAM_DEBUG_TRACE,
2135				   ("SCAN done for %p\n", scan_info));
2136				free(scan_info, M_CAMXPT);
2137				request_ccb->ccb_h.status = CAM_REQ_CMP;
2138				xpt_done(request_ccb);
2139				break;
2140			}
2141
2142			if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
2143				mtx_unlock(mtx);
2144				xpt_free_ccb(request_ccb);
2145				break;
2146			}
2147			status = xpt_create_path(&path, NULL,
2148			    scan_info->request_ccb->ccb_h.path_id,
2149			    scan_info->counter, 0);
2150			if (status != CAM_REQ_CMP) {
2151				mtx_unlock(mtx);
2152				printf("scsi_scan_bus: xpt_create_path failed"
2153				    " with status %#x, bus scan halted\n",
2154			       	    status);
2155				xpt_free_ccb(request_ccb);
2156				xpt_free_ccb((union ccb *)scan_info->cpi);
2157				request_ccb = scan_info->request_ccb;
2158				free(scan_info, M_CAMXPT);
2159				request_ccb->ccb_h.status = status;
2160				xpt_done(request_ccb);
2161				break;
2162			}
2163			xpt_setup_ccb(&request_ccb->ccb_h, path,
2164			    request_ccb->ccb_h.pinfo.priority);
2165			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2166			request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2167			request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2168			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
2169			request_ccb->crcn.flags =
2170			    scan_info->request_ccb->crcn.flags;
2171		} else {
2172			status = xpt_create_path(&path, NULL,
2173						 path_id, target_id, lun_id);
2174			/*
2175			 * Free the old request path- we're done with it. We
2176			 * do this *after* creating the new path so that
2177			 * we don't remove a target that has our lun list
2178			 * in the case that lun 0 is not present.
2179			 */
2180			xpt_free_path(oldpath);
2181			if (status != CAM_REQ_CMP) {
2182				printf("scsi_scan_bus: xpt_create_path failed "
2183				       "with status %#x, halting LUN scan\n",
2184			 	       status);
2185				goto hop_again;
2186			}
2187			xpt_setup_ccb(&request_ccb->ccb_h, path,
2188				      request_ccb->ccb_h.pinfo.priority);
2189			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2190			request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2191			request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2192			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
2193			request_ccb->crcn.flags =
2194				scan_info->request_ccb->crcn.flags;
2195		}
2196		mtx_unlock(mtx);
2197		xpt_action(request_ccb);
2198		break;
2199	}
2200	default:
2201		break;
2202	}
2203}
2204
2205static void
2206scsi_scan_lun(struct cam_periph *periph, struct cam_path *path,
2207	     cam_flags flags, union ccb *request_ccb)
2208{
2209	struct ccb_pathinq cpi;
2210	cam_status status;
2211	struct cam_path *new_path;
2212	struct cam_periph *old_periph;
2213	int lock;
2214
2215	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("scsi_scan_lun\n"));
2216
2217	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2218	cpi.ccb_h.func_code = XPT_PATH_INQ;
2219	xpt_action((union ccb *)&cpi);
2220
2221	if (cpi.ccb_h.status != CAM_REQ_CMP) {
2222		if (request_ccb != NULL) {
2223			request_ccb->ccb_h.status = cpi.ccb_h.status;
2224			xpt_done(request_ccb);
2225		}
2226		return;
2227	}
2228
2229	if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
2230		/*
2231		 * Can't scan the bus on an adapter that
2232		 * cannot perform the initiator role.
2233		 */
2234		if (request_ccb != NULL) {
2235			request_ccb->ccb_h.status = CAM_REQ_CMP;
2236			xpt_done(request_ccb);
2237		}
2238		return;
2239	}
2240
2241	if (request_ccb == NULL) {
2242		request_ccb = xpt_alloc_ccb_nowait();
2243		if (request_ccb == NULL) {
2244			xpt_print(path, "scsi_scan_lun: can't allocate CCB, "
2245			    "can't continue\n");
2246			return;
2247		}
2248		status = xpt_create_path(&new_path, NULL,
2249					  path->bus->path_id,
2250					  path->target->target_id,
2251					  path->device->lun_id);
2252		if (status != CAM_REQ_CMP) {
2253			xpt_print(path, "scsi_scan_lun: can't create path, "
2254			    "can't continue\n");
2255			xpt_free_ccb(request_ccb);
2256			return;
2257		}
2258		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
2259		request_ccb->ccb_h.cbfcnp = xptscandone;
2260		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2261		request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2262		request_ccb->crcn.flags = flags;
2263	}
2264
2265	lock = (xpt_path_owned(path) == 0);
2266	if (lock)
2267		xpt_path_lock(path);
2268	if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
2269		if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
2270			probe_softc *softc;
2271
2272			softc = (probe_softc *)old_periph->softc;
2273			TAILQ_INSERT_TAIL(&softc->request_ccbs,
2274			    &request_ccb->ccb_h, periph_links.tqe);
2275		} else {
2276			request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2277			xpt_done(request_ccb);
2278		}
2279	} else {
2280		status = cam_periph_alloc(proberegister, NULL, probecleanup,
2281					  probestart, "probe",
2282					  CAM_PERIPH_BIO,
2283					  request_ccb->ccb_h.path, NULL, 0,
2284					  request_ccb);
2285
2286		if (status != CAM_REQ_CMP) {
2287			xpt_print(path, "scsi_scan_lun: cam_alloc_periph "
2288			    "returned an error, can't continue probe\n");
2289			request_ccb->ccb_h.status = status;
2290			xpt_done(request_ccb);
2291		}
2292	}
2293	if (lock)
2294		xpt_path_unlock(path);
2295}
2296
2297static void
2298xptscandone(struct cam_periph *periph, union ccb *done_ccb)
2299{
2300
2301	xpt_free_path(done_ccb->ccb_h.path);
2302	xpt_free_ccb(done_ccb);
2303}
2304
2305static struct cam_ed *
2306scsi_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
2307{
2308	struct scsi_quirk_entry *quirk;
2309	struct cam_ed *device;
2310
2311	device = xpt_alloc_device(bus, target, lun_id);
2312	if (device == NULL)
2313		return (NULL);
2314
2315	/*
2316	 * Take the default quirk entry until we have inquiry
2317	 * data and can determine a better quirk to use.
2318	 */
2319	quirk = &scsi_quirk_table[scsi_quirk_table_size - 1];
2320	device->quirk = (void *)quirk;
2321	device->mintags = quirk->mintags;
2322	device->maxtags = quirk->maxtags;
2323	bzero(&device->inq_data, sizeof(device->inq_data));
2324	device->inq_flags = 0;
2325	device->queue_flags = 0;
2326	device->serial_num = NULL;
2327	device->serial_num_len = 0;
2328	device->device_id = NULL;
2329	device->device_id_len = 0;
2330	device->supported_vpds = NULL;
2331	device->supported_vpds_len = 0;
2332	return (device);
2333}
2334
2335static void
2336scsi_devise_transport(struct cam_path *path)
2337{
2338	struct ccb_pathinq cpi;
2339	struct ccb_trans_settings cts;
2340	struct scsi_inquiry_data *inq_buf;
2341
2342	/* Get transport information from the SIM */
2343	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2344	cpi.ccb_h.func_code = XPT_PATH_INQ;
2345	xpt_action((union ccb *)&cpi);
2346
2347	inq_buf = NULL;
2348	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
2349		inq_buf = &path->device->inq_data;
2350	path->device->protocol = PROTO_SCSI;
2351	path->device->protocol_version =
2352	    inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
2353	path->device->transport = cpi.transport;
2354	path->device->transport_version = cpi.transport_version;
2355
2356	/*
2357	 * Any device not using SPI3 features should
2358	 * be considered SPI2 or lower.
2359	 */
2360	if (inq_buf != NULL) {
2361		if (path->device->transport == XPORT_SPI
2362		 && (inq_buf->spi3data & SID_SPI_MASK) == 0
2363		 && path->device->transport_version > 2)
2364			path->device->transport_version = 2;
2365	} else {
2366		struct cam_ed* otherdev;
2367
2368		for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
2369		     otherdev != NULL;
2370		     otherdev = TAILQ_NEXT(otherdev, links)) {
2371			if (otherdev != path->device)
2372				break;
2373		}
2374
2375		if (otherdev != NULL) {
2376			/*
2377			 * Initially assume the same versioning as
2378			 * prior luns for this target.
2379			 */
2380			path->device->protocol_version =
2381			    otherdev->protocol_version;
2382			path->device->transport_version =
2383			    otherdev->transport_version;
2384		} else {
2385			/* Until we know better, opt for safty */
2386			path->device->protocol_version = 2;
2387			if (path->device->transport == XPORT_SPI)
2388				path->device->transport_version = 2;
2389			else
2390				path->device->transport_version = 0;
2391		}
2392	}
2393
2394	/*
2395	 * XXX
2396	 * For a device compliant with SPC-2 we should be able
2397	 * to determine the transport version supported by
2398	 * scrutinizing the version descriptors in the
2399	 * inquiry buffer.
2400	 */
2401
2402	/* Tell the controller what we think */
2403	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2404	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
2405	cts.type = CTS_TYPE_CURRENT_SETTINGS;
2406	cts.transport = path->device->transport;
2407	cts.transport_version = path->device->transport_version;
2408	cts.protocol = path->device->protocol;
2409	cts.protocol_version = path->device->protocol_version;
2410	cts.proto_specific.valid = 0;
2411	cts.xport_specific.valid = 0;
2412	xpt_action((union ccb *)&cts);
2413}
2414
2415static void
2416scsi_dev_advinfo(union ccb *start_ccb)
2417{
2418	struct cam_ed *device;
2419	struct ccb_dev_advinfo *cdai;
2420	off_t amt;
2421
2422	start_ccb->ccb_h.status = CAM_REQ_INVALID;
2423	device = start_ccb->ccb_h.path->device;
2424	cdai = &start_ccb->cdai;
2425	switch(cdai->buftype) {
2426	case CDAI_TYPE_SCSI_DEVID:
2427		if (cdai->flags & CDAI_FLAG_STORE)
2428			return;
2429		cdai->provsiz = device->device_id_len;
2430		if (device->device_id_len == 0)
2431			break;
2432		amt = device->device_id_len;
2433		if (cdai->provsiz > cdai->bufsiz)
2434			amt = cdai->bufsiz;
2435		memcpy(cdai->buf, device->device_id, amt);
2436		break;
2437	case CDAI_TYPE_SERIAL_NUM:
2438		if (cdai->flags & CDAI_FLAG_STORE)
2439			return;
2440		cdai->provsiz = device->serial_num_len;
2441		if (device->serial_num_len == 0)
2442			break;
2443		amt = device->serial_num_len;
2444		if (cdai->provsiz > cdai->bufsiz)
2445			amt = cdai->bufsiz;
2446		memcpy(cdai->buf, device->serial_num, amt);
2447		break;
2448	case CDAI_TYPE_PHYS_PATH:
2449		if (cdai->flags & CDAI_FLAG_STORE) {
2450			if (device->physpath != NULL) {
2451				free(device->physpath, M_CAMXPT);
2452				device->physpath = NULL;
2453			}
2454			device->physpath_len = cdai->bufsiz;
2455			/* Clear existing buffer if zero length */
2456			if (cdai->bufsiz == 0)
2457				break;
2458			device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
2459			if (device->physpath == NULL) {
2460				start_ccb->ccb_h.status = CAM_REQ_ABORTED;
2461				return;
2462			}
2463			memcpy(device->physpath, cdai->buf, cdai->bufsiz);
2464		} else {
2465			cdai->provsiz = device->physpath_len;
2466			if (device->physpath_len == 0)
2467				break;
2468			amt = device->physpath_len;
2469			if (cdai->provsiz > cdai->bufsiz)
2470				amt = cdai->bufsiz;
2471			memcpy(cdai->buf, device->physpath, amt);
2472		}
2473		break;
2474	case CDAI_TYPE_RCAPLONG:
2475		if (cdai->flags & CDAI_FLAG_STORE) {
2476			if (device->rcap_buf != NULL) {
2477				free(device->rcap_buf, M_CAMXPT);
2478				device->rcap_buf = NULL;
2479			}
2480
2481			device->rcap_len = cdai->bufsiz;
2482			/* Clear existing buffer if zero length */
2483			if (cdai->bufsiz == 0)
2484				break;
2485
2486			device->rcap_buf = malloc(cdai->bufsiz, M_CAMXPT,
2487						  M_NOWAIT);
2488			if (device->rcap_buf == NULL) {
2489				start_ccb->ccb_h.status = CAM_REQ_ABORTED;
2490				return;
2491			}
2492
2493			memcpy(device->rcap_buf, cdai->buf, cdai->bufsiz);
2494		} else {
2495			cdai->provsiz = device->rcap_len;
2496			if (device->rcap_len == 0)
2497				break;
2498			amt = device->rcap_len;
2499			if (cdai->provsiz > cdai->bufsiz)
2500				amt = cdai->bufsiz;
2501			memcpy(cdai->buf, device->rcap_buf, amt);
2502		}
2503		break;
2504	default:
2505		return;
2506	}
2507	start_ccb->ccb_h.status = CAM_REQ_CMP;
2508
2509	if (cdai->flags & CDAI_FLAG_STORE) {
2510		xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
2511			  (void *)(uintptr_t)cdai->buftype);
2512	}
2513}
2514
2515static void
2516scsi_action(union ccb *start_ccb)
2517{
2518
2519	switch (start_ccb->ccb_h.func_code) {
2520	case XPT_SET_TRAN_SETTINGS:
2521	{
2522		scsi_set_transfer_settings(&start_ccb->cts,
2523					   start_ccb->ccb_h.path,
2524					   /*async_update*/FALSE);
2525		break;
2526	}
2527	case XPT_SCAN_BUS:
2528	case XPT_SCAN_TGT:
2529		scsi_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
2530		break;
2531	case XPT_SCAN_LUN:
2532		scsi_scan_lun(start_ccb->ccb_h.path->periph,
2533			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
2534			      start_ccb);
2535		break;
2536	case XPT_DEV_ADVINFO:
2537	{
2538		scsi_dev_advinfo(start_ccb);
2539		break;
2540	}
2541	default:
2542		xpt_action_default(start_ccb);
2543		break;
2544	}
2545}
2546
2547static void
2548scsi_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_path *path,
2549			   int async_update)
2550{
2551	struct	ccb_pathinq cpi;
2552	struct	ccb_trans_settings cur_cts;
2553	struct	ccb_trans_settings_scsi *scsi;
2554	struct	ccb_trans_settings_scsi *cur_scsi;
2555	struct	scsi_inquiry_data *inq_data;
2556	struct	cam_ed *device;
2557
2558	if (path == NULL || (device = path->device) == NULL) {
2559		cts->ccb_h.status = CAM_PATH_INVALID;
2560		xpt_done((union ccb *)cts);
2561		return;
2562	}
2563
2564	if (cts->protocol == PROTO_UNKNOWN
2565	 || cts->protocol == PROTO_UNSPECIFIED) {
2566		cts->protocol = device->protocol;
2567		cts->protocol_version = device->protocol_version;
2568	}
2569
2570	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
2571	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
2572		cts->protocol_version = device->protocol_version;
2573
2574	if (cts->protocol != device->protocol) {
2575		xpt_print(path, "Uninitialized Protocol %x:%x?\n",
2576		       cts->protocol, device->protocol);
2577		cts->protocol = device->protocol;
2578	}
2579
2580	if (cts->protocol_version > device->protocol_version) {
2581		if (bootverbose) {
2582			xpt_print(path, "Down reving Protocol "
2583			    "Version from %d to %d?\n", cts->protocol_version,
2584			    device->protocol_version);
2585		}
2586		cts->protocol_version = device->protocol_version;
2587	}
2588
2589	if (cts->transport == XPORT_UNKNOWN
2590	 || cts->transport == XPORT_UNSPECIFIED) {
2591		cts->transport = device->transport;
2592		cts->transport_version = device->transport_version;
2593	}
2594
2595	if (cts->transport_version == XPORT_VERSION_UNKNOWN
2596	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
2597		cts->transport_version = device->transport_version;
2598
2599	if (cts->transport != device->transport) {
2600		xpt_print(path, "Uninitialized Transport %x:%x?\n",
2601		    cts->transport, device->transport);
2602		cts->transport = device->transport;
2603	}
2604
2605	if (cts->transport_version > device->transport_version) {
2606		if (bootverbose) {
2607			xpt_print(path, "Down reving Transport "
2608			    "Version from %d to %d?\n", cts->transport_version,
2609			    device->transport_version);
2610		}
2611		cts->transport_version = device->transport_version;
2612	}
2613
2614	/*
2615	 * Nothing more of interest to do unless
2616	 * this is a device connected via the
2617	 * SCSI protocol.
2618	 */
2619	if (cts->protocol != PROTO_SCSI) {
2620		if (async_update == FALSE)
2621			xpt_action_default((union ccb *)cts);
2622		return;
2623	}
2624
2625	inq_data = &device->inq_data;
2626	scsi = &cts->proto_specific.scsi;
2627	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2628	cpi.ccb_h.func_code = XPT_PATH_INQ;
2629	xpt_action((union ccb *)&cpi);
2630
2631	/* SCSI specific sanity checking */
2632	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
2633	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
2634	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
2635	 || (device->mintags == 0)) {
2636		/*
2637		 * Can't tag on hardware that doesn't support tags,
2638		 * doesn't have it enabled, or has broken tag support.
2639		 */
2640		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2641	}
2642
2643	if (async_update == FALSE) {
2644		/*
2645		 * Perform sanity checking against what the
2646		 * controller and device can do.
2647		 */
2648		xpt_setup_ccb(&cur_cts.ccb_h, path, CAM_PRIORITY_NONE);
2649		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2650		cur_cts.type = cts->type;
2651		xpt_action((union ccb *)&cur_cts);
2652		if (cam_ccb_status((union ccb *)&cur_cts) != CAM_REQ_CMP) {
2653			return;
2654		}
2655		cur_scsi = &cur_cts.proto_specific.scsi;
2656		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
2657			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2658			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
2659		}
2660		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
2661			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2662	}
2663
2664	/* SPI specific sanity checking */
2665	if (cts->transport == XPORT_SPI && async_update == FALSE) {
2666		u_int spi3caps;
2667		struct ccb_trans_settings_spi *spi;
2668		struct ccb_trans_settings_spi *cur_spi;
2669
2670		spi = &cts->xport_specific.spi;
2671
2672		cur_spi = &cur_cts.xport_specific.spi;
2673
2674		/* Fill in any gaps in what the user gave us */
2675		if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2676			spi->sync_period = cur_spi->sync_period;
2677		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2678			spi->sync_period = 0;
2679		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2680			spi->sync_offset = cur_spi->sync_offset;
2681		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2682			spi->sync_offset = 0;
2683		if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2684			spi->ppr_options = cur_spi->ppr_options;
2685		if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2686			spi->ppr_options = 0;
2687		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2688			spi->bus_width = cur_spi->bus_width;
2689		if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2690			spi->bus_width = 0;
2691		if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
2692			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2693			spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
2694		}
2695		if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
2696			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2697		if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2698		  && (inq_data->flags & SID_Sync) == 0
2699		  && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2700		 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)) {
2701			/* Force async */
2702			spi->sync_period = 0;
2703			spi->sync_offset = 0;
2704		}
2705
2706		switch (spi->bus_width) {
2707		case MSG_EXT_WDTR_BUS_32_BIT:
2708			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2709			  || (inq_data->flags & SID_WBus32) != 0
2710			  || cts->type == CTS_TYPE_USER_SETTINGS)
2711			 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
2712				break;
2713			/* Fall Through to 16-bit */
2714		case MSG_EXT_WDTR_BUS_16_BIT:
2715			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2716			  || (inq_data->flags & SID_WBus16) != 0
2717			  || cts->type == CTS_TYPE_USER_SETTINGS)
2718			 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
2719				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2720				break;
2721			}
2722			/* Fall Through to 8-bit */
2723		default: /* New bus width?? */
2724		case MSG_EXT_WDTR_BUS_8_BIT:
2725			/* All targets can do this */
2726			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2727			break;
2728		}
2729
2730		spi3caps = cpi.xport_specific.spi.ppr_options;
2731		if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2732		 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2733			spi3caps &= inq_data->spi3data;
2734
2735		if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
2736			spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2737
2738		if ((spi3caps & SID_SPI_IUS) == 0)
2739			spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
2740
2741		if ((spi3caps & SID_SPI_QAS) == 0)
2742			spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
2743
2744		/* No SPI Transfer settings are allowed unless we are wide */
2745		if (spi->bus_width == 0)
2746			spi->ppr_options = 0;
2747
2748		if ((spi->valid & CTS_SPI_VALID_DISC)
2749		 && ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0)) {
2750			/*
2751			 * Can't tag queue without disconnection.
2752			 */
2753			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2754			scsi->valid |= CTS_SCSI_VALID_TQ;
2755		}
2756
2757		/*
2758		 * If we are currently performing tagged transactions to
2759		 * this device and want to change its negotiation parameters,
2760		 * go non-tagged for a bit to give the controller a chance to
2761		 * negotiate unhampered by tag messages.
2762		 */
2763		if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2764		 && (device->inq_flags & SID_CmdQue) != 0
2765		 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2766		 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
2767				   CTS_SPI_VALID_SYNC_OFFSET|
2768				   CTS_SPI_VALID_BUS_WIDTH)) != 0)
2769			scsi_toggle_tags(path);
2770	}
2771
2772	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2773	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
2774		int device_tagenb;
2775
2776		/*
2777		 * If we are transitioning from tags to no-tags or
2778		 * vice-versa, we need to carefully freeze and restart
2779		 * the queue so that we don't overlap tagged and non-tagged
2780		 * commands.  We also temporarily stop tags if there is
2781		 * a change in transfer negotiation settings to allow
2782		 * "tag-less" negotiation.
2783		 */
2784		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2785		 || (device->inq_flags & SID_CmdQue) != 0)
2786			device_tagenb = TRUE;
2787		else
2788			device_tagenb = FALSE;
2789
2790		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2791		  && device_tagenb == FALSE)
2792		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
2793		  && device_tagenb == TRUE)) {
2794
2795			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
2796				/*
2797				 * Delay change to use tags until after a
2798				 * few commands have gone to this device so
2799				 * the controller has time to perform transfer
2800				 * negotiations without tagged messages getting
2801				 * in the way.
2802				 */
2803				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
2804				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
2805			} else {
2806				xpt_stop_tags(path);
2807			}
2808		}
2809	}
2810	if (async_update == FALSE)
2811		xpt_action_default((union ccb *)cts);
2812}
2813
2814static void
2815scsi_toggle_tags(struct cam_path *path)
2816{
2817	struct cam_ed *dev;
2818
2819	/*
2820	 * Give controllers a chance to renegotiate
2821	 * before starting tag operations.  We
2822	 * "toggle" tagged queuing off then on
2823	 * which causes the tag enable command delay
2824	 * counter to come into effect.
2825	 */
2826	dev = path->device;
2827	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2828	 || ((dev->inq_flags & SID_CmdQue) != 0
2829 	  && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
2830		struct ccb_trans_settings cts;
2831
2832		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2833		cts.protocol = PROTO_SCSI;
2834		cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
2835		cts.transport = XPORT_UNSPECIFIED;
2836		cts.transport_version = XPORT_VERSION_UNSPECIFIED;
2837		cts.proto_specific.scsi.flags = 0;
2838		cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
2839		scsi_set_transfer_settings(&cts, path,
2840					  /*async_update*/TRUE);
2841		cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
2842		scsi_set_transfer_settings(&cts, path,
2843					  /*async_update*/TRUE);
2844	}
2845}
2846
2847/*
2848 * Handle any per-device event notifications that require action by the XPT.
2849 */
2850static void
2851scsi_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
2852	      struct cam_ed *device, void *async_arg)
2853{
2854	cam_status status;
2855	struct cam_path newpath;
2856
2857	/*
2858	 * We only need to handle events for real devices.
2859	 */
2860	if (target->target_id == CAM_TARGET_WILDCARD
2861	 || device->lun_id == CAM_LUN_WILDCARD)
2862		return;
2863
2864	/*
2865	 * We need our own path with wildcards expanded to
2866	 * handle certain types of events.
2867	 */
2868	if ((async_code == AC_SENT_BDR)
2869	 || (async_code == AC_BUS_RESET)
2870	 || (async_code == AC_INQ_CHANGED))
2871		status = xpt_compile_path(&newpath, NULL,
2872					  bus->path_id,
2873					  target->target_id,
2874					  device->lun_id);
2875	else
2876		status = CAM_REQ_CMP_ERR;
2877
2878	if (status == CAM_REQ_CMP) {
2879
2880		/*
2881		 * Allow transfer negotiation to occur in a
2882		 * tag free environment and after settle delay.
2883		 */
2884		if (async_code == AC_SENT_BDR
2885		 || async_code == AC_BUS_RESET) {
2886			cam_freeze_devq(&newpath);
2887			cam_release_devq(&newpath,
2888				RELSIM_RELEASE_AFTER_TIMEOUT,
2889				/*reduction*/0,
2890				/*timeout*/scsi_delay,
2891				/*getcount_only*/0);
2892			scsi_toggle_tags(&newpath);
2893		}
2894
2895		if (async_code == AC_INQ_CHANGED) {
2896			/*
2897			 * We've sent a start unit command, or
2898			 * something similar to a device that
2899			 * may have caused its inquiry data to
2900			 * change. So we re-scan the device to
2901			 * refresh the inquiry data for it.
2902			 */
2903			scsi_scan_lun(newpath.periph, &newpath,
2904				     CAM_EXPECT_INQ_CHANGE, NULL);
2905		}
2906		xpt_release_path(&newpath);
2907	} else if (async_code == AC_LOST_DEVICE &&
2908	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
2909		device->flags |= CAM_DEV_UNCONFIGURED;
2910		xpt_release_device(device);
2911	} else if (async_code == AC_TRANSFER_NEG) {
2912		struct ccb_trans_settings *settings;
2913		struct cam_path path;
2914
2915		settings = (struct ccb_trans_settings *)async_arg;
2916		xpt_compile_path(&path, NULL, bus->path_id, target->target_id,
2917				 device->lun_id);
2918		scsi_set_transfer_settings(settings, &path,
2919					  /*async_update*/TRUE);
2920		xpt_release_path(&path);
2921	}
2922}
2923
2924static void
2925scsi_announce_periph(struct cam_periph *periph)
2926{
2927	struct	ccb_pathinq cpi;
2928	struct	ccb_trans_settings cts;
2929	struct	cam_path *path = periph->path;
2930	u_int	speed;
2931	u_int	freq;
2932	u_int	mb;
2933
2934	cam_periph_assert(periph, MA_OWNED);
2935
2936	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
2937	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2938	cts.type = CTS_TYPE_CURRENT_SETTINGS;
2939	xpt_action((union ccb*)&cts);
2940	if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP)
2941		return;
2942	/* Ask the SIM for its base transfer speed */
2943	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
2944	cpi.ccb_h.func_code = XPT_PATH_INQ;
2945	xpt_action((union ccb *)&cpi);
2946	/* Report connection speed */
2947	speed = cpi.base_transfer_speed;
2948	freq = 0;
2949	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
2950		struct	ccb_trans_settings_spi *spi =
2951		    &cts.xport_specific.spi;
2952
2953		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
2954		  && spi->sync_offset != 0) {
2955			freq = scsi_calc_syncsrate(spi->sync_period);
2956			speed = freq;
2957		}
2958		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
2959			speed *= (0x01 << spi->bus_width);
2960	}
2961	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
2962		struct	ccb_trans_settings_fc *fc =
2963		    &cts.xport_specific.fc;
2964
2965		if (fc->valid & CTS_FC_VALID_SPEED)
2966			speed = fc->bitrate;
2967	}
2968	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SAS) {
2969		struct	ccb_trans_settings_sas *sas =
2970		    &cts.xport_specific.sas;
2971
2972		if (sas->valid & CTS_SAS_VALID_SPEED)
2973			speed = sas->bitrate;
2974	}
2975	mb = speed / 1000;
2976	if (mb > 0)
2977		printf("%s%d: %d.%03dMB/s transfers",
2978		       periph->periph_name, periph->unit_number,
2979		       mb, speed % 1000);
2980	else
2981		printf("%s%d: %dKB/s transfers", periph->periph_name,
2982		       periph->unit_number, speed);
2983	/* Report additional information about SPI connections */
2984	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
2985		struct	ccb_trans_settings_spi *spi;
2986
2987		spi = &cts.xport_specific.spi;
2988		if (freq != 0) {
2989			printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
2990			       freq % 1000,
2991			       (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
2992			     ? " DT" : "",
2993			       spi->sync_offset);
2994		}
2995		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
2996		 && spi->bus_width > 0) {
2997			if (freq != 0) {
2998				printf(", ");
2999			} else {
3000				printf(" (");
3001			}
3002			printf("%dbit)", 8 * (0x01 << spi->bus_width));
3003		} else if (freq != 0) {
3004			printf(")");
3005		}
3006	}
3007	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
3008		struct	ccb_trans_settings_fc *fc;
3009
3010		fc = &cts.xport_specific.fc;
3011		if (fc->valid & CTS_FC_VALID_WWNN)
3012			printf(" WWNN 0x%llx", (long long) fc->wwnn);
3013		if (fc->valid & CTS_FC_VALID_WWPN)
3014			printf(" WWPN 0x%llx", (long long) fc->wwpn);
3015		if (fc->valid & CTS_FC_VALID_PORT)
3016			printf(" PortID 0x%x", fc->port);
3017	}
3018	printf("\n");
3019}
3020
3021