Home | History | Annotate | Download | only in conf
      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 2009 Sun Microsystems, Inc.  All rights reserved.
     23  * Use is subject to license terms.
     24  */
     25 
     26 /*
     27  * Utility SCSI configuration routines
     28  */
     29 /*
     30  * Many routines in this file have built in parallel bus assumption
     31  * which might need to change as other interconnect evolve.
     32  */
     33 
     34 #include <sys/scsi/scsi.h>
     35 #include <sys/modctl.h>
     36 #include <sys/bitmap.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 static void create_inquiry_props(struct scsi_device *);
     61 
     62 static int scsi_check_ss2_LUN_limit(struct scsi_device *);
     63 static void scsi_establish_LUN_limit(struct scsi_device *);
     64 static void scsi_update_parent_ss2_prop(dev_info_t *, int, int);
     65 
     66 static int check_vpd_page_support8083(struct scsi_device *sd,
     67 		int (*callback)(), int *, int *);
     68 static int send_scsi_INQUIRY(struct scsi_device *sd,
     69 		int (*callback)(), uchar_t *bufaddr, size_t buflen,
     70 		uchar_t evpd, uchar_t page_code, size_t *lenp);
     71 
     72 /*
     73  * this int-array HBA-node property keeps track of strictly SCSI-2
     74  * target IDs
     75  */
     76 #define	SS2_LUN0_TGT_LIST_PROP	"ss2-targets"
     77 
     78 /*
     79  * for keeping track of nodes for which we do *NOT* want to probe above LUN 7
     80  * (i.e. strict SCSI-2 targets)
     81  *
     82  * note that we could also keep track of dtype (SCSI device type) and
     83  * ANSI (SCSI standard conformance level), but all currently-known cases of
     84  * this problem are on SCSI-2 PROCESSOR device types
     85  */
     86 typedef struct ss2_lun0_info {
     87 	const char	*sli_vid;	/* SCSI inquiry VID */
     88 	const char	*sli_pid;	/* SCSI inquiry PID */
     89 	const char	*sli_rev;	/* SCSI inquiry REV */
     90 } ss2_lun0_info_t;
     91 
     92 /*
     93  * these two workarounds are for the SCSI-2 GEM2* chips used in the
     94  * D1000 and D240
     95  */
     96 #define	SES_D1000_VID		"SYMBIOS"
     97 #define	SES_D1000_PID		"D1000"		/* the D1000 */
     98 #define	SES_D1000_REV		"2"
     99 
    100 #define	SES_D240_VID		"SUN"
    101 #define	SES_D240_PID		"D240"		/* the D240 */
    102 #define	SES_D240_REV		"2"
    103 
    104 /*
    105  * a static list of targets where we do *not* want to probe above LUN 7
    106  */
    107 static const ss2_lun0_info_t	scsi_probe_strict_s2_list[] = {
    108 	{SES_D1000_VID, SES_D1000_PID, SES_D1000_REV},
    109 	{SES_D240_VID, SES_D240_PID, SES_D240_REV},
    110 };
    111 
    112 static const int		scsi_probe_strict_s2_size =
    113 	sizeof (scsi_probe_strict_s2_list) / sizeof (struct ss2_lun0_info);
    114 
    115 
    116 #ifdef	DEBUG
    117 
    118 int	scsi_probe_debug = 0;
    119 
    120 #define	SCSI_PROBE_DEBUG0(l, s)		\
    121 		if (scsi_probe_debug >= (l)) printf(s)
    122 #define	SCSI_PROBE_DEBUG1(l, s, a1)	\
    123 		if (scsi_probe_debug >= (l)) printf(s, a1)
    124 #define	SCSI_PROBE_DEBUG2(l, s, a1, a2)	\
    125 		if (scsi_probe_debug >= (l)) printf(s, a1, a2)
    126 #define	SCSI_PROBE_DEBUG3(l, s, a1, a2, a3)	\
    127 		if (scsi_probe_debug >= (l)) printf(s, a1, a2, a3)
    128 
    129 #else	/* DEBUG */
    130 
    131 #define	SCSI_PROBE_DEBUG0(l, s)
    132 #define	SCSI_PROBE_DEBUG1(l, s, a1)
    133 #define	SCSI_PROBE_DEBUG2(l, s, a1, a2)
    134 #define	SCSI_PROBE_DEBUG3(l, s, a1, a2, a3)
    135 
    136 #endif	/* DEBUG */
    137 
    138 int	scsi_test_busy_timeout = SCSI_POLL_TIMEOUT;	/* in seconds */
    139 int	scsi_test_busy_delay = 10000;			/* 10msec in usec */
    140 
    141 /*
    142  * architecture dependent allocation restrictions. For x86, we'll set
    143  * dma_attr_addr_hi to scsi_max_phys_addr and dma_attr_sgllen to
    144  * scsi_sgl_size during _init().
    145  */
    146 #if defined(__sparc)
    147 ddi_dma_attr_t scsi_alloc_attr = {
    148 	DMA_ATTR_V0,	/* version number */
    149 	0x0,		/* lowest usable address */
    150 	0xFFFFFFFFull,	/* high DMA address range */
    151 	0xFFFFFFFFull,	/* DMA counter register */
    152 	1,		/* DMA address alignment */
    153 	1,		/* DMA burstsizes */
    154 	1,		/* min effective DMA size */
    155 	0xFFFFFFFFull,	/* max DMA xfer size */
    156 	0xFFFFFFFFull,	/* segment boundary */
    157 	1,		/* s/g list length */
    158 	512,		/* granularity of device */
    159 	0		/* DMA transfer flags */
    160 };
    161 #elif defined(__x86)
    162 ddi_dma_attr_t scsi_alloc_attr = {
    163 	DMA_ATTR_V0,	/* version number */
    164 	0x0,		/* lowest usable address */
    165 	0x0,		/* high DMA address range [set in _init()] */
    166 	0xFFFFull,	/* DMA counter register */
    167 	1,		/* DMA address alignment */
    168 	1,		/* DMA burstsizes */
    169 	1,		/* min effective DMA size */
    170 	0xFFFFFFFFull,	/* max DMA xfer size */
    171 	0xFFFFFFFFull,  /* segment boundary */
    172 	0,		/* s/g list length */
    173 	512,		/* granularity of device [set in _init()] */
    174 	0		/* DMA transfer flags */
    175 };
    176 uint64_t scsi_max_phys_addr = 0xFFFFFFFFull;
    177 int scsi_sgl_size = 0xFF;
    178 #endif
    179 
    180 ulong_t	*scsi_pkt_bad_alloc_bitmap;
    181 
    182 int
    183 _init()
    184 {
    185 	scsi_initialize_hba_interface();
    186 	scsi_watch_init();
    187 
    188 #if defined(__x86)
    189 	/* set the max physical address for iob allocs on x86 */
    190 	scsi_alloc_attr.dma_attr_addr_hi = scsi_max_phys_addr;
    191 
    192 	/*
    193 	 * set the sgllen for iob allocs on x86. If this is set less than
    194 	 * the number of pages the buffer will take (taking into account
    195 	 * alignment), it would force the allocator to try and allocate
    196 	 * contiguous pages.
    197 	 */
    198 	scsi_alloc_attr.dma_attr_sgllen = scsi_sgl_size;
    199 #endif
    200 
    201 	/* bitmap to limit scsi_pkt allocation violation messages */
    202 	scsi_pkt_bad_alloc_bitmap = kmem_zalloc(BT_SIZEOFMAP(devcnt), KM_SLEEP);
    203 
    204 	return (mod_install(&modlinkage));
    205 }
    206 
    207 /*
    208  * there is no _fini() routine because this module is never unloaded
    209  */
    210 
    211 int
    212 _info(struct modinfo *modinfop)
    213 {
    214 	return (mod_info(&modlinkage, modinfop));
    215 }
    216 
    217 #define	ROUTE	(&sd->sd_address)
    218 
    219 static int
    220 scsi_slave_do_rqsense(struct scsi_device *sd, int (*callback)())
    221 {
    222 	struct scsi_pkt *rq_pkt = NULL;
    223 	struct buf *rq_bp = NULL;
    224 	int rval = SCSIPROBE_EXISTS;
    225 
    226 	/*
    227 	 * prepare rqsense packet
    228 	 */
    229 	rq_bp = scsi_alloc_consistent_buf(ROUTE, (struct buf *)NULL,
    230 	    (uint_t)SENSE_LENGTH, B_READ, callback, NULL);
    231 	if (rq_bp == NULL) {
    232 		rval = SCSIPROBE_NOMEM;
    233 		goto out;
    234 	}
    235 
    236 	rq_pkt = scsi_init_pkt(ROUTE, (struct scsi_pkt *)NULL,
    237 	    rq_bp, CDB_GROUP0, 1, 0, PKT_CONSISTENT,
    238 	    callback, NULL);
    239 
    240 	if (rq_pkt == NULL) {
    241 		if (rq_bp->b_error == 0)
    242 			rval = SCSIPROBE_NOMEM_CB;
    243 		else
    244 			rval = SCSIPROBE_NOMEM;
    245 		goto out;
    246 	}
    247 	ASSERT(rq_bp->b_error == 0);
    248 
    249 	(void) scsi_setup_cdb((union scsi_cdb *)rq_pkt->
    250 	    pkt_cdbp, SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0);
    251 	FILL_SCSI1_LUN(sd, rq_pkt);
    252 	rq_pkt->pkt_flags = FLAG_NOINTR|FLAG_NOPARITY|FLAG_SENSING;
    253 
    254 	/*
    255 	 * The controller type is as yet unknown, so we
    256 	 * have to do a throwaway non-extended request sense,
    257 	 * and hope that that clears the check condition
    258 	 * for that unit until we can find out what kind
    259 	 * of drive it is. A non-extended request sense
    260 	 * is specified by stating that the sense block
    261 	 * has 0 length, which is taken to mean that it
    262 	 * is four bytes in length.
    263 	 */
    264 	if (scsi_poll(rq_pkt) < 0) {
    265 		rval = SCSIPROBE_FAILURE;
    266 	}
    267 
    268 out:
    269 	if (rq_pkt) {
    270 		scsi_destroy_pkt(rq_pkt);
    271 	}
    272 	if (rq_bp) {
    273 		scsi_free_consistent_buf(rq_bp);
    274 	}
    275 
    276 	return (rval);
    277 }
    278 
    279 /*
    280  *
    281  * SCSI slave probe routine - provided as a service to target drivers
    282  *
    283  * Mostly attempts to allocate and fill sd inquiry data..
    284  */
    285 
    286 int
    287 scsi_slave(struct scsi_device *sd, int (*callback)())
    288 {
    289 	struct scsi_pkt	*pkt;
    290 	int		rval = SCSIPROBE_EXISTS;
    291 
    292 	/*
    293 	 * the first test unit ready will tell us whether a target
    294 	 * responded and if there was one, it will clear the unit attention
    295 	 * condition
    296 	 */
    297 	pkt = scsi_init_pkt(ROUTE, (struct scsi_pkt *)NULL, NULL,
    298 	    CDB_GROUP0, sizeof (struct scsi_arq_status), 0, 0, callback, NULL);
    299 
    300 	if (pkt == NULL) {
    301 		return (SCSIPROBE_NOMEM_CB);
    302 	}
    303 
    304 	(void) scsi_setup_cdb((union scsi_cdb *)pkt->pkt_cdbp,
    305 	    SCMD_TEST_UNIT_READY, 0, 0, 0);
    306 	FILL_SCSI1_LUN(sd, pkt);
    307 	pkt->pkt_flags = FLAG_NOINTR|FLAG_NOPARITY;
    308 
    309 	if (scsi_poll(pkt) < 0) {
    310 		if (pkt->pkt_reason == CMD_INCOMPLETE)
    311 			rval = SCSIPROBE_NORESP;
    312 		else
    313 			rval = SCSIPROBE_FAILURE;
    314 
    315 		if ((pkt->pkt_state & STATE_ARQ_DONE) == 0) {
    316 			if (((struct scsi_status *)pkt->pkt_scbp)->sts_chk)
    317 				/*
    318 				 * scanner and processor devices can return a
    319 				 * check condition here
    320 				 */
    321 				rval = scsi_slave_do_rqsense(sd, callback);
    322 		}
    323 
    324 		if (rval != SCSIPROBE_EXISTS) {
    325 			scsi_destroy_pkt(pkt);
    326 			return (rval);
    327 		}
    328 	}
    329 
    330 	/*
    331 	 * the second test unit ready, allows the host adapter to negotiate
    332 	 * synchronous transfer period and offset
    333 	 */
    334 	if (scsi_poll(pkt) < 0) {
    335 		if (pkt->pkt_reason == CMD_INCOMPLETE)
    336 			rval = SCSIPROBE_NORESP;
    337 		else
    338 			rval = SCSIPROBE_FAILURE;
    339 	}
    340 
    341 	/*
    342 	 * do a rqsense if there was a check condition and ARQ was not done
    343 	 */
    344 	if ((pkt->pkt_state & STATE_ARQ_DONE) == 0) {
    345 		if (((struct scsi_status *)pkt->pkt_scbp)->sts_chk) {
    346 			rval = scsi_slave_do_rqsense(sd, callback);
    347 		}
    348 	}
    349 
    350 	/*
    351 	 * call scsi_probe to do the inquiry
    352 	 *
    353 	 * NOTE: there is minor difference with the old scsi_slave
    354 	 * implementation: busy conditions are not handled in scsi_probe.
    355 	 */
    356 	scsi_destroy_pkt(pkt);
    357 	if (rval == SCSIPROBE_EXISTS) {
    358 		return (scsi_probe(sd, callback));
    359 	} else {
    360 		return (rval);
    361 	}
    362 }
    363 
    364 /*
    365  * Undo scsi_slave - older interface, but still supported
    366  *
    367  * NOTE: The 'sd_inq' inquiry data is now freed by scsi_hba/scsi_vhci code
    368  * as part of free of scsi_device(9S).
    369  */
    370 /*ARGSUSED*/
    371 void
    372 scsi_unslave(struct scsi_device *sd)
    373 {
    374 }
    375 
    376 /*
    377  * Undo scsi_probe
    378  *
    379  * NOTE: The 'sd_inq' inquiry data is now freed by scsi_hba/scsi_vhci code
    380  * as part of free of scsi_device(9S).
    381  */
    382 /*ARGSUSED*/
    383 void
    384 scsi_unprobe(struct scsi_device *sd)
    385 {
    386 }
    387 
    388 /*
    389  * This is like scsi_poll, but only does retry for TRAN_BUSY.
    390  */
    391 static int
    392 scsi_test(struct scsi_pkt *pkt)
    393 {
    394 	int		rval = -1;
    395 	int		wait_usec;
    396 	int		rc;
    397 	extern int	do_polled_io;
    398 
    399 	pkt->pkt_flags |= FLAG_NOINTR;
    400 	pkt->pkt_time = SCSI_POLL_TIMEOUT;	/* in seconds */
    401 
    402 	if (scsi_ifgetcap(&pkt->pkt_address, "tagged-qing", 1) == 1) {
    403 		pkt->pkt_flags |= FLAG_STAG;
    404 	}
    405 
    406 	/*
    407 	 * Each TRAN_BUSY response waits scsi_test_busy_delay usec up to a
    408 	 * maximum of scsi_test_busy_timeout.
    409 	 */
    410 	for (wait_usec = 0; (wait_usec / 1000000) <= scsi_test_busy_timeout;
    411 	    wait_usec += scsi_test_busy_delay) {
    412 
    413 		/* Initialize pkt status variables */
    414 		*pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0;
    415 
    416 		rc = scsi_transport(pkt);
    417 		if ((rc != TRAN_BUSY) || (scsi_test_busy_delay == 0) ||
    418 		    (scsi_test_busy_timeout == 0))
    419 			break;
    420 
    421 		/* transport busy, wait */
    422 		if ((curthread->t_flag & T_INTR_THREAD) == 0 && !do_polled_io) {
    423 			delay(drv_usectohz(scsi_test_busy_delay));
    424 		} else {
    425 			/* we busy wait during cpr_dump or interrupt threads */
    426 			drv_usecwait(scsi_test_busy_delay);
    427 		}
    428 	}
    429 
    430 	if (rc != TRAN_ACCEPT) {
    431 		goto exit;
    432 	} else if (pkt->pkt_reason == CMD_INCOMPLETE && pkt->pkt_state == 0) {
    433 		goto exit;
    434 	} else if (pkt->pkt_reason != CMD_CMPLT) {
    435 		goto exit;
    436 	} else if (((*pkt->pkt_scbp) & STATUS_MASK) == STATUS_BUSY) {
    437 		rval = 0;
    438 	} else {
    439 		rval = 0;
    440 	}
    441 
    442 exit:
    443 	return (rval);
    444 }
    445 
    446 /*
    447  * The implementation of scsi_probe now allows a particular
    448  * HBA to intercept the call, for any post- or pre-processing
    449  * it may need.  The default, if the HBA does not override it,
    450  * is to call scsi_hba_probe(), which retains the old functionality
    451  * intact.
    452  */
    453 int
    454 scsi_probe(struct scsi_device *sd, int (*callback)())
    455 {
    456 	int			ret;
    457 	scsi_hba_tran_t		*tran = sd->sd_address.a_hba_tran;
    458 
    459 	if (scsi_check_ss2_LUN_limit(sd) != 0) {
    460 		/*
    461 		 * caller is trying to probe a strictly-SCSI-2 device
    462 		 * with a LUN that is too large, so do not allow it
    463 		 */
    464 		return (SCSIPROBE_NORESP);	/* skip probing this one */
    465 	}
    466 
    467 	if (tran->tran_tgt_probe != NULL) {
    468 		ret = (*tran->tran_tgt_probe)(sd, callback);
    469 	} else {
    470 		ret = scsi_hba_probe(sd, callback);
    471 	}
    472 
    473 	if (ret == SCSIPROBE_EXISTS) {
    474 		create_inquiry_props(sd);
    475 		/* is this a strictly-SCSI-2 node ?? */
    476 		scsi_establish_LUN_limit(sd);
    477 	}
    478 
    479 	return (ret);
    480 }
    481 /*
    482  * probe scsi device using any available path
    483  *
    484  */
    485 int
    486 scsi_hba_probe(struct scsi_device *sd, int (*callback)())
    487 {
    488 	return (scsi_hba_probe_pi(sd, callback, 0));
    489 }
    490 
    491 /*
    492  * probe scsi device using specific path
    493  *
    494  * scsi_hba_probe_pi does not do any test unit ready's which access the medium
    495  * and could cause busy or not ready conditions.
    496  * scsi_hba_probe_pi does 2 inquiries and a rqsense to clear unit attention
    497  * and to allow sync negotiation to take place
    498  * finally, scsi_hba_probe_pi does one more inquiry which should
    499  * reliably tell us what kind of target we have.
    500  * A scsi-2 compliant target should be able to	return inquiry with 250ms
    501  * and we actually wait more than a second after reset.
    502  */
    503 int
    504 scsi_hba_probe_pi(struct scsi_device *sd, int (*callback)(), int pi)
    505 {
    506 	struct scsi_pkt		*inq_pkt = NULL;
    507 	struct scsi_pkt		*rq_pkt = NULL;
    508 	int			rval = SCSIPROBE_NOMEM;
    509 	struct buf		*inq_bp = NULL;
    510 	struct buf		*rq_bp = NULL;
    511 	int			(*cb_flag)();
    512 	int			pass = 1;
    513 
    514 	if (sd->sd_inq == NULL) {
    515 		sd->sd_inq = (struct scsi_inquiry *)
    516 		    kmem_alloc(SUN_INQSIZE, ((callback == SLEEP_FUNC) ?
    517 		    KM_SLEEP : KM_NOSLEEP));
    518 		if (sd->sd_inq == NULL) {
    519 			goto out;
    520 		}
    521 	}
    522 
    523 	if (callback != SLEEP_FUNC && callback != NULL_FUNC) {
    524 		cb_flag = NULL_FUNC;
    525 	} else {
    526 		cb_flag = callback;
    527 	}
    528 	inq_bp = scsi_alloc_consistent_buf(ROUTE,
    529 	    (struct buf *)NULL, SUN_INQSIZE, B_READ, cb_flag, NULL);
    530 	if (inq_bp == NULL) {
    531 		goto out;
    532 	}
    533 
    534 	inq_pkt = scsi_init_pkt(ROUTE, (struct scsi_pkt *)NULL,
    535 	    inq_bp, CDB_GROUP0, sizeof (struct scsi_arq_status),
    536 	    0, PKT_CONSISTENT, callback, NULL);
    537 	if (inq_pkt == NULL) {
    538 		if (inq_bp->b_error == 0)
    539 			rval = SCSIPROBE_NOMEM_CB;
    540 		goto out;
    541 	}
    542 	ASSERT(inq_bp->b_error == 0);
    543 
    544 	(void) scsi_setup_cdb((union scsi_cdb *)inq_pkt->pkt_cdbp,
    545 	    SCMD_INQUIRY, 0, SUN_INQSIZE, 0);
    546 	inq_pkt->pkt_flags = FLAG_NOINTR|FLAG_NOPARITY;
    547 
    548 	/*
    549 	 * set transport path
    550 	 */
    551 	if (pi && scsi_pkt_allocated_correctly(inq_pkt)) {
    552 		inq_pkt->pkt_path_instance = pi;
    553 		inq_pkt->pkt_flags |= FLAG_PKT_PATH_INSTANCE;
    554 	}
    555 
    556 	/*
    557 	 * the first inquiry will tell us whether a target
    558 	 * responded
    559 	 *
    560 	 * The FILL_SCSI1_LUN below will find "ansi_ver != 1" on first pass
    561 	 * because of bzero initilization. If this assumption turns out to be
    562 	 * incorrect after we have real sd_inq data (for lun0) we will do a
    563 	 * second pass during which FILL_SCSI1_LUN will place lun in CDB.
    564 	 */
    565 	bzero((caddr_t)sd->sd_inq, SUN_INQSIZE);
    566 again:	FILL_SCSI1_LUN(sd, inq_pkt);
    567 
    568 	if (scsi_test(inq_pkt) < 0) {
    569 		if (inq_pkt->pkt_reason == CMD_INCOMPLETE) {
    570 			rval = SCSIPROBE_NORESP;
    571 			goto out;
    572 		} else {
    573 			/*
    574 			 * retry one more time
    575 			 */
    576 			if (scsi_test(inq_pkt) < 0) {
    577 				rval = SCSIPROBE_FAILURE;
    578 				goto out;
    579 			}
    580 		}
    581 	}
    582 
    583 	/*
    584 	 * if we are lucky, this inquiry succeeded
    585 	 */
    586 	if ((inq_pkt->pkt_reason == CMD_CMPLT) &&
    587 	    (((*inq_pkt->pkt_scbp) & STATUS_MASK) == 0)) {
    588 		goto done;
    589 	}
    590 
    591 	/*
    592 	 * the second inquiry, allows the host adapter to negotiate
    593 	 * synchronous transfer period and offset
    594 	 */
    595 	if (scsi_test(inq_pkt) < 0) {
    596 		if (inq_pkt->pkt_reason == CMD_INCOMPLETE)
    597 			rval = SCSIPROBE_NORESP;
    598 		else
    599 			rval = SCSIPROBE_FAILURE;
    600 		goto out;
    601 	}
    602 
    603 	/*
    604 	 * if target is still busy, give up now
    605 	 */
    606 	if (((struct scsi_status *)inq_pkt->pkt_scbp)->sts_busy) {
    607 		rval = SCSIPROBE_BUSY;
    608 		goto out;
    609 	}
    610 
    611 	/*
    612 	 * do a rqsense if there was a check condition and ARQ was not done
    613 	 */
    614 	if ((inq_pkt->pkt_state & STATE_ARQ_DONE) == 0) {
    615 		if (((struct scsi_status *)inq_pkt->pkt_scbp)->sts_chk) {
    616 
    617 			/*
    618 			 * prepare rqsense packet
    619 			 * there is no real need for this because the
    620 			 * check condition should have been cleared by now.
    621 			 */
    622 			rq_bp = scsi_alloc_consistent_buf(ROUTE,
    623 			    (struct buf *)NULL,
    624 			    (uint_t)SENSE_LENGTH, B_READ, cb_flag, NULL);
    625 			if (rq_bp == NULL) {
    626 				goto out;
    627 			}
    628 
    629 			rq_pkt = scsi_init_pkt(ROUTE, (struct scsi_pkt *)NULL,
    630 			    rq_bp, CDB_GROUP0, 1, 0, PKT_CONSISTENT, callback,
    631 			    NULL);
    632 
    633 			if (rq_pkt == NULL) {
    634 				if (rq_bp->b_error == 0)
    635 					rval = SCSIPROBE_NOMEM_CB;
    636 				goto out;
    637 			}
    638 			ASSERT(rq_bp->b_error == 0);
    639 
    640 			(void) scsi_setup_cdb((union scsi_cdb *)rq_pkt->
    641 			    pkt_cdbp, SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0);
    642 			FILL_SCSI1_LUN(sd, rq_pkt);
    643 			rq_pkt->pkt_flags = FLAG_NOINTR|FLAG_NOPARITY;
    644 
    645 			/*
    646 			 * set transport path
    647 			 */
    648 			if (pi && scsi_pkt_allocated_correctly(rq_pkt)) {
    649 				rq_pkt->pkt_path_instance = pi;
    650 				rq_pkt->pkt_flags |= FLAG_PKT_PATH_INSTANCE;
    651 			}
    652 
    653 			/*
    654 			 * The FILL_SCSI1_LUN above will find "inq_ansi != 1"
    655 			 * on first pass, see "again" comment above.
    656 			 *
    657 			 * The controller type is as yet unknown, so we
    658 			 * have to do a throwaway non-extended request sense,
    659 			 * and hope that that clears the check condition for
    660 			 * that unit until we can find out what kind of drive
    661 			 * it is. A non-extended request sense is specified
    662 			 * by stating that the sense block has 0 length,
    663 			 * which is taken to mean that it is four bytes in
    664 			 * length.
    665 			 */
    666 			if (scsi_test(rq_pkt) < 0) {
    667 				rval = SCSIPROBE_FAILURE;
    668 				goto out;
    669 			}
    670 		}
    671 	}
    672 
    673 	/*
    674 	 * At this point, we are guaranteed that something responded
    675 	 * to this scsi bus target id. We don't know yet what
    676 	 * kind of device it is, or even whether there really is
    677 	 * a logical unit attached (as some SCSI target controllers
    678 	 * lie about a unit being ready, e.g., the Emulex MD21).
    679 	 */
    680 
    681 	if (scsi_test(inq_pkt) < 0) {
    682 		rval = SCSIPROBE_FAILURE;
    683 		goto out;
    684 	}
    685 
    686 	if (((struct scsi_status *)inq_pkt->pkt_scbp)->sts_busy) {
    687 		rval = SCSIPROBE_BUSY;
    688 		goto out;
    689 	}
    690 
    691 	/*
    692 	 * Okay we sent the INQUIRY command.
    693 	 *
    694 	 * If enough data was transferred, we count that the
    695 	 * Inquiry command succeeded, else we have to assume
    696 	 * that this is a non-CCS scsi target (or a nonexistent
    697 	 * target/lun).
    698 	 */
    699 
    700 	if (((struct scsi_status *)inq_pkt->pkt_scbp)->sts_chk) {
    701 		/*
    702 		 * try a request sense if we have a pkt, otherwise
    703 		 * just retry the inquiry one more time
    704 		 */
    705 		if (rq_pkt) {
    706 			(void) scsi_test(rq_pkt);
    707 		}
    708 
    709 		/*
    710 		 * retry inquiry
    711 		 */
    712 		if (scsi_test(inq_pkt) < 0) {
    713 			rval = SCSIPROBE_FAILURE;
    714 			goto out;
    715 		}
    716 		if (((struct scsi_status *)inq_pkt->pkt_scbp)->sts_chk) {
    717 			rval = SCSIPROBE_FAILURE;
    718 			goto out;
    719 		}
    720 	}
    721 
    722 done:
    723 	/*
    724 	 * If we got a parity error on receive of inquiry data,
    725 	 * we're just plain out of luck because we told the host
    726 	 * adapter to not watch for parity errors.
    727 	 */
    728 	if ((inq_pkt->pkt_state & STATE_XFERRED_DATA) == 0 ||
    729 	    ((SUN_INQSIZE - inq_pkt->pkt_resid) < SUN_MIN_INQLEN)) {
    730 		rval = SCSIPROBE_NONCCS;
    731 	} else {
    732 		ASSERT(inq_pkt->pkt_resid >= 0);
    733 		bcopy((caddr_t)inq_bp->b_un.b_addr,
    734 		    (caddr_t)sd->sd_inq, (SUN_INQSIZE - inq_pkt->pkt_resid));
    735 		rval = SCSIPROBE_EXISTS;
    736 	}
    737 
    738 out:
    739 	/*
    740 	 * If lun > 0 we need to figure out if this is a scsi-1 device where
    741 	 * the "real" lun needs to be embedded into the cdb.
    742 	 */
    743 	if ((rval == SCSIPROBE_EXISTS) && (pass == 1) &&
    744 	    (sd->sd_address.a_lun > 0) && (sd->sd_inq->inq_ansi == 0x1)) {
    745 		pass++;
    746 		if (sd->sd_address.a_lun <= 7)
    747 			goto again;
    748 
    749 		/*
    750 		 * invalid lun for scsi-1,
    751 		 * return probe failure.
    752 		 */
    753 		rval = SCSIPROBE_FAILURE;
    754 	}
    755 
    756 	if (rq_pkt) {
    757 		scsi_destroy_pkt(rq_pkt);
    758 	}
    759 	if (inq_pkt) {
    760 		scsi_destroy_pkt(inq_pkt);
    761 	}
    762 	if (rq_bp) {
    763 		scsi_free_consistent_buf(rq_bp);
    764 	}
    765 	if (inq_bp) {
    766 		scsi_free_consistent_buf(inq_bp);
    767 	}
    768 	return (rval);
    769 }
    770 
    771 /*
    772  * Convert from a scsi_device structure pointer to a scsi_hba_tran structure
    773  * pointer. The correct way to do this is
    774  *
    775  *	#define	DEVP_TO_TRAN(sd)	((sd)->sd_address.a_hba_tran)
    776  *
    777  * however we have some consumers that place their own vector in a_hba_tran. To
    778  * avoid problems, we implement this using the sd_tran_safe. See
    779  * scsi_hba_initchild for more details.
    780  */
    781 #define	DEVP_TO_TRAN(sd)	((sd)->sd_tran_safe)
    782 
    783 /*
    784  * Function, callable from SCSA framework, to get 'human' readable REPORTDEV
    785  * addressing information from scsi_device properties.
    786  */
    787 int
    788 scsi_ua_get_reportdev(struct scsi_device *sd, char *ra, int len)
    789 {
    790 	/* use deprecated tran_get_bus_addr interface if it is defined */
    791 	/* NOTE: tran_get_bus_addr is a poor name choice for interface */
    792 	if (DEVP_TO_TRAN(sd)->tran_get_bus_addr)
    793 		return ((*DEVP_TO_TRAN(sd)->tran_get_bus_addr)(sd, ra, len));
    794 	return (scsi_hba_ua_get_reportdev(sd, ra, len));
    795 }
    796 
    797 /*
    798  * Function, callable from HBA driver's tran_get_bus_addr(9E) implementation,
    799  * to get standard form of human readable REPORTDEV addressing information
    800  * from scsi_device properties.
    801  */
    802 int
    803 scsi_hba_ua_get_reportdev(struct scsi_device *sd, char *ra, int len)
    804 {
    805 	int		tgt, lun, sfunc;
    806 	char		*tgt_port;
    807 	scsi_lun64_t	lun64;
    808 
    809 	/* get device unit-address properties */
    810 	tgt = scsi_device_prop_get_int(sd, SCSI_DEVICE_PROP_PATH,
    811 	    SCSI_ADDR_PROP_TARGET, -1);
    812 	if (scsi_device_prop_lookup_string(sd, SCSI_DEVICE_PROP_PATH,
    813 	    SCSI_ADDR_PROP_TARGET_PORT, &tgt_port) != DDI_PROP_SUCCESS)
    814 		tgt_port = NULL;
    815 	if ((tgt == -1) && (tgt_port == NULL))
    816 		return (0);		/* no target */
    817 
    818 	lun = scsi_device_prop_get_int(sd, SCSI_DEVICE_PROP_PATH,
    819 	    SCSI_ADDR_PROP_LUN, 0);
    820 	lun64 = scsi_device_prop_get_int64(sd, SCSI_DEVICE_PROP_PATH,
    821 	    SCSI_ADDR_PROP_LUN64, lun);
    822 	sfunc = scsi_device_prop_get_int(sd, SCSI_DEVICE_PROP_PATH,
    823 	    SCSI_ADDR_PROP_SFUNC, -1);
    824 
    825 	/*
    826 	 * XXX should the default be to print this in decimal for
    827 	 * "human readable" form, so it matches conf files?
    828 	 */
    829 	if (tgt_port) {
    830 		if (sfunc == -1)
    831 			(void) snprintf(ra, len,
    832 			    "%s %s lun %" PRIx64,
    833 			    SCSI_ADDR_PROP_TARGET_PORT, tgt_port, lun64);
    834 		else
    835 			(void) snprintf(ra, len,
    836 			    "%s %s lun %" PRIx64 " sfunc %x",
    837 			    SCSI_ADDR_PROP_TARGET_PORT, tgt_port, lun64, sfunc);
    838 		scsi_device_prop_free(sd, SCSI_DEVICE_PROP_PATH, tgt_port);
    839 	} else {
    840 		if (sfunc == -1)
    841 			(void) snprintf(ra, len,
    842 			    "%s %x lun %" PRIx64,
    843 			    SCSI_ADDR_PROP_TARGET, tgt, lun64);
    844 		else
    845 			(void) snprintf(ra, len,
    846 			    "%s %x lun %" PRIx64 " sfunc %x",
    847 			    SCSI_ADDR_PROP_TARGET, tgt, lun64, sfunc);
    848 	}
    849 
    850 	return (1);
    851 }
    852 
    853 /*
    854  * scsi_ua_get: using properties, return "unit-address" string.
    855  * Called by SCSA framework, may call HBAs tran function.
    856  */
    857 int
    858 scsi_ua_get(struct scsi_device *sd, char *ua, int len)
    859 {
    860 	char		*eua;
    861 
    862 	/* See if we already have established the unit-address. */
    863 	if ((eua = scsi_device_unit_address(sd)) != NULL) {
    864 		(void) strlcpy(ua, eua, len);
    865 		return (1);
    866 	}
    867 
    868 	/* Use deprecated tran_get_name interface if it is defined. */
    869 	/* NOTE: tran_get_name is a poor name choice for interface */
    870 	if (DEVP_TO_TRAN(sd)->tran_get_name)
    871 		return ((*DEVP_TO_TRAN(sd)->tran_get_name)(sd, ua, len));
    872 
    873 	/* Use generic property implementation */
    874 	return (scsi_hba_ua_get(sd, ua, len));
    875 }
    876 
    877 /*
    878  * scsi_hba_ua_get: using properties, return "unit-address" string.
    879  * This function may be called from an HBAs tran function.
    880  *
    881  * Function to get "unit-address" in "name@unit-address" /devices path
    882  * component form from the scsi_device unit-address properties on a node.
    883  *
    884  * NOTE: This function works in conjunction with scsi_hba_ua_set().
    885  */
    886 int
    887 scsi_hba_ua_get(struct scsi_device *sd, char *ua, int len)
    888 {
    889 	int		tgt, lun, sfunc;
    890 	char		*tgt_port;
    891 	scsi_lun64_t	lun64;
    892 
    893 	/* get device unit-address properties */
    894 	tgt = scsi_device_prop_get_int(sd, SCSI_DEVICE_PROP_PATH,
    895 	    SCSI_ADDR_PROP_TARGET, -1);
    896 	if (scsi_device_prop_lookup_string(sd, SCSI_DEVICE_PROP_PATH,
    897 	    SCSI_ADDR_PROP_TARGET_PORT, &tgt_port) != DDI_PROP_SUCCESS)
    898 		tgt_port = NULL;
    899 	if ((tgt == -1) && (tgt_port == NULL))
    900 		return (0);		/* no target */
    901 
    902 	lun = scsi_device_prop_get_int(sd, SCSI_DEVICE_PROP_PATH,
    903 	    SCSI_ADDR_PROP_LUN, 0);
    904 	lun64 = scsi_device_prop_get_int64(sd, SCSI_DEVICE_PROP_PATH,
    905 	    SCSI_ADDR_PROP_LUN64, lun);
    906 	sfunc = scsi_device_prop_get_int(sd, SCSI_DEVICE_PROP_PATH,
    907 	    SCSI_ADDR_PROP_SFUNC, -1);
    908 	if (tgt_port) {
    909 		if (sfunc == -1)
    910 			(void) snprintf(ua, len, "%s,%" PRIx64,
    911 			    tgt_port, lun64);
    912 		else
    913 			(void) snprintf(ua, len, "%s,%" PRIx64 ",%x",
    914 			    tgt_port, lun64, sfunc);
    915 		scsi_device_prop_free(sd, SCSI_DEVICE_PROP_PATH, tgt_port);
    916 	} else {
    917 		if (sfunc == -1)
    918 			(void) snprintf(ua, len, "%x,%" PRIx64, tgt, lun64);
    919 		else
    920 			(void) snprintf(ua, len, "%x,%" PRIx64 ",%x",
    921 			    tgt, lun64, sfunc);
    922 	}
    923 	return (1);
    924 }
    925 
    926 static void
    927 create_inquiry_props(struct scsi_device *sd)
    928 {
    929 	struct scsi_inquiry *inq = sd->sd_inq;
    930 
    931 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
    932 	    INQUIRY_DEVICE_TYPE, (int)inq->inq_dtype);
    933 
    934 	/*
    935 	 * Create the following properties:
    936 	 *
    937 	 * inquiry-vendor-id	Vendor id (INQUIRY data bytes 8-15)
    938 	 * inquiry-product-id	Product id (INQUIRY data bytes 16-31)
    939 	 * inquiry-revision-id	Product Rev level (INQUIRY data bytes 32-35)
    940 	 *
    941 	 * NOTE: We don't support creation of these properties for scsi-1
    942 	 * devices (as the vid, pid and revision were not defined) and we
    943 	 * don't create the property if they are of zero length when
    944 	 * stripped of Nulls and spaces.
    945 	 *
    946 	 * NOTE: The first definition of these properties sticks. This gives
    947 	 * a transport the ability to provide a higher-quality definition
    948 	 * than the standard SCSI INQUIRY data.
    949 	 */
    950 	if (inq->inq_ansi != 1) {
    951 		if (ddi_prop_exists(DDI_DEV_T_NONE, sd->sd_dev,
    952 		    DDI_PROP_TYPE_STRING, INQUIRY_VENDOR_ID) == 0)
    953 			(void) scsi_device_prop_update_inqstring(sd,
    954 			    INQUIRY_VENDOR_ID,
    955 			    inq->inq_vid, sizeof (inq->inq_vid));
    956 
    957 		if (ddi_prop_exists(DDI_DEV_T_NONE, sd->sd_dev,
    958 		    DDI_PROP_TYPE_STRING, INQUIRY_PRODUCT_ID) == 0)
    959 			(void) scsi_device_prop_update_inqstring(sd,
    960 			    INQUIRY_PRODUCT_ID,
    961 			    inq->inq_pid, sizeof (inq->inq_pid));
    962 
    963 		if (ddi_prop_exists(DDI_DEV_T_NONE, sd->sd_dev,
    964 		    DDI_PROP_TYPE_STRING, INQUIRY_REVISION_ID) == 0)
    965 			(void) scsi_device_prop_update_inqstring(sd,
    966 			    INQUIRY_REVISION_ID,
    967 			    inq->inq_revision, sizeof (inq->inq_revision));
    968 	}
    969 }
    970 
    971 /*
    972  * Create 'inquiry' string properties.  An 'inquiry' string gets special
    973  * treatment to trim trailing blanks (etc) and ensure null termination.
    974  */
    975 int
    976 scsi_device_prop_update_inqstring(struct scsi_device *sd,
    977     char *name, char *data, size_t len)
    978 {
    979 	int	ilen;
    980 	char	*data_string;
    981 	int	rv;
    982 
    983 	ilen = scsi_ascii_inquiry_len(data, len);
    984 	ASSERT(ilen <= (int)len);
    985 	if (ilen <= 0)
    986 		return (DDI_PROP_INVAL_ARG);
    987 
    988 	/* ensure null termination */
    989 	data_string = kmem_zalloc(ilen + 1, KM_SLEEP);
    990 	bcopy(data, data_string, ilen);
    991 	rv = ndi_prop_update_string(DDI_DEV_T_NONE,
    992 	    sd->sd_dev, name, data_string);
    993 	kmem_free(data_string, ilen + 1);
    994 	return (rv);
    995 }
    996 
    997 /*
    998  * Interfaces associated with SCSI_HBA_ADDR_COMPLEX
    999  * per-scsi_device HBA private data support.
   1000  */
   1001 struct scsi_device *
   1002 scsi_address_device(struct scsi_address *sa)
   1003 {
   1004 	ASSERT(sa->a_hba_tran->tran_hba_flags & SCSI_HBA_ADDR_COMPLEX);
   1005 	return (sa->a.a_sd);
   1006 }
   1007 
   1008 void
   1009 scsi_device_hba_private_set(struct scsi_device *sd, void *data)
   1010 {
   1011 	ASSERT(sd->sd_address.a_hba_tran->tran_hba_flags &
   1012 	    SCSI_HBA_ADDR_COMPLEX);
   1013 	sd->sd_hba_private = data;
   1014 }
   1015 
   1016 void *
   1017 scsi_device_hba_private_get(struct scsi_device *sd)
   1018 {
   1019 	ASSERT(sd->sd_address.a_hba_tran->tran_hba_flags &
   1020 	    SCSI_HBA_ADDR_COMPLEX);
   1021 	return (sd->sd_hba_private);
   1022 }
   1023 
   1024 /*
   1025  * This routine is called from the start of scsi_probe() if a tgt/LUN to be
   1026  * probed *may* be a request to probe a strictly SCSI-2 target (with respect
   1027  * to LUNs) -- and this probe may be for a LUN number greater than 7,
   1028  * which can cause a hardware hang
   1029  *
   1030  * return 0 if the probe can proceed,
   1031  * else return 1, meaning do *NOT* probe this target/LUN
   1032  */
   1033 static int
   1034 scsi_check_ss2_LUN_limit(struct scsi_device *sd)
   1035 {
   1036 	struct scsi_address	*ap = &(sd->sd_address);
   1037 	dev_info_t		*pdevi =
   1038 	    (dev_info_t *)DEVI(sd->sd_dev)->devi_parent;
   1039 	int			ret_val = 0;	/* default return value */
   1040 	uchar_t			*tgt_list;
   1041 	uint_t			tgt_nelements;
   1042 	int			i;
   1043 
   1044 
   1045 	/*
   1046 	 * check for what *might* be a problem probe, only we don't
   1047 	 * know yet what's really at the destination target/LUN
   1048 	 */
   1049 	if ((ap->a_target >= NTARGETS_WIDE) ||
   1050 	    (ap->a_lun < NLUNS_PER_TARGET)) {
   1051 		return (0);		/* okay to probe this target */
   1052 	}
   1053 
   1054 	/*
   1055 	 * this *might* be a problematic probe, so look to see
   1056 	 * if the inquiry data matches
   1057 	 */
   1058 	SCSI_PROBE_DEBUG2(1, "SCSA pre-probe: checking tgt.LUN=%d.%d\n",
   1059 	    ap->a_target, ap->a_lun);
   1060 	SCSI_PROBE_DEBUG1(2,
   1061 	    "SCSA pre-probe: scanning parent node name: %s ...\n",
   1062 	    ddi_node_name(pdevi));
   1063 
   1064 	/*
   1065 	 * look for a special property of our parent node that lists
   1066 	 * the targets under it for which we do *NOT* want to probe
   1067 	 * if LUN>7 -- if the property is found, look to see if our
   1068 	 * target ID is on that list
   1069 	 */
   1070 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, pdevi,
   1071 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SS2_LUN0_TGT_LIST_PROP,
   1072 	    &tgt_list, &tgt_nelements) != DDI_PROP_SUCCESS) {
   1073 		/*
   1074 		 * no list, so it must be okay to probe this target.LUN
   1075 		 */
   1076 		SCSI_PROBE_DEBUG0(3,
   1077 		    "SCSA pre-probe: NO parent prop found\n");
   1078 	} else {
   1079 		for (i = 0; i < tgt_nelements; i++) {
   1080 			if (tgt_list[i] == ap->a_target) {
   1081 				/*
   1082 				 * we found a match, which means we do *NOT*
   1083 				 * want to probe the specified target.LUN
   1084 				 */
   1085 				ret_val = 1;
   1086 				break;
   1087 			}
   1088 		}
   1089 		ddi_prop_free(tgt_list);
   1090 #ifdef	DEBUG
   1091 		if (ret_val == 1) {
   1092 			SCSI_PROBE_DEBUG2(1,
   1093 			    "SCSA pre-probe: marker node FOUND for "
   1094 			    "tgt.LUN=%d.%d, so SKIPPING it\n",
   1095 			    ap->a_target, ap->a_lun);
   1096 		} else {
   1097 			SCSI_PROBE_DEBUG0(2,
   1098 			    "SCSA pre-probe: NO marker node found"
   1099 			    " -- OK to probe\n");
   1100 		}
   1101 #endif
   1102 	}
   1103 	return (ret_val);
   1104 }
   1105 
   1106 
   1107 /*
   1108  * this routine is called from near the end of scsi_probe(),
   1109  * to see if the just-probed node is on our list of strictly-SCSI-2 nodes,
   1110  * and if it is we mark our parent node with this information
   1111  */
   1112 static void
   1113 scsi_establish_LUN_limit(struct scsi_device *sd)
   1114 {
   1115 	struct scsi_address	*ap = &(sd->sd_address);
   1116 	struct scsi_inquiry	*inq = sd->sd_inq;
   1117 	dev_info_t		*devi = sd->sd_dev;
   1118 	char			*vid = NULL;
   1119 	char			*pid = NULL;
   1120 	char			*rev = NULL;
   1121 	int			i;
   1122 	const ss2_lun0_info_t	*p;
   1123 	int			bad_target_found = 0;
   1124 
   1125 
   1126 	/*
   1127 	 * if this inquiry data shows that we have a strictly-SCSI-2 device
   1128 	 * at LUN 0, then add it to our list of strictly-SCSI-2 devices,
   1129 	 * so that we can avoid probes where LUN>7 on this device later
   1130 	 */
   1131 	if ((ap->a_lun != 0) ||
   1132 	    (ap->a_target >= NTARGETS_WIDE) ||
   1133 	    (inq->inq_dtype != DTYPE_PROCESSOR) ||
   1134 	    (inq->inq_ansi != 2)) {
   1135 		/*
   1136 		 * this can't possibly be a node we want to look at, since
   1137 		 * either LUN is greater than 0, target is greater than or
   1138 		 * equal to 16, device type
   1139 		 * is not processor, or SCSI level is not SCSI-2,
   1140 		 * so don't bother checking for a strictly SCSI-2
   1141 		 * (only 8 LUN) target
   1142 		 */
   1143 		return;				/* don't care */
   1144 	}
   1145 
   1146 	SCSI_PROBE_DEBUG2(1, "SCSA post-probe: LUN limit on tgt.LUN=%d.%d, "
   1147 	    "SCSI-2 PROCESSOR?\n", ap->a_target, ap->a_lun);
   1148 
   1149 	ASSERT(devi != NULL);
   1150 
   1151 	/*
   1152 	 * we have a node that has been probed that is: LUN=0, target<16,
   1153 	 * PROCESSOR-type SCSI target, and at the SCSI-2 level, so
   1154 	 * check INQ properties to see if it's in our list of strictly
   1155 	 * SCSI-2 targets
   1156 	 *
   1157 	 * first we have to get the VID/PID/REV INQUIRY properties for
   1158 	 * comparison
   1159 	 */
   1160 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi,
   1161 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
   1162 	    INQUIRY_VENDOR_ID, &vid) != DDI_PROP_SUCCESS) {
   1163 		SCSI_PROBE_DEBUG1(2, "SCSA post-probe: prop \"%s\" missing\n",
   1164 		    INQUIRY_VENDOR_ID);
   1165 		goto dun;
   1166 	}
   1167 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi,
   1168 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
   1169 	    INQUIRY_PRODUCT_ID, &pid) != DDI_PROP_SUCCESS) {
   1170 		SCSI_PROBE_DEBUG1(2, "SCSA post-probe: prop \"%s\" missing\n",
   1171 		    INQUIRY_PRODUCT_ID);
   1172 		goto dun;
   1173 	}
   1174 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi,
   1175 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
   1176 	    INQUIRY_REVISION_ID, &rev) != DDI_PROP_SUCCESS) {
   1177 		SCSI_PROBE_DEBUG1(2, "SCSA post-probe: prop \"%s\" missing\n",
   1178 		    INQUIRY_REVISION_ID);
   1179 		goto dun;
   1180 	}
   1181 
   1182 	SCSI_PROBE_DEBUG3(3, "SCSA post-probe: looking for vid/pid/rev = "
   1183 	    "\"%s\"/\"%s\"/\"%s\"\n", vid, pid, rev);
   1184 
   1185 	/*
   1186 	 * now that we have the INQUIRY properties from the device node,
   1187 	 * compare them with our known offenders
   1188 	 *
   1189 	 * Note: comparison is *CASE* *SENSITIVE*
   1190 	 */
   1191 	for (i = 0; i < scsi_probe_strict_s2_size; i++) {
   1192 		p = &scsi_probe_strict_s2_list[i];
   1193 
   1194 		if ((strcmp(p->sli_vid, vid) == 0) &&
   1195 		    (strcmp(p->sli_pid, pid) == 0) &&
   1196 		    (strcmp(p->sli_rev, rev) == 0)) {
   1197 			/*
   1198 			 * we found a match -- do NOT want to probe this one
   1199 			 */
   1200 			SCSI_PROBE_DEBUG3(1,
   1201 			    "SCSA post-probe: recording strict SCSI-2 node "
   1202 			    "vid/pid/rev = \"%s\"/\"%s\"/\"%s\"\n",
   1203 			    vid, pid, rev);
   1204 
   1205 			/*
   1206 			 * set/update private parent-node property,
   1207 			 * so we can find out about this node later
   1208 			 */
   1209 			bad_target_found = 1;
   1210 			break;
   1211 		}
   1212 	}
   1213 
   1214 	/*
   1215 	 * either add remove target number from parent property
   1216 	 */
   1217 	scsi_update_parent_ss2_prop(devi, ap->a_target, bad_target_found);
   1218 
   1219 dun:
   1220 	if (vid != NULL) {
   1221 		ddi_prop_free(vid);
   1222 	}
   1223 	if (pid != NULL) {
   1224 		ddi_prop_free(pid);
   1225 	}
   1226 	if (rev != NULL) {
   1227 		ddi_prop_free(rev);
   1228 	}
   1229 }
   1230 
   1231 
   1232 /*
   1233  * update the parent node to add in the supplied tgt number to the target
   1234  * list property already present (if any)
   1235  *
   1236  * since the target list can never be longer than 16, and each target
   1237  * number is also small, we can save having to alloc memory by putting
   1238  * a 16-byte array on the stack and using it for property memory
   1239  *
   1240  * if "add_tgt" is set then add the target to the parent's property, else
   1241  * remove it (if present)
   1242  */
   1243 static void
   1244 scsi_update_parent_ss2_prop(dev_info_t *devi, int tgt, int add_tgt)
   1245 {
   1246 	dev_info_t	*pdevi = (dev_info_t *)DEVI(devi)->devi_parent;
   1247 	uchar_t		*tgt_list;
   1248 	uint_t		nelements;
   1249 	uint_t		new_nelements;
   1250 	int		i;
   1251 	int		update_result;
   1252 	uchar_t		new_tgt_list[NTARGETS_WIDE];
   1253 
   1254 
   1255 	ASSERT(pdevi != NULL);
   1256 
   1257 	SCSI_PROBE_DEBUG3(3,
   1258 	    "SCSA post-probe: updating parent=%s property to %s tgt=%d\n",
   1259 	    ddi_node_name(pdevi), add_tgt ? "add" : "remove", tgt);
   1260 
   1261 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, pdevi,
   1262 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
   1263 	    SS2_LUN0_TGT_LIST_PROP, &tgt_list, &nelements) ==
   1264 	    DDI_PROP_SUCCESS) {
   1265 
   1266 		if (add_tgt) {
   1267 			/*
   1268 			 * we found an existing property -- we might need
   1269 			 *	to add to it
   1270 			 */
   1271 			for (i = 0; i < nelements; i++) {
   1272 				if (tgt_list[i] == tgt) {
   1273 					/* target already in list */
   1274 					SCSI_PROBE_DEBUG1(2, "SCSA post-probe:"
   1275 					    " tgt %d already listed\n", tgt);
   1276 					ddi_prop_free(tgt_list);
   1277 					return;
   1278 				}
   1279 			}
   1280 
   1281 			/*
   1282 			 * need to append our target number to end of list
   1283 			 *	(no need sorting list, as it's so short)
   1284 			 */
   1285 
   1286 			/*
   1287 			 * will this new entry fit ?? -- it should, since
   1288 			 *	the array is 16-wide and only keep track of
   1289 			 *	16 targets, but check just in case
   1290 			 */
   1291 			new_nelements = nelements + 1;
   1292 			if (new_nelements >= NTARGETS_WIDE) {
   1293 				SCSI_PROBE_DEBUG0(1, "SCSA post-probe: "
   1294 				    "internal error: no room "
   1295 				    "for more targets?\n");
   1296 				ddi_prop_free(tgt_list);
   1297 				return;
   1298 			}
   1299 
   1300 			/* copy existing list then add our tgt number to end */
   1301 			bcopy((void *)tgt_list, (void *)new_tgt_list,
   1302 			    sizeof (uchar_t) * nelements);
   1303 			new_tgt_list[new_nelements - 1] = (uchar_t)tgt;
   1304 		} else {
   1305 			/*
   1306 			 * we need to remove our target number from the list,
   1307 			 *	so copy all of the other target numbers,
   1308 			 *	skipping ours
   1309 			 */
   1310 			int	tgt_removed = 0;
   1311 
   1312 			new_nelements = 0;
   1313 			for (i = 0; i < nelements; i++) {
   1314 				if (tgt_list[i] != tgt) {
   1315 					new_tgt_list[new_nelements++] =
   1316 					    tgt_list[i];
   1317 				} else {
   1318 					/* skip this target */
   1319 					tgt_removed++;
   1320 				}
   1321 			}
   1322 
   1323 			if (!tgt_removed) {
   1324 				SCSI_PROBE_DEBUG1(2, "SCSA post-probe:"
   1325 				    " no need to remove tgt %d\n", tgt);
   1326 				ddi_prop_free(tgt_list);
   1327 				return;
   1328 			}
   1329 		}
   1330 
   1331 		update_result = ddi_prop_update_byte_array(DDI_DEV_T_NONE,
   1332 		    pdevi, SS2_LUN0_TGT_LIST_PROP, new_tgt_list,
   1333 		    new_nelements);
   1334 
   1335 		ddi_prop_free(tgt_list);
   1336 	} else {
   1337 		/*
   1338 		 * no property yet
   1339 		 */
   1340 		if (add_tgt) {
   1341 			/*
   1342 			 * create a property with just our tgt
   1343 			 */
   1344 			new_tgt_list[0] = (uchar_t)tgt;
   1345 			new_nelements = 1;	/* just one element */
   1346 
   1347 			update_result = ddi_prop_update_byte_array(
   1348 			    DDI_DEV_T_NONE, pdevi, SS2_LUN0_TGT_LIST_PROP,
   1349 			    new_tgt_list, new_nelements);
   1350 		} else {
   1351 			/*
   1352 			 * no list so no need to remove tgt from that list
   1353 			 */
   1354 			return;
   1355 		}
   1356 	}
   1357 
   1358 #ifdef	DEBUG
   1359 	/*
   1360 	 * if we get here we have tried to add/update properties
   1361 	 */
   1362 	if (update_result != DDI_PROP_SUCCESS) {
   1363 		SCSI_PROBE_DEBUG2(1, "SCSA post-probe: can't update parent "
   1364 		    "property with tgt=%d (%d)\n", tgt, update_result);
   1365 	} else {
   1366 		if (add_tgt) {
   1367 			SCSI_PROBE_DEBUG3(2,
   1368 			    "SCSA post-probe: added tgt=%d to parent "
   1369 			    "prop=\"%s\" (now %d entries)\n",
   1370 			    tgt, SS2_LUN0_TGT_LIST_PROP, new_nelements);
   1371 		} else {
   1372 			SCSI_PROBE_DEBUG3(2,
   1373 			    "SCSA post-probe: removed tgt=%d from parent "
   1374 			    "prop=\"%s\" (now %d entries)\n",
   1375 			    tgt, SS2_LUN0_TGT_LIST_PROP, new_nelements);
   1376 		}
   1377 	}
   1378 #endif
   1379 }
   1380 
   1381 
   1382 /* XXX BEGIN: find a better place for this: inquiry.h? */
   1383 /*
   1384  * Definitions used by device id registration routines
   1385  */
   1386 #define	VPD_HEAD_OFFSET		3	/* size of head for vpd page */
   1387 #define	VPD_PAGE_LENGTH		3	/* offset for pge length data */
   1388 #define	VPD_MODE_PAGE		1	/* offset into vpd pg for "page code" */
   1389 
   1390 /* size for devid inquiries */
   1391 #define	MAX_INQUIRY_SIZE	0xF0
   1392 #define	MAX_INQUIRY_SIZE_EVPD	0xFF	/* XXX why is this longer */
   1393 /* XXX END: find a better place for these */
   1394 
   1395 
   1396 /*
   1397  * Decorate devinfo node with identity properties using information obtained
   1398  * from device. These properties are used by device enumeration code to derive
   1399  * the devid, and guid for the device. These properties are also used to
   1400  * determine if a device should be enumerated under the physical HBA (PHCI) or
   1401  * the virtual HBA (VHCI, for mpxio support).
   1402  *
   1403  * Return zero on success. If commands that should succeed fail or allocations
   1404  * fail then return failure (non-zero). It is possible for this function to
   1405  * return success and not have decorated the node with any additional identity
   1406  * information if the device correctly responds indicating that they are not
   1407  * supported.  When failure occurs the caller should consider not making the
   1408  * device accessible.
   1409  */
   1410 int
   1411 scsi_device_identity(struct scsi_device *sd, int (*callback)())
   1412 {
   1413 	dev_info_t	*devi		= sd->sd_dev;
   1414 	uchar_t		*inq80		= NULL;
   1415 	uchar_t		*inq83		= NULL;
   1416 	int		rval;
   1417 	size_t		len;
   1418 	int		pg80, pg83;
   1419 
   1420 	/* find out what pages are supported by device */
   1421 	if (check_vpd_page_support8083(sd, callback, &pg80, &pg83) == -1)
   1422 		return (-1);
   1423 
   1424 	/* if available, collect page 80 data and add as property */
   1425 	if (pg80) {
   1426 		inq80 = kmem_zalloc(MAX_INQUIRY_SIZE,
   1427 		    ((callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP));
   1428 		if (inq80 == NULL) {
   1429 			rval = -1;
   1430 			goto out;
   1431 		}
   1432 
   1433 		rval = send_scsi_INQUIRY(sd, callback, inq80,
   1434 		    MAX_INQUIRY_SIZE, 0x01, 0x80, &len);
   1435 		if (rval)
   1436 			goto out;		/* should have worked */
   1437 
   1438 		if (len && (ndi_prop_update_byte_array(DDI_DEV_T_NONE, devi,
   1439 		    "inquiry-page-80", inq80, len) != DDI_PROP_SUCCESS)) {
   1440 			cmn_err(CE_WARN, "scsi_device_identity: "
   1441 			    "failed to add page80 prop");
   1442 			rval = -1;
   1443 			goto out;
   1444 		}
   1445 	}
   1446 
   1447 	/* if available, collect page 83 data and add as property */
   1448 	if (pg83) {
   1449 		inq83 = kmem_zalloc(MAX_INQUIRY_SIZE,
   1450 		    ((callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP));
   1451 		if (inq83 == NULL) {
   1452 			rval = -1;
   1453 			goto out;
   1454 		}
   1455 
   1456 		rval = send_scsi_INQUIRY(sd, callback, inq83,
   1457 		    MAX_INQUIRY_SIZE, 0x01, 0x83, &len);
   1458 		if (rval)
   1459 			goto out;		/* should have worked */
   1460 
   1461 		if (len && (ndi_prop_update_byte_array(DDI_DEV_T_NONE, devi,
   1462 		    "inquiry-page-83", inq83, len) != DDI_PROP_SUCCESS)) {
   1463 			cmn_err(CE_WARN, "scsi_device_identity: "
   1464 			    "failed to add page83 prop");
   1465 			rval = -1;
   1466 			goto out;
   1467 		}
   1468 	}
   1469 
   1470 	/* Commands worked, identity information that exists has been added. */
   1471 	rval = 0;
   1472 
   1473 	/* clean up resources */
   1474 out:	if (inq80 != NULL)
   1475 		kmem_free(inq80, MAX_INQUIRY_SIZE);
   1476 	if (inq83 != NULL)
   1477 		kmem_free(inq83, MAX_INQUIRY_SIZE);
   1478 
   1479 	return (rval);
   1480 }
   1481 
   1482 /*
   1483  * Send an INQUIRY command with the EVPD bit set and a page code of 0x00 to
   1484  * the device, returning zero on success. Returned INQUIRY data is used to
   1485  * determine which vital product pages are supported. The device idenity
   1486  * information we are looking for is in pages 0x83 and/or 0x80. If the device
   1487  * fails the EVPD inquiry then no pages are supported but the call succeeds.
   1488  * Return -1 (failure) if there were memory allocation failures or if a
   1489  * command faild that should have worked.
   1490  */
   1491 static int
   1492 check_vpd_page_support8083(struct scsi_device *sd, int (*callback)(),
   1493 	int *ppg80, int *ppg83)
   1494 {
   1495 	uchar_t *page_list;
   1496 	int	counter;
   1497 	int	rval;
   1498 
   1499 	/* pages are not supported */
   1500 	*ppg80 = 0;
   1501 	*ppg83 = 0;
   1502 
   1503 	/*
   1504 	 * We'll set the page length to the maximum to save figuring it out
   1505 	 * with an additional call.
   1506 	 */
   1507 	page_list =  kmem_zalloc(MAX_INQUIRY_SIZE_EVPD,
   1508 	    ((callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP));
   1509 	if (page_list == NULL)
   1510 		return (-1);		/* memory allocation problem */
   1511 
   1512 	/* issue page 0 (Supported VPD Pages) INQUIRY with evpd set */
   1513 	rval = send_scsi_INQUIRY(sd, callback,
   1514 	    page_list, MAX_INQUIRY_SIZE_EVPD, 1, 0, NULL);
   1515 
   1516 	/*
   1517 	 * Now we must validate that the device accepted the command (some
   1518 	 * devices do not support it) and if the idenity pages we are
   1519 	 * interested in are supported.
   1520 	 */
   1521 	if ((rval == 0) &&
   1522 	    (page_list[VPD_MODE_PAGE] == 0x00)) {
   1523 		/* Loop to find one of the 2 pages we need */
   1524 		counter = 4;  /* Supported pages start at byte 4, with 0x00 */
   1525 
   1526 		/*
   1527 		 * Pages are returned in ascending order, and 0x83 is the
   1528 		 * last page we are hoping to find.
   1529 		 */
   1530 		while ((page_list[counter] <= 0x83) &&
   1531 		    (counter <= (page_list[VPD_PAGE_LENGTH] +
   1532 		    VPD_HEAD_OFFSET))) {
   1533 			/*
   1534 			 * Add 3 because page_list[3] is the number of
   1535 			 * pages minus 3
   1536 			 */
   1537 
   1538 			switch (page_list[counter]) {
   1539 			case 0x80:
   1540 				*ppg80 = 1;
   1541 				break;
   1542 			case 0x83:
   1543 				*ppg83 = 1;
   1544 				break;
   1545 			}
   1546 			counter++;
   1547 		}
   1548 	}
   1549 
   1550 	kmem_free(page_list, MAX_INQUIRY_SIZE_EVPD);
   1551 	return (0);
   1552 }
   1553 
   1554 /*
   1555  * Send INQUIRY command with specified EVPD and page code.  Return
   1556  * zero on success.  On success, the amount of data transferred
   1557  * is returned in *lenp.
   1558  */
   1559 static int
   1560 send_scsi_INQUIRY(struct scsi_device *sd, int (*callback)(),
   1561     uchar_t *bufaddr, size_t buflen,
   1562     uchar_t evpd, uchar_t page_code, size_t *lenp)
   1563 {
   1564 	int		(*cb_flag)();
   1565 	struct buf	*inq_bp;
   1566 	struct scsi_pkt *inq_pkt = NULL;
   1567 	int		rval = -1;
   1568 
   1569 	if (lenp)
   1570 		*lenp = 0;
   1571 	if (callback != SLEEP_FUNC && callback != NULL_FUNC)
   1572 		cb_flag = NULL_FUNC;
   1573 	else
   1574 		cb_flag = callback;
   1575 	inq_bp = scsi_alloc_consistent_buf(ROUTE,
   1576 	    (struct buf *)NULL, buflen, B_READ, cb_flag, NULL);
   1577 	if (inq_bp == NULL)
   1578 		goto out;		/* memory allocation problem */
   1579 
   1580 	inq_pkt = scsi_init_pkt(ROUTE, (struct scsi_pkt *)NULL,
   1581 	    inq_bp, CDB_GROUP0, sizeof (struct scsi_arq_status),
   1582 	    0, PKT_CONSISTENT, callback, NULL);
   1583 	if (inq_pkt == NULL)
   1584 		goto out;		/* memory allocation problem */
   1585 
   1586 	ASSERT(inq_bp->b_error == 0);
   1587 
   1588 	/* form INQUIRY cdb with specified EVPD and page code */
   1589 	(void) scsi_setup_cdb((union scsi_cdb *)inq_pkt->pkt_cdbp,
   1590 	    SCMD_INQUIRY, 0, buflen, 0);
   1591 	inq_pkt->pkt_cdbp[1] = evpd;
   1592 	inq_pkt->pkt_cdbp[2] = page_code;
   1593 
   1594 	inq_pkt->pkt_time = SCSI_POLL_TIMEOUT;	/* in seconds */
   1595 	inq_pkt->pkt_flags = FLAG_NOINTR|FLAG_NOPARITY;
   1596 
   1597 	/*
   1598 	 * Issue inquiry command thru scsi_test
   1599 	 *
   1600 	 * NOTE: This is important data about device identity, not sure why
   1601 	 * NOPARITY is used. Also seems like we should check pkt_stat for
   1602 	 * STATE_XFERRED_DATA.
   1603 	 */
   1604 	if ((scsi_test(inq_pkt) == 0) &&
   1605 	    (inq_pkt->pkt_reason == CMD_CMPLT) &&
   1606 	    (((*inq_pkt->pkt_scbp) & STATUS_MASK) == 0)) {
   1607 		ASSERT(inq_pkt->pkt_resid >= 0);
   1608 		ASSERT(inq_pkt->pkt_resid <= buflen);
   1609 
   1610 		bcopy(inq_bp->b_un.b_addr,
   1611 		    bufaddr, buflen - inq_pkt->pkt_resid);
   1612 		if (lenp)
   1613 			*lenp = (buflen - inq_pkt->pkt_resid);
   1614 		rval = 0;
   1615 	}
   1616 
   1617 out:	if (inq_pkt)
   1618 		scsi_destroy_pkt(inq_pkt);
   1619 	if (inq_bp)
   1620 		scsi_free_consistent_buf(inq_bp);
   1621 	return (rval);
   1622 }
   1623