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