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