xref: /illumos-gate/usr/src/uts/common/io/scsi/conf/scsi_confsubr.c (revision cd3e933325e68e23516a196a8fea7f49b1e497c3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 /*
26  * Utility SCSI configuration routines
27  */
28 /*
29  * Many routines in this file have built in parallel bus assumption
30  * which might need to change as other interconnect evolve.
31  */
32 
33 #include <sys/scsi/scsi.h>
34 #include <sys/modctl.h>
35 #include <sys/bitmap.h>
36 #include <sys/fm/protocol.h>
37 
38 /*
39  * macro for filling in lun value for scsi-1 support
40  */
41 
42 #define	FILL_SCSI1_LUN(sd, pkt) \
43 	if ((sd->sd_address.a_lun > 0) && \
44 	    (sd->sd_inq->inq_ansi == 0x1)) { \
45 		((union scsi_cdb *)(pkt)->pkt_cdbp)->scc_lun = \
46 		    sd->sd_address.a_lun; \
47 	}
48 
49 extern struct mod_ops mod_miscops;
50 
51 static struct modlmisc modlmisc = {
52 	&mod_miscops,	/* Type of module */
53 	"SCSI Bus Utility Routines"
54 };
55 
56 static struct modlinkage modlinkage = {
57 	MODREV_1, (void *)&modlmisc, NULL
58 };
59 
60 /*
61  * Contexts from which we call scsi_test
62  */
63 enum scsi_test_ctxt {
64 	/*
65 	 * Those in scsi_hba_probe_pi()
66 	 */
67 	STC_PROBE_FIRST_INQ,
68 	STC_PROBE_FIRST_INQ_RETRY,
69 	STC_PROBE_PARTIAL_SUCCESS,
70 	STC_PROBE_RQSENSE1,
71 	STC_PROBE_CHK_CLEARED,
72 	STC_PROBE_RQSENSE2,
73 	STC_PROBE_INQ_FINAL,
74 	/*
75 	 * Those in check_vpd_page_support8083()
76 	 */
77 	STC_VPD_CHECK,
78 	/*
79 	 * Those in scsi_device_identity()
80 	 */
81 	STC_IDENTITY_PG80,
82 	STC_IDENTITY_PG83,
83 };
84 
85 static void create_inquiry_props(struct scsi_device *);
86 
87 static int scsi_check_ss2_LUN_limit(struct scsi_device *);
88 static void scsi_establish_LUN_limit(struct scsi_device *);
89 static void scsi_update_parent_ss2_prop(dev_info_t *, int, int);
90 
91 static int check_vpd_page_support8083(struct scsi_device *sd,
92 		int (*callback)(), int *, int *);
93 static int send_scsi_INQUIRY(struct scsi_device *sd,
94 		int (*callback)(), uchar_t *bufaddr, size_t buflen,
95 		uchar_t evpd, uchar_t page_code, size_t *lenp,
96 		enum scsi_test_ctxt);
97 
98 /*
99  * this int-array HBA-node property keeps track of strictly SCSI-2
100  * target IDs
101  */
102 #define	SS2_LUN0_TGT_LIST_PROP	"ss2-targets"
103 
104 /*
105  * for keeping track of nodes for which we do *NOT* want to probe above LUN 7
106  * (i.e. strict SCSI-2 targets)
107  *
108  * note that we could also keep track of dtype (SCSI device type) and
109  * ANSI (SCSI standard conformance level), but all currently-known cases of
110  * this problem are on SCSI-2 PROCESSOR device types
111  */
112 typedef struct ss2_lun0_info {
113 	const char	*sli_vid;	/* SCSI inquiry VID */
114 	const char	*sli_pid;	/* SCSI inquiry PID */
115 	const char	*sli_rev;	/* SCSI inquiry REV */
116 } ss2_lun0_info_t;
117 
118 /*
119  * these two workarounds are for the SCSI-2 GEM2* chips used in the
120  * D1000 and D240
121  */
122 #define	SES_D1000_VID		"SYMBIOS"
123 #define	SES_D1000_PID		"D1000"		/* the D1000 */
124 #define	SES_D1000_REV		"2"
125 
126 #define	SES_D240_VID		"SUN"
127 #define	SES_D240_PID		"D240"		/* the D240 */
128 #define	SES_D240_REV		"2"
129 
130 /*
131  * a static list of targets where we do *not* want to probe above LUN 7
132  */
133 static const ss2_lun0_info_t	scsi_probe_strict_s2_list[] = {
134 	{SES_D1000_VID, SES_D1000_PID, SES_D1000_REV},
135 	{SES_D240_VID, SES_D240_PID, SES_D240_REV},
136 };
137 
138 static const int		scsi_probe_strict_s2_size =
139 	sizeof (scsi_probe_strict_s2_list) / sizeof (struct ss2_lun0_info);
140 
141 
142 #ifdef	DEBUG
143 
144 int	scsi_probe_debug = 0;
145 
146 #define	SCSI_PROBE_DEBUG0(l, s)		\
147 		if (scsi_probe_debug >= (l)) printf(s)
148 #define	SCSI_PROBE_DEBUG1(l, s, a1)	\
149 		if (scsi_probe_debug >= (l)) printf(s, a1)
150 #define	SCSI_PROBE_DEBUG2(l, s, a1, a2)	\
151 		if (scsi_probe_debug >= (l)) printf(s, a1, a2)
152 #define	SCSI_PROBE_DEBUG3(l, s, a1, a2, a3)	\
153 		if (scsi_probe_debug >= (l)) printf(s, a1, a2, a3)
154 
155 #else	/* DEBUG */
156 
157 #define	SCSI_PROBE_DEBUG0(l, s)
158 #define	SCSI_PROBE_DEBUG1(l, s, a1)
159 #define	SCSI_PROBE_DEBUG2(l, s, a1, a2)
160 #define	SCSI_PROBE_DEBUG3(l, s, a1, a2, a3)
161 
162 #endif	/* DEBUG */
163 
164 int	scsi_test_busy_timeout = SCSI_POLL_TIMEOUT;	/* in seconds */
165 int	scsi_test_busy_delay = 10000;			/* 10msec in usec */
166 
167 
168 /*
169  * Returns from scsi_test.
170  *
171  * SCSI_TEST_CMPLT_GOOD => TRAN_ACCEPT, CMD_CMPLT, STATUS_GOOD
172  *
173  * SCSI_TEST_CMPLT_BUSY => TRAN_ACCEPT, CMD_CMPLT, STATUS_BUSY
174  *
175  * SCSI_TEST_CMPLT_CHECK => TRAN_ACCEPT, CMD_CMPLT, STATUS_CHECK
176  *
177  * SCSI_TEST_CMPLT_OTHER => TRAN_ACCEPT, CMD_CMPLT, !STATUS_{GOOD,BUSY,CHECK}
178  *
179  * SCSI_TEST_CMD_INCOMPLETE => TRAN_ACCEPT, CMD_INCOMPLETE
180  *
181  * SCSI_TEST_NOTCMPLT => TRAN_ACCEPT, pkt_reason != CMD_{CMPLT,INCOMPLETE}
182  *
183  * SCSI_TEST_TRAN_BUSY => (Repeated) TRAN_BUSY from attempt scsi_transport
184  *
185  * SCSI_TEST_TRAN_REJECT => TRAN_BADPKT or TRAN_FATAL_ERROR
186  *
187  */
188 #define	SCSI_TEST_CMPLT_GOOD		0x01U
189 #define	SCSI_TEST_CMPLT_BUSY		0x02U
190 #define	SCSI_TEST_CMPLT_CHECK		0x04U
191 #define	SCSI_TEST_CMPLT_OTHER		0x08U
192 
193 #define	SCSI_TEST_CMPLTMASK \
194 	(SCSI_TEST_CMPLT_GOOD | SCSI_TEST_CMPLT_BUSY | \
195 	SCSI_TEST_CMPLT_CHECK | SCSI_TEST_CMPLT_OTHER)
196 
197 #define	SCSI_TEST_PARTCMPLTMASK \
198 	(SCSI_TEST_CMPLTMASK & ~SCSI_TEST_CMPLT_GOOD)
199 
200 #define	SCSI_TEST_CMD_INCOMPLETE	0x10U
201 #define	SCSI_TEST_NOTCMPLT		0x20U
202 #define	SCSI_TEST_TRAN_BUSY		0x40U
203 #define	SCSI_TEST_TRAN_REJECT		0x80U
204 
205 #define	SCSI_TEST_FAILMASK \
206 	(SCSI_TEST_CMD_INCOMPLETE | SCSI_TEST_NOTCMPLT | \
207 	SCSI_TEST_TRAN_BUSY | SCSI_TEST_TRAN_REJECT)
208 
209 #define	SCSI_TEST_FAILURE(x) (((x) & SCSI_TEST_FAILMASK) != 0)
210 
211 /*
212  * architecture dependent allocation restrictions. For x86, we'll set
213  * dma_attr_addr_hi to scsi_max_phys_addr and dma_attr_sgllen to
214  * scsi_sgl_size during _init().
215  */
216 #if defined(__sparc)
217 ddi_dma_attr_t scsi_alloc_attr = {
218 	DMA_ATTR_V0,	/* version number */
219 	0x0,		/* lowest usable address */
220 	0xFFFFFFFFull,	/* high DMA address range */
221 	0xFFFFFFFFull,	/* DMA counter register */
222 	1,		/* DMA address alignment */
223 	1,		/* DMA burstsizes */
224 	1,		/* min effective DMA size */
225 	0xFFFFFFFFull,	/* max DMA xfer size */
226 	0xFFFFFFFFull,	/* segment boundary */
227 	1,		/* s/g list length */
228 	512,		/* granularity of device */
229 	0		/* DMA transfer flags */
230 };
231 #elif defined(__x86)
232 ddi_dma_attr_t scsi_alloc_attr = {
233 	DMA_ATTR_V0,	/* version number */
234 	0x0,		/* lowest usable address */
235 	0x0,		/* high DMA address range [set in _init()] */
236 	0xFFFFull,	/* DMA counter register */
237 	1,		/* DMA address alignment */
238 	1,		/* DMA burstsizes */
239 	1,		/* min effective DMA size */
240 	0xFFFFFFFFull,	/* max DMA xfer size */
241 	0xFFFFFFFFull,  /* segment boundary */
242 	0,		/* s/g list length */
243 	512,		/* granularity of device [set in _init()] */
244 	0		/* DMA transfer flags */
245 };
246 uint64_t scsi_max_phys_addr = 0xFFFFFFFFull;
247 int scsi_sgl_size = 0xFF;
248 #endif
249 
250 ulong_t	*scsi_pkt_bad_alloc_bitmap;
251 
252 int
253 _init()
254 {
255 	scsi_initialize_hba_interface();
256 	scsi_watch_init();
257 
258 #if defined(__x86)
259 	/* set the max physical address for iob allocs on x86 */
260 	scsi_alloc_attr.dma_attr_addr_hi = scsi_max_phys_addr;
261 
262 	/*
263 	 * set the sgllen for iob allocs on x86. If this is set less than
264 	 * the number of pages the buffer will take (taking into account
265 	 * alignment), it would force the allocator to try and allocate
266 	 * contiguous pages.
267 	 */
268 	scsi_alloc_attr.dma_attr_sgllen = scsi_sgl_size;
269 #endif
270 
271 	/* bitmap to limit scsi_pkt allocation violation messages */
272 	scsi_pkt_bad_alloc_bitmap = kmem_zalloc(BT_SIZEOFMAP(devcnt), KM_SLEEP);
273 
274 	return (mod_install(&modlinkage));
275 }
276 
277 /*
278  * there is no _fini() routine because this module is never unloaded
279  */
280 
281 int
282 _info(struct modinfo *modinfop)
283 {
284 	return (mod_info(&modlinkage, modinfop));
285 }
286 
287 #define	ROUTE	(&sd->sd_address)
288 
289 static int
290 scsi_slave_do_rqsense(struct scsi_device *sd, int (*callback)())
291 {
292 	struct scsi_pkt *rq_pkt = NULL;
293 	struct buf *rq_bp = NULL;
294 	int rval = SCSIPROBE_EXISTS;
295 
296 	/*
297 	 * prepare rqsense packet
298 	 */
299 	rq_bp = scsi_alloc_consistent_buf(ROUTE, (struct buf *)NULL,
300 	    (uint_t)SENSE_LENGTH, B_READ, callback, NULL);
301 	if (rq_bp == NULL) {
302 		rval = SCSIPROBE_NOMEM;
303 		goto out;
304 	}
305 
306 	rq_pkt = scsi_init_pkt(ROUTE, (struct scsi_pkt *)NULL,
307 	    rq_bp, CDB_GROUP0, 1, 0, PKT_CONSISTENT,
308 	    callback, NULL);
309 
310 	if (rq_pkt == NULL) {
311 		if (rq_bp->b_error == 0)
312 			rval = SCSIPROBE_NOMEM_CB;
313 		else
314 			rval = SCSIPROBE_NOMEM;
315 		goto out;
316 	}
317 	ASSERT(rq_bp->b_error == 0);
318 
319 	(void) scsi_setup_cdb((union scsi_cdb *)rq_pkt->
320 	    pkt_cdbp, SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0);
321 	FILL_SCSI1_LUN(sd, rq_pkt);
322 	rq_pkt->pkt_flags = FLAG_NOINTR|FLAG_NOPARITY|FLAG_SENSING;
323 
324 	/*
325 	 * The controller type is as yet unknown, so we
326 	 * have to do a throwaway non-extended request sense,
327 	 * and hope that that clears the check condition
328 	 * for that unit until we can find out what kind
329 	 * of drive it is. A non-extended request sense
330 	 * is specified by stating that the sense block
331 	 * has 0 length, which is taken to mean that it
332 	 * is four bytes in length.
333 	 */
334 	if (scsi_poll(rq_pkt) < 0) {
335 		rval = SCSIPROBE_FAILURE;
336 	}
337 
338 out:
339 	if (rq_pkt) {
340 		scsi_destroy_pkt(rq_pkt);
341 	}
342 	if (rq_bp) {
343 		scsi_free_consistent_buf(rq_bp);
344 	}
345 
346 	return (rval);
347 }
348 
349 /*
350  *
351  * SCSI slave probe routine - provided as a service to target drivers
352  *
353  * Mostly attempts to allocate and fill sd inquiry data..
354  */
355 
356 int
357 scsi_slave(struct scsi_device *sd, int (*callback)())
358 {
359 	struct scsi_pkt	*pkt;
360 	int		rval = SCSIPROBE_EXISTS;
361 
362 	/*
363 	 * the first test unit ready will tell us whether a target
364 	 * responded and if there was one, it will clear the unit attention
365 	 * condition
366 	 */
367 	pkt = scsi_init_pkt(ROUTE, (struct scsi_pkt *)NULL, NULL,
368 	    CDB_GROUP0, sizeof (struct scsi_arq_status), 0, 0, callback, NULL);
369 
370 	if (pkt == NULL) {
371 		return (SCSIPROBE_NOMEM_CB);
372 	}
373 
374 	(void) scsi_setup_cdb((union scsi_cdb *)pkt->pkt_cdbp,
375 	    SCMD_TEST_UNIT_READY, 0, 0, 0);
376 	FILL_SCSI1_LUN(sd, pkt);
377 	pkt->pkt_flags = FLAG_NOINTR|FLAG_NOPARITY;
378 
379 	if (scsi_poll(pkt) < 0) {
380 		if (pkt->pkt_reason == CMD_INCOMPLETE)
381 			rval = SCSIPROBE_NORESP;
382 		else
383 			rval = SCSIPROBE_FAILURE;
384 
385 		if ((pkt->pkt_state & STATE_ARQ_DONE) == 0) {
386 			if (((struct scsi_status *)pkt->pkt_scbp)->sts_chk)
387 				/*
388 				 * scanner and processor devices can return a
389 				 * check condition here
390 				 */
391 				rval = scsi_slave_do_rqsense(sd, callback);
392 		}
393 
394 		if (rval != SCSIPROBE_EXISTS) {
395 			scsi_destroy_pkt(pkt);
396 			return (rval);
397 		}
398 	}
399 
400 	/*
401 	 * the second test unit ready, allows the host adapter to negotiate
402 	 * synchronous transfer period and offset
403 	 */
404 	if (scsi_poll(pkt) < 0) {
405 		if (pkt->pkt_reason == CMD_INCOMPLETE)
406 			rval = SCSIPROBE_NORESP;
407 		else
408 			rval = SCSIPROBE_FAILURE;
409 	}
410 
411 	/*
412 	 * do a rqsense if there was a check condition and ARQ was not done
413 	 */
414 	if ((pkt->pkt_state & STATE_ARQ_DONE) == 0) {
415 		if (((struct scsi_status *)pkt->pkt_scbp)->sts_chk) {
416 			rval = scsi_slave_do_rqsense(sd, callback);
417 		}
418 	}
419 
420 	/*
421 	 * call scsi_probe to do the inquiry
422 	 *
423 	 * NOTE: there is minor difference with the old scsi_slave
424 	 * implementation: busy conditions are not handled in scsi_probe.
425 	 */
426 	scsi_destroy_pkt(pkt);
427 	if (rval == SCSIPROBE_EXISTS) {
428 		return (scsi_probe(sd, callback));
429 	} else {
430 		return (rval);
431 	}
432 }
433 
434 /*
435  * Undo scsi_slave - older interface, but still supported
436  *
437  * NOTE: The 'sd_inq' inquiry data is now freed by scsi_hba/scsi_vhci code
438  * as part of free of scsi_device(9S).
439  */
440 /*ARGSUSED*/
441 void
442 scsi_unslave(struct scsi_device *sd)
443 {
444 }
445 
446 /*
447  * Undo scsi_probe
448  *
449  * NOTE: The 'sd_inq' inquiry data is now freed by scsi_hba/scsi_vhci code
450  * as part of free of scsi_device(9S).
451  */
452 /*ARGSUSED*/
453 void
454 scsi_unprobe(struct scsi_device *sd)
455 {
456 }
457 
458 /*
459  * We log all scsi_test failures (as long as we are SE_HP etc).  The
460  * following table controls the "driver-assessment" payload item
461  * in the ereports we raise.  If a scsi_test return features in the
462  * retry mask then the calling context will retry; if it features in
463  * the fatal mask then the caller will not retry (although higher-level
464  * software might); if in neither (which shouldn't happen - you either
465  * retry or give up) default to 'retry'.
466  */
467 static const struct scsi_test_profile {
468 	enum scsi_test_ctxt stp_ctxt;	/* Calling context */
469 	uint32_t stp_retrymask;		/* Returns caller will retry for */
470 	uint32_t stp_fatalmask;		/* Returns caller considers fatal */
471 } scsi_test_profile[] = {
472 	/*
473 	 * This caller will retry on SCSI_TEST_FAILMASK as long as it was
474 	 * not SCSI_TEST_CMD_INCOMPLETE which is terminal.  A return from
475 	 * SCSI_TEST_PARTCMPLTMASK (command complete but status other than
476 	 * STATUS_GOOD) is not terminal and we'll move on to the context
477 	 * of STC_PROBE_PARTIAL_SUCCESS so that's a retry, too.
478 	 */
479 	{
480 		STC_PROBE_FIRST_INQ,
481 		SCSI_TEST_FAILMASK & ~SCSI_TEST_CMD_INCOMPLETE |
482 		    SCSI_TEST_PARTCMPLTMASK,
483 		SCSI_TEST_CMD_INCOMPLETE
484 	},
485 
486 	/*
487 	 * If the first inquiry fails outright we always retry just once
488 	 * (except for SCSI_TEST_CMD_INCOMPLETE as above).  A return in
489 	 * SCSI_TEST_FAILMASK is terminal; for SCSI_TEST_PARTCMPLTMASK
490 	 * we will retry at STC_PROBE_PARTIAL_SUCCESS.
491 	 */
492 	{
493 		STC_PROBE_FIRST_INQ_RETRY,
494 		SCSI_TEST_PARTCMPLTMASK,
495 		SCSI_TEST_FAILMASK
496 	},
497 
498 	/*
499 	 * If we've met with partial success we retry at caller context
500 	 * STC_PROBE_PARTIAL_SUCCESS.  Any SCSI_TEST_FAILMASK return
501 	 * here is terminal, as too is SCSI_TEST_CMPLT_BUSY.  A return in
502 	 * SCSI_TEST_PARTCMPLTMASK and we will continue with further
503 	 * inquiry attempts.
504 	 */
505 	{
506 		STC_PROBE_PARTIAL_SUCCESS,
507 		SCSI_TEST_PARTCMPLTMASK & ~SCSI_TEST_CMPLT_BUSY,
508 		SCSI_TEST_FAILMASK | SCSI_TEST_CMPLT_BUSY
509 	},
510 
511 	/*
512 	 * If we get past the above target busy case then we will
513 	 * perform a sense request if scsi_test indicates STATUS_CHECK
514 	 * and ARQ was not done.  We are not interested in logging telemetry
515 	 * for transports that do not perform ARQ automatically.
516 	 */
517 	{
518 		STC_PROBE_RQSENSE1,
519 		0,
520 		0
521 	},
522 
523 	/*
524 	 * If "something" responded to the probe but then the next inquiry
525 	 * sees a change of heart then we fail the probe on any of
526 	 * SCSI_TEST_FAILMASK or SCSI_TEST_CMPLT_BUSY.  For other values
527 	 * in SCSI_TEST_PARTCMPLTMASK we soldier on.
528 	 */
529 	{
530 		STC_PROBE_CHK_CLEARED,
531 		SCSI_TEST_PARTCMPLTMASK & ~SCSI_TEST_CMPLT_BUSY,
532 		SCSI_TEST_FAILMASK | SCSI_TEST_CMPLT_BUSY
533 	},
534 
535 	/*
536 	 * If after all that there we still have STATUS_CHECK from the
537 	 * inquiry status then we resend the sense request but the
538 	 * result is ignored (just clearing the condition).  Do not
539 	 * log.
540 	 */
541 	{
542 		STC_PROBE_RQSENSE2,
543 		0,
544 		0
545 	},
546 
547 	/*
548 	 * After the above sense request we once again send an inquiry.
549 	 * If it fails outright or STATUS_CHECK persists we give up.
550 	 * Any partial result is considered success.
551 	 */
552 	{
553 		STC_PROBE_INQ_FINAL,
554 		0,
555 		SCSI_TEST_FAILMASK | SCSI_TEST_CMPLT_CHECK
556 	},
557 
558 	/*
559 	 * check_vpd_page_support8083 called from scsi_device_identity
560 	 * performs an inquiry with EVPD set (and page necessarily 0)
561 	 * to see what pages are supported.
562 	 *
563 	 * Some devices do not support this command and therefore
564 	 * check_vpd_page_support8083 only returns an error of kmem_zalloc
565 	 * fails.  If the send_scsi_INQUIRY does not meet with complete
566 	 * success (SCSI_TEST_CMPLT_GOOD) it returns -1, othewise 0.
567 	 * So any scsi_test failure here will cause us to assume no page
568 	 * 80/83 support, and we will proceed without devid support.
569 	 * So -1 returns from send_scsi_INQUIRY are not terminal.
570 	 */
571 	{
572 		STC_VPD_CHECK,
573 		0,
574 		0
575 	},
576 
577 	/*
578 	 * If the above inquiry claims pg80 support then scsi_device_identity
579 	 * will perform a send_scsi_INQUIRY to retrieve that page.
580 	 * Anything other than SCSI_TEST_CMPLT_GOOD is a failure and will
581 	 * cause scsi_device_identity to return non-zero at which point the
582 	 * caller goes to SCSIPROBE_FAILURE.
583 	 */
584 	{
585 		STC_IDENTITY_PG80,
586 		0,
587 		SCSI_TEST_FAILMASK | SCSI_TEST_CMPLTMASK
588 	},
589 
590 	/*
591 	 * Similarly for pg83
592 	 */
593 	{
594 		STC_IDENTITY_PG83,
595 		0,
596 		SCSI_TEST_FAILMASK | SCSI_TEST_CMPLTMASK
597 	}
598 };
599 
600 int scsi_test_ereport_disable = 0;
601 
602 extern int e_devid_cache_path_to_devid(char *, char *, char *, ddi_devid_t *);
603 
604 static void
605 scsi_test_ereport_post(struct scsi_pkt *pkt, enum scsi_test_ctxt ctxt,
606     uint32_t stresult)
607 {
608 	char *nodename = NULL, *devidstr_buf = NULL, *devidstr = NULL;
609 	const struct scsi_test_profile *tp = &scsi_test_profile[ctxt];
610 	char ua[SCSI_MAXNAMELEN], nodenamebuf[SCSI_MAXNAMELEN];
611 	union scsi_cdb *cdbp = (union scsi_cdb *)pkt->pkt_cdbp;
612 	struct scsi_address *ap = &pkt->pkt_address;
613 	char *tgt_port, *tpl0 = NULL;
614 	ddi_devid_t devid = NULL;
615 	dev_info_t *probe, *hba;
616 	struct scsi_device *sd;
617 	scsi_lun64_t lun64;
618 	const char *d_ass;
619 	const char *class;
620 	char *pathbuf;
621 	nvlist_t *pl;
622 	uint64_t wwn;
623 	int err = 0;
624 	int dad = 0;
625 	size_t len;
626 	int lun;
627 
628 	if (scsi_test_ereport_disable)
629 		return;
630 
631 	ASSERT(tp->stp_ctxt == ctxt);
632 
633 	if ((sd = scsi_address_device(ap)) == NULL)
634 		return;		/* Not SCSI_HBA_ADDR_COMPLEX */
635 
636 	probe = sd->sd_dev;
637 	hba = ddi_get_parent(probe);
638 
639 	/*
640 	 * We only raise telemetry for SE_HP style enumeration
641 	 */
642 	if (!ndi_dev_is_hotplug_node(hba))
643 		return;
644 
645 	/*
646 	 * scsi_fm_ereport_post will use the hba for the fm-enabled devinfo
647 	 */
648 	if (!DDI_FM_EREPORT_CAP(ddi_fm_capable(hba)))
649 		return;
650 
651 	/*
652 	 * Retrieve the unit address we were probing and the target
653 	 * port component thereof.
654 	 */
655 	if (!scsi_ua_get(sd, ua, sizeof (ua)) ||
656 	    scsi_device_prop_lookup_string(sd, SCSI_DEVICE_PROP_PATH,
657 	    SCSI_ADDR_PROP_TARGET_PORT, &tgt_port) != DDI_PROP_SUCCESS)
658 		return;
659 
660 	/*
661 	 * Determine whether unit address is location based or identity (wwn)
662 	 * based.  If we can't convert the target port address to a wwn then
663 	 * we're location based.
664 	 */
665 	if (scsi_wwnstr_to_wwn(tgt_port, &wwn) == DDI_FAILURE)
666 		return;
667 
668 	/*
669 	 * Get lun and lun64
670 	 */
671 	lun = scsi_device_prop_get_int(sd, SCSI_DEVICE_PROP_PATH,
672 	    SCSI_ADDR_PROP_LUN, 0);
673 	lun64 = scsi_device_prop_get_int64(sd, SCSI_DEVICE_PROP_PATH,
674 	    SCSI_ADDR_PROP_LUN64, lun);
675 
676 	/*
677 	 * We are guaranteed not to be in interrupt or any other
678 	 * problematic context.  So instead of repeated varargs
679 	 * style calls to scsi_fm_ereport_post for each flavour of
680 	 * ereport we have the luxury of being able to allocate
681 	 * and build an nvlist here.
682 	 *
683 	 * The ereports we raise here are all under the category
684 	 * ereport.io.scsi.cmd.disk category, namely
685 	 *
686 	 *	ereport.io.scsi.cmd.disk.
687 	 *			{dev.rqs.derr,dev.serr,tran}.
688 	 *
689 	 * For all ereports we also add the scsi_test specific payload.
690 	 * If we have it then we always include the devid in the payload
691 	 * (but only in the detector for device-as-detector ereports).
692 	 *
693 	 * Inherited From	Member Name
694 	 * -------------------- -------------------
695 	 *	.cmd		driver-assessment
696 	 *	.cmd		op-code
697 	 *	.cmd		cdb
698 	 *	.cmd		pkt-reason
699 	 *	.cmd		pkt-state
700 	 *	.cmd		pkt-stats
701 	 *	.cmd.disk	stat-code
702 	 *	-		scsi-test-return
703 	 *	-		scsi-test-context
704 	 */
705 
706 	if (nvlist_alloc(&pl, NV_UNIQUE_NAME, 0) != 0)
707 		return;
708 
709 	err |= nvlist_add_uint8(pl, "op-code", cdbp->scc_cmd);
710 	err |= nvlist_add_uint8_array(pl, "cdb", pkt->pkt_cdbp,
711 	    pkt->pkt_cdblen);
712 	err |= nvlist_add_uint8(pl, "pkt-reason", pkt->pkt_reason);
713 	err |= nvlist_add_uint32(pl, "pkt-state", pkt->pkt_state);
714 	err |= nvlist_add_uint32(pl, "pkt-stats", pkt->pkt_statistics);
715 	err |= nvlist_add_uint32(pl, "stat-code", *pkt->pkt_scbp);
716 	err |= nvlist_add_uint32(pl, "scsi-test-return", stresult);
717 	err |= nvlist_add_int32(pl, "scsi-test-context", ctxt);
718 
719 	switch (stresult) {
720 	case SCSI_TEST_CMPLT_BUSY:
721 		dad = 1;
722 		class = "cmd.disk.dev.serr";
723 		break;
724 
725 	case SCSI_TEST_CMPLT_CHECK:
726 		dad = 1;
727 
728 		if ((pkt->pkt_state & STATE_ARQ_DONE)) {
729 			struct scsi_arq_status *arqstat;
730 			uint8_t key, asc, ascq;
731 			uint8_t *sensep;
732 
733 			class = "cmd.disk.dev.rqs.derr";
734 			arqstat = (struct scsi_arq_status *)pkt->pkt_scbp;
735 			sensep = (uint8_t *)&arqstat->sts_sensedata;
736 			key = scsi_sense_key(sensep);
737 			asc = scsi_sense_asc(sensep);
738 			ascq = scsi_sense_ascq(sensep);
739 
740 			/*
741 			 * Add to payload.
742 			 */
743 			err |= nvlist_add_uint8(pl, "key", key);
744 			err |= nvlist_add_uint8(pl, "asc", asc);
745 			err |= nvlist_add_uint8(pl, "ascq", ascq);
746 			err |= nvlist_add_uint8_array(pl, "sense-data",
747 			    sensep, sizeof (arqstat->sts_sensedata));
748 		} else {
749 			class = "cmd.disk.dev.serr";
750 		}
751 
752 		break;
753 
754 	case SCSI_TEST_CMPLT_OTHER:
755 		dad = 1;
756 		class = "cmd.disk.dev.serr";
757 		break;
758 
759 	case SCSI_TEST_CMD_INCOMPLETE:
760 	case SCSI_TEST_NOTCMPLT:
761 	case SCSI_TEST_TRAN_BUSY:
762 	case SCSI_TEST_TRAN_REJECT:
763 		class = "cmd.disk.tran";
764 		break;
765 	}
766 
767 	/*
768 	 * Determine driver-assessment and add to payload.
769 	 */
770 	if (dad) {
771 		/*
772 		 * While higher level software can retry the enumeration
773 		 * the belief is that any device-as-detector style error
774 		 * will be persistent and will survive retries.  So we
775 		 * can make a local determination of driver assessment.
776 		 * Some day it may be more elegant to raise an ereport from
777 		 * scsi_tgtmap_scsi_deactivate to confirm retries failed,
778 		 * and correlate that ereport during diagnosis.
779 		 */
780 		if (stresult & tp->stp_fatalmask)
781 			d_ass = (const char *)"fatal";
782 		else if (stresult & tp->stp_retrymask)
783 			d_ass = (const char *)"retry";
784 		else
785 			d_ass = (const char *)"retry";
786 	} else {
787 		/* We do not diagnose transport errors (yet) */
788 			d_ass = (const char *)"retry";
789 	}
790 
791 	err |= nvlist_add_string(pl, "driver-assessment", d_ass);
792 
793 	/*
794 	 * If we're hoping for a device-as-detector style ereport then
795 	 * we're going to need a devid for the detector FMRI.  We
796 	 * don't have the devid because the target won't talk to us.
797 	 * But we do know which hba iport we were probing out of, and
798 	 * we know the unit address that was being probed (but not
799 	 * what type of device is or should be there).  So we
800 	 * search the devid cache for any cached devid matching
801 	 * path <iport-path>/<nodename>@<unit-address> with nodename
802 	 * wildcarded.  If a match is made we are returned not only the
803 	 * devid but also the nodename for the path that cached that
804 	 * entry.
805 	 *
806 	 * We also attempt to dig up a devid even for transport errors;
807 	 * we'll include that in the payload but not in the detector FMRI.
808 	 */
809 
810 	pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
811 	(void) ddi_pathname(hba, pathbuf);
812 
813 	if (e_devid_cache_path_to_devid(pathbuf, ua, nodenamebuf,
814 	    &devid) == DDI_SUCCESS) {
815 		nodename = nodenamebuf;
816 		devidstr = devidstr_buf = ddi_devid_str_encode(devid, NULL);
817 		kmem_free(devid, ddi_devid_sizeof(devid));
818 		err |= nvlist_add_string(pl, "devid", devidstr);
819 	}
820 
821 	/*
822 	 * If this is lun 0 we will include the target-port-l0id
823 	 * in the dev scheme detector for device-as-detector.
824 	 */
825 	if (dad && (lun == 0 || lun64 == 0))
826 		tpl0 = tgt_port;
827 
828 	/* Construct the devpath to use in the detector */
829 	(void) ddi_pathname(hba, pathbuf);
830 	len = strlen(pathbuf);
831 	(void) snprintf(pathbuf + len, MAXPATHLEN - len, "/%s@%s",
832 	    nodename ? nodename : "unknown", ua);
833 
834 	/*
835 	 * Let's review.
836 	 *
837 	 * Device-as-detector ereports for which the attempted lookup of
838 	 * devid and nodename succeeded:
839 	 *
840 	 *	- pathbuf has the full device path including nodename we
841 	 *	  dug up from the devid cache
842 	 *
843 	 *	- class is one of cmd.disk.{dev.rqs.derr,dev.serr}
844 	 *
845 	 *	- devidstr is non NULL and a valid devid string
846 	 *
847 	 * Would-be device-as-detector ereport for which the attempted lookup
848 	 * of devid failed:
849 	 *
850 	 *	- pathbuf has a device path with leaf nodename of "unknown"
851 	 *	  but still including the unit-address
852 	 *	- class is one of cmd.disk.{dev.rqs.derr,dev.serr}
853 	 *
854 	 * Transport errors:
855 	 *
856 	 *	class is cmd.disk.tran
857 	 *	devidstr is NULL
858 	 *
859 	 *	- we may have succeeded in looking up a devid and nodename -
860 	 *	  the devid we'll have added to the payload but we must not
861 	 *	  add to detector FMRI, and if we have have nodename then
862 	 *	  we have a full devpath otherwise one with "unknown" for
863 	 *	  nodename
864 	 */
865 
866 	if (err)
867 		(void) nvlist_add_boolean_value(pl, "payload-incomplete",
868 		    B_TRUE);
869 
870 	scsi_fm_ereport_post(
871 	    sd,
872 	    0,				/* path_instance - always 0 */
873 	    pathbuf,			/* devpath for detector */
874 	    class,			/* ereport class suffix */
875 	    0,				/* ENA - generate for us */
876 	    dad ? devidstr : NULL,	/* dtcr devid, dev-as-det only */
877 	    tpl0,			/* target-port-l0id */
878 	    DDI_SLEEP,
879 	    pl, /* preconstructed payload */
880 	    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
881 	    NULL);
882 
883 	nvlist_free(pl);
884 	if (devidstr_buf)
885 		ddi_devid_str_free(devidstr_buf);
886 	kmem_free(pathbuf, MAXPATHLEN);
887 }
888 
889 #ifdef	DEBUG
890 /*
891  * Testing - fake scsi_test fails
892  */
893 char scsi_test_fail_ua[SCSI_MAXNAMELEN];	/* unit address to object to */
894 int scsi_test_fail_rc = TRAN_ACCEPT;		/* scsi_transport return */
895 uchar_t scsi_test_fail_pkt_reason = CMD_CMPLT;	/* pkt_reason */
896 uchar_t scsi_test_fail_status = STATUS_BUSY;	/* status */
897 uint_t scsi_test_fail_repeat = (uint_t)-1;	/* number of times to fail ua */
898 #endif
899 
900 /*
901  * This is like scsi_poll, but only does retry for TRAN_BUSY.
902  */
903 static uint32_t
904 scsi_test(struct scsi_pkt *pkt, enum scsi_test_ctxt ctxt)
905 {
906 	uint32_t	rval;
907 	int		wait_usec;
908 	int		rc;
909 	extern int	do_polled_io;
910 
911 	pkt->pkt_flags |= FLAG_NOINTR;
912 	pkt->pkt_time = SCSI_POLL_TIMEOUT;	/* in seconds */
913 
914 	if (scsi_ifgetcap(&pkt->pkt_address, "tagged-qing", 1) == 1) {
915 		pkt->pkt_flags |= FLAG_STAG;
916 	}
917 
918 	/*
919 	 * Each TRAN_BUSY response waits scsi_test_busy_delay usec up to a
920 	 * maximum of scsi_test_busy_timeout.
921 	 */
922 	for (wait_usec = 0; (wait_usec / 1000000) <= scsi_test_busy_timeout;
923 	    wait_usec += scsi_test_busy_delay) {
924 
925 		/* Initialize pkt status variables */
926 		*pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0;
927 
928 		rc = scsi_transport(pkt);
929 		if ((rc != TRAN_BUSY) || (scsi_test_busy_delay == 0) ||
930 		    (scsi_test_busy_timeout == 0))
931 			break;
932 
933 		/* transport busy, wait */
934 		if ((curthread->t_flag & T_INTR_THREAD) == 0 && !do_polled_io) {
935 			delay(drv_usectohz(scsi_test_busy_delay));
936 		} else {
937 			/* we busy wait during cpr_dump or interrupt threads */
938 			drv_usecwait(scsi_test_busy_delay);
939 		}
940 	}
941 
942 #ifdef	DEBUG
943 	if (scsi_test_fail_ua[0] != '\0' && scsi_test_fail_repeat > 0) {
944 		struct scsi_address *ap = &pkt->pkt_address;
945 		struct scsi_device *sd;
946 		dev_info_t *probe;
947 		char ua[SCSI_MAXNAMELEN];
948 
949 		if ((sd = scsi_address_device(ap)) != NULL) {
950 			probe = sd->sd_dev;
951 
952 			if (probe && scsi_ua_get(sd, ua, sizeof (ua)) &&
953 			    strncmp(ua, scsi_test_fail_ua, sizeof (ua)) == 0) {
954 				scsi_test_fail_repeat--;
955 				rc = scsi_test_fail_rc;
956 				if (rc == TRAN_ACCEPT)
957 					pkt->pkt_reason =
958 					    scsi_test_fail_pkt_reason;
959 				*pkt->pkt_scbp = scsi_test_fail_status;
960 				if (scsi_test_fail_status == STATUS_CHECK)
961 					pkt->pkt_state |= STATE_ARQ_DONE;
962 
963 			}
964 		}
965 	}
966 #endif
967 
968 	switch (rc) {
969 	case TRAN_ACCEPT:
970 		switch (pkt->pkt_reason) {
971 		case CMD_CMPLT:
972 			switch ((*pkt->pkt_scbp) & STATUS_MASK) {
973 			case STATUS_GOOD:
974 				rval = SCSI_TEST_CMPLT_GOOD;
975 				break;
976 
977 			case STATUS_BUSY:
978 				rval = SCSI_TEST_CMPLT_BUSY;
979 				break;
980 
981 			case STATUS_CHECK:
982 				rval = SCSI_TEST_CMPLT_CHECK;
983 				break;
984 
985 			default:
986 				rval = SCSI_TEST_CMPLT_OTHER;
987 				break;
988 			}
989 			break;
990 
991 		case CMD_INCOMPLETE:
992 			rval = SCSI_TEST_CMD_INCOMPLETE;
993 			break;
994 
995 		default:
996 			rval = SCSI_TEST_NOTCMPLT;
997 			break;
998 		}
999 		break;
1000 
1001 	case TRAN_BUSY:
1002 		rval = SCSI_TEST_TRAN_BUSY;
1003 		break;
1004 
1005 	default:
1006 		rval = SCSI_TEST_TRAN_REJECT;
1007 		break;
1008 	}
1009 
1010 	if (rval != SCSI_TEST_CMPLT_GOOD)
1011 		scsi_test_ereport_post(pkt, ctxt, rval);
1012 
1013 	return (rval);
1014 }
1015 
1016 /*
1017  * The implementation of scsi_probe now allows a particular
1018  * HBA to intercept the call, for any post- or pre-processing
1019  * it may need.  The default, if the HBA does not override it,
1020  * is to call scsi_hba_probe(), which retains the old functionality
1021  * intact.
1022  */
1023 int
1024 scsi_probe(struct scsi_device *sd, int (*callback)())
1025 {
1026 	int			ret;
1027 	scsi_hba_tran_t		*tran = sd->sd_address.a_hba_tran;
1028 
1029 	if (scsi_check_ss2_LUN_limit(sd) != 0) {
1030 		/*
1031 		 * caller is trying to probe a strictly-SCSI-2 device
1032 		 * with a LUN that is too large, so do not allow it
1033 		 */
1034 		return (SCSIPROBE_NORESP);	/* skip probing this one */
1035 	}
1036 
1037 	if (tran->tran_tgt_probe != NULL) {
1038 		ret = (*tran->tran_tgt_probe)(sd, callback);
1039 	} else {
1040 		ret = scsi_hba_probe(sd, callback);
1041 	}
1042 
1043 	if (ret == SCSIPROBE_EXISTS) {
1044 		create_inquiry_props(sd);
1045 		/* is this a strictly-SCSI-2 node ?? */
1046 		scsi_establish_LUN_limit(sd);
1047 	}
1048 
1049 	return (ret);
1050 }
1051 /*
1052  * probe scsi device using any available path
1053  *
1054  */
1055 int
1056 scsi_hba_probe(struct scsi_device *sd, int (*callback)())
1057 {
1058 	return (scsi_hba_probe_pi(sd, callback, 0));
1059 }
1060 
1061 /*
1062  * probe scsi device using specific path
1063  *
1064  * scsi_hba_probe_pi does not do any test unit ready's which access the medium
1065  * and could cause busy or not ready conditions.
1066  * scsi_hba_probe_pi does 2 inquiries and a rqsense to clear unit attention
1067  * and to allow sync negotiation to take place
1068  * finally, scsi_hba_probe_pi does one more inquiry which should
1069  * reliably tell us what kind of target we have.
1070  * A scsi-2 compliant target should be able to	return inquiry with 250ms
1071  * and we actually wait more than a second after reset.
1072  */
1073 int
1074 scsi_hba_probe_pi(struct scsi_device *sd, int (*callback)(), int pi)
1075 {
1076 	struct scsi_pkt		*inq_pkt = NULL;
1077 	struct scsi_pkt		*rq_pkt = NULL;
1078 	int			rval = SCSIPROBE_NOMEM;
1079 	struct buf		*inq_bp = NULL;
1080 	struct buf		*rq_bp = NULL;
1081 	int			(*cb_flag)();
1082 	int			pass = 1;
1083 	uint32_t		str;
1084 
1085 	if (sd->sd_inq == NULL) {
1086 		sd->sd_inq = (struct scsi_inquiry *)
1087 		    kmem_alloc(SUN_INQSIZE, ((callback == SLEEP_FUNC) ?
1088 		    KM_SLEEP : KM_NOSLEEP));
1089 		if (sd->sd_inq == NULL) {
1090 			goto out;
1091 		}
1092 	}
1093 
1094 	if (callback != SLEEP_FUNC && callback != NULL_FUNC) {
1095 		cb_flag = NULL_FUNC;
1096 	} else {
1097 		cb_flag = callback;
1098 	}
1099 	inq_bp = scsi_alloc_consistent_buf(ROUTE,
1100 	    (struct buf *)NULL, SUN_INQSIZE, B_READ, cb_flag, NULL);
1101 	if (inq_bp == NULL) {
1102 		goto out;
1103 	}
1104 
1105 	inq_pkt = scsi_init_pkt(ROUTE, (struct scsi_pkt *)NULL,
1106 	    inq_bp, CDB_GROUP0, sizeof (struct scsi_arq_status),
1107 	    0, PKT_CONSISTENT, callback, NULL);
1108 	if (inq_pkt == NULL) {
1109 		if (inq_bp->b_error == 0)
1110 			rval = SCSIPROBE_NOMEM_CB;
1111 		goto out;
1112 	}
1113 	ASSERT(inq_bp->b_error == 0);
1114 
1115 	(void) scsi_setup_cdb((union scsi_cdb *)inq_pkt->pkt_cdbp,
1116 	    SCMD_INQUIRY, 0, SUN_INQSIZE, 0);
1117 	inq_pkt->pkt_flags = FLAG_NOINTR|FLAG_NOPARITY;
1118 
1119 	/*
1120 	 * set transport path
1121 	 */
1122 	if (pi && scsi_pkt_allocated_correctly(inq_pkt)) {
1123 		inq_pkt->pkt_path_instance = pi;
1124 		inq_pkt->pkt_flags |= FLAG_PKT_PATH_INSTANCE;
1125 	}
1126 
1127 	/*
1128 	 * the first inquiry will tell us whether a target
1129 	 * responded
1130 	 *
1131 	 * The FILL_SCSI1_LUN below will find "ansi_ver != 1" on first pass
1132 	 * because of bzero initilization. If this assumption turns out to be
1133 	 * incorrect after we have real sd_inq data (for lun0) we will do a
1134 	 * second pass during which FILL_SCSI1_LUN will place lun in CDB.
1135 	 */
1136 	bzero((caddr_t)sd->sd_inq, SUN_INQSIZE);
1137 again:	FILL_SCSI1_LUN(sd, inq_pkt);
1138 
1139 	str = scsi_test(inq_pkt, STC_PROBE_FIRST_INQ);
1140 	if (SCSI_TEST_FAILURE(str)) {
1141 		if (str == SCSI_TEST_CMD_INCOMPLETE) {
1142 			rval = SCSIPROBE_NORESP;
1143 			goto out;
1144 		}
1145 
1146 		/*
1147 		 * Retry one more time for anything other than CMD_INCOMPLETE.
1148 		 */
1149 		str = scsi_test(inq_pkt, STC_PROBE_FIRST_INQ_RETRY);
1150 		if (SCSI_TEST_FAILURE(str)) {
1151 			rval = SCSIPROBE_FAILURE;
1152 			goto out;
1153 		}
1154 	}
1155 
1156 	/*
1157 	 * Did the inquiry complete and transfer inquiry information,
1158 	 * perhaps after retry?
1159 	 */
1160 	if (str == SCSI_TEST_CMPLT_GOOD)
1161 		goto done;
1162 
1163 	/*
1164 	 * We get here for SCSI_TEST_CMPLT_{BUSY,CHECK,OTHER}. We term
1165 	 * this "partial success" in that at least something is talking
1166 	 * to us.
1167 	 *
1168 	 * A second inquiry allows the host adapter to negotiate
1169 	 * synchronous transfer period and offset
1170 	 */
1171 	str = scsi_test(inq_pkt, STC_PROBE_PARTIAL_SUCCESS);
1172 	if (SCSI_TEST_FAILURE(str)) {
1173 		if (str == SCSI_TEST_CMD_INCOMPLETE)
1174 			rval = SCSIPROBE_NORESP;
1175 		else
1176 			rval = SCSIPROBE_FAILURE;
1177 		goto out;
1178 	}
1179 
1180 	/*
1181 	 * If target is still busy, give up now.
1182 	 * XXX There's no interval between retries - scsi_test should
1183 	 * probably have a builtin retry on target busy.
1184 	 */
1185 	if (str == SCSI_TEST_CMPLT_BUSY) {
1186 		rval = SCSIPROBE_BUSY;
1187 		goto out;
1188 	}
1189 
1190 	/*
1191 	 * At this point we are SCSI_TEST_CMPLT_GOOD, SCSI_TEST_CMPLT_CHECK
1192 	 * or SCSI_TEST_CMPLT_OTHER.
1193 	 *
1194 	 * Do a rqsense if there was a check condition and ARQ was not done
1195 	 */
1196 	if (str == SCSI_TEST_CMPLT_CHECK &&
1197 	    (inq_pkt->pkt_state & STATE_ARQ_DONE) == 0) {
1198 		/*
1199 		 * prepare rqsense packet
1200 		 * there is no real need for this because the
1201 		 * check condition should have been cleared by now.
1202 		 */
1203 		rq_bp = scsi_alloc_consistent_buf(ROUTE, (struct buf *)NULL,
1204 		    (uint_t)SENSE_LENGTH, B_READ, cb_flag, NULL);
1205 		if (rq_bp == NULL) {
1206 			goto out;
1207 		}
1208 
1209 		rq_pkt = scsi_init_pkt(ROUTE, (struct scsi_pkt *)NULL,
1210 		    rq_bp, CDB_GROUP0, 1, 0, PKT_CONSISTENT, callback, NULL);
1211 
1212 		if (rq_pkt == NULL) {
1213 			if (rq_bp->b_error == 0)
1214 				rval = SCSIPROBE_NOMEM_CB;
1215 			goto out;
1216 		}
1217 		ASSERT(rq_bp->b_error == 0);
1218 
1219 		(void) scsi_setup_cdb((union scsi_cdb *)rq_pkt->
1220 		    pkt_cdbp, SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0);
1221 		FILL_SCSI1_LUN(sd, rq_pkt);
1222 		rq_pkt->pkt_flags = FLAG_NOINTR|FLAG_NOPARITY;
1223 
1224 		/*
1225 		 * set transport path
1226 		 */
1227 		if (pi && scsi_pkt_allocated_correctly(rq_pkt)) {
1228 			rq_pkt->pkt_path_instance = pi;
1229 			rq_pkt->pkt_flags |= FLAG_PKT_PATH_INSTANCE;
1230 		}
1231 
1232 		/*
1233 		 * The FILL_SCSI1_LUN above will find "inq_ansi != 1"
1234 		 * on first pass, see "again" comment above.
1235 		 *
1236 		 * The controller type is as yet unknown, so we
1237 		 * have to do a throwaway non-extended request sense,
1238 		 * and hope that that clears the check condition for
1239 		 * that unit until we can find out what kind of drive
1240 		 * it is. A non-extended request sense is specified
1241 		 * by stating that the sense block has 0 length,
1242 		 * which is taken to mean that it is four bytes in
1243 		 * length.
1244 		 */
1245 		if (SCSI_TEST_FAILURE(scsi_test(rq_pkt, STC_PROBE_RQSENSE1))) {
1246 			rval = SCSIPROBE_FAILURE;
1247 			goto out;
1248 		}
1249 	}
1250 
1251 	/*
1252 	 * At this point, we are guaranteed that something responded
1253 	 * to this scsi bus target id. We don't know yet what
1254 	 * kind of device it is, or even whether there really is
1255 	 * a logical unit attached (as some SCSI target controllers
1256 	 * lie about a unit being ready, e.g., the Emulex MD21).
1257 	 */
1258 
1259 	str = scsi_test(inq_pkt, STC_PROBE_CHK_CLEARED);
1260 	if (SCSI_TEST_FAILURE(str)) {
1261 		rval = SCSIPROBE_FAILURE;
1262 		goto out;
1263 	}
1264 
1265 	if (str == SCSI_TEST_CMPLT_BUSY) {
1266 		rval = SCSIPROBE_BUSY;
1267 		goto out;
1268 	}
1269 
1270 	/*
1271 	 * Okay we sent the INQUIRY command.
1272 	 *
1273 	 * If enough data was transferred, we count that the
1274 	 * Inquiry command succeeded, else we have to assume
1275 	 * that this is a non-CCS scsi target (or a nonexistent
1276 	 * target/lun).
1277 	 */
1278 
1279 	if (str == SCSI_TEST_CMPLT_CHECK) {
1280 		/*
1281 		 * try a request sense if we have a pkt, otherwise
1282 		 * just retry the inquiry one more time
1283 		 */
1284 		if (rq_pkt)
1285 			(void) scsi_test(rq_pkt, STC_PROBE_RQSENSE2);
1286 
1287 		/*
1288 		 * retry inquiry
1289 		 */
1290 		str = scsi_test(inq_pkt, STC_PROBE_INQ_FINAL);
1291 		if (SCSI_TEST_FAILURE(str)) {
1292 			rval = SCSIPROBE_FAILURE;
1293 			goto out;
1294 		} else if (str == SCSI_TEST_CMPLT_CHECK) {
1295 			rval = SCSIPROBE_FAILURE;
1296 			goto out;
1297 		}
1298 	}
1299 
1300 done:
1301 	/*
1302 	 * If we got a parity error on receive of inquiry data,
1303 	 * we're just plain out of luck because we told the host
1304 	 * adapter to not watch for parity errors.
1305 	 */
1306 	if ((inq_pkt->pkt_state & STATE_XFERRED_DATA) == 0 ||
1307 	    ((SUN_INQSIZE - inq_pkt->pkt_resid) < SUN_MIN_INQLEN)) {
1308 		rval = SCSIPROBE_NONCCS;
1309 	} else {
1310 		ASSERT(inq_pkt->pkt_resid >= 0);
1311 		bcopy((caddr_t)inq_bp->b_un.b_addr,
1312 		    (caddr_t)sd->sd_inq, (SUN_INQSIZE - inq_pkt->pkt_resid));
1313 		rval = SCSIPROBE_EXISTS;
1314 	}
1315 
1316 out:
1317 	/*
1318 	 * If lun > 0 we need to figure out if this is a scsi-1 device where
1319 	 * the "real" lun needs to be embedded into the cdb.
1320 	 */
1321 	if ((rval == SCSIPROBE_EXISTS) && (pass == 1) &&
1322 	    (sd->sd_address.a_lun > 0) && (sd->sd_inq->inq_ansi == 0x1)) {
1323 		pass++;
1324 		if (sd->sd_address.a_lun <= 7)
1325 			goto again;
1326 
1327 		/*
1328 		 * invalid lun for scsi-1,
1329 		 * return probe failure.
1330 		 */
1331 		rval = SCSIPROBE_FAILURE;
1332 	}
1333 
1334 	if (rq_pkt) {
1335 		scsi_destroy_pkt(rq_pkt);
1336 	}
1337 	if (inq_pkt) {
1338 		scsi_destroy_pkt(inq_pkt);
1339 	}
1340 	if (rq_bp) {
1341 		scsi_free_consistent_buf(rq_bp);
1342 	}
1343 	if (inq_bp) {
1344 		scsi_free_consistent_buf(inq_bp);
1345 	}
1346 	return (rval);
1347 }
1348 
1349 /*
1350  * Convert from a scsi_device structure pointer to a scsi_hba_tran structure
1351  * pointer. The correct way to do this is
1352  *
1353  *	#define	DEVP_TO_TRAN(sd)	((sd)->sd_address.a_hba_tran)
1354  *
1355  * however we have some consumers that place their own vector in a_hba_tran. To
1356  * avoid problems, we implement this using the sd_tran_safe. See
1357  * scsi_hba_initchild for more details.
1358  */
1359 #define	DEVP_TO_TRAN(sd)	((sd)->sd_tran_safe)
1360 
1361 /*
1362  * Function, callable from SCSA framework, to get 'human' readable REPORTDEV
1363  * addressing information from scsi_device properties.
1364  */
1365 int
1366 scsi_ua_get_reportdev(struct scsi_device *sd, char *ra, int len)
1367 {
1368 	/* use deprecated tran_get_bus_addr interface if it is defined */
1369 	/* NOTE: tran_get_bus_addr is a poor name choice for interface */
1370 	if (DEVP_TO_TRAN(sd)->tran_get_bus_addr)
1371 		return ((*DEVP_TO_TRAN(sd)->tran_get_bus_addr)(sd, ra, len));
1372 	return (scsi_hba_ua_get_reportdev(sd, ra, len));
1373 }
1374 
1375 /*
1376  * Function, callable from HBA driver's tran_get_bus_addr(9E) implementation,
1377  * to get standard form of human readable REPORTDEV addressing information
1378  * from scsi_device properties.
1379  */
1380 int
1381 scsi_hba_ua_get_reportdev(struct scsi_device *sd, char *ra, int len)
1382 {
1383 	int		tgt, lun, sfunc;
1384 	char		*tgt_port;
1385 	scsi_lun64_t	lun64;
1386 
1387 	/* get device unit-address properties */
1388 	tgt = scsi_device_prop_get_int(sd, SCSI_DEVICE_PROP_PATH,
1389 	    SCSI_ADDR_PROP_TARGET, -1);
1390 	if (scsi_device_prop_lookup_string(sd, SCSI_DEVICE_PROP_PATH,
1391 	    SCSI_ADDR_PROP_TARGET_PORT, &tgt_port) != DDI_PROP_SUCCESS)
1392 		tgt_port = NULL;
1393 	if ((tgt == -1) && (tgt_port == NULL))
1394 		return (0);		/* no target */
1395 
1396 	lun = scsi_device_prop_get_int(sd, SCSI_DEVICE_PROP_PATH,
1397 	    SCSI_ADDR_PROP_LUN, 0);
1398 	lun64 = scsi_device_prop_get_int64(sd, SCSI_DEVICE_PROP_PATH,
1399 	    SCSI_ADDR_PROP_LUN64, lun);
1400 	sfunc = scsi_device_prop_get_int(sd, SCSI_DEVICE_PROP_PATH,
1401 	    SCSI_ADDR_PROP_SFUNC, -1);
1402 
1403 	/*
1404 	 * XXX should the default be to print this in decimal for
1405 	 * "human readable" form, so it matches conf files?
1406 	 */
1407 	if (tgt_port) {
1408 		if (sfunc == -1)
1409 			(void) snprintf(ra, len,
1410 			    "%s %s lun %" PRIx64,
1411 			    SCSI_ADDR_PROP_TARGET_PORT, tgt_port, lun64);
1412 		else
1413 			(void) snprintf(ra, len,
1414 			    "%s %s lun %" PRIx64 " sfunc %x",
1415 			    SCSI_ADDR_PROP_TARGET_PORT, tgt_port, lun64, sfunc);
1416 		scsi_device_prop_free(sd, SCSI_DEVICE_PROP_PATH, tgt_port);
1417 	} else {
1418 		if (sfunc == -1)
1419 			(void) snprintf(ra, len,
1420 			    "%s %x lun %" PRIx64,
1421 			    SCSI_ADDR_PROP_TARGET, tgt, lun64);
1422 		else
1423 			(void) snprintf(ra, len,
1424 			    "%s %x lun %" PRIx64 " sfunc %x",
1425 			    SCSI_ADDR_PROP_TARGET, tgt, lun64, sfunc);
1426 	}
1427 
1428 	return (1);
1429 }
1430 
1431 /*
1432  * scsi_ua_get: using properties, return "unit-address" string.
1433  * Called by SCSA framework, may call HBAs tran function.
1434  */
1435 int
1436 scsi_ua_get(struct scsi_device *sd, char *ua, int len)
1437 {
1438 	char		*eua;
1439 
1440 	/* See if we already have established the unit-address. */
1441 	if ((eua = scsi_device_unit_address(sd)) != NULL) {
1442 		(void) strlcpy(ua, eua, len);
1443 		return (1);
1444 	}
1445 
1446 	/* Use deprecated tran_get_name interface if it is defined. */
1447 	/* NOTE: tran_get_name is a poor name choice for interface */
1448 	if (DEVP_TO_TRAN(sd)->tran_get_name)
1449 		return ((*DEVP_TO_TRAN(sd)->tran_get_name)(sd, ua, len));
1450 
1451 	/* Use generic property implementation */
1452 	return (scsi_hba_ua_get(sd, ua, len));
1453 }
1454 
1455 /*
1456  * scsi_hba_ua_get: using properties, return "unit-address" string.
1457  * This function may be called from an HBAs tran function.
1458  *
1459  * Function to get "unit-address" in "name@unit-address" /devices path
1460  * component form from the scsi_device unit-address properties on a node.
1461  *
1462  * NOTE: This function works in conjunction with scsi_hba_ua_set().
1463  */
1464 int
1465 scsi_hba_ua_get(struct scsi_device *sd, char *ua, int len)
1466 {
1467 	int		tgt, lun, sfunc;
1468 	char		*tgt_port;
1469 	scsi_lun64_t	lun64;
1470 
1471 	/* get device unit-address properties */
1472 	tgt = scsi_device_prop_get_int(sd, SCSI_DEVICE_PROP_PATH,
1473 	    SCSI_ADDR_PROP_TARGET, -1);
1474 	if (scsi_device_prop_lookup_string(sd, SCSI_DEVICE_PROP_PATH,
1475 	    SCSI_ADDR_PROP_TARGET_PORT, &tgt_port) != DDI_PROP_SUCCESS)
1476 		tgt_port = NULL;
1477 	if ((tgt == -1) && (tgt_port == NULL))
1478 		return (0);		/* no target */
1479 
1480 	lun = scsi_device_prop_get_int(sd, SCSI_DEVICE_PROP_PATH,
1481 	    SCSI_ADDR_PROP_LUN, 0);
1482 	lun64 = scsi_device_prop_get_int64(sd, SCSI_DEVICE_PROP_PATH,
1483 	    SCSI_ADDR_PROP_LUN64, lun);
1484 	sfunc = scsi_device_prop_get_int(sd, SCSI_DEVICE_PROP_PATH,
1485 	    SCSI_ADDR_PROP_SFUNC, -1);
1486 	if (tgt_port) {
1487 		if (sfunc == -1)
1488 			(void) snprintf(ua, len, "%s,%" PRIx64,
1489 			    tgt_port, lun64);
1490 		else
1491 			(void) snprintf(ua, len, "%s,%" PRIx64 ",%x",
1492 			    tgt_port, lun64, sfunc);
1493 		scsi_device_prop_free(sd, SCSI_DEVICE_PROP_PATH, tgt_port);
1494 	} else {
1495 		if (sfunc == -1)
1496 			(void) snprintf(ua, len, "%x,%" PRIx64, tgt, lun64);
1497 		else
1498 			(void) snprintf(ua, len, "%x,%" PRIx64 ",%x",
1499 			    tgt, lun64, sfunc);
1500 	}
1501 	return (1);
1502 }
1503 
1504 static void
1505 create_inquiry_props(struct scsi_device *sd)
1506 {
1507 	struct scsi_inquiry *inq = sd->sd_inq;
1508 
1509 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
1510 	    INQUIRY_DEVICE_TYPE, (int)inq->inq_dtype);
1511 
1512 	/*
1513 	 * Create the following properties:
1514 	 *
1515 	 * inquiry-vendor-id	Vendor id (INQUIRY data bytes 8-15)
1516 	 * inquiry-product-id	Product id (INQUIRY data bytes 16-31)
1517 	 * inquiry-revision-id	Product Rev level (INQUIRY data bytes 32-35)
1518 	 *
1519 	 * NOTE: We don't support creation of these properties for scsi-1
1520 	 * devices (as the vid, pid and revision were not defined) and we
1521 	 * don't create the property if they are of zero length when
1522 	 * stripped of Nulls and spaces.
1523 	 *
1524 	 * NOTE: The first definition of these properties sticks. This gives
1525 	 * a transport the ability to provide a higher-quality definition
1526 	 * than the standard SCSI INQUIRY data.
1527 	 */
1528 	if (inq->inq_ansi != 1) {
1529 		if (ddi_prop_exists(DDI_DEV_T_NONE, sd->sd_dev,
1530 		    DDI_PROP_TYPE_STRING, INQUIRY_VENDOR_ID) == 0)
1531 			(void) scsi_device_prop_update_inqstring(sd,
1532 			    INQUIRY_VENDOR_ID,
1533 			    inq->inq_vid, sizeof (inq->inq_vid));
1534 
1535 		if (ddi_prop_exists(DDI_DEV_T_NONE, sd->sd_dev,
1536 		    DDI_PROP_TYPE_STRING, INQUIRY_PRODUCT_ID) == 0)
1537 			(void) scsi_device_prop_update_inqstring(sd,
1538 			    INQUIRY_PRODUCT_ID,
1539 			    inq->inq_pid, sizeof (inq->inq_pid));
1540 
1541 		if (ddi_prop_exists(DDI_DEV_T_NONE, sd->sd_dev,
1542 		    DDI_PROP_TYPE_STRING, INQUIRY_REVISION_ID) == 0)
1543 			(void) scsi_device_prop_update_inqstring(sd,
1544 			    INQUIRY_REVISION_ID,
1545 			    inq->inq_revision, sizeof (inq->inq_revision));
1546 	}
1547 }
1548 
1549 /*
1550  * Create 'inquiry' string properties.  An 'inquiry' string gets special
1551  * treatment to trim trailing blanks (etc) and ensure null termination.
1552  */
1553 int
1554 scsi_device_prop_update_inqstring(struct scsi_device *sd,
1555     char *name, char *data, size_t len)
1556 {
1557 	int	ilen;
1558 	char	*data_string;
1559 	int	rv;
1560 
1561 	ilen = scsi_ascii_inquiry_len(data, len);
1562 	ASSERT(ilen <= (int)len);
1563 	if (ilen <= 0)
1564 		return (DDI_PROP_INVAL_ARG);
1565 
1566 	/* ensure null termination */
1567 	data_string = kmem_zalloc(ilen + 1, KM_SLEEP);
1568 	bcopy(data, data_string, ilen);
1569 	rv = ndi_prop_update_string(DDI_DEV_T_NONE,
1570 	    sd->sd_dev, name, data_string);
1571 	kmem_free(data_string, ilen + 1);
1572 	return (rv);
1573 }
1574 
1575 /*
1576  * Interfaces associated with SCSI_HBA_ADDR_COMPLEX
1577  * per-scsi_device HBA private data support.
1578  *
1579  * scsi_address_device returns NULL if we're not SCSI_HBA_ADDR_COMPLEX,
1580  * thereby allowing use of scsi_address_device as a test for
1581  * SCSI_HBA_ADDR_COMPLEX.
1582  */
1583 struct scsi_device *
1584 scsi_address_device(struct scsi_address *sa)
1585 {
1586 	return ((sa->a_hba_tran->tran_hba_flags & SCSI_HBA_ADDR_COMPLEX) ?
1587 	    sa->a.a_sd : NULL);
1588 }
1589 
1590 void
1591 scsi_device_hba_private_set(struct scsi_device *sd, void *data)
1592 {
1593 	ASSERT(sd->sd_address.a_hba_tran->tran_hba_flags &
1594 	    SCSI_HBA_ADDR_COMPLEX);
1595 	sd->sd_hba_private = data;
1596 }
1597 
1598 void *
1599 scsi_device_hba_private_get(struct scsi_device *sd)
1600 {
1601 	ASSERT(sd->sd_address.a_hba_tran->tran_hba_flags &
1602 	    SCSI_HBA_ADDR_COMPLEX);
1603 	return (sd->sd_hba_private);
1604 }
1605 
1606 /*
1607  * This routine is called from the start of scsi_probe() if a tgt/LUN to be
1608  * probed *may* be a request to probe a strictly SCSI-2 target (with respect
1609  * to LUNs) -- and this probe may be for a LUN number greater than 7,
1610  * which can cause a hardware hang
1611  *
1612  * return 0 if the probe can proceed,
1613  * else return 1, meaning do *NOT* probe this target/LUN
1614  */
1615 static int
1616 scsi_check_ss2_LUN_limit(struct scsi_device *sd)
1617 {
1618 	struct scsi_address	*ap = &(sd->sd_address);
1619 	dev_info_t		*pdevi =
1620 	    (dev_info_t *)DEVI(sd->sd_dev)->devi_parent;
1621 	int			ret_val = 0;	/* default return value */
1622 	uchar_t			*tgt_list;
1623 	uint_t			tgt_nelements;
1624 	int			i;
1625 
1626 
1627 	/*
1628 	 * check for what *might* be a problem probe, only we don't
1629 	 * know yet what's really at the destination target/LUN
1630 	 */
1631 	if ((ap->a_target >= NTARGETS_WIDE) ||
1632 	    (ap->a_lun < NLUNS_PER_TARGET)) {
1633 		return (0);		/* okay to probe this target */
1634 	}
1635 
1636 	/*
1637 	 * this *might* be a problematic probe, so look to see
1638 	 * if the inquiry data matches
1639 	 */
1640 	SCSI_PROBE_DEBUG2(1, "SCSA pre-probe: checking tgt.LUN=%d.%d\n",
1641 	    ap->a_target, ap->a_lun);
1642 	SCSI_PROBE_DEBUG1(2,
1643 	    "SCSA pre-probe: scanning parent node name: %s ...\n",
1644 	    ddi_node_name(pdevi));
1645 
1646 	/*
1647 	 * look for a special property of our parent node that lists
1648 	 * the targets under it for which we do *NOT* want to probe
1649 	 * if LUN>7 -- if the property is found, look to see if our
1650 	 * target ID is on that list
1651 	 */
1652 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, pdevi,
1653 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SS2_LUN0_TGT_LIST_PROP,
1654 	    &tgt_list, &tgt_nelements) != DDI_PROP_SUCCESS) {
1655 		/*
1656 		 * no list, so it must be okay to probe this target.LUN
1657 		 */
1658 		SCSI_PROBE_DEBUG0(3,
1659 		    "SCSA pre-probe: NO parent prop found\n");
1660 	} else {
1661 		for (i = 0; i < tgt_nelements; i++) {
1662 			if (tgt_list[i] == ap->a_target) {
1663 				/*
1664 				 * we found a match, which means we do *NOT*
1665 				 * want to probe the specified target.LUN
1666 				 */
1667 				ret_val = 1;
1668 				break;
1669 			}
1670 		}
1671 		ddi_prop_free(tgt_list);
1672 #ifdef	DEBUG
1673 		if (ret_val == 1) {
1674 			SCSI_PROBE_DEBUG2(1,
1675 			    "SCSA pre-probe: marker node FOUND for "
1676 			    "tgt.LUN=%d.%d, so SKIPPING it\n",
1677 			    ap->a_target, ap->a_lun);
1678 		} else {
1679 			SCSI_PROBE_DEBUG0(2,
1680 			    "SCSA pre-probe: NO marker node found"
1681 			    " -- OK to probe\n");
1682 		}
1683 #endif
1684 	}
1685 	return (ret_val);
1686 }
1687 
1688 
1689 /*
1690  * this routine is called from near the end of scsi_probe(),
1691  * to see if the just-probed node is on our list of strictly-SCSI-2 nodes,
1692  * and if it is we mark our parent node with this information
1693  */
1694 static void
1695 scsi_establish_LUN_limit(struct scsi_device *sd)
1696 {
1697 	struct scsi_address	*ap = &(sd->sd_address);
1698 	struct scsi_inquiry	*inq = sd->sd_inq;
1699 	dev_info_t		*devi = sd->sd_dev;
1700 	char			*vid = NULL;
1701 	char			*pid = NULL;
1702 	char			*rev = NULL;
1703 	int			i;
1704 	const ss2_lun0_info_t	*p;
1705 	int			bad_target_found = 0;
1706 
1707 
1708 	/*
1709 	 * if this inquiry data shows that we have a strictly-SCSI-2 device
1710 	 * at LUN 0, then add it to our list of strictly-SCSI-2 devices,
1711 	 * so that we can avoid probes where LUN>7 on this device later
1712 	 */
1713 	if ((ap->a_lun != 0) ||
1714 	    (ap->a_target >= NTARGETS_WIDE) ||
1715 	    (inq->inq_dtype != DTYPE_PROCESSOR) ||
1716 	    (inq->inq_ansi != 2)) {
1717 		/*
1718 		 * this can't possibly be a node we want to look at, since
1719 		 * either LUN is greater than 0, target is greater than or
1720 		 * equal to 16, device type
1721 		 * is not processor, or SCSI level is not SCSI-2,
1722 		 * so don't bother checking for a strictly SCSI-2
1723 		 * (only 8 LUN) target
1724 		 */
1725 		return;				/* don't care */
1726 	}
1727 
1728 	SCSI_PROBE_DEBUG2(1, "SCSA post-probe: LUN limit on tgt.LUN=%d.%d, "
1729 	    "SCSI-2 PROCESSOR?\n", ap->a_target, ap->a_lun);
1730 
1731 	ASSERT(devi != NULL);
1732 
1733 	/*
1734 	 * we have a node that has been probed that is: LUN=0, target<16,
1735 	 * PROCESSOR-type SCSI target, and at the SCSI-2 level, so
1736 	 * check INQ properties to see if it's in our list of strictly
1737 	 * SCSI-2 targets
1738 	 *
1739 	 * first we have to get the VID/PID/REV INQUIRY properties for
1740 	 * comparison
1741 	 */
1742 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi,
1743 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1744 	    INQUIRY_VENDOR_ID, &vid) != DDI_PROP_SUCCESS) {
1745 		SCSI_PROBE_DEBUG1(2, "SCSA post-probe: prop \"%s\" missing\n",
1746 		    INQUIRY_VENDOR_ID);
1747 		goto dun;
1748 	}
1749 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi,
1750 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1751 	    INQUIRY_PRODUCT_ID, &pid) != DDI_PROP_SUCCESS) {
1752 		SCSI_PROBE_DEBUG1(2, "SCSA post-probe: prop \"%s\" missing\n",
1753 		    INQUIRY_PRODUCT_ID);
1754 		goto dun;
1755 	}
1756 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi,
1757 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1758 	    INQUIRY_REVISION_ID, &rev) != DDI_PROP_SUCCESS) {
1759 		SCSI_PROBE_DEBUG1(2, "SCSA post-probe: prop \"%s\" missing\n",
1760 		    INQUIRY_REVISION_ID);
1761 		goto dun;
1762 	}
1763 
1764 	SCSI_PROBE_DEBUG3(3, "SCSA post-probe: looking for vid/pid/rev = "
1765 	    "\"%s\"/\"%s\"/\"%s\"\n", vid, pid, rev);
1766 
1767 	/*
1768 	 * now that we have the INQUIRY properties from the device node,
1769 	 * compare them with our known offenders
1770 	 *
1771 	 * Note: comparison is *CASE* *SENSITIVE*
1772 	 */
1773 	for (i = 0; i < scsi_probe_strict_s2_size; i++) {
1774 		p = &scsi_probe_strict_s2_list[i];
1775 
1776 		if ((strcmp(p->sli_vid, vid) == 0) &&
1777 		    (strcmp(p->sli_pid, pid) == 0) &&
1778 		    (strcmp(p->sli_rev, rev) == 0)) {
1779 			/*
1780 			 * we found a match -- do NOT want to probe this one
1781 			 */
1782 			SCSI_PROBE_DEBUG3(1,
1783 			    "SCSA post-probe: recording strict SCSI-2 node "
1784 			    "vid/pid/rev = \"%s\"/\"%s\"/\"%s\"\n",
1785 			    vid, pid, rev);
1786 
1787 			/*
1788 			 * set/update private parent-node property,
1789 			 * so we can find out about this node later
1790 			 */
1791 			bad_target_found = 1;
1792 			break;
1793 		}
1794 	}
1795 
1796 	/*
1797 	 * either add remove target number from parent property
1798 	 */
1799 	scsi_update_parent_ss2_prop(devi, ap->a_target, bad_target_found);
1800 
1801 dun:
1802 	if (vid != NULL) {
1803 		ddi_prop_free(vid);
1804 	}
1805 	if (pid != NULL) {
1806 		ddi_prop_free(pid);
1807 	}
1808 	if (rev != NULL) {
1809 		ddi_prop_free(rev);
1810 	}
1811 }
1812 
1813 
1814 /*
1815  * update the parent node to add in the supplied tgt number to the target
1816  * list property already present (if any)
1817  *
1818  * since the target list can never be longer than 16, and each target
1819  * number is also small, we can save having to alloc memory by putting
1820  * a 16-byte array on the stack and using it for property memory
1821  *
1822  * if "add_tgt" is set then add the target to the parent's property, else
1823  * remove it (if present)
1824  */
1825 static void
1826 scsi_update_parent_ss2_prop(dev_info_t *devi, int tgt, int add_tgt)
1827 {
1828 	dev_info_t	*pdevi = (dev_info_t *)DEVI(devi)->devi_parent;
1829 	uchar_t		*tgt_list;
1830 	uint_t		nelements;
1831 	uint_t		new_nelements;
1832 	int		i;
1833 	int		update_result;
1834 	uchar_t		new_tgt_list[NTARGETS_WIDE];
1835 
1836 
1837 	ASSERT(pdevi != NULL);
1838 
1839 	SCSI_PROBE_DEBUG3(3,
1840 	    "SCSA post-probe: updating parent=%s property to %s tgt=%d\n",
1841 	    ddi_node_name(pdevi), add_tgt ? "add" : "remove", tgt);
1842 
1843 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, pdevi,
1844 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1845 	    SS2_LUN0_TGT_LIST_PROP, &tgt_list, &nelements) ==
1846 	    DDI_PROP_SUCCESS) {
1847 
1848 		if (add_tgt) {
1849 			/*
1850 			 * we found an existing property -- we might need
1851 			 *	to add to it
1852 			 */
1853 			for (i = 0; i < nelements; i++) {
1854 				if (tgt_list[i] == tgt) {
1855 					/* target already in list */
1856 					SCSI_PROBE_DEBUG1(2, "SCSA post-probe:"
1857 					    " tgt %d already listed\n", tgt);
1858 					ddi_prop_free(tgt_list);
1859 					return;
1860 				}
1861 			}
1862 
1863 			/*
1864 			 * need to append our target number to end of list
1865 			 *	(no need sorting list, as it's so short)
1866 			 */
1867 
1868 			/*
1869 			 * will this new entry fit ?? -- it should, since
1870 			 *	the array is 16-wide and only keep track of
1871 			 *	16 targets, but check just in case
1872 			 */
1873 			new_nelements = nelements + 1;
1874 			if (new_nelements >= NTARGETS_WIDE) {
1875 				SCSI_PROBE_DEBUG0(1, "SCSA post-probe: "
1876 				    "internal error: no room "
1877 				    "for more targets?\n");
1878 				ddi_prop_free(tgt_list);
1879 				return;
1880 			}
1881 
1882 			/* copy existing list then add our tgt number to end */
1883 			bcopy((void *)tgt_list, (void *)new_tgt_list,
1884 			    sizeof (uchar_t) * nelements);
1885 			new_tgt_list[new_nelements - 1] = (uchar_t)tgt;
1886 		} else {
1887 			/*
1888 			 * we need to remove our target number from the list,
1889 			 *	so copy all of the other target numbers,
1890 			 *	skipping ours
1891 			 */
1892 			int	tgt_removed = 0;
1893 
1894 			new_nelements = 0;
1895 			for (i = 0; i < nelements; i++) {
1896 				if (tgt_list[i] != tgt) {
1897 					new_tgt_list[new_nelements++] =
1898 					    tgt_list[i];
1899 				} else {
1900 					/* skip this target */
1901 					tgt_removed++;
1902 				}
1903 			}
1904 
1905 			if (!tgt_removed) {
1906 				SCSI_PROBE_DEBUG1(2, "SCSA post-probe:"
1907 				    " no need to remove tgt %d\n", tgt);
1908 				ddi_prop_free(tgt_list);
1909 				return;
1910 			}
1911 		}
1912 
1913 		update_result = ddi_prop_update_byte_array(DDI_DEV_T_NONE,
1914 		    pdevi, SS2_LUN0_TGT_LIST_PROP, new_tgt_list,
1915 		    new_nelements);
1916 
1917 		ddi_prop_free(tgt_list);
1918 	} else {
1919 		/*
1920 		 * no property yet
1921 		 */
1922 		if (add_tgt) {
1923 			/*
1924 			 * create a property with just our tgt
1925 			 */
1926 			new_tgt_list[0] = (uchar_t)tgt;
1927 			new_nelements = 1;	/* just one element */
1928 
1929 			update_result = ddi_prop_update_byte_array(
1930 			    DDI_DEV_T_NONE, pdevi, SS2_LUN0_TGT_LIST_PROP,
1931 			    new_tgt_list, new_nelements);
1932 		} else {
1933 			/*
1934 			 * no list so no need to remove tgt from that list
1935 			 */
1936 			return;
1937 		}
1938 	}
1939 
1940 #ifdef	DEBUG
1941 	/*
1942 	 * if we get here we have tried to add/update properties
1943 	 */
1944 	if (update_result != DDI_PROP_SUCCESS) {
1945 		SCSI_PROBE_DEBUG2(1, "SCSA post-probe: can't update parent "
1946 		    "property with tgt=%d (%d)\n", tgt, update_result);
1947 	} else {
1948 		if (add_tgt) {
1949 			SCSI_PROBE_DEBUG3(2,
1950 			    "SCSA post-probe: added tgt=%d to parent "
1951 			    "prop=\"%s\" (now %d entries)\n",
1952 			    tgt, SS2_LUN0_TGT_LIST_PROP, new_nelements);
1953 		} else {
1954 			SCSI_PROBE_DEBUG3(2,
1955 			    "SCSA post-probe: removed tgt=%d from parent "
1956 			    "prop=\"%s\" (now %d entries)\n",
1957 			    tgt, SS2_LUN0_TGT_LIST_PROP, new_nelements);
1958 		}
1959 	}
1960 #endif
1961 }
1962 
1963 
1964 /* XXX BEGIN: find a better place for this: inquiry.h? */
1965 /*
1966  * Definitions used by device id registration routines
1967  */
1968 #define	VPD_HEAD_OFFSET		3	/* size of head for vpd page */
1969 #define	VPD_PAGE_LENGTH		3	/* offset for pge length data */
1970 #define	VPD_MODE_PAGE		1	/* offset into vpd pg for "page code" */
1971 
1972 /* size for devid inquiries */
1973 #define	MAX_INQUIRY_SIZE	0xF0
1974 #define	MAX_INQUIRY_SIZE_EVPD	0xFF	/* XXX why is this longer */
1975 /* XXX END: find a better place for these */
1976 
1977 
1978 /*
1979  * Decorate devinfo node with identity properties using information obtained
1980  * from device. These properties are used by device enumeration code to derive
1981  * the devid, and guid for the device. These properties are also used to
1982  * determine if a device should be enumerated under the physical HBA (PHCI) or
1983  * the virtual HBA (VHCI, for mpxio support).
1984  *
1985  * Return zero on success. If commands that should succeed fail or allocations
1986  * fail then return failure (non-zero). It is possible for this function to
1987  * return success and not have decorated the node with any additional identity
1988  * information if the device correctly responds indicating that they are not
1989  * supported.  When failure occurs the caller should consider not making the
1990  * device accessible.
1991  */
1992 int
1993 scsi_device_identity(struct scsi_device *sd, int (*callback)())
1994 {
1995 	dev_info_t	*devi		= sd->sd_dev;
1996 	uchar_t		*inq80		= NULL;
1997 	uchar_t		*inq83		= NULL;
1998 	int		rval;
1999 	size_t		len;
2000 	int		pg80, pg83;
2001 
2002 	/* find out what pages are supported by device */
2003 	if (check_vpd_page_support8083(sd, callback, &pg80, &pg83) == -1)
2004 		return (-1);
2005 
2006 	/* if available, collect page 80 data and add as property */
2007 	if (pg80) {
2008 		inq80 = kmem_zalloc(MAX_INQUIRY_SIZE,
2009 		    ((callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP));
2010 		if (inq80 == NULL) {
2011 			rval = -1;
2012 			goto out;
2013 		}
2014 
2015 		rval = send_scsi_INQUIRY(sd, callback, inq80,
2016 		    MAX_INQUIRY_SIZE, 0x01, 0x80, &len, STC_IDENTITY_PG80);
2017 		if (rval)
2018 			goto out;		/* should have worked */
2019 
2020 		if (len && (ndi_prop_update_byte_array(DDI_DEV_T_NONE, devi,
2021 		    "inquiry-page-80", inq80, len) != DDI_PROP_SUCCESS)) {
2022 			cmn_err(CE_WARN, "scsi_device_identity: "
2023 			    "failed to add page80 prop");
2024 			rval = -1;
2025 			goto out;
2026 		}
2027 	}
2028 
2029 	/* if available, collect page 83 data and add as property */
2030 	if (pg83) {
2031 		inq83 = kmem_zalloc(MAX_INQUIRY_SIZE,
2032 		    ((callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP));
2033 		if (inq83 == NULL) {
2034 			rval = -1;
2035 			goto out;
2036 		}
2037 
2038 		rval = send_scsi_INQUIRY(sd, callback, inq83,
2039 		    MAX_INQUIRY_SIZE, 0x01, 0x83, &len, STC_IDENTITY_PG83);
2040 		if (rval)
2041 			goto out;		/* should have worked */
2042 
2043 		if (len && (ndi_prop_update_byte_array(DDI_DEV_T_NONE, devi,
2044 		    "inquiry-page-83", inq83, len) != DDI_PROP_SUCCESS)) {
2045 			cmn_err(CE_WARN, "scsi_device_identity: "
2046 			    "failed to add page83 prop");
2047 			rval = -1;
2048 			goto out;
2049 		}
2050 	}
2051 
2052 	/* Commands worked, identity information that exists has been added. */
2053 	rval = 0;
2054 
2055 	/* clean up resources */
2056 out:	if (inq80 != NULL)
2057 		kmem_free(inq80, MAX_INQUIRY_SIZE);
2058 	if (inq83 != NULL)
2059 		kmem_free(inq83, MAX_INQUIRY_SIZE);
2060 
2061 	return (rval);
2062 }
2063 
2064 /*
2065  * Send an INQUIRY command with the EVPD bit set and a page code of 0x00 to
2066  * the device, returning zero on success. Returned INQUIRY data is used to
2067  * determine which vital product pages are supported. The device idenity
2068  * information we are looking for is in pages 0x83 and/or 0x80. If the device
2069  * fails the EVPD inquiry then no pages are supported but the call succeeds.
2070  * Return -1 (failure) if there were memory allocation failures or if a
2071  * command faild that should have worked.
2072  */
2073 static int
2074 check_vpd_page_support8083(struct scsi_device *sd, int (*callback)(),
2075 	int *ppg80, int *ppg83)
2076 {
2077 	uchar_t *page_list;
2078 	int	counter;
2079 	int	rval;
2080 
2081 	/* pages are not supported */
2082 	*ppg80 = 0;
2083 	*ppg83 = 0;
2084 
2085 	/*
2086 	 * We'll set the page length to the maximum to save figuring it out
2087 	 * with an additional call.
2088 	 */
2089 	page_list =  kmem_zalloc(MAX_INQUIRY_SIZE_EVPD,
2090 	    ((callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP));
2091 	if (page_list == NULL)
2092 		return (-1);		/* memory allocation problem */
2093 
2094 	/* issue page 0 (Supported VPD Pages) INQUIRY with evpd set */
2095 	rval = send_scsi_INQUIRY(sd, callback,
2096 	    page_list, MAX_INQUIRY_SIZE_EVPD, 1, 0, NULL, STC_VPD_CHECK);
2097 
2098 	/*
2099 	 * Now we must validate that the device accepted the command (some
2100 	 * devices do not support it) and if the idenity pages we are
2101 	 * interested in are supported.
2102 	 */
2103 	if ((rval == 0) &&
2104 	    (page_list[VPD_MODE_PAGE] == 0x00)) {
2105 		/* Loop to find one of the 2 pages we need */
2106 		counter = 4;  /* Supported pages start at byte 4, with 0x00 */
2107 
2108 		/*
2109 		 * Pages are returned in ascending order, and 0x83 is the
2110 		 * last page we are hoping to find.
2111 		 */
2112 		while ((page_list[counter] <= 0x83) &&
2113 		    (counter <= (page_list[VPD_PAGE_LENGTH] +
2114 		    VPD_HEAD_OFFSET))) {
2115 			/*
2116 			 * Add 3 because page_list[3] is the number of
2117 			 * pages minus 3
2118 			 */
2119 
2120 			switch (page_list[counter]) {
2121 			case 0x80:
2122 				*ppg80 = 1;
2123 				break;
2124 			case 0x83:
2125 				*ppg83 = 1;
2126 				break;
2127 			}
2128 			counter++;
2129 		}
2130 	}
2131 
2132 	kmem_free(page_list, MAX_INQUIRY_SIZE_EVPD);
2133 	return (0);
2134 }
2135 
2136 /*
2137  * Send INQUIRY command with specified EVPD and page code.  Return
2138  * zero on success.  On success, the amount of data transferred
2139  * is returned in *lenp.
2140  */
2141 static int
2142 send_scsi_INQUIRY(struct scsi_device *sd, int (*callback)(),
2143     uchar_t *bufaddr, size_t buflen,
2144     uchar_t evpd, uchar_t page_code, size_t *lenp,
2145     enum scsi_test_ctxt ctxt)
2146 {
2147 	int		(*cb_flag)();
2148 	struct buf	*inq_bp;
2149 	struct scsi_pkt *inq_pkt = NULL;
2150 	int		rval = -1;
2151 
2152 	if (lenp)
2153 		*lenp = 0;
2154 	if (callback != SLEEP_FUNC && callback != NULL_FUNC)
2155 		cb_flag = NULL_FUNC;
2156 	else
2157 		cb_flag = callback;
2158 	inq_bp = scsi_alloc_consistent_buf(ROUTE,
2159 	    (struct buf *)NULL, buflen, B_READ, cb_flag, NULL);
2160 	if (inq_bp == NULL)
2161 		goto out;		/* memory allocation problem */
2162 
2163 	inq_pkt = scsi_init_pkt(ROUTE, (struct scsi_pkt *)NULL,
2164 	    inq_bp, CDB_GROUP0, sizeof (struct scsi_arq_status),
2165 	    0, PKT_CONSISTENT, callback, NULL);
2166 	if (inq_pkt == NULL)
2167 		goto out;		/* memory allocation problem */
2168 
2169 	ASSERT(inq_bp->b_error == 0);
2170 
2171 	/* form INQUIRY cdb with specified EVPD and page code */
2172 	(void) scsi_setup_cdb((union scsi_cdb *)inq_pkt->pkt_cdbp,
2173 	    SCMD_INQUIRY, 0, buflen, 0);
2174 	inq_pkt->pkt_cdbp[1] = evpd;
2175 	inq_pkt->pkt_cdbp[2] = page_code;
2176 
2177 	inq_pkt->pkt_time = SCSI_POLL_TIMEOUT;	/* in seconds */
2178 	inq_pkt->pkt_flags = FLAG_NOINTR|FLAG_NOPARITY;
2179 
2180 	/*
2181 	 * Issue inquiry command thru scsi_test
2182 	 *
2183 	 * NOTE: This is important data about device identity, not sure why
2184 	 * NOPARITY is used. Also seems like we should check pkt_stat for
2185 	 * STATE_XFERRED_DATA.
2186 	 */
2187 	if (scsi_test(inq_pkt, ctxt) == SCSI_TEST_CMPLT_GOOD) {
2188 		ASSERT(inq_pkt->pkt_resid >= 0);
2189 		ASSERT(inq_pkt->pkt_resid <= buflen);
2190 
2191 		bcopy(inq_bp->b_un.b_addr,
2192 		    bufaddr, buflen - inq_pkt->pkt_resid);
2193 		if (lenp)
2194 			*lenp = (buflen - inq_pkt->pkt_resid);
2195 		rval = 0;
2196 	}
2197 
2198 	/*
2199 	 * XXX We should retry on target busy
2200 	 */
2201 
2202 out:	if (inq_pkt)
2203 		scsi_destroy_pkt(inq_pkt);
2204 	if (inq_bp)
2205 		scsi_free_consistent_buf(inq_bp);
2206 	return (rval);
2207 }
2208