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