xref: /illumos-gate/usr/src/uts/common/io/scsi/conf/scsi_confsubr.c (revision 5084e753b79a753c8b532c06eb3ad1d025e8e472)
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, retry = 0;
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 again:
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) && (retry == 0)) {
1044 		if (scsi_reset(&sd->sd_address, RESET_LUN) != 1) {
1045 			cmn_err(CE_WARN, "scsi_probe: scsi_reset failed");
1046 		}
1047 		retry = 1;
1048 		goto again;
1049 	}
1050 
1051 	if (ret == SCSIPROBE_EXISTS) {
1052 		create_inquiry_props(sd);
1053 		/* is this a strictly-SCSI-2 node ?? */
1054 		scsi_establish_LUN_limit(sd);
1055 	}
1056 
1057 	return (ret);
1058 }
1059 /*
1060  * probe scsi device using any available path
1061  *
1062  */
1063 int
1064 scsi_hba_probe(struct scsi_device *sd, int (*callback)())
1065 {
1066 	return (scsi_hba_probe_pi(sd, callback, 0));
1067 }
1068 
1069 /*
1070  * probe scsi device using specific path
1071  *
1072  * scsi_hba_probe_pi does not do any test unit ready's which access the medium
1073  * and could cause busy or not ready conditions.
1074  * scsi_hba_probe_pi does 2 inquiries and a rqsense to clear unit attention
1075  * and to allow sync negotiation to take place
1076  * finally, scsi_hba_probe_pi does one more inquiry which should
1077  * reliably tell us what kind of target we have.
1078  * A scsi-2 compliant target should be able to	return inquiry with 250ms
1079  * and we actually wait more than a second after reset.
1080  */
1081 int
1082 scsi_hba_probe_pi(struct scsi_device *sd, int (*callback)(), int pi)
1083 {
1084 	struct scsi_pkt		*inq_pkt = NULL;
1085 	struct scsi_pkt		*rq_pkt = NULL;
1086 	int			rval = SCSIPROBE_NOMEM;
1087 	struct buf		*inq_bp = NULL;
1088 	struct buf		*rq_bp = NULL;
1089 	int			(*cb_flag)();
1090 	int			pass = 1;
1091 	uint32_t		str;
1092 
1093 	if (sd->sd_inq == NULL) {
1094 		sd->sd_inq = (struct scsi_inquiry *)
1095 		    kmem_alloc(SUN_INQSIZE, ((callback == SLEEP_FUNC) ?
1096 		    KM_SLEEP : KM_NOSLEEP));
1097 		if (sd->sd_inq == NULL) {
1098 			goto out;
1099 		}
1100 	}
1101 
1102 	if (callback != SLEEP_FUNC && callback != NULL_FUNC) {
1103 		cb_flag = NULL_FUNC;
1104 	} else {
1105 		cb_flag = callback;
1106 	}
1107 	inq_bp = scsi_alloc_consistent_buf(ROUTE,
1108 	    (struct buf *)NULL, SUN_INQSIZE, B_READ, cb_flag, NULL);
1109 	if (inq_bp == NULL) {
1110 		goto out;
1111 	}
1112 
1113 	inq_pkt = scsi_init_pkt(ROUTE, (struct scsi_pkt *)NULL,
1114 	    inq_bp, CDB_GROUP0, sizeof (struct scsi_arq_status),
1115 	    0, PKT_CONSISTENT, callback, NULL);
1116 	if (inq_pkt == NULL) {
1117 		if (inq_bp->b_error == 0)
1118 			rval = SCSIPROBE_NOMEM_CB;
1119 		goto out;
1120 	}
1121 	ASSERT(inq_bp->b_error == 0);
1122 
1123 	(void) scsi_setup_cdb((union scsi_cdb *)inq_pkt->pkt_cdbp,
1124 	    SCMD_INQUIRY, 0, SUN_INQSIZE, 0);
1125 	inq_pkt->pkt_flags = FLAG_NOINTR|FLAG_NOPARITY;
1126 
1127 	/*
1128 	 * set transport path
1129 	 */
1130 	if (pi && scsi_pkt_allocated_correctly(inq_pkt)) {
1131 		inq_pkt->pkt_path_instance = pi;
1132 		inq_pkt->pkt_flags |= FLAG_PKT_PATH_INSTANCE;
1133 	}
1134 
1135 	/*
1136 	 * the first inquiry will tell us whether a target
1137 	 * responded
1138 	 *
1139 	 * The FILL_SCSI1_LUN below will find "ansi_ver != 1" on first pass
1140 	 * because of bzero initilization. If this assumption turns out to be
1141 	 * incorrect after we have real sd_inq data (for lun0) we will do a
1142 	 * second pass during which FILL_SCSI1_LUN will place lun in CDB.
1143 	 */
1144 	bzero((caddr_t)sd->sd_inq, SUN_INQSIZE);
1145 again:	FILL_SCSI1_LUN(sd, inq_pkt);
1146 
1147 	str = scsi_test(inq_pkt, STC_PROBE_FIRST_INQ);
1148 	if (SCSI_TEST_FAILURE(str)) {
1149 		if (str == SCSI_TEST_CMD_INCOMPLETE) {
1150 			rval = SCSIPROBE_NORESP;
1151 			goto out;
1152 		}
1153 
1154 		/*
1155 		 * Retry one more time for anything other than CMD_INCOMPLETE.
1156 		 */
1157 		str = scsi_test(inq_pkt, STC_PROBE_FIRST_INQ_RETRY);
1158 		if (SCSI_TEST_FAILURE(str)) {
1159 			rval = SCSIPROBE_FAILURE;
1160 			goto out;
1161 		}
1162 	}
1163 
1164 	/*
1165 	 * Did the inquiry complete and transfer inquiry information,
1166 	 * perhaps after retry?
1167 	 */
1168 	if (str == SCSI_TEST_CMPLT_GOOD)
1169 		goto done;
1170 
1171 	/*
1172 	 * We get here for SCSI_TEST_CMPLT_{BUSY,CHECK,OTHER}. We term
1173 	 * this "partial success" in that at least something is talking
1174 	 * to us.
1175 	 *
1176 	 * A second inquiry allows the host adapter to negotiate
1177 	 * synchronous transfer period and offset
1178 	 */
1179 	str = scsi_test(inq_pkt, STC_PROBE_PARTIAL_SUCCESS);
1180 	if (SCSI_TEST_FAILURE(str)) {
1181 		if (str == SCSI_TEST_CMD_INCOMPLETE)
1182 			rval = SCSIPROBE_NORESP;
1183 		else
1184 			rval = SCSIPROBE_FAILURE;
1185 		goto out;
1186 	}
1187 
1188 	/*
1189 	 * If target is still busy, give up now.
1190 	 * XXX There's no interval between retries - scsi_test should
1191 	 * probably have a builtin retry on target busy.
1192 	 */
1193 	if (str == SCSI_TEST_CMPLT_BUSY) {
1194 		rval = SCSIPROBE_BUSY;
1195 		goto out;
1196 	}
1197 
1198 	/*
1199 	 * At this point we are SCSI_TEST_CMPLT_GOOD, SCSI_TEST_CMPLT_CHECK
1200 	 * or SCSI_TEST_CMPLT_OTHER.
1201 	 *
1202 	 * Do a rqsense if there was a check condition and ARQ was not done
1203 	 */
1204 	if (str == SCSI_TEST_CMPLT_CHECK &&
1205 	    (inq_pkt->pkt_state & STATE_ARQ_DONE) == 0) {
1206 		/*
1207 		 * prepare rqsense packet
1208 		 * there is no real need for this because the
1209 		 * check condition should have been cleared by now.
1210 		 */
1211 		rq_bp = scsi_alloc_consistent_buf(ROUTE, (struct buf *)NULL,
1212 		    (uint_t)SENSE_LENGTH, B_READ, cb_flag, NULL);
1213 		if (rq_bp == NULL) {
1214 			goto out;
1215 		}
1216 
1217 		rq_pkt = scsi_init_pkt(ROUTE, (struct scsi_pkt *)NULL,
1218 		    rq_bp, CDB_GROUP0, 1, 0, PKT_CONSISTENT, callback, NULL);
1219 
1220 		if (rq_pkt == NULL) {
1221 			if (rq_bp->b_error == 0)
1222 				rval = SCSIPROBE_NOMEM_CB;
1223 			goto out;
1224 		}
1225 		ASSERT(rq_bp->b_error == 0);
1226 
1227 		(void) scsi_setup_cdb((union scsi_cdb *)rq_pkt->
1228 		    pkt_cdbp, SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0);
1229 		FILL_SCSI1_LUN(sd, rq_pkt);
1230 		rq_pkt->pkt_flags = FLAG_NOINTR|FLAG_NOPARITY;
1231 
1232 		/*
1233 		 * set transport path
1234 		 */
1235 		if (pi && scsi_pkt_allocated_correctly(rq_pkt)) {
1236 			rq_pkt->pkt_path_instance = pi;
1237 			rq_pkt->pkt_flags |= FLAG_PKT_PATH_INSTANCE;
1238 		}
1239 
1240 		/*
1241 		 * The FILL_SCSI1_LUN above will find "inq_ansi != 1"
1242 		 * on first pass, see "again" comment above.
1243 		 *
1244 		 * The controller type is as yet unknown, so we
1245 		 * have to do a throwaway non-extended request sense,
1246 		 * and hope that that clears the check condition for
1247 		 * that unit until we can find out what kind of drive
1248 		 * it is. A non-extended request sense is specified
1249 		 * by stating that the sense block has 0 length,
1250 		 * which is taken to mean that it is four bytes in
1251 		 * length.
1252 		 */
1253 		if (SCSI_TEST_FAILURE(scsi_test(rq_pkt, STC_PROBE_RQSENSE1))) {
1254 			rval = SCSIPROBE_FAILURE;
1255 			goto out;
1256 		}
1257 	}
1258 
1259 	/*
1260 	 * At this point, we are guaranteed that something responded
1261 	 * to this scsi bus target id. We don't know yet what
1262 	 * kind of device it is, or even whether there really is
1263 	 * a logical unit attached (as some SCSI target controllers
1264 	 * lie about a unit being ready, e.g., the Emulex MD21).
1265 	 */
1266 
1267 	str = scsi_test(inq_pkt, STC_PROBE_CHK_CLEARED);
1268 	if (SCSI_TEST_FAILURE(str)) {
1269 		rval = SCSIPROBE_FAILURE;
1270 		goto out;
1271 	}
1272 
1273 	if (str == SCSI_TEST_CMPLT_BUSY) {
1274 		rval = SCSIPROBE_BUSY;
1275 		goto out;
1276 	}
1277 
1278 	/*
1279 	 * Okay we sent the INQUIRY command.
1280 	 *
1281 	 * If enough data was transferred, we count that the
1282 	 * Inquiry command succeeded, else we have to assume
1283 	 * that this is a non-CCS scsi target (or a nonexistent
1284 	 * target/lun).
1285 	 */
1286 
1287 	if (str == SCSI_TEST_CMPLT_CHECK) {
1288 		/*
1289 		 * try a request sense if we have a pkt, otherwise
1290 		 * just retry the inquiry one more time
1291 		 */
1292 		if (rq_pkt)
1293 			(void) scsi_test(rq_pkt, STC_PROBE_RQSENSE2);
1294 
1295 		/*
1296 		 * retry inquiry
1297 		 */
1298 		str = scsi_test(inq_pkt, STC_PROBE_INQ_FINAL);
1299 		if (SCSI_TEST_FAILURE(str)) {
1300 			rval = SCSIPROBE_FAILURE;
1301 			goto out;
1302 		} else if (str == SCSI_TEST_CMPLT_CHECK) {
1303 			rval = SCSIPROBE_FAILURE;
1304 			goto out;
1305 		}
1306 	}
1307 
1308 done:
1309 	/*
1310 	 * If we got a parity error on receive of inquiry data,
1311 	 * we're just plain out of luck because we told the host
1312 	 * adapter to not watch for parity errors.
1313 	 */
1314 	if ((inq_pkt->pkt_state & STATE_XFERRED_DATA) == 0 ||
1315 	    ((SUN_INQSIZE - inq_pkt->pkt_resid) < SUN_MIN_INQLEN)) {
1316 		rval = SCSIPROBE_NONCCS;
1317 	} else {
1318 		ASSERT(inq_pkt->pkt_resid >= 0);
1319 		bcopy((caddr_t)inq_bp->b_un.b_addr,
1320 		    (caddr_t)sd->sd_inq, (SUN_INQSIZE - inq_pkt->pkt_resid));
1321 		rval = SCSIPROBE_EXISTS;
1322 	}
1323 
1324 out:
1325 	/*
1326 	 * If lun > 0 we need to figure out if this is a scsi-1 device where
1327 	 * the "real" lun needs to be embedded into the cdb.
1328 	 */
1329 	if ((rval == SCSIPROBE_EXISTS) && (pass == 1) &&
1330 	    (sd->sd_address.a_lun > 0) && (sd->sd_inq->inq_ansi == 0x1)) {
1331 		pass++;
1332 		if (sd->sd_address.a_lun <= 7)
1333 			goto again;
1334 
1335 		/*
1336 		 * invalid lun for scsi-1,
1337 		 * return probe failure.
1338 		 */
1339 		rval = SCSIPROBE_FAILURE;
1340 	}
1341 
1342 	if (rq_pkt) {
1343 		scsi_destroy_pkt(rq_pkt);
1344 	}
1345 	if (inq_pkt) {
1346 		scsi_destroy_pkt(inq_pkt);
1347 	}
1348 	if (rq_bp) {
1349 		scsi_free_consistent_buf(rq_bp);
1350 	}
1351 	if (inq_bp) {
1352 		scsi_free_consistent_buf(inq_bp);
1353 	}
1354 	return (rval);
1355 }
1356 
1357 /*
1358  * Convert from a scsi_device structure pointer to a scsi_hba_tran structure
1359  * pointer. The correct way to do this is
1360  *
1361  *	#define	DEVP_TO_TRAN(sd)	((sd)->sd_address.a_hba_tran)
1362  *
1363  * however we have some consumers that place their own vector in a_hba_tran. To
1364  * avoid problems, we implement this using the sd_tran_safe. See
1365  * scsi_hba_initchild for more details.
1366  */
1367 #define	DEVP_TO_TRAN(sd)	((sd)->sd_tran_safe)
1368 
1369 /*
1370  * Function, callable from SCSA framework, to get 'human' readable REPORTDEV
1371  * addressing information from scsi_device properties.
1372  */
1373 int
1374 scsi_ua_get_reportdev(struct scsi_device *sd, char *ra, int len)
1375 {
1376 	/* use deprecated tran_get_bus_addr interface if it is defined */
1377 	/* NOTE: tran_get_bus_addr is a poor name choice for interface */
1378 	if (DEVP_TO_TRAN(sd)->tran_get_bus_addr)
1379 		return ((*DEVP_TO_TRAN(sd)->tran_get_bus_addr)(sd, ra, len));
1380 	return (scsi_hba_ua_get_reportdev(sd, ra, len));
1381 }
1382 
1383 /*
1384  * Function, callable from HBA driver's tran_get_bus_addr(9E) implementation,
1385  * to get standard form of human readable REPORTDEV addressing information
1386  * from scsi_device properties.
1387  */
1388 int
1389 scsi_hba_ua_get_reportdev(struct scsi_device *sd, char *ra, int len)
1390 {
1391 	int		tgt, lun, sfunc;
1392 	char		*tgt_port;
1393 	scsi_lun64_t	lun64;
1394 
1395 	/* get device unit-address properties */
1396 	tgt = scsi_device_prop_get_int(sd, SCSI_DEVICE_PROP_PATH,
1397 	    SCSI_ADDR_PROP_TARGET, -1);
1398 	if (scsi_device_prop_lookup_string(sd, SCSI_DEVICE_PROP_PATH,
1399 	    SCSI_ADDR_PROP_TARGET_PORT, &tgt_port) != DDI_PROP_SUCCESS)
1400 		tgt_port = NULL;
1401 	if ((tgt == -1) && (tgt_port == NULL))
1402 		return (0);		/* no target */
1403 
1404 	lun = scsi_device_prop_get_int(sd, SCSI_DEVICE_PROP_PATH,
1405 	    SCSI_ADDR_PROP_LUN, 0);
1406 	lun64 = scsi_device_prop_get_int64(sd, SCSI_DEVICE_PROP_PATH,
1407 	    SCSI_ADDR_PROP_LUN64, lun);
1408 	sfunc = scsi_device_prop_get_int(sd, SCSI_DEVICE_PROP_PATH,
1409 	    SCSI_ADDR_PROP_SFUNC, -1);
1410 
1411 	/*
1412 	 * XXX should the default be to print this in decimal for
1413 	 * "human readable" form, so it matches conf files?
1414 	 */
1415 	if (tgt_port) {
1416 		if (sfunc == -1)
1417 			(void) snprintf(ra, len,
1418 			    "%s %s lun %" PRIx64,
1419 			    SCSI_ADDR_PROP_TARGET_PORT, tgt_port, lun64);
1420 		else
1421 			(void) snprintf(ra, len,
1422 			    "%s %s lun %" PRIx64 " sfunc %x",
1423 			    SCSI_ADDR_PROP_TARGET_PORT, tgt_port, lun64, sfunc);
1424 		scsi_device_prop_free(sd, SCSI_DEVICE_PROP_PATH, tgt_port);
1425 	} else {
1426 		if (sfunc == -1)
1427 			(void) snprintf(ra, len,
1428 			    "%s %x lun %" PRIx64,
1429 			    SCSI_ADDR_PROP_TARGET, tgt, lun64);
1430 		else
1431 			(void) snprintf(ra, len,
1432 			    "%s %x lun %" PRIx64 " sfunc %x",
1433 			    SCSI_ADDR_PROP_TARGET, tgt, lun64, sfunc);
1434 	}
1435 
1436 	return (1);
1437 }
1438 
1439 /*
1440  * scsi_ua_get: using properties, return "unit-address" string.
1441  * Called by SCSA framework, may call HBAs tran function.
1442  */
1443 int
1444 scsi_ua_get(struct scsi_device *sd, char *ua, int len)
1445 {
1446 	char		*eua;
1447 
1448 	/* See if we already have established the unit-address. */
1449 	if ((eua = scsi_device_unit_address(sd)) != NULL) {
1450 		(void) strlcpy(ua, eua, len);
1451 		return (1);
1452 	}
1453 
1454 	/* Use deprecated tran_get_name interface if it is defined. */
1455 	/* NOTE: tran_get_name is a poor name choice for interface */
1456 	if (DEVP_TO_TRAN(sd)->tran_get_name)
1457 		return ((*DEVP_TO_TRAN(sd)->tran_get_name)(sd, ua, len));
1458 
1459 	/* Use generic property implementation */
1460 	return (scsi_hba_ua_get(sd, ua, len));
1461 }
1462 
1463 /*
1464  * scsi_hba_ua_get: using properties, return "unit-address" string.
1465  * This function may be called from an HBAs tran function.
1466  *
1467  * Function to get "unit-address" in "name@unit-address" /devices path
1468  * component form from the scsi_device unit-address properties on a node.
1469  *
1470  * NOTE: This function works in conjunction with scsi_hba_ua_set().
1471  */
1472 int
1473 scsi_hba_ua_get(struct scsi_device *sd, char *ua, int len)
1474 {
1475 	int		tgt, lun, sfunc;
1476 	char		*tgt_port;
1477 	scsi_lun64_t	lun64;
1478 
1479 	/* get device unit-address properties */
1480 	tgt = scsi_device_prop_get_int(sd, SCSI_DEVICE_PROP_PATH,
1481 	    SCSI_ADDR_PROP_TARGET, -1);
1482 	if (scsi_device_prop_lookup_string(sd, SCSI_DEVICE_PROP_PATH,
1483 	    SCSI_ADDR_PROP_TARGET_PORT, &tgt_port) != DDI_PROP_SUCCESS)
1484 		tgt_port = NULL;
1485 	if ((tgt == -1) && (tgt_port == NULL))
1486 		return (0);		/* no target */
1487 
1488 	lun = scsi_device_prop_get_int(sd, SCSI_DEVICE_PROP_PATH,
1489 	    SCSI_ADDR_PROP_LUN, 0);
1490 	lun64 = scsi_device_prop_get_int64(sd, SCSI_DEVICE_PROP_PATH,
1491 	    SCSI_ADDR_PROP_LUN64, lun);
1492 	sfunc = scsi_device_prop_get_int(sd, SCSI_DEVICE_PROP_PATH,
1493 	    SCSI_ADDR_PROP_SFUNC, -1);
1494 	if (tgt_port) {
1495 		if (sfunc == -1)
1496 			(void) snprintf(ua, len, "%s,%" PRIx64,
1497 			    tgt_port, lun64);
1498 		else
1499 			(void) snprintf(ua, len, "%s,%" PRIx64 ",%x",
1500 			    tgt_port, lun64, sfunc);
1501 		scsi_device_prop_free(sd, SCSI_DEVICE_PROP_PATH, tgt_port);
1502 	} else {
1503 		if (sfunc == -1)
1504 			(void) snprintf(ua, len, "%x,%" PRIx64, tgt, lun64);
1505 		else
1506 			(void) snprintf(ua, len, "%x,%" PRIx64 ",%x",
1507 			    tgt, lun64, sfunc);
1508 	}
1509 	return (1);
1510 }
1511 
1512 static void
1513 create_inquiry_props(struct scsi_device *sd)
1514 {
1515 	struct scsi_inquiry *inq = sd->sd_inq;
1516 
1517 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
1518 	    INQUIRY_DEVICE_TYPE, (int)inq->inq_dtype);
1519 
1520 	/*
1521 	 * Create the following properties:
1522 	 *
1523 	 * inquiry-vendor-id	Vendor id (INQUIRY data bytes 8-15)
1524 	 * inquiry-product-id	Product id (INQUIRY data bytes 16-31)
1525 	 * inquiry-revision-id	Product Rev level (INQUIRY data bytes 32-35)
1526 	 *
1527 	 * NOTE: We don't support creation of these properties for scsi-1
1528 	 * devices (as the vid, pid and revision were not defined) and we
1529 	 * don't create the property if they are of zero length when
1530 	 * stripped of Nulls and spaces.
1531 	 *
1532 	 * NOTE: The first definition of these properties sticks. This gives
1533 	 * a transport the ability to provide a higher-quality definition
1534 	 * than the standard SCSI INQUIRY data.
1535 	 */
1536 	if (inq->inq_ansi != 1) {
1537 		if (ddi_prop_exists(DDI_DEV_T_NONE, sd->sd_dev,
1538 		    DDI_PROP_TYPE_STRING, INQUIRY_VENDOR_ID) == 0)
1539 			(void) scsi_device_prop_update_inqstring(sd,
1540 			    INQUIRY_VENDOR_ID,
1541 			    inq->inq_vid, sizeof (inq->inq_vid));
1542 
1543 		if (ddi_prop_exists(DDI_DEV_T_NONE, sd->sd_dev,
1544 		    DDI_PROP_TYPE_STRING, INQUIRY_PRODUCT_ID) == 0)
1545 			(void) scsi_device_prop_update_inqstring(sd,
1546 			    INQUIRY_PRODUCT_ID,
1547 			    inq->inq_pid, sizeof (inq->inq_pid));
1548 
1549 		if (ddi_prop_exists(DDI_DEV_T_NONE, sd->sd_dev,
1550 		    DDI_PROP_TYPE_STRING, INQUIRY_REVISION_ID) == 0)
1551 			(void) scsi_device_prop_update_inqstring(sd,
1552 			    INQUIRY_REVISION_ID,
1553 			    inq->inq_revision, sizeof (inq->inq_revision));
1554 	}
1555 }
1556 
1557 /*
1558  * Create 'inquiry' string properties.  An 'inquiry' string gets special
1559  * treatment to trim trailing blanks (etc) and ensure null termination.
1560  */
1561 int
1562 scsi_device_prop_update_inqstring(struct scsi_device *sd,
1563     char *name, char *data, size_t len)
1564 {
1565 	int	ilen;
1566 	char	*data_string;
1567 	int	rv;
1568 
1569 	ilen = scsi_ascii_inquiry_len(data, len);
1570 	ASSERT(ilen <= (int)len);
1571 	if (ilen <= 0)
1572 		return (DDI_PROP_INVAL_ARG);
1573 
1574 	/* ensure null termination */
1575 	data_string = kmem_zalloc(ilen + 1, KM_SLEEP);
1576 	bcopy(data, data_string, ilen);
1577 	rv = ndi_prop_update_string(DDI_DEV_T_NONE,
1578 	    sd->sd_dev, name, data_string);
1579 	kmem_free(data_string, ilen + 1);
1580 	return (rv);
1581 }
1582 
1583 /*
1584  * Interfaces associated with SCSI_HBA_ADDR_COMPLEX
1585  * per-scsi_device HBA private data support.
1586  *
1587  * scsi_address_device returns NULL if we're not SCSI_HBA_ADDR_COMPLEX,
1588  * thereby allowing use of scsi_address_device as a test for
1589  * SCSI_HBA_ADDR_COMPLEX.
1590  */
1591 struct scsi_device *
1592 scsi_address_device(struct scsi_address *sa)
1593 {
1594 	return ((sa->a_hba_tran->tran_hba_flags & SCSI_HBA_ADDR_COMPLEX) ?
1595 	    sa->a.a_sd : NULL);
1596 }
1597 
1598 void
1599 scsi_device_hba_private_set(struct scsi_device *sd, void *data)
1600 {
1601 	ASSERT(sd->sd_address.a_hba_tran->tran_hba_flags &
1602 	    SCSI_HBA_ADDR_COMPLEX);
1603 	sd->sd_hba_private = data;
1604 }
1605 
1606 void *
1607 scsi_device_hba_private_get(struct scsi_device *sd)
1608 {
1609 	ASSERT(sd->sd_address.a_hba_tran->tran_hba_flags &
1610 	    SCSI_HBA_ADDR_COMPLEX);
1611 	return (sd->sd_hba_private);
1612 }
1613 
1614 /*
1615  * This routine is called from the start of scsi_probe() if a tgt/LUN to be
1616  * probed *may* be a request to probe a strictly SCSI-2 target (with respect
1617  * to LUNs) -- and this probe may be for a LUN number greater than 7,
1618  * which can cause a hardware hang
1619  *
1620  * return 0 if the probe can proceed,
1621  * else return 1, meaning do *NOT* probe this target/LUN
1622  */
1623 static int
1624 scsi_check_ss2_LUN_limit(struct scsi_device *sd)
1625 {
1626 	struct scsi_address	*ap = &(sd->sd_address);
1627 	dev_info_t		*pdevi =
1628 	    (dev_info_t *)DEVI(sd->sd_dev)->devi_parent;
1629 	int			ret_val = 0;	/* default return value */
1630 	uchar_t			*tgt_list;
1631 	uint_t			tgt_nelements;
1632 	int			i;
1633 
1634 
1635 	/*
1636 	 * check for what *might* be a problem probe, only we don't
1637 	 * know yet what's really at the destination target/LUN
1638 	 */
1639 	if ((ap->a_target >= NTARGETS_WIDE) ||
1640 	    (ap->a_lun < NLUNS_PER_TARGET)) {
1641 		return (0);		/* okay to probe this target */
1642 	}
1643 
1644 	/*
1645 	 * this *might* be a problematic probe, so look to see
1646 	 * if the inquiry data matches
1647 	 */
1648 	SCSI_PROBE_DEBUG2(1, "SCSA pre-probe: checking tgt.LUN=%d.%d\n",
1649 	    ap->a_target, ap->a_lun);
1650 	SCSI_PROBE_DEBUG1(2,
1651 	    "SCSA pre-probe: scanning parent node name: %s ...\n",
1652 	    ddi_node_name(pdevi));
1653 
1654 	/*
1655 	 * look for a special property of our parent node that lists
1656 	 * the targets under it for which we do *NOT* want to probe
1657 	 * if LUN>7 -- if the property is found, look to see if our
1658 	 * target ID is on that list
1659 	 */
1660 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, pdevi,
1661 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SS2_LUN0_TGT_LIST_PROP,
1662 	    &tgt_list, &tgt_nelements) != DDI_PROP_SUCCESS) {
1663 		/*
1664 		 * no list, so it must be okay to probe this target.LUN
1665 		 */
1666 		SCSI_PROBE_DEBUG0(3,
1667 		    "SCSA pre-probe: NO parent prop found\n");
1668 	} else {
1669 		for (i = 0; i < tgt_nelements; i++) {
1670 			if (tgt_list[i] == ap->a_target) {
1671 				/*
1672 				 * we found a match, which means we do *NOT*
1673 				 * want to probe the specified target.LUN
1674 				 */
1675 				ret_val = 1;
1676 				break;
1677 			}
1678 		}
1679 		ddi_prop_free(tgt_list);
1680 #ifdef	DEBUG
1681 		if (ret_val == 1) {
1682 			SCSI_PROBE_DEBUG2(1,
1683 			    "SCSA pre-probe: marker node FOUND for "
1684 			    "tgt.LUN=%d.%d, so SKIPPING it\n",
1685 			    ap->a_target, ap->a_lun);
1686 		} else {
1687 			SCSI_PROBE_DEBUG0(2,
1688 			    "SCSA pre-probe: NO marker node found"
1689 			    " -- OK to probe\n");
1690 		}
1691 #endif
1692 	}
1693 	return (ret_val);
1694 }
1695 
1696 
1697 /*
1698  * this routine is called from near the end of scsi_probe(),
1699  * to see if the just-probed node is on our list of strictly-SCSI-2 nodes,
1700  * and if it is we mark our parent node with this information
1701  */
1702 static void
1703 scsi_establish_LUN_limit(struct scsi_device *sd)
1704 {
1705 	struct scsi_address	*ap = &(sd->sd_address);
1706 	struct scsi_inquiry	*inq = sd->sd_inq;
1707 	dev_info_t		*devi = sd->sd_dev;
1708 	char			*vid = NULL;
1709 	char			*pid = NULL;
1710 	char			*rev = NULL;
1711 	int			i;
1712 	const ss2_lun0_info_t	*p;
1713 	int			bad_target_found = 0;
1714 
1715 
1716 	/*
1717 	 * if this inquiry data shows that we have a strictly-SCSI-2 device
1718 	 * at LUN 0, then add it to our list of strictly-SCSI-2 devices,
1719 	 * so that we can avoid probes where LUN>7 on this device later
1720 	 */
1721 	if ((ap->a_lun != 0) ||
1722 	    (ap->a_target >= NTARGETS_WIDE) ||
1723 	    (inq->inq_dtype != DTYPE_PROCESSOR) ||
1724 	    (inq->inq_ansi != 2)) {
1725 		/*
1726 		 * this can't possibly be a node we want to look at, since
1727 		 * either LUN is greater than 0, target is greater than or
1728 		 * equal to 16, device type
1729 		 * is not processor, or SCSI level is not SCSI-2,
1730 		 * so don't bother checking for a strictly SCSI-2
1731 		 * (only 8 LUN) target
1732 		 */
1733 		return;				/* don't care */
1734 	}
1735 
1736 	SCSI_PROBE_DEBUG2(1, "SCSA post-probe: LUN limit on tgt.LUN=%d.%d, "
1737 	    "SCSI-2 PROCESSOR?\n", ap->a_target, ap->a_lun);
1738 
1739 	ASSERT(devi != NULL);
1740 
1741 	/*
1742 	 * we have a node that has been probed that is: LUN=0, target<16,
1743 	 * PROCESSOR-type SCSI target, and at the SCSI-2 level, so
1744 	 * check INQ properties to see if it's in our list of strictly
1745 	 * SCSI-2 targets
1746 	 *
1747 	 * first we have to get the VID/PID/REV INQUIRY properties for
1748 	 * comparison
1749 	 */
1750 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi,
1751 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1752 	    INQUIRY_VENDOR_ID, &vid) != DDI_PROP_SUCCESS) {
1753 		SCSI_PROBE_DEBUG1(2, "SCSA post-probe: prop \"%s\" missing\n",
1754 		    INQUIRY_VENDOR_ID);
1755 		goto dun;
1756 	}
1757 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi,
1758 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1759 	    INQUIRY_PRODUCT_ID, &pid) != DDI_PROP_SUCCESS) {
1760 		SCSI_PROBE_DEBUG1(2, "SCSA post-probe: prop \"%s\" missing\n",
1761 		    INQUIRY_PRODUCT_ID);
1762 		goto dun;
1763 	}
1764 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi,
1765 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1766 	    INQUIRY_REVISION_ID, &rev) != DDI_PROP_SUCCESS) {
1767 		SCSI_PROBE_DEBUG1(2, "SCSA post-probe: prop \"%s\" missing\n",
1768 		    INQUIRY_REVISION_ID);
1769 		goto dun;
1770 	}
1771 
1772 	SCSI_PROBE_DEBUG3(3, "SCSA post-probe: looking for vid/pid/rev = "
1773 	    "\"%s\"/\"%s\"/\"%s\"\n", vid, pid, rev);
1774 
1775 	/*
1776 	 * now that we have the INQUIRY properties from the device node,
1777 	 * compare them with our known offenders
1778 	 *
1779 	 * Note: comparison is *CASE* *SENSITIVE*
1780 	 */
1781 	for (i = 0; i < scsi_probe_strict_s2_size; i++) {
1782 		p = &scsi_probe_strict_s2_list[i];
1783 
1784 		if ((strcmp(p->sli_vid, vid) == 0) &&
1785 		    (strcmp(p->sli_pid, pid) == 0) &&
1786 		    (strcmp(p->sli_rev, rev) == 0)) {
1787 			/*
1788 			 * we found a match -- do NOT want to probe this one
1789 			 */
1790 			SCSI_PROBE_DEBUG3(1,
1791 			    "SCSA post-probe: recording strict SCSI-2 node "
1792 			    "vid/pid/rev = \"%s\"/\"%s\"/\"%s\"\n",
1793 			    vid, pid, rev);
1794 
1795 			/*
1796 			 * set/update private parent-node property,
1797 			 * so we can find out about this node later
1798 			 */
1799 			bad_target_found = 1;
1800 			break;
1801 		}
1802 	}
1803 
1804 	/*
1805 	 * either add remove target number from parent property
1806 	 */
1807 	scsi_update_parent_ss2_prop(devi, ap->a_target, bad_target_found);
1808 
1809 dun:
1810 	if (vid != NULL) {
1811 		ddi_prop_free(vid);
1812 	}
1813 	if (pid != NULL) {
1814 		ddi_prop_free(pid);
1815 	}
1816 	if (rev != NULL) {
1817 		ddi_prop_free(rev);
1818 	}
1819 }
1820 
1821 
1822 /*
1823  * update the parent node to add in the supplied tgt number to the target
1824  * list property already present (if any)
1825  *
1826  * since the target list can never be longer than 16, and each target
1827  * number is also small, we can save having to alloc memory by putting
1828  * a 16-byte array on the stack and using it for property memory
1829  *
1830  * if "add_tgt" is set then add the target to the parent's property, else
1831  * remove it (if present)
1832  */
1833 static void
1834 scsi_update_parent_ss2_prop(dev_info_t *devi, int tgt, int add_tgt)
1835 {
1836 	dev_info_t	*pdevi = (dev_info_t *)DEVI(devi)->devi_parent;
1837 	uchar_t		*tgt_list;
1838 	uint_t		nelements;
1839 	uint_t		new_nelements;
1840 	int		i;
1841 	int		update_result;
1842 	uchar_t		new_tgt_list[NTARGETS_WIDE];
1843 
1844 
1845 	ASSERT(pdevi != NULL);
1846 
1847 	SCSI_PROBE_DEBUG3(3,
1848 	    "SCSA post-probe: updating parent=%s property to %s tgt=%d\n",
1849 	    ddi_node_name(pdevi), add_tgt ? "add" : "remove", tgt);
1850 
1851 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, pdevi,
1852 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1853 	    SS2_LUN0_TGT_LIST_PROP, &tgt_list, &nelements) ==
1854 	    DDI_PROP_SUCCESS) {
1855 
1856 		if (add_tgt) {
1857 			/*
1858 			 * we found an existing property -- we might need
1859 			 *	to add to it
1860 			 */
1861 			for (i = 0; i < nelements; i++) {
1862 				if (tgt_list[i] == tgt) {
1863 					/* target already in list */
1864 					SCSI_PROBE_DEBUG1(2, "SCSA post-probe:"
1865 					    " tgt %d already listed\n", tgt);
1866 					ddi_prop_free(tgt_list);
1867 					return;
1868 				}
1869 			}
1870 
1871 			/*
1872 			 * need to append our target number to end of list
1873 			 *	(no need sorting list, as it's so short)
1874 			 */
1875 
1876 			/*
1877 			 * will this new entry fit ?? -- it should, since
1878 			 *	the array is 16-wide and only keep track of
1879 			 *	16 targets, but check just in case
1880 			 */
1881 			new_nelements = nelements + 1;
1882 			if (new_nelements >= NTARGETS_WIDE) {
1883 				SCSI_PROBE_DEBUG0(1, "SCSA post-probe: "
1884 				    "internal error: no room "
1885 				    "for more targets?\n");
1886 				ddi_prop_free(tgt_list);
1887 				return;
1888 			}
1889 
1890 			/* copy existing list then add our tgt number to end */
1891 			bcopy((void *)tgt_list, (void *)new_tgt_list,
1892 			    sizeof (uchar_t) * nelements);
1893 			new_tgt_list[new_nelements - 1] = (uchar_t)tgt;
1894 		} else {
1895 			/*
1896 			 * we need to remove our target number from the list,
1897 			 *	so copy all of the other target numbers,
1898 			 *	skipping ours
1899 			 */
1900 			int	tgt_removed = 0;
1901 
1902 			new_nelements = 0;
1903 			for (i = 0; i < nelements; i++) {
1904 				if (tgt_list[i] != tgt) {
1905 					new_tgt_list[new_nelements++] =
1906 					    tgt_list[i];
1907 				} else {
1908 					/* skip this target */
1909 					tgt_removed++;
1910 				}
1911 			}
1912 
1913 			if (!tgt_removed) {
1914 				SCSI_PROBE_DEBUG1(2, "SCSA post-probe:"
1915 				    " no need to remove tgt %d\n", tgt);
1916 				ddi_prop_free(tgt_list);
1917 				return;
1918 			}
1919 		}
1920 
1921 		update_result = ddi_prop_update_byte_array(DDI_DEV_T_NONE,
1922 		    pdevi, SS2_LUN0_TGT_LIST_PROP, new_tgt_list,
1923 		    new_nelements);
1924 
1925 		ddi_prop_free(tgt_list);
1926 	} else {
1927 		/*
1928 		 * no property yet
1929 		 */
1930 		if (add_tgt) {
1931 			/*
1932 			 * create a property with just our tgt
1933 			 */
1934 			new_tgt_list[0] = (uchar_t)tgt;
1935 			new_nelements = 1;	/* just one element */
1936 
1937 			update_result = ddi_prop_update_byte_array(
1938 			    DDI_DEV_T_NONE, pdevi, SS2_LUN0_TGT_LIST_PROP,
1939 			    new_tgt_list, new_nelements);
1940 		} else {
1941 			/*
1942 			 * no list so no need to remove tgt from that list
1943 			 */
1944 			return;
1945 		}
1946 	}
1947 
1948 #ifdef	DEBUG
1949 	/*
1950 	 * if we get here we have tried to add/update properties
1951 	 */
1952 	if (update_result != DDI_PROP_SUCCESS) {
1953 		SCSI_PROBE_DEBUG2(1, "SCSA post-probe: can't update parent "
1954 		    "property with tgt=%d (%d)\n", tgt, update_result);
1955 	} else {
1956 		if (add_tgt) {
1957 			SCSI_PROBE_DEBUG3(2,
1958 			    "SCSA post-probe: added tgt=%d to parent "
1959 			    "prop=\"%s\" (now %d entries)\n",
1960 			    tgt, SS2_LUN0_TGT_LIST_PROP, new_nelements);
1961 		} else {
1962 			SCSI_PROBE_DEBUG3(2,
1963 			    "SCSA post-probe: removed tgt=%d from parent "
1964 			    "prop=\"%s\" (now %d entries)\n",
1965 			    tgt, SS2_LUN0_TGT_LIST_PROP, new_nelements);
1966 		}
1967 	}
1968 #endif
1969 }
1970 
1971 
1972 /* XXX BEGIN: find a better place for this: inquiry.h? */
1973 /*
1974  * Definitions used by device id registration routines
1975  */
1976 #define	VPD_HEAD_OFFSET		3	/* size of head for vpd page */
1977 #define	VPD_PAGE_LENGTH		3	/* offset for pge length data */
1978 #define	VPD_MODE_PAGE		1	/* offset into vpd pg for "page code" */
1979 
1980 /* size for devid inquiries */
1981 #define	MAX_INQUIRY_SIZE	0xF0
1982 #define	MAX_INQUIRY_SIZE_EVPD	0xFF	/* XXX why is this longer */
1983 /* XXX END: find a better place for these */
1984 
1985 
1986 /*
1987  * Decorate devinfo node with identity properties using information obtained
1988  * from device. These properties are used by device enumeration code to derive
1989  * the devid, and guid for the device. These properties are also used to
1990  * determine if a device should be enumerated under the physical HBA (PHCI) or
1991  * the virtual HBA (VHCI, for mpxio support).
1992  *
1993  * Return zero on success. If commands that should succeed fail or allocations
1994  * fail then return failure (non-zero). It is possible for this function to
1995  * return success and not have decorated the node with any additional identity
1996  * information if the device correctly responds indicating that they are not
1997  * supported.  When failure occurs the caller should consider not making the
1998  * device accessible.
1999  */
2000 int
2001 scsi_device_identity(struct scsi_device *sd, int (*callback)())
2002 {
2003 	dev_info_t	*devi		= sd->sd_dev;
2004 	uchar_t		*inq80		= NULL;
2005 	uchar_t		*inq83		= NULL;
2006 	int		rval;
2007 	size_t		len;
2008 	int		pg80, pg83;
2009 
2010 	/* find out what pages are supported by device */
2011 	if (check_vpd_page_support8083(sd, callback, &pg80, &pg83) == -1)
2012 		return (-1);
2013 
2014 	/* if available, collect page 80 data and add as property */
2015 	if (pg80) {
2016 		inq80 = kmem_zalloc(MAX_INQUIRY_SIZE,
2017 		    ((callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP));
2018 		if (inq80 == NULL) {
2019 			rval = -1;
2020 			goto out;
2021 		}
2022 
2023 		rval = send_scsi_INQUIRY(sd, callback, inq80,
2024 		    MAX_INQUIRY_SIZE, 0x01, 0x80, &len, STC_IDENTITY_PG80);
2025 		if (rval)
2026 			goto out;		/* should have worked */
2027 
2028 		if (len && (ndi_prop_update_byte_array(DDI_DEV_T_NONE, devi,
2029 		    "inquiry-page-80", inq80, len) != DDI_PROP_SUCCESS)) {
2030 			cmn_err(CE_WARN, "scsi_device_identity: "
2031 			    "failed to add page80 prop");
2032 			rval = -1;
2033 			goto out;
2034 		}
2035 	}
2036 
2037 	/* if available, collect page 83 data and add as property */
2038 	if (pg83) {
2039 		inq83 = kmem_zalloc(MAX_INQUIRY_SIZE,
2040 		    ((callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP));
2041 		if (inq83 == NULL) {
2042 			rval = -1;
2043 			goto out;
2044 		}
2045 
2046 		rval = send_scsi_INQUIRY(sd, callback, inq83,
2047 		    MAX_INQUIRY_SIZE, 0x01, 0x83, &len, STC_IDENTITY_PG83);
2048 		if (rval)
2049 			goto out;		/* should have worked */
2050 
2051 		if (len && (ndi_prop_update_byte_array(DDI_DEV_T_NONE, devi,
2052 		    "inquiry-page-83", inq83, len) != DDI_PROP_SUCCESS)) {
2053 			cmn_err(CE_WARN, "scsi_device_identity: "
2054 			    "failed to add page83 prop");
2055 			rval = -1;
2056 			goto out;
2057 		}
2058 	}
2059 
2060 	/* Commands worked, identity information that exists has been added. */
2061 	rval = 0;
2062 
2063 	/* clean up resources */
2064 out:	if (inq80 != NULL)
2065 		kmem_free(inq80, MAX_INQUIRY_SIZE);
2066 	if (inq83 != NULL)
2067 		kmem_free(inq83, MAX_INQUIRY_SIZE);
2068 
2069 	return (rval);
2070 }
2071 
2072 /*
2073  * Send an INQUIRY command with the EVPD bit set and a page code of 0x00 to
2074  * the device, returning zero on success. Returned INQUIRY data is used to
2075  * determine which vital product pages are supported. The device idenity
2076  * information we are looking for is in pages 0x83 and/or 0x80. If the device
2077  * fails the EVPD inquiry then no pages are supported but the call succeeds.
2078  * Return -1 (failure) if there were memory allocation failures or if a
2079  * command faild that should have worked.
2080  */
2081 static int
2082 check_vpd_page_support8083(struct scsi_device *sd, int (*callback)(),
2083 	int *ppg80, int *ppg83)
2084 {
2085 	uchar_t *page_list;
2086 	int	counter;
2087 	int	rval;
2088 
2089 	/* pages are not supported */
2090 	*ppg80 = 0;
2091 	*ppg83 = 0;
2092 
2093 	/*
2094 	 * We'll set the page length to the maximum to save figuring it out
2095 	 * with an additional call.
2096 	 */
2097 	page_list =  kmem_zalloc(MAX_INQUIRY_SIZE_EVPD,
2098 	    ((callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP));
2099 	if (page_list == NULL)
2100 		return (-1);		/* memory allocation problem */
2101 
2102 	/* issue page 0 (Supported VPD Pages) INQUIRY with evpd set */
2103 	rval = send_scsi_INQUIRY(sd, callback,
2104 	    page_list, MAX_INQUIRY_SIZE_EVPD, 1, 0, NULL, STC_VPD_CHECK);
2105 
2106 	/*
2107 	 * Now we must validate that the device accepted the command (some
2108 	 * devices do not support it) and if the idenity pages we are
2109 	 * interested in are supported.
2110 	 */
2111 	if ((rval == 0) &&
2112 	    (page_list[VPD_MODE_PAGE] == 0x00)) {
2113 		/* Loop to find one of the 2 pages we need */
2114 		counter = 4;  /* Supported pages start at byte 4, with 0x00 */
2115 
2116 		/*
2117 		 * Pages are returned in ascending order, and 0x83 is the
2118 		 * last page we are hoping to find.
2119 		 */
2120 		while ((page_list[counter] <= 0x83) &&
2121 		    (counter <= (page_list[VPD_PAGE_LENGTH] +
2122 		    VPD_HEAD_OFFSET))) {
2123 			/*
2124 			 * Add 3 because page_list[3] is the number of
2125 			 * pages minus 3
2126 			 */
2127 
2128 			switch (page_list[counter]) {
2129 			case 0x80:
2130 				*ppg80 = 1;
2131 				break;
2132 			case 0x83:
2133 				*ppg83 = 1;
2134 				break;
2135 			}
2136 			counter++;
2137 		}
2138 	}
2139 
2140 	kmem_free(page_list, MAX_INQUIRY_SIZE_EVPD);
2141 	return (0);
2142 }
2143 
2144 /*
2145  * Send INQUIRY command with specified EVPD and page code.  Return
2146  * zero on success.  On success, the amount of data transferred
2147  * is returned in *lenp.
2148  */
2149 static int
2150 send_scsi_INQUIRY(struct scsi_device *sd, int (*callback)(),
2151     uchar_t *bufaddr, size_t buflen,
2152     uchar_t evpd, uchar_t page_code, size_t *lenp,
2153     enum scsi_test_ctxt ctxt)
2154 {
2155 	int		(*cb_flag)();
2156 	struct buf	*inq_bp;
2157 	struct scsi_pkt *inq_pkt = NULL;
2158 	int		rval = -1;
2159 
2160 	if (lenp)
2161 		*lenp = 0;
2162 	if (callback != SLEEP_FUNC && callback != NULL_FUNC)
2163 		cb_flag = NULL_FUNC;
2164 	else
2165 		cb_flag = callback;
2166 	inq_bp = scsi_alloc_consistent_buf(ROUTE,
2167 	    (struct buf *)NULL, buflen, B_READ, cb_flag, NULL);
2168 	if (inq_bp == NULL)
2169 		goto out;		/* memory allocation problem */
2170 
2171 	inq_pkt = scsi_init_pkt(ROUTE, (struct scsi_pkt *)NULL,
2172 	    inq_bp, CDB_GROUP0, sizeof (struct scsi_arq_status),
2173 	    0, PKT_CONSISTENT, callback, NULL);
2174 	if (inq_pkt == NULL)
2175 		goto out;		/* memory allocation problem */
2176 
2177 	ASSERT(inq_bp->b_error == 0);
2178 
2179 	/* form INQUIRY cdb with specified EVPD and page code */
2180 	(void) scsi_setup_cdb((union scsi_cdb *)inq_pkt->pkt_cdbp,
2181 	    SCMD_INQUIRY, 0, buflen, 0);
2182 	inq_pkt->pkt_cdbp[1] = evpd;
2183 	inq_pkt->pkt_cdbp[2] = page_code;
2184 
2185 	inq_pkt->pkt_time = SCSI_POLL_TIMEOUT;	/* in seconds */
2186 	inq_pkt->pkt_flags = FLAG_NOINTR|FLAG_NOPARITY;
2187 
2188 	/*
2189 	 * Issue inquiry command thru scsi_test
2190 	 *
2191 	 * NOTE: This is important data about device identity, not sure why
2192 	 * NOPARITY is used. Also seems like we should check pkt_stat for
2193 	 * STATE_XFERRED_DATA.
2194 	 */
2195 	if (scsi_test(inq_pkt, ctxt) == SCSI_TEST_CMPLT_GOOD) {
2196 		ASSERT(inq_pkt->pkt_resid >= 0);
2197 		ASSERT(inq_pkt->pkt_resid <= buflen);
2198 
2199 		bcopy(inq_bp->b_un.b_addr,
2200 		    bufaddr, buflen - inq_pkt->pkt_resid);
2201 		if (lenp)
2202 			*lenp = (buflen - inq_pkt->pkt_resid);
2203 		rval = 0;
2204 	}
2205 
2206 	/*
2207 	 * XXX We should retry on target busy
2208 	 */
2209 
2210 out:	if (inq_pkt)
2211 		scsi_destroy_pkt(inq_pkt);
2212 	if (inq_bp)
2213 		scsi_free_consistent_buf(inq_bp);
2214 	return (rval);
2215 }
2216