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