scsi_xpt.c revision 350783
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 350783 2019-08-08 21:30:44Z 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_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
701	cpi.ccb_h.func_code = XPT_PATH_INQ;
702	xpt_action((union ccb *)&cpi);
703
704	/*
705	 * If a device has gone away and another device, or the same one,
706	 * is back in the same place, it should have a unit attention
707	 * condition pending.  It will not report the unit attention in
708	 * response to an inquiry, which may leave invalid transfer
709	 * negotiations in effect.  The TUR will reveal the unit attention
710	 * condition.  Only send the TUR for lun 0, since some devices
711	 * will get confused by commands other than inquiry to non-existent
712	 * luns.  If you think a device has gone away start your scan from
713	 * lun 0.  This will insure that any bogus transfer settings are
714	 * invalidated.
715	 *
716	 * If we haven't seen the device before and the controller supports
717	 * some kind of transfer negotiation, negotiate with the first
718	 * sent command if no bus reset was performed at startup.  This
719	 * ensures that the device is not confused by transfer negotiation
720	 * settings left over by loader or BIOS action.
721	 */
722	if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
723	 && (ccb->ccb_h.target_lun == 0)) {
724		PROBE_SET_ACTION(softc, PROBE_TUR);
725	} else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
726	      && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
727		proberequestdefaultnegotiation(periph);
728		PROBE_SET_ACTION(softc, PROBE_INQUIRY);
729	} else {
730		PROBE_SET_ACTION(softc, PROBE_INQUIRY);
731	}
732
733	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
734		softc->flags |= PROBE_NO_ANNOUNCE;
735	else
736		softc->flags &= ~PROBE_NO_ANNOUNCE;
737
738	if (cpi.hba_misc & PIM_EXTLUNS)
739		softc->flags |= PROBE_EXTLUN;
740	else
741		softc->flags &= ~PROBE_EXTLUN;
742
743	xpt_schedule(periph, CAM_PRIORITY_XPT);
744}
745
746static void
747probestart(struct cam_periph *periph, union ccb *start_ccb)
748{
749	/* Probe the device that our peripheral driver points to */
750	struct ccb_scsiio *csio;
751	probe_softc *softc;
752
753	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
754
755	softc = (probe_softc *)periph->softc;
756	csio = &start_ccb->csio;
757again:
758
759	switch (softc->action) {
760	case PROBE_TUR:
761	case PROBE_TUR_FOR_NEGOTIATION:
762	case PROBE_DV_EXIT:
763	{
764		scsi_test_unit_ready(csio,
765				     /*retries*/4,
766				     probedone,
767				     MSG_SIMPLE_Q_TAG,
768				     SSD_FULL_SIZE,
769				     /*timeout*/60000);
770		break;
771	}
772	case PROBE_INQUIRY:
773	case PROBE_FULL_INQUIRY:
774	case PROBE_INQUIRY_BASIC_DV1:
775	case PROBE_INQUIRY_BASIC_DV2:
776	{
777		u_int inquiry_len;
778		struct scsi_inquiry_data *inq_buf;
779
780		inq_buf = &periph->path->device->inq_data;
781
782		/*
783		 * If the device is currently configured, we calculate an
784		 * MD5 checksum of the inquiry data, and if the serial number
785		 * length is greater than 0, add the serial number data
786		 * into the checksum as well.  Once the inquiry and the
787		 * serial number check finish, we attempt to figure out
788		 * whether we still have the same device.
789		 */
790		if (((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
791		 && ((softc->flags & PROBE_INQUIRY_CKSUM) == 0)) {
792
793			MD5Init(&softc->context);
794			MD5Update(&softc->context, (unsigned char *)inq_buf,
795				  sizeof(struct scsi_inquiry_data));
796			softc->flags |= PROBE_INQUIRY_CKSUM;
797			if (periph->path->device->serial_num_len > 0) {
798				MD5Update(&softc->context,
799					  periph->path->device->serial_num,
800					  periph->path->device->serial_num_len);
801				softc->flags |= PROBE_SERIAL_CKSUM;
802			}
803			MD5Final(softc->digest, &softc->context);
804		}
805
806		if (softc->action == PROBE_INQUIRY)
807			inquiry_len = SHORT_INQUIRY_LENGTH;
808		else
809			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
810
811		/*
812		 * Some parallel SCSI devices fail to send an
813		 * ignore wide residue message when dealing with
814		 * odd length inquiry requests.  Round up to be
815		 * safe.
816		 */
817		inquiry_len = roundup2(inquiry_len, 2);
818
819		if (softc->action == PROBE_INQUIRY_BASIC_DV1
820		 || softc->action == PROBE_INQUIRY_BASIC_DV2) {
821			inq_buf = malloc(inquiry_len, M_CAMXPT, M_NOWAIT);
822		}
823		if (inq_buf == NULL) {
824			xpt_print(periph->path, "malloc failure- skipping Basic"
825			    "Domain Validation\n");
826			PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
827			scsi_test_unit_ready(csio,
828					     /*retries*/4,
829					     probedone,
830					     MSG_SIMPLE_Q_TAG,
831					     SSD_FULL_SIZE,
832					     /*timeout*/60000);
833			break;
834		}
835		scsi_inquiry(csio,
836			     /*retries*/4,
837			     probedone,
838			     MSG_SIMPLE_Q_TAG,
839			     (u_int8_t *)inq_buf,
840			     inquiry_len,
841			     /*evpd*/FALSE,
842			     /*page_code*/0,
843			     SSD_MIN_SIZE,
844			     /*timeout*/60 * 1000);
845		break;
846	}
847	case PROBE_REPORT_LUNS:
848	{
849		void *rp;
850
851		rp = malloc(periph->path->target->rpl_size,
852		    M_CAMXPT, M_NOWAIT | M_ZERO);
853		if (rp == NULL) {
854			struct scsi_inquiry_data *inq_buf;
855			inq_buf = &periph->path->device->inq_data;
856			xpt_print(periph->path,
857			    "Unable to alloc report luns storage\n");
858			if (INQ_DATA_TQ_ENABLED(inq_buf))
859				PROBE_SET_ACTION(softc, PROBE_MODE_SENSE);
860			else
861				PROBE_SET_ACTION(softc,
862				    PROBE_SUPPORTED_VPD_LIST);
863			goto again;
864		}
865		scsi_report_luns(csio, 5, probedone, MSG_SIMPLE_Q_TAG,
866		    RPL_REPORT_DEFAULT, rp, periph->path->target->rpl_size,
867		    SSD_FULL_SIZE, 60000); break;
868		break;
869	}
870	case PROBE_MODE_SENSE:
871	{
872		void  *mode_buf;
873		int    mode_buf_len;
874
875		mode_buf_len = sizeof(struct scsi_mode_header_6)
876			     + sizeof(struct scsi_mode_blk_desc)
877			     + sizeof(struct scsi_control_page);
878		mode_buf = malloc(mode_buf_len, M_CAMXPT, M_NOWAIT);
879		if (mode_buf != NULL) {
880	                scsi_mode_sense(csio,
881					/*retries*/4,
882					probedone,
883					MSG_SIMPLE_Q_TAG,
884					/*dbd*/FALSE,
885					SMS_PAGE_CTRL_CURRENT,
886					SMS_CONTROL_MODE_PAGE,
887					mode_buf,
888					mode_buf_len,
889					SSD_FULL_SIZE,
890					/*timeout*/60000);
891			break;
892		}
893		xpt_print(periph->path, "Unable to mode sense control page - "
894		    "malloc failure\n");
895		PROBE_SET_ACTION(softc, PROBE_SUPPORTED_VPD_LIST);
896	}
897	/* FALLTHROUGH */
898	case PROBE_SUPPORTED_VPD_LIST:
899	{
900		struct scsi_vpd_supported_page_list *vpd_list;
901		struct cam_ed *device;
902
903		vpd_list = NULL;
904		device = periph->path->device;
905
906		if ((SCSI_QUIRK(device)->quirks & CAM_QUIRK_NOVPDS) == 0)
907			vpd_list = malloc(sizeof(*vpd_list), M_CAMXPT,
908			    M_NOWAIT | M_ZERO);
909
910		if (vpd_list != NULL) {
911			scsi_inquiry(csio,
912				     /*retries*/4,
913				     probedone,
914				     MSG_SIMPLE_Q_TAG,
915				     (u_int8_t *)vpd_list,
916				     sizeof(*vpd_list),
917				     /*evpd*/TRUE,
918				     SVPD_SUPPORTED_PAGE_LIST,
919				     SSD_MIN_SIZE,
920				     /*timeout*/60 * 1000);
921			break;
922		}
923done:
924		/*
925		 * We'll have to do without, let our probedone
926		 * routine finish up for us.
927		 */
928		start_ccb->csio.data_ptr = NULL;
929		cam_freeze_devq(periph->path);
930		cam_periph_doacquire(periph);
931		probedone(periph, start_ccb);
932		return;
933	}
934	case PROBE_DEVICE_ID:
935	{
936		struct scsi_vpd_device_id *devid;
937
938		devid = NULL;
939		if (scsi_vpd_supported_page(periph, SVPD_DEVICE_ID))
940			devid = malloc(SVPD_DEVICE_ID_MAX_SIZE, M_CAMXPT,
941			    M_NOWAIT | M_ZERO);
942
943		if (devid != NULL) {
944			scsi_inquiry(csio,
945				     /*retries*/4,
946				     probedone,
947				     MSG_SIMPLE_Q_TAG,
948				     (uint8_t *)devid,
949				     SVPD_DEVICE_ID_MAX_SIZE,
950				     /*evpd*/TRUE,
951				     SVPD_DEVICE_ID,
952				     SSD_MIN_SIZE,
953				     /*timeout*/60 * 1000);
954			break;
955		}
956		goto done;
957	}
958	case PROBE_EXTENDED_INQUIRY:
959	{
960		struct scsi_vpd_extended_inquiry_data *ext_inq;
961
962		ext_inq = NULL;
963		if (scsi_vpd_supported_page(periph, SVPD_EXTENDED_INQUIRY_DATA))
964			ext_inq = malloc(sizeof(*ext_inq), M_CAMXPT,
965			    M_NOWAIT | M_ZERO);
966
967		if (ext_inq != NULL) {
968			scsi_inquiry(csio,
969				     /*retries*/4,
970				     probedone,
971				     MSG_SIMPLE_Q_TAG,
972				     (uint8_t *)ext_inq,
973				     sizeof(*ext_inq),
974				     /*evpd*/TRUE,
975				     SVPD_EXTENDED_INQUIRY_DATA,
976				     SSD_MIN_SIZE,
977				     /*timeout*/60 * 1000);
978			break;
979		}
980		/*
981		 * We'll have to do without, let our probedone
982		 * routine finish up for us.
983		 */
984		goto done;
985	}
986	case PROBE_SERIAL_NUM:
987	{
988		struct scsi_vpd_unit_serial_number *serial_buf;
989		struct cam_ed* device;
990
991		serial_buf = NULL;
992		device = periph->path->device;
993		if (device->serial_num != NULL) {
994			free(device->serial_num, M_CAMXPT);
995			device->serial_num = NULL;
996			device->serial_num_len = 0;
997		}
998
999		if (scsi_vpd_supported_page(periph, SVPD_UNIT_SERIAL_NUMBER))
1000			serial_buf = (struct scsi_vpd_unit_serial_number *)
1001				malloc(sizeof(*serial_buf), M_CAMXPT,
1002				    M_NOWAIT|M_ZERO);
1003
1004		if (serial_buf != NULL) {
1005			scsi_inquiry(csio,
1006				     /*retries*/4,
1007				     probedone,
1008				     MSG_SIMPLE_Q_TAG,
1009				     (u_int8_t *)serial_buf,
1010				     sizeof(*serial_buf),
1011				     /*evpd*/TRUE,
1012				     SVPD_UNIT_SERIAL_NUMBER,
1013				     SSD_MIN_SIZE,
1014				     /*timeout*/60 * 1000);
1015			break;
1016		}
1017		goto done;
1018	}
1019	default:
1020		panic("probestart: invalid action state 0x%x\n", softc->action);
1021	}
1022	start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1023	cam_periph_doacquire(periph);
1024	xpt_action(start_ccb);
1025}
1026
1027static void
1028proberequestdefaultnegotiation(struct cam_periph *periph)
1029{
1030	struct ccb_trans_settings cts;
1031
1032	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1033	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1034	cts.type = CTS_TYPE_USER_SETTINGS;
1035	xpt_action((union ccb *)&cts);
1036	if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP) {
1037		return;
1038	}
1039	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1040	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1041	xpt_action((union ccb *)&cts);
1042}
1043
1044/*
1045 * Backoff Negotiation Code- only pertinent for SPI devices.
1046 */
1047static int
1048proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
1049{
1050	struct ccb_trans_settings cts;
1051	struct ccb_trans_settings_spi *spi;
1052
1053	memset(&cts, 0, sizeof (cts));
1054	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1055	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1056	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1057	xpt_action((union ccb *)&cts);
1058	if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP) {
1059		if (bootverbose) {
1060			xpt_print(periph->path,
1061			    "failed to get current device settings\n");
1062		}
1063		return (0);
1064	}
1065	if (cts.transport != XPORT_SPI) {
1066		if (bootverbose) {
1067			xpt_print(periph->path, "not SPI transport\n");
1068		}
1069		return (0);
1070	}
1071	spi = &cts.xport_specific.spi;
1072
1073	/*
1074	 * We cannot renegotiate sync rate if we don't have one.
1075	 */
1076	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
1077		if (bootverbose) {
1078			xpt_print(periph->path, "no sync rate known\n");
1079		}
1080		return (0);
1081	}
1082
1083	/*
1084	 * We'll assert that we don't have to touch PPR options- the
1085	 * SIM will see what we do with period and offset and adjust
1086	 * the PPR options as appropriate.
1087	 */
1088
1089	/*
1090	 * A sync rate with unknown or zero offset is nonsensical.
1091	 * A sync period of zero means Async.
1092	 */
1093	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
1094	 || spi->sync_offset == 0 || spi->sync_period == 0) {
1095		if (bootverbose) {
1096			xpt_print(periph->path, "no sync rate available\n");
1097		}
1098		return (0);
1099	}
1100
1101	if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
1102		CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1103		    ("hit async: giving up on DV\n"));
1104		return (0);
1105	}
1106
1107
1108	/*
1109	 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
1110	 * We don't try to remember 'last' settings to see if the SIM actually
1111	 * gets into the speed we want to set. We check on the SIM telling
1112	 * us that a requested speed is bad, but otherwise don't try and
1113	 * check the speed due to the asynchronous and handshake nature
1114	 * of speed setting.
1115	 */
1116	spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
1117	for (;;) {
1118		spi->sync_period++;
1119		if (spi->sync_period >= 0xf) {
1120			spi->sync_period = 0;
1121			spi->sync_offset = 0;
1122			CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1123			    ("setting to async for DV\n"));
1124			/*
1125			 * Once we hit async, we don't want to try
1126			 * any more settings.
1127			 */
1128			device->flags |= CAM_DEV_DV_HIT_BOTTOM;
1129		} else if (bootverbose) {
1130			CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1131			    ("DV: period 0x%x\n", spi->sync_period));
1132			printf("setting period to 0x%x\n", spi->sync_period);
1133		}
1134		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1135		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1136		xpt_action((union ccb *)&cts);
1137		if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP) {
1138			break;
1139		}
1140		CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1141		    ("DV: failed to set period 0x%x\n", spi->sync_period));
1142		if (spi->sync_period == 0) {
1143			return (0);
1144		}
1145	}
1146	return (1);
1147}
1148
1149#define CCB_COMPLETED_OK(ccb) (((ccb).status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1150
1151static void
1152probedone(struct cam_periph *periph, union ccb *done_ccb)
1153{
1154	probe_softc *softc;
1155	struct cam_path *path;
1156	struct scsi_inquiry_data *inq_buf;
1157	u_int32_t  priority;
1158
1159	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
1160
1161	softc = (probe_softc *)periph->softc;
1162	path = done_ccb->ccb_h.path;
1163	priority = done_ccb->ccb_h.pinfo.priority;
1164	cam_periph_assert(periph, MA_OWNED);
1165
1166	switch (softc->action) {
1167	case PROBE_TUR:
1168	{
1169		if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1170
1171			if (cam_periph_error(done_ccb, 0,
1172					     SF_NO_PRINT, NULL) == ERESTART) {
1173outr:
1174				/* Drop freeze taken due to CAM_DEV_QFREEZE */
1175				cam_release_devq(path, 0, 0, 0, FALSE);
1176				return;
1177			}
1178			else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1179				/* Don't wedge the queue */
1180				xpt_release_devq(done_ccb->ccb_h.path,
1181						 /*count*/1,
1182						 /*run_queue*/TRUE);
1183		}
1184		PROBE_SET_ACTION(softc, PROBE_INQUIRY);
1185		xpt_release_ccb(done_ccb);
1186		xpt_schedule(periph, priority);
1187out:
1188		/* Drop freeze taken due to CAM_DEV_QFREEZE and release. */
1189		cam_release_devq(path, 0, 0, 0, FALSE);
1190		cam_periph_release_locked(periph);
1191		return;
1192	}
1193	case PROBE_INQUIRY:
1194	case PROBE_FULL_INQUIRY:
1195	{
1196		if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
1197			u_int8_t periph_qual;
1198
1199			path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1200			scsi_find_quirk(path->device);
1201			inq_buf = &path->device->inq_data;
1202
1203			periph_qual = SID_QUAL(inq_buf);
1204
1205			if (periph_qual == SID_QUAL_LU_CONNECTED ||
1206			    periph_qual == SID_QUAL_LU_OFFLINE) {
1207				u_int8_t len;
1208
1209				/*
1210				 * We conservatively request only
1211				 * SHORT_INQUIRY_LEN bytes of inquiry
1212				 * information during our first try
1213				 * at sending an INQUIRY. If the device
1214				 * has more information to give,
1215				 * perform a second request specifying
1216				 * the amount of information the device
1217				 * is willing to give.
1218				 */
1219				len = inq_buf->additional_length
1220				    + offsetof(struct scsi_inquiry_data,
1221                                               additional_length) + 1;
1222				if (softc->action == PROBE_INQUIRY
1223				    && len > SHORT_INQUIRY_LENGTH) {
1224					PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1225					xpt_release_ccb(done_ccb);
1226					xpt_schedule(periph, priority);
1227					goto out;
1228				}
1229
1230				scsi_devise_transport(path);
1231
1232				if (path->device->lun_id == 0 &&
1233				    SID_ANSI_REV(inq_buf) > SCSI_REV_SPC2 &&
1234				    (SCSI_QUIRK(path->device)->quirks &
1235				     CAM_QUIRK_NORPTLUNS) == 0) {
1236					PROBE_SET_ACTION(softc,
1237					    PROBE_REPORT_LUNS);
1238					/*
1239					 * Start with room for *one* lun.
1240					 */
1241					periph->path->target->rpl_size = 16;
1242				} else if (INQ_DATA_TQ_ENABLED(inq_buf))
1243					PROBE_SET_ACTION(softc,
1244					    PROBE_MODE_SENSE);
1245				else
1246					PROBE_SET_ACTION(softc,
1247					    PROBE_SUPPORTED_VPD_LIST);
1248
1249				if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1250					path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1251					xpt_acquire_device(path->device);
1252				}
1253				xpt_release_ccb(done_ccb);
1254				xpt_schedule(periph, priority);
1255				goto out;
1256			} else if (path->device->lun_id == 0 &&
1257			    SID_ANSI_REV(inq_buf) >= SCSI_REV_SPC2 &&
1258			    (SCSI_QUIRK(path->device)->quirks &
1259			     CAM_QUIRK_NORPTLUNS) == 0) {
1260				PROBE_SET_ACTION(softc, PROBE_REPORT_LUNS);
1261				periph->path->target->rpl_size = 16;
1262				xpt_release_ccb(done_ccb);
1263				xpt_schedule(periph, priority);
1264				goto out;
1265			}
1266		} else if (cam_periph_error(done_ccb, 0,
1267					    done_ccb->ccb_h.target_lun > 0
1268					    ? SF_RETRY_UA|SF_QUIET_IR
1269					    : SF_RETRY_UA,
1270					    &softc->saved_ccb) == ERESTART) {
1271			goto outr;
1272		} else {
1273			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1274				/* Don't wedge the queue */
1275				xpt_release_devq(done_ccb->ccb_h.path,
1276				    /*count*/1, /*run_queue*/TRUE);
1277			}
1278			path->device->flags &= ~CAM_DEV_INQUIRY_DATA_VALID;
1279		}
1280		/*
1281		 * If we get to this point, we got an error status back
1282		 * from the inquiry and the error status doesn't require
1283		 * automatically retrying the command.  Therefore, the
1284		 * inquiry failed.  If we had inquiry information before
1285		 * for this device, but this latest inquiry command failed,
1286		 * the device has probably gone away.  If this device isn't
1287		 * already marked unconfigured, notify the peripheral
1288		 * drivers that this device is no more.
1289		 */
1290		if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
1291			/* Send the async notification. */
1292			xpt_async(AC_LOST_DEVICE, path, NULL);
1293		PROBE_SET_ACTION(softc, PROBE_INVALID);
1294
1295		xpt_release_ccb(done_ccb);
1296		break;
1297	}
1298	case PROBE_REPORT_LUNS:
1299	{
1300		struct ccb_scsiio *csio;
1301		struct scsi_report_luns_data *lp;
1302		u_int nlun, maxlun;
1303
1304		csio = &done_ccb->csio;
1305
1306		lp = (struct scsi_report_luns_data *)csio->data_ptr;
1307		nlun = scsi_4btoul(lp->length) / 8;
1308		maxlun = (csio->dxfer_len / 8) - 1;
1309
1310		if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1311			if (cam_periph_error(done_ccb, 0,
1312			    done_ccb->ccb_h.target_lun > 0 ?
1313			    SF_RETRY_UA|SF_QUIET_IR : SF_RETRY_UA,
1314			    &softc->saved_ccb) == ERESTART) {
1315				goto outr;
1316			}
1317			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1318				xpt_release_devq(done_ccb->ccb_h.path, 1,
1319				    TRUE);
1320			}
1321			free(lp, M_CAMXPT);
1322			lp = NULL;
1323		} else if (nlun > maxlun) {
1324			/*
1325			 * Reallocate and retry to cover all luns
1326			 */
1327			CAM_DEBUG(path, CAM_DEBUG_PROBE,
1328			    ("Probe: reallocating REPORT_LUNS for %u luns\n",
1329			     nlun));
1330			free(lp, M_CAMXPT);
1331			path->target->rpl_size = (nlun << 3) + 8;
1332			xpt_release_ccb(done_ccb);
1333			xpt_schedule(periph, priority);
1334			goto out;
1335		} else if (nlun == 0) {
1336			/*
1337			 * If there don't appear to be any luns, bail.
1338			 */
1339			free(lp, M_CAMXPT);
1340			lp = NULL;
1341		} else {
1342			lun_id_t lun;
1343			int idx;
1344
1345			CAM_DEBUG(path, CAM_DEBUG_PROBE,
1346			   ("Probe: %u lun(s) reported\n", nlun));
1347
1348			CAM_GET_LUN(lp, 0, lun);
1349			/*
1350			 * If the first lun is not lun 0, then either there
1351			 * is no lun 0 in the list, or the list is unsorted.
1352			 */
1353			if (lun != 0) {
1354				for (idx = 0; idx < nlun; idx++) {
1355					CAM_GET_LUN(lp, idx, lun);
1356					if (lun == 0) {
1357						break;
1358					}
1359				}
1360				if (idx != nlun) {
1361					uint8_t tlun[8];
1362					memcpy(tlun,
1363					    lp->luns[0].lundata, 8);
1364					memcpy(lp->luns[0].lundata,
1365					    lp->luns[idx].lundata, 8);
1366					memcpy(lp->luns[idx].lundata,
1367					    tlun, 8);
1368					CAM_DEBUG(path, CAM_DEBUG_PROBE,
1369					    ("lun 0 in position %u\n", idx));
1370				}
1371			}
1372			/*
1373			 * If we have an old lun list, We can either
1374			 * retest luns that appear to have been dropped,
1375			 * or just nuke them.  We'll opt for the latter.
1376			 * This function will also install the new list
1377			 * in the target structure.
1378			 */
1379			probe_purge_old(path, lp, softc->flags);
1380			lp = NULL;
1381		}
1382		/* The processing above should either exit via a `goto
1383		 * out` or leave the `lp` variable `NULL` and (if
1384		 * applicable) `free()` the storage to which it had
1385		 * pointed. Assert here that is the case.
1386		 */
1387		KASSERT(lp == NULL, ("%s: lp is not NULL", __func__));
1388		inq_buf = &path->device->inq_data;
1389		if (path->device->flags & CAM_DEV_INQUIRY_DATA_VALID &&
1390		    (SID_QUAL(inq_buf) == SID_QUAL_LU_CONNECTED ||
1391		    SID_QUAL(inq_buf) == SID_QUAL_LU_OFFLINE)) {
1392			if (INQ_DATA_TQ_ENABLED(inq_buf))
1393				PROBE_SET_ACTION(softc, PROBE_MODE_SENSE);
1394			else
1395				PROBE_SET_ACTION(softc,
1396				    PROBE_SUPPORTED_VPD_LIST);
1397			xpt_release_ccb(done_ccb);
1398			xpt_schedule(periph, priority);
1399			goto out;
1400		}
1401		PROBE_SET_ACTION(softc, PROBE_INVALID);
1402		xpt_release_ccb(done_ccb);
1403		break;
1404	}
1405	case PROBE_MODE_SENSE:
1406	{
1407		struct ccb_scsiio *csio;
1408		struct scsi_mode_header_6 *mode_hdr;
1409
1410		csio = &done_ccb->csio;
1411		mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
1412		if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
1413			struct scsi_control_page *page;
1414			u_int8_t *offset;
1415
1416			offset = ((u_int8_t *)&mode_hdr[1])
1417			    + mode_hdr->blk_desc_len;
1418			page = (struct scsi_control_page *)offset;
1419			path->device->queue_flags = page->queue_flags;
1420		} else if (cam_periph_error(done_ccb, 0,
1421					    SF_RETRY_UA|SF_NO_PRINT,
1422					    &softc->saved_ccb) == ERESTART) {
1423			goto outr;
1424		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1425			/* Don't wedge the queue */
1426			xpt_release_devq(done_ccb->ccb_h.path,
1427					 /*count*/1, /*run_queue*/TRUE);
1428		}
1429		xpt_release_ccb(done_ccb);
1430		free(mode_hdr, M_CAMXPT);
1431		PROBE_SET_ACTION(softc, PROBE_SUPPORTED_VPD_LIST);
1432		xpt_schedule(periph, priority);
1433		goto out;
1434	}
1435	case PROBE_SUPPORTED_VPD_LIST:
1436	{
1437		struct ccb_scsiio *csio;
1438		struct scsi_vpd_supported_page_list *page_list;
1439
1440		csio = &done_ccb->csio;
1441		page_list =
1442		    (struct scsi_vpd_supported_page_list *)csio->data_ptr;
1443
1444		if (path->device->supported_vpds != NULL) {
1445			free(path->device->supported_vpds, M_CAMXPT);
1446			path->device->supported_vpds = NULL;
1447			path->device->supported_vpds_len = 0;
1448		}
1449
1450		if (page_list == NULL) {
1451			/*
1452			 * Don't process the command as it was never sent
1453			 */
1454		} else if (CCB_COMPLETED_OK(csio->ccb_h)) {
1455			/* Got vpd list */
1456			path->device->supported_vpds_len = page_list->length +
1457			    SVPD_SUPPORTED_PAGES_HDR_LEN;
1458			path->device->supported_vpds = (uint8_t *)page_list;
1459			xpt_release_ccb(done_ccb);
1460			PROBE_SET_ACTION(softc, PROBE_DEVICE_ID);
1461			xpt_schedule(periph, priority);
1462			goto out;
1463		} else if (cam_periph_error(done_ccb, 0,
1464					    SF_RETRY_UA|SF_NO_PRINT,
1465					    &softc->saved_ccb) == ERESTART) {
1466			goto outr;
1467		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1468			/* Don't wedge the queue */
1469			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1470					 /*run_queue*/TRUE);
1471		}
1472
1473		if (page_list)
1474			free(page_list, M_CAMXPT);
1475		/* No VPDs available, skip to device check. */
1476		csio->data_ptr = NULL;
1477		goto probe_device_check;
1478	}
1479	case PROBE_DEVICE_ID:
1480	{
1481		struct scsi_vpd_device_id *devid;
1482		struct ccb_scsiio *csio;
1483		uint32_t length = 0;
1484
1485		csio = &done_ccb->csio;
1486		devid = (struct scsi_vpd_device_id *)csio->data_ptr;
1487
1488		/* Clean up from previous instance of this device */
1489		if (path->device->device_id != NULL) {
1490			path->device->device_id_len = 0;
1491			free(path->device->device_id, M_CAMXPT);
1492			path->device->device_id = NULL;
1493		}
1494
1495		if (devid == NULL) {
1496			/* Don't process the command as it was never sent */
1497		} else if (CCB_COMPLETED_OK(csio->ccb_h)) {
1498			length = scsi_2btoul(devid->length);
1499			if (length != 0) {
1500				/*
1501				 * NB: device_id_len is actual response
1502				 * size, not buffer size.
1503				 */
1504				path->device->device_id_len = length +
1505				    SVPD_DEVICE_ID_HDR_LEN;
1506				path->device->device_id = (uint8_t *)devid;
1507			}
1508		} else if (cam_periph_error(done_ccb, 0,
1509					    SF_RETRY_UA,
1510					    &softc->saved_ccb) == ERESTART) {
1511			goto outr;
1512		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1513			/* Don't wedge the queue */
1514			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1515					 /*run_queue*/TRUE);
1516		}
1517
1518		/* Free the device id space if we don't use it */
1519		if (devid && length == 0)
1520			free(devid, M_CAMXPT);
1521		xpt_release_ccb(done_ccb);
1522		PROBE_SET_ACTION(softc, PROBE_EXTENDED_INQUIRY);
1523		xpt_schedule(periph, priority);
1524		goto out;
1525	}
1526	case PROBE_EXTENDED_INQUIRY: {
1527		struct scsi_vpd_extended_inquiry_data *ext_inq;
1528		struct ccb_scsiio *csio;
1529		int32_t length = 0;
1530
1531		csio = &done_ccb->csio;
1532		ext_inq = (struct scsi_vpd_extended_inquiry_data *)
1533		    csio->data_ptr;
1534		if (path->device->ext_inq != NULL) {
1535			path->device->ext_inq_len = 0;
1536			free(path->device->ext_inq, M_CAMXPT);
1537			path->device->ext_inq = NULL;
1538		}
1539
1540		if (ext_inq == NULL) {
1541			/* Don't process the command as it was never sent */
1542		} else if (CCB_COMPLETED_OK(csio->ccb_h)) {
1543			length = scsi_2btoul(ext_inq->page_length) +
1544			    __offsetof(struct scsi_vpd_extended_inquiry_data,
1545			    flags1);
1546			length = min(length, sizeof(*ext_inq));
1547			length -= csio->resid;
1548			if (length > 0) {
1549				path->device->ext_inq_len = length;
1550				path->device->ext_inq = (uint8_t *)ext_inq;
1551			}
1552		} else if (cam_periph_error(done_ccb, 0,
1553					    SF_RETRY_UA,
1554					    &softc->saved_ccb) == ERESTART) {
1555			goto outr;
1556		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1557			/* Don't wedge the queue */
1558			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1559					 /*run_queue*/TRUE);
1560		}
1561
1562		/* Free the device id space if we don't use it */
1563		if (ext_inq && length <= 0)
1564			free(ext_inq, M_CAMXPT);
1565		xpt_release_ccb(done_ccb);
1566		PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM);
1567		xpt_schedule(periph, priority);
1568		goto out;
1569	}
1570
1571probe_device_check:
1572	case PROBE_SERIAL_NUM:
1573	{
1574		struct ccb_scsiio *csio;
1575		struct scsi_vpd_unit_serial_number *serial_buf;
1576		u_int32_t  priority;
1577		int changed;
1578		int have_serialnum;
1579
1580		changed = 1;
1581		have_serialnum = 0;
1582		csio = &done_ccb->csio;
1583		priority = done_ccb->ccb_h.pinfo.priority;
1584		serial_buf =
1585		    (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
1586
1587		if (serial_buf == NULL) {
1588			/*
1589			 * Don't process the command as it was never sent
1590			 */
1591		} else if (cam_ccb_status(done_ccb) == CAM_REQ_CMP
1592			&& (serial_buf->length > 0)) {
1593
1594			have_serialnum = 1;
1595			path->device->serial_num =
1596				(u_int8_t *)malloc((serial_buf->length + 1),
1597						   M_CAMXPT, M_NOWAIT);
1598			if (path->device->serial_num != NULL) {
1599				int start, slen;
1600
1601				start = strspn(serial_buf->serial_num, " ");
1602				slen = serial_buf->length - start;
1603				if (slen <= 0) {
1604					/*
1605					 * SPC5r05 says that an all-space serial
1606					 * number means no product serial number
1607					 * is available
1608					 */
1609					slen = 0;
1610				}
1611				memcpy(path->device->serial_num,
1612				       &serial_buf->serial_num[start], slen);
1613				path->device->serial_num_len = slen;
1614				path->device->serial_num[slen] = '\0';
1615			}
1616		} else if (cam_periph_error(done_ccb, 0,
1617					    SF_RETRY_UA|SF_NO_PRINT,
1618					    &softc->saved_ccb) == ERESTART) {
1619			goto outr;
1620		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1621			/* Don't wedge the queue */
1622			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1623					 /*run_queue*/TRUE);
1624		}
1625
1626		/*
1627		 * Let's see if we have seen this device before.
1628		 */
1629		if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
1630			MD5_CTX context;
1631			u_int8_t digest[16];
1632
1633			MD5Init(&context);
1634
1635			MD5Update(&context,
1636				  (unsigned char *)&path->device->inq_data,
1637				  sizeof(struct scsi_inquiry_data));
1638
1639			if (have_serialnum)
1640				MD5Update(&context, path->device->serial_num,
1641					  path->device->serial_num_len);
1642
1643			MD5Final(digest, &context);
1644			if (bcmp(softc->digest, digest, 16) == 0)
1645				changed = 0;
1646
1647			/*
1648			 * XXX Do we need to do a TUR in order to ensure
1649			 *     that the device really hasn't changed???
1650			 */
1651			if ((changed != 0)
1652			 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
1653				xpt_async(AC_LOST_DEVICE, path, NULL);
1654		}
1655		if (serial_buf != NULL)
1656			free(serial_buf, M_CAMXPT);
1657
1658		if (changed != 0) {
1659			/*
1660			 * Now that we have all the necessary
1661			 * information to safely perform transfer
1662			 * negotiations... Controllers don't perform
1663			 * any negotiation or tagged queuing until
1664			 * after the first XPT_SET_TRAN_SETTINGS ccb is
1665			 * received.  So, on a new device, just retrieve
1666			 * the user settings, and set them as the current
1667			 * settings to set the device up.
1668			 */
1669			proberequestdefaultnegotiation(periph);
1670			xpt_release_ccb(done_ccb);
1671
1672			/*
1673			 * Perform a TUR to allow the controller to
1674			 * perform any necessary transfer negotiation.
1675			 */
1676			PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1677			xpt_schedule(periph, priority);
1678			goto out;
1679		}
1680		xpt_release_ccb(done_ccb);
1681		break;
1682	}
1683	case PROBE_TUR_FOR_NEGOTIATION:
1684	case PROBE_DV_EXIT:
1685		if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1686			cam_periph_error(done_ccb, 0,
1687			    SF_NO_PRINT | SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1688		}
1689		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1690			/* Don't wedge the queue */
1691			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1692					 /*run_queue*/TRUE);
1693		}
1694		/*
1695		 * Do Domain Validation for lun 0 on devices that claim
1696		 * to support Synchronous Transfer modes.
1697		 */
1698	 	if (softc->action == PROBE_TUR_FOR_NEGOTIATION
1699		 && done_ccb->ccb_h.target_lun == 0
1700		 && (path->device->inq_data.flags & SID_Sync) != 0
1701                 && (path->device->flags & CAM_DEV_IN_DV) == 0) {
1702			CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1703			    ("Begin Domain Validation\n"));
1704			path->device->flags |= CAM_DEV_IN_DV;
1705			xpt_release_ccb(done_ccb);
1706			PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV1);
1707			xpt_schedule(periph, priority);
1708			goto out;
1709		}
1710		if (softc->action == PROBE_DV_EXIT) {
1711			CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1712			    ("Leave Domain Validation\n"));
1713		}
1714		if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1715			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1716			xpt_acquire_device(path->device);
1717		}
1718		path->device->flags &=
1719		    ~(CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1720		if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1721			/* Inform the XPT that a new device has been found */
1722			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1723			xpt_action(done_ccb);
1724			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1725				  done_ccb);
1726		}
1727		PROBE_SET_ACTION(softc, PROBE_DONE);
1728		xpt_release_ccb(done_ccb);
1729		break;
1730	case PROBE_INQUIRY_BASIC_DV1:
1731	case PROBE_INQUIRY_BASIC_DV2:
1732	{
1733		struct scsi_inquiry_data *nbuf;
1734		struct ccb_scsiio *csio;
1735
1736		if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1737			cam_periph_error(done_ccb, 0,
1738			    SF_NO_PRINT | SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1739		}
1740		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1741			/* Don't wedge the queue */
1742			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1743					 /*run_queue*/TRUE);
1744		}
1745		csio = &done_ccb->csio;
1746		nbuf = (struct scsi_inquiry_data *)csio->data_ptr;
1747		if (bcmp(nbuf, &path->device->inq_data, SHORT_INQUIRY_LENGTH)) {
1748			xpt_print(path,
1749			    "inquiry data fails comparison at DV%d step\n",
1750			    softc->action == PROBE_INQUIRY_BASIC_DV1 ? 1 : 2);
1751			if (proberequestbackoff(periph, path->device)) {
1752				path->device->flags &= ~CAM_DEV_IN_DV;
1753				PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1754			} else {
1755				/* give up */
1756				PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
1757			}
1758			free(nbuf, M_CAMXPT);
1759			xpt_release_ccb(done_ccb);
1760			xpt_schedule(periph, priority);
1761			goto out;
1762		}
1763		free(nbuf, M_CAMXPT);
1764		if (softc->action == PROBE_INQUIRY_BASIC_DV1) {
1765			PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV2);
1766			xpt_release_ccb(done_ccb);
1767			xpt_schedule(periph, priority);
1768			goto out;
1769		}
1770		if (softc->action == PROBE_INQUIRY_BASIC_DV2) {
1771			CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1772			    ("Leave Domain Validation Successfully\n"));
1773		}
1774		if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1775			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1776			xpt_acquire_device(path->device);
1777		}
1778		path->device->flags &=
1779		    ~(CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1780		if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1781			/* Inform the XPT that a new device has been found */
1782			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1783			xpt_action(done_ccb);
1784			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1785				  done_ccb);
1786		}
1787		PROBE_SET_ACTION(softc, PROBE_DONE);
1788		xpt_release_ccb(done_ccb);
1789		break;
1790	}
1791	default:
1792		panic("probedone: invalid action state 0x%x\n", softc->action);
1793	}
1794	done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
1795	TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
1796	done_ccb->ccb_h.status = CAM_REQ_CMP;
1797	xpt_done(done_ccb);
1798	if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
1799		CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
1800		/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1801		cam_release_devq(path, 0, 0, 0, FALSE);
1802		cam_periph_release_locked(periph);
1803		cam_periph_invalidate(periph);
1804		cam_periph_release_locked(periph);
1805	} else {
1806		probeschedule(periph);
1807		goto out;
1808	}
1809}
1810
1811static void
1812probe_purge_old(struct cam_path *path, struct scsi_report_luns_data *new,
1813    probe_flags flags)
1814{
1815	struct cam_path *tp;
1816	struct scsi_report_luns_data *old;
1817	u_int idx1, idx2, nlun_old, nlun_new;
1818	lun_id_t this_lun;
1819	u_int8_t *ol, *nl;
1820
1821	if (path->target == NULL) {
1822		return;
1823	}
1824	mtx_lock(&path->target->luns_mtx);
1825	old = path->target->luns;
1826	path->target->luns = new;
1827	mtx_unlock(&path->target->luns_mtx);
1828	if (old == NULL)
1829		return;
1830	nlun_old = scsi_4btoul(old->length) / 8;
1831	nlun_new = scsi_4btoul(new->length) / 8;
1832
1833	/*
1834	 * We are not going to assume sorted lists. Deal.
1835	 */
1836	for (idx1 = 0; idx1 < nlun_old; idx1++) {
1837		ol = old->luns[idx1].lundata;
1838		for (idx2 = 0; idx2 < nlun_new; idx2++) {
1839			nl = new->luns[idx2].lundata;
1840			if (memcmp(nl, ol, 8) == 0) {
1841				break;
1842			}
1843		}
1844		if (idx2 < nlun_new) {
1845			continue;
1846		}
1847		/*
1848		 * An 'old' item not in the 'new' list.
1849		 * Nuke it. Except that if it is lun 0,
1850		 * that would be what the probe state
1851		 * machine is currently working on,
1852		 * so we won't do that.
1853		 */
1854		CAM_GET_LUN(old, idx1, this_lun);
1855		if (this_lun == 0) {
1856			continue;
1857		}
1858
1859		/*
1860		 * We also cannot nuke it if it is
1861		 * not in a lun format we understand
1862		 * and replace the LUN with a "simple" LUN
1863		 * if that is all the HBA supports.
1864		 */
1865		if (!(flags & PROBE_EXTLUN)) {
1866			if (!CAM_CAN_GET_SIMPLE_LUN(old, idx1))
1867				continue;
1868			CAM_GET_SIMPLE_LUN(old, idx1, this_lun);
1869		}
1870
1871		if (xpt_create_path(&tp, NULL, xpt_path_path_id(path),
1872		    xpt_path_target_id(path), this_lun) == CAM_REQ_CMP) {
1873			xpt_async(AC_LOST_DEVICE, tp, NULL);
1874			xpt_free_path(tp);
1875		}
1876	}
1877	free(old, M_CAMXPT);
1878}
1879
1880static void
1881probecleanup(struct cam_periph *periph)
1882{
1883	free(periph->softc, M_CAMXPT);
1884}
1885
1886static void
1887scsi_find_quirk(struct cam_ed *device)
1888{
1889	struct scsi_quirk_entry *quirk;
1890	caddr_t	match;
1891
1892	match = cam_quirkmatch((caddr_t)&device->inq_data,
1893			       (caddr_t)scsi_quirk_table,
1894			       nitems(scsi_quirk_table),
1895			       sizeof(*scsi_quirk_table), scsi_inquiry_match);
1896
1897	if (match == NULL)
1898		panic("xpt_find_quirk: device didn't match wildcard entry!!");
1899
1900	quirk = (struct scsi_quirk_entry *)match;
1901	device->quirk = quirk;
1902	device->mintags = quirk->mintags;
1903	device->maxtags = quirk->maxtags;
1904}
1905
1906static int
1907sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS)
1908{
1909	int error, val;
1910
1911	val = cam_srch_hi;
1912	error = sysctl_handle_int(oidp, &val, 0, req);
1913	if (error != 0 || req->newptr == NULL)
1914		return (error);
1915	if (val == 0 || val == 1) {
1916		cam_srch_hi = val;
1917		return (0);
1918	} else {
1919		return (EINVAL);
1920	}
1921}
1922
1923typedef struct {
1924	union	ccb *request_ccb;
1925	struct 	ccb_pathinq *cpi;
1926	int	counter;
1927	int	lunindex[0];
1928} scsi_scan_bus_info;
1929
1930/*
1931 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1932 * As the scan progresses, scsi_scan_bus is used as the
1933 * callback on completion function.
1934 */
1935static void
1936scsi_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1937{
1938	struct mtx *mtx;
1939
1940	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1941		  ("scsi_scan_bus\n"));
1942	switch (request_ccb->ccb_h.func_code) {
1943	case XPT_SCAN_BUS:
1944	case XPT_SCAN_TGT:
1945	{
1946		scsi_scan_bus_info *scan_info;
1947		union	ccb *work_ccb, *reset_ccb;
1948		struct	cam_path *path;
1949		u_int	i;
1950		u_int	low_target, max_target;
1951		u_int	initiator_id;
1952
1953		/* Find out the characteristics of the bus */
1954		work_ccb = xpt_alloc_ccb_nowait();
1955		if (work_ccb == NULL) {
1956			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1957			xpt_done(request_ccb);
1958			return;
1959		}
1960		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1961			      request_ccb->ccb_h.pinfo.priority);
1962		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1963		xpt_action(work_ccb);
1964		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1965			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1966			xpt_free_ccb(work_ccb);
1967			xpt_done(request_ccb);
1968			return;
1969		}
1970
1971		if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
1972			/*
1973			 * Can't scan the bus on an adapter that
1974			 * cannot perform the initiator role.
1975			 */
1976			request_ccb->ccb_h.status = CAM_REQ_CMP;
1977			xpt_free_ccb(work_ccb);
1978			xpt_done(request_ccb);
1979			return;
1980		}
1981
1982		/* We may need to reset bus first, if we haven't done it yet. */
1983		if ((work_ccb->cpi.hba_inquiry &
1984		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1985		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1986		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset) &&
1987		    (reset_ccb = xpt_alloc_ccb_nowait()) != NULL) {
1988			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1989			      CAM_PRIORITY_NONE);
1990			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1991			xpt_action(reset_ccb);
1992			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1993				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1994				xpt_free_ccb(reset_ccb);
1995				xpt_free_ccb(work_ccb);
1996				xpt_done(request_ccb);
1997				return;
1998			}
1999			xpt_free_ccb(reset_ccb);
2000		}
2001
2002		/* Save some state for use while we probe for devices */
2003		scan_info = (scsi_scan_bus_info *) malloc(sizeof(scsi_scan_bus_info) +
2004		    (work_ccb->cpi.max_target * sizeof (u_int)), M_CAMXPT, M_ZERO|M_NOWAIT);
2005		if (scan_info == NULL) {
2006			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2007			xpt_free_ccb(work_ccb);
2008			xpt_done(request_ccb);
2009			return;
2010		}
2011		CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
2012		   ("SCAN start for %p\n", scan_info));
2013		scan_info->request_ccb = request_ccb;
2014		scan_info->cpi = &work_ccb->cpi;
2015
2016		/* Cache on our stack so we can work asynchronously */
2017		max_target = scan_info->cpi->max_target;
2018		low_target = 0;
2019		initiator_id = scan_info->cpi->initiator_id;
2020
2021
2022		/*
2023		 * We can scan all targets in parallel, or do it sequentially.
2024		 */
2025
2026		if (request_ccb->ccb_h.func_code == XPT_SCAN_TGT) {
2027			max_target = low_target = request_ccb->ccb_h.target_id;
2028			scan_info->counter = 0;
2029		} else if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
2030			max_target = 0;
2031			scan_info->counter = 0;
2032		} else {
2033			scan_info->counter = scan_info->cpi->max_target + 1;
2034			if (scan_info->cpi->initiator_id < scan_info->counter) {
2035				scan_info->counter--;
2036			}
2037		}
2038		mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
2039		mtx_unlock(mtx);
2040
2041		for (i = low_target; i <= max_target; i++) {
2042			cam_status status;
2043			if (i == initiator_id)
2044				continue;
2045
2046			status = xpt_create_path(&path, NULL,
2047						 request_ccb->ccb_h.path_id,
2048						 i, 0);
2049			if (status != CAM_REQ_CMP) {
2050				printf("scsi_scan_bus: xpt_create_path failed"
2051				       " with status %#x, bus scan halted\n",
2052				       status);
2053				free(scan_info, M_CAMXPT);
2054				request_ccb->ccb_h.status = status;
2055				xpt_free_ccb(work_ccb);
2056				xpt_done(request_ccb);
2057				break;
2058			}
2059			work_ccb = xpt_alloc_ccb_nowait();
2060			if (work_ccb == NULL) {
2061				xpt_free_ccb((union ccb *)scan_info->cpi);
2062				free(scan_info, M_CAMXPT);
2063				xpt_free_path(path);
2064				request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2065				xpt_done(request_ccb);
2066				break;
2067			}
2068			xpt_setup_ccb(&work_ccb->ccb_h, path,
2069				      request_ccb->ccb_h.pinfo.priority);
2070			work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2071			work_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2072			work_ccb->ccb_h.flags |= CAM_UNLOCKED;
2073			work_ccb->ccb_h.ppriv_ptr0 = scan_info;
2074			work_ccb->crcn.flags = request_ccb->crcn.flags;
2075			xpt_action(work_ccb);
2076		}
2077
2078		mtx_lock(mtx);
2079		break;
2080	}
2081	case XPT_SCAN_LUN:
2082	{
2083		cam_status status;
2084		struct cam_path *path, *oldpath;
2085		scsi_scan_bus_info *scan_info;
2086		struct cam_et *target;
2087		struct cam_ed *device, *nextdev;
2088		int next_target;
2089		path_id_t path_id;
2090		target_id_t target_id;
2091		lun_id_t lun_id;
2092
2093		oldpath = request_ccb->ccb_h.path;
2094
2095		status = cam_ccb_status(request_ccb);
2096		scan_info = (scsi_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
2097		path_id = request_ccb->ccb_h.path_id;
2098		target_id = request_ccb->ccb_h.target_id;
2099		lun_id = request_ccb->ccb_h.target_lun;
2100		target = request_ccb->ccb_h.path->target;
2101		next_target = 1;
2102
2103		mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
2104		mtx_lock(mtx);
2105		mtx_lock(&target->luns_mtx);
2106		if (target->luns) {
2107			lun_id_t first;
2108			u_int nluns = scsi_4btoul(target->luns->length) / 8;
2109
2110			/*
2111			 * Make sure we skip over lun 0 if it's the first member
2112			 * of the list as we've actually just finished probing
2113			 * it.
2114			 */
2115			CAM_GET_LUN(target->luns, 0, first);
2116			if (first == 0 && scan_info->lunindex[target_id] == 0) {
2117				scan_info->lunindex[target_id]++;
2118			}
2119
2120			/*
2121			 * Skip any LUNs that the HBA can't deal with.
2122			 */
2123			while (scan_info->lunindex[target_id] < nluns) {
2124				if (scan_info->cpi->hba_misc & PIM_EXTLUNS) {
2125					CAM_GET_LUN(target->luns,
2126					    scan_info->lunindex[target_id],
2127					    lun_id);
2128					break;
2129				}
2130
2131				if (CAM_CAN_GET_SIMPLE_LUN(target->luns,
2132				    scan_info->lunindex[target_id])) {
2133					CAM_GET_SIMPLE_LUN(target->luns,
2134					    scan_info->lunindex[target_id],
2135					    lun_id);
2136					break;
2137				}
2138
2139				scan_info->lunindex[target_id]++;
2140			}
2141
2142			if (scan_info->lunindex[target_id] < nluns) {
2143				mtx_unlock(&target->luns_mtx);
2144				next_target = 0;
2145				CAM_DEBUG(request_ccb->ccb_h.path,
2146				    CAM_DEBUG_PROBE,
2147				   ("next lun to try at index %u is %jx\n",
2148				   scan_info->lunindex[target_id],
2149				   (uintmax_t)lun_id));
2150				scan_info->lunindex[target_id]++;
2151			} else {
2152				mtx_unlock(&target->luns_mtx);
2153				/* We're done with scanning all luns. */
2154			}
2155		} else {
2156			mtx_unlock(&target->luns_mtx);
2157			device = request_ccb->ccb_h.path->device;
2158			/* Continue sequential LUN scan if: */
2159			/*  -- we have more LUNs that need recheck */
2160			mtx_lock(&target->bus->eb_mtx);
2161			nextdev = device;
2162			while ((nextdev = TAILQ_NEXT(nextdev, links)) != NULL)
2163				if ((nextdev->flags & CAM_DEV_UNCONFIGURED) == 0)
2164					break;
2165			mtx_unlock(&target->bus->eb_mtx);
2166			if (nextdev != NULL) {
2167				next_target = 0;
2168			/*  -- stop if CAM_QUIRK_NOLUNS is set. */
2169			} else if (SCSI_QUIRK(device)->quirks & CAM_QUIRK_NOLUNS) {
2170				next_target = 1;
2171			/*  -- this LUN is connected and its SCSI version
2172			 *     allows more LUNs. */
2173			} else if ((device->flags & CAM_DEV_UNCONFIGURED) == 0) {
2174				if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
2175				    CAN_SRCH_HI_DENSE(device))
2176					next_target = 0;
2177			/*  -- this LUN is disconnected, its SCSI version
2178			 *     allows more LUNs and we guess they may be. */
2179			} else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0) {
2180				if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
2181				    CAN_SRCH_HI_SPARSE(device))
2182					next_target = 0;
2183			}
2184			if (next_target == 0) {
2185				lun_id++;
2186				if (lun_id > scan_info->cpi->max_lun)
2187					next_target = 1;
2188			}
2189		}
2190
2191		/*
2192		 * Check to see if we scan any further luns.
2193		 */
2194		if (next_target) {
2195			int done;
2196
2197			/*
2198			 * Free the current request path- we're done with it.
2199			 */
2200			xpt_free_path(oldpath);
2201 hop_again:
2202			done = 0;
2203			if (scan_info->request_ccb->ccb_h.func_code == XPT_SCAN_TGT) {
2204				done = 1;
2205			} else if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
2206				scan_info->counter++;
2207				if (scan_info->counter ==
2208				    scan_info->cpi->initiator_id) {
2209					scan_info->counter++;
2210				}
2211				if (scan_info->counter >=
2212				    scan_info->cpi->max_target+1) {
2213					done = 1;
2214				}
2215			} else {
2216				scan_info->counter--;
2217				if (scan_info->counter == 0) {
2218					done = 1;
2219				}
2220			}
2221			if (done) {
2222				mtx_unlock(mtx);
2223				xpt_free_ccb(request_ccb);
2224				xpt_free_ccb((union ccb *)scan_info->cpi);
2225				request_ccb = scan_info->request_ccb;
2226				CAM_DEBUG(request_ccb->ccb_h.path,
2227				    CAM_DEBUG_TRACE,
2228				   ("SCAN done for %p\n", scan_info));
2229				free(scan_info, M_CAMXPT);
2230				request_ccb->ccb_h.status = CAM_REQ_CMP;
2231				xpt_done(request_ccb);
2232				break;
2233			}
2234
2235			if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
2236				mtx_unlock(mtx);
2237				xpt_free_ccb(request_ccb);
2238				break;
2239			}
2240			status = xpt_create_path(&path, NULL,
2241			    scan_info->request_ccb->ccb_h.path_id,
2242			    scan_info->counter, 0);
2243			if (status != CAM_REQ_CMP) {
2244				mtx_unlock(mtx);
2245				printf("scsi_scan_bus: xpt_create_path failed"
2246				    " with status %#x, bus scan halted\n",
2247			       	    status);
2248				xpt_free_ccb(request_ccb);
2249				xpt_free_ccb((union ccb *)scan_info->cpi);
2250				request_ccb = scan_info->request_ccb;
2251				free(scan_info, M_CAMXPT);
2252				request_ccb->ccb_h.status = status;
2253				xpt_done(request_ccb);
2254				break;
2255			}
2256			xpt_setup_ccb(&request_ccb->ccb_h, path,
2257			    request_ccb->ccb_h.pinfo.priority);
2258			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2259			request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2260			request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2261			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
2262			request_ccb->crcn.flags =
2263			    scan_info->request_ccb->crcn.flags;
2264		} else {
2265			status = xpt_create_path(&path, NULL,
2266						 path_id, target_id, lun_id);
2267			/*
2268			 * Free the old request path- we're done with it. We
2269			 * do this *after* creating the new path so that
2270			 * we don't remove a target that has our lun list
2271			 * in the case that lun 0 is not present.
2272			 */
2273			xpt_free_path(oldpath);
2274			if (status != CAM_REQ_CMP) {
2275				printf("scsi_scan_bus: xpt_create_path failed "
2276				       "with status %#x, halting LUN scan\n",
2277			 	       status);
2278				goto hop_again;
2279			}
2280			xpt_setup_ccb(&request_ccb->ccb_h, path,
2281				      request_ccb->ccb_h.pinfo.priority);
2282			request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2283			request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2284			request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2285			request_ccb->ccb_h.ppriv_ptr0 = scan_info;
2286			request_ccb->crcn.flags =
2287				scan_info->request_ccb->crcn.flags;
2288		}
2289		mtx_unlock(mtx);
2290		xpt_action(request_ccb);
2291		break;
2292	}
2293	default:
2294		break;
2295	}
2296}
2297
2298static void
2299scsi_scan_lun(struct cam_periph *periph, struct cam_path *path,
2300	     cam_flags flags, union ccb *request_ccb)
2301{
2302	struct ccb_pathinq cpi;
2303	cam_status status;
2304	struct cam_path *new_path;
2305	struct cam_periph *old_periph;
2306	int lock;
2307
2308	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("scsi_scan_lun\n"));
2309
2310	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2311	cpi.ccb_h.func_code = XPT_PATH_INQ;
2312	xpt_action((union ccb *)&cpi);
2313
2314	if (cpi.ccb_h.status != CAM_REQ_CMP) {
2315		if (request_ccb != NULL) {
2316			request_ccb->ccb_h.status = cpi.ccb_h.status;
2317			xpt_done(request_ccb);
2318		}
2319		return;
2320	}
2321
2322	if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
2323		/*
2324		 * Can't scan the bus on an adapter that
2325		 * cannot perform the initiator role.
2326		 */
2327		if (request_ccb != NULL) {
2328			request_ccb->ccb_h.status = CAM_REQ_CMP;
2329			xpt_done(request_ccb);
2330		}
2331		return;
2332	}
2333
2334	if (request_ccb == NULL) {
2335		request_ccb = xpt_alloc_ccb_nowait();
2336		if (request_ccb == NULL) {
2337			xpt_print(path, "scsi_scan_lun: can't allocate CCB, "
2338			    "can't continue\n");
2339			return;
2340		}
2341		status = xpt_create_path(&new_path, NULL,
2342					  path->bus->path_id,
2343					  path->target->target_id,
2344					  path->device->lun_id);
2345		if (status != CAM_REQ_CMP) {
2346			xpt_print(path, "scsi_scan_lun: can't create path, "
2347			    "can't continue\n");
2348			xpt_free_ccb(request_ccb);
2349			return;
2350		}
2351		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
2352		request_ccb->ccb_h.cbfcnp = xptscandone;
2353		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2354		request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2355		request_ccb->crcn.flags = flags;
2356	}
2357
2358	lock = (xpt_path_owned(path) == 0);
2359	if (lock)
2360		xpt_path_lock(path);
2361	if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
2362		if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
2363			probe_softc *softc;
2364
2365			softc = (probe_softc *)old_periph->softc;
2366			TAILQ_INSERT_TAIL(&softc->request_ccbs,
2367			    &request_ccb->ccb_h, periph_links.tqe);
2368		} else {
2369			request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2370			xpt_done(request_ccb);
2371		}
2372	} else {
2373		status = cam_periph_alloc(proberegister, NULL, probecleanup,
2374					  probestart, "probe",
2375					  CAM_PERIPH_BIO,
2376					  request_ccb->ccb_h.path, NULL, 0,
2377					  request_ccb);
2378
2379		if (status != CAM_REQ_CMP) {
2380			xpt_print(path, "scsi_scan_lun: cam_alloc_periph "
2381			    "returned an error, can't continue probe\n");
2382			request_ccb->ccb_h.status = status;
2383			xpt_done(request_ccb);
2384		}
2385	}
2386	if (lock)
2387		xpt_path_unlock(path);
2388}
2389
2390static void
2391xptscandone(struct cam_periph *periph, union ccb *done_ccb)
2392{
2393
2394	xpt_free_path(done_ccb->ccb_h.path);
2395	xpt_free_ccb(done_ccb);
2396}
2397
2398static struct cam_ed *
2399scsi_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
2400{
2401	struct scsi_quirk_entry *quirk;
2402	struct cam_ed *device;
2403
2404	device = xpt_alloc_device(bus, target, lun_id);
2405	if (device == NULL)
2406		return (NULL);
2407
2408	/*
2409	 * Take the default quirk entry until we have inquiry
2410	 * data and can determine a better quirk to use.
2411	 */
2412	quirk = &scsi_quirk_table[nitems(scsi_quirk_table) - 1];
2413	device->quirk = (void *)quirk;
2414	device->mintags = quirk->mintags;
2415	device->maxtags = quirk->maxtags;
2416	bzero(&device->inq_data, sizeof(device->inq_data));
2417	device->inq_flags = 0;
2418	device->queue_flags = 0;
2419	device->serial_num = NULL;
2420	device->serial_num_len = 0;
2421	device->device_id = NULL;
2422	device->device_id_len = 0;
2423	device->supported_vpds = NULL;
2424	device->supported_vpds_len = 0;
2425	return (device);
2426}
2427
2428static void
2429scsi_devise_transport(struct cam_path *path)
2430{
2431	struct ccb_pathinq cpi;
2432	struct ccb_trans_settings cts;
2433	struct scsi_inquiry_data *inq_buf;
2434
2435	/* Get transport information from the SIM */
2436	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2437	cpi.ccb_h.func_code = XPT_PATH_INQ;
2438	xpt_action((union ccb *)&cpi);
2439
2440	inq_buf = NULL;
2441	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
2442		inq_buf = &path->device->inq_data;
2443	path->device->protocol = PROTO_SCSI;
2444	path->device->protocol_version =
2445	    inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
2446	path->device->transport = cpi.transport;
2447	path->device->transport_version = cpi.transport_version;
2448
2449	/*
2450	 * Any device not using SPI3 features should
2451	 * be considered SPI2 or lower.
2452	 */
2453	if (inq_buf != NULL) {
2454		if (path->device->transport == XPORT_SPI
2455		 && (inq_buf->spi3data & SID_SPI_MASK) == 0
2456		 && path->device->transport_version > 2)
2457			path->device->transport_version = 2;
2458	} else {
2459		struct cam_ed* otherdev;
2460
2461		for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
2462		     otherdev != NULL;
2463		     otherdev = TAILQ_NEXT(otherdev, links)) {
2464			if (otherdev != path->device)
2465				break;
2466		}
2467
2468		if (otherdev != NULL) {
2469			/*
2470			 * Initially assume the same versioning as
2471			 * prior luns for this target.
2472			 */
2473			path->device->protocol_version =
2474			    otherdev->protocol_version;
2475			path->device->transport_version =
2476			    otherdev->transport_version;
2477		} else {
2478			/* Until we know better, opt for safety */
2479			path->device->protocol_version = 2;
2480			if (path->device->transport == XPORT_SPI)
2481				path->device->transport_version = 2;
2482			else
2483				path->device->transport_version = 0;
2484		}
2485	}
2486
2487	/*
2488	 * XXX
2489	 * For a device compliant with SPC-2 we should be able
2490	 * to determine the transport version supported by
2491	 * scrutinizing the version descriptors in the
2492	 * inquiry buffer.
2493	 */
2494
2495	/* Tell the controller what we think */
2496	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2497	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
2498	cts.type = CTS_TYPE_CURRENT_SETTINGS;
2499	cts.transport = path->device->transport;
2500	cts.transport_version = path->device->transport_version;
2501	cts.protocol = path->device->protocol;
2502	cts.protocol_version = path->device->protocol_version;
2503	cts.proto_specific.valid = 0;
2504	cts.xport_specific.valid = 0;
2505	xpt_action((union ccb *)&cts);
2506}
2507
2508static void
2509scsi_dev_advinfo(union ccb *start_ccb)
2510{
2511	struct cam_ed *device;
2512	struct ccb_dev_advinfo *cdai;
2513	off_t amt;
2514
2515	start_ccb->ccb_h.status = CAM_REQ_INVALID;
2516	device = start_ccb->ccb_h.path->device;
2517	cdai = &start_ccb->cdai;
2518	switch(cdai->buftype) {
2519	case CDAI_TYPE_SCSI_DEVID:
2520		if (cdai->flags & CDAI_FLAG_STORE)
2521			return;
2522		cdai->provsiz = device->device_id_len;
2523		if (device->device_id_len == 0)
2524			break;
2525		amt = device->device_id_len;
2526		if (cdai->provsiz > cdai->bufsiz)
2527			amt = cdai->bufsiz;
2528		memcpy(cdai->buf, device->device_id, amt);
2529		break;
2530	case CDAI_TYPE_SERIAL_NUM:
2531		if (cdai->flags & CDAI_FLAG_STORE)
2532			return;
2533		cdai->provsiz = device->serial_num_len;
2534		if (device->serial_num_len == 0)
2535			break;
2536		amt = device->serial_num_len;
2537		if (cdai->provsiz > cdai->bufsiz)
2538			amt = cdai->bufsiz;
2539		memcpy(cdai->buf, device->serial_num, amt);
2540		break;
2541	case CDAI_TYPE_PHYS_PATH:
2542		if (cdai->flags & CDAI_FLAG_STORE) {
2543			if (device->physpath != NULL) {
2544				free(device->physpath, M_CAMXPT);
2545				device->physpath = NULL;
2546				device->physpath_len = 0;
2547			}
2548			/* Clear existing buffer if zero length */
2549			if (cdai->bufsiz == 0)
2550				break;
2551			device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
2552			if (device->physpath == NULL) {
2553				start_ccb->ccb_h.status = CAM_REQ_ABORTED;
2554				return;
2555			}
2556			device->physpath_len = cdai->bufsiz;
2557			memcpy(device->physpath, cdai->buf, cdai->bufsiz);
2558		} else {
2559			cdai->provsiz = device->physpath_len;
2560			if (device->physpath_len == 0)
2561				break;
2562			amt = device->physpath_len;
2563			if (cdai->provsiz > cdai->bufsiz)
2564				amt = cdai->bufsiz;
2565			memcpy(cdai->buf, device->physpath, amt);
2566		}
2567		break;
2568	case CDAI_TYPE_RCAPLONG:
2569		if (cdai->flags & CDAI_FLAG_STORE) {
2570			if (device->rcap_buf != NULL) {
2571				free(device->rcap_buf, M_CAMXPT);
2572				device->rcap_buf = NULL;
2573			}
2574
2575			device->rcap_len = cdai->bufsiz;
2576			/* Clear existing buffer if zero length */
2577			if (cdai->bufsiz == 0)
2578				break;
2579
2580			device->rcap_buf = malloc(cdai->bufsiz, M_CAMXPT,
2581						  M_NOWAIT);
2582			if (device->rcap_buf == NULL) {
2583				start_ccb->ccb_h.status = CAM_REQ_ABORTED;
2584				return;
2585			}
2586
2587			memcpy(device->rcap_buf, cdai->buf, cdai->bufsiz);
2588		} else {
2589			cdai->provsiz = device->rcap_len;
2590			if (device->rcap_len == 0)
2591				break;
2592			amt = device->rcap_len;
2593			if (cdai->provsiz > cdai->bufsiz)
2594				amt = cdai->bufsiz;
2595			memcpy(cdai->buf, device->rcap_buf, amt);
2596		}
2597		break;
2598	case CDAI_TYPE_EXT_INQ:
2599		/*
2600		 * We fetch extended inquiry data during probe, if
2601		 * available.  We don't allow changing it.
2602		 */
2603		if (cdai->flags & CDAI_FLAG_STORE)
2604			return;
2605		cdai->provsiz = device->ext_inq_len;
2606		if (device->ext_inq_len == 0)
2607			break;
2608		amt = device->ext_inq_len;
2609		if (cdai->provsiz > cdai->bufsiz)
2610			amt = cdai->bufsiz;
2611		memcpy(cdai->buf, device->ext_inq, amt);
2612		break;
2613	default:
2614		return;
2615	}
2616	start_ccb->ccb_h.status = CAM_REQ_CMP;
2617
2618	if (cdai->flags & CDAI_FLAG_STORE) {
2619		xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
2620			  (void *)(uintptr_t)cdai->buftype);
2621	}
2622}
2623
2624static void
2625scsi_action(union ccb *start_ccb)
2626{
2627
2628	switch (start_ccb->ccb_h.func_code) {
2629	case XPT_SET_TRAN_SETTINGS:
2630	{
2631		scsi_set_transfer_settings(&start_ccb->cts,
2632					   start_ccb->ccb_h.path,
2633					   /*async_update*/FALSE);
2634		break;
2635	}
2636	case XPT_SCAN_BUS:
2637	case XPT_SCAN_TGT:
2638		scsi_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
2639		break;
2640	case XPT_SCAN_LUN:
2641		scsi_scan_lun(start_ccb->ccb_h.path->periph,
2642			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
2643			      start_ccb);
2644		break;
2645	case XPT_DEV_ADVINFO:
2646	{
2647		scsi_dev_advinfo(start_ccb);
2648		break;
2649	}
2650	default:
2651		xpt_action_default(start_ccb);
2652		break;
2653	}
2654}
2655
2656static void
2657scsi_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_path *path,
2658			   int async_update)
2659{
2660	struct	ccb_pathinq cpi;
2661	struct	ccb_trans_settings cur_cts;
2662	struct	ccb_trans_settings_scsi *scsi;
2663	struct	ccb_trans_settings_scsi *cur_scsi;
2664	struct	scsi_inquiry_data *inq_data;
2665	struct	cam_ed *device;
2666
2667	if (path == NULL || (device = path->device) == NULL) {
2668		cts->ccb_h.status = CAM_PATH_INVALID;
2669		xpt_done((union ccb *)cts);
2670		return;
2671	}
2672
2673	if (cts->protocol == PROTO_UNKNOWN
2674	 || cts->protocol == PROTO_UNSPECIFIED) {
2675		cts->protocol = device->protocol;
2676		cts->protocol_version = device->protocol_version;
2677	}
2678
2679	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
2680	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
2681		cts->protocol_version = device->protocol_version;
2682
2683	if (cts->protocol != device->protocol) {
2684		xpt_print(path, "Uninitialized Protocol %x:%x?\n",
2685		       cts->protocol, device->protocol);
2686		cts->protocol = device->protocol;
2687	}
2688
2689	if (cts->protocol_version > device->protocol_version) {
2690		if (bootverbose) {
2691			xpt_print(path, "Down reving Protocol "
2692			    "Version from %d to %d?\n", cts->protocol_version,
2693			    device->protocol_version);
2694		}
2695		cts->protocol_version = device->protocol_version;
2696	}
2697
2698	if (cts->transport == XPORT_UNKNOWN
2699	 || cts->transport == XPORT_UNSPECIFIED) {
2700		cts->transport = device->transport;
2701		cts->transport_version = device->transport_version;
2702	}
2703
2704	if (cts->transport_version == XPORT_VERSION_UNKNOWN
2705	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
2706		cts->transport_version = device->transport_version;
2707
2708	if (cts->transport != device->transport) {
2709		xpt_print(path, "Uninitialized Transport %x:%x?\n",
2710		    cts->transport, device->transport);
2711		cts->transport = device->transport;
2712	}
2713
2714	if (cts->transport_version > device->transport_version) {
2715		if (bootverbose) {
2716			xpt_print(path, "Down reving Transport "
2717			    "Version from %d to %d?\n", cts->transport_version,
2718			    device->transport_version);
2719		}
2720		cts->transport_version = device->transport_version;
2721	}
2722
2723	/*
2724	 * Nothing more of interest to do unless
2725	 * this is a device connected via the
2726	 * SCSI protocol.
2727	 */
2728	if (cts->protocol != PROTO_SCSI) {
2729		if (async_update == FALSE)
2730			xpt_action_default((union ccb *)cts);
2731		return;
2732	}
2733
2734	inq_data = &device->inq_data;
2735	scsi = &cts->proto_specific.scsi;
2736	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2737	cpi.ccb_h.func_code = XPT_PATH_INQ;
2738	xpt_action((union ccb *)&cpi);
2739
2740	/* SCSI specific sanity checking */
2741	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
2742	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
2743	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
2744	 || (device->mintags == 0)) {
2745		/*
2746		 * Can't tag on hardware that doesn't support tags,
2747		 * doesn't have it enabled, or has broken tag support.
2748		 */
2749		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2750	}
2751
2752	if (async_update == FALSE) {
2753		/*
2754		 * Perform sanity checking against what the
2755		 * controller and device can do.
2756		 */
2757		xpt_setup_ccb(&cur_cts.ccb_h, path, CAM_PRIORITY_NONE);
2758		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2759		cur_cts.type = cts->type;
2760		xpt_action((union ccb *)&cur_cts);
2761		if (cam_ccb_status((union ccb *)&cur_cts) != CAM_REQ_CMP) {
2762			return;
2763		}
2764		cur_scsi = &cur_cts.proto_specific.scsi;
2765		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
2766			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2767			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
2768		}
2769		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
2770			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2771	}
2772
2773	/* SPI specific sanity checking */
2774	if (cts->transport == XPORT_SPI && async_update == FALSE) {
2775		u_int spi3caps;
2776		struct ccb_trans_settings_spi *spi;
2777		struct ccb_trans_settings_spi *cur_spi;
2778
2779		spi = &cts->xport_specific.spi;
2780
2781		cur_spi = &cur_cts.xport_specific.spi;
2782
2783		/* Fill in any gaps in what the user gave us */
2784		if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2785			spi->sync_period = cur_spi->sync_period;
2786		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2787			spi->sync_period = 0;
2788		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2789			spi->sync_offset = cur_spi->sync_offset;
2790		if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2791			spi->sync_offset = 0;
2792		if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2793			spi->ppr_options = cur_spi->ppr_options;
2794		if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2795			spi->ppr_options = 0;
2796		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2797			spi->bus_width = cur_spi->bus_width;
2798		if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2799			spi->bus_width = 0;
2800		if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
2801			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2802			spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
2803		}
2804		if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
2805			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2806		if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2807		  && (inq_data->flags & SID_Sync) == 0
2808		  && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2809		 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)) {
2810			/* Force async */
2811			spi->sync_period = 0;
2812			spi->sync_offset = 0;
2813		}
2814
2815		switch (spi->bus_width) {
2816		case MSG_EXT_WDTR_BUS_32_BIT:
2817			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2818			  || (inq_data->flags & SID_WBus32) != 0
2819			  || cts->type == CTS_TYPE_USER_SETTINGS)
2820			 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
2821				break;
2822			/* Fall Through to 16-bit */
2823		case MSG_EXT_WDTR_BUS_16_BIT:
2824			if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2825			  || (inq_data->flags & SID_WBus16) != 0
2826			  || cts->type == CTS_TYPE_USER_SETTINGS)
2827			 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
2828				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2829				break;
2830			}
2831			/* Fall Through to 8-bit */
2832		default: /* New bus width?? */
2833		case MSG_EXT_WDTR_BUS_8_BIT:
2834			/* All targets can do this */
2835			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2836			break;
2837		}
2838
2839		spi3caps = cpi.xport_specific.spi.ppr_options;
2840		if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2841		 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2842			spi3caps &= inq_data->spi3data;
2843
2844		if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
2845			spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2846
2847		if ((spi3caps & SID_SPI_IUS) == 0)
2848			spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
2849
2850		if ((spi3caps & SID_SPI_QAS) == 0)
2851			spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
2852
2853		/* No SPI Transfer settings are allowed unless we are wide */
2854		if (spi->bus_width == 0)
2855			spi->ppr_options = 0;
2856
2857		if ((spi->valid & CTS_SPI_VALID_DISC)
2858		 && ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0)) {
2859			/*
2860			 * Can't tag queue without disconnection.
2861			 */
2862			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2863			scsi->valid |= CTS_SCSI_VALID_TQ;
2864		}
2865
2866		/*
2867		 * If we are currently performing tagged transactions to
2868		 * this device and want to change its negotiation parameters,
2869		 * go non-tagged for a bit to give the controller a chance to
2870		 * negotiate unhampered by tag messages.
2871		 */
2872		if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2873		 && (device->inq_flags & SID_CmdQue) != 0
2874		 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2875		 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
2876				   CTS_SPI_VALID_SYNC_OFFSET|
2877				   CTS_SPI_VALID_BUS_WIDTH)) != 0)
2878			scsi_toggle_tags(path);
2879	}
2880
2881	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2882	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
2883		int device_tagenb;
2884
2885		/*
2886		 * If we are transitioning from tags to no-tags or
2887		 * vice-versa, we need to carefully freeze and restart
2888		 * the queue so that we don't overlap tagged and non-tagged
2889		 * commands.  We also temporarily stop tags if there is
2890		 * a change in transfer negotiation settings to allow
2891		 * "tag-less" negotiation.
2892		 */
2893		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2894		 || (device->inq_flags & SID_CmdQue) != 0)
2895			device_tagenb = TRUE;
2896		else
2897			device_tagenb = FALSE;
2898
2899		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2900		  && device_tagenb == FALSE)
2901		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
2902		  && device_tagenb == TRUE)) {
2903
2904			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
2905				/*
2906				 * Delay change to use tags until after a
2907				 * few commands have gone to this device so
2908				 * the controller has time to perform transfer
2909				 * negotiations without tagged messages getting
2910				 * in the way.
2911				 */
2912				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
2913				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
2914			} else {
2915				xpt_stop_tags(path);
2916			}
2917		}
2918	}
2919	if (async_update == FALSE)
2920		xpt_action_default((union ccb *)cts);
2921}
2922
2923static void
2924scsi_toggle_tags(struct cam_path *path)
2925{
2926	struct cam_ed *dev;
2927
2928	/*
2929	 * Give controllers a chance to renegotiate
2930	 * before starting tag operations.  We
2931	 * "toggle" tagged queuing off then on
2932	 * which causes the tag enable command delay
2933	 * counter to come into effect.
2934	 */
2935	dev = path->device;
2936	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2937	 || ((dev->inq_flags & SID_CmdQue) != 0
2938 	  && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
2939		struct ccb_trans_settings cts;
2940
2941		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2942		cts.protocol = PROTO_SCSI;
2943		cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
2944		cts.transport = XPORT_UNSPECIFIED;
2945		cts.transport_version = XPORT_VERSION_UNSPECIFIED;
2946		cts.proto_specific.scsi.flags = 0;
2947		cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
2948		scsi_set_transfer_settings(&cts, path,
2949					  /*async_update*/TRUE);
2950		cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
2951		scsi_set_transfer_settings(&cts, path,
2952					  /*async_update*/TRUE);
2953	}
2954}
2955
2956/*
2957 * Handle any per-device event notifications that require action by the XPT.
2958 */
2959static void
2960scsi_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
2961	      struct cam_ed *device, void *async_arg)
2962{
2963	cam_status status;
2964	struct cam_path newpath;
2965
2966	/*
2967	 * We only need to handle events for real devices.
2968	 */
2969	if (target->target_id == CAM_TARGET_WILDCARD
2970	 || device->lun_id == CAM_LUN_WILDCARD)
2971		return;
2972
2973	/*
2974	 * We need our own path with wildcards expanded to
2975	 * handle certain types of events.
2976	 */
2977	if ((async_code == AC_SENT_BDR)
2978	 || (async_code == AC_BUS_RESET)
2979	 || (async_code == AC_INQ_CHANGED))
2980		status = xpt_compile_path(&newpath, NULL,
2981					  bus->path_id,
2982					  target->target_id,
2983					  device->lun_id);
2984	else
2985		status = CAM_REQ_CMP_ERR;
2986
2987	if (status == CAM_REQ_CMP) {
2988
2989		/*
2990		 * Allow transfer negotiation to occur in a
2991		 * tag free environment and after settle delay.
2992		 */
2993		if (async_code == AC_SENT_BDR
2994		 || async_code == AC_BUS_RESET) {
2995			cam_freeze_devq(&newpath);
2996			cam_release_devq(&newpath,
2997				RELSIM_RELEASE_AFTER_TIMEOUT,
2998				/*reduction*/0,
2999				/*timeout*/scsi_delay,
3000				/*getcount_only*/0);
3001			scsi_toggle_tags(&newpath);
3002		}
3003
3004		if (async_code == AC_INQ_CHANGED) {
3005			/*
3006			 * We've sent a start unit command, or
3007			 * something similar to a device that
3008			 * may have caused its inquiry data to
3009			 * change. So we re-scan the device to
3010			 * refresh the inquiry data for it.
3011			 */
3012			scsi_scan_lun(newpath.periph, &newpath,
3013				     CAM_EXPECT_INQ_CHANGE, NULL);
3014		}
3015		xpt_release_path(&newpath);
3016	} else if (async_code == AC_LOST_DEVICE &&
3017	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
3018		device->flags |= CAM_DEV_UNCONFIGURED;
3019		xpt_release_device(device);
3020	} else if (async_code == AC_TRANSFER_NEG) {
3021		struct ccb_trans_settings *settings;
3022		struct cam_path path;
3023
3024		settings = (struct ccb_trans_settings *)async_arg;
3025		xpt_compile_path(&path, NULL, bus->path_id, target->target_id,
3026				 device->lun_id);
3027		scsi_set_transfer_settings(settings, &path,
3028					  /*async_update*/TRUE);
3029		xpt_release_path(&path);
3030	}
3031}
3032
3033static void
3034scsi_announce_periph(struct cam_periph *periph)
3035{
3036	struct	ccb_pathinq cpi;
3037	struct	ccb_trans_settings cts;
3038	struct	cam_path *path = periph->path;
3039	u_int	speed;
3040	u_int	freq;
3041	u_int	mb;
3042
3043	cam_periph_assert(periph, MA_OWNED);
3044
3045	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
3046	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
3047	cts.type = CTS_TYPE_CURRENT_SETTINGS;
3048	xpt_action((union ccb*)&cts);
3049	if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP)
3050		return;
3051	/* Ask the SIM for its base transfer speed */
3052	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3053	cpi.ccb_h.func_code = XPT_PATH_INQ;
3054	xpt_action((union ccb *)&cpi);
3055	/* Report connection speed */
3056	speed = cpi.base_transfer_speed;
3057	freq = 0;
3058	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
3059		struct	ccb_trans_settings_spi *spi =
3060		    &cts.xport_specific.spi;
3061
3062		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
3063		  && spi->sync_offset != 0) {
3064			freq = scsi_calc_syncsrate(spi->sync_period);
3065			speed = freq;
3066		}
3067		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
3068			speed *= (0x01 << spi->bus_width);
3069	}
3070	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
3071		struct	ccb_trans_settings_fc *fc =
3072		    &cts.xport_specific.fc;
3073
3074		if (fc->valid & CTS_FC_VALID_SPEED)
3075			speed = fc->bitrate;
3076	}
3077	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SAS) {
3078		struct	ccb_trans_settings_sas *sas =
3079		    &cts.xport_specific.sas;
3080
3081		if (sas->valid & CTS_SAS_VALID_SPEED)
3082			speed = sas->bitrate;
3083	}
3084	mb = speed / 1000;
3085	if (mb > 0)
3086		printf("%s%d: %d.%03dMB/s transfers",
3087		       periph->periph_name, periph->unit_number,
3088		       mb, speed % 1000);
3089	else
3090		printf("%s%d: %dKB/s transfers", periph->periph_name,
3091		       periph->unit_number, speed);
3092	/* Report additional information about SPI connections */
3093	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
3094		struct	ccb_trans_settings_spi *spi;
3095
3096		spi = &cts.xport_specific.spi;
3097		if (freq != 0) {
3098			printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
3099			       freq % 1000,
3100			       (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
3101			     ? " DT" : "",
3102			       spi->sync_offset);
3103		}
3104		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
3105		 && spi->bus_width > 0) {
3106			if (freq != 0) {
3107				printf(", ");
3108			} else {
3109				printf(" (");
3110			}
3111			printf("%dbit)", 8 * (0x01 << spi->bus_width));
3112		} else if (freq != 0) {
3113			printf(")");
3114		}
3115	}
3116	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
3117		struct	ccb_trans_settings_fc *fc;
3118
3119		fc = &cts.xport_specific.fc;
3120		if (fc->valid & CTS_FC_VALID_WWNN)
3121			printf(" WWNN 0x%llx", (long long) fc->wwnn);
3122		if (fc->valid & CTS_FC_VALID_WWPN)
3123			printf(" WWPN 0x%llx", (long long) fc->wwpn);
3124		if (fc->valid & CTS_FC_VALID_PORT)
3125			printf(" PortID 0x%x", fc->port);
3126	}
3127	printf("\n");
3128}
3129
3130static void
3131scsi_proto_announce(struct cam_ed *device)
3132{
3133	scsi_print_inquiry(&device->inq_data);
3134}
3135
3136static void
3137scsi_proto_denounce(struct cam_ed *device)
3138{
3139	scsi_print_inquiry_short(&device->inq_data);
3140}
3141
3142static void
3143scsi_proto_debug_out(union ccb *ccb)
3144{
3145	char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
3146	struct cam_ed *device;
3147
3148	if (ccb->ccb_h.func_code != XPT_SCSI_IO)
3149		return;
3150
3151	device = ccb->ccb_h.path->device;
3152	CAM_DEBUG(ccb->ccb_h.path,
3153	    CAM_DEBUG_CDB,("%s. CDB: %s\n",
3154		scsi_op_desc(ccb->csio.cdb_io.cdb_bytes[0], &device->inq_data),
3155		scsi_cdb_string(ccb->csio.cdb_io.cdb_bytes, cdb_str, sizeof(cdb_str))));
3156}
3157