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