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