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