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