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