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