Home | History | Annotate | Download | only in targets
      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 /*
     23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     24  * Use is subject to license terms.
     25  */
     26 
     27 /*
     28  * SCSI disk target driver.
     29  */
     30 #include <sys/scsi/scsi.h>
     31 #include <sys/dkbad.h>
     32 #include <sys/dklabel.h>
     33 #include <sys/dkio.h>
     34 #include <sys/fdio.h>
     35 #include <sys/cdio.h>
     36 #include <sys/mhd.h>
     37 #include <sys/vtoc.h>
     38 #include <sys/dktp/fdisk.h>
     39 #include <sys/kstat.h>
     40 #include <sys/vtrace.h>
     41 #include <sys/note.h>
     42 #include <sys/thread.h>
     43 #include <sys/proc.h>
     44 #include <sys/efi_partition.h>
     45 #include <sys/var.h>
     46 #include <sys/aio_req.h>
     47 
     48 #ifdef __lock_lint
     49 #define	_LP64
     50 #define	__amd64
     51 #endif
     52 
     53 #if (defined(__fibre))
     54 /* Note: is there a leadville version of the following? */
     55 #include <sys/fc4/fcal_linkapp.h>
     56 #endif
     57 #include <sys/taskq.h>
     58 #include <sys/uuid.h>
     59 #include <sys/byteorder.h>
     60 #include <sys/sdt.h>
     61 
     62 #include "sd_xbuf.h"
     63 
     64 #include <sys/scsi/targets/sddef.h>
     65 #include <sys/cmlb.h>
     66 #include <sys/sysevent/eventdefs.h>
     67 #include <sys/sysevent/dev.h>
     68 
     69 #include <sys/fm/protocol.h>
     70 
     71 /*
     72  * Loadable module info.
     73  */
     74 #if (defined(__fibre))
     75 #define	SD_MODULE_NAME	"SCSI SSA/FCAL Disk Driver"
     76 char _depends_on[]	= "misc/scsi misc/cmlb drv/fcp";
     77 #else /* !__fibre */
     78 #define	SD_MODULE_NAME	"SCSI Disk Driver"
     79 char _depends_on[]	= "misc/scsi misc/cmlb";
     80 #endif /* !__fibre */
     81 
     82 /*
     83  * Define the interconnect type, to allow the driver to distinguish
     84  * between parallel SCSI (sd) and fibre channel (ssd) behaviors.
     85  *
     86  * This is really for backward compatibility. In the future, the driver
     87  * should actually check the "interconnect-type" property as reported by
     88  * the HBA; however at present this property is not defined by all HBAs,
     89  * so we will use this #define (1) to permit the driver to run in
     90  * backward-compatibility mode; and (2) to print a notification message
     91  * if an FC HBA does not support the "interconnect-type" property.  The
     92  * behavior of the driver will be to assume parallel SCSI behaviors unless
     93  * the "interconnect-type" property is defined by the HBA **AND** has a
     94  * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or
     95  * INTERCONNECT_FABRIC, in which case the driver will assume Fibre
     96  * Channel behaviors (as per the old ssd).  (Note that the
     97  * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and
     98  * will result in the driver assuming parallel SCSI behaviors.)
     99  *
    100  * (see common/sys/scsi/impl/services.h)
    101  *
    102  * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default
    103  * since some FC HBAs may already support that, and there is some code in
    104  * the driver that already looks for it.  Using INTERCONNECT_FABRIC as the
    105  * default would confuse that code, and besides things should work fine
    106  * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the
    107  * "interconnect_type" property.
    108  *
    109  */
    110 #if (defined(__fibre))
    111 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_FIBRE
    112 #else
    113 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_PARALLEL
    114 #endif
    115 
    116 /*
    117  * The name of the driver, established from the module name in _init.
    118  */
    119 static	char *sd_label			= NULL;
    120 
    121 /*
    122  * Driver name is unfortunately prefixed on some driver.conf properties.
    123  */
    124 #if (defined(__fibre))
    125 #define	sd_max_xfer_size		ssd_max_xfer_size
    126 #define	sd_config_list			ssd_config_list
    127 static	char *sd_max_xfer_size		= "ssd_max_xfer_size";
    128 static	char *sd_config_list		= "ssd-config-list";
    129 #else
    130 static	char *sd_max_xfer_size		= "sd_max_xfer_size";
    131 static	char *sd_config_list		= "sd-config-list";
    132 #endif
    133 
    134 /*
    135  * Driver global variables
    136  */
    137 
    138 #if (defined(__fibre))
    139 /*
    140  * These #defines are to avoid namespace collisions that occur because this
    141  * code is currently used to compile two separate driver modules: sd and ssd.
    142  * All global variables need to be treated this way (even if declared static)
    143  * in order to allow the debugger to resolve the names properly.
    144  * It is anticipated that in the near future the ssd module will be obsoleted,
    145  * at which time this namespace issue should go away.
    146  */
    147 #define	sd_state			ssd_state
    148 #define	sd_io_time			ssd_io_time
    149 #define	sd_failfast_enable		ssd_failfast_enable
    150 #define	sd_ua_retry_count		ssd_ua_retry_count
    151 #define	sd_report_pfa			ssd_report_pfa
    152 #define	sd_max_throttle			ssd_max_throttle
    153 #define	sd_min_throttle			ssd_min_throttle
    154 #define	sd_rot_delay			ssd_rot_delay
    155 
    156 #define	sd_retry_on_reservation_conflict	\
    157 					ssd_retry_on_reservation_conflict
    158 #define	sd_reinstate_resv_delay		ssd_reinstate_resv_delay
    159 #define	sd_resv_conflict_name		ssd_resv_conflict_name
    160 
    161 #define	sd_component_mask		ssd_component_mask
    162 #define	sd_level_mask			ssd_level_mask
    163 #define	sd_debug_un			ssd_debug_un
    164 #define	sd_error_level			ssd_error_level
    165 
    166 #define	sd_xbuf_active_limit		ssd_xbuf_active_limit
    167 #define	sd_xbuf_reserve_limit		ssd_xbuf_reserve_limit
    168 
    169 #define	sd_tr				ssd_tr
    170 #define	sd_reset_throttle_timeout	ssd_reset_throttle_timeout
    171 #define	sd_qfull_throttle_timeout	ssd_qfull_throttle_timeout
    172 #define	sd_qfull_throttle_enable	ssd_qfull_throttle_enable
    173 #define	sd_check_media_time		ssd_check_media_time
    174 #define	sd_wait_cmds_complete		ssd_wait_cmds_complete
    175 #define	sd_label_mutex			ssd_label_mutex
    176 #define	sd_detach_mutex			ssd_detach_mutex
    177 #define	sd_log_buf			ssd_log_buf
    178 #define	sd_log_mutex			ssd_log_mutex
    179 
    180 #define	sd_disk_table			ssd_disk_table
    181 #define	sd_disk_table_size		ssd_disk_table_size
    182 #define	sd_sense_mutex			ssd_sense_mutex
    183 #define	sd_cdbtab			ssd_cdbtab
    184 
    185 #define	sd_cb_ops			ssd_cb_ops
    186 #define	sd_ops				ssd_ops
    187 #define	sd_additional_codes		ssd_additional_codes
    188 #define	sd_tgops			ssd_tgops
    189 
    190 #define	sd_minor_data			ssd_minor_data
    191 #define	sd_minor_data_efi		ssd_minor_data_efi
    192 
    193 #define	sd_tq				ssd_tq
    194 #define	sd_wmr_tq			ssd_wmr_tq
    195 #define	sd_taskq_name			ssd_taskq_name
    196 #define	sd_wmr_taskq_name		ssd_wmr_taskq_name
    197 #define	sd_taskq_minalloc		ssd_taskq_minalloc
    198 #define	sd_taskq_maxalloc		ssd_taskq_maxalloc
    199 
    200 #define	sd_dump_format_string		ssd_dump_format_string
    201 
    202 #define	sd_iostart_chain		ssd_iostart_chain
    203 #define	sd_iodone_chain			ssd_iodone_chain
    204 
    205 #define	sd_pm_idletime			ssd_pm_idletime
    206 
    207 #define	sd_force_pm_supported		ssd_force_pm_supported
    208 
    209 #define	sd_dtype_optical_bind		ssd_dtype_optical_bind
    210 
    211 #define	sd_ssc_init			ssd_ssc_init
    212 #define	sd_ssc_send			ssd_ssc_send
    213 #define	sd_ssc_fini			ssd_ssc_fini
    214 #define	sd_ssc_assessment		ssd_ssc_assessment
    215 #define	sd_ssc_post			ssd_ssc_post
    216 #define	sd_ssc_print			ssd_ssc_print
    217 #define	sd_ssc_ereport_post		ssd_ssc_ereport_post
    218 #define	sd_ssc_set_info			ssd_ssc_set_info
    219 #define	sd_ssc_extract_info		ssd_ssc_extract_info
    220 
    221 #endif
    222 
    223 #ifdef	SDDEBUG
    224 int	sd_force_pm_supported		= 0;
    225 #endif	/* SDDEBUG */
    226 
    227 void *sd_state				= NULL;
    228 int sd_io_time				= SD_IO_TIME;
    229 int sd_failfast_enable			= 1;
    230 int sd_ua_retry_count			= SD_UA_RETRY_COUNT;
    231 int sd_report_pfa			= 1;
    232 int sd_max_throttle			= SD_MAX_THROTTLE;
    233 int sd_min_throttle			= SD_MIN_THROTTLE;
    234 int sd_rot_delay			= 4; /* Default 4ms Rotation delay */
    235 int sd_qfull_throttle_enable		= TRUE;
    236 
    237 int sd_retry_on_reservation_conflict	= 1;
    238 int sd_reinstate_resv_delay		= SD_REINSTATE_RESV_DELAY;
    239 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay))
    240 
    241 static int sd_dtype_optical_bind	= -1;
    242 
    243 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */
    244 static	char *sd_resv_conflict_name	= "sd_retry_on_reservation_conflict";
    245 
    246 /*
    247  * Global data for debug logging. To enable debug printing, sd_component_mask
    248  * and sd_level_mask should be set to the desired bit patterns as outlined in
    249  * sddef.h.
    250  */
    251 uint_t	sd_component_mask		= 0x0;
    252 uint_t	sd_level_mask			= 0x0;
    253 struct	sd_lun *sd_debug_un		= NULL;
    254 uint_t	sd_error_level			= SCSI_ERR_RETRYABLE;
    255 
    256 /* Note: these may go away in the future... */
    257 static uint32_t	sd_xbuf_active_limit	= 512;
    258 static uint32_t sd_xbuf_reserve_limit	= 16;
    259 
    260 static struct sd_resv_reclaim_request	sd_tr = { NULL, NULL, NULL, 0, 0, 0 };
    261 
    262 /*
    263  * Timer value used to reset the throttle after it has been reduced
    264  * (typically in response to TRAN_BUSY or STATUS_QFULL)
    265  */
    266 static int sd_reset_throttle_timeout	= SD_RESET_THROTTLE_TIMEOUT;
    267 static int sd_qfull_throttle_timeout	= SD_QFULL_THROTTLE_TIMEOUT;
    268 
    269 /*
    270  * Interval value associated with the media change scsi watch.
    271  */
    272 static int sd_check_media_time		= 3000000;
    273 
    274 /*
    275  * Wait value used for in progress operations during a DDI_SUSPEND
    276  */
    277 static int sd_wait_cmds_complete	= SD_WAIT_CMDS_COMPLETE;
    278 
    279 /*
    280  * sd_label_mutex protects a static buffer used in the disk label
    281  * component of the driver
    282  */
    283 static kmutex_t sd_label_mutex;
    284 
    285 /*
    286  * sd_detach_mutex protects un_layer_count, un_detach_count, and
    287  * un_opens_in_progress in the sd_lun structure.
    288  */
    289 static kmutex_t sd_detach_mutex;
    290 
    291 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex,
    292 	sd_lun::{un_layer_count un_detach_count un_opens_in_progress}))
    293 
    294 /*
    295  * Global buffer and mutex for debug logging
    296  */
    297 static char	sd_log_buf[1024];
    298 static kmutex_t	sd_log_mutex;
    299 
    300 /*
    301  * Structs and globals for recording attached lun information.
    302  * This maintains a chain. Each node in the chain represents a SCSI controller.
    303  * The structure records the number of luns attached to each target connected
    304  * with the controller.
    305  * For parallel scsi device only.
    306  */
    307 struct sd_scsi_hba_tgt_lun {
    308 	struct sd_scsi_hba_tgt_lun	*next;
    309 	dev_info_t			*pdip;
    310 	int				nlun[NTARGETS_WIDE];
    311 };
    312 
    313 /*
    314  * Flag to indicate the lun is attached or detached
    315  */
    316 #define	SD_SCSI_LUN_ATTACH	0
    317 #define	SD_SCSI_LUN_DETACH	1
    318 
    319 static kmutex_t	sd_scsi_target_lun_mutex;
    320 static struct sd_scsi_hba_tgt_lun	*sd_scsi_target_lun_head = NULL;
    321 
    322 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
    323     sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip))
    324 
    325 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
    326     sd_scsi_target_lun_head))
    327 
    328 /*
    329  * "Smart" Probe Caching structs, globals, #defines, etc.
    330  * For parallel scsi and non-self-identify device only.
    331  */
    332 
    333 /*
    334  * The following resources and routines are implemented to support
    335  * "smart" probing, which caches the scsi_probe() results in an array,
    336  * in order to help avoid long probe times.
    337  */
    338 struct sd_scsi_probe_cache {
    339 	struct	sd_scsi_probe_cache	*next;
    340 	dev_info_t	*pdip;
    341 	int		cache[NTARGETS_WIDE];
    342 };
    343 
    344 static kmutex_t	sd_scsi_probe_cache_mutex;
    345 static struct	sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL;
    346 
    347 /*
    348  * Really we only need protection on the head of the linked list, but
    349  * better safe than sorry.
    350  */
    351 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
    352     sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip))
    353 
    354 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
    355     sd_scsi_probe_cache_head))
    356 
    357 /*
    358  * Power attribute table
    359  */
    360 static sd_power_attr_ss sd_pwr_ss = {
    361 	{ "NAME=spindle-motor", "0=off", "1=on", NULL },
    362 	{0, 100},
    363 	{30, 0},
    364 	{20000, 0}
    365 };
    366 
    367 static sd_power_attr_pc sd_pwr_pc = {
    368 	{ "NAME=spindle-motor", "0=stopped", "1=standby", "2=idle",
    369 		"3=active", NULL },
    370 	{0, 0, 0, 100},
    371 	{90, 90, 20, 0},
    372 	{15000, 15000, 1000, 0}
    373 };
    374 
    375 /*
    376  * Power level to power condition
    377  */
    378 static int sd_pl2pc[] = {
    379 	SD_TARGET_START_VALID,
    380 	SD_TARGET_STANDBY,
    381 	SD_TARGET_IDLE,
    382 	SD_TARGET_ACTIVE
    383 };
    384 
    385 /*
    386  * Vendor specific data name property declarations
    387  */
    388 
    389 #if defined(__fibre) || defined(__i386) ||defined(__amd64)
    390 
    391 static sd_tunables seagate_properties = {
    392 	SEAGATE_THROTTLE_VALUE,
    393 	0,
    394 	0,
    395 	0,
    396 	0,
    397 	0,
    398 	0,
    399 	0,
    400 	0
    401 };
    402 
    403 
    404 static sd_tunables fujitsu_properties = {
    405 	FUJITSU_THROTTLE_VALUE,
    406 	0,
    407 	0,
    408 	0,
    409 	0,
    410 	0,
    411 	0,
    412 	0,
    413 	0
    414 };
    415 
    416 static sd_tunables ibm_properties = {
    417 	IBM_THROTTLE_VALUE,
    418 	0,
    419 	0,
    420 	0,
    421 	0,
    422 	0,
    423 	0,
    424 	0,
    425 	0
    426 };
    427 
    428 static sd_tunables purple_properties = {
    429 	PURPLE_THROTTLE_VALUE,
    430 	0,
    431 	0,
    432 	PURPLE_BUSY_RETRIES,
    433 	PURPLE_RESET_RETRY_COUNT,
    434 	PURPLE_RESERVE_RELEASE_TIME,
    435 	0,
    436 	0,
    437 	0
    438 };
    439 
    440 static sd_tunables sve_properties = {
    441 	SVE_THROTTLE_VALUE,
    442 	0,
    443 	0,
    444 	SVE_BUSY_RETRIES,
    445 	SVE_RESET_RETRY_COUNT,
    446 	SVE_RESERVE_RELEASE_TIME,
    447 	SVE_MIN_THROTTLE_VALUE,
    448 	SVE_DISKSORT_DISABLED_FLAG,
    449 	0
    450 };
    451 
    452 static sd_tunables maserati_properties = {
    453 	0,
    454 	0,
    455 	0,
    456 	0,
    457 	0,
    458 	0,
    459 	0,
    460 	MASERATI_DISKSORT_DISABLED_FLAG,
    461 	MASERATI_LUN_RESET_ENABLED_FLAG
    462 };
    463 
    464 static sd_tunables pirus_properties = {
    465 	PIRUS_THROTTLE_VALUE,
    466 	0,
    467 	PIRUS_NRR_COUNT,
    468 	PIRUS_BUSY_RETRIES,
    469 	PIRUS_RESET_RETRY_COUNT,
    470 	0,
    471 	PIRUS_MIN_THROTTLE_VALUE,
    472 	PIRUS_DISKSORT_DISABLED_FLAG,
    473 	PIRUS_LUN_RESET_ENABLED_FLAG
    474 };
    475 
    476 #endif
    477 
    478 #if (defined(__sparc) && !defined(__fibre)) || \
    479 	(defined(__i386) || defined(__amd64))
    480 
    481 
    482 static sd_tunables elite_properties = {
    483 	ELITE_THROTTLE_VALUE,
    484 	0,
    485 	0,
    486 	0,
    487 	0,
    488 	0,
    489 	0,
    490 	0,
    491 	0
    492 };
    493 
    494 static sd_tunables st31200n_properties = {
    495 	ST31200N_THROTTLE_VALUE,
    496 	0,
    497 	0,
    498 	0,
    499 	0,
    500 	0,
    501 	0,
    502 	0,
    503 	0
    504 };
    505 
    506 #endif /* Fibre or not */
    507 
    508 static sd_tunables lsi_properties_scsi = {
    509 	LSI_THROTTLE_VALUE,
    510 	0,
    511 	LSI_NOTREADY_RETRIES,
    512 	0,
    513 	0,
    514 	0,
    515 	0,
    516 	0,
    517 	0
    518 };
    519 
    520 static sd_tunables symbios_properties = {
    521 	SYMBIOS_THROTTLE_VALUE,
    522 	0,
    523 	SYMBIOS_NOTREADY_RETRIES,
    524 	0,
    525 	0,
    526 	0,
    527 	0,
    528 	0,
    529 	0
    530 };
    531 
    532 static sd_tunables lsi_properties = {
    533 	0,
    534 	0,
    535 	LSI_NOTREADY_RETRIES,
    536 	0,
    537 	0,
    538 	0,
    539 	0,
    540 	0,
    541 	0
    542 };
    543 
    544 static sd_tunables lsi_oem_properties = {
    545 	0,
    546 	0,
    547 	LSI_OEM_NOTREADY_RETRIES,
    548 	0,
    549 	0,
    550 	0,
    551 	0,
    552 	0,
    553 	0,
    554 	1
    555 };
    556 
    557 
    558 
    559 #if (defined(SD_PROP_TST))
    560 
    561 #define	SD_TST_CTYPE_VAL	CTYPE_CDROM
    562 #define	SD_TST_THROTTLE_VAL	16
    563 #define	SD_TST_NOTREADY_VAL	12
    564 #define	SD_TST_BUSY_VAL		60
    565 #define	SD_TST_RST_RETRY_VAL	36
    566 #define	SD_TST_RSV_REL_TIME	60
    567 
    568 static sd_tunables tst_properties = {
    569 	SD_TST_THROTTLE_VAL,
    570 	SD_TST_CTYPE_VAL,
    571 	SD_TST_NOTREADY_VAL,
    572 	SD_TST_BUSY_VAL,
    573 	SD_TST_RST_RETRY_VAL,
    574 	SD_TST_RSV_REL_TIME,
    575 	0,
    576 	0,
    577 	0
    578 };
    579 #endif
    580 
    581 /* This is similar to the ANSI toupper implementation */
    582 #define	SD_TOUPPER(C)	(((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C))
    583 
    584 /*
    585  * Static Driver Configuration Table
    586  *
    587  * This is the table of disks which need throttle adjustment (or, perhaps
    588  * something else as defined by the flags at a future time.)  device_id
    589  * is a string consisting of concatenated vid (vendor), pid (product/model)
    590  * and revision strings as defined in the scsi_inquiry structure.  Offsets of
    591  * the parts of the string are as defined by the sizes in the scsi_inquiry
    592  * structure.  Device type is searched as far as the device_id string is
    593  * defined.  Flags defines which values are to be set in the driver from the
    594  * properties list.
    595  *
    596  * Entries below which begin and end with a "*" are a special case.
    597  * These do not have a specific vendor, and the string which follows
    598  * can appear anywhere in the 16 byte PID portion of the inquiry data.
    599  *
    600  * Entries below which begin and end with a " " (blank) are a special
    601  * case. The comparison function will treat multiple consecutive blanks
    602  * as equivalent to a single blank. For example, this causes a
    603  * sd_disk_table entry of " NEC CDROM " to match a device's id string
    604  * of  "NEC       CDROM".
    605  *
    606  * Note: The MD21 controller type has been obsoleted.
    607  *	 ST318202F is a Legacy device
    608  *	 MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been
    609  *	 made with an FC connection. The entries here are a legacy.
    610  */
    611 static sd_disk_config_t sd_disk_table[] = {
    612 #if defined(__fibre) || defined(__i386) || defined(__amd64)
    613 	{ "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
    614 	{ "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
    615 	{ "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
    616 	{ "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
    617 	{ "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties },
    618 	{ "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties },
    619 	{ "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties },
    620 	{ "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties },
    621 	{ "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties },
    622 	{ "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties },
    623 	{ "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties },
    624 	{ "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties },
    625 	{ "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties },
    626 	{ "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties },
    627 	{ "FUJITSU MAG3091F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
    628 	{ "FUJITSU MAG3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
    629 	{ "FUJITSU MAA3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
    630 	{ "FUJITSU MAF3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
    631 	{ "FUJITSU MAL3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
    632 	{ "FUJITSU MAL3738F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
    633 	{ "FUJITSU MAM3182FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
    634 	{ "FUJITSU MAM3364FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
    635 	{ "FUJITSU MAM3738FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
    636 	{ "IBM     DDYFT1835",  SD_CONF_BSET_THROTTLE, &ibm_properties },
    637 	{ "IBM     DDYFT3695",  SD_CONF_BSET_THROTTLE, &ibm_properties },
    638 	{ "IBM     IC35LF2D2",  SD_CONF_BSET_THROTTLE, &ibm_properties },
    639 	{ "IBM     IC35LF2PR",  SD_CONF_BSET_THROTTLE, &ibm_properties },
    640 	{ "IBM     1724-100",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    641 	{ "IBM     1726-2xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    642 	{ "IBM     1726-22x",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    643 	{ "IBM     1726-4xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    644 	{ "IBM     1726-42x",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    645 	{ "IBM     1726-3xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    646 	{ "IBM     3526",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    647 	{ "IBM     3542",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    648 	{ "IBM     3552",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    649 	{ "IBM     1722",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    650 	{ "IBM     1742",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    651 	{ "IBM     1815",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    652 	{ "IBM     FAStT",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    653 	{ "IBM     1814",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    654 	{ "IBM     1814-200",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    655 	{ "IBM     1818",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    656 	{ "DELL    MD3000",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    657 	{ "DELL    MD3000i",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    658 	{ "LSI     INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    659 	{ "ENGENIO INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    660 	{ "SGI     TP",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    661 	{ "SGI     IS",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    662 	{ "*CSM100_*",		SD_CONF_BSET_NRR_COUNT |
    663 			SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
    664 	{ "*CSM200_*",		SD_CONF_BSET_NRR_COUNT |
    665 			SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
    666 	{ "Fujitsu SX300",	SD_CONF_BSET_THROTTLE,  &lsi_oem_properties },
    667 	{ "LSI",		SD_CONF_BSET_NRR_COUNT, &lsi_properties },
    668 	{ "SUN     T3", SD_CONF_BSET_THROTTLE |
    669 			SD_CONF_BSET_BSY_RETRY_COUNT|
    670 			SD_CONF_BSET_RST_RETRIES|
    671 			SD_CONF_BSET_RSV_REL_TIME,
    672 		&purple_properties },
    673 	{ "SUN     SESS01", SD_CONF_BSET_THROTTLE |
    674 		SD_CONF_BSET_BSY_RETRY_COUNT|
    675 		SD_CONF_BSET_RST_RETRIES|
    676 		SD_CONF_BSET_RSV_REL_TIME|
    677 		SD_CONF_BSET_MIN_THROTTLE|
    678 		SD_CONF_BSET_DISKSORT_DISABLED,
    679 		&sve_properties },
    680 	{ "SUN     T4", SD_CONF_BSET_THROTTLE |
    681 			SD_CONF_BSET_BSY_RETRY_COUNT|
    682 			SD_CONF_BSET_RST_RETRIES|
    683 			SD_CONF_BSET_RSV_REL_TIME,
    684 		&purple_properties },
    685 	{ "SUN     SVE01", SD_CONF_BSET_DISKSORT_DISABLED |
    686 		SD_CONF_BSET_LUN_RESET_ENABLED,
    687 		&maserati_properties },
    688 	{ "SUN     SE6920", SD_CONF_BSET_THROTTLE |
    689 		SD_CONF_BSET_NRR_COUNT|
    690 		SD_CONF_BSET_BSY_RETRY_COUNT|
    691 		SD_CONF_BSET_RST_RETRIES|
    692 		SD_CONF_BSET_MIN_THROTTLE|
    693 		SD_CONF_BSET_DISKSORT_DISABLED|
    694 		SD_CONF_BSET_LUN_RESET_ENABLED,
    695 		&pirus_properties },
    696 	{ "SUN     SE6940", SD_CONF_BSET_THROTTLE |
    697 		SD_CONF_BSET_NRR_COUNT|
    698 		SD_CONF_BSET_BSY_RETRY_COUNT|
    699 		SD_CONF_BSET_RST_RETRIES|
    700 		SD_CONF_BSET_MIN_THROTTLE|
    701 		SD_CONF_BSET_DISKSORT_DISABLED|
    702 		SD_CONF_BSET_LUN_RESET_ENABLED,
    703 		&pirus_properties },
    704 	{ "SUN     StorageTek 6920", SD_CONF_BSET_THROTTLE |
    705 		SD_CONF_BSET_NRR_COUNT|
    706 		SD_CONF_BSET_BSY_RETRY_COUNT|
    707 		SD_CONF_BSET_RST_RETRIES|
    708 		SD_CONF_BSET_MIN_THROTTLE|
    709 		SD_CONF_BSET_DISKSORT_DISABLED|
    710 		SD_CONF_BSET_LUN_RESET_ENABLED,
    711 		&pirus_properties },
    712 	{ "SUN     StorageTek 6940", SD_CONF_BSET_THROTTLE |
    713 		SD_CONF_BSET_NRR_COUNT|
    714 		SD_CONF_BSET_BSY_RETRY_COUNT|
    715 		SD_CONF_BSET_RST_RETRIES|
    716 		SD_CONF_BSET_MIN_THROTTLE|
    717 		SD_CONF_BSET_DISKSORT_DISABLED|
    718 		SD_CONF_BSET_LUN_RESET_ENABLED,
    719 		&pirus_properties },
    720 	{ "SUN     PSX1000", SD_CONF_BSET_THROTTLE |
    721 		SD_CONF_BSET_NRR_COUNT|
    722 		SD_CONF_BSET_BSY_RETRY_COUNT|
    723 		SD_CONF_BSET_RST_RETRIES|
    724 		SD_CONF_BSET_MIN_THROTTLE|
    725 		SD_CONF_BSET_DISKSORT_DISABLED|
    726 		SD_CONF_BSET_LUN_RESET_ENABLED,
    727 		&pirus_properties },
    728 	{ "SUN     SE6330", SD_CONF_BSET_THROTTLE |
    729 		SD_CONF_BSET_NRR_COUNT|
    730 		SD_CONF_BSET_BSY_RETRY_COUNT|
    731 		SD_CONF_BSET_RST_RETRIES|
    732 		SD_CONF_BSET_MIN_THROTTLE|
    733 		SD_CONF_BSET_DISKSORT_DISABLED|
    734 		SD_CONF_BSET_LUN_RESET_ENABLED,
    735 		&pirus_properties },
    736 	{ "SUN     STK6580_6780", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    737 	{ "SUN     SUN_6180", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    738 	{ "STK     OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    739 	{ "STK     OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    740 	{ "STK     BladeCtlr",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    741 	{ "STK     FLEXLINE",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
    742 	{ "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties },
    743 #endif /* fibre or NON-sparc platforms */
    744 #if ((defined(__sparc) && !defined(__fibre)) ||\
    745 	(defined(__i386) || defined(__amd64)))
    746 	{ "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties },
    747 	{ "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties },
    748 	{ "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL },
    749 	{ "CONNER  CP30540",  SD_CONF_BSET_NOCACHE,  NULL },
    750 	{ "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL },
    751 	{ "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL },
    752 	{ "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL },
    753 	{ "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL },
    754 	{ "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL },
    755 	{ "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL },
    756 	{ "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL },
    757 	{ "SYMBIOS INF-01-00       ", SD_CONF_BSET_FAB_DEVID, NULL },
    758 	{ "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT,
    759 	    &symbios_properties },
    760 	{ "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT,
    761 	    &lsi_properties_scsi },
    762 #if defined(__i386) || defined(__amd64)
    763 	{ " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD
    764 				    | SD_CONF_BSET_READSUB_BCD
    765 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
    766 				    | SD_CONF_BSET_NO_READ_HEADER
    767 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
    768 
    769 	{ " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD
    770 				    | SD_CONF_BSET_READSUB_BCD
    771 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
    772 				    | SD_CONF_BSET_NO_READ_HEADER
    773 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
    774 #endif /* __i386 || __amd64 */
    775 #endif /* sparc NON-fibre or NON-sparc platforms */
    776 
    777 #if (defined(SD_PROP_TST))
    778 	{ "VENDOR  PRODUCT ", (SD_CONF_BSET_THROTTLE
    779 				| SD_CONF_BSET_CTYPE
    780 				| SD_CONF_BSET_NRR_COUNT
    781 				| SD_CONF_BSET_FAB_DEVID
    782 				| SD_CONF_BSET_NOCACHE
    783 				| SD_CONF_BSET_BSY_RETRY_COUNT
    784 				| SD_CONF_BSET_PLAYMSF_BCD
    785 				| SD_CONF_BSET_READSUB_BCD
    786 				| SD_CONF_BSET_READ_TOC_TRK_BCD
    787 				| SD_CONF_BSET_READ_TOC_ADDR_BCD
    788 				| SD_CONF_BSET_NO_READ_HEADER
    789 				| SD_CONF_BSET_READ_CD_XD4
    790 				| SD_CONF_BSET_RST_RETRIES
    791 				| SD_CONF_BSET_RSV_REL_TIME
    792 				| SD_CONF_BSET_TUR_CHECK), &tst_properties},
    793 #endif
    794 };
    795 
    796 static const int sd_disk_table_size =
    797 	sizeof (sd_disk_table)/ sizeof (sd_disk_config_t);
    798 
    799 
    800 
    801 #define	SD_INTERCONNECT_PARALLEL	0
    802 #define	SD_INTERCONNECT_FABRIC		1
    803 #define	SD_INTERCONNECT_FIBRE		2
    804 #define	SD_INTERCONNECT_SSA		3
    805 #define	SD_INTERCONNECT_SATA		4
    806 #define	SD_INTERCONNECT_SAS		5
    807 
    808 #define	SD_IS_PARALLEL_SCSI(un)		\
    809 	((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL)
    810 #define	SD_IS_SERIAL(un)		\
    811 	(((un)->un_interconnect_type == SD_INTERCONNECT_SATA) ||\
    812 	((un)->un_interconnect_type == SD_INTERCONNECT_SAS))
    813 
    814 /*
    815  * Definitions used by device id registration routines
    816  */
    817 #define	VPD_HEAD_OFFSET		3	/* size of head for vpd page */
    818 #define	VPD_PAGE_LENGTH		3	/* offset for pge length data */
    819 #define	VPD_MODE_PAGE		1	/* offset into vpd pg for "page code" */
    820 
    821 static kmutex_t sd_sense_mutex = {0};
    822 
    823 /*
    824  * Macros for updates of the driver state
    825  */
    826 #define	New_state(un, s)        \
    827 	(un)->un_last_state = (un)->un_state, (un)->un_state = (s)
    828 #define	Restore_state(un)	\
    829 	{ uchar_t tmp = (un)->un_last_state; New_state((un), tmp); }
    830 
    831 static struct sd_cdbinfo sd_cdbtab[] = {
    832 	{ CDB_GROUP0, 0x00,	   0x1FFFFF,   0xFF,	    },
    833 	{ CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF,	    },
    834 	{ CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF,  },
    835 	{ CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, },
    836 };
    837 
    838 /*
    839  * Specifies the number of seconds that must have elapsed since the last
    840  * cmd. has completed for a device to be declared idle to the PM framework.
    841  */
    842 static int sd_pm_idletime = 1;
    843 
    844 /*
    845  * Internal function prototypes
    846  */
    847 
    848 #if (defined(__fibre))
    849 /*
    850  * These #defines are to avoid namespace collisions that occur because this
    851  * code is currently used to compile two separate driver modules: sd and ssd.
    852  * All function names need to be treated this way (even if declared static)
    853  * in order to allow the debugger to resolve the names properly.
    854  * It is anticipated that in the near future the ssd module will be obsoleted,
    855  * at which time this ugliness should go away.
    856  */
    857 #define	sd_log_trace			ssd_log_trace
    858 #define	sd_log_info			ssd_log_info
    859 #define	sd_log_err			ssd_log_err
    860 #define	sdprobe				ssdprobe
    861 #define	sdinfo				ssdinfo
    862 #define	sd_prop_op			ssd_prop_op
    863 #define	sd_scsi_probe_cache_init	ssd_scsi_probe_cache_init
    864 #define	sd_scsi_probe_cache_fini	ssd_scsi_probe_cache_fini
    865 #define	sd_scsi_clear_probe_cache	ssd_scsi_clear_probe_cache
    866 #define	sd_scsi_probe_with_cache	ssd_scsi_probe_with_cache
    867 #define	sd_scsi_target_lun_init		ssd_scsi_target_lun_init
    868 #define	sd_scsi_target_lun_fini		ssd_scsi_target_lun_fini
    869 #define	sd_scsi_get_target_lun_count	ssd_scsi_get_target_lun_count
    870 #define	sd_scsi_update_lun_on_target	ssd_scsi_update_lun_on_target
    871 #define	sd_spin_up_unit			ssd_spin_up_unit
    872 #define	sd_enable_descr_sense		ssd_enable_descr_sense
    873 #define	sd_reenable_dsense_task		ssd_reenable_dsense_task
    874 #define	sd_set_mmc_caps			ssd_set_mmc_caps
    875 #define	sd_read_unit_properties		ssd_read_unit_properties
    876 #define	sd_process_sdconf_file		ssd_process_sdconf_file
    877 #define	sd_process_sdconf_table		ssd_process_sdconf_table
    878 #define	sd_sdconf_id_match		ssd_sdconf_id_match
    879 #define	sd_blank_cmp			ssd_blank_cmp
    880 #define	sd_chk_vers1_data		ssd_chk_vers1_data
    881 #define	sd_set_vers1_properties		ssd_set_vers1_properties
    882 #define	sd_check_solid_state		ssd_check_solid_state
    883 
    884 #define	sd_get_physical_geometry	ssd_get_physical_geometry
    885 #define	sd_get_virtual_geometry		ssd_get_virtual_geometry
    886 #define	sd_update_block_info		ssd_update_block_info
    887 #define	sd_register_devid		ssd_register_devid
    888 #define	sd_get_devid			ssd_get_devid
    889 #define	sd_create_devid			ssd_create_devid
    890 #define	sd_write_deviceid		ssd_write_deviceid
    891 #define	sd_check_vpd_page_support	ssd_check_vpd_page_support
    892 #define	sd_setup_pm			ssd_setup_pm
    893 #define	sd_create_pm_components		ssd_create_pm_components
    894 #define	sd_ddi_suspend			ssd_ddi_suspend
    895 #define	sd_ddi_resume			ssd_ddi_resume
    896 #define	sd_pm_state_change		ssd_pm_state_change
    897 #define	sdpower				ssdpower
    898 #define	sdattach			ssdattach
    899 #define	sddetach			ssddetach
    900 #define	sd_unit_attach			ssd_unit_attach
    901 #define	sd_unit_detach			ssd_unit_detach
    902 #define	sd_set_unit_attributes		ssd_set_unit_attributes
    903 #define	sd_create_errstats		ssd_create_errstats
    904 #define	sd_set_errstats			ssd_set_errstats
    905 #define	sd_set_pstats			ssd_set_pstats
    906 #define	sddump				ssddump
    907 #define	sd_scsi_poll			ssd_scsi_poll
    908 #define	sd_send_polled_RQS		ssd_send_polled_RQS
    909 #define	sd_ddi_scsi_poll		ssd_ddi_scsi_poll
    910 #define	sd_init_event_callbacks		ssd_init_event_callbacks
    911 #define	sd_event_callback		ssd_event_callback
    912 #define	sd_cache_control		ssd_cache_control
    913 #define	sd_get_write_cache_enabled	ssd_get_write_cache_enabled
    914 #define	sd_get_nv_sup			ssd_get_nv_sup
    915 #define	sd_make_device			ssd_make_device
    916 #define	sdopen				ssdopen
    917 #define	sdclose				ssdclose
    918 #define	sd_ready_and_valid		ssd_ready_and_valid
    919 #define	sdmin				ssdmin
    920 #define	sdread				ssdread
    921 #define	sdwrite				ssdwrite
    922 #define	sdaread				ssdaread
    923 #define	sdawrite			ssdawrite
    924 #define	sdstrategy			ssdstrategy
    925 #define	sdioctl				ssdioctl
    926 #define	sd_mapblockaddr_iostart		ssd_mapblockaddr_iostart
    927 #define	sd_mapblocksize_iostart		ssd_mapblocksize_iostart
    928 #define	sd_checksum_iostart		ssd_checksum_iostart
    929 #define	sd_checksum_uscsi_iostart	ssd_checksum_uscsi_iostart
    930 #define	sd_pm_iostart			ssd_pm_iostart
    931 #define	sd_core_iostart			ssd_core_iostart
    932 #define	sd_mapblockaddr_iodone		ssd_mapblockaddr_iodone
    933 #define	sd_mapblocksize_iodone		ssd_mapblocksize_iodone
    934 #define	sd_checksum_iodone		ssd_checksum_iodone
    935 #define	sd_checksum_uscsi_iodone	ssd_checksum_uscsi_iodone
    936 #define	sd_pm_iodone			ssd_pm_iodone
    937 #define	sd_initpkt_for_buf		ssd_initpkt_for_buf
    938 #define	sd_destroypkt_for_buf		ssd_destroypkt_for_buf
    939 #define	sd_setup_rw_pkt			ssd_setup_rw_pkt
    940 #define	sd_setup_next_rw_pkt		ssd_setup_next_rw_pkt
    941 #define	sd_buf_iodone			ssd_buf_iodone
    942 #define	sd_uscsi_strategy		ssd_uscsi_strategy
    943 #define	sd_initpkt_for_uscsi		ssd_initpkt_for_uscsi
    944 #define	sd_destroypkt_for_uscsi		ssd_destroypkt_for_uscsi
    945 #define	sd_uscsi_iodone			ssd_uscsi_iodone
    946 #define	sd_xbuf_strategy		ssd_xbuf_strategy
    947 #define	sd_xbuf_init			ssd_xbuf_init
    948 #define	sd_pm_entry			ssd_pm_entry
    949 #define	sd_pm_exit			ssd_pm_exit
    950 
    951 #define	sd_pm_idletimeout_handler	ssd_pm_idletimeout_handler
    952 #define	sd_pm_timeout_handler		ssd_pm_timeout_handler
    953 
    954 #define	sd_add_buf_to_waitq		ssd_add_buf_to_waitq
    955 #define	sdintr				ssdintr
    956 #define	sd_start_cmds			ssd_start_cmds
    957 #define	sd_send_scsi_cmd		ssd_send_scsi_cmd
    958 #define	sd_bioclone_alloc		ssd_bioclone_alloc
    959 #define	sd_bioclone_free		ssd_bioclone_free
    960 #define	sd_shadow_buf_alloc		ssd_shadow_buf_alloc
    961 #define	sd_shadow_buf_free		ssd_shadow_buf_free
    962 #define	sd_print_transport_rejected_message	\
    963 					ssd_print_transport_rejected_message
    964 #define	sd_retry_command		ssd_retry_command
    965 #define	sd_set_retry_bp			ssd_set_retry_bp
    966 #define	sd_send_request_sense_command	ssd_send_request_sense_command
    967 #define	sd_start_retry_command		ssd_start_retry_command
    968 #define	sd_start_direct_priority_command	\
    969 					ssd_start_direct_priority_command
    970 #define	sd_return_failed_command	ssd_return_failed_command
    971 #define	sd_return_failed_command_no_restart	\
    972 					ssd_return_failed_command_no_restart
    973 #define	sd_return_command		ssd_return_command
    974 #define	sd_sync_with_callback		ssd_sync_with_callback
    975 #define	sdrunout			ssdrunout
    976 #define	sd_mark_rqs_busy		ssd_mark_rqs_busy
    977 #define	sd_mark_rqs_idle		ssd_mark_rqs_idle
    978 #define	sd_reduce_throttle		ssd_reduce_throttle
    979 #define	sd_restore_throttle		ssd_restore_throttle
    980 #define	sd_print_incomplete_msg		ssd_print_incomplete_msg
    981 #define	sd_init_cdb_limits		ssd_init_cdb_limits
    982 #define	sd_pkt_status_good		ssd_pkt_status_good
    983 #define	sd_pkt_status_check_condition	ssd_pkt_status_check_condition
    984 #define	sd_pkt_status_busy		ssd_pkt_status_busy
    985 #define	sd_pkt_status_reservation_conflict	\
    986 					ssd_pkt_status_reservation_conflict
    987 #define	sd_pkt_status_qfull		ssd_pkt_status_qfull
    988 #define	sd_handle_request_sense		ssd_handle_request_sense
    989 #define	sd_handle_auto_request_sense	ssd_handle_auto_request_sense
    990 #define	sd_print_sense_failed_msg	ssd_print_sense_failed_msg
    991 #define	sd_validate_sense_data		ssd_validate_sense_data
    992 #define	sd_decode_sense			ssd_decode_sense
    993 #define	sd_print_sense_msg		ssd_print_sense_msg
    994 #define	sd_sense_key_no_sense		ssd_sense_key_no_sense
    995 #define	sd_sense_key_recoverable_error	ssd_sense_key_recoverable_error
    996 #define	sd_sense_key_not_ready		ssd_sense_key_not_ready
    997 #define	sd_sense_key_medium_or_hardware_error	\
    998 					ssd_sense_key_medium_or_hardware_error
    999 #define	sd_sense_key_illegal_request	ssd_sense_key_illegal_request
   1000 #define	sd_sense_key_unit_attention	ssd_sense_key_unit_attention
   1001 #define	sd_sense_key_fail_command	ssd_sense_key_fail_command
   1002 #define	sd_sense_key_blank_check	ssd_sense_key_blank_check
   1003 #define	sd_sense_key_aborted_command	ssd_sense_key_aborted_command
   1004 #define	sd_sense_key_default		ssd_sense_key_default
   1005 #define	sd_print_retry_msg		ssd_print_retry_msg
   1006 #define	sd_print_cmd_incomplete_msg	ssd_print_cmd_incomplete_msg
   1007 #define	sd_pkt_reason_cmd_incomplete	ssd_pkt_reason_cmd_incomplete
   1008 #define	sd_pkt_reason_cmd_tran_err	ssd_pkt_reason_cmd_tran_err
   1009 #define	sd_pkt_reason_cmd_reset		ssd_pkt_reason_cmd_reset
   1010 #define	sd_pkt_reason_cmd_aborted	ssd_pkt_reason_cmd_aborted
   1011 #define	sd_pkt_reason_cmd_timeout	ssd_pkt_reason_cmd_timeout
   1012 #define	sd_pkt_reason_cmd_unx_bus_free	ssd_pkt_reason_cmd_unx_bus_free
   1013 #define	sd_pkt_reason_cmd_tag_reject	ssd_pkt_reason_cmd_tag_reject
   1014 #define	sd_pkt_reason_default		ssd_pkt_reason_default
   1015 #define	sd_reset_target			ssd_reset_target
   1016 #define	sd_start_stop_unit_callback	ssd_start_stop_unit_callback
   1017 #define	sd_start_stop_unit_task		ssd_start_stop_unit_task
   1018 #define	sd_taskq_create			ssd_taskq_create
   1019 #define	sd_taskq_delete			ssd_taskq_delete
   1020 #define	sd_target_change_task		ssd_target_change_task
   1021 #define	sd_log_dev_status_event		ssd_log_dev_status_event
   1022 #define	sd_log_lun_expansion_event	ssd_log_lun_expansion_event
   1023 #define	sd_log_eject_request_event	ssd_log_eject_request_event
   1024 #define	sd_media_change_task		ssd_media_change_task
   1025 #define	sd_handle_mchange		ssd_handle_mchange
   1026 #define	sd_send_scsi_DOORLOCK		ssd_send_scsi_DOORLOCK
   1027 #define	sd_send_scsi_READ_CAPACITY	ssd_send_scsi_READ_CAPACITY
   1028 #define	sd_send_scsi_READ_CAPACITY_16	ssd_send_scsi_READ_CAPACITY_16
   1029 #define	sd_send_scsi_GET_CONFIGURATION	ssd_send_scsi_GET_CONFIGURATION
   1030 #define	sd_send_scsi_feature_GET_CONFIGURATION	\
   1031 					sd_send_scsi_feature_GET_CONFIGURATION
   1032 #define	sd_send_scsi_START_STOP_UNIT	ssd_send_scsi_START_STOP_UNIT
   1033 #define	sd_send_scsi_INQUIRY		ssd_send_scsi_INQUIRY
   1034 #define	sd_send_scsi_TEST_UNIT_READY	ssd_send_scsi_TEST_UNIT_READY
   1035 #define	sd_send_scsi_PERSISTENT_RESERVE_IN	\
   1036 					ssd_send_scsi_PERSISTENT_RESERVE_IN
   1037 #define	sd_send_scsi_PERSISTENT_RESERVE_OUT	\
   1038 					ssd_send_scsi_PERSISTENT_RESERVE_OUT
   1039 #define	sd_send_scsi_SYNCHRONIZE_CACHE	ssd_send_scsi_SYNCHRONIZE_CACHE
   1040 #define	sd_send_scsi_SYNCHRONIZE_CACHE_biodone	\
   1041 					ssd_send_scsi_SYNCHRONIZE_CACHE_biodone
   1042 #define	sd_send_scsi_MODE_SENSE		ssd_send_scsi_MODE_SENSE
   1043 #define	sd_send_scsi_MODE_SELECT	ssd_send_scsi_MODE_SELECT
   1044 #define	sd_send_scsi_RDWR		ssd_send_scsi_RDWR
   1045 #define	sd_send_scsi_LOG_SENSE		ssd_send_scsi_LOG_SENSE
   1046 #define	sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION	\
   1047 				ssd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
   1048 #define	sd_gesn_media_data_valid	ssd_gesn_media_data_valid
   1049 #define	sd_alloc_rqs			ssd_alloc_rqs
   1050 #define	sd_free_rqs			ssd_free_rqs
   1051 #define	sd_dump_memory			ssd_dump_memory
   1052 #define	sd_get_media_info		ssd_get_media_info
   1053 #define	sd_get_media_info_ext		ssd_get_media_info_ext
   1054 #define	sd_dkio_ctrl_info		ssd_dkio_ctrl_info
   1055 #define	sd_nvpair_str_decode		ssd_nvpair_str_decode
   1056 #define	sd_strtok_r			ssd_strtok_r
   1057 #define	sd_set_properties		ssd_set_properties
   1058 #define	sd_get_tunables_from_conf	ssd_get_tunables_from_conf
   1059 #define	sd_setup_next_xfer		ssd_setup_next_xfer
   1060 #define	sd_dkio_get_temp		ssd_dkio_get_temp
   1061 #define	sd_check_mhd			ssd_check_mhd
   1062 #define	sd_mhd_watch_cb			ssd_mhd_watch_cb
   1063 #define	sd_mhd_watch_incomplete		ssd_mhd_watch_incomplete
   1064 #define	sd_sname			ssd_sname
   1065 #define	sd_mhd_resvd_recover		ssd_mhd_resvd_recover
   1066 #define	sd_resv_reclaim_thread		ssd_resv_reclaim_thread
   1067 #define	sd_take_ownership		ssd_take_ownership
   1068 #define	sd_reserve_release		ssd_reserve_release
   1069 #define	sd_rmv_resv_reclaim_req		ssd_rmv_resv_reclaim_req
   1070 #define	sd_mhd_reset_notify_cb		ssd_mhd_reset_notify_cb
   1071 #define	sd_persistent_reservation_in_read_keys	\
   1072 					ssd_persistent_reservation_in_read_keys
   1073 #define	sd_persistent_reservation_in_read_resv	\
   1074 					ssd_persistent_reservation_in_read_resv
   1075 #define	sd_mhdioc_takeown		ssd_mhdioc_takeown
   1076 #define	sd_mhdioc_failfast		ssd_mhdioc_failfast
   1077 #define	sd_mhdioc_release		ssd_mhdioc_release
   1078 #define	sd_mhdioc_register_devid	ssd_mhdioc_register_devid
   1079 #define	sd_mhdioc_inkeys		ssd_mhdioc_inkeys
   1080 #define	sd_mhdioc_inresv		ssd_mhdioc_inresv
   1081 #define	sr_change_blkmode		ssr_change_blkmode
   1082 #define	sr_change_speed			ssr_change_speed
   1083 #define	sr_atapi_change_speed		ssr_atapi_change_speed
   1084 #define	sr_pause_resume			ssr_pause_resume
   1085 #define	sr_play_msf			ssr_play_msf
   1086 #define	sr_play_trkind			ssr_play_trkind
   1087 #define	sr_read_all_subcodes		ssr_read_all_subcodes
   1088 #define	sr_read_subchannel		ssr_read_subchannel
   1089 #define	sr_read_tocentry		ssr_read_tocentry
   1090 #define	sr_read_tochdr			ssr_read_tochdr
   1091 #define	sr_read_cdda			ssr_read_cdda
   1092 #define	sr_read_cdxa			ssr_read_cdxa
   1093 #define	sr_read_mode1			ssr_read_mode1
   1094 #define	sr_read_mode2			ssr_read_mode2
   1095 #define	sr_read_cd_mode2		ssr_read_cd_mode2
   1096 #define	sr_sector_mode			ssr_sector_mode
   1097 #define	sr_eject			ssr_eject
   1098 #define	sr_ejected			ssr_ejected
   1099 #define	sr_check_wp			ssr_check_wp
   1100 #define	sd_watch_request_submit		ssd_watch_request_submit
   1101 #define	sd_check_media			ssd_check_media
   1102 #define	sd_media_watch_cb		ssd_media_watch_cb
   1103 #define	sd_delayed_cv_broadcast		ssd_delayed_cv_broadcast
   1104 #define	sr_volume_ctrl			ssr_volume_ctrl
   1105 #define	sr_read_sony_session_offset	ssr_read_sony_session_offset
   1106 #define	sd_log_page_supported		ssd_log_page_supported
   1107 #define	sd_check_for_writable_cd	ssd_check_for_writable_cd
   1108 #define	sd_wm_cache_constructor		ssd_wm_cache_constructor
   1109 #define	sd_wm_cache_destructor		ssd_wm_cache_destructor
   1110 #define	sd_range_lock			ssd_range_lock
   1111 #define	sd_get_range			ssd_get_range
   1112 #define	sd_free_inlist_wmap		ssd_free_inlist_wmap
   1113 #define	sd_range_unlock			ssd_range_unlock
   1114 #define	sd_read_modify_write_task	ssd_read_modify_write_task
   1115 #define	sddump_do_read_of_rmw		ssddump_do_read_of_rmw
   1116 
   1117 #define	sd_iostart_chain		ssd_iostart_chain
   1118 #define	sd_iodone_chain			ssd_iodone_chain
   1119 #define	sd_initpkt_map			ssd_initpkt_map
   1120 #define	sd_destroypkt_map		ssd_destroypkt_map
   1121 #define	sd_chain_type_map		ssd_chain_type_map
   1122 #define	sd_chain_index_map		ssd_chain_index_map
   1123 
   1124 #define	sd_failfast_flushctl		ssd_failfast_flushctl
   1125 #define	sd_failfast_flushq		ssd_failfast_flushq
   1126 #define	sd_failfast_flushq_callback	ssd_failfast_flushq_callback
   1127 
   1128 #define	sd_is_lsi			ssd_is_lsi
   1129 #define	sd_tg_rdwr			ssd_tg_rdwr
   1130 #define	sd_tg_getinfo			ssd_tg_getinfo
   1131 #define	sd_rmw_msg_print_handler	ssd_rmw_msg_print_handler
   1132 
   1133 #endif	/* #if (defined(__fibre)) */
   1134 
   1135 
   1136 int _init(void);
   1137 int _fini(void);
   1138 int _info(struct modinfo *modinfop);
   1139 
   1140 /*PRINTFLIKE3*/
   1141 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...);
   1142 /*PRINTFLIKE3*/
   1143 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...);
   1144 /*PRINTFLIKE3*/
   1145 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...);
   1146 
   1147 static int sdprobe(dev_info_t *devi);
   1148 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
   1149     void **result);
   1150 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
   1151     int mod_flags, char *name, caddr_t valuep, int *lengthp);
   1152 
   1153 /*
   1154  * Smart probe for parallel scsi
   1155  */
   1156 static void sd_scsi_probe_cache_init(void);
   1157 static void sd_scsi_probe_cache_fini(void);
   1158 static void sd_scsi_clear_probe_cache(void);
   1159 static int  sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)());
   1160 
   1161 /*
   1162  * Attached luns on target for parallel scsi
   1163  */
   1164 static void sd_scsi_target_lun_init(void);
   1165 static void sd_scsi_target_lun_fini(void);
   1166 static int  sd_scsi_get_target_lun_count(dev_info_t *dip, int target);
   1167 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag);
   1168 
   1169 static int	sd_spin_up_unit(sd_ssc_t *ssc);
   1170 
   1171 /*
   1172  * Using sd_ssc_init to establish sd_ssc_t struct
   1173  * Using sd_ssc_send to send uscsi internal command
   1174  * Using sd_ssc_fini to free sd_ssc_t struct
   1175  */
   1176 static sd_ssc_t *sd_ssc_init(struct sd_lun *un);
   1177 static int sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd,
   1178     int flag, enum uio_seg dataspace, int path_flag);
   1179 static void sd_ssc_fini(sd_ssc_t *ssc);
   1180 
   1181 /*
   1182  * Using sd_ssc_assessment to set correct type-of-assessment
   1183  * Using sd_ssc_post to post ereport & system log
   1184  *       sd_ssc_post will call sd_ssc_print to print system log
   1185  *       sd_ssc_post will call sd_ssd_ereport_post to post ereport
   1186  */
   1187 static void sd_ssc_assessment(sd_ssc_t *ssc,
   1188     enum sd_type_assessment tp_assess);
   1189 
   1190 static void sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess);
   1191 static void sd_ssc_print(sd_ssc_t *ssc, int sd_severity);
   1192 static void sd_ssc_ereport_post(sd_ssc_t *ssc,
   1193     enum sd_driver_assessment drv_assess);
   1194 
   1195 /*
   1196  * Using sd_ssc_set_info to mark an un-decodable-data error.
   1197  * Using sd_ssc_extract_info to transfer information from internal
   1198  *       data structures to sd_ssc_t.
   1199  */
   1200 static void sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp,
   1201     const char *fmt, ...);
   1202 static void sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un,
   1203     struct scsi_pkt *pktp, struct buf *bp, struct sd_xbuf *xp);
   1204 
   1205 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
   1206     enum uio_seg dataspace, int path_flag);
   1207 
   1208 #ifdef _LP64
   1209 static void	sd_enable_descr_sense(sd_ssc_t *ssc);
   1210 static void	sd_reenable_dsense_task(void *arg);
   1211 #endif /* _LP64 */
   1212 
   1213 static void	sd_set_mmc_caps(sd_ssc_t *ssc);
   1214 
   1215 static void sd_read_unit_properties(struct sd_lun *un);
   1216 static int  sd_process_sdconf_file(struct sd_lun *un);
   1217 static void sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str);
   1218 static char *sd_strtok_r(char *string, const char *sepset, char **lasts);
   1219 static void sd_set_properties(struct sd_lun *un, char *name, char *value);
   1220 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags,
   1221     int *data_list, sd_tunables *values);
   1222 static void sd_process_sdconf_table(struct sd_lun *un);
   1223 static int  sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen);
   1224 static int  sd_blank_cmp(struct sd_lun *un, char *id, int idlen);
   1225 static int  sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
   1226 	int list_len, char *dataname_ptr);
   1227 static void sd_set_vers1_properties(struct sd_lun *un, int flags,
   1228     sd_tunables *prop_list);
   1229 
   1230 static void sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi,
   1231     int reservation_flag);
   1232 static int  sd_get_devid(sd_ssc_t *ssc);
   1233 static ddi_devid_t sd_create_devid(sd_ssc_t *ssc);
   1234 static int  sd_write_deviceid(sd_ssc_t *ssc);
   1235 static int  sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len);
   1236 static int  sd_check_vpd_page_support(sd_ssc_t *ssc);
   1237 
   1238 static void sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi);
   1239 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un);
   1240 
   1241 static int  sd_ddi_suspend(dev_info_t *devi);
   1242 static int  sd_ddi_resume(dev_info_t *devi);
   1243 static int  sd_pm_state_change(struct sd_lun *un, int level, int flag);
   1244 static int  sdpower(dev_info_t *devi, int component, int level);
   1245 
   1246 static int  sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd);
   1247 static int  sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd);
   1248 static int  sd_unit_attach(dev_info_t *devi);
   1249 static int  sd_unit_detach(dev_info_t *devi);
   1250 
   1251 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi);
   1252 static void sd_create_errstats(struct sd_lun *un, int instance);
   1253 static void sd_set_errstats(struct sd_lun *un);
   1254 static void sd_set_pstats(struct sd_lun *un);
   1255 
   1256 static int  sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
   1257 static int  sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt);
   1258 static int  sd_send_polled_RQS(struct sd_lun *un);
   1259 static int  sd_ddi_scsi_poll(struct scsi_pkt *pkt);
   1260 
   1261 #if (defined(__fibre))
   1262 /*
   1263  * Event callbacks (photon)
   1264  */
   1265 static void sd_init_event_callbacks(struct sd_lun *un);
   1266 static void  sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *);
   1267 #endif
   1268 
   1269 /*
   1270  * Defines for sd_cache_control
   1271  */
   1272 
   1273 #define	SD_CACHE_ENABLE		1
   1274 #define	SD_CACHE_DISABLE	0
   1275 #define	SD_CACHE_NOCHANGE	-1
   1276 
   1277 static int   sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag);
   1278 static int   sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled);
   1279 static void  sd_get_nv_sup(sd_ssc_t *ssc);
   1280 static dev_t sd_make_device(dev_info_t *devi);
   1281 static void  sd_check_solid_state(sd_ssc_t *ssc);
   1282 
   1283 static void  sd_update_block_info(struct sd_lun *un, uint32_t lbasize,
   1284 	uint64_t capacity);
   1285 
   1286 /*
   1287  * Driver entry point functions.
   1288  */
   1289 static int  sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
   1290 static int  sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p);
   1291 static int  sd_ready_and_valid(sd_ssc_t *ssc, int part);
   1292 
   1293 static void sdmin(struct buf *bp);
   1294 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p);
   1295 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p);
   1296 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p);
   1297 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p);
   1298 
   1299 static int sdstrategy(struct buf *bp);
   1300 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *);
   1301 
   1302 /*
   1303  * Function prototypes for layering functions in the iostart chain.
   1304  */
   1305 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un,
   1306 	struct buf *bp);
   1307 static void sd_mapblocksize_iostart(int index, struct sd_lun *un,
   1308 	struct buf *bp);
   1309 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp);
   1310 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un,
   1311 	struct buf *bp);
   1312 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp);
   1313 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp);
   1314 
   1315 /*
   1316  * Function prototypes for layering functions in the iodone chain.
   1317  */
   1318 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp);
   1319 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp);
   1320 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un,
   1321 	struct buf *bp);
   1322 static void sd_mapblocksize_iodone(int index, struct sd_lun *un,
   1323 	struct buf *bp);
   1324 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp);
   1325 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un,
   1326 	struct buf *bp);
   1327 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp);
   1328 
   1329 /*
   1330  * Prototypes for functions to support buf(9S) based IO.
   1331  */
   1332 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg);
   1333 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **);
   1334 static void sd_destroypkt_for_buf(struct buf *);
   1335 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp,
   1336 	struct buf *bp, int flags,
   1337 	int (*callback)(caddr_t), caddr_t callback_arg,
   1338 	diskaddr_t lba, uint32_t blockcount);
   1339 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp,
   1340 	struct buf *bp, diskaddr_t lba, uint32_t blockcount);
   1341 
   1342 /*
   1343  * Prototypes for functions to support USCSI IO.
   1344  */
   1345 static int sd_uscsi_strategy(struct buf *bp);
   1346 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **);
   1347 static void sd_destroypkt_for_uscsi(struct buf *);
   1348 
   1349 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
   1350 	uchar_t chain_type, void *pktinfop);
   1351 
   1352 static int  sd_pm_entry(struct sd_lun *un);
   1353 static void sd_pm_exit(struct sd_lun *un);
   1354 
   1355 static void sd_pm_idletimeout_handler(void *arg);
   1356 
   1357 /*
   1358  * sd_core internal functions (used at the sd_core_io layer).
   1359  */
   1360 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp);
   1361 static void sdintr(struct scsi_pkt *pktp);
   1362 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp);
   1363 
   1364 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
   1365 	enum uio_seg dataspace, int path_flag);
   1366 
   1367 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen,
   1368 	daddr_t blkno, int (*func)(struct buf *));
   1369 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen,
   1370 	uint_t bflags, daddr_t blkno, int (*func)(struct buf *));
   1371 static void sd_bioclone_free(struct buf *bp);
   1372 static void sd_shadow_buf_free(struct buf *bp);
   1373 
   1374 static void sd_print_transport_rejected_message(struct sd_lun *un,
   1375 	struct sd_xbuf *xp, int code);
   1376 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp,
   1377     void *arg, int code);
   1378 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp,
   1379     void *arg, int code);
   1380 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp,
   1381     void *arg, int code);
   1382 
   1383 static void sd_retry_command(struct sd_lun *un, struct buf *bp,
   1384 	int retry_check_flag,
   1385 	void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp,
   1386 		int c),
   1387 	void *user_arg, int failure_code,  clock_t retry_delay,
   1388 	void (*statp)(kstat_io_t *));
   1389 
   1390 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp,
   1391 	clock_t retry_delay, void (*statp)(kstat_io_t *));
   1392 
   1393 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
   1394 	struct scsi_pkt *pktp);
   1395 static void sd_start_retry_command(void *arg);
   1396 static void sd_start_direct_priority_command(void *arg);
   1397 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp,
   1398 	int errcode);
   1399 static void sd_return_failed_command_no_restart(struct sd_lun *un,
   1400 	struct buf *bp, int errcode);
   1401 static void sd_return_command(struct sd_lun *un, struct buf *bp);
   1402 static void sd_sync_with_callback(struct sd_lun *un);
   1403 static int sdrunout(caddr_t arg);
   1404 
   1405 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp);
   1406 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp);
   1407 
   1408 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type);
   1409 static void sd_restore_throttle(void *arg);
   1410 
   1411 static void sd_init_cdb_limits(struct sd_lun *un);
   1412 
   1413 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
   1414 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1415 
   1416 /*
   1417  * Error handling functions
   1418  */
   1419 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
   1420 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1421 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp,
   1422 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1423 static void sd_pkt_status_reservation_conflict(struct sd_lun *un,
   1424 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1425 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
   1426 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1427 
   1428 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp,
   1429 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1430 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
   1431 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1432 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp,
   1433 	struct sd_xbuf *xp, size_t actual_len);
   1434 static void sd_decode_sense(struct sd_lun *un, struct buf *bp,
   1435 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1436 
   1437 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp,
   1438 	void *arg, int code);
   1439 
   1440 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
   1441 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1442 static void sd_sense_key_recoverable_error(struct sd_lun *un,
   1443 	uint8_t *sense_datap,
   1444 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1445 static void sd_sense_key_not_ready(struct sd_lun *un,
   1446 	uint8_t *sense_datap,
   1447 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1448 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
   1449 	uint8_t *sense_datap,
   1450 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1451 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
   1452 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1453 static void sd_sense_key_unit_attention(struct sd_lun *un,
   1454 	uint8_t *sense_datap,
   1455 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1456 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
   1457 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1458 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
   1459 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1460 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
   1461 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1462 static void sd_sense_key_default(struct sd_lun *un,
   1463 	uint8_t *sense_datap,
   1464 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1465 
   1466 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp,
   1467 	void *arg, int flag);
   1468 
   1469 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
   1470 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1471 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
   1472 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1473 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
   1474 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1475 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
   1476 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1477 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
   1478 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1479 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
   1480 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1481 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
   1482 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1483 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
   1484 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
   1485 
   1486 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp);
   1487 
   1488 static void sd_start_stop_unit_callback(void *arg);
   1489 static void sd_start_stop_unit_task(void *arg);
   1490 
   1491 static void sd_taskq_create(void);
   1492 static void sd_taskq_delete(void);
   1493 static void sd_target_change_task(void *arg);
   1494 static void sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag);
   1495 static void sd_log_lun_expansion_event(struct sd_lun *un, int km_flag);
   1496 static void sd_log_eject_request_event(struct sd_lun *un, int km_flag);
   1497 static void sd_media_change_task(void *arg);
   1498 
   1499 static int sd_handle_mchange(struct sd_lun *un);
   1500 static int sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag);
   1501 static int sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp,
   1502 	uint32_t *lbap, int path_flag);
   1503 static int sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
   1504 	uint32_t *lbap, uint32_t *psp, int path_flag);
   1505 static int sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag,
   1506 	int flag, int path_flag);
   1507 static int sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr,
   1508 	size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp);
   1509 static int sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag);
   1510 static int sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc,
   1511 	uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp);
   1512 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc,
   1513 	uchar_t usr_cmd, uchar_t *usr_bufp);
   1514 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un,
   1515 	struct dk_callback *dkc);
   1516 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp);
   1517 static int sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc,
   1518 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
   1519 	uchar_t *bufaddr, uint_t buflen, int path_flag);
   1520 static int sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
   1521 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
   1522 	uchar_t *bufaddr, uint_t buflen, char feature, int path_flag);
   1523 static int sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize,
   1524 	uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag);
   1525 static int sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize,
   1526 	uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag);
   1527 static int sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
   1528 	size_t buflen, daddr_t start_block, int path_flag);
   1529 #define	sd_send_scsi_READ(ssc, bufaddr, buflen, start_block, path_flag)	\
   1530 	sd_send_scsi_RDWR(ssc, SCMD_READ, bufaddr, buflen, start_block, \
   1531 	path_flag)
   1532 #define	sd_send_scsi_WRITE(ssc, bufaddr, buflen, start_block, path_flag)\
   1533 	sd_send_scsi_RDWR(ssc, SCMD_WRITE, bufaddr, buflen, start_block,\
   1534 	path_flag)
   1535 
   1536 static int sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr,
   1537 	uint16_t buflen, uchar_t page_code, uchar_t page_control,
   1538 	uint16_t param_ptr, int path_flag);
   1539 static int sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc,
   1540 	uchar_t *bufaddr, size_t buflen, uchar_t class_req);
   1541 static boolean_t sd_gesn_media_data_valid(uchar_t *data);
   1542 
   1543 static int  sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un);
   1544 static void sd_free_rqs(struct sd_lun *un);
   1545 
   1546 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title,
   1547 	uchar_t *data, int len, int fmt);
   1548 static void sd_panic_for_res_conflict(struct sd_lun *un);
   1549 
   1550 /*
   1551  * Disk Ioctl Function Prototypes
   1552  */
   1553 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag);
   1554 static int sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag);
   1555 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag);
   1556 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag);
   1557 
   1558 /*
   1559  * Multi-host Ioctl Prototypes
   1560  */
   1561 static int sd_check_mhd(dev_t dev, int interval);
   1562 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
   1563 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt);
   1564 static char *sd_sname(uchar_t status);
   1565 static void sd_mhd_resvd_recover(void *arg);
   1566 static void sd_resv_reclaim_thread();
   1567 static int sd_take_ownership(dev_t dev, struct mhioctkown *p);
   1568 static int sd_reserve_release(dev_t dev, int cmd);
   1569 static void sd_rmv_resv_reclaim_req(dev_t dev);
   1570 static void sd_mhd_reset_notify_cb(caddr_t arg);
   1571 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un,
   1572 	mhioc_inkeys_t *usrp, int flag);
   1573 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un,
   1574 	mhioc_inresvs_t *usrp, int flag);
   1575 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag);
   1576 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag);
   1577 static int sd_mhdioc_release(dev_t dev);
   1578 static int sd_mhdioc_register_devid(dev_t dev);
   1579 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag);
   1580 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag);
   1581 
   1582 /*
   1583  * SCSI removable prototypes
   1584  */
   1585 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag);
   1586 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
   1587 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
   1588 static int sr_pause_resume(dev_t dev, int mode);
   1589 static int sr_play_msf(dev_t dev, caddr_t data, int flag);
   1590 static int sr_play_trkind(dev_t dev, caddr_t data, int flag);
   1591 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag);
   1592 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag);
   1593 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag);
   1594 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag);
   1595 static int sr_read_cdda(dev_t dev, caddr_t data, int flag);
   1596 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag);
   1597 static int sr_read_mode1(dev_t dev, caddr_t data, int flag);
   1598 static int sr_read_mode2(dev_t dev, caddr_t data, int flag);
   1599 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag);
   1600 static int sr_sector_mode(dev_t dev, uint32_t blksize);
   1601 static int sr_eject(dev_t dev);
   1602 static void sr_ejected(register struct sd_lun *un);
   1603 static int sr_check_wp(dev_t dev);
   1604 static opaque_t sd_watch_request_submit(struct sd_lun *un);
   1605 static int sd_check_media(dev_t dev, enum dkio_state state);
   1606 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
   1607 static void sd_delayed_cv_broadcast(void *arg);
   1608 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag);
   1609 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag);
   1610 
   1611 static int sd_log_page_supported(sd_ssc_t *ssc, int log_page);
   1612 
   1613 /*
   1614  * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions.
   1615  */
   1616 static void sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag);
   1617 static int sd_wm_cache_constructor(void *wm, void *un, int flags);
   1618 static void sd_wm_cache_destructor(void *wm, void *un);
   1619 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb,
   1620 	daddr_t endb, ushort_t typ);
   1621 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb,
   1622 	daddr_t endb);
   1623 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp);
   1624 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm);
   1625 static void sd_read_modify_write_task(void * arg);
   1626 static int
   1627 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
   1628 	struct buf **bpp);
   1629 
   1630 
   1631 /*
   1632  * Function prototypes for failfast support.
   1633  */
   1634 static void sd_failfast_flushq(struct sd_lun *un);
   1635 static int sd_failfast_flushq_callback(struct buf *bp);
   1636 
   1637 /*
   1638  * Function prototypes to check for lsi devices
   1639  */
   1640 static void sd_is_lsi(struct sd_lun *un);
   1641 
   1642 /*
   1643  * Function prototypes for partial DMA support
   1644  */
   1645 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
   1646 		struct scsi_pkt *pkt, struct sd_xbuf *xp);
   1647 
   1648 
   1649 /* Function prototypes for cmlb */
   1650 static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
   1651     diskaddr_t start_block, size_t reqlength, void *tg_cookie);
   1652 
   1653 static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie);
   1654 
   1655 /*
   1656  * For printing RMW warning message timely
   1657  */
   1658 static void sd_rmw_msg_print_handler(void *arg);
   1659 
   1660 /*
   1661  * Constants for failfast support:
   1662  *
   1663  * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO
   1664  * failfast processing being performed.
   1665  *
   1666  * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing
   1667  * failfast processing on all bufs with B_FAILFAST set.
   1668  */
   1669 
   1670 #define	SD_FAILFAST_INACTIVE		0
   1671 #define	SD_FAILFAST_ACTIVE		1
   1672 
   1673 /*
   1674  * Bitmask to control behavior of buf(9S) flushes when a transition to
   1675  * the failfast state occurs. Optional bits include:
   1676  *
   1677  * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that
   1678  * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will
   1679  * be flushed.
   1680  *
   1681  * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the
   1682  * driver, in addition to the regular wait queue. This includes the xbuf
   1683  * queues. When clear, only the driver's wait queue will be flushed.
   1684  */
   1685 #define	SD_FAILFAST_FLUSH_ALL_BUFS	0x01
   1686 #define	SD_FAILFAST_FLUSH_ALL_QUEUES	0x02
   1687 
   1688 /*
   1689  * The default behavior is to only flush bufs that have B_FAILFAST set, but
   1690  * to flush all queues within the driver.
   1691  */
   1692 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES;
   1693 
   1694 
   1695 /*
   1696  * SD Testing Fault Injection
   1697  */
   1698 #ifdef SD_FAULT_INJECTION
   1699 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un);
   1700 static void sd_faultinjection(struct scsi_pkt *pktp);
   1701 static void sd_injection_log(char *buf, struct sd_lun *un);
   1702 #endif
   1703 
   1704 /*
   1705  * Device driver ops vector
   1706  */
   1707 static struct cb_ops sd_cb_ops = {
   1708 	sdopen,			/* open */
   1709 	sdclose,		/* close */
   1710 	sdstrategy,		/* strategy */
   1711 	nodev,			/* print */
   1712 	sddump,			/* dump */
   1713 	sdread,			/* read */
   1714 	sdwrite,		/* write */
   1715 	sdioctl,		/* ioctl */
   1716 	nodev,			/* devmap */
   1717 	nodev,			/* mmap */
   1718 	nodev,			/* segmap */
   1719 	nochpoll,		/* poll */
   1720 	sd_prop_op,		/* cb_prop_op */
   1721 	0,			/* streamtab  */
   1722 	D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */
   1723 	CB_REV,			/* cb_rev */
   1724 	sdaread, 		/* async I/O read entry point */
   1725 	sdawrite		/* async I/O write entry point */
   1726 };
   1727 
   1728 struct dev_ops sd_ops = {
   1729 	DEVO_REV,		/* devo_rev, */
   1730 	0,			/* refcnt  */
   1731 	sdinfo,			/* info */
   1732 	nulldev,		/* identify */
   1733 	sdprobe,		/* probe */
   1734 	sdattach,		/* attach */
   1735 	sddetach,		/* detach */
   1736 	nodev,			/* reset */
   1737 	&sd_cb_ops,		/* driver operations */
   1738 	NULL,			/* bus operations */
   1739 	sdpower,		/* power */
   1740 	ddi_quiesce_not_needed,		/* quiesce */
   1741 };
   1742 
   1743 /*
   1744  * This is the loadable module wrapper.
   1745  */
   1746 #include <sys/modctl.h>
   1747 
   1748 #ifndef XPV_HVM_DRIVER
   1749 static struct modldrv modldrv = {
   1750 	&mod_driverops,		/* Type of module. This one is a driver */
   1751 	SD_MODULE_NAME,		/* Module name. */
   1752 	&sd_ops			/* driver ops */
   1753 };
   1754 
   1755 static struct modlinkage modlinkage = {
   1756 	MODREV_1, &modldrv, NULL
   1757 };
   1758 
   1759 #else /* XPV_HVM_DRIVER */
   1760 static struct modlmisc modlmisc = {
   1761 	&mod_miscops,		/* Type of module. This one is a misc */
   1762 	"HVM " SD_MODULE_NAME,		/* Module name. */
   1763 };
   1764 
   1765 static struct modlinkage modlinkage = {
   1766 	MODREV_1, &modlmisc, NULL
   1767 };
   1768 
   1769 #endif /* XPV_HVM_DRIVER */
   1770 
   1771 static cmlb_tg_ops_t sd_tgops = {
   1772 	TG_DK_OPS_VERSION_1,
   1773 	sd_tg_rdwr,
   1774 	sd_tg_getinfo
   1775 };
   1776 
   1777 static struct scsi_asq_key_strings sd_additional_codes[] = {
   1778 	0x81, 0, "Logical Unit is Reserved",
   1779 	0x85, 0, "Audio Address Not Valid",
   1780 	0xb6, 0, "Media Load Mechanism Failed",
   1781 	0xB9, 0, "Audio Play Operation Aborted",
   1782 	0xbf, 0, "Buffer Overflow for Read All Subcodes Command",
   1783 	0x53, 2, "Medium removal prevented",
   1784 	0x6f, 0, "Authentication failed during key exchange",
   1785 	0x6f, 1, "Key not present",
   1786 	0x6f, 2, "Key not established",
   1787 	0x6f, 3, "Read without proper authentication",
   1788 	0x6f, 4, "Mismatched region to this logical unit",
   1789 	0x6f, 5, "Region reset count error",
   1790 	0xffff, 0x0, NULL
   1791 };
   1792 
   1793 
   1794 /*
   1795  * Struct for passing printing information for sense data messages
   1796  */
   1797 struct sd_sense_info {
   1798 	int	ssi_severity;
   1799 	int	ssi_pfa_flag;
   1800 };
   1801 
   1802 /*
   1803  * Table of function pointers for iostart-side routines. Separate "chains"
   1804  * of layered function calls are formed by placing the function pointers
   1805  * sequentially in the desired order. Functions are called according to an
   1806  * incrementing table index ordering. The last function in each chain must
   1807  * be sd_core_iostart(). The corresponding iodone-side routines are expected
   1808  * in the sd_iodone_chain[] array.
   1809  *
   1810  * Note: It may seem more natural to organize both the iostart and iodone
   1811  * functions together, into an array of structures (or some similar
   1812  * organization) with a common index, rather than two separate arrays which
   1813  * must be maintained in synchronization. The purpose of this division is
   1814  * to achieve improved performance: individual arrays allows for more
   1815  * effective cache line utilization on certain platforms.
   1816  */
   1817 
   1818 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp);
   1819 
   1820 
   1821 static sd_chain_t sd_iostart_chain[] = {
   1822 
   1823 	/* Chain for buf IO for disk drive targets (PM enabled) */
   1824 	sd_mapblockaddr_iostart,	/* Index: 0 */
   1825 	sd_pm_iostart,			/* Index: 1 */
   1826 	sd_core_iostart,		/* Index: 2 */
   1827 
   1828 	/* Chain for buf IO for disk drive targets (PM disabled) */
   1829 	sd_mapblockaddr_iostart,	/* Index: 3 */
   1830 	sd_core_iostart,		/* Index: 4 */
   1831 
   1832 	/*
   1833 	 * Chain for buf IO for removable-media or large sector size
   1834 	 * disk drive targets with RMW needed (PM enabled)
   1835 	 */
   1836 	sd_mapblockaddr_iostart,	/* Index: 5 */
   1837 	sd_mapblocksize_iostart,	/* Index: 6 */
   1838 	sd_pm_iostart,			/* Index: 7 */
   1839 	sd_core_iostart,		/* Index: 8 */
   1840 
   1841 	/*
   1842 	 * Chain for buf IO for removable-media or large sector size
   1843 	 * disk drive targets with RMW needed (PM disabled)
   1844 	 */
   1845 	sd_mapblockaddr_iostart,	/* Index: 9 */
   1846 	sd_mapblocksize_iostart,	/* Index: 10 */
   1847 	sd_core_iostart,		/* Index: 11 */
   1848 
   1849 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
   1850 	sd_mapblockaddr_iostart,	/* Index: 12 */
   1851 	sd_checksum_iostart,		/* Index: 13 */
   1852 	sd_pm_iostart,			/* Index: 14 */
   1853 	sd_core_iostart,		/* Index: 15 */
   1854 
   1855 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
   1856 	sd_mapblockaddr_iostart,	/* Index: 16 */
   1857 	sd_checksum_iostart,		/* Index: 17 */
   1858 	sd_core_iostart,		/* Index: 18 */
   1859 
   1860 	/* Chain for USCSI commands (all targets) */
   1861 	sd_pm_iostart,			/* Index: 19 */
   1862 	sd_core_iostart,		/* Index: 20 */
   1863 
   1864 	/* Chain for checksumming USCSI commands (all targets) */
   1865 	sd_checksum_uscsi_iostart,	/* Index: 21 */
   1866 	sd_pm_iostart,			/* Index: 22 */
   1867 	sd_core_iostart,		/* Index: 23 */
   1868 
   1869 	/* Chain for "direct" USCSI commands (all targets) */
   1870 	sd_core_iostart,		/* Index: 24 */
   1871 
   1872 	/* Chain for "direct priority" USCSI commands (all targets) */
   1873 	sd_core_iostart,		/* Index: 25 */
   1874 
   1875 	/*
   1876 	 * Chain for buf IO for large sector size disk drive targets
   1877 	 * with RMW needed with checksumming (PM enabled)
   1878 	 */
   1879 	sd_mapblockaddr_iostart,	/* Index: 26 */
   1880 	sd_mapblocksize_iostart,	/* Index: 27 */
   1881 	sd_checksum_iostart,		/* Index: 28 */
   1882 	sd_pm_iostart,			/* Index: 29 */
   1883 	sd_core_iostart,		/* Index: 30 */
   1884 
   1885 	/*
   1886 	 * Chain for buf IO for large sector size disk drive targets
   1887 	 * with RMW needed with checksumming (PM disabled)
   1888 	 */
   1889 	sd_mapblockaddr_iostart,	/* Index: 31 */
   1890 	sd_mapblocksize_iostart,	/* Index: 32 */
   1891 	sd_checksum_iostart,		/* Index: 33 */
   1892 	sd_core_iostart,		/* Index: 34 */
   1893 
   1894 };
   1895 
   1896 /*
   1897  * Macros to locate the first function of each iostart chain in the
   1898  * sd_iostart_chain[] array. These are located by the index in the array.
   1899  */
   1900 #define	SD_CHAIN_DISK_IOSTART			0
   1901 #define	SD_CHAIN_DISK_IOSTART_NO_PM		3
   1902 #define	SD_CHAIN_MSS_DISK_IOSTART		5
   1903 #define	SD_CHAIN_RMMEDIA_IOSTART		5
   1904 #define	SD_CHAIN_MSS_DISK_IOSTART_NO_PM		9
   1905 #define	SD_CHAIN_RMMEDIA_IOSTART_NO_PM		9
   1906 #define	SD_CHAIN_CHKSUM_IOSTART			12
   1907 #define	SD_CHAIN_CHKSUM_IOSTART_NO_PM		16
   1908 #define	SD_CHAIN_USCSI_CMD_IOSTART		19
   1909 #define	SD_CHAIN_USCSI_CHKSUM_IOSTART		21
   1910 #define	SD_CHAIN_DIRECT_CMD_IOSTART		24
   1911 #define	SD_CHAIN_PRIORITY_CMD_IOSTART		25
   1912 #define	SD_CHAIN_MSS_CHKSUM_IOSTART		26
   1913 #define	SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM	31
   1914 
   1915 
   1916 /*
   1917  * Table of function pointers for the iodone-side routines for the driver-
   1918  * internal layering mechanism.  The calling sequence for iodone routines
   1919  * uses a decrementing table index, so the last routine called in a chain
   1920  * must be at the lowest array index location for that chain.  The last
   1921  * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs)
   1922  * or sd_uscsi_iodone() (for uscsi IOs).  Other than this, the ordering
   1923  * of the functions in an iodone side chain must correspond to the ordering
   1924  * of the iostart routines for that chain.  Note that there is no iodone
   1925  * side routine that corresponds to sd_core_iostart(), so there is no
   1926  * entry in the table for this.
   1927  */
   1928 
   1929 static sd_chain_t sd_iodone_chain[] = {
   1930 
   1931 	/* Chain for buf IO for disk drive targets (PM enabled) */
   1932 	sd_buf_iodone,			/* Index: 0 */
   1933 	sd_mapblockaddr_iodone,		/* Index: 1 */
   1934 	sd_pm_iodone,			/* Index: 2 */
   1935 
   1936 	/* Chain for buf IO for disk drive targets (PM disabled) */
   1937 	sd_buf_iodone,			/* Index: 3 */
   1938 	sd_mapblockaddr_iodone,		/* Index: 4 */
   1939 
   1940 	/*
   1941 	 * Chain for buf IO for removable-media or large sector size
   1942 	 * disk drive targets with RMW needed (PM enabled)
   1943 	 */
   1944 	sd_buf_iodone,			/* Index: 5 */
   1945 	sd_mapblockaddr_iodone,		/* Index: 6 */
   1946 	sd_mapblocksize_iodone,		/* Index: 7 */
   1947 	sd_pm_iodone,			/* Index: 8 */
   1948 
   1949 	/*
   1950 	 * Chain for buf IO for removable-media or large sector size
   1951 	 * disk drive targets with RMW needed (PM disabled)
   1952 	 */
   1953 	sd_buf_iodone,			/* Index: 9 */
   1954 	sd_mapblockaddr_iodone,		/* Index: 10 */
   1955 	sd_mapblocksize_iodone,		/* Index: 11 */
   1956 
   1957 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
   1958 	sd_buf_iodone,			/* Index: 12 */
   1959 	sd_mapblockaddr_iodone,		/* Index: 13 */
   1960 	sd_checksum_iodone,		/* Index: 14 */
   1961 	sd_pm_iodone,			/* Index: 15 */
   1962 
   1963 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
   1964 	sd_buf_iodone,			/* Index: 16 */
   1965 	sd_mapblockaddr_iodone,		/* Index: 17 */
   1966 	sd_checksum_iodone,		/* Index: 18 */
   1967 
   1968 	/* Chain for USCSI commands (non-checksum targets) */
   1969 	sd_uscsi_iodone,		/* Index: 19 */
   1970 	sd_pm_iodone,			/* Index: 20 */
   1971 
   1972 	/* Chain for USCSI commands (checksum targets) */
   1973 	sd_uscsi_iodone,		/* Index: 21 */
   1974 	sd_checksum_uscsi_iodone,	/* Index: 22 */
   1975 	sd_pm_iodone,			/* Index: 22 */
   1976 
   1977 	/* Chain for "direct" USCSI commands (all targets) */
   1978 	sd_uscsi_iodone,		/* Index: 24 */
   1979 
   1980 	/* Chain for "direct priority" USCSI commands (all targets) */
   1981 	sd_uscsi_iodone,		/* Index: 25 */
   1982 
   1983 	/*
   1984 	 * Chain for buf IO for large sector size disk drive targets
   1985 	 * with checksumming (PM enabled)
   1986 	 */
   1987 	sd_buf_iodone,			/* Index: 26 */
   1988 	sd_mapblockaddr_iodone,		/* Index: 27 */
   1989 	sd_mapblocksize_iodone,		/* Index: 28 */
   1990 	sd_checksum_iodone,		/* Index: 29 */
   1991 	sd_pm_iodone,			/* Index: 30 */
   1992 
   1993 	/*
   1994 	 * Chain for buf IO for large sector size disk drive targets
   1995 	 * with checksumming (PM disabled)
   1996 	 */
   1997 	sd_buf_iodone,			/* Index: 31 */
   1998 	sd_mapblockaddr_iodone,		/* Index: 32 */
   1999 	sd_mapblocksize_iodone,		/* Index: 33 */
   2000 	sd_checksum_iodone,		/* Index: 34 */
   2001 };
   2002 
   2003 
   2004 /*
   2005  * Macros to locate the "first" function in the sd_iodone_chain[] array for
   2006  * each iodone-side chain. These are located by the array index, but as the
   2007  * iodone side functions are called in a decrementing-index order, the
   2008  * highest index number in each chain must be specified (as these correspond
   2009  * to the first function in the iodone chain that will be called by the core
   2010  * at IO completion time).
   2011  */
   2012 
   2013 #define	SD_CHAIN_DISK_IODONE			2
   2014 #define	SD_CHAIN_DISK_IODONE_NO_PM		4
   2015 #define	SD_CHAIN_RMMEDIA_IODONE			8
   2016 #define	SD_CHAIN_MSS_DISK_IODONE		8
   2017 #define	SD_CHAIN_RMMEDIA_IODONE_NO_PM		11
   2018 #define	SD_CHAIN_MSS_DISK_IODONE_NO_PM		11
   2019 #define	SD_CHAIN_CHKSUM_IODONE			15
   2020 #define	SD_CHAIN_CHKSUM_IODONE_NO_PM		18
   2021 #define	SD_CHAIN_USCSI_CMD_IODONE		20
   2022 #define	SD_CHAIN_USCSI_CHKSUM_IODONE		22
   2023 #define	SD_CHAIN_DIRECT_CMD_IODONE		24
   2024 #define	SD_CHAIN_PRIORITY_CMD_IODONE		25
   2025 #define	SD_CHAIN_MSS_CHKSUM_IODONE		30
   2026 #define	SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM	34
   2027 
   2028 
   2029 
   2030 /*
   2031  * Array to map a layering chain index to the appropriate initpkt routine.
   2032  * The redundant entries are present so that the index used for accessing
   2033  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
   2034  * with this table as well.
   2035  */
   2036 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **);
   2037 
   2038 static sd_initpkt_t	sd_initpkt_map[] = {
   2039 
   2040 	/* Chain for buf IO for disk drive targets (PM enabled) */
   2041 	sd_initpkt_for_buf,		/* Index: 0 */
   2042 	sd_initpkt_for_buf,		/* Index: 1 */
   2043 	sd_initpkt_for_buf,		/* Index: 2 */
   2044 
   2045 	/* Chain for buf IO for disk drive targets (PM disabled) */
   2046 	sd_initpkt_for_buf,		/* Index: 3 */
   2047 	sd_initpkt_for_buf,		/* Index: 4 */
   2048 
   2049 	/*
   2050 	 * Chain for buf IO for removable-media or large sector size
   2051 	 * disk drive targets (PM enabled)
   2052 	 */
   2053 	sd_initpkt_for_buf,		/* Index: 5 */
   2054 	sd_initpkt_for_buf,		/* Index: 6 */
   2055 	sd_initpkt_for_buf,		/* Index: 7 */
   2056 	sd_initpkt_for_buf,		/* Index: 8 */
   2057 
   2058 	/*
   2059 	 * Chain for buf IO for removable-media or large sector size
   2060 	 * disk drive targets (PM disabled)
   2061 	 */
   2062 	sd_initpkt_for_buf,		/* Index: 9 */
   2063 	sd_initpkt_for_buf,		/* Index: 10 */
   2064 	sd_initpkt_for_buf,		/* Index: 11 */
   2065 
   2066 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
   2067 	sd_initpkt_for_buf,		/* Index: 12 */
   2068 	sd_initpkt_for_buf,		/* Index: 13 */
   2069 	sd_initpkt_for_buf,		/* Index: 14 */
   2070 	sd_initpkt_for_buf,		/* Index: 15 */
   2071 
   2072 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
   2073 	sd_initpkt_for_buf,		/* Index: 16 */
   2074 	sd_initpkt_for_buf,		/* Index: 17 */
   2075 	sd_initpkt_for_buf,		/* Index: 18 */
   2076 
   2077 	/* Chain for USCSI commands (non-checksum targets) */
   2078 	sd_initpkt_for_uscsi,		/* Index: 19 */
   2079 	sd_initpkt_for_uscsi,		/* Index: 20 */
   2080 
   2081 	/* Chain for USCSI commands (checksum targets) */
   2082 	sd_initpkt_for_uscsi,		/* Index: 21 */
   2083 	sd_initpkt_for_uscsi,		/* Index: 22 */
   2084 	sd_initpkt_for_uscsi,		/* Index: 22 */
   2085 
   2086 	/* Chain for "direct" USCSI commands (all targets) */
   2087 	sd_initpkt_for_uscsi,		/* Index: 24 */
   2088 
   2089 	/* Chain for "direct priority" USCSI commands (all targets) */
   2090 	sd_initpkt_for_uscsi,		/* Index: 25 */
   2091 
   2092 	/*
   2093 	 * Chain for buf IO for large sector size disk drive targets
   2094 	 * with checksumming (PM enabled)
   2095 	 */
   2096 	sd_initpkt_for_buf,		/* Index: 26 */
   2097 	sd_initpkt_for_buf,		/* Index: 27 */
   2098 	sd_initpkt_for_buf,		/* Index: 28 */
   2099 	sd_initpkt_for_buf,		/* Index: 29 */
   2100 	sd_initpkt_for_buf,		/* Index: 30 */
   2101 
   2102 	/*
   2103 	 * Chain for buf IO for large sector size disk drive targets
   2104 	 * with checksumming (PM disabled)
   2105 	 */
   2106 	sd_initpkt_for_buf,		/* Index: 31 */
   2107 	sd_initpkt_for_buf,		/* Index: 32 */
   2108 	sd_initpkt_for_buf,		/* Index: 33 */
   2109 	sd_initpkt_for_buf,		/* Index: 34 */
   2110 };
   2111 
   2112 
   2113 /*
   2114  * Array to map a layering chain index to the appropriate destroypktpkt routine.
   2115  * The redundant entries are present so that the index used for accessing
   2116  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
   2117  * with this table as well.
   2118  */
   2119 typedef void (*sd_destroypkt_t)(struct buf *);
   2120 
   2121 static sd_destroypkt_t	sd_destroypkt_map[] = {
   2122 
   2123 	/* Chain for buf IO for disk drive targets (PM enabled) */
   2124 	sd_destroypkt_for_buf,		/* Index: 0 */
   2125 	sd_destroypkt_for_buf,		/* Index: 1 */
   2126 	sd_destroypkt_for_buf,		/* Index: 2 */
   2127 
   2128 	/* Chain for buf IO for disk drive targets (PM disabled) */
   2129 	sd_destroypkt_for_buf,		/* Index: 3 */
   2130 	sd_destroypkt_for_buf,		/* Index: 4 */
   2131 
   2132 	/*
   2133 	 * Chain for buf IO for removable-media or large sector size
   2134 	 * disk drive targets (PM enabled)
   2135 	 */
   2136 	sd_destroypkt_for_buf,		/* Index: 5 */
   2137 	sd_destroypkt_for_buf,		/* Index: 6 */
   2138 	sd_destroypkt_for_buf,		/* Index: 7 */
   2139 	sd_destroypkt_for_buf,		/* Index: 8 */
   2140 
   2141 	/*
   2142 	 * Chain for buf IO for removable-media or large sector size
   2143 	 * disk drive targets (PM disabled)
   2144 	 */
   2145 	sd_destroypkt_for_buf,		/* Index: 9 */
   2146 	sd_destroypkt_for_buf,		/* Index: 10 */
   2147 	sd_destroypkt_for_buf,		/* Index: 11 */
   2148 
   2149 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
   2150 	sd_destroypkt_for_buf,		/* Index: 12 */
   2151 	sd_destroypkt_for_buf,		/* Index: 13 */
   2152 	sd_destroypkt_for_buf,		/* Index: 14 */
   2153 	sd_destroypkt_for_buf,		/* Index: 15 */
   2154 
   2155 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
   2156 	sd_destroypkt_for_buf,		/* Index: 16 */
   2157 	sd_destroypkt_for_buf,		/* Index: 17 */
   2158 	sd_destroypkt_for_buf,		/* Index: 18 */
   2159 
   2160 	/* Chain for USCSI commands (non-checksum targets) */
   2161 	sd_destroypkt_for_uscsi,	/* Index: 19 */
   2162 	sd_destroypkt_for_uscsi,	/* Index: 20 */
   2163 
   2164 	/* Chain for USCSI commands (checksum targets) */
   2165 	sd_destroypkt_for_uscsi,	/* Index: 21 */
   2166 	sd_destroypkt_for_uscsi,	/* Index: 22 */
   2167 	sd_destroypkt_for_uscsi,	/* Index: 22 */
   2168 
   2169 	/* Chain for "direct" USCSI commands (all targets) */
   2170 	sd_destroypkt_for_uscsi,	/* Index: 24 */
   2171 
   2172 	/* Chain for "direct priority" USCSI commands (all targets) */
   2173 	sd_destroypkt_for_uscsi,	/* Index: 25 */
   2174 
   2175 	/*
   2176 	 * Chain for buf IO for large sector size disk drive targets
   2177 	 * with checksumming (PM disabled)
   2178 	 */
   2179 	sd_destroypkt_for_buf,		/* Index: 26 */
   2180 	sd_destroypkt_for_buf,		/* Index: 27 */
   2181 	sd_destroypkt_for_buf,		/* Index: 28 */
   2182 	sd_destroypkt_for_buf,		/* Index: 29 */
   2183 	sd_destroypkt_for_buf,		/* Index: 30 */
   2184 
   2185 	/*
   2186 	 * Chain for buf IO for large sector size disk drive targets
   2187 	 * with checksumming (PM enabled)
   2188 	 */
   2189 	sd_destroypkt_for_buf,		/* Index: 31 */
   2190 	sd_destroypkt_for_buf,		/* Index: 32 */
   2191 	sd_destroypkt_for_buf,		/* Index: 33 */
   2192 	sd_destroypkt_for_buf,		/* Index: 34 */
   2193 };
   2194 
   2195 
   2196 
   2197 /*
   2198  * Array to map a layering chain index to the appropriate chain "type".
   2199  * The chain type indicates a specific property/usage of the chain.
   2200  * The redundant entries are present so that the index used for accessing
   2201  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
   2202  * with this table as well.
   2203  */
   2204 
   2205 #define	SD_CHAIN_NULL			0	/* for the special RQS cmd */
   2206 #define	SD_CHAIN_BUFIO			1	/* regular buf IO */
   2207 #define	SD_CHAIN_USCSI			2	/* regular USCSI commands */
   2208 #define	SD_CHAIN_DIRECT			3	/* uscsi, w/ bypass power mgt */
   2209 #define	SD_CHAIN_DIRECT_PRIORITY	4	/* uscsi, w/ bypass power mgt */
   2210 						/* (for error recovery) */
   2211 
   2212 static int sd_chain_type_map[] = {
   2213 
   2214 	/* Chain for buf IO for disk drive targets (PM enabled) */
   2215 	SD_CHAIN_BUFIO,			/* Index: 0 */
   2216 	SD_CHAIN_BUFIO,			/* Index: 1 */
   2217 	SD_CHAIN_BUFIO,			/* Index: 2 */
   2218 
   2219 	/* Chain for buf IO for disk drive targets (PM disabled) */
   2220 	SD_CHAIN_BUFIO,			/* Index: 3 */
   2221 	SD_CHAIN_BUFIO,			/* Index: 4 */
   2222 
   2223 	/*
   2224 	 * Chain for buf IO for removable-media or large sector size
   2225 	 * disk drive targets (PM enabled)
   2226 	 */
   2227 	SD_CHAIN_BUFIO,			/* Index: 5 */
   2228 	SD_CHAIN_BUFIO,			/* Index: 6 */
   2229 	SD_CHAIN_BUFIO,			/* Index: 7 */
   2230 	SD_CHAIN_BUFIO,			/* Index: 8 */
   2231 
   2232 	/*
   2233 	 * Chain for buf IO for removable-media or large sector size
   2234 	 * disk drive targets (PM disabled)
   2235 	 */
   2236 	SD_CHAIN_BUFIO,			/* Index: 9 */
   2237 	SD_CHAIN_BUFIO,			/* Index: 10 */
   2238 	SD_CHAIN_BUFIO,			/* Index: 11 */
   2239 
   2240 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
   2241 	SD_CHAIN_BUFIO,			/* Index: 12 */
   2242 	SD_CHAIN_BUFIO,			/* Index: 13 */
   2243 	SD_CHAIN_BUFIO,			/* Index: 14 */
   2244 	SD_CHAIN_BUFIO,			/* Index: 15 */
   2245 
   2246 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
   2247 	SD_CHAIN_BUFIO,			/* Index: 16 */
   2248 	SD_CHAIN_BUFIO,			/* Index: 17 */
   2249 	SD_CHAIN_BUFIO,			/* Index: 18 */
   2250 
   2251 	/* Chain for USCSI commands (non-checksum targets) */
   2252 	SD_CHAIN_USCSI,			/* Index: 19 */
   2253 	SD_CHAIN_USCSI,			/* Index: 20 */
   2254 
   2255 	/* Chain for USCSI commands (checksum targets) */
   2256 	SD_CHAIN_USCSI,			/* Index: 21 */
   2257 	SD_CHAIN_USCSI,			/* Index: 22 */
   2258 	SD_CHAIN_USCSI,			/* Index: 23 */
   2259 
   2260 	/* Chain for "direct" USCSI commands (all targets) */
   2261 	SD_CHAIN_DIRECT,		/* Index: 24 */
   2262 
   2263 	/* Chain for "direct priority" USCSI commands (all targets) */
   2264 	SD_CHAIN_DIRECT_PRIORITY,	/* Index: 25 */
   2265 
   2266 	/*
   2267 	 * Chain for buf IO for large sector size disk drive targets
   2268 	 * with checksumming (PM enabled)
   2269 	 */
   2270 	SD_CHAIN_BUFIO,			/* Index: 26 */
   2271 	SD_CHAIN_BUFIO,			/* Index: 27 */
   2272 	SD_CHAIN_BUFIO,			/* Index: 28 */
   2273 	SD_CHAIN_BUFIO,			/* Index: 29 */
   2274 	SD_CHAIN_BUFIO,			/* Index: 30 */
   2275 
   2276 	/*
   2277 	 * Chain for buf IO for large sector size disk drive targets
   2278 	 * with checksumming (PM disabled)
   2279 	 */
   2280 	SD_CHAIN_BUFIO,			/* Index: 31 */
   2281 	SD_CHAIN_BUFIO,			/* Index: 32 */
   2282 	SD_CHAIN_BUFIO,			/* Index: 33 */
   2283 	SD_CHAIN_BUFIO,			/* Index: 34 */
   2284 };
   2285 
   2286 
   2287 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */
   2288 #define	SD_IS_BUFIO(xp)			\
   2289 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO)
   2290 
   2291 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */
   2292 #define	SD_IS_DIRECT_PRIORITY(xp)	\
   2293 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY)
   2294 
   2295 
   2296 
   2297 /*
   2298  * Struct, array, and macros to map a specific chain to the appropriate
   2299  * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays.
   2300  *
   2301  * The sd_chain_index_map[] array is used at attach time to set the various
   2302  * un_xxx_chain type members of the sd_lun softstate to the specific layering
   2303  * chain to be used with the instance. This allows different instances to use
   2304  * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart
   2305  * and xb_chain_iodone index values in the sd_xbuf are initialized to these
   2306  * values at sd_xbuf init time, this allows (1) layering chains may be changed
   2307  * dynamically & without the use of locking; and (2) a layer may update the
   2308  * xb_chain_io[start|done] member in a given xbuf with its current index value,
   2309  * to allow for deferred processing of an IO within the same chain from a
   2310  * different execution context.
   2311  */
   2312 
   2313 struct sd_chain_index {
   2314 	int	sci_iostart_index;
   2315 	int	sci_iodone_index;
   2316 };
   2317 
   2318 static struct sd_chain_index	sd_chain_index_map[] = {
   2319 	{ SD_CHAIN_DISK_IOSTART,		SD_CHAIN_DISK_IODONE },
   2320 	{ SD_CHAIN_DISK_IOSTART_NO_PM,		SD_CHAIN_DISK_IODONE_NO_PM },
   2321 	{ SD_CHAIN_RMMEDIA_IOSTART,		SD_CHAIN_RMMEDIA_IODONE },
   2322 	{ SD_CHAIN_RMMEDIA_IOSTART_NO_PM,	SD_CHAIN_RMMEDIA_IODONE_NO_PM },
   2323 	{ SD_CHAIN_CHKSUM_IOSTART,		SD_CHAIN_CHKSUM_IODONE },
   2324 	{ SD_CHAIN_CHKSUM_IOSTART_NO_PM,	SD_CHAIN_CHKSUM_IODONE_NO_PM },
   2325 	{ SD_CHAIN_USCSI_CMD_IOSTART,		SD_CHAIN_USCSI_CMD_IODONE },
   2326 	{ SD_CHAIN_USCSI_CHKSUM_IOSTART,	SD_CHAIN_USCSI_CHKSUM_IODONE },
   2327 	{ SD_CHAIN_DIRECT_CMD_IOSTART,		SD_CHAIN_DIRECT_CMD_IODONE },
   2328 	{ SD_CHAIN_PRIORITY_CMD_IOSTART,	SD_CHAIN_PRIORITY_CMD_IODONE },
   2329 	{ SD_CHAIN_MSS_CHKSUM_IOSTART,		SD_CHAIN_MSS_CHKSUM_IODONE },
   2330 	{ SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM, SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM },
   2331 
   2332 };
   2333 
   2334 
   2335 /*
   2336  * The following are indexes into the sd_chain_index_map[] array.
   2337  */
   2338 
   2339 /* un->un_buf_chain_type must be set to one of these */
   2340 #define	SD_CHAIN_INFO_DISK		0
   2341 #define	SD_CHAIN_INFO_DISK_NO_PM	1
   2342 #define	SD_CHAIN_INFO_RMMEDIA		2
   2343 #define	SD_CHAIN_INFO_MSS_DISK		2
   2344 #define	SD_CHAIN_INFO_RMMEDIA_NO_PM	3
   2345 #define	SD_CHAIN_INFO_MSS_DSK_NO_PM	3
   2346 #define	SD_CHAIN_INFO_CHKSUM		4
   2347 #define	SD_CHAIN_INFO_CHKSUM_NO_PM	5
   2348 #define	SD_CHAIN_INFO_MSS_DISK_CHKSUM	10
   2349 #define	SD_CHAIN_INFO_MSS_DISK_CHKSUM_NO_PM	11
   2350 
   2351 /* un->un_uscsi_chain_type must be set to one of these */
   2352 #define	SD_CHAIN_INFO_USCSI_CMD		6
   2353 /* USCSI with PM disabled is the same as DIRECT */
   2354 #define	SD_CHAIN_INFO_USCSI_CMD_NO_PM	8
   2355 #define	SD_CHAIN_INFO_USCSI_CHKSUM	7
   2356 
   2357 /* un->un_direct_chain_type must be set to one of these */
   2358 #define	SD_CHAIN_INFO_DIRECT_CMD	8
   2359 
   2360 /* un->un_priority_chain_type must be set to one of these */
   2361 #define	SD_CHAIN_INFO_PRIORITY_CMD	9
   2362 
   2363 /* size for devid inquiries */
   2364 #define	MAX_INQUIRY_SIZE		0xF0
   2365 
   2366 /*
   2367  * Macros used by functions to pass a given buf(9S) struct along to the
   2368  * next function in the layering chain for further processing.
   2369  *
   2370  * In the following macros, passing more than three arguments to the called
   2371  * routines causes the optimizer for the SPARC compiler to stop doing tail
   2372  * call elimination which results in significant performance degradation.
   2373  */
   2374 #define	SD_BEGIN_IOSTART(index, un, bp)	\
   2375 	((*(sd_iostart_chain[index]))(index, un, bp))
   2376 
   2377 #define	SD_BEGIN_IODONE(index, un, bp)	\
   2378 	((*(sd_iodone_chain[index]))(index, un, bp))
   2379 
   2380 #define	SD_NEXT_IOSTART(index, un, bp)				\
   2381 	((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp))
   2382 
   2383 #define	SD_NEXT_IODONE(index, un, bp)				\
   2384 	((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp))
   2385 
   2386 /*
   2387  *    Function: _init
   2388  *
   2389  * Description: This is the driver _init(9E) entry point.
   2390  *
   2391  * Return Code: Returns the value from mod_install(9F) or
   2392  *		ddi_soft_state_init(9F) as appropriate.
   2393  *
   2394  *     Context: Called when driver module loaded.
   2395  */
   2396 
   2397 int
   2398 _init(void)
   2399 {
   2400 	int	err;
   2401 
   2402 	/* establish driver name from module name */
   2403 	sd_label = (char *)mod_modname(&modlinkage);
   2404 
   2405 #ifndef XPV_HVM_DRIVER
   2406 	err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun),
   2407 	    SD_MAXUNIT);
   2408 	if (err != 0) {
   2409 		return (err);
   2410 	}
   2411 
   2412 #else /* XPV_HVM_DRIVER */
   2413 	/* Remove the leading "hvm_" from the module name */
   2414 	ASSERT(strncmp(sd_label, "hvm_", strlen("hvm_")) == 0);
   2415 	sd_label += strlen("hvm_");
   2416 
   2417 #endif /* XPV_HVM_DRIVER */
   2418 
   2419 	mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL);
   2420 	mutex_init(&sd_log_mutex,    NULL, MUTEX_DRIVER, NULL);
   2421 	mutex_init(&sd_label_mutex,  NULL, MUTEX_DRIVER, NULL);
   2422 
   2423 	mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL);
   2424 	cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL);
   2425 	cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL);
   2426 
   2427 	/*
   2428 	 * it's ok to init here even for fibre device
   2429 	 */
   2430 	sd_scsi_probe_cache_init();
   2431 
   2432 	sd_scsi_target_lun_init();
   2433 
   2434 	/*
   2435 	 * Creating taskq before mod_install ensures that all callers (threads)
   2436 	 * that enter the module after a successful mod_install encounter
   2437 	 * a valid taskq.
   2438 	 */
   2439 	sd_taskq_create();
   2440 
   2441 	err = mod_install(&modlinkage);
   2442 	if (err != 0) {
   2443 		/* delete taskq if install fails */
   2444 		sd_taskq_delete();
   2445 
   2446 		mutex_destroy(&sd_detach_mutex);
   2447 		mutex_destroy(&sd_log_mutex);
   2448 		mutex_destroy(&sd_label_mutex);
   2449 
   2450 		mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
   2451 		cv_destroy(&sd_tr.srq_resv_reclaim_cv);
   2452 		cv_destroy(&sd_tr.srq_inprocess_cv);
   2453 
   2454 		sd_scsi_probe_cache_fini();
   2455 
   2456 		sd_scsi_target_lun_fini();
   2457 
   2458 #ifndef XPV_HVM_DRIVER
   2459 		ddi_soft_state_fini(&sd_state);
   2460 #endif /* !XPV_HVM_DRIVER */
   2461 		return (err);
   2462 	}
   2463 
   2464 	return (err);
   2465 }
   2466 
   2467 
   2468 /*
   2469  *    Function: _fini
   2470  *
   2471  * Description: This is the driver _fini(9E) entry point.
   2472  *
   2473  * Return Code: Returns the value from mod_remove(9F)
   2474  *
   2475  *     Context: Called when driver module is unloaded.
   2476  */
   2477 
   2478 int
   2479 _fini(void)
   2480 {
   2481 	int err;
   2482 
   2483 	if ((err = mod_remove(&modlinkage)) != 0) {
   2484 		return (err);
   2485 	}
   2486 
   2487 	sd_taskq_delete();
   2488 
   2489 	mutex_destroy(&sd_detach_mutex);
   2490 	mutex_destroy(&sd_log_mutex);
   2491 	mutex_destroy(&sd_label_mutex);
   2492 	mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
   2493 
   2494 	sd_scsi_probe_cache_fini();
   2495 
   2496 	sd_scsi_target_lun_fini();
   2497 
   2498 	cv_destroy(&sd_tr.srq_resv_reclaim_cv);
   2499 	cv_destroy(&sd_tr.srq_inprocess_cv);
   2500 
   2501 #ifndef XPV_HVM_DRIVER
   2502 	ddi_soft_state_fini(&sd_state);
   2503 #endif /* !XPV_HVM_DRIVER */
   2504 
   2505 	return (err);
   2506 }
   2507 
   2508 
   2509 /*
   2510  *    Function: _info
   2511  *
   2512  * Description: This is the driver _info(9E) entry point.
   2513  *
   2514  *   Arguments: modinfop - pointer to the driver modinfo structure
   2515  *
   2516  * Return Code: Returns the value from mod_info(9F).
   2517  *
   2518  *     Context: Kernel thread context
   2519  */
   2520 
   2521 int
   2522 _info(struct modinfo *modinfop)
   2523 {
   2524 	return (mod_info(&modlinkage, modinfop));
   2525 }
   2526 
   2527 
   2528 /*
   2529  * The following routines implement the driver message logging facility.
   2530  * They provide component- and level- based debug output filtering.
   2531  * Output may also be restricted to messages for a single instance by
   2532  * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set
   2533  * to NULL, then messages for all instances are printed.
   2534  *
   2535  * These routines have been cloned from each other due to the language
   2536  * constraints of macros and variable argument list processing.
   2537  */
   2538 
   2539 
   2540 /*
   2541  *    Function: sd_log_err
   2542  *
   2543  * Description: This routine is called by the SD_ERROR macro for debug
   2544  *		logging of error conditions.
   2545  *
   2546  *   Arguments: comp - driver component being logged
   2547  *		dev  - pointer to driver info structure
   2548  *		fmt  - error string and format to be logged
   2549  */
   2550 
   2551 static void
   2552 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...)
   2553 {
   2554 	va_list		ap;
   2555 	dev_info_t	*dev;
   2556 
   2557 	ASSERT(un != NULL);
   2558 	dev = SD_DEVINFO(un);
   2559 	ASSERT(dev != NULL);
   2560 
   2561 	/*
   2562 	 * Filter messages based on the global component and level masks.
   2563 	 * Also print if un matches the value of sd_debug_un, or if
   2564 	 * sd_debug_un is set to NULL.
   2565 	 */
   2566 	if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) &&
   2567 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
   2568 		mutex_enter(&sd_log_mutex);
   2569 		va_start(ap, fmt);
   2570 		(void) vsprintf(sd_log_buf, fmt, ap);
   2571 		va_end(ap);
   2572 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
   2573 		mutex_exit(&sd_log_mutex);
   2574 	}
   2575 #ifdef SD_FAULT_INJECTION
   2576 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
   2577 	if (un->sd_injection_mask & comp) {
   2578 		mutex_enter(&sd_log_mutex);
   2579 		va_start(ap, fmt);
   2580 		(void) vsprintf(sd_log_buf, fmt, ap);
   2581 		va_end(ap);
   2582 		sd_injection_log(sd_log_buf, un);
   2583 		mutex_exit(&sd_log_mutex);
   2584 	}
   2585 #endif
   2586 }
   2587 
   2588 
   2589 /*
   2590  *    Function: sd_log_info
   2591  *
   2592  * Description: This routine is called by the SD_INFO macro for debug
   2593  *		logging of general purpose informational conditions.
   2594  *
   2595  *   Arguments: comp - driver component being logged
   2596  *		dev  - pointer to driver info structure
   2597  *		fmt  - info string and format to be logged
   2598  */
   2599 
   2600 static void
   2601 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...)
   2602 {
   2603 	va_list		ap;
   2604 	dev_info_t	*dev;
   2605 
   2606 	ASSERT(un != NULL);
   2607 	dev = SD_DEVINFO(un);
   2608 	ASSERT(dev != NULL);
   2609 
   2610 	/*
   2611 	 * Filter messages based on the global component and level masks.
   2612 	 * Also print if un matches the value of sd_debug_un, or if
   2613 	 * sd_debug_un is set to NULL.
   2614 	 */
   2615 	if ((sd_component_mask & component) &&
   2616 	    (sd_level_mask & SD_LOGMASK_INFO) &&
   2617 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
   2618 		mutex_enter(&sd_log_mutex);
   2619 		va_start(ap, fmt);
   2620 		(void) vsprintf(sd_log_buf, fmt, ap);
   2621 		va_end(ap);
   2622 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
   2623 		mutex_exit(&sd_log_mutex);
   2624 	}
   2625 #ifdef SD_FAULT_INJECTION
   2626 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
   2627 	if (un->sd_injection_mask & component) {
   2628 		mutex_enter(&sd_log_mutex);
   2629 		va_start(ap, fmt);
   2630 		(void) vsprintf(sd_log_buf, fmt, ap);
   2631 		va_end(ap);
   2632 		sd_injection_log(sd_log_buf, un);
   2633 		mutex_exit(&sd_log_mutex);
   2634 	}
   2635 #endif
   2636 }
   2637 
   2638 
   2639 /*
   2640  *    Function: sd_log_trace
   2641  *
   2642  * Description: This routine is called by the SD_TRACE macro for debug
   2643  *		logging of trace conditions (i.e. function entry/exit).
   2644  *
   2645  *   Arguments: comp - driver component being logged
   2646  *		dev  - pointer to driver info structure
   2647  *		fmt  - trace string and format to be logged
   2648  */
   2649 
   2650 static void
   2651 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...)
   2652 {
   2653 	va_list		ap;
   2654 	dev_info_t	*dev;
   2655 
   2656 	ASSERT(un != NULL);
   2657 	dev = SD_DEVINFO(un);
   2658 	ASSERT(dev != NULL);
   2659 
   2660 	/*
   2661 	 * Filter messages based on the global component and level masks.
   2662 	 * Also print if un matches the value of sd_debug_un, or if
   2663 	 * sd_debug_un is set to NULL.
   2664 	 */
   2665 	if ((sd_component_mask & component) &&
   2666 	    (sd_level_mask & SD_LOGMASK_TRACE) &&
   2667 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
   2668 		mutex_enter(&sd_log_mutex);
   2669 		va_start(ap, fmt);
   2670 		(void) vsprintf(sd_log_buf, fmt, ap);
   2671 		va_end(ap);
   2672 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
   2673 		mutex_exit(&sd_log_mutex);
   2674 	}
   2675 #ifdef SD_FAULT_INJECTION
   2676 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
   2677 	if (un->sd_injection_mask & component) {
   2678 		mutex_enter(&sd_log_mutex);
   2679 		va_start(ap, fmt);
   2680 		(void) vsprintf(sd_log_buf, fmt, ap);
   2681 		va_end(ap);
   2682 		sd_injection_log(sd_log_buf, un);
   2683 		mutex_exit(&sd_log_mutex);
   2684 	}
   2685 #endif
   2686 }
   2687 
   2688 
   2689 /*
   2690  *    Function: sdprobe
   2691  *
   2692  * Description: This is the driver probe(9e) entry point function.
   2693  *
   2694  *   Arguments: devi - opaque device info handle
   2695  *
   2696  * Return Code: DDI_PROBE_SUCCESS: If the probe was successful.
   2697  *              DDI_PROBE_FAILURE: If the probe failed.
   2698  *              DDI_PROBE_PARTIAL: If the instance is not present now,
   2699  *				   but may be present in the future.
   2700  */
   2701 
   2702 static int
   2703 sdprobe(dev_info_t *devi)
   2704 {
   2705 	struct scsi_device	*devp;
   2706 	int			rval;
   2707 #ifndef XPV_HVM_DRIVER
   2708 	int			instance = ddi_get_instance(devi);
   2709 #endif /* !XPV_HVM_DRIVER */
   2710 
   2711 	/*
   2712 	 * if it wasn't for pln, sdprobe could actually be nulldev
   2713 	 * in the "__fibre" case.
   2714 	 */
   2715 	if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
   2716 		return (DDI_PROBE_DONTCARE);
   2717 	}
   2718 
   2719 	devp = ddi_get_driver_private(devi);
   2720 
   2721 	if (devp == NULL) {
   2722 		/* Ooops... nexus driver is mis-configured... */
   2723 		return (DDI_PROBE_FAILURE);
   2724 	}
   2725 
   2726 #ifndef XPV_HVM_DRIVER
   2727 	if (ddi_get_soft_state(sd_state, instance) != NULL) {
   2728 		return (DDI_PROBE_PARTIAL);
   2729 	}
   2730 #endif /* !XPV_HVM_DRIVER */
   2731 
   2732 	/*
   2733 	 * Call the SCSA utility probe routine to see if we actually
   2734 	 * have a target at this SCSI nexus.
   2735 	 */
   2736 	switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) {
   2737 	case SCSIPROBE_EXISTS:
   2738 		switch (devp->sd_inq->inq_dtype) {
   2739 		case DTYPE_DIRECT:
   2740 			rval = DDI_PROBE_SUCCESS;
   2741 			break;
   2742 		case DTYPE_RODIRECT:
   2743 			/* CDs etc. Can be removable media */
   2744 			rval = DDI_PROBE_SUCCESS;
   2745 			break;
   2746 		case DTYPE_OPTICAL:
   2747 			/*
   2748 			 * Rewritable optical driver HP115AA
   2749 			 * Can also be removable media
   2750 			 */
   2751 
   2752 			/*
   2753 			 * Do not attempt to bind to  DTYPE_OPTICAL if
   2754 			 * pre solaris 9 sparc sd behavior is required
   2755 			 *
   2756 			 * If first time through and sd_dtype_optical_bind
   2757 			 * has not been set in /etc/system check properties
   2758 			 */
   2759 
   2760 			if (sd_dtype_optical_bind  < 0) {
   2761 				sd_dtype_optical_bind = ddi_prop_get_int
   2762 				    (DDI_DEV_T_ANY, devi, 0,
   2763 				    "optical-device-bind", 1);
   2764 			}
   2765 
   2766 			if (sd_dtype_optical_bind == 0) {
   2767 				rval = DDI_PROBE_FAILURE;
   2768 			} else {
   2769 				rval = DDI_PROBE_SUCCESS;
   2770 			}
   2771 			break;
   2772 
   2773 		case DTYPE_NOTPRESENT:
   2774 		default:
   2775 			rval = DDI_PROBE_FAILURE;
   2776 			break;
   2777 		}
   2778 		break;
   2779 	default:
   2780 		rval = DDI_PROBE_PARTIAL;
   2781 		break;
   2782 	}
   2783 
   2784 	/*
   2785 	 * This routine checks for resource allocation prior to freeing,
   2786 	 * so it will take care of the "smart probing" case where a
   2787 	 * scsi_probe() may or may not have been issued and will *not*
   2788 	 * free previously-freed resources.
   2789 	 */
   2790 	scsi_unprobe(devp);
   2791 	return (rval);
   2792 }
   2793 
   2794 
   2795 /*
   2796  *    Function: sdinfo
   2797  *
   2798  * Description: This is the driver getinfo(9e) entry point function.
   2799  * 		Given the device number, return the devinfo pointer from
   2800  *		the scsi_device structure or the instance number
   2801  *		associated with the dev_t.
   2802  *
   2803  *   Arguments: dip     - pointer to device info structure
   2804  *		infocmd - command argument (DDI_INFO_DEVT2DEVINFO,
   2805  *			  DDI_INFO_DEVT2INSTANCE)
   2806  *		arg     - driver dev_t
   2807  *		resultp - user buffer for request response
   2808  *
   2809  * Return Code: DDI_SUCCESS
   2810  *              DDI_FAILURE
   2811  */
   2812 /* ARGSUSED */
   2813 static int
   2814 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
   2815 {
   2816 	struct sd_lun	*un;
   2817 	dev_t		dev;
   2818 	int		instance;
   2819 	int		error;
   2820 
   2821 	switch (infocmd) {
   2822 	case DDI_INFO_DEVT2DEVINFO:
   2823 		dev = (dev_t)arg;
   2824 		instance = SDUNIT(dev);
   2825 		if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
   2826 			return (DDI_FAILURE);
   2827 		}
   2828 		*result = (void *) SD_DEVINFO(un);
   2829 		error = DDI_SUCCESS;
   2830 		break;
   2831 	case DDI_INFO_DEVT2INSTANCE:
   2832 		dev = (dev_t)arg;
   2833 		instance = SDUNIT(dev);
   2834 		*result = (void *)(uintptr_t)instance;
   2835 		error = DDI_SUCCESS;
   2836 		break;
   2837 	default:
   2838 		error = DDI_FAILURE;
   2839 	}
   2840 	return (error);
   2841 }
   2842 
   2843 /*
   2844  *    Function: sd_prop_op
   2845  *
   2846  * Description: This is the driver prop_op(9e) entry point function.
   2847  *		Return the number of blocks for the partition in question
   2848  *		or forward the request to the property facilities.
   2849  *
   2850  *   Arguments: dev       - device number
   2851  *		dip       - pointer to device info structure
   2852  *		prop_op   - property operator
   2853  *		mod_flags - DDI_PROP_DONTPASS, don't pass to parent
   2854  *		name      - pointer to property name
   2855  *		valuep    - pointer or address of the user buffer
   2856  *		lengthp   - property length
   2857  *
   2858  * Return Code: DDI_PROP_SUCCESS
   2859  *              DDI_PROP_NOT_FOUND
   2860  *              DDI_PROP_UNDEFINED
   2861  *              DDI_PROP_NO_MEMORY
   2862  *              DDI_PROP_BUF_TOO_SMALL
   2863  */
   2864 
   2865 static int
   2866 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
   2867 	char *name, caddr_t valuep, int *lengthp)
   2868 {
   2869 	struct sd_lun	*un;
   2870 
   2871 	if ((un = ddi_get_soft_state(sd_state, ddi_get_instance(dip))) == NULL)
   2872 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
   2873 		    name, valuep, lengthp));
   2874 
   2875 	return (cmlb_prop_op(un->un_cmlbhandle,
   2876 	    dev, dip, prop_op, mod_flags, name, valuep, lengthp,
   2877 	    SDPART(dev), (void *)SD_PATH_DIRECT));
   2878 }
   2879 
   2880 /*
   2881  * The following functions are for smart probing:
   2882  * sd_scsi_probe_cache_init()
   2883  * sd_scsi_probe_cache_fini()
   2884  * sd_scsi_clear_probe_cache()
   2885  * sd_scsi_probe_with_cache()
   2886  */
   2887 
   2888 /*
   2889  *    Function: sd_scsi_probe_cache_init
   2890  *
   2891  * Description: Initializes the probe response cache mutex and head pointer.
   2892  *
   2893  *     Context: Kernel thread context
   2894  */
   2895 
   2896 static void
   2897 sd_scsi_probe_cache_init(void)
   2898 {
   2899 	mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL);
   2900 	sd_scsi_probe_cache_head = NULL;
   2901 }
   2902 
   2903 
   2904 /*
   2905  *    Function: sd_scsi_probe_cache_fini
   2906  *
   2907  * Description: Frees all resources associated with the probe response cache.
   2908  *
   2909  *     Context: Kernel thread context
   2910  */
   2911 
   2912 static void
   2913 sd_scsi_probe_cache_fini(void)
   2914 {
   2915 	struct sd_scsi_probe_cache *cp;
   2916 	struct sd_scsi_probe_cache *ncp;
   2917 
   2918 	/* Clean up our smart probing linked list */
   2919 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) {
   2920 		ncp = cp->next;
   2921 		kmem_free(cp, sizeof (struct sd_scsi_probe_cache));
   2922 	}
   2923 	sd_scsi_probe_cache_head = NULL;
   2924 	mutex_destroy(&sd_scsi_probe_cache_mutex);
   2925 }
   2926 
   2927 
   2928 /*
   2929  *    Function: sd_scsi_clear_probe_cache
   2930  *
   2931  * Description: This routine clears the probe response cache. This is
   2932  *		done when open() returns ENXIO so that when deferred
   2933  *		attach is attempted (possibly after a device has been
   2934  *		turned on) we will retry the probe. Since we don't know
   2935  *		which target we failed to open, we just clear the
   2936  *		entire cache.
   2937  *
   2938  *     Context: Kernel thread context
   2939  */
   2940 
   2941 static void
   2942 sd_scsi_clear_probe_cache(void)
   2943 {
   2944 	struct sd_scsi_probe_cache	*cp;
   2945 	int				i;
   2946 
   2947 	mutex_enter(&sd_scsi_probe_cache_mutex);
   2948 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
   2949 		/*
   2950 		 * Reset all entries to SCSIPROBE_EXISTS.  This will
   2951 		 * force probing to be performed the next time
   2952 		 * sd_scsi_probe_with_cache is called.
   2953 		 */
   2954 		for (i = 0; i < NTARGETS_WIDE; i++) {
   2955 			cp->cache[i] = SCSIPROBE_EXISTS;
   2956 		}
   2957 	}
   2958 	mutex_exit(&sd_scsi_probe_cache_mutex);
   2959 }
   2960 
   2961 
   2962 /*
   2963  *    Function: sd_scsi_probe_with_cache
   2964  *
   2965  * Description: This routine implements support for a scsi device probe
   2966  *		with cache. The driver maintains a cache of the target
   2967  *		responses to scsi probes. If we get no response from a
   2968  *		target during a probe inquiry, we remember that, and we
   2969  *		avoid additional calls to scsi_probe on non-zero LUNs
   2970  *		on the same target until the cache is cleared. By doing
   2971  *		so we avoid the 1/4 sec selection timeout for nonzero
   2972  *		LUNs. lun0 of a target is always probed.
   2973  *
   2974  *   Arguments: devp     - Pointer to a scsi_device(9S) structure
   2975  *              waitfunc - indicates what the allocator routines should
   2976  *			   do when resources are not available. This value
   2977  *			   is passed on to scsi_probe() when that routine
   2978  *			   is called.
   2979  *
   2980  * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache;
   2981  *		otherwise the value returned by scsi_probe(9F).
   2982  *
   2983  *     Context: Kernel thread context
   2984  */
   2985 
   2986 static int
   2987 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)())
   2988 {
   2989 	struct sd_scsi_probe_cache	*cp;
   2990 	dev_info_t	*pdip = ddi_get_parent(devp->sd_dev);
   2991 	int		lun, tgt;
   2992 
   2993 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
   2994 	    SCSI_ADDR_PROP_LUN, 0);
   2995 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
   2996 	    SCSI_ADDR_PROP_TARGET, -1);
   2997 
   2998 	/* Make sure caching enabled and target in range */
   2999 	if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) {
   3000 		/* do it the old way (no cache) */
   3001 		return (scsi_probe(devp, waitfn));
   3002 	}
   3003 
   3004 	mutex_enter(&sd_scsi_probe_cache_mutex);
   3005 
   3006 	/* Find the cache for this scsi bus instance */
   3007 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
   3008 		if (cp->pdip == pdip) {
   3009 			break;
   3010 		}
   3011 	}
   3012 
   3013 	/* If we can't find a cache for this pdip, create one */
   3014 	if (cp == NULL) {
   3015 		int i;
   3016 
   3017 		cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache),
   3018 		    KM_SLEEP);
   3019 		cp->pdip = pdip;
   3020 		cp->next = sd_scsi_probe_cache_head;
   3021 		sd_scsi_probe_cache_head = cp;
   3022 		for (i = 0; i < NTARGETS_WIDE; i++) {
   3023 			cp->cache[i] = SCSIPROBE_EXISTS;
   3024 		}
   3025 	}
   3026 
   3027 	mutex_exit(&sd_scsi_probe_cache_mutex);
   3028 
   3029 	/* Recompute the cache for this target if LUN zero */
   3030 	if (lun == 0) {
   3031 		cp->cache[tgt] = SCSIPROBE_EXISTS;
   3032 	}
   3033 
   3034 	/* Don't probe if cache remembers a NORESP from a previous LUN. */
   3035 	if (cp->cache[tgt] != SCSIPROBE_EXISTS) {
   3036 		return (SCSIPROBE_NORESP);
   3037 	}
   3038 
   3039 	/* Do the actual probe; save & return the result */
   3040 	return (cp->cache[tgt] = scsi_probe(devp, waitfn));
   3041 }
   3042 
   3043 
   3044 /*
   3045  *    Function: sd_scsi_target_lun_init
   3046  *
   3047  * Description: Initializes the attached lun chain mutex and head pointer.
   3048  *
   3049  *     Context: Kernel thread context
   3050  */
   3051 
   3052 static void
   3053 sd_scsi_target_lun_init(void)
   3054 {
   3055 	mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL);
   3056 	sd_scsi_target_lun_head = NULL;
   3057 }
   3058 
   3059 
   3060 /*
   3061  *    Function: sd_scsi_target_lun_fini
   3062  *
   3063  * Description: Frees all resources associated with the attached lun
   3064  *              chain
   3065  *
   3066  *     Context: Kernel thread context
   3067  */
   3068 
   3069 static void
   3070 sd_scsi_target_lun_fini(void)
   3071 {
   3072 	struct sd_scsi_hba_tgt_lun	*cp;
   3073 	struct sd_scsi_hba_tgt_lun	*ncp;
   3074 
   3075 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) {
   3076 		ncp = cp->next;
   3077 		kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun));
   3078 	}
   3079 	sd_scsi_target_lun_head = NULL;
   3080 	mutex_destroy(&sd_scsi_target_lun_mutex);
   3081 }
   3082 
   3083 
   3084 /*
   3085  *    Function: sd_scsi_get_target_lun_count
   3086  *
   3087  * Description: This routine will check in the attached lun chain to see
   3088  * 		how many luns are attached on the required SCSI controller
   3089  * 		and target. Currently, some capabilities like tagged queue
   3090  *		are supported per target based by HBA. So all luns in a
   3091  *		target have the same capabilities. Based on this assumption,
   3092  * 		sd should only set these capabilities once per target. This
   3093  *		function is called when sd needs to decide how many luns
   3094  *		already attached on a target.
   3095  *
   3096  *   Arguments: dip	- Pointer to the system's dev_info_t for the SCSI
   3097  *			  controller device.
   3098  *              target	- The target ID on the controller's SCSI bus.
   3099  *
   3100  * Return Code: The number of luns attached on the required target and
   3101  *		controller.
   3102  *		-1 if target ID is not in parallel SCSI scope or the given
   3103  * 		dip is not in the chain.
   3104  *
   3105  *     Context: Kernel thread context
   3106  */
   3107 
   3108 static int
   3109 sd_scsi_get_target_lun_count(dev_info_t *dip, int target)
   3110 {
   3111 	struct sd_scsi_hba_tgt_lun	*cp;
   3112 
   3113 	if ((target < 0) || (target >= NTARGETS_WIDE)) {
   3114 		return (-1);
   3115 	}
   3116 
   3117 	mutex_enter(&sd_scsi_target_lun_mutex);
   3118 
   3119 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
   3120 		if (cp->pdip == dip) {
   3121 			break;
   3122 		}
   3123 	}
   3124 
   3125 	mutex_exit(&sd_scsi_target_lun_mutex);
   3126 
   3127 	if (cp == NULL) {
   3128 		return (-1);
   3129 	}
   3130 
   3131 	return (cp->nlun[target]);
   3132 }
   3133 
   3134 
   3135 /*
   3136  *    Function: sd_scsi_update_lun_on_target
   3137  *
   3138  * Description: This routine is used to update the attached lun chain when a
   3139  *		lun is attached or detached on a target.
   3140  *
   3141  *   Arguments: dip     - Pointer to the system's dev_info_t for the SCSI
   3142  *                        controller device.
   3143  *              target  - The target ID on the controller's SCSI bus.
   3144  *		flag	- Indicate the lun is attached or detached.
   3145  *
   3146  *     Context: Kernel thread context
   3147  */
   3148 
   3149 static void
   3150 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag)
   3151 {
   3152 	struct sd_scsi_hba_tgt_lun	*cp;
   3153 
   3154 	mutex_enter(&sd_scsi_target_lun_mutex);
   3155 
   3156 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
   3157 		if (cp->pdip == dip) {
   3158 			break;
   3159 		}
   3160 	}
   3161 
   3162 	if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) {
   3163 		cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun),
   3164 		    KM_SLEEP);
   3165 		cp->pdip = dip;
   3166 		cp->next = sd_scsi_target_lun_head;
   3167 		sd_scsi_target_lun_head = cp;
   3168 	}
   3169 
   3170 	mutex_exit(&sd_scsi_target_lun_mutex);
   3171 
   3172 	if (cp != NULL) {
   3173 		if (flag == SD_SCSI_LUN_ATTACH) {
   3174 			cp->nlun[target] ++;
   3175 		} else {
   3176 			cp->nlun[target] --;
   3177 		}
   3178 	}
   3179 }
   3180 
   3181 
   3182 /*
   3183  *    Function: sd_spin_up_unit
   3184  *
   3185  * Description: Issues the following commands to spin-up the device:
   3186  *		START STOP UNIT, and INQUIRY.
   3187  *
   3188  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
   3189  *                      structure for this target.
   3190  *
   3191  * Return Code: 0 - success
   3192  *		EIO - failure
   3193  *		EACCES - reservation conflict
   3194  *
   3195  *     Context: Kernel thread context
   3196  */
   3197 
   3198 static int
   3199 sd_spin_up_unit(sd_ssc_t *ssc)
   3200 {
   3201 	size_t	resid		= 0;
   3202 	int	has_conflict	= FALSE;
   3203 	uchar_t *bufaddr;
   3204 	int 	status;
   3205 	struct sd_lun	*un;
   3206 
   3207 	ASSERT(ssc != NULL);
   3208 	un = ssc->ssc_un;
   3209 	ASSERT(un != NULL);
   3210 
   3211 	/*
   3212 	 * Send a throwaway START UNIT command.
   3213 	 *
   3214 	 * If we fail on this, we don't care presently what precisely
   3215 	 * is wrong.  EMC's arrays will also fail this with a check
   3216 	 * condition (0x2/0x4/0x3) if the device is "inactive," but
   3217 	 * we don't want to fail the attach because it may become
   3218 	 * "active" later.
   3219 	 * We don't know if power condition is supported or not at
   3220 	 * this stage, use START STOP bit.
   3221 	 */
   3222 	status = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
   3223 	    SD_TARGET_START, SD_PATH_DIRECT);
   3224 
   3225 	if (status != 0) {
   3226 		if (status == EACCES)
   3227 			has_conflict = TRUE;
   3228 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   3229 	}
   3230 
   3231 	/*
   3232 	 * Send another INQUIRY command to the target. This is necessary for
   3233 	 * non-removable media direct access devices because their INQUIRY data
   3234 	 * may not be fully qualified until they are spun up (perhaps via the
   3235 	 * START command above).  Note: This seems to be needed for some
   3236 	 * legacy devices only.) The INQUIRY command should succeed even if a
   3237 	 * Reservation Conflict is present.
   3238 	 */
   3239 	bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP);
   3240 
   3241 	if (sd_send_scsi_INQUIRY(ssc, bufaddr, SUN_INQSIZE, 0, 0, &resid)
   3242 	    != 0) {
   3243 		kmem_free(bufaddr, SUN_INQSIZE);
   3244 		sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
   3245 		return (EIO);
   3246 	}
   3247 
   3248 	/*
   3249 	 * If we got enough INQUIRY data, copy it over the old INQUIRY data.
   3250 	 * Note that this routine does not return a failure here even if the
   3251 	 * INQUIRY command did not return any data.  This is a legacy behavior.
   3252 	 */
   3253 	if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) {
   3254 		bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE);
   3255 	}
   3256 
   3257 	kmem_free(bufaddr, SUN_INQSIZE);
   3258 
   3259 	/* If we hit a reservation conflict above, tell the caller. */
   3260 	if (has_conflict == TRUE) {
   3261 		return (EACCES);
   3262 	}
   3263 
   3264 	return (0);
   3265 }
   3266 
   3267 #ifdef _LP64
   3268 /*
   3269  *    Function: sd_enable_descr_sense
   3270  *
   3271  * Description: This routine attempts to select descriptor sense format
   3272  *		using the Control mode page.  Devices that support 64 bit
   3273  *		LBAs (for >2TB luns) should also implement descriptor
   3274  *		sense data so we will call this function whenever we see
   3275  *		a lun larger than 2TB.  If for some reason the device
   3276  *		supports 64 bit LBAs but doesn't support descriptor sense
   3277  *		presumably the mode select will fail.  Everything will
   3278  *		continue to work normally except that we will not get
   3279  *		complete sense data for commands that fail with an LBA
   3280  *		larger than 32 bits.
   3281  *
   3282  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
   3283  *                      structure for this target.
   3284  *
   3285  *     Context: Kernel thread context only
   3286  */
   3287 
   3288 static void
   3289 sd_enable_descr_sense(sd_ssc_t *ssc)
   3290 {
   3291 	uchar_t			*header;
   3292 	struct mode_control_scsi3 *ctrl_bufp;
   3293 	size_t			buflen;
   3294 	size_t			bd_len;
   3295 	int			status;
   3296 	struct sd_lun		*un;
   3297 
   3298 	ASSERT(ssc != NULL);
   3299 	un = ssc->ssc_un;
   3300 	ASSERT(un != NULL);
   3301 
   3302 	/*
   3303 	 * Read MODE SENSE page 0xA, Control Mode Page
   3304 	 */
   3305 	buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH +
   3306 	    sizeof (struct mode_control_scsi3);
   3307 	header = kmem_zalloc(buflen, KM_SLEEP);
   3308 
   3309 	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
   3310 	    MODEPAGE_CTRL_MODE, SD_PATH_DIRECT);
   3311 
   3312 	if (status != 0) {
   3313 		SD_ERROR(SD_LOG_COMMON, un,
   3314 		    "sd_enable_descr_sense: mode sense ctrl page failed\n");
   3315 		goto eds_exit;
   3316 	}
   3317 
   3318 	/*
   3319 	 * Determine size of Block Descriptors in order to locate
   3320 	 * the mode page data. ATAPI devices return 0, SCSI devices
   3321 	 * should return MODE_BLK_DESC_LENGTH.
   3322 	 */
   3323 	bd_len  = ((struct mode_header *)header)->bdesc_length;
   3324 
   3325 	/* Clear the mode data length field for MODE SELECT */
   3326 	((struct mode_header *)header)->length = 0;
   3327 
   3328 	ctrl_bufp = (struct mode_control_scsi3 *)
   3329 	    (header + MODE_HEADER_LENGTH + bd_len);
   3330 
   3331 	/*
   3332 	 * If the page length is smaller than the expected value,
   3333 	 * the target device doesn't support D_SENSE. Bail out here.
   3334 	 */
   3335 	if (ctrl_bufp->mode_page.length <
   3336 	    sizeof (struct mode_control_scsi3) - 2) {
   3337 		SD_ERROR(SD_LOG_COMMON, un,
   3338 		    "sd_enable_descr_sense: enable D_SENSE failed\n");
   3339 		goto eds_exit;
   3340 	}
   3341 
   3342 	/*
   3343 	 * Clear PS bit for MODE SELECT
   3344 	 */
   3345 	ctrl_bufp->mode_page.ps = 0;
   3346 
   3347 	/*
   3348 	 * Set D_SENSE to enable descriptor sense format.
   3349 	 */
   3350 	ctrl_bufp->d_sense = 1;
   3351 
   3352 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   3353 
   3354 	/*
   3355 	 * Use MODE SELECT to commit the change to the D_SENSE bit
   3356 	 */
   3357 	status = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header,
   3358 	    buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT);
   3359 
   3360 	if (status != 0) {
   3361 		SD_INFO(SD_LOG_COMMON, un,
   3362 		    "sd_enable_descr_sense: mode select ctrl page failed\n");
   3363 	} else {
   3364 		kmem_free(header, buflen);
   3365 		return;
   3366 	}
   3367 
   3368 eds_exit:
   3369 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   3370 	kmem_free(header, buflen);
   3371 }
   3372 
   3373 /*
   3374  *    Function: sd_reenable_dsense_task
   3375  *
   3376  * Description: Re-enable descriptor sense after device or bus reset
   3377  *
   3378  *     Context: Executes in a taskq() thread context
   3379  */
   3380 static void
   3381 sd_reenable_dsense_task(void *arg)
   3382 {
   3383 	struct	sd_lun	*un = arg;
   3384 	sd_ssc_t	*ssc;
   3385 
   3386 	ASSERT(un != NULL);
   3387 
   3388 	ssc = sd_ssc_init(un);
   3389 	sd_enable_descr_sense(ssc);
   3390 	sd_ssc_fini(ssc);
   3391 }
   3392 #endif /* _LP64 */
   3393 
   3394 /*
   3395  *    Function: sd_set_mmc_caps
   3396  *
   3397  * Description: This routine determines if the device is MMC compliant and if
   3398  *		the device supports CDDA via a mode sense of the CDVD
   3399  *		capabilities mode page. Also checks if the device is a
   3400  *		dvdram writable device.
   3401  *
   3402  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
   3403  *                      structure for this target.
   3404  *
   3405  *     Context: Kernel thread context only
   3406  */
   3407 
   3408 static void
   3409 sd_set_mmc_caps(sd_ssc_t *ssc)
   3410 {
   3411 	struct mode_header_grp2		*sense_mhp;
   3412 	uchar_t				*sense_page;
   3413 	caddr_t				buf;
   3414 	int				bd_len;
   3415 	int				status;
   3416 	struct uscsi_cmd		com;
   3417 	int				rtn;
   3418 	uchar_t				*out_data_rw, *out_data_hd;
   3419 	uchar_t				*rqbuf_rw, *rqbuf_hd;
   3420 	uchar_t				*out_data_gesn;
   3421 	int				gesn_len;
   3422 	struct sd_lun			*un;
   3423 
   3424 	ASSERT(ssc != NULL);
   3425 	un = ssc->ssc_un;
   3426 	ASSERT(un != NULL);
   3427 
   3428 	/*
   3429 	 * The flags which will be set in this function are - mmc compliant,
   3430 	 * dvdram writable device, cdda support. Initialize them to FALSE
   3431 	 * and if a capability is detected - it will be set to TRUE.
   3432 	 */
   3433 	un->un_f_mmc_cap = FALSE;
   3434 	un->un_f_dvdram_writable_device = FALSE;
   3435 	un->un_f_cfg_cdda = FALSE;
   3436 
   3437 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
   3438 	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
   3439 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
   3440 
   3441 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   3442 
   3443 	if (status != 0) {
   3444 		/* command failed; just return */
   3445 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
   3446 		return;
   3447 	}
   3448 	/*
   3449 	 * If the mode sense request for the CDROM CAPABILITIES
   3450 	 * page (0x2A) succeeds the device is assumed to be MMC.
   3451 	 */
   3452 	un->un_f_mmc_cap = TRUE;
   3453 
   3454 	/* See if GET STATUS EVENT NOTIFICATION is supported */
   3455 	if (un->un_f_mmc_gesn_polling) {
   3456 		gesn_len = SD_GESN_HEADER_LEN + SD_GESN_MEDIA_DATA_LEN;
   3457 		out_data_gesn = kmem_zalloc(gesn_len, KM_SLEEP);
   3458 
   3459 		rtn = sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(ssc,
   3460 		    out_data_gesn, gesn_len, 1 << SD_GESN_MEDIA_CLASS);
   3461 
   3462 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   3463 
   3464 		if ((rtn != 0) || !sd_gesn_media_data_valid(out_data_gesn)) {
   3465 			un->un_f_mmc_gesn_polling = FALSE;
   3466 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   3467 			    "sd_set_mmc_caps: gesn not supported "
   3468 			    "%d %x %x %x %x\n", rtn,
   3469 			    out_data_gesn[0], out_data_gesn[1],
   3470 			    out_data_gesn[2], out_data_gesn[3]);
   3471 		}
   3472 
   3473 		kmem_free(out_data_gesn, gesn_len);
   3474 	}
   3475 
   3476 	/* Get to the page data */
   3477 	sense_mhp = (struct mode_header_grp2 *)buf;
   3478 	bd_len = (sense_mhp->bdesc_length_hi << 8) |
   3479 	    sense_mhp->bdesc_length_lo;
   3480 	if (bd_len > MODE_BLK_DESC_LENGTH) {
   3481 		/*
   3482 		 * We did not get back the expected block descriptor
   3483 		 * length so we cannot determine if the device supports
   3484 		 * CDDA. However, we still indicate the device is MMC
   3485 		 * according to the successful response to the page
   3486 		 * 0x2A mode sense request.
   3487 		 */
   3488 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
   3489 		    "sd_set_mmc_caps: Mode Sense returned "
   3490 		    "invalid block descriptor length\n");
   3491 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
   3492 		return;
   3493 	}
   3494 
   3495 	/* See if read CDDA is supported */
   3496 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 +
   3497 	    bd_len);
   3498 	un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE;
   3499 
   3500 	/* See if writing DVD RAM is supported. */
   3501 	un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE;
   3502 	if (un->un_f_dvdram_writable_device == TRUE) {
   3503 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
   3504 		return;
   3505 	}
   3506 
   3507 	/*
   3508 	 * If the device presents DVD or CD capabilities in the mode
   3509 	 * page, we can return here since a RRD will not have
   3510 	 * these capabilities.
   3511 	 */
   3512 	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
   3513 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
   3514 		return;
   3515 	}
   3516 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
   3517 
   3518 	/*
   3519 	 * If un->un_f_dvdram_writable_device is still FALSE,
   3520 	 * check for a Removable Rigid Disk (RRD).  A RRD
   3521 	 * device is identified by the features RANDOM_WRITABLE and
   3522 	 * HARDWARE_DEFECT_MANAGEMENT.
   3523 	 */
   3524 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
   3525 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
   3526 
   3527 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
   3528 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
   3529 	    RANDOM_WRITABLE, SD_PATH_STANDARD);
   3530 
   3531 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   3532 
   3533 	if (rtn != 0) {
   3534 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
   3535 		kmem_free(rqbuf_rw, SENSE_LENGTH);
   3536 		return;
   3537 	}
   3538 
   3539 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
   3540 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
   3541 
   3542 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
   3543 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
   3544 	    HARDWARE_DEFECT_MANAGEMENT, SD_PATH_STANDARD);
   3545 
   3546 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   3547 
   3548 	if (rtn == 0) {
   3549 		/*
   3550 		 * We have good information, check for random writable
   3551 		 * and hardware defect features.
   3552 		 */
   3553 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
   3554 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) {
   3555 			un->un_f_dvdram_writable_device = TRUE;
   3556 		}
   3557 	}
   3558 
   3559 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
   3560 	kmem_free(rqbuf_rw, SENSE_LENGTH);
   3561 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
   3562 	kmem_free(rqbuf_hd, SENSE_LENGTH);
   3563 }
   3564 
   3565 /*
   3566  *    Function: sd_check_for_writable_cd
   3567  *
   3568  * Description: This routine determines if the media in the device is
   3569  *		writable or not. It uses the get configuration command (0x46)
   3570  *		to determine if the media is writable
   3571  *
   3572  *   Arguments: un - driver soft state (unit) structure
   3573  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct"
   3574  *                           chain and the normal command waitq, or
   3575  *                           SD_PATH_DIRECT_PRIORITY to use the USCSI
   3576  *                           "direct" chain and bypass the normal command
   3577  *                           waitq.
   3578  *
   3579  *     Context: Never called at interrupt context.
   3580  */
   3581 
   3582 static void
   3583 sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag)
   3584 {
   3585 	struct uscsi_cmd		com;
   3586 	uchar_t				*out_data;
   3587 	uchar_t				*rqbuf;
   3588 	int				rtn;
   3589 	uchar_t				*out_data_rw, *out_data_hd;
   3590 	uchar_t				*rqbuf_rw, *rqbuf_hd;
   3591 	struct mode_header_grp2		*sense_mhp;
   3592 	uchar_t				*sense_page;
   3593 	caddr_t				buf;
   3594 	int				bd_len;
   3595 	int				status;
   3596 	struct sd_lun			*un;
   3597 
   3598 	ASSERT(ssc != NULL);
   3599 	un = ssc->ssc_un;
   3600 	ASSERT(un != NULL);
   3601 	ASSERT(mutex_owned(SD_MUTEX(un)));
   3602 
   3603 	/*
   3604 	 * Initialize the writable media to false, if configuration info.
   3605 	 * tells us otherwise then only we will set it.
   3606 	 */
   3607 	un->un_f_mmc_writable_media = FALSE;
   3608 	mutex_exit(SD_MUTEX(un));
   3609 
   3610 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
   3611 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
   3612 
   3613 	rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, SENSE_LENGTH,
   3614 	    out_data, SD_PROFILE_HEADER_LEN, path_flag);
   3615 
   3616 	if (rtn != 0)
   3617 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   3618 
   3619 	mutex_enter(SD_MUTEX(un));
   3620 	if (rtn == 0) {
   3621 		/*
   3622 		 * We have good information, check for writable DVD.
   3623 		 */
   3624 		if ((out_data[6] == 0) && (out_data[7] == 0x12)) {
   3625 			un->un_f_mmc_writable_media = TRUE;
   3626 			kmem_free(out_data, SD_PROFILE_HEADER_LEN);
   3627 			kmem_free(rqbuf, SENSE_LENGTH);
   3628 			return;
   3629 		}
   3630 	}
   3631 
   3632 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
   3633 	kmem_free(rqbuf, SENSE_LENGTH);
   3634 
   3635 	/*
   3636 	 * Determine if this is a RRD type device.
   3637 	 */
   3638 	mutex_exit(SD_MUTEX(un));
   3639 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
   3640 	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
   3641 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, path_flag);
   3642 
   3643 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   3644 
   3645 	mutex_enter(SD_MUTEX(un));
   3646 	if (status != 0) {
   3647 		/* command failed; just return */
   3648 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
   3649 		return;
   3650 	}
   3651 
   3652 	/* Get to the page data */
   3653 	sense_mhp = (struct mode_header_grp2 *)buf;
   3654 	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
   3655 	if (bd_len > MODE_BLK_DESC_LENGTH) {
   3656 		/*
   3657 		 * We did not get back the expected block descriptor length so
   3658 		 * we cannot check the mode page.
   3659 		 */
   3660 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
   3661 		    "sd_check_for_writable_cd: Mode Sense returned "
   3662 		    "invalid block descriptor length\n");
   3663 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
   3664 		return;
   3665 	}
   3666 
   3667 	/*
   3668 	 * If the device presents DVD or CD capabilities in the mode
   3669 	 * page, we can return here since a RRD device will not have
   3670 	 * these capabilities.
   3671 	 */
   3672 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len);
   3673 	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
   3674 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
   3675 		return;
   3676 	}
   3677 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
   3678 
   3679 	/*
   3680 	 * If un->un_f_mmc_writable_media is still FALSE,
   3681 	 * check for RRD type media.  A RRD device is identified
   3682 	 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT.
   3683 	 */
   3684 	mutex_exit(SD_MUTEX(un));
   3685 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
   3686 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
   3687 
   3688 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
   3689 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
   3690 	    RANDOM_WRITABLE, path_flag);
   3691 
   3692 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   3693 	if (rtn != 0) {
   3694 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
   3695 		kmem_free(rqbuf_rw, SENSE_LENGTH);
   3696 		mutex_enter(SD_MUTEX(un));
   3697 		return;
   3698 	}
   3699 
   3700 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
   3701 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
   3702 
   3703 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
   3704 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
   3705 	    HARDWARE_DEFECT_MANAGEMENT, path_flag);
   3706 
   3707 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   3708 	mutex_enter(SD_MUTEX(un));
   3709 	if (rtn == 0) {
   3710 		/*
   3711 		 * We have good information, check for random writable
   3712 		 * and hardware defect features as current.
   3713 		 */
   3714 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
   3715 		    (out_data_rw[10] & 0x1) &&
   3716 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) &&
   3717 		    (out_data_hd[10] & 0x1)) {
   3718 			un->un_f_mmc_writable_media = TRUE;
   3719 		}
   3720 	}
   3721 
   3722 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
   3723 	kmem_free(rqbuf_rw, SENSE_LENGTH);
   3724 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
   3725 	kmem_free(rqbuf_hd, SENSE_LENGTH);
   3726 }
   3727 
   3728 /*
   3729  *    Function: sd_read_unit_properties
   3730  *
   3731  * Description: The following implements a property lookup mechanism.
   3732  *		Properties for particular disks (keyed on vendor, model
   3733  *		and rev numbers) are sought in the sd.conf file via
   3734  *		sd_process_sdconf_file(), and if not found there, are
   3735  *		looked for in a list hardcoded in this driver via
   3736  *		sd_process_sdconf_table() Once located the properties
   3737  *		are used to update the driver unit structure.
   3738  *
   3739  *   Arguments: un - driver soft state (unit) structure
   3740  */
   3741 
   3742 static void
   3743 sd_read_unit_properties(struct sd_lun *un)
   3744 {
   3745 	/*
   3746 	 * sd_process_sdconf_file returns SD_FAILURE if it cannot find
   3747 	 * the "sd-config-list" property (from the sd.conf file) or if
   3748 	 * there was not a match for the inquiry vid/pid. If this event
   3749 	 * occurs the static driver configuration table is searched for
   3750 	 * a match.
   3751 	 */
   3752 	ASSERT(un != NULL);
   3753 	if (sd_process_sdconf_file(un) == SD_FAILURE) {
   3754 		sd_process_sdconf_table(un);
   3755 	}
   3756 
   3757 	/* check for LSI device */
   3758 	sd_is_lsi(un);
   3759 
   3760 
   3761 }
   3762 
   3763 
   3764 /*
   3765  *    Function: sd_process_sdconf_file
   3766  *
   3767  * Description: Use ddi_prop_lookup(9F) to obtain the properties from the
   3768  *		driver's config file (ie, sd.conf) and update the driver
   3769  *		soft state structure accordingly.
   3770  *
   3771  *   Arguments: un - driver soft state (unit) structure
   3772  *
   3773  * Return Code: SD_SUCCESS - The properties were successfully set according
   3774  *			     to the driver configuration file.
   3775  *		SD_FAILURE - The driver config list was not obtained or
   3776  *			     there was no vid/pid match. This indicates that
   3777  *			     the static config table should be used.
   3778  *
   3779  * The config file has a property, "sd-config-list". Currently we support
   3780  * two kinds of formats. For both formats, the value of this property
   3781  * is a list of duplets:
   3782  *
   3783  *  sd-config-list=
   3784  *	<duplet>,
   3785  *	[,<duplet>]*;
   3786  *
   3787  * For the improved format, where
   3788  *
   3789  *     <duplet>:= "<vid+pid>","<tunable-list>"
   3790  *
   3791  * and
   3792  *
   3793  *     <tunable-list>:=   <tunable> [, <tunable> ]*;
   3794  *     <tunable> =        <name> : <value>
   3795  *
   3796  * The <vid+pid> is the string that is returned by the target device on a
   3797  * SCSI inquiry command, the <tunable-list> contains one or more tunables
   3798  * to apply to all target devices with the specified <vid+pid>.
   3799  *
   3800  * Each <tunable> is a "<name> : <value>" pair.
   3801  *
   3802  * For the old format, the structure of each duplet is as follows:
   3803  *
   3804  *  <duplet>:= "<vid+pid>","<data-property-name_list>"
   3805  *
   3806  * The first entry of the duplet is the device ID string (the concatenated
   3807  * vid & pid; not to be confused with a device_id).  This is defined in
   3808  * the same way as in the sd_disk_table.
   3809  *
   3810  * The second part of the duplet is a string that identifies a
   3811  * data-property-name-list. The data-property-name-list is defined as
   3812  * follows:
   3813  *
   3814  *  <data-property-name-list>:=<data-property-name> [<data-property-name>]
   3815  *
   3816  * The syntax of <data-property-name> depends on the <version> field.
   3817  *
   3818  * If version = SD_CONF_VERSION_1 we have the following syntax:
   3819  *
   3820  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
   3821  *
   3822  * where the prop0 value will be used to set prop0 if bit0 set in the
   3823  * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1
   3824  *
   3825  */
   3826 
   3827 static int
   3828 sd_process_sdconf_file(struct sd_lun *un)
   3829 {
   3830 	char	**config_list = NULL;
   3831 	uint_t	nelements;
   3832 	char	*vidptr;
   3833 	int	vidlen;
   3834 	char	*dnlist_ptr;
   3835 	char	*dataname_ptr;
   3836 	char	*dataname_lasts;
   3837 	int	*data_list = NULL;
   3838 	uint_t	data_list_len;
   3839 	int	rval = SD_FAILURE;
   3840 	int	i;
   3841 
   3842 	ASSERT(un != NULL);
   3843 
   3844 	/* Obtain the configuration list associated with the .conf file */
   3845 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, SD_DEVINFO(un),
   3846 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, sd_config_list,
   3847 	    &config_list, &nelements) != DDI_PROP_SUCCESS) {
   3848 		return (SD_FAILURE);
   3849 	}
   3850 
   3851 	/*
   3852 	 * Compare vids in each duplet to the inquiry vid - if a match is
   3853 	 * made, get the data value and update the soft state structure
   3854 	 * accordingly.
   3855 	 *
   3856 	 * Each duplet should show as a pair of strings, return SD_FAILURE
   3857 	 * otherwise.
   3858 	 */
   3859 	if (nelements & 1) {
   3860 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
   3861 		    "sd-config-list should show as pairs of strings.\n");
   3862 		if (config_list)
   3863 			ddi_prop_free(config_list);
   3864 		return (SD_FAILURE);
   3865 	}
   3866 
   3867 	for (i = 0; i < nelements; i += 2) {
   3868 		/*
   3869 		 * Note: The assumption here is that each vid entry is on
   3870 		 * a unique line from its associated duplet.
   3871 		 */
   3872 		vidptr = config_list[i];
   3873 		vidlen = (int)strlen(vidptr);
   3874 		if ((vidlen == 0) ||
   3875 		    (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) {
   3876 			continue;
   3877 		}
   3878 
   3879 		/*
   3880 		 * dnlist contains 1 or more blank separated
   3881 		 * data-property-name entries
   3882 		 */
   3883 		dnlist_ptr = config_list[i + 1];
   3884 
   3885 		if (strchr(dnlist_ptr, ':') != NULL) {
   3886 			/*
   3887 			 * Decode the improved format sd-config-list.
   3888 			 */
   3889 			sd_nvpair_str_decode(un, dnlist_ptr);
   3890 		} else {
   3891 			/*
   3892 			 * The old format sd-config-list, loop through all
   3893 			 * data-property-name entries in the
   3894 			 * data-property-name-list
   3895 			 * setting the properties for each.
   3896 			 */
   3897 			for (dataname_ptr = sd_strtok_r(dnlist_ptr, " \t",
   3898 			    &dataname_lasts); dataname_ptr != NULL;
   3899 			    dataname_ptr = sd_strtok_r(NULL, " \t",
   3900 			    &dataname_lasts)) {
   3901 				int version;
   3902 
   3903 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
   3904 				    "sd_process_sdconf_file: disk:%s, "
   3905 				    "data:%s\n", vidptr, dataname_ptr);
   3906 
   3907 				/* Get the data list */
   3908 				if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
   3909 				    SD_DEVINFO(un), 0, dataname_ptr, &data_list,
   3910 				    &data_list_len) != DDI_PROP_SUCCESS) {
   3911 					SD_INFO(SD_LOG_ATTACH_DETACH, un,
   3912 					    "sd_process_sdconf_file: data "
   3913 					    "property (%s) has no value\n",
   3914 					    dataname_ptr);
   3915 					continue;
   3916 				}
   3917 
   3918 				version = data_list[0];
   3919 
   3920 				if (version == SD_CONF_VERSION_1) {
   3921 					sd_tunables values;
   3922 
   3923 					/* Set the properties */
   3924 					if (sd_chk_vers1_data(un, data_list[1],
   3925 					    &data_list[2], data_list_len,
   3926 					    dataname_ptr) == SD_SUCCESS) {
   3927 						sd_get_tunables_from_conf(un,
   3928 						    data_list[1], &data_list[2],
   3929 						    &values);
   3930 						sd_set_vers1_properties(un,
   3931 						    data_list[1], &values);
   3932 						rval = SD_SUCCESS;
   3933 					} else {
   3934 						rval = SD_FAILURE;
   3935 					}
   3936 				} else {
   3937 					scsi_log(SD_DEVINFO(un), sd_label,
   3938 					    CE_WARN, "data property %s version "
   3939 					    "0x%x is invalid.",
   3940 					    dataname_ptr, version);
   3941 					rval = SD_FAILURE;
   3942 				}
   3943 				if (data_list)
   3944 					ddi_prop_free(data_list);
   3945 			}
   3946 		}
   3947 	}
   3948 
   3949 	/* free up the memory allocated by ddi_prop_lookup_string_array(). */
   3950 	if (config_list) {
   3951 		ddi_prop_free(config_list);
   3952 	}
   3953 
   3954 	return (rval);
   3955 }
   3956 
   3957 /*
   3958  *    Function: sd_nvpair_str_decode()
   3959  *
   3960  * Description: Parse the improved format sd-config-list to get
   3961  *    each entry of tunable, which includes a name-value pair.
   3962  *    Then call sd_set_properties() to set the property.
   3963  *
   3964  *   Arguments: un - driver soft state (unit) structure
   3965  *    nvpair_str - the tunable list
   3966  */
   3967 static void
   3968 sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str)
   3969 {
   3970 	char	*nv, *name, *value, *token;
   3971 	char	*nv_lasts, *v_lasts, *x_lasts;
   3972 
   3973 	for (nv = sd_strtok_r(nvpair_str, ",", &nv_lasts); nv != NULL;
   3974 	    nv = sd_strtok_r(NULL, ",", &nv_lasts)) {
   3975 		token = sd_strtok_r(nv, ":", &v_lasts);
   3976 		name  = sd_strtok_r(token, " \t", &x_lasts);
   3977 		token = sd_strtok_r(NULL, ":", &v_lasts);
   3978 		value = sd_strtok_r(token, " \t", &x_lasts);
   3979 		if (name == NULL || value == NULL) {
   3980 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   3981 			    "sd_nvpair_str_decode: "
   3982 			    "name or value is not valid!\n");
   3983 		} else {
   3984 			sd_set_properties(un, name, value);
   3985 		}
   3986 	}
   3987 }
   3988 
   3989 /*
   3990  *    Function: sd_strtok_r()
   3991  *
   3992  * Description: This function uses strpbrk and strspn to break
   3993  *    string into tokens on sequentially subsequent calls. Return
   3994  *    NULL when no non-separator characters remain. The first
   3995  *    argument is NULL for subsequent calls.
   3996  */
   3997 static char *
   3998 sd_strtok_r(char *string, const char *sepset, char **lasts)
   3999 {
   4000 	char	*q, *r;
   4001 
   4002 	/* First or subsequent call */
   4003 	if (string == NULL)
   4004 		string = *lasts;
   4005 
   4006 	if (string == NULL)
   4007 		return (NULL);
   4008 
   4009 	/* Skip leading separators */
   4010 	q = string + strspn(string, sepset);
   4011 
   4012 	if (*q == '\0')
   4013 		return (NULL);
   4014 
   4015 	if ((r = strpbrk(q, sepset)) == NULL)
   4016 		*lasts = NULL;
   4017 	else {
   4018 		*r = '\0';
   4019 		*lasts = r + 1;
   4020 	}
   4021 	return (q);
   4022 }
   4023 
   4024 /*
   4025  *    Function: sd_set_properties()
   4026  *
   4027  * Description: Set device properties based on the improved
   4028  *    format sd-config-list.
   4029  *
   4030  *   Arguments: un - driver soft state (unit) structure
   4031  *    name  - supported tunable name
   4032  *    value - tunable value
   4033  */
   4034 static void
   4035 sd_set_properties(struct sd_lun *un, char *name, char *value)
   4036 {
   4037 	char	*endptr = NULL;
   4038 	long	val = 0;
   4039 
   4040 	if (strcasecmp(name, "cache-nonvolatile") == 0) {
   4041 		if (strcasecmp(value, "true") == 0) {
   4042 			un->un_f_suppress_cache_flush = TRUE;
   4043 		} else if (strcasecmp(value, "false") == 0) {
   4044 			un->un_f_suppress_cache_flush = FALSE;
   4045 		} else {
   4046 			goto value_invalid;
   4047 		}
   4048 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
   4049 		    "suppress_cache_flush flag set to %d\n",
   4050 		    un->un_f_suppress_cache_flush);
   4051 		return;
   4052 	}
   4053 
   4054 	if (strcasecmp(name, "controller-type") == 0) {
   4055 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
   4056 			un->un_ctype = val;
   4057 		} else {
   4058 			goto value_invalid;
   4059 		}
   4060 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
   4061 		    "ctype set to %d\n", un->un_ctype);
   4062 		return;
   4063 	}
   4064 
   4065 	if (strcasecmp(name, "delay-busy") == 0) {
   4066 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
   4067 			un->un_busy_timeout = drv_usectohz(val / 1000);
   4068 		} else {
   4069 			goto value_invalid;
   4070 		}
   4071 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
   4072 		    "busy_timeout set to %d\n", un->un_busy_timeout);
   4073 		return;
   4074 	}
   4075 
   4076 	if (strcasecmp(name, "disksort") == 0) {
   4077 		if (strcasecmp(value, "true") == 0) {
   4078 			un->un_f_disksort_disabled = FALSE;
   4079 		} else if (strcasecmp(value, "false") == 0) {
   4080 			un->un_f_disksort_disabled = TRUE;
   4081 		} else {
   4082 			goto value_invalid;
   4083 		}
   4084 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
   4085 		    "disksort disabled flag set to %d\n",
   4086 		    un->un_f_disksort_disabled);
   4087 		return;
   4088 	}
   4089 
   4090 	if (strcasecmp(name, "power-condition") == 0) {
   4091 		if (strcasecmp(value, "true") == 0) {
   4092 			un->un_f_power_condition_disabled = FALSE;
   4093 		} else if (strcasecmp(value, "false") == 0) {
   4094 			un->un_f_power_condition_disabled = TRUE;
   4095 		} else {
   4096 			goto value_invalid;
   4097 		}
   4098 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
   4099 		    "power condition disabled flag set to %d\n",
   4100 		    un->un_f_power_condition_disabled);
   4101 		return;
   4102 	}
   4103 
   4104 	if (strcasecmp(name, "timeout-releasereservation") == 0) {
   4105 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
   4106 			un->un_reserve_release_time = val;
   4107 		} else {
   4108 			goto value_invalid;
   4109 		}
   4110 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
   4111 		    "reservation release timeout set to %d\n",
   4112 		    un->un_reserve_release_time);
   4113 		return;
   4114 	}
   4115 
   4116 	if (strcasecmp(name, "reset-lun") == 0) {
   4117 		if (strcasecmp(value, "true") == 0) {
   4118 			un->un_f_lun_reset_enabled = TRUE;
   4119 		} else if (strcasecmp(value, "false") == 0) {
   4120 			un->un_f_lun_reset_enabled = FALSE;
   4121 		} else {
   4122 			goto value_invalid;
   4123 		}
   4124 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
   4125 		    "lun reset enabled flag set to %d\n",
   4126 		    un->un_f_lun_reset_enabled);
   4127 		return;
   4128 	}
   4129 
   4130 	if (strcasecmp(name, "retries-busy") == 0) {
   4131 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
   4132 			un->un_busy_retry_count = val;
   4133 		} else {
   4134 			goto value_invalid;
   4135 		}
   4136 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
   4137 		    "busy retry count set to %d\n", un->un_busy_retry_count);
   4138 		return;
   4139 	}
   4140 
   4141 	if (strcasecmp(name, "retries-timeout") == 0) {
   4142 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
   4143 			un->un_retry_count = val;
   4144 		} else {
   4145 			goto value_invalid;
   4146 		}
   4147 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
   4148 		    "timeout retry count set to %d\n", un->un_retry_count);
   4149 		return;
   4150 	}
   4151 
   4152 	if (strcasecmp(name, "retries-notready") == 0) {
   4153 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
   4154 			un->un_notready_retry_count = val;
   4155 		} else {
   4156 			goto value_invalid;
   4157 		}
   4158 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
   4159 		    "notready retry count set to %d\n",
   4160 		    un->un_notready_retry_count);
   4161 		return;
   4162 	}
   4163 
   4164 	if (strcasecmp(name, "retries-reset") == 0) {
   4165 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
   4166 			un->un_reset_retry_count = val;
   4167 		} else {
   4168 			goto value_invalid;
   4169 		}
   4170 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
   4171 		    "reset retry count set to %d\n",
   4172 		    un->un_reset_retry_count);
   4173 		return;
   4174 	}
   4175 
   4176 	if (strcasecmp(name, "throttle-max") == 0) {
   4177 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
   4178 			un->un_saved_throttle = un->un_throttle = val;
   4179 		} else {
   4180 			goto value_invalid;
   4181 		}
   4182 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
   4183 		    "throttle set to %d\n", un->un_throttle);
   4184 	}
   4185 
   4186 	if (strcasecmp(name, "throttle-min") == 0) {
   4187 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
   4188 			un->un_min_throttle = val;
   4189 		} else {
   4190 			goto value_invalid;
   4191 		}
   4192 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
   4193 		    "min throttle set to %d\n", un->un_min_throttle);
   4194 	}
   4195 
   4196 	if (strcasecmp(name, "rmw-type") == 0) {
   4197 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
   4198 			un->un_f_rmw_type = val;
   4199 		} else {
   4200 			goto value_invalid;
   4201 		}
   4202 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
   4203 		    "RMW type set to %d\n", un->un_f_rmw_type);
   4204 	}
   4205 
   4206 	/*
   4207 	 * Validate the throttle values.
   4208 	 * If any of the numbers are invalid, set everything to defaults.
   4209 	 */
   4210 	if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
   4211 	    (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
   4212 	    (un->un_min_throttle > un->un_throttle)) {
   4213 		un->un_saved_throttle = un->un_throttle = sd_max_throttle;
   4214 		un->un_min_throttle = sd_min_throttle;
   4215 	}
   4216 
   4217 	if (strcasecmp(name, "mmc-gesn-polling") == 0) {
   4218 		if (strcasecmp(value, "true") == 0) {
   4219 			un->un_f_mmc_gesn_polling = TRUE;
   4220 		} else if (strcasecmp(value, "false") == 0) {
   4221 			un->un_f_mmc_gesn_polling = FALSE;
   4222 		} else {
   4223 			goto value_invalid;
   4224 		}
   4225 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
   4226 		    "mmc-gesn-polling set to %d\n",
   4227 		    un->un_f_mmc_gesn_polling);
   4228 	}
   4229 
   4230 	return;
   4231 
   4232 value_invalid:
   4233 	SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
   4234 	    "value of prop %s is invalid\n", name);
   4235 }
   4236 
   4237 /*
   4238  *    Function: sd_get_tunables_from_conf()
   4239  *
   4240  *
   4241  *    This function reads the data list from the sd.conf file and pulls
   4242  *    the values that can have numeric values as arguments and places
   4243  *    the values in the appropriate sd_tunables member.
   4244  *    Since the order of the data list members varies across platforms
   4245  *    This function reads them from the data list in a platform specific
   4246  *    order and places them into the correct sd_tunable member that is
   4247  *    consistent across all platforms.
   4248  */
   4249 static void
   4250 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list,
   4251     sd_tunables *values)
   4252 {
   4253 	int i;
   4254 	int mask;
   4255 
   4256 	bzero(values, sizeof (sd_tunables));
   4257 
   4258 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
   4259 
   4260 		mask = 1 << i;
   4261 		if (mask > flags) {
   4262 			break;
   4263 		}
   4264 
   4265 		switch (mask & flags) {
   4266 		case 0:	/* This mask bit not set in flags */
   4267 			continue;
   4268 		case SD_CONF_BSET_THROTTLE:
   4269 			values->sdt_throttle = data_list[i];
   4270 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4271 			    "sd_get_tunables_from_conf: throttle = %d\n",
   4272 			    values->sdt_throttle);
   4273 			break;
   4274 		case SD_CONF_BSET_CTYPE:
   4275 			values->sdt_ctype = data_list[i];
   4276 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4277 			    "sd_get_tunables_from_conf: ctype = %d\n",
   4278 			    values->sdt_ctype);
   4279 			break;
   4280 		case SD_CONF_BSET_NRR_COUNT:
   4281 			values->sdt_not_rdy_retries = data_list[i];
   4282 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4283 			    "sd_get_tunables_from_conf: not_rdy_retries = %d\n",
   4284 			    values->sdt_not_rdy_retries);
   4285 			break;
   4286 		case SD_CONF_BSET_BSY_RETRY_COUNT:
   4287 			values->sdt_busy_retries = data_list[i];
   4288 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4289 			    "sd_get_tunables_from_conf: busy_retries = %d\n",
   4290 			    values->sdt_busy_retries);
   4291 			break;
   4292 		case SD_CONF_BSET_RST_RETRIES:
   4293 			values->sdt_reset_retries = data_list[i];
   4294 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4295 			    "sd_get_tunables_from_conf: reset_retries = %d\n",
   4296 			    values->sdt_reset_retries);
   4297 			break;
   4298 		case SD_CONF_BSET_RSV_REL_TIME:
   4299 			values->sdt_reserv_rel_time = data_list[i];
   4300 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4301 			    "sd_get_tunables_from_conf: reserv_rel_time = %d\n",
   4302 			    values->sdt_reserv_rel_time);
   4303 			break;
   4304 		case SD_CONF_BSET_MIN_THROTTLE:
   4305 			values->sdt_min_throttle = data_list[i];
   4306 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4307 			    "sd_get_tunables_from_conf: min_throttle = %d\n",
   4308 			    values->sdt_min_throttle);
   4309 			break;
   4310 		case SD_CONF_BSET_DISKSORT_DISABLED:
   4311 			values->sdt_disk_sort_dis = data_list[i];
   4312 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4313 			    "sd_get_tunables_from_conf: disk_sort_dis = %d\n",
   4314 			    values->sdt_disk_sort_dis);
   4315 			break;
   4316 		case SD_CONF_BSET_LUN_RESET_ENABLED:
   4317 			values->sdt_lun_reset_enable = data_list[i];
   4318 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4319 			    "sd_get_tunables_from_conf: lun_reset_enable = %d"
   4320 			    "\n", values->sdt_lun_reset_enable);
   4321 			break;
   4322 		case SD_CONF_BSET_CACHE_IS_NV:
   4323 			values->sdt_suppress_cache_flush = data_list[i];
   4324 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4325 			    "sd_get_tunables_from_conf: \
   4326 			    suppress_cache_flush = %d"
   4327 			    "\n", values->sdt_suppress_cache_flush);
   4328 			break;
   4329 		case SD_CONF_BSET_PC_DISABLED:
   4330 			values->sdt_disk_sort_dis = data_list[i];
   4331 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4332 			    "sd_get_tunables_from_conf: power_condition_dis = "
   4333 			    "%d\n", values->sdt_power_condition_dis);
   4334 			break;
   4335 		}
   4336 	}
   4337 }
   4338 
   4339 /*
   4340  *    Function: sd_process_sdconf_table
   4341  *
   4342  * Description: Search the static configuration table for a match on the
   4343  *		inquiry vid/pid and update the driver soft state structure
   4344  *		according to the table property values for the device.
   4345  *
   4346  *		The form of a configuration table entry is:
   4347  *		  <vid+pid>,<flags>,<property-data>
   4348  *		  "SEAGATE ST42400N",1,0x40000,
   4349  *		  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1;
   4350  *
   4351  *   Arguments: un - driver soft state (unit) structure
   4352  */
   4353 
   4354 static void
   4355 sd_process_sdconf_table(struct sd_lun *un)
   4356 {
   4357 	char	*id = NULL;
   4358 	int	table_index;
   4359 	int	idlen;
   4360 
   4361 	ASSERT(un != NULL);
   4362 	for (table_index = 0; table_index < sd_disk_table_size;
   4363 	    table_index++) {
   4364 		id = sd_disk_table[table_index].device_id;
   4365 		idlen = strlen(id);
   4366 		if (idlen == 0) {
   4367 			continue;
   4368 		}
   4369 
   4370 		/*
   4371 		 * The static configuration table currently does not
   4372 		 * implement version 10 properties. Additionally,
   4373 		 * multiple data-property-name entries are not
   4374 		 * implemented in the static configuration table.
   4375 		 */
   4376 		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
   4377 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4378 			    "sd_process_sdconf_table: disk %s\n", id);
   4379 			sd_set_vers1_properties(un,
   4380 			    sd_disk_table[table_index].flags,
   4381 			    sd_disk_table[table_index].properties);
   4382 			break;
   4383 		}
   4384 	}
   4385 }
   4386 
   4387 
   4388 /*
   4389  *    Function: sd_sdconf_id_match
   4390  *
   4391  * Description: This local function implements a case sensitive vid/pid
   4392  *		comparison as well as the boundary cases of wild card and
   4393  *		multiple blanks.
   4394  *
   4395  *		Note: An implicit assumption made here is that the scsi
   4396  *		inquiry structure will always keep the vid, pid and
   4397  *		revision strings in consecutive sequence, so they can be
   4398  *		read as a single string. If this assumption is not the
   4399  *		case, a separate string, to be used for the check, needs
   4400  *		to be built with these strings concatenated.
   4401  *
   4402  *   Arguments: un - driver soft state (unit) structure
   4403  *		id - table or config file vid/pid
   4404  *		idlen  - length of the vid/pid (bytes)
   4405  *
   4406  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
   4407  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
   4408  */
   4409 
   4410 static int
   4411 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen)
   4412 {
   4413 	struct scsi_inquiry	*sd_inq;
   4414 	int 			rval = SD_SUCCESS;
   4415 
   4416 	ASSERT(un != NULL);
   4417 	sd_inq = un->un_sd->sd_inq;
   4418 	ASSERT(id != NULL);
   4419 
   4420 	/*
   4421 	 * We use the inq_vid as a pointer to a buffer containing the
   4422 	 * vid and pid and use the entire vid/pid length of the table
   4423 	 * entry for the comparison. This works because the inq_pid
   4424 	 * data member follows inq_vid in the scsi_inquiry structure.
   4425 	 */
   4426 	if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) {
   4427 		/*
   4428 		 * The user id string is compared to the inquiry vid/pid
   4429 		 * using a case insensitive comparison and ignoring
   4430 		 * multiple spaces.
   4431 		 */
   4432 		rval = sd_blank_cmp(un, id, idlen);
   4433 		if (rval != SD_SUCCESS) {
   4434 			/*
   4435 			 * User id strings that start and end with a "*"
   4436 			 * are a special case. These do not have a
   4437 			 * specific vendor, and the product string can
   4438 			 * appear anywhere in the 16 byte PID portion of
   4439 			 * the inquiry data. This is a simple strstr()
   4440 			 * type search for the user id in the inquiry data.
   4441 			 */
   4442 			if ((id[0] == '*') && (id[idlen - 1] == '*')) {
   4443 				char	*pidptr = &id[1];
   4444 				int	i;
   4445 				int	j;
   4446 				int	pidstrlen = idlen - 2;
   4447 				j = sizeof (SD_INQUIRY(un)->inq_pid) -
   4448 				    pidstrlen;
   4449 
   4450 				if (j < 0) {
   4451 					return (SD_FAILURE);
   4452 				}
   4453 				for (i = 0; i < j; i++) {
   4454 					if (bcmp(&SD_INQUIRY(un)->inq_pid[i],
   4455 					    pidptr, pidstrlen) == 0) {
   4456 						rval = SD_SUCCESS;
   4457 						break;
   4458 					}
   4459 				}
   4460 			}
   4461 		}
   4462 	}
   4463 	return (rval);
   4464 }
   4465 
   4466 
   4467 /*
   4468  *    Function: sd_blank_cmp
   4469  *
   4470  * Description: If the id string starts and ends with a space, treat
   4471  *		multiple consecutive spaces as equivalent to a single
   4472  *		space. For example, this causes a sd_disk_table entry
   4473  *		of " NEC CDROM " to match a device's id string of
   4474  *		"NEC       CDROM".
   4475  *
   4476  *		Note: The success exit condition for this routine is if
   4477  *		the pointer to the table entry is '\0' and the cnt of
   4478  *		the inquiry length is zero. This will happen if the inquiry
   4479  *		string returned by the device is padded with spaces to be
   4480  *		exactly 24 bytes in length (8 byte vid + 16 byte pid). The
   4481  *		SCSI spec states that the inquiry string is to be padded with
   4482  *		spaces.
   4483  *
   4484  *   Arguments: un - driver soft state (unit) structure
   4485  *		id - table or config file vid/pid
   4486  *		idlen  - length of the vid/pid (bytes)
   4487  *
   4488  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
   4489  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
   4490  */
   4491 
   4492 static int
   4493 sd_blank_cmp(struct sd_lun *un, char *id, int idlen)
   4494 {
   4495 	char		*p1;
   4496 	char		*p2;
   4497 	int		cnt;
   4498 	cnt = sizeof (SD_INQUIRY(un)->inq_vid) +
   4499 	    sizeof (SD_INQUIRY(un)->inq_pid);
   4500 
   4501 	ASSERT(un != NULL);
   4502 	p2 = un->un_sd->sd_inq->inq_vid;
   4503 	ASSERT(id != NULL);
   4504 	p1 = id;
   4505 
   4506 	if ((id[0] == ' ') && (id[idlen - 1] == ' ')) {
   4507 		/*
   4508 		 * Note: string p1 is terminated by a NUL but string p2
   4509 		 * isn't.  The end of p2 is determined by cnt.
   4510 		 */
   4511 		for (;;) {
   4512 			/* skip over any extra blanks in both strings */
   4513 			while ((*p1 != '\0') && (*p1 == ' ')) {
   4514 				p1++;
   4515 			}
   4516 			while ((cnt != 0) && (*p2 == ' ')) {
   4517 				p2++;
   4518 				cnt--;
   4519 			}
   4520 
   4521 			/* compare the two strings */
   4522 			if ((cnt == 0) ||
   4523 			    (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) {
   4524 				break;
   4525 			}
   4526 			while ((cnt > 0) &&
   4527 			    (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) {
   4528 				p1++;
   4529 				p2++;
   4530 				cnt--;
   4531 			}
   4532 		}
   4533 	}
   4534 
   4535 	/* return SD_SUCCESS if both strings match */
   4536 	return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE);
   4537 }
   4538 
   4539 
   4540 /*
   4541  *    Function: sd_chk_vers1_data
   4542  *
   4543  * Description: Verify the version 1 device properties provided by the
   4544  *		user via the configuration file
   4545  *
   4546  *   Arguments: un	     - driver soft state (unit) structure
   4547  *		flags	     - integer mask indicating properties to be set
   4548  *		prop_list    - integer list of property values
   4549  *		list_len     - number of the elements
   4550  *
   4551  * Return Code: SD_SUCCESS - Indicates the user provided data is valid
   4552  *		SD_FAILURE - Indicates the user provided data is invalid
   4553  */
   4554 
   4555 static int
   4556 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
   4557     int list_len, char *dataname_ptr)
   4558 {
   4559 	int i;
   4560 	int mask = 1;
   4561 	int index = 0;
   4562 
   4563 	ASSERT(un != NULL);
   4564 
   4565 	/* Check for a NULL property name and list */
   4566 	if (dataname_ptr == NULL) {
   4567 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
   4568 		    "sd_chk_vers1_data: NULL data property name.");
   4569 		return (SD_FAILURE);
   4570 	}
   4571 	if (prop_list == NULL) {
   4572 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
   4573 		    "sd_chk_vers1_data: %s NULL data property list.",
   4574 		    dataname_ptr);
   4575 		return (SD_FAILURE);
   4576 	}
   4577 
   4578 	/* Display a warning if undefined bits are set in the flags */
   4579 	if (flags & ~SD_CONF_BIT_MASK) {
   4580 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
   4581 		    "sd_chk_vers1_data: invalid bits 0x%x in data list %s. "
   4582 		    "Properties not set.",
   4583 		    (flags & ~SD_CONF_BIT_MASK), dataname_ptr);
   4584 		return (SD_FAILURE);
   4585 	}
   4586 
   4587 	/*
   4588 	 * Verify the length of the list by identifying the highest bit set
   4589 	 * in the flags and validating that the property list has a length
   4590 	 * up to the index of this bit.
   4591 	 */
   4592 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
   4593 		if (flags & mask) {
   4594 			index++;
   4595 		}
   4596 		mask = 1 << i;
   4597 	}
   4598 	if (list_len < (index + 2)) {
   4599 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
   4600 		    "sd_chk_vers1_data: "
   4601 		    "Data property list %s size is incorrect. "
   4602 		    "Properties not set.", dataname_ptr);
   4603 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: "
   4604 		    "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS);
   4605 		return (SD_FAILURE);
   4606 	}
   4607 	return (SD_SUCCESS);
   4608 }
   4609 
   4610 
   4611 /*
   4612  *    Function: sd_set_vers1_properties
   4613  *
   4614  * Description: Set version 1 device properties based on a property list
   4615  *		retrieved from the driver configuration file or static
   4616  *		configuration table. Version 1 properties have the format:
   4617  *
   4618  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
   4619  *
   4620  *		where the prop0 value will be used to set prop0 if bit0
   4621  *		is set in the flags
   4622  *
   4623  *   Arguments: un	     - driver soft state (unit) structure
   4624  *		flags	     - integer mask indicating properties to be set
   4625  *		prop_list    - integer list of property values
   4626  */
   4627 
   4628 static void
   4629 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list)
   4630 {
   4631 	ASSERT(un != NULL);
   4632 
   4633 	/*
   4634 	 * Set the flag to indicate cache is to be disabled. An attempt
   4635 	 * to disable the cache via sd_cache_control() will be made
   4636 	 * later during attach once the basic initialization is complete.
   4637 	 */
   4638 	if (flags & SD_CONF_BSET_NOCACHE) {
   4639 		un->un_f_opt_disable_cache = TRUE;
   4640 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4641 		    "sd_set_vers1_properties: caching disabled flag set\n");
   4642 	}
   4643 
   4644 	/* CD-specific configuration parameters */
   4645 	if (flags & SD_CONF_BSET_PLAYMSF_BCD) {
   4646 		un->un_f_cfg_playmsf_bcd = TRUE;
   4647 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4648 		    "sd_set_vers1_properties: playmsf_bcd set\n");
   4649 	}
   4650 	if (flags & SD_CONF_BSET_READSUB_BCD) {
   4651 		un->un_f_cfg_readsub_bcd = TRUE;
   4652 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4653 		    "sd_set_vers1_properties: readsub_bcd set\n");
   4654 	}
   4655 	if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) {
   4656 		un->un_f_cfg_read_toc_trk_bcd = TRUE;
   4657 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4658 		    "sd_set_vers1_properties: read_toc_trk_bcd set\n");
   4659 	}
   4660 	if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) {
   4661 		un->un_f_cfg_read_toc_addr_bcd = TRUE;
   4662 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4663 		    "sd_set_vers1_properties: read_toc_addr_bcd set\n");
   4664 	}
   4665 	if (flags & SD_CONF_BSET_NO_READ_HEADER) {
   4666 		un->un_f_cfg_no_read_header = TRUE;
   4667 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4668 		    "sd_set_vers1_properties: no_read_header set\n");
   4669 	}
   4670 	if (flags & SD_CONF_BSET_READ_CD_XD4) {
   4671 		un->un_f_cfg_read_cd_xd4 = TRUE;
   4672 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4673 		    "sd_set_vers1_properties: read_cd_xd4 set\n");
   4674 	}
   4675 
   4676 	/* Support for devices which do not have valid/unique serial numbers */
   4677 	if (flags & SD_CONF_BSET_FAB_DEVID) {
   4678 		un->un_f_opt_fab_devid = TRUE;
   4679 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4680 		    "sd_set_vers1_properties: fab_devid bit set\n");
   4681 	}
   4682 
   4683 	/* Support for user throttle configuration */
   4684 	if (flags & SD_CONF_BSET_THROTTLE) {
   4685 		ASSERT(prop_list != NULL);
   4686 		un->un_saved_throttle = un->un_throttle =
   4687 		    prop_list->sdt_throttle;
   4688 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4689 		    "sd_set_vers1_properties: throttle set to %d\n",
   4690 		    prop_list->sdt_throttle);
   4691 	}
   4692 
   4693 	/* Set the per disk retry count according to the conf file or table. */
   4694 	if (flags & SD_CONF_BSET_NRR_COUNT) {
   4695 		ASSERT(prop_list != NULL);
   4696 		if (prop_list->sdt_not_rdy_retries) {
   4697 			un->un_notready_retry_count =
   4698 			    prop_list->sdt_not_rdy_retries;
   4699 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4700 			    "sd_set_vers1_properties: not ready retry count"
   4701 			    " set to %d\n", un->un_notready_retry_count);
   4702 		}
   4703 	}
   4704 
   4705 	/* The controller type is reported for generic disk driver ioctls */
   4706 	if (flags & SD_CONF_BSET_CTYPE) {
   4707 		ASSERT(prop_list != NULL);
   4708 		switch (prop_list->sdt_ctype) {
   4709 		case CTYPE_CDROM:
   4710 			un->un_ctype = prop_list->sdt_ctype;
   4711 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4712 			    "sd_set_vers1_properties: ctype set to "
   4713 			    "CTYPE_CDROM\n");
   4714 			break;
   4715 		case CTYPE_CCS:
   4716 			un->un_ctype = prop_list->sdt_ctype;
   4717 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4718 			    "sd_set_vers1_properties: ctype set to "
   4719 			    "CTYPE_CCS\n");
   4720 			break;
   4721 		case CTYPE_ROD:		/* RW optical */
   4722 			un->un_ctype = prop_list->sdt_ctype;
   4723 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4724 			    "sd_set_vers1_properties: ctype set to "
   4725 			    "CTYPE_ROD\n");
   4726 			break;
   4727 		default:
   4728 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
   4729 			    "sd_set_vers1_properties: Could not set "
   4730 			    "invalid ctype value (%d)",
   4731 			    prop_list->sdt_ctype);
   4732 		}
   4733 	}
   4734 
   4735 	/* Purple failover timeout */
   4736 	if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) {
   4737 		ASSERT(prop_list != NULL);
   4738 		un->un_busy_retry_count =
   4739 		    prop_list->sdt_busy_retries;
   4740 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4741 		    "sd_set_vers1_properties: "
   4742 		    "busy retry count set to %d\n",
   4743 		    un->un_busy_retry_count);
   4744 	}
   4745 
   4746 	/* Purple reset retry count */
   4747 	if (flags & SD_CONF_BSET_RST_RETRIES) {
   4748 		ASSERT(prop_list != NULL);
   4749 		un->un_reset_retry_count =
   4750 		    prop_list->sdt_reset_retries;
   4751 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4752 		    "sd_set_vers1_properties: "
   4753 		    "reset retry count set to %d\n",
   4754 		    un->un_reset_retry_count);
   4755 	}
   4756 
   4757 	/* Purple reservation release timeout */
   4758 	if (flags & SD_CONF_BSET_RSV_REL_TIME) {
   4759 		ASSERT(prop_list != NULL);
   4760 		un->un_reserve_release_time =
   4761 		    prop_list->sdt_reserv_rel_time;
   4762 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4763 		    "sd_set_vers1_properties: "
   4764 		    "reservation release timeout set to %d\n",
   4765 		    un->un_reserve_release_time);
   4766 	}
   4767 
   4768 	/*
   4769 	 * Driver flag telling the driver to verify that no commands are pending
   4770 	 * for a device before issuing a Test Unit Ready. This is a workaround
   4771 	 * for a firmware bug in some Seagate eliteI drives.
   4772 	 */
   4773 	if (flags & SD_CONF_BSET_TUR_CHECK) {
   4774 		un->un_f_cfg_tur_check = TRUE;
   4775 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4776 		    "sd_set_vers1_properties: tur queue check set\n");
   4777 	}
   4778 
   4779 	if (flags & SD_CONF_BSET_MIN_THROTTLE) {
   4780 		un->un_min_throttle = prop_list->sdt_min_throttle;
   4781 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4782 		    "sd_set_vers1_properties: min throttle set to %d\n",
   4783 		    un->un_min_throttle);
   4784 	}
   4785 
   4786 	if (flags & SD_CONF_BSET_DISKSORT_DISABLED) {
   4787 		un->un_f_disksort_disabled =
   4788 		    (prop_list->sdt_disk_sort_dis != 0) ?
   4789 		    TRUE : FALSE;
   4790 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4791 		    "sd_set_vers1_properties: disksort disabled "
   4792 		    "flag set to %d\n",
   4793 		    prop_list->sdt_disk_sort_dis);
   4794 	}
   4795 
   4796 	if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) {
   4797 		un->un_f_lun_reset_enabled =
   4798 		    (prop_list->sdt_lun_reset_enable != 0) ?
   4799 		    TRUE : FALSE;
   4800 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4801 		    "sd_set_vers1_properties: lun reset enabled "
   4802 		    "flag set to %d\n",
   4803 		    prop_list->sdt_lun_reset_enable);
   4804 	}
   4805 
   4806 	if (flags & SD_CONF_BSET_CACHE_IS_NV) {
   4807 		un->un_f_suppress_cache_flush =
   4808 		    (prop_list->sdt_suppress_cache_flush != 0) ?
   4809 		    TRUE : FALSE;
   4810 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4811 		    "sd_set_vers1_properties: suppress_cache_flush "
   4812 		    "flag set to %d\n",
   4813 		    prop_list->sdt_suppress_cache_flush);
   4814 	}
   4815 
   4816 	if (flags & SD_CONF_BSET_PC_DISABLED) {
   4817 		un->un_f_power_condition_disabled =
   4818 		    (prop_list->sdt_power_condition_dis != 0) ?
   4819 		    TRUE : FALSE;
   4820 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   4821 		    "sd_set_vers1_properties: power_condition_disabled "
   4822 		    "flag set to %d\n",
   4823 		    prop_list->sdt_power_condition_dis);
   4824 	}
   4825 
   4826 	/*
   4827 	 * Validate the throttle values.
   4828 	 * If any of the numbers are invalid, set everything to defaults.
   4829 	 */
   4830 	if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
   4831 	    (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
   4832 	    (un->un_min_throttle > un->un_throttle)) {
   4833 		un->un_saved_throttle = un->un_throttle = sd_max_throttle;
   4834 		un->un_min_throttle = sd_min_throttle;
   4835 	}
   4836 }
   4837 
   4838 /*
   4839  *   Function: sd_is_lsi()
   4840  *
   4841  *   Description: Check for lsi devices, step through the static device
   4842  *	table to match vid/pid.
   4843  *
   4844  *   Args: un - ptr to sd_lun
   4845  *
   4846  *   Notes:  When creating new LSI property, need to add the new LSI property
   4847  *		to this function.
   4848  */
   4849 static void
   4850 sd_is_lsi(struct sd_lun *un)
   4851 {
   4852 	char	*id = NULL;
   4853 	int	table_index;
   4854 	int	idlen;
   4855 	void	*prop;
   4856 
   4857 	ASSERT(un != NULL);
   4858 	for (table_index = 0; table_index < sd_disk_table_size;
   4859 	    table_index++) {
   4860 		id = sd_disk_table[table_index].device_id;
   4861 		idlen = strlen(id);
   4862 		if (idlen == 0) {
   4863 			continue;
   4864 		}
   4865 
   4866 		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
   4867 			prop = sd_disk_table[table_index].properties;
   4868 			if (prop == &lsi_properties ||
   4869 			    prop == &lsi_oem_properties ||
   4870 			    prop == &lsi_properties_scsi ||
   4871 			    prop == &symbios_properties) {
   4872 				un->un_f_cfg_is_lsi = TRUE;
   4873 			}
   4874 			break;
   4875 		}
   4876 	}
   4877 }
   4878 
   4879 /*
   4880  *    Function: sd_get_physical_geometry
   4881  *
   4882  * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and
   4883  *		MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the
   4884  *		target, and use this information to initialize the physical
   4885  *		geometry cache specified by pgeom_p.
   4886  *
   4887  *		MODE SENSE is an optional command, so failure in this case
   4888  *		does not necessarily denote an error. We want to use the
   4889  *		MODE SENSE commands to derive the physical geometry of the
   4890  *		device, but if either command fails, the logical geometry is
   4891  *		used as the fallback for disk label geometry in cmlb.
   4892  *
   4893  *		This requires that un->un_blockcount and un->un_tgt_blocksize
   4894  *		have already been initialized for the current target and
   4895  *		that the current values be passed as args so that we don't
   4896  *		end up ever trying to use -1 as a valid value. This could
   4897  *		happen if either value is reset while we're not holding
   4898  *		the mutex.
   4899  *
   4900  *   Arguments: un - driver soft state (unit) structure
   4901  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
   4902  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
   4903  *			to use the USCSI "direct" chain and bypass the normal
   4904  *			command waitq.
   4905  *
   4906  *     Context: Kernel thread only (can sleep).
   4907  */
   4908 
   4909 static int
   4910 sd_get_physical_geometry(struct sd_lun *un, cmlb_geom_t *pgeom_p,
   4911 	diskaddr_t capacity, int lbasize, int path_flag)
   4912 {
   4913 	struct	mode_format	*page3p;
   4914 	struct	mode_geometry	*page4p;
   4915 	struct	mode_header	*headerp;
   4916 	int	sector_size;
   4917 	int	nsect;
   4918 	int	nhead;
   4919 	int	ncyl;
   4920 	int	intrlv;
   4921 	int	spc;
   4922 	diskaddr_t	modesense_capacity;
   4923 	int	rpm;
   4924 	int	bd_len;
   4925 	int	mode_header_length;
   4926 	uchar_t	*p3bufp;
   4927 	uchar_t	*p4bufp;
   4928 	int	cdbsize;
   4929 	int 	ret = EIO;
   4930 	sd_ssc_t *ssc;
   4931 	int	status;
   4932 
   4933 	ASSERT(un != NULL);
   4934 
   4935 	if (lbasize == 0) {
   4936 		if (ISCD(un)) {
   4937 			lbasize = 2048;
   4938 		} else {
   4939 			lbasize = un->un_sys_blocksize;
   4940 		}
   4941 	}
   4942 	pgeom_p->g_secsize = (unsigned short)lbasize;
   4943 
   4944 	/*
   4945 	 * If the unit is a cd/dvd drive MODE SENSE page three
   4946 	 * and MODE SENSE page four are reserved (see SBC spec
   4947 	 * and MMC spec). To prevent soft errors just return
   4948 	 * using the default LBA size.
   4949 	 */
   4950 	if (ISCD(un))
   4951 		return (ret);
   4952 
   4953 	cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0;
   4954 
   4955 	/*
   4956 	 * Retrieve MODE SENSE page 3 - Format Device Page
   4957 	 */
   4958 	p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP);
   4959 	ssc = sd_ssc_init(un);
   4960 	status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p3bufp,
   4961 	    SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag);
   4962 	if (status != 0) {
   4963 		SD_ERROR(SD_LOG_COMMON, un,
   4964 		    "sd_get_physical_geometry: mode sense page 3 failed\n");
   4965 		goto page3_exit;
   4966 	}
   4967 
   4968 	/*
   4969 	 * Determine size of Block Descriptors in order to locate the mode
   4970 	 * page data.  ATAPI devices return 0, SCSI devices should return
   4971 	 * MODE_BLK_DESC_LENGTH.
   4972 	 */
   4973 	headerp = (struct mode_header *)p3bufp;
   4974 	if (un->un_f_cfg_is_atapi == TRUE) {
   4975 		struct mode_header_grp2 *mhp =
   4976 		    (struct mode_header_grp2 *)headerp;
   4977 		mode_header_length = MODE_HEADER_LENGTH_GRP2;
   4978 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
   4979 	} else {
   4980 		mode_header_length = MODE_HEADER_LENGTH;
   4981 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
   4982 	}
   4983 
   4984 	if (bd_len > MODE_BLK_DESC_LENGTH) {
   4985 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
   4986 		    "sd_get_physical_geometry: received unexpected bd_len "
   4987 		    "of %d, page3\n", bd_len);
   4988 		status = EIO;
   4989 		goto page3_exit;
   4990 	}
   4991 
   4992 	page3p = (struct mode_format *)
   4993 	    ((caddr_t)headerp + mode_header_length + bd_len);
   4994 
   4995 	if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) {
   4996 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
   4997 		    "sd_get_physical_geometry: mode sense pg3 code mismatch "
   4998 		    "%d\n", page3p->mode_page.code);
   4999 		status = EIO;
   5000 		goto page3_exit;
   5001 	}
   5002 
   5003 	/*
   5004 	 * Use this physical geometry data only if BOTH MODE SENSE commands
   5005 	 * complete successfully; otherwise, revert to the logical geometry.
   5006 	 * So, we need to save everything in temporary variables.
   5007 	 */
   5008 	sector_size = BE_16(page3p->data_bytes_sect);
   5009 
   5010 	/*
   5011 	 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size
   5012 	 */
   5013 	if (sector_size == 0) {
   5014 		sector_size = un->un_sys_blocksize;
   5015 	} else {
   5016 		sector_size &= ~(un->un_sys_blocksize - 1);
   5017 	}
   5018 
   5019 	nsect  = BE_16(page3p->sect_track);
   5020 	intrlv = BE_16(page3p->interleave);
   5021 
   5022 	SD_INFO(SD_LOG_COMMON, un,
   5023 	    "sd_get_physical_geometry: Format Parameters (page 3)\n");
   5024 	SD_INFO(SD_LOG_COMMON, un,
   5025 	    "   mode page: %d; nsect: %d; sector size: %d;\n",
   5026 	    page3p->mode_page.code, nsect, sector_size);
   5027 	SD_INFO(SD_LOG_COMMON, un,
   5028 	    "   interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv,
   5029 	    BE_16(page3p->track_skew),
   5030 	    BE_16(page3p->cylinder_skew));
   5031 
   5032 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
   5033 
   5034 	/*
   5035 	 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page
   5036 	 */
   5037 	p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP);
   5038 	status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p4bufp,
   5039 	    SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag);
   5040 	if (status != 0) {
   5041 		SD_ERROR(SD_LOG_COMMON, un,
   5042 		    "sd_get_physical_geometry: mode sense page 4 failed\n");
   5043 		goto page4_exit;
   5044 	}
   5045 
   5046 	/*
   5047 	 * Determine size of Block Descriptors in order to locate the mode
   5048 	 * page data.  ATAPI devices return 0, SCSI devices should return
   5049 	 * MODE_BLK_DESC_LENGTH.
   5050 	 */
   5051 	headerp = (struct mode_header *)p4bufp;
   5052 	if (un->un_f_cfg_is_atapi == TRUE) {
   5053 		struct mode_header_grp2 *mhp =
   5054 		    (struct mode_header_grp2 *)headerp;
   5055 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
   5056 	} else {
   5057 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
   5058 	}
   5059 
   5060 	if (bd_len > MODE_BLK_DESC_LENGTH) {
   5061 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
   5062 		    "sd_get_physical_geometry: received unexpected bd_len of "
   5063 		    "%d, page4\n", bd_len);
   5064 		status = EIO;
   5065 		goto page4_exit;
   5066 	}
   5067 
   5068 	page4p = (struct mode_geometry *)
   5069 	    ((caddr_t)headerp + mode_header_length + bd_len);
   5070 
   5071 	if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) {
   5072 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
   5073 		    "sd_get_physical_geometry: mode sense pg4 code mismatch "
   5074 		    "%d\n", page4p->mode_page.code);
   5075 		status = EIO;
   5076 		goto page4_exit;
   5077 	}
   5078 
   5079 	/*
   5080 	 * Stash the data now, after we know that both commands completed.
   5081 	 */
   5082 
   5083 
   5084 	nhead = (int)page4p->heads;	/* uchar, so no conversion needed */
   5085 	spc   = nhead * nsect;
   5086 	ncyl  = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb;
   5087 	rpm   = BE_16(page4p->rpm);
   5088 
   5089 	modesense_capacity = spc * ncyl;
   5090 
   5091 	SD_INFO(SD_LOG_COMMON, un,
   5092 	    "sd_get_physical_geometry: Geometry Parameters (page 4)\n");
   5093 	SD_INFO(SD_LOG_COMMON, un,
   5094 	    "   cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm);
   5095 	SD_INFO(SD_LOG_COMMON, un,
   5096 	    "   computed capacity(h*s*c): %d;\n", modesense_capacity);
   5097 	SD_INFO(SD_LOG_COMMON, un, "   pgeom_p: %p; read cap: %d\n",
   5098 	    (void *)pgeom_p, capacity);
   5099 
   5100 	/*
   5101 	 * Compensate if the drive's geometry is not rectangular, i.e.,
   5102 	 * the product of C * H * S returned by MODE SENSE >= that returned
   5103 	 * by read capacity. This is an idiosyncrasy of the original x86
   5104 	 * disk subsystem.
   5105 	 */
   5106 	if (modesense_capacity >= capacity) {
   5107 		SD_INFO(SD_LOG_COMMON, un,
   5108 		    "sd_get_physical_geometry: adjusting acyl; "
   5109 		    "old: %d; new: %d\n", pgeom_p->g_acyl,
   5110 		    (modesense_capacity - capacity + spc - 1) / spc);
   5111 		if (sector_size != 0) {
   5112 			/* 1243403: NEC D38x7 drives don't support sec size */
   5113 			pgeom_p->g_secsize = (unsigned short)sector_size;
   5114 		}
   5115 		pgeom_p->g_nsect    = (unsigned short)nsect;
   5116 		pgeom_p->g_nhead    = (unsigned short)nhead;
   5117 		pgeom_p->g_capacity = capacity;
   5118 		pgeom_p->g_acyl	    =
   5119 		    (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc;
   5120 		pgeom_p->g_ncyl	    = ncyl - pgeom_p->g_acyl;
   5121 	}
   5122 
   5123 	pgeom_p->g_rpm    = (unsigned short)rpm;
   5124 	pgeom_p->g_intrlv = (unsigned short)intrlv;
   5125 	ret = 0;
   5126 
   5127 	SD_INFO(SD_LOG_COMMON, un,
   5128 	    "sd_get_physical_geometry: mode sense geometry:\n");
   5129 	SD_INFO(SD_LOG_COMMON, un,
   5130 	    "   nsect: %d; sector size: %d; interlv: %d\n",
   5131 	    nsect, sector_size, intrlv);
   5132 	SD_INFO(SD_LOG_COMMON, un,
   5133 	    "   nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n",
   5134 	    nhead, ncyl, rpm, modesense_capacity);
   5135 	SD_INFO(SD_LOG_COMMON, un,
   5136 	    "sd_get_physical_geometry: (cached)\n");
   5137 	SD_INFO(SD_LOG_COMMON, un,
   5138 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
   5139 	    pgeom_p->g_ncyl,  pgeom_p->g_acyl,
   5140 	    pgeom_p->g_nhead, pgeom_p->g_nsect);
   5141 	SD_INFO(SD_LOG_COMMON, un,
   5142 	    "   lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n",
   5143 	    pgeom_p->g_secsize, pgeom_p->g_capacity,
   5144 	    pgeom_p->g_intrlv, pgeom_p->g_rpm);
   5145 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
   5146 
   5147 page4_exit:
   5148 	kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH);
   5149 
   5150 page3_exit:
   5151 	kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH);
   5152 
   5153 	if (status != 0) {
   5154 		if (status == EIO) {
   5155 			/*
   5156 			 * Some disks do not support mode sense(6), we
   5157 			 * should ignore this kind of error(sense key is
   5158 			 * 0x5 - illegal request).
   5159 			 */
   5160 			uint8_t *sensep;
   5161 			int senlen;
   5162 
   5163 			sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
   5164 			senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
   5165 			    ssc->ssc_uscsi_cmd->uscsi_rqresid);
   5166 
   5167 			if (senlen > 0 &&
   5168 			    scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
   5169 				sd_ssc_assessment(ssc,
   5170 				    SD_FMT_IGNORE_COMPROMISE);
   5171 			} else {
   5172 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
   5173 			}
   5174 		} else {
   5175 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   5176 		}
   5177 	}
   5178 	sd_ssc_fini(ssc);
   5179 	return (ret);
   5180 }
   5181 
   5182 /*
   5183  *    Function: sd_get_virtual_geometry
   5184  *
   5185  * Description: Ask the controller to tell us about the target device.
   5186  *
   5187  *   Arguments: un - pointer to softstate
   5188  *		capacity - disk capacity in #blocks
   5189  *		lbasize - disk block size in bytes
   5190  *
   5191  *     Context: Kernel thread only
   5192  */
   5193 
   5194 static int
   5195 sd_get_virtual_geometry(struct sd_lun *un, cmlb_geom_t *lgeom_p,
   5196     diskaddr_t capacity, int lbasize)
   5197 {
   5198 	uint_t	geombuf;
   5199 	int	spc;
   5200 
   5201 	ASSERT(un != NULL);
   5202 
   5203 	/* Set sector size, and total number of sectors */
   5204 	(void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size",   lbasize,  1);
   5205 	(void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1);
   5206 
   5207 	/* Let the HBA tell us its geometry */
   5208 	geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1);
   5209 
   5210 	/* A value of -1 indicates an undefined "geometry" property */
   5211 	if (geombuf == (-1)) {
   5212 		return (EINVAL);
   5213 	}
   5214 
   5215 	/* Initialize the logical geometry cache. */
   5216 	lgeom_p->g_nhead   = (geombuf >> 16) & 0xffff;
   5217 	lgeom_p->g_nsect   = geombuf & 0xffff;
   5218 	lgeom_p->g_secsize = un->un_sys_blocksize;
   5219 
   5220 	spc = lgeom_p->g_nhead * lgeom_p->g_nsect;
   5221 
   5222 	/*
   5223 	 * Note: The driver originally converted the capacity value from
   5224 	 * target blocks to system blocks. However, the capacity value passed
   5225 	 * to this routine is already in terms of system blocks (this scaling
   5226 	 * is done when the READ CAPACITY command is issued and processed).
   5227 	 * This 'error' may have gone undetected because the usage of g_ncyl
   5228 	 * (which is based upon g_capacity) is very limited within the driver
   5229 	 */
   5230 	lgeom_p->g_capacity = capacity;
   5231 
   5232 	/*
   5233 	 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The
   5234 	 * hba may return zero values if the device has been removed.
   5235 	 */
   5236 	if (spc == 0) {
   5237 		lgeom_p->g_ncyl = 0;
   5238 	} else {
   5239 		lgeom_p->g_ncyl = lgeom_p->g_capacity / spc;
   5240 	}
   5241 	lgeom_p->g_acyl = 0;
   5242 
   5243 	SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n");
   5244 	return (0);
   5245 
   5246 }
   5247 /*
   5248  *    Function: sd_update_block_info
   5249  *
   5250  * Description: Calculate a byte count to sector count bitshift value
   5251  *		from sector size.
   5252  *
   5253  *   Arguments: un: unit struct.
   5254  *		lbasize: new target sector size
   5255  *		capacity: new target capacity, ie. block count
   5256  *
   5257  *     Context: Kernel thread context
   5258  */
   5259 
   5260 static void
   5261 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity)
   5262 {
   5263 	if (lbasize != 0) {
   5264 		un->un_tgt_blocksize = lbasize;
   5265 		un->un_f_tgt_blocksize_is_valid = TRUE;
   5266 		if (!un->un_f_has_removable_media) {
   5267 			un->un_sys_blocksize = lbasize;
   5268 		}
   5269 	}
   5270 
   5271 	if (capacity != 0) {
   5272 		un->un_blockcount		= capacity;
   5273 		un->un_f_blockcount_is_valid	= TRUE;
   5274 	}
   5275 }
   5276 
   5277 
   5278 /*
   5279  *    Function: sd_register_devid
   5280  *
   5281  * Description: This routine will obtain the device id information from the
   5282  *		target, obtain the serial number, and register the device
   5283  *		id with the ddi framework.
   5284  *
   5285  *   Arguments: devi - the system's dev_info_t for the device.
   5286  *		un - driver soft state (unit) structure
   5287  *		reservation_flag - indicates if a reservation conflict
   5288  *		occurred during attach
   5289  *
   5290  *     Context: Kernel Thread
   5291  */
   5292 static void
   5293 sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, int reservation_flag)
   5294 {
   5295 	int		rval		= 0;
   5296 	uchar_t		*inq80		= NULL;
   5297 	size_t		inq80_len	= MAX_INQUIRY_SIZE;
   5298 	size_t		inq80_resid	= 0;
   5299 	uchar_t		*inq83		= NULL;
   5300 	size_t		inq83_len	= MAX_INQUIRY_SIZE;
   5301 	size_t		inq83_resid	= 0;
   5302 	int		dlen, len;
   5303 	char		*sn;
   5304 	struct sd_lun	*un;
   5305 
   5306 	ASSERT(ssc != NULL);
   5307 	un = ssc->ssc_un;
   5308 	ASSERT(un != NULL);
   5309 	ASSERT(mutex_owned(SD_MUTEX(un)));
   5310 	ASSERT((SD_DEVINFO(un)) == devi);
   5311 
   5312 
   5313 	/*
   5314 	 * We check the availability of the World Wide Name (0x83) and Unit
   5315 	 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using
   5316 	 * un_vpd_page_mask from them, we decide which way to get the WWN.  If
   5317 	 * 0x83 is available, that is the best choice.  Our next choice is
   5318 	 * 0x80.  If neither are available, we munge the devid from the device
   5319 	 * vid/pid/serial # for Sun qualified disks, or use the ddi framework
   5320 	 * to fabricate a devid for non-Sun qualified disks.
   5321 	 */
   5322 	if (sd_check_vpd_page_support(ssc) == 0) {
   5323 		/* collect page 80 data if available */
   5324 		if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) {
   5325 
   5326 			mutex_exit(SD_MUTEX(un));
   5327 			inq80 = kmem_zalloc(inq80_len, KM_SLEEP);
   5328 
   5329 			rval = sd_send_scsi_INQUIRY(ssc, inq80, inq80_len,
   5330 			    0x01, 0x80, &inq80_resid);
   5331 
   5332 			if (rval != 0) {
   5333 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   5334 				kmem_free(inq80, inq80_len);
   5335 				inq80 = NULL;
   5336 				inq80_len = 0;
   5337 			} else if (ddi_prop_exists(
   5338 			    DDI_DEV_T_NONE, SD_DEVINFO(un),
   5339 			    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
   5340 			    INQUIRY_SERIAL_NO) == 0) {
   5341 				/*
   5342 				 * If we don't already have a serial number
   5343 				 * property, do quick verify of data returned
   5344 				 * and define property.
   5345 				 */
   5346 				dlen = inq80_len - inq80_resid;
   5347 				len = (size_t)inq80[3];
   5348 				if ((dlen >= 4) && ((len + 4) <= dlen)) {
   5349 					/*
   5350 					 * Ensure sn termination, skip leading
   5351 					 * blanks, and create property
   5352 					 * 'inquiry-serial-no'.
   5353 					 */
   5354 					sn = (char *)&inq80[4];
   5355 					sn[len] = 0;
   5356 					while (*sn && (*sn == ' '))
   5357 						sn++;
   5358 					if (*sn) {
   5359 						(void) ddi_prop_update_string(
   5360 						    DDI_DEV_T_NONE,
   5361 						    SD_DEVINFO(un),
   5362 						    INQUIRY_SERIAL_NO, sn);
   5363 					}
   5364 				}
   5365 			}
   5366 			mutex_enter(SD_MUTEX(un));
   5367 		}
   5368 
   5369 		/* collect page 83 data if available */
   5370 		if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) {
   5371 			mutex_exit(SD_MUTEX(un));
   5372 			inq83 = kmem_zalloc(inq83_len, KM_SLEEP);
   5373 
   5374 			rval = sd_send_scsi_INQUIRY(ssc, inq83, inq83_len,
   5375 			    0x01, 0x83, &inq83_resid);
   5376 
   5377 			if (rval != 0) {
   5378 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   5379 				kmem_free(inq83, inq83_len);
   5380 				inq83 = NULL;
   5381 				inq83_len = 0;
   5382 			}
   5383 			mutex_enter(SD_MUTEX(un));
   5384 		}
   5385 	}
   5386 
   5387 	/*
   5388 	 * If transport has already registered a devid for this target
   5389 	 * then that takes precedence over the driver's determination
   5390 	 * of the devid.
   5391 	 *
   5392 	 * NOTE: The reason this check is done here instead of at the beginning
   5393 	 * of the function is to allow the code above to create the
   5394 	 * 'inquiry-serial-no' property.
   5395 	 */
   5396 	if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) {
   5397 		ASSERT(un->un_devid);
   5398 		un->un_f_devid_transport_defined = TRUE;
   5399 		goto cleanup; /* use devid registered by the transport */
   5400 	}
   5401 
   5402 	/*
   5403 	 * This is the case of antiquated Sun disk drives that have the
   5404 	 * FAB_DEVID property set in the disk_table.  These drives
   5405 	 * manage the devid's by storing them in last 2 available sectors
   5406 	 * on the drive and have them fabricated by the ddi layer by calling
   5407 	 * ddi_devid_init and passing the DEVID_FAB flag.
   5408 	 */
   5409 	if (un->un_f_opt_fab_devid == TRUE) {
   5410 		/*
   5411 		 * Depending on EINVAL isn't reliable, since a reserved disk
   5412 		 * may result in invalid geometry, so check to make sure a
   5413 		 * reservation conflict did not occur during attach.
   5414 		 */
   5415 		if ((sd_get_devid(ssc) == EINVAL) &&
   5416 		    (reservation_flag != SD_TARGET_IS_RESERVED)) {
   5417 			/*
   5418 			 * The devid is invalid AND there is no reservation
   5419 			 * conflict.  Fabricate a new devid.
   5420 			 */
   5421 			(void) sd_create_devid(ssc);
   5422 		}
   5423 
   5424 		/* Register the devid if it exists */
   5425 		if (un->un_devid != NULL) {
   5426 			(void) ddi_devid_register(SD_DEVINFO(un),
   5427 			    un->un_devid);
   5428 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   5429 			    "sd_register_devid: Devid Fabricated\n");
   5430 		}
   5431 		goto cleanup;
   5432 	}
   5433 
   5434 	/* encode best devid possible based on data available */
   5435 	if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST,
   5436 	    (char *)ddi_driver_name(SD_DEVINFO(un)),
   5437 	    (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)),
   5438 	    inq80, inq80_len - inq80_resid, inq83, inq83_len -
   5439 	    inq83_resid, &un->un_devid) == DDI_SUCCESS) {
   5440 
   5441 		/* devid successfully encoded, register devid */
   5442 		(void) ddi_devid_register(SD_DEVINFO(un), un->un_devid);
   5443 
   5444 	} else {
   5445 		/*
   5446 		 * Unable to encode a devid based on data available.
   5447 		 * This is not a Sun qualified disk.  Older Sun disk
   5448 		 * drives that have the SD_FAB_DEVID property
   5449 		 * set in the disk_table and non Sun qualified
   5450 		 * disks are treated in the same manner.  These
   5451 		 * drives manage the devid's by storing them in
   5452 		 * last 2 available sectors on the drive and
   5453 		 * have them fabricated by the ddi layer by
   5454 		 * calling ddi_devid_init and passing the
   5455 		 * DEVID_FAB flag.
   5456 		 * Create a fabricate devid only if there's no
   5457 		 * fabricate devid existed.
   5458 		 */
   5459 		if (sd_get_devid(ssc) == EINVAL) {
   5460 			(void) sd_create_devid(ssc);
   5461 		}
   5462 		un->un_f_opt_fab_devid = TRUE;
   5463 
   5464 		/* Register the devid if it exists */
   5465 		if (un->un_devid != NULL) {
   5466 			(void) ddi_devid_register(SD_DEVINFO(un),
   5467 			    un->un_devid);
   5468 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   5469 			    "sd_register_devid: devid fabricated using "
   5470 			    "ddi framework\n");
   5471 		}
   5472 	}
   5473 
   5474 cleanup:
   5475 	/* clean up resources */
   5476 	if (inq80 != NULL) {
   5477 		kmem_free(inq80, inq80_len);
   5478 	}
   5479 	if (inq83 != NULL) {
   5480 		kmem_free(inq83, inq83_len);
   5481 	}
   5482 }
   5483 
   5484 
   5485 
   5486 /*
   5487  *    Function: sd_get_devid
   5488  *
   5489  * Description: This routine will return 0 if a valid device id has been
   5490  *		obtained from the target and stored in the soft state. If a
   5491  *		valid device id has not been previously read and stored, a
   5492  *		read attempt will be made.
   5493  *
   5494  *   Arguments: un - driver soft state (unit) structure
   5495  *
   5496  * Return Code: 0 if we successfully get the device id
   5497  *
   5498  *     Context: Kernel Thread
   5499  */
   5500 
   5501 static int
   5502 sd_get_devid(sd_ssc_t *ssc)
   5503 {
   5504 	struct dk_devid		*dkdevid;
   5505 	ddi_devid_t		tmpid;
   5506 	uint_t			*ip;
   5507 	size_t			sz;
   5508 	diskaddr_t		blk;
   5509 	int			status;
   5510 	int			chksum;
   5511 	int			i;
   5512 	size_t			buffer_size;
   5513 	struct sd_lun		*un;
   5514 
   5515 	ASSERT(ssc != NULL);
   5516 	un = ssc->ssc_un;
   5517 	ASSERT(un != NULL);
   5518 	ASSERT(mutex_owned(SD_MUTEX(un)));
   5519 
   5520 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n",
   5521 	    un);
   5522 
   5523 	if (un->un_devid != NULL) {
   5524 		return (0);
   5525 	}
   5526 
   5527 	mutex_exit(SD_MUTEX(un));
   5528 	if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
   5529 	    (void *)SD_PATH_DIRECT) != 0) {
   5530 		mutex_enter(SD_MUTEX(un));
   5531 		return (EINVAL);
   5532 	}
   5533 
   5534 	/*
   5535 	 * Read and verify device id, stored in the reserved cylinders at the
   5536 	 * end of the disk. Backup label is on the odd sectors of the last
   5537 	 * track of the last cylinder. Device id will be on track of the next
   5538 	 * to last cylinder.
   5539 	 */
   5540 	mutex_enter(SD_MUTEX(un));
   5541 	buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid));
   5542 	mutex_exit(SD_MUTEX(un));
   5543 	dkdevid = kmem_alloc(buffer_size, KM_SLEEP);
   5544 	status = sd_send_scsi_READ(ssc, dkdevid, buffer_size, blk,
   5545 	    SD_PATH_DIRECT);
   5546 
   5547 	if (status != 0) {
   5548 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   5549 		goto error;
   5550 	}
   5551 
   5552 	/* Validate the revision */
   5553 	if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
   5554 	    (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
   5555 		status = EINVAL;
   5556 		goto error;
   5557 	}
   5558 
   5559 	/* Calculate the checksum */
   5560 	chksum = 0;
   5561 	ip = (uint_t *)dkdevid;
   5562 	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
   5563 	    i++) {
   5564 		chksum ^= ip[i];
   5565 	}
   5566 
   5567 	/* Compare the checksums */
   5568 	if (DKD_GETCHKSUM(dkdevid) != chksum) {
   5569 		status = EINVAL;
   5570 		goto error;
   5571 	}
   5572 
   5573 	/* Validate the device id */
   5574 	if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
   5575 		status = EINVAL;
   5576 		goto error;
   5577 	}
   5578 
   5579 	/*
   5580 	 * Store the device id in the driver soft state
   5581 	 */
   5582 	sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
   5583 	tmpid = kmem_alloc(sz, KM_SLEEP);
   5584 
   5585 	mutex_enter(SD_MUTEX(un));
   5586 
   5587 	un->un_devid = tmpid;
   5588 	bcopy(&dkdevid->dkd_devid, un->un_devid, sz);
   5589 
   5590 	kmem_free(dkdevid, buffer_size);
   5591 
   5592 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un);
   5593 
   5594 	return (status);
   5595 error:
   5596 	mutex_enter(SD_MUTEX(un));
   5597 	kmem_free(dkdevid, buffer_size);
   5598 	return (status);
   5599 }
   5600 
   5601 
   5602 /*
   5603  *    Function: sd_create_devid
   5604  *
   5605  * Description: This routine will fabricate the device id and write it
   5606  *		to the disk.
   5607  *
   5608  *   Arguments: un - driver soft state (unit) structure
   5609  *
   5610  * Return Code: value of the fabricated device id
   5611  *
   5612  *     Context: Kernel Thread
   5613  */
   5614 
   5615 static ddi_devid_t
   5616 sd_create_devid(sd_ssc_t *ssc)
   5617 {
   5618 	struct sd_lun	*un;
   5619 
   5620 	ASSERT(ssc != NULL);
   5621 	un = ssc->ssc_un;
   5622 	ASSERT(un != NULL);
   5623 
   5624 	/* Fabricate the devid */
   5625 	if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid)
   5626 	    == DDI_FAILURE) {
   5627 		return (NULL);
   5628 	}
   5629 
   5630 	/* Write the devid to disk */
   5631 	if (sd_write_deviceid(ssc) != 0) {
   5632 		ddi_devid_free(un->un_devid);
   5633 		un->un_devid = NULL;
   5634 	}
   5635 
   5636 	return (un->un_devid);
   5637 }
   5638 
   5639 
   5640 /*
   5641  *    Function: sd_write_deviceid
   5642  *
   5643  * Description: This routine will write the device id to the disk
   5644  *		reserved sector.
   5645  *
   5646  *   Arguments: un - driver soft state (unit) structure
   5647  *
   5648  * Return Code: EINVAL
   5649  *		value returned by sd_send_scsi_cmd
   5650  *
   5651  *     Context: Kernel Thread
   5652  */
   5653 
   5654 static int
   5655 sd_write_deviceid(sd_ssc_t *ssc)
   5656 {
   5657 	struct dk_devid		*dkdevid;
   5658 	uchar_t			*buf;
   5659 	diskaddr_t		blk;
   5660 	uint_t			*ip, chksum;
   5661 	int			status;
   5662 	int			i;
   5663 	struct sd_lun		*un;
   5664 
   5665 	ASSERT(ssc != NULL);
   5666 	un = ssc->ssc_un;
   5667 	ASSERT(un != NULL);
   5668 	ASSERT(mutex_owned(SD_MUTEX(un)));
   5669 
   5670 	mutex_exit(SD_MUTEX(un));
   5671 	if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
   5672 	    (void *)SD_PATH_DIRECT) != 0) {
   5673 		mutex_enter(SD_MUTEX(un));
   5674 		return (-1);
   5675 	}
   5676 
   5677 
   5678 	/* Allocate the buffer */
   5679 	buf = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP);
   5680 	dkdevid = (struct dk_devid *)buf;
   5681 
   5682 	/* Fill in the revision */
   5683 	dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
   5684 	dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
   5685 
   5686 	/* Copy in the device id */
   5687 	mutex_enter(SD_MUTEX(un));
   5688 	bcopy(un->un_devid, &dkdevid->dkd_devid,
   5689 	    ddi_devid_sizeof(un->un_devid));
   5690 	mutex_exit(SD_MUTEX(un));
   5691 
   5692 	/* Calculate the checksum */
   5693 	chksum = 0;
   5694 	ip = (uint_t *)dkdevid;
   5695 	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
   5696 	    i++) {
   5697 		chksum ^= ip[i];
   5698 	}
   5699 
   5700 	/* Fill-in checksum */
   5701 	DKD_FORMCHKSUM(chksum, dkdevid);
   5702 
   5703 	/* Write the reserved sector */
   5704 	status = sd_send_scsi_WRITE(ssc, buf, un->un_sys_blocksize, blk,
   5705 	    SD_PATH_DIRECT);
   5706 	if (status != 0)
   5707 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   5708 
   5709 	kmem_free(buf, un->un_sys_blocksize);
   5710 
   5711 	mutex_enter(SD_MUTEX(un));
   5712 	return (status);
   5713 }
   5714 
   5715 
   5716 /*
   5717  *    Function: sd_check_vpd_page_support
   5718  *
   5719  * Description: This routine sends an inquiry command with the EVPD bit set and
   5720  *		a page code of 0x00 to the device. It is used to determine which
   5721  *		vital product pages are available to find the devid. We are
   5722  *		looking for pages 0x83 0x80 or 0xB1.  If we return a negative 1,
   5723  *		the device does not support that command.
   5724  *
   5725  *   Arguments: un  - driver soft state (unit) structure
   5726  *
   5727  * Return Code: 0 - success
   5728  *		1 - check condition
   5729  *
   5730  *     Context: This routine can sleep.
   5731  */
   5732 
   5733 static int
   5734 sd_check_vpd_page_support(sd_ssc_t *ssc)
   5735 {
   5736 	uchar_t	*page_list	= NULL;
   5737 	uchar_t	page_length	= 0xff;	/* Use max possible length */
   5738 	uchar_t	evpd		= 0x01;	/* Set the EVPD bit */
   5739 	uchar_t	page_code	= 0x00;	/* Supported VPD Pages */
   5740 	int    	rval		= 0;
   5741 	int	counter;
   5742 	struct sd_lun		*un;
   5743 
   5744 	ASSERT(ssc != NULL);
   5745 	un = ssc->ssc_un;
   5746 	ASSERT(un != NULL);
   5747 	ASSERT(mutex_owned(SD_MUTEX(un)));
   5748 
   5749 	mutex_exit(SD_MUTEX(un));
   5750 
   5751 	/*
   5752 	 * We'll set the page length to the maximum to save figuring it out
   5753 	 * with an additional call.
   5754 	 */
   5755 	page_list =  kmem_zalloc(page_length, KM_SLEEP);
   5756 
   5757 	rval = sd_send_scsi_INQUIRY(ssc, page_list, page_length, evpd,
   5758 	    page_code, NULL);
   5759 
   5760 	if (rval != 0)
   5761 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   5762 
   5763 	mutex_enter(SD_MUTEX(un));
   5764 
   5765 	/*
   5766 	 * Now we must validate that the device accepted the command, as some
   5767 	 * drives do not support it.  If the drive does support it, we will
   5768 	 * return 0, and the supported pages will be in un_vpd_page_mask.  If
   5769 	 * not, we return -1.
   5770 	 */
   5771 	if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) {
   5772 		/* Loop to find one of the 2 pages we need */
   5773 		counter = 4;  /* Supported pages start at byte 4, with 0x00 */
   5774 
   5775 		/*
   5776 		 * Pages are returned in ascending order, and 0x83 is what we
   5777 		 * are hoping for.
   5778 		 */
   5779 		while ((page_list[counter] <= 0xB1) &&
   5780 		    (counter <= (page_list[VPD_PAGE_LENGTH] +
   5781 		    VPD_HEAD_OFFSET))) {
   5782 			/*
   5783 			 * Add 3 because page_list[3] is the number of
   5784 			 * pages minus 3
   5785 			 */
   5786 
   5787 			switch (page_list[counter]) {
   5788 			case 0x00:
   5789 				un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG;
   5790 				break;
   5791 			case 0x80:
   5792 				un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG;
   5793 				break;
   5794 			case 0x81:
   5795 				un->un_vpd_page_mask |= SD_VPD_OPERATING_PG;
   5796 				break;
   5797 			case 0x82:
   5798 				un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG;
   5799 				break;
   5800 			case 0x83:
   5801 				un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG;
   5802 				break;
   5803 			case 0x86:
   5804 				un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG;
   5805 				break;
   5806 			case 0xB1:
   5807 				un->un_vpd_page_mask |= SD_VPD_DEV_CHARACTER_PG;
   5808 				break;
   5809 			}
   5810 			counter++;
   5811 		}
   5812 
   5813 	} else {
   5814 		rval = -1;
   5815 
   5816 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   5817 		    "sd_check_vpd_page_support: This drive does not implement "
   5818 		    "VPD pages.\n");
   5819 	}
   5820 
   5821 	kmem_free(page_list, page_length);
   5822 
   5823 	return (rval);
   5824 }
   5825 
   5826 
   5827 /*
   5828  *    Function: sd_setup_pm
   5829  *
   5830  * Description: Initialize Power Management on the device
   5831  *
   5832  *     Context: Kernel Thread
   5833  */
   5834 
   5835 static void
   5836 sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi)
   5837 {
   5838 	uint_t		log_page_size;
   5839 	uchar_t		*log_page_data;
   5840 	int		rval = 0;
   5841 	struct sd_lun	*un;
   5842 
   5843 	ASSERT(ssc != NULL);
   5844 	un = ssc->ssc_un;
   5845 	ASSERT(un != NULL);
   5846 
   5847 	/*
   5848 	 * Since we are called from attach, holding a mutex for
   5849 	 * un is unnecessary. Because some of the routines called
   5850 	 * from here require SD_MUTEX to not be held, assert this
   5851 	 * right up front.
   5852 	 */
   5853 	ASSERT(!mutex_owned(SD_MUTEX(un)));
   5854 	/*
   5855 	 * Since the sd device does not have the 'reg' property,
   5856 	 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries.
   5857 	 * The following code is to tell cpr that this device
   5858 	 * DOES need to be suspended and resumed.
   5859 	 */
   5860 	(void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
   5861 	    "pm-hardware-state", "needs-suspend-resume");
   5862 
   5863 	/*
   5864 	 * This complies with the new power management framework
   5865 	 * for certain desktop machines. Create the pm_components
   5866 	 * property as a string array property.
   5867 	 * If un_f_pm_supported is TRUE, that means the disk
   5868 	 * attached HBA has set the "pm-capable" property and
   5869 	 * the value of this property is bigger than 0.
   5870 	 */
   5871 	if (un->un_f_pm_supported) {
   5872 		/*
   5873 		 * not all devices have a motor, try it first.
   5874 		 * some devices may return ILLEGAL REQUEST, some
   5875 		 * will hang
   5876 		 * The following START_STOP_UNIT is used to check if target
   5877 		 * device has a motor.
   5878 		 */
   5879 		un->un_f_start_stop_supported = TRUE;
   5880 
   5881 		if (un->un_f_power_condition_supported) {
   5882 			rval = sd_send_scsi_START_STOP_UNIT(ssc,
   5883 			    SD_POWER_CONDITION, SD_TARGET_ACTIVE,
   5884 			    SD_PATH_DIRECT);
   5885 			if (rval != 0) {
   5886 				un->un_f_power_condition_supported = FALSE;
   5887 			}
   5888 		}
   5889 		if (!un->un_f_power_condition_supported) {
   5890 			rval = sd_send_scsi_START_STOP_UNIT(ssc,
   5891 			    SD_START_STOP, SD_TARGET_START, SD_PATH_DIRECT);
   5892 		}
   5893 		if (rval != 0) {
   5894 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   5895 			un->un_f_start_stop_supported = FALSE;
   5896 		}
   5897 
   5898 		/*
   5899 		 * create pm properties anyways otherwise the parent can't
   5900 		 * go to sleep
   5901 		 */
   5902 		un->un_f_pm_is_enabled = TRUE;
   5903 		(void) sd_create_pm_components(devi, un);
   5904 
   5905 		/*
   5906 		 * If it claims that log sense is supported, check it out.
   5907 		 */
   5908 		if (un->un_f_log_sense_supported) {
   5909 			rval = sd_log_page_supported(ssc,
   5910 			    START_STOP_CYCLE_PAGE);
   5911 			if (rval == 1) {
   5912 				/* Page found, use it. */
   5913 				un->un_start_stop_cycle_page =
   5914 				    START_STOP_CYCLE_PAGE;
   5915 			} else {
   5916 				/*
   5917 				 * Page not found or log sense is not
   5918 				 * supported.
   5919 				 * Notice we do not check the old style
   5920 				 * START_STOP_CYCLE_VU_PAGE because this
   5921 				 * code path does not apply to old disks.
   5922 				 */
   5923 				un->un_f_log_sense_supported = FALSE;
   5924 				un->un_f_pm_log_sense_smart = FALSE;
   5925 			}
   5926 		}
   5927 
   5928 		return;
   5929 	}
   5930 
   5931 	/*
   5932 	 * For the disk whose attached HBA has not set the "pm-capable"
   5933 	 * property, check if it supports the power management.
   5934 	 */
   5935 	if (!un->un_f_log_sense_supported) {
   5936 		un->un_power_level = SD_SPINDLE_ON;
   5937 		un->un_f_pm_is_enabled = FALSE;
   5938 		return;
   5939 	}
   5940 
   5941 	rval = sd_log_page_supported(ssc, START_STOP_CYCLE_PAGE);
   5942 
   5943 #ifdef	SDDEBUG
   5944 	if (sd_force_pm_supported) {
   5945 		/* Force a successful result */
   5946 		rval = 1;
   5947 	}
   5948 #endif
   5949 
   5950 	/*
   5951 	 * If the start-stop cycle counter log page is not supported
   5952 	 * or if the pm-capable property is set to be false (0),
   5953 	 * then we should not create the pm_components property.
   5954 	 */
   5955 	if (rval == -1) {
   5956 		/*
   5957 		 * Error.
   5958 		 * Reading log sense failed, most likely this is
   5959 		 * an older drive that does not support log sense.
   5960 		 * If this fails auto-pm is not supported.
   5961 		 */
   5962 		un->un_power_level = SD_SPINDLE_ON;
   5963 		un->un_f_pm_is_enabled = FALSE;
   5964 
   5965 	} else if (rval == 0) {
   5966 		/*
   5967 		 * Page not found.
   5968 		 * The start stop cycle counter is implemented as page
   5969 		 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For
   5970 		 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE).
   5971 		 */
   5972 		if (sd_log_page_supported(ssc, START_STOP_CYCLE_VU_PAGE) == 1) {
   5973 			/*
   5974 			 * Page found, use this one.
   5975 			 */
   5976 			un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE;
   5977 			un->un_f_pm_is_enabled = TRUE;
   5978 		} else {
   5979 			/*
   5980 			 * Error or page not found.
   5981 			 * auto-pm is not supported for this device.
   5982 			 */
   5983 			un->un_power_level = SD_SPINDLE_ON;
   5984 			un->un_f_pm_is_enabled = FALSE;
   5985 		}
   5986 	} else {
   5987 		/*
   5988 		 * Page found, use it.
   5989 		 */
   5990 		un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE;
   5991 		un->un_f_pm_is_enabled = TRUE;
   5992 	}
   5993 
   5994 
   5995 	if (un->un_f_pm_is_enabled == TRUE) {
   5996 		log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
   5997 		log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
   5998 
   5999 		rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
   6000 		    log_page_size, un->un_start_stop_cycle_page,
   6001 		    0x01, 0, SD_PATH_DIRECT);
   6002 
   6003 		if (rval != 0) {
   6004 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   6005 		}
   6006 
   6007 #ifdef	SDDEBUG
   6008 		if (sd_force_pm_supported) {
   6009 			/* Force a successful result */
   6010 			rval = 0;
   6011 		}
   6012 #endif
   6013 
   6014 		/*
   6015 		 * If the Log sense for Page( Start/stop cycle counter page)
   6016 		 * succeeds, then power management is supported and we can
   6017 		 * enable auto-pm.
   6018 		 */
   6019 		if (rval == 0)  {
   6020 			(void) sd_create_pm_components(devi, un);
   6021 		} else {
   6022 			un->un_power_level = SD_SPINDLE_ON;
   6023 			un->un_f_pm_is_enabled = FALSE;
   6024 		}
   6025 
   6026 		kmem_free(log_page_data, log_page_size);
   6027 	}
   6028 }
   6029 
   6030 
   6031 /*
   6032  *    Function: sd_create_pm_components
   6033  *
   6034  * Description: Initialize PM property.
   6035  *
   6036  *     Context: Kernel thread context
   6037  */
   6038 
   6039 static void
   6040 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un)
   6041 {
   6042 	ASSERT(!mutex_owned(SD_MUTEX(un)));
   6043 
   6044 	if (un->un_f_power_condition_supported) {
   6045 		if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
   6046 		    "pm-components", sd_pwr_pc.pm_comp, 5)
   6047 		    != DDI_PROP_SUCCESS) {
   6048 			un->un_power_level = SD_SPINDLE_ACTIVE;
   6049 			un->un_f_pm_is_enabled = FALSE;
   6050 			return;
   6051 		}
   6052 	} else {
   6053 		if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
   6054 		    "pm-components", sd_pwr_ss.pm_comp, 3)
   6055 		    != DDI_PROP_SUCCESS) {
   6056 			un->un_power_level = SD_SPINDLE_ON;
   6057 			un->un_f_pm_is_enabled = FALSE;
   6058 			return;
   6059 		}
   6060 	}
   6061 	/*
   6062 	 * When components are initially created they are idle,
   6063 	 * power up any non-removables.
   6064 	 * Note: the return value of pm_raise_power can't be used
   6065 	 * for determining if PM should be enabled for this device.
   6066 	 * Even if you check the return values and remove this
   6067 	 * property created above, the PM framework will not honor the
   6068 	 * change after the first call to pm_raise_power. Hence,
   6069 	 * removal of that property does not help if pm_raise_power
   6070 	 * fails. In the case of removable media, the start/stop
   6071 	 * will fail if the media is not present.
   6072 	 */
   6073 	if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0,
   6074 	    SD_PM_STATE_ACTIVE(un)) == DDI_SUCCESS)) {
   6075 		mutex_enter(SD_MUTEX(un));
   6076 		un->un_power_level = SD_PM_STATE_ACTIVE(un);
   6077 		mutex_enter(&un->un_pm_mutex);
   6078 		/* Set to on and not busy. */
   6079 		un->un_pm_count = 0;
   6080 	} else {
   6081 		mutex_enter(SD_MUTEX(un));
   6082 		un->un_power_level = SD_PM_STATE_STOPPED(un);
   6083 		mutex_enter(&un->un_pm_mutex);
   6084 		/* Set to off. */
   6085 		un->un_pm_count = -1;
   6086 	}
   6087 	mutex_exit(&un->un_pm_mutex);
   6088 	mutex_exit(SD_MUTEX(un));
   6089 }
   6090 
   6091 
   6092 /*
   6093  *    Function: sd_ddi_suspend
   6094  *
   6095  * Description: Performs system power-down operations. This includes
   6096  *		setting the drive state to indicate its suspended so
   6097  *		that no new commands will be accepted. Also, wait for
   6098  *		all commands that are in transport or queued to a timer
   6099  *		for retry to complete. All timeout threads are cancelled.
   6100  *
   6101  * Return Code: DDI_FAILURE or DDI_SUCCESS
   6102  *
   6103  *     Context: Kernel thread context
   6104  */
   6105 
   6106 static int
   6107 sd_ddi_suspend(dev_info_t *devi)
   6108 {
   6109 	struct	sd_lun	*un;
   6110 	clock_t		wait_cmds_complete;
   6111 
   6112 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
   6113 	if (un == NULL) {
   6114 		return (DDI_FAILURE);
   6115 	}
   6116 
   6117 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n");
   6118 
   6119 	mutex_enter(SD_MUTEX(un));
   6120 
   6121 	/* Return success if the device is already suspended. */
   6122 	if (un->un_state == SD_STATE_SUSPENDED) {
   6123 		mutex_exit(SD_MUTEX(un));
   6124 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
   6125 		    "device already suspended, exiting\n");
   6126 		return (DDI_SUCCESS);
   6127 	}
   6128 
   6129 	/* Return failure if the device is being used by HA */
   6130 	if (un->un_resvd_status &
   6131 	    (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) {
   6132 		mutex_exit(SD_MUTEX(un));
   6133 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
   6134 		    "device in use by HA, exiting\n");
   6135 		return (DDI_FAILURE);
   6136 	}
   6137 
   6138 	/*
   6139 	 * Return failure if the device is in a resource wait
   6140 	 * or power changing state.
   6141 	 */
   6142 	if ((un->un_state == SD_STATE_RWAIT) ||
   6143 	    (un->un_state == SD_STATE_PM_CHANGING)) {
   6144 		mutex_exit(SD_MUTEX(un));
   6145 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
   6146 		    "device in resource wait state, exiting\n");
   6147 		return (DDI_FAILURE);
   6148 	}
   6149 
   6150 
   6151 	un->un_save_state = un->un_last_state;
   6152 	New_state(un, SD_STATE_SUSPENDED);
   6153 
   6154 	/*
   6155 	 * Wait for all commands that are in transport or queued to a timer
   6156 	 * for retry to complete.
   6157 	 *
   6158 	 * While waiting, no new commands will be accepted or sent because of
   6159 	 * the new state we set above.
   6160 	 *
   6161 	 * Wait till current operation has completed. If we are in the resource
   6162 	 * wait state (with an intr outstanding) then we need to wait till the
   6163 	 * intr completes and starts the next cmd. We want to wait for
   6164 	 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND.
   6165 	 */
   6166 	wait_cmds_complete = ddi_get_lbolt() +
   6167 	    (sd_wait_cmds_complete * drv_usectohz(1000000));
   6168 
   6169 	while (un->un_ncmds_in_transport != 0) {
   6170 		/*
   6171 		 * Fail if commands do not finish in the specified time.
   6172 		 */
   6173 		if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un),
   6174 		    wait_cmds_complete) == -1) {
   6175 			/*
   6176 			 * Undo the state changes made above. Everything
   6177 			 * must go back to it's original value.
   6178 			 */
   6179 			Restore_state(un);
   6180 			un->un_last_state = un->un_save_state;
   6181 			/* Wake up any threads that might be waiting. */
   6182 			cv_broadcast(&un->un_suspend_cv);
   6183 			mutex_exit(SD_MUTEX(un));
   6184 			SD_ERROR(SD_LOG_IO_PM, un,
   6185 			    "sd_ddi_suspend: failed due to outstanding cmds\n");
   6186 			SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n");
   6187 			return (DDI_FAILURE);
   6188 		}
   6189 	}
   6190 
   6191 	/*
   6192 	 * Cancel SCSI watch thread and timeouts, if any are active
   6193 	 */
   6194 
   6195 	if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) {
   6196 		opaque_t temp_token = un->un_swr_token;
   6197 		mutex_exit(SD_MUTEX(un));
   6198 		scsi_watch_suspend(temp_token);
   6199 		mutex_enter(SD_MUTEX(un));
   6200 	}
   6201 
   6202 	if (un->un_reset_throttle_timeid != NULL) {
   6203 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
   6204 		un->un_reset_throttle_timeid = NULL;
   6205 		mutex_exit(SD_MUTEX(un));
   6206 		(void) untimeout(temp_id);
   6207 		mutex_enter(SD_MUTEX(un));
   6208 	}
   6209 
   6210 	if (un->un_dcvb_timeid != NULL) {
   6211 		timeout_id_t temp_id = un->un_dcvb_timeid;
   6212 		un->un_dcvb_timeid = NULL;
   6213 		mutex_exit(SD_MUTEX(un));
   6214 		(void) untimeout(temp_id);
   6215 		mutex_enter(SD_MUTEX(un));
   6216 	}
   6217 
   6218 	mutex_enter(&un->un_pm_mutex);
   6219 	if (un->un_pm_timeid != NULL) {
   6220 		timeout_id_t temp_id = un->un_pm_timeid;
   6221 		un->un_pm_timeid = NULL;
   6222 		mutex_exit(&un->un_pm_mutex);
   6223 		mutex_exit(SD_MUTEX(un));
   6224 		(void) untimeout(temp_id);
   6225 		mutex_enter(SD_MUTEX(un));
   6226 	} else {
   6227 		mutex_exit(&un->un_pm_mutex);
   6228 	}
   6229 
   6230 	if (un->un_rmw_msg_timeid != NULL) {
   6231 		timeout_id_t temp_id = un->un_rmw_msg_timeid;
   6232 		un->un_rmw_msg_timeid = NULL;
   6233 		mutex_exit(SD_MUTEX(un));
   6234 		(void) untimeout(temp_id);
   6235 		mutex_enter(SD_MUTEX(un));
   6236 	}
   6237 
   6238 	if (un->un_retry_timeid != NULL) {
   6239 		timeout_id_t temp_id = un->un_retry_timeid;
   6240 		un->un_retry_timeid = NULL;
   6241 		mutex_exit(SD_MUTEX(un));
   6242 		(void) untimeout(temp_id);
   6243 		mutex_enter(SD_MUTEX(un));
   6244 
   6245 		if (un->un_retry_bp != NULL) {
   6246 			un->un_retry_bp->av_forw = un->un_waitq_headp;
   6247 			un->un_waitq_headp = un->un_retry_bp;
   6248 			if (un->un_waitq_tailp == NULL) {
   6249 				un->un_waitq_tailp = un->un_retry_bp;
   6250 			}
   6251 			un->un_retry_bp = NULL;
   6252 			un->un_retry_statp = NULL;
   6253 		}
   6254 	}
   6255 
   6256 	if (un->un_direct_priority_timeid != NULL) {
   6257 		timeout_id_t temp_id = un->un_direct_priority_timeid;
   6258 		un->un_direct_priority_timeid = NULL;
   6259 		mutex_exit(SD_MUTEX(un));
   6260 		(void) untimeout(temp_id);
   6261 		mutex_enter(SD_MUTEX(un));
   6262 	}
   6263 
   6264 	if (un->un_f_is_fibre == TRUE) {
   6265 		/*
   6266 		 * Remove callbacks for insert and remove events
   6267 		 */
   6268 		if (un->un_insert_event != NULL) {
   6269 			mutex_exit(SD_MUTEX(un));
   6270 			(void) ddi_remove_event_handler(un->un_insert_cb_id);
   6271 			mutex_enter(SD_MUTEX(un));
   6272 			un->un_insert_event = NULL;
   6273 		}
   6274 
   6275 		if (un->un_remove_event != NULL) {
   6276 			mutex_exit(SD_MUTEX(un));
   6277 			(void) ddi_remove_event_handler(un->un_remove_cb_id);
   6278 			mutex_enter(SD_MUTEX(un));
   6279 			un->un_remove_event = NULL;
   6280 		}
   6281 	}
   6282 
   6283 	mutex_exit(SD_MUTEX(un));
   6284 
   6285 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n");
   6286 
   6287 	return (DDI_SUCCESS);
   6288 }
   6289 
   6290 
   6291 /*
   6292  *    Function: sd_ddi_resume
   6293  *
   6294  * Description: Performs system power-up operations..
   6295  *
   6296  * Return Code: DDI_SUCCESS
   6297  *		DDI_FAILURE
   6298  *
   6299  *     Context: Kernel thread context
   6300  */
   6301 
   6302 static int
   6303 sd_ddi_resume(dev_info_t *devi)
   6304 {
   6305 	struct	sd_lun	*un;
   6306 
   6307 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
   6308 	if (un == NULL) {
   6309 		return (DDI_FAILURE);
   6310 	}
   6311 
   6312 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n");
   6313 
   6314 	mutex_enter(SD_MUTEX(un));
   6315 	Restore_state(un);
   6316 
   6317 	/*
   6318 	 * Restore the state which was saved to give the
   6319 	 * the right state in un_last_state
   6320 	 */
   6321 	un->un_last_state = un->un_save_state;
   6322 	/*
   6323 	 * Note: throttle comes back at full.
   6324 	 * Also note: this MUST be done before calling pm_raise_power
   6325 	 * otherwise the system can get hung in biowait. The scenario where
   6326 	 * this'll happen is under cpr suspend. Writing of the system
   6327 	 * state goes through sddump, which writes 0 to un_throttle. If
   6328 	 * writing the system state then fails, example if the partition is
   6329 	 * too small, then cpr attempts a resume. If throttle isn't restored
   6330 	 * from the saved value until after calling pm_raise_power then
   6331 	 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs
   6332 	 * in biowait.
   6333 	 */
   6334 	un->un_throttle = un->un_saved_throttle;
   6335 
   6336 	/*
   6337 	 * The chance of failure is very rare as the only command done in power
   6338 	 * entry point is START command when you transition from 0->1 or
   6339 	 * unknown->1. Put it to SPINDLE ON state irrespective of the state at
   6340 	 * which suspend was done. Ignore the return value as the resume should
   6341 	 * not be failed. In the case of removable media the media need not be
   6342 	 * inserted and hence there is a chance that raise power will fail with
   6343 	 * media not present.
   6344 	 */
   6345 	if (un->un_f_attach_spinup) {
   6346 		mutex_exit(SD_MUTEX(un));
   6347 		(void) pm_raise_power(SD_DEVINFO(un), 0,
   6348 		    SD_PM_STATE_ACTIVE(un));
   6349 		mutex_enter(SD_MUTEX(un));
   6350 	}
   6351 
   6352 	/*
   6353 	 * Don't broadcast to the suspend cv and therefore possibly
   6354 	 * start I/O until after power has been restored.
   6355 	 */
   6356 	cv_broadcast(&un->un_suspend_cv);
   6357 	cv_broadcast(&un->un_state_cv);
   6358 
   6359 	/* restart thread */
   6360 	if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) {
   6361 		scsi_watch_resume(un->un_swr_token);
   6362 	}
   6363 
   6364 #if (defined(__fibre))
   6365 	if (un->un_f_is_fibre == TRUE) {
   6366 		/*
   6367 		 * Add callbacks for insert and remove events
   6368 		 */
   6369 		if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
   6370 			sd_init_event_callbacks(un);
   6371 		}
   6372 	}
   6373 #endif
   6374 
   6375 	/*
   6376 	 * Transport any pending commands to the target.
   6377 	 *
   6378 	 * If this is a low-activity device commands in queue will have to wait
   6379 	 * until new commands come in, which may take awhile. Also, we
   6380 	 * specifically don't check un_ncmds_in_transport because we know that
   6381 	 * there really are no commands in progress after the unit was
   6382 	 * suspended and we could have reached the throttle level, been
   6383 	 * suspended, and have no new commands coming in for awhile. Highly
   6384 	 * unlikely, but so is the low-activity disk scenario.
   6385 	 */
   6386 	ddi_xbuf_dispatch(un->un_xbuf_attr);
   6387 
   6388 	sd_start_cmds(un, NULL);
   6389 	mutex_exit(SD_MUTEX(un));
   6390 
   6391 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n");
   6392 
   6393 	return (DDI_SUCCESS);
   6394 }
   6395 
   6396 
   6397 /*
   6398  *    Function: sd_pm_state_change
   6399  *
   6400  * Description: Change the driver power state.
   6401  * 		Someone else is required to actually change the driver
   6402  * 		power level.
   6403  *
   6404  *   Arguments: un - driver soft state (unit) structure
   6405  *              level - the power level that is changed to
   6406  *              flag - to decide how to change the power state
   6407  *
   6408  * Return Code: DDI_SUCCESS
   6409  *
   6410  *     Context: Kernel thread context
   6411  */
   6412 static int
   6413 sd_pm_state_change(struct sd_lun *un, int level, int flag)
   6414 {
   6415 	ASSERT(un != NULL);
   6416 	SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: entry\n");
   6417 
   6418 	ASSERT(!mutex_owned(SD_MUTEX(un)));
   6419 	mutex_enter(SD_MUTEX(un));
   6420 
   6421 	if (flag == SD_PM_STATE_ROLLBACK || SD_PM_IS_IO_CAPABLE(un, level)) {
   6422 		un->un_power_level = level;
   6423 		ASSERT(!mutex_owned(&un->un_pm_mutex));
   6424 		mutex_enter(&un->un_pm_mutex);
   6425 		if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
   6426 			un->un_pm_count++;
   6427 			ASSERT(un->un_pm_count == 0);
   6428 		}
   6429 		mutex_exit(&un->un_pm_mutex);
   6430 	} else {
   6431 		/*
   6432 		 * Exit if power management is not enabled for this device,
   6433 		 * or if the device is being used by HA.
   6434 		 */
   6435 		if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status &
   6436 		    (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) {
   6437 			mutex_exit(SD_MUTEX(un));
   6438 			SD_TRACE(SD_LOG_POWER, un,
   6439 			    "sd_pm_state_change: exiting\n");
   6440 			return (DDI_FAILURE);
   6441 		}
   6442 
   6443 		SD_INFO(SD_LOG_POWER, un, "sd_pm_state_change: "
   6444 		    "un_ncmds_in_driver=%ld\n", un->un_ncmds_in_driver);
   6445 
   6446 		/*
   6447 		 * See if the device is not busy, ie.:
   6448 		 *    - we have no commands in the driver for this device
   6449 		 *    - not waiting for resources
   6450 		 */
   6451 		if ((un->un_ncmds_in_driver == 0) &&
   6452 		    (un->un_state != SD_STATE_RWAIT)) {
   6453 			/*
   6454 			 * The device is not busy, so it is OK to go to low
   6455 			 * power state. Indicate low power, but rely on someone
   6456 			 * else to actually change it.
   6457 			 */
   6458 			mutex_enter(&un->un_pm_mutex);
   6459 			un->un_pm_count = -1;
   6460 			mutex_exit(&un->un_pm_mutex);
   6461 			un->un_power_level = level;
   6462 		}
   6463 	}
   6464 
   6465 	mutex_exit(SD_MUTEX(un));
   6466 
   6467 	SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: exit\n");
   6468 
   6469 	return (DDI_SUCCESS);
   6470 }
   6471 
   6472 
   6473 /*
   6474  *    Function: sd_pm_idletimeout_handler
   6475  *
   6476  * Description: A timer routine that's active only while a device is busy.
   6477  *		The purpose is to extend slightly the pm framework's busy
   6478  *		view of the device to prevent busy/idle thrashing for
   6479  *		back-to-back commands. Do this by comparing the current time
   6480  *		to the time at which the last command completed and when the
   6481  *		difference is greater than sd_pm_idletime, call
   6482  *		pm_idle_component. In addition to indicating idle to the pm
   6483  *		framework, update the chain type to again use the internal pm
   6484  *		layers of the driver.
   6485  *
   6486  *   Arguments: arg - driver soft state (unit) structure
   6487  *
   6488  *     Context: Executes in a timeout(9F) thread context
   6489  */
   6490 
   6491 static void
   6492 sd_pm_idletimeout_handler(void *arg)
   6493 {
   6494 	struct sd_lun *un = arg;
   6495 
   6496 	time_t	now;
   6497 
   6498 	mutex_enter(&sd_detach_mutex);
   6499 	if (un->un_detach_count != 0) {
   6500 		/* Abort if the instance is detaching */
   6501 		mutex_exit(&sd_detach_mutex);
   6502 		return;
   6503 	}
   6504 	mutex_exit(&sd_detach_mutex);
   6505 
   6506 	now = ddi_get_time();
   6507 	/*
   6508 	 * Grab both mutexes, in the proper order, since we're accessing
   6509 	 * both PM and softstate variables.
   6510 	 */
   6511 	mutex_enter(SD_MUTEX(un));
   6512 	mutex_enter(&un->un_pm_mutex);
   6513 	if (((now - un->un_pm_idle_time) > sd_pm_idletime) &&
   6514 	    (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) {
   6515 		/*
   6516 		 * Update the chain types.
   6517 		 * This takes affect on the next new command received.
   6518 		 */
   6519 		if (un->un_f_non_devbsize_supported) {
   6520 			un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
   6521 		} else {
   6522 			un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
   6523 		}
   6524 		un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD;
   6525 
   6526 		SD_TRACE(SD_LOG_IO_PM, un,
   6527 		    "sd_pm_idletimeout_handler: idling device\n");
   6528 		(void) pm_idle_component(SD_DEVINFO(un), 0);
   6529 		un->un_pm_idle_timeid = NULL;
   6530 	} else {
   6531 		un->un_pm_idle_timeid =
   6532 		    timeout(sd_pm_idletimeout_handler, un,
   6533 		    (drv_usectohz((clock_t)300000))); /* 300 ms. */
   6534 	}
   6535 	mutex_exit(&un->un_pm_mutex);
   6536 	mutex_exit(SD_MUTEX(un));
   6537 }
   6538 
   6539 
   6540 /*
   6541  *    Function: sd_pm_timeout_handler
   6542  *
   6543  * Description: Callback to tell framework we are idle.
   6544  *
   6545  *     Context: timeout(9f) thread context.
   6546  */
   6547 
   6548 static void
   6549 sd_pm_timeout_handler(void *arg)
   6550 {
   6551 	struct sd_lun *un = arg;
   6552 
   6553 	(void) pm_idle_component(SD_DEVINFO(un), 0);
   6554 	mutex_enter(&un->un_pm_mutex);
   6555 	un->un_pm_timeid = NULL;
   6556 	mutex_exit(&un->un_pm_mutex);
   6557 }
   6558 
   6559 
   6560 /*
   6561  *    Function: sdpower
   6562  *
   6563  * Description: PM entry point.
   6564  *
   6565  * Return Code: DDI_SUCCESS
   6566  *		DDI_FAILURE
   6567  *
   6568  *     Context: Kernel thread context
   6569  */
   6570 
   6571 static int
   6572 sdpower(dev_info_t *devi, int component, int level)
   6573 {
   6574 	struct sd_lun	*un;
   6575 	int		instance;
   6576 	int		rval = DDI_SUCCESS;
   6577 	uint_t		i, log_page_size, maxcycles, ncycles;
   6578 	uchar_t		*log_page_data;
   6579 	int		log_sense_page;
   6580 	int		medium_present;
   6581 	time_t		intvlp;
   6582 	struct pm_trans_data	sd_pm_tran_data;
   6583 	uchar_t		save_state;
   6584 	int		sval;
   6585 	uchar_t		state_before_pm;
   6586 	int		got_semaphore_here;
   6587 	sd_ssc_t	*ssc;
   6588 	int	last_power_level;
   6589 
   6590 	instance = ddi_get_instance(devi);
   6591 
   6592 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
   6593 	    !SD_PM_IS_LEVEL_VALID(un, level) || component != 0) {
   6594 		return (DDI_FAILURE);
   6595 	}
   6596 
   6597 	ssc = sd_ssc_init(un);
   6598 
   6599 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level);
   6600 
   6601 	/*
   6602 	 * Must synchronize power down with close.
   6603 	 * Attempt to decrement/acquire the open/close semaphore,
   6604 	 * but do NOT wait on it. If it's not greater than zero,
   6605 	 * ie. it can't be decremented without waiting, then
   6606 	 * someone else, either open or close, already has it
   6607 	 * and the try returns 0. Use that knowledge here to determine
   6608 	 * if it's OK to change the device power level.
   6609 	 * Also, only increment it on exit if it was decremented, ie. gotten,
   6610 	 * here.
   6611 	 */
   6612 	got_semaphore_here = sema_tryp(&un->un_semoclose);
   6613 
   6614 	mutex_enter(SD_MUTEX(un));
   6615 
   6616 	SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n",
   6617 	    un->un_ncmds_in_driver);
   6618 
   6619 	/*
   6620 	 * If un_ncmds_in_driver is non-zero it indicates commands are
   6621 	 * already being processed in the driver, or if the semaphore was
   6622 	 * not gotten here it indicates an open or close is being processed.
   6623 	 * At the same time somebody is requesting to go to a lower power
   6624 	 * that can't perform I/O, which can't happen, therefore we need to
   6625 	 * return failure.
   6626 	 */
   6627 	if ((!SD_PM_IS_IO_CAPABLE(un, level)) &&
   6628 	    ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) {
   6629 		mutex_exit(SD_MUTEX(un));
   6630 
   6631 		if (got_semaphore_here != 0) {
   6632 			sema_v(&un->un_semoclose);
   6633 		}
   6634 		SD_TRACE(SD_LOG_IO_PM, un,
   6635 		    "sdpower: exit, device has queued cmds.\n");
   6636 
   6637 		goto sdpower_failed;
   6638 	}
   6639 
   6640 	/*
   6641 	 * if it is OFFLINE that means the disk is completely dead
   6642 	 * in our case we have to put the disk in on or off by sending commands
   6643 	 * Of course that will fail anyway so return back here.
   6644 	 *
   6645 	 * Power changes to a device that's OFFLINE or SUSPENDED
   6646 	 * are not allowed.
   6647 	 */
   6648 	if ((un->un_state == SD_STATE_OFFLINE) ||
   6649 	    (un->un_state == SD_STATE_SUSPENDED)) {
   6650 		mutex_exit(SD_MUTEX(un));
   6651 
   6652 		if (got_semaphore_here != 0) {
   6653 			sema_v(&un->un_semoclose);
   6654 		}
   6655 		SD_TRACE(SD_LOG_IO_PM, un,
   6656 		    "sdpower: exit, device is off-line.\n");
   6657 
   6658 		goto sdpower_failed;
   6659 	}
   6660 
   6661 	/*
   6662 	 * Change the device's state to indicate it's power level
   6663 	 * is being changed. Do this to prevent a power off in the
   6664 	 * middle of commands, which is especially bad on devices
   6665 	 * that are really powered off instead of just spun down.
   6666 	 */
   6667 	state_before_pm = un->un_state;
   6668 	un->un_state = SD_STATE_PM_CHANGING;
   6669 
   6670 	mutex_exit(SD_MUTEX(un));
   6671 
   6672 	/*
   6673 	 * If log sense command is not supported, bypass the
   6674 	 * following checking, otherwise, check the log sense
   6675 	 * information for this device.
   6676 	 */
   6677 	if (SD_PM_STOP_MOTOR_NEEDED(un, level) &&
   6678 	    un->un_f_log_sense_supported) {
   6679 		/*
   6680 		 * Get the log sense information to understand whether the
   6681 		 * the powercycle counts have gone beyond the threshhold.
   6682 		 */
   6683 		log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
   6684 		log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
   6685 
   6686 		mutex_enter(SD_MUTEX(un));
   6687 		log_sense_page = un->un_start_stop_cycle_page;
   6688 		mutex_exit(SD_MUTEX(un));
   6689 
   6690 		rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
   6691 		    log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT);
   6692 
   6693 		if (rval != 0) {
   6694 			if (rval == EIO)
   6695 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
   6696 			else
   6697 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   6698 		}
   6699 
   6700 #ifdef	SDDEBUG
   6701 		if (sd_force_pm_supported) {
   6702 			/* Force a successful result */
   6703 			rval = 0;
   6704 		}
   6705 #endif
   6706 		if (rval != 0) {
   6707 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
   6708 			    "Log Sense Failed\n");
   6709 
   6710 			kmem_free(log_page_data, log_page_size);
   6711 			/* Cannot support power management on those drives */
   6712 
   6713 			if (got_semaphore_here != 0) {
   6714 				sema_v(&un->un_semoclose);
   6715 			}
   6716 			/*
   6717 			 * On exit put the state back to it's original value
   6718 			 * and broadcast to anyone waiting for the power
   6719 			 * change completion.
   6720 			 */
   6721 			mutex_enter(SD_MUTEX(un));
   6722 			un->un_state = state_before_pm;
   6723 			cv_broadcast(&un->un_suspend_cv);
   6724 			mutex_exit(SD_MUTEX(un));
   6725 			SD_TRACE(SD_LOG_IO_PM, un,
   6726 			    "sdpower: exit, Log Sense Failed.\n");
   6727 
   6728 			goto sdpower_failed;
   6729 		}
   6730 
   6731 		/*
   6732 		 * From the page data - Convert the essential information to
   6733 		 * pm_trans_data
   6734 		 */
   6735 		maxcycles =
   6736 		    (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) |
   6737 		    (log_page_data[0x1E] << 8)  | log_page_data[0x1F];
   6738 
   6739 		ncycles =
   6740 		    (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) |
   6741 		    (log_page_data[0x26] << 8)  | log_page_data[0x27];
   6742 
   6743 		if (un->un_f_pm_log_sense_smart) {
   6744 			sd_pm_tran_data.un.smart_count.allowed = maxcycles;
   6745 			sd_pm_tran_data.un.smart_count.consumed = ncycles;
   6746 			sd_pm_tran_data.un.smart_count.flag = 0;
   6747 			sd_pm_tran_data.format = DC_SMART_FORMAT;
   6748 		} else {
   6749 			sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles;
   6750 			sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles;
   6751 			for (i = 0; i < DC_SCSI_MFR_LEN; i++) {
   6752 				sd_pm_tran_data.un.scsi_cycles.svc_date[i] =
   6753 				    log_page_data[8+i];
   6754 			}
   6755 			sd_pm_tran_data.un.scsi_cycles.flag = 0;
   6756 			sd_pm_tran_data.format = DC_SCSI_FORMAT;
   6757 		}
   6758 
   6759 		kmem_free(log_page_data, log_page_size);
   6760 
   6761 		/*
   6762 		 * Call pm_trans_check routine to get the Ok from
   6763 		 * the global policy
   6764 		 */
   6765 		rval = pm_trans_check(&sd_pm_tran_data, &intvlp);
   6766 #ifdef	SDDEBUG
   6767 		if (sd_force_pm_supported) {
   6768 			/* Force a successful result */
   6769 			rval = 1;
   6770 		}
   6771 #endif
   6772 		switch (rval) {
   6773 		case 0:
   6774 			/*
   6775 			 * Not Ok to Power cycle or error in parameters passed
   6776 			 * Would have given the advised time to consider power
   6777 			 * cycle. Based on the new intvlp parameter we are
   6778 			 * supposed to pretend we are busy so that pm framework
   6779 			 * will never call our power entry point. Because of
   6780 			 * that install a timeout handler and wait for the
   6781 			 * recommended time to elapse so that power management
   6782 			 * can be effective again.
   6783 			 *
   6784 			 * To effect this behavior, call pm_busy_component to
   6785 			 * indicate to the framework this device is busy.
   6786 			 * By not adjusting un_pm_count the rest of PM in
   6787 			 * the driver will function normally, and independent
   6788 			 * of this but because the framework is told the device
   6789 			 * is busy it won't attempt powering down until it gets
   6790 			 * a matching idle. The timeout handler sends this.
   6791 			 * Note: sd_pm_entry can't be called here to do this
   6792 			 * because sdpower may have been called as a result
   6793 			 * of a call to pm_raise_power from within sd_pm_entry.
   6794 			 *
   6795 			 * If a timeout handler is already active then
   6796 			 * don't install another.
   6797 			 */
   6798 			mutex_enter(&un->un_pm_mutex);
   6799 			if (un->un_pm_timeid == NULL) {
   6800 				un->un_pm_timeid =
   6801 				    timeout(sd_pm_timeout_handler,
   6802 				    un, intvlp * drv_usectohz(1000000));
   6803 				mutex_exit(&un->un_pm_mutex);
   6804 				(void) pm_busy_component(SD_DEVINFO(un), 0);
   6805 			} else {
   6806 				mutex_exit(&un->un_pm_mutex);
   6807 			}
   6808 			if (got_semaphore_here != 0) {
   6809 				sema_v(&un->un_semoclose);
   6810 			}
   6811 			/*
   6812 			 * On exit put the state back to it's original value
   6813 			 * and broadcast to anyone waiting for the power
   6814 			 * change completion.
   6815 			 */
   6816 			mutex_enter(SD_MUTEX(un));
   6817 			un->un_state = state_before_pm;
   6818 			cv_broadcast(&un->un_suspend_cv);
   6819 			mutex_exit(SD_MUTEX(un));
   6820 
   6821 			SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, "
   6822 			    "trans check Failed, not ok to power cycle.\n");
   6823 
   6824 			goto sdpower_failed;
   6825 		case -1:
   6826 			if (got_semaphore_here != 0) {
   6827 				sema_v(&un->un_semoclose);
   6828 			}
   6829 			/*
   6830 			 * On exit put the state back to it's original value
   6831 			 * and broadcast to anyone waiting for the power
   6832 			 * change completion.
   6833 			 */
   6834 			mutex_enter(SD_MUTEX(un));
   6835 			un->un_state = state_before_pm;
   6836 			cv_broadcast(&un->un_suspend_cv);
   6837 			mutex_exit(SD_MUTEX(un));
   6838 			SD_TRACE(SD_LOG_IO_PM, un,
   6839 			    "sdpower: exit, trans check command Failed.\n");
   6840 
   6841 			goto sdpower_failed;
   6842 		}
   6843 	}
   6844 
   6845 	if (!SD_PM_IS_IO_CAPABLE(un, level)) {
   6846 		/*
   6847 		 * Save the last state... if the STOP FAILS we need it
   6848 		 * for restoring
   6849 		 */
   6850 		mutex_enter(SD_MUTEX(un));
   6851 		save_state = un->un_last_state;
   6852 		last_power_level = un->un_power_level;
   6853 		/*
   6854 		 * There must not be any cmds. getting processed
   6855 		 * in the driver when we get here. Power to the
   6856 		 * device is potentially going off.
   6857 		 */
   6858 		ASSERT(un->un_ncmds_in_driver == 0);
   6859 		mutex_exit(SD_MUTEX(un));
   6860 
   6861 		/*
   6862 		 * For now PM suspend the device completely before spindle is
   6863 		 * turned off
   6864 		 */
   6865 		if ((rval = sd_pm_state_change(un, level, SD_PM_STATE_CHANGE))
   6866 		    == DDI_FAILURE) {
   6867 			if (got_semaphore_here != 0) {
   6868 				sema_v(&un->un_semoclose);
   6869 			}
   6870 			/*
   6871 			 * On exit put the state back to it's original value
   6872 			 * and broadcast to anyone waiting for the power
   6873 			 * change completion.
   6874 			 */
   6875 			mutex_enter(SD_MUTEX(un));
   6876 			un->un_state = state_before_pm;
   6877 			un->un_power_level = last_power_level;
   6878 			cv_broadcast(&un->un_suspend_cv);
   6879 			mutex_exit(SD_MUTEX(un));
   6880 			SD_TRACE(SD_LOG_IO_PM, un,
   6881 			    "sdpower: exit, PM suspend Failed.\n");
   6882 
   6883 			goto sdpower_failed;
   6884 		}
   6885 	}
   6886 
   6887 	/*
   6888 	 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open,
   6889 	 * close, or strategy. Dump no long uses this routine, it uses it's
   6890 	 * own code so it can be done in polled mode.
   6891 	 */
   6892 
   6893 	medium_present = TRUE;
   6894 
   6895 	/*
   6896 	 * When powering up, issue a TUR in case the device is at unit
   6897 	 * attention.  Don't do retries. Bypass the PM layer, otherwise
   6898 	 * a deadlock on un_pm_busy_cv will occur.
   6899 	 */
   6900 	if (SD_PM_IS_IO_CAPABLE(un, level)) {
   6901 		sval = sd_send_scsi_TEST_UNIT_READY(ssc,
   6902 		    SD_DONT_RETRY_TUR | SD_BYPASS_PM);
   6903 		if (sval != 0)
   6904 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   6905 	}
   6906 
   6907 	if (un->un_f_power_condition_supported) {
   6908 		char *pm_condition_name[] = {"STOPPED", "STANDBY",
   6909 		    "IDLE", "ACTIVE"};
   6910 		SD_TRACE(SD_LOG_IO_PM, un,
   6911 		    "sdpower: sending \'%s\' power condition",
   6912 		    pm_condition_name[level]);
   6913 		sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
   6914 		    sd_pl2pc[level], SD_PATH_DIRECT);
   6915 	} else {
   6916 		SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n",
   6917 		    ((level == SD_SPINDLE_ON) ? "START" : "STOP"));
   6918 		sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
   6919 		    ((level == SD_SPINDLE_ON) ? SD_TARGET_START :
   6920 		    SD_TARGET_STOP), SD_PATH_DIRECT);
   6921 	}
   6922 	if (sval != 0) {
   6923 		if (sval == EIO)
   6924 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
   6925 		else
   6926 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   6927 	}
   6928 
   6929 	/* Command failed, check for media present. */
   6930 	if ((sval == ENXIO) && un->un_f_has_removable_media) {
   6931 		medium_present = FALSE;
   6932 	}
   6933 
   6934 	/*
   6935 	 * The conditions of interest here are:
   6936 	 *   if a spindle off with media present fails,
   6937 	 *	then restore the state and return an error.
   6938 	 *   else if a spindle on fails,
   6939 	 *	then return an error (there's no state to restore).
   6940 	 * In all other cases we setup for the new state
   6941 	 * and return success.
   6942 	 */
   6943 	if (!SD_PM_IS_IO_CAPABLE(un, level)) {
   6944 		if ((medium_present == TRUE) && (sval != 0)) {
   6945 			/* The stop command from above failed */
   6946 			rval = DDI_FAILURE;
   6947 			/*
   6948 			 * The stop command failed, and we have media
   6949 			 * present. Put the level back by calling the
   6950 			 * sd_pm_resume() and set the state back to
   6951 			 * it's previous value.
   6952 			 */
   6953 			(void) sd_pm_state_change(un, last_power_level,
   6954 			    SD_PM_STATE_ROLLBACK);
   6955 			mutex_enter(SD_MUTEX(un));
   6956 			un->un_last_state = save_state;
   6957 			mutex_exit(SD_MUTEX(un));
   6958 		} else if (un->un_f_monitor_media_state) {
   6959 			/*
   6960 			 * The stop command from above succeeded.
   6961 			 * Terminate watch thread in case of removable media
   6962 			 * devices going into low power state. This is as per
   6963 			 * the requirements of pm framework, otherwise commands
   6964 			 * will be generated for the device (through watch
   6965 			 * thread), even when the device is in low power state.
   6966 			 */
   6967 			mutex_enter(SD_MUTEX(un));
   6968 			un->un_f_watcht_stopped = FALSE;
   6969 			if (un->un_swr_token != NULL) {
   6970 				opaque_t temp_token = un->un_swr_token;
   6971 				un->un_f_watcht_stopped = TRUE;
   6972 				un->un_swr_token = NULL;
   6973 				mutex_exit(SD_MUTEX(un));
   6974 				(void) scsi_watch_request_terminate(temp_token,
   6975 				    SCSI_WATCH_TERMINATE_ALL_WAIT);
   6976 			} else {
   6977 				mutex_exit(SD_MUTEX(un));
   6978 			}
   6979 		}
   6980 	} else {
   6981 		/*
   6982 		 * The level requested is I/O capable.
   6983 		 * Legacy behavior: return success on a failed spinup
   6984 		 * if there is no media in the drive.
   6985 		 * Do this by looking at medium_present here.
   6986 		 */
   6987 		if ((sval != 0) && medium_present) {
   6988 			/* The start command from above failed */
   6989 			rval = DDI_FAILURE;
   6990 		} else {
   6991 			/*
   6992 			 * The start command from above succeeded
   6993 			 * PM resume the devices now that we have
   6994 			 * started the disks
   6995 			 */
   6996 			(void) sd_pm_state_change(un, level,
   6997 			    SD_PM_STATE_CHANGE);
   6998 
   6999 			/*
   7000 			 * Resume the watch thread since it was suspended
   7001 			 * when the device went into low power mode.
   7002 			 */
   7003 			if (un->un_f_monitor_media_state) {
   7004 				mutex_enter(SD_MUTEX(un));
   7005 				if (un->un_f_watcht_stopped == TRUE) {
   7006 					opaque_t temp_token;
   7007 
   7008 					un->un_f_watcht_stopped = FALSE;
   7009 					mutex_exit(SD_MUTEX(un));
   7010 					temp_token =
   7011 					    sd_watch_request_submit(un);
   7012 					mutex_enter(SD_MUTEX(un));
   7013 					un->un_swr_token = temp_token;
   7014 				}
   7015 				mutex_exit(SD_MUTEX(un));
   7016 			}
   7017 		}
   7018 	}
   7019 
   7020 	if (got_semaphore_here != 0) {
   7021 		sema_v(&un->un_semoclose);
   7022 	}
   7023 	/*
   7024 	 * On exit put the state back to it's original value
   7025 	 * and broadcast to anyone waiting for the power
   7026 	 * change completion.
   7027 	 */
   7028 	mutex_enter(SD_MUTEX(un));
   7029 	un->un_state = state_before_pm;
   7030 	cv_broadcast(&un->un_suspend_cv);
   7031 	mutex_exit(SD_MUTEX(un));
   7032 
   7033 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval);
   7034 
   7035 	sd_ssc_fini(ssc);
   7036 	return (rval);
   7037 
   7038 sdpower_failed:
   7039 
   7040 	sd_ssc_fini(ssc);
   7041 	return (DDI_FAILURE);
   7042 }
   7043 
   7044 
   7045 
   7046 /*
   7047  *    Function: sdattach
   7048  *
   7049  * Description: Driver's attach(9e) entry point function.
   7050  *
   7051  *   Arguments: devi - opaque device info handle
   7052  *		cmd  - attach  type
   7053  *
   7054  * Return Code: DDI_SUCCESS
   7055  *		DDI_FAILURE
   7056  *
   7057  *     Context: Kernel thread context
   7058  */
   7059 
   7060 static int
   7061 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
   7062 {
   7063 	switch (cmd) {
   7064 	case DDI_ATTACH:
   7065 		return (sd_unit_attach(devi));
   7066 	case DDI_RESUME:
   7067 		return (sd_ddi_resume(devi));
   7068 	default:
   7069 		break;
   7070 	}
   7071 	return (DDI_FAILURE);
   7072 }
   7073 
   7074 
   7075 /*
   7076  *    Function: sddetach
   7077  *
   7078  * Description: Driver's detach(9E) entry point function.
   7079  *
   7080  *   Arguments: devi - opaque device info handle
   7081  *		cmd  - detach  type
   7082  *
   7083  * Return Code: DDI_SUCCESS
   7084  *		DDI_FAILURE
   7085  *
   7086  *     Context: Kernel thread context
   7087  */
   7088 
   7089 static int
   7090 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
   7091 {
   7092 	switch (cmd) {
   7093 	case DDI_DETACH:
   7094 		return (sd_unit_detach(devi));
   7095 	case DDI_SUSPEND:
   7096 		return (sd_ddi_suspend(devi));
   7097 	default:
   7098 		break;
   7099 	}
   7100 	return (DDI_FAILURE);
   7101 }
   7102 
   7103 
   7104 /*
   7105  *     Function: sd_sync_with_callback
   7106  *
   7107  *  Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft
   7108  *		 state while the callback routine is active.
   7109  *
   7110  *    Arguments: un: softstate structure for the instance
   7111  *
   7112  *	Context: Kernel thread context
   7113  */
   7114 
   7115 static void
   7116 sd_sync_with_callback(struct sd_lun *un)
   7117 {
   7118 	ASSERT(un != NULL);
   7119 
   7120 	mutex_enter(SD_MUTEX(un));
   7121 
   7122 	ASSERT(un->un_in_callback >= 0);
   7123 
   7124 	while (un->un_in_callback > 0) {
   7125 		mutex_exit(SD_MUTEX(un));
   7126 		delay(2);
   7127 		mutex_enter(SD_MUTEX(un));
   7128 	}
   7129 
   7130 	mutex_exit(SD_MUTEX(un));
   7131 }
   7132 
   7133 /*
   7134  *    Function: sd_unit_attach
   7135  *
   7136  * Description: Performs DDI_ATTACH processing for sdattach(). Allocates
   7137  *		the soft state structure for the device and performs
   7138  *		all necessary structure and device initializations.
   7139  *
   7140  *   Arguments: devi: the system's dev_info_t for the device.
   7141  *
   7142  * Return Code: DDI_SUCCESS if attach is successful.
   7143  *		DDI_FAILURE if any part of the attach fails.
   7144  *
   7145  *     Context: Called at attach(9e) time for the DDI_ATTACH flag.
   7146  *		Kernel thread context only.  Can sleep.
   7147  */
   7148 
   7149 static int
   7150 sd_unit_attach(dev_info_t *devi)
   7151 {
   7152 	struct	scsi_device	*devp;
   7153 	struct	sd_lun		*un;
   7154 	char			*variantp;
   7155 	char			name_str[48];
   7156 	int	reservation_flag = SD_TARGET_IS_UNRESERVED;
   7157 	int	instance;
   7158 	int	rval;
   7159 	int	wc_enabled;
   7160 	int	tgt;
   7161 	uint64_t	capacity;
   7162 	uint_t		lbasize = 0;
   7163 	dev_info_t	*pdip = ddi_get_parent(devi);
   7164 	int		offbyone = 0;
   7165 	int		geom_label_valid = 0;
   7166 	sd_ssc_t	*ssc;
   7167 	int		status;
   7168 	struct sd_fm_internal	*sfip = NULL;
   7169 	int		max_xfer_size;
   7170 
   7171 	/*
   7172 	 * Retrieve the target driver's private data area. This was set
   7173 	 * up by the HBA.
   7174 	 */
   7175 	devp = ddi_get_driver_private(devi);
   7176 
   7177 	/*
   7178 	 * Retrieve the target ID of the device.
   7179 	 */
   7180 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
   7181 	    SCSI_ADDR_PROP_TARGET, -1);
   7182 
   7183 	/*
   7184 	 * Since we have no idea what state things were left in by the last
   7185 	 * user of the device, set up some 'default' settings, ie. turn 'em
   7186 	 * off. The scsi_ifsetcap calls force re-negotiations with the drive.
   7187 	 * Do this before the scsi_probe, which sends an inquiry.
   7188 	 * This is a fix for bug (4430280).
   7189 	 * Of special importance is wide-xfer. The drive could have been left
   7190 	 * in wide transfer mode by the last driver to communicate with it,
   7191 	 * this includes us. If that's the case, and if the following is not
   7192 	 * setup properly or we don't re-negotiate with the drive prior to
   7193 	 * transferring data to/from the drive, it causes bus parity errors,
   7194 	 * data overruns, and unexpected interrupts. This first occurred when
   7195 	 * the fix for bug (4378686) was made.
   7196 	 */
   7197 	(void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1);
   7198 	(void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1);
   7199 	(void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1);
   7200 
   7201 	/*
   7202 	 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs
   7203 	 * on a target. Setting it per lun instance actually sets the
   7204 	 * capability of this target, which affects those luns already
   7205 	 * attached on the same target. So during attach, we can only disable
   7206 	 * this capability only when no other lun has been attached on this
   7207 	 * target. By doing this, we assume a target has the same tagged-qing
   7208 	 * capability for every lun. The condition can be removed when HBA
   7209 	 * is changed to support per lun based tagged-qing capability.
   7210 	 */
   7211 	if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
   7212 		(void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1);
   7213 	}
   7214 
   7215 	/*
   7216 	 * Use scsi_probe() to issue an INQUIRY command to the device.
   7217 	 * This call will allocate and fill in the scsi_inquiry structure
   7218 	 * and point the sd_inq member of the scsi_device structure to it.
   7219 	 * If the attach succeeds, then this memory will not be de-allocated
   7220 	 * (via scsi_unprobe()) until the instance is detached.
   7221 	 */
   7222 	if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) {
   7223 		goto probe_failed;
   7224 	}
   7225 
   7226 	/*
   7227 	 * Check the device type as specified in the inquiry data and
   7228 	 * claim it if it is of a type that we support.
   7229 	 */
   7230 	switch (devp->sd_inq->inq_dtype) {
   7231 	case DTYPE_DIRECT:
   7232 		break;
   7233 	case DTYPE_RODIRECT:
   7234 		break;
   7235 	case DTYPE_OPTICAL:
   7236 		break;
   7237 	case DTYPE_NOTPRESENT:
   7238 	default:
   7239 		/* Unsupported device type; fail the attach. */
   7240 		goto probe_failed;
   7241 	}
   7242 
   7243 	/*
   7244 	 * Allocate the soft state structure for this unit.
   7245 	 *
   7246 	 * We rely upon this memory being set to all zeroes by
   7247 	 * ddi_soft_state_zalloc().  We assume that any member of the
   7248 	 * soft state structure that is not explicitly initialized by
   7249 	 * this routine will have a value of zero.
   7250 	 */
   7251 	instance = ddi_get_instance(devp->sd_dev);
   7252 #ifndef XPV_HVM_DRIVER
   7253 	if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) {
   7254 		goto probe_failed;
   7255 	}
   7256 #endif /* !XPV_HVM_DRIVER */
   7257 
   7258 	/*
   7259 	 * Retrieve a pointer to the newly-allocated soft state.
   7260 	 *
   7261 	 * This should NEVER fail if the ddi_soft_state_zalloc() call above
   7262 	 * was successful, unless something has gone horribly wrong and the
   7263 	 * ddi's soft state internals are corrupt (in which case it is
   7264 	 * probably better to halt here than just fail the attach....)
   7265 	 */
   7266 	if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
   7267 		panic("sd_unit_attach: NULL soft state on instance:0x%x",
   7268 		    instance);
   7269 		/*NOTREACHED*/
   7270 	}
   7271 
   7272 	/*
   7273 	 * Link the back ptr of the driver soft state to the scsi_device
   7274 	 * struct for this lun.
   7275 	 * Save a pointer to the softstate in the driver-private area of
   7276 	 * the scsi_device struct.
   7277 	 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until
   7278 	 * we first set un->un_sd below.
   7279 	 */
   7280 	un->un_sd = devp;
   7281 	devp->sd_private = (opaque_t)un;
   7282 
   7283 	/*
   7284 	 * The following must be after devp is stored in the soft state struct.
   7285 	 */
   7286 #ifdef SDDEBUG
   7287 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
   7288 	    "%s_unit_attach: un:0x%p instance:%d\n",
   7289 	    ddi_driver_name(devi), un, instance);
   7290 #endif
   7291 
   7292 	/*
   7293 	 * Set up the device type and node type (for the minor nodes).
   7294 	 * By default we assume that the device can at least support the
   7295 	 * Common Command Set. Call it a CD-ROM if it reports itself
   7296 	 * as a RODIRECT device.
   7297 	 */
   7298 	switch (devp->sd_inq->inq_dtype) {
   7299 	case DTYPE_RODIRECT:
   7300 		un->un_node_type = DDI_NT_CD_CHAN;
   7301 		un->un_ctype	 = CTYPE_CDROM;
   7302 		break;
   7303 	case DTYPE_OPTICAL:
   7304 		un->un_node_type = DDI_NT_BLOCK_CHAN;
   7305 		un->un_ctype	 = CTYPE_ROD;
   7306 		break;
   7307 	default:
   7308 		un->un_node_type = DDI_NT_BLOCK_CHAN;
   7309 		un->un_ctype	 = CTYPE_CCS;
   7310 		break;
   7311 	}
   7312 
   7313 	/*
   7314 	 * Try to read the interconnect type from the HBA.
   7315 	 *
   7316 	 * Note: This driver is currently compiled as two binaries, a parallel
   7317 	 * scsi version (sd) and a fibre channel version (ssd). All functional
   7318 	 * differences are determined at compile time. In the future a single
   7319 	 * binary will be provided and the interconnect type will be used to
   7320 	 * differentiate between fibre and parallel scsi behaviors. At that time
   7321 	 * it will be necessary for all fibre channel HBAs to support this
   7322 	 * property.
   7323 	 *
   7324 	 * set un_f_is_fiber to TRUE ( default fiber )
   7325 	 */
   7326 	un->un_f_is_fibre = TRUE;
   7327 	switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) {
   7328 	case INTERCONNECT_SSA:
   7329 		un->un_interconnect_type = SD_INTERCONNECT_SSA;
   7330 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   7331 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un);
   7332 		break;
   7333 	case INTERCONNECT_PARALLEL:
   7334 		un->un_f_is_fibre = FALSE;
   7335 		un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
   7336 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   7337 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un);
   7338 		break;
   7339 	case INTERCONNECT_SAS:
   7340 		un->un_f_is_fibre = FALSE;
   7341 		un->un_interconnect_type = SD_INTERCONNECT_SAS;
   7342 		un->un_node_type = DDI_NT_BLOCK_SAS;
   7343 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   7344 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SAS\n", un);
   7345 		break;
   7346 	case INTERCONNECT_SATA:
   7347 		un->un_f_is_fibre = FALSE;
   7348 		un->un_interconnect_type = SD_INTERCONNECT_SATA;
   7349 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   7350 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un);
   7351 		break;
   7352 	case INTERCONNECT_FIBRE:
   7353 		un->un_interconnect_type = SD_INTERCONNECT_FIBRE;
   7354 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   7355 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un);
   7356 		break;
   7357 	case INTERCONNECT_FABRIC:
   7358 		un->un_interconnect_type = SD_INTERCONNECT_FABRIC;
   7359 		un->un_node_type = DDI_NT_BLOCK_FABRIC;
   7360 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   7361 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un);
   7362 		break;
   7363 	default:
   7364 #ifdef SD_DEFAULT_INTERCONNECT_TYPE
   7365 		/*
   7366 		 * The HBA does not support the "interconnect-type" property
   7367 		 * (or did not provide a recognized type).
   7368 		 *
   7369 		 * Note: This will be obsoleted when a single fibre channel
   7370 		 * and parallel scsi driver is delivered. In the meantime the
   7371 		 * interconnect type will be set to the platform default.If that
   7372 		 * type is not parallel SCSI, it means that we should be
   7373 		 * assuming "ssd" semantics. However, here this also means that
   7374 		 * the FC HBA is not supporting the "interconnect-type" property
   7375 		 * like we expect it to, so log this occurrence.
   7376 		 */
   7377 		un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE;
   7378 		if (!SD_IS_PARALLEL_SCSI(un)) {
   7379 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   7380 			    "sd_unit_attach: un:0x%p Assuming "
   7381 			    "INTERCONNECT_FIBRE\n", un);
   7382 		} else {
   7383 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   7384 			    "sd_unit_attach: un:0x%p Assuming "
   7385 			    "INTERCONNECT_PARALLEL\n", un);
   7386 			un->un_f_is_fibre = FALSE;
   7387 		}
   7388 #else
   7389 		/*
   7390 		 * Note: This source will be implemented when a single fibre
   7391 		 * channel and parallel scsi driver is delivered. The default
   7392 		 * will be to assume that if a device does not support the
   7393 		 * "interconnect-type" property it is a parallel SCSI HBA and
   7394 		 * we will set the interconnect type for parallel scsi.
   7395 		 */
   7396 		un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
   7397 		un->un_f_is_fibre = FALSE;
   7398 #endif
   7399 		break;
   7400 	}
   7401 
   7402 	if (un->un_f_is_fibre == TRUE) {
   7403 		if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) ==
   7404 		    SCSI_VERSION_3) {
   7405 			switch (un->un_interconnect_type) {
   7406 			case SD_INTERCONNECT_FIBRE:
   7407 			case SD_INTERCONNECT_SSA:
   7408 				un->un_node_type = DDI_NT_BLOCK_WWN;
   7409 				break;
   7410 			default:
   7411 				break;
   7412 			}
   7413 		}
   7414 	}
   7415 
   7416 	/*
   7417 	 * Initialize the Request Sense command for the target
   7418 	 */
   7419 	if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) {
   7420 		goto alloc_rqs_failed;
   7421 	}
   7422 
   7423 	/*
   7424 	 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc
   7425 	 * with separate binary for sd and ssd.
   7426 	 *
   7427 	 * x86 has 1 binary, un_retry_count is set base on connection type.
   7428 	 * The hardcoded values will go away when Sparc uses 1 binary
   7429 	 * for sd and ssd.  This hardcoded values need to match
   7430 	 * SD_RETRY_COUNT in sddef.h
   7431 	 * The value used is base on interconnect type.
   7432 	 * fibre = 3, parallel = 5
   7433 	 */
   7434 #if defined(__i386) || defined(__amd64)
   7435 	un->un_retry_count = un->un_f_is_fibre ? 3 : 5;
   7436 #else
   7437 	un->un_retry_count = SD_RETRY_COUNT;
   7438 #endif
   7439 
   7440 	/*
   7441 	 * Set the per disk retry count to the default number of retries
   7442 	 * for disks and CDROMs. This value can be overridden by the
   7443 	 * disk property list or an entry in sd.conf.
   7444 	 */
   7445 	un->un_notready_retry_count =
   7446 	    ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un)
   7447 	    : DISK_NOT_READY_RETRY_COUNT(un);
   7448 
   7449 	/*
   7450 	 * Set the busy retry count to the default value of un_retry_count.
   7451 	 * This can be overridden by entries in sd.conf or the device
   7452 	 * config table.
   7453 	 */
   7454 	un->un_busy_retry_count = un->un_retry_count;
   7455 
   7456 	/*
   7457 	 * Init the reset threshold for retries.  This number determines
   7458 	 * how many retries must be performed before a reset can be issued
   7459 	 * (for certain error conditions). This can be overridden by entries
   7460 	 * in sd.conf or the device config table.
   7461 	 */
   7462 	un->un_reset_retry_count = (un->un_retry_count / 2);
   7463 
   7464 	/*
   7465 	 * Set the victim_retry_count to the default un_retry_count
   7466 	 */
   7467 	un->un_victim_retry_count = (2 * un->un_retry_count);
   7468 
   7469 	/*
   7470 	 * Set the reservation release timeout to the default value of
   7471 	 * 5 seconds. This can be overridden by entries in ssd.conf or the
   7472 	 * device config table.
   7473 	 */
   7474 	un->un_reserve_release_time = 5;
   7475 
   7476 	/*
   7477 	 * Set up the default maximum transfer size. Note that this may
   7478 	 * get updated later in the attach, when setting up default wide
   7479 	 * operations for disks.
   7480 	 */
   7481 #if defined(__i386) || defined(__amd64)
   7482 	un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE;
   7483 	un->un_partial_dma_supported = 1;
   7484 #else
   7485 	un->un_max_xfer_size = (uint_t)maxphys;
   7486 #endif
   7487 
   7488 	/*
   7489 	 * Get "allow bus device reset" property (defaults to "enabled" if
   7490 	 * the property was not defined). This is to disable bus resets for
   7491 	 * certain kinds of error recovery. Note: In the future when a run-time
   7492 	 * fibre check is available the soft state flag should default to
   7493 	 * enabled.
   7494 	 */
   7495 	if (un->un_f_is_fibre == TRUE) {
   7496 		un->un_f_allow_bus_device_reset = TRUE;
   7497 	} else {
   7498 		if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
   7499 		    "allow-bus-device-reset", 1) != 0) {
   7500 			un->un_f_allow_bus_device_reset = TRUE;
   7501 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   7502 			    "sd_unit_attach: un:0x%p Bus device reset "
   7503 			    "enabled\n", un);
   7504 		} else {
   7505 			un->un_f_allow_bus_device_reset = FALSE;
   7506 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   7507 			    "sd_unit_attach: un:0x%p Bus device reset "
   7508 			    "disabled\n", un);
   7509 		}
   7510 	}
   7511 
   7512 	/*
   7513 	 * Check if this is an ATAPI device. ATAPI devices use Group 1
   7514 	 * Read/Write commands and Group 2 Mode Sense/Select commands.
   7515 	 *
   7516 	 * Note: The "obsolete" way of doing this is to check for the "atapi"
   7517 	 * property. The new "variant" property with a value of "atapi" has been
   7518 	 * introduced so that future 'variants' of standard SCSI behavior (like
   7519 	 * atapi) could be specified by the underlying HBA drivers by supplying
   7520 	 * a new value for the "variant" property, instead of having to define a
   7521 	 * new property.
   7522 	 */
   7523 	if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) {
   7524 		un->un_f_cfg_is_atapi = TRUE;
   7525 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
   7526 		    "sd_unit_attach: un:0x%p Atapi device\n", un);
   7527 	}
   7528 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant",
   7529 	    &variantp) == DDI_PROP_SUCCESS) {
   7530 		if (strcmp(variantp, "atapi") == 0) {
   7531 			un->un_f_cfg_is_atapi = TRUE;
   7532 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   7533 			    "sd_unit_attach: un:0x%p Atapi device\n", un);
   7534 		}
   7535 		ddi_prop_free(variantp);
   7536 	}
   7537 
   7538 	un->un_cmd_timeout	= SD_IO_TIME;
   7539 
   7540 	un->un_busy_timeout  = SD_BSY_TIMEOUT;
   7541 
   7542 	/* Info on current states, statuses, etc. (Updated frequently) */
   7543 	un->un_state		= SD_STATE_NORMAL;
   7544 	un->un_last_state	= SD_STATE_NORMAL;
   7545 
   7546 	/* Control & status info for command throttling */
   7547 	un->un_throttle		= sd_max_throttle;
   7548 	un->un_saved_throttle	= sd_max_throttle;
   7549 	un->un_min_throttle	= sd_min_throttle;
   7550 
   7551 	if (un->un_f_is_fibre == TRUE) {
   7552 		un->un_f_use_adaptive_throttle = TRUE;
   7553 	} else {
   7554 		un->un_f_use_adaptive_throttle = FALSE;
   7555 	}
   7556 
   7557 	/* Removable media support. */
   7558 	cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL);
   7559 	un->un_mediastate		= DKIO_NONE;
   7560 	un->un_specified_mediastate	= DKIO_NONE;
   7561 
   7562 	/* CVs for suspend/resume (PM or DR) */
   7563 	cv_init(&un->un_suspend_cv,   NULL, CV_DRIVER, NULL);
   7564 	cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL);
   7565 
   7566 	/* Power management support. */
   7567 	un->un_power_level = SD_SPINDLE_UNINIT;
   7568 
   7569 	cv_init(&un->un_wcc_cv,   NULL, CV_DRIVER, NULL);
   7570 	un->un_f_wcc_inprog = 0;
   7571 
   7572 	/*
   7573 	 * The open/close semaphore is used to serialize threads executing
   7574 	 * in the driver's open & close entry point routines for a given
   7575 	 * instance.
   7576 	 */
   7577 	(void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL);
   7578 
   7579 	/*
   7580 	 * The conf file entry and softstate variable is a forceful override,
   7581 	 * meaning a non-zero value must be entered to change the default.
   7582 	 */
   7583 	un->un_f_disksort_disabled = FALSE;
   7584 	un->un_f_rmw_type = SD_RMW_TYPE_DEFAULT;
   7585 
   7586 	/*
   7587 	 * GET EVENT STATUS NOTIFICATION media polling enabled by default, but
   7588 	 * can be overridden via [s]sd-config-list "mmc-gesn-polling" property.
   7589 	 */
   7590 	un->un_f_mmc_gesn_polling = TRUE;
   7591 
   7592 	/*
   7593 	 * Retrieve the properties from the static driver table or the driver
   7594 	 * configuration file (.conf) for this unit and update the soft state
   7595 	 * for the device as needed for the indicated properties.
   7596 	 * Note: the property configuration needs to occur here as some of the
   7597 	 * following routines may have dependencies on soft state flags set
   7598 	 * as part of the driver property configuration.
   7599 	 */
   7600 	sd_read_unit_properties(un);
   7601 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
   7602 	    "sd_unit_attach: un:0x%p property configuration complete.\n", un);
   7603 
   7604 	/*
   7605 	 * Only if a device has "hotpluggable" property, it is
   7606 	 * treated as hotpluggable device. Otherwise, it is
   7607 	 * regarded as non-hotpluggable one.
   7608 	 */
   7609 	if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable",
   7610 	    -1) != -1) {
   7611 		un->un_f_is_hotpluggable = TRUE;
   7612 	}
   7613 
   7614 	/*
   7615 	 * set unit's attributes(flags) according to "hotpluggable" and
   7616 	 * RMB bit in INQUIRY data.
   7617 	 */
   7618 	sd_set_unit_attributes(un, devi);
   7619 
   7620 	/*
   7621 	 * By default, we mark the capacity, lbasize, and geometry
   7622 	 * as invalid. Only if we successfully read a valid capacity
   7623 	 * will we update the un_blockcount and un_tgt_blocksize with the
   7624 	 * valid values (the geometry will be validated later).
   7625 	 */
   7626 	un->un_f_blockcount_is_valid	= FALSE;
   7627 	un->un_f_tgt_blocksize_is_valid	= FALSE;
   7628 
   7629 	/*
   7630 	 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine
   7631 	 * otherwise.
   7632 	 */
   7633 	un->un_tgt_blocksize  = un->un_sys_blocksize  = DEV_BSIZE;
   7634 	un->un_blockcount = 0;
   7635 
   7636 	/*
   7637 	 * Set up the per-instance info needed to determine the correct
   7638 	 * CDBs and other info for issuing commands to the target.
   7639 	 */
   7640 	sd_init_cdb_limits(un);
   7641 
   7642 	/*
   7643 	 * Set up the IO chains to use, based upon the target type.
   7644 	 */
   7645 	if (un->un_f_non_devbsize_supported) {
   7646 		un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
   7647 	} else {
   7648 		un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
   7649 	}
   7650 	un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD;
   7651 	un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD;
   7652 	un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD;
   7653 
   7654 	un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf),
   7655 	    sd_xbuf_strategy, un, sd_xbuf_active_limit,  sd_xbuf_reserve_limit,
   7656 	    ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER);
   7657 	ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi);
   7658 
   7659 
   7660 	if (ISCD(un)) {
   7661 		un->un_additional_codes = sd_additional_codes;
   7662 	} else {
   7663 		un->un_additional_codes = NULL;
   7664 	}
   7665 
   7666 	/*
   7667 	 * Create the kstats here so they can be available for attach-time
   7668 	 * routines that send commands to the unit (either polled or via
   7669 	 * sd_send_scsi_cmd).
   7670 	 *
   7671 	 * Note: This is a critical sequence that needs to be maintained:
   7672 	 *	1) Instantiate the kstats here, before any routines using the
   7673 	 *	   iopath (i.e. sd_send_scsi_cmd).
   7674 	 *	2) Instantiate and initialize the partition stats
   7675 	 *	   (sd_set_pstats).
   7676 	 *	3) Initialize the error stats (sd_set_errstats), following
   7677 	 *	   sd_validate_geometry(),sd_register_devid(),
   7678 	 *	   and sd_cache_control().
   7679 	 */
   7680 
   7681 	un->un_stats = kstat_create(sd_label, instance,
   7682 	    NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
   7683 	if (un->un_stats != NULL) {
   7684 		un->un_stats->ks_lock = SD_MUTEX(un);
   7685 		kstat_install(un->un_stats);
   7686 	}
   7687 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
   7688 	    "sd_unit_attach: un:0x%p un_stats created\n", un);
   7689 
   7690 	sd_create_errstats(un, instance);
   7691 	if (un->un_errstats == NULL) {
   7692 		goto create_errstats_failed;
   7693 	}
   7694 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
   7695 	    "sd_unit_attach: un:0x%p errstats created\n", un);
   7696 
   7697 	/*
   7698 	 * The following if/else code was relocated here from below as part
   7699 	 * of the fix for bug (4430280). However with the default setup added
   7700 	 * on entry to this routine, it's no longer absolutely necessary for
   7701 	 * this to be before the call to sd_spin_up_unit.
   7702 	 */
   7703 	if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) {
   7704 		int tq_trigger_flag = (((devp->sd_inq->inq_ansi == 4) ||
   7705 		    (devp->sd_inq->inq_ansi == 5)) &&
   7706 		    devp->sd_inq->inq_bque) || devp->sd_inq->inq_cmdque;
   7707 
   7708 		/*
   7709 		 * If tagged queueing is supported by the target
   7710 		 * and by the host adapter then we will enable it
   7711 		 */
   7712 		un->un_tagflags = 0;
   7713 		if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && tq_trigger_flag &&
   7714 		    (un->un_f_arq_enabled == TRUE)) {
   7715 			if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing",
   7716 			    1, 1) == 1) {
   7717 				un->un_tagflags = FLAG_STAG;
   7718 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
   7719 				    "sd_unit_attach: un:0x%p tag queueing "
   7720 				    "enabled\n", un);
   7721 			} else if (scsi_ifgetcap(SD_ADDRESS(un),
   7722 			    "untagged-qing", 0) == 1) {
   7723 				un->un_f_opt_queueing = TRUE;
   7724 				un->un_saved_throttle = un->un_throttle =
   7725 				    min(un->un_throttle, 3);
   7726 			} else {
   7727 				un->un_f_opt_queueing = FALSE;
   7728 				un->un_saved_throttle = un->un_throttle = 1;
   7729 			}
   7730 		} else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0)
   7731 		    == 1) && (un->un_f_arq_enabled == TRUE)) {
   7732 			/* The Host Adapter supports internal queueing. */
   7733 			un->un_f_opt_queueing = TRUE;
   7734 			un->un_saved_throttle = un->un_throttle =
   7735 			    min(un->un_throttle, 3);
   7736 		} else {
   7737 			un->un_f_opt_queueing = FALSE;
   7738 			un->un_saved_throttle = un->un_throttle = 1;
   7739 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   7740 			    "sd_unit_attach: un:0x%p no tag queueing\n", un);
   7741 		}
   7742 
   7743 		/*
   7744 		 * Enable large transfers for SATA/SAS drives
   7745 		 */
   7746 		if (SD_IS_SERIAL(un)) {
   7747 			un->un_max_xfer_size =
   7748 			    ddi_getprop(DDI_DEV_T_ANY, devi, 0,
   7749 			    sd_max_xfer_size, SD_MAX_XFER_SIZE);
   7750 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   7751 			    "sd_unit_attach: un:0x%p max transfer "
   7752 			    "size=0x%x\n", un, un->un_max_xfer_size);
   7753 
   7754 		}
   7755 
   7756 		/* Setup or tear down default wide operations for disks */
   7757 
   7758 		/*
   7759 		 * Note: Legacy: it may be possible for both "sd_max_xfer_size"
   7760 		 * and "ssd_max_xfer_size" to exist simultaneously on the same
   7761 		 * system and be set to different values. In the future this
   7762 		 * code may need to be updated when the ssd module is
   7763 		 * obsoleted and removed from the system. (4299588)
   7764 		 */
   7765 		if (SD_IS_PARALLEL_SCSI(un) &&
   7766 		    (devp->sd_inq->inq_rdf == RDF_SCSI2) &&
   7767 		    (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) {
   7768 			if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
   7769 			    1, 1) == 1) {
   7770 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
   7771 				    "sd_unit_attach: un:0x%p Wide Transfer "
   7772 				    "enabled\n", un);
   7773 			}
   7774 
   7775 			/*
   7776 			 * If tagged queuing has also been enabled, then
   7777 			 * enable large xfers
   7778 			 */
   7779 			if (un->un_saved_throttle == sd_max_throttle) {
   7780 				un->un_max_xfer_size =
   7781 				    ddi_getprop(DDI_DEV_T_ANY, devi, 0,
   7782 				    sd_max_xfer_size, SD_MAX_XFER_SIZE);
   7783 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
   7784 				    "sd_unit_attach: un:0x%p max transfer "
   7785 				    "size=0x%x\n", un, un->un_max_xfer_size);
   7786 			}
   7787 		} else {
   7788 			if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
   7789 			    0, 1) == 1) {
   7790 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
   7791 				    "sd_unit_attach: un:0x%p "
   7792 				    "Wide Transfer disabled\n", un);
   7793 			}
   7794 		}
   7795 	} else {
   7796 		un->un_tagflags = FLAG_STAG;
   7797 		un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY,
   7798 		    devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE);
   7799 	}
   7800 
   7801 	/*
   7802 	 * If this target supports LUN reset, try to enable it.
   7803 	 */
   7804 	if (un->un_f_lun_reset_enabled) {
   7805 		if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) {
   7806 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
   7807 			    "un:0x%p lun_reset capability set\n", un);
   7808 		} else {
   7809 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
   7810 			    "un:0x%p lun-reset capability not set\n", un);
   7811 		}
   7812 	}
   7813 
   7814 	/*
   7815 	 * Adjust the maximum transfer size. This is to fix
   7816 	 * the problem of partial DMA support on SPARC. Some
   7817 	 * HBA driver, like aac, has very small dma_attr_maxxfer
   7818 	 * size, which requires partial DMA support on SPARC.
   7819 	 * In the future the SPARC pci nexus driver may solve
   7820 	 * the problem instead of this fix.
   7821 	 */
   7822 	max_xfer_size = scsi_ifgetcap(SD_ADDRESS(un), "dma-max", 1);
   7823 	if ((max_xfer_size > 0) && (max_xfer_size < un->un_max_xfer_size)) {
   7824 		/* We need DMA partial even on sparc to ensure sddump() works */
   7825 		un->un_max_xfer_size = max_xfer_size;
   7826 		if (un->un_partial_dma_supported == 0)
   7827 			un->un_partial_dma_supported = 1;
   7828 	}
   7829 	if (ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
   7830 	    DDI_PROP_DONTPASS, "buf_break", 0) == 1) {
   7831 		if (ddi_xbuf_attr_setup_brk(un->un_xbuf_attr,
   7832 		    un->un_max_xfer_size) == 1) {
   7833 			un->un_buf_breakup_supported = 1;
   7834 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
   7835 			    "un:0x%p Buf breakup enabled\n", un);
   7836 		}
   7837 	}
   7838 
   7839 	/*
   7840 	 * Set PKT_DMA_PARTIAL flag.
   7841 	 */
   7842 	if (un->un_partial_dma_supported == 1) {
   7843 		un->un_pkt_flags = PKT_DMA_PARTIAL;
   7844 	} else {
   7845 		un->un_pkt_flags = 0;
   7846 	}
   7847 
   7848 	/* Initialize sd_ssc_t for internal uscsi commands */
   7849 	ssc = sd_ssc_init(un);
   7850 	scsi_fm_init(devp);
   7851 
   7852 	/*
   7853 	 * Allocate memory for SCSI FMA stuffs.
   7854 	 */
   7855 	un->un_fm_private =
   7856 	    kmem_zalloc(sizeof (struct sd_fm_internal), KM_SLEEP);
   7857 	sfip = (struct sd_fm_internal *)un->un_fm_private;
   7858 	sfip->fm_ssc.ssc_uscsi_cmd = &sfip->fm_ucmd;
   7859 	sfip->fm_ssc.ssc_uscsi_info = &sfip->fm_uinfo;
   7860 	sfip->fm_ssc.ssc_un = un;
   7861 
   7862 	if (ISCD(un) ||
   7863 	    un->un_f_has_removable_media ||
   7864 	    devp->sd_fm_capable == DDI_FM_NOT_CAPABLE) {
   7865 		/*
   7866 		 * We don't touch CDROM or the DDI_FM_NOT_CAPABLE device.
   7867 		 * Their log are unchanged.
   7868 		 */
   7869 		sfip->fm_log_level = SD_FM_LOG_NSUP;
   7870 	} else {
   7871 		/*
   7872 		 * If enter here, it should be non-CDROM and FM-capable
   7873 		 * device, and it will not keep the old scsi_log as before
   7874 		 * in /var/adm/messages. However, the property
   7875 		 * "fm-scsi-log" will control whether the FM telemetry will
   7876 		 * be logged in /var/adm/messages.
   7877 		 */
   7878 		int fm_scsi_log;
   7879 		fm_scsi_log = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
   7880 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "fm-scsi-log", 0);
   7881 
   7882 		if (fm_scsi_log)
   7883 			sfip->fm_log_level = SD_FM_LOG_EREPORT;
   7884 		else
   7885 			sfip->fm_log_level = SD_FM_LOG_SILENT;
   7886 	}
   7887 
   7888 	/*
   7889 	 * At this point in the attach, we have enough info in the
   7890 	 * soft state to be able to issue commands to the target.
   7891 	 *
   7892 	 * All command paths used below MUST issue their commands as
   7893 	 * SD_PATH_DIRECT. This is important as intermediate layers
   7894 	 * are not all initialized yet (such as PM).
   7895 	 */
   7896 
   7897 	/*
   7898 	 * Send a TEST UNIT READY command to the device. This should clear
   7899 	 * any outstanding UNIT ATTENTION that may be present.
   7900 	 *
   7901 	 * Note: Don't check for success, just track if there is a reservation,
   7902 	 * this is a throw away command to clear any unit attentions.
   7903 	 *
   7904 	 * Note: This MUST be the first command issued to the target during
   7905 	 * attach to ensure power on UNIT ATTENTIONS are cleared.
   7906 	 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated
   7907 	 * with attempts at spinning up a device with no media.
   7908 	 */
   7909 	status = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
   7910 	if (status != 0) {
   7911 		if (status == EACCES)
   7912 			reservation_flag = SD_TARGET_IS_RESERVED;
   7913 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   7914 	}
   7915 
   7916 	/*
   7917 	 * If the device is NOT a removable media device, attempt to spin
   7918 	 * it up (using the START_STOP_UNIT command) and read its capacity
   7919 	 * (using the READ CAPACITY command).  Note, however, that either
   7920 	 * of these could fail and in some cases we would continue with
   7921 	 * the attach despite the failure (see below).
   7922 	 */
   7923 	if (un->un_f_descr_format_supported) {
   7924 
   7925 		switch (sd_spin_up_unit(ssc)) {
   7926 		case 0:
   7927 			/*
   7928 			 * Spin-up was successful; now try to read the
   7929 			 * capacity.  If successful then save the results
   7930 			 * and mark the capacity & lbasize as valid.
   7931 			 */
   7932 			SD_TRACE(SD_LOG_ATTACH_DETACH, un,
   7933 			    "sd_unit_attach: un:0x%p spin-up successful\n", un);
   7934 
   7935 			status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
   7936 			    &lbasize, SD_PATH_DIRECT);
   7937 
   7938 			switch (status) {
   7939 			case 0: {
   7940 				if (capacity > DK_MAX_BLOCKS) {
   7941 #ifdef _LP64
   7942 					if ((capacity + 1) >
   7943 					    SD_GROUP1_MAX_ADDRESS) {
   7944 						/*
   7945 						 * Enable descriptor format
   7946 						 * sense data so that we can
   7947 						 * get 64 bit sense data
   7948 						 * fields.
   7949 						 */
   7950 						sd_enable_descr_sense(ssc);
   7951 					}
   7952 #else
   7953 					/* 32-bit kernels can't handle this */
   7954 					scsi_log(SD_DEVINFO(un),
   7955 					    sd_label, CE_WARN,
   7956 					    "disk has %llu blocks, which "
   7957 					    "is too large for a 32-bit "
   7958 					    "kernel", capacity);
   7959 
   7960 #if defined(__i386) || defined(__amd64)
   7961 					/*
   7962 					 * 1TB disk was treated as (1T - 512)B
   7963 					 * in the past, so that it might have
   7964 					 * valid VTOC and solaris partitions,
   7965 					 * we have to allow it to continue to
   7966 					 * work.
   7967 					 */
   7968 					if (capacity -1 > DK_MAX_BLOCKS)
   7969 #endif
   7970 					goto spinup_failed;
   7971 #endif
   7972 				}
   7973 
   7974 				/*
   7975 				 * Here it's not necessary to check the case:
   7976 				 * the capacity of the device is bigger than
   7977 				 * what the max hba cdb can support. Because
   7978 				 * sd_send_scsi_READ_CAPACITY will retrieve
   7979 				 * the capacity by sending USCSI command, which
   7980 				 * is constrained by the max hba cdb. Actually,
   7981 				 * sd_send_scsi_READ_CAPACITY will return
   7982 				 * EINVAL when using bigger cdb than required
   7983 				 * cdb length. Will handle this case in
   7984 				 * "case EINVAL".
   7985 				 */
   7986 
   7987 				/*
   7988 				 * The following relies on
   7989 				 * sd_send_scsi_READ_CAPACITY never
   7990 				 * returning 0 for capacity and/or lbasize.
   7991 				 */
   7992 				sd_update_block_info(un, lbasize, capacity);
   7993 
   7994 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
   7995 				    "sd_unit_attach: un:0x%p capacity = %ld "
   7996 				    "blocks; lbasize= %ld.\n", un,
   7997 				    un->un_blockcount, un->un_tgt_blocksize);
   7998 
   7999 				break;
   8000 			}
   8001 			case EINVAL:
   8002 				/*
   8003 				 * In the case where the max-cdb-length property
   8004 				 * is smaller than the required CDB length for
   8005 				 * a SCSI device, a target driver can fail to
   8006 				 * attach to that device.
   8007 				 */
   8008 				scsi_log(SD_DEVINFO(un),
   8009 				    sd_label, CE_WARN,
   8010 				    "disk capacity is too large "
   8011 				    "for current cdb length");
   8012 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   8013 
   8014 				goto spinup_failed;
   8015 			case EACCES:
   8016 				/*
   8017 				 * Should never get here if the spin-up
   8018 				 * succeeded, but code it in anyway.
   8019 				 * From here, just continue with the attach...
   8020 				 */
   8021 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
   8022 				    "sd_unit_attach: un:0x%p "
   8023 				    "sd_send_scsi_READ_CAPACITY "
   8024 				    "returned reservation conflict\n", un);
   8025 				reservation_flag = SD_TARGET_IS_RESERVED;
   8026 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
   8027 				break;
   8028 			default:
   8029 				/*
   8030 				 * Likewise, should never get here if the
   8031 				 * spin-up succeeded. Just continue with
   8032 				 * the attach...
   8033 				 */
   8034 				if (status == EIO)
   8035 					sd_ssc_assessment(ssc,
   8036 					    SD_FMT_STATUS_CHECK);
   8037 				else
   8038 					sd_ssc_assessment(ssc,
   8039 					    SD_FMT_IGNORE);
   8040 				break;
   8041 			}
   8042 			break;
   8043 		case EACCES:
   8044 			/*
   8045 			 * Device is reserved by another host.  In this case
   8046 			 * we could not spin it up or read the capacity, but
   8047 			 * we continue with the attach anyway.
   8048 			 */
   8049 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   8050 			    "sd_unit_attach: un:0x%p spin-up reservation "
   8051 			    "conflict.\n", un);
   8052 			reservation_flag = SD_TARGET_IS_RESERVED;
   8053 			break;
   8054 		default:
   8055 			/* Fail the attach if the spin-up failed. */
   8056 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
   8057 			    "sd_unit_attach: un:0x%p spin-up failed.", un);
   8058 			goto spinup_failed;
   8059 		}
   8060 
   8061 	}
   8062 
   8063 	/*
   8064 	 * Check to see if this is a MMC drive
   8065 	 */
   8066 	if (ISCD(un)) {
   8067 		sd_set_mmc_caps(ssc);
   8068 	}
   8069 
   8070 	/*
   8071 	 * Add a zero-length attribute to tell the world we support
   8072 	 * kernel ioctls (for layered drivers)
   8073 	 */
   8074 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
   8075 	    DDI_KERNEL_IOCTL, NULL, 0);
   8076 
   8077 	/*
   8078 	 * Add a boolean property to tell the world we support
   8079 	 * the B_FAILFAST flag (for layered drivers)
   8080 	 */
   8081 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
   8082 	    "ddi-failfast-supported", NULL, 0);
   8083 
   8084 	/*
   8085 	 * Initialize power management
   8086 	 */
   8087 	mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL);
   8088 	cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL);
   8089 	sd_setup_pm(ssc, devi);
   8090 	if (un->un_f_pm_is_enabled == FALSE) {
   8091 		/*
   8092 		 * For performance, point to a jump table that does
   8093 		 * not include pm.
   8094 		 * The direct and priority chains don't change with PM.
   8095 		 *
   8096 		 * Note: this is currently done based on individual device
   8097 		 * capabilities. When an interface for determining system
   8098 		 * power enabled state becomes available, or when additional
   8099 		 * layers are added to the command chain, these values will
   8100 		 * have to be re-evaluated for correctness.
   8101 		 */
   8102 		if (un->un_f_non_devbsize_supported) {
   8103 			un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM;
   8104 		} else {
   8105 			un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM;
   8106 		}
   8107 		un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
   8108 	}
   8109 
   8110 	/*
   8111 	 * This property is set to 0 by HA software to avoid retries
   8112 	 * on a reserved disk. (The preferred property name is
   8113 	 * "retry-on-reservation-conflict") (1189689)
   8114 	 *
   8115 	 * Note: The use of a global here can have unintended consequences. A
   8116 	 * per instance variable is preferable to match the capabilities of
   8117 	 * different underlying hba's (4402600)
   8118 	 */
   8119 	sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi,
   8120 	    DDI_PROP_DONTPASS, "retry-on-reservation-conflict",
   8121 	    sd_retry_on_reservation_conflict);
   8122 	if (sd_retry_on_reservation_conflict != 0) {
   8123 		sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY,
   8124 		    devi, DDI_PROP_DONTPASS, sd_resv_conflict_name,
   8125 		    sd_retry_on_reservation_conflict);
   8126 	}
   8127 
   8128 	/* Set up options for QFULL handling. */
   8129 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
   8130 	    "qfull-retries", -1)) != -1) {
   8131 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries",
   8132 		    rval, 1);
   8133 	}
   8134 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
   8135 	    "qfull-retry-interval", -1)) != -1) {
   8136 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval",
   8137 		    rval, 1);
   8138 	}
   8139 
   8140 	/*
   8141 	 * This just prints a message that announces the existence of the
   8142 	 * device. The message is always printed in the system logfile, but
   8143 	 * only appears on the console if the system is booted with the
   8144 	 * -v (verbose) argument.
   8145 	 */
   8146 	ddi_report_dev(devi);
   8147 
   8148 	un->un_mediastate = DKIO_NONE;
   8149 
   8150 	/*
   8151 	 * Check if this is a SSD(Solid State Drive).
   8152 	 */
   8153 	sd_check_solid_state(ssc);
   8154 
   8155 	cmlb_alloc_handle(&un->un_cmlbhandle);
   8