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 (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
  24  */
  25 
  26 /*
  27  * Copyright 2011 cyril.galibern@opensvc.com
  28  * Copyright (c) 2011 Bayard G. Bell.  All rights reserved.
  29  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
  30  * Copyright 2012 DEY Storage Systems, Inc.  All rights reserved.
  31  * Copyright 2019 Nexenta Systems, Inc.
  32  */
  33 
  34 /*
  35  * SCSI disk target driver.
  36  */
  37 #include <sys/aio_req.h>
  38 #include <sys/byteorder.h>
  39 #include <sys/cdio.h>
  40 #include <sys/cmlb.h>
  41 #include <sys/debug.h>
  42 #include <sys/dkbad.h>
  43 #include <sys/dkio.h>
  44 #include <sys/dkioc_free_util.h>
  45 #include <sys/dklabel.h>
  46 #include <sys/dktp/fdisk.h>
  47 #include <sys/efi_partition.h>
  48 #include <sys/fdio.h>
  49 #include <sys/fm/protocol.h>
  50 #include <sys/fs/dv_node.h>
  51 #include <sys/kstat.h>
  52 #include <sys/mhd.h>
  53 #include <sys/proc.h>
  54 #include <sys/scsi/scsi.h>
  55 #include <sys/scsi/targets/sddef.h>
  56 #include <sys/sdt.h>
  57 #include <sys/sysevent/dev.h>
  58 #include <sys/sysevent/eventdefs.h>
  59 #include <sys/taskq.h>
  60 #include <sys/thread.h>
  61 #include <sys/uuid.h>
  62 #include <sys/var.h>
  63 #include <sys/vtoc.h>
  64 #include <sys/vtrace.h>
  65 
  66 #include "sd_xbuf.h"
  67 
  68 #define SD_MODULE_NAME  "SCSI Disk Driver"
  69 static  char *sd_label = "sd";
  70 
  71 /*
  72  * Driver global variables
  73  */
  74 
  75 #ifdef  SDDEBUG
  76 int     sd_force_pm_supported           = 0;
  77 #endif  /* SDDEBUG */
  78 
  79 void *sd_state                          = NULL;
  80 int sd_io_time                          = SD_IO_TIME;
  81 int sd_ua_retry_count                   = SD_UA_RETRY_COUNT;
  82 int sd_report_pfa                       = 1;
  83 int sd_max_throttle                     = SD_MAX_THROTTLE;
  84 int sd_min_throttle                     = SD_MIN_THROTTLE;
  85 int sd_rot_delay                        = 4; /* Default 4ms Rotation delay */
  86 int sd_qfull_throttle_enable            = TRUE;
  87 
  88 int sd_retry_on_reservation_conflict    = 1;
  89 int sd_reinstate_resv_delay             = SD_REINSTATE_RESV_DELAY;
  90 int sd_enable_lun_reset                 = FALSE;
  91 
  92 /*
  93  * Default safe I/O delay threshold of 30s for all devices.
  94  * Can be overriden for vendor/device id in sd.conf
  95  */
  96 hrtime_t sd_slow_io_threshold           = 30LL * NANOSEC;
  97 
  98 /*
  99  * Global data for debug logging. To enable debug printing, sd_component_mask
 100  * and sd_level_mask should be set to the desired bit patterns as outlined in
 101  * sddef.h.
 102  */
 103 uint_t  sd_component_mask               = 0x0;
 104 uint_t  sd_level_mask                   = 0x0;
 105 struct  sd_lun *sd_debug_un             = NULL;
 106 uint_t  sd_error_level                  = SCSI_ERR_RETRYABLE;
 107 
 108 /* Note: these may go away in the future... */
 109 static uint32_t sd_xbuf_active_limit    = 512;
 110 static uint32_t sd_xbuf_reserve_limit   = 16;
 111 
 112 static struct sd_resv_reclaim_request   sd_tr = { NULL, NULL, NULL, 0, 0, 0 };
 113 
 114 /*
 115  * Timer value used to reset the throttle after it has been reduced
 116  * (typically in response to TRAN_BUSY or STATUS_QFULL)
 117  */
 118 static int sd_reset_throttle_timeout    = SD_RESET_THROTTLE_TIMEOUT;
 119 static int sd_qfull_throttle_timeout    = SD_QFULL_THROTTLE_TIMEOUT;
 120 
 121 /*
 122  * Interval value associated with the media change scsi watch.
 123  */
 124 static int sd_check_media_time          = 3000000;
 125 
 126 /*
 127  * Wait value used for in progress operations during a DDI_SUSPEND
 128  */
 129 static int sd_wait_cmds_complete        = SD_WAIT_CMDS_COMPLETE;
 130 
 131 /*
 132  * sd_label_mutex protects a static buffer used in the disk label
 133  * component of the driver
 134  */
 135 static kmutex_t sd_label_mutex;
 136 
 137 /*
 138  * sd_detach_mutex protects un_layer_count, un_detach_count, and
 139  * un_opens_in_progress in the sd_lun structure.
 140  */
 141 static kmutex_t sd_detach_mutex;
 142 
 143 /*
 144  * Global buffer and mutex for debug logging
 145  */
 146 static char     sd_log_buf[1024];
 147 static kmutex_t sd_log_mutex;
 148 
 149 /*
 150  * Structs and globals for recording attached lun information.
 151  * This maintains a chain. Each node in the chain represents a SCSI controller.
 152  * The structure records the number of luns attached to each target connected
 153  * with the controller.
 154  * For parallel scsi device only.
 155  */
 156 struct sd_scsi_hba_tgt_lun {
 157         struct sd_scsi_hba_tgt_lun      *next;
 158         dev_info_t                      *pdip;
 159         int                             nlun[NTARGETS_WIDE];
 160 };
 161 
 162 /*
 163  * Flag to indicate the lun is attached or detached
 164  */
 165 #define SD_SCSI_LUN_ATTACH      0
 166 #define SD_SCSI_LUN_DETACH      1
 167 
 168 static kmutex_t sd_scsi_target_lun_mutex;
 169 static struct sd_scsi_hba_tgt_lun       *sd_scsi_target_lun_head = NULL;
 170 
 171 /*
 172  * "Smart" Probe Caching structs, globals, #defines, etc.
 173  * For parallel scsi and non-self-identify device only.
 174  */
 175 
 176 /*
 177  * The following resources and routines are implemented to support
 178  * "smart" probing, which caches the scsi_probe() results in an array,
 179  * in order to help avoid long probe times.
 180  */
 181 struct sd_scsi_probe_cache {
 182         struct  sd_scsi_probe_cache     *next;
 183         dev_info_t      *pdip;
 184         int             cache[NTARGETS_WIDE];
 185 };
 186 
 187 static kmutex_t sd_scsi_probe_cache_mutex;
 188 static struct   sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL;
 189 
 190 /*
 191  * Create taskq for all targets in the system. This is created at
 192  * _init(9E) and destroyed at _fini(9E).
 193  *
 194  * Note: here we set the minalloc to a reasonably high number to ensure that
 195  * we will have an adequate supply of task entries available at interrupt time.
 196  * This is used in conjunction with the TASKQ_PREPOPULATE flag in
 197  * sd_create_taskq().  Since we do not want to sleep for allocations at
 198  * interrupt time, set maxalloc equal to minalloc. That way we will just fail
 199  * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq
 200  * requests any one instant in time.
 201  */
 202 #define SD_TASKQ_NUMTHREADS     8
 203 #define SD_TASKQ_MINALLOC       256
 204 #define SD_TASKQ_MAXALLOC       256
 205 
 206 static taskq_t  *sd_tq = NULL;
 207 
 208 static int      sd_taskq_minalloc = SD_TASKQ_MINALLOC;
 209 static int      sd_taskq_maxalloc = SD_TASKQ_MAXALLOC;
 210 
 211 #define SD_BAIL_CHECK(a) if ((a)->un_detach_count != 0) { \
 212         mutex_exit(SD_MUTEX((a))); \
 213         return (ENXIO); \
 214         }
 215 /*
 216  * The following task queue is being created for the write part of
 217  * read-modify-write of non-512 block size devices.
 218  * Limit the number of threads to 1 for now. This number has been chosen
 219  * considering the fact that it applies only to dvd ram drives/MO drives
 220  * currently. Performance for which is not main criteria at this stage.
 221  * Note: It needs to be explored if we can use a single taskq in future
 222  */
 223 #define SD_WMR_TASKQ_NUMTHREADS 1
 224 static taskq_t  *sd_wmr_tq = NULL;
 225 
 226 /*
 227  * Power attribute table
 228  */
 229 static sd_power_attr_ss sd_pwr_ss = {
 230         { "NAME=spindle-motor", "0=off", "1=on", NULL },
 231         {0, 100},
 232         {30, 0},
 233         {20000, 0}
 234 };
 235 
 236 static sd_power_attr_pc sd_pwr_pc = {
 237         { "NAME=spindle-motor", "0=stopped", "1=standby", "2=idle",
 238                 "3=active", NULL },
 239         {0, 0, 0, 100},
 240         {90, 90, 20, 0},
 241         {15000, 15000, 1000, 0}
 242 };
 243 
 244 /*
 245  * Power level to power condition
 246  */
 247 static int sd_pl2pc[] = {
 248         SD_TARGET_START_VALID,
 249         SD_TARGET_STANDBY,
 250         SD_TARGET_IDLE,
 251         SD_TARGET_ACTIVE
 252 };
 253 
 254 /*
 255  * Vendor specific data name property declarations
 256  */
 257 
 258 static sd_tunables seagate_properties = {
 259         SEAGATE_THROTTLE_VALUE,
 260         0,
 261         0,
 262         0,
 263         0,
 264         0,
 265         0,
 266         0,
 267         0
 268 };
 269 
 270 static sd_tunables fujitsu_properties = {
 271         FUJITSU_THROTTLE_VALUE,
 272         0,
 273         0,
 274         0,
 275         0,
 276         0,
 277         0,
 278         0,
 279         0
 280 };
 281 
 282 static sd_tunables ibm_properties = {
 283         IBM_THROTTLE_VALUE,
 284         0,
 285         0,
 286         0,
 287         0,
 288         0,
 289         0,
 290         0,
 291         0
 292 };
 293 
 294 static sd_tunables purple_properties = {
 295         PURPLE_THROTTLE_VALUE,
 296         0,
 297         0,
 298         PURPLE_BUSY_RETRIES,
 299         PURPLE_RESET_RETRY_COUNT,
 300         PURPLE_RESERVE_RELEASE_TIME,
 301         0,
 302         0,
 303         0
 304 };
 305 
 306 static sd_tunables sve_properties = {
 307         SVE_THROTTLE_VALUE,
 308         0,
 309         0,
 310         SVE_BUSY_RETRIES,
 311         SVE_RESET_RETRY_COUNT,
 312         SVE_RESERVE_RELEASE_TIME,
 313         SVE_MIN_THROTTLE_VALUE,
 314         SVE_DISKSORT_DISABLED_FLAG,
 315         0
 316 };
 317 
 318 static sd_tunables maserati_properties = {
 319         0,
 320         0,
 321         0,
 322         0,
 323         0,
 324         0,
 325         0,
 326         MASERATI_DISKSORT_DISABLED_FLAG,
 327         MASERATI_LUN_RESET_ENABLED_FLAG
 328 };
 329 
 330 static sd_tunables pirus_properties = {
 331         PIRUS_THROTTLE_VALUE,
 332         0,
 333         PIRUS_NRR_COUNT,
 334         PIRUS_BUSY_RETRIES,
 335         PIRUS_RESET_RETRY_COUNT,
 336         0,
 337         PIRUS_MIN_THROTTLE_VALUE,
 338         PIRUS_DISKSORT_DISABLED_FLAG,
 339         PIRUS_LUN_RESET_ENABLED_FLAG
 340 };
 341 
 342 static sd_tunables elite_properties = {
 343         ELITE_THROTTLE_VALUE,
 344         0,
 345         0,
 346         0,
 347         0,
 348         0,
 349         0,
 350         0,
 351         0
 352 };
 353 
 354 static sd_tunables st31200n_properties = {
 355         ST31200N_THROTTLE_VALUE,
 356         0,
 357         0,
 358         0,
 359         0,
 360         0,
 361         0,
 362         0,
 363         0
 364 };
 365 
 366 static sd_tunables lsi_properties_scsi = {
 367         LSI_THROTTLE_VALUE,
 368         0,
 369         LSI_NOTREADY_RETRIES,
 370         0,
 371         0,
 372         0,
 373         0,
 374         0,
 375         0
 376 };
 377 
 378 static sd_tunables symbios_properties = {
 379         SYMBIOS_THROTTLE_VALUE,
 380         0,
 381         SYMBIOS_NOTREADY_RETRIES,
 382         0,
 383         0,
 384         0,
 385         0,
 386         0,
 387         0
 388 };
 389 
 390 static sd_tunables lsi_properties = {
 391         0,
 392         0,
 393         LSI_NOTREADY_RETRIES,
 394         0,
 395         0,
 396         0,
 397         0,
 398         0,
 399         0
 400 };
 401 
 402 static sd_tunables lsi_oem_properties = {
 403         0,
 404         0,
 405         LSI_OEM_NOTREADY_RETRIES,
 406         0,
 407         0,
 408         0,
 409         0,
 410         0,
 411         0,
 412         1
 413 };
 414 
 415 #if (defined(SD_PROP_TST))
 416 #define SD_TST_CTYPE_VAL        CTYPE_CDROM
 417 #define SD_TST_THROTTLE_VAL     16
 418 #define SD_TST_NOTREADY_VAL     12
 419 #define SD_TST_BUSY_VAL         60
 420 #define SD_TST_RST_RETRY_VAL    36
 421 #define SD_TST_RSV_REL_TIME     60
 422 static sd_tunables tst_properties = {
 423         SD_TST_THROTTLE_VAL,
 424         SD_TST_CTYPE_VAL,
 425         SD_TST_NOTREADY_VAL,
 426         SD_TST_BUSY_VAL,
 427         SD_TST_RST_RETRY_VAL,
 428         SD_TST_RSV_REL_TIME,
 429         0,
 430         0,
 431         0
 432 };
 433 #endif
 434 
 435 /* This is similar to the ANSI toupper implementation */
 436 #define SD_TOUPPER(C)   (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C))
 437 
 438 /*
 439  * Static Driver Configuration Table
 440  *
 441  * This is the table of disks which need throttle adjustment (or, perhaps
 442  * something else as defined by the flags at a future time.)  device_id
 443  * is a string consisting of concatenated vid (vendor), pid (product/model)
 444  * and revision strings as defined in the scsi_inquiry structure.  Offsets of
 445  * the parts of the string are as defined by the sizes in the scsi_inquiry
 446  * structure.  Device type is searched as far as the device_id string is
 447  * defined.  Flags defines which values are to be set in the driver from the
 448  * properties list.
 449  *
 450  * Entries below which begin and end with a "*" are a special case.
 451  * These do not have a specific vendor, and the string which follows
 452  * can appear anywhere in the 16 byte PID portion of the inquiry data.
 453  *
 454  * Entries below which begin and end with a " " (blank) are a special
 455  * case. The comparison function will treat multiple consecutive blanks
 456  * as equivalent to a single blank. For example, this causes a
 457  * sd_disk_table entry of " NEC CDROM " to match a device's id string
 458  * of  "NEC       CDROM".
 459  *
 460  * Note: The MD21 controller type has been obsoleted.
 461  *       ST318202F is a Legacy device
 462  *       MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been
 463  *       made with an FC connection. The entries here are a legacy.
 464  */
 465 static sd_disk_config_t sd_disk_table[] = {
 466         { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
 467         { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
 468         { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
 469         { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
 470         { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties },
 471         { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties },
 472         { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties },
 473         { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties },
 474         { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties },
 475         { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties },
 476         { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties },
 477         { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties },
 478         { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties },
 479         { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties },
 480         { "FUJITSU MAG3091F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
 481         { "FUJITSU MAG3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
 482         { "FUJITSU MAA3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
 483         { "FUJITSU MAF3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
 484         { "FUJITSU MAL3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
 485         { "FUJITSU MAL3738F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
 486         { "FUJITSU MAM3182FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
 487         { "FUJITSU MAM3364FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
 488         { "FUJITSU MAM3738FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
 489         { "IBM     DDYFT1835",  SD_CONF_BSET_THROTTLE, &ibm_properties },
 490         { "IBM     DDYFT3695",  SD_CONF_BSET_THROTTLE, &ibm_properties },
 491         { "IBM     IC35LF2D2",  SD_CONF_BSET_THROTTLE, &ibm_properties },
 492         { "IBM     IC35LF2PR",  SD_CONF_BSET_THROTTLE, &ibm_properties },
 493         { "IBM     1724-100",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 494         { "IBM     1726-2xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 495         { "IBM     1726-22x",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 496         { "IBM     1726-4xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 497         { "IBM     1726-42x",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 498         { "IBM     1726-3xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 499         { "IBM     3526",       SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 500         { "IBM     3542",       SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 501         { "IBM     3552",       SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 502         { "IBM     1722",       SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 503         { "IBM     1742",       SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 504         { "IBM     1815",       SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 505         { "IBM     FAStT",      SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 506         { "IBM     1814",       SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 507         { "IBM     1814-200",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 508         { "IBM     1818",       SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 509         { "DELL    MD3000",     SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 510         { "DELL    MD3000i",    SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 511         { "LSI     INF",        SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 512         { "ENGENIO INF",        SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 513         { "SGI     TP",         SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 514         { "SGI     IS",         SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 515         { "*CSM100_*",          SD_CONF_BSET_NRR_COUNT|
 516                                 SD_CONF_BSET_CACHE_IS_NV,
 517                                 &lsi_oem_properties },
 518         { "*CSM200_*",          SD_CONF_BSET_NRR_COUNT|
 519                                 SD_CONF_BSET_CACHE_IS_NV,
 520                                 &lsi_oem_properties },
 521         { "Fujitsu SX300",      SD_CONF_BSET_THROTTLE, &lsi_oem_properties },
 522         { "LSI",                SD_CONF_BSET_NRR_COUNT, &lsi_properties },
 523         { "SUN     T3",         SD_CONF_BSET_THROTTLE|
 524                                 SD_CONF_BSET_BSY_RETRY_COUNT|
 525                                 SD_CONF_BSET_RST_RETRIES|
 526                                 SD_CONF_BSET_RSV_REL_TIME,
 527                                 &purple_properties },
 528         { "SUN     SESS01",     SD_CONF_BSET_THROTTLE|
 529                                 SD_CONF_BSET_BSY_RETRY_COUNT|
 530                                 SD_CONF_BSET_RST_RETRIES|
 531                                 SD_CONF_BSET_RSV_REL_TIME|
 532                                 SD_CONF_BSET_MIN_THROTTLE|
 533                                 SD_CONF_BSET_DISKSORT_DISABLED,
 534                                 &sve_properties },
 535         { "SUN     T4",         SD_CONF_BSET_THROTTLE|
 536                                 SD_CONF_BSET_BSY_RETRY_COUNT|
 537                                 SD_CONF_BSET_RST_RETRIES|
 538                                 SD_CONF_BSET_RSV_REL_TIME,
 539                                 &purple_properties },
 540         { "SUN     SVE01",      SD_CONF_BSET_DISKSORT_DISABLED|
 541                                 SD_CONF_BSET_LUN_RESET_ENABLED,
 542                                 &maserati_properties },
 543         { "SUN     SE6920",     SD_CONF_BSET_THROTTLE|
 544                                 SD_CONF_BSET_NRR_COUNT|
 545                                 SD_CONF_BSET_BSY_RETRY_COUNT|
 546                                 SD_CONF_BSET_RST_RETRIES|
 547                                 SD_CONF_BSET_MIN_THROTTLE|
 548                                 SD_CONF_BSET_DISKSORT_DISABLED|
 549                                 SD_CONF_BSET_LUN_RESET_ENABLED,
 550                                 &pirus_properties },
 551         { "SUN     SE6940",     SD_CONF_BSET_THROTTLE|
 552                                 SD_CONF_BSET_NRR_COUNT|
 553                                 SD_CONF_BSET_BSY_RETRY_COUNT|
 554                                 SD_CONF_BSET_RST_RETRIES|
 555                                 SD_CONF_BSET_MIN_THROTTLE|
 556                                 SD_CONF_BSET_DISKSORT_DISABLED|
 557                                 SD_CONF_BSET_LUN_RESET_ENABLED,
 558                                 &pirus_properties },
 559         { "SUN     StorageTek 6920", SD_CONF_BSET_THROTTLE|
 560                                 SD_CONF_BSET_NRR_COUNT|
 561                                 SD_CONF_BSET_BSY_RETRY_COUNT|
 562                                 SD_CONF_BSET_RST_RETRIES|
 563                                 SD_CONF_BSET_MIN_THROTTLE|
 564                                 SD_CONF_BSET_DISKSORT_DISABLED|
 565                                 SD_CONF_BSET_LUN_RESET_ENABLED,
 566                                 &pirus_properties },
 567         { "SUN     StorageTek 6940", SD_CONF_BSET_THROTTLE|
 568                                 SD_CONF_BSET_NRR_COUNT|
 569                                 SD_CONF_BSET_BSY_RETRY_COUNT|
 570                                 SD_CONF_BSET_RST_RETRIES|
 571                                 SD_CONF_BSET_MIN_THROTTLE|
 572                                 SD_CONF_BSET_DISKSORT_DISABLED|
 573                                 SD_CONF_BSET_LUN_RESET_ENABLED,
 574                                 &pirus_properties },
 575         { "SUN     PSX1000",    SD_CONF_BSET_THROTTLE|
 576                                 SD_CONF_BSET_NRR_COUNT|
 577                                 SD_CONF_BSET_BSY_RETRY_COUNT|
 578                                 SD_CONF_BSET_RST_RETRIES|
 579                                 SD_CONF_BSET_MIN_THROTTLE|
 580                                 SD_CONF_BSET_DISKSORT_DISABLED|
 581                                 SD_CONF_BSET_LUN_RESET_ENABLED,
 582                                 &pirus_properties },
 583         { "SUN     SE6330",     SD_CONF_BSET_THROTTLE|
 584                                 SD_CONF_BSET_NRR_COUNT|
 585                                 SD_CONF_BSET_BSY_RETRY_COUNT|
 586                                 SD_CONF_BSET_RST_RETRIES|
 587                                 SD_CONF_BSET_MIN_THROTTLE|
 588                                 SD_CONF_BSET_DISKSORT_DISABLED|
 589                                 SD_CONF_BSET_LUN_RESET_ENABLED,
 590                                 &pirus_properties },
 591         { "SUN     STK6580_6780", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 592         { "SUN     SUN_6180",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 593         { "STK     OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 594         { "STK     OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 595         { "STK     BladeCtlr",  SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 596         { "STK     FLEXLINE",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 597         { "SYMBIOS",            SD_CONF_BSET_NRR_COUNT, &symbios_properties },
 598         { "SEAGATE ST42400N",   SD_CONF_BSET_THROTTLE, &elite_properties },
 599         { "SEAGATE ST31200N",   SD_CONF_BSET_THROTTLE, &st31200n_properties },
 600         { "SEAGATE ST41600N",   SD_CONF_BSET_TUR_CHECK, NULL },
 601         { "CONNER  CP30540",    SD_CONF_BSET_NOCACHE,  NULL },
 602         { "*SUN0104*",          SD_CONF_BSET_FAB_DEVID, NULL },
 603         { "*SUN0207*",          SD_CONF_BSET_FAB_DEVID, NULL },
 604         { "*SUN0327*",          SD_CONF_BSET_FAB_DEVID, NULL },
 605         { "*SUN0340*",          SD_CONF_BSET_FAB_DEVID, NULL },
 606         { "*SUN0424*",          SD_CONF_BSET_FAB_DEVID, NULL },
 607         { "*SUN0669*",          SD_CONF_BSET_FAB_DEVID, NULL },
 608         { "*SUN1.0G*",          SD_CONF_BSET_FAB_DEVID, NULL },
 609         { "SYMBIOS INF-01-00",  SD_CONF_BSET_FAB_DEVID, NULL },
 610         { "SYMBIOS",            SD_CONF_BSET_THROTTLE|
 611                                 SD_CONF_BSET_NRR_COUNT,
 612                                 &symbios_properties },
 613         { "LSI",                SD_CONF_BSET_THROTTLE|
 614                                 SD_CONF_BSET_NRR_COUNT,
 615                                 &lsi_properties_scsi },
 616         { " NEC CD-ROM DRIVE:260 ", SD_CONF_BSET_PLAYMSF_BCD|
 617                                 SD_CONF_BSET_READSUB_BCD|
 618                                 SD_CONF_BSET_READ_TOC_ADDR_BCD|
 619                                 SD_CONF_BSET_NO_READ_HEADER|
 620                                 SD_CONF_BSET_READ_CD_XD4,
 621                                 NULL },
 622         { " NEC CD-ROM DRIVE:270 ", SD_CONF_BSET_PLAYMSF_BCD|
 623                                 SD_CONF_BSET_READSUB_BCD|
 624                                 SD_CONF_BSET_READ_TOC_ADDR_BCD|
 625                                 SD_CONF_BSET_NO_READ_HEADER|
 626                                 SD_CONF_BSET_READ_CD_XD4,
 627                                 NULL },
 628 #if (defined(SD_PROP_TST))
 629         { "VENDOR  PRODUCT ",   SD_CONF_BSET_THROTTLE|
 630                                 SD_CONF_BSET_CTYPE|
 631                                 SD_CONF_BSET_NRR_COUNT|
 632                                 SD_CONF_BSET_FAB_DEVID|
 633                                 SD_CONF_BSET_NOCACHE|
 634                                 SD_CONF_BSET_BSY_RETRY_COUNT|
 635                                 SD_CONF_BSET_PLAYMSF_BCD|
 636                                 SD_CONF_BSET_READSUB_BCD|
 637                                 SD_CONF_BSET_READ_TOC_TRK_BCD|
 638                                 SD_CONF_BSET_READ_TOC_ADDR_BCD|
 639                                 SD_CONF_BSET_NO_READ_HEADER|
 640                                 SD_CONF_BSET_READ_CD_XD4|
 641                                 SD_CONF_BSET_RST_RETRIES|
 642                                 SD_CONF_BSET_RSV_REL_TIME|
 643                                 SD_CONF_BSET_TUR_CHECK,
 644                                 &tst_properties},
 645 #endif
 646 };
 647 
 648 static const int sd_disk_table_size =
 649         sizeof (sd_disk_table)/ sizeof (sd_disk_config_t);
 650 
 651 /*
 652  * Emulation mode disk drive VID/PID table
 653  */
 654 static char sd_flash_dev_table[][25] = {
 655         "ATA     MARVELL SD88SA02",
 656         "MARVELL SD88SA02",
 657         "TOSHIBA THNSNV05",
 658 };
 659 
 660 static const int sd_flash_dev_table_size =
 661         sizeof (sd_flash_dev_table) / sizeof (sd_flash_dev_table[0]);
 662 
 663 #define SD_INTERCONNECT_PARALLEL        0
 664 #define SD_INTERCONNECT_FABRIC          1
 665 #define SD_INTERCONNECT_FIBRE           2
 666 #define SD_INTERCONNECT_SSA             3
 667 #define SD_INTERCONNECT_SATA            4
 668 #define SD_INTERCONNECT_SAS             5
 669 
 670 #define SD_IS_PARALLEL_SCSI(un)         \
 671         ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL)
 672 #define SD_IS_SERIAL(un)                \
 673         (((un)->un_interconnect_type == SD_INTERCONNECT_SATA) ||\
 674         ((un)->un_interconnect_type == SD_INTERCONNECT_SAS))
 675 
 676 /*
 677  * Definitions used by device id registration routines
 678  */
 679 #define VPD_HEAD_OFFSET         3       /* size of head for vpd page */
 680 #define VPD_PAGE_LENGTH         3       /* offset for pge length data */
 681 #define VPD_MODE_PAGE           1       /* offset into vpd pg for "page code" */
 682 
 683 static kmutex_t sd_sense_mutex = {0};
 684 
 685 /*
 686  * Macros for updates of the driver state
 687  */
 688 #define New_state(un, s)        \
 689         (un)->un_last_state = (un)->un_state, (un)->un_state = (s)
 690 #define Restore_state(un)       \
 691         { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); }
 692 
 693 static struct sd_cdbinfo sd_cdbtab[] = {
 694         { CDB_GROUP0, 0x00,        0x1FFFFF,   0xFF,        },
 695         { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF,      },
 696         { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF,  },
 697         { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, },
 698 };
 699 
 700 /*
 701  * Specifies the number of seconds that must have elapsed since the last
 702  * cmd. has completed for a device to be declared idle to the PM framework.
 703  */
 704 static int sd_pm_idletime = 1;
 705 
 706 /*
 707  * Internal function prototypes
 708  */
 709 typedef struct unmap_param_hdr_s {
 710         uint16_t        uph_data_len;
 711         uint16_t        uph_descr_data_len;
 712         uint32_t        uph_reserved;
 713 } unmap_param_hdr_t;
 714 
 715 typedef struct unmap_blk_descr_s {
 716         uint64_t        ubd_lba;
 717         uint32_t        ubd_lba_cnt;
 718         uint32_t        ubd_reserved;
 719 } unmap_blk_descr_t;
 720 
 721 /* Max number of block descriptors in UNMAP command */
 722 #define SD_UNMAP_MAX_DESCR \
 723         ((UINT16_MAX - sizeof (unmap_param_hdr_t)) / sizeof (unmap_blk_descr_t))
 724 /* Max size of the UNMAP parameter list in bytes */
 725 #define SD_UNMAP_PARAM_LIST_MAXSZ       (sizeof (unmap_param_hdr_t) + \
 726         SD_UNMAP_MAX_DESCR * sizeof (unmap_blk_descr_t))
 727 
 728 int _init(void);
 729 int _fini(void);
 730 int _info(struct modinfo *modinfop);
 731 
 732 /*PRINTFLIKE3*/
 733 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...);
 734 /*PRINTFLIKE3*/
 735 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...);
 736 /*PRINTFLIKE3*/
 737 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...);
 738 
 739 static int sdprobe(dev_info_t *devi);
 740 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
 741     void **result);
 742 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
 743     int mod_flags, char *name, caddr_t valuep, int *lengthp);
 744 
 745 /*
 746  * Smart probe for parallel scsi
 747  */
 748 static void sd_scsi_probe_cache_init(void);
 749 static void sd_scsi_probe_cache_fini(void);
 750 static void sd_scsi_clear_probe_cache(void);
 751 static int  sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)());
 752 
 753 /*
 754  * Attached luns on target for parallel scsi
 755  */
 756 static void sd_scsi_target_lun_init(void);
 757 static void sd_scsi_target_lun_fini(void);
 758 static int  sd_scsi_get_target_lun_count(dev_info_t *dip, int target);
 759 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag);
 760 
 761 static int      sd_spin_up_unit(sd_ssc_t *ssc);
 762 
 763 /*
 764  * Using sd_ssc_init to establish sd_ssc_t struct
 765  * Using sd_ssc_send to send uscsi internal command
 766  * Using sd_ssc_fini to free sd_ssc_t struct
 767  */
 768 static sd_ssc_t *sd_ssc_init(struct sd_lun *un);
 769 static int sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd,
 770     int flag, enum uio_seg dataspace, int path_flag);
 771 static void sd_ssc_fini(sd_ssc_t *ssc);
 772 
 773 /*
 774  * Using sd_ssc_assessment to set correct type-of-assessment
 775  * Using sd_ssc_post to post ereport & system log
 776  *       sd_ssc_post will call sd_ssc_print to print system log
 777  *       sd_ssc_post will call sd_ssc_ereport_post to post ereport
 778  */
 779 static void sd_ssc_assessment(sd_ssc_t *ssc,
 780     enum sd_type_assessment tp_assess);
 781 
 782 static void sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess);
 783 static void sd_ssc_print(sd_ssc_t *ssc, int sd_severity);
 784 static void sd_ssc_ereport_post(sd_ssc_t *ssc,
 785     enum sd_driver_assessment drv_assess);
 786 
 787 /*
 788  * Using sd_ssc_set_info to mark an un-decodable-data error.
 789  * Using sd_ssc_extract_info to transfer information from internal
 790  *       data structures to sd_ssc_t.
 791  */
 792 static void sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp,
 793     const char *fmt, ...);
 794 static void sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un,
 795     struct scsi_pkt *pktp, struct buf *bp, struct sd_xbuf *xp);
 796 
 797 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
 798     enum uio_seg dataspace, int path_flag);
 799 
 800 #ifdef _LP64
 801 static void     sd_enable_descr_sense(sd_ssc_t *ssc);
 802 static void     sd_reenable_dsense_task(void *arg);
 803 #endif /* _LP64 */
 804 
 805 static void     sd_set_mmc_caps(sd_ssc_t *ssc);
 806 
 807 static void sd_read_unit_properties(struct sd_lun *un);
 808 static int  sd_process_sdconf_file(struct sd_lun *un);
 809 static void sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str);
 810 static char *sd_strtok_r(char *string, const char *sepset, char **lasts);
 811 static void sd_set_properties(struct sd_lun *un, char *name, char *value);
 812 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags,
 813     int *data_list, sd_tunables *values);
 814 static void sd_process_sdconf_table(struct sd_lun *un);
 815 static int  sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen);
 816 static int  sd_blank_cmp(struct sd_lun *un, char *id, int idlen);
 817 static int  sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
 818         int list_len, char *dataname_ptr);
 819 static void sd_set_vers1_properties(struct sd_lun *un, int flags,
 820     sd_tunables *prop_list);
 821 
 822 static void sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi,
 823     int reservation_flag);
 824 static int  sd_get_devid(sd_ssc_t *ssc);
 825 static ddi_devid_t sd_create_devid(sd_ssc_t *ssc);
 826 static int  sd_write_deviceid(sd_ssc_t *ssc);
 827 static int  sd_check_vpd_page_support(sd_ssc_t *ssc);
 828 
 829 #ifdef notyet
 830 static void sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi);
 831 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un);
 832 #endif
 833 
 834 static int  sd_ddi_suspend(dev_info_t *devi);
 835 static int  sd_ddi_resume(dev_info_t *devi);
 836 static int  sd_pm_state_change(struct sd_lun *un, int level, int flag);
 837 static int  sdpower(dev_info_t *devi, int component, int level);
 838 
 839 static int  sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd);
 840 static int  sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd);
 841 static void sd_unit_attach(void *arg);
 842 static int  sd_unit_detach(dev_info_t *devi);
 843 
 844 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi);
 845 static void sd_create_errstats(struct sd_lun *un, int instance);
 846 static void sd_set_errstats(struct sd_lun *un);
 847 static void sd_set_pstats(struct sd_lun *un);
 848 
 849 static int  sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
 850 static int  sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt);
 851 static int  sd_send_polled_RQS(struct sd_lun *un);
 852 static int  sd_ddi_scsi_poll(struct scsi_pkt *pkt);
 853 
 854 /*
 855  * Defines for sd_cache_control
 856  */
 857 #define SD_CACHE_ENABLE         1
 858 #define SD_CACHE_DISABLE        0
 859 #define SD_CACHE_NOCHANGE       -1
 860 
 861 static int   sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag);
 862 static int   sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled);
 863 static void  sd_get_write_cache_changeable(sd_ssc_t *ssc, int *is_changeable);
 864 static void  sd_get_nv_sup(sd_ssc_t *ssc);
 865 static dev_t sd_make_device(dev_info_t *devi);
 866 static void  sd_check_bdc_vpd(sd_ssc_t *ssc);
 867 static void  sd_check_emulation_mode(sd_ssc_t *ssc);
 868 static void  sd_update_block_info(struct sd_lun *un, uint32_t lbasize,
 869         uint64_t capacity);
 870 
 871 /*
 872  * Driver entry point functions.
 873  */
 874 static int  sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
 875 static int  sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p);
 876 static int  sd_ready_and_valid(sd_ssc_t *ssc, int part);
 877 
 878 static void sdmin(struct buf *bp);
 879 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p);
 880 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p);
 881 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p);
 882 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p);
 883 
 884 static int sdstrategy(struct buf *bp);
 885 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *);
 886 
 887 /*
 888  * Function prototypes for layering functions in the iostart chain.
 889  */
 890 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un,
 891         struct buf *bp);
 892 static void sd_mapblocksize_iostart(int index, struct sd_lun *un,
 893         struct buf *bp);
 894 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp);
 895 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un,
 896         struct buf *bp);
 897 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp);
 898 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp);
 899 
 900 /*
 901  * Function prototypes for layering functions in the iodone chain.
 902  */
 903 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp);
 904 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp);
 905 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un,
 906         struct buf *bp);
 907 static void sd_mapblocksize_iodone(int index, struct sd_lun *un,
 908         struct buf *bp);
 909 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp);
 910 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un,
 911         struct buf *bp);
 912 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp);
 913 
 914 /*
 915  * Prototypes for functions to support buf(9S) based IO.
 916  */
 917 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg);
 918 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **);
 919 static void sd_destroypkt_for_buf(struct buf *);
 920 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp,
 921         struct buf *bp, int flags,
 922         int (*callback)(caddr_t), caddr_t callback_arg,
 923         diskaddr_t lba, uint32_t blockcount);
 924 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp,
 925         struct buf *bp, diskaddr_t lba, uint32_t blockcount);
 926 
 927 /*
 928  * Prototypes for functions to support USCSI IO.
 929  */
 930 static int sd_uscsi_strategy(struct buf *bp);
 931 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **);
 932 static void sd_destroypkt_for_uscsi(struct buf *);
 933 
 934 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
 935         uchar_t chain_type, void *pktinfop);
 936 
 937 static int  sd_pm_entry(struct sd_lun *un);
 938 static void sd_pm_exit(struct sd_lun *un);
 939 
 940 static void sd_pm_idletimeout_handler(void *arg);
 941 
 942 /*
 943  * sd_core internal functions (used at the sd_core_io layer).
 944  */
 945 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp);
 946 static void sdintr(struct scsi_pkt *pktp);
 947 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp);
 948 
 949 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
 950         enum uio_seg dataspace, int path_flag);
 951 
 952 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen,
 953         daddr_t blkno, int (*func)(struct buf *));
 954 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen,
 955         uint_t bflags, daddr_t blkno, int (*func)(struct buf *));
 956 static void sd_bioclone_free(struct buf *bp);
 957 static void sd_shadow_buf_free(struct buf *bp);
 958 
 959 static void sd_print_transport_rejected_message(struct sd_lun *un,
 960         struct sd_xbuf *xp, int code);
 961 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp,
 962     void *arg, int code);
 963 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp,
 964     void *arg, int code);
 965 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp,
 966     void *arg, int code);
 967 
 968 static void sd_retry_command(struct sd_lun *un, struct buf *bp,
 969         int retry_check_flag,
 970         void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp,
 971                 int c),
 972         void *user_arg, int failure_code,  clock_t retry_delay,
 973         void (*statp)(kstat_io_t *));
 974 
 975 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp,
 976         clock_t retry_delay, void (*statp)(kstat_io_t *));
 977 
 978 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
 979         int retry_check_flag, struct scsi_pkt *pktp);
 980 static void sd_start_retry_command(void *arg);
 981 static void sd_start_direct_priority_command(void *arg);
 982 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp,
 983         int errcode);
 984 static void sd_return_failed_command_no_restart(struct sd_lun *un,
 985         struct buf *bp, int errcode);
 986 static void sd_return_command(struct sd_lun *un, struct buf *bp);
 987 static void sd_sync_with_callback(struct sd_lun *un);
 988 static int sdrunout(caddr_t arg);
 989 
 990 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp);
 991 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp);
 992 
 993 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type);
 994 static void sd_restore_throttle(void *arg);
 995 
 996 static void sd_init_cdb_limits(struct sd_lun *un);
 997 
 998 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
 999         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1000 
1001 /*
1002  * Error handling functions
1003  */
1004 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
1005         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1006 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp,
1007         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1008 static void sd_pkt_status_reservation_conflict(struct sd_lun *un,
1009         struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1010 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
1011         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1012 
1013 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp,
1014         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1015 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
1016         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1017 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp,
1018         struct sd_xbuf *xp, size_t actual_len);
1019 static void sd_decode_sense(struct sd_lun *un, struct buf *bp,
1020         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1021 
1022 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp,
1023         void *arg, int code);
1024 
1025 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
1026         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1027 static void sd_sense_key_recoverable_error(struct sd_lun *un,
1028         uint8_t *sense_datap,
1029         struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1030 static void sd_sense_key_not_ready(struct sd_lun *un,
1031         uint8_t *sense_datap,
1032         struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1033 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
1034         uint8_t *sense_datap,
1035         struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1036 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
1037         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1038 static void sd_sense_key_unit_attention(struct sd_lun *un,
1039         uint8_t *sense_datap,
1040         struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1041 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
1042         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1043 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
1044         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1045 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
1046         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1047 static void sd_sense_key_default(struct sd_lun *un,
1048         uint8_t *sense_datap,
1049         struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1050 
1051 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp,
1052         void *arg, int flag);
1053 
1054 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
1055         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1056 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
1057         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1058 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
1059         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1060 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
1061         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1062 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
1063         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1064 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
1065         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1066 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
1067         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1068 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
1069         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1070 
1071 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp);
1072 
1073 static void sd_start_stop_unit_callback(void *arg);
1074 static void sd_start_stop_unit_task(void *arg);
1075 
1076 static void sd_taskq_create(void);
1077 static void sd_taskq_delete(void);
1078 static void sd_target_change_task(void *arg);
1079 static void sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag);
1080 static void sd_log_lun_expansion_event(struct sd_lun *un, int km_flag);
1081 static void sd_log_eject_request_event(struct sd_lun *un, int km_flag);
1082 static void sd_media_change_task(void *arg);
1083 
1084 static int sd_handle_mchange(struct sd_lun *un);
1085 static int sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag);
1086 static int sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp,
1087         uint32_t *lbap, int path_flag);
1088 static int sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
1089         uint32_t *lbap, uint32_t *psp, int path_flag);
1090 static int sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag,
1091         int flag, int path_flag);
1092 static int sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr,
1093         size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp);
1094 static int sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag);
1095 static int sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc,
1096         uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp);
1097 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc,
1098         uchar_t usr_cmd, uchar_t *usr_bufp);
1099 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un,
1100         struct dk_callback *dkc);
1101 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp);
1102 static int sd_send_scsi_UNMAP(dev_t dev, sd_ssc_t *ssc, dkioc_free_list_t *dfl,
1103         int flag);
1104 static int sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc,
1105         struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1106         uchar_t *bufaddr, uint_t buflen, int path_flag);
1107 static int sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
1108         struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1109         uchar_t *bufaddr, uint_t buflen, char feature, int path_flag);
1110 static int sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize,
1111         uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag);
1112 static int sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize,
1113         uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag);
1114 static int sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
1115         size_t buflen, daddr_t start_block, int path_flag);
1116 #define sd_send_scsi_READ(ssc, bufaddr, buflen, start_block, path_flag) \
1117         sd_send_scsi_RDWR(ssc, SCMD_READ, bufaddr, buflen, start_block, \
1118         path_flag)
1119 #define sd_send_scsi_WRITE(ssc, bufaddr, buflen, start_block, path_flag)\
1120         sd_send_scsi_RDWR(ssc, SCMD_WRITE, bufaddr, buflen, start_block,\
1121         path_flag)
1122 
1123 static int sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr,
1124         uint16_t buflen, uchar_t page_code, uchar_t page_control,
1125         uint16_t param_ptr, int path_flag);
1126 static int sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc,
1127         uchar_t *bufaddr, size_t buflen, uchar_t class_req);
1128 static boolean_t sd_gesn_media_data_valid(uchar_t *data);
1129 
1130 static int  sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un);
1131 static void sd_free_rqs(struct sd_lun *un);
1132 
1133 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title,
1134         uchar_t *data, int len, int fmt);
1135 static void sd_panic_for_res_conflict(struct sd_lun *un);
1136 
1137 /*
1138  * Disk Ioctl Function Prototypes
1139  */
1140 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag);
1141 static int sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag);
1142 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag);
1143 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag);
1144 
1145 /*
1146  * Multi-host Ioctl Prototypes
1147  */
1148 static int sd_check_mhd(dev_t dev, int interval);
1149 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1150 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt);
1151 static char *sd_sname(uchar_t status);
1152 static void sd_mhd_resvd_recover(void *arg);
1153 static void sd_resv_reclaim_thread();
1154 static int sd_take_ownership(dev_t dev, struct mhioctkown *p);
1155 static int sd_reserve_release(dev_t dev, int cmd);
1156 static void sd_rmv_resv_reclaim_req(dev_t dev);
1157 static void sd_mhd_reset_notify_cb(caddr_t arg);
1158 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un,
1159         mhioc_inkeys_t *usrp, int flag);
1160 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un,
1161         mhioc_inresvs_t *usrp, int flag);
1162 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag);
1163 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag);
1164 static int sd_mhdioc_release(dev_t dev);
1165 static int sd_mhdioc_register_devid(dev_t dev);
1166 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag);
1167 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag);
1168 
1169 /*
1170  * SCSI removable prototypes
1171  */
1172 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag);
1173 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1174 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1175 static int sr_pause_resume(dev_t dev, int mode);
1176 static int sr_play_msf(dev_t dev, caddr_t data, int flag);
1177 static int sr_play_trkind(dev_t dev, caddr_t data, int flag);
1178 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag);
1179 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag);
1180 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag);
1181 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag);
1182 static int sr_read_cdda(dev_t dev, caddr_t data, int flag);
1183 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag);
1184 static int sr_read_mode1(dev_t dev, caddr_t data, int flag);
1185 static int sr_read_mode2(dev_t dev, caddr_t data, int flag);
1186 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag);
1187 static int sr_sector_mode(dev_t dev, uint32_t blksize);
1188 static int sr_eject(dev_t dev);
1189 static void sr_ejected(register struct sd_lun *un);
1190 static int sr_check_wp(dev_t dev);
1191 static opaque_t sd_watch_request_submit(struct sd_lun *un);
1192 static int sd_check_media(dev_t dev, enum dkio_state state);
1193 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1194 static void sd_delayed_cv_broadcast(void *arg);
1195 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag);
1196 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag);
1197 
1198 #ifdef notyet
1199 static int sd_log_page_supported(sd_ssc_t *ssc, int log_page);
1200 #endif
1201 
1202 /*
1203  * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions.
1204  */
1205 static void sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag);
1206 static int sd_wm_cache_constructor(void *wm, void *un, int flags);
1207 static void sd_wm_cache_destructor(void *wm, void *un);
1208 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb,
1209     daddr_t endb, ushort_t typ);
1210 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb,
1211     daddr_t endb);
1212 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp);
1213 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm);
1214 static void sd_read_modify_write_task(void * arg);
1215 static int
1216 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
1217     struct buf **bpp);
1218 
1219 
1220 /*
1221  * Function prototypes for failfast support.
1222  */
1223 static void sd_failfast_flushq(struct sd_lun *un, boolean_t flush_all);
1224 static int sd_failfast_flushq_callback(struct buf *bp);
1225 
1226 /*
1227  * Function prototypes to check for lsi devices
1228  */
1229 static void sd_is_lsi(struct sd_lun *un);
1230 
1231 /*
1232  * Function prototypes for partial DMA support
1233  */
1234 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
1235                 struct scsi_pkt *pkt, struct sd_xbuf *xp);
1236 
1237 
1238 /* Function prototypes for cmlb */
1239 static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
1240     diskaddr_t start_block, size_t reqlength, void *tg_cookie);
1241 
1242 static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie);
1243 
1244 /*
1245  * For printing RMW warning message timely
1246  */
1247 static void sd_rmw_msg_print_handler(void *arg);
1248 
1249 /*
1250  * Constants for failfast support:
1251  *
1252  * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO
1253  * failfast processing being performed.
1254  *
1255  * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing
1256  * failfast processing on all bufs with B_FAILFAST set.
1257  */
1258 
1259 #define SD_FAILFAST_INACTIVE            0
1260 #define SD_FAILFAST_ACTIVE              1
1261 
1262 /*
1263  * Bitmask to control behaviour in failfast active state:
1264  *
1265  * SD_FAILFAST_ENABLE_FORCE_INACTIVE: When set, allow retries without
1266  * SD_RETRIES_FAILFAST to cause transition to failfast inactive state.
1267  *
1268  * SD_FAILFAST_ENABLE_FAIL_RETRIES: When set, cause retries with the flag
1269  * SD_RETRIES_FAILFAST set (following a timeout) to fail when in failfast
1270  * active state.
1271  *
1272  * SD_FAILFAST_ENABLE_FAIL_ALL_RETRIES: When set, cause ALL retries,
1273  * regardless of reason, to fail when in failfast active state. This takes
1274  * precedence over SD_FAILFAST_FAIL_RETRIES.
1275  *
1276  * SD_FAILFAST_ENABLE_FAIL_USCSI: When set, discard all commands in the USCSI
1277  * chain (sdioctl or driver generated) when in failfast active state.
1278  * To prevent problems with sdopen, this is limited to when there are
1279  * multiple pending commands.
1280  */
1281 
1282 #define SD_FAILFAST_ENABLE_FORCE_INACTIVE       0x01
1283 #define SD_FAILFAST_ENABLE_FAIL_RETRIES         0x02
1284 #define SD_FAILFAST_ENABLE_FAIL_ALL_RETRIES     0x04
1285 #define SD_FAILFAST_ENABLE_FAIL_USCSI           0x08
1286 
1287 /*
1288  * The default behaviour is to fail all retries due to timeout when in failfast
1289  * active state, and not allow other retries to transition to inactive.
1290  */
1291 static int sd_failfast_enable = SD_FAILFAST_ENABLE_FAIL_RETRIES |
1292     SD_FAILFAST_ENABLE_FAIL_USCSI;
1293 
1294 /*
1295  * Bitmask to control behavior of buf(9S) flushes when a transition to
1296  * the failfast state occurs. Optional bits include:
1297  *
1298  * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that
1299  * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will
1300  * be flushed.
1301  *
1302  * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the
1303  * driver, in addition to the regular wait queue. This includes the xbuf
1304  * queues. When clear, only the driver's wait queue will be flushed.
1305  */
1306 #define SD_FAILFAST_FLUSH_ALL_BUFS      0x01
1307 #define SD_FAILFAST_FLUSH_ALL_QUEUES    0x02
1308 
1309 /*
1310  * The default behavior is to flush all bufs in all queues within the driver.
1311  */
1312 static int sd_failfast_flushctl =
1313     SD_FAILFAST_FLUSH_ALL_BUFS | SD_FAILFAST_FLUSH_ALL_QUEUES;
1314 
1315 #ifdef SD_FAULT_INJECTION
1316 static uint_t   sd_fault_injection_on = 0;
1317 #endif
1318 
1319 /*
1320  * SD Testing Fault Injection
1321  */
1322 #ifdef SD_FAULT_INJECTION
1323 static int sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un);
1324 static void sd_faultinjection(struct scsi_pkt *pktp);
1325 static void sd_prefaultinjection(struct scsi_pkt *pktp);
1326 static void sd_injection_log(char *buf, struct sd_lun *un);
1327 #endif
1328 
1329 /*
1330  * Device driver ops vector
1331  */
1332 static struct cb_ops sd_cb_ops = {
1333         sdopen,                 /* open */
1334         sdclose,                /* close */
1335         sdstrategy,             /* strategy */
1336         nodev,                  /* print */
1337         sddump,                 /* dump */
1338         sdread,                 /* read */
1339         sdwrite,                /* write */
1340         sdioctl,                /* ioctl */
1341         nodev,                  /* devmap */
1342         nodev,                  /* mmap */
1343         nodev,                  /* segmap */
1344         nochpoll,               /* poll */
1345         sd_prop_op,             /* cb_prop_op */
1346         0,                      /* streamtab  */
1347         D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */
1348         CB_REV,                 /* cb_rev */
1349         sdaread,                /* async I/O read entry point */
1350         sdawrite                /* async I/O write entry point */
1351 };
1352 
1353 struct dev_ops sd_ops = {
1354         DEVO_REV,               /* devo_rev, */
1355         0,                      /* refcnt  */
1356         sdinfo,                 /* info */
1357         nulldev,                /* identify */
1358         sdprobe,                /* probe */
1359         sdattach,               /* attach */
1360         sddetach,               /* detach */
1361         nodev,                  /* reset */
1362         &sd_cb_ops,         /* driver operations */
1363         NULL,                   /* bus operations */
1364         sdpower,                /* power */
1365         ddi_quiesce_not_needed,         /* quiesce */
1366 };
1367 
1368 /*
1369  * This is the loadable module wrapper.
1370  */
1371 #include <sys/modctl.h>
1372 
1373 static struct modldrv modldrv = {
1374         &mod_driverops,             /* Type of module. This one is a driver */
1375         SD_MODULE_NAME,         /* Module name. */
1376         &sd_ops                     /* driver ops */
1377 };
1378 
1379 static struct modlinkage modlinkage = {
1380         MODREV_1, &modldrv, NULL
1381 };
1382 
1383 static cmlb_tg_ops_t sd_tgops = {
1384         TG_DK_OPS_VERSION_1,
1385         sd_tg_rdwr,
1386         sd_tg_getinfo
1387 };
1388 
1389 static struct scsi_asq_key_strings sd_additional_codes[] = {
1390         0x81, 0, "Logical Unit is Reserved",
1391         0x85, 0, "Audio Address Not Valid",
1392         0xb6, 0, "Media Load Mechanism Failed",
1393         0xB9, 0, "Audio Play Operation Aborted",
1394         0xbf, 0, "Buffer Overflow for Read All Subcodes Command",
1395         0x53, 2, "Medium removal prevented",
1396         0x6f, 0, "Authentication failed during key exchange",
1397         0x6f, 1, "Key not present",
1398         0x6f, 2, "Key not established",
1399         0x6f, 3, "Read without proper authentication",
1400         0x6f, 4, "Mismatched region to this logical unit",
1401         0x6f, 5, "Region reset count error",
1402         0xffff, 0x0, NULL
1403 };
1404 
1405 
1406 /*
1407  * Struct for passing printing information for sense data messages
1408  */
1409 struct sd_sense_info {
1410         int     ssi_severity;
1411         int     ssi_pfa_flag;
1412 };
1413 
1414 /*
1415  * Table of function pointers for iostart-side routines. Separate "chains"
1416  * of layered function calls are formed by placing the function pointers
1417  * sequentially in the desired order. Functions are called according to an
1418  * incrementing table index ordering. The last function in each chain must
1419  * be sd_core_iostart(). The corresponding iodone-side routines are expected
1420  * in the sd_iodone_chain[] array.
1421  *
1422  * Note: It may seem more natural to organize both the iostart and iodone
1423  * functions together, into an array of structures (or some similar
1424  * organization) with a common index, rather than two separate arrays which
1425  * must be maintained in synchronization. The purpose of this division is
1426  * to achieve improved performance: individual arrays allows for more
1427  * effective cache line utilization on certain platforms.
1428  */
1429 
1430 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp);
1431 
1432 
1433 static sd_chain_t sd_iostart_chain[] = {
1434 
1435         /* Chain for buf IO for disk drive targets (PM enabled) */
1436         sd_mapblockaddr_iostart,        /* Index: 0 */
1437         sd_pm_iostart,                  /* Index: 1 */
1438         sd_core_iostart,                /* Index: 2 */
1439 
1440         /* Chain for buf IO for disk drive targets (PM disabled) */
1441         sd_mapblockaddr_iostart,        /* Index: 3 */
1442         sd_core_iostart,                /* Index: 4 */
1443 
1444         /*
1445          * Chain for buf IO for removable-media or large sector size
1446          * disk drive targets with RMW needed (PM enabled)
1447          */
1448         sd_mapblockaddr_iostart,        /* Index: 5 */
1449         sd_mapblocksize_iostart,        /* Index: 6 */
1450         sd_pm_iostart,                  /* Index: 7 */
1451         sd_core_iostart,                /* Index: 8 */
1452 
1453         /*
1454          * Chain for buf IO for removable-media or large sector size
1455          * disk drive targets with RMW needed (PM disabled)
1456          */
1457         sd_mapblockaddr_iostart,        /* Index: 9 */
1458         sd_mapblocksize_iostart,        /* Index: 10 */
1459         sd_core_iostart,                /* Index: 11 */
1460 
1461         /* Chain for buf IO for disk drives with checksumming (PM enabled) */
1462         sd_mapblockaddr_iostart,        /* Index: 12 */
1463         sd_checksum_iostart,            /* Index: 13 */
1464         sd_pm_iostart,                  /* Index: 14 */
1465         sd_core_iostart,                /* Index: 15 */
1466 
1467         /* Chain for buf IO for disk drives with checksumming (PM disabled) */
1468         sd_mapblockaddr_iostart,        /* Index: 16 */
1469         sd_checksum_iostart,            /* Index: 17 */
1470         sd_core_iostart,                /* Index: 18 */
1471 
1472         /* Chain for USCSI commands (all targets) */
1473         sd_pm_iostart,                  /* Index: 19 */
1474         sd_core_iostart,                /* Index: 20 */
1475 
1476         /* Chain for checksumming USCSI commands (all targets) */
1477         sd_checksum_uscsi_iostart,      /* Index: 21 */
1478         sd_pm_iostart,                  /* Index: 22 */
1479         sd_core_iostart,                /* Index: 23 */
1480 
1481         /* Chain for "direct" USCSI commands (all targets) */
1482         sd_core_iostart,                /* Index: 24 */
1483 
1484         /* Chain for "direct priority" USCSI commands (all targets) */
1485         sd_core_iostart,                /* Index: 25 */
1486 
1487         /*
1488          * Chain for buf IO for large sector size disk drive targets
1489          * with RMW needed with checksumming (PM enabled)
1490          */
1491         sd_mapblockaddr_iostart,        /* Index: 26 */
1492         sd_mapblocksize_iostart,        /* Index: 27 */
1493         sd_checksum_iostart,            /* Index: 28 */
1494         sd_pm_iostart,                  /* Index: 29 */
1495         sd_core_iostart,                /* Index: 30 */
1496 
1497         /*
1498          * Chain for buf IO for large sector size disk drive targets
1499          * with RMW needed with checksumming (PM disabled)
1500          */
1501         sd_mapblockaddr_iostart,        /* Index: 31 */
1502         sd_mapblocksize_iostart,        /* Index: 32 */
1503         sd_checksum_iostart,            /* Index: 33 */
1504         sd_core_iostart,                /* Index: 34 */
1505 
1506 };
1507 
1508 /*
1509  * Macros to locate the first function of each iostart chain in the
1510  * sd_iostart_chain[] array. These are located by the index in the array.
1511  */
1512 #define SD_CHAIN_DISK_IOSTART                   0
1513 #define SD_CHAIN_DISK_IOSTART_NO_PM             3
1514 #define SD_CHAIN_MSS_DISK_IOSTART               5
1515 #define SD_CHAIN_RMMEDIA_IOSTART                5
1516 #define SD_CHAIN_MSS_DISK_IOSTART_NO_PM         9
1517 #define SD_CHAIN_RMMEDIA_IOSTART_NO_PM          9
1518 #define SD_CHAIN_CHKSUM_IOSTART                 12
1519 #define SD_CHAIN_CHKSUM_IOSTART_NO_PM           16
1520 #define SD_CHAIN_USCSI_CMD_IOSTART              19
1521 #define SD_CHAIN_USCSI_CHKSUM_IOSTART           21
1522 #define SD_CHAIN_DIRECT_CMD_IOSTART             24
1523 #define SD_CHAIN_PRIORITY_CMD_IOSTART           25
1524 #define SD_CHAIN_MSS_CHKSUM_IOSTART             26
1525 #define SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM       31
1526 
1527 
1528 /*
1529  * Table of function pointers for the iodone-side routines for the driver-
1530  * internal layering mechanism.  The calling sequence for iodone routines
1531  * uses a decrementing table index, so the last routine called in a chain
1532  * must be at the lowest array index location for that chain.  The last
1533  * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs)
1534  * or sd_uscsi_iodone() (for uscsi IOs).  Other than this, the ordering
1535  * of the functions in an iodone side chain must correspond to the ordering
1536  * of the iostart routines for that chain.  Note that there is no iodone
1537  * side routine that corresponds to sd_core_iostart(), so there is no
1538  * entry in the table for this.
1539  */
1540 
1541 static sd_chain_t sd_iodone_chain[] = {
1542 
1543         /* Chain for buf IO for disk drive targets (PM enabled) */
1544         sd_buf_iodone,                  /* Index: 0 */
1545         sd_mapblockaddr_iodone,         /* Index: 1 */
1546         sd_pm_iodone,                   /* Index: 2 */
1547 
1548         /* Chain for buf IO for disk drive targets (PM disabled) */
1549         sd_buf_iodone,                  /* Index: 3 */
1550         sd_mapblockaddr_iodone,         /* Index: 4 */
1551 
1552         /*
1553          * Chain for buf IO for removable-media or large sector size
1554          * disk drive targets with RMW needed (PM enabled)
1555          */
1556         sd_buf_iodone,                  /* Index: 5 */
1557         sd_mapblockaddr_iodone,         /* Index: 6 */
1558         sd_mapblocksize_iodone,         /* Index: 7 */
1559         sd_pm_iodone,                   /* Index: 8 */
1560 
1561         /*
1562          * Chain for buf IO for removable-media or large sector size
1563          * disk drive targets with RMW needed (PM disabled)
1564          */
1565         sd_buf_iodone,                  /* Index: 9 */
1566         sd_mapblockaddr_iodone,         /* Index: 10 */
1567         sd_mapblocksize_iodone,         /* Index: 11 */
1568 
1569         /* Chain for buf IO for disk drives with checksumming (PM enabled) */
1570         sd_buf_iodone,                  /* Index: 12 */
1571         sd_mapblockaddr_iodone,         /* Index: 13 */
1572         sd_checksum_iodone,             /* Index: 14 */
1573         sd_pm_iodone,                   /* Index: 15 */
1574 
1575         /* Chain for buf IO for disk drives with checksumming (PM disabled) */
1576         sd_buf_iodone,                  /* Index: 16 */
1577         sd_mapblockaddr_iodone,         /* Index: 17 */
1578         sd_checksum_iodone,             /* Index: 18 */
1579 
1580         /* Chain for USCSI commands (non-checksum targets) */
1581         sd_uscsi_iodone,                /* Index: 19 */
1582         sd_pm_iodone,                   /* Index: 20 */
1583 
1584         /* Chain for USCSI commands (checksum targets) */
1585         sd_uscsi_iodone,                /* Index: 21 */
1586         sd_checksum_uscsi_iodone,       /* Index: 22 */
1587         sd_pm_iodone,                   /* Index: 22 */
1588 
1589         /* Chain for "direct" USCSI commands (all targets) */
1590         sd_uscsi_iodone,                /* Index: 24 */
1591 
1592         /* Chain for "direct priority" USCSI commands (all targets) */
1593         sd_uscsi_iodone,                /* Index: 25 */
1594 
1595         /*
1596          * Chain for buf IO for large sector size disk drive targets
1597          * with checksumming (PM enabled)
1598          */
1599         sd_buf_iodone,                  /* Index: 26 */
1600         sd_mapblockaddr_iodone,         /* Index: 27 */
1601         sd_mapblocksize_iodone,         /* Index: 28 */
1602         sd_checksum_iodone,             /* Index: 29 */
1603         sd_pm_iodone,                   /* Index: 30 */
1604 
1605         /*
1606          * Chain for buf IO for large sector size disk drive targets
1607          * with checksumming (PM disabled)
1608          */
1609         sd_buf_iodone,                  /* Index: 31 */
1610         sd_mapblockaddr_iodone,         /* Index: 32 */
1611         sd_mapblocksize_iodone,         /* Index: 33 */
1612         sd_checksum_iodone,             /* Index: 34 */
1613 };
1614 
1615 
1616 /*
1617  * Macros to locate the "first" function in the sd_iodone_chain[] array for
1618  * each iodone-side chain. These are located by the array index, but as the
1619  * iodone side functions are called in a decrementing-index order, the
1620  * highest index number in each chain must be specified (as these correspond
1621  * to the first function in the iodone chain that will be called by the core
1622  * at IO completion time).
1623  */
1624 
1625 #define SD_CHAIN_DISK_IODONE                    2
1626 #define SD_CHAIN_DISK_IODONE_NO_PM              4
1627 #define SD_CHAIN_RMMEDIA_IODONE                 8
1628 #define SD_CHAIN_MSS_DISK_IODONE                8
1629 #define SD_CHAIN_RMMEDIA_IODONE_NO_PM           11
1630 #define SD_CHAIN_MSS_DISK_IODONE_NO_PM          11
1631 #define SD_CHAIN_CHKSUM_IODONE                  15
1632 #define SD_CHAIN_CHKSUM_IODONE_NO_PM            18
1633 #define SD_CHAIN_USCSI_CMD_IODONE               20
1634 #define SD_CHAIN_USCSI_CHKSUM_IODONE            22
1635 #define SD_CHAIN_DIRECT_CMD_IODONE              24
1636 #define SD_CHAIN_PRIORITY_CMD_IODONE            25
1637 #define SD_CHAIN_MSS_CHKSUM_IODONE              30
1638 #define SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM        34
1639 
1640 
1641 
1642 /*
1643  * Array to map a layering chain index to the appropriate initpkt routine.
1644  * The redundant entries are present so that the index used for accessing
1645  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
1646  * with this table as well.
1647  */
1648 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **);
1649 
1650 static sd_initpkt_t     sd_initpkt_map[] = {
1651 
1652         /* Chain for buf IO for disk drive targets (PM enabled) */
1653         sd_initpkt_for_buf,             /* Index: 0 */
1654         sd_initpkt_for_buf,             /* Index: 1 */
1655         sd_initpkt_for_buf,             /* Index: 2 */
1656 
1657         /* Chain for buf IO for disk drive targets (PM disabled) */
1658         sd_initpkt_for_buf,             /* Index: 3 */
1659         sd_initpkt_for_buf,             /* Index: 4 */
1660 
1661         /*
1662          * Chain for buf IO for removable-media or large sector size
1663          * disk drive targets (PM enabled)
1664          */
1665         sd_initpkt_for_buf,             /* Index: 5 */
1666         sd_initpkt_for_buf,             /* Index: 6 */
1667         sd_initpkt_for_buf,             /* Index: 7 */
1668         sd_initpkt_for_buf,             /* Index: 8 */
1669 
1670         /*
1671          * Chain for buf IO for removable-media or large sector size
1672          * disk drive targets (PM disabled)
1673          */
1674         sd_initpkt_for_buf,             /* Index: 9 */
1675         sd_initpkt_for_buf,             /* Index: 10 */
1676         sd_initpkt_for_buf,             /* Index: 11 */
1677 
1678         /* Chain for buf IO for disk drives with checksumming (PM enabled) */
1679         sd_initpkt_for_buf,             /* Index: 12 */
1680         sd_initpkt_for_buf,             /* Index: 13 */
1681         sd_initpkt_for_buf,             /* Index: 14 */
1682         sd_initpkt_for_buf,             /* Index: 15 */
1683 
1684         /* Chain for buf IO for disk drives with checksumming (PM disabled) */
1685         sd_initpkt_for_buf,             /* Index: 16 */
1686         sd_initpkt_for_buf,             /* Index: 17 */
1687         sd_initpkt_for_buf,             /* Index: 18 */
1688 
1689         /* Chain for USCSI commands (non-checksum targets) */
1690         sd_initpkt_for_uscsi,           /* Index: 19 */
1691         sd_initpkt_for_uscsi,           /* Index: 20 */
1692 
1693         /* Chain for USCSI commands (checksum targets) */
1694         sd_initpkt_for_uscsi,           /* Index: 21 */
1695         sd_initpkt_for_uscsi,           /* Index: 22 */
1696         sd_initpkt_for_uscsi,           /* Index: 22 */
1697 
1698         /* Chain for "direct" USCSI commands (all targets) */
1699         sd_initpkt_for_uscsi,           /* Index: 24 */
1700 
1701         /* Chain for "direct priority" USCSI commands (all targets) */
1702         sd_initpkt_for_uscsi,           /* Index: 25 */
1703 
1704         /*
1705          * Chain for buf IO for large sector size disk drive targets
1706          * with checksumming (PM enabled)
1707          */
1708         sd_initpkt_for_buf,             /* Index: 26 */
1709         sd_initpkt_for_buf,             /* Index: 27 */
1710         sd_initpkt_for_buf,             /* Index: 28 */
1711         sd_initpkt_for_buf,             /* Index: 29 */
1712         sd_initpkt_for_buf,             /* Index: 30 */
1713 
1714         /*
1715          * Chain for buf IO for large sector size disk drive targets
1716          * with checksumming (PM disabled)
1717          */
1718         sd_initpkt_for_buf,             /* Index: 31 */
1719         sd_initpkt_for_buf,             /* Index: 32 */
1720         sd_initpkt_for_buf,             /* Index: 33 */
1721         sd_initpkt_for_buf,             /* Index: 34 */
1722 };
1723 
1724 
1725 /*
1726  * Array to map a layering chain index to the appropriate destroypktpkt routine.
1727  * The redundant entries are present so that the index used for accessing
1728  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
1729  * with this table as well.
1730  */
1731 typedef void (*sd_destroypkt_t)(struct buf *);
1732 
1733 static sd_destroypkt_t  sd_destroypkt_map[] = {
1734 
1735         /* Chain for buf IO for disk drive targets (PM enabled) */
1736         sd_destroypkt_for_buf,          /* Index: 0 */
1737         sd_destroypkt_for_buf,          /* Index: 1 */
1738         sd_destroypkt_for_buf,          /* Index: 2 */
1739 
1740         /* Chain for buf IO for disk drive targets (PM disabled) */
1741         sd_destroypkt_for_buf,          /* Index: 3 */
1742         sd_destroypkt_for_buf,          /* Index: 4 */
1743 
1744         /*
1745          * Chain for buf IO for removable-media or large sector size
1746          * disk drive targets (PM enabled)
1747          */
1748         sd_destroypkt_for_buf,          /* Index: 5 */
1749         sd_destroypkt_for_buf,          /* Index: 6 */
1750         sd_destroypkt_for_buf,          /* Index: 7 */
1751         sd_destroypkt_for_buf,          /* Index: 8 */
1752 
1753         /*
1754          * Chain for buf IO for removable-media or large sector size
1755          * disk drive targets (PM disabled)
1756          */
1757         sd_destroypkt_for_buf,          /* Index: 9 */
1758         sd_destroypkt_for_buf,          /* Index: 10 */
1759         sd_destroypkt_for_buf,          /* Index: 11 */
1760 
1761         /* Chain for buf IO for disk drives with checksumming (PM enabled) */
1762         sd_destroypkt_for_buf,          /* Index: 12 */
1763         sd_destroypkt_for_buf,          /* Index: 13 */
1764         sd_destroypkt_for_buf,          /* Index: 14 */
1765         sd_destroypkt_for_buf,          /* Index: 15 */
1766 
1767         /* Chain for buf IO for disk drives with checksumming (PM disabled) */
1768         sd_destroypkt_for_buf,          /* Index: 16 */
1769         sd_destroypkt_for_buf,          /* Index: 17 */
1770         sd_destroypkt_for_buf,          /* Index: 18 */
1771 
1772         /* Chain for USCSI commands (non-checksum targets) */
1773         sd_destroypkt_for_uscsi,        /* Index: 19 */
1774         sd_destroypkt_for_uscsi,        /* Index: 20 */
1775 
1776         /* Chain for USCSI commands (checksum targets) */
1777         sd_destroypkt_for_uscsi,        /* Index: 21 */
1778         sd_destroypkt_for_uscsi,        /* Index: 22 */
1779         sd_destroypkt_for_uscsi,        /* Index: 22 */
1780 
1781         /* Chain for "direct" USCSI commands (all targets) */
1782         sd_destroypkt_for_uscsi,        /* Index: 24 */
1783 
1784         /* Chain for "direct priority" USCSI commands (all targets) */
1785         sd_destroypkt_for_uscsi,        /* Index: 25 */
1786 
1787         /*
1788          * Chain for buf IO for large sector size disk drive targets
1789          * with checksumming (PM disabled)
1790          */
1791         sd_destroypkt_for_buf,          /* Index: 26 */
1792         sd_destroypkt_for_buf,          /* Index: 27 */
1793         sd_destroypkt_for_buf,          /* Index: 28 */
1794         sd_destroypkt_for_buf,          /* Index: 29 */
1795         sd_destroypkt_for_buf,          /* Index: 30 */
1796 
1797         /*
1798          * Chain for buf IO for large sector size disk drive targets
1799          * with checksumming (PM enabled)
1800          */
1801         sd_destroypkt_for_buf,          /* Index: 31 */
1802         sd_destroypkt_for_buf,          /* Index: 32 */
1803         sd_destroypkt_for_buf,          /* Index: 33 */
1804         sd_destroypkt_for_buf,          /* Index: 34 */
1805 };
1806 
1807 
1808 
1809 /*
1810  * Array to map a layering chain index to the appropriate chain "type".
1811  * The chain type indicates a specific property/usage of the chain.
1812  * The redundant entries are present so that the index used for accessing
1813  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
1814  * with this table as well.
1815  */
1816 
1817 #define SD_CHAIN_NULL                   0       /* for the special RQS cmd */
1818 #define SD_CHAIN_BUFIO                  1       /* regular buf IO */
1819 #define SD_CHAIN_USCSI                  2       /* regular USCSI commands */
1820 #define SD_CHAIN_DIRECT                 3       /* uscsi, w/ bypass power mgt */
1821 #define SD_CHAIN_DIRECT_PRIORITY        4       /* uscsi, w/ bypass power mgt */
1822                                                 /* (for error recovery) */
1823 
1824 static int sd_chain_type_map[] = {
1825 
1826         /* Chain for buf IO for disk drive targets (PM enabled) */
1827         SD_CHAIN_BUFIO,                 /* Index: 0 */
1828         SD_CHAIN_BUFIO,                 /* Index: 1 */
1829         SD_CHAIN_BUFIO,                 /* Index: 2 */
1830 
1831         /* Chain for buf IO for disk drive targets (PM disabled) */
1832         SD_CHAIN_BUFIO,                 /* Index: 3 */
1833         SD_CHAIN_BUFIO,                 /* Index: 4 */
1834 
1835         /*
1836          * Chain for buf IO for removable-media or large sector size
1837          * disk drive targets (PM enabled)
1838          */
1839         SD_CHAIN_BUFIO,                 /* Index: 5 */
1840         SD_CHAIN_BUFIO,                 /* Index: 6 */
1841         SD_CHAIN_BUFIO,                 /* Index: 7 */
1842         SD_CHAIN_BUFIO,                 /* Index: 8 */
1843 
1844         /*
1845          * Chain for buf IO for removable-media or large sector size
1846          * disk drive targets (PM disabled)
1847          */
1848         SD_CHAIN_BUFIO,                 /* Index: 9 */
1849         SD_CHAIN_BUFIO,                 /* Index: 10 */
1850         SD_CHAIN_BUFIO,                 /* Index: 11 */
1851 
1852         /* Chain for buf IO for disk drives with checksumming (PM enabled) */
1853         SD_CHAIN_BUFIO,                 /* Index: 12 */
1854         SD_CHAIN_BUFIO,                 /* Index: 13 */
1855         SD_CHAIN_BUFIO,                 /* Index: 14 */
1856         SD_CHAIN_BUFIO,                 /* Index: 15 */
1857 
1858         /* Chain for buf IO for disk drives with checksumming (PM disabled) */
1859         SD_CHAIN_BUFIO,                 /* Index: 16 */
1860         SD_CHAIN_BUFIO,                 /* Index: 17 */
1861         SD_CHAIN_BUFIO,                 /* Index: 18 */
1862 
1863         /* Chain for USCSI commands (non-checksum targets) */
1864         SD_CHAIN_USCSI,                 /* Index: 19 */
1865         SD_CHAIN_USCSI,                 /* Index: 20 */
1866 
1867         /* Chain for USCSI commands (checksum targets) */
1868         SD_CHAIN_USCSI,                 /* Index: 21 */
1869         SD_CHAIN_USCSI,                 /* Index: 22 */
1870         SD_CHAIN_USCSI,                 /* Index: 23 */
1871 
1872         /* Chain for "direct" USCSI commands (all targets) */
1873         SD_CHAIN_DIRECT,                /* Index: 24 */
1874 
1875         /* Chain for "direct priority" USCSI commands (all targets) */
1876         SD_CHAIN_DIRECT_PRIORITY,       /* Index: 25 */
1877 
1878         /*
1879          * Chain for buf IO for large sector size disk drive targets
1880          * with checksumming (PM enabled)
1881          */
1882         SD_CHAIN_BUFIO,                 /* Index: 26 */
1883         SD_CHAIN_BUFIO,                 /* Index: 27 */
1884         SD_CHAIN_BUFIO,                 /* Index: 28 */
1885         SD_CHAIN_BUFIO,                 /* Index: 29 */
1886         SD_CHAIN_BUFIO,                 /* Index: 30 */
1887 
1888         /*
1889          * Chain for buf IO for large sector size disk drive targets
1890          * with checksumming (PM disabled)
1891          */
1892         SD_CHAIN_BUFIO,                 /* Index: 31 */
1893         SD_CHAIN_BUFIO,                 /* Index: 32 */
1894         SD_CHAIN_BUFIO,                 /* Index: 33 */
1895         SD_CHAIN_BUFIO,                 /* Index: 34 */
1896 };
1897 
1898 
1899 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */
1900 #define SD_IS_BUFIO(xp)                 \
1901         (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO)
1902 
1903 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */
1904 #define SD_IS_DIRECT_PRIORITY(xp)       \
1905         (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY)
1906 
1907 
1908 
1909 /*
1910  * Struct, array, and macros to map a specific chain to the appropriate
1911  * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays.
1912  *
1913  * The sd_chain_index_map[] array is used at attach time to set the various
1914  * un_xxx_chain type members of the sd_lun softstate to the specific layering
1915  * chain to be used with the instance. This allows different instances to use
1916  * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart
1917  * and xb_chain_iodone index values in the sd_xbuf are initialized to these
1918  * values at sd_xbuf init time, this allows (1) layering chains may be changed
1919  * dynamically & without the use of locking; and (2) a layer may update the
1920  * xb_chain_io[start|done] member in a given xbuf with its current index value,
1921  * to allow for deferred processing of an IO within the same chain from a
1922  * different execution context.
1923  */
1924 
1925 struct sd_chain_index {
1926         int     sci_iostart_index;
1927         int     sci_iodone_index;
1928 };
1929 
1930 static struct sd_chain_index    sd_chain_index_map[] = {
1931         { SD_CHAIN_DISK_IOSTART,                SD_CHAIN_DISK_IODONE },
1932         { SD_CHAIN_DISK_IOSTART_NO_PM,          SD_CHAIN_DISK_IODONE_NO_PM },
1933         { SD_CHAIN_RMMEDIA_IOSTART,             SD_CHAIN_RMMEDIA_IODONE },
1934         { SD_CHAIN_RMMEDIA_IOSTART_NO_PM,       SD_CHAIN_RMMEDIA_IODONE_NO_PM },
1935         { SD_CHAIN_CHKSUM_IOSTART,              SD_CHAIN_CHKSUM_IODONE },
1936         { SD_CHAIN_CHKSUM_IOSTART_NO_PM,        SD_CHAIN_CHKSUM_IODONE_NO_PM },
1937         { SD_CHAIN_USCSI_CMD_IOSTART,           SD_CHAIN_USCSI_CMD_IODONE },
1938         { SD_CHAIN_USCSI_CHKSUM_IOSTART,        SD_CHAIN_USCSI_CHKSUM_IODONE },
1939         { SD_CHAIN_DIRECT_CMD_IOSTART,          SD_CHAIN_DIRECT_CMD_IODONE },
1940         { SD_CHAIN_PRIORITY_CMD_IOSTART,        SD_CHAIN_PRIORITY_CMD_IODONE },
1941         { SD_CHAIN_MSS_CHKSUM_IOSTART,          SD_CHAIN_MSS_CHKSUM_IODONE },
1942         { SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM, SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM },
1943 
1944 };
1945 
1946 
1947 /*
1948  * The following are indexes into the sd_chain_index_map[] array.
1949  */
1950 
1951 /* un->un_buf_chain_type must be set to one of these */
1952 #define SD_CHAIN_INFO_DISK              0
1953 #define SD_CHAIN_INFO_DISK_NO_PM        1
1954 #define SD_CHAIN_INFO_RMMEDIA           2
1955 #define SD_CHAIN_INFO_MSS_DISK          2
1956 #define SD_CHAIN_INFO_RMMEDIA_NO_PM     3
1957 #define SD_CHAIN_INFO_MSS_DSK_NO_PM     3
1958 #define SD_CHAIN_INFO_CHKSUM            4
1959 #define SD_CHAIN_INFO_CHKSUM_NO_PM      5
1960 #define SD_CHAIN_INFO_MSS_DISK_CHKSUM   10
1961 #define SD_CHAIN_INFO_MSS_DISK_CHKSUM_NO_PM     11
1962 
1963 /* un->un_uscsi_chain_type must be set to one of these */
1964 #define SD_CHAIN_INFO_USCSI_CMD         6
1965 /* USCSI with PM disabled is the same as DIRECT */
1966 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM   8
1967 #define SD_CHAIN_INFO_USCSI_CHKSUM      7
1968 
1969 /* un->un_direct_chain_type must be set to one of these */
1970 #define SD_CHAIN_INFO_DIRECT_CMD        8
1971 
1972 /* un->un_priority_chain_type must be set to one of these */
1973 #define SD_CHAIN_INFO_PRIORITY_CMD      9
1974 
1975 /* size for devid inquiries */
1976 #define MAX_INQUIRY_SIZE                0xF0
1977 
1978 /*
1979  * Macros used by functions to pass a given buf(9S) struct along to the
1980  * next function in the layering chain for further processing.
1981  */
1982 #define SD_BEGIN_IOSTART(index, un, bp) \
1983         ((*(sd_iostart_chain[index]))(index, un, bp))
1984 
1985 #define SD_BEGIN_IODONE(index, un, bp)  \
1986         ((*(sd_iodone_chain[index]))(index, un, bp))
1987 
1988 #define SD_NEXT_IOSTART(index, un, bp)                          \
1989         ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp))
1990 
1991 #define SD_NEXT_IODONE(index, un, bp)                           \
1992         ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp))
1993 
1994 /*
1995  *    Function: _init
1996  *
1997  * Description: This is the driver _init(9E) entry point.
1998  *
1999  * Return Code: Returns the value from mod_install(9F) or
2000  *              ddi_soft_state_init(9F) as appropriate.
2001  *
2002  *     Context: Called when driver module loaded.
2003  */
2004 
2005 int
2006 _init(void)
2007 {
2008         int     err;
2009 
2010         /* establish driver name from module name */
2011         sd_label = (char *)mod_modname(&modlinkage);
2012 
2013         err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun),
2014             SD_MAXUNIT);
2015         if (err != 0) {
2016                 return (err);
2017         }
2018 
2019         mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL);
2020         mutex_init(&sd_log_mutex,    NULL, MUTEX_DRIVER, NULL);
2021         mutex_init(&sd_label_mutex,  NULL, MUTEX_DRIVER, NULL);
2022 
2023         mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL);
2024         cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL);
2025         cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL);
2026 
2027         /*
2028          * it's ok to init here even for fibre device
2029          */
2030         sd_scsi_probe_cache_init();
2031 
2032         sd_scsi_target_lun_init();
2033 
2034         /*
2035          * Creating taskq before mod_install ensures that all callers (threads)
2036          * that enter the module after a successful mod_install encounter
2037          * a valid taskq.
2038          */
2039         sd_taskq_create();
2040 
2041         err = mod_install(&modlinkage);
2042         if (err != 0) {
2043                 /* delete taskq if install fails */
2044                 sd_taskq_delete();
2045 
2046                 mutex_destroy(&sd_detach_mutex);
2047                 mutex_destroy(&sd_log_mutex);
2048                 mutex_destroy(&sd_label_mutex);
2049 
2050                 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2051                 cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2052                 cv_destroy(&sd_tr.srq_inprocess_cv);
2053 
2054                 sd_scsi_probe_cache_fini();
2055 
2056                 sd_scsi_target_lun_fini();
2057 
2058                 ddi_soft_state_fini(&sd_state);
2059 
2060                 return (err);
2061         }
2062 
2063         return (err);
2064 }
2065 
2066 
2067 /*
2068  *    Function: _fini
2069  *
2070  * Description: This is the driver _fini(9E) entry point.
2071  *
2072  * Return Code: Returns the value from mod_remove(9F)
2073  *
2074  *     Context: Called when driver module is unloaded.
2075  */
2076 
2077 int
2078 _fini(void)
2079 {
2080         int err;
2081 
2082         if ((err = mod_remove(&modlinkage)) != 0) {
2083                 return (err);
2084         }
2085 
2086         sd_taskq_delete();
2087 
2088         mutex_destroy(&sd_detach_mutex);
2089         mutex_destroy(&sd_log_mutex);
2090         mutex_destroy(&sd_label_mutex);
2091         mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2092 
2093         sd_scsi_probe_cache_fini();
2094 
2095         sd_scsi_target_lun_fini();
2096 
2097         cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2098         cv_destroy(&sd_tr.srq_inprocess_cv);
2099 
2100         ddi_soft_state_fini(&sd_state);
2101 
2102         return (err);
2103 }
2104 
2105 
2106 /*
2107  *    Function: _info
2108  *
2109  * Description: This is the driver _info(9E) entry point.
2110  *
2111  *   Arguments: modinfop - pointer to the driver modinfo structure
2112  *
2113  * Return Code: Returns the value from mod_info(9F).
2114  *
2115  *     Context: Kernel thread context
2116  */
2117 
2118 int
2119 _info(struct modinfo *modinfop)
2120 {
2121         return (mod_info(&modlinkage, modinfop));
2122 }
2123 
2124 
2125 /*
2126  * The following routines implement the driver message logging facility.
2127  * They provide component- and level- based debug output filtering.
2128  * Output may also be restricted to messages for a single instance by
2129  * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set
2130  * to NULL, then messages for all instances are printed.
2131  *
2132  * These routines have been cloned from each other due to the language
2133  * constraints of macros and variable argument list processing.
2134  */
2135 
2136 
2137 /*
2138  *    Function: sd_log_err
2139  *
2140  * Description: This routine is called by the SD_ERROR macro for debug
2141  *              logging of error conditions.
2142  *
2143  *   Arguments: comp - driver component being logged
2144  *              dev  - pointer to driver info structure
2145  *              fmt  - error string and format to be logged
2146  */
2147 
2148 static void
2149 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...)
2150 {
2151         va_list         ap;
2152         dev_info_t      *dev;
2153 
2154         ASSERT(un != NULL);
2155         dev = SD_DEVINFO(un);
2156         ASSERT(dev != NULL);
2157 
2158         /*
2159          * Filter messages based on the global component and level masks.
2160          * Also print if un matches the value of sd_debug_un, or if
2161          * sd_debug_un is set to NULL.
2162          */
2163         if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) &&
2164             ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2165                 mutex_enter(&sd_log_mutex);
2166                 va_start(ap, fmt);
2167                 (void) vsprintf(sd_log_buf, fmt, ap);
2168                 va_end(ap);
2169                 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2170                 mutex_exit(&sd_log_mutex);
2171         }
2172 #ifdef SD_FAULT_INJECTION
2173         if (un->sd_injection_mask & comp) {
2174                 mutex_enter(&sd_log_mutex);
2175                 va_start(ap, fmt);
2176                 (void) vsprintf(sd_log_buf, fmt, ap);
2177                 va_end(ap);
2178                 sd_injection_log(sd_log_buf, un);
2179                 mutex_exit(&sd_log_mutex);
2180         }
2181 #endif
2182 }
2183 
2184 
2185 /*
2186  *    Function: sd_log_info
2187  *
2188  * Description: This routine is called by the SD_INFO macro for debug
2189  *              logging of general purpose informational conditions.
2190  *
2191  *   Arguments: comp - driver component being logged
2192  *              dev  - pointer to driver info structure
2193  *              fmt  - info string and format to be logged
2194  */
2195 
2196 static void
2197 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...)
2198 {
2199         va_list         ap;
2200         dev_info_t      *dev;
2201 
2202         ASSERT(un != NULL);
2203         dev = SD_DEVINFO(un);
2204         ASSERT(dev != NULL);
2205 
2206         /*
2207          * Filter messages based on the global component and level masks.
2208          * Also print if un matches the value of sd_debug_un, or if
2209          * sd_debug_un is set to NULL.
2210          */
2211         if ((sd_component_mask & component) &&
2212             (sd_level_mask & SD_LOGMASK_INFO) &&
2213             ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2214                 mutex_enter(&sd_log_mutex);
2215                 va_start(ap, fmt);
2216                 (void) vsprintf(sd_log_buf, fmt, ap);
2217                 va_end(ap);
2218                 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2219                 mutex_exit(&sd_log_mutex);
2220         }
2221 #ifdef SD_FAULT_INJECTION
2222         if (un->sd_injection_mask & component) {
2223                 mutex_enter(&sd_log_mutex);
2224                 va_start(ap, fmt);
2225                 (void) vsprintf(sd_log_buf, fmt, ap);
2226                 va_end(ap);
2227                 sd_injection_log(sd_log_buf, un);
2228                 mutex_exit(&sd_log_mutex);
2229         }
2230 #endif
2231 }
2232 
2233 
2234 /*
2235  *    Function: sd_log_trace
2236  *
2237  * Description: This routine is called by the SD_TRACE macro for debug
2238  *              logging of trace conditions (i.e. function entry/exit).
2239  *
2240  *   Arguments: comp - driver component being logged
2241  *              dev  - pointer to driver info structure
2242  *              fmt  - trace string and format to be logged
2243  */
2244 
2245 static void
2246 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...)
2247 {
2248         va_list         ap;
2249         dev_info_t      *dev;
2250 
2251         ASSERT(un != NULL);
2252         dev = SD_DEVINFO(un);
2253         ASSERT(dev != NULL);
2254 
2255         /*
2256          * Filter messages based on the global component and level masks.
2257          * Also print if un matches the value of sd_debug_un, or if
2258          * sd_debug_un is set to NULL.
2259          */
2260         if ((sd_component_mask & component) &&
2261             (sd_level_mask & SD_LOGMASK_TRACE) &&
2262             ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2263                 mutex_enter(&sd_log_mutex);
2264                 va_start(ap, fmt);
2265                 (void) vsprintf(sd_log_buf, fmt, ap);
2266                 va_end(ap);
2267                 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2268                 mutex_exit(&sd_log_mutex);
2269         }
2270 #ifdef SD_FAULT_INJECTION
2271         if (un->sd_injection_mask & component) {
2272                 mutex_enter(&sd_log_mutex);
2273                 va_start(ap, fmt);
2274                 (void) vsprintf(sd_log_buf, fmt, ap);
2275                 va_end(ap);
2276                 sd_injection_log(sd_log_buf, un);
2277                 mutex_exit(&sd_log_mutex);
2278         }
2279 #endif
2280 }
2281 
2282 
2283 /*
2284  *    Function: sdprobe
2285  *
2286  * Description: This is the driver probe(9e) entry point function.
2287  *
2288  *   Arguments: devi - opaque device info handle
2289  *
2290  * Return Code: DDI_PROBE_SUCCESS: If the probe was successful.
2291  *              DDI_PROBE_FAILURE: If the probe failed.
2292  *              DDI_PROBE_PARTIAL: If the instance is not present now,
2293  *                                 but may be present in the future.
2294  */
2295 
2296 static int
2297 sdprobe(dev_info_t *devi)
2298 {
2299         struct scsi_device      *devp;
2300         int                     rval;
2301         int                     instance = ddi_get_instance(devi);
2302 
2303         if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
2304                 return (DDI_PROBE_DONTCARE);
2305         }
2306 
2307         devp = ddi_get_driver_private(devi);
2308 
2309         if (devp == NULL) {
2310                 /* Ooops... nexus driver is mis-configured... */
2311                 return (DDI_PROBE_FAILURE);
2312         }
2313 
2314         if (ddi_get_soft_state(sd_state, instance) != NULL) {
2315                 return (DDI_PROBE_PARTIAL);
2316         }
2317 
2318         /*
2319          * Call the SCSA utility probe routine to see if we actually
2320          * have a target at this SCSI nexus.
2321          */
2322         switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) {
2323         case SCSIPROBE_EXISTS:
2324                 switch (devp->sd_inq->inq_dtype) {
2325                 case DTYPE_DIRECT:
2326                         rval = DDI_PROBE_SUCCESS;
2327                         break;
2328                 case DTYPE_RODIRECT:
2329                         /* CDs etc. Can be removable media. */
2330                         rval = DDI_PROBE_SUCCESS;
2331                         break;
2332                 case DTYPE_OPTICAL:
2333                         /*
2334                          * Rewritable optical driver HP115AA.
2335                          * Can also be removable media.
2336                          */
2337                         rval = DDI_PROBE_SUCCESS;
2338                         break;
2339                 case DTYPE_NOTPRESENT:
2340                 default:
2341                         rval = DDI_PROBE_FAILURE;
2342                         break;
2343                 }
2344                 break;
2345         default:
2346                 rval = DDI_PROBE_PARTIAL;
2347                 break;
2348         }
2349 
2350         /*
2351          * This routine checks for resource allocation prior to freeing,
2352          * so it will take care of the "smart probing" case where a
2353          * scsi_probe() may or may not have been issued and will *not*
2354          * free previously-freed resources.
2355          */
2356         scsi_unprobe(devp);
2357         return (rval);
2358 }
2359 
2360 
2361 /*
2362  *    Function: sdinfo
2363  *
2364  * Description: This is the driver getinfo(9e) entry point function.
2365  *              Given the device number, return the devinfo pointer from
2366  *              the scsi_device structure or the instance number
2367  *              associated with the dev_t.
2368  *
2369  *   Arguments: dip     - pointer to device info structure
2370  *              infocmd - command argument (DDI_INFO_DEVT2DEVINFO,
2371  *                        DDI_INFO_DEVT2INSTANCE)
2372  *              arg     - driver dev_t
2373  *              resultp - user buffer for request response
2374  *
2375  * Return Code: DDI_SUCCESS
2376  *              DDI_FAILURE
2377  */
2378 /* ARGSUSED */
2379 static int
2380 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2381 {
2382         struct sd_lun   *un;
2383         dev_t           dev;
2384         int             instance;
2385         int             error;
2386 
2387         switch (infocmd) {
2388         case DDI_INFO_DEVT2DEVINFO:
2389                 dev = (dev_t)arg;
2390                 instance = SDUNIT(dev);
2391                 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
2392                         return (DDI_FAILURE);
2393                 }
2394                 *result = (void *) SD_DEVINFO(un);
2395                 error = DDI_SUCCESS;
2396                 break;
2397         case DDI_INFO_DEVT2INSTANCE:
2398                 dev = (dev_t)arg;
2399                 instance = SDUNIT(dev);
2400                 *result = (void *)(uintptr_t)instance;
2401                 error = DDI_SUCCESS;
2402                 break;
2403         default:
2404                 error = DDI_FAILURE;
2405         }
2406         return (error);
2407 }
2408 
2409 /*
2410  *    Function: sd_prop_op
2411  *
2412  * Description: This is the driver prop_op(9e) entry point function.
2413  *              Return the number of blocks for the partition in question
2414  *              or forward the request to the property facilities.
2415  *
2416  *   Arguments: dev       - device number
2417  *              dip       - pointer to device info structure
2418  *              prop_op   - property operator
2419  *              mod_flags - DDI_PROP_DONTPASS, don't pass to parent
2420  *              name      - pointer to property name
2421  *              valuep    - pointer or address of the user buffer
2422  *              lengthp   - property length
2423  *
2424  * Return Code: DDI_PROP_SUCCESS
2425  *              DDI_PROP_NOT_FOUND
2426  *              DDI_PROP_UNDEFINED
2427  *              DDI_PROP_NO_MEMORY
2428  *              DDI_PROP_BUF_TOO_SMALL
2429  */
2430 
2431 static int
2432 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
2433     char *name, caddr_t valuep, int *lengthp)
2434 {
2435         struct sd_lun   *un;
2436 
2437         if ((un = ddi_get_soft_state(sd_state, ddi_get_instance(dip))) == NULL)
2438                 goto fallback;
2439 
2440         mutex_enter(SD_MUTEX(un));
2441         while ((un->un_state == SD_STATE_ATTACHING))
2442                 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
2443 
2444         if (un->un_state == SD_STATE_ATTACH_FAILED) {
2445                 mutex_exit(SD_MUTEX(un));
2446                 goto fallback;
2447         }
2448         mutex_exit(SD_MUTEX(un));
2449 
2450         return (cmlb_prop_op(un->un_cmlbhandle,
2451             dev, dip, prop_op, mod_flags, name, valuep, lengthp,
2452             SDPART(dev), (void *)SD_PATH_DIRECT));
2453 
2454 fallback:
2455         return (ddi_prop_op(dev, dip, prop_op, mod_flags, name, valuep,
2456             lengthp));
2457 }
2458 
2459 /*
2460  * The following functions are for smart probing:
2461  * sd_scsi_probe_cache_init()
2462  * sd_scsi_probe_cache_fini()
2463  * sd_scsi_clear_probe_cache()
2464  * sd_scsi_probe_with_cache()
2465  */
2466 
2467 /*
2468  *    Function: sd_scsi_probe_cache_init
2469  *
2470  * Description: Initializes the probe response cache mutex and head pointer.
2471  *
2472  *     Context: Kernel thread context
2473  */
2474 
2475 static void
2476 sd_scsi_probe_cache_init(void)
2477 {
2478         mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL);
2479         sd_scsi_probe_cache_head = NULL;
2480 }
2481 
2482 
2483 /*
2484  *    Function: sd_scsi_probe_cache_fini
2485  *
2486  * Description: Frees all resources associated with the probe response cache.
2487  *
2488  *     Context: Kernel thread context
2489  */
2490 
2491 static void
2492 sd_scsi_probe_cache_fini(void)
2493 {
2494         struct sd_scsi_probe_cache *cp;
2495         struct sd_scsi_probe_cache *ncp;
2496 
2497         /* Clean up our smart probing linked list */
2498         for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) {
2499                 ncp = cp->next;
2500                 kmem_free(cp, sizeof (struct sd_scsi_probe_cache));
2501         }
2502         sd_scsi_probe_cache_head = NULL;
2503         mutex_destroy(&sd_scsi_probe_cache_mutex);
2504 }
2505 
2506 
2507 /*
2508  *    Function: sd_scsi_clear_probe_cache
2509  *
2510  * Description: This routine clears the probe response cache. This is
2511  *              done when open() returns ENXIO so that when deferred
2512  *              attach is attempted (possibly after a device has been
2513  *              turned on) we will retry the probe. Since we don't know
2514  *              which target we failed to open, we just clear the
2515  *              entire cache.
2516  *
2517  *     Context: Kernel thread context
2518  */
2519 
2520 static void
2521 sd_scsi_clear_probe_cache(void)
2522 {
2523         struct sd_scsi_probe_cache      *cp;
2524         int                             i;
2525 
2526         mutex_enter(&sd_scsi_probe_cache_mutex);
2527         for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2528                 /*
2529                  * Reset all entries to SCSIPROBE_EXISTS.  This will
2530                  * force probing to be performed the next time
2531                  * sd_scsi_probe_with_cache is called.
2532                  */
2533                 for (i = 0; i < NTARGETS_WIDE; i++) {
2534                         cp->cache[i] = SCSIPROBE_EXISTS;
2535                 }
2536         }
2537         mutex_exit(&sd_scsi_probe_cache_mutex);
2538 }
2539 
2540 
2541 /*
2542  *    Function: sd_scsi_probe_with_cache
2543  *
2544  * Description: This routine implements support for a scsi device probe
2545  *              with cache. The driver maintains a cache of the target
2546  *              responses to scsi probes. If we get no response from a
2547  *              target during a probe inquiry, we remember that, and we
2548  *              avoid additional calls to scsi_probe on non-zero LUNs
2549  *              on the same target until the cache is cleared. By doing
2550  *              so we avoid the 1/4 sec selection timeout for nonzero
2551  *              LUNs. lun0 of a target is always probed.
2552  *
2553  *   Arguments: devp     - Pointer to a scsi_device(9S) structure
2554  *              waitfunc - indicates what the allocator routines should
2555  *                         do when resources are not available. This value
2556  *                         is passed on to scsi_probe() when that routine
2557  *                         is called.
2558  *
2559  * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache;
2560  *              otherwise the value returned by scsi_probe(9F).
2561  *
2562  *     Context: Kernel thread context
2563  */
2564 
2565 static int
2566 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)())
2567 {
2568         struct sd_scsi_probe_cache      *cp;
2569         dev_info_t      *pdip = ddi_get_parent(devp->sd_dev);
2570         int             lun, tgt;
2571 
2572         lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
2573             SCSI_ADDR_PROP_LUN, 0);
2574         tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
2575             SCSI_ADDR_PROP_TARGET, -1);
2576 
2577         /* Make sure caching enabled and target in range */
2578         if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) {
2579                 /* do it the old way (no cache) */
2580                 return (scsi_probe(devp, waitfn));
2581         }
2582 
2583         mutex_enter(&sd_scsi_probe_cache_mutex);
2584 
2585         /* Find the cache for this scsi bus instance */
2586         for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2587                 if (cp->pdip == pdip) {
2588                         break;
2589                 }
2590         }
2591 
2592         /* If we can't find a cache for this pdip, create one */
2593         if (cp == NULL) {
2594                 int i;
2595 
2596                 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache),
2597                     KM_SLEEP);
2598                 cp->pdip = pdip;
2599                 cp->next = sd_scsi_probe_cache_head;
2600                 sd_scsi_probe_cache_head = cp;
2601                 for (i = 0; i < NTARGETS_WIDE; i++) {
2602                         cp->cache[i] = SCSIPROBE_EXISTS;
2603                 }
2604         }
2605 
2606         mutex_exit(&sd_scsi_probe_cache_mutex);
2607 
2608         /* Recompute the cache for this target if LUN zero */
2609         if (lun == 0) {
2610                 cp->cache[tgt] = SCSIPROBE_EXISTS;
2611         }
2612 
2613         /* Don't probe if cache remembers a NORESP from a previous LUN. */
2614         if (cp->cache[tgt] != SCSIPROBE_EXISTS) {
2615                 return (SCSIPROBE_NORESP);
2616         }
2617 
2618         /* Do the actual probe; save & return the result */
2619         return (cp->cache[tgt] = scsi_probe(devp, waitfn));
2620 }
2621 
2622 
2623 /*
2624  *    Function: sd_scsi_target_lun_init
2625  *
2626  * Description: Initializes the attached lun chain mutex and head pointer.
2627  *
2628  *     Context: Kernel thread context
2629  */
2630 
2631 static void
2632 sd_scsi_target_lun_init(void)
2633 {
2634         mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL);
2635         sd_scsi_target_lun_head = NULL;
2636 }
2637 
2638 
2639 /*
2640  *    Function: sd_scsi_target_lun_fini
2641  *
2642  * Description: Frees all resources associated with the attached lun
2643  *              chain
2644  *
2645  *     Context: Kernel thread context
2646  */
2647 
2648 static void
2649 sd_scsi_target_lun_fini(void)
2650 {
2651         struct sd_scsi_hba_tgt_lun      *cp;
2652         struct sd_scsi_hba_tgt_lun      *ncp;
2653 
2654         for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) {
2655                 ncp = cp->next;
2656                 kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun));
2657         }
2658         sd_scsi_target_lun_head = NULL;
2659         mutex_destroy(&sd_scsi_target_lun_mutex);
2660 }
2661 
2662 
2663 /*
2664  *    Function: sd_scsi_get_target_lun_count
2665  *
2666  * Description: This routine will check in the attached lun chain to see
2667  *              how many luns are attached on the required SCSI controller
2668  *              and target. Currently, some capabilities like tagged queue
2669  *              are supported per target based by HBA. So all luns in a
2670  *              target have the same capabilities. Based on this assumption,
2671  *              sd should only set these capabilities once per target. This
2672  *              function is called when sd needs to decide how many luns
2673  *              already attached on a target.
2674  *
2675  *   Arguments: dip     - Pointer to the system's dev_info_t for the SCSI
2676  *                        controller device.
2677  *              target  - The target ID on the controller's SCSI bus.
2678  *
2679  * Return Code: The number of luns attached on the required target and
2680  *              controller.
2681  *              -1 if target ID is not in parallel SCSI scope or the given
2682  *              dip is not in the chain.
2683  *
2684  *     Context: Kernel thread context
2685  */
2686 
2687 static int
2688 sd_scsi_get_target_lun_count(dev_info_t *dip, int target)
2689 {
2690         struct sd_scsi_hba_tgt_lun      *cp;
2691 
2692         if ((target < 0) || (target >= NTARGETS_WIDE)) {
2693                 return (-1);
2694         }
2695 
2696         mutex_enter(&sd_scsi_target_lun_mutex);
2697 
2698         for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
2699                 if (cp->pdip == dip) {
2700                         break;
2701                 }
2702         }
2703 
2704         mutex_exit(&sd_scsi_target_lun_mutex);
2705 
2706         if (cp == NULL) {
2707                 return (-1);
2708         }
2709 
2710         return (cp->nlun[target]);
2711 }
2712 
2713 
2714 /*
2715  *    Function: sd_scsi_update_lun_on_target
2716  *
2717  * Description: This routine is used to update the attached lun chain when a
2718  *              lun is attached or detached on a target.
2719  *
2720  *   Arguments: dip     - Pointer to the system's dev_info_t for the SCSI
2721  *                        controller device.
2722  *              target  - The target ID on the controller's SCSI bus.
2723  *              flag    - Indicate the lun is attached or detached.
2724  *
2725  *     Context: Kernel thread context
2726  */
2727 
2728 static void
2729 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag)
2730 {
2731         struct sd_scsi_hba_tgt_lun      *cp;
2732 
2733         mutex_enter(&sd_scsi_target_lun_mutex);
2734 
2735         for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
2736                 if (cp->pdip == dip) {
2737                         break;
2738                 }
2739         }
2740 
2741         if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) {
2742                 cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun),
2743                     KM_SLEEP);
2744                 cp->pdip = dip;
2745                 cp->next = sd_scsi_target_lun_head;
2746                 sd_scsi_target_lun_head = cp;
2747         }
2748 
2749         mutex_exit(&sd_scsi_target_lun_mutex);
2750 
2751         if (cp != NULL) {
2752                 if (flag == SD_SCSI_LUN_ATTACH) {
2753                         cp->nlun[target] ++;
2754                 } else {
2755                         cp->nlun[target] --;
2756                 }
2757         }
2758 }
2759 
2760 
2761 /*
2762  *    Function: sd_spin_up_unit
2763  *
2764  * Description: Issues the following commands to spin-up the device:
2765  *              START STOP UNIT, and INQUIRY.
2766  *
2767  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
2768  *                      structure for this target.
2769  *
2770  * Return Code: 0 - success
2771  *              EIO - failure
2772  *              EACCES - reservation conflict
2773  *
2774  *     Context: Kernel thread context
2775  */
2776 
2777 static int
2778 sd_spin_up_unit(sd_ssc_t *ssc)
2779 {
2780         size_t  resid           = 0;
2781         int     has_conflict    = FALSE;
2782         uchar_t *bufaddr;
2783         int     status;
2784         struct sd_lun   *un;
2785 
2786         ASSERT(ssc != NULL);
2787         un = ssc->ssc_un;
2788         ASSERT(un != NULL);
2789 
2790         /*
2791          * Send a throwaway START UNIT command.
2792          *
2793          * If we fail on this, we don't care presently what precisely
2794          * is wrong.  EMC's arrays will also fail this with a check
2795          * condition (0x2/0x4/0x3) if the device is "inactive," but
2796          * we don't want to fail the attach because it may become
2797          * "active" later.
2798          * We don't know if power condition is supported or not at
2799          * this stage, use START STOP bit.
2800          */
2801         status = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
2802             SD_TARGET_START, SD_PATH_DIRECT);
2803 
2804         switch (status) {
2805         case EIO:
2806                 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
2807                 return (status);
2808         case EACCES:
2809                 has_conflict = TRUE;
2810         default: /*FALLTHROUGH*/
2811                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
2812         }
2813 
2814         /*
2815          * Send another INQUIRY command to the target. This is necessary for
2816          * non-removable media direct access devices because their INQUIRY data
2817          * may not be fully qualified until they are spun up (perhaps via the
2818          * START command above).  Note: This seems to be needed for some
2819          * legacy devices only.) The INQUIRY command should succeed even if a
2820          * Reservation Conflict is present.
2821          */
2822         bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP);
2823 
2824         if (sd_send_scsi_INQUIRY(ssc, bufaddr, SUN_INQSIZE, 0, 0, &resid)
2825             != 0) {
2826                 kmem_free(bufaddr, SUN_INQSIZE);
2827                 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
2828                 return (EIO);
2829         }
2830 
2831         /*
2832          * If we got enough INQUIRY data, copy it over the old INQUIRY data.
2833          * Note that this routine does not return a failure here even if the
2834          * INQUIRY command did not return any data.  This is a legacy behavior.
2835          */
2836         if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) {
2837                 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE);
2838         }
2839 
2840         kmem_free(bufaddr, SUN_INQSIZE);
2841 
2842         /* If we hit a reservation conflict above, tell the caller. */
2843         if (has_conflict == TRUE) {
2844                 return (EACCES);
2845         }
2846 
2847         return (0);
2848 }
2849 
2850 #ifdef _LP64
2851 /*
2852  *    Function: sd_enable_descr_sense
2853  *
2854  * Description: This routine attempts to select descriptor sense format
2855  *              using the Control mode page.  Devices that support 64 bit
2856  *              LBAs (for >2TB luns) should also implement descriptor
2857  *              sense data so we will call this function whenever we see
2858  *              a lun larger than 2TB.  If for some reason the device
2859  *              supports 64 bit LBAs but doesn't support descriptor sense
2860  *              presumably the mode select will fail.  Everything will
2861  *              continue to work normally except that we will not get
2862  *              complete sense data for commands that fail with an LBA
2863  *              larger than 32 bits.
2864  *
2865  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
2866  *                      structure for this target.
2867  *
2868  *     Context: Kernel thread context only
2869  */
2870 
2871 static void
2872 sd_enable_descr_sense(sd_ssc_t *ssc)
2873 {
2874         uchar_t                 *header;
2875         struct mode_control_scsi3 *ctrl_bufp;
2876         size_t                  buflen;
2877         size_t                  bd_len;
2878         int                     status;
2879         struct sd_lun           *un;
2880 
2881         ASSERT(ssc != NULL);
2882         un = ssc->ssc_un;
2883         ASSERT(un != NULL);
2884 
2885         /*
2886          * Read MODE SENSE page 0xA, Control Mode Page
2887          */
2888         buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH +
2889             sizeof (struct mode_control_scsi3);
2890         header = kmem_zalloc(buflen, KM_SLEEP);
2891 
2892         status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
2893             MODEPAGE_CTRL_MODE, SD_PATH_DIRECT);
2894 
2895         if (status != 0) {
2896                 SD_ERROR(SD_LOG_COMMON, un,
2897                     "sd_enable_descr_sense: mode sense ctrl page failed\n");
2898                 goto eds_exit;
2899         }
2900 
2901         /*
2902          * Determine size of Block Descriptors in order to locate
2903          * the mode page data. ATAPI devices return 0, SCSI devices
2904          * should return MODE_BLK_DESC_LENGTH.
2905          */
2906         bd_len  = ((struct mode_header *)header)->bdesc_length;
2907 
2908         /* Clear the mode data length field for MODE SELECT */
2909         ((struct mode_header *)header)->length = 0;
2910 
2911         ctrl_bufp = (struct mode_control_scsi3 *)
2912             (header + MODE_HEADER_LENGTH + bd_len);
2913 
2914         /*
2915          * If the page length is smaller than the expected value,
2916          * the target device doesn't support D_SENSE. Bail out here.
2917          */
2918         if (ctrl_bufp->mode_page.length <
2919             sizeof (struct mode_control_scsi3) - 2) {
2920                 SD_ERROR(SD_LOG_COMMON, un,
2921                     "sd_enable_descr_sense: enable D_SENSE failed\n");
2922                 goto eds_exit;
2923         }
2924 
2925         /*
2926          * Clear PS bit for MODE SELECT
2927          */
2928         ctrl_bufp->mode_page.ps = 0;
2929 
2930         /*
2931          * Set D_SENSE to enable descriptor sense format.
2932          */
2933         ctrl_bufp->d_sense = 1;
2934 
2935         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
2936 
2937         /*
2938          * Use MODE SELECT to commit the change to the D_SENSE bit
2939          */
2940         status = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header,
2941             buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT);
2942 
2943         if (status != 0) {
2944                 SD_INFO(SD_LOG_COMMON, un,
2945                     "sd_enable_descr_sense: mode select ctrl page failed\n");
2946         } else {
2947                 kmem_free(header, buflen);
2948                 return;
2949         }
2950 
2951 eds_exit:
2952         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
2953         kmem_free(header, buflen);
2954 }
2955 
2956 /*
2957  *    Function: sd_reenable_dsense_task
2958  *
2959  * Description: Re-enable descriptor sense after device or bus reset
2960  *
2961  *     Context: Executes in a taskq() thread context
2962  */
2963 static void
2964 sd_reenable_dsense_task(void *arg)
2965 {
2966         struct  sd_lun  *un = arg;
2967         sd_ssc_t        *ssc;
2968 
2969         ASSERT(un != NULL);
2970 
2971         ssc = sd_ssc_init(un);
2972         sd_enable_descr_sense(ssc);
2973         sd_ssc_fini(ssc);
2974 }
2975 #endif /* _LP64 */
2976 
2977 /*
2978  *    Function: sd_set_mmc_caps
2979  *
2980  * Description: This routine determines if the device is MMC compliant and if
2981  *              the device supports CDDA via a mode sense of the CDVD
2982  *              capabilities mode page. Also checks if the device is a
2983  *              dvdram writable device.
2984  *
2985  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
2986  *                      structure for this target.
2987  *
2988  *     Context: Kernel thread context only
2989  */
2990 
2991 static void
2992 sd_set_mmc_caps(sd_ssc_t *ssc)
2993 {
2994         struct mode_header_grp2         *sense_mhp;
2995         uchar_t                         *sense_page;
2996         caddr_t                         buf;
2997         int                             bd_len;
2998         int                             status;
2999         struct uscsi_cmd                com;
3000         int                             rtn;
3001         uchar_t                         *out_data_rw, *out_data_hd;
3002         uchar_t                         *rqbuf_rw, *rqbuf_hd;
3003         uchar_t                         *out_data_gesn;
3004         int                             gesn_len;
3005         struct sd_lun                   *un;
3006 
3007         ASSERT(ssc != NULL);
3008         un = ssc->ssc_un;
3009         ASSERT(un != NULL);
3010 
3011         /*
3012          * The flags which will be set in this function are - mmc compliant,
3013          * dvdram writable device, cdda support. Initialize them to FALSE
3014          * and if a capability is detected - it will be set to TRUE.
3015          */
3016         un->un_f_mmc_cap = FALSE;
3017         un->un_f_dvdram_writable_device = FALSE;
3018         un->un_f_cfg_cdda = FALSE;
3019 
3020         buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3021         status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3022             BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
3023 
3024         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3025 
3026         if (status != 0) {
3027                 /* command failed; just return */
3028                 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3029                 return;
3030         }
3031         /*
3032          * If the mode sense request for the CDROM CAPABILITIES
3033          * page (0x2A) succeeds the device is assumed to be MMC.
3034          */
3035         un->un_f_mmc_cap = TRUE;
3036 
3037         /* See if GET STATUS EVENT NOTIFICATION is supported */
3038         if (un->un_f_mmc_gesn_polling) {
3039                 gesn_len = SD_GESN_HEADER_LEN + SD_GESN_MEDIA_DATA_LEN;
3040                 out_data_gesn = kmem_zalloc(gesn_len, KM_SLEEP);
3041 
3042                 rtn = sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(ssc,
3043                     out_data_gesn, gesn_len, 1 << SD_GESN_MEDIA_CLASS);
3044 
3045                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3046 
3047                 if ((rtn != 0) || !sd_gesn_media_data_valid(out_data_gesn)) {
3048                         un->un_f_mmc_gesn_polling = FALSE;
3049                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
3050                             "sd_set_mmc_caps: gesn not supported "
3051                             "%d %x %x %x %x\n", rtn,
3052                             out_data_gesn[0], out_data_gesn[1],
3053                             out_data_gesn[2], out_data_gesn[3]);
3054                 }
3055 
3056                 kmem_free(out_data_gesn, gesn_len);
3057         }
3058 
3059         /* Get to the page data */
3060         sense_mhp = (struct mode_header_grp2 *)buf;
3061         bd_len = (sense_mhp->bdesc_length_hi << 8) |
3062             sense_mhp->bdesc_length_lo;
3063         if (bd_len > MODE_BLK_DESC_LENGTH) {
3064                 /*
3065                  * We did not get back the expected block descriptor
3066                  * length so we cannot determine if the device supports
3067                  * CDDA. However, we still indicate the device is MMC
3068                  * according to the successful response to the page
3069                  * 0x2A mode sense request.
3070                  */
3071                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3072                     "sd_set_mmc_caps: Mode Sense returned "
3073                     "invalid block descriptor length\n");
3074                 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3075                 return;
3076         }
3077 
3078         /* See if read CDDA is supported */
3079         sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 +
3080             bd_len);
3081         un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE;
3082 
3083         /* See if writing DVD RAM is supported. */
3084         un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE;
3085         if (un->un_f_dvdram_writable_device == TRUE) {
3086                 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3087                 return;
3088         }
3089 
3090         /*
3091          * If the device presents DVD or CD capabilities in the mode
3092          * page, we can return here since a RRD will not have
3093          * these capabilities.
3094          */
3095         if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3096                 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3097                 return;
3098         }
3099         kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3100 
3101         /*
3102          * If un->un_f_dvdram_writable_device is still FALSE,
3103          * check for a Removable Rigid Disk (RRD).  A RRD
3104          * device is identified by the features RANDOM_WRITABLE and
3105          * HARDWARE_DEFECT_MANAGEMENT.
3106          */
3107         out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3108         rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3109 
3110         rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3111             SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3112             RANDOM_WRITABLE, SD_PATH_STANDARD);
3113 
3114         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3115 
3116         if (rtn != 0) {
3117                 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3118                 kmem_free(rqbuf_rw, SENSE_LENGTH);
3119                 return;
3120         }
3121 
3122         out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3123         rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3124 
3125         rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3126             SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3127             HARDWARE_DEFECT_MANAGEMENT, SD_PATH_STANDARD);
3128 
3129         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3130 
3131         if (rtn == 0) {
3132                 /*
3133                  * We have good information, check for random writable
3134                  * and hardware defect features.
3135                  */
3136                 if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3137                     (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) {
3138                         un->un_f_dvdram_writable_device = TRUE;
3139                 }
3140         }
3141 
3142         kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3143         kmem_free(rqbuf_rw, SENSE_LENGTH);
3144         kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3145         kmem_free(rqbuf_hd, SENSE_LENGTH);
3146 }
3147 
3148 /*
3149  *    Function: sd_check_for_writable_cd
3150  *
3151  * Description: This routine determines if the media in the device is
3152  *              writable or not. It uses the get configuration command (0x46)
3153  *              to determine if the media is writable
3154  *
3155  *   Arguments: un - driver soft state (unit) structure
3156  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct"
3157  *                           chain and the normal command waitq, or
3158  *                           SD_PATH_DIRECT_PRIORITY to use the USCSI
3159  *                           "direct" chain and bypass the normal command
3160  *                           waitq.
3161  *
3162  *     Context: Never called at interrupt context.
3163  */
3164 
3165 static void
3166 sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag)
3167 {
3168         struct uscsi_cmd                com;
3169         uchar_t                         *out_data;
3170         uchar_t                         *rqbuf;
3171         int                             rtn;
3172         uchar_t                         *out_data_rw, *out_data_hd;
3173         uchar_t                         *rqbuf_rw, *rqbuf_hd;
3174         struct mode_header_grp2         *sense_mhp;
3175         uchar_t                         *sense_page;
3176         caddr_t                         buf;
3177         int                             bd_len;
3178         int                             status;
3179         struct sd_lun                   *un;
3180 
3181         ASSERT(ssc != NULL);
3182         un = ssc->ssc_un;
3183         ASSERT(un != NULL);
3184         ASSERT(mutex_owned(SD_MUTEX(un)));
3185 
3186         /*
3187          * Initialize the writable media to false, if configuration info.
3188          * tells us otherwise then only we will set it.
3189          */
3190         un->un_f_mmc_writable_media = FALSE;
3191         mutex_exit(SD_MUTEX(un));
3192 
3193         out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
3194         rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3195 
3196         rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, SENSE_LENGTH,
3197             out_data, SD_PROFILE_HEADER_LEN, path_flag);
3198 
3199         if (rtn != 0)
3200                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3201 
3202         mutex_enter(SD_MUTEX(un));
3203         if (rtn == 0) {
3204                 /*
3205                  * We have good information, check for writable DVD.
3206                  */
3207                 if ((out_data[6] == 0) && (out_data[7] == 0x12)) {
3208                         un->un_f_mmc_writable_media = TRUE;
3209                         kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3210                         kmem_free(rqbuf, SENSE_LENGTH);
3211                         return;
3212                 }
3213         }
3214 
3215         kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3216         kmem_free(rqbuf, SENSE_LENGTH);
3217 
3218         /*
3219          * Determine if this is a RRD type device.
3220          */
3221         mutex_exit(SD_MUTEX(un));
3222         buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3223         status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3224             BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, path_flag);
3225 
3226         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3227 
3228         mutex_enter(SD_MUTEX(un));
3229         if (status != 0) {
3230                 /* command failed; just return */
3231                 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3232                 return;
3233         }
3234 
3235         /* Get to the page data */
3236         sense_mhp = (struct mode_header_grp2 *)buf;
3237         bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
3238         if (bd_len > MODE_BLK_DESC_LENGTH) {
3239                 /*
3240                  * We did not get back the expected block descriptor length so
3241                  * we cannot check the mode page.
3242                  */
3243                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3244                     "sd_check_for_writable_cd: Mode Sense returned "
3245                     "invalid block descriptor length\n");
3246                 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3247                 return;
3248         }
3249 
3250         /*
3251          * If the device presents DVD or CD capabilities in the mode
3252          * page, we can return here since a RRD device will not have
3253          * these capabilities.
3254          */
3255         sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len);
3256         if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3257                 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3258                 return;
3259         }
3260         kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3261 
3262         /*
3263          * If un->un_f_mmc_writable_media is still FALSE,
3264          * check for RRD type media.  A RRD device is identified
3265          * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT.
3266          */
3267         mutex_exit(SD_MUTEX(un));
3268         out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3269         rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3270 
3271         rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3272             SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3273             RANDOM_WRITABLE, path_flag);
3274 
3275         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3276         if (rtn != 0) {
3277                 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3278                 kmem_free(rqbuf_rw, SENSE_LENGTH);
3279                 mutex_enter(SD_MUTEX(un));
3280                 return;
3281         }
3282 
3283         out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3284         rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3285 
3286         rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3287             SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3288             HARDWARE_DEFECT_MANAGEMENT, path_flag);
3289 
3290         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3291         mutex_enter(SD_MUTEX(un));
3292         if (rtn == 0) {
3293                 /*
3294                  * We have good information, check for random writable
3295                  * and hardware defect features as current.
3296                  */
3297                 if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3298                     (out_data_rw[10] & 0x1) &&
3299                     (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) &&
3300                     (out_data_hd[10] & 0x1)) {
3301                         un->un_f_mmc_writable_media = TRUE;
3302                 }
3303         }
3304 
3305         kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3306         kmem_free(rqbuf_rw, SENSE_LENGTH);
3307         kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3308         kmem_free(rqbuf_hd, SENSE_LENGTH);
3309 }
3310 
3311 /*
3312  *    Function: sd_read_unit_properties
3313  *
3314  * Description: The following implements a property lookup mechanism.
3315  *              Properties for particular disks (keyed on vendor, model
3316  *              and rev numbers) are sought in the sd.conf file via
3317  *              sd_process_sdconf_file(), and if not found there, are
3318  *              looked for in a list hardcoded in this driver via
3319  *              sd_process_sdconf_table() Once located the properties
3320  *              are used to update the driver unit structure.
3321  *
3322  *   Arguments: un - driver soft state (unit) structure
3323  */
3324 
3325 static void
3326 sd_read_unit_properties(struct sd_lun *un)
3327 {
3328         /*
3329          * sd_process_sdconf_file returns SD_FAILURE if it cannot find
3330          * the "sd-config-list" property (from the sd.conf file) or if
3331          * there was not a match for the inquiry vid/pid. If this event
3332          * occurs the static driver configuration table is searched for
3333          * a match.
3334          */
3335         ASSERT(un != NULL);
3336         if (sd_process_sdconf_file(un) == SD_FAILURE) {
3337                 sd_process_sdconf_table(un);
3338         }
3339 
3340         /* check for LSI device */
3341         sd_is_lsi(un);
3342 
3343 
3344 }
3345 
3346 
3347 /*
3348  *    Function: sd_process_sdconf_file
3349  *
3350  * Description: Use ddi_prop_lookup(9F) to obtain the properties from the
3351  *              driver's config file (ie, sd.conf) and update the driver
3352  *              soft state structure accordingly.
3353  *
3354  *   Arguments: un - driver soft state (unit) structure
3355  *
3356  * Return Code: SD_SUCCESS - The properties were successfully set according
3357  *                           to the driver configuration file.
3358  *              SD_FAILURE - The driver config list was not obtained or
3359  *                           there was no vid/pid match. This indicates that
3360  *                           the static config table should be used.
3361  *
3362  * The config file has a property, "sd-config-list". Currently we support
3363  * two kinds of formats. For both formats, the value of this property
3364  * is a list of duplets:
3365  *
3366  *  sd-config-list=
3367  *      <duplet>,
3368  *      [,<duplet>]*;
3369  *
3370  * For the improved format, where
3371  *
3372  *     <duplet>:= "<vid+pid>","<tunable-list>"
3373  *
3374  * and
3375  *
3376  *     <tunable-list>:=   <tunable> [, <tunable> ]*;
3377  *     <tunable> =        <name> : <value>
3378  *
3379  * The <vid+pid> is the string that is returned by the target device on a
3380  * SCSI inquiry command, the <tunable-list> contains one or more tunables
3381  * to apply to all target devices with the specified <vid+pid>.
3382  *
3383  * Each <tunable> is a "<name> : <value>" pair.
3384  *
3385  * For the old format, the structure of each duplet is as follows:
3386  *
3387  *  <duplet>:= "<vid+pid>","<data-property-name_list>"
3388  *
3389  * The first entry of the duplet is the device ID string (the concatenated
3390  * vid & pid; not to be confused with a device_id).  This is defined in
3391  * the same way as in the sd_disk_table.
3392  *
3393  * The second part of the duplet is a string that identifies a
3394  * data-property-name-list. The data-property-name-list is defined as
3395  * follows:
3396  *
3397  *  <data-property-name-list>:=<data-property-name> [<data-property-name>]
3398  *
3399  * The syntax of <data-property-name> depends on the <version> field.
3400  *
3401  * If version = SD_CONF_VERSION_1 we have the following syntax:
3402  *
3403  *      <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
3404  *
3405  * where the prop0 value will be used to set prop0 if bit0 set in the
3406  * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1
3407  *
3408  */
3409 
3410 static int
3411 sd_process_sdconf_file(struct sd_lun *un)
3412 {
3413         char    **config_list = NULL;
3414         uint_t  nelements;
3415         char    *vidptr;
3416         int     vidlen;
3417         char    *dnlist_ptr;
3418         char    *dataname_ptr;
3419         char    *dataname_lasts;
3420         int     *data_list = NULL;
3421         uint_t  data_list_len;
3422         int     rval = SD_FAILURE;
3423         int     i;
3424 
3425         ASSERT(un != NULL);
3426 
3427         /* Obtain the configuration list associated with the .conf file */
3428         if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, SD_DEVINFO(un),
3429             DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "sd-config-list",
3430             &config_list, &nelements) != DDI_PROP_SUCCESS) {
3431                 return (SD_FAILURE);
3432         }
3433 
3434         /*
3435          * Compare vids in each duplet to the inquiry vid - if a match is
3436          * made, get the data value and update the soft state structure
3437          * accordingly.
3438          *
3439          * Each duplet should show as a pair of strings, return SD_FAILURE
3440          * otherwise.
3441          */
3442         if (nelements & 1) {
3443                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3444                     "sd-config-list should show as pairs of strings.\n");
3445                 if (config_list)
3446                         ddi_prop_free(config_list);
3447                 return (SD_FAILURE);
3448         }
3449 
3450         for (i = 0; i < nelements; i += 2) {
3451                 /*
3452                  * Note: The assumption here is that each vid entry is on
3453                  * a unique line from its associated duplet.
3454                  */
3455                 vidptr = config_list[i];
3456                 vidlen = (int)strlen(vidptr);
3457                 if (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS) {
3458                         continue;
3459                 }
3460 
3461                 /*
3462                  * dnlist contains 1 or more blank separated
3463                  * data-property-name entries
3464                  */
3465                 dnlist_ptr = config_list[i + 1];
3466 
3467                 if (strchr(dnlist_ptr, ':') != NULL) {
3468                         /*
3469                          * Decode the improved format sd-config-list.
3470                          */
3471                         sd_nvpair_str_decode(un, dnlist_ptr);
3472                 } else {
3473                         /*
3474                          * The old format sd-config-list, loop through all
3475                          * data-property-name entries in the
3476                          * data-property-name-list
3477                          * setting the properties for each.
3478                          */
3479                         for (dataname_ptr = sd_strtok_r(dnlist_ptr, " \t",
3480                             &dataname_lasts); dataname_ptr != NULL;
3481                             dataname_ptr = sd_strtok_r(NULL, " \t",
3482                             &dataname_lasts)) {
3483                                 int version;
3484 
3485                                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3486                                     "sd_process_sdconf_file: disk:%s, "
3487                                     "data:%s\n", vidptr, dataname_ptr);
3488 
3489                                 /* Get the data list */
3490                                 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
3491                                     SD_DEVINFO(un), 0, dataname_ptr, &data_list,
3492                                     &data_list_len) != DDI_PROP_SUCCESS) {
3493                                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
3494                                             "sd_process_sdconf_file: data "
3495                                             "property (%s) has no value\n",
3496                                             dataname_ptr);
3497                                         continue;
3498                                 }
3499 
3500                                 version = data_list[0];
3501 
3502                                 if (version == SD_CONF_VERSION_1) {
3503                                         sd_tunables values;
3504 
3505                                         /* Set the properties */
3506                                         if (sd_chk_vers1_data(un, data_list[1],
3507                                             &data_list[2], data_list_len,
3508                                             dataname_ptr) == SD_SUCCESS) {
3509                                                 sd_get_tunables_from_conf(un,
3510                                                     data_list[1], &data_list[2],
3511                                                     &values);
3512                                                 sd_set_vers1_properties(un,
3513                                                     data_list[1], &values);
3514                                                 rval = SD_SUCCESS;
3515                                         } else {
3516                                                 rval = SD_FAILURE;
3517                                         }
3518                                 } else {
3519                                         scsi_log(SD_DEVINFO(un), sd_label,
3520                                             CE_WARN, "data property %s version "
3521                                             "0x%x is invalid.",
3522                                             dataname_ptr, version);
3523                                         rval = SD_FAILURE;
3524                                 }
3525                                 if (data_list)
3526                                         ddi_prop_free(data_list);
3527                         }
3528                 }
3529         }
3530 
3531         /* free up the memory allocated by ddi_prop_lookup_string_array(). */
3532         if (config_list) {
3533                 ddi_prop_free(config_list);
3534         }
3535 
3536         return (rval);
3537 }
3538 
3539 /*
3540  *    Function: sd_nvpair_str_decode()
3541  *
3542  * Description: Parse the improved format sd-config-list to get
3543  *    each entry of tunable, which includes a name-value pair.
3544  *    Then call sd_set_properties() to set the property.
3545  *
3546  *   Arguments: un - driver soft state (unit) structure
3547  *    nvpair_str - the tunable list
3548  */
3549 static void
3550 sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str)
3551 {
3552         char    *nv, *name, *value, *token;
3553         char    *nv_lasts, *v_lasts, *x_lasts;
3554 
3555         for (nv = sd_strtok_r(nvpair_str, ",", &nv_lasts); nv != NULL;
3556             nv = sd_strtok_r(NULL, ",", &nv_lasts)) {
3557                 token = sd_strtok_r(nv, ":", &v_lasts);
3558                 name  = sd_strtok_r(token, " \t", &x_lasts);
3559                 token = sd_strtok_r(NULL, ":", &v_lasts);
3560                 value = sd_strtok_r(token, " \t", &x_lasts);
3561                 if (name == NULL || value == NULL) {
3562                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
3563                             "sd_nvpair_str_decode: "
3564                             "name or value is not valid!\n");
3565                 } else {
3566                         sd_set_properties(un, name, value);
3567                 }
3568         }
3569 }
3570 
3571 /*
3572  *    Function: sd_strtok_r()
3573  *
3574  * Description: This function uses strpbrk and strspn to break
3575  *    string into tokens on sequentially subsequent calls. Return
3576  *    NULL when no non-separator characters remain. The first
3577  *    argument is NULL for subsequent calls.
3578  */
3579 static char *
3580 sd_strtok_r(char *string, const char *sepset, char **lasts)
3581 {
3582         char    *q, *r;
3583 
3584         /* First or subsequent call */
3585         if (string == NULL)
3586                 string = *lasts;
3587 
3588         if (string == NULL)
3589                 return (NULL);
3590 
3591         /* Skip leading separators */
3592         q = string + strspn(string, sepset);
3593 
3594         if (*q == '\0')
3595                 return (NULL);
3596 
3597         if ((r = strpbrk(q, sepset)) == NULL)
3598                 *lasts = NULL;
3599         else {
3600                 *r = '\0';
3601                 *lasts = r + 1;
3602         }
3603         return (q);
3604 }
3605 
3606 /*
3607  *    Function: sd_set_properties()
3608  *
3609  * Description: Set device properties based on the improved
3610  *    format sd-config-list.
3611  *
3612  *   Arguments: un - driver soft state (unit) structure
3613  *    name  - supported tunable name
3614  *    value - tunable value
3615  */
3616 static void
3617 sd_set_properties(struct sd_lun *un, char *name, char *value)
3618 {
3619         char    *endptr = NULL;
3620         long    val = 0;
3621 
3622         if (strcasecmp(name, "cache-nonvolatile") == 0) {
3623                 if (strcasecmp(value, "true") == 0) {
3624                         un->un_f_suppress_cache_flush = TRUE;
3625                 } else if (strcasecmp(value, "false") == 0) {
3626                         un->un_f_suppress_cache_flush = FALSE;
3627                 } else {
3628                         goto value_invalid;
3629                 }
3630                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3631                     "suppress_cache_flush flag set to %d\n",
3632                     un->un_f_suppress_cache_flush);
3633                 return;
3634         }
3635 
3636         if (strcasecmp(name, "controller-type") == 0) {
3637                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3638                         un->un_ctype = val;
3639                 } else {
3640                         goto value_invalid;
3641                 }
3642                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3643                     "ctype set to %d\n", un->un_ctype);
3644                 return;
3645         }
3646 
3647         if (strcasecmp(name, "delay-busy") == 0) {
3648                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3649                         un->un_busy_timeout = drv_usectohz(val / 1000);
3650                 } else {
3651                         goto value_invalid;
3652                 }
3653                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3654                     "busy_timeout set to %d\n", un->un_busy_timeout);
3655                 return;
3656         }
3657 
3658         if (strcasecmp(name, "disksort") == 0) {
3659                 if (strcasecmp(value, "true") == 0) {
3660                         un->un_f_disksort_disabled = FALSE;
3661                 } else if (strcasecmp(value, "false") == 0) {
3662                         un->un_f_disksort_disabled = TRUE;
3663                 } else {
3664                         goto value_invalid;
3665                 }
3666                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3667                     "disksort disabled flag set to %d\n",
3668                     un->un_f_disksort_disabled);
3669                 return;
3670         }
3671 
3672         if (strcasecmp(name, "power-condition") == 0) {
3673                 if (strcasecmp(value, "true") == 0) {
3674                         un->un_f_power_condition_disabled = FALSE;
3675                 } else if (strcasecmp(value, "false") == 0) {
3676                         un->un_f_power_condition_disabled = TRUE;
3677                 } else {
3678                         goto value_invalid;
3679                 }
3680                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3681                     "power condition disabled flag set to %d\n",
3682                     un->un_f_power_condition_disabled);
3683                 return;
3684         }
3685 
3686         if (strcasecmp(name, "timeout-releasereservation") == 0) {
3687                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3688                         un->un_reserve_release_time = val;
3689                 } else {
3690                         goto value_invalid;
3691                 }
3692                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3693                     "reservation release timeout set to %d\n",
3694                     un->un_reserve_release_time);
3695                 return;
3696         }
3697 
3698         if (strcasecmp(name, "reset-lun") == 0) {
3699                 if (strcasecmp(value, "true") == 0) {
3700                         un->un_f_lun_reset_enabled = TRUE;
3701                 } else if (strcasecmp(value, "false") == 0) {
3702                         un->un_f_lun_reset_enabled = FALSE;
3703                 } else {
3704                         goto value_invalid;
3705                 }
3706                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3707                     "lun reset enabled flag set to %d\n",
3708                     un->un_f_lun_reset_enabled);
3709                 return;
3710         }
3711 
3712         if (strcasecmp(name, "retries-busy") == 0) {
3713                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3714                         un->un_busy_retry_count = val;
3715                 } else {
3716                         goto value_invalid;
3717                 }
3718                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3719                     "busy retry count set to %d\n", un->un_busy_retry_count);
3720                 return;
3721         }
3722 
3723         if (strcasecmp(name, "retries-timeout") == 0) {
3724                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3725                         un->un_retry_count = val;
3726                 } else {
3727                         goto value_invalid;
3728                 }
3729                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3730                     "timeout retry count set to %d\n", un->un_retry_count);
3731                 return;
3732         }
3733 
3734         if (strcasecmp(name, "retries-notready") == 0) {
3735                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3736                         un->un_notready_retry_count = val;
3737                 } else {
3738                         goto value_invalid;
3739                 }
3740                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3741                     "notready retry count set to %d\n",
3742                     un->un_notready_retry_count);
3743                 return;
3744         }
3745 
3746         if (strcasecmp(name, "retries-reset") == 0) {
3747                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3748                         un->un_reset_retry_count = val;
3749                 } else {
3750                         goto value_invalid;
3751                 }
3752                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3753                     "reset retry count set to %d\n",
3754                     un->un_reset_retry_count);
3755                 return;
3756         }
3757 
3758         if (strcasecmp(name, "throttle-max") == 0) {
3759                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3760                         un->un_saved_throttle = un->un_throttle = val;
3761                 } else {
3762                         goto value_invalid;
3763                 }
3764                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3765                     "throttle set to %d\n", un->un_throttle);
3766         }
3767 
3768         if (strcasecmp(name, "throttle-min") == 0) {
3769                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3770                         un->un_min_throttle = val;
3771                 } else {
3772                         goto value_invalid;
3773                 }
3774                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3775                     "min throttle set to %d\n", un->un_min_throttle);
3776         }
3777 
3778         if (strcasecmp(name, "rmw-type") == 0) {
3779                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3780                         un->un_f_rmw_type = val;
3781                 } else {
3782                         goto value_invalid;
3783                 }
3784                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3785                     "RMW type set to %d\n", un->un_f_rmw_type);
3786         }
3787 
3788         if (strcasecmp(name, "physical-block-size") == 0) {
3789                 if (ddi_strtol(value, &endptr, 0, &val) == 0 &&
3790                     ISP2(val) && val >= un->un_tgt_blocksize &&
3791                     val >= un->un_sys_blocksize) {
3792                         un->un_phy_blocksize = val;
3793                         un->un_f_sdconf_phy_blocksize = TRUE;
3794                 } else {
3795                         goto value_invalid;
3796                 }
3797                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3798                     "physical block size set to %d\n", un->un_phy_blocksize);
3799         }
3800 
3801         if (strcasecmp(name, "slow-io-threshold") == 0) {
3802                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3803                         un->un_slow_io_threshold = (hrtime_t)val * NANOSEC;
3804                 } else {
3805                         un->un_slow_io_threshold =
3806                             (hrtime_t)sd_slow_io_threshold;
3807                         goto value_invalid;
3808                 }
3809                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3810                     "slow IO threshold set to %llu\n",
3811                     un->un_slow_io_threshold);
3812         }
3813 
3814         if (strcasecmp(name, "io-time") == 0) {
3815                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3816                         un->un_io_time = val;
3817                 } else {
3818                         un->un_io_time = sd_io_time;
3819                         goto value_invalid;
3820                 }
3821                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3822                     "IO time set to %llu\n", un->un_io_time);
3823         }
3824 
3825         if (strcasecmp(name, "retries-victim") == 0) {
3826                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3827                         un->un_victim_retry_count = val;
3828                 } else {
3829                         goto value_invalid;
3830                 }
3831                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3832                     "victim retry count set to %d\n",
3833                     un->un_victim_retry_count);
3834                 return;
3835         }
3836 
3837         /*
3838          * Validate the throttle values.
3839          * If any of the numbers are invalid, set everything to defaults.
3840          */
3841         if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
3842             (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
3843             (un->un_min_throttle > un->un_throttle)) {
3844                 un->un_saved_throttle = un->un_throttle = sd_max_throttle;
3845                 un->un_min_throttle = sd_min_throttle;
3846         }
3847 
3848         if (strcasecmp(name, "mmc-gesn-polling") == 0) {
3849                 if (strcasecmp(value, "true") == 0) {
3850                         un->un_f_mmc_gesn_polling = TRUE;
3851                 } else if (strcasecmp(value, "false") == 0) {
3852                         un->un_f_mmc_gesn_polling = FALSE;
3853                 } else {
3854                         goto value_invalid;
3855                 }
3856                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3857                     "mmc-gesn-polling set to %d\n",
3858                     un->un_f_mmc_gesn_polling);
3859         }
3860 
3861         return;
3862 
3863 value_invalid:
3864         SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3865             "value of prop %s is invalid\n", name);
3866 }
3867 
3868 /*
3869  *    Function: sd_get_tunables_from_conf()
3870  *
3871  *
3872  *    This function reads the data list from the sd.conf file and pulls
3873  *    the values that can have numeric values as arguments and places
3874  *    the values in the appropriate sd_tunables member.
3875  *    Since the order of the data list members varies across platforms
3876  *    This function reads them from the data list in a platform specific
3877  *    order and places them into the correct sd_tunable member that is
3878  *    consistent across all platforms.
3879  */
3880 static void
3881 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list,
3882     sd_tunables *values)
3883 {
3884         int i;
3885         int mask;
3886 
3887         bzero(values, sizeof (sd_tunables));
3888 
3889         for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
3890 
3891                 mask = 1 << i;
3892                 if (mask > flags) {
3893                         break;
3894                 }
3895 
3896                 switch (mask & flags) {
3897                 case 0: /* This mask bit not set in flags */
3898                         continue;
3899                 case SD_CONF_BSET_THROTTLE:
3900                         values->sdt_throttle = data_list[i];
3901                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
3902                             "sd_get_tunables_from_conf: throttle = %d\n",
3903                             values->sdt_throttle);
3904                         break;
3905                 case SD_CONF_BSET_CTYPE:
3906                         values->sdt_ctype = data_list[i];
3907                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
3908                             "sd_get_tunables_from_conf: ctype = %d\n",
3909                             values->sdt_ctype);
3910                         break;
3911                 case SD_CONF_BSET_NRR_COUNT:
3912                         values->sdt_not_rdy_retries = data_list[i];
3913                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
3914                             "sd_get_tunables_from_conf: not_rdy_retries = %d\n",
3915                             values->sdt_not_rdy_retries);
3916                         break;
3917                 case SD_CONF_BSET_BSY_RETRY_COUNT:
3918                         values->sdt_busy_retries = data_list[i];
3919                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
3920                             "sd_get_tunables_from_conf: busy_retries = %d\n",
3921                             values->sdt_busy_retries);
3922                         break;
3923                 case SD_CONF_BSET_RST_RETRIES:
3924                         values->sdt_reset_retries = data_list[i];
3925                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
3926                             "sd_get_tunables_from_conf: reset_retries = %d\n",
3927                             values->sdt_reset_retries);
3928                         break;
3929                 case SD_CONF_BSET_RSV_REL_TIME:
3930                         values->sdt_reserv_rel_time = data_list[i];
3931                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
3932                             "sd_get_tunables_from_conf: reserv_rel_time = %d\n",
3933                             values->sdt_reserv_rel_time);
3934                         break;
3935                 case SD_CONF_BSET_MIN_THROTTLE:
3936                         values->sdt_min_throttle = data_list[i];
3937                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
3938                             "sd_get_tunables_from_conf: min_throttle = %d\n",
3939                             values->sdt_min_throttle);
3940                         break;
3941                 case SD_CONF_BSET_DISKSORT_DISABLED:
3942                         values->sdt_disk_sort_dis = data_list[i];
3943                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
3944                             "sd_get_tunables_from_conf: disk_sort_dis = %d\n",
3945                             values->sdt_disk_sort_dis);
3946                         break;
3947                 case SD_CONF_BSET_LUN_RESET_ENABLED:
3948                         values->sdt_lun_reset_enable = data_list[i];
3949                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
3950                             "sd_get_tunables_from_conf: lun_reset_enable = %d"
3951                             "\n", values->sdt_lun_reset_enable);
3952                         break;
3953                 case SD_CONF_BSET_CACHE_IS_NV:
3954                         values->sdt_suppress_cache_flush = data_list[i];
3955                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
3956                             "sd_get_tunables_from_conf: \
3957                             suppress_cache_flush = %d"
3958                             "\n", values->sdt_suppress_cache_flush);
3959                         break;
3960                 case SD_CONF_BSET_PC_DISABLED:
3961                         values->sdt_disk_sort_dis = data_list[i];
3962                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
3963                             "sd_get_tunables_from_conf: power_condition_dis = "
3964                             "%d\n", values->sdt_power_condition_dis);
3965                         break;
3966                 }
3967         }
3968 }
3969 
3970 /*
3971  *    Function: sd_process_sdconf_table
3972  *
3973  * Description: Search the static configuration table for a match on the
3974  *              inquiry vid/pid and update the driver soft state structure
3975  *              according to the table property values for the device.
3976  *
3977  *              The form of a configuration table entry is:
3978  *                <vid+pid>,<flags>,<property-data>
3979  *                "SEAGATE ST42400N",1,0x40000,
3980  *                0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1;
3981  *
3982  *   Arguments: un - driver soft state (unit) structure
3983  */
3984 
3985 static void
3986 sd_process_sdconf_table(struct sd_lun *un)
3987 {
3988         char    *id = NULL;
3989         int     table_index;
3990         int     idlen;
3991 
3992         ASSERT(un != NULL);
3993         for (table_index = 0; table_index < sd_disk_table_size;
3994             table_index++) {
3995                 id = sd_disk_table[table_index].device_id;
3996                 idlen = strlen(id);
3997 
3998                 /*
3999                  * The static configuration table currently does not
4000                  * implement version 10 properties. Additionally,
4001                  * multiple data-property-name entries are not
4002                  * implemented in the static configuration table.
4003                  */
4004                 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4005                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
4006                             "sd_process_sdconf_table: disk %s\n", id);
4007                         sd_set_vers1_properties(un,
4008                             sd_disk_table[table_index].flags,
4009                             sd_disk_table[table_index].properties);
4010                         break;
4011                 }
4012         }
4013 }
4014 
4015 
4016 /*
4017  *    Function: sd_sdconf_id_match
4018  *
4019  * Description: This local function implements a case sensitive vid/pid
4020  *              comparison as well as the boundary cases of wild card and
4021  *              multiple blanks.
4022  *
4023  *              Note: An implicit assumption made here is that the scsi
4024  *              inquiry structure will always keep the vid, pid and
4025  *              revision strings in consecutive sequence, so they can be
4026  *              read as a single string. If this assumption is not the
4027  *              case, a separate string, to be used for the check, needs
4028  *              to be built with these strings concatenated.
4029  *
4030  *   Arguments: un - driver soft state (unit) structure
4031  *              id - table or config file vid/pid
4032  *              idlen  - length of the vid/pid (bytes)
4033  *
4034  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4035  *              SD_FAILURE - Indicates no match with the inquiry vid/pid
4036  */
4037 
4038 static int
4039 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen)
4040 {
4041         struct scsi_inquiry     *sd_inq;
4042         int                     rval = SD_SUCCESS;
4043 
4044         ASSERT(un != NULL);
4045         sd_inq = un->un_sd->sd_inq;
4046         ASSERT(id != NULL);
4047 
4048         /*
4049          * We use the inq_vid as a pointer to a buffer containing the
4050          * vid and pid and use the entire vid/pid length of the table
4051          * entry for the comparison. This works because the inq_pid
4052          * data member follows inq_vid in the scsi_inquiry structure.
4053          */
4054         if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) {
4055                 /*
4056                  * The user id string is compared to the inquiry vid/pid
4057                  * using a case insensitive comparison and ignoring
4058                  * multiple spaces.
4059                  */
4060                 rval = sd_blank_cmp(un, id, idlen);
4061                 if (rval != SD_SUCCESS) {
4062                         /*
4063                          * User id strings that start and end with a "*"
4064                          * are a special case. These do not have a
4065                          * specific vendor, and the product string can
4066                          * appear anywhere in the 16 byte PID portion of
4067                          * the inquiry data. This is a simple strstr()
4068                          * type search for the user id in the inquiry data.
4069                          */
4070                         if ((id[0] == '*') && (id[idlen - 1] == '*')) {
4071                                 char    *pidptr = &id[1];
4072                                 int     i;
4073                                 int     j;
4074                                 int     pidstrlen = idlen - 2;
4075                                 j = sizeof (SD_INQUIRY(un)->inq_pid) -
4076                                     pidstrlen;
4077 
4078                                 if (j < 0) {
4079                                         return (SD_FAILURE);
4080                                 }
4081                                 for (i = 0; i < j; i++) {
4082                                         if (bcmp(&SD_INQUIRY(un)->inq_pid[i],
4083                                             pidptr, pidstrlen) == 0) {
4084                                                 rval = SD_SUCCESS;
4085                                                 break;
4086                                         }
4087                                 }
4088                         }
4089                 }
4090         }
4091         return (rval);
4092 }
4093 
4094 
4095 /*
4096  *    Function: sd_blank_cmp
4097  *
4098  * Description: If the id string starts and ends with a space, treat
4099  *              multiple consecutive spaces as equivalent to a single
4100  *              space. For example, this causes a sd_disk_table entry
4101  *              of " NEC CDROM " to match a device's id string of
4102  *              "NEC       CDROM".
4103  *
4104  *              Note: The success exit condition for this routine is if
4105  *              the pointer to the table entry is '\0' and the cnt of
4106  *              the inquiry length is zero. This will happen if the inquiry
4107  *              string returned by the device is padded with spaces to be
4108  *              exactly 24 bytes in length (8 byte vid + 16 byte pid). The
4109  *              SCSI spec states that the inquiry string is to be padded with
4110  *              spaces.
4111  *
4112  *   Arguments: un - driver soft state (unit) structure
4113  *              id - table or config file vid/pid
4114  *              idlen  - length of the vid/pid (bytes)
4115  *
4116  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4117  *              SD_FAILURE - Indicates no match with the inquiry vid/pid
4118  */
4119 
4120 static int
4121 sd_blank_cmp(struct sd_lun *un, char *id, int idlen)
4122 {
4123         char            *p1;
4124         char            *p2;
4125         int             cnt;
4126         cnt = sizeof (SD_INQUIRY(un)->inq_vid) +
4127             sizeof (SD_INQUIRY(un)->inq_pid);
4128 
4129         ASSERT(un != NULL);
4130         p2 = un->un_sd->sd_inq->inq_vid;
4131         ASSERT(id != NULL);
4132         p1 = id;
4133 
4134         if ((id[0] == ' ') && (id[idlen - 1] == ' ')) {
4135                 /*
4136                  * Note: string p1 is terminated by a NUL but string p2
4137                  * isn't.  The end of p2 is determined by cnt.
4138                  */
4139                 for (;;) {
4140                         /* skip over any extra blanks in both strings */
4141                         while ((*p1 != '\0') && (*p1 == ' ')) {
4142                                 p1++;
4143                         }
4144                         while ((cnt != 0) && (*p2 == ' ')) {
4145                                 p2++;
4146                                 cnt--;
4147                         }
4148 
4149                         /* compare the two strings */
4150                         if ((cnt == 0) ||
4151                             (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) {
4152                                 break;
4153                         }
4154                         while ((cnt > 0) &&
4155                             (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) {
4156                                 p1++;
4157                                 p2++;
4158                                 cnt--;
4159                         }
4160                 }
4161         }
4162 
4163         /* return SD_SUCCESS if both strings match */
4164         return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE);
4165 }
4166 
4167 
4168 /*
4169  *    Function: sd_chk_vers1_data
4170  *
4171  * Description: Verify the version 1 device properties provided by the
4172  *              user via the configuration file
4173  *
4174  *   Arguments: un           - driver soft state (unit) structure
4175  *              flags        - integer mask indicating properties to be set
4176  *              prop_list    - integer list of property values
4177  *              list_len     - number of the elements
4178  *
4179  * Return Code: SD_SUCCESS - Indicates the user provided data is valid
4180  *              SD_FAILURE - Indicates the user provided data is invalid
4181  */
4182 
4183 static int
4184 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
4185     int list_len, char *dataname_ptr)
4186 {
4187         int i;
4188         int mask = 1;
4189         int index = 0;
4190 
4191         ASSERT(un != NULL);
4192 
4193         /* Check for a NULL property name and list */
4194         if (dataname_ptr == NULL) {
4195                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4196                     "sd_chk_vers1_data: NULL data property name.");
4197                 return (SD_FAILURE);
4198         }
4199         if (prop_list == NULL) {
4200                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4201                     "sd_chk_vers1_data: %s NULL data property list.",
4202                     dataname_ptr);
4203                 return (SD_FAILURE);
4204         }
4205 
4206         /* Display a warning if undefined bits are set in the flags */
4207         if (flags & ~SD_CONF_BIT_MASK) {
4208                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4209                     "sd_chk_vers1_data: invalid bits 0x%x in data list %s. "
4210                     "Properties not set.",
4211                     (flags & ~SD_CONF_BIT_MASK), dataname_ptr);
4212                 return (SD_FAILURE);
4213         }
4214 
4215         /*
4216          * Verify the length of the list by identifying the highest bit set
4217          * in the flags and validating that the property list has a length
4218          * up to the index of this bit.
4219          */
4220         for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4221                 if (flags & mask) {
4222                         index++;
4223                 }
4224                 mask = 1 << i;
4225         }
4226         if (list_len < (index + 2)) {
4227                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4228                     "sd_chk_vers1_data: "
4229                     "Data property list %s size is incorrect. "
4230                     "Properties not set.", dataname_ptr);
4231                 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: "
4232                     "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS);
4233                 return (SD_FAILURE);
4234         }
4235         return (SD_SUCCESS);
4236 }
4237 
4238 
4239 /*
4240  *    Function: sd_set_vers1_properties
4241  *
4242  * Description: Set version 1 device properties based on a property list
4243  *              retrieved from the driver configuration file or static
4244  *              configuration table. Version 1 properties have the format:
4245  *
4246  *      <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
4247  *
4248  *              where the prop0 value will be used to set prop0 if bit0
4249  *              is set in the flags
4250  *
4251  *   Arguments: un           - driver soft state (unit) structure
4252  *              flags        - integer mask indicating properties to be set
4253  *              prop_list    - integer list of property values
4254  */
4255 
4256 static void
4257 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list)
4258 {
4259         ASSERT(un != NULL);
4260 
4261         /*
4262          * Set the flag to indicate cache is to be disabled. An attempt
4263          * to disable the cache via sd_cache_control() will be made
4264          * later during attach once the basic initialization is complete.
4265          */
4266         if (flags & SD_CONF_BSET_NOCACHE) {
4267                 un->un_f_opt_disable_cache = TRUE;
4268                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4269                     "sd_set_vers1_properties: caching disabled flag set\n");
4270         }
4271 
4272         /* CD-specific configuration parameters */
4273         if (flags & SD_CONF_BSET_PLAYMSF_BCD) {
4274                 un->un_f_cfg_playmsf_bcd = TRUE;
4275                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4276                     "sd_set_vers1_properties: playmsf_bcd set\n");
4277         }
4278         if (flags & SD_CONF_BSET_READSUB_BCD) {
4279                 un->un_f_cfg_readsub_bcd = TRUE;
4280                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4281                     "sd_set_vers1_properties: readsub_bcd set\n");
4282         }
4283         if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) {
4284                 un->un_f_cfg_read_toc_trk_bcd = TRUE;
4285                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4286                     "sd_set_vers1_properties: read_toc_trk_bcd set\n");
4287         }
4288         if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) {
4289                 un->un_f_cfg_read_toc_addr_bcd = TRUE;
4290                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4291                     "sd_set_vers1_properties: read_toc_addr_bcd set\n");
4292         }
4293         if (flags & SD_CONF_BSET_NO_READ_HEADER) {
4294                 un->un_f_cfg_no_read_header = TRUE;
4295                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4296                     "sd_set_vers1_properties: no_read_header set\n");
4297         }
4298         if (flags & SD_CONF_BSET_READ_CD_XD4) {
4299                 un->un_f_cfg_read_cd_xd4 = TRUE;
4300                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4301                     "sd_set_vers1_properties: read_cd_xd4 set\n");
4302         }
4303 
4304         /* Support for devices which do not have valid/unique serial numbers */
4305         if (flags & SD_CONF_BSET_FAB_DEVID) {
4306                 un->un_f_opt_fab_devid = TRUE;
4307                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4308                     "sd_set_vers1_properties: fab_devid bit set\n");
4309         }
4310 
4311         /* Support for user throttle configuration */
4312         if (flags & SD_CONF_BSET_THROTTLE) {
4313                 ASSERT(prop_list != NULL);
4314                 un->un_saved_throttle = un->un_throttle =
4315                     prop_list->sdt_throttle;
4316                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4317                     "sd_set_vers1_properties: throttle set to %d\n",
4318                     prop_list->sdt_throttle);
4319         }
4320 
4321         /* Set the per disk retry count according to the conf file or table. */
4322         if (flags & SD_CONF_BSET_NRR_COUNT) {
4323                 ASSERT(prop_list != NULL);
4324                 if (prop_list->sdt_not_rdy_retries) {
4325                         un->un_notready_retry_count =
4326                             prop_list->sdt_not_rdy_retries;
4327                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
4328                             "sd_set_vers1_properties: not ready retry count"
4329                             " set to %d\n", un->un_notready_retry_count);
4330                 }
4331         }
4332 
4333         /* The controller type is reported for generic disk driver ioctls */
4334         if (flags & SD_CONF_BSET_CTYPE) {
4335                 ASSERT(prop_list != NULL);
4336                 switch (prop_list->sdt_ctype) {
4337                 case CTYPE_CDROM:
4338                         un->un_ctype = prop_list->sdt_ctype;
4339                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
4340                             "sd_set_vers1_properties: ctype set to "
4341                             "CTYPE_CDROM\n");
4342                         break;
4343                 case CTYPE_CCS:
4344                         un->un_ctype = prop_list->sdt_ctype;
4345                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
4346                             "sd_set_vers1_properties: ctype set to "
4347                             "CTYPE_CCS\n");
4348                         break;
4349                 case CTYPE_ROD:         /* RW optical */
4350                         un->un_ctype = prop_list->sdt_ctype;
4351                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
4352                             "sd_set_vers1_properties: ctype set to "
4353                             "CTYPE_ROD\n");
4354                         break;
4355                 default:
4356                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4357                             "sd_set_vers1_properties: Could not set "
4358                             "invalid ctype value (%d)",
4359                             prop_list->sdt_ctype);
4360                 }
4361         }
4362 
4363         /* Purple failover timeout */
4364         if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) {
4365                 ASSERT(prop_list != NULL);
4366                 un->un_busy_retry_count =
4367                     prop_list->sdt_busy_retries;
4368                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4369                     "sd_set_vers1_properties: "
4370                     "busy retry count set to %d\n",
4371                     un->un_busy_retry_count);
4372         }
4373 
4374         /* Purple reset retry count */
4375         if (flags & SD_CONF_BSET_RST_RETRIES) {
4376                 ASSERT(prop_list != NULL);
4377                 un->un_reset_retry_count =
4378                     prop_list->sdt_reset_retries;
4379                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4380                     "sd_set_vers1_properties: "
4381                     "reset retry count set to %d\n",
4382                     un->un_reset_retry_count);
4383         }
4384 
4385         /* Purple reservation release timeout */
4386         if (flags & SD_CONF_BSET_RSV_REL_TIME) {
4387                 ASSERT(prop_list != NULL);
4388                 un->un_reserve_release_time =
4389                     prop_list->sdt_reserv_rel_time;
4390                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4391                     "sd_set_vers1_properties: "
4392                     "reservation release timeout set to %d\n",
4393                     un->un_reserve_release_time);
4394         }
4395 
4396         /*
4397          * Driver flag telling the driver to verify that no commands are pending
4398          * for a device before issuing a Test Unit Ready. This is a workaround
4399          * for a firmware bug in some Seagate eliteI drives.
4400          */
4401         if (flags & SD_CONF_BSET_TUR_CHECK) {
4402                 un->un_f_cfg_tur_check = TRUE;
4403                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4404                     "sd_set_vers1_properties: tur queue check set\n");
4405         }
4406 
4407         if (flags & SD_CONF_BSET_MIN_THROTTLE) {
4408                 un->un_min_throttle = prop_list->sdt_min_throttle;
4409                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4410                     "sd_set_vers1_properties: min throttle set to %d\n",
4411                     un->un_min_throttle);
4412         }
4413 
4414         if (flags & SD_CONF_BSET_DISKSORT_DISABLED) {
4415                 un->un_f_disksort_disabled =
4416                     (prop_list->sdt_disk_sort_dis != 0) ?
4417                     TRUE : FALSE;
4418                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4419                     "sd_set_vers1_properties: disksort disabled "
4420                     "flag set to %d\n",
4421                     prop_list->sdt_disk_sort_dis);
4422         }
4423 
4424         if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) {
4425                 un->un_f_lun_reset_enabled =
4426                     (prop_list->sdt_lun_reset_enable != 0) ?
4427                     TRUE : FALSE;
4428                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4429                     "sd_set_vers1_properties: lun reset enabled "
4430                     "flag set to %d\n",
4431                     prop_list->sdt_lun_reset_enable);
4432         }
4433 
4434         if (flags & SD_CONF_BSET_CACHE_IS_NV) {
4435                 un->un_f_suppress_cache_flush =
4436                     (prop_list->sdt_suppress_cache_flush != 0) ?
4437                     TRUE : FALSE;
4438                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4439                     "sd_set_vers1_properties: suppress_cache_flush "
4440                     "flag set to %d\n",
4441                     prop_list->sdt_suppress_cache_flush);
4442         }
4443 
4444         if (flags & SD_CONF_BSET_PC_DISABLED) {
4445                 un->un_f_power_condition_disabled =
4446                     (prop_list->sdt_power_condition_dis != 0) ?
4447                     TRUE : FALSE;
4448                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4449                     "sd_set_vers1_properties: power_condition_disabled "
4450                     "flag set to %d\n",
4451                     prop_list->sdt_power_condition_dis);
4452         }
4453 
4454         /*
4455          * Validate the throttle values.
4456          * If any of the numbers are invalid, set everything to defaults.
4457          */
4458         if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4459             (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4460             (un->un_min_throttle > un->un_throttle)) {
4461                 un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4462                 un->un_min_throttle = sd_min_throttle;
4463         }
4464 }
4465 
4466 /*
4467  *   Function: sd_is_lsi()
4468  *
4469  *   Description: Check for lsi devices, step through the static device
4470  *      table to match vid/pid.
4471  *
4472  *   Args: un - ptr to sd_lun
4473  *
4474  *   Notes:  When creating new LSI property, need to add the new LSI property
4475  *              to this function.
4476  */
4477 static void
4478 sd_is_lsi(struct sd_lun *un)
4479 {
4480         char    *id = NULL;
4481         int     table_index;
4482         int     idlen;
4483         void    *prop;
4484 
4485         ASSERT(un != NULL);
4486         for (table_index = 0; table_index < sd_disk_table_size;
4487             table_index++) {
4488                 id = sd_disk_table[table_index].device_id;
4489                 idlen = strlen(id);
4490                 if (idlen == 0) {
4491                         continue;
4492                 }
4493 
4494                 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4495                         prop = sd_disk_table[table_index].properties;
4496                         if (prop == &lsi_properties ||
4497                             prop == &lsi_oem_properties ||
4498                             prop == &lsi_properties_scsi ||
4499                             prop == &symbios_properties) {
4500                                 un->un_f_cfg_is_lsi = TRUE;
4501                         }
4502                         break;
4503                 }
4504         }
4505 }
4506 
4507 /*
4508  *    Function: sd_get_physical_geometry
4509  *
4510  * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and
4511  *              MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the
4512  *              target, and use this information to initialize the physical
4513  *              geometry cache specified by pgeom_p.
4514  *
4515  *              MODE SENSE is an optional command, so failure in this case
4516  *              does not necessarily denote an error. We want to use the
4517  *              MODE SENSE commands to derive the physical geometry of the
4518  *              device, but if either command fails, the logical geometry is
4519  *              used as the fallback for disk label geometry in cmlb.
4520  *
4521  *              This requires that un->un_blockcount and un->un_tgt_blocksize
4522  *              have already been initialized for the current target and
4523  *              that the current values be passed as args so that we don't
4524  *              end up ever trying to use -1 as a valid value. This could
4525  *              happen if either value is reset while we're not holding
4526  *              the mutex.
4527  *
4528  *   Arguments: un - driver soft state (unit) structure
4529  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4530  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4531  *                      to use the USCSI "direct" chain and bypass the normal
4532  *                      command waitq.
4533  *
4534  *     Context: Kernel thread only (can sleep).
4535  */
4536 
4537 static int
4538 sd_get_physical_geometry(struct sd_lun *un, cmlb_geom_t *pgeom_p,
4539     diskaddr_t capacity, int lbasize, int path_flag)
4540 {
4541         struct  mode_format     *page3p;
4542         struct  mode_geometry   *page4p;
4543         struct  mode_header     *headerp;
4544         int     sector_size;
4545         int     nsect;
4546         int     nhead;
4547         int     ncyl;
4548         int     intrlv;
4549         int     spc;
4550         diskaddr_t      modesense_capacity;
4551         int     rpm;
4552         int     bd_len;
4553         int     mode_header_length;
4554         uchar_t *p3bufp;
4555         uchar_t *p4bufp;
4556         int     cdbsize;
4557         int     ret = EIO;
4558         sd_ssc_t *ssc;
4559         int     status;
4560 
4561         ASSERT(un != NULL);
4562 
4563         if (lbasize == 0) {
4564                 if (ISCD(un)) {
4565                         lbasize = 2048;
4566                 } else {
4567                         lbasize = un->un_sys_blocksize;
4568                 }
4569         }
4570         pgeom_p->g_secsize = (unsigned short)lbasize;
4571 
4572         /*
4573          * If the unit is a cd/dvd drive MODE SENSE page three
4574          * and MODE SENSE page four are reserved (see SBC spec
4575          * and MMC spec). To prevent soft errors just return
4576          * using the default LBA size.
4577          *
4578          * Since SATA MODE SENSE function (sata_txlt_mode_sense()) does not
4579          * implement support for mode pages 3 and 4 return here to prevent
4580          * illegal requests on SATA drives.
4581          *
4582          * These pages are also reserved in SBC-2 and later.  We assume SBC-2
4583          * or later for a direct-attached block device if the SCSI version is
4584          * at least SPC-3.
4585          */
4586         if (ISCD(un) ||
4587             un->un_interconnect_type == SD_INTERCONNECT_SATA ||
4588             (un->un_ctype == CTYPE_CCS && SD_INQUIRY(un)->inq_ansi >= 5))
4589                 return (ret);
4590 
4591         cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0;
4592 
4593         /*
4594          * Retrieve MODE SENSE page 3 - Format Device Page
4595          */
4596         p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP);
4597         ssc = sd_ssc_init(un);
4598         status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p3bufp,
4599             SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag);
4600         if (status != 0) {
4601                 SD_ERROR(SD_LOG_COMMON, un,
4602                     "sd_get_physical_geometry: mode sense page 3 failed\n");
4603                 goto page3_exit;
4604         }
4605 
4606         /*
4607          * Determine size of Block Descriptors in order to locate the mode
4608          * page data.  ATAPI devices return 0, SCSI devices should return
4609          * MODE_BLK_DESC_LENGTH.
4610          */
4611         headerp = (struct mode_header *)p3bufp;
4612         if (un->un_f_cfg_is_atapi == TRUE) {
4613                 struct mode_header_grp2 *mhp =
4614                     (struct mode_header_grp2 *)headerp;
4615                 mode_header_length = MODE_HEADER_LENGTH_GRP2;
4616                 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
4617         } else {
4618                 mode_header_length = MODE_HEADER_LENGTH;
4619                 bd_len = ((struct mode_header *)headerp)->bdesc_length;
4620         }
4621 
4622         if (bd_len > MODE_BLK_DESC_LENGTH) {
4623                 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
4624                     "sd_get_physical_geometry: received unexpected bd_len "
4625                     "of %d, page3\n", bd_len);
4626                 status = EIO;
4627                 goto page3_exit;
4628         }
4629 
4630         page3p = (struct mode_format *)
4631             ((caddr_t)headerp + mode_header_length + bd_len);
4632 
4633         if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) {
4634                 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
4635                     "sd_get_physical_geometry: mode sense pg3 code mismatch "
4636                     "%d\n", page3p->mode_page.code);
4637                 status = EIO;
4638                 goto page3_exit;
4639         }
4640 
4641         /*
4642          * Use this physical geometry data only if BOTH MODE SENSE commands
4643          * complete successfully; otherwise, revert to the logical geometry.
4644          * So, we need to save everything in temporary variables.
4645          */
4646         sector_size = BE_16(page3p->data_bytes_sect);
4647 
4648         /*
4649          * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size
4650          */
4651         if (sector_size == 0) {
4652                 sector_size = un->un_sys_blocksize;
4653         } else {
4654                 sector_size &= ~(un->un_sys_blocksize - 1);
4655         }
4656 
4657         nsect  = BE_16(page3p->sect_track);
4658         intrlv = BE_16(page3p->interleave);
4659 
4660         SD_INFO(SD_LOG_COMMON, un,
4661             "sd_get_physical_geometry: Format Parameters (page 3)\n");
4662         SD_INFO(SD_LOG_COMMON, un,
4663             "   mode page: %d; nsect: %d; sector size: %d;\n",
4664             page3p->mode_page.code, nsect, sector_size);
4665         SD_INFO(SD_LOG_COMMON, un,
4666             "   interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv,
4667             BE_16(page3p->track_skew),
4668             BE_16(page3p->cylinder_skew));
4669 
4670         sd_ssc_assessment(ssc, SD_FMT_STANDARD);
4671 
4672         /*
4673          * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page
4674          */
4675         p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP);
4676         status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p4bufp,
4677             SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag);
4678         if (status != 0) {
4679                 SD_ERROR(SD_LOG_COMMON, un,
4680                     "sd_get_physical_geometry: mode sense page 4 failed\n");
4681                 goto page4_exit;
4682         }
4683 
4684         /*
4685          * Determine size of Block Descriptors in order to locate the mode
4686          * page data.  ATAPI devices return 0, SCSI devices should return
4687          * MODE_BLK_DESC_LENGTH.
4688          */
4689         headerp = (struct mode_header *)p4bufp;
4690         if (un->un_f_cfg_is_atapi == TRUE) {
4691                 struct mode_header_grp2 *mhp =
4692                     (struct mode_header_grp2 *)headerp;
4693                 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
4694         } else {
4695                 bd_len = ((struct mode_header *)headerp)->bdesc_length;
4696         }
4697 
4698         if (bd_len > MODE_BLK_DESC_LENGTH) {
4699                 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
4700                     "sd_get_physical_geometry: received unexpected bd_len of "
4701                     "%d, page4\n", bd_len);
4702                 status = EIO;
4703                 goto page4_exit;
4704         }
4705 
4706         page4p = (struct mode_geometry *)
4707             ((caddr_t)headerp + mode_header_length + bd_len);
4708 
4709         if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) {
4710                 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
4711                     "sd_get_physical_geometry: mode sense pg4 code mismatch "
4712                     "%d\n", page4p->mode_page.code);
4713                 status = EIO;
4714                 goto page4_exit;
4715         }
4716 
4717         /*
4718          * Stash the data now, after we know that both commands completed.
4719          */
4720 
4721 
4722         nhead = (int)page4p->heads;  /* uchar, so no conversion needed */
4723         spc   = nhead * nsect;
4724         ncyl  = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb;
4725         rpm   = BE_16(page4p->rpm);
4726 
4727         modesense_capacity = spc * ncyl;
4728 
4729         SD_INFO(SD_LOG_COMMON, un,
4730             "sd_get_physical_geometry: Geometry Parameters (page 4)\n");
4731         SD_INFO(SD_LOG_COMMON, un,
4732             "   cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm);
4733         SD_INFO(SD_LOG_COMMON, un,
4734             "   computed capacity(h*s*c): %d;\n", modesense_capacity);
4735         SD_INFO(SD_LOG_COMMON, un, "   pgeom_p: %p; read cap: %d\n",
4736             (void *)pgeom_p, capacity);
4737 
4738         /*
4739          * Compensate if the drive's geometry is not rectangular, i.e.,
4740          * the product of C * H * S returned by MODE SENSE >= that returned
4741          * by read capacity. This is an idiosyncrasy of the original x86
4742          * disk subsystem.
4743          */
4744         if (modesense_capacity >= capacity) {
4745                 SD_INFO(SD_LOG_COMMON, un,
4746                     "sd_get_physical_geometry: adjusting acyl; "
4747                     "old: %d; new: %d\n", pgeom_p->g_acyl,
4748                     (modesense_capacity - capacity + spc - 1) / spc);
4749                 if (sector_size != 0) {
4750                         /* 1243403: NEC D38x7 drives don't support sec size */
4751                         pgeom_p->g_secsize = (unsigned short)sector_size;
4752                 }
4753                 pgeom_p->g_nsect    = (unsigned short)nsect;
4754                 pgeom_p->g_nhead    = (unsigned short)nhead;
4755                 pgeom_p->g_capacity = capacity;
4756                 pgeom_p->g_acyl          =
4757                     (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc;
4758                 pgeom_p->g_ncyl          = ncyl - pgeom_p->g_acyl;
4759         }
4760 
4761         pgeom_p->g_rpm    = (unsigned short)rpm;
4762         pgeom_p->g_intrlv = (unsigned short)intrlv;
4763         ret = 0;
4764 
4765         SD_INFO(SD_LOG_COMMON, un,
4766             "sd_get_physical_geometry: mode sense geometry:\n");
4767         SD_INFO(SD_LOG_COMMON, un,
4768             "   nsect: %d; sector size: %d; interlv: %d\n",
4769             nsect, sector_size, intrlv);
4770         SD_INFO(SD_LOG_COMMON, un,
4771             "   nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n",
4772             nhead, ncyl, rpm, modesense_capacity);
4773         SD_INFO(SD_LOG_COMMON, un,
4774             "sd_get_physical_geometry: (cached)\n");
4775         SD_INFO(SD_LOG_COMMON, un,
4776             "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
4777             pgeom_p->g_ncyl,  pgeom_p->g_acyl,
4778             pgeom_p->g_nhead, pgeom_p->g_nsect);
4779         SD_INFO(SD_LOG_COMMON, un,
4780             "   lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n",
4781             pgeom_p->g_secsize, pgeom_p->g_capacity,
4782             pgeom_p->g_intrlv, pgeom_p->g_rpm);
4783         sd_ssc_assessment(ssc, SD_FMT_STANDARD);
4784 
4785 page4_exit:
4786         kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH);
4787 
4788 page3_exit:
4789         kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH);
4790 
4791         if (status != 0) {
4792                 if (status == EIO) {
4793                         /*
4794                          * Some disks do not support mode sense(6), we
4795                          * should ignore this kind of error(sense key is
4796                          * 0x5 - illegal request).
4797                          */
4798                         uint8_t *sensep;
4799                         int senlen;
4800 
4801                         sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
4802                         senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
4803                             ssc->ssc_uscsi_cmd->uscsi_rqresid);
4804 
4805                         if (senlen > 0 &&
4806                             scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
4807                                 sd_ssc_assessment(ssc,
4808                                     SD_FMT_IGNORE_COMPROMISE);
4809                         } else {
4810                                 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
4811                         }
4812                 } else {
4813                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
4814                 }
4815         }
4816         sd_ssc_fini(ssc);
4817         return (ret);
4818 }
4819 
4820 /*
4821  *    Function: sd_get_virtual_geometry
4822  *
4823  * Description: Ask the controller to tell us about the target device.
4824  *
4825  *   Arguments: un - pointer to softstate
4826  *              capacity - disk capacity in #blocks
4827  *              lbasize - disk block size in bytes
4828  *
4829  *     Context: Kernel thread only
4830  */
4831 
4832 static int
4833 sd_get_virtual_geometry(struct sd_lun *un, cmlb_geom_t *lgeom_p,
4834     diskaddr_t capacity, int lbasize)
4835 {
4836         uint_t  geombuf;
4837         int     spc;
4838 
4839         ASSERT(un != NULL);
4840 
4841         /* Set sector size, and total number of sectors */
4842         (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size",   lbasize,  1);
4843         (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1);
4844 
4845         /* Let the HBA tell us its geometry */
4846         geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1);
4847 
4848         /* A value of -1 indicates an undefined "geometry" property */
4849         if (geombuf == (-1)) {
4850                 return (EINVAL);
4851         }
4852 
4853         /* Initialize the logical geometry cache. */
4854         lgeom_p->g_nhead   = (geombuf >> 16) & 0xffff;
4855         lgeom_p->g_nsect   = geombuf & 0xffff;
4856         lgeom_p->g_secsize = un->un_sys_blocksize;
4857 
4858         spc = lgeom_p->g_nhead * lgeom_p->g_nsect;
4859 
4860         /*
4861          * Note: The driver originally converted the capacity value from
4862          * target blocks to system blocks. However, the capacity value passed
4863          * to this routine is already in terms of system blocks (this scaling
4864          * is done when the READ CAPACITY command is issued and processed).
4865          * This 'error' may have gone undetected because the usage of g_ncyl
4866          * (which is based upon g_capacity) is very limited within the driver
4867          */
4868         lgeom_p->g_capacity = capacity;
4869 
4870         /*
4871          * Set ncyl to zero if the hba returned a zero nhead or nsect value. The
4872          * hba may return zero values if the device has been removed.
4873          */
4874         if (spc == 0) {
4875                 lgeom_p->g_ncyl = 0;
4876         } else {
4877                 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc;
4878         }
4879         lgeom_p->g_acyl = 0;
4880 
4881         SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n");
4882         return (0);
4883 
4884 }
4885 /*
4886  *    Function: sd_update_block_info
4887  *
4888  * Description: Calculate a byte count to sector count bitshift value
4889  *              from sector size.
4890  *
4891  *   Arguments: un: unit struct.
4892  *              lbasize: new target sector size
4893  *              capacity: new target capacity, ie. block count
4894  *
4895  *     Context: Kernel thread context
4896  */
4897 
4898 static void
4899 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity)
4900 {
4901         if (lbasize != 0) {
4902                 un->un_tgt_blocksize = lbasize;
4903                 un->un_f_tgt_blocksize_is_valid = TRUE;
4904                 if (!un->un_f_has_removable_media) {
4905                         un->un_sys_blocksize = lbasize;
4906                 }
4907         }
4908 
4909         if (capacity != 0) {
4910                 un->un_blockcount            = capacity;
4911                 un->un_f_blockcount_is_valid = TRUE;
4912 
4913                 /*
4914                  * The capacity has changed so update the errstats.
4915                  */
4916                 if (un->un_errstats != NULL) {
4917                         struct sd_errstats *stp;
4918 
4919                         capacity *= un->un_sys_blocksize;
4920                         stp = (struct sd_errstats *)un->un_errstats->ks_data;
4921                         if (stp->sd_capacity.value.ui64 < capacity)
4922                                 stp->sd_capacity.value.ui64 = capacity;
4923                 }
4924         }
4925 }
4926 
4927 /*
4928  * Parses the SCSI Block Limits VPD page (0xB0). It's legal to pass NULL for
4929  * vpd_pg, in which case all the block limits will be reset to the defaults.
4930  */
4931 static void
4932 sd_parse_blk_limits_vpd(struct sd_lun *un, uchar_t *vpd_pg)
4933 {
4934         sd_blk_limits_t *lim = &un->un_blk_lim;
4935         unsigned pg_len;
4936 
4937         if (vpd_pg != NULL)
4938                 pg_len = BE_IN16(&vpd_pg[2]);
4939         else
4940                 pg_len = 0;
4941 
4942         /* Block Limits VPD can be 16 bytes or 64 bytes long - support both */
4943         if (pg_len >= 0x10) {
4944                 lim->lim_opt_xfer_len_gran = BE_IN16(&vpd_pg[6]);
4945                 lim->lim_max_xfer_len = BE_IN32(&vpd_pg[8]);
4946                 lim->lim_opt_xfer_len = BE_IN32(&vpd_pg[12]);
4947         } else {
4948                 lim->lim_opt_xfer_len_gran = 0;
4949                 lim->lim_max_xfer_len = UINT32_MAX;
4950                 lim->lim_opt_xfer_len = UINT32_MAX;
4951         }
4952         if (pg_len >= 0x3c) {
4953                 lim->lim_max_pfetch_len = BE_IN32(&vpd_pg[16]);
4954                 /*
4955                  * A zero in either of the following two fields indicates lack
4956                  * of UNMAP support.
4957                  */
4958                 lim->lim_max_unmap_lba_cnt = BE_IN32(&vpd_pg[20]);
4959                 lim->lim_max_unmap_descr_cnt = BE_IN32(&vpd_pg[24]);
4960                 lim->lim_opt_unmap_gran = BE_IN32(&vpd_pg[28]);
4961                 if ((vpd_pg[32] >> 7) == 1) {
4962                         /* left-most bit on each byte is a flag */
4963                         lim->lim_unmap_gran_align =
4964                             ((vpd_pg[32] & 0x7f) << 24) | (vpd_pg[33] << 16) |
4965                             (vpd_pg[34] << 8) | vpd_pg[35];
4966                 } else {
4967                         lim->lim_unmap_gran_align = 0;
4968                 }
4969                 lim->lim_max_write_same_len = BE_IN64(&vpd_pg[36]);
4970         } else {
4971                 lim->lim_max_pfetch_len = UINT32_MAX;
4972                 lim->lim_max_unmap_lba_cnt = UINT32_MAX;
4973                 lim->lim_max_unmap_descr_cnt = SD_UNMAP_MAX_DESCR;
4974                 lim->lim_opt_unmap_gran = 0;
4975                 lim->lim_unmap_gran_align = 0;
4976                 lim->lim_max_write_same_len = UINT64_MAX;
4977         }
4978 }
4979 
4980 /*
4981  * Collects VPD page B0 data if available (block limits). If the data is
4982  * not available or querying the device failed, we revert to the defaults.
4983  */
4984 static void
4985 sd_setup_blk_limits(sd_ssc_t *ssc)
4986 {
4987         struct sd_lun   *un             = ssc->ssc_un;
4988         uchar_t         *inqB0          = NULL;
4989         size_t          inqB0_resid     = 0;
4990         int             rval;
4991 
4992         if (un->un_vpd_page_mask & SD_VPD_BLK_LIMITS_PG) {
4993                 inqB0 = kmem_zalloc(MAX_INQUIRY_SIZE, KM_SLEEP);
4994                 rval = sd_send_scsi_INQUIRY(ssc, inqB0, MAX_INQUIRY_SIZE, 0x01,
4995                     0xB0, &inqB0_resid);
4996                 if (rval != 0) {
4997                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
4998                         kmem_free(inqB0, MAX_INQUIRY_SIZE);
4999                         inqB0 = NULL;
5000                 }
5001         }
5002         /* passing NULL inqB0 will reset to defaults */
5003         sd_parse_blk_limits_vpd(ssc->ssc_un, inqB0);
5004         if (inqB0)
5005                 kmem_free(inqB0, MAX_INQUIRY_SIZE);
5006 }
5007 
5008 #define DEVID_IF_KNOWN(d) "devid", DATA_TYPE_STRING, (d) ? (d) : "unknown"
5009 
5010 /*
5011  *    Function: sd_register_devid
5012  *
5013  * Description: This routine will obtain the device id information from the
5014  *              target, obtain the serial number, and register the device
5015  *              id with the ddi framework.
5016  *
5017  *   Arguments: devi - the system's dev_info_t for the device.
5018  *              un - driver soft state (unit) structure
5019  *              reservation_flag - indicates if a reservation conflict
5020  *              occurred during attach
5021  *
5022  *     Context: Kernel Thread
5023  */
5024 static void
5025 sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, int reservation_flag)
5026 {
5027         int             rval            = 0;
5028         uchar_t         *inq80          = NULL;
5029         size_t          inq80_len       = MAX_INQUIRY_SIZE;
5030         size_t          inq80_resid     = 0;
5031         uchar_t         *inq83          = NULL;
5032         size_t          inq83_len       = MAX_INQUIRY_SIZE;
5033         size_t          inq83_resid     = 0;
5034         int             dlen, len;
5035         char            *sn;
5036         struct sd_lun   *un;
5037 
5038         ASSERT(ssc != NULL);
5039         un = ssc->ssc_un;
5040         ASSERT(un != NULL);
5041         ASSERT(mutex_owned(SD_MUTEX(un)));
5042         ASSERT((SD_DEVINFO(un)) == devi);
5043 
5044 
5045         /*
5046          * We check the availability of the World Wide Name (0x83) and Unit
5047          * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using
5048          * un_vpd_page_mask from them, we decide which way to get the WWN.  If
5049          * 0x83 is available, that is the best choice.  Our next choice is
5050          * 0x80.  If neither are available, we munge the devid from the device
5051          * vid/pid/serial # for Sun qualified disks, or use the ddi framework
5052          * to fabricate a devid for non-Sun qualified disks.
5053          */
5054         if (sd_check_vpd_page_support(ssc) == 0) {
5055                 /* collect page 80 data if available */
5056                 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) {
5057 
5058                         mutex_exit(SD_MUTEX(un));
5059                         inq80 = kmem_zalloc(inq80_len, KM_SLEEP);
5060 
5061                         rval = sd_send_scsi_INQUIRY(ssc, inq80, inq80_len,
5062                             0x01, 0x80, &inq80_resid);
5063 
5064                         if (rval != 0) {
5065                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5066                                 kmem_free(inq80, inq80_len);
5067                                 inq80 = NULL;
5068                                 inq80_len = 0;
5069                         } else if (ddi_prop_exists(
5070                             DDI_DEV_T_NONE, SD_DEVINFO(un),
5071                             DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
5072                             INQUIRY_SERIAL_NO) == 0) {
5073                                 /*
5074                                  * If we don't already have a serial number
5075                                  * property, do quick verify of data returned
5076                                  * and define property.
5077                                  */
5078                                 dlen = inq80_len - inq80_resid;
5079                                 len = (size_t)inq80[3];
5080                                 if ((dlen >= 4) && ((len + 4) <= dlen)) {
5081                                         /*
5082                                          * Ensure sn termination, skip leading
5083                                          * blanks, and create property
5084                                          * 'inquiry-serial-no'.
5085                                          */
5086                                         sn = (char *)&inq80[4];
5087                                         sn[len] = 0;
5088                                         while (*sn && (*sn == ' '))
5089                                                 sn++;
5090                                         if (*sn) {
5091                                                 (void) ddi_prop_update_string(
5092                                                     DDI_DEV_T_NONE,
5093                                                     SD_DEVINFO(un),
5094                                                     INQUIRY_SERIAL_NO, sn);
5095                                         }
5096                                 }
5097                         }
5098                         mutex_enter(SD_MUTEX(un));
5099                 }
5100 
5101                 /* collect page 83 data if available */
5102                 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) {
5103                         mutex_exit(SD_MUTEX(un));
5104                         inq83 = kmem_zalloc(inq83_len, KM_SLEEP);
5105 
5106                         rval = sd_send_scsi_INQUIRY(ssc, inq83, inq83_len,
5107                             0x01, 0x83, &inq83_resid);
5108 
5109                         if (rval != 0) {
5110                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5111                                 kmem_free(inq83, inq83_len);
5112                                 inq83 = NULL;
5113                                 inq83_len = 0;
5114                         }
5115                         mutex_enter(SD_MUTEX(un));
5116                 }
5117         }
5118 
5119         /*
5120          * If transport has already registered a devid for this target
5121          * then that takes precedence over the driver's determination
5122          * of the devid.
5123          *
5124          * NOTE: The reason this check is done here instead of at the beginning
5125          * of the function is to allow the code above to create the
5126          * 'inquiry-serial-no' property.
5127          */
5128         if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) {
5129                 ASSERT(un->un_devid);
5130                 un->un_f_devid_transport_defined = TRUE;
5131                 goto cleanup; /* use devid registered by the transport */
5132         }
5133 
5134         /*
5135          * This is the case of antiquated Sun disk drives that have the
5136          * FAB_DEVID property set in the disk_table.  These drives
5137          * manage the devid's by storing them in last 2 available sectors
5138          * on the drive and have them fabricated by the ddi layer by calling
5139          * ddi_devid_init and passing the DEVID_FAB flag.
5140          */
5141         if (un->un_f_opt_fab_devid == TRUE &&
5142             reservation_flag != SD_TARGET_IS_RESERVED) {
5143                 if (sd_get_devid(ssc) == EINVAL)
5144                         /*
5145                          * The devid is invalid AND there is no reservation
5146                          * conflict.  Fabricate a new devid.
5147                          */
5148                         (void) sd_create_devid(ssc);
5149 
5150                 /* Register the devid if it exists */
5151                 if (un->un_devid != NULL) {
5152                         (void) ddi_devid_register(SD_DEVINFO(un),
5153                             un->un_devid);
5154                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
5155                             "sd_register_devid: Devid Fabricated\n");
5156                 }
5157                 goto cleanup;
5158         }
5159 
5160         /* encode best devid possible based on data available */
5161         if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST,
5162             (char *)ddi_driver_name(SD_DEVINFO(un)),
5163             (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)),
5164             inq80, inq80_len - inq80_resid, inq83, inq83_len -
5165             inq83_resid, &un->un_devid) == DDI_SUCCESS) {
5166 
5167                 /* devid successfully encoded, register devid */
5168                 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid);
5169 
5170         } else if (reservation_flag != SD_TARGET_IS_RESERVED) {
5171                 /*
5172                  * Unable to encode a devid based on data available.
5173                  * This is not a Sun qualified disk.  Older Sun disk
5174                  * drives that have the SD_FAB_DEVID property
5175                  * set in the disk_table and non Sun qualified
5176                  * disks are treated in the same manner.  These
5177                  * drives manage the devid's by storing them in
5178                  * last 2 available sectors on the drive and
5179                  * have them fabricated by the ddi layer by
5180                  * calling ddi_devid_init and passing the
5181                  * DEVID_FAB flag.
5182                  * Create a fabricate devid only if there's no
5183                  * fabricate devid existed.
5184                  */
5185                 if (sd_get_devid(ssc) == EINVAL) {
5186                         (void) sd_create_devid(ssc);
5187                 }
5188                 un->un_f_opt_fab_devid = TRUE;
5189 
5190                 /* Register the devid if it exists */
5191                 if (un->un_devid != NULL) {
5192                         (void) ddi_devid_register(SD_DEVINFO(un),
5193                             un->un_devid);
5194                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
5195                             "sd_register_devid: devid fabricated using "
5196                             "ddi framework\n");
5197                 }
5198         }
5199 
5200 cleanup:
5201         /* clean up resources */
5202         if (inq80 != NULL) {
5203                 kmem_free(inq80, inq80_len);
5204         }
5205         if (inq83 != NULL) {
5206                 kmem_free(inq83, inq83_len);
5207         }
5208 }
5209 
5210 
5211 
5212 /*
5213  *    Function: sd_get_devid
5214  *
5215  * Description: This routine will return 0 if a valid device id has been
5216  *              obtained from the target and stored in the soft state. If a
5217  *              valid device id has not been previously read and stored, a
5218  *              read attempt will be made.
5219  *
5220  *   Arguments: un - driver soft state (unit) structure
5221  *
5222  * Return Code: 0 if we successfully get the device id
5223  *
5224  *     Context: Kernel Thread
5225  */
5226 
5227 static int
5228 sd_get_devid(sd_ssc_t *ssc)
5229 {
5230         struct dk_devid         *dkdevid;
5231         ddi_devid_t             tmpid;
5232         uint_t                  *ip;
5233         size_t                  sz;
5234         diskaddr_t              blk;
5235         int                     status;
5236         int                     chksum;
5237         int                     i;
5238         size_t                  buffer_size;
5239         struct sd_lun           *un;
5240 
5241         ASSERT(ssc != NULL);
5242         un = ssc->ssc_un;
5243         ASSERT(un != NULL);
5244         ASSERT(mutex_owned(SD_MUTEX(un)));
5245 
5246         SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n",
5247             un);
5248 
5249         if (un->un_devid != NULL) {
5250                 return (0);
5251         }
5252 
5253         mutex_exit(SD_MUTEX(un));
5254         if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5255             (void *)SD_PATH_DIRECT) != 0) {
5256                 mutex_enter(SD_MUTEX(un));
5257                 return (EINVAL);
5258         }
5259 
5260         /*
5261          * Read and verify device id, stored in the reserved cylinders at the
5262          * end of the disk. Backup label is on the odd sectors of the last
5263          * track of the last cylinder. Device id will be on track of the next
5264          * to last cylinder.
5265          */
5266         mutex_enter(SD_MUTEX(un));
5267         buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid));
5268         mutex_exit(SD_MUTEX(un));
5269         dkdevid = kmem_alloc(buffer_size, KM_SLEEP);
5270         status = sd_send_scsi_READ(ssc, dkdevid, buffer_size, blk,
5271             SD_PATH_DIRECT);
5272 
5273         if (status != 0) {
5274                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5275                 goto error;
5276         }
5277 
5278         /* Validate the revision */
5279         if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
5280             (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
5281                 status = EINVAL;
5282                 goto error;
5283         }
5284 
5285         /* Calculate the checksum */
5286         chksum = 0;
5287         ip = (uint_t *)dkdevid;
5288         for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5289             i++) {
5290                 chksum ^= ip[i];
5291         }
5292 
5293         /* Compare the checksums */
5294         if (DKD_GETCHKSUM(dkdevid) != chksum) {
5295                 status = EINVAL;
5296                 goto error;
5297         }
5298 
5299         /* Validate the device id */
5300         if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
5301                 status = EINVAL;
5302                 goto error;
5303         }
5304 
5305         /*
5306          * Store the device id in the driver soft state
5307          */
5308         sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
5309         tmpid = kmem_alloc(sz, KM_SLEEP);
5310 
5311         mutex_enter(SD_MUTEX(un));
5312 
5313         un->un_devid = tmpid;
5314         bcopy(&dkdevid->dkd_devid, un->un_devid, sz);
5315 
5316         kmem_free(dkdevid, buffer_size);
5317 
5318         SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un);
5319 
5320         return (status);
5321 error:
5322         mutex_enter(SD_MUTEX(un));
5323         kmem_free(dkdevid, buffer_size);
5324         return (status);
5325 }
5326 
5327 
5328 /*
5329  *    Function: sd_create_devid
5330  *
5331  * Description: This routine will fabricate the device id and write it
5332  *              to the disk.
5333  *
5334  *   Arguments: un - driver soft state (unit) structure
5335  *
5336  * Return Code: value of the fabricated device id
5337  *
5338  *     Context: Kernel Thread
5339  */
5340 
5341 static ddi_devid_t
5342 sd_create_devid(sd_ssc_t *ssc)
5343 {
5344         struct sd_lun   *un;
5345 
5346         ASSERT(ssc != NULL);
5347         un = ssc->ssc_un;
5348         ASSERT(un != NULL);
5349 
5350         /* Fabricate the devid */
5351         if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid)
5352             == DDI_FAILURE) {
5353                 return (NULL);
5354         }
5355 
5356         /* Write the devid to disk */
5357         if (sd_write_deviceid(ssc) != 0) {
5358                 ddi_devid_free(un->un_devid);
5359                 un->un_devid = NULL;
5360         }
5361 
5362         return (un->un_devid);
5363 }
5364 
5365 
5366 /*
5367  *    Function: sd_write_deviceid
5368  *
5369  * Description: This routine will write the device id to the disk
5370  *              reserved sector.
5371  *
5372  *   Arguments: un - driver soft state (unit) structure
5373  *
5374  * Return Code: EINVAL
5375  *              value returned by sd_send_scsi_cmd
5376  *
5377  *     Context: Kernel Thread
5378  */
5379 
5380 static int
5381 sd_write_deviceid(sd_ssc_t *ssc)
5382 {
5383         struct dk_devid         *dkdevid;
5384         uchar_t                 *buf;
5385         diskaddr_t              blk;
5386         uint_t                  *ip, chksum;
5387         int                     status;
5388         int                     i;
5389         struct sd_lun           *un;
5390 
5391         ASSERT(ssc != NULL);
5392         un = ssc->ssc_un;
5393         ASSERT(un != NULL);
5394         ASSERT(mutex_owned(SD_MUTEX(un)));
5395 
5396         mutex_exit(SD_MUTEX(un));
5397         if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5398             (void *)SD_PATH_DIRECT) != 0) {
5399                 mutex_enter(SD_MUTEX(un));
5400                 return (-1);
5401         }
5402 
5403 
5404         /* Allocate the buffer */
5405         buf = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP);
5406         dkdevid = (struct dk_devid *)buf;
5407 
5408         /* Fill in the revision */
5409         dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
5410         dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
5411 
5412         /* Copy in the device id */
5413         mutex_enter(SD_MUTEX(un));
5414         bcopy(un->un_devid, &dkdevid->dkd_devid,
5415             ddi_devid_sizeof(un->un_devid));
5416         mutex_exit(SD_MUTEX(un));
5417 
5418         /* Calculate the checksum */
5419         chksum = 0;
5420         ip = (uint_t *)dkdevid;
5421         for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5422             i++) {
5423                 chksum ^= ip[i];
5424         }
5425 
5426         /* Fill-in checksum */
5427         DKD_FORMCHKSUM(chksum, dkdevid);
5428 
5429         /* Write the reserved sector */
5430         status = sd_send_scsi_WRITE(ssc, buf, un->un_sys_blocksize, blk,
5431             SD_PATH_DIRECT);
5432         if (status != 0)
5433                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5434 
5435         kmem_free(buf, un->un_sys_blocksize);
5436 
5437         mutex_enter(SD_MUTEX(un));
5438         return (status);
5439 }
5440 
5441 
5442 /*
5443  *    Function: sd_check_vpd_page_support
5444  *
5445  * Description: This routine sends an inquiry command with the EVPD bit set and
5446  *              a page code of 0x00 to the device. It is used to determine which
5447  *              vital product pages are available to find the devid. We are
5448  *              looking for pages 0x83 0x80 or 0xB1.  If we return a negative 1,
5449  *              the device does not support that command.
5450  *
5451  *   Arguments: un  - driver soft state (unit) structure
5452  *
5453  * Return Code: 0 - success
5454  *              1 - check condition
5455  *
5456  *     Context: This routine can sleep.
5457  */
5458 
5459 static int
5460 sd_check_vpd_page_support(sd_ssc_t *ssc)
5461 {
5462         uchar_t *page_list      = NULL;
5463         uchar_t page_length     = 0xff; /* Use max possible length */
5464         uchar_t evpd            = 0x01; /* Set the EVPD bit */
5465         uchar_t page_code       = 0x00; /* Supported VPD Pages */
5466         int     rval            = 0;
5467         int     counter;
5468         struct sd_lun           *un;
5469 
5470         ASSERT(ssc != NULL);
5471         un = ssc->ssc_un;
5472         ASSERT(un != NULL);
5473         ASSERT(mutex_owned(SD_MUTEX(un)));
5474 
5475         mutex_exit(SD_MUTEX(un));
5476 
5477         /*
5478          * We'll set the page length to the maximum to save figuring it out
5479          * with an additional call.
5480          */
5481         page_list =  kmem_zalloc(page_length, KM_SLEEP);
5482 
5483         rval = sd_send_scsi_INQUIRY(ssc, page_list, page_length, evpd,
5484             page_code, NULL);
5485 
5486         if (rval != 0)
5487                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5488 
5489         mutex_enter(SD_MUTEX(un));
5490 
5491         /*
5492          * Now we must validate that the device accepted the command, as some
5493          * drives do not support it.  If the drive does support it, we will
5494          * return 0, and the supported pages will be in un_vpd_page_mask.  If
5495          * not, we return -1.
5496          */
5497         if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) {
5498                 /* Loop to find one of the 2 pages we need */
5499                 counter = 4;  /* Supported pages start at byte 4, with 0x00 */
5500 
5501                 /*
5502                  * Pages are returned in ascending order, and 0x83 is what we
5503                  * are hoping for.
5504                  */
5505                 while ((page_list[counter] <= 0xB1) &&
5506                     (counter <= (page_list[VPD_PAGE_LENGTH] +
5507                     VPD_HEAD_OFFSET))) {
5508                         /*
5509                          * Add 3 because page_list[3] is the number of
5510                          * pages minus 3
5511                          */
5512 
5513                         switch (page_list[counter]) {
5514                         case 0x00:
5515                                 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG;
5516                                 break;
5517                         case 0x80:
5518                                 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG;
5519                                 break;
5520                         case 0x81:
5521                                 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG;
5522                                 break;
5523                         case 0x82:
5524                                 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG;
5525                                 break;
5526                         case 0x83:
5527                                 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG;
5528                                 break;
5529                         case 0x86:
5530                                 un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG;
5531                                 break;
5532                         case 0xB0:
5533                                 un->un_vpd_page_mask |= SD_VPD_BLK_LIMITS_PG;
5534                                 break;
5535                         case 0xB1:
5536                                 un->un_vpd_page_mask |= SD_VPD_DEV_CHARACTER_PG;
5537                                 break;
5538                         }
5539                         counter++;
5540                 }
5541 
5542         } else {
5543                 rval = -1;
5544 
5545                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
5546                     "sd_check_vpd_page_support: This drive does not implement "
5547                     "VPD pages.\n");
5548         }
5549 
5550         kmem_free(page_list, page_length);
5551 
5552         return (rval);
5553 }
5554 
5555 
5556 /*
5557  *    Function: sd_setup_pm
5558  *
5559  * Description: Initialize Power Management on the device
5560  *
5561  *     Context: Kernel Thread
5562  */
5563 #ifdef notyet
5564 static void
5565 sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi)
5566 {
5567         uint_t          log_page_size;
5568         uchar_t         *log_page_data;
5569         int             rval = 0;
5570         struct sd_lun   *un;
5571 
5572         ASSERT(ssc != NULL);
5573         un = ssc->ssc_un;
5574         ASSERT(un != NULL);
5575 
5576         /*
5577          * Since we are called from attach, holding a mutex for
5578          * un is unnecessary. Because some of the routines called
5579          * from here require SD_MUTEX to not be held, assert this
5580          * right up front.
5581          */
5582         ASSERT(!mutex_owned(SD_MUTEX(un)));
5583         /*
5584          * Since the sd device does not have the 'reg' property,
5585          * cpr will not call its DDI_SUSPEND/DDI_RESUME entries.
5586          * The following code is to tell cpr that this device
5587          * DOES need to be suspended and resumed.
5588          */
5589         (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
5590             "pm-hardware-state", "needs-suspend-resume");
5591 
5592         /*
5593          * This complies with the new power management framework
5594          * for certain desktop machines. Create the pm_components
5595          * property as a string array property.
5596          * If un_f_pm_supported is TRUE, that means the disk
5597          * attached HBA has set the "pm-capable" property and
5598          * the value of this property is bigger than 0.
5599          */
5600         if (un->un_f_pm_supported) {
5601                 /*
5602                  * not all devices have a motor, try it first.
5603                  * some devices may return ILLEGAL REQUEST, some
5604                  * will hang
5605                  * The following START_STOP_UNIT is used to check if target
5606                  * device has a motor.
5607                  */
5608                 un->un_f_start_stop_supported = TRUE;
5609 
5610                 if (un->un_f_power_condition_supported) {
5611                         rval = sd_send_scsi_START_STOP_UNIT(ssc,
5612                             SD_POWER_CONDITION, SD_TARGET_ACTIVE,
5613                             SD_PATH_DIRECT);
5614                         if (rval != 0) {
5615                                 un->un_f_power_condition_supported = FALSE;
5616                         }
5617                 }
5618                 /* WTF? this fails for optical drives with no media */
5619                 if (!un->un_f_power_condition_supported) {
5620                         rval = sd_send_scsi_START_STOP_UNIT(ssc,
5621                             SD_START_STOP, SD_TARGET_START, SD_PATH_DIRECT);
5622                 }
5623                 if (rval != 0) {
5624                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5625                         un->un_f_start_stop_supported = FALSE;
5626                 }
5627 
5628                 /*
5629                  * create pm properties anyways otherwise the parent can't
5630                  * go to sleep
5631                  */
5632                 un->un_f_pm_is_enabled = TRUE;
5633                 (void) sd_create_pm_components(devi, un);
5634 
5635                 /*
5636                  * If it claims that log sense is supported, check it out.
5637                  */
5638                 if (un->un_f_log_sense_supported) {
5639                         rval = sd_log_page_supported(ssc,
5640                             START_STOP_CYCLE_PAGE);
5641                         if (rval == 1) {
5642                                 /* Page found, use it. */
5643                                 un->un_start_stop_cycle_page =
5644                                     START_STOP_CYCLE_PAGE;
5645                         } else {
5646                                 /*
5647                                  * Page not found or log sense is not
5648                                  * supported.
5649                                  * Notice we do not check the old style
5650                                  * START_STOP_CYCLE_VU_PAGE because this
5651                                  * code path does not apply to old disks.
5652                                  */
5653                                 un->un_f_log_sense_supported = FALSE;
5654                                 un->un_f_pm_log_sense_smart = FALSE;
5655                         }
5656                 }
5657 
5658                 return;
5659         }
5660 
5661         /*
5662          * For the disk whose attached HBA has not set the "pm-capable"
5663          * property, check if it supports the power management.
5664          */
5665         if (!un->un_f_log_sense_supported) {
5666                 un->un_power_level = SD_SPINDLE_ON;
5667                 un->un_f_pm_is_enabled = FALSE;
5668                 return;
5669         }
5670 
5671         rval = sd_log_page_supported(ssc, START_STOP_CYCLE_PAGE);
5672 
5673 #ifdef  SDDEBUG
5674         if (sd_force_pm_supported) {
5675                 /* Force a successful result */
5676                 rval = 1;
5677         }
5678 #endif
5679 
5680         /*
5681          * If the start-stop cycle counter log page is not supported
5682          * or if the pm-capable property is set to be false (0),
5683          * then we should not create the pm_components property.
5684          */
5685         if (rval == -1) {
5686                 /*
5687                  * Error.
5688                  * Reading log sense failed, most likely this is
5689                  * an older drive that does not support log sense.
5690                  * If this fails auto-pm is not supported.
5691                  */
5692                 un->un_power_level = SD_SPINDLE_ON;
5693                 un->un_f_pm_is_enabled = FALSE;
5694 
5695         } else if (rval == 0) {
5696                 /*
5697                  * Page not found.
5698                  * The start stop cycle counter is implemented as page
5699                  * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For
5700                  * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE).
5701                  */
5702                 if (sd_log_page_supported(ssc, START_STOP_CYCLE_VU_PAGE) == 1) {
5703                         /*
5704                          * Page found, use this one.
5705                          */
5706                         un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE;
5707                         un->un_f_pm_is_enabled = TRUE;
5708                 } else {
5709                         /*
5710                          * Error or page not found.
5711                          * auto-pm is not supported for this device.
5712                          */
5713                         un->un_power_level = SD_SPINDLE_ON;
5714                         un->un_f_pm_is_enabled = FALSE;
5715                 }
5716         } else {
5717                 /*
5718                  * Page found, use it.
5719                  */
5720                 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE;
5721                 un->un_f_pm_is_enabled = TRUE;
5722         }
5723 
5724 
5725         if (un->un_f_pm_is_enabled == TRUE) {
5726                 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
5727                 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
5728 
5729                 rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
5730                     log_page_size, un->un_start_stop_cycle_page,
5731                     0x01, 0, SD_PATH_DIRECT);
5732 
5733                 if (rval != 0) {
5734                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5735                 }
5736 
5737 #ifdef  SDDEBUG
5738                 if (sd_force_pm_supported) {
5739                         /* Force a successful result */
5740                         rval = 0;
5741                 }
5742 #endif
5743 
5744                 /*
5745                  * If the Log sense for Page( Start/stop cycle counter page)
5746                  * succeeds, then power management is supported and we can
5747                  * enable auto-pm.
5748                  */
5749                 if (rval == 0)  {
5750                         (void) sd_create_pm_components(devi, un);
5751                 } else {
5752                         un->un_power_level = SD_SPINDLE_ON;
5753                         un->un_f_pm_is_enabled = FALSE;
5754                 }
5755 
5756                 kmem_free(log_page_data, log_page_size);
5757         }
5758 }
5759 
5760 
5761 /*
5762  *    Function: sd_create_pm_components
5763  *
5764  * Description: Initialize PM property.
5765  *
5766  *     Context: Kernel thread context
5767  */
5768 
5769 static void
5770 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un)
5771 {
5772         ASSERT(!mutex_owned(SD_MUTEX(un)));
5773 
5774         if (un->un_f_power_condition_supported) {
5775                 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
5776                     "pm-components", sd_pwr_pc.pm_comp, 5)
5777                     != DDI_PROP_SUCCESS) {
5778                         un->un_power_level = SD_SPINDLE_ACTIVE;
5779                         un->un_f_pm_is_enabled = FALSE;
5780                         return;
5781                 }
5782         } else {
5783                 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
5784                     "pm-components", sd_pwr_ss.pm_comp, 3)
5785                     != DDI_PROP_SUCCESS) {
5786                         un->un_power_level = SD_SPINDLE_ON;
5787                         un->un_f_pm_is_enabled = FALSE;
5788                         return;
5789                 }
5790         }
5791         /*
5792          * When components are initially created they are idle,
5793          * power up any non-removables.
5794          * Note: the return value of pm_raise_power can't be used
5795          * for determining if PM should be enabled for this device.
5796          * Even if you check the return values and remove this
5797          * property created above, the PM framework will not honor the
5798          * change after the first call to pm_raise_power. Hence,
5799          * removal of that property does not help if pm_raise_power
5800          * fails. In the case of removable media, the start/stop
5801          * will fail if the media is not present.
5802          */
5803         if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0,
5804             SD_PM_STATE_ACTIVE(un)) == DDI_SUCCESS)) {
5805                 mutex_enter(SD_MUTEX(un));
5806                 un->un_power_level = SD_PM_STATE_ACTIVE(un);
5807                 mutex_enter(&un->un_pm_mutex);
5808                 /* Set to on and not busy. */
5809                 un->un_pm_count = 0;
5810         } else {
5811                 mutex_enter(SD_MUTEX(un));
5812                 un->un_power_level = SD_PM_STATE_STOPPED(un);
5813                 mutex_enter(&un->un_pm_mutex);
5814                 /* Set to off. */
5815                 un->un_pm_count = -1;
5816         }
5817         mutex_exit(&un->un_pm_mutex);
5818         mutex_exit(SD_MUTEX(un));
5819 }
5820 #endif
5821 
5822 /*
5823  *    Function: sd_ddi_suspend
5824  *
5825  * Description: Performs system power-down operations. This includes
5826  *              setting the drive state to indicate its suspended so
5827  *              that no new commands will be accepted. Also, wait for
5828  *              all commands that are in transport or queued to a timer
5829  *              for retry to complete. All timeout threads are cancelled.
5830  *
5831  * Return Code: DDI_FAILURE or DDI_SUCCESS
5832  *
5833  *     Context: Kernel thread context
5834  */
5835 
5836 static int
5837 sd_ddi_suspend(dev_info_t *devi)
5838 {
5839         struct  sd_lun  *un;
5840         clock_t         wait_cmds_complete;
5841 
5842         un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
5843         if (un == NULL) {
5844                 return (DDI_FAILURE);
5845         }
5846 
5847         SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n");
5848 
5849         mutex_enter(SD_MUTEX(un));
5850 
5851         /* Return success if the device is already suspended. */
5852         if (un->un_state == SD_STATE_SUSPENDED) {
5853                 mutex_exit(SD_MUTEX(un));
5854                 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
5855                     "device already suspended, exiting\n");
5856                 return (DDI_SUCCESS);
5857         }
5858 
5859         /* Return failure if the device is being used by HA */
5860         if (un->un_resvd_status &
5861             (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) {
5862                 mutex_exit(SD_MUTEX(un));
5863                 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
5864                     "device in use by HA, exiting\n");
5865                 return (DDI_FAILURE);
5866         }
5867 
5868         /*
5869          * Return failure if the device is in a resource wait
5870          * or power changing state.
5871          */
5872         if ((un->un_state == SD_STATE_RWAIT) ||
5873             (un->un_state == SD_STATE_PM_CHANGING)) {
5874                 mutex_exit(SD_MUTEX(un));
5875                 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
5876                     "device in resource wait state, exiting\n");
5877                 return (DDI_FAILURE);
5878         }
5879 
5880 
5881         un->un_save_state = un->un_last_state;
5882         New_state(un, SD_STATE_SUSPENDED);
5883 
5884         /*
5885          * Wait for all commands that are in transport or queued to a timer
5886          * for retry to complete.
5887          *
5888          * While waiting, no new commands will be accepted or sent because of
5889          * the new state we set above.
5890          *
5891          * Wait till current operation has completed. If we are in the resource
5892          * wait state (with an intr outstanding) then we need to wait till the
5893          * intr completes and starts the next cmd. We want to wait for
5894          * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND.
5895          */
5896         wait_cmds_complete = ddi_get_lbolt() +
5897             (sd_wait_cmds_complete * drv_usectohz(1000000));
5898 
5899         while (un->un_ncmds_in_transport != 0) {
5900                 /*
5901                  * Fail if commands do not finish in the specified time.
5902                  */
5903                 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un),
5904                     wait_cmds_complete) == -1) {
5905                         /*
5906                          * Undo the state changes made above. Everything
5907                          * must go back to it's original value.
5908                          */
5909                         Restore_state(un);
5910                         un->un_last_state = un->un_save_state;
5911                         /* Wake up any threads that might be waiting. */
5912                         cv_broadcast(&un->un_suspend_cv);
5913                         mutex_exit(SD_MUTEX(un));
5914                         SD_ERROR(SD_LOG_IO_PM, un,
5915                             "sd_ddi_suspend: failed due to outstanding cmds\n");
5916                         SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n");
5917                         return (DDI_FAILURE);
5918                 }
5919         }
5920 
5921         /*
5922          * Cancel SCSI watch thread and timeouts, if any are active
5923          */
5924 
5925         if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) {
5926                 opaque_t temp_token = un->un_swr_token;
5927                 mutex_exit(SD_MUTEX(un));
5928                 scsi_watch_suspend(temp_token);
5929                 mutex_enter(SD_MUTEX(un));
5930         }
5931 
5932         if (un->un_reset_throttle_timeid != NULL) {
5933                 timeout_id_t temp_id = un->un_reset_throttle_timeid;
5934                 un->un_reset_throttle_timeid = NULL;
5935                 mutex_exit(SD_MUTEX(un));
5936                 (void) untimeout(temp_id);
5937                 mutex_enter(SD_MUTEX(un));
5938         }
5939 
5940         if (un->un_dcvb_timeid != NULL) {
5941                 timeout_id_t temp_id = un->un_dcvb_timeid;
5942                 un->un_dcvb_timeid = NULL;
5943                 mutex_exit(SD_MUTEX(un));
5944                 (void) untimeout(temp_id);
5945                 mutex_enter(SD_MUTEX(un));
5946         }
5947 
5948         mutex_enter(&un->un_pm_mutex);
5949         if (un->un_pm_timeid != NULL) {
5950                 timeout_id_t temp_id = un->un_pm_timeid;
5951                 un->un_pm_timeid = NULL;
5952                 mutex_exit(&un->un_pm_mutex);
5953                 mutex_exit(SD_MUTEX(un));
5954                 (void) untimeout(temp_id);
5955                 mutex_enter(SD_MUTEX(un));
5956         } else {
5957                 mutex_exit(&un->un_pm_mutex);
5958         }
5959 
5960         if (un->un_rmw_msg_timeid != NULL) {
5961                 timeout_id_t temp_id = un->un_rmw_msg_timeid;
5962                 un->un_rmw_msg_timeid = NULL;
5963                 mutex_exit(SD_MUTEX(un));
5964                 (void) untimeout(temp_id);
5965                 mutex_enter(SD_MUTEX(un));
5966         }
5967 
5968         if (un->un_retry_timeid != NULL) {
5969                 timeout_id_t temp_id = un->un_retry_timeid;
5970                 un->un_retry_timeid = NULL;
5971                 mutex_exit(SD_MUTEX(un));
5972                 (void) untimeout(temp_id);
5973                 mutex_enter(SD_MUTEX(un));
5974 
5975                 if (un->un_retry_bp != NULL) {
5976                         un->un_retry_bp->av_forw = un->un_waitq_headp;
5977                         un->un_waitq_headp = un->un_retry_bp;
5978                         if (un->un_waitq_tailp == NULL) {
5979                                 un->un_waitq_tailp = un->un_retry_bp;
5980                         }
5981                         un->un_retry_bp = NULL;
5982                         un->un_retry_statp = NULL;
5983                 }
5984         }
5985 
5986         if (un->un_direct_priority_timeid != NULL) {
5987                 timeout_id_t temp_id = un->un_direct_priority_timeid;
5988                 un->un_direct_priority_timeid = NULL;
5989                 mutex_exit(SD_MUTEX(un));
5990                 (void) untimeout(temp_id);
5991                 mutex_enter(SD_MUTEX(un));
5992         }
5993 
5994         mutex_exit(SD_MUTEX(un));
5995 
5996         SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n");
5997 
5998         return (DDI_SUCCESS);
5999 }
6000 
6001 
6002 /*
6003  *    Function: sd_ddi_resume
6004  *
6005  * Description: Performs system power-up operations..
6006  *
6007  * Return Code: DDI_SUCCESS
6008  *              DDI_FAILURE
6009  *
6010  *     Context: Kernel thread context
6011  */
6012 
6013 static int
6014 sd_ddi_resume(dev_info_t *devi)
6015 {
6016         struct  sd_lun  *un;
6017 
6018         un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6019         if (un == NULL) {
6020                 return (DDI_FAILURE);
6021         }
6022 
6023         SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n");
6024 
6025         mutex_enter(SD_MUTEX(un));
6026         Restore_state(un);
6027 
6028         /*
6029          * Restore the state which was saved to give the
6030          * the right state in un_last_state
6031          */
6032         un->un_last_state = un->un_save_state;
6033         /*
6034          * Note: throttle comes back at full.
6035          * Also note: this MUST be done before calling pm_raise_power
6036          * otherwise the system can get hung in biowait. The scenario where
6037          * this'll happen is under cpr suspend. Writing of the system
6038          * state goes through sddump, which writes 0 to un_throttle. If
6039          * writing the system state then fails, example if the partition is
6040          * too small, then cpr attempts a resume. If throttle isn't restored
6041          * from the saved value until after calling pm_raise_power then
6042          * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs
6043          * in biowait.
6044          */
6045         un->un_throttle = un->un_saved_throttle;
6046 
6047         /*
6048          * The chance of failure is very rare as the only command done in power
6049          * entry point is START command when you transition from 0->1 or
6050          * unknown->1. Put it to SPINDLE ON state irrespective of the state at
6051          * which suspend was done. Ignore the return value as the resume should
6052          * not be failed. In the case of removable media the media need not be
6053          * inserted and hence there is a chance that raise power will fail with
6054          * media not present.
6055          */
6056         if (un->un_f_attach_spinup) {
6057                 mutex_exit(SD_MUTEX(un));
6058                 (void) pm_raise_power(SD_DEVINFO(un), 0,
6059                     SD_PM_STATE_ACTIVE(un));
6060                 mutex_enter(SD_MUTEX(un));
6061         }
6062 
6063         /*
6064          * Don't broadcast to the suspend cv and therefore possibly
6065          * start I/O until after power has been restored.
6066          */
6067         cv_broadcast(&un->un_suspend_cv);
6068         cv_broadcast(&un->un_state_cv);
6069 
6070         /* restart thread */
6071         if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) {
6072                 scsi_watch_resume(un->un_swr_token);
6073         }
6074 
6075         /*
6076          * Transport any pending commands to the target.
6077          *
6078          * If this is a low-activity device commands in queue will have to wait
6079          * until new commands come in, which may take awhile. Also, we
6080          * specifically don't check un_ncmds_in_transport because we know that
6081          * there really are no commands in progress after the unit was
6082          * suspended and we could have reached the throttle level, been
6083          * suspended, and have no new commands coming in for awhile. Highly
6084          * unlikely, but so is the low-activity disk scenario.
6085          */
6086         ddi_xbuf_dispatch(un->un_xbuf_attr);
6087 
6088         sd_start_cmds(un, NULL);
6089         mutex_exit(SD_MUTEX(un));
6090 
6091         SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n");
6092 
6093         return (DDI_SUCCESS);
6094 }
6095 
6096 
6097 /*
6098  *    Function: sd_pm_state_change
6099  *
6100  * Description: Change the driver power state.
6101  *              Someone else is required to actually change the driver
6102  *              power level.
6103  *
6104  *   Arguments: un - driver soft state (unit) structure
6105  *              level - the power level that is changed to
6106  *              flag - to decide how to change the power state
6107  *
6108  * Return Code: DDI_SUCCESS
6109  *
6110  *     Context: Kernel thread context
6111  */
6112 static int
6113 sd_pm_state_change(struct sd_lun *un, int level, int flag)
6114 {
6115         ASSERT(un != NULL);
6116         SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: entry\n");
6117 
6118         ASSERT(!mutex_owned(SD_MUTEX(un)));
6119         mutex_enter(SD_MUTEX(un));
6120 
6121         if (flag == SD_PM_STATE_ROLLBACK || SD_PM_IS_IO_CAPABLE(un, level)) {
6122                 un->un_power_level = level;
6123                 ASSERT(!mutex_owned(&un->un_pm_mutex));
6124                 mutex_enter(&un->un_pm_mutex);
6125                 if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
6126                         un->un_pm_count++;
6127                         ASSERT(un->un_pm_count == 0);
6128                 }
6129                 mutex_exit(&un->un_pm_mutex);
6130         } else {
6131                 /*
6132                  * Exit if power management is not enabled for this device,
6133                  * or if the device is being used by HA.
6134                  */
6135                 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status &
6136                     (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) {
6137                         mutex_exit(SD_MUTEX(un));
6138                         SD_TRACE(SD_LOG_POWER, un,
6139                             "sd_pm_state_change: exiting\n");
6140                         return (DDI_FAILURE);
6141                 }
6142 
6143                 SD_INFO(SD_LOG_POWER, un, "sd_pm_state_change: "
6144                     "un_ncmds_in_driver=%ld\n", un->un_ncmds_in_driver);
6145 
6146                 /*
6147                  * See if the device is not busy, ie.:
6148                  *    - we have no commands in the driver for this device
6149                  *    - not waiting for resources
6150                  */
6151                 if ((un->un_ncmds_in_driver == 0) &&
6152                     (un->un_state != SD_STATE_RWAIT)) {
6153                         /*
6154                          * The device is not busy, so it is OK to go to low
6155                          * power state. Indicate low power, but rely on someone
6156                          * else to actually change it.
6157                          */
6158                         mutex_enter(&un->un_pm_mutex);
6159                         un->un_pm_count = -1;
6160                         mutex_exit(&un->un_pm_mutex);
6161                         un->un_power_level = level;
6162                 }
6163         }
6164 
6165         mutex_exit(SD_MUTEX(un));
6166 
6167         SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: exit\n");
6168 
6169         return (DDI_SUCCESS);
6170 }
6171 
6172 
6173 /*
6174  *    Function: sd_pm_idletimeout_handler
6175  *
6176  * Description: A timer routine that's active only while a device is busy.
6177  *              The purpose is to extend slightly the pm framework's busy
6178  *              view of the device to prevent busy/idle thrashing for
6179  *              back-to-back commands. Do this by comparing the current time
6180  *              to the time at which the last command completed and when the
6181  *              difference is greater than sd_pm_idletime, call
6182  *              pm_idle_component. In addition to indicating idle to the pm
6183  *              framework, update the chain type to again use the internal pm
6184  *              layers of the driver.
6185  *
6186  *   Arguments: arg - driver soft state (unit) structure
6187  *
6188  *     Context: Executes in a timeout(9F) thread context
6189  */
6190 
6191 static void
6192 sd_pm_idletimeout_handler(void *arg)
6193 {
6194         const hrtime_t idletime = sd_pm_idletime * NANOSEC;
6195         struct sd_lun *un = arg;
6196 
6197         mutex_enter(&sd_detach_mutex);
6198         if (un->un_detach_count != 0) {
6199                 /* Abort if the instance is detaching */
6200                 mutex_exit(&sd_detach_mutex);
6201                 return;
6202         }
6203         mutex_exit(&sd_detach_mutex);
6204 
6205         /*
6206          * Grab both mutexes, in the proper order, since we're accessing
6207          * both PM and softstate variables.
6208          */
6209         mutex_enter(SD_MUTEX(un));
6210         mutex_enter(&un->un_pm_mutex);
6211         if (((gethrtime() - un->un_pm_idle_time) > idletime) &&
6212             (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) {
6213                 /*
6214                  * Update the chain types.
6215                  * This takes affect on the next new command received.
6216                  */
6217                 if (un->un_f_non_devbsize_supported) {
6218                         un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
6219                 } else {
6220                         un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
6221                 }
6222                 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD;
6223 
6224                 SD_TRACE(SD_LOG_IO_PM, un,
6225                     "sd_pm_idletimeout_handler: idling device\n");
6226                 (void) pm_idle_component(SD_DEVINFO(un), 0);
6227                 un->un_pm_idle_timeid = NULL;
6228         } else {
6229                 un->un_pm_idle_timeid =
6230                     timeout(sd_pm_idletimeout_handler, un,
6231                     (drv_usectohz((clock_t)300000))); /* 300 ms. */
6232         }
6233         mutex_exit(&un->un_pm_mutex);
6234         mutex_exit(SD_MUTEX(un));
6235 }
6236 
6237 
6238 /*
6239  *    Function: sd_pm_timeout_handler
6240  *
6241  * Description: Callback to tell framework we are idle.
6242  *
6243  *     Context: timeout(9f) thread context.
6244  */
6245 
6246 static void
6247 sd_pm_timeout_handler(void *arg)
6248 {
6249         struct sd_lun *un = arg;
6250 
6251         (void) pm_idle_component(SD_DEVINFO(un), 0);
6252         mutex_enter(&un->un_pm_mutex);
6253         un->un_pm_timeid = NULL;
6254         mutex_exit(&un->un_pm_mutex);
6255 }
6256 
6257 
6258 /*
6259  *    Function: sdpower
6260  *
6261  * Description: PM entry point.
6262  *
6263  * Return Code: DDI_SUCCESS
6264  *              DDI_FAILURE
6265  *
6266  *     Context: Kernel thread context
6267  */
6268 
6269 static int
6270 sdpower(dev_info_t *devi, int component, int level)
6271 {
6272         struct sd_lun   *un;
6273         int             instance;
6274         int             rval = DDI_SUCCESS;
6275         uint_t          i, log_page_size, maxcycles, ncycles;
6276         uchar_t         *log_page_data;
6277         int             log_sense_page;
6278         int             medium_present;
6279         time_t          intvlp;
6280         struct pm_trans_data    sd_pm_tran_data;
6281         uchar_t         save_state = SD_STATE_NORMAL;
6282         int             sval;
6283         uchar_t         state_before_pm;
6284         int             got_semaphore_here;
6285         sd_ssc_t        *ssc;
6286         int     last_power_level = SD_SPINDLE_UNINIT;
6287 
6288         instance = ddi_get_instance(devi);
6289 
6290         if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
6291             !SD_PM_IS_LEVEL_VALID(un, level) || component != 0) {
6292                 return (DDI_FAILURE);
6293         }
6294 
6295         ssc = sd_ssc_init(un);
6296 
6297         SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level);
6298 
6299         /*
6300          * Must synchronize power down with close.
6301          * Attempt to decrement/acquire the open/close semaphore,
6302          * but do NOT wait on it. If it's not greater than zero,
6303          * ie. it can't be decremented without waiting, then
6304          * someone else, either open or close, already has it
6305          * and the try returns 0. Use that knowledge here to determine
6306          * if it's OK to change the device power level.
6307          * Also, only increment it on exit if it was decremented, ie. gotten,
6308          * here.
6309          */
6310         got_semaphore_here = sema_tryp(&un->un_semoclose);
6311 
6312         mutex_enter(SD_MUTEX(un));
6313 
6314         SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n",
6315             un->un_ncmds_in_driver);
6316 
6317         /*
6318          * If un_ncmds_in_driver is non-zero it indicates commands are
6319          * already being processed in the driver, or if the semaphore was
6320          * not gotten here it indicates an open or close is being processed.
6321          * At the same time somebody is requesting to go to a lower power
6322          * that can't perform I/O, which can't happen, therefore we need to
6323          * return failure.
6324          */
6325         if ((!SD_PM_IS_IO_CAPABLE(un, level)) &&
6326             ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) {
6327                 mutex_exit(SD_MUTEX(un));
6328 
6329                 if (got_semaphore_here != 0) {
6330                         sema_v(&un->un_semoclose);
6331                 }
6332                 SD_TRACE(SD_LOG_IO_PM, un,
6333                     "sdpower: exit, device has queued cmds.\n");
6334 
6335                 goto sdpower_failed;
6336         }
6337 
6338         /*
6339          * if it is OFFLINE that means the disk is completely dead
6340          * in our case we have to put the disk in on or off by sending commands
6341          * Of course that will fail anyway so return back here.
6342          *
6343          * Power changes to a device that's OFFLINE or SUSPENDED
6344          * are not allowed.
6345          */
6346         if ((un->un_state == SD_STATE_OFFLINE) ||
6347             (un->un_state == SD_STATE_SUSPENDED)) {
6348                 mutex_exit(SD_MUTEX(un));
6349 
6350                 if (got_semaphore_here != 0) {
6351                         sema_v(&un->un_semoclose);
6352                 }
6353                 SD_TRACE(SD_LOG_IO_PM, un,
6354                     "sdpower: exit, device is off-line.\n");
6355 
6356                 goto sdpower_failed;
6357         }
6358 
6359         /*
6360          * Change the device's state to indicate it's power level
6361          * is being changed. Do this to prevent a power off in the
6362          * middle of commands, which is especially bad on devices
6363          * that are really powered off instead of just spun down.
6364          */
6365         state_before_pm = un->un_state;
6366         un->un_state = SD_STATE_PM_CHANGING;
6367 
6368         mutex_exit(SD_MUTEX(un));
6369 
6370         /*
6371          * If log sense command is not supported, bypass the
6372          * following checking, otherwise, check the log sense
6373          * information for this device.
6374          */
6375         if (SD_PM_STOP_MOTOR_NEEDED(un, level) &&
6376             un->un_f_log_sense_supported) {
6377                 /*
6378                  * Get the log sense information to understand whether the
6379                  * the powercycle counts have gone beyond the threshhold.
6380                  */
6381                 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6382                 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6383 
6384                 mutex_enter(SD_MUTEX(un));
6385                 log_sense_page = un->un_start_stop_cycle_page;
6386                 mutex_exit(SD_MUTEX(un));
6387 
6388                 rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
6389                     log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT);
6390 
6391                 if (rval != 0) {
6392                         if (rval == EIO)
6393                                 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
6394                         else
6395                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6396                 }
6397 
6398 #ifdef  SDDEBUG
6399                 if (sd_force_pm_supported) {
6400                         /* Force a successful result */
6401                         rval = 0;
6402                 }
6403 #endif
6404                 if (rval != 0) {
6405                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
6406                             "Log Sense Failed\n");
6407 
6408                         kmem_free(log_page_data, log_page_size);
6409                         /* Cannot support power management on those drives */
6410 
6411                         if (got_semaphore_here != 0) {
6412                                 sema_v(&un->un_semoclose);
6413                         }
6414                         /*
6415                          * On exit put the state back to it's original value
6416                          * and broadcast to anyone waiting for the power
6417                          * change completion.
6418                          */
6419                         mutex_enter(SD_MUTEX(un));
6420                         un->un_state = state_before_pm;
6421                         cv_broadcast(&un->un_suspend_cv);
6422                         mutex_exit(SD_MUTEX(un));
6423                         SD_TRACE(SD_LOG_IO_PM, un,
6424                             "sdpower: exit, Log Sense Failed.\n");
6425 
6426                         goto sdpower_failed;
6427                 }
6428 
6429                 /*
6430                  * From the page data - Convert the essential information to
6431                  * pm_trans_data
6432                  */
6433                 maxcycles =
6434                     (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) |
6435                     (log_page_data[0x1E] << 8)  | log_page_data[0x1F];
6436 
6437                 ncycles =
6438                     (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) |
6439                     (log_page_data[0x26] << 8)  | log_page_data[0x27];
6440 
6441                 if (un->un_f_pm_log_sense_smart) {
6442                         sd_pm_tran_data.un.smart_count.allowed = maxcycles;
6443                         sd_pm_tran_data.un.smart_count.consumed = ncycles;
6444                         sd_pm_tran_data.un.smart_count.flag = 0;
6445                         sd_pm_tran_data.format = DC_SMART_FORMAT;
6446                 } else {
6447                         sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles;
6448                         sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles;
6449                         for (i = 0; i < DC_SCSI_MFR_LEN; i++) {
6450                                 sd_pm_tran_data.un.scsi_cycles.svc_date[i] =
6451                                     log_page_data[8+i];
6452                         }
6453                         sd_pm_tran_data.un.scsi_cycles.flag = 0;
6454                         sd_pm_tran_data.format = DC_SCSI_FORMAT;
6455                 }
6456 
6457                 kmem_free(log_page_data, log_page_size);
6458 
6459                 /*
6460                  * Call pm_trans_check routine to get the Ok from
6461                  * the global policy
6462                  */
6463                 rval = pm_trans_check(&sd_pm_tran_data, &intvlp);
6464 #ifdef  SDDEBUG
6465                 if (sd_force_pm_supported) {
6466                         /* Force a successful result */
6467                         rval = 1;
6468                 }
6469 #endif
6470                 switch (rval) {
6471                 case 0:
6472                         /*
6473                          * Not Ok to Power cycle or error in parameters passed
6474                          * Would have given the advised time to consider power
6475                          * cycle. Based on the new intvlp parameter we are
6476                          * supposed to pretend we are busy so that pm framework
6477                          * will never call our power entry point. Because of
6478                          * that install a timeout handler and wait for the
6479                          * recommended time to elapse so that power management
6480                          * can be effective again.
6481                          *
6482                          * To effect this behavior, call pm_busy_component to
6483                          * indicate to the framework this device is busy.
6484                          * By not adjusting un_pm_count the rest of PM in
6485                          * the driver will function normally, and independent
6486                          * of this but because the framework is told the device
6487                          * is busy it won't attempt powering down until it gets
6488                          * a matching idle. The timeout handler sends this.
6489                          * Note: sd_pm_entry can't be called here to do this
6490                          * because sdpower may have been called as a result
6491                          * of a call to pm_raise_power from within sd_pm_entry.
6492                          *
6493                          * If a timeout handler is already active then
6494                          * don't install another.
6495                          */
6496                         mutex_enter(&un->un_pm_mutex);
6497                         if (un->un_pm_timeid == NULL) {
6498                                 un->un_pm_timeid =
6499                                     timeout(sd_pm_timeout_handler,
6500                                     un, intvlp * drv_usectohz(1000000));
6501                                 mutex_exit(&un->un_pm_mutex);
6502                                 (void) pm_busy_component(SD_DEVINFO(un), 0);
6503                         } else {
6504                                 mutex_exit(&un->un_pm_mutex);
6505                         }
6506                         if (got_semaphore_here != 0) {
6507                                 sema_v(&un->un_semoclose);
6508                         }
6509                         /*
6510                          * On exit put the state back to it's original value
6511                          * and broadcast to anyone waiting for the power
6512                          * change completion.
6513                          */
6514                         mutex_enter(SD_MUTEX(un));
6515                         un->un_state = state_before_pm;
6516                         cv_broadcast(&un->un_suspend_cv);
6517                         mutex_exit(SD_MUTEX(un));
6518 
6519                         SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, "
6520                             "trans check Failed, not ok to power cycle.\n");
6521 
6522                         goto sdpower_failed;
6523                 case -1:
6524                         if (got_semaphore_here != 0) {
6525                                 sema_v(&un->un_semoclose);
6526                         }
6527                         /*
6528                          * On exit put the state back to it's original value
6529                          * and broadcast to anyone waiting for the power
6530                          * change completion.
6531                          */
6532                         mutex_enter(SD_MUTEX(un));
6533                         un->un_state = state_before_pm;
6534                         cv_broadcast(&un->un_suspend_cv);
6535                         mutex_exit(SD_MUTEX(un));
6536                         SD_TRACE(SD_LOG_IO_PM, un,
6537                             "sdpower: exit, trans check command Failed.\n");
6538 
6539                         goto sdpower_failed;
6540                 }
6541         }
6542 
6543         if (!SD_PM_IS_IO_CAPABLE(un, level)) {
6544                 /*
6545                  * Save the last state... if the STOP FAILS we need it
6546                  * for restoring
6547                  */
6548                 mutex_enter(SD_MUTEX(un));
6549                 save_state = un->un_last_state;
6550                 last_power_level = un->un_power_level;
6551                 /*
6552                  * There must not be any cmds. getting processed
6553                  * in the driver when we get here. Power to the
6554                  * device is potentially going off.
6555                  */
6556                 ASSERT(un->un_ncmds_in_driver == 0);
6557                 mutex_exit(SD_MUTEX(un));
6558 
6559                 /*
6560                  * For now PM suspend the device completely before spindle is
6561                  * turned off
6562                  */
6563                 if ((rval = sd_pm_state_change(un, level, SD_PM_STATE_CHANGE))
6564                     == DDI_FAILURE) {
6565                         if (got_semaphore_here != 0) {
6566                                 sema_v(&un->un_semoclose);
6567                         }
6568                         /*
6569                          * On exit put the state back to it's original value
6570                          * and broadcast to anyone waiting for the power
6571                          * change completion.
6572                          */
6573                         mutex_enter(SD_MUTEX(un));
6574                         un->un_state = state_before_pm;
6575                         un->un_power_level = last_power_level;
6576                         cv_broadcast(&un->un_suspend_cv);
6577                         mutex_exit(SD_MUTEX(un));
6578                         SD_TRACE(SD_LOG_IO_PM, un,
6579                             "sdpower: exit, PM suspend Failed.\n");
6580 
6581                         goto sdpower_failed;
6582                 }
6583         }
6584 
6585         /*
6586          * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open,
6587          * close, or strategy. Dump no long uses this routine, it uses it's
6588          * own code so it can be done in polled mode.
6589          */
6590 
6591         medium_present = TRUE;
6592 
6593         /*
6594          * When powering up, issue a TUR in case the device is at unit
6595          * attention.  Don't do retries. Bypass the PM layer, otherwise
6596          * a deadlock on un_pm_busy_cv will occur.
6597          */
6598         if (SD_PM_IS_IO_CAPABLE(un, level)) {
6599                 sval = sd_send_scsi_TEST_UNIT_READY(ssc,
6600                     SD_DONT_RETRY_TUR | SD_BYPASS_PM);
6601                 if (sval != 0)
6602                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6603         }
6604 
6605         if (un->un_f_power_condition_supported) {
6606                 char *pm_condition_name[] = {"STOPPED", "STANDBY",
6607                     "IDLE", "ACTIVE"};
6608                 SD_TRACE(SD_LOG_IO_PM, un,
6609                     "sdpower: sending \'%s\' power condition",
6610                     pm_condition_name[level]);
6611                 sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
6612                     sd_pl2pc[level], SD_PATH_DIRECT);
6613         } else {
6614                 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n",
6615                     ((level == SD_SPINDLE_ON) ? "START" : "STOP"));
6616                 sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
6617                     ((level == SD_SPINDLE_ON) ? SD_TARGET_START :
6618                     SD_TARGET_STOP), SD_PATH_DIRECT);
6619         }
6620         if (sval != 0) {
6621                 if (sval == EIO)
6622                         sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
6623                 else
6624                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6625         }
6626 
6627         /* Command failed, check for media present. */
6628         if ((sval == ENXIO) && un->un_f_has_removable_media) {
6629                 medium_present = FALSE;
6630         }
6631 
6632         /*
6633          * The conditions of interest here are:
6634          *   if a spindle off with media present fails,
6635          *      then restore the state and return an error.
6636          *   else if a spindle on fails,
6637          *      then return an error (there's no state to restore).
6638          * In all other cases we setup for the new state
6639          * and return success.
6640          */
6641         if (!SD_PM_IS_IO_CAPABLE(un, level)) {
6642                 if ((medium_present == TRUE) && (sval != 0)) {
6643                         /* The stop command from above failed */
6644                         rval = DDI_FAILURE;
6645                         /*
6646                          * The stop command failed, and we have media
6647                          * present. Put the level back by calling the
6648                          * sd_pm_resume() and set the state back to
6649                          * it's previous value.
6650                          */
6651                         (void) sd_pm_state_change(un, last_power_level,
6652                             SD_PM_STATE_ROLLBACK);
6653                         mutex_enter(SD_MUTEX(un));
6654                         un->un_last_state = save_state;
6655                         mutex_exit(SD_MUTEX(un));
6656                 } else if (un->un_f_monitor_media_state) {
6657                         /*
6658                          * The stop command from above succeeded.
6659                          * Terminate watch thread in case of removable media
6660                          * devices going into low power state. This is as per
6661                          * the requirements of pm framework, otherwise commands
6662                          * will be generated for the device (through watch
6663                          * thread), even when the device is in low power state.
6664                          */
6665                         mutex_enter(SD_MUTEX(un));
6666                         un->un_f_watcht_stopped = FALSE;
6667                         if (un->un_swr_token != NULL) {
6668                                 opaque_t temp_token = un->un_swr_token;
6669                                 un->un_f_watcht_stopped = TRUE;
6670                                 un->un_swr_token = NULL;
6671                                 mutex_exit(SD_MUTEX(un));
6672                                 (void) scsi_watch_request_terminate(temp_token,
6673                                     SCSI_WATCH_TERMINATE_ALL_WAIT);
6674                         } else {
6675                                 mutex_exit(SD_MUTEX(un));
6676                         }
6677                 }
6678         } else {
6679                 /*
6680                  * The level requested is I/O capable.
6681                  * Legacy behavior: return success on a failed spinup
6682                  * if there is no media in the drive.
6683                  * Do this by looking at medium_present here.
6684                  */
6685                 if ((sval != 0) && medium_present) {
6686                         /* The start command from above failed */
6687                         rval = DDI_FAILURE;
6688                 } else {
6689                         /*
6690                          * The start command from above succeeded
6691                          * PM resume the devices now that we have
6692                          * started the disks
6693                          */
6694                         (void) sd_pm_state_change(un, level,
6695                             SD_PM_STATE_CHANGE);
6696 
6697                         /*
6698                          * Resume the watch thread since it was suspended
6699                          * when the device went into low power mode.
6700                          */
6701                         if (un->un_f_monitor_media_state) {
6702                                 mutex_enter(SD_MUTEX(un));
6703                                 if (un->un_f_watcht_stopped == TRUE) {
6704                                         opaque_t temp_token;
6705 
6706                                         un->un_f_watcht_stopped = FALSE;
6707                                         mutex_exit(SD_MUTEX(un));
6708                                         temp_token =
6709                                             sd_watch_request_submit(un);
6710                                         mutex_enter(SD_MUTEX(un));
6711                                         un->un_swr_token = temp_token;
6712                                 }
6713                                 mutex_exit(SD_MUTEX(un));
6714                         }
6715                 }
6716         }
6717 
6718         if (got_semaphore_here != 0) {
6719                 sema_v(&un->un_semoclose);
6720         }
6721         /*
6722          * On exit put the state back to it's original value
6723          * and broadcast to anyone waiting for the power
6724          * change completion.
6725          */
6726         mutex_enter(SD_MUTEX(un));
6727         un->un_state = state_before_pm;
6728         cv_broadcast(&un->un_suspend_cv);
6729         mutex_exit(SD_MUTEX(un));
6730 
6731         SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval);
6732 
6733         sd_ssc_fini(ssc);
6734         return (rval);
6735 
6736 sdpower_failed:
6737 
6738         sd_ssc_fini(ssc);
6739         return (DDI_FAILURE);
6740 }
6741 
6742 
6743 
6744 /*
6745  *    Function: sdattach
6746  *
6747  * Description: Driver's attach(9e) entry point function.
6748  *
6749  *   Arguments: devi - opaque device info handle
6750  *              cmd  - attach  type
6751  *
6752  * Return Code: DDI_SUCCESS
6753  *              DDI_FAILURE
6754  *
6755  *     Context: Kernel thread context
6756  */
6757 
6758 static int
6759 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
6760 {
6761         struct  scsi_device     *devp;
6762         struct  sd_lun          *un;
6763         char                    *variantp;
6764         int     instance;
6765         int     tgt;
6766         dev_info_t      *pdip = ddi_get_parent(devi);
6767         int             max_xfer_size;
6768         sd_ssc_t        *ssc;
6769         struct sd_fm_internal   *sfip = NULL;
6770 
6771         switch (cmd) {
6772         case DDI_ATTACH:
6773                 break;
6774         case DDI_RESUME:
6775                 return (sd_ddi_resume(devi));
6776         default:
6777                 return (DDI_FAILURE);
6778         }
6779 
6780         /*
6781          * Retrieve the target driver's private data area. This was set
6782          * up by the HBA.
6783          */
6784         devp = ddi_get_driver_private(devi);
6785 
6786         /*
6787          * Retrieve the target ID of the device.
6788          */
6789         tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
6790             SCSI_ADDR_PROP_TARGET, -1);
6791 
6792         /*
6793          * Since we have no idea what state things were left in by the last
6794          * user of the device, set up some 'default' settings, ie. turn 'em
6795          * off. The scsi_ifsetcap calls force re-negotiations with the drive.
6796          * Do this before the scsi_probe, which sends an inquiry.
6797          * This is a fix for bug (4430280).
6798          * Of special importance is wide-xfer. The drive could have been left
6799          * in wide transfer mode by the last driver to communicate with it,
6800          * this includes us. If that's the case, and if the following is not
6801          * setup properly or we don't re-negotiate with the drive prior to
6802          * transferring data to/from the drive, it causes bus parity errors,
6803          * data overruns, and unexpected interrupts. This first occurred when
6804          * the fix for bug (4378686) was made.
6805          */
6806         (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1);
6807         (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1);
6808         (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1);
6809 
6810         /*
6811          * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs
6812          * on a target. Setting it per lun instance actually sets the
6813          * capability of this target, which affects those luns already
6814          * attached on the same target. So during attach, we can only disable
6815          * this capability only when no other lun has been attached on this
6816          * target. By doing this, we assume a target has the same tagged-qing
6817          * capability for every lun. The condition can be removed when HBA
6818          * is changed to support per lun based tagged-qing capability.
6819          */
6820         if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
6821                 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1);
6822         }
6823 
6824         /*
6825          * Use scsi_probe() to issue an INQUIRY command to the device.
6826          * This call will allocate and fill in the scsi_inquiry structure
6827          * and point the sd_inq member of the scsi_device structure to it.
6828          * If the attach succeeds, then this memory will not be de-allocated
6829          * (via scsi_unprobe()) until the instance is detached.
6830          */
6831         if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) {
6832                 goto probe_failed;
6833         }
6834 
6835         /*
6836          * Check the device type as specified in the inquiry data and
6837          * claim it if it is of a type that we support.
6838          */
6839         switch (devp->sd_inq->inq_dtype) {
6840         case DTYPE_DIRECT:
6841                 break;
6842         case DTYPE_RODIRECT:
6843                 break;
6844         case DTYPE_OPTICAL:
6845                 break;
6846         case DTYPE_NOTPRESENT:
6847         default:
6848                 /* Unsupported device type; fail the attach. */
6849                 goto probe_failed;
6850         }
6851 
6852         /*
6853          * Allocate the soft state structure for this unit.
6854          *
6855          * We rely upon this memory being set to all zeroes by
6856          * ddi_soft_state_zalloc().  We assume that any member of the
6857          * soft state structure that is not explicitly initialized by
6858          * this routine will have a value of zero.
6859          */
6860         instance = ddi_get_instance(devp->sd_dev);
6861         if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) {
6862                 goto probe_failed;
6863         }
6864 
6865         /*
6866          * Retrieve a pointer to the newly-allocated soft state.
6867          *
6868          * This should NEVER fail if the ddi_soft_state_zalloc() call above
6869          * was successful, unless something has gone horribly wrong and the
6870          * ddi's soft state internals are corrupt (in which case it is
6871          * probably better to halt here than just fail the attach....)
6872          */
6873         if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
6874                 panic("sdattach: NULL soft state on instance:0x%x",
6875                     instance);
6876                 /*NOTREACHED*/
6877         }
6878 
6879         /*
6880          * Link the back ptr of the driver soft state to the scsi_device
6881          * struct for this lun.
6882          * Save a pointer to the softstate in the driver-private area of
6883          * the scsi_device struct.
6884          * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until
6885          * we first set un->un_sd below.
6886          */
6887         un->un_sd = devp;
6888         devp->sd_private = (opaque_t)un;
6889 
6890         /*
6891          * The following must be after devp is stored in the soft state struct.
6892          */
6893 #ifdef SDDEBUG
6894         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
6895             "%s_unit_attach: un:0x%p instance:%d\n",
6896             ddi_driver_name(devi), un, instance);
6897 #endif
6898 
6899         /*
6900          * Set up the device type and node type (for the minor nodes).
6901          * By default we assume that the device can at least support the
6902          * Common Command Set. Call it a CD-ROM if it reports itself
6903          * as a RODIRECT device.
6904          */
6905         switch (devp->sd_inq->inq_dtype) {
6906         case DTYPE_RODIRECT:
6907                 un->un_node_type = DDI_NT_CD_CHAN;
6908                 un->un_ctype  = CTYPE_CDROM;
6909                 break;
6910         case DTYPE_OPTICAL:
6911                 un->un_node_type = DDI_NT_BLOCK_CHAN;
6912                 un->un_ctype  = CTYPE_ROD;
6913                 break;
6914         default:
6915                 un->un_node_type = DDI_NT_BLOCK_CHAN;
6916                 un->un_ctype  = CTYPE_CCS;
6917                 break;
6918         }
6919 
6920         /* Try to read the interconnect type from the HBA */
6921         un->un_f_is_fibre = FALSE;
6922         switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) {
6923         case INTERCONNECT_SSA:
6924                 un->un_f_is_fibre = TRUE;
6925                 un->un_interconnect_type = SD_INTERCONNECT_SSA;
6926                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
6927                     "sdattach: un:0x%p SD_INTERCONNECT_SSA\n", un);
6928                 break;
6929         case INTERCONNECT_PARALLEL:
6930                 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
6931                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
6932                     "sdattach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un);
6933                 break;
6934         case INTERCONNECT_SAS:
6935                 un->un_interconnect_type = SD_INTERCONNECT_SAS;
6936                 un->un_node_type = DDI_NT_BLOCK_SAS;
6937                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
6938                     "sdattach: un:0x%p SD_INTERCONNECT_SAS\n", un);
6939                 break;
6940         case INTERCONNECT_SATA:
6941                 un->un_interconnect_type = SD_INTERCONNECT_SATA;
6942                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
6943                     "sdattach: un:0x%p SD_INTERCONNECT_SATA\n", un);
6944                 break;
6945         case INTERCONNECT_FIBRE:
6946                 un->un_f_is_fibre = TRUE;
6947                 un->un_interconnect_type = SD_INTERCONNECT_FIBRE;
6948                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
6949                     "sdattach: un:0x%p SD_INTERCONNECT_FIBRE\n", un);
6950                 break;
6951         case INTERCONNECT_FABRIC:
6952                 un->un_f_is_fibre = TRUE;
6953                 un->un_interconnect_type = SD_INTERCONNECT_FABRIC;
6954                 un->un_node_type = DDI_NT_BLOCK_FABRIC;
6955                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
6956                     "sdattach: un:0x%p SD_INTERCONNECT_FABRIC\n", un);
6957                 break;
6958         default:
6959                 /*
6960                  * The default is to assume that if a device does not support
6961                  * the "interconnect-type" property it is a parallel SCSI HBA
6962                  * and set the interconnect type for parallel SCSI.
6963                  */
6964                 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
6965                 break;
6966         }
6967 
6968         if (un->un_f_is_fibre == TRUE) {
6969                 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) ==
6970                     SCSI_VERSION_3) {
6971                         switch (un->un_interconnect_type) {
6972                         case SD_INTERCONNECT_FIBRE:
6973                         case SD_INTERCONNECT_SSA:
6974                                 un->un_node_type = DDI_NT_BLOCK_WWN;
6975                                 break;
6976                         default:
6977                                 break;
6978                         }
6979                 }
6980         }
6981 
6982         (void) ddi_prop_update_int(DDI_DEV_T_NONE, devi,
6983             "allow-unconstrained-retire", 1);
6984 
6985         /*
6986          * Initialize the Request Sense command for the target
6987          */
6988         if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) {
6989                 goto alloc_rqs_failed;
6990         }
6991 
6992         /* The value used is base on interconnect type */
6993         un->un_retry_count = un->un_f_is_fibre ? 3 : 5;
6994 
6995         /*
6996          * Set the per disk retry count to the default number of retries
6997          * for disks and CDROMs. This value can be overridden by the
6998          * disk property list or an entry in sd.conf.
6999          */
7000         un->un_notready_retry_count =
7001             ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un)
7002             : DISK_NOT_READY_RETRY_COUNT(un);
7003 
7004         /*
7005          * Set the busy retry count to the default value of un_retry_count.
7006          * This can be overridden by entries in sd.conf or the device
7007          * config table.
7008          */
7009         un->un_busy_retry_count = un->un_retry_count;
7010 
7011         /*
7012          * Init the reset threshold for retries.  This number determines
7013          * how many retries must be performed before a reset can be issued
7014          * (for certain error conditions). This can be overridden by entries
7015          * in sd.conf or the device config table.
7016          */
7017         un->un_reset_retry_count = (un->un_retry_count / 2);
7018 
7019         /*
7020          * Set the victim_retry_count to the default un_retry_count.
7021          * This value is used in addition to the standard retry count.
7022          * This can be overridden by entries in sd.conf or the device
7023          * config table.
7024          */
7025         un->un_victim_retry_count = un->un_retry_count;
7026 
7027         /*
7028          * Set the reservation release timeout to the default value of
7029          * 5 seconds. This can be overridden by entries in sd.conf or the
7030          * device config table.
7031          */
7032         un->un_reserve_release_time = 5;
7033 
7034         un->un_io_time = sd_io_time;
7035 
7036         un->un_slow_io_threshold = sd_slow_io_threshold;
7037 
7038         un->un_f_lun_reset_enabled = sd_enable_lun_reset;
7039 
7040         /*
7041          * Set up the default maximum transfer size. Note that this may
7042          * get updated later in the attach, when setting up default wide
7043          * operations for disks.
7044          */
7045         un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE;
7046         un->un_partial_dma_supported = 1;
7047 
7048         /*
7049          * Get "allow bus device reset" property (defaults to "enabled" if
7050          * the property was not defined). This is to disable bus resets for
7051          * certain kinds of error recovery. Note: In the future when a run-time
7052          * fibre check is available the soft state flag should default to
7053          * enabled.
7054          */
7055         if (un->un_f_is_fibre == TRUE) {
7056                 un->un_f_allow_bus_device_reset = TRUE;
7057         } else {
7058                 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7059                     "allow-bus-device-reset", 1) != 0) {
7060                         un->un_f_allow_bus_device_reset = TRUE;
7061                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
7062                             "sdattach: un:0x%p Bus device reset "
7063                             "enabled\n", un);
7064                 } else {
7065                         un->un_f_allow_bus_device_reset = FALSE;
7066                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
7067                             "sdattach: un:0x%p Bus device reset "
7068                             "disabled\n", un);
7069                 }
7070         }
7071 
7072         /*
7073          * Check if this is an ATAPI device. ATAPI devices use Group 1
7074          * Read/Write commands and Group 2 Mode Sense/Select commands.
7075          *
7076          * Note: The "obsolete" way of doing this is to check for the "atapi"
7077          * property. The new "variant" property with a value of "atapi" has been
7078          * introduced so that future 'variants' of standard SCSI behavior (like
7079          * atapi) could be specified by the underlying HBA drivers by supplying
7080          * a new value for the "variant" property, instead of having to define a
7081          * new property.
7082          */
7083         if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) {
7084                 un->un_f_cfg_is_atapi = TRUE;
7085                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7086                     "sdattach: un:0x%p Atapi device\n", un);
7087         }
7088         if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant",
7089             &variantp) == DDI_PROP_SUCCESS) {
7090                 if (strcmp(variantp, "atapi") == 0) {
7091                         un->un_f_cfg_is_atapi = TRUE;
7092                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
7093                             "sdattach: un:0x%p Atapi device\n", un);
7094                 }
7095                 ddi_prop_free(variantp);
7096         }
7097 
7098         un->un_cmd_timeout = ((ISCD(un)) ? 2 : 1) * (ushort_t)un->un_io_time;
7099         un->un_uscsi_timeout = un->un_cmd_timeout;
7100         un->un_busy_timeout = SD_BSY_TIMEOUT;
7101 
7102         /*
7103          * Info on current states, statuses, etc. (Updated frequently)
7104          *
7105          * Current state is ATTACHING until we finished sd_unit_attach.
7106          * Last state is NORMAL so that sd_unit_attach can Restore_state()
7107          * when it finishes successfully.
7108          */
7109         un->un_state         = SD_STATE_ATTACHING;
7110         un->un_last_state    = SD_STATE_NORMAL;
7111 
7112         /* Control & status info for command throttling */
7113         un->un_throttle              = sd_max_throttle;
7114         un->un_saved_throttle        = sd_max_throttle;
7115         un->un_min_throttle  = sd_min_throttle;
7116 
7117         if (un->un_f_is_fibre == TRUE) {
7118                 un->un_f_use_adaptive_throttle = TRUE;
7119         } else {
7120                 un->un_f_use_adaptive_throttle = FALSE;
7121         }
7122 
7123         /* Unit detach has to pause until outstanding commands abort */
7124         un->un_f_detach_waiting = 0;
7125         cv_init(&un->un_detach_cv, NULL, CV_DRIVER, NULL);
7126 
7127         /* Removable media support. */
7128         cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL);
7129         un->un_mediastate            = DKIO_NONE;
7130         un->un_specified_mediastate  = DKIO_NONE;
7131 
7132         /* CVs for suspend/resume (PM or DR) */
7133         cv_init(&un->un_suspend_cv,   NULL, CV_DRIVER, NULL);
7134         cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL);
7135 
7136         /* Power management support. */
7137         un->un_power_level = SD_SPINDLE_UNINIT;
7138 
7139         cv_init(&un->un_wcc_cv,   NULL, CV_DRIVER, NULL);
7140         un->un_f_wcc_inprog = 0;
7141 
7142         /*
7143          * The open/close semaphore is used to serialize threads executing
7144          * in the driver's open & close entry point routines for a given
7145          * instance.
7146          */
7147         (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL);
7148 
7149         /*
7150          * The conf file entry and softstate variable is a forceful override,
7151          * meaning a non-zero value must be entered to change the default.
7152          */
7153         un->un_f_disksort_disabled = FALSE;
7154         un->un_f_rmw_type = SD_RMW_TYPE_DEFAULT;
7155         un->un_f_enable_rmw = FALSE;
7156 
7157         /*
7158          * GET EVENT STATUS NOTIFICATION media polling enabled by default, but
7159          * can be overridden via [s]sd-config-list "mmc-gesn-polling" property.
7160          */
7161         un->un_f_mmc_gesn_polling = TRUE;
7162 
7163         /*
7164          * physical sector size defaults to DEV_BSIZE currently. We can
7165          * override this value via the driver configuration file so we must
7166          * set it before calling sd_read_unit_properties().
7167          */
7168         un->un_phy_blocksize = DEV_BSIZE;
7169         un->un_f_sdconf_phy_blocksize = FALSE;
7170 
7171         /*
7172          * Retrieve the properties from the static driver table or the driver
7173          * configuration file (.conf) for this unit and update the soft state
7174          * for the device as needed for the indicated properties.
7175          * Note: the property configuration needs to occur here as some of the
7176          * following routines may have dependencies on soft state flags set
7177          * as part of the driver property configuration.
7178          */
7179         sd_read_unit_properties(un);
7180         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7181             "sdattach: un:0x%p property configuration complete.\n", un);
7182 
7183         /*
7184          * Only if a device has "hotpluggable" property, it is
7185          * treated as hotpluggable device. Otherwise, it is
7186          * regarded as non-hotpluggable one.
7187          */
7188         if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable",
7189             -1) != -1) {
7190                 un->un_f_is_hotpluggable = TRUE;
7191         }
7192 
7193         /*
7194          * set unit's attributes(flags) according to "hotpluggable" and
7195          * RMB bit in INQUIRY data.
7196          */
7197         sd_set_unit_attributes(un, devi);
7198 
7199         /*
7200          * By default, we mark the capacity, lbasize, and geometry
7201          * as invalid. Only if we successfully read a valid capacity
7202          * will we update the un_blockcount and un_tgt_blocksize with the
7203          * valid values (the geometry will be validated later).
7204          */
7205         un->un_f_blockcount_is_valid = FALSE;
7206         un->un_f_tgt_blocksize_is_valid      = FALSE;
7207 
7208         /*
7209          * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine
7210          * otherwise.
7211          */
7212         un->un_tgt_blocksize  = un->un_sys_blocksize  = DEV_BSIZE;
7213         un->un_blockcount = 0;
7214 
7215         /*
7216          * Set up the per-instance info needed to determine the correct
7217          * CDBs and other info for issuing commands to the target.
7218          */
7219         sd_init_cdb_limits(un);
7220 
7221         /*
7222          * Set up the IO chains to use, based upon the target type.
7223          */
7224         if (un->un_f_non_devbsize_supported) {
7225                 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
7226         } else {
7227                 un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
7228         }
7229         un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD;
7230         un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD;
7231         un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD;
7232 
7233         un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf),
7234             sd_xbuf_strategy, un, sd_xbuf_active_limit,  sd_xbuf_reserve_limit,
7235             ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER);
7236         ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi);
7237 
7238 
7239         if (ISCD(un)) {
7240                 un->un_additional_codes = sd_additional_codes;
7241         } else {
7242                 un->un_additional_codes = NULL;
7243         }
7244 
7245         /*
7246          * Create the kstats here so they can be available for attach-time
7247          * routines that send commands to the unit (either polled or via
7248          * sd_send_scsi_cmd).
7249          *
7250          * Note: This is a critical sequence that needs to be maintained:
7251          *      1) Instantiate the kstats here, before any routines using the
7252          *         iopath (i.e. sd_send_scsi_cmd).
7253          *      2) Instantiate and initialize the partition stats
7254          *         (sd_set_pstats).
7255          *      3) Initialize the error stats (sd_set_errstats), following
7256          *         sd_validate_geometry(),sd_register_devid(),
7257          *         and sd_cache_control().
7258          */
7259 
7260         un->un_stats = kstat_create(sd_label, instance,
7261             NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
7262         if (un->un_stats != NULL) {
7263                 un->un_stats->ks_lock = SD_MUTEX(un);
7264                 kstat_install(un->un_stats);
7265         }
7266         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7267             "sdattach: un:0x%p un_stats created\n", un);
7268 
7269         un->un_unmapstats_ks = kstat_create(sd_label, instance, "unmapstats",
7270             "misc", KSTAT_TYPE_NAMED, sizeof (*un->un_unmapstats) /
7271             sizeof (kstat_named_t), 0);
7272         if (un->un_unmapstats_ks) {
7273                 un->un_unmapstats = un->un_unmapstats_ks->ks_data;
7274 
7275                 kstat_named_init(&un->un_unmapstats->us_cmds,
7276                     "commands", KSTAT_DATA_UINT64);
7277                 kstat_named_init(&un->un_unmapstats->us_errs,
7278                     "errors", KSTAT_DATA_UINT64);
7279                 kstat_named_init(&un->un_unmapstats->us_extents,
7280                     "extents", KSTAT_DATA_UINT64);
7281                 kstat_named_init(&un->un_unmapstats->us_bytes,
7282                     "bytes", KSTAT_DATA_UINT64);
7283 
7284                 kstat_install(un->un_unmapstats_ks);
7285         } else {
7286                 cmn_err(CE_NOTE, "!Cannot create unmap kstats for disk %d",
7287                     instance);
7288         }
7289 
7290         un->un_lat_ksp = kstat_create(sd_label, instance, "io_latency",
7291             "io_latency", KSTAT_TYPE_RAW, sizeof (un_lat_stat_t),
7292             KSTAT_FLAG_PERSISTENT);
7293 
7294         if (un->un_lat_ksp != NULL) {
7295                 un->un_lat_ksp->ks_lock = SD_MUTEX(un);
7296                 un->un_lat_stats = (un_lat_stat_t *)un->un_lat_ksp->ks_data;
7297                 kstat_install(un->un_lat_ksp);
7298         } else {
7299                 un->un_lat_stats = NULL;
7300         }
7301 
7302         sd_create_errstats(un, instance);
7303         if (un->un_errstats == NULL) {
7304                 goto create_errstats_failed;
7305         }
7306         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7307             "sdattach: un:0x%p errstats created\n", un);
7308 
7309         /*
7310          * The following if/else code was relocated here from below as part
7311          * of the fix for bug (4430280). However with the default setup added
7312          * on entry to this routine, it's no longer absolutely necessary for
7313          * this to be before the call to sd_spin_up_unit.
7314          */
7315         if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) {
7316                 int tq_trigger_flag = (((devp->sd_inq->inq_ansi == 4) ||
7317                     (devp->sd_inq->inq_ansi == 5)) &&
7318                     devp->sd_inq->inq_bque) || devp->sd_inq->inq_cmdque;
7319 
7320                 /*
7321                  * If tagged queueing is supported by the target
7322                  * and by the host adapter then we will enable it
7323                  */
7324                 un->un_tagflags = 0;
7325                 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && tq_trigger_flag &&
7326                     (un->un_f_arq_enabled == TRUE)) {
7327                         if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing",
7328                             1, 1) == 1) {
7329                                 un->un_tagflags = FLAG_STAG;
7330                                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7331                                     "sdattach: un:0x%p tag queueing "
7332                                     "enabled\n", un);
7333                         } else if (scsi_ifgetcap(SD_ADDRESS(un),
7334                             "untagged-qing", 0) == 1) {
7335                                 un->un_f_opt_queueing = TRUE;
7336                                 un->un_saved_throttle = un->un_throttle =
7337                                     min(un->un_throttle, 3);
7338                         } else {
7339                                 un->un_f_opt_queueing = FALSE;
7340                                 un->un_saved_throttle = un->un_throttle = 1;
7341                         }
7342                 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0)
7343                     == 1) && (un->un_f_arq_enabled == TRUE)) {
7344                         /* The Host Adapter supports internal queueing. */
7345                         un->un_f_opt_queueing = TRUE;
7346                         un->un_saved_throttle = un->un_throttle =
7347                             min(un->un_throttle, 3);
7348                 } else {
7349                         un->un_f_opt_queueing = FALSE;
7350                         un->un_saved_throttle = un->un_throttle = 1;
7351                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
7352                             "sdattach: un:0x%p no tag queueing\n", un);
7353                 }
7354 
7355                 /*
7356                  * Enable large transfers for SATA/SAS drives
7357                  */
7358                 if (SD_IS_SERIAL(un)) {
7359                         un->un_max_xfer_size =
7360                             ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7361                             "sd_max_xfer_size", SD_MAX_XFER_SIZE);
7362                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
7363                             "sdattach: un:0x%p max transfer "
7364                             "size=0x%x\n", un, un->un_max_xfer_size);
7365 
7366                 }
7367 
7368                 /* Setup or tear down default wide operations for disks */
7369                 if (SD_IS_PARALLEL_SCSI(un) &&
7370                     (devp->sd_inq->inq_rdf == RDF_SCSI2) &&
7371                     (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) {
7372                         if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7373                             1, 1) == 1) {
7374                                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7375                                     "sdattach: un:0x%p Wide Transfer "
7376                                     "enabled\n", un);
7377                         }
7378 
7379                         /*
7380                          * If tagged queuing has also been enabled, then
7381                          * enable large xfers
7382                          */
7383                         if (un->un_saved_throttle == sd_max_throttle) {
7384                                 un->un_max_xfer_size =
7385                                     ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7386                                     "sd_max_xfer_size", SD_MAX_XFER_SIZE);
7387                                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7388                                     "sdattach: un:0x%p max transfer "
7389                                     "size=0x%x\n", un, un->un_max_xfer_size);
7390                         }
7391                 } else {
7392                         if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7393                             0, 1) == 1) {
7394                                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7395                                     "sdattach: un:0x%p "
7396                                     "Wide Transfer disabled\n", un);
7397                         }
7398                 }
7399         } else {
7400                 un->un_tagflags = FLAG_STAG;
7401                 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY,
7402                     devi, 0, "sd_max_xfer_size", SD_MAX_XFER_SIZE);
7403         }
7404 
7405         /*
7406          * If this target supports LUN reset, try to enable it.
7407          */
7408         if (un->un_f_lun_reset_enabled) {
7409                 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) {
7410                         SD_INFO(SD_LOG_ATTACH_DETACH, un, "sdattach: "
7411                             "un:0x%p lun_reset capability set\n", un);
7412                 } else {
7413                         SD_INFO(SD_LOG_ATTACH_DETACH, un, "sdattach: "
7414                             "un:0x%p lun-reset capability not set\n", un);
7415                 }
7416         }
7417 
7418         /*
7419          * XXX Adjust the maximum transfer size. This was to fix
7420          * the problem of partial DMA support on SPARC. Some
7421          * HBA driver, like aac, has very small dma_attr_maxxfer
7422          * size, which requires partial DMA support on SPARC.
7423          */
7424         max_xfer_size = scsi_ifgetcap(SD_ADDRESS(un), "dma-max", 1);
7425         if ((max_xfer_size > 0) && (max_xfer_size < un->un_max_xfer_size)) {
7426                 un->un_max_xfer_size = max_xfer_size;
7427                 if (un->un_partial_dma_supported == 0)
7428                         un->un_partial_dma_supported = 1;
7429         }
7430         if (ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7431             DDI_PROP_DONTPASS, "buf_break", 0) == 1) {
7432                 if (ddi_xbuf_attr_setup_brk(un->un_xbuf_attr,
7433                     un->un_max_xfer_size) == 1) {
7434                         un->un_buf_breakup_supported = 1;
7435                         SD_INFO(SD_LOG_ATTACH_DETACH, un, "sdattach: "
7436                             "un:0x%p Buf breakup enabled\n", un);
7437                 }
7438         }
7439 
7440         /*
7441          * Set PKT_DMA_PARTIAL flag.
7442          */
7443         if (un->un_partial_dma_supported == 1) {
7444                 un->un_pkt_flags = PKT_DMA_PARTIAL;
7445         } else {
7446                 un->un_pkt_flags = 0;
7447         }
7448 
7449         scsi_fm_init(devp);
7450 
7451         /*
7452          * Allocate memory for SCSI FMA stuff.
7453          */
7454         un->un_fm_private =
7455             kmem_zalloc(sizeof (struct sd_fm_internal), KM_SLEEP);
7456         sfip = (struct sd_fm_internal *)un->un_fm_private;
7457         sfip->fm_ssc.ssc_uscsi_cmd = &sfip->fm_ucmd;
7458         sfip->fm_ssc.ssc_uscsi_info = &sfip->fm_uinfo;
7459         sfip->fm_ssc.ssc_un = un;
7460 
7461         if (ISCD(un) ||
7462             un->un_f_has_removable_media ||
7463             devp->sd_fm_capable == DDI_FM_NOT_CAPABLE) {
7464                 /*
7465                  * We don't touch CDROM or the DDI_FM_NOT_CAPABLE device.
7466                  * Their log are unchanged.
7467                  */
7468                 sfip->fm_log_level = SD_FM_LOG_NSUP;
7469         } else {
7470                 /*
7471                  * If enter here, it should be non-CDROM and FM-capable
7472                  * device, and it will not keep the old scsi_log as before
7473                  * in /var/adm/messages. However, the property
7474                  * "fm-scsi-log" will control whether the FM telemetry will
7475                  * be logged in /var/adm/messages.
7476                  */
7477                 int fm_scsi_log;
7478                 fm_scsi_log = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7479                     DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "fm-scsi-log", 0);
7480 
7481                 if (fm_scsi_log)
7482                         sfip->fm_log_level = SD_FM_LOG_EREPORT;
7483                 else
7484                         sfip->fm_log_level = SD_FM_LOG_SILENT;
7485         }
7486 
7487         /* Initialize sd_ssc_t for internal uscsi commands */
7488         ssc = sd_ssc_init(un);
7489 
7490         mutex_enter(SD_MUTEX(un));
7491         /*
7492          * Initialize the devid for the unit. Indicate target reservation so
7493          * that no real I/O is done for devices that need devid fabrication.
7494          * We will try again in sd_unit_attach() if necessary.
7495          */
7496         if (un->un_f_devid_supported) {
7497                 sd_register_devid(ssc, devi, SD_TARGET_IS_RESERVED);
7498         }
7499         mutex_exit(SD_MUTEX(un));
7500 
7501         /* Uninitialize sd_ssc_t pointer */
7502         sd_ssc_fini(ssc);
7503 
7504         cmlb_alloc_handle(&un->un_cmlbhandle);
7505 
7506         if (cmlb_attach(devi, &sd_tgops, (int)devp->sd_inq->inq_dtype,
7507             VOID2BOOLEAN(un->un_f_has_removable_media != 0),
7508             VOID2BOOLEAN(un->un_f_is_hotpluggable != 0),
7509             un->un_node_type, 0, un->un_cmlbhandle,
7510             (void *)SD_PATH_DIRECT) != 0) {
7511                 goto cmlb_attach_failed;
7512         }
7513 
7514         /*
7515          * At this point in the attach, we have enough info in the
7516          * soft state to be able to issue commands to the target.
7517          *
7518          * Schedule a taskq to finish attach to avoid holding the
7519          * device tree lock for too long. If this fails, rollback
7520          * and fail the attach.
7521          */
7522 
7523         if (taskq_dispatch(sd_tq, sd_unit_attach, devi, KM_PUSHPAGE) != NULL)
7524                 return (DDI_SUCCESS);
7525 
7526         cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
7527         cmlb_free_handle(&un->un_cmlbhandle);
7528 
7529 cmlb_attach_failed:
7530         mutex_enter(SD_MUTEX(un));
7531 
7532         /* Deallocate SCSI FMA memory spaces */
7533         kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
7534 
7535         /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */
7536         if (un->un_direct_priority_timeid != NULL) {
7537                 timeout_id_t temp_id = un->un_direct_priority_timeid;
7538                 un->un_direct_priority_timeid = NULL;
7539                 mutex_exit(SD_MUTEX(un));
7540                 (void) untimeout(temp_id);
7541                 mutex_enter(SD_MUTEX(un));
7542         }
7543 
7544         /* Cancel any pending start/stop timeouts */
7545         if (un->un_startstop_timeid != NULL) {
7546                 timeout_id_t temp_id = un->un_startstop_timeid;
7547                 un->un_startstop_timeid = NULL;
7548                 mutex_exit(SD_MUTEX(un));
7549                 (void) untimeout(temp_id);
7550                 mutex_enter(SD_MUTEX(un));
7551         }
7552 
7553         /* Cancel any pending reset-throttle timeouts */
7554         if (un->un_reset_throttle_timeid != NULL) {
7555                 timeout_id_t temp_id = un->un_reset_throttle_timeid;
7556                 un->un_reset_throttle_timeid = NULL;
7557                 mutex_exit(SD_MUTEX(un));
7558                 (void) untimeout(temp_id);
7559                 mutex_enter(SD_MUTEX(un));
7560         }
7561 
7562         /* Cancel rmw warning message timeouts */
7563         if (un->un_rmw_msg_timeid != NULL) {
7564                 timeout_id_t temp_id = un->un_rmw_msg_timeid;
7565                 un->un_rmw_msg_timeid = NULL;
7566                 mutex_exit(SD_MUTEX(un));
7567                 (void) untimeout(temp_id);
7568                 mutex_enter(SD_MUTEX(un));
7569         }
7570 
7571         /* Cancel any pending retry timeouts */
7572         if (un->un_retry_timeid != NULL) {
7573                 timeout_id_t temp_id = un->un_retry_timeid;
7574                 un->un_retry_timeid = NULL;
7575                 mutex_exit(SD_MUTEX(un));
7576                 (void) untimeout(temp_id);
7577                 mutex_enter(SD_MUTEX(un));
7578         }
7579 
7580         /* Cancel any pending delayed cv broadcast timeouts */
7581         if (un->un_dcvb_timeid != NULL) {
7582                 timeout_id_t temp_id = un->un_dcvb_timeid;
7583                 un->un_dcvb_timeid = NULL;
7584                 mutex_exit(SD_MUTEX(un));
7585                 (void) untimeout(temp_id);
7586                 mutex_enter(SD_MUTEX(un));
7587         }
7588 
7589         mutex_exit(SD_MUTEX(un));
7590 
7591         /* There should not be any in-progress I/O so ASSERT this check */
7592         ASSERT(un->un_ncmds_in_transport == 0);
7593         ASSERT(un->un_ncmds_in_driver == 0);
7594 
7595         /* Do not free the softstate if the callback routine is active */
7596         sd_sync_with_callback(un);
7597 
7598         /*
7599          * Partition stats apparently are not used with removables. These would
7600          * not have been created during attach, so no need to clean them up...
7601          */
7602         if (un->un_errstats != NULL) {
7603                 kstat_delete(un->un_errstats);
7604                 un->un_errstats = NULL;
7605         }
7606 
7607 create_errstats_failed:
7608 
7609         if (un->un_stats != NULL) {
7610                 kstat_delete(un->un_stats);
7611                 un->un_stats = NULL;
7612         }
7613 
7614         if (un->un_unmapstats != NULL) {
7615                 kstat_delete(un->un_unmapstats_ks);
7616                 un->un_unmapstats_ks = NULL;
7617                 un->un_unmapstats = NULL;
7618         }
7619 
7620         if (un->un_lat_ksp != NULL) {
7621                 kstat_delete(un->un_lat_ksp);
7622                 un->un_lat_ksp = NULL;
7623                 un->un_lat_stats = NULL;
7624         }
7625 
7626         ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
7627         ddi_xbuf_attr_destroy(un->un_xbuf_attr);
7628 
7629         ddi_prop_remove_all(devi);
7630         sema_destroy(&un->un_semoclose);
7631         cv_destroy(&un->un_state_cv);
7632         cv_destroy(&un->un_detach_cv);
7633         sd_free_rqs(un);
7634 
7635 alloc_rqs_failed:
7636 
7637         devp->sd_private = NULL;
7638         bzero(un, sizeof (struct sd_lun));      /* Clear any stale data! */
7639 
7640         /*
7641          * Note: the man pages are unclear as to whether or not doing a
7642          * ddi_soft_state_free(sd_state, instance) is the right way to
7643          * clean up after the ddi_soft_state_zalloc() if the subsequent
7644          * ddi_get_soft_state() fails.  The implication seems to be
7645          * that the get_soft_state cannot fail if the zalloc succeeds.
7646          */
7647         ddi_soft_state_free(sd_state, instance);
7648 
7649 probe_failed:
7650         scsi_unprobe(devp);
7651 
7652         return (DDI_FAILURE);
7653 }
7654 
7655 
7656 /*
7657  *    Function: sddetach
7658  *
7659  * Description: Driver's detach(9E) entry point function.
7660  *
7661  *   Arguments: devi - opaque device info handle
7662  *              cmd  - detach  type
7663  *
7664  * Return Code: DDI_SUCCESS
7665  *              DDI_FAILURE
7666  *
7667  *     Context: Kernel thread context
7668  */
7669 
7670 static int
7671 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
7672 {
7673         switch (cmd) {
7674         case DDI_DETACH:
7675                 return (sd_unit_detach(devi));
7676         case DDI_SUSPEND:
7677                 return (sd_ddi_suspend(devi));
7678         default:
7679                 break;
7680         }
7681         return (DDI_FAILURE);
7682 }
7683 
7684 
7685 /*
7686  *     Function: sd_sync_with_callback
7687  *
7688  *  Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft
7689  *               state while the callback routine is active.
7690  *
7691  *    Arguments: un: softstate structure for the instance
7692  *
7693  *      Context: Kernel thread context
7694  */
7695 
7696 static void
7697 sd_sync_with_callback(struct sd_lun *un)
7698 {
7699         ASSERT(un != NULL);
7700 
7701         mutex_enter(SD_MUTEX(un));
7702 
7703         ASSERT(un->un_in_callback >= 0);
7704 
7705         while (un->un_in_callback > 0) {
7706                 mutex_exit(SD_MUTEX(un));
7707                 delay(2);
7708                 mutex_enter(SD_MUTEX(un));
7709         }
7710 
7711         mutex_exit(SD_MUTEX(un));
7712 }
7713 
7714 /*
7715  *    Function: sd_unit_attach
7716  *
7717  * Description: Performs DDI_ATTACH processing for sdattach(). Allocates
7718  *              the soft state structure for the device and performs
7719  *              all necessary structure and device initializations.
7720  *
7721  *   Arguments: devi: the system's dev_info_t for the device.
7722  *
7723  * Return Code: DDI_SUCCESS if attach is successful.
7724  *              DDI_FAILURE if any part of the attach fails.
7725  *
7726  *     Context: Called at attach(9e) time for the DDI_ATTACH flag.
7727  *              Kernel thread context only.  Can sleep.
7728  */
7729 void
7730 sd_unit_attach(void *arg)
7731 {
7732         dev_info_t              *devi = arg;
7733         struct  scsi_device     *devp = ddi_get_driver_private(devi);
7734         struct  sd_lun          *un = (struct sd_lun *)devp->sd_private;
7735         char                    name_str[48];
7736         int     reservation_flag = SD_TARGET_IS_UNRESERVED;
7737         int     rval;
7738         int     wc_enabled;
7739         int     wc_changeable;
7740         int     tgt;
7741         uint64_t        capacity;
7742         uint_t          lbasize = 0;
7743         dev_info_t      *pdip = ddi_get_parent(devi);
7744         int             geom_label_valid = 0;
7745         sd_ssc_t        *ssc;
7746         int             status;
7747         char            *devid;
7748 
7749         /*
7750          * Retrieve the target ID of the device.
7751          */
7752         tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7753             SCSI_ADDR_PROP_TARGET, -1);
7754 
7755         /*
7756          * All command paths used below MUST issue their commands as
7757          * SD_PATH_DIRECT. This is important as intermediate layers
7758          * are not all initialized yet (such as PM).
7759          */
7760 
7761         /* Initialize sd_ssc_t for internal uscsi commands */
7762         ssc = sd_ssc_init(un);
7763 
7764         /*
7765          * Send a TEST UNIT READY command to the device. This should clear
7766          * any outstanding UNIT ATTENTION that may be present.
7767          *
7768          * Note: Don't check for success, just track if there is a reservation,
7769          * this is a throw away command to clear any unit attentions.
7770          *
7771          * Note: This MUST be the first command issued to the target during
7772          * attach to ensure power on UNIT ATTENTIONS are cleared.
7773          * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated
7774          * with attempts at spinning up a device with no media.
7775          */
7776         status = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
7777         if (status != 0) {
7778                 if (status == EACCES)
7779                         reservation_flag = SD_TARGET_IS_RESERVED;
7780                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
7781         }
7782 
7783         /*
7784          * If the device is NOT a removable media device, attempt to spin
7785          * it up (using the START_STOP_UNIT command) and read its capacity
7786          * (using the READ CAPACITY command).  Note, however, that either
7787          * of these could fail and in some cases we would continue with
7788          * the attach despite the failure (see below).
7789          */
7790         if (un->un_f_descr_format_supported) {
7791 
7792                 switch (sd_spin_up_unit(ssc)) {
7793                 case 0:
7794                         /*
7795                          * Spin-up was successful; now try to read the
7796                          * capacity.  If successful then save the results
7797                          * and mark the capacity & lbasize as valid.
7798                          */
7799                         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7800                             "sd_unit_attach: un:0x%p spin-up successful\n", un);
7801 
7802                         status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
7803                             &lbasize, SD_PATH_DIRECT);
7804 
7805                         switch (status) {
7806                         case 0: {
7807                                 if (capacity > DK_MAX_BLOCKS) {
7808 #ifdef _LP64
7809                                         if ((capacity + 1) >
7810                                             SD_GROUP1_MAX_ADDRESS) {
7811                                                 /*
7812                                                  * Enable descriptor format
7813                                                  * sense data so that we can
7814                                                  * get 64 bit sense data
7815                                                  * fields.
7816                                                  */
7817                                                 sd_enable_descr_sense(ssc);
7818                                         }
7819 #else
7820                                         /* 32-bit kernels can't handle this */
7821                                         scsi_log(SD_DEVINFO(un),
7822                                             sd_label, CE_WARN,
7823                                             "disk has %llu blocks, which "
7824                                             "is too large for a 32-bit "
7825                                             "kernel", capacity);
7826 
7827                                         /*
7828                                          * 1TB disk was treated as (1T - 512)B
7829                                          * in the past, so that it might have
7830                                          * valid VTOC and solaris partitions,
7831                                          * we have to allow it to continue to
7832                                          * work.
7833                                          */
7834                                         if (capacity - 1 > DK_MAX_BLOCKS)
7835                                                 goto spinup_failed;
7836 #endif
7837                                 }
7838 
7839                                 /*
7840                                  * Here it's not necessary to check the case:
7841                                  * the capacity of the device is bigger than
7842                                  * what the max hba cdb can support. Because
7843                                  * sd_send_scsi_READ_CAPACITY will retrieve
7844                                  * the capacity by sending USCSI command, which
7845                                  * is constrained by the max hba cdb. Actually,
7846                                  * sd_send_scsi_READ_CAPACITY will return
7847                                  * EINVAL when using bigger cdb than required
7848                                  * cdb length. Will handle this case in
7849                                  * "case EINVAL".
7850                                  */
7851 
7852                                 /*
7853                                  * The following relies on
7854                                  * sd_send_scsi_READ_CAPACITY never
7855                                  * returning 0 for capacity and/or lbasize.
7856                                  */
7857                                 sd_update_block_info(un, lbasize, capacity);
7858 
7859                                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7860                                     "sd_unit_attach: un:0x%p capacity = %ld "
7861                                     "blocks; lbasize= %ld.\n", un,
7862                                     un->un_blockcount, un->un_tgt_blocksize);
7863 
7864                                 break;
7865                         }
7866                         case EINVAL:
7867                                 /*
7868                                  * In the case where the max-cdb-length property
7869                                  * is smaller than the required CDB length for
7870                                  * a SCSI device, a target driver can fail to
7871                                  * attach to that device.
7872                                  */
7873                                 scsi_log(SD_DEVINFO(un),
7874                                     sd_label, CE_WARN,
7875                                     "disk capacity is too large "
7876                                     "for current cdb length");
7877                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
7878 
7879                                 goto spinup_failed;
7880                         case EACCES:
7881                                 /*
7882                                  * Should never get here if the spin-up
7883                                  * succeeded, but code it in anyway.
7884                                  * From here, just continue with the attach...
7885                                  */
7886                                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7887                                     "sd_unit_attach: un:0x%p "
7888                                     "sd_send_scsi_READ_CAPACITY "
7889                                     "returned reservation conflict\n", un);
7890                                 reservation_flag = SD_TARGET_IS_RESERVED;
7891                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
7892                                 break;
7893                         default:
7894                                 /*
7895                                  * Likewise, should never get here if the
7896                                  * spin-up succeeded. Just continue with
7897                                  * the attach...
7898                                  */
7899                                 if (status == EIO) {
7900                                         sd_ssc_assessment(ssc,
7901                                             SD_FMT_STATUS_CHECK);
7902                                         goto spinup_failed;
7903                                 } else {
7904                                         sd_ssc_assessment(ssc,
7905                                             SD_FMT_IGNORE);
7906                                 }
7907                                 break;
7908                         }
7909                         break;
7910                 case EACCES:
7911                         /*
7912                          * Device is reserved by another host.  In this case
7913                          * we could not spin it up or read the capacity, but
7914                          * we continue with the attach anyway.
7915                          */
7916                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
7917                             "sd_unit_attach: un:0x%p spin-up reservation "
7918                             "conflict.\n", un);
7919                         reservation_flag = SD_TARGET_IS_RESERVED;
7920                         break;
7921                 default:
7922                         /* Fail the attach if the spin-up failed. */
7923                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
7924                             "sd_unit_attach: un:0x%p spin-up failed.", un);
7925                         goto spinup_failed;
7926                 }
7927 
7928         }
7929 
7930         /*
7931          * Check to see if this is a MMC drive
7932          */
7933         if (ISCD(un)) {
7934                 sd_set_mmc_caps(ssc);
7935         }
7936 
7937         /*
7938          * Add a zero-length attribute to tell the world we support
7939          * kernel ioctls (for layered drivers)
7940          */
7941         (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
7942             DDI_KERNEL_IOCTL, NULL, 0);
7943 
7944         /*
7945          * Add a boolean property to tell the world we support
7946          * the B_FAILFAST flag (for layered drivers)
7947          */
7948         (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
7949             "ddi-failfast-supported", NULL, 0);
7950 
7951         /*
7952          * Initialize power management
7953          */
7954         mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL);
7955         cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL);
7956 #ifdef notyet
7957         sd_setup_pm(ssc, devi);
7958 #endif
7959         if (un->un_f_pm_is_enabled == FALSE) {
7960                 /*
7961                  * For performance, point to a jump table that does
7962                  * not include pm.
7963                  * The direct and priority chains don't change with PM.
7964                  *
7965                  * Note: this is currently done based on individual device
7966                  * capabilities. When an interface for determining system
7967                  * power enabled state becomes available, or when additional
7968                  * layers are added to the command chain, these values will
7969                  * have to be re-evaluated for correctness.
7970                  */
7971                 if (un->un_f_non_devbsize_supported) {
7972                         un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM;
7973                 } else {
7974                         un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM;
7975                 }
7976                 un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
7977         }
7978 
7979         /*
7980          * This property is set to 0 by HA software to avoid retries
7981          * on a reserved disk. (The preferred property name is
7982          * "retry-on-reservation-conflict") (1189689)
7983          *
7984          * Note: The use of a global here can have unintended consequences. A
7985          * per instance variable is preferable to match the capabilities of
7986          * different underlying hba's (4402600)
7987          */
7988         sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi,
7989             DDI_PROP_DONTPASS, "retry-on-reservation-conflict",
7990             sd_retry_on_reservation_conflict);
7991         if (sd_retry_on_reservation_conflict != 0) {
7992                 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY,
7993                     devi, DDI_PROP_DONTPASS, "sd_retry_on_reservation_conflict",
7994                     sd_retry_on_reservation_conflict);
7995         }
7996 
7997         /* Set up options for QFULL handling. */
7998         if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7999             "qfull-retries", -1)) != -1) {
8000                 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries",
8001                     rval, 1);
8002         }
8003         if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8004             "qfull-retry-interval", -1)) != -1) {
8005                 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval",
8006                     rval, 1);
8007         }
8008 
8009         /*
8010          * This just prints a message that announces the existence of the
8011          * device. The message is always printed in the system logfile, but
8012          * only appears on the console if the system is booted with the
8013          * -v (verbose) argument.
8014          */
8015         ddi_report_dev(devi);
8016 
8017         un->un_mediastate = DKIO_NONE;
8018 
8019         /*
8020          * Check Block Device Characteristics VPD.
8021          */
8022         sd_check_bdc_vpd(ssc);
8023 
8024         /*
8025          * Check whether the drive is in emulation mode.
8026          */
8027         sd_check_emulation_mode(ssc);
8028 
8029         /* Compensate for off-by-1 legacy error */
8030         if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable &&
8031             (lbasize == un->un_sys_blocksize))
8032                 cmlb_workaround_off_by_one(un->un_cmlbhandle);
8033 
8034         /*
8035          * Read and validate the device's geometry (ie, disk label)
8036          * A new unformatted drive will not have a valid geometry, but
8037          * the driver needs to successfully attach to this device so
8038          * the drive can be formatted via ioctls.
8039          */
8040         geom_label_valid = (cmlb_validate(un->un_cmlbhandle, 0,
8041             (void *)SD_PATH_DIRECT) == 0) ? 1: 0;
8042 
8043         mutex_enter(SD_MUTEX(un));
8044 
8045         /*
8046          * Read and initialize the devid for the unit if not done already.
8047          */
8048         if (un->un_f_devid_supported && un->un_devid == NULL) {
8049                 sd_register_devid(ssc, devi, reservation_flag);
8050         }
8051         mutex_exit(SD_MUTEX(un));
8052 
8053         if (un->un_f_opt_disable_cache == TRUE) {
8054                 /*
8055                  * Disable both read cache and write cache.  This is
8056                  * the historic behavior of the keywords in the config file.
8057                  */
8058                 if (sd_cache_control(ssc, SD_CACHE_DISABLE, SD_CACHE_DISABLE) !=
8059                     0) {
8060                         SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8061                             "sd_unit_attach: un:0x%p Could not disable "
8062                             "caching", un);
8063                         goto devid_failed;
8064                 }
8065         }
8066 
8067         /*
8068          * Check the value of the WCE bit and if it's allowed to be changed,
8069          * set un_f_write_cache_enabled and un_f_cache_mode_changeable
8070          * accordingly.
8071          */
8072         (void) sd_get_write_cache_enabled(ssc, &wc_enabled);
8073         sd_get_write_cache_changeable(ssc, &wc_changeable);
8074         mutex_enter(SD_MUTEX(un));
8075         un->un_f_write_cache_enabled = (wc_enabled != 0);
8076         un->un_f_cache_mode_changeable = (wc_changeable != 0);
8077         mutex_exit(SD_MUTEX(un));
8078 
8079         if ((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR &&
8080             un->un_tgt_blocksize != DEV_BSIZE) ||
8081             un->un_f_enable_rmw) {
8082                 if (!(un->un_wm_cache)) {
8083                         (void) snprintf(name_str, sizeof (name_str),
8084                             "%s%d_cache",
8085                             ddi_driver_name(SD_DEVINFO(un)),
8086                             ddi_get_instance(SD_DEVINFO(un)));
8087                         un->un_wm_cache = kmem_cache_create(
8088                             name_str, sizeof (struct sd_w_map),
8089                             8, sd_wm_cache_constructor,
8090                             sd_wm_cache_destructor, NULL,
8091                             (void *)un, NULL, 0);
8092                         if (!(un->un_wm_cache)) {
8093                                 goto wm_cache_failed;
8094                         }
8095                 }
8096         }
8097 
8098         /*
8099          * Check the value of the NV_SUP bit and set
8100          * un_f_suppress_cache_flush accordingly.
8101          */
8102         sd_get_nv_sup(ssc);
8103 
8104         /*
8105          * Find out what type of reservation this disk supports.
8106          */
8107         status = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS, 0, NULL);
8108 
8109         switch (status) {
8110         case 0:
8111                 /*
8112                  * SCSI-3 reservations are supported.
8113                  */
8114                 un->un_reservation_type = SD_SCSI3_RESERVATION;
8115                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8116                     "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un);
8117                 break;
8118         case ENOTSUP:
8119                 /*
8120                  * The PERSISTENT RESERVE IN command would not be recognized by
8121                  * a SCSI-2 device, so assume the reservation type is SCSI-2.
8122                  */
8123                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8124                     "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un);
8125                 un->un_reservation_type = SD_SCSI2_RESERVATION;
8126 
8127                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8128                 break;
8129         default:
8130                 /*
8131                  * default to SCSI-3 reservations
8132                  */
8133                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8134                     "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un);
8135                 un->un_reservation_type = SD_SCSI3_RESERVATION;
8136 
8137                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8138                 break;
8139         }
8140 
8141         /*
8142          * Set the pstat and error stat values here, so data obtained during the
8143          * previous attach-time routines is available.
8144          *
8145          * Note: This is a critical sequence that needs to be maintained:
8146          *      1) Instantiate the kstats before any routines using the iopath
8147          *         (i.e. sd_send_scsi_cmd).
8148          *      2) Initialize the error stats (sd_set_errstats) and partition
8149          *         stats (sd_set_pstats)here, following
8150          *         cmlb_validate_geometry(), sd_register_devid(), and
8151          *         sd_cache_control().
8152          */
8153 
8154         if (un->un_f_pkstats_enabled && geom_label_valid) {
8155                 sd_set_pstats(un);
8156                 SD_TRACE(SD_LOG_IO_PARTITION, un,
8157                     "sd_unit_attach: un:0x%p pstats created and set\n", un);
8158         }
8159 
8160         sd_set_errstats(un);
8161         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8162             "sd_unit_attach: un:0x%p errstats set\n", un);
8163 
8164         sd_setup_blk_limits(ssc);
8165 
8166         /*
8167          * After successfully attaching an instance, we record the information
8168          * of how many luns have been attached on the relative target and
8169          * controller for parallel SCSI. This information is used when sd tries
8170          * to set the tagged queuing capability in HBA.
8171          */
8172         if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) {
8173                 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH);
8174         }
8175 
8176         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8177             "sd_unit_attach: un:0x%p exit success\n", un);
8178 
8179         /* Uninitialize sd_ssc_t pointer */
8180         sd_ssc_fini(ssc);
8181 
8182         /* attach finished, switch to SD_STATE_NORMAL */
8183         mutex_enter(SD_MUTEX(un));
8184         New_state(un, SD_STATE_NORMAL);
8185         cv_broadcast(&un->un_suspend_cv);
8186         mutex_exit(SD_MUTEX(un));
8187 
8188         return;
8189 
8190         /*
8191          * An error occurred during the attach; clean up & return failure.
8192          */
8193 
8194 wm_cache_failed:
8195 devid_failed:
8196         /*
8197          * Cleanup from the scsi_ifsetcap() calls (437868)
8198          */
8199         (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8200         (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8201 
8202         /*
8203          * Refer to the comments of setting tagged-qing in the beginning of
8204          * sd_unit_attach. We can only disable tagged queuing when there is
8205          * no lun attached on the target.
8206          */
8207         if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
8208                 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8209         }
8210 
8211         if (un->un_f_is_fibre == FALSE) {
8212                 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8213         }
8214 
8215 spinup_failed:
8216         /* attach failed, switch to SD_STATE_ATTACH_FAILED */
8217         mutex_enter(SD_MUTEX(un));
8218         New_state(un, SD_STATE_ATTACH_FAILED);
8219         cv_broadcast(&un->un_suspend_cv);
8220         mutex_exit(SD_MUTEX(un));
8221 
8222         devid = DEVI(devi)->devi_devid_str;
8223         scsi_fm_ereport_post(un->un_sd, 0,
8224             NULL, "disk.attach-failure", ssc->ssc_uscsi_info->ui_ena,
8225             devid, NULL, DDI_NOSLEEP, NULL,
8226             FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
8227             DEVID_IF_KNOWN(devid));
8228 
8229         /* Uninitialize sd_ssc_t pointer */
8230         sd_ssc_fini(ssc);
8231         SD_ERROR(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach failed: un: %p",
8232             (void *)un);
8233 }
8234 
8235 
8236 /*
8237  *    Function: sd_unit_detach
8238  *
8239  * Description: Performs DDI_DETACH processing for sddetach().
8240  *
8241  * Return Code: DDI_SUCCESS
8242  *              DDI_FAILURE
8243  *
8244  *     Context: Kernel thread context
8245  */
8246 
8247 static int
8248 sd_unit_detach(dev_info_t *devi)
8249 {
8250         struct scsi_device      *devp;
8251         struct sd_lun           *un;
8252         int                     i;
8253         int                     tgt;
8254         dev_t                   dev;
8255         dev_info_t              *pdip = ddi_get_parent(devi);
8256         int                     instance = ddi_get_instance(devi);
8257 
8258         mutex_enter(&sd_detach_mutex);
8259 
8260         /*
8261          * Fail the detach for any of the following:
8262          * - Unable to get the sd_lun struct for the instance
8263          * - The instance is still attaching
8264          * - Another thread is already detaching this instance
8265          * - Another thread is currently performing an open
8266          *
8267          * Additionaly, if "device gone" flag is not set:
8268          * - There are outstanding commands in driver
8269          * - There are outstanding commands in transport
8270          */
8271         devp = ddi_get_driver_private(devi);
8272         if (devp == NULL || (un = (struct sd_lun *)devp->sd_private) == NULL ||
8273             un->un_detach_count != 0 || un->un_opens_in_progress != 0 ||
8274             (!DEVI_IS_GONE(devi) &&
8275             (un->un_state == SD_STATE_RWAIT ||
8276             un->un_state == SD_STATE_ATTACHING ||
8277             un->un_ncmds_in_driver != 0 ||
8278             un->un_ncmds_in_transport != 0))) {
8279                 mutex_exit(&sd_detach_mutex);
8280                 return (DDI_FAILURE);
8281         }
8282 
8283         SD_TRACE(SD_LOG_ATTACH_DETACH, un, "%s: entry 0x%p\n", __func__, un);
8284 
8285         /*
8286          * Mark this instance as currently in a detach, to inhibit any
8287          * opens from a layered driver.
8288          */
8289         un->un_detach_count++;
8290         mutex_exit(&sd_detach_mutex);
8291 
8292         tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
8293             SCSI_ADDR_PROP_TARGET, -1);
8294 
8295         dev = sd_make_device(SD_DEVINFO(un));
8296 
8297         mutex_enter(SD_MUTEX(un));
8298 
8299         /*
8300          * Fail the detach if there are any outstanding layered
8301          * opens on this device.
8302          */
8303         for (i = 0; i < NDKMAP; i++) {
8304                 if (un->un_ocmap.lyropen[i] != 0) {
8305                         goto err_notclosed;
8306                 }
8307         }
8308 
8309         /*
8310          * If the attach wasn't successful, some normal cleanup work must not
8311          * be done.
8312          */
8313         if (un->un_state == SD_STATE_ATTACH_FAILED) {
8314                 mutex_exit(SD_MUTEX(un));
8315                 goto no_attach_cleanup;
8316         }
8317 
8318         /*
8319          * If we have the device reserved, release the reservation.
8320          */
8321         if (!DEVI_IS_GONE(devi) &&
8322             (un->un_resvd_status & SD_RESERVE) &&
8323             !(un->un_resvd_status & SD_LOST_RESERVE)) {
8324                 mutex_exit(SD_MUTEX(un));
8325                 /*
8326                  * Note: sd_reserve_release sends a command to the device
8327                  * via the sd_ioctlcmd() path, and can sleep.
8328                  */
8329                 if (sd_reserve_release(dev, SD_RELEASE) != 0) {
8330                         SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8331                             "%s: cannot release reservation\n", __func__);
8332                 }
8333         } else {
8334                 mutex_exit(SD_MUTEX(un));
8335         }
8336 
8337         /*
8338          * Untimeout any reserve recover, throttle reset, restart unit
8339          * and delayed broadcast timeout threads. Protect the timeout pointer
8340          * from getting nulled by their callback functions.
8341          */
8342         mutex_enter(SD_MUTEX(un));
8343         if (un->un_resvd_timeid != NULL) {
8344                 timeout_id_t temp_id = un->un_resvd_timeid;
8345                 un->un_resvd_timeid = NULL;
8346                 mutex_exit(SD_MUTEX(un));
8347                 (void) untimeout(temp_id);
8348                 mutex_enter(SD_MUTEX(un));
8349         }
8350 
8351         if (un->un_reset_throttle_timeid != NULL) {
8352                 timeout_id_t temp_id = un->un_reset_throttle_timeid;
8353                 un->un_reset_throttle_timeid = NULL;
8354                 mutex_exit(SD_MUTEX(un));
8355                 (void) untimeout(temp_id);
8356                 mutex_enter(SD_MUTEX(un));
8357         }
8358 
8359         if (un->un_startstop_timeid != NULL) {
8360                 timeout_id_t temp_id = un->un_startstop_timeid;
8361                 un->un_startstop_timeid = NULL;
8362                 mutex_exit(SD_MUTEX(un));
8363                 (void) untimeout(temp_id);
8364                 mutex_enter(SD_MUTEX(un));
8365         }
8366 
8367         if (un->un_rmw_msg_timeid != NULL) {
8368                 timeout_id_t temp_id = un->un_rmw_msg_timeid;
8369                 un->un_rmw_msg_timeid = NULL;
8370                 mutex_exit(SD_MUTEX(un));
8371                 (void) untimeout(temp_id);
8372                 mutex_enter(SD_MUTEX(un));
8373         }
8374 
8375         if (un->un_dcvb_timeid != NULL) {
8376                 timeout_id_t temp_id = un->un_dcvb_timeid;
8377                 un->un_dcvb_timeid = NULL;
8378                 mutex_exit(SD_MUTEX(un));
8379                 (void) untimeout(temp_id);
8380         } else {
8381                 mutex_exit(SD_MUTEX(un));
8382         }
8383 
8384         /* Remove any pending reservation reclaim requests for this device */
8385         sd_rmv_resv_reclaim_req(dev);
8386 
8387         mutex_enter(SD_MUTEX(un));
8388         if (un->un_retry_timeid != NULL) {
8389                 timeout_id_t temp_id = un->un_retry_timeid;
8390                 un->un_retry_timeid = NULL;
8391                 mutex_exit(SD_MUTEX(un));
8392                 (void) untimeout(temp_id);
8393                 mutex_enter(SD_MUTEX(un));
8394 
8395                 if (un->un_retry_bp != NULL) {
8396                         un->un_retry_bp->av_forw = un->un_waitq_headp;
8397                         un->un_waitq_headp = un->un_retry_bp;
8398                         if (un->un_waitq_tailp == NULL)
8399                                 un->un_waitq_tailp = un->un_retry_bp;
8400                         un->un_retry_bp = NULL;
8401                         un->un_retry_statp = NULL;
8402                 }
8403         }
8404 
8405         if (DEVI_IS_GONE(SD_DEVINFO(un))) {
8406                 /* abort in-flight IO */
8407                 (void) scsi_abort(SD_ADDRESS(un), NULL);
8408                 /* abort pending IO */
8409                 un->un_failfast_state = SD_FAILFAST_ACTIVE;
8410                 un->un_failfast_bp = NULL;
8411                 sd_failfast_flushq(un, B_TRUE);
8412         }
8413 
8414         /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */
8415         if (un->un_direct_priority_timeid != NULL) {
8416                 timeout_id_t temp_id = un->un_direct_priority_timeid;
8417                 un->un_direct_priority_timeid = NULL;
8418                 mutex_exit(SD_MUTEX(un));
8419                 (void) untimeout(temp_id);
8420                 mutex_enter(SD_MUTEX(un));
8421         }
8422 
8423         /* Cancel any active multi-host disk watch thread requests */
8424         if (un->un_mhd_token != NULL) {
8425                 mutex_exit(SD_MUTEX(un));
8426                 if (scsi_watch_request_terminate(un->un_mhd_token,
8427                     SCSI_WATCH_TERMINATE_NOWAIT)) {
8428                         SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8429                             "%s: cannot cancel mhd watch request\n", __func__);
8430                         /*
8431                          * Note: We are returning here after having removed
8432                          * some driver timeouts above. This is consistent with
8433                          * the legacy implementation but perhaps the watch
8434                          * terminate call should be made with the wait flag set.
8435                          */
8436                         goto err_remove_event;
8437                 }
8438                 mutex_enter(SD_MUTEX(un));
8439                 un->un_mhd_token = NULL;
8440         }
8441 
8442         if (un->un_swr_token != NULL) {
8443                 mutex_exit(SD_MUTEX(un));
8444                 if (scsi_watch_request_terminate(un->un_swr_token,
8445                     SCSI_WATCH_TERMINATE_NOWAIT)) {
8446                         SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8447                             "%s: cannot cancel swr watch request\n", __func__);
8448                         /*
8449                          * Note: We are returning here after having removed
8450                          * some driver timeouts above. This is consistent with
8451                          * the legacy implementation but perhaps the watch
8452                          * terminate call should be made with the wait flag set.
8453                          */
8454                         goto err_remove_event;
8455                 }
8456                 mutex_enter(SD_MUTEX(un));
8457                 un->un_swr_token = NULL;
8458         }
8459 
8460         /*
8461          * Clear any scsi_reset_notifies. We clear the reset notifies
8462          * if we have not registered one.
8463          * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX!
8464          */
8465         mutex_exit(SD_MUTEX(un));
8466         (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
8467             sd_mhd_reset_notify_cb, (caddr_t)un);
8468 
8469         mutex_enter(&un->un_pm_mutex);
8470         if (un->un_pm_idle_timeid != NULL) {
8471                 timeout_id_t temp_id = un->un_pm_idle_timeid;
8472                 un->un_pm_idle_timeid = NULL;
8473                 mutex_exit(&un->un_pm_mutex);
8474 
8475                 /*
8476                  * Timeout is active; cancel it.
8477                  * Note that it'll never be active on a device
8478                  * that does not support PM therefore we don't
8479                  * have to check before calling pm_idle_component.
8480                  */
8481                 (void) untimeout(temp_id);
8482                 (void) pm_idle_component(SD_DEVINFO(un), 0);
8483                 mutex_enter(&un->un_pm_mutex);
8484         }
8485 
8486         /*
8487          * Check whether there is already a timeout scheduled for power
8488          * management. If yes then don't lower the power here, that's.
8489          * the timeout handler's job.
8490          */
8491         if (un->un_pm_timeid != NULL) {
8492                 timeout_id_t temp_id = un->un_pm_timeid;
8493                 un->un_pm_timeid = NULL;
8494                 mutex_exit(&un->un_pm_mutex);
8495                 /*
8496                  * Timeout is active; cancel it.
8497                  * Note that it'll never be active on a device
8498                  * that does not support PM therefore we don't
8499                  * have to check before calling pm_idle_component.
8500                  */
8501                 (void) untimeout(temp_id);
8502                 (void) pm_idle_component(SD_DEVINFO(un), 0);
8503 
8504         } else {
8505                 mutex_exit(&un->un_pm_mutex);
8506                 if ((un->un_f_pm_is_enabled == TRUE) &&
8507                     (pm_lower_power(SD_DEVINFO(un), 0, SD_PM_STATE_STOPPED(un))
8508                     != DDI_SUCCESS)) {
8509                         SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8510                             "%s: lower power request failed, ignoring\n",
8511                             __func__);
8512                         /*
8513                          * The above test now includes a check to see if PM is
8514                          * supported by this device before call
8515                          * pm_lower_power().
8516                          * Note, the following is not dead code. The call to
8517                          * pm_lower_power above will generate a call back into
8518                          * our sdpower routine which might result in a timeout
8519                          * handler getting activated. Therefore the following
8520                          * code is valid and necessary.
8521                          */
8522                         mutex_enter(&un->un_pm_mutex);
8523                         if (un->un_pm_timeid != NULL) {
8524                                 timeout_id_t temp_id = un->un_pm_timeid;
8525                                 un->un_pm_timeid = NULL;
8526                                 mutex_exit(&un->un_pm_mutex);
8527                                 (void) untimeout(temp_id);
8528                                 (void) pm_idle_component(SD_DEVINFO(un), 0);
8529                         } else {
8530                                 mutex_exit(&un->un_pm_mutex);
8531                         }
8532                 }
8533         }
8534 
8535         /*
8536          * Cleanup from the scsi_ifsetcap() calls (437868)
8537          * Relocated here from above to be after the call to
8538          * pm_lower_power, which was getting errors.
8539          */
8540         (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8541         (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8542 
8543         /*
8544          * Currently, tagged queuing is supported per target based by HBA.
8545          * Setting this per lun instance actually sets the capability of this
8546          * target in HBA, which affects those luns already attached on the
8547          * same target. So during detach, we can only disable this capability
8548          * only when this is the only lun left on this target. By doing
8549          * this, we assume a target has the same tagged queuing capability
8550          * for every lun. The condition can be removed when HBA is changed to
8551          * support per lun based tagged queuing capability.
8552          */
8553         if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) {
8554                 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8555         }
8556 
8557         if (un->un_f_is_fibre == FALSE) {
8558                 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8559         }
8560 
8561         /*
8562          * Remove any event callbacks, fibre only
8563          */
8564         if (un->un_f_is_fibre == TRUE) {
8565                 if ((un->un_insert_event != NULL) &&
8566                     (ddi_remove_event_handler(un->un_insert_cb_id) !=
8567                     DDI_SUCCESS)) {
8568                         /*
8569                          * Note: We are returning here after having done
8570                          * substantial cleanup above. This is consistent
8571                          * with the legacy implementation but this may not
8572                          * be the right thing to do.
8573                          */
8574                         SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8575                             "%s: cannot cancel insert event\n", __func__);
8576                         goto err_remove_event;
8577                 }
8578                 un->un_insert_event = NULL;
8579 
8580                 if ((un->un_remove_event != NULL) &&
8581                     (ddi_remove_event_handler(un->un_remove_cb_id) !=
8582                     DDI_SUCCESS)) {
8583                         /*
8584                          * Note: We are returning here after having done
8585                          * substantial cleanup above. This is consistent
8586                          * with the legacy implementation but this may not
8587                          * be the right thing to do.
8588                          */
8589                         SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8590                             "%s: cannot cancel remove event\n", __func__);
8591                         goto err_remove_event;
8592                 }
8593                 un->un_remove_event = NULL;
8594         }
8595 
8596         /* Do not free the softstate if the callback routine is active */
8597         sd_sync_with_callback(un);
8598 
8599 no_attach_cleanup:
8600         /*
8601          * The driver must wait, at least attempt to wait, for any commands
8602          * still in the driver.
8603          */
8604         mutex_enter(SD_MUTEX(un));
8605 
8606         while (un->un_ncmds_in_driver != 0) {
8607                 clock_t max_delay = ddi_get_lbolt() + SEC_TO_TICK(30);
8608                 un->un_f_detach_waiting = 1;
8609                 if (cv_timedwait(&un->un_detach_cv, SD_MUTEX(un),
8610                     max_delay) == -1) {
8611                         break;
8612                 }
8613         }
8614 
8615         un->un_f_detach_waiting = 0;
8616         mutex_exit(SD_MUTEX(un));
8617 
8618         cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
8619         cmlb_free_handle(&un->un_cmlbhandle);
8620 
8621         /*
8622          * Hold the detach mutex here, to make sure that no other threads ever
8623          * can access a (partially) freed soft state structure.
8624          */
8625         mutex_enter(&sd_detach_mutex);
8626 
8627         /*
8628          * Clean up the soft state struct.
8629          * Cleanup is done in reverse order of allocs/inits.
8630          * At this point there should be no competing threads anymore.
8631          */
8632 
8633         scsi_fm_fini(devp);
8634 
8635         /*
8636          * Deallocate memory for SCSI FMA.
8637          */
8638         kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
8639 
8640         /*
8641          * Unregister and free device id if it was not registered
8642          * by the transport.
8643          */
8644         if (un->un_f_devid_transport_defined == FALSE)
8645                 ddi_devid_unregister(devi);
8646 
8647         /*
8648          * free the devid structure if allocated before (by ddi_devid_init()
8649          * or ddi_devid_get()).
8650          */
8651         if (un->un_devid) {
8652                 ddi_devid_free(un->un_devid);
8653                 un->un_devid = NULL;
8654         }
8655 
8656         /*
8657          * Destroy wmap cache if it exists.
8658          */
8659         if (un->un_wm_cache != NULL) {
8660                 kmem_cache_destroy(un->un_wm_cache);
8661                 un->un_wm_cache = NULL;
8662         }
8663 
8664         /*
8665          * kstat cleanup is done in detach for all device types (4363169).
8666          * We do not want to fail detach if the device kstats are not deleted
8667          * since there is a confusion about the devo_refcnt for the device.
8668          * We just delete the kstats and let detach complete successfully.
8669          */
8670         if (un->un_stats != NULL) {
8671                 kstat_delete(un->un_stats);
8672                 un->un_stats = NULL;
8673         }
8674         if (un->un_unmapstats != NULL) {
8675                 kstat_delete(un->un_unmapstats_ks);
8676                 un->un_unmapstats_ks = NULL;
8677                 un->un_unmapstats = NULL;
8678         }
8679         if (un->un_lat_ksp != NULL) {
8680                 kstat_delete(un->un_lat_ksp);
8681                 un->un_lat_stats = NULL;
8682                 un->un_lat_ksp = NULL;
8683         }
8684         if (un->un_errstats != NULL) {
8685                 kstat_delete(un->un_errstats);
8686                 un->un_errstats = NULL;
8687         }
8688 
8689         /* Remove partition stats */
8690         if (un->un_f_pkstats_enabled) {
8691                 for (i = 0; i < NSDMAP; i++) {
8692                         if (un->un_pstats[i] != NULL) {
8693                                 kstat_delete(un->un_pstats[i]);
8694                                 un->un_pstats[i] = NULL;
8695                         }
8696                 }
8697         }
8698 
8699         /* Remove xbuf registration */
8700         ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8701         ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8702 
8703         /* Remove driver properties */
8704         ddi_prop_remove_all(devi);
8705 
8706         mutex_destroy(&un->un_pm_mutex);
8707         cv_destroy(&un->un_pm_busy_cv);
8708 
8709         cv_destroy(&un->un_wcc_cv);
8710 
8711         /* Open/close semaphore */
8712         sema_destroy(&un->un_semoclose);
8713 
8714         /* Used to wait for outstanding commands */
8715         cv_destroy(&un->un_detach_cv);
8716 
8717         /* Removable media condvar. */
8718         cv_destroy(&un->un_state_cv);
8719 
8720         /* Suspend/resume condvar. */
8721         cv_destroy(&un->un_suspend_cv);
8722         cv_destroy(&un->un_disk_busy_cv);
8723 
8724         sd_free_rqs(un);
8725 
8726         /* Free up soft state */
8727         devp->sd_private = NULL;
8728 
8729         bzero(un, sizeof (struct sd_lun));
8730 
8731         ddi_soft_state_free(sd_state, instance);
8732 
8733         mutex_exit(&sd_detach_mutex);
8734 
8735         /* This frees up the INQUIRY data associated with the device. */
8736         scsi_unprobe(devp);
8737 
8738         /*
8739          * After successfully detaching an instance, we update the information
8740          * of how many luns have been attached in the relative target and
8741          * controller for parallel SCSI. This information is used when sd tries
8742          * to set the tagged queuing capability in HBA.
8743          * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to
8744          * check if the device is parallel SCSI. However, we don't need to
8745          * check here because we've already checked during attach. No device
8746          * that is not parallel SCSI is in the chain.
8747          */
8748         if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) {
8749                 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH);
8750         }
8751 
8752         ddi_remove_minor_node(devi, NULL);
8753         (void) devfs_clean(devi, NULL, DV_CLEAN_FORCE);
8754 
8755         return (DDI_SUCCESS);
8756 
8757 err_notclosed:
8758         mutex_exit(SD_MUTEX(un));
8759 
8760 err_remove_event:
8761         mutex_enter(&sd_detach_mutex);
8762         un->un_detach_count--;
8763         mutex_exit(&sd_detach_mutex);
8764 
8765         SD_TRACE(SD_LOG_ATTACH_DETACH, un, "%s: exit failure\n", __func__);
8766         return (DDI_FAILURE);
8767 }
8768 
8769 
8770 /*
8771  *    Function: sd_create_errstats
8772  *
8773  * Description: This routine instantiates the device error stats.
8774  *
8775  *              Note: During attach the stats are instantiated first so they are
8776  *              available for attach-time routines that utilize the driver
8777  *              iopath to send commands to the device. The stats are initialized
8778  *              separately so data obtained during some attach-time routines is
8779  *              available. (4362483)
8780  *
8781  *   Arguments: un - driver soft state (unit) structure
8782  *              instance - driver instance
8783  *
8784  *     Context: Kernel thread context
8785  */
8786 
8787 static void
8788 sd_create_errstats(struct sd_lun *un, int instance)
8789 {
8790         struct  sd_errstats     *stp;
8791         char    kstatmodule_err[KSTAT_STRLEN];
8792         char    kstatname[KSTAT_STRLEN];
8793         int     ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t));
8794 
8795         ASSERT(un != NULL);
8796 
8797         if (un->un_errstats != NULL) {
8798                 return;
8799         }
8800 
8801         (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
8802             "%serr", sd_label);
8803         (void) snprintf(kstatname, sizeof (kstatname),
8804             "%s%d,err", sd_label, instance);
8805 
8806         un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname,
8807             "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
8808 
8809         if (un->un_errstats == NULL) {
8810                 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8811                     "sd_create_errstats: Failed kstat_create\n");
8812                 return;
8813         }
8814 
8815         stp = (struct sd_errstats *)un->un_errstats->ks_data;
8816         kstat_named_init(&stp->sd_softerrs,      "Soft Errors",
8817             KSTAT_DATA_UINT32);
8818         kstat_named_init(&stp->sd_harderrs,      "Hard Errors",
8819             KSTAT_DATA_UINT32);
8820         kstat_named_init(&stp->sd_transerrs,     "Transport Errors",
8821             KSTAT_DATA_UINT32);
8822         kstat_named_init(&stp->sd_vid,           "Vendor",
8823             KSTAT_DATA_CHAR);
8824         kstat_named_init(&stp->sd_pid,           "Product",
8825             KSTAT_DATA_CHAR);
8826         kstat_named_init(&stp->sd_revision,      "Revision",
8827             KSTAT_DATA_CHAR);
8828         kstat_named_init(&stp->sd_serial,        "Serial No",
8829             KSTAT_DATA_CHAR);
8830         kstat_named_init(&stp->sd_capacity,      "Size",
8831             KSTAT_DATA_ULONGLONG);
8832         kstat_named_init(&stp->sd_rq_media_err,  "Media Error",
8833             KSTAT_DATA_UINT32);
8834         kstat_named_init(&stp->sd_rq_ntrdy_err,  "Device Not Ready",
8835             KSTAT_DATA_UINT32);
8836         kstat_named_init(&stp->sd_rq_nodev_err,  "No Device",
8837             KSTAT_DATA_UINT32);
8838         kstat_named_init(&stp->sd_rq_recov_err,  "Recoverable",
8839             KSTAT_DATA_UINT32);
8840         kstat_named_init(&stp->sd_rq_illrq_err,  "Illegal Request",
8841             KSTAT_DATA_UINT32);
8842         kstat_named_init(&stp->sd_rq_pfa_err,    "Predictive Failure Analysis",
8843             KSTAT_DATA_UINT32);
8844 
8845         un->un_errstats->ks_private = un;
8846         un->un_errstats->ks_update  = nulldev;
8847 
8848         kstat_install(un->un_errstats);
8849 }
8850 
8851 
8852 /*
8853  *    Function: sd_set_errstats
8854  *
8855  * Description: This routine sets the value of the vendor id, product id,
8856  *              revision, serial number, and capacity device error stats.
8857  *
8858  *              Note: During attach the stats are instantiated first so they are
8859  *              available for attach-time routines that utilize the driver
8860  *              iopath to send commands to the device. The stats are initialized
8861  *              separately so data obtained during some attach-time routines is
8862  *              available. (4362483)
8863  *
8864  *   Arguments: un - driver soft state (unit) structure
8865  *
8866  *     Context: Kernel thread context
8867  */
8868 
8869 static void
8870 sd_set_errstats(struct sd_lun *un)
8871 {
8872         struct  sd_errstats     *stp;
8873         char                    *sn;
8874 
8875         ASSERT(un != NULL);
8876         ASSERT(un->un_errstats != NULL);
8877         stp = (struct sd_errstats *)un->un_errstats->ks_data;
8878         ASSERT(stp != NULL);
8879         (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8);
8880         (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16);
8881         (void) strncpy(stp->sd_revision.value.c,
8882             un->un_sd->sd_inq->inq_revision, 4);
8883 
8884         /*
8885          * All the errstats are persistent across detach/attach,
8886          * so reset all the errstats here in case of the hot
8887          * replacement of disk drives, except for not changed
8888          * Sun qualified drives.
8889          */
8890         if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) ||
8891             (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
8892             sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) {
8893                 stp->sd_softerrs.value.ui32 = 0;
8894                 stp->sd_harderrs.value.ui32 = 0;
8895                 stp->sd_transerrs.value.ui32 = 0;
8896                 stp->sd_rq_media_err.value.ui32 = 0;
8897                 stp->sd_rq_ntrdy_err.value.ui32 = 0;
8898                 stp->sd_rq_nodev_err.value.ui32 = 0;
8899                 stp->sd_rq_recov_err.value.ui32 = 0;
8900                 stp->sd_rq_illrq_err.value.ui32 = 0;
8901                 stp->sd_rq_pfa_err.value.ui32 = 0;
8902         }
8903 
8904         /*
8905          * Set the "Serial No" kstat for Sun qualified drives (indicated by
8906          * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid)
8907          * (4376302))
8908          */
8909         if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) {
8910                 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
8911                     sizeof (SD_INQUIRY(un)->inq_serial));
8912         } else {
8913                 /*
8914                  * Set the "Serial No" kstat for non-Sun qualified drives
8915                  */
8916                 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, SD_DEVINFO(un),
8917                     DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
8918                     INQUIRY_SERIAL_NO, &sn) == DDI_SUCCESS) {
8919                         (void) strlcpy(stp->sd_serial.value.c, sn,
8920                             sizeof (stp->sd_serial.value.c));
8921                         ddi_prop_free(sn);
8922                 }
8923         }
8924 
8925         if (un->un_f_blockcount_is_valid != TRUE) {
8926                 /*
8927                  * Set capacity error stat to 0 for no media. This ensures
8928                  * a valid capacity is displayed in response to 'iostat -E'
8929                  * when no media is present in the device.
8930                  */
8931                 stp->sd_capacity.value.ui64 = 0;
8932         } else {
8933                 /*
8934                  * Multiply un_blockcount by un->un_sys_blocksize to get
8935                  * capacity.
8936                  *
8937                  * Note: for non-512 blocksize devices "un_blockcount" has been
8938                  * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by
8939                  * (un_tgt_blocksize / un->un_sys_blocksize).
8940                  */
8941                 stp->sd_capacity.value.ui64 = (uint64_t)
8942                     ((uint64_t)un->un_blockcount * un->un_sys_blocksize);
8943         }
8944 }
8945 
8946 
8947 /*
8948  *    Function: sd_set_pstats
8949  *
8950  * Description: This routine instantiates and initializes the partition
8951  *              stats for each partition with more than zero blocks.
8952  *              (4363169)
8953  *
8954  *   Arguments: un - driver soft state (unit) structure
8955  *
8956  *     Context: Kernel thread context
8957  */
8958 
8959 static void
8960 sd_set_pstats(struct sd_lun *un)
8961 {
8962         char    kstatname[KSTAT_STRLEN];
8963         int     instance;
8964         int     i;
8965         diskaddr_t      nblks = 0;
8966         char    *partname = NULL;
8967 
8968         ASSERT(un != NULL);
8969 
8970         instance = ddi_get_instance(SD_DEVINFO(un));
8971 
8972         /* XXX is this a VTOC8/VTOC16 difference? */
8973         for (i = 0; i < NSDMAP; i++) {
8974                 if (cmlb_partinfo(un->un_cmlbhandle, i,
8975                     &nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0)
8976                         continue;
8977                 mutex_enter(SD_MUTEX(un));
8978 
8979                 if ((un->un_pstats[i] == NULL) &&
8980                     (nblks != 0)) {
8981 
8982                         (void) snprintf(kstatname, sizeof (kstatname),
8983                             "%s%d,%s", sd_label, instance,
8984                             partname);
8985 
8986                         un->un_pstats[i] = kstat_create(sd_label,
8987                             instance, kstatname, "partition", KSTAT_TYPE_IO,
8988                             1, KSTAT_FLAG_PERSISTENT);
8989                         if (un->un_pstats[i] != NULL) {
8990                                 un->un_pstats[i]->ks_lock = SD_MUTEX(un);
8991                                 kstat_install(un->un_pstats[i]);
8992                         }
8993                 }
8994                 mutex_exit(SD_MUTEX(un));
8995         }
8996 }
8997 
8998 /*
8999  * Values related to caching mode page depending on whether the unit is ATAPI.
9000  */
9001 #define SDC_CDB_GROUP(un) ((un->un_f_cfg_is_atapi == TRUE) ? \
9002         CDB_GROUP1 : CDB_GROUP0)
9003 #define SDC_HDRLEN(un) ((un->un_f_cfg_is_atapi == TRUE) ? \
9004         MODE_HEADER_LENGTH_GRP2 : MODE_HEADER_LENGTH)
9005 /*
9006  * Use mode_cache_scsi3 to ensure we get all of the mode sense data, otherwise
9007  * the mode select will fail (mode_cache_scsi3 is a superset of mode_caching).
9008  */
9009 #define SDC_BUFLEN(un) (SDC_HDRLEN(un) + MODE_BLK_DESC_LENGTH + \
9010         sizeof (struct mode_cache_scsi3))
9011 
9012 static int
9013 sd_get_caching_mode_page(sd_ssc_t *ssc, uchar_t page_control, uchar_t **header,
9014     int *bdlen)
9015 {
9016         struct sd_lun   *un = ssc->ssc_un;
9017         struct mode_caching *mode_caching_page;
9018         size_t          buflen = SDC_BUFLEN(un);
9019         int             hdrlen = SDC_HDRLEN(un);
9020         int             rval;
9021 
9022         /*
9023          * Do a test unit ready, otherwise a mode sense may not work if this
9024          * is the first command sent to the device after boot.
9025          */
9026         if (sd_send_scsi_TEST_UNIT_READY(ssc, 0) != 0)
9027                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9028 
9029         /*
9030          * Allocate memory for the retrieved mode page and its headers.  Set
9031          * a pointer to the page itself.
9032          */
9033         *header = kmem_zalloc(buflen, KM_SLEEP);
9034 
9035         /* Get the information from the device */
9036         rval = sd_send_scsi_MODE_SENSE(ssc, SDC_CDB_GROUP(un), *header, buflen,
9037             page_control | MODEPAGE_CACHING, SD_PATH_DIRECT);
9038         if (rval != 0) {
9039                 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, "%s: Mode Sense Failed\n",
9040                     __func__);
9041                 goto mode_sense_failed;
9042         }
9043 
9044         /*
9045          * Determine size of Block Descriptors in order to locate
9046          * the mode page data. ATAPI devices return 0, SCSI devices
9047          * should return MODE_BLK_DESC_LENGTH.
9048          */
9049         if (un->un_f_cfg_is_atapi == TRUE) {
9050                 struct mode_header_grp2 *mhp =
9051                     (struct mode_header_grp2 *)(*header);
9052                 *bdlen = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
9053         } else {
9054                 *bdlen = ((struct mode_header *)(*header))->bdesc_length;
9055         }
9056 
9057         if (*bdlen > MODE_BLK_DESC_LENGTH) {
9058                 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0,
9059                     "%s: Mode Sense returned invalid block descriptor length\n",
9060                     __func__);
9061                 rval = EIO;
9062                 goto mode_sense_failed;
9063         }
9064 
9065         mode_caching_page = (struct mode_caching *)(*header + hdrlen + *bdlen);
9066         if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) {
9067                 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
9068                     "%s: Mode Sense caching page code mismatch %d\n",
9069                     __func__, mode_caching_page->mode_page.code);
9070                 rval = EIO;
9071         }
9072 
9073 mode_sense_failed:
9074         if (rval != 0) {
9075                 kmem_free(*header, buflen);
9076                 *header = NULL;
9077                 *bdlen = 0;
9078         }
9079         return (rval);
9080 }
9081 
9082 /*
9083  *    Function: sd_cache_control()
9084  *
9085  * Description: This routine is the driver entry point for setting
9086  *              read and write caching by modifying the WCE (write cache
9087  *              enable) and RCD (read cache disable) bits of mode
9088  *              page 8 (MODEPAGE_CACHING).
9089  *
9090  *   Arguments: ssc             - ssc contains pointer to driver soft state
9091  *                                (unit) structure for this target.
9092  *              rcd_flag        - flag for controlling the read cache
9093  *              wce_flag        - flag for controlling the write cache
9094  *
9095  * Return Code: EIO
9096  *              code returned by sd_send_scsi_MODE_SENSE and
9097  *              sd_send_scsi_MODE_SELECT
9098  *
9099  *     Context: Kernel Thread
9100  */
9101 
9102 static int
9103 sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag)
9104 {
9105         struct sd_lun   *un = ssc->ssc_un;
9106         struct mode_caching *mode_caching_page;
9107         uchar_t         *header;
9108         size_t          buflen = SDC_BUFLEN(un);
9109         int             hdrlen = SDC_HDRLEN(un);
9110         int             bdlen;
9111         int             rval;
9112 
9113         rval = sd_get_caching_mode_page(ssc, MODEPAGE_CURRENT, &header, &bdlen);
9114         switch (rval) {
9115         case 0:
9116                 /* Check the relevant bits on successful mode sense */
9117                 mode_caching_page = (struct mode_caching *)(header + hdrlen +
9118                     bdlen);
9119                 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) ||
9120                     (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) ||
9121                     (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) ||
9122                     (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) {
9123                         size_t sbuflen;
9124                         uchar_t save_pg;
9125 
9126                         /*
9127                          * Construct select buffer length based on the
9128                          * length of the sense data returned.
9129                          */
9130                         sbuflen = hdrlen + bdlen + sizeof (struct mode_page) +
9131                             (int)mode_caching_page->mode_page.length;
9132 
9133                         /* Set the caching bits as requested */
9134                         if (rcd_flag == SD_CACHE_ENABLE)
9135                                 mode_caching_page->rcd = 0;
9136                         else if (rcd_flag == SD_CACHE_DISABLE)
9137                                 mode_caching_page->rcd = 1;
9138 
9139                         if (wce_flag == SD_CACHE_ENABLE)
9140                                 mode_caching_page->wce = 1;
9141                         else if (wce_flag == SD_CACHE_DISABLE)
9142                                 mode_caching_page->wce = 0;
9143 
9144                         /*
9145                          * Save the page if the mode sense says the
9146                          * drive supports it.
9147                          */
9148                         save_pg = mode_caching_page->mode_page.ps ?
9149                             SD_SAVE_PAGE : SD_DONTSAVE_PAGE;
9150 
9151                         /* Clear reserved bits before mode select */
9152                         mode_caching_page->mode_page.ps = 0;
9153 
9154                         /*
9155                          * Clear out mode header for mode select.
9156                          * The rest of the retrieved page will be reused.
9157                          */
9158                         bzero(header, hdrlen);
9159 
9160                         if (un->un_f_cfg_is_atapi == TRUE) {
9161                                 struct mode_header_grp2 *mhp =
9162                                     (struct mode_header_grp2 *)header;
9163                                 mhp->bdesc_length_hi = bdlen >> 8;
9164                                 mhp->bdesc_length_lo = (uchar_t)bdlen & 0xff;
9165                         } else {
9166                                 ((struct mode_header *)header)->bdesc_length =
9167                                     bdlen;
9168                         }
9169 
9170                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9171 
9172                         /* Issue mode select to change the cache settings */
9173                         rval = sd_send_scsi_MODE_SELECT(ssc, SDC_CDB_GROUP(un),
9174                             header, sbuflen, save_pg, SD_PATH_DIRECT);
9175                 }
9176                 kmem_free(header, buflen);
9177                 break;
9178         case EIO:
9179                 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9180                 break;
9181         default:
9182                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9183                 break;
9184         }
9185 
9186         return (rval);
9187 }
9188 
9189 
9190 /*
9191  *    Function: sd_get_write_cache_enabled()
9192  *
9193  * Description: This routine is the driver entry point for determining if write
9194  *              caching is enabled.  It examines the WCE (write cache enable)
9195  *              bits of mode page 8 (MODEPAGE_CACHING) with Page Control field
9196  *              bits set to MODEPAGE_CURRENT.
9197  *
9198  *   Arguments: ssc             - ssc contains pointer to driver soft state
9199  *                                (unit) structure for this target.
9200  *              is_enabled      - pointer to int where write cache enabled state
9201  *                                is returned (non-zero -> write cache enabled)
9202  *
9203  * Return Code: EIO
9204  *              code returned by sd_send_scsi_MODE_SENSE
9205  *
9206  *     Context: Kernel Thread
9207  *
9208  * NOTE: If ioctl is added to disable write cache, this sequence should
9209  * be followed so that no locking is required for accesses to
9210  * un->un_f_write_cache_enabled:
9211  *      do mode select to clear wce
9212  *      do synchronize cache to flush cache
9213  *      set un->un_f_write_cache_enabled = FALSE
9214  *
9215  * Conversely, an ioctl to enable the write cache should be done
9216  * in this order:
9217  *      set un->un_f_write_cache_enabled = TRUE
9218  *      do mode select to set wce
9219  */
9220 
9221 static int
9222 sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled)
9223 {
9224         struct sd_lun   *un = ssc->ssc_un;
9225         struct mode_caching *mode_caching_page;
9226         uchar_t         *header;
9227         size_t          buflen = SDC_BUFLEN(un);
9228         int             hdrlen = SDC_HDRLEN(un);
9229         int             bdlen;
9230         int             rval;
9231 
9232         /* In case of error, flag as enabled */
9233         *is_enabled = TRUE;
9234 
9235         rval = sd_get_caching_mode_page(ssc, MODEPAGE_CURRENT, &header, &bdlen);
9236         switch (rval) {
9237         case 0:
9238                 mode_caching_page = (struct mode_caching *)(header + hdrlen +
9239                     bdlen);
9240                 *is_enabled = mode_caching_page->wce;
9241                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
9242                 kmem_free(header, buflen);
9243                 break;
9244         case EIO: {
9245                 /*
9246                  * Some disks do not support Mode Sense(6), we
9247                  * should ignore this kind of error (sense key is
9248                  * 0x5 - illegal request).
9249                  */
9250                 uint8_t *sensep;
9251                 int senlen;
9252 
9253                 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
9254                 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
9255                     ssc->ssc_uscsi_cmd->uscsi_rqresid);
9256 
9257                 if (senlen > 0 &&
9258                     scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
9259                         sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
9260                 } else {
9261                         sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9262                 }
9263                 break;
9264         }
9265         default:
9266                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9267                 break;
9268         }
9269 
9270         return (rval);
9271 }
9272 
9273 /*
9274  *    Function: sd_get_write_cache_changeable()
9275  *
9276  * Description: This routine is the driver entry point for determining if write
9277  *              caching is changeable.  It examines the WCE (write cache enable)
9278  *              bits of mode page 8 (MODEPAGE_CACHING) with Page Control field
9279  *              bits set to MODEPAGE_CHANGEABLE.
9280  *
9281  *   Arguments: ssc             - ssc contains pointer to driver soft state
9282  *                                (unit) structure for this target.
9283  *              is_changeable   - pointer to int where write cache changeable
9284  *                                state is returned (non-zero -> write cache
9285  *                                changeable)
9286  *
9287  *     Context: Kernel Thread
9288  */
9289 
9290 static void
9291 sd_get_write_cache_changeable(sd_ssc_t *ssc, int *is_changeable)
9292 {
9293         struct sd_lun   *un = ssc->ssc_un;
9294         struct mode_caching *mode_caching_page;
9295         uchar_t         *header;
9296         size_t          buflen = SDC_BUFLEN(un);
9297         int             hdrlen = SDC_HDRLEN(un);
9298         int             bdlen;
9299         int             rval;
9300 
9301         /* In case of error, flag as enabled */
9302         *is_changeable = TRUE;
9303 
9304         rval = sd_get_caching_mode_page(ssc, MODEPAGE_CHANGEABLE, &header,
9305             &bdlen);
9306         switch (rval) {
9307         case 0:
9308                 mode_caching_page = (struct mode_caching *)(header + hdrlen +
9309                     bdlen);
9310                 *is_changeable = mode_caching_page->wce;
9311                 kmem_free(header, buflen);
9312                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
9313                 break;
9314         case EIO:
9315                 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9316                 break;
9317         default:
9318                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9319                 break;
9320         }
9321 }
9322 
9323 /*
9324  *    Function: sd_get_nv_sup()
9325  *
9326  * Description: This routine is the driver entry point for
9327  * determining whether non-volatile cache is supported. This
9328  * determination process works as follows:
9329  *
9330  * 1. sd first queries sd.conf on whether
9331  * suppress_cache_flush bit is set for this device.
9332  *
9333  * 2. if not there, then queries the internal disk table.
9334  *
9335  * 3. if either sd.conf or internal disk table specifies
9336  * cache flush be suppressed, we don't bother checking
9337  * NV_SUP bit.
9338  *
9339  * If SUPPRESS_CACHE_FLUSH bit is not set to 1, sd queries
9340  * the optional INQUIRY VPD page 0x86. If the device
9341  * supports VPD page 0x86, sd examines the NV_SUP
9342  * (non-volatile cache support) bit in the INQUIRY VPD page
9343  * 0x86:
9344  *   o If NV_SUP bit is set, sd assumes the device has a
9345  *   non-volatile cache and set the
9346  *   un_f_sync_nv_supported to TRUE.
9347  *   o Otherwise cache is not non-volatile,
9348  *   un_f_sync_nv_supported is set to FALSE.
9349  *
9350  * Arguments: un - driver soft state (unit) structure
9351  *
9352  * Return Code:
9353  *
9354  *     Context: Kernel Thread
9355  */
9356 
9357 static void
9358 sd_get_nv_sup(sd_ssc_t *ssc)
9359 {
9360         int             rval            = 0;
9361         uchar_t         *inq86          = NULL;
9362         size_t          inq86_len       = MAX_INQUIRY_SIZE;
9363         size_t          inq86_resid     = 0;
9364         struct          dk_callback *dkc;
9365         struct sd_lun   *un;
9366 
9367         ASSERT(ssc != NULL);
9368         un = ssc->ssc_un;
9369         ASSERT(un != NULL);
9370 
9371         mutex_enter(SD_MUTEX(un));
9372 
9373         /*
9374          * Be conservative on the device's support of
9375          * SYNC_NV bit: un_f_sync_nv_supported is
9376          * initialized to be false.
9377          */
9378         un->un_f_sync_nv_supported = FALSE;
9379 
9380         /*
9381          * If either sd.conf or internal disk table
9382          * specifies cache flush be suppressed, then
9383          * we don't bother checking NV_SUP bit.
9384          */
9385         if (un->un_f_suppress_cache_flush == TRUE) {
9386                 mutex_exit(SD_MUTEX(un));
9387                 return;
9388         }
9389 
9390         if (sd_check_vpd_page_support(ssc) == 0 &&
9391             un->un_vpd_page_mask & SD_VPD_EXTENDED_DATA_PG) {
9392                 mutex_exit(SD_MUTEX(un));
9393                 /* collect page 86 data if available */
9394                 inq86 = kmem_zalloc(inq86_len, KM_SLEEP);
9395 
9396                 rval = sd_send_scsi_INQUIRY(ssc, inq86, inq86_len,
9397                     0x01, 0x86, &inq86_resid);
9398 
9399                 if (rval == 0 && (inq86_len - inq86_resid > 6)) {
9400                         SD_TRACE(SD_LOG_COMMON, un,
9401                             "sd_get_nv_sup: successfully get VPD page: %x "
9402                             "PAGE LENGTH: %x BYTE 6: %x\n",
9403                             inq86[1], inq86[3], inq86[6]);
9404 
9405                         mutex_enter(SD_MUTEX(un));
9406                         /*
9407                          * check the value of NV_SUP bit: only if the device
9408                          * reports NV_SUP bit to be 1, the
9409                          * un_f_sync_nv_supported bit will be set to true.
9410                          */
9411                         if (inq86[6] & SD_VPD_NV_SUP) {
9412                                 un->un_f_sync_nv_supported = TRUE;
9413                         }
9414                         mutex_exit(SD_MUTEX(un));
9415                 } else if (rval != 0) {
9416                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9417                 }
9418 
9419                 kmem_free(inq86, inq86_len);
9420         } else {
9421                 mutex_exit(SD_MUTEX(un));
9422         }
9423 
9424         /*
9425          * Send a SYNC CACHE command to check whether
9426          * SYNC_NV bit is supported. This command should have
9427          * un_f_sync_nv_supported set to correct value.
9428          */
9429         mutex_enter(SD_MUTEX(un));
9430         if (un->un_f_sync_nv_supported) {
9431                 mutex_exit(SD_MUTEX(un));
9432                 dkc = kmem_zalloc(sizeof (struct dk_callback), KM_SLEEP);
9433                 dkc->dkc_flag = FLUSH_VOLATILE;
9434                 (void) sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
9435 
9436                 /*
9437                  * Send a TEST UNIT READY command to the device. This should
9438                  * clear any outstanding UNIT ATTENTION that may be present.
9439                  */
9440                 rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
9441                 if (rval != 0)
9442                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9443 
9444                 kmem_free(dkc, sizeof (struct dk_callback));
9445         } else {
9446                 mutex_exit(SD_MUTEX(un));
9447         }
9448 
9449         SD_TRACE(SD_LOG_COMMON, un, "sd_get_nv_sup: \
9450             un_f_suppress_cache_flush is set to %d\n",
9451             un->un_f_suppress_cache_flush);
9452 }
9453 
9454 /*
9455  *    Function: sd_make_device
9456  *
9457  * Description: Utility routine to return the Solaris device number from
9458  *              the data in the device's dev_info structure.
9459  *
9460  * Return Code: The Solaris device number
9461  *
9462  *     Context: Any
9463  */
9464 
9465 static dev_t
9466 sd_make_device(dev_info_t *devi)
9467 {
9468         return (makedevice(ddi_driver_major(devi),
9469             ddi_get_instance(devi) << SDUNIT_SHIFT));
9470 }
9471 
9472 
9473 /*
9474  *    Function: sd_pm_entry
9475  *
9476  * Description: Called at the start of a new command to manage power
9477  *              and busy status of a device. This includes determining whether
9478  *              the current power state of the device is sufficient for
9479  *              performing the command or whether it must be changed.
9480  *              The PM framework is notified appropriately.
9481  *              Only with a return status of DDI_SUCCESS will the
9482  *              component be busy to the framework.
9483  *
9484  *              All callers of sd_pm_entry must check the return status
9485  *              and only call sd_pm_exit it it was DDI_SUCCESS. A status
9486  *              of DDI_FAILURE indicates the device failed to power up.
9487  *              In this case un_pm_count has been adjusted so the result
9488  *              on exit is still powered down, ie. count is less than 0.
9489  *              Calling sd_pm_exit with this count value hits an ASSERT.
9490  *
9491  * Return Code: DDI_SUCCESS or DDI_FAILURE
9492  *
9493  *     Context: Kernel thread context.
9494  */
9495 
9496 static int
9497 sd_pm_entry(struct sd_lun *un)
9498 {
9499         int return_status = DDI_SUCCESS;
9500 
9501         ASSERT(!mutex_owned(SD_MUTEX(un)));
9502         ASSERT(!mutex_owned(&un->un_pm_mutex));
9503 
9504         SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n");
9505 
9506         if (un->un_f_pm_is_enabled == FALSE) {
9507                 SD_TRACE(SD_LOG_IO_PM, un,
9508                     "sd_pm_entry: exiting, PM not enabled\n");
9509                 return (return_status);
9510         }
9511 
9512         /*
9513          * Just increment a counter if PM is enabled. On the transition from
9514          * 0 ==> 1, mark the device as busy.  The iodone side will decrement
9515          * the count with each IO and mark the device as idle when the count
9516          * hits 0.
9517          *
9518          * If the count is less than 0 the device is powered down. If a powered
9519          * down device is successfully powered up then the count must be
9520          * incremented to reflect the power up. Note that it'll get incremented
9521          * a second time to become busy.
9522          *
9523          * Because the following has the potential to change the device state
9524          * and must release the un_pm_mutex to do so, only one thread can be
9525          * allowed through at a time.
9526          */
9527 
9528         mutex_enter(&un->un_pm_mutex);
9529         while (un->un_pm_busy == TRUE) {
9530                 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex);
9531         }
9532         un->un_pm_busy = TRUE;
9533 
9534         if (un->un_pm_count < 1) {
9535 
9536                 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n");
9537 
9538                 /*
9539                  * Indicate we are now busy so the framework won't attempt to
9540                  * power down the device. This call will only fail if either
9541                  * we passed a bad component number or the device has no
9542                  * components. Neither of these should ever happen.
9543                  */
9544                 mutex_exit(&un->un_pm_mutex);
9545                 return_status = pm_busy_component(SD_DEVINFO(un), 0);
9546                 ASSERT(return_status == DDI_SUCCESS);
9547 
9548                 mutex_enter(&un->un_pm_mutex);
9549 
9550                 if (un->un_pm_count < 0) {
9551                         mutex_exit(&un->un_pm_mutex);
9552 
9553                         SD_TRACE(SD_LOG_IO_PM, un,
9554                             "sd_pm_entry: power up component\n");
9555 
9556                         /*
9557                          * pm_raise_power will cause sdpower to be called
9558                          * which brings the device power level to the
9559                          * desired state, If successful, un_pm_count and
9560                          * un_power_level will be updated appropriately.
9561                          */
9562                         return_status = pm_raise_power(SD_DEVINFO(un), 0,
9563                             SD_PM_STATE_ACTIVE(un));
9564 
9565                         mutex_enter(&un->un_pm_mutex);
9566 
9567                         if (return_status != DDI_SUCCESS) {
9568                                 /*
9569                                  * Power up failed.
9570                                  * Idle the device and adjust the count
9571                                  * so the result on exit is that we're
9572                                  * still powered down, ie. count is less than 0.
9573                                  */
9574                                 SD_TRACE(SD_LOG_IO_PM, un,
9575                                     "sd_pm_entry: power up failed,"
9576                                     " idle the component\n");
9577 
9578                                 (void) pm_idle_component(SD_DEVINFO(un), 0);
9579                                 un->un_pm_count--;
9580                         } else {
9581                                 /*
9582                                  * Device is powered up, verify the
9583                                  * count is non-negative.
9584                                  * This is debug only.
9585                                  */
9586                                 ASSERT(un->un_pm_count == 0);
9587                         }
9588                 }
9589 
9590                 if (return_status == DDI_SUCCESS) {
9591                         /*
9592                          * For performance, now that the device has been tagged
9593                          * as busy, and it's known to be powered up, update the
9594                          * chain types to use jump tables that do not include
9595                          * pm. This significantly lowers the overhead and
9596                          * therefore improves performance.
9597                          */
9598 
9599                         mutex_exit(&un->un_pm_mutex);
9600                         mutex_enter(SD_MUTEX(un));
9601                         SD_TRACE(SD_LOG_IO_PM, un,
9602                             "sd_pm_entry: changing uscsi_chain_type from %d\n",
9603                             un->un_uscsi_chain_type);
9604 
9605                         if (un->un_f_non_devbsize_supported) {
9606                                 un->un_buf_chain_type =
9607                                     SD_CHAIN_INFO_RMMEDIA_NO_PM;
9608                         } else {
9609                                 un->un_buf_chain_type =
9610                                     SD_CHAIN_INFO_DISK_NO_PM;
9611                         }
9612                         un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
9613 
9614                         SD_TRACE(SD_LOG_IO_PM, un,
9615                             "             changed  uscsi_chain_type to   %d\n",
9616                             un->un_uscsi_chain_type);
9617                         mutex_exit(SD_MUTEX(un));
9618                         mutex_enter(&un->un_pm_mutex);
9619 
9620                         if (un->un_pm_idle_timeid == NULL) {
9621                                 /* 300 ms. */
9622                                 un->un_pm_idle_timeid =
9623                                     timeout(sd_pm_idletimeout_handler, un,
9624                                     (drv_usectohz((clock_t)300000)));
9625                                 /*
9626                                  * Include an extra call to busy which keeps the
9627                                  * device busy with-respect-to the PM layer
9628                                  * until the timer fires, at which time it'll
9629                                  * get the extra idle call.
9630                                  */
9631                                 (void) pm_busy_component(SD_DEVINFO(un), 0);
9632                         }
9633                 }
9634         }
9635         un->un_pm_busy = FALSE;
9636         /* Next... */
9637         cv_signal(&un->un_pm_busy_cv);
9638 
9639         un->un_pm_count++;
9640 
9641         SD_TRACE(SD_LOG_IO_PM, un,
9642             "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count);
9643 
9644         mutex_exit(&un->un_pm_mutex);
9645 
9646         return (return_status);
9647 }
9648 
9649 
9650 /*
9651  *    Function: sd_pm_exit
9652  *
9653  * Description: Called at the completion of a command to manage busy
9654  *              status for the device. If the device becomes idle the
9655  *              PM framework is notified.
9656  *
9657  *     Context: Kernel thread context
9658  */
9659 
9660 static void
9661 sd_pm_exit(struct sd_lun *un)
9662 {
9663         ASSERT(!mutex_owned(SD_MUTEX(un)));
9664         ASSERT(!mutex_owned(&un->un_pm_mutex));
9665 
9666         SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n");
9667 
9668         /*
9669          * After attach the following flag is only read, so don't
9670          * take the penalty of acquiring a mutex for it.
9671          */
9672         if (un->un_f_pm_is_enabled == TRUE) {
9673 
9674                 mutex_enter(&un->un_pm_mutex);
9675                 un->un_pm_count--;
9676 
9677                 SD_TRACE(SD_LOG_IO_PM, un,
9678                     "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count);
9679 
9680                 ASSERT(un->un_pm_count >= 0);
9681                 if (un->un_pm_count == 0) {
9682                         mutex_exit(&un->un_pm_mutex);
9683 
9684                         SD_TRACE(SD_LOG_IO_PM, un,
9685                             "sd_pm_exit: idle component\n");
9686 
9687                         (void) pm_idle_component(SD_DEVINFO(un), 0);
9688 
9689                 } else {
9690                         mutex_exit(&un->un_pm_mutex);
9691                 }
9692         }
9693 
9694         SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n");
9695 }
9696 
9697 
9698 /*
9699  *    Function: sdopen
9700  *
9701  * Description: Driver's open(9e) entry point function.
9702  *
9703  *   Arguments: dev_i   - pointer to device number
9704  *              flag    - how to open file (FEXCL, FNDELAY, FREAD, FWRITE)
9705  *              otyp    - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
9706  *              cred_p  - user credential pointer
9707  *
9708  * Return Code: EINVAL
9709  *              ENXIO
9710  *              EIO
9711  *              EROFS
9712  *              EBUSY
9713  *
9714  *     Context: Kernel thread context
9715  */
9716 /* ARGSUSED */
9717 static int
9718 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
9719 {
9720         struct sd_lun   *un;
9721         int             nodelay;
9722         int             part;
9723         uint64_t        partmask;
9724         int             instance;
9725         dev_t           dev;
9726         int             rval = EIO;
9727         diskaddr_t      nblks = 0;
9728         diskaddr_t      label_cap;
9729 
9730         /* Validate the open type */
9731         if (otyp >= OTYPCNT) {
9732                 return (EINVAL);
9733         }
9734 
9735         dev = *dev_p;
9736         instance = SDUNIT(dev);
9737         mutex_enter(&sd_detach_mutex);
9738 
9739         /*
9740          * Fail the open if there is no softstate for the instance, or
9741          * if another thread somewhere is trying to detach the instance.
9742          */
9743         if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
9744             un->un_detach_count != 0 || DEVI_IS_GONE(SD_DEVINFO(un))) {
9745                 mutex_exit(&sd_detach_mutex);
9746                 /*
9747                  * The probe cache only needs to be cleared when open (9E) fails
9748                  * with ENXIO.
9749                  */
9750                 sd_scsi_clear_probe_cache();
9751                 return (ENXIO);
9752         }
9753 
9754         /*
9755          * The un_layer_count is to prevent another thread in specfs from
9756          * trying to detach the instance, which can happen when we are
9757          * called from a higher-layer driver instead of thru specfs.
9758          * This will not be needed when DDI provides a layered driver
9759          * interface that allows specfs to know that an instance is in
9760          * use by a layered driver & should not be detached.
9761          *
9762          * Note: the semantics for layered driver opens are exactly one
9763          * close for every open.
9764          */
9765         if (otyp == OTYP_LYR) {
9766                 un->un_layer_count++;
9767         }
9768 
9769         /*
9770          * Keep a count of the current # of opens in progress. This is because
9771          * some layered drivers try to call us as a regular open. This can
9772          * cause problems that we cannot prevent, however by keeping this count
9773          * we can at least keep our open and detach routines from racing against
9774          * each other under such conditions.
9775          */
9776         un->un_opens_in_progress++;
9777         mutex_exit(&sd_detach_mutex);
9778 
9779         nodelay  = (flag & (FNDELAY | FNONBLOCK));
9780         part     = SDPART(dev);
9781         partmask = 1 << part;
9782 
9783         /*
9784          * We use a semaphore here in order to serialize
9785          * open and close requests on the device.
9786          */
9787         sema_p(&un->un_semoclose);
9788 
9789         mutex_enter(SD_MUTEX(un));
9790 
9791         /*
9792          * All device accesses go thru sdstrategy() where we check
9793          * on suspend status but there could be a scsi_poll command,
9794          * which bypasses sdstrategy(), so we need to check pm
9795          * status.
9796          */
9797 
9798         if (!nodelay) {
9799                 while ((un->un_state == SD_STATE_SUSPENDED) ||
9800                     (un->un_state == SD_STATE_PM_CHANGING) ||
9801                     (un->un_state == SD_STATE_ATTACHING)) {
9802                         cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
9803                 }
9804                 mutex_exit(SD_MUTEX(un));
9805                 if (sd_pm_entry(un) != DDI_SUCCESS) {
9806                         rval = EIO;
9807                         SD_ERROR(SD_LOG_OPEN_CLOSE, un,
9808                             "sdopen: sd_pm_entry failed\n");
9809                         goto open_failed_with_pm;
9810                 }
9811                 mutex_enter(SD_MUTEX(un));
9812         } else if (un->un_state == SD_STATE_ATTACH_FAILED) {
9813                 mutex_exit(SD_MUTEX(un));
9814                 rval = EIO;
9815                 SD_ERROR(SD_LOG_OPEN_CLOSE, un,
9816                     "sdopen: attach failed, can't open\n");
9817                 goto open_failed_not_attached;
9818         }
9819 
9820         /* check for previous exclusive open */
9821         SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un);
9822         SD_TRACE(SD_LOG_OPEN_CLOSE, un,
9823             "sdopen: exclopen=%x, flag=%x, regopen=%x\n",
9824             un->un_exclopen, flag, un->un_ocmap.regopen[otyp]);
9825 
9826         if (un->un_exclopen & (partmask)) {
9827                 goto excl_open_fail;
9828         }
9829 
9830         if (flag & FEXCL) {
9831                 int i;
9832                 if (un->un_ocmap.lyropen[part]) {
9833                         goto excl_open_fail;
9834                 }
9835                 for (i = 0; i < (OTYPCNT - 1); i++) {
9836                         if (un->un_ocmap.regopen[i] & (partmask)) {
9837                                 goto excl_open_fail;
9838                         }
9839                 }
9840         }
9841 
9842         /*
9843          * Check the write permission if this is a removable media device,
9844          * NDELAY has not been set, and writable permission is requested.
9845          *
9846          * Note: If NDELAY was set and this is write-protected media the WRITE
9847          * attempt will fail with EIO as part of the I/O processing. This is a
9848          * more permissive implementation that allows the open to succeed and
9849          * WRITE attempts to fail when appropriate.
9850          */
9851         if (un->un_f_chk_wp_open) {
9852                 if ((flag & FWRITE) && (!nodelay)) {
9853                         mutex_exit(SD_MUTEX(un));
9854                         /*
9855                          * Defer the check for write permission on writable
9856                          * DVD drive till sdstrategy and will not fail open even
9857                          * if FWRITE is set as the device can be writable
9858                          * depending upon the media and the media can change
9859                          * after the call to open().
9860                          */
9861                         if (un->un_f_dvdram_writable_device == FALSE) {
9862                                 if (ISCD(un) || sr_check_wp(dev)) {
9863                                 rval = EROFS;
9864                                 mutex_enter(SD_MUTEX(un));
9865                                 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
9866                                     "write to cd or write protected media\n");
9867                                 goto open_fail;
9868                                 }
9869                         }
9870                         mutex_enter(SD_MUTEX(un));
9871                 }
9872         }
9873 
9874         /*
9875          * If opening in NDELAY/NONBLOCK mode, just return.
9876          * Check if disk is ready and has a valid geometry later.
9877          */
9878         if (!nodelay) {
9879                 sd_ssc_t        *ssc;
9880 
9881                 mutex_exit(SD_MUTEX(un));
9882                 ssc = sd_ssc_init(un);
9883                 rval = sd_ready_and_valid(ssc, part);
9884                 sd_ssc_fini(ssc);
9885                 mutex_enter(SD_MUTEX(un));
9886                 /*
9887                  * Fail if device is not ready or if the number of disk
9888                  * blocks is zero or negative for non CD devices.
9889                  */
9890 
9891                 nblks = 0;
9892 
9893                 if (rval == SD_READY_VALID && (!ISCD(un))) {
9894                         /* if cmlb_partinfo fails, nblks remains 0 */
9895                         mutex_exit(SD_MUTEX(un));
9896                         (void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks,
9897                             NULL, NULL, NULL, (void *)SD_PATH_DIRECT);
9898                         mutex_enter(SD_MUTEX(un));
9899                 }
9900 
9901                 if ((rval != SD_READY_VALID) ||
9902                     (!ISCD(un) && nblks <= 0)) {
9903                         rval = un->un_f_has_removable_media ? ENXIO : EIO;
9904                         SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
9905                             "device not ready or invalid disk block value\n");
9906                         goto open_fail;
9907                 }
9908         } else {
9909                 uchar_t *cp;
9910                 /*
9911                  * x86 requires special nodelay handling, so that p0 is
9912                  * always defined and accessible.
9913                  * Invalidate geometry only if device is not already open.
9914                  */
9915                 cp = &un->un_ocmap.chkd[0];
9916                 while (cp < &un->un_ocmap.chkd[OCSIZE]) {
9917                         if (*cp != (uchar_t)0) {
9918                                 break;
9919                         }
9920                         cp++;
9921                 }
9922                 if (cp == &un->un_ocmap.chkd[OCSIZE]) {
9923                         mutex_exit(SD_MUTEX(un));
9924                         cmlb_invalidate(un->un_cmlbhandle,
9925                             (void *)SD_PATH_DIRECT);
9926                         mutex_enter(SD_MUTEX(un));
9927                 }
9928         }
9929 
9930         if (otyp == OTYP_LYR) {
9931                 un->un_ocmap.lyropen[part]++;
9932         } else {
9933                 un->un_ocmap.regopen[otyp] |= partmask;
9934         }
9935 
9936         /* Set up open and exclusive open flags */
9937         if (flag & FEXCL) {
9938                 un->un_exclopen |= (partmask);
9939         }
9940 
9941         /*
9942          * If the lun is EFI labeled and lun capacity is greater than the
9943          * capacity contained in the label, log a sys-event to notify the
9944          * interested module.
9945          * To avoid an infinite loop of logging sys-event, we only log the
9946          * event when the lun is not opened in NDELAY mode. The event handler
9947          * should open the lun in NDELAY mode.
9948          */
9949         if (!nodelay) {
9950                 mutex_exit(SD_MUTEX(un));
9951                 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
9952                     (void*)SD_PATH_DIRECT) == 0) {
9953                         mutex_enter(SD_MUTEX(un));
9954                         if (un->un_f_blockcount_is_valid &&
9955                             un->un_blockcount > label_cap &&
9956                             un->un_f_expnevent == B_FALSE) {
9957                                 un->un_f_expnevent = B_TRUE;
9958                                 mutex_exit(SD_MUTEX(un));
9959                                 sd_log_lun_expansion_event(un,
9960                                     (nodelay ? KM_NOSLEEP : KM_SLEEP));
9961                                 mutex_enter(SD_MUTEX(un));
9962                         }
9963                 } else {
9964                         mutex_enter(SD_MUTEX(un));
9965                 }
9966         }
9967 
9968         SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: "
9969             "open of part %d type %d\n", part, otyp);
9970 
9971         /*
9972          * If we made it here, the disk is alive.
9973          * Make sure it is set to normal state.
9974          */
9975         New_state(un, SD_STATE_NORMAL);
9976 
9977         mutex_exit(SD_MUTEX(un));
9978         if (!nodelay) {
9979                 sd_pm_exit(un);
9980         }
9981 
9982         sema_v(&un->un_semoclose);
9983 
9984         mutex_enter(&sd_detach_mutex);
9985         un->un_opens_in_progress--;
9986         mutex_exit(&sd_detach_mutex);
9987 
9988         SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n");
9989         return (DDI_SUCCESS);
9990 
9991 excl_open_fail:
9992         SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n");
9993         rval = EBUSY;
9994 
9995 open_fail:
9996         mutex_exit(SD_MUTEX(un));
9997 
9998         /*
9999          * On a failed open we must exit the pm management.
10000          */
10001         if (!nodelay) {
10002                 sd_pm_exit(un);
10003         }
10004 open_failed_with_pm:
10005 open_failed_not_attached:
10006         sema_v(&un->un_semoclose);
10007 
10008         mutex_enter(&sd_detach_mutex);
10009         un->un_opens_in_progress--;
10010         if (otyp == OTYP_LYR) {
10011                 un->un_layer_count--;
10012         }
10013         mutex_exit(&sd_detach_mutex);
10014 
10015         return (rval);
10016 }
10017 
10018 
10019 /*
10020  *    Function: sdclose
10021  *
10022  * Description: Driver's close(9e) entry point function.
10023  *
10024  *   Arguments: dev    - device number
10025  *              flag   - file status flag, informational only
10026  *              otyp   - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10027  *              cred_p - user credential pointer
10028  *
10029  * Return Code: ENXIO
10030  *
10031  *     Context: Kernel thread context
10032  */
10033 /* ARGSUSED */
10034 static int
10035 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p)
10036 {
10037         struct sd_lun   *un;
10038         uchar_t         *cp;
10039         int             part;
10040         int             nodelay;
10041         int             rval = 0;
10042 
10043         /* Validate the open type */
10044         if (otyp >= OTYPCNT) {
10045                 return (ENXIO);
10046         }
10047 
10048         /* Hold the detach mutex to allow close to complete */
10049         mutex_enter(&sd_detach_mutex);
10050 
10051         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10052                 mutex_exit(&sd_detach_mutex);
10053                 return (ENXIO);
10054         }
10055 
10056         part = SDPART(dev);
10057         nodelay = flag & (FNDELAY | FNONBLOCK);
10058 
10059         SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10060             "sdclose: close of part %d type %d\n", part, otyp);
10061 
10062         /*
10063          * We use a semaphore here in order to serialize
10064          * open and close requests on the device.
10065          */
10066         sema_p(&un->un_semoclose);
10067 
10068         mutex_enter(SD_MUTEX(un));
10069 
10070         /* Don't proceed if power is being changed or we're still attaching. */
10071         while ((un->un_state == SD_STATE_PM_CHANGING) ||
10072             (un->un_state == SD_STATE_ATTACHING)) {
10073                 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10074         }
10075 
10076         if (un->un_exclopen & (1 << part)) {
10077                 un->un_exclopen &= ~(1 << part);
10078         }
10079 
10080         /* Update the open partition map */
10081         if (otyp == OTYP_LYR) {
10082                 un->un_ocmap.lyropen[part] -= 1;
10083         } else {
10084                 un->un_ocmap.regopen[otyp] &= ~(1 << part);
10085         }
10086 
10087         cp = &un->un_ocmap.chkd[0];
10088         while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10089                 if (*cp != NULL) {
10090                         break;
10091                 }
10092                 cp++;
10093         }
10094 
10095         if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10096                 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n");
10097 
10098                 /*
10099                  * We avoid persistance upon the last close, and set
10100                  * the throttle back to the maximum.
10101                  */
10102                 un->un_throttle = un->un_saved_throttle;
10103 
10104                 if (un->un_state == SD_STATE_OFFLINE) {
10105                         if (un->un_f_is_fibre == FALSE) {
10106                                 scsi_log(SD_DEVINFO(un), sd_label,
10107                                     CE_WARN, "offline\n");
10108                         }
10109                         mutex_exit(SD_MUTEX(un));
10110                         cmlb_invalidate(un->un_cmlbhandle,
10111                             (void *)SD_PATH_DIRECT);
10112                         mutex_enter(SD_MUTEX(un));
10113                 } else if (un->un_state != SD_STATE_ATTACH_FAILED) {
10114                         /*
10115                          * Flush any outstanding writes in NVRAM cache.
10116                          * Note: SYNCHRONIZE CACHE is an optional SCSI-2
10117                          * cmd, it may not work for non-Pluto devices.
10118                          * SYNCHRONIZE CACHE is not required for removables,
10119                          * except DVD-RAM drives.
10120                          *
10121                          * Also note: because SYNCHRONIZE CACHE is currently
10122                          * the only command issued here that requires the
10123                          * drive be powered up, only do the power up before
10124                          * sending the Sync Cache command. If additional
10125                          * commands are added which require a powered up
10126                          * drive, the following sequence may have to change.
10127                          */
10128                         if (!DEVI_IS_GONE(SD_DEVINFO(un)) &&
10129                             ((un->un_f_sync_cache_supported &&
10130                             un->un_f_sync_cache_required) ||
10131                             un->un_f_dvdram_writable_device == TRUE)) {
10132                                 mutex_exit(SD_MUTEX(un));
10133                                 if (sd_pm_entry(un) == DDI_SUCCESS) {
10134                                         rval =
10135                                             sd_send_scsi_SYNCHRONIZE_CACHE(un,
10136                                             NULL);
10137                                         /* ignore error if not supported */
10138                                         if (rval == ENOTSUP) {
10139                                                 rval = 0;
10140                                         } else if (rval != 0) {
10141                                                 rval = EIO;
10142                                         }
10143                                         sd_pm_exit(un);
10144                                 } else {
10145                                         rval = EIO;
10146                                 }
10147                                 mutex_enter(SD_MUTEX(un));
10148                         }
10149 
10150                         /*
10151                          * For devices which supports DOOR_LOCK, send an ALLOW
10152                          * MEDIA REMOVAL command, but don't get upset if it
10153                          * fails. We need to raise the power of the drive before
10154                          * we can call sd_send_scsi_DOORLOCK()
10155                          */
10156                         if (un->un_f_doorlock_supported) {
10157                                 mutex_exit(SD_MUTEX(un));
10158                                 if (sd_pm_entry(un) == DDI_SUCCESS) {
10159                                         sd_ssc_t        *ssc;
10160 
10161                                         ssc = sd_ssc_init(un);
10162                                         rval = sd_send_scsi_DOORLOCK(ssc,
10163                                             SD_REMOVAL_ALLOW, SD_PATH_DIRECT);
10164                                         if (rval != 0)
10165                                                 sd_ssc_assessment(ssc,
10166                                                     SD_FMT_IGNORE);
10167                                         sd_ssc_fini(ssc);
10168 
10169                                         sd_pm_exit(un);
10170                                         if (ISCD(un) && (rval != 0) &&
10171                                             (nodelay != 0)) {
10172                                                 rval = ENXIO;
10173                                         }
10174                                 } else {
10175                                         rval = EIO;
10176                                 }
10177                                 mutex_enter(SD_MUTEX(un));
10178                         }
10179 
10180                         /*
10181                          * Pardon a device that is currently in failfast
10182                          * active state, to not bias a future open.
10183                          */
10184                         un->un_failfast_state = SD_FAILFAST_INACTIVE;
10185 
10186                         /*
10187                          * If a device has removable media, invalidate all
10188                          * parameters related to media, such as geometry,
10189                          * blocksize, and blockcount.
10190                          */
10191                         if (un->un_f_has_removable_media) {
10192                                 sr_ejected(un);
10193                         }
10194 
10195                         /*
10196                          * Destroy the cache (if it exists) which was
10197                          * allocated for the write maps since this is
10198                          * the last close for this media.
10199                          */
10200                         if (un->un_wm_cache) {
10201                                 /*
10202                                  * Check if there are pending commands.
10203                                  * and if there are give a warning and
10204                                  * do not destroy the cache.
10205                                  */
10206                                 if (un->un_ncmds_in_driver > 0) {
10207                                         scsi_log(SD_DEVINFO(un),
10208                                             sd_label, CE_WARN,
10209                                             "Unable to clean up memory "
10210                                             "because of pending I/O\n");
10211                                 } else {
10212                                         kmem_cache_destroy(
10213                                             un->un_wm_cache);
10214                                         un->un_wm_cache = NULL;
10215                                 }
10216                         }
10217                 }
10218         }
10219 
10220         mutex_exit(SD_MUTEX(un));
10221         sema_v(&un->un_semoclose);
10222 
10223         if (otyp == OTYP_LYR)
10224                 un->un_layer_count--;
10225 
10226         mutex_exit(&sd_detach_mutex);
10227 
10228         return (rval);
10229 }
10230 
10231 
10232 /*
10233  *    Function: sd_ready_and_valid
10234  *
10235  * Description: Test if device is ready and has a valid geometry.
10236  *
10237  *   Arguments: ssc - sd_ssc_t will contain un
10238  *              un  - driver soft state (unit) structure
10239  *
10240  * Return Code: SD_READY_VALID          ready and valid label
10241  *              SD_NOT_READY_VALID      not ready, no label
10242  *              SD_RESERVED_BY_OTHERS   reservation conflict
10243  *
10244  *     Context: Never called at interrupt context.
10245  */
10246 
10247 static int
10248 sd_ready_and_valid(sd_ssc_t *ssc, int part)
10249 {
10250         struct sd_errstats      *stp;
10251         uint64_t                capacity;
10252         uint_t                  lbasize;
10253         int                     rval = SD_READY_VALID;
10254         char                    name_str[48];
10255         boolean_t               is_valid;
10256         struct sd_lun           *un;
10257         int                     status;
10258 
10259         ASSERT(ssc != NULL);
10260         un = ssc->ssc_un;
10261         ASSERT(un != NULL);
10262         ASSERT(!mutex_owned(SD_MUTEX(un)));
10263 
10264         mutex_enter(SD_MUTEX(un));
10265         /*
10266          * If a device has removable media, we must check if media is
10267          * ready when checking if this device is ready and valid.
10268          */
10269         if (un->un_f_has_removable_media) {
10270                 mutex_exit(SD_MUTEX(un));
10271                 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10272 
10273                 if (status != 0) {
10274                         rval = SD_NOT_READY_VALID;
10275                         mutex_enter(SD_MUTEX(un));
10276 
10277                         /* Ignore all failed status for removalbe media */
10278                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10279 
10280                         goto done;
10281                 }
10282 
10283                 is_valid = SD_IS_VALID_LABEL(un);
10284                 mutex_enter(SD_MUTEX(un));
10285                 if (!is_valid ||
10286                     (un->un_f_blockcount_is_valid == FALSE) ||
10287                     (un->un_f_tgt_blocksize_is_valid == FALSE)) {
10288 
10289                         /* capacity has to be read every open. */
10290                         mutex_exit(SD_MUTEX(un));
10291                         status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
10292                             &lbasize, SD_PATH_DIRECT);
10293 
10294                         if (status != 0) {
10295                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10296 
10297                                 cmlb_invalidate(un->un_cmlbhandle,
10298                                     (void *)SD_PATH_DIRECT);
10299                                 mutex_enter(SD_MUTEX(un));
10300                                 rval = SD_NOT_READY_VALID;
10301 
10302                                 goto done;
10303                         } else {
10304                                 mutex_enter(SD_MUTEX(un));
10305                                 sd_update_block_info(un, lbasize, capacity);
10306                         }
10307                 }
10308 
10309                 /*
10310                  * Check if the media in the device is writable or not.
10311                  */
10312                 if (!is_valid && ISCD(un)) {
10313                         sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
10314                 }
10315 
10316         } else {
10317                 /*
10318                  * Do a test unit ready to clear any unit attention from non-cd
10319                  * devices.
10320                  */
10321                 mutex_exit(SD_MUTEX(un));
10322 
10323                 status = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
10324                 if (status != 0) {
10325                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10326                 }
10327 
10328                 mutex_enter(SD_MUTEX(un));
10329         }
10330 
10331 
10332         /*
10333          * If this is a non 512 block device, allocate space for
10334          * the wmap cache. This is being done here since every time
10335          * a media is changed this routine will be called and the
10336          * block size is a function of media rather than device.
10337          */
10338         if (((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR ||
10339             un->un_f_non_devbsize_supported) &&
10340             un->un_tgt_blocksize != DEV_BSIZE) ||
10341             un->un_f_enable_rmw) {
10342                 if (!(un->un_wm_cache)) {
10343                         (void) snprintf(name_str, sizeof (name_str),
10344                             "%s%d_cache",
10345                             ddi_driver_name(SD_DEVINFO(un)),
10346                             ddi_get_instance(SD_DEVINFO(un)));
10347                         un->un_wm_cache = kmem_cache_create(
10348                             name_str, sizeof (struct sd_w_map),
10349                             8, sd_wm_cache_constructor,
10350                             sd_wm_cache_destructor, NULL,
10351                             (void *)un, NULL, 0);
10352                         if (!(un->un_wm_cache)) {
10353                                 rval = ENOMEM;
10354                                 goto done;
10355                         }
10356                 }
10357         }
10358 
10359         if (un->un_state == SD_STATE_NORMAL) {
10360                 /*
10361                  * If the target is not yet ready here (defined by a TUR
10362                  * failure), invalidate the geometry and print an 'offline'
10363                  * message. This is a legacy message, as the state of the
10364                  * target is not actually changed to SD_STATE_OFFLINE.
10365                  *
10366                  * If the TUR fails for EACCES (Reservation Conflict),
10367                  * SD_RESERVED_BY_OTHERS will be returned to indicate
10368                  * reservation conflict. If the TUR fails for other
10369                  * reasons, SD_NOT_READY_VALID will be returned.
10370                  */
10371                 int err;
10372 
10373                 mutex_exit(SD_MUTEX(un));
10374                 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10375                 mutex_enter(SD_MUTEX(un));
10376 
10377                 if (err != 0) {
10378                         mutex_exit(SD_MUTEX(un));
10379                         cmlb_invalidate(un->un_cmlbhandle,
10380                             (void *)SD_PATH_DIRECT);
10381                         mutex_enter(SD_MUTEX(un));
10382                         if (err == EACCES) {
10383                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10384                                     "reservation conflict\n");
10385                                 rval = SD_RESERVED_BY_OTHERS;
10386                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10387                         } else {
10388                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10389                                     "drive offline\n");
10390                                 rval = SD_NOT_READY_VALID;
10391                                 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
10392                         }
10393                         goto done;
10394                 }
10395         }
10396 
10397         if (un->un_f_format_in_progress == FALSE) {
10398                 mutex_exit(SD_MUTEX(un));
10399 
10400                 (void) cmlb_validate(un->un_cmlbhandle, 0,
10401                     (void *)SD_PATH_DIRECT);
10402                 if (cmlb_partinfo(un->un_cmlbhandle, part, NULL, NULL, NULL,
10403                     NULL, (void *) SD_PATH_DIRECT) != 0) {
10404                         rval = SD_NOT_READY_VALID;
10405                         mutex_enter(SD_MUTEX(un));
10406 
10407                         goto done;
10408                 }
10409                 if (un->un_f_pkstats_enabled) {
10410                         sd_set_pstats(un);
10411                         SD_TRACE(SD_LOG_IO_PARTITION, un,
10412                             "sd_ready_and_valid: un:0x%p pstats created and "
10413                             "set\n", un);
10414                 }
10415                 mutex_enter(SD_MUTEX(un));
10416         }
10417 
10418         /*
10419          * If this device supports DOOR_LOCK command, try and send
10420          * this command to PREVENT MEDIA REMOVAL, but don't get upset
10421          * if it fails. For a CD, however, it is an error
10422          */
10423         if (un->un_f_doorlock_supported) {
10424                 mutex_exit(SD_MUTEX(un));
10425                 status = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
10426                     SD_PATH_DIRECT);
10427 
10428                 if ((status != 0) && ISCD(un)) {
10429                         rval = SD_NOT_READY_VALID;
10430                         mutex_enter(SD_MUTEX(un));
10431 
10432                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10433 
10434                         goto done;
10435                 } else if (status != 0)
10436                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10437                 mutex_enter(SD_MUTEX(un));
10438         }
10439 
10440         /* The state has changed, inform the media watch routines */
10441         un->un_mediastate = DKIO_INSERTED;
10442         cv_broadcast(&un->un_state_cv);
10443         rval = SD_READY_VALID;
10444 
10445 done:
10446 
10447         /*
10448          * Initialize the capacity kstat value, if no media previously
10449          * (capacity kstat is 0) and a media has been inserted
10450          * (un_blockcount > 0).
10451          */
10452         if (un->un_errstats != NULL) {
10453                 stp = (struct sd_errstats *)un->un_errstats->ks_data;
10454                 if ((stp->sd_capacity.value.ui64 == 0) &&
10455                     (un->un_f_blockcount_is_valid == TRUE)) {
10456                         stp->sd_capacity.value.ui64 =
10457                             (uint64_t)((uint64_t)un->un_blockcount *
10458                             un->un_sys_blocksize);
10459                 }
10460         }
10461 
10462         mutex_exit(SD_MUTEX(un));
10463         return (rval);
10464 }
10465 
10466 
10467 /*
10468  *    Function: sdmin
10469  *
10470  * Description: Routine to limit the size of a data transfer. Used in
10471  *              conjunction with physio(9F).
10472  *
10473  *   Arguments: bp - pointer to the indicated buf(9S) struct.
10474  *
10475  *     Context: Kernel thread context.
10476  */
10477 
10478 static void
10479 sdmin(struct buf *bp)
10480 {
10481         struct sd_lun   *un;
10482         int             instance;
10483 
10484         instance = SDUNIT(bp->b_edev);
10485 
10486         un = ddi_get_soft_state(sd_state, instance);
10487         ASSERT(un != NULL);
10488 
10489         /*
10490          * We depend on buf breakup to restrict
10491          * IO size if it is enabled.
10492          */
10493         if (un->un_buf_breakup_supported) {
10494                 return;
10495         }
10496 
10497         if (bp->b_bcount > un->un_max_xfer_size) {
10498                 bp->b_bcount = un->un_max_xfer_size;
10499         }
10500 }
10501 
10502 
10503 /*
10504  *    Function: sdread
10505  *
10506  * Description: Driver's read(9e) entry point function.
10507  *
10508  *   Arguments: dev   - device number
10509  *              uio   - structure pointer describing where data is to be stored
10510  *                      in user's space
10511  *              cred_p  - user credential pointer
10512  *
10513  * Return Code: ENXIO
10514  *              EIO
10515  *              EINVAL
10516  *              value returned by physio
10517  *
10518  *     Context: Kernel thread context.
10519  */
10520 /* ARGSUSED */
10521 static int
10522 sdread(dev_t dev, struct uio *uio, cred_t *cred_p)
10523 {
10524         struct sd_lun   *un = NULL;
10525         int             secmask;
10526         int             err = 0;
10527         sd_ssc_t        *ssc;
10528 
10529         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
10530             DEVI_IS_GONE(SD_DEVINFO(un)))
10531                 return (ENXIO);
10532 
10533         ASSERT(!mutex_owned(SD_MUTEX(un)));
10534 
10535         mutex_enter(SD_MUTEX(un));
10536         while (un->un_state == SD_STATE_ATTACHING)
10537                 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10538 
10539         if (un->un_state == SD_STATE_ATTACH_FAILED) {
10540                 mutex_exit(SD_MUTEX(un));
10541                 SD_ERROR(SD_LOG_READ_WRITE, un, "sdread: attach failed\n");
10542                 return (EIO);
10543         }
10544         mutex_exit(SD_MUTEX(un));
10545 
10546         if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10547                 mutex_enter(SD_MUTEX(un));
10548                 /*
10549                  * Because the call to sd_ready_and_valid will issue I/O we
10550                  * must wait here if either the device is suspended or
10551                  * if it's power level is changing.
10552                  */
10553                 while ((un->un_state == SD_STATE_SUSPENDED) ||
10554                     (un->un_state == SD_STATE_PM_CHANGING)) {
10555                         cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10556                 }
10557 
10558                 SD_BAIL_CHECK(un);
10559                 un->un_ncmds_in_driver++;
10560                 mutex_exit(SD_MUTEX(un));
10561 
10562                 /* Initialize sd_ssc_t for internal uscsi commands */
10563                 ssc = sd_ssc_init(un);
10564                 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10565                         err = EIO;
10566                 } else {
10567                         err = 0;
10568                 }
10569                 sd_ssc_fini(ssc);
10570 
10571                 mutex_enter(SD_MUTEX(un));
10572                 un->un_ncmds_in_driver--;
10573                 if (un->un_f_detach_waiting)
10574                         cv_signal(&un->un_detach_cv);
10575                 ASSERT(un->un_ncmds_in_driver >= 0);
10576                 mutex_exit(SD_MUTEX(un));
10577                 if (err != 0)
10578                         return (err);
10579         }
10580 
10581         /*
10582          * Read requests are restricted to multiples of the system block size.
10583          */
10584         if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
10585             !un->un_f_enable_rmw)
10586                 secmask = un->un_tgt_blocksize - 1;
10587         else
10588                 secmask = DEV_BSIZE - 1;
10589 
10590         if (uio->uio_loffset & ((offset_t)(secmask))) {
10591                 SD_ERROR(SD_LOG_READ_WRITE, un,
10592                     "sdread: file offset not modulo %d\n",
10593                     secmask + 1);
10594                 err = EINVAL;
10595         } else if (uio->uio_iov->iov_len & (secmask)) {
10596                 SD_ERROR(SD_LOG_READ_WRITE, un,
10597                     "sdread: transfer length not modulo %d\n",
10598                     secmask + 1);
10599                 err = EINVAL;
10600         } else {
10601                 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio);
10602         }
10603 
10604         return (err);
10605 }
10606 
10607 
10608 /*
10609  *    Function: sdwrite
10610  *
10611  * Description: Driver's write(9e) entry point function.
10612  *
10613  *   Arguments: dev   - device number
10614  *              uio   - structure pointer describing where data is stored in
10615  *                      user's space
10616  *              cred_p  - user credential pointer
10617  *
10618  * Return Code: ENXIO
10619  *              EIO
10620  *              EINVAL
10621  *              value returned by physio
10622  *
10623  *     Context: Kernel thread context.
10624  */
10625 /* ARGSUSED */
10626 static int
10627 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p)
10628 {
10629         struct sd_lun   *un = NULL;
10630         int             secmask;
10631         int             err = 0;
10632         sd_ssc_t        *ssc;
10633 
10634         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
10635             DEVI_IS_GONE(SD_DEVINFO(un)))
10636                 return (ENXIO);
10637 
10638         ASSERT(!mutex_owned(SD_MUTEX(un)));
10639 
10640         mutex_enter(SD_MUTEX(un));
10641         while (un->un_state == SD_STATE_ATTACHING)
10642                 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10643 
10644         if (un->un_state == SD_STATE_ATTACH_FAILED) {
10645                 mutex_exit(SD_MUTEX(un));
10646                 SD_ERROR(SD_LOG_READ_WRITE, un, "sdwrite: attach failed\n");
10647                 return (EIO);
10648         }
10649         mutex_exit(SD_MUTEX(un));
10650 
10651         if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10652                 mutex_enter(SD_MUTEX(un));
10653                 /*
10654                  * Because the call to sd_ready_and_valid will issue I/O we
10655                  * must wait here if either the device is suspended or
10656                  * if it's power level is changing.
10657                  */
10658                 while ((un->un_state == SD_STATE_SUSPENDED) ||
10659                     (un->un_state == SD_STATE_PM_CHANGING)) {
10660                         cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10661                 }
10662 
10663                 SD_BAIL_CHECK(un);
10664                 un->un_ncmds_in_driver++;
10665                 mutex_exit(SD_MUTEX(un));
10666 
10667                 /* Initialize sd_ssc_t for internal uscsi commands */
10668                 ssc = sd_ssc_init(un);
10669                 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10670                         err = EIO;
10671                 } else {
10672                         err = 0;
10673                 }
10674                 sd_ssc_fini(ssc);
10675 
10676                 mutex_enter(SD_MUTEX(un));
10677                 un->un_ncmds_in_driver--;
10678                 ASSERT(un->un_ncmds_in_driver >= 0);
10679                 if (un->un_f_detach_waiting)
10680                         cv_signal(&un->un_detach_cv);
10681                 mutex_exit(SD_MUTEX(un));
10682                 if (err != 0)
10683                         return (err);
10684         }
10685 
10686         /*
10687          * Write requests are restricted to multiples of the system block size.
10688          */
10689         if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
10690             !un->un_f_enable_rmw)
10691                 secmask = un->un_tgt_blocksize - 1;
10692         else
10693                 secmask = DEV_BSIZE - 1;
10694 
10695         if (uio->uio_loffset & ((offset_t)(secmask))) {
10696                 SD_ERROR(SD_LOG_READ_WRITE, un,
10697                     "sdwrite: file offset not modulo %d\n",
10698                     secmask + 1);
10699                 err = EINVAL;
10700         } else if (uio->uio_iov->iov_len & (secmask)) {
10701                 SD_ERROR(SD_LOG_READ_WRITE, un,
10702                     "sdwrite: transfer length not modulo %d\n",
10703                     secmask + 1);
10704                 err = EINVAL;
10705         } else {
10706                 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio);
10707         }
10708 
10709         return (err);
10710 }
10711 
10712 
10713 /*
10714  *    Function: sdaread
10715  *
10716  * Description: Driver's aread(9e) entry point function.
10717  *
10718  *   Arguments: dev   - device number
10719  *              aio   - structure pointer describing where data is to be stored
10720  *              cred_p  - user credential pointer
10721  *
10722  * Return Code: ENXIO
10723  *              EIO
10724  *              EINVAL
10725  *              value returned by aphysio
10726  *
10727  *     Context: Kernel thread context.
10728  */
10729 /* ARGSUSED */
10730 static int
10731 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p)
10732 {
10733         struct sd_lun   *un = NULL;
10734         struct uio      *uio = aio->aio_uio;
10735         int             secmask;
10736         int             err = 0;
10737         sd_ssc_t        *ssc;
10738 
10739         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
10740             DEVI_IS_GONE(SD_DEVINFO(un)))
10741                 return (ENXIO);
10742 
10743         ASSERT(!mutex_owned(SD_MUTEX(un)));
10744 
10745         mutex_enter(SD_MUTEX(un));
10746         while (un->un_state == SD_STATE_ATTACHING)
10747                 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10748 
10749         if (un->un_state == SD_STATE_ATTACH_FAILED) {
10750                 mutex_exit(SD_MUTEX(un));
10751                 SD_ERROR(SD_LOG_READ_WRITE, un, "sdaread: attach failed\n");
10752                 return (EIO);
10753         }
10754         mutex_exit(SD_MUTEX(un));
10755 
10756         if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10757                 mutex_enter(SD_MUTEX(un));
10758                 /*
10759                  * Because the call to sd_ready_and_valid will issue I/O we
10760                  * must wait here if either the device is suspended or
10761                  * if it's power level is changing.
10762                  */
10763                 while ((un->un_state == SD_STATE_SUSPENDED) ||
10764                     (un->un_state == SD_STATE_PM_CHANGING)) {
10765                         cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10766                 }
10767 
10768                 SD_BAIL_CHECK(un);
10769                 un->un_ncmds_in_driver++;
10770                 mutex_exit(SD_MUTEX(un));
10771 
10772                 /* Initialize sd_ssc_t for internal uscsi commands */
10773                 ssc = sd_ssc_init(un);
10774                 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10775                         err = EIO;
10776                 } else {
10777                         err = 0;
10778                 }
10779                 sd_ssc_fini(ssc);
10780 
10781                 mutex_enter(SD_MUTEX(un));
10782                 un->un_ncmds_in_driver--;
10783                 ASSERT(un->un_ncmds_in_driver >= 0);
10784                 if (un->un_f_detach_waiting)
10785                         cv_signal(&un->un_detach_cv);
10786                 mutex_exit(SD_MUTEX(un));
10787                 if (err != 0)
10788                         return (err);
10789         }
10790 
10791         /*
10792          * Read requests are restricted to multiples of the system block size.
10793          */
10794         if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
10795             !un->un_f_enable_rmw)
10796                 secmask = un->un_tgt_blocksize - 1;
10797         else
10798                 secmask = DEV_BSIZE - 1;
10799 
10800         if (uio->uio_loffset & ((offset_t)(secmask))) {
10801                 SD_ERROR(SD_LOG_READ_WRITE, un,
10802                     "sdaread: file offset not modulo %d\n",
10803                     secmask + 1);
10804                 err = EINVAL;
10805         } else if (uio->uio_iov->iov_len & (secmask)) {
10806                 SD_ERROR(SD_LOG_READ_WRITE, un,
10807                     "sdaread: transfer length not modulo %d\n",
10808                     secmask + 1);
10809                 err = EINVAL;
10810         } else {
10811                 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio);
10812         }
10813 
10814         return (err);
10815 }
10816 
10817 
10818 /*
10819  *    Function: sdawrite
10820  *
10821  * Description: Driver's awrite(9e) entry point function.
10822  *
10823  *   Arguments: dev   - device number
10824  *              aio   - structure pointer describing where data is stored
10825  *              cred_p  - user credential pointer
10826  *
10827  * Return Code: ENXIO
10828  *              EIO
10829  *              EINVAL
10830  *              value returned by aphysio
10831  *
10832  *     Context: Kernel thread context.
10833  */
10834 /* ARGSUSED */
10835 static int
10836 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p)
10837 {
10838         struct sd_lun   *un = NULL;
10839         struct uio      *uio = aio->aio_uio;
10840         int             secmask;
10841         int             err = 0;
10842         sd_ssc_t        *ssc;
10843 
10844         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
10845             DEVI_IS_GONE(SD_DEVINFO(un)))
10846                 return (ENXIO);
10847 
10848         ASSERT(!mutex_owned(SD_MUTEX(un)));
10849 
10850         mutex_enter(SD_MUTEX(un));
10851         while (un->un_state == SD_STATE_ATTACHING)
10852                 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10853 
10854         if (un->un_state == SD_STATE_ATTACH_FAILED) {
10855                 mutex_exit(SD_MUTEX(un));
10856                 SD_ERROR(SD_LOG_READ_WRITE, un,
10857                     "sdawrite: attach failed\n");
10858                 return (EIO);
10859         }
10860         mutex_exit(SD_MUTEX(un));
10861 
10862         if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10863                 mutex_enter(SD_MUTEX(un));
10864                 /*
10865                  * Because the call to sd_ready_and_valid will issue I/O we
10866                  * must wait here if either the device is suspended or
10867                  * if it's power level is changing.
10868                  */
10869                 while ((un->un_state == SD_STATE_SUSPENDED) ||
10870                     (un->un_state == SD_STATE_PM_CHANGING)) {
10871                         cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10872                 }
10873 
10874                 SD_BAIL_CHECK(un);
10875                 un->un_ncmds_in_driver++;
10876                 mutex_exit(SD_MUTEX(un));
10877 
10878                 /* Initialize sd_ssc_t for internal uscsi commands */
10879                 ssc = sd_ssc_init(un);
10880                 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10881                         err = EIO;
10882                 } else {
10883                         err = 0;
10884                 }
10885                 sd_ssc_fini(ssc);
10886 
10887                 mutex_enter(SD_MUTEX(un));
10888                 un->un_ncmds_in_driver--;
10889                 ASSERT(un->un_ncmds_in_driver >= 0);
10890                 if (un->un_f_detach_waiting)
10891                         cv_signal(&un->un_detach_cv);
10892                 mutex_exit(SD_MUTEX(un));
10893                 if (err != 0)
10894                         return (err);
10895         }
10896 
10897         /*
10898          * Write requests are restricted to multiples of the system block size.
10899          */
10900         if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
10901             !un->un_f_enable_rmw)
10902                 secmask = un->un_tgt_blocksize - 1;
10903         else
10904                 secmask = DEV_BSIZE - 1;
10905 
10906         if (uio->uio_loffset & ((offset_t)(secmask))) {
10907                 SD_ERROR(SD_LOG_READ_WRITE, un,
10908                     "sdawrite: file offset not modulo %d\n",
10909                     secmask + 1);
10910                 err = EINVAL;
10911         } else if (uio->uio_iov->iov_len & (secmask)) {
10912                 SD_ERROR(SD_LOG_READ_WRITE, un,
10913                     "sdawrite: transfer length not modulo %d\n",
10914                     secmask + 1);
10915                 err = EINVAL;
10916         } else {
10917                 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio);
10918         }
10919 
10920         return (err);
10921 }
10922 
10923 
10924 
10925 
10926 
10927 /*
10928  * Driver IO processing follows the following sequence:
10929  *
10930  *     sdioctl(9E)     sdstrategy(9E)         biodone(9F)
10931  *         |                |                     ^
10932  *         v                v                     |
10933  * sd_send_scsi_cmd()  ddi_xbuf_qstrategy()       +-------------------+
10934  *         |                |                     |                   |
10935  *         v                |                     |                   |
10936  * sd_uscsi_strategy() sd_xbuf_strategy()   sd_buf_iodone()   sd_uscsi_iodone()
10937  *         |                |                     ^                   ^
10938  *         v                v                     |                   |
10939  * SD_BEGIN_IOSTART()  SD_BEGIN_IOSTART()         |                   |
10940  *         |                |                     |                   |
10941  *     +---+                |                     +------------+      +-------+
10942  *     |                    |                                  |              |
10943  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
10944  *     |                    v                                  |              |
10945  *     |         sd_mapblockaddr_iostart()           sd_mapblockaddr_iodone() |
10946  *     |                    |                                  ^              |
10947  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
10948  *     |                    v                                  |              |
10949  *     |         sd_mapblocksize_iostart()           sd_mapblocksize_iodone() |
10950  *     |                    |                                  ^              |
10951  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
10952  *     |                    v                                  |              |
10953  *     |           sd_checksum_iostart()               sd_checksum_iodone()   |
10954  *     |                    |                                  ^              |
10955  *     +-> SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()+------------->+
10956  *     |                    v                                  |              |
10957  *     |              sd_pm_iostart()                     sd_pm_iodone()      |
10958  *     |                    |                                  ^              |
10959  *     |                    |                                  |              |
10960  *     +-> SD_NEXT_IOSTART()|               SD_BEGIN_IODONE()--+--------------+
10961  *                          |                           ^
10962  *                          v                           |
10963  *                   sd_core_iostart()                  |
10964  *                          |                           |
10965  *                          |                           +------>(*destroypkt)()
10966  *                          +-> sd_start_cmds() <-+     |           |
10967  *                          |                     |     |           v
10968  *                          |                     |     |  scsi_destroy_pkt(9F)
10969  *                          |                     |     |
10970  *                          +->(*initpkt)()       +- sdintr()
10971  *                          |  |                        |  |
10972  *                          |  +-> scsi_init_pkt(9F)    |  +-> sd_handle_xxx()
10973  *                          |  +-> scsi_setup_cdb(9F)   |
10974  *                          |                           |
10975  *                          +--> scsi_transport(9F)     |
10976  *                                     |                |
10977  *                                     +----> SCSA ---->+
10978  *
10979  *
10980  * This code is based upon the following presumptions:
10981  *
10982  *   - iostart and iodone functions operate on buf(9S) structures. These
10983  *     functions perform the necessary operations on the buf(9S) and pass
10984  *     them along to the next function in the chain by using the macros
10985  *     SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE()
10986  *     (for iodone side functions).
10987  *
10988  *   - The iostart side functions may sleep. The iodone side functions
10989  *     are called under interrupt context and may NOT sleep. Therefore
10990  *     iodone side functions also may not call iostart side functions.
10991  *     (NOTE: iostart side functions should NOT sleep for memory, as
10992  *     this could result in deadlock.)
10993  *
10994  *   - An iostart side function may call its corresponding iodone side
10995  *     function directly (if necessary).
10996  *
10997  *   - In the event of an error, an iostart side function can return a buf(9S)
10998  *     to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and
10999  *     b_error in the usual way of course).
11000  *
11001  *   - The taskq mechanism may be used by the iodone side functions to dispatch
11002  *     requests to the iostart side functions.  The iostart side functions in
11003  *     this case would be called under the context of a taskq thread, so it's
11004  *     OK for them to block/sleep/spin in this case.
11005  *
11006  *   - iostart side functions may allocate "shadow" buf(9S) structs and
11007  *     pass them along to the next function in the chain.  The corresponding
11008  *     iodone side functions must coalesce the "shadow" bufs and return
11009  *     the "original" buf to the next higher layer.
11010  *
11011  *   - The b_private field of the buf(9S) struct holds a pointer to
11012  *     an sd_xbuf struct, which contains information needed to
11013  *     construct the scsi_pkt for the command.
11014  *
11015  *   - The SD_MUTEX(un) is NOT held across calls to the next layer. Each
11016  *     layer must acquire & release the SD_MUTEX(un) as needed.
11017  */
11018 
11019 
11020 /*
11021  *    Function: sd_taskq_create
11022  *
11023  * Description: Create taskq thread(s) and preallocate task entries
11024  *
11025  * Return Code: Returns a pointer to the allocated taskq_t.
11026  *
11027  *     Context: Can sleep. Requires blockable context.
11028  *
11029  *       Notes: - The taskq() facility currently is NOT part of the DDI.
11030  *                (definitely NOT recommeded for 3rd-party drivers!) :-)
11031  *              - taskq_create() will block for memory, also it will panic
11032  *                if it cannot create the requested number of threads.
11033  *              - Currently taskq_create() creates threads that cannot be
11034  *                swapped.
11035  *              - We use TASKQ_PREPOPULATE to ensure we have an adequate
11036  *                supply of taskq entries at interrupt time (ie, so that we
11037  *                do not have to sleep for memory)
11038  */
11039 
11040 static void
11041 sd_taskq_create(void)
11042 {
11043         char    taskq_name[TASKQ_NAMELEN];
11044 
11045         ASSERT(sd_tq == NULL);
11046         ASSERT(sd_wmr_tq == NULL);
11047 
11048         (void) snprintf(taskq_name, sizeof (taskq_name),
11049             "%s_drv_taskq", sd_label);
11050         sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS,
11051             (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11052             TASKQ_PREPOPULATE));
11053 
11054         (void) snprintf(taskq_name, sizeof (taskq_name),
11055             "%s_rmw_taskq", sd_label);
11056         sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS,
11057             (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11058             TASKQ_PREPOPULATE));
11059 }
11060 
11061 
11062 /*
11063  *    Function: sd_taskq_delete
11064  *
11065  * Description: Complementary cleanup routine for sd_taskq_create().
11066  *
11067  *     Context: Kernel thread context.
11068  */
11069 
11070 static void
11071 sd_taskq_delete(void)
11072 {
11073         ASSERT(sd_tq != NULL);
11074         ASSERT(sd_wmr_tq != NULL);
11075         taskq_destroy(sd_tq);
11076         taskq_destroy(sd_wmr_tq);
11077         sd_tq = NULL;
11078         sd_wmr_tq = NULL;
11079 }
11080 
11081 
11082 /*
11083  *    Function: sdstrategy
11084  *
11085  * Description: Driver's strategy (9E) entry point function.
11086  *
11087  *   Arguments: bp - pointer to buf(9S)
11088  *
11089  * Return Code: Always returns zero
11090  *
11091  *     Context: Kernel thread context.
11092  */
11093 
11094 static int
11095 sdstrategy(struct buf *bp)
11096 {
11097         struct sd_lun *un;
11098         int error = EIO;
11099 
11100         if ((un = ddi_get_soft_state(sd_state,
11101             SD_GET_INSTANCE_FROM_BUF(bp))) == NULL)
11102                 goto fail;
11103 
11104         /* Fail new cmds if state is dumping or device is gone */
11105         if (un->un_state == SD_STATE_DUMPING ||
11106             DEVI_IS_GONE(SD_DEVINFO(un))) {
11107                 error = ENXIO;
11108                 goto fail;
11109         }
11110 
11111         ASSERT(!mutex_owned(SD_MUTEX(un)));
11112 
11113         /*
11114          * Commands may sneak in while we released the mutex in
11115          * DDI_SUSPEND, we should block new commands. However, old
11116          * commands that are still in the driver at this point should
11117          * still be allowed to drain.
11118          */
11119         mutex_enter(SD_MUTEX(un));
11120         /*
11121          * Must wait here if either the device is suspended or
11122          * if it's power level is changing.
11123          */
11124         while ((un->un_state == SD_STATE_SUSPENDED) ||
11125             (un->un_state == SD_STATE_PM_CHANGING) ||
11126             (un->un_state == SD_STATE_ATTACHING)) {
11127                 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11128         }
11129 
11130         if (un->un_state == SD_STATE_ATTACH_FAILED) {
11131                 mutex_exit(SD_MUTEX(un));
11132                 SD_ERROR(SD_LOG_READ_WRITE, un,
11133                     "sdstrategy: attach failed\n");
11134                 goto fail;
11135         }
11136         if (un->un_detach_count != 0) {
11137                 mutex_exit(SD_MUTEX(un));
11138                 goto fail;
11139         }
11140 
11141         un->un_ncmds_in_driver++;
11142 
11143         /*
11144          * atapi: Since we are running the CD for now in PIO mode we need to
11145          * call bp_mapin here to avoid bp_mapin called interrupt context under
11146          * the HBA's init_pkt routine.
11147          */
11148         if (un->un_f_cfg_is_atapi == TRUE) {
11149                 mutex_exit(SD_MUTEX(un));
11150                 bp_mapin(bp);
11151                 mutex_enter(SD_MUTEX(un));
11152         }
11153         SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n",
11154             un->un_ncmds_in_driver);
11155 
11156         if (bp->b_flags & B_WRITE)
11157                 un->un_f_sync_cache_required = TRUE;
11158 
11159         mutex_exit(SD_MUTEX(un));
11160 
11161         /*
11162          * This will (eventually) allocate the sd_xbuf area and
11163          * call sd_xbuf_strategy().  We just want to return the
11164          * result of ddi_xbuf_qstrategy so that we have an opt-
11165          * imized tail call which saves us a stack frame.
11166          */
11167         return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr));
11168 
11169 fail:
11170         bioerror(bp, error);
11171         bp->b_resid = bp->b_bcount;
11172         biodone(bp);
11173         return (0);
11174 }
11175 
11176 /*
11177  *    Function: sd_xbuf_strategy
11178  *
11179  * Description: Function for initiating IO operations via the
11180  *              ddi_xbuf_qstrategy() mechanism.
11181  *
11182  *     Context: Kernel thread context.
11183  */
11184 
11185 static void
11186 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg)
11187 {
11188         struct sd_lun *un = arg;
11189 
11190         ASSERT(bp != NULL);
11191         ASSERT(xp != NULL);
11192         ASSERT(un != NULL);
11193         ASSERT(!mutex_owned(SD_MUTEX(un)));
11194 
11195         /*
11196          * Initialize the fields in the xbuf and save a pointer to the
11197          * xbuf in bp->b_private.
11198          */
11199         sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL);
11200 
11201         /* Send the buf down the iostart chain */
11202         SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp);
11203 }
11204 
11205 
11206 /*
11207  *    Function: sd_xbuf_init
11208  *
11209  * Description: Prepare the given sd_xbuf struct for use.
11210  *
11211  *   Arguments: un - ptr to softstate
11212  *              bp - ptr to associated buf(9S)
11213  *              xp - ptr to associated sd_xbuf
11214  *              chain_type - IO chain type to use:
11215  *                      SD_CHAIN_NULL
11216  *                      SD_CHAIN_BUFIO
11217  *                      SD_CHAIN_USCSI
11218  *                      SD_CHAIN_DIRECT
11219  *                      SD_CHAIN_DIRECT_PRIORITY
11220  *              pktinfop - ptr to private data struct for scsi_pkt(9S)
11221  *                      initialization; may be NULL if none.
11222  *
11223  *     Context: Kernel thread context
11224  */
11225 
11226 static void
11227 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
11228     uchar_t chain_type, void *pktinfop)
11229 {
11230         int index;
11231 
11232         ASSERT(un != NULL);
11233         ASSERT(bp != NULL);
11234         ASSERT(xp != NULL);
11235 
11236         SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n",
11237             bp, chain_type);
11238 
11239         xp->xb_un    = un;
11240         xp->xb_pktp  = NULL;
11241         xp->xb_pktinfo       = pktinfop;
11242         xp->xb_private       = bp->b_private;
11243         xp->xb_blkno = (daddr_t)bp->b_blkno;
11244 
11245         /*
11246          * Set up the iostart and iodone chain indexes in the xbuf, based
11247          * upon the specified chain type to use.
11248          */
11249         switch (chain_type) {
11250         case SD_CHAIN_NULL:
11251                 /*
11252                  * Fall thru to just use the values for the buf type, even
11253                  * tho for the NULL chain these values will never be used.
11254                  */
11255                 /* FALLTHRU */
11256         case SD_CHAIN_BUFIO:
11257                 index = un->un_buf_chain_type;
11258                 if ((!un->un_f_has_removable_media) &&
11259                     (un->un_tgt_blocksize != 0) &&
11260                     (un->un_tgt_blocksize != DEV_BSIZE ||
11261                     un->un_f_enable_rmw)) {
11262                         int secmask = 0, blknomask = 0;
11263                         if (un->un_f_enable_rmw) {
11264                                 blknomask =
11265                                     (un->un_phy_blocksize / DEV_BSIZE) - 1;
11266                                 secmask = un->un_phy_blocksize - 1;
11267                         } else {
11268                                 blknomask =
11269                                     (un->un_tgt_blocksize / DEV_BSIZE) - 1;
11270                                 secmask = un->un_tgt_blocksize - 1;
11271                         }
11272 
11273                         if ((bp->b_lblkno & (blknomask)) ||
11274                             (bp->b_bcount & (secmask))) {
11275                                 if ((un->un_f_rmw_type !=
11276                                     SD_RMW_TYPE_RETURN_ERROR) ||
11277                                     un->un_f_enable_rmw) {
11278                                         if (un->un_f_pm_is_enabled == FALSE)
11279                                                 index =
11280                                                     SD_CHAIN_INFO_MSS_DSK_NO_PM;
11281                                         else
11282                                                 index =
11283                                                     SD_CHAIN_INFO_MSS_DISK;
11284                                 }
11285                         }
11286                 }
11287                 break;
11288         case SD_CHAIN_USCSI:
11289                 index = un->un_uscsi_chain_type;
11290                 break;
11291         case SD_CHAIN_DIRECT:
11292                 index = un->un_direct_chain_type;
11293                 break;
11294         case SD_CHAIN_DIRECT_PRIORITY:
11295                 index = un->un_priority_chain_type;
11296                 break;
11297         default:
11298                 /* We're really broken if we ever get here... */
11299                 panic("sd_xbuf_init: illegal chain type!");
11300                 /*NOTREACHED*/
11301         }
11302 
11303         xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index;
11304         xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index;
11305 
11306         /*
11307          * It might be a bit easier to simply bzero the entire xbuf above,
11308          * but it turns out that since we init a fair number of members anyway,
11309          * we save a fair number cycles by doing explicit assignment of zero.
11310          */
11311         xp->xb_pkt_flags     = 0;
11312         xp->xb_dma_resid     = 0;
11313         xp->xb_retry_count   = 0;
11314         xp->xb_victim_retry_count = 0;
11315         xp->xb_ua_retry_count        = 0;
11316         xp->xb_nr_retry_count        = 0;
11317         xp->xb_sense_bp              = NULL;
11318         xp->xb_sense_status  = 0;
11319         xp->xb_sense_state   = 0;
11320         xp->xb_sense_resid   = 0;
11321         xp->xb_ena           = 0;
11322 
11323         bp->b_private        = xp;
11324         bp->b_flags  &= ~(B_DONE | B_ERROR);
11325         bp->b_resid  = 0;
11326         bp->av_forw  = NULL;
11327         bp->av_back  = NULL;
11328         bioerror(bp, 0);
11329 
11330         SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n");
11331 }
11332 
11333 
11334 /*
11335  *    Function: sd_uscsi_strategy
11336  *
11337  * Description: Wrapper for calling into the USCSI chain via physio(9F)
11338  *
11339  *   Arguments: bp - buf struct ptr
11340  *
11341  * Return Code: Always returns 0
11342  *
11343  *     Context: Kernel thread context
11344  */
11345 
11346 static int
11347 sd_uscsi_strategy(struct buf *bp)
11348 {
11349         struct sd_lun           *un;
11350         struct sd_uscsi_info    *uip;
11351         struct sd_xbuf          *xp;
11352         uchar_t                 chain_type;
11353         uchar_t                 cmd;
11354 
11355         ASSERT(bp != NULL);
11356 
11357         un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11358         if (un == NULL || DEVI_IS_GONE(SD_DEVINFO(un))) {
11359                 bioerror(bp, EIO);
11360                 bp->b_resid = bp->b_bcount;
11361                 biodone(bp);
11362                 return (0);
11363         }
11364 
11365         ASSERT(!mutex_owned(SD_MUTEX(un)));
11366 
11367         SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp);
11368 
11369         /*
11370          * A pointer to a struct sd_uscsi_info is expected in bp->b_private
11371          */
11372         ASSERT(bp->b_private != NULL);
11373         uip = (struct sd_uscsi_info *)bp->b_private;
11374         cmd = ((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_cdb[0];
11375 
11376         mutex_enter(SD_MUTEX(un));
11377         /*
11378          * atapi: Since we are running the CD for now in PIO mode we need to
11379          * call bp_mapin here to avoid bp_mapin called interrupt context under
11380          * the HBA's init_pkt routine.
11381          */
11382         if (un->un_f_cfg_is_atapi == TRUE) {
11383                 mutex_exit(SD_MUTEX(un));
11384                 bp_mapin(bp);
11385                 mutex_enter(SD_MUTEX(un));
11386         }
11387         un->un_ncmds_in_driver++;
11388         SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n",
11389             un->un_ncmds_in_driver);
11390 
11391         if ((bp->b_flags & B_WRITE) && (bp->b_bcount != 0) &&
11392             (cmd != SCMD_MODE_SELECT) && (cmd != SCMD_MODE_SELECT_G1))
11393                 un->un_f_sync_cache_required = TRUE;
11394 
11395         if (sd_failfast_enable & SD_FAILFAST_ENABLE_FAIL_USCSI) {
11396                 /*
11397                  * If there are outstanding commands, treat all
11398                  * USCSI commands as if they have B_FAILFAST set.
11399                  */
11400                 if (un->un_ncmds_in_driver != 1)
11401                         bp->b_flags |= B_FAILFAST;
11402         }
11403 
11404         mutex_exit(SD_MUTEX(un));
11405 
11406         switch (uip->ui_flags) {
11407         case SD_PATH_DIRECT:
11408                 chain_type = SD_CHAIN_DIRECT;
11409                 break;
11410         case SD_PATH_DIRECT_PRIORITY:
11411                 chain_type = SD_CHAIN_DIRECT_PRIORITY;
11412                 break;
11413         default:
11414                 chain_type = SD_CHAIN_USCSI;
11415                 break;
11416         }
11417 
11418         /*
11419          * We may allocate extra buf for external USCSI commands. If the
11420          * application asks for bigger than 20-byte sense data via USCSI,
11421          * SCSA layer will allocate 252 bytes sense buf for that command.
11422          */
11423         if (((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_rqlen >
11424             SENSE_LENGTH) {
11425                 xp = kmem_zalloc(sizeof (struct sd_xbuf) - SENSE_LENGTH +
11426                     MAX_SENSE_LENGTH, KM_SLEEP);
11427         } else {
11428                 xp = kmem_zalloc(sizeof (struct sd_xbuf), KM_SLEEP);
11429         }
11430 
11431         sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp);
11432 
11433         /* Use the index obtained within xbuf_init */
11434         SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp);
11435 
11436         SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp);
11437 
11438         return (0);
11439 }
11440 
11441 /*
11442  *    Function: sd_send_scsi_cmd
11443  *
11444  * Description: Runs a USCSI command for user (when called thru sdioctl),
11445  *              or for the driver
11446  *
11447  *   Arguments: dev - the dev_t for the device
11448  *              incmd - ptr to a valid uscsi_cmd struct
11449  *              flag - bit flag, indicating open settings, 32/64 bit type
11450  *              dataspace - UIO_USERSPACE or UIO_SYSSPACE
11451  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11452  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11453  *                      to use the USCSI "direct" chain and bypass the normal
11454  *                      command waitq.
11455  *
11456  * Return Code: 0 -  successful completion of the given command
11457  *              EIO - scsi_uscsi_handle_command() failed
11458  *              ENXIO  - soft state not found for specified dev
11459  *              EINVAL
11460  *              EFAULT - copyin/copyout error
11461  *              return code of scsi_uscsi_handle_command():
11462  *                      EIO
11463  *                      ENXIO
11464  *                      EACCES
11465  *
11466  *     Context: Waits for command to complete. Can sleep.
11467  */
11468 
11469 static int
11470 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
11471     enum uio_seg dataspace, int path_flag)
11472 {
11473         struct sd_lun   *un;
11474         sd_ssc_t        *ssc;
11475         int             rval;
11476 
11477         un = ddi_get_soft_state(sd_state, SDUNIT(dev));
11478         if (un == NULL || DEVI_IS_GONE(SD_DEVINFO(un)))
11479                 return (ENXIO);
11480 
11481         /*
11482          * Using sd_ssc_send to handle uscsi cmd
11483          */
11484         ssc = sd_ssc_init(un);
11485         rval = sd_ssc_send(ssc, incmd, flag, dataspace, path_flag);
11486         sd_ssc_fini(ssc);
11487 
11488         return (rval);
11489 }
11490 
11491 /*
11492  *    Function: sd_ssc_init
11493  *
11494  * Description: Uscsi end-user call this function to initialize necessary
11495  *              fields, such as uscsi_cmd and sd_uscsi_info struct.
11496  *
11497  *              The return value of sd_send_scsi_cmd will be treated as a
11498  *              fault in various conditions. Even it is not Zero, some
11499  *              callers may ignore the return value. That is to say, we can
11500  *              not make an accurate assessment in sdintr, since if a
11501  *              command is failed in sdintr it does not mean the caller of
11502  *              sd_send_scsi_cmd will treat it as a real failure.
11503  *
11504  *              To avoid printing too many error logs for a failed uscsi
11505  *              packet that the caller may not treat it as a failure, the
11506  *              sd will keep silent for handling all uscsi commands.
11507  *
11508  *              During detach->attach and attach-open, for some types of
11509  *              problems, the driver should be providing information about
11510  *              the problem encountered. Device use USCSI_SILENT, which
11511  *              suppresses all driver information. The result is that no
11512  *              information about the problem is available. Being
11513  *              completely silent during this time is inappropriate. The
11514  *              driver needs a more selective filter than USCSI_SILENT, so
11515  *              that information related to faults is provided.
11516  *
11517  *              To make the accurate accessment, the caller  of
11518  *              sd_send_scsi_USCSI_CMD should take the ownership and
11519  *              get necessary information to print error messages.
11520  *
11521  *              If we want to print necessary info of uscsi command, we need to
11522  *              keep the uscsi_cmd and sd_uscsi_info till we can make the
11523  *              assessment. We use sd_ssc_init to alloc necessary
11524  *              structs for sending an uscsi command and we are also
11525  *              responsible for free the memory by calling
11526  *              sd_ssc_fini.
11527  *
11528  *              The calling secquences will look like:
11529  *              sd_ssc_init->
11530  *
11531  *                  ...
11532  *
11533  *                  sd_send_scsi_USCSI_CMD->
11534  *                      sd_ssc_send-> - - - sdintr
11535  *                  ...
11536  *
11537  *                  if we think the return value should be treated as a
11538  *                  failure, we make the accessment here and print out
11539  *                  necessary by retrieving uscsi_cmd and sd_uscsi_info'
11540  *
11541  *                  ...
11542  *
11543  *              sd_ssc_fini
11544  *
11545  *
11546  *   Arguments: un - pointer to driver soft state (unit) structure for this
11547  *                   target.
11548  *
11549  * Return code: sd_ssc_t - pointer to allocated sd_ssc_t struct, it contains
11550  *                         uscsi_cmd and sd_uscsi_info.
11551  *                  NULL - if can not alloc memory for sd_ssc_t struct
11552  *
11553  *     Context: Kernel Thread.
11554  */
11555 static sd_ssc_t *
11556 sd_ssc_init(struct sd_lun *un)
11557 {
11558         sd_ssc_t                *ssc;
11559         struct uscsi_cmd        *ucmdp;
11560         struct sd_uscsi_info    *uip;
11561 
11562         ASSERT(un != NULL);
11563         ASSERT(!mutex_owned(SD_MUTEX(un)));
11564 
11565         /*
11566          * Allocate sd_ssc_t structure
11567          */
11568         ssc = kmem_zalloc(sizeof (sd_ssc_t), KM_SLEEP);
11569 
11570         /*
11571          * Allocate uscsi_cmd by calling scsi_uscsi_alloc common routine
11572          */
11573         ucmdp = scsi_uscsi_alloc();
11574 
11575         /*
11576          * Allocate sd_uscsi_info structure
11577          */
11578         uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
11579 
11580         ssc->ssc_uscsi_cmd = ucmdp;
11581         ssc->ssc_uscsi_info = uip;
11582         ssc->ssc_un = un;
11583 
11584         return (ssc);
11585 }
11586 
11587 /*
11588  * Function: sd_ssc_fini
11589  *
11590  * Description: To free sd_ssc_t and it's hanging off
11591  *
11592  * Arguments: ssc - struct pointer of sd_ssc_t.
11593  */
11594 static void
11595 sd_ssc_fini(sd_ssc_t *ssc)
11596 {
11597         scsi_uscsi_free(ssc->ssc_uscsi_cmd);
11598 
11599         if (ssc->ssc_uscsi_info != NULL) {
11600                 kmem_free(ssc->ssc_uscsi_info, sizeof (struct sd_uscsi_info));
11601                 ssc->ssc_uscsi_info = NULL;
11602         }
11603 
11604         kmem_free(ssc, sizeof (sd_ssc_t));
11605         ssc = NULL;
11606 }
11607 
11608 /*
11609  * Function: sd_ssc_send
11610  *
11611  * Description: Runs a USCSI command for user when called through sdioctl,
11612  *              or for the driver.
11613  *
11614  *   Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
11615  *                    sd_uscsi_info in.
11616  *              incmd - ptr to a valid uscsi_cmd struct
11617  *              flag - bit flag, indicating open settings, 32/64 bit type
11618  *              dataspace - UIO_USERSPACE or UIO_SYSSPACE
11619  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11620  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11621  *                      to use the USCSI "direct" chain and bypass the normal
11622  *                      command waitq.
11623  *
11624  * Return Code: 0 -  successful completion of the given command
11625  *              EIO - scsi_uscsi_handle_command() failed
11626  *              ENXIO  - soft state not found for specified dev
11627  *              ECANCELED - command cancelled due to low power
11628  *              EINVAL
11629  *              EFAULT - copyin/copyout error
11630  *              return code of scsi_uscsi_handle_command():
11631  *                      EIO
11632  *                      ENXIO
11633  *                      EACCES
11634  *
11635  *     Context: Kernel Thread;
11636  *              Waits for command to complete. Can sleep.
11637  */
11638 static int
11639 sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, int flag,
11640     enum uio_seg dataspace, int path_flag)
11641 {
11642         struct sd_uscsi_info    *uip;
11643         struct uscsi_cmd        *uscmd;
11644         struct sd_lun           *un;
11645         dev_t                   dev;
11646         dev_info_t              *dip = SD_DEVINFO(ssc->ssc_un);
11647 
11648         int     format = 0;
11649         int     rval;
11650 
11651         ASSERT(ssc != NULL);
11652         un = ssc->ssc_un;
11653         ASSERT(un != NULL);
11654         uscmd = ssc->ssc_uscsi_cmd;
11655         ASSERT(uscmd != NULL);
11656         ASSERT(!mutex_owned(SD_MUTEX(un)));
11657         if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
11658                 /*
11659                  * If enter here, it indicates that the previous uscsi
11660                  * command has not been processed by sd_ssc_assessment.
11661                  * This is violating our rules of FMA telemetry processing.
11662                  * We should print out this message and the last undisposed
11663                  * uscsi command.
11664                  */
11665                 if (uscmd->uscsi_cdb != NULL) {
11666                         SD_INFO(SD_LOG_SDTEST, un,
11667                             "sd_ssc_send is missing the alternative "
11668                             "sd_ssc_assessment when running command 0x%x.\n",
11669                             uscmd->uscsi_cdb[0]);
11670                 }
11671                 /*
11672                  * Set the ssc_flags to SSC_FLAGS_UNKNOWN, which should be
11673                  * the initial status.
11674                  */
11675                 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
11676         }
11677 
11678         /*
11679          * We need to make sure sd_ssc_send will have sd_ssc_assessment
11680          * followed to avoid missing FMA telemetries.
11681          */
11682         ssc->ssc_flags |= SSC_FLAGS_NEED_ASSESSMENT;
11683 
11684         /*
11685          * if USCSI_PMFAILFAST is set and un is in low power, fail the
11686          * command immediately.
11687          */
11688         mutex_enter(SD_MUTEX(un));
11689         mutex_enter(&un->un_pm_mutex);
11690 
11691         if ((uscmd->uscsi_flags & USCSI_PMFAILFAST) &&
11692             SD_DEVICE_IS_IN_LOW_POWER(un)) {
11693                 SD_TRACE(SD_LOG_IO, un, "sd_ssc_send:"
11694                     "un:0x%p is in low power\n", un);
11695                 mutex_exit(&un->un_pm_mutex);
11696                 mutex_exit(SD_MUTEX(un));
11697                 return (ECANCELED);
11698         }
11699         mutex_exit(&un->un_pm_mutex);
11700         mutex_exit(SD_MUTEX(un));
11701 
11702 #ifdef SDDEBUG
11703         switch (dataspace) {
11704         case UIO_USERSPACE:
11705                 SD_TRACE(SD_LOG_IO, un,
11706                     "sd_ssc_send: entry: un:0x%p UIO_USERSPACE\n", un);
11707                 break;
11708         case UIO_SYSSPACE:
11709                 SD_TRACE(SD_LOG_IO, un,
11710                     "sd_ssc_send: entry: un:0x%p UIO_SYSSPACE\n", un);
11711                 break;
11712         default:
11713                 SD_TRACE(SD_LOG_IO, un,
11714                     "sd_ssc_send: entry: un:0x%p UNEXPECTED SPACE\n", un);
11715                 break;
11716         }
11717 #endif
11718 
11719         rval = scsi_uscsi_copyin((intptr_t)incmd, flag,
11720             SD_ADDRESS(un), &uscmd);
11721         if (rval != 0) {
11722                 SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: "
11723                     "scsi_uscsi_alloc_and_copyin failed\n", un);
11724                 return (rval);
11725         }
11726 
11727         if ((uscmd->uscsi_cdb != NULL) &&
11728             (uscmd->uscsi_cdb[0] == SCMD_FORMAT)) {
11729                 mutex_enter(SD_MUTEX(un));
11730                 un->un_f_format_in_progress = TRUE;
11731                 mutex_exit(SD_MUTEX(un));
11732                 format = 1;
11733         }
11734 
11735         /*
11736          * Allocate an sd_uscsi_info struct and fill it with the info
11737          * needed by sd_initpkt_for_uscsi().  Then put the pointer into
11738          * b_private in the buf for sd_initpkt_for_uscsi().  Note that
11739          * since we allocate the buf here in this function, we do not
11740          * need to preserve the prior contents of b_private.
11741          * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
11742          */
11743         uip = ssc->ssc_uscsi_info;
11744         uip->ui_flags = path_flag;
11745         uip->ui_cmdp = uscmd;
11746 
11747         /*
11748          * Commands sent with priority are intended for error recovery
11749          * situations, and do not have retries performed.
11750          */
11751         if (path_flag == SD_PATH_DIRECT_PRIORITY) {
11752                 uscmd->uscsi_flags |= USCSI_DIAGNOSE;
11753         }
11754         uscmd->uscsi_flags &= ~USCSI_NOINTR;
11755 
11756         dev = SD_GET_DEV(un);
11757         rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd,
11758             sd_uscsi_strategy, NULL, uip);
11759         if (DEVI_IS_GONE(dip)) {
11760                 cmn_err(CE_WARN, "%s-%d: device is gone!", __func__, __LINE__);
11761                 return (ENXIO);
11762         }
11763 
11764         /*
11765          * mark ssc_flags right after handle_cmd to make sure
11766          * the uscsi has been sent
11767          */
11768         ssc->ssc_flags |= SSC_FLAGS_CMD_ISSUED;
11769 
11770 #ifdef SDDEBUG
11771         SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
11772             "uscsi_status: 0x%02x  uscsi_resid:0x%x\n",
11773             uscmd->uscsi_status, uscmd->uscsi_resid);
11774         if (uscmd->uscsi_bufaddr != NULL) {
11775                 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
11776                     "uscmd->uscsi_bufaddr: 0x%p  uscmd->uscsi_buflen:%d\n",
11777                     uscmd->uscsi_bufaddr, uscmd->uscsi_buflen);
11778                 if (dataspace == UIO_SYSSPACE) {
11779                         SD_DUMP_MEMORY(un, SD_LOG_IO,
11780                             "data", (uchar_t *)uscmd->uscsi_bufaddr,
11781                             uscmd->uscsi_buflen, SD_LOG_HEX);
11782                 }
11783         }
11784 #endif
11785 
11786         if (format == 1) {
11787                 mutex_enter(SD_MUTEX(un));
11788                 un->un_f_format_in_progress = FALSE;
11789                 mutex_exit(SD_MUTEX(un));
11790         }
11791 
11792         (void) scsi_uscsi_copyout((intptr_t)incmd, uscmd);
11793 
11794         return (rval);
11795 }
11796 
11797 /*
11798  *     Function: sd_ssc_print
11799  *
11800  * Description: Print information available to the console.
11801  *
11802  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
11803  *                    sd_uscsi_info in.
11804  *            sd_severity - log level.
11805  *     Context: Kernel thread or interrupt context.
11806  */
11807 static void
11808 sd_ssc_print(sd_ssc_t *ssc, int sd_severity)
11809 {
11810         struct uscsi_cmd        *ucmdp;
11811         struct scsi_device      *devp;
11812         dev_info_t              *devinfo;
11813         uchar_t                 *sensep;
11814         int                     senlen;
11815         union scsi_cdb          *cdbp;
11816         uchar_t                 com;
11817         extern struct scsi_key_strings scsi_cmds[];
11818 
11819         ASSERT(ssc != NULL);
11820         ASSERT(ssc->ssc_un != NULL);
11821 
11822         if (SD_FM_LOG(ssc->ssc_un) != SD_FM_LOG_EREPORT)
11823                 return;
11824         ucmdp = ssc->ssc_uscsi_cmd;
11825         devp = SD_SCSI_DEVP(ssc->ssc_un);
11826         devinfo = SD_DEVINFO(ssc->ssc_un);
11827         ASSERT(ucmdp != NULL);
11828         ASSERT(devp != NULL);
11829         ASSERT(devinfo != NULL);
11830         sensep = (uint8_t *)ucmdp->uscsi_rqbuf;
11831         senlen = ucmdp->uscsi_rqlen - ucmdp->uscsi_rqresid;
11832         cdbp = (union scsi_cdb *)ucmdp->uscsi_cdb;
11833 
11834         /* In certain case (like DOORLOCK), the cdb could be NULL. */
11835         if (cdbp == NULL)
11836                 return;
11837         /* We don't print log if no sense data available. */
11838         if (senlen == 0)
11839                 sensep = NULL;
11840         com = cdbp->scc_cmd;
11841         scsi_generic_errmsg(devp, sd_label, sd_severity, 0, 0, com,
11842             scsi_cmds, sensep, ssc->ssc_un->un_additional_codes, NULL);
11843 }
11844 
11845 /*
11846  *     Function: sd_ssc_assessment
11847  *
11848  * Description: We use this function to make an assessment at the point
11849  *              where SD driver may encounter a potential error.
11850  *
11851  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
11852  *                  sd_uscsi_info in.
11853  *            tp_assess - a hint of strategy for ereport posting.
11854  *            Possible values of tp_assess include:
11855  *                SD_FMT_IGNORE - we don't post any ereport because we're
11856  *                sure that it is ok to ignore the underlying problems.
11857  *                SD_FMT_IGNORE_COMPROMISE - we don't post any ereport for now
11858  *                but it might be not correct to ignore the underlying hardware
11859  *                error.
11860  *                SD_FMT_STATUS_CHECK - we will post an ereport with the
11861  *                payload driver-assessment of value "fail" or
11862  *                "fatal"(depending on what information we have here). This
11863  *                assessment value is usually set when SD driver think there
11864  *                is a potential error occurred(Typically, when return value
11865  *                of the SCSI command is EIO).
11866  *                SD_FMT_STANDARD - we will post an ereport with the payload
11867  *                driver-assessment of value "info". This assessment value is
11868  *                set when the SCSI command returned successfully and with
11869  *                sense data sent back.
11870  *
11871  *     Context: Kernel thread.
11872  */
11873 static void
11874 sd_ssc_assessment(sd_ssc_t *ssc, enum sd_type_assessment tp_assess)
11875 {
11876         int senlen = 0;
11877         struct uscsi_cmd *ucmdp = NULL;
11878         struct sd_lun *un;
11879 
11880         ASSERT(ssc != NULL);
11881         un = ssc->ssc_un;
11882         ASSERT(un != NULL);
11883         ucmdp = ssc->ssc_uscsi_cmd;
11884         ASSERT(ucmdp != NULL);
11885 
11886         if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
11887                 ssc->ssc_flags &= ~SSC_FLAGS_NEED_ASSESSMENT;
11888         } else {
11889                 /*
11890                  * If enter here, it indicates that we have a wrong
11891                  * calling sequence of sd_ssc_send and sd_ssc_assessment,
11892                  * both of which should be called in a pair in case of
11893                  * loss of FMA telemetries.
11894                  */
11895                 if (ucmdp->uscsi_cdb != NULL) {
11896                         SD_INFO(SD_LOG_SDTEST, un,
11897                             "sd_ssc_assessment is missing the "
11898                             "alternative sd_ssc_send when running 0x%x, "
11899                             "or there are superfluous sd_ssc_assessment for "
11900                             "the same sd_ssc_send.\n",
11901                             ucmdp->uscsi_cdb[0]);
11902                 }
11903                 /*
11904                  * Set the ssc_flags to the initial value to avoid passing
11905                  * down dirty flags to the following sd_ssc_send function.
11906                  */
11907                 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
11908                 return;
11909         }
11910 
11911         /*
11912          * Only handle an issued command which is waiting for assessment.
11913          * A command which is not issued will not have
11914          * SSC_FLAGS_INVALID_DATA set, so it'ok we just return here.
11915          */
11916         if (!(ssc->ssc_flags & SSC_FLAGS_CMD_ISSUED)) {
11917                 sd_ssc_print(ssc, SCSI_ERR_INFO);
11918                 return;
11919         } else {
11920                 /*
11921                  * For an issued command, we should clear this flag in
11922                  * order to make the sd_ssc_t structure be used off
11923                  * multiple uscsi commands.
11924                  */
11925                 ssc->ssc_flags &= ~SSC_FLAGS_CMD_ISSUED;
11926         }
11927 
11928         /*
11929          * We will not deal with non-retryable(flag USCSI_DIAGNOSE set)
11930          * commands here. And we should clear the ssc_flags before return.
11931          */
11932         if (ucmdp->uscsi_flags & USCSI_DIAGNOSE) {
11933                 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
11934                 return;
11935         }
11936 
11937         switch (tp_assess) {
11938         case SD_FMT_IGNORE:
11939         case SD_FMT_IGNORE_COMPROMISE:
11940                 break;
11941         case SD_FMT_STATUS_CHECK:
11942                 /*
11943                  * For a failed command(including the succeeded command
11944                  * with invalid data sent back).
11945                  */
11946                 sd_ssc_post(ssc, SD_FM_DRV_FATAL);
11947                 break;
11948         case SD_FMT_STANDARD:
11949                 /*
11950                  * Always for the succeeded commands probably with sense
11951                  * data sent back.
11952                  * Limitation:
11953                  *      We can only handle a succeeded command with sense
11954                  *      data sent back when auto-request-sense is enabled.
11955                  */
11956                 senlen = ssc->ssc_uscsi_cmd->uscsi_rqlen -
11957                     ssc->ssc_uscsi_cmd->uscsi_rqresid;
11958                 if ((ssc->ssc_uscsi_info->ui_pkt_state & STATE_ARQ_DONE) &&
11959                     (un->un_f_arq_enabled == TRUE) &&
11960                     senlen > 0 &&
11961                     ssc->ssc_uscsi_cmd->uscsi_rqbuf != NULL) {
11962                         sd_ssc_post(ssc, SD_FM_DRV_NOTICE);
11963                 }
11964                 break;
11965         default:
11966                 /*
11967                  * Should not have other type of assessment.
11968                  */
11969                 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
11970                     "sd_ssc_assessment got wrong "
11971                     "sd_type_assessment %d.\n", tp_assess);
11972                 break;
11973         }
11974         /*
11975          * Clear up the ssc_flags before return.
11976          */
11977         ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
11978 }
11979 
11980 /*
11981  *    Function: sd_ssc_post
11982  *
11983  * Description: 1. read the driver property to get fm-scsi-log flag.
11984  *              2. print log if fm_log_capable is non-zero.
11985  *              3. call sd_ssc_ereport_post to post ereport if possible.
11986  *
11987  *    Context: May be called from kernel thread or interrupt context.
11988  */
11989 static void
11990 sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess)
11991 {
11992         struct sd_lun   *un;
11993         int             sd_severity;
11994 
11995         ASSERT(ssc != NULL);
11996         un = ssc->ssc_un;
11997         ASSERT(un != NULL);
11998 
11999         /*
12000          * We may enter here from sd_ssc_assessment(for USCSI command) or
12001          * by directly called from sdintr context.
12002          * We don't handle a non-disk drive(CD-ROM, removable media).
12003          * Clear the ssc_flags before return in case we've set
12004          * SSC_FLAGS_INVALID_XXX which should be skipped for a non-disk
12005          * driver.
12006          */
12007         if (ISCD(un) || un->un_f_has_removable_media) {
12008                 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12009                 return;
12010         }
12011 
12012         switch (sd_assess) {
12013                 case SD_FM_DRV_FATAL:
12014                         sd_severity = SCSI_ERR_FATAL;
12015                         break;
12016                 case SD_FM_DRV_RECOVERY:
12017                         sd_severity = SCSI_ERR_RECOVERED;
12018                         break;
12019                 case SD_FM_DRV_RETRY:
12020                         sd_severity = SCSI_ERR_RETRYABLE;
12021                         break;
12022                 case SD_FM_DRV_NOTICE:
12023                         sd_severity = SCSI_ERR_INFO;
12024                         break;
12025                 default:
12026                         sd_severity = SCSI_ERR_UNKNOWN;
12027         }
12028         /* print log */
12029         sd_ssc_print(ssc, sd_severity);
12030 
12031         /* always post ereport */
12032         sd_ssc_ereport_post(ssc, sd_assess);
12033 }
12034 
12035 /*
12036  *    Function: sd_ssc_set_info
12037  *
12038  * Description: Mark ssc_flags and set ssc_info which would be the
12039  *              payload of uderr ereport. This function will cause
12040  *              sd_ssc_ereport_post to post uderr ereport only.
12041  *              Besides, when ssc_flags == SSC_FLAGS_INVALID_DATA(USCSI),
12042  *              the function will also call SD_ERROR or scsi_log for a
12043  *              CDROM/removable-media/DDI_FM_NOT_CAPABLE device.
12044  *
12045  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12046  *                  sd_uscsi_info in.
12047  *            ssc_flags - indicate the sub-category of a uderr.
12048  *            comp - this argument is meaningful only when
12049  *                   ssc_flags == SSC_FLAGS_INVALID_DATA, and its possible
12050  *                   values include:
12051  *                   > 0, SD_ERROR is used with comp as the driver logging
12052  *                   component;
12053  *                   = 0, scsi-log is used to log error telemetries;
12054  *                   < 0, no log available for this telemetry.
12055  *
12056  *    Context: Kernel thread or interrupt context
12057  */
12058 static void
12059 sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp, const char *fmt, ...)
12060 {
12061         va_list ap;
12062 
12063         ASSERT(ssc != NULL);
12064         ASSERT(ssc->ssc_un != NULL);
12065 
12066         ssc->ssc_flags |= ssc_flags;
12067         va_start(ap, fmt);
12068         (void) vsnprintf(ssc->ssc_info, sizeof (ssc->ssc_info), fmt, ap);
12069         va_end(ap);
12070 
12071         /*
12072          * If SSC_FLAGS_INVALID_DATA is set, it should be a uscsi command
12073          * with invalid data sent back. For non-uscsi command, the
12074          * following code will be bypassed.
12075          */
12076         if (ssc_flags & SSC_FLAGS_INVALID_DATA) {
12077                 if (SD_FM_LOG(ssc->ssc_un) == SD_FM_LOG_NSUP) {
12078                         /*
12079                          * If the error belong to certain component and we
12080                          * do not want it to show up on the console, we
12081                          * will use SD_ERROR, otherwise scsi_log is
12082                          * preferred.
12083                          */
12084                         if (comp > 0) {
12085                                 SD_ERROR(comp, ssc->ssc_un, ssc->ssc_info);
12086                         } else if (comp == 0) {
12087                                 scsi_log(SD_DEVINFO(ssc->ssc_un), sd_label,
12088                                     CE_WARN, ssc->ssc_info);
12089                         }
12090                 }
12091         }
12092 }
12093 
12094 /*
12095  *    Function: sd_buf_iodone
12096  *
12097  * Description: Frees the sd_xbuf & returns the buf to its originator.
12098  *
12099  *     Context: May be called from interrupt context.
12100  */
12101 /* ARGSUSED */
12102 static void
12103 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp)
12104 {
12105         struct sd_xbuf *xp;
12106 
12107         ASSERT(un != NULL);
12108         ASSERT(bp != NULL);
12109         ASSERT(!mutex_owned(SD_MUTEX(un)));
12110 
12111         SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n");
12112 
12113         xp = SD_GET_XBUF(bp);
12114         ASSERT(xp != NULL);
12115 
12116         /* xbuf is gone after this */
12117         if (ddi_xbuf_done(bp, un->un_xbuf_attr)) {
12118                 mutex_enter(SD_MUTEX(un));
12119 
12120                 /*
12121                  * Grab time when the cmd completed.
12122                  * This is used for determining if the system has been
12123                  * idle long enough to make it idle to the PM framework.
12124                  * This is for lowering the overhead, and therefore improving
12125                  * performance per I/O operation.
12126                  */
12127                 un->un_pm_idle_time = gethrtime();
12128 
12129                 un->un_ncmds_in_driver--;
12130                 ASSERT(un->un_ncmds_in_driver >= 0);
12131                 if (un->un_f_detach_waiting)
12132                         cv_signal(&un->un_detach_cv);
12133                 SD_INFO(SD_LOG_IO, un,
12134                     "sd_buf_iodone: un_ncmds_in_driver = %ld\n",
12135                     un->un_ncmds_in_driver);
12136 
12137                 mutex_exit(SD_MUTEX(un));
12138         }
12139 
12140         biodone(bp);                            /* bp is gone after this */
12141 
12142         SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n");
12143 }
12144 
12145 
12146 /*
12147  *    Function: sd_uscsi_iodone
12148  *
12149  * Description: Frees the sd_xbuf & returns the buf to its originator.
12150  *
12151  *     Context: May be called from interrupt context.
12152  */
12153 /* ARGSUSED */
12154 static void
12155 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
12156 {
12157         struct sd_xbuf *xp;
12158 
12159         ASSERT(un != NULL);
12160         ASSERT(bp != NULL);
12161 
12162         xp = SD_GET_XBUF(bp);
12163         ASSERT(xp != NULL);
12164         ASSERT(!mutex_owned(SD_MUTEX(un)));
12165 
12166         SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n");
12167 
12168         bp->b_private = xp->xb_private;
12169 
12170         mutex_enter(SD_MUTEX(un));
12171 
12172         /*
12173          * Grab time when the cmd completed.
12174          * This is used for determining if the system has been
12175          * idle long enough to make it idle to the PM framework.
12176          * This is for lowering the overhead, and therefore improving
12177          * performance per I/O operation.
12178          */
12179         un->un_pm_idle_time = gethrtime();
12180 
12181         un->un_ncmds_in_driver--;
12182         ASSERT(un->un_ncmds_in_driver >= 0);
12183         if (un->un_f_detach_waiting)
12184                 cv_signal(&un->un_detach_cv);
12185         SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n",
12186             un->un_ncmds_in_driver);
12187 
12188         mutex_exit(SD_MUTEX(un));
12189 
12190         if (((struct uscsi_cmd *)(xp->xb_pktinfo))->uscsi_rqlen >
12191             SENSE_LENGTH) {
12192                 kmem_free(xp, sizeof (struct sd_xbuf) - SENSE_LENGTH +
12193                     MAX_SENSE_LENGTH);
12194         } else {
12195                 kmem_free(xp, sizeof (struct sd_xbuf));
12196         }
12197 
12198         biodone(bp);
12199 
12200         SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n");
12201 }
12202 
12203 
12204 /*
12205  *    Function: sd_mapblockaddr_iostart
12206  *
12207  * Description: Verify request lies within the partition limits for
12208  *              the indicated minor device.  Issue "overrun" buf if
12209  *              request would exceed partition range.  Converts
12210  *              partition-relative block address to absolute.
12211  *
12212  *              Upon exit of this function:
12213  *              1.I/O is aligned
12214  *                 xp->xb_blkno represents the absolute sector address
12215  *              2.I/O is misaligned
12216  *                 xp->xb_blkno represents the absolute logical block address
12217  *                 based on DEV_BSIZE. The logical block address will be
12218  *                 converted to physical sector address in sd_mapblocksize_\
12219  *                 iostart.
12220  *              3.I/O is misaligned but is aligned in "overrun" buf
12221  *                 xp->xb_blkno represents the absolute logical block address
12222  *                 based on DEV_BSIZE. The logical block address will be
12223  *                 converted to physical sector address in sd_mapblocksize_\
12224  *                 iostart. But no RMW will be issued in this case.
12225  *
12226  *     Context: Can sleep
12227  *
12228  *      Issues: This follows what the old code did, in terms of accessing
12229  *              some of the partition info in the unit struct without holding
12230  *              the mutext.  This is a general issue, if the partition info
12231  *              can be altered while IO is in progress... as soon as we send
12232  *              a buf, its partitioning can be invalid before it gets to the
12233  *              device.  Probably the right fix is to move partitioning out
12234  *              of the driver entirely.
12235  */
12236 
12237 static void
12238 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp)
12239 {
12240         diskaddr_t      nblocks;        /* #blocks in the given partition */
12241         daddr_t blocknum;       /* Block number specified by the buf */
12242         size_t  requested_nblocks;
12243         size_t  available_nblocks;
12244         int     partition;
12245         diskaddr_t      partition_offset;
12246         struct sd_xbuf *xp;
12247         int secmask = 0, blknomask = 0;
12248         ushort_t is_aligned = TRUE;
12249 
12250         ASSERT(un != NULL);
12251         ASSERT(bp != NULL);
12252         ASSERT(!mutex_owned(SD_MUTEX(un)));
12253 
12254         SD_TRACE(SD_LOG_IO_PARTITION, un,
12255             "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp);
12256 
12257         xp = SD_GET_XBUF(bp);
12258         ASSERT(xp != NULL);
12259 
12260         /*
12261          * If the geometry is not indicated as valid, attempt to access
12262          * the unit & verify the geometry/label. This can be the case for
12263          * removable-media devices, of if the device was opened in
12264          * NDELAY/NONBLOCK mode.
12265          */
12266         partition = SDPART(bp->b_edev);
12267 
12268         if (!SD_IS_VALID_LABEL(un)) {
12269                 sd_ssc_t *ssc;
12270                 /*
12271                  * Initialize sd_ssc_t for internal uscsi commands
12272                  * In case of potential porformance issue, we need
12273                  * to alloc memory only if there is invalid label
12274                  */
12275                 ssc = sd_ssc_init(un);
12276 
12277                 if (sd_ready_and_valid(ssc, partition) != SD_READY_VALID) {
12278                         /*
12279                          * For removable devices it is possible to start an
12280                          * I/O without a media by opening the device in nodelay
12281                          * mode. Also for writable CDs there can be many
12282                          * scenarios where there is no geometry yet but volume
12283                          * manager is trying to issue a read() just because
12284                          * it can see TOC on the CD. So do not print a message
12285                          * for removables.
12286                          */
12287                         if (!un->un_f_has_removable_media) {
12288                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
12289                                     "i/o to invalid geometry\n");
12290                         }
12291                         bioerror(bp, EIO);
12292                         bp->b_resid = bp->b_bcount;
12293                         SD_BEGIN_IODONE(index, un, bp);
12294 
12295                         sd_ssc_fini(ssc);
12296                         return;
12297                 }
12298                 sd_ssc_fini(ssc);
12299         }
12300 
12301         nblocks = 0;
12302         (void) cmlb_partinfo(un->un_cmlbhandle, partition,
12303             &nblocks, &partition_offset, NULL, NULL, (void *)SD_PATH_DIRECT);
12304 
12305         if (un->un_f_enable_rmw) {
12306                 blknomask = (un->un_phy_blocksize / DEV_BSIZE) - 1;
12307                 secmask = un->un_phy_blocksize - 1;
12308         } else {
12309                 blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
12310                 secmask = un->un_tgt_blocksize - 1;
12311         }
12312 
12313         if ((bp->b_lblkno & (blknomask)) || (bp->b_bcount & (secmask))) {
12314                 is_aligned = FALSE;
12315         }
12316 
12317         if (!(NOT_DEVBSIZE(un)) || un->un_f_enable_rmw) {
12318                 /*
12319                  * If I/O is aligned, no need to involve RMW(Read Modify Write)
12320                  * Convert the logical block number to target's physical sector
12321                  * number.
12322                  */
12323                 if (is_aligned) {
12324                         xp->xb_blkno = SD_SYS2TGTBLOCK(un, xp->xb_blkno);
12325                 } else {
12326                         /*
12327                          * There is no RMW if we're just reading, so don't
12328                          * warn or error out because of it.
12329                          */
12330                         if (bp->b_flags & B_READ) {
12331                                 /*EMPTY*/
12332                         } else if (!un->un_f_enable_rmw &&
12333                             un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR) {
12334                                 bp->b_flags |= B_ERROR;
12335                                 goto error_exit;
12336                         } else if (un->un_f_rmw_type == SD_RMW_TYPE_DEFAULT) {
12337                                 mutex_enter(SD_MUTEX(un));
12338                                 if (!un->un_f_enable_rmw &&
12339                                     un->un_rmw_msg_timeid == NULL) {
12340                                         scsi_log(SD_DEVINFO(un), sd_label,
12341                                             CE_WARN, "I/O request is not "
12342                                             "aligned with %d disk sector size. "
12343                                             "It is handled through Read Modify "
12344                                             "Write but the performance is "
12345                                             "very low.\n",
12346                                             un->un_tgt_blocksize);
12347                                         un->un_rmw_msg_timeid =
12348                                             timeout(sd_rmw_msg_print_handler,
12349                                             un, SD_RMW_MSG_PRINT_TIMEOUT);
12350                                 } else {
12351                                         un->un_rmw_incre_count ++;
12352                                 }
12353                                 mutex_exit(SD_MUTEX(un));
12354                         }
12355 
12356                         nblocks = SD_TGT2SYSBLOCK(un, nblocks);
12357                         partition_offset = SD_TGT2SYSBLOCK(un,
12358                             partition_offset);
12359                 }
12360         }
12361 
12362         /*
12363          * blocknum is the starting block number of the request. At this
12364          * point it is still relative to the start of the minor device.
12365          */
12366         blocknum = xp->xb_blkno;
12367 
12368         /*
12369          * Legacy: If the starting block number is one past the last block
12370          * in the partition, do not set B_ERROR in the buf.
12371          */
12372         if (blocknum == nblocks)  {
12373                 goto error_exit;
12374         }
12375 
12376         /*
12377          * Confirm that the first block of the request lies within the
12378          * partition limits. Also the requested number of bytes must be
12379          * a multiple of the system block size.
12380          */
12381         if ((blocknum < 0) || (blocknum >= nblocks) ||
12382             ((bp->b_bcount & (DEV_BSIZE - 1)) != 0)) {
12383                 bp->b_flags |= B_ERROR;
12384                 goto error_exit;
12385         }
12386 
12387         /*
12388          * If the requsted # blocks exceeds the available # blocks, that
12389          * is an overrun of the partition.
12390          */
12391         if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12392                 requested_nblocks = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
12393         } else {
12394                 requested_nblocks = SD_BYTES2SYSBLOCKS(bp->b_bcount);
12395         }
12396 
12397         available_nblocks = (size_t)(nblocks - blocknum);
12398         ASSERT(nblocks >= blocknum);
12399 
12400         if (requested_nblocks > available_nblocks) {
12401                 size_t resid;
12402 
12403                 /*
12404                  * Allocate an "overrun" buf to allow the request to proceed
12405                  * for the amount of space available in the partition. The
12406                  * amount not transferred will be added into the b_resid
12407                  * when the operation is complete. The overrun buf
12408                  * replaces the original buf here, and the original buf
12409                  * is saved inside the overrun buf, for later use.
12410                  */
12411                 if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12412                         resid = SD_TGTBLOCKS2BYTES(un,
12413                             (offset_t)(requested_nblocks - available_nblocks));
12414                 } else {
12415                         resid = SD_SYSBLOCKS2BYTES(
12416                             (offset_t)(requested_nblocks - available_nblocks));
12417                 }
12418 
12419                 size_t count = bp->b_bcount - resid;
12420                 /*
12421                  * Note: count is an unsigned entity thus it'll NEVER
12422                  * be less than 0 so ASSERT the original values are
12423                  * correct.
12424                  */
12425                 ASSERT(bp->b_bcount >= resid);
12426 
12427                 bp = sd_bioclone_alloc(bp, count, blocknum,
12428                     (int (*)(struct buf *)) sd_mapblockaddr_iodone);
12429                 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */
12430                 ASSERT(xp != NULL);
12431         }
12432 
12433         /* At this point there should be no residual for this buf. */
12434         ASSERT(bp->b_resid == 0);
12435 
12436         /* Convert the block number to an absolute address. */
12437         xp->xb_blkno += partition_offset;
12438 
12439         SD_NEXT_IOSTART(index, un, bp);
12440 
12441         SD_TRACE(SD_LOG_IO_PARTITION, un,
12442             "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp);
12443 
12444         return;
12445 
12446 error_exit:
12447         bp->b_resid = bp->b_bcount;
12448         SD_BEGIN_IODONE(index, un, bp);
12449         SD_TRACE(SD_LOG_IO_PARTITION, un,
12450             "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp);
12451 }
12452 
12453 
12454 /*
12455  *    Function: sd_mapblockaddr_iodone
12456  *
12457  * Description: Completion-side processing for partition management.
12458  *
12459  *     Context: May be called under interrupt context
12460  */
12461 
12462 static void
12463 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp)
12464 {
12465         /* int  partition; */   /* Not used, see below. */
12466         ASSERT(un != NULL);
12467         ASSERT(bp != NULL);
12468         ASSERT(!mutex_owned(SD_MUTEX(un)));
12469 
12470         SD_TRACE(SD_LOG_IO_PARTITION, un,
12471             "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp);
12472 
12473         if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) {
12474                 /*
12475                  * We have an "overrun" buf to deal with...
12476                  */
12477                 struct sd_xbuf  *xp;
12478                 struct buf      *obp;   /* ptr to the original buf */
12479 
12480                 xp = SD_GET_XBUF(bp);
12481                 ASSERT(xp != NULL);
12482 
12483                 /* Retrieve the pointer to the original buf */
12484                 obp = (struct buf *)xp->xb_private;
12485                 ASSERT(obp != NULL);
12486 
12487                 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid);
12488                 bioerror(obp, bp->b_error);
12489 
12490                 sd_bioclone_free(bp);
12491 
12492                 /*
12493                  * Get back the original buf.
12494                  * Note that since the restoration of xb_blkno below
12495                  * was removed, the sd_xbuf is not needed.
12496                  */
12497                 bp = obp;
12498                 /*
12499                  * xp = SD_GET_XBUF(bp);
12500                  * ASSERT(xp != NULL);
12501                  */
12502         }
12503 
12504         /*
12505          * Convert sd->xb_blkno back to a minor-device relative value.
12506          * Note: this has been commented out, as it is not needed in the
12507          * current implementation of the driver (ie, since this function
12508          * is at the top of the layering chains, so the info will be
12509          * discarded) and it is in the "hot" IO path.
12510          *
12511          * partition = getminor(bp->b_edev) & SDPART_MASK;
12512          * xp->xb_blkno -= un->un_offset[partition];
12513          */
12514 
12515         SD_NEXT_IODONE(index, un, bp);
12516 
12517         SD_TRACE(SD_LOG_IO_PARTITION, un,
12518             "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp);
12519 }
12520 
12521 
12522 /*
12523  *    Function: sd_mapblocksize_iostart
12524  *
12525  * Description: Convert between system block size (un->un_sys_blocksize)
12526  *              and target block size (un->un_tgt_blocksize).
12527  *
12528  *     Context: Can sleep to allocate resources.
12529  *
12530  * Assumptions: A higher layer has already performed any partition validation,
12531  *              and converted the xp->xb_blkno to an absolute value relative
12532  *              to the start of the device.
12533  *
12534  *              It is also assumed that the higher layer has implemented
12535  *              an "overrun" mechanism for the case where the request would
12536  *              read/write beyond the end of a partition.  In this case we
12537  *              assume (and ASSERT) that bp->b_resid == 0.
12538  *
12539  *              Note: The implementation for this routine assumes the target
12540  *              block size remains constant between allocation and transport.
12541  */
12542 
12543 static void
12544 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp)
12545 {
12546         struct sd_mapblocksize_info     *bsp;
12547         struct sd_xbuf                  *xp;
12548         offset_t first_byte;
12549         daddr_t start_block, end_block;
12550         daddr_t request_bytes;
12551         ushort_t is_aligned = FALSE;
12552 
12553         ASSERT(un != NULL);
12554         ASSERT(bp != NULL);
12555         ASSERT(!mutex_owned(SD_MUTEX(un)));
12556         ASSERT(bp->b_resid == 0);
12557 
12558         SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12559             "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp);
12560 
12561         /*
12562          * For a non-writable CD, a write request is an error
12563          */
12564         if (ISCD(un) && ((bp->b_flags & B_READ) == 0) &&
12565             (un->un_f_mmc_writable_media == FALSE)) {
12566                 bioerror(bp, EIO);
12567                 bp->b_resid = bp->b_bcount;
12568                 SD_BEGIN_IODONE(index, un, bp);
12569                 return;
12570         }
12571 
12572         /*
12573          * We do not need a shadow buf if the device is using
12574          * un->un_sys_blocksize as its block size or if bcount == 0.
12575          * In this case there is no layer-private data block allocated.
12576          */
12577         if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
12578             (bp->b_bcount == 0)) {
12579                 goto done;
12580         }
12581 
12582         /* We do not support non-block-aligned transfers for ROD devices */
12583         ASSERT(!ISROD(un));
12584 
12585         xp = SD_GET_XBUF(bp);
12586         ASSERT(xp != NULL);
12587 
12588         SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12589             "tgt_blocksize:0x%x sys_blocksize: 0x%x\n",
12590             un->un_tgt_blocksize, DEV_BSIZE);
12591         SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12592             "request start block:0x%x\n", xp->xb_blkno);
12593         SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12594             "request len:0x%x\n", bp->b_bcount);
12595 
12596         /*
12597          * Allocate the layer-private data area for the mapblocksize layer.
12598          * Layers are allowed to use the xp_private member of the sd_xbuf
12599          * struct to store the pointer to their layer-private data block, but
12600          * each layer also has the responsibility of restoring the prior
12601          * contents of xb_private before returning the buf/xbuf to the
12602          * higher layer that sent it.
12603          *
12604          * Here we save the prior contents of xp->xb_private into the
12605          * bsp->mbs_oprivate field of our layer-private data area. This value
12606          * is restored by sd_mapblocksize_iodone() just prior to freeing up
12607          * the layer-private area and returning the buf/xbuf to the layer
12608          * that sent it.
12609          *
12610          * Note that here we use kmem_zalloc for the allocation as there are
12611          * parts of the mapblocksize code that expect certain fields to be
12612          * zero unless explicitly set to a required value.
12613          */
12614         bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12615         bsp->mbs_oprivate = xp->xb_private;
12616         xp->xb_private = bsp;
12617 
12618         /*
12619          * This treats the data on the disk (target) as an array of bytes.
12620          * first_byte is the byte offset, from the beginning of the device,
12621          * to the location of the request. This is converted from a
12622          * un->un_sys_blocksize block address to a byte offset, and then back
12623          * to a block address based upon a un->un_tgt_blocksize block size.
12624          *
12625          * xp->xb_blkno should be absolute upon entry into this function,
12626          * but, but it is based upon partitions that use the "system"
12627          * block size. It must be adjusted to reflect the block size of
12628          * the target.
12629          *
12630          * Note that end_block is actually the block that follows the last
12631          * block of the request, but that's what is needed for the computation.
12632          */
12633         first_byte  = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
12634         if (un->un_f_enable_rmw) {
12635                 start_block = xp->xb_blkno =
12636                     (first_byte / un->un_phy_blocksize) *
12637                     (un->un_phy_blocksize / DEV_BSIZE);
12638                 end_block   = ((first_byte + bp->b_bcount +
12639                     un->un_phy_blocksize - 1) / un->un_phy_blocksize) *
12640                     (un->un_phy_blocksize / DEV_BSIZE);
12641         } else {
12642                 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize;
12643                 end_block   = (first_byte + bp->b_bcount +
12644                     un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
12645         }
12646 
12647         /* request_bytes is rounded up to a multiple of the target block size */
12648         request_bytes = (end_block - start_block) * un->un_tgt_blocksize;
12649 
12650         /*
12651          * See if the starting address of the request and the request
12652          * length are aligned on a un->un_tgt_blocksize boundary. If aligned
12653          * then we do not need to allocate a shadow buf to handle the request.
12654          */
12655         if (un->un_f_enable_rmw) {
12656                 if (((first_byte % un->un_phy_blocksize) == 0) &&
12657                     ((bp->b_bcount % un->un_phy_blocksize) == 0)) {
12658                         is_aligned = TRUE;
12659                 }
12660         } else {
12661                 if (((first_byte % un->un_tgt_blocksize) == 0) &&
12662                     ((bp->b_bcount % un->un_tgt_blocksize) == 0)) {
12663                         is_aligned = TRUE;
12664                 }
12665         }
12666 
12667         if ((bp->b_flags & B_READ) == 0) {
12668                 /*
12669                  * Lock the range for a write operation. An aligned request is
12670                  * considered a simple write; otherwise the request must be a
12671                  * read-modify-write.
12672                  */
12673                 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1,
12674                     (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW);
12675         }
12676 
12677         /*
12678          * Alloc a shadow buf if the request is not aligned. Also, this is
12679          * where the READ command is generated for a read-modify-write. (The
12680          * write phase is deferred until after the read completes.)
12681          */
12682         if (is_aligned == FALSE) {
12683 
12684                 struct sd_mapblocksize_info     *shadow_bsp;
12685                 struct sd_xbuf  *shadow_xp;
12686                 struct buf      *shadow_bp;
12687 
12688                 /*
12689                  * Allocate the shadow buf and it associated xbuf. Note that
12690                  * after this call the xb_blkno value in both the original
12691                  * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the
12692                  * same: absolute relative to the start of the device, and
12693                  * adjusted for the target block size. The b_blkno in the
12694                  * shadow buf will also be set to this value. We should never
12695                  * change b_blkno in the original bp however.
12696                  *
12697                  * Note also that the shadow buf will always need to be a
12698                  * READ command, regardless of whether the incoming command
12699                  * is a READ or a WRITE.
12700                  */
12701                 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ,
12702                     xp->xb_blkno,
12703                     (int (*)(struct buf *)) sd_mapblocksize_iodone);
12704 
12705                 shadow_xp = SD_GET_XBUF(shadow_bp);
12706 
12707                 /*
12708                  * Allocate the layer-private data for the shadow buf.
12709                  * (No need to preserve xb_private in the shadow xbuf.)
12710                  */
12711                 shadow_xp->xb_private = shadow_bsp =
12712                     kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12713 
12714                 /*
12715                  * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone
12716                  * to figure out where the start of the user data is (based upon
12717                  * the system block size) in the data returned by the READ
12718                  * command (which will be based upon the target blocksize). Note
12719                  * that this is only really used if the request is unaligned.
12720                  */
12721                 if (un->un_f_enable_rmw) {
12722                         bsp->mbs_copy_offset = (ssize_t)(first_byte -
12723                             ((offset_t)xp->xb_blkno * un->un_sys_blocksize));
12724                         ASSERT((bsp->mbs_copy_offset >= 0) &&
12725                             (bsp->mbs_copy_offset < un->un_phy_blocksize));
12726                 } else {
12727                         bsp->mbs_copy_offset = (ssize_t)(first_byte -
12728                             ((offset_t)xp->xb_blkno * un->un_tgt_blocksize));
12729                         ASSERT((bsp->mbs_copy_offset >= 0) &&
12730                             (bsp->mbs_copy_offset < un->un_tgt_blocksize));
12731                 }
12732 
12733                 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset;
12734 
12735                 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index;
12736 
12737                 /* Transfer the wmap (if any) to the shadow buf */
12738                 shadow_bsp->mbs_wmp = bsp->mbs_wmp;
12739                 bsp->mbs_wmp = NULL;
12740 
12741                 /*
12742                  * The shadow buf goes on from here in place of the
12743                  * original buf.
12744                  */
12745                 shadow_bsp->mbs_orig_bp = bp;
12746                 bp = shadow_bp;
12747         }
12748 
12749         SD_INFO(SD_LOG_IO_RMMEDIA, un,
12750             "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno);
12751         SD_INFO(SD_LOG_IO_RMMEDIA, un,
12752             "sd_mapblocksize_iostart: tgt request len:0x%x\n",
12753             request_bytes);
12754         SD_INFO(SD_LOG_IO_RMMEDIA, un,
12755             "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp);
12756 
12757 done:
12758         SD_NEXT_IOSTART(index, un, bp);
12759 
12760         SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12761             "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp);
12762 }
12763 
12764 
12765 /*
12766  *    Function: sd_mapblocksize_iodone
12767  *
12768  * Description: Completion side processing for block-size mapping.
12769  *
12770  *     Context: May be called under interrupt context
12771  */
12772 
12773 static void
12774 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp)
12775 {
12776         struct sd_mapblocksize_info     *bsp;
12777         struct sd_xbuf  *xp;
12778         struct sd_xbuf  *orig_xp;       /* sd_xbuf for the original buf */
12779         struct buf      *orig_bp;       /* ptr to the original buf */
12780         offset_t        shadow_end;
12781         offset_t        request_end;
12782         offset_t        shadow_start;
12783         ssize_t         copy_offset;
12784         size_t          copy_length;
12785         size_t          shortfall;
12786         uint_t          is_write;       /* TRUE if this bp is a WRITE */
12787         uint_t          has_wmap;       /* TRUE is this bp has a wmap */
12788 
12789         ASSERT(un != NULL);
12790         ASSERT(bp != NULL);
12791 
12792         SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12793             "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp);
12794 
12795         /*
12796          * There is no shadow buf or layer-private data if the target is
12797          * using un->un_sys_blocksize as its block size or if bcount == 0.
12798          */
12799         if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
12800             (bp->b_bcount == 0)) {
12801                 goto exit;
12802         }
12803 
12804         xp = SD_GET_XBUF(bp);
12805         ASSERT(xp != NULL);
12806 
12807         /* Retrieve the pointer to the layer-private data area from the xbuf. */
12808         bsp = xp->xb_private;
12809 
12810         is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE;
12811         has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE;
12812 
12813         if (is_write) {
12814                 /*
12815                  * For a WRITE request we must free up the block range that
12816                  * we have locked up.  This holds regardless of whether this is
12817                  * an aligned write request or a read-modify-write request.
12818                  */
12819                 sd_range_unlock(un, bsp->mbs_wmp);
12820                 bsp->mbs_wmp = NULL;
12821         }
12822 
12823         if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) {
12824                 /*
12825                  * An aligned read or write command will have no shadow buf;
12826                  * there is not much else to do with it.
12827                  */
12828                 goto done;
12829         }
12830 
12831         orig_bp = bsp->mbs_orig_bp;
12832         ASSERT(orig_bp != NULL);
12833         orig_xp = SD_GET_XBUF(orig_bp);
12834         ASSERT(orig_xp != NULL);
12835         ASSERT(!mutex_owned(SD_MUTEX(un)));
12836 
12837         if (!is_write && has_wmap) {
12838                 /*
12839                  * A READ with a wmap means this is the READ phase of a
12840                  * read-modify-write. If an error occurred on the READ then
12841                  * we do not proceed with the WRITE phase or copy any data.
12842                  * Just release the write maps and return with an error.
12843                  */
12844                 if ((bp->b_resid != 0) || (bp->b_error != 0)) {
12845                         orig_bp->b_resid = orig_bp->b_bcount;
12846                         bioerror(orig_bp, bp->b_error);
12847                         sd_range_unlock(un, bsp->mbs_wmp);
12848                         goto freebuf_done;
12849                 }
12850         }
12851 
12852         /*
12853          * Here is where we set up to copy the data from the shadow buf
12854          * into the space associated with the original buf.
12855          *
12856          * To deal with the conversion between block sizes, these
12857          * computations treat the data as an array of bytes, with the
12858          * first byte (byte 0) corresponding to the first byte in the
12859          * first block on the disk.
12860          */
12861 
12862         /*
12863          * shadow_start and shadow_len indicate the location and size of
12864          * the data returned with the shadow IO request.
12865          */
12866         if (un->un_f_enable_rmw) {
12867                 shadow_start  = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
12868         } else {
12869                 shadow_start  = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
12870         }
12871         shadow_end    = shadow_start + bp->b_bcount - bp->b_resid;
12872 
12873         /*
12874          * copy_offset gives the offset (in bytes) from the start of the first
12875          * block of the READ request to the beginning of the data.  We retrieve
12876          * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved
12877          * there by sd_mapblockize_iostart(). copy_length gives the amount of
12878          * data to be copied (in bytes).
12879          */
12880         copy_offset  = bsp->mbs_copy_offset;
12881         if (un->un_f_enable_rmw) {
12882                 ASSERT((copy_offset >= 0) &&
12883                     (copy_offset < un->un_phy_blocksize));
12884         } else {
12885                 ASSERT((copy_offset >= 0) &&
12886                     (copy_offset < un->un_tgt_blocksize));
12887         }
12888 
12889         copy_length  = orig_bp->b_bcount;
12890         request_end  = shadow_start + copy_offset + orig_bp->b_bcount;
12891 
12892         /*
12893          * Set up the resid and error fields of orig_bp as appropriate.
12894          */
12895         if (shadow_end >= request_end) {
12896                 /* We got all the requested data; set resid to zero */
12897                 orig_bp->b_resid = 0;
12898         } else {
12899                 /*
12900                  * We failed to get enough data to fully satisfy the original
12901                  * request. Just copy back whatever data we got and set
12902                  * up the residual and error code as required.
12903                  *
12904                  * 'shortfall' is the amount by which the data received with the
12905                  * shadow buf has "fallen short" of the requested amount.
12906                  */
12907                 shortfall = (size_t)(request_end - shadow_end);
12908 
12909                 if (shortfall > orig_bp->b_bcount) {
12910                         /*
12911                          * We did not get enough data to even partially
12912                          * fulfill the original request.  The residual is
12913                          * equal to the amount requested.
12914                          */
12915                         orig_bp->b_resid = orig_bp->b_bcount;
12916                 } else {
12917                         /*
12918                          * We did not get all the data that we requested
12919                          * from the device, but we will try to return what
12920                          * portion we did get.
12921                          */
12922                         orig_bp->b_resid = shortfall;
12923                 }
12924                 ASSERT(copy_length >= orig_bp->b_resid);
12925                 copy_length  -= orig_bp->b_resid;
12926         }
12927 
12928         /* Propagate the error code from the shadow buf to the original buf */
12929         bioerror(orig_bp, bp->b_error);
12930 
12931         if (is_write) {
12932                 goto freebuf_done;      /* No data copying for a WRITE */
12933         }
12934 
12935         if (has_wmap) {
12936                 /*
12937                  * This is a READ command from the READ phase of a
12938                  * read-modify-write request. We have to copy the data given
12939                  * by the user OVER the data returned by the READ command,
12940                  * then convert the command from a READ to a WRITE and send
12941                  * it back to the target.
12942                  */
12943                 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset,
12944                     copy_length);
12945 
12946                 bp->b_flags &= ~((int)B_READ);   /* Convert to a WRITE */
12947 
12948                 /*
12949                  * Dispatch the WRITE command to the taskq thread, which
12950                  * will in turn send the command to the target. When the
12951                  * WRITE command completes, we (sd_mapblocksize_iodone())
12952                  * will get called again as part of the iodone chain
12953                  * processing for it. Note that we will still be dealing
12954                  * with the shadow buf at that point.
12955                  */
12956                 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp,
12957                     KM_NOSLEEP) != 0) {
12958                         /*
12959                          * Dispatch was successful so we are done. Return
12960                          * without going any higher up the iodone chain. Do
12961                          * not free up any layer-private data until after the
12962                          * WRITE completes.
12963                          */
12964                         return;
12965                 }
12966 
12967                 /*
12968                  * Dispatch of the WRITE command failed; set up the error
12969                  * condition and send this IO back up the iodone chain.
12970                  */
12971                 bioerror(orig_bp, EIO);
12972                 orig_bp->b_resid = orig_bp->b_bcount;
12973 
12974         } else {
12975                 /*
12976                  * This is a regular READ request (ie, not a RMW). Copy the
12977                  * data from the shadow buf into the original buf. The
12978                  * copy_offset compensates for any "misalignment" between the
12979                  * shadow buf (with its un->un_tgt_blocksize blocks) and the
12980                  * original buf (with its un->un_sys_blocksize blocks).
12981                  */
12982                 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr,
12983                     copy_length);
12984         }
12985 
12986 freebuf_done:
12987 
12988         /*
12989          * At this point we still have both the shadow buf AND the original
12990          * buf to deal with, as well as the layer-private data area in each.
12991          * Local variables are as follows:
12992          *
12993          * bp -- points to shadow buf
12994          * xp -- points to xbuf of shadow buf
12995          * bsp -- points to layer-private data area of shadow buf
12996          * orig_bp -- points to original buf
12997          *
12998          * First free the shadow buf and its associated xbuf, then free the
12999          * layer-private data area from the shadow buf. There is no need to
13000          * restore xb_private in the shadow xbuf.
13001          */
13002         sd_shadow_buf_free(bp);
13003         kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13004 
13005         /*
13006          * Now update the local variables to point to the original buf, xbuf,
13007          * and layer-private area.
13008          */
13009         bp = orig_bp;
13010         xp = SD_GET_XBUF(bp);
13011         ASSERT(xp != NULL);
13012         ASSERT(xp == orig_xp);
13013         bsp = xp->xb_private;
13014         ASSERT(bsp != NULL);
13015 
13016 done:
13017         /*
13018          * Restore xb_private to whatever it was set to by the next higher
13019          * layer in the chain, then free the layer-private data area.
13020          */
13021         xp->xb_private = bsp->mbs_oprivate;
13022         kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13023 
13024 exit:
13025         SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp),
13026             "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp);
13027 
13028         SD_NEXT_IODONE(index, un, bp);
13029 }
13030 
13031 
13032 /*
13033  *    Function: sd_checksum_iostart
13034  *
13035  * Description: A stub function for a layer that's currently not used.
13036  *              For now just a placeholder.
13037  *
13038  *     Context: Kernel thread context
13039  */
13040 
13041 static void
13042 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp)
13043 {
13044         ASSERT(un != NULL);
13045         ASSERT(bp != NULL);
13046         ASSERT(!mutex_owned(SD_MUTEX(un)));
13047         SD_NEXT_IOSTART(index, un, bp);
13048 }
13049 
13050 
13051 /*
13052  *    Function: sd_checksum_iodone
13053  *
13054  * Description: A stub function for a layer that's currently not used.
13055  *              For now just a placeholder.
13056  *
13057  *     Context: May be called under interrupt context
13058  */
13059 
13060 static void
13061 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp)
13062 {
13063         ASSERT(un != NULL);
13064         ASSERT(bp != NULL);
13065         ASSERT(!mutex_owned(SD_MUTEX(un)));
13066         SD_NEXT_IODONE(index, un, bp);
13067 }
13068 
13069 
13070 /*
13071  *    Function: sd_checksum_uscsi_iostart
13072  *
13073  * Description: A stub function for a layer that's currently not used.
13074  *              For now just a placeholder.
13075  *
13076  *     Context: Kernel thread context
13077  */
13078 
13079 static void
13080 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp)
13081 {
13082         ASSERT(un != NULL);
13083         ASSERT(bp != NULL);
13084         ASSERT(!mutex_owned(SD_MUTEX(un)));
13085         SD_NEXT_IOSTART(index, un, bp);
13086 }
13087 
13088 
13089 /*
13090  *    Function: sd_checksum_uscsi_iodone
13091  *
13092  * Description: A stub function for a layer that's currently not used.
13093  *              For now just a placeholder.
13094  *
13095  *     Context: May be called under interrupt context
13096  */
13097 
13098 static void
13099 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
13100 {
13101         ASSERT(un != NULL);
13102         ASSERT(bp != NULL);
13103         ASSERT(!mutex_owned(SD_MUTEX(un)));
13104         SD_NEXT_IODONE(index, un, bp);
13105 }
13106 
13107 
13108 /*
13109  *    Function: sd_pm_iostart
13110  *
13111  * Description: iostart-side routine for Power mangement.
13112  *
13113  *     Context: Kernel thread context
13114  */
13115 
13116 static void
13117 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp)
13118 {
13119         ASSERT(un != NULL);
13120         ASSERT(bp != NULL);
13121         ASSERT(!mutex_owned(SD_MUTEX(un)));
13122         ASSERT(!mutex_owned(&un->un_pm_mutex));
13123 
13124         SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n");
13125 
13126         if (sd_pm_entry(un) != DDI_SUCCESS) {
13127                 /*
13128                  * Set up to return the failed buf back up the 'iodone'
13129                  * side of the calling chain.
13130                  */
13131                 bioerror(bp, EIO);
13132                 bp->b_resid = bp->b_bcount;
13133 
13134                 SD_BEGIN_IODONE(index, un, bp);
13135 
13136                 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13137                 return;
13138         }
13139 
13140         SD_NEXT_IOSTART(index, un, bp);
13141 
13142         SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13143 }
13144 
13145 
13146 /*
13147  *    Function: sd_pm_iodone
13148  *
13149  * Description: iodone-side routine for power mangement.
13150  *
13151  *     Context: may be called from interrupt context
13152  */
13153 
13154 static void
13155 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp)
13156 {
13157         ASSERT(un != NULL);
13158         ASSERT(bp != NULL);
13159         ASSERT(!mutex_owned(&un->un_pm_mutex));
13160 
13161         SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n");
13162 
13163         /*
13164          * After attach the following flag is only read, so don't
13165          * take the penalty of acquiring a mutex for it.
13166          */
13167         if (un->un_f_pm_is_enabled == TRUE) {
13168                 sd_pm_exit(un);
13169         }
13170 
13171         SD_NEXT_IODONE(index, un, bp);
13172 
13173         SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n");
13174 }
13175 
13176 
13177 /*
13178  *    Function: sd_core_iostart
13179  *
13180  * Description: Primary driver function for enqueuing buf(9S) structs from
13181  *              the system and initiating IO to the target device
13182  *
13183  *     Context: Kernel thread context. Can sleep.
13184  *
13185  * Assumptions:  - The given xp->xb_blkno is absolute
13186  *                 (ie, relative to the start of the device).
13187  *               - The IO is to be done using the native blocksize of
13188  *                 the device, as specified in un->un_tgt_blocksize.
13189  */
13190 /* ARGSUSED */
13191 static void
13192 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp)
13193 {
13194         struct sd_xbuf *xp;
13195 
13196         ASSERT(un != NULL);
13197         ASSERT(bp != NULL);
13198         ASSERT(!mutex_owned(SD_MUTEX(un)));
13199         ASSERT(bp->b_resid == 0);
13200 
13201         SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp);
13202 
13203         xp = SD_GET_XBUF(bp);
13204         ASSERT(xp != NULL);
13205 
13206         mutex_enter(SD_MUTEX(un));
13207 
13208         /*
13209          * If we are currently in the failfast state, fail any new IO
13210          * that has B_FAILFAST set, then return.
13211          */
13212         if ((bp->b_flags & B_FAILFAST) &&
13213             (un->un_failfast_state == SD_FAILFAST_ACTIVE)) {
13214                 mutex_exit(SD_MUTEX(un));
13215                 bioerror(bp, EIO);
13216                 bp->b_resid = bp->b_bcount;
13217                 SD_BEGIN_IODONE(index, un, bp);
13218                 return;
13219         }
13220 
13221         if (SD_IS_DIRECT_PRIORITY(xp)) {
13222                 /*
13223                  * Priority command -- transport it immediately.
13224                  *
13225                  * Note: We may want to assert that USCSI_DIAGNOSE is set,
13226                  * because all direct priority commands should be associated
13227                  * with error recovery actions which we don't want to retry.
13228                  */
13229                 sd_start_cmds(un, bp);
13230         } else {
13231                 /*
13232                  * Normal command -- add it to the wait queue, then start
13233                  * transporting commands from the wait queue.
13234                  */
13235                 sd_add_buf_to_waitq(un, bp);
13236                 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
13237                 sd_start_cmds(un, NULL);
13238         }
13239 
13240         mutex_exit(SD_MUTEX(un));
13241 
13242         SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp);
13243 }
13244 
13245 
13246 /*
13247  *    Function: sd_init_cdb_limits
13248  *
13249  * Description: This is to handle scsi_pkt initialization differences
13250  *              between the driver platforms.
13251  *
13252  *              Legacy behaviors:
13253  *
13254  *              If the block number or the sector count exceeds the
13255  *              capabilities of a Group 0 command, shift over to a
13256  *              Group 1 command. We don't blindly use Group 1
13257  *              commands because a) some drives (CDC Wren IVs) get a
13258  *              bit confused, and b) there is probably a fair amount
13259  *              of speed difference for a target to receive and decode
13260  *              a 10 byte command instead of a 6 byte command.
13261  *
13262  *              The xfer time difference of 6 vs 10 byte CDBs is
13263  *              still significant so this code is still worthwhile.
13264  *              10 byte CDBs are very inefficient with the fas HBA driver
13265  *              and older disks. Each CDB byte took 1 usec with some
13266  *              popular disks.
13267  *
13268  *     Context: Must be called at attach time
13269  */
13270 
13271 static void
13272 sd_init_cdb_limits(struct sd_lun *un)
13273 {
13274         int hba_cdb_limit;
13275 
13276         /*
13277          * Use CDB_GROUP1 commands for most devices except for
13278          * parallel SCSI fixed drives in which case we get better
13279          * performance using CDB_GROUP0 commands (where applicable).
13280          */
13281         un->un_mincdb = SD_CDB_GROUP1;
13282         if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) &&
13283             !un->un_f_has_removable_media) {
13284                 un->un_mincdb = SD_CDB_GROUP0;
13285         }
13286 
13287         /*
13288          * Try to read the max-cdb-length supported by HBA.
13289          */
13290         un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1);
13291         if (0 >= un->un_max_hba_cdb) {
13292                 un->un_max_hba_cdb = CDB_GROUP4;
13293                 hba_cdb_limit = SD_CDB_GROUP4;
13294         } else if (0 < un->un_max_hba_cdb &&
13295             un->un_max_hba_cdb < CDB_GROUP1) {
13296                 hba_cdb_limit = SD_CDB_GROUP0;
13297         } else if (CDB_GROUP1 <= un->un_max_hba_cdb &&
13298             un->un_max_hba_cdb < CDB_GROUP5) {
13299                 hba_cdb_limit = SD_CDB_GROUP1;
13300         } else if (CDB_GROUP5 <= un->un_max_hba_cdb &&
13301             un->un_max_hba_cdb < CDB_GROUP4) {
13302                 hba_cdb_limit = SD_CDB_GROUP5;
13303         } else {
13304                 hba_cdb_limit = SD_CDB_GROUP4;
13305         }
13306 
13307         /*
13308          * Use CDB_GROUP5 commands for removable devices.  Use CDB_GROUP4
13309          * commands for fixed disks unless we are building for a 32 bit
13310          * kernel.
13311          */
13312 #ifdef _LP64
13313         un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13314             min(hba_cdb_limit, SD_CDB_GROUP4);
13315 #else
13316         un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13317             min(hba_cdb_limit, SD_CDB_GROUP1);
13318 #endif
13319 
13320         un->un_status_len = (int)((un->un_f_arq_enabled == TRUE)
13321             ? sizeof (struct scsi_arq_status) : 1);
13322         if (!ISCD(un))
13323                 un->un_cmd_timeout = (ushort_t)un->un_io_time;
13324         un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout;
13325 }
13326 
13327 
13328 /*
13329  *    Function: sd_initpkt_for_buf
13330  *
13331  * Description: Allocate and initialize for transport a scsi_pkt struct,
13332  *              based upon the info specified in the given buf struct.
13333  *
13334  *              Assumes the xb_blkno in the request is absolute (ie,
13335  *              relative to the start of the device (NOT partition!).
13336  *              Also assumes that the request is using the native block
13337  *              size of the device (as returned by the READ CAPACITY
13338  *              command).
13339  *
13340  * Return Code: SD_PKT_ALLOC_SUCCESS
13341  *              SD_PKT_ALLOC_FAILURE
13342  *              SD_PKT_ALLOC_FAILURE_NO_DMA
13343  *              SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13344  *
13345  *     Context: Kernel thread and may be called from software interrupt context
13346  *              as part of a sdrunout callback. This function may not block or
13347  *              call routines that block
13348  */
13349 
13350 static int
13351 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp)
13352 {
13353         struct sd_xbuf  *xp;
13354         struct scsi_pkt *pktp = NULL;
13355         struct sd_lun   *un;
13356         size_t          blockcount;
13357         daddr_t         startblock;
13358         int             rval;
13359         int             cmd_flags;
13360 
13361         ASSERT(bp != NULL);
13362         ASSERT(pktpp != NULL);
13363         xp = SD_GET_XBUF(bp);
13364         ASSERT(xp != NULL);
13365         un = SD_GET_UN(bp);
13366         ASSERT(un != NULL);
13367         ASSERT(mutex_owned(SD_MUTEX(un)));
13368         ASSERT(bp->b_resid == 0);
13369 
13370         SD_TRACE(SD_LOG_IO_CORE, un,
13371             "sd_initpkt_for_buf: entry: buf:0x%p\n", bp);
13372 
13373         mutex_exit(SD_MUTEX(un));
13374 
13375         if (xp->xb_pkt_flags & SD_XB_DMA_FREED) {
13376                 /*
13377                  * Already have a scsi_pkt -- just need DMA resources.
13378                  * We must recompute the CDB in case the mapping returns
13379                  * a nonzero pkt_resid.
13380                  * Note: if this is a portion of a PKT_DMA_PARTIAL transfer
13381                  * that is being retried, the unmap/remap of the DMA resouces
13382                  * will result in the entire transfer starting over again
13383                  * from the very first block.
13384                  */
13385                 ASSERT(xp->xb_pktp != NULL);
13386                 pktp = xp->xb_pktp;
13387         } else {
13388                 pktp = NULL;
13389         }
13390 
13391         startblock = xp->xb_blkno;   /* Absolute block num. */
13392         blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
13393 
13394         cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK);
13395 
13396         /*
13397          * sd_setup_rw_pkt will determine the appropriate CDB group to use,
13398          * call scsi_init_pkt, and build the CDB.
13399          */
13400         rval = sd_setup_rw_pkt(un, &pktp, bp,
13401             cmd_flags, sdrunout, (caddr_t)un,
13402             startblock, blockcount);
13403 
13404         if (rval == 0) {
13405                 /*
13406                  * Success.
13407                  *
13408                  * If partial DMA is being used and required for this transfer.
13409                  * set it up here.
13410                  */
13411                 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 &&
13412                     (pktp->pkt_resid != 0)) {
13413 
13414                         /*
13415                          * Save the CDB length and pkt_resid for the
13416                          * next xfer
13417                          */
13418                         xp->xb_dma_resid = pktp->pkt_resid;
13419 
13420                         /* rezero resid */
13421                         pktp->pkt_resid = 0;
13422 
13423                 } else {
13424                         xp->xb_dma_resid = 0;
13425                 }
13426 
13427                 pktp->pkt_flags = un->un_tagflags;
13428                 pktp->pkt_time  = un->un_cmd_timeout;
13429                 pktp->pkt_comp  = sdintr;
13430 
13431                 pktp->pkt_private = bp;
13432                 *pktpp = pktp;
13433 
13434                 SD_TRACE(SD_LOG_IO_CORE, un,
13435                     "sd_initpkt_for_buf: exit: buf:0x%p\n", bp);
13436 
13437                 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED;
13438 
13439                 mutex_enter(SD_MUTEX(un));
13440                 return (SD_PKT_ALLOC_SUCCESS);
13441 
13442         }
13443 
13444         /*
13445          * SD_PKT_ALLOC_FAILURE is the only expected failure code
13446          * from sd_setup_rw_pkt.
13447          */
13448         ASSERT(rval == SD_PKT_ALLOC_FAILURE);
13449 
13450         if (rval == SD_PKT_ALLOC_FAILURE) {
13451                 *pktpp = NULL;
13452                 /*
13453                  * Set the driver state to RWAIT to indicate the driver
13454                  * is waiting on resource allocations. The driver will not
13455                  * suspend, pm_suspend, or detatch while the state is RWAIT.
13456                  */
13457                 mutex_enter(SD_MUTEX(un));
13458                 New_state(un, SD_STATE_RWAIT);
13459 
13460                 SD_ERROR(SD_LOG_IO_CORE, un,
13461                     "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp);
13462 
13463                 if ((bp->b_flags & B_ERROR) != 0) {
13464                         return (SD_PKT_ALLOC_FAILURE_NO_DMA);
13465                 }
13466                 return (SD_PKT_ALLOC_FAILURE);
13467         } else {
13468                 /*
13469                  * PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13470                  *
13471                  * This should never happen.  Maybe someone messed with the
13472                  * kernel's minphys?
13473                  */
13474                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
13475                     "Request rejected: too large for CDB: "
13476                     "lba:0x%08lx  len:0x%08lx\n", startblock, blockcount);
13477                 SD_ERROR(SD_LOG_IO_CORE, un,
13478                     "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp);
13479                 mutex_enter(SD_MUTEX(un));
13480                 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13481 
13482         }
13483 }
13484 
13485 
13486 /*
13487  *    Function: sd_destroypkt_for_buf
13488  *
13489  * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing).
13490  *
13491  *     Context: Kernel thread or interrupt context
13492  */
13493 
13494 static void
13495 sd_destroypkt_for_buf(struct buf *bp)
13496 {
13497         ASSERT(bp != NULL);
13498         ASSERT(SD_GET_UN(bp) != NULL);
13499 
13500         SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13501             "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp);
13502 
13503         ASSERT(SD_GET_PKTP(bp) != NULL);
13504         scsi_destroy_pkt(SD_GET_PKTP(bp));
13505 
13506         SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13507             "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp);
13508 }
13509 
13510 /*
13511  *    Function: sd_setup_rw_pkt
13512  *
13513  * Description: Determines appropriate CDB group for the requested LBA
13514  *              and transfer length, calls scsi_init_pkt, and builds
13515  *              the CDB.  Do not use for partial DMA transfers except
13516  *              for the initial transfer since the CDB size must
13517  *              remain constant.
13518  *
13519  *     Context: Kernel thread and may be called from software interrupt
13520  *              context as part of a sdrunout callback. This function may not
13521  *              block or call routines that block
13522  */
13523 
13524 
13525 int
13526 sd_setup_rw_pkt(struct sd_lun *un,
13527     struct scsi_pkt **pktpp, struct buf *bp, int flags,
13528     int (*callback)(caddr_t), caddr_t callback_arg,
13529     diskaddr_t lba, uint32_t blockcount)
13530 {
13531         struct scsi_pkt *return_pktp;
13532         union scsi_cdb *cdbp;
13533         struct sd_cdbinfo *cp = NULL;
13534         int i;
13535 
13536         /*
13537          * See which size CDB to use, based upon the request.
13538          */
13539         for (i = un->un_mincdb; i <= un->un_maxcdb; i++) {
13540 
13541                 /*
13542                  * Check lba and block count against sd_cdbtab limits.
13543                  * In the partial DMA case, we have to use the same size
13544                  * CDB for all the transfers.  Check lba + blockcount
13545                  * against the max LBA so we know that segment of the
13546                  * transfer can use the CDB we select.
13547                  */
13548                 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) &&
13549                     (blockcount <= sd_cdbtab[i].sc_maxlen)) {
13550 
13551                         /*
13552                          * The command will fit into the CDB type
13553                          * specified by sd_cdbtab[i].
13554                          */
13555                         cp = sd_cdbtab + i;
13556 
13557                         /*
13558                          * Call scsi_init_pkt so we can fill in the
13559                          * CDB.
13560                          */
13561                         return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp,
13562                             bp, cp->sc_grpcode, un->un_status_len, 0,
13563                             flags, callback, callback_arg);
13564 
13565                         if (return_pktp != NULL) {
13566 
13567                                 /*
13568                                  * Return new value of pkt
13569                                  */
13570                                 *pktpp = return_pktp;
13571 
13572                                 /*
13573                                  * To be safe, zero the CDB insuring there is
13574                                  * no leftover data from a previous command.
13575                                  */
13576                                 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode);
13577 
13578                                 /*
13579                                  * Handle partial DMA mapping
13580                                  */
13581                                 if (return_pktp->pkt_resid != 0) {
13582 
13583                                         /*
13584                                          * Not going to xfer as many blocks as
13585                                          * originally expected
13586                                          */
13587                                         blockcount -=
13588                                             SD_BYTES2TGTBLOCKS(un,
13589                                             return_pktp->pkt_resid);
13590                                 }
13591 
13592                                 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp;
13593 
13594                                 /*
13595                                  * Set command byte based on the CDB
13596                                  * type we matched.
13597                                  */
13598                                 cdbp->scc_cmd = cp->sc_grpmask |
13599                                     ((bp->b_flags & B_READ) ?
13600                                     SCMD_READ : SCMD_WRITE);
13601 
13602                                 SD_FILL_SCSI1_LUN(un, return_pktp);
13603 
13604                                 /*
13605                                  * Fill in LBA and length
13606                                  */
13607                                 ASSERT((cp->sc_grpcode == CDB_GROUP1) ||
13608                                     (cp->sc_grpcode == CDB_GROUP4) ||
13609                                     (cp->sc_grpcode == CDB_GROUP0) ||
13610                                     (cp->sc_grpcode == CDB_GROUP5));
13611 
13612                                 if (cp->sc_grpcode == CDB_GROUP1) {
13613                                         FORMG1ADDR(cdbp, lba);
13614                                         FORMG1COUNT(cdbp, blockcount);
13615                                         return (0);
13616                                 } else if (cp->sc_grpcode == CDB_GROUP4) {
13617                                         FORMG4LONGADDR(cdbp, lba);
13618                                         FORMG4COUNT(cdbp, blockcount);
13619                                         return (0);
13620                                 } else if (cp->sc_grpcode == CDB_GROUP0) {
13621                                         FORMG0ADDR(cdbp, lba);
13622                                         FORMG0COUNT(cdbp, blockcount);
13623                                         return (0);
13624                                 } else if (cp->sc_grpcode == CDB_GROUP5) {
13625                                         FORMG5ADDR(cdbp, lba);
13626                                         FORMG5COUNT(cdbp, blockcount);
13627                                         return (0);
13628                                 }
13629 
13630                                 /*
13631                                  * It should be impossible to not match one
13632                                  * of the CDB types above, so we should never
13633                                  * reach this point.  Set the CDB command byte
13634                                  * to test-unit-ready to avoid writing
13635                                  * to somewhere we don't intend.
13636                                  */
13637                                 cdbp->scc_cmd = SCMD_TEST_UNIT_READY;
13638                                 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13639                         } else {
13640                                 /*
13641                                  * Couldn't get scsi_pkt
13642                                  */
13643                                 return (SD_PKT_ALLOC_FAILURE);
13644                         }
13645                 }
13646         }
13647 
13648         /*
13649          * None of the available CDB types were suitable.  This really
13650          * should never happen:  on a 64 bit system we support
13651          * READ16/WRITE16 which will hold an entire 64 bit disk address
13652          * and on a 32 bit system we will refuse to bind to a device
13653          * larger than 2TB so addresses will never be larger than 32 bits.
13654          */
13655         return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13656 }
13657 
13658 /*
13659  *    Function: sd_setup_next_rw_pkt
13660  *
13661  * Description: Setup packet for partial DMA transfers, except for the
13662  *              initial transfer.  sd_setup_rw_pkt should be used for
13663  *              the initial transfer.
13664  *
13665  *     Context: Kernel thread and may be called from interrupt context.
13666  */
13667 
13668 int
13669 sd_setup_next_rw_pkt(struct sd_lun *un,
13670     struct scsi_pkt *pktp, struct buf *bp,
13671     diskaddr_t lba, uint32_t blockcount)
13672 {
13673         uchar_t com;
13674         union scsi_cdb *cdbp;
13675         uchar_t cdb_group_id;
13676 
13677         ASSERT(pktp != NULL);
13678         ASSERT(pktp->pkt_cdbp != NULL);
13679 
13680         cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
13681         com = cdbp->scc_cmd;
13682         cdb_group_id = CDB_GROUPID(com);
13683 
13684         ASSERT((cdb_group_id == CDB_GROUPID_0) ||
13685             (cdb_group_id == CDB_GROUPID_1) ||
13686             (cdb_group_id == CDB_GROUPID_4) ||
13687             (cdb_group_id == CDB_GROUPID_5));
13688 
13689         /*
13690          * Move pkt to the next portion of the xfer.
13691          * func is NULL_FUNC so we do not have to release
13692          * the disk mutex here.
13693          */
13694         if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0,
13695             NULL_FUNC, NULL) == pktp) {
13696                 /* Success.  Handle partial DMA */
13697                 if (pktp->pkt_resid != 0) {
13698                         blockcount -=
13699                             SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid);
13700                 }
13701 
13702                 cdbp->scc_cmd = com;
13703                 SD_FILL_SCSI1_LUN(un, pktp);
13704                 if (cdb_group_id == CDB_GROUPID_1) {
13705                         FORMG1ADDR(cdbp, lba);
13706                         FORMG1COUNT(cdbp, blockcount);
13707                         return (0);
13708                 } else if (cdb_group_id == CDB_GROUPID_4) {
13709                         FORMG4LONGADDR(cdbp, lba);
13710                         FORMG4COUNT(cdbp, blockcount);
13711                         return (0);
13712                 } else if (cdb_group_id == CDB_GROUPID_0) {
13713                         FORMG0ADDR(cdbp, lba);
13714                         FORMG0COUNT(cdbp, blockcount);
13715                         return (0);
13716                 } else if (cdb_group_id == CDB_GROUPID_5) {
13717                         FORMG5ADDR(cdbp, lba);
13718                         FORMG5COUNT(cdbp, blockcount);
13719                         return (0);
13720                 }
13721 
13722                 /* Unreachable */
13723                 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13724         }
13725 
13726         /*
13727          * Error setting up next portion of cmd transfer.
13728          * Something is definitely very wrong and this
13729          * should not happen.
13730          */
13731         return (SD_PKT_ALLOC_FAILURE);
13732 }
13733 
13734 /*
13735  *    Function: sd_initpkt_for_uscsi
13736  *
13737  * Description: Allocate and initialize for transport a scsi_pkt struct,
13738  *              based upon the info specified in the given uscsi_cmd struct.
13739  *
13740  * Return Code: SD_PKT_ALLOC_SUCCESS
13741  *              SD_PKT_ALLOC_FAILURE
13742  *              SD_PKT_ALLOC_FAILURE_NO_DMA
13743  *              SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13744  *
13745  *     Context: Kernel thread and may be called from software interrupt context
13746  *              as part of a sdrunout callback. This function may not block or
13747  *              call routines that block
13748  */
13749 
13750 static int
13751 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp)
13752 {
13753         struct uscsi_cmd *uscmd;
13754         struct sd_xbuf  *xp;
13755         struct scsi_pkt *pktp;
13756         struct sd_lun   *un;
13757         uint32_t        flags = 0;
13758 
13759         ASSERT(bp != NULL);
13760         ASSERT(pktpp != NULL);
13761         xp = SD_GET_XBUF(bp);
13762         ASSERT(xp != NULL);
13763         un = SD_GET_UN(bp);
13764         ASSERT(un != NULL);
13765         ASSERT(mutex_owned(SD_MUTEX(un)));
13766 
13767         /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
13768         uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
13769         ASSERT(uscmd != NULL);
13770 
13771         SD_TRACE(SD_LOG_IO_CORE, un,
13772             "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp);
13773 
13774         /*
13775          * Allocate the scsi_pkt for the command.
13776          * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path
13777          *       during scsi_init_pkt time and will continue to use the
13778          *       same path as long as the same scsi_pkt is used without
13779          *       intervening scsi_dma_free(). Since uscsi command does
13780          *       not call scsi_dmafree() before retry failed command, it
13781          *       is necessary to make sure PKT_DMA_PARTIAL flag is NOT
13782          *       set such that scsi_vhci can use other available path for
13783          *       retry. Besides, ucsci command does not allow DMA breakup,
13784          *       so there is no need to set PKT_DMA_PARTIAL flag.
13785          */
13786         if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
13787                 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
13788                     ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
13789                     ((int)(uscmd->uscsi_rqlen) + sizeof (struct scsi_arq_status)
13790                     - sizeof (struct scsi_extended_sense)), 0,
13791                     (un->un_pkt_flags & ~PKT_DMA_PARTIAL) | PKT_XARQ,
13792                     sdrunout, (caddr_t)un);
13793         } else {
13794                 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
13795                     ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
13796                     sizeof (struct scsi_arq_status), 0,
13797                     (un->un_pkt_flags & ~PKT_DMA_PARTIAL),
13798                     sdrunout, (caddr_t)un);
13799         }
13800 
13801         if (pktp == NULL) {
13802                 *pktpp = NULL;
13803                 /*
13804                  * Set the driver state to RWAIT to indicate the driver
13805                  * is waiting on resource allocations. The driver will not
13806                  * suspend, pm_suspend, or detatch while the state is RWAIT.
13807                  */
13808                 New_state(un, SD_STATE_RWAIT);
13809 
13810                 SD_ERROR(SD_LOG_IO_CORE, un,
13811                     "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp);
13812 
13813                 if ((bp->b_flags & B_ERROR) != 0) {
13814                         return (SD_PKT_ALLOC_FAILURE_NO_DMA);
13815                 }
13816                 return (SD_PKT_ALLOC_FAILURE);
13817         }
13818 
13819         /*
13820          * We do not do DMA breakup for USCSI commands, so return failure
13821          * here if all the needed DMA resources were not allocated.
13822          */
13823         if ((un->un_pkt_flags & PKT_DMA_PARTIAL) &&
13824             (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) {
13825                 scsi_destroy_pkt(pktp);
13826                 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: "
13827                     "No partial DMA for USCSI. exit: buf:0x%p\n", bp);
13828                 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL);
13829         }
13830 
13831         /* Init the cdb from the given uscsi struct */
13832         (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp,
13833             uscmd->uscsi_cdb[0], 0, 0, 0);
13834 
13835         SD_FILL_SCSI1_LUN(un, pktp);
13836 
13837         /*
13838          * Set up the optional USCSI flags. See the uscsi (7I) man page
13839          * for listing of the supported flags.
13840          */
13841 
13842         if (uscmd->uscsi_flags & USCSI_SILENT) {
13843                 flags |= FLAG_SILENT;
13844         }
13845 
13846         if (uscmd->uscsi_flags & USCSI_DIAGNOSE) {
13847                 flags |= FLAG_DIAGNOSE;
13848         }
13849 
13850         if (uscmd->uscsi_flags & USCSI_ISOLATE) {
13851                 flags |= FLAG_ISOLATE;
13852         }
13853 
13854         if (un->un_f_is_fibre == FALSE) {
13855                 if (uscmd->uscsi_flags & USCSI_RENEGOT) {
13856                         flags |= FLAG_RENEGOTIATE_WIDE_SYNC;
13857                 }
13858         }
13859 
13860         /*
13861          * Set the pkt flags here so we save time later.
13862          * Note: These flags are NOT in the uscsi man page!!!
13863          */
13864         if (uscmd->uscsi_flags & USCSI_HEAD) {
13865                 flags |= FLAG_HEAD;
13866         }
13867 
13868         if (uscmd->uscsi_flags & USCSI_NOINTR) {
13869                 flags |= FLAG_NOINTR;
13870         }
13871 
13872         /*
13873          * For tagged queueing, things get a bit complicated.
13874          * Check first for head of queue and last for ordered queue.
13875          * If neither head nor order, use the default driver tag flags.
13876          */
13877         if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) {
13878                 if (uscmd->uscsi_flags & USCSI_HTAG) {
13879                         flags |= FLAG_HTAG;
13880                 } else if (uscmd->uscsi_flags & USCSI_OTAG) {
13881                         flags |= FLAG_OTAG;
13882                 } else {
13883                         flags |= un->un_tagflags & FLAG_TAGMASK;
13884                 }
13885         }
13886 
13887         if (uscmd->uscsi_flags & USCSI_NODISCON) {
13888                 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON;
13889         }
13890 
13891         pktp->pkt_flags = flags;
13892 
13893         /* Transfer uscsi information to scsi_pkt */
13894         (void) scsi_uscsi_pktinit(uscmd, pktp);
13895 
13896         /* Copy the caller's CDB into the pkt... */
13897         bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen);
13898 
13899         if (uscmd->uscsi_timeout == 0) {
13900                 pktp->pkt_time = un->un_uscsi_timeout;
13901         } else {
13902                 pktp->pkt_time = uscmd->uscsi_timeout;
13903         }
13904 
13905         /* need it later to identify USCSI request in sdintr */
13906         xp->xb_pkt_flags |= SD_XB_USCSICMD;
13907 
13908         xp->xb_sense_resid = uscmd->uscsi_rqresid;
13909 
13910         pktp->pkt_private = bp;
13911         pktp->pkt_comp = sdintr;
13912         *pktpp = pktp;
13913 
13914         SD_TRACE(SD_LOG_IO_CORE, un,
13915             "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp);
13916 
13917         return (SD_PKT_ALLOC_SUCCESS);
13918 }
13919 
13920 
13921 /*
13922  *    Function: sd_destroypkt_for_uscsi
13923  *
13924  * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi
13925  *              IOs.. Also saves relevant info into the associated uscsi_cmd
13926  *              struct.
13927  *
13928  *     Context: May be called under interrupt context
13929  */
13930 
13931 static void
13932 sd_destroypkt_for_uscsi(struct buf *bp)
13933 {
13934         struct uscsi_cmd *uscmd;
13935         struct sd_xbuf  *xp;
13936         struct scsi_pkt *pktp;
13937         struct sd_lun   *un;
13938         struct sd_uscsi_info *suip;
13939 
13940         ASSERT(bp != NULL);
13941         xp = SD_GET_XBUF(bp);
13942         ASSERT(xp != NULL);
13943         un = SD_GET_UN(bp);
13944         ASSERT(un != NULL);
13945         ASSERT(!mutex_owned(SD_MUTEX(un)));
13946         pktp = SD_GET_PKTP(bp);
13947         ASSERT(pktp != NULL);
13948 
13949         SD_TRACE(SD_LOG_IO_CORE, un,
13950             "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp);
13951 
13952         /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
13953         uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
13954         ASSERT(uscmd != NULL);
13955 
13956         /* Save the status and the residual into the uscsi_cmd struct */
13957         uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
13958         uscmd->uscsi_resid  = bp->b_resid;
13959 
13960         /* Transfer scsi_pkt information to uscsi */
13961         (void) scsi_uscsi_pktfini(pktp, uscmd);
13962 
13963         /*
13964          * If enabled, copy any saved sense data into the area specified
13965          * by the uscsi command.
13966          */
13967         if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
13968             (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
13969                 /*
13970                  * Note: uscmd->uscsi_rqbuf should always point to a buffer
13971                  * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd())
13972                  */
13973                 uscmd->uscsi_rqstatus = xp->xb_sense_status;
13974                 uscmd->uscsi_rqresid  = xp->xb_sense_resid;
13975                 if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
13976                         bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
13977                             MAX_SENSE_LENGTH);
13978                 } else {
13979                         bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
13980                             SENSE_LENGTH);
13981                 }
13982         }
13983         /*
13984          * The following assignments are for SCSI FMA.
13985          */
13986         ASSERT(xp->xb_private != NULL);
13987         suip = (struct sd_uscsi_info *)xp->xb_private;
13988         suip->ui_pkt_reason = pktp->pkt_reason;
13989         suip->ui_pkt_state = pktp->pkt_state;
13990         suip->ui_pkt_statistics = pktp->pkt_statistics;
13991         suip->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
13992 
13993         /* We are done with the scsi_pkt; free it now */
13994         ASSERT(SD_GET_PKTP(bp) != NULL);
13995         scsi_destroy_pkt(SD_GET_PKTP(bp));
13996 
13997         SD_TRACE(SD_LOG_IO_CORE, un,
13998             "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp);
13999 }
14000 
14001 
14002 /*
14003  *    Function: sd_bioclone_alloc
14004  *
14005  * Description: Allocate a buf(9S) and init it as per the given buf
14006  *              and the various arguments.  The associated sd_xbuf
14007  *              struct is (nearly) duplicated.  The struct buf *bp
14008  *              argument is saved in new_xp->xb_private.
14009  *
14010  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
14011  *              datalen - size of data area for the shadow bp
14012  *              blkno - starting LBA
14013  *              func - function pointer for b_iodone in the shadow buf. (May
14014  *                      be NULL if none.)
14015  *
14016  * Return Code: Pointer to allocates buf(9S) struct
14017  *
14018  *     Context: Can sleep.
14019  */
14020 
14021 static struct buf *
14022 sd_bioclone_alloc(struct buf *bp, size_t datalen, daddr_t blkno,
14023     int (*func)(struct buf *))
14024 {
14025         struct  sd_lun  *un;
14026         struct  sd_xbuf *xp;
14027         struct  sd_xbuf *new_xp;
14028         struct  buf     *new_bp;
14029 
14030         ASSERT(bp != NULL);
14031         xp = SD_GET_XBUF(bp);
14032         ASSERT(xp != NULL);
14033         un = SD_GET_UN(bp);
14034         ASSERT(un != NULL);
14035         ASSERT(!mutex_owned(SD_MUTEX(un)));
14036 
14037         new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func,
14038             NULL, KM_SLEEP);
14039 
14040         new_bp->b_lblkno     = blkno;
14041 
14042         /*
14043          * Allocate an xbuf for the shadow bp and copy the contents of the
14044          * original xbuf into it.
14045          */
14046         new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14047         bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14048 
14049         /*
14050          * The given bp is automatically saved in the xb_private member
14051          * of the new xbuf.  Callers are allowed to depend on this.
14052          */
14053         new_xp->xb_private = bp;
14054 
14055         new_bp->b_private  = new_xp;
14056 
14057         return (new_bp);
14058 }
14059 
14060 /*
14061  *    Function: sd_shadow_buf_alloc
14062  *
14063  * Description: Allocate a buf(9S) and init it as per the given buf
14064  *              and the various arguments.  The associated sd_xbuf
14065  *              struct is (nearly) duplicated.  The struct buf *bp
14066  *              argument is saved in new_xp->xb_private.
14067  *
14068  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
14069  *              datalen - size of data area for the shadow bp
14070  *              bflags - B_READ or B_WRITE (pseudo flag)
14071  *              blkno - starting LBA
14072  *              func - function pointer for b_iodone in the shadow buf. (May
14073  *                      be NULL if none.)
14074  *
14075  * Return Code: Pointer to allocates buf(9S) struct
14076  *
14077  *     Context: Can sleep.
14078  */
14079 
14080 static struct buf *
14081 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags,
14082     daddr_t blkno, int (*func)(struct buf *))
14083 {
14084         struct  sd_lun  *un;
14085         struct  sd_xbuf *xp;
14086         struct  sd_xbuf *new_xp;
14087         struct  buf     *new_bp;
14088 
14089         ASSERT(bp != NULL);
14090         xp = SD_GET_XBUF(bp);
14091         ASSERT(xp != NULL);
14092         un = SD_GET_UN(bp);
14093         ASSERT(un != NULL);
14094         ASSERT(!mutex_owned(SD_MUTEX(un)));
14095 
14096         if (bp->b_flags & (B_PAGEIO | B_PHYS)) {
14097                 bp_mapin(bp);
14098         }
14099 
14100         bflags &= (B_READ | B_WRITE);
14101         new_bp = getrbuf(KM_SLEEP);
14102         new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP);
14103         new_bp->b_bcount = datalen;
14104         new_bp->b_flags = bflags |
14105             (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW));
14106         new_bp->av_forw      = NULL;
14107         new_bp->av_back      = NULL;
14108         new_bp->b_dev        = bp->b_dev;
14109         new_bp->b_blkno      = blkno;
14110         new_bp->b_iodone = func;
14111         new_bp->b_edev       = bp->b_edev;
14112         new_bp->b_resid      = 0;
14113 
14114         /* We need to preserve the B_FAILFAST flag */
14115         if (bp->b_flags & B_FAILFAST) {
14116                 new_bp->b_flags |= B_FAILFAST;
14117         }
14118 
14119         /*
14120          * Allocate an xbuf for the shadow bp and copy the contents of the
14121          * original xbuf into it.
14122          */
14123         new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14124         bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14125 
14126         /* Need later to copy data between the shadow buf & original buf! */
14127         new_xp->xb_pkt_flags |= PKT_CONSISTENT;
14128 
14129         /*
14130          * The given bp is automatically saved in the xb_private member
14131          * of the new xbuf.  Callers are allowed to depend on this.
14132          */
14133         new_xp->xb_private = bp;
14134 
14135         new_bp->b_private  = new_xp;
14136 
14137         return (new_bp);
14138 }
14139 
14140 /*
14141  *    Function: sd_bioclone_free
14142  *
14143  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations
14144  *              in the larger than partition operation.
14145  *
14146  *     Context: May be called under interrupt context
14147  */
14148 
14149 static void
14150 sd_bioclone_free(struct buf *bp)
14151 {
14152         struct sd_xbuf  *xp;
14153 
14154         ASSERT(bp != NULL);
14155         xp = SD_GET_XBUF(bp);
14156         ASSERT(xp != NULL);
14157 
14158         /*
14159          * Call bp_mapout() before freeing the buf,  in case a lower
14160          * layer or HBA  had done a bp_mapin().  we must do this here
14161          * as we are the "originator" of the shadow buf.
14162          */
14163         bp_mapout(bp);
14164 
14165         /*
14166          * Null out b_iodone before freeing the bp, to ensure that the driver
14167          * never gets confused by a stale value in this field. (Just a little
14168          * extra defensiveness here.)
14169          */
14170         bp->b_iodone = NULL;
14171 
14172         freerbuf(bp);
14173 
14174         kmem_free(xp, sizeof (struct sd_xbuf));
14175 }
14176 
14177 /*
14178  *    Function: sd_shadow_buf_free
14179  *
14180  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations.
14181  *
14182  *     Context: May be called under interrupt context
14183  */
14184 
14185 static void
14186 sd_shadow_buf_free(struct buf *bp)
14187 {
14188         struct sd_xbuf  *xp;
14189 
14190         ASSERT(bp != NULL);
14191         xp = SD_GET_XBUF(bp);
14192         ASSERT(xp != NULL);
14193 
14194         /*
14195          * Null out b_iodone before freeing the bp, to ensure that the driver
14196          * never gets confused by a stale value in this field. (Just a little
14197          * extra defensiveness here.)
14198          */
14199         bp->b_iodone = NULL;
14200 
14201         kmem_free(bp->b_un.b_addr, bp->b_bcount);
14202         freerbuf(bp);
14203 
14204         kmem_free(xp, sizeof (struct sd_xbuf));
14205 }
14206 
14207 
14208 /*
14209  *    Function: sd_print_transport_rejected_message
14210  *
14211  * Description: This implements the ludicrously complex rules for printing
14212  *              a "transport rejected" message.  This is to address the
14213  *              specific problem of having a flood of this error message
14214  *              produced when a failover occurs.
14215  *
14216  *     Context: Any.
14217  */
14218 
14219 static void
14220 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp,
14221     int code)
14222 {
14223         ASSERT(un != NULL);
14224         ASSERT(mutex_owned(SD_MUTEX(un)));
14225         ASSERT(xp != NULL);
14226 
14227         /*
14228          * Print the "transport rejected" message under the following
14229          * conditions:
14230          *
14231          * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set
14232          * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR.
14233          * - If the error code IS a TRAN_FATAL_ERROR, then the message is
14234          *   printed the FIRST time a TRAN_FATAL_ERROR is returned from
14235          *   scsi_transport(9F) (which indicates that the target might have
14236          *   gone off-line).  This uses the un->un_tran_fatal_count
14237          *   count, which is incremented whenever a TRAN_FATAL_ERROR is
14238          *   received, and reset to zero whenver a TRAN_ACCEPT is returned
14239          *   from scsi_transport().
14240          *
14241          * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of
14242          * the preceeding cases in order for the message to be printed.
14243          */
14244         if (((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) &&
14245             (SD_FM_LOG(un) == SD_FM_LOG_NSUP)) {
14246                 if ((sd_level_mask & SD_LOGMASK_DIAG) ||
14247                     (code != TRAN_FATAL_ERROR) ||
14248                     (un->un_tran_fatal_count == 1)) {
14249                         switch (code) {
14250                         case TRAN_BADPKT:
14251                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14252                                     "transport rejected bad packet\n");
14253                                 break;
14254                         case TRAN_FATAL_ERROR:
14255                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14256                                     "transport rejected fatal error\n");
14257                                 break;
14258                         default:
14259                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14260                                     "transport rejected (%d)\n", code);
14261                                 break;
14262                         }
14263                 }
14264         }
14265 }
14266 
14267 
14268 /*
14269  *    Function: sd_add_buf_to_waitq
14270  *
14271  * Description: Add the given buf(9S) struct to the wait queue for the
14272  *              instance.  If sorting is enabled, then the buf is added
14273  *              to the queue via an elevator sort algorithm (a la
14274  *              disksort(9F)).  The SD_GET_BLKNO(bp) is used as the sort key.
14275  *              If sorting is not enabled, then the buf is just added
14276  *              to the end of the wait queue.
14277  *
14278  * Return Code: void
14279  *
14280  *     Context: Does not sleep/block, therefore technically can be called
14281  *              from any context.  However if sorting is enabled then the
14282  *              execution time is indeterminate, and may take long if
14283  *              the wait queue grows large.
14284  */
14285 
14286 static void
14287 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp)
14288 {
14289         struct buf *ap;
14290 
14291         ASSERT(bp != NULL);
14292         ASSERT(un != NULL);
14293         ASSERT(mutex_owned(SD_MUTEX(un)));
14294 
14295         /* If the queue is empty, add the buf as the only entry & return. */
14296         if (un->un_waitq_headp == NULL) {
14297                 ASSERT(un->un_waitq_tailp == NULL);
14298                 un->un_waitq_headp = un->un_waitq_tailp = bp;
14299                 bp->av_forw = NULL;
14300                 return;
14301         }
14302 
14303         ASSERT(un->un_waitq_tailp != NULL);
14304 
14305         /*
14306          * If sorting is disabled, just add the buf to the tail end of
14307          * the wait queue and return.
14308          */
14309         if (un->un_f_disksort_disabled || un->un_f_enable_rmw) {
14310                 un->un_waitq_tailp->av_forw = bp;
14311                 un->un_waitq_tailp = bp;
14312                 bp->av_forw = NULL;
14313                 return;
14314         }
14315 
14316         /*
14317          * Sort thru the list of requests currently on the wait queue
14318          * and add the new buf request at the appropriate position.
14319          *
14320          * The un->un_waitq_headp is an activity chain pointer on which
14321          * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The
14322          * first queue holds those requests which are positioned after
14323          * the current SD_GET_BLKNO() (in the first request); the second holds
14324          * requests which came in after their SD_GET_BLKNO() number was passed.
14325          * Thus we implement a one way scan, retracting after reaching
14326          * the end of the drive to the first request on the second
14327          * queue, at which time it becomes the first queue.
14328          * A one-way scan is natural because of the way UNIX read-ahead
14329          * blocks are allocated.
14330          *
14331          * If we lie after the first request, then we must locate the
14332          * second request list and add ourselves to it.
14333          */
14334         ap = un->un_waitq_headp;
14335         if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) {
14336                 while (ap->av_forw != NULL) {
14337                         /*
14338                          * Look for an "inversion" in the (normally
14339                          * ascending) block numbers. This indicates
14340                          * the start of the second request list.
14341                          */
14342                         if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) {
14343                                 /*
14344                                  * Search the second request list for the
14345                                  * first request at a larger block number.
14346                                  * We go before that; however if there is
14347                                  * no such request, we go at the end.
14348                                  */
14349                                 do {
14350                                         if (SD_GET_BLKNO(bp) <
14351                                             SD_GET_BLKNO(ap->av_forw)) {
14352                                                 goto insert;
14353                                         }
14354                                         ap = ap->av_forw;
14355                                 } while (ap->av_forw != NULL);
14356                                 goto insert;            /* after last */
14357                         }
14358                         ap = ap->av_forw;
14359                 }
14360 
14361                 /*
14362                  * No inversions... we will go after the last, and
14363                  * be the first request in the second request list.
14364                  */
14365                 goto insert;
14366         }
14367 
14368         /*
14369          * Request is at/after the current request...
14370          * sort in the first request list.
14371          */
14372         while (ap->av_forw != NULL) {
14373                 /*
14374                  * We want to go after the current request (1) if
14375                  * there is an inversion after it (i.e. it is the end
14376                  * of the first request list), or (2) if the next
14377                  * request is a larger block no. than our request.
14378                  */
14379                 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) ||
14380                     (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) {
14381                         goto insert;
14382                 }
14383                 ap = ap->av_forw;
14384         }
14385 
14386         /*
14387          * Neither a second list nor a larger request, therefore
14388          * we go at the end of the first list (which is the same
14389          * as the end of the whole schebang).
14390          */
14391 insert:
14392         bp->av_forw = ap->av_forw;
14393         ap->av_forw = bp;
14394 
14395         /*
14396          * If we inserted onto the tail end of the waitq, make sure the
14397          * tail pointer is updated.
14398          */
14399         if (ap == un->un_waitq_tailp) {
14400                 un->un_waitq_tailp = bp;
14401         }
14402 }
14403 
14404 
14405 /*
14406  *    Function: sd_start_cmds
14407  *
14408  * Description: Remove and transport cmds from the driver queues.
14409  *
14410  *   Arguments: un - pointer to the unit (soft state) struct for the target.
14411  *
14412  *              immed_bp - ptr to a buf to be transported immediately. Only
14413  *              the immed_bp is transported; bufs on the waitq are not
14414  *              processed and the un_retry_bp is not checked.  If immed_bp is
14415  *              NULL, then normal queue processing is performed.
14416  *
14417  *     Context: May be called from kernel thread context, interrupt context,
14418  *              or runout callback context. This function may not block or
14419  *              call routines that block.
14420  */
14421 
14422 static void
14423 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp)
14424 {
14425         struct  sd_xbuf *xp;
14426         struct  buf     *bp;
14427         void    (*statp)(kstat_io_t *);
14428         void    (*saved_statp)(kstat_io_t *);
14429         int     rval;
14430         struct sd_fm_internal *sfip = NULL;
14431 
14432         ASSERT(un != NULL);
14433         ASSERT(mutex_owned(SD_MUTEX(un)));
14434         ASSERT(un->un_ncmds_in_transport >= 0);
14435         ASSERT(un->un_throttle >= 0);
14436 
14437         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n");
14438 
14439         /*
14440          * If device is currently retired, we should abort all pending I/O.
14441          */
14442         if (DEVI(un->un_sd->sd_dev)->devi_flags & DEVI_RETIRED) {
14443                 if (immed_bp) {
14444                         immed_bp->b_resid = immed_bp->b_bcount;
14445                         bioerror(immed_bp, ENXIO);
14446                         biodone(immed_bp);
14447                 }
14448                 /* abort in-flight IO */
14449                 (void) scsi_abort(SD_ADDRESS(un), NULL);
14450                 /* abort pending IO */
14451                 un->un_failfast_state = SD_FAILFAST_ACTIVE;
14452                 un->un_failfast_bp = NULL;
14453                 sd_failfast_flushq(un, B_TRUE);
14454                 return;
14455         }
14456 
14457         do {
14458                 saved_statp = NULL;
14459 
14460                 /*
14461                  * If we are syncing or dumping, fail the command to
14462                  * avoid recursively calling back into scsi_transport().
14463                  * The dump I/O itself uses a separate code path so this
14464                  * only prevents non-dump I/O from being sent while dumping.
14465                  * File system sync takes place before dumping begins.
14466                  * During panic, filesystem I/O is allowed provided
14467                  * un_in_callback is <= 1.  This is to prevent recursion
14468                  * such as sd_start_cmds -> scsi_transport -> sdintr ->
14469                  * sd_start_cmds and so on.  See panic.c for more information
14470                  * about the states the system can be in during panic.
14471                  */
14472                 if ((un->un_state == SD_STATE_DUMPING) ||
14473                     (ddi_in_panic() && (un->un_in_callback > 1))) {
14474                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14475                             "sd_start_cmds: panicking\n");
14476                         goto exit;
14477                 }
14478 
14479                 if ((bp = immed_bp) != NULL) {
14480                         /*
14481                          * We have a bp that must be transported immediately.
14482                          * It's OK to transport the immed_bp here without doing
14483                          * the throttle limit check because the immed_bp is
14484                          * always used in a retry/recovery case. This means
14485                          * that we know we are not at the throttle limit by
14486                          * virtue of the fact that to get here we must have
14487                          * already gotten a command back via sdintr(). This also
14488                          * relies on (1) the command on un_retry_bp preventing
14489                          * further commands from the waitq from being issued;
14490                          * and (2) the code in sd_retry_command checking the
14491                          * throttle limit before issuing a delayed or immediate
14492                          * retry. This holds even if the throttle limit is
14493                          * currently ratcheted down from its maximum value.
14494                          */
14495                         statp = kstat_runq_enter;
14496                         if (bp == un->un_retry_bp) {
14497                                 ASSERT((un->un_retry_statp == NULL) ||
14498                                     (un->un_retry_statp == kstat_waitq_enter) ||
14499                                     (un->un_retry_statp ==
14500                                     kstat_runq_back_to_waitq));
14501                                 /*
14502                                  * If the waitq kstat was incremented when
14503                                  * sd_set_retry_bp() queued this bp for a retry,
14504                                  * then we must set up statp so that the waitq
14505                                  * count will get decremented correctly below.
14506                                  * Also we must clear un->un_retry_statp to
14507                                  * ensure that we do not act on a stale value
14508                                  * in this field.
14509                                  */
14510                                 if ((un->un_retry_statp == kstat_waitq_enter) ||
14511                                     (un->un_retry_statp ==
14512                                     kstat_runq_back_to_waitq)) {
14513                                         statp = kstat_waitq_to_runq;
14514                                 }
14515                                 saved_statp = un->un_retry_statp;
14516                                 un->un_retry_statp = NULL;
14517 
14518                                 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
14519                                     "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p "
14520                                     "un_throttle:%d un_ncmds_in_transport:%d\n",
14521                                     un, un->un_retry_bp, un->un_throttle,
14522                                     un->un_ncmds_in_transport);
14523                         } else {
14524                                 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: "
14525                                     "processing priority bp:0x%p\n", bp);
14526                         }
14527 
14528                 } else if ((bp = un->un_waitq_headp) != NULL) {
14529                         /*
14530                          * A command on the waitq is ready to go, but do not
14531                          * send it if:
14532                          *
14533                          * (1) the throttle limit has been reached, or
14534                          * (2) a retry is pending, or
14535                          * (3) a START_STOP_UNIT callback pending, or
14536                          * (4) a callback for a SD_PATH_DIRECT_PRIORITY
14537                          *      command is pending.
14538                          *
14539                          * For all of these conditions, IO processing will
14540                          * restart after the condition is cleared.
14541                          */
14542                         if (un->un_ncmds_in_transport >= un->un_throttle) {
14543                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14544                                     "sd_start_cmds: exiting, "
14545                                     "throttle limit reached!\n");
14546                                 goto exit;
14547                         }
14548                         if (un->un_retry_bp != NULL) {
14549                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14550                                     "sd_start_cmds: exiting, retry pending!\n");
14551                                 goto exit;
14552                         }
14553                         if (un->un_startstop_timeid != NULL) {
14554                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14555                                     "sd_start_cmds: exiting, "
14556                                     "START_STOP pending!\n");
14557                                 goto exit;
14558                         }
14559                         if (un->un_direct_priority_timeid != NULL) {
14560                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14561                                     "sd_start_cmds: exiting, "
14562                                     "SD_PATH_DIRECT_PRIORITY cmd. pending!\n");
14563                                 goto exit;
14564                         }
14565 
14566                         /* Dequeue the command */
14567                         un->un_waitq_headp = bp->av_forw;
14568                         if (un->un_waitq_headp == NULL) {
14569                                 un->un_waitq_tailp = NULL;
14570                         }
14571                         bp->av_forw = NULL;
14572                         statp = kstat_waitq_to_runq;
14573                         SD_TRACE(SD_LOG_IO_CORE, un,
14574                             "sd_start_cmds: processing waitq bp:0x%p\n", bp);
14575 
14576                 } else {
14577                         /* No work to do so bail out now */
14578                         SD_TRACE(SD_LOG_IO_CORE, un,
14579                             "sd_start_cmds: no more work, exiting!\n");
14580                         goto exit;
14581                 }
14582 
14583                 /*
14584                  * Reset the state to normal. This is the mechanism by which
14585                  * the state transitions from either SD_STATE_RWAIT or
14586                  * SD_STATE_OFFLINE to SD_STATE_NORMAL.
14587                  * If state is SD_STATE_PM_CHANGING then this command is
14588                  * part of the device power control and the state must
14589                  * not be put back to normal. Doing so would would
14590                  * allow new commands to proceed when they shouldn't,
14591                  * the device may be going off.
14592                  *
14593                  * Similarly, if the state is SD_STATE_ATTACHING we should
14594                  * not set it to SD_STATE_NORMAL to avoid corruption.
14595                  */
14596                 if ((un->un_state != SD_STATE_SUSPENDED) &&
14597                     (un->un_state != SD_STATE_PM_CHANGING) &&
14598                     (un->un_state != SD_STATE_ATTACHING)) {
14599                         New_state(un, SD_STATE_NORMAL);
14600                 }
14601 
14602                 xp = SD_GET_XBUF(bp);
14603                 ASSERT(xp != NULL);
14604 
14605                 /*
14606                  * Allocate the scsi_pkt if we need one, or attach DMA
14607                  * resources if we have a scsi_pkt that needs them. The
14608                  * latter should only occur for commands that are being
14609                  * retried.
14610                  */
14611                 if ((xp->xb_pktp == NULL) ||
14612                     ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) {
14613                         /*
14614                          * There is no scsi_pkt allocated for this buf. Call
14615                          * the initpkt function to allocate & init one.
14616                          *
14617                          * The scsi_init_pkt runout callback functionality is
14618                          * implemented as follows:
14619                          *
14620                          * 1) The initpkt function always calls
14621                          *    scsi_init_pkt(9F) with sdrunout specified as the
14622                          *    callback routine.
14623                          * 2) A successful packet allocation is initialized and
14624                          *    the I/O is transported.
14625                          * 3) The I/O associated with an allocation resource
14626                          *    failure is left on its queue to be retried via
14627                          *    runout or the next I/O.
14628                          * 4) The I/O associated with a DMA error is removed
14629                          *    from the queue and failed with EIO. Processing of
14630                          *    the transport queues is also halted to be
14631                          *    restarted via runout or the next I/O.
14632                          * 5) The I/O associated with a CDB size or packet
14633                          *    size error is removed from the queue and failed
14634                          *    with EIO. Processing of the transport queues is
14635                          *    continued.
14636                          *
14637                          * Note: there is no interface for canceling a runout
14638                          * callback. To prevent the driver from detaching or
14639                          * suspending while a runout is pending the driver
14640                          * state is set to SD_STATE_RWAIT
14641                          *
14642                          * Note: using the scsi_init_pkt callback facility can
14643                          * result in an I/O request persisting at the head of
14644                          * the list which cannot be satisfied even after
14645                          * multiple retries. In the future the driver may
14646                          * implement some kind of maximum runout count before
14647                          * failing an I/O.
14648                          *
14649                          * Note: the use of funcp below may seem superfluous,
14650                          * but it helps warlock figure out the correct
14651                          * initpkt function calls (see [s]sd.wlcmd).
14652                          */
14653                         struct scsi_pkt *pktp;
14654                         int (*funcp)(struct buf *bp, struct scsi_pkt **pktp);
14655 
14656                         ASSERT(bp != un->un_rqs_bp);
14657 
14658                         funcp = sd_initpkt_map[xp->xb_chain_iostart];
14659                         switch ((*funcp)(bp, &pktp)) {
14660                         case  SD_PKT_ALLOC_SUCCESS:
14661                                 xp->xb_pktp = pktp;
14662                                 SD_TRACE(SD_LOG_IO_CORE, un,
14663                                     "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n",
14664                                     pktp);
14665                                 goto got_pkt;
14666 
14667                         case SD_PKT_ALLOC_FAILURE:
14668                                 /*
14669                                  * Temporary (hopefully) resource depletion.
14670                                  * Since retries and RQS commands always have a
14671                                  * scsi_pkt allocated, these cases should never
14672                                  * get here. So the only cases this needs to
14673                                  * handle is a bp from the waitq (which we put
14674                                  * back onto the waitq for sdrunout), or a bp
14675                                  * sent as an immed_bp (which we just fail).
14676                                  */
14677                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14678                                     "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n");
14679 
14680                                 if (bp == immed_bp) {
14681                                         /*
14682                                          * If SD_XB_DMA_FREED is clear, then
14683                                          * this is a failure to allocate a
14684                                          * scsi_pkt, and we must fail the
14685                                          * command.
14686                                          */
14687                                         if ((xp->xb_pkt_flags &
14688                                             SD_XB_DMA_FREED) == 0) {
14689                                                 break;
14690                                         }
14691 
14692                                         /*
14693                                          * If this immediate command is NOT our
14694                                          * un_retry_bp, then we must fail it.
14695                                          */
14696                                         if (bp != un->un_retry_bp) {
14697                                                 break;
14698                                         }
14699 
14700                                         /*
14701                                          * We get here if this cmd is our
14702                                          * un_retry_bp that was DMAFREED, but
14703                                          * scsi_init_pkt() failed to reallocate
14704                                          * DMA resources when we attempted to
14705                                          * retry it. This can happen when an
14706                                          * mpxio failover is in progress, but
14707                                          * we don't want to just fail the
14708                                          * command in this case.
14709                                          *
14710                                          * Use timeout(9F) to restart it after
14711                                          * a 100ms delay.  We don't want to
14712                                          * let sdrunout() restart it, because
14713                                          * sdrunout() is just supposed to start
14714                                          * commands that are sitting on the
14715                                          * wait queue.  The un_retry_bp stays
14716                                          * set until the command completes, but
14717                                          * sdrunout can be called many times
14718                                          * before that happens.  Since sdrunout
14719                                          * cannot tell if the un_retry_bp is
14720                                          * already in the transport, it could
14721                                          * end up calling scsi_transport() for
14722                                          * the un_retry_bp multiple times.
14723                                          *
14724                                          * Also: don't schedule the callback
14725                                          * if some other callback is already
14726                                          * pending.
14727                                          */
14728                                         if (un->un_retry_statp == NULL) {
14729                                                 /*
14730                                                  * restore the kstat pointer to
14731                                                  * keep kstat counts coherent
14732                                                  * when we do retry the command.
14733                                                  */
14734                                                 un->un_retry_statp =
14735                                                     saved_statp;
14736                                         }
14737 
14738                                         if ((un->un_startstop_timeid == NULL) &&
14739                                             (un->un_retry_timeid == NULL) &&
14740                                             (un->un_direct_priority_timeid ==
14741                                             NULL)) {
14742 
14743                                                 un->un_retry_timeid =
14744                                                     timeout(
14745                                                     sd_start_retry_command,
14746                                                     un, SD_RESTART_TIMEOUT);
14747                                         }
14748                                         goto exit;
14749                                 }
14750 
14751                                 /* Add the buf back to the head of the waitq */
14752                                 bp->av_forw = un->un_waitq_headp;
14753                                 un->un_waitq_headp = bp;
14754                                 if (un->un_waitq_tailp == NULL) {
14755                                         un->un_waitq_tailp = bp;
14756                                 }
14757                                 goto exit;
14758 
14759                         case SD_PKT_ALLOC_FAILURE_NO_DMA:
14760                                 /*
14761                                  * HBA DMA resource failure. Fail the command
14762                                  * and continue processing of the queues.
14763                                  */
14764                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14765                                     "sd_start_cmds: "
14766                                     "SD_PKT_ALLOC_FAILURE_NO_DMA\n");
14767                                 break;
14768 
14769                         case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL:
14770                                 /*
14771                                  * Partial DMA mapping not supported for USCSI
14772                                  * commands, and all the needed DMA resources
14773                                  * were not allocated.
14774                                  */
14775                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14776                                     "sd_start_cmds: "
14777                                     "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n");
14778                                 break;
14779 
14780                         case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL:
14781                                 /*
14782                                  * Request cannot fit into CDB based on lba
14783                                  * and len.
14784                                  */
14785                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14786                                     "sd_start_cmds: "
14787                                     "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n");
14788                                 break;
14789 
14790                         default:
14791                                 /* Should NEVER get here! */
14792                                 panic("scsi_initpkt error");
14793                                 /*NOTREACHED*/
14794                         }
14795 
14796                         /*
14797                          * Fatal error in allocating a scsi_pkt for this buf.
14798                          * Update kstats & return the buf with an error code.
14799                          * We must use sd_return_failed_command_no_restart() to
14800                          * avoid a recursive call back into sd_start_cmds().
14801                          * However this also means that we must keep processing
14802                          * the waitq here in order to avoid stalling.
14803                          */
14804                         if (statp == kstat_waitq_to_runq) {
14805                                 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
14806                         }
14807                         sd_return_failed_command_no_restart(un, bp, EIO);
14808                         if (bp == immed_bp) {
14809                                 /* immed_bp is gone by now, so clear this */
14810                                 immed_bp = NULL;
14811                         }
14812                         continue;
14813                 }
14814 got_pkt:
14815                 if (bp == immed_bp) {
14816                         /* goto the head of the class.... */
14817                         xp->xb_pktp->pkt_flags |= FLAG_HEAD;
14818                 }
14819 
14820                 un->un_ncmds_in_transport++;
14821                 SD_UPDATE_KSTATS(un, statp, bp);
14822                 /* The start time MAY be overriden by the HBA driver. */
14823                 xp->xb_pktp->pkt_start = gethrtime();
14824                 xp->xb_pktp->pkt_stop = 0;
14825 
14826                 /*
14827                  * Call scsi_transport() to send the command to the target.
14828                  * According to SCSA architecture, we must drop the mutex here
14829                  * before calling scsi_transport() in order to avoid deadlock.
14830                  * Note that the scsi_pkt's completion routine can be executed
14831                  * (from interrupt context) even before the call to
14832                  * scsi_transport() returns.
14833                  */
14834                 SD_TRACE(SD_LOG_IO_CORE, un,
14835                     "sd_start_cmds: calling scsi_transport()\n");
14836                 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp);
14837 
14838 #ifdef SD_FAULT_INJECTION
14839                 /*
14840                  * Packet is ready for submission to the HBA. Perform HBA-based
14841                  * fault-injection.
14842                  */
14843                 sd_prefaultinjection(xp->xb_pktp);
14844 #endif /* SD_FAULT_INJECTION */
14845 
14846                 mutex_exit(SD_MUTEX(un));
14847                 rval = scsi_transport(xp->xb_pktp);
14848                 mutex_enter(SD_MUTEX(un));
14849 
14850                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14851                     "sd_start_cmds: scsi_transport() returned %d\n", rval);
14852 
14853                 switch (rval) {
14854                 case TRAN_ACCEPT:
14855                         /* Clear this with every pkt accepted by the HBA */
14856                         un->un_tran_fatal_count = 0;
14857                         break;  /* Success; try the next cmd (if any) */
14858 
14859                 case TRAN_BUSY:
14860                         un->un_ncmds_in_transport--;
14861                         ASSERT(un->un_ncmds_in_transport >= 0);
14862 
14863 #ifdef SD_FAULT_INJECTION
14864                         /*
14865                          * If the packet was rejected during active fault
14866                          * injection session, move to the next fault slot
14867                          * and reset packet flag related to rejection.
14868                          */
14869                         if (sd_fault_injection_on) {
14870                                 uint_t i = un->sd_fi_fifo_start;
14871 
14872                                 if (un->sd_fi_fifo_tran[i] != NULL) {
14873                                         kmem_free(un->sd_fi_fifo_tran[i],
14874                                             sizeof (struct sd_fi_tran));
14875                                         un->sd_fi_fifo_tran[i] = NULL;
14876                                 }
14877                                 un->sd_fi_fifo_start++;
14878                         }
14879 
14880                         if (xp->xb_pktp->pkt_flags & FLAG_PKT_BUSY) {
14881                                 xp->xb_pktp->pkt_flags &= ~FLAG_PKT_BUSY;
14882                         }
14883 #endif /* SD_FAULT_INJECTION */
14884 
14885                         /*
14886                          * Don't retry request sense, the sense data
14887                          * is lost when another request is sent.
14888                          * Free up the rqs buf and retry
14889                          * the original failed cmd.  Update kstat.
14890                          */
14891                         if ((un->un_ncmds_in_transport > 0) &&
14892                             (bp == un->un_rqs_bp)) {
14893                                 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
14894                                 bp = sd_mark_rqs_idle(un, xp);
14895                                 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
14896                                     NULL, NULL, EIO, un->un_busy_timeout / 500,
14897                                     kstat_waitq_enter);
14898                                 goto exit;
14899                         }
14900 
14901                         /*
14902                          * Free the DMA resources for the  scsi_pkt. This will
14903                          * allow mpxio to select another path the next time
14904                          * we call scsi_transport() with this scsi_pkt.
14905                          * See sdintr() for the rationalization behind this.
14906                          */
14907                         if ((un->un_f_is_fibre == TRUE) &&
14908                             ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
14909                             ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) {
14910                                 scsi_dmafree(xp->xb_pktp);
14911                                 xp->xb_pkt_flags |= SD_XB_DMA_FREED;
14912                         }
14913 
14914                         if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) {
14915                                 /*
14916                                  * Commands that are SD_PATH_DIRECT_PRIORITY
14917                                  * are for error recovery situations. These do
14918                                  * not use the normal command waitq, so if they
14919                                  * get a TRAN_BUSY we cannot put them back onto
14920                                  * the waitq for later retry. One possible
14921                                  * problem is that there could already be some
14922                                  * other command on un_retry_bp that is waiting
14923                                  * for this one to complete, so we would be
14924                                  * deadlocked if we put this command back onto
14925                                  * the waitq for later retry (since un_retry_bp
14926                                  * must complete before the driver gets back to
14927                                  * commands on the waitq).
14928                                  *
14929                                  * To avoid deadlock we must schedule a callback
14930                                  * that will restart this command after a set
14931                                  * interval.  This should keep retrying for as
14932                                  * long as the underlying transport keeps
14933                                  * returning TRAN_BUSY (just like for other
14934                                  * commands).  Use the same timeout interval as
14935                                  * for the ordinary TRAN_BUSY retry.
14936                                  */
14937                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14938                                     "sd_start_cmds: scsi_transport() returned "
14939                                     "TRAN_BUSY for DIRECT_PRIORITY cmd!\n");
14940 
14941                                 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
14942                                 un->un_direct_priority_timeid =
14943                                     timeout(sd_start_direct_priority_command,
14944                                     bp, un->un_busy_timeout / 500);
14945 
14946                                 goto exit;
14947                         }
14948 
14949                         /*
14950                          * For TRAN_BUSY, we want to reduce the throttle value,
14951                          * unless we are retrying a command.
14952                          */
14953                         if (bp != un->un_retry_bp) {
14954                                 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY);
14955                         }
14956 
14957                         /*
14958                          * Set up the bp to be tried again 10 ms later.
14959                          * XXX Is there a timeout value in the sd_lun
14960                          * for this condition?
14961                          */
14962                         sd_set_retry_bp(un, bp, un->un_busy_timeout / 500,
14963                             kstat_runq_back_to_waitq);
14964                         goto exit;
14965 
14966                 case TRAN_FATAL_ERROR:
14967                         un->un_tran_fatal_count++;
14968                         /* FALLTHRU */
14969 
14970                 case TRAN_BADPKT:
14971                 default:
14972                         un->un_ncmds_in_transport--;
14973                         ASSERT(un->un_ncmds_in_transport >= 0);
14974 
14975                         /*
14976                          * If this is our REQUEST SENSE command with a
14977                          * transport error, we must get back the pointers
14978                          * to the original buf, and mark the REQUEST
14979                          * SENSE command as "available".
14980                          */
14981                         if (bp == un->un_rqs_bp) {
14982                                 bp = sd_mark_rqs_idle(un, xp);
14983                                 xp = SD_GET_XBUF(bp);
14984                         } else {
14985                                 /*
14986                                  * Legacy behavior: do not update transport
14987                                  * error count for request sense commands.
14988                                  */
14989                                 SD_UPDATE_ERRSTATS(un, sd_transerrs);
14990                         }
14991 
14992                         SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
14993                         sd_print_transport_rejected_message(un, xp, rval);
14994 
14995                         /*
14996                          * This command will be terminated by SD driver due
14997                          * to a fatal transport error. We should post
14998                          * ereport.io.scsi.cmd.disk.tran with driver-assessment
14999                          * of "fail" for any command to indicate this
15000                          * situation.
15001                          */
15002                         if (xp->xb_ena > 0) {
15003                                 ASSERT(un->un_fm_private != NULL);
15004                                 sfip = un->un_fm_private;
15005                                 sfip->fm_ssc.ssc_flags |= SSC_FLAGS_TRAN_ABORT;
15006                                 sd_ssc_extract_info(&sfip->fm_ssc, un,
15007                                     xp->xb_pktp, bp, xp);
15008                                 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
15009                         }
15010 
15011                         /*
15012                          * We must use sd_return_failed_command_no_restart() to
15013                          * avoid a recursive call back into sd_start_cmds().
15014                          * However this also means that we must keep processing
15015                          * the waitq here in order to avoid stalling.
15016                          */
15017                         sd_return_failed_command_no_restart(un, bp, EIO);
15018 
15019                         /*
15020                          * Notify any threads waiting in sd_ddi_suspend() that
15021                          * a command completion has occurred.
15022                          */
15023                         if (un->un_state == SD_STATE_SUSPENDED) {
15024                                 cv_broadcast(&un->un_disk_busy_cv);
15025                         }
15026 
15027                         if (bp == immed_bp) {
15028                                 /* immed_bp is gone by now, so clear this */
15029                                 immed_bp = NULL;
15030                         }
15031                         break;
15032                 }
15033 
15034         } while (immed_bp == NULL);
15035 
15036 exit:
15037         ASSERT(mutex_owned(SD_MUTEX(un)));
15038         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n");
15039 }
15040 
15041 
15042 /*
15043  *    Function: sd_return_command
15044  *
15045  * Description: Returns a command to its originator (with or without an
15046  *              error).  Also starts commands waiting to be transported
15047  *              to the target.
15048  *
15049  *     Context: May be called from interrupt, kernel, or timeout context
15050  */
15051 
15052 static void
15053 sd_return_command(struct sd_lun *un, struct buf *bp)
15054 {
15055         struct sd_xbuf *xp;
15056         struct scsi_pkt *pktp;
15057         struct sd_fm_internal *sfip;
15058 
15059         ASSERT(bp != NULL);
15060         ASSERT(un != NULL);
15061         ASSERT(mutex_owned(SD_MUTEX(un)));
15062         ASSERT(bp != un->un_rqs_bp);
15063         xp = SD_GET_XBUF(bp);
15064         ASSERT(xp != NULL);
15065 
15066         pktp = SD_GET_PKTP(bp);
15067         sfip = (struct sd_fm_internal *)un->un_fm_private;
15068         ASSERT(sfip != NULL);
15069 
15070         SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n");
15071 
15072         /*
15073          * Note: check for the "sdrestart failed" case.
15074          */
15075         if ((un->un_partial_dma_supported == 1) &&
15076             ((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) &&
15077             (geterror(bp) == 0) && (xp->xb_dma_resid != 0) &&
15078             (xp->xb_pktp->pkt_resid == 0)) {
15079 
15080                 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) {
15081                         /*
15082                          * Successfully set up next portion of cmd
15083                          * transfer, try sending it
15084                          */
15085                         sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
15086                             NULL, NULL, 0, (clock_t)0, NULL);
15087                         sd_start_cmds(un, NULL);
15088                         return; /* XXX need a return here? */
15089                 }
15090         }
15091 
15092         /*
15093          * If this is the failfast bp, clear it from un_failfast_bp. This
15094          * can happen if upon being re-tried the failfast bp either
15095          * succeeded or encountered another error (possibly even a different
15096          * error than the one that precipitated the failfast state, but in
15097          * that case it would have had to exhaust retries as well). Regardless,
15098          * this should not occur whenever the instance is in the active
15099          * failfast state.
15100          */
15101         if (bp == un->un_failfast_bp) {
15102                 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15103                 un->un_failfast_bp = NULL;
15104         }
15105 
15106         /*
15107          * Clear the failfast state upon successful completion of ANY cmd.
15108          */
15109         if (bp->b_error == 0) {
15110                 un->un_failfast_state = SD_FAILFAST_INACTIVE;
15111                 /*
15112                  * If this is a successful command, but used to be retried,
15113                  * we will take it as a recovered command and post an
15114                  * ereport with driver-assessment of "recovered".
15115                  */
15116                 if (xp->xb_ena > 0) {
15117                         sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15118                         sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RECOVERY);
15119                 }
15120         } else {
15121                 /*
15122                  * If this is a failed non-USCSI command we will post an
15123                  * ereport with driver-assessment set accordingly("fail" or
15124                  * "fatal").
15125                  */
15126                 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
15127                         sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15128                         sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
15129                 }
15130         }
15131 
15132         /*
15133          * This is used if the command was retried one or more times. Show that
15134          * we are done with it, and allow processing of the waitq to resume.
15135          */
15136         if (bp == un->un_retry_bp) {
15137                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15138                     "sd_return_command: un:0x%p: "
15139                     "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15140                 un->un_retry_bp = NULL;
15141                 un->un_retry_statp = NULL;
15142         }
15143 
15144         SD_UPDATE_RDWR_STATS(un, bp);
15145         SD_UPDATE_PARTITION_STATS(un, bp);
15146 
15147         switch (un->un_state) {
15148         case SD_STATE_SUSPENDED:
15149                 /*
15150                  * Notify any threads waiting in sd_ddi_suspend() that
15151                  * a command completion has occurred.
15152                  */
15153                 cv_broadcast(&un->un_disk_busy_cv);
15154                 break;
15155         default:
15156                 sd_start_cmds(un, NULL);
15157                 break;
15158         }
15159 
15160         /* Return this command up the iodone chain to its originator. */
15161         mutex_exit(SD_MUTEX(un));
15162 
15163         (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15164         xp->xb_pktp = NULL;
15165 
15166         SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15167 
15168         ASSERT(!mutex_owned(SD_MUTEX(un)));
15169         mutex_enter(SD_MUTEX(un));
15170 
15171         SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n");
15172 }
15173 
15174 
15175 /*
15176  *    Function: sd_return_failed_command
15177  *
15178  * Description: Command completion when an error occurred.
15179  *
15180  *     Context: May be called from interrupt context
15181  */
15182 
15183 static void
15184 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode)
15185 {
15186         ASSERT(bp != NULL);
15187         ASSERT(un != NULL);
15188         ASSERT(mutex_owned(SD_MUTEX(un)));
15189 
15190         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15191             "sd_return_failed_command: entry\n");
15192 
15193         /*
15194          * b_resid could already be nonzero due to a partial data
15195          * transfer, so do not change it here.
15196          */
15197         SD_BIOERROR(bp, errcode);
15198 
15199         sd_return_command(un, bp);
15200         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15201             "sd_return_failed_command: exit\n");
15202 }
15203 
15204 
15205 /*
15206  *    Function: sd_return_failed_command_no_restart
15207  *
15208  * Description: Same as sd_return_failed_command, but ensures that no
15209  *              call back into sd_start_cmds will be issued.
15210  *
15211  *     Context: May be called from interrupt context
15212  */
15213 
15214 static void
15215 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp,
15216     int errcode)
15217 {
15218         struct sd_xbuf *xp;
15219 
15220         ASSERT(bp != NULL);
15221         ASSERT(un != NULL);
15222         ASSERT(mutex_owned(SD_MUTEX(un)));
15223         xp = SD_GET_XBUF(bp);
15224         ASSERT(xp != NULL);
15225         ASSERT(errcode != 0);
15226 
15227         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15228             "sd_return_failed_command_no_restart: entry\n");
15229 
15230         /*
15231          * b_resid could already be nonzero due to a partial data
15232          * transfer, so do not change it here.
15233          */
15234         SD_BIOERROR(bp, errcode);
15235 
15236         /*
15237          * If this is the failfast bp, clear it. This can happen if the
15238          * failfast bp encounterd a fatal error when we attempted to
15239          * re-try it (such as a scsi_transport(9F) failure).  However
15240          * we should NOT be in an active failfast state if the failfast
15241          * bp is not NULL.
15242          */
15243         if (bp == un->un_failfast_bp) {
15244                 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15245                 un->un_failfast_bp = NULL;
15246         }
15247 
15248         if (bp == un->un_retry_bp) {
15249                 /*
15250                  * This command was retried one or more times. Show that we are
15251                  * done with it, and allow processing of the waitq to resume.
15252                  */
15253                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15254                     "sd_return_failed_command_no_restart: "
15255                     " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15256                 un->un_retry_bp = NULL;
15257                 un->un_retry_statp = NULL;
15258         }
15259 
15260         SD_UPDATE_RDWR_STATS(un, bp);
15261         SD_UPDATE_PARTITION_STATS(un, bp);
15262 
15263         mutex_exit(SD_MUTEX(un));
15264 
15265         if (xp->xb_pktp != NULL) {
15266                 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15267                 xp->xb_pktp = NULL;
15268         }
15269 
15270         SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15271 
15272         mutex_enter(SD_MUTEX(un));
15273 
15274         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15275             "sd_return_failed_command_no_restart: exit\n");
15276 }
15277 
15278 
15279 /*
15280  *    Function: sd_retry_command
15281  *
15282  * Description: queue up a command for retry, or (optionally) fail it
15283  *              if retry counts are exhausted.
15284  *
15285  *   Arguments: un - Pointer to the sd_lun struct for the target.
15286  *
15287  *              bp - Pointer to the buf for the command to be retried.
15288  *
15289  *              retry_check_flag - Flag to see which (if any) of the retry
15290  *                 counts should be decremented/checked. If the indicated
15291  *                 retry count is exhausted, then the command will not be
15292  *                 retried; it will be failed instead. This should use a
15293  *                 value equal to one of the following:
15294  *
15295  *                      SD_RETRIES_NOCHECK
15296  *                      SD_RESD_RETRIES_STANDARD
15297  *                      SD_RETRIES_VICTIM
15298  *
15299  *                 Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE
15300  *                 if the check should be made to see of FLAG_ISOLATE is set
15301  *                 in the pkt. If FLAG_ISOLATE is set, then the command is
15302  *                 not retried, it is simply failed.
15303  *
15304  *                 Optionally may be bitwise-OR'ed with SD_RETRIES_FAILFAST
15305  *                 to indicate a retry following a command timeout, and check
15306  *                 if the target should transition to failfast pending or
15307  *                 failfast active. If the buf has B_FAILFAST set, the
15308  *                 command should be failed when failfast is active.
15309  *
15310  *              user_funcp - Ptr to function to call before dispatching the
15311  *                 command. May be NULL if no action needs to be performed.
15312  *                 (Primarily intended for printing messages.)
15313  *
15314  *              user_arg - Optional argument to be passed along to
15315  *                 the user_funcp call.
15316  *
15317  *              failure_code - errno return code to set in the bp if the
15318  *                 command is going to be failed.
15319  *
15320  *              retry_delay - Retry delay interval in (clock_t) units. May
15321  *                 be zero which indicates that the retry should be retried
15322  *                 immediately (ie, without an intervening delay).
15323  *
15324  *              statp - Ptr to kstat function to be updated if the command
15325  *                 is queued for a delayed retry. May be NULL if no kstat
15326  *                 update is desired.
15327  *
15328  *     Context: May be called from interrupt context.
15329  */
15330 
15331 static void
15332 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag,
15333     void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int code),
15334     void *user_arg, int failure_code, clock_t retry_delay,
15335     void (*statp)(kstat_io_t *))
15336 {
15337         struct sd_xbuf  *xp;
15338         struct scsi_pkt *pktp;
15339         struct sd_fm_internal *sfip;
15340 
15341         ASSERT(un != NULL);
15342         ASSERT(mutex_owned(SD_MUTEX(un)));
15343         ASSERT(bp != NULL);
15344         xp = SD_GET_XBUF(bp);
15345         ASSERT(xp != NULL);
15346         pktp = SD_GET_PKTP(bp);
15347         ASSERT(pktp != NULL);
15348 
15349         sfip = (struct sd_fm_internal *)un->un_fm_private;
15350         ASSERT(sfip != NULL);
15351 
15352         SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15353             "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp);
15354 
15355         /*
15356          * If we are syncing or dumping, fail the command to avoid
15357          * recursively calling back into scsi_transport().
15358          */
15359         if (ddi_in_panic()) {
15360                 goto fail_command_no_log;
15361         }
15362 
15363         /*
15364          * We should never be be retrying a command with FLAG_DIAGNOSE set, so
15365          * log an error and fail the command.
15366          */
15367         if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
15368                 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
15369                     "ERROR, retrying FLAG_DIAGNOSE command.\n");
15370                 sd_dump_memory(un, SD_LOG_IO, "CDB",
15371                     (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
15372                 sd_dump_memory(un, SD_LOG_IO, "Sense Data",
15373                     (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
15374                 goto fail_command;
15375         }
15376 
15377         /*
15378          * If we are suspended, then put the command onto head of the
15379          * wait queue since we don't want to start more commands, and
15380          * clear the un_retry_bp. Next time when we are resumed, will
15381          * handle the command in the wait queue.
15382          */
15383         switch (un->un_state) {
15384         case SD_STATE_SUSPENDED:
15385         case SD_STATE_DUMPING:
15386                 bp->av_forw = un->un_waitq_headp;
15387                 un->un_waitq_headp = bp;
15388                 if (un->un_waitq_tailp == NULL) {
15389                         un->un_waitq_tailp = bp;
15390                 }
15391                 if (bp == un->un_retry_bp) {
15392                         un->un_retry_bp = NULL;
15393                         un->un_retry_statp = NULL;
15394                 }
15395                 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
15396                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: "
15397                     "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp);
15398                 return;
15399         default:
15400                 break;
15401         }
15402 
15403         /*
15404          * If the caller wants us to check FLAG_ISOLATE, then see if that
15405          * is set; if it is then we do not want to retry the command.
15406          * Normally, FLAG_ISOLATE is only used with USCSI cmds.
15407          */
15408         if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) {
15409                 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) {
15410                         goto fail_command;
15411                 }
15412         }
15413 
15414         if (sd_failfast_enable & (SD_FAILFAST_ENABLE_FAIL_RETRIES |
15415             SD_FAILFAST_ENABLE_FAIL_ALL_RETRIES)) {
15416                 if (sd_failfast_enable & SD_FAILFAST_ENABLE_FAIL_ALL_RETRIES) {
15417                         /*
15418                          * Fail ALL retries when in active failfast state,
15419                          * regardless of reason.
15420                          */
15421                         if (un->un_failfast_state == SD_FAILFAST_ACTIVE) {
15422                                 goto fail_command;
15423                         }
15424                 }
15425                 /*
15426                  * Treat bufs being retried as if they have the
15427                  * B_FAILFAST flag set.
15428                  */
15429                 bp->b_flags |= B_FAILFAST;
15430         }
15431 
15432         /*
15433          * If SD_RETRIES_FAILFAST is set, it indicates that either a
15434          * command timeout or a selection timeout has occurred. This means
15435          * that we were unable to establish an kind of communication with
15436          * the target, and subsequent retries and/or commands are likely
15437          * to encounter similar results and take a long time to complete.
15438          *
15439          * If this is a failfast error condition, we need to update the
15440          * failfast state, even if this bp does not have B_FAILFAST set.
15441          */
15442         if (retry_check_flag & SD_RETRIES_FAILFAST) {
15443                 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) {
15444                         ASSERT(un->un_failfast_bp == NULL);
15445                         /*
15446                          * If we are already in the active failfast state, and
15447                          * another failfast error condition has been detected,
15448                          * then fail this command if it has B_FAILFAST set.
15449                          * If B_FAILFAST is clear, then maintain the legacy
15450                          * behavior of retrying heroically, even tho this will
15451                          * take a lot more time to fail the command.
15452                          */
15453                         if (bp->b_flags & B_FAILFAST) {
15454                                 goto fail_command;
15455                         }
15456                 } else {
15457                         /*
15458                          * We're not in the active failfast state, but we
15459                          * have a failfast error condition, so we must begin
15460                          * transition to the next state. We do this regardless
15461                          * of whether or not this bp has B_FAILFAST set.
15462                          */
15463                         if (un->un_failfast_bp == NULL) {
15464                                 /*
15465                                  * This is the first bp to meet a failfast
15466                                  * condition so save it on un_failfast_bp &
15467                                  * do normal retry processing. Do not enter
15468                                  * active failfast state yet. This marks
15469                                  * entry into the "failfast pending" state.
15470                                  */
15471                                 un->un_failfast_bp = bp;
15472 
15473                         } else if (un->un_failfast_bp == bp) {
15474                                 /*
15475                                  * This is the second time *this* bp has
15476                                  * encountered a failfast error condition,
15477                                  * so enter active failfast state & flush
15478                                  * queues as appropriate.
15479                                  */
15480                                 un->un_failfast_state = SD_FAILFAST_ACTIVE;
15481                                 un->un_failfast_bp = NULL;
15482                                 sd_failfast_flushq(un, B_FALSE);
15483 
15484                                 /*
15485                                  * Fail this bp now if B_FAILFAST set;
15486                                  * otherwise continue with retries. (It would
15487                                  * be pretty ironic if this bp succeeded on a
15488                                  * subsequent retry after we just flushed all
15489                                  * the queues).
15490                                  */
15491                                 if (bp->b_flags & B_FAILFAST) {
15492                                         goto fail_command;
15493                                 }
15494 
15495 #if !defined(lint) && !defined(__lint)
15496                         } else {
15497                                 /*
15498                                  * If neither of the preceeding conditionals
15499                                  * was true, it means that there is some
15500                                  * *other* bp that has met an inital failfast
15501                                  * condition and is currently either being
15502                                  * retried or is waiting to be retried. In
15503                                  * that case we should perform normal retry
15504                                  * processing on *this* bp, since there is a
15505                                  * chance that the current failfast condition
15506                                  * is transient and recoverable. If that does
15507                                  * not turn out to be the case, then retries
15508                                  * will be cleared when the wait queue is
15509                                  * flushed anyway.
15510                                  */
15511 #endif
15512                         }
15513                 }
15514         } else {
15515                 /*
15516                  * SD_RETRIES_FAILFAST is clear, which indicates that we
15517                  * likely were able to at least establish some level of
15518                  * communication with the target and subsequent commands
15519                  * and/or retries are likely to get through to the target,
15520                  * In this case we want to be aggressive about clearing
15521                  * the failfast state. Note that this does not affect
15522                  * the "failfast pending" condition.
15523                  *
15524                  * We limit this to retries that are not a side effect of an
15525                  * unrelated event, as it would be unwise to clear failfast
15526                  * active state when we see retries due to a reset.
15527                  */
15528                 if ((sd_failfast_enable & SD_FAILFAST_ENABLE_FORCE_INACTIVE) &&
15529                     (retry_check_flag & SD_RETRIES_MASK) != SD_RETRIES_VICTIM)
15530                         un->un_failfast_state = SD_FAILFAST_INACTIVE;
15531         }
15532 
15533 
15534         /*
15535          * Check the specified retry count to see if we can still do
15536          * any retries with this pkt before we should fail it.
15537          */
15538         switch (retry_check_flag & SD_RETRIES_MASK) {
15539         case SD_RETRIES_VICTIM:
15540                 /*
15541                  * Check the victim retry count. If exhausted, then fall
15542                  * thru & check against the standard retry count.
15543                  */
15544                 if (xp->xb_victim_retry_count < un->un_victim_retry_count) {
15545                         /* Increment count & proceed with the retry */
15546                         xp->xb_victim_retry_count++;
15547                         break;
15548                 }
15549                 /* Victim retries exhausted, fall back to std. retries... */
15550                 /* FALLTHRU */
15551 
15552         case SD_RETRIES_STANDARD:
15553                 if (xp->xb_retry_count >= un->un_retry_count) {
15554                         /* Retries exhausted, fail the command */
15555                         SD_TRACE(SD_LOG_IO_CORE, un,
15556                             "sd_retry_command: retries exhausted!\n");
15557                         /*
15558                          * update b_resid for failed SCMD_READ & SCMD_WRITE
15559                          * commands with nonzero pkt_resid.
15560                          */
15561                         if ((pktp->pkt_reason == CMD_CMPLT) &&
15562                             (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) &&
15563                             (pktp->pkt_resid != 0)) {
15564                                 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F;
15565                                 if ((op == SCMD_READ) || (op == SCMD_WRITE)) {
15566                                         SD_UPDATE_B_RESID(bp, pktp);
15567                                 }
15568                         }
15569                         goto fail_command;
15570                 }
15571                 xp->xb_retry_count++;
15572                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15573                     "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15574                 break;
15575 
15576         case SD_RETRIES_UA:
15577                 if (xp->xb_ua_retry_count >= sd_ua_retry_count) {
15578                         /* Retries exhausted, fail the command */
15579                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
15580                             "Unit Attention retries exhausted. "
15581                             "Check the target.\n");
15582                         goto fail_command;
15583                 }
15584                 xp->xb_ua_retry_count++;
15585                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15586                     "sd_retry_command: retry count:%d\n",
15587                     xp->xb_ua_retry_count);
15588                 break;
15589 
15590         case SD_RETRIES_BUSY:
15591                 if (xp->xb_retry_count >= un->un_busy_retry_count) {
15592                         /* Retries exhausted, fail the command */
15593                         SD_TRACE(SD_LOG_IO_CORE, un,
15594                             "sd_retry_command: retries exhausted!\n");
15595                         goto fail_command;
15596                 }
15597                 xp->xb_retry_count++;
15598                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15599                     "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15600                 break;
15601 
15602         case SD_RETRIES_NOCHECK:
15603         default:
15604                 /* No retry count to check. Just proceed with the retry */
15605                 break;
15606         }
15607 
15608         xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15609 
15610         /*
15611          * If this is a non-USCSI command being retried
15612          * during execution last time, we should post an ereport with
15613          * driver-assessment of the value "retry".
15614          * For partial DMA, request sense and STATUS_QFULL, there are no
15615          * hardware errors, we bypass ereport posting.
15616          */
15617         if (failure_code != 0) {
15618                 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
15619                         sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15620                         sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RETRY);
15621                 }
15622         }
15623 
15624         /*
15625          * If we were given a zero timeout, we must attempt to retry the
15626          * command immediately (ie, without a delay).
15627          */
15628         if (retry_delay == 0) {
15629                 /*
15630                  * Check some limiting conditions to see if we can actually
15631                  * do the immediate retry.  If we cannot, then we must
15632                  * fall back to queueing up a delayed retry.
15633                  */
15634                 if (un->un_ncmds_in_transport >= un->un_throttle) {
15635                         /*
15636                          * We are at the throttle limit for the target,
15637                          * fall back to delayed retry.
15638                          */
15639                         retry_delay = un->un_busy_timeout;
15640                         statp = kstat_waitq_enter;
15641                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15642                             "sd_retry_command: immed. retry hit "
15643                             "throttle!\n");
15644                 } else {
15645                         /*
15646                          * We're clear to proceed with the immediate retry.
15647                          * First call the user-provided function (if any)
15648                          */
15649                         if (user_funcp != NULL) {
15650                                 (*user_funcp)(un, bp, user_arg,
15651                                     SD_IMMEDIATE_RETRY_ISSUED);
15652 #ifdef __lock_lint
15653                                 sd_print_incomplete_msg(un, bp, user_arg,
15654                                     SD_IMMEDIATE_RETRY_ISSUED);
15655                                 sd_print_cmd_incomplete_msg(un, bp, user_arg,
15656                                     SD_IMMEDIATE_RETRY_ISSUED);
15657                                 sd_print_sense_failed_msg(un, bp, user_arg,
15658                                     SD_IMMEDIATE_RETRY_ISSUED);
15659 #endif
15660                         }
15661 
15662                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15663                             "sd_retry_command: issuing immediate retry\n");
15664 
15665                         /*
15666                          * Call sd_start_cmds() to transport the command to
15667                          * the target.
15668                          */
15669                         sd_start_cmds(un, bp);
15670 
15671                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15672                             "sd_retry_command exit\n");
15673                         return;
15674                 }
15675         }
15676 
15677         /*
15678          * Set up to retry the command after a delay.
15679          * First call the user-provided function (if any)
15680          */
15681         if (user_funcp != NULL) {
15682                 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED);
15683         }
15684 
15685         sd_set_retry_bp(un, bp, retry_delay, statp);
15686 
15687         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15688         return;
15689 
15690 fail_command:
15691 
15692         if (user_funcp != NULL) {
15693                 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED);
15694         }
15695 
15696 fail_command_no_log:
15697 
15698         SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15699             "sd_retry_command: returning failed command\n");
15700 
15701         sd_return_failed_command(un, bp, failure_code);
15702 
15703         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15704 }
15705 
15706 
15707 /*
15708  *    Function: sd_set_retry_bp
15709  *
15710  * Description: Set up the given bp for retry.
15711  *
15712  *   Arguments: un - ptr to associated softstate
15713  *              bp - ptr to buf(9S) for the command
15714  *              retry_delay - time interval before issuing retry (may be 0)
15715  *              statp - optional pointer to kstat function
15716  *
15717  *     Context: May be called under interrupt context
15718  */
15719 
15720 static void
15721 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay,
15722     void (*statp)(kstat_io_t *))
15723 {
15724         ASSERT(un != NULL);
15725         ASSERT(mutex_owned(SD_MUTEX(un)));
15726         ASSERT(bp != NULL);
15727 
15728         SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15729             "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp);
15730 
15731         /*
15732          * Indicate that the command is being retried. This will not allow any
15733          * other commands on the wait queue to be transported to the target
15734          * until this command has been completed (success or failure). The
15735          * "retry command" is not transported to the target until the given
15736          * time delay expires, unless the user specified a 0 retry_delay.
15737          *
15738          * Note: the timeout(9F) callback routine is what actually calls
15739          * sd_start_cmds() to transport the command, with the exception of a
15740          * zero retry_delay. The only current implementor of a zero retry delay
15741          * is the case where a START_STOP_UNIT is sent to spin-up a device.
15742          */
15743         if (un->un_retry_bp == NULL) {
15744                 ASSERT(un->un_retry_statp == NULL);
15745                 un->un_retry_bp = bp;
15746 
15747                 /*
15748                  * If the user has not specified a delay the command should
15749                  * be queued and no timeout should be scheduled.
15750                  */
15751                 if (retry_delay == 0) {
15752                         /*
15753                          * Save the kstat pointer that will be used in the
15754                          * call to SD_UPDATE_KSTATS() below, so that
15755                          * sd_start_cmds() can correctly decrement the waitq
15756                          * count when it is time to transport this command.
15757                          */
15758                         un->un_retry_statp = statp;
15759                         goto done;
15760                 }
15761         }
15762 
15763         if (un->un_retry_bp == bp) {
15764                 /*
15765                  * Save the kstat pointer that will be used in the call to
15766                  * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can
15767                  * correctly decrement the waitq count when it is time to
15768                  * transport this command.
15769                  */
15770                 un->un_retry_statp = statp;
15771 
15772                 /*
15773                  * Schedule a timeout if:
15774                  *   1) The user has specified a delay.
15775                  *   2) There is not a START_STOP_UNIT callback pending.
15776                  *
15777                  * If no delay has been specified, then it is up to the caller
15778                  * to ensure that IO processing continues without stalling.
15779                  * Effectively, this means that the caller will issue the
15780                  * required call to sd_start_cmds(). The START_STOP_UNIT
15781                  * callback does this after the START STOP UNIT command has
15782                  * completed. In either of these cases we should not schedule
15783                  * a timeout callback here.  Also don't schedule the timeout if
15784                  * an SD_PATH_DIRECT_PRIORITY command is waiting to restart.
15785                  */
15786                 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) &&
15787                     (un->un_direct_priority_timeid == NULL)) {
15788                         un->un_retry_timeid =
15789                             timeout(sd_start_retry_command, un, retry_delay);
15790                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15791                             "sd_set_retry_bp: setting timeout: un: 0x%p"
15792                             " bp:0x%p un_retry_timeid:0x%p\n",
15793                             un, bp, un->un_retry_timeid);
15794                 }
15795         } else {
15796                 /*
15797                  * We only get in here if there is already another command
15798                  * waiting to be retried.  In this case, we just put the
15799                  * given command onto the wait queue, so it can be transported
15800                  * after the current retry command has completed.
15801                  *
15802                  * Also we have to make sure that if the command at the head
15803                  * of the wait queue is the un_failfast_bp, that we do not
15804                  * put ahead of it any other commands that are to be retried.
15805                  */
15806                 if ((un->un_failfast_bp != NULL) &&
15807                     (un->un_failfast_bp == un->un_waitq_headp)) {
15808                         /*
15809                          * Enqueue this command AFTER the first command on
15810                          * the wait queue (which is also un_failfast_bp).
15811                          */
15812                         bp->av_forw = un->un_waitq_headp->av_forw;
15813                         un->un_waitq_headp->av_forw = bp;
15814                         if (un->un_waitq_headp == un->un_waitq_tailp) {
15815                                 un->un_waitq_tailp = bp;
15816                         }
15817                 } else {
15818                         /* Enqueue this command at the head of the waitq. */
15819                         bp->av_forw = un->un_waitq_headp;
15820                         un->un_waitq_headp = bp;
15821                         if (un->un_waitq_tailp == NULL) {
15822                                 un->un_waitq_tailp = bp;
15823                         }
15824                 }
15825 
15826                 if (statp == NULL) {
15827                         statp = kstat_waitq_enter;
15828                 }
15829                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15830                     "sd_set_retry_bp: un:0x%p already delayed retry\n", un);
15831         }
15832 
15833 done:
15834         if (statp != NULL) {
15835                 SD_UPDATE_KSTATS(un, statp, bp);
15836         }
15837 
15838         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15839             "sd_set_retry_bp: exit un:0x%p\n", un);
15840 }
15841 
15842 
15843 /*
15844  *    Function: sd_start_retry_command
15845  *
15846  * Description: Start the command that has been waiting on the target's
15847  *              retry queue.  Called from timeout(9F) context after the
15848  *              retry delay interval has expired.
15849  *
15850  *   Arguments: arg - pointer to associated softstate for the device.
15851  *
15852  *     Context: timeout(9F) thread context.  May not sleep.
15853  */
15854 
15855 static void
15856 sd_start_retry_command(void *arg)
15857 {
15858         struct sd_lun *un = arg;
15859 
15860         ASSERT(un != NULL);
15861         ASSERT(!mutex_owned(SD_MUTEX(un)));
15862 
15863         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15864             "sd_start_retry_command: entry\n");
15865 
15866         mutex_enter(SD_MUTEX(un));
15867 
15868         un->un_retry_timeid = NULL;
15869 
15870         if (un->un_retry_bp != NULL) {
15871                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15872                     "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n",
15873                     un, un->un_retry_bp);
15874                 sd_start_cmds(un, un->un_retry_bp);
15875         }
15876 
15877         mutex_exit(SD_MUTEX(un));
15878 
15879         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15880             "sd_start_retry_command: exit\n");
15881 }
15882 
15883 /*
15884  *    Function: sd_rmw_msg_print_handler
15885  *
15886  * Description: If RMW mode is enabled and warning message is triggered
15887  *              print I/O count during a fixed interval.
15888  *
15889  *   Arguments: arg - pointer to associated softstate for the device.
15890  *
15891  *     Context: timeout(9F) thread context. May not sleep.
15892  */
15893 static void
15894 sd_rmw_msg_print_handler(void *arg)
15895 {
15896         struct sd_lun *un = arg;
15897 
15898         ASSERT(un != NULL);
15899         ASSERT(!mutex_owned(SD_MUTEX(un)));
15900 
15901         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15902             "sd_rmw_msg_print_handler: entry\n");
15903 
15904         mutex_enter(SD_MUTEX(un));
15905 
15906         if (un->un_rmw_incre_count > 0) {
15907                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
15908                     "%"PRIu64" I/O requests are not aligned with %d disk "
15909                     "sector size in %ld seconds. They are handled through "
15910                     "Read Modify Write but the performance is very low!\n",
15911                     un->un_rmw_incre_count, un->un_tgt_blocksize,
15912                     drv_hztousec(SD_RMW_MSG_PRINT_TIMEOUT) / 1000000);
15913                 un->un_rmw_incre_count = 0;
15914                 un->un_rmw_msg_timeid = timeout(sd_rmw_msg_print_handler,
15915                     un, SD_RMW_MSG_PRINT_TIMEOUT);
15916         } else {
15917                 un->un_rmw_msg_timeid = NULL;
15918         }
15919 
15920         mutex_exit(SD_MUTEX(un));
15921 
15922         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15923             "sd_rmw_msg_print_handler: exit\n");
15924 }
15925 
15926 /*
15927  *    Function: sd_start_direct_priority_command
15928  *
15929  * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had
15930  *              received TRAN_BUSY when we called scsi_transport() to send it
15931  *              to the underlying HBA. This function is called from timeout(9F)
15932  *              context after the delay interval has expired.
15933  *
15934  *   Arguments: arg - pointer to associated buf(9S) to be restarted.
15935  *
15936  *     Context: timeout(9F) thread context.  May not sleep.
15937  */
15938 
15939 static void
15940 sd_start_direct_priority_command(void *arg)
15941 {
15942         struct buf      *priority_bp = arg;
15943         struct sd_lun   *un;
15944 
15945         ASSERT(priority_bp != NULL);
15946         un = SD_GET_UN(priority_bp);
15947         ASSERT(un != NULL);
15948         ASSERT(!mutex_owned(SD_MUTEX(un)));
15949 
15950         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15951             "sd_start_direct_priority_command: entry\n");
15952 
15953         mutex_enter(SD_MUTEX(un));
15954         un->un_direct_priority_timeid = NULL;
15955         sd_start_cmds(un, priority_bp);
15956         mutex_exit(SD_MUTEX(un));
15957 
15958         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15959             "sd_start_direct_priority_command: exit\n");
15960 }
15961 
15962 
15963 /*
15964  *    Function: sd_send_request_sense_command
15965  *
15966  * Description: Sends a REQUEST SENSE command to the target
15967  *
15968  *     Context: May be called from interrupt context.
15969  */
15970 
15971 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
15972     int retry_check_flag, struct scsi_pkt *pktp)
15973 {
15974         ASSERT(bp != NULL);
15975         ASSERT(un != NULL);
15976         ASSERT(mutex_owned(SD_MUTEX(un)));
15977 
15978         SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: "
15979             "entry: buf:0x%p\n", bp);
15980 
15981         /*
15982          * If we are syncing or dumping, then fail the command to avoid a
15983          * recursive callback into scsi_transport(). Also fail the command
15984          * if we are suspended (legacy behavior).
15985          */
15986         if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
15987             (un->un_state == SD_STATE_DUMPING)) {
15988                 sd_return_failed_command(un, bp, EIO);
15989                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15990                     "sd_send_request_sense_command: syncing/dumping, exit\n");
15991                 return;
15992         }
15993 
15994         /*
15995          * Retry the failed command and don't issue the request sense if:
15996          *    1) the sense buf is busy
15997          *    2) we have 1 or more outstanding commands on the target
15998          *    (the sense data will be cleared or invalidated any way)
15999          */
16000         if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) {
16001                 /* Don't retry if the command is flagged as non-retryable */
16002                 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16003                         sd_retry_command(un, bp, retry_check_flag,
16004                             NULL, NULL, 0, un->un_busy_timeout,
16005                             kstat_waitq_enter);
16006                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16007                             "sd_send_request_sense_command: "
16008                             "at full throttle, retrying exit\n");
16009                 } else {
16010                         sd_return_failed_command(un, bp, EIO);
16011                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16012                             "sd_send_request_sense_command: "
16013                             "at full throttle, non-retryable exit\n");
16014                 }
16015                 return;
16016         }
16017 
16018         sd_mark_rqs_busy(un, bp);
16019         sd_start_cmds(un, un->un_rqs_bp);
16020 
16021         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16022             "sd_send_request_sense_command: exit\n");
16023 }
16024 
16025 
16026 /*
16027  *    Function: sd_mark_rqs_busy
16028  *
16029  * Description: Indicate that the request sense bp for this instance is
16030  *              in use.
16031  *
16032  *     Context: May be called under interrupt context
16033  */
16034 
16035 static void
16036 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp)
16037 {
16038         struct sd_xbuf  *sense_xp;
16039 
16040         ASSERT(un != NULL);
16041         ASSERT(bp != NULL);
16042         ASSERT(mutex_owned(SD_MUTEX(un)));
16043         ASSERT(un->un_sense_isbusy == 0);
16044 
16045         SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: "
16046             "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un);
16047 
16048         sense_xp = SD_GET_XBUF(un->un_rqs_bp);
16049         ASSERT(sense_xp != NULL);
16050 
16051         SD_INFO(SD_LOG_IO, un,
16052             "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp);
16053 
16054         ASSERT(sense_xp->xb_pktp != NULL);
16055         ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD))
16056             == (FLAG_SENSING | FLAG_HEAD));
16057 
16058         un->un_sense_isbusy = 1;
16059         un->un_rqs_bp->b_resid = 0;
16060         sense_xp->xb_pktp->pkt_resid  = 0;
16061         sense_xp->xb_pktp->pkt_reason = 0;
16062 
16063         /* So we can get back the bp at interrupt time! */
16064         sense_xp->xb_sense_bp = bp;
16065 
16066         bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH);
16067 
16068         /*
16069          * Mark this buf as awaiting sense data. (This is already set in
16070          * the pkt_flags for the RQS packet.)
16071          */
16072         ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING;
16073 
16074         /* Request sense down same path */
16075         if (scsi_pkt_allocated_correctly((SD_GET_XBUF(bp))->xb_pktp) &&
16076             ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance)
16077                 sense_xp->xb_pktp->pkt_path_instance =
16078                     ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance;
16079 
16080         sense_xp->xb_retry_count     = 0;
16081         sense_xp->xb_victim_retry_count = 0;
16082         sense_xp->xb_ua_retry_count  = 0;
16083         sense_xp->xb_nr_retry_count  = 0;
16084         sense_xp->xb_dma_resid  = 0;
16085 
16086         /* Clean up the fields for auto-request sense */
16087         sense_xp->xb_sense_status = 0;
16088         sense_xp->xb_sense_state  = 0;
16089         sense_xp->xb_sense_resid  = 0;
16090         bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data));
16091 
16092         SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n");
16093 }
16094 
16095 
16096 /*
16097  *    Function: sd_mark_rqs_idle
16098  *
16099  * Description: SD_MUTEX must be held continuously through this routine
16100  *              to prevent reuse of the rqs struct before the caller can
16101  *              complete it's processing.
16102  *
16103  * Return Code: Pointer to the RQS buf
16104  *
16105  *     Context: May be called under interrupt context
16106  */
16107 
16108 static struct buf *
16109 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp)
16110 {
16111         struct buf *bp;
16112         ASSERT(un != NULL);
16113         ASSERT(sense_xp != NULL);
16114         ASSERT(mutex_owned(SD_MUTEX(un)));
16115         ASSERT(un->un_sense_isbusy != 0);
16116 
16117         un->un_sense_isbusy = 0;
16118         bp = sense_xp->xb_sense_bp;
16119         sense_xp->xb_sense_bp = NULL;
16120 
16121         /* This pkt is no longer interested in getting sense data */
16122         ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING;
16123 
16124         return (bp);
16125 }
16126 
16127 
16128 
16129 /*
16130  *    Function: sd_alloc_rqs
16131  *
16132  * Description: Set up the unit to receive auto request sense data
16133  *
16134  * Return Code: DDI_SUCCESS or DDI_FAILURE
16135  *
16136  *     Context: Called under attach(9E) context
16137  */
16138 
16139 static int
16140 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un)
16141 {
16142         struct sd_xbuf *xp;
16143 
16144         ASSERT(un != NULL);
16145         ASSERT(!mutex_owned(SD_MUTEX(un)));
16146         ASSERT(un->un_rqs_bp == NULL);
16147         ASSERT(un->un_rqs_pktp == NULL);
16148 
16149         /*
16150          * First allocate the required buf and scsi_pkt structs, then set up
16151          * the CDB in the scsi_pkt for a REQUEST SENSE command.
16152          */
16153         un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL,
16154             MAX_SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL);
16155         if (un->un_rqs_bp == NULL) {
16156                 return (DDI_FAILURE);
16157         }
16158 
16159         un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp,
16160             CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL);
16161 
16162         if (un->un_rqs_pktp == NULL) {
16163                 sd_free_rqs(un);
16164                 return (DDI_FAILURE);
16165         }
16166 
16167         /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */
16168         (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp,
16169             SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0);
16170 
16171         SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp);
16172 
16173         /* Set up the other needed members in the ARQ scsi_pkt. */
16174         un->un_rqs_pktp->pkt_comp = sdintr;
16175         un->un_rqs_pktp->pkt_time = ((ISCD(un)) ? 2 : 1) *
16176             (ushort_t)un->un_io_time;
16177         un->un_rqs_pktp->pkt_flags |= (FLAG_SENSING | FLAG_HEAD);
16178 
16179         /*
16180          * Allocate  & init the sd_xbuf struct for the RQS command. Do not
16181          * provide any intpkt, destroypkt routines as we take care of
16182          * scsi_pkt allocation/freeing here and in sd_free_rqs().
16183          */
16184         xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
16185         sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL);
16186         xp->xb_pktp = un->un_rqs_pktp;
16187         SD_INFO(SD_LOG_ATTACH_DETACH, un,
16188             "sd_alloc_rqs: un 0x%p, rqs  xp 0x%p,  pkt 0x%p,  buf 0x%p\n",
16189             un, xp, un->un_rqs_pktp, un->un_rqs_bp);
16190 
16191         /*
16192          * Save the pointer to the request sense private bp so it can
16193          * be retrieved in sdintr.
16194          */
16195         un->un_rqs_pktp->pkt_private = un->un_rqs_bp;
16196         ASSERT(un->un_rqs_bp->b_private == xp);
16197 
16198         /*
16199          * See if the HBA supports auto-request sense for the specified
16200          * target/lun. If it does, then try to enable it (if not already
16201          * enabled).
16202          *
16203          * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return
16204          * failure, while for other HBAs (pln) scsi_ifsetcap will always
16205          * return success.  However, in both of these cases ARQ is always
16206          * enabled and scsi_ifgetcap will always return true. The best approach
16207          * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap().
16208          *
16209          * The 3rd case is the HBA (adp) always return enabled on
16210          * scsi_ifgetgetcap even when it's not enable, the best approach
16211          * is issue a scsi_ifsetcap then a scsi_ifgetcap
16212          */
16213 
16214         if (un->un_f_is_fibre == TRUE) {
16215                 un->un_f_arq_enabled = TRUE;
16216         } else {
16217                 /*
16218                  * XXX Circumvent the Adaptec bug, remove this code when
16219                  * the bug is fixed.
16220                  */
16221                 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1);
16222                 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) {
16223                 case 0:
16224                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
16225                             "sd_alloc_rqs: HBA supports ARQ\n");
16226                         /*
16227                          * ARQ is supported by this HBA but currently is not
16228                          * enabled. Attempt to enable it and if successful then
16229                          * mark this instance as ARQ enabled.
16230                          */
16231                         if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1)
16232                             == 1) {
16233                                 /* Successfully enabled ARQ in the HBA */
16234                                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16235                                     "sd_alloc_rqs: ARQ enabled\n");
16236                                 un->un_f_arq_enabled = TRUE;
16237                         } else {
16238                                 /* Could not enable ARQ in the HBA */
16239                                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16240                                     "sd_alloc_rqs: failed ARQ enable\n");
16241                                 un->un_f_arq_enabled = FALSE;
16242                         }
16243                         break;
16244                 case 1:
16245                         /*
16246                          * ARQ is supported by this HBA and is already enabled.
16247                          * Just mark ARQ as enabled for this instance.
16248                          */
16249                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
16250                             "sd_alloc_rqs: ARQ already enabled\n");
16251                         un->un_f_arq_enabled = TRUE;
16252                         break;
16253                 default:
16254                         /*
16255                          * ARQ is not supported by this HBA; disable it for this
16256                          * instance.
16257                          */
16258                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
16259                             "sd_alloc_rqs: HBA does not support ARQ\n");
16260                         un->un_f_arq_enabled = FALSE;
16261                         break;
16262                 }
16263         }
16264 
16265         return (DDI_SUCCESS);
16266 }
16267 
16268 
16269 /*
16270  *    Function: sd_free_rqs
16271  *
16272  * Description: Cleanup for the pre-instance RQS command.
16273  *
16274  *     Context: Kernel thread context
16275  */
16276 
16277 static void
16278 sd_free_rqs(struct sd_lun *un)
16279 {
16280         ASSERT(un != NULL);
16281 
16282         SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n");
16283 
16284         /*
16285          * If consistent memory is bound to a scsi_pkt, the pkt
16286          * has to be destroyed *before* freeing the consistent memory.
16287          * Don't change the sequence of this operations.
16288          * scsi_destroy_pkt() might access memory, which isn't allowed,
16289          * after it was freed in scsi_free_consistent_buf().
16290          */
16291         if (un->un_rqs_pktp != NULL) {
16292                 scsi_destroy_pkt(un->un_rqs_pktp);
16293                 un->un_rqs_pktp = NULL;
16294         }
16295 
16296         if (un->un_rqs_bp != NULL) {
16297                 struct sd_xbuf *xp = SD_GET_XBUF(un->un_rqs_bp);
16298                 if (xp != NULL) {
16299                         kmem_free(xp, sizeof (struct sd_xbuf));
16300                 }
16301                 scsi_free_consistent_buf(un->un_rqs_bp);
16302                 un->un_rqs_bp = NULL;
16303         }
16304         SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n");
16305 }
16306 
16307 
16308 
16309 /*
16310  *    Function: sd_reduce_throttle
16311  *
16312  * Description: Reduces the maximum # of outstanding commands on a
16313  *              target to the current number of outstanding commands.
16314  *              Queues a tiemout(9F) callback to restore the limit
16315  *              after a specified interval has elapsed.
16316  *              Typically used when we get a TRAN_BUSY return code
16317  *              back from scsi_transport().
16318  *
16319  *   Arguments: un - ptr to the sd_lun softstate struct
16320  *              throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL
16321  *
16322  *     Context: May be called from interrupt context
16323  */
16324 
16325 static void
16326 sd_reduce_throttle(struct sd_lun *un, int throttle_type)
16327 {
16328         ASSERT(un != NULL);
16329         ASSERT(mutex_owned(SD_MUTEX(un)));
16330         ASSERT(un->un_ncmds_in_transport >= 0);
16331 
16332         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16333             "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n",
16334             un, un->un_throttle, un->un_ncmds_in_transport);
16335 
16336         if (un->un_throttle > 1) {
16337                 if (un->un_f_use_adaptive_throttle == TRUE) {
16338                         switch (throttle_type) {
16339                         case SD_THROTTLE_TRAN_BUSY:
16340                                 if (un->un_busy_throttle == 0) {
16341                                         un->un_busy_throttle = un->un_throttle;
16342                                 }
16343                                 break;
16344                         case SD_THROTTLE_QFULL:
16345                                 un->un_busy_throttle = 0;
16346                                 break;
16347                         default:
16348                                 ASSERT(FALSE);
16349                         }
16350 
16351                         if (un->un_ncmds_in_transport > 0) {
16352                                 un->un_throttle = un->un_ncmds_in_transport;
16353                         }
16354 
16355                 } else {
16356                         if (un->un_ncmds_in_transport == 0) {
16357                                 un->un_throttle = 1;
16358                         } else {
16359                                 un->un_throttle = un->un_ncmds_in_transport;
16360                         }
16361                 }
16362         }
16363 
16364         /* Reschedule the timeout if none is currently active */
16365         if (un->un_reset_throttle_timeid == NULL) {
16366                 un->un_reset_throttle_timeid = timeout(sd_restore_throttle,
16367                     un, SD_THROTTLE_RESET_INTERVAL);
16368                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16369                     "sd_reduce_throttle: timeout scheduled!\n");
16370         }
16371 
16372         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16373             "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16374 }
16375 
16376 
16377 
16378 /*
16379  *    Function: sd_restore_throttle
16380  *
16381  * Description: Callback function for timeout(9F).  Resets the current
16382  *              value of un->un_throttle to its default.
16383  *
16384  *   Arguments: arg - pointer to associated softstate for the device.
16385  *
16386  *     Context: May be called from interrupt context
16387  */
16388 
16389 static void
16390 sd_restore_throttle(void *arg)
16391 {
16392         struct sd_lun   *un = arg;
16393 
16394         ASSERT(un != NULL);
16395         ASSERT(!mutex_owned(SD_MUTEX(un)));
16396 
16397         mutex_enter(SD_MUTEX(un));
16398 
16399         SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16400             "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16401 
16402         un->un_reset_throttle_timeid = NULL;
16403 
16404         if (un->un_f_use_adaptive_throttle == TRUE) {
16405                 /*
16406                  * If un_busy_throttle is nonzero, then it contains the
16407                  * value that un_throttle was when we got a TRAN_BUSY back
16408                  * from scsi_transport(). We want to revert back to this
16409                  * value.
16410                  *
16411                  * In the QFULL case, the throttle limit will incrementally
16412                  * increase until it reaches max throttle.
16413                  */
16414                 if (un->un_busy_throttle > 0) {
16415                         un->un_throttle = un->un_busy_throttle;
16416                         un->un_busy_throttle = 0;
16417                 } else {
16418                         /*
16419                          * increase throttle by 10% open gate slowly, schedule
16420                          * another restore if saved throttle has not been
16421                          * reached
16422                          */
16423                         short throttle;
16424                         if (sd_qfull_throttle_enable) {
16425                                 throttle = un->un_throttle +
16426                                     max((un->un_throttle / 10), 1);
16427                                 un->un_throttle =
16428                                     (throttle < un->un_saved_throttle) ?
16429                                     throttle : un->un_saved_throttle;
16430                                 if (un->un_throttle < un->un_saved_throttle) {
16431                                         un->un_reset_throttle_timeid =
16432                                             timeout(sd_restore_throttle,
16433                                             un,
16434                                             SD_QFULL_THROTTLE_RESET_INTERVAL);
16435                                 }
16436                         }
16437                 }
16438 
16439                 /*
16440                  * If un_throttle has fallen below the low-water mark, we
16441                  * restore the maximum value here (and allow it to ratchet
16442                  * down again if necessary).
16443                  */
16444                 if (un->un_throttle < un->un_min_throttle) {
16445                         un->un_throttle = un->un_saved_throttle;
16446                 }
16447         } else {
16448                 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16449                     "restoring limit from 0x%x to 0x%x\n",
16450                     un->un_throttle, un->un_saved_throttle);
16451                 un->un_throttle = un->un_saved_throttle;
16452         }
16453 
16454         SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16455             "sd_restore_throttle: calling sd_start_cmds!\n");
16456 
16457         sd_start_cmds(un, NULL);
16458 
16459         SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16460             "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n",
16461             un, un->un_throttle);
16462 
16463         mutex_exit(SD_MUTEX(un));
16464 
16465         SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n");
16466 }
16467 
16468 /*
16469  *    Function: sdrunout
16470  *
16471  * Description: Callback routine for scsi_init_pkt when a resource allocation
16472  *              fails.
16473  *
16474  *   Arguments: arg - a pointer to the sd_lun unit struct for the particular
16475  *              soft state instance.
16476  *
16477  * Return Code: The scsi_init_pkt routine allows for the callback function to
16478  *              return a 0 indicating the callback should be rescheduled or a 1
16479  *              indicating not to reschedule. This routine always returns 1
16480  *              because the driver always provides a callback function to
16481  *              scsi_init_pkt. This results in a callback always being scheduled
16482  *              (via the scsi_init_pkt callback implementation) if a resource
16483  *              failure occurs.
16484  *
16485  *     Context: This callback function may not block or call routines that block
16486  *
16487  *        Note: Using the scsi_init_pkt callback facility can result in an I/O
16488  *              request persisting at the head of the list which cannot be
16489  *              satisfied even after multiple retries. In the future the driver
16490  *              may implement some time of maximum runout count before failing
16491  *              an I/O.
16492  */
16493 
16494 static int
16495 sdrunout(caddr_t arg)
16496 {
16497         struct sd_lun   *un = (struct sd_lun *)arg;
16498 
16499         ASSERT(un != NULL);
16500         ASSERT(!mutex_owned(SD_MUTEX(un)));
16501 
16502         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n");
16503 
16504         mutex_enter(SD_MUTEX(un));
16505         sd_start_cmds(un, NULL);
16506         mutex_exit(SD_MUTEX(un));
16507         /*
16508          * This callback routine always returns 1 (i.e. do not reschedule)
16509          * because we always specify sdrunout as the callback handler for
16510          * scsi_init_pkt inside the call to sd_start_cmds.
16511          */
16512         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n");
16513         return (1);
16514 }
16515 
16516 static void
16517 sd_slow_io_ereport(struct scsi_pkt *pktp)
16518 {
16519         struct buf *bp;
16520         struct sd_lun *un;
16521         char *devid;
16522 
16523         ASSERT(pktp != NULL);
16524         bp = (struct buf *)pktp->pkt_private;
16525         ASSERT(bp != NULL);
16526         un = SD_GET_UN(bp);
16527         ASSERT(un != NULL);
16528 
16529         SD_ERROR(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16530             "Slow IO detected SD: 0x%p delta in nsec: %llu",
16531             (void *)un, pktp->pkt_stop - pktp->pkt_start);
16532 
16533         devid = DEVI(un->un_sd->sd_dev)->devi_devid_str;
16534         scsi_fm_ereport_post(un->un_sd, 0, NULL, "cmd.disk.slow-io",
16535             fm_ena_generate(0, FM_ENA_FMT1), devid, NULL, DDI_NOSLEEP, NULL,
16536             FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
16537             "start", DATA_TYPE_UINT64, pktp->pkt_start,
16538             "stop", DATA_TYPE_UINT64, pktp->pkt_stop,
16539             "delta", DATA_TYPE_UINT64, pktp->pkt_stop - pktp->pkt_start,
16540             "threshold", DATA_TYPE_UINT64, un->un_slow_io_threshold,
16541             "pkt-reason", DATA_TYPE_UINT32, pktp->pkt_reason,
16542             NULL);
16543 }
16544 
16545 /* Clamp the value between 0..max using min as the offset */
16546 static int
16547 clamp_lat(int bucket, int min, int max)
16548 {
16549 
16550         if (max < bucket)
16551                 bucket = max;
16552         if (min > bucket)
16553                 bucket = min;
16554 
16555         return (bucket - min);
16556 }
16557 
16558 /*
16559  *    Function: sdintr
16560  *
16561  * Description: Completion callback routine for scsi_pkt(9S) structs
16562  *              sent to the HBA driver via scsi_transport(9F).
16563  *
16564  *     Context: Interrupt context
16565  */
16566 
16567 static void
16568 sdintr(struct scsi_pkt *pktp)
16569 {
16570         struct buf      *bp;
16571         struct sd_xbuf  *xp;
16572         struct sd_lun   *un;
16573         size_t          actual_len;
16574         sd_ssc_t        *sscp;
16575         hrtime_t        io_delta = 0LL;
16576         int             bucket;
16577 
16578         ASSERT(pktp != NULL);
16579         bp = (struct buf *)pktp->pkt_private;
16580         ASSERT(bp != NULL);
16581         xp = SD_GET_XBUF(bp);
16582         ASSERT(xp != NULL);
16583         ASSERT(xp->xb_pktp != NULL);
16584         un = SD_GET_UN(bp);
16585         ASSERT(un != NULL);
16586         ASSERT(!mutex_owned(SD_MUTEX(un)));
16587 
16588 #ifdef SD_FAULT_INJECTION
16589 
16590         SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n");
16591         /* SD FaultInjection */
16592         sd_faultinjection(pktp);
16593 
16594 #endif /* SD_FAULT_INJECTION */
16595 
16596         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p,"
16597             " xp:0x%p, un:0x%p\n", bp, xp, un);
16598 
16599         mutex_enter(SD_MUTEX(un));
16600 
16601         ASSERT(un->un_fm_private != NULL);
16602         sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
16603         ASSERT(sscp != NULL);
16604 
16605         /* Reduce the count of the #commands currently in transport */
16606         un->un_ncmds_in_transport--;
16607         ASSERT(un->un_ncmds_in_transport >= 0);
16608 
16609         /* Increment counter to indicate that the callback routine is active */
16610         un->un_in_callback++;
16611 
16612         SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
16613         /* If the HBA driver did not set the stop time, set it now. */
16614         if (pktp->pkt_stop == 0)
16615                 pktp->pkt_stop = gethrtime();
16616         /*
16617          * If there are HBA drivers or layered drivers which do not participate
16618          * in slow-io diagnosis, the start time, set above may be overwritten
16619          * with zero. If pkt_start is zero, the delta should also be zero.
16620          */
16621         if (pktp->pkt_start != 0)
16622                 io_delta = pktp->pkt_stop - pktp->pkt_start;
16623         if (un->un_slow_io_threshold > 0 && io_delta > un->un_slow_io_threshold)
16624                 sd_slow_io_ereport(pktp);
16625         if (un->un_lat_stats) {
16626                 un->un_lat_stats->l_nrequest++;
16627                 un->un_lat_stats->l_sum += io_delta;
16628 
16629                 /* Track the latency in usec and quantize by power of 2 */
16630                 bucket = clamp_lat(ddi_fls(io_delta / 1000),
16631                     SD_LAT_MIN_USEC_SHIFT, SD_LAT_MAX_USEC_SHIFT - 1);
16632                 ASSERT3S(bucket, >=, 0);
16633                 ASSERT3S(bucket, <, ARRAY_SIZE(un->un_lat_stats->l_histogram));
16634                 un->un_lat_stats->l_histogram[bucket]++;
16635         }
16636 
16637 #ifdef  SDDEBUG
16638         if (bp == un->un_retry_bp) {
16639                 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: "
16640                     "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n",
16641                     un, un->un_retry_bp, un->un_ncmds_in_transport);
16642         }
16643 #endif
16644 
16645         /*
16646          * If pkt_reason is CMD_DEV_GONE, fail the command, and update the media
16647          * state if needed.
16648          */
16649         if (pktp->pkt_reason == CMD_DEV_GONE) {
16650                 /* Prevent multiple console messages for the same failure. */
16651                 if (un->un_last_pkt_reason != CMD_DEV_GONE) {
16652                         un->un_last_pkt_reason = CMD_DEV_GONE;
16653                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16654                             "Command failed to complete...Device is gone\n");
16655                 }
16656                 if (un->un_mediastate != DKIO_DEV_GONE) {
16657                         un->un_mediastate = DKIO_DEV_GONE;
16658                         cv_broadcast(&un->un_state_cv);
16659                 }
16660                 /*
16661                  * If the command happens to be the REQUEST SENSE command,
16662                  * free up the rqs buf and fail the original command.
16663                  */
16664                 if (bp == un->un_rqs_bp) {
16665                         bp = sd_mark_rqs_idle(un, xp);
16666                 }
16667                 sd_return_failed_command(un, bp, EIO);
16668                 goto exit;
16669         }
16670 
16671         if (pktp->pkt_state & STATE_XARQ_DONE) {
16672                 SD_TRACE(SD_LOG_COMMON, un,
16673                     "sdintr: extra sense data received. pkt=%p\n", pktp);
16674         }
16675 
16676         /*
16677          * First see if the pkt has auto-request sense data with it....
16678          * Look at the packet state first so we don't take a performance
16679          * hit looking at the arq enabled flag unless absolutely necessary.
16680          */
16681         if ((pktp->pkt_state & STATE_ARQ_DONE) &&
16682             (un->un_f_arq_enabled == TRUE)) {
16683                 /*
16684                  * The HBA did an auto request sense for this command so check
16685                  * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16686                  * driver command that should not be retried.
16687                  */
16688                 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16689                         /*
16690                          * Save the relevant sense info into the xp for the
16691                          * original cmd.
16692                          */
16693                         struct scsi_arq_status *asp;
16694                         asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
16695                         xp->xb_sense_status =
16696                             *((uchar_t *)(&(asp->sts_rqpkt_status)));
16697                         xp->xb_sense_state  = asp->sts_rqpkt_state;
16698                         xp->xb_sense_resid  = asp->sts_rqpkt_resid;
16699                         if (pktp->pkt_state & STATE_XARQ_DONE) {
16700                                 actual_len = MAX_SENSE_LENGTH -
16701                                     xp->xb_sense_resid;
16702                                 bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16703                                     MAX_SENSE_LENGTH);
16704                         } else {
16705                                 if (xp->xb_sense_resid > SENSE_LENGTH) {
16706                                         actual_len = MAX_SENSE_LENGTH -
16707                                             xp->xb_sense_resid;
16708                                 } else {
16709                                         actual_len = SENSE_LENGTH -
16710                                             xp->xb_sense_resid;
16711                                 }
16712                                 if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
16713                                         if ((((struct uscsi_cmd *)
16714                                             (xp->xb_pktinfo))->uscsi_rqlen) >
16715                                             actual_len) {
16716                                                 xp->xb_sense_resid =
16717                                                     (((struct uscsi_cmd *)
16718                                                     (xp->xb_pktinfo))->
16719                                                     uscsi_rqlen) - actual_len;
16720                                         } else {
16721                                                 xp->xb_sense_resid = 0;
16722                                         }
16723                                 }
16724                                 bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16725                                     SENSE_LENGTH);
16726                         }
16727 
16728                         /* fail the command */
16729                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16730                             "sdintr: arq done and FLAG_DIAGNOSE set\n");
16731                         sd_return_failed_command(un, bp, EIO);
16732                         goto exit;
16733                 }
16734 
16735                 /*
16736                  * We want to either retry or fail this command, so free
16737                  * the DMA resources here.  If we retry the command then
16738                  * the DMA resources will be reallocated in sd_start_cmds().
16739                  * Note that when PKT_DMA_PARTIAL is used, this reallocation
16740                  * causes the *entire* transfer to start over again from the
16741                  * beginning of the request, even for PARTIAL chunks that
16742                  * have already transferred successfully.
16743                  */
16744                 if ((un->un_f_is_fibre == TRUE) &&
16745                     ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16746                     ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
16747                         scsi_dmafree(pktp);
16748                         xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16749                 }
16750 
16751                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16752                     "sdintr: arq done, sd_handle_auto_request_sense\n");
16753 
16754                 sd_handle_auto_request_sense(un, bp, xp, pktp);
16755                 goto exit;
16756         }
16757 
16758         /* Next see if this is the REQUEST SENSE pkt for the instance */
16759         if (pktp->pkt_flags & FLAG_SENSING)  {
16760                 /* This pktp is from the unit's REQUEST_SENSE command */
16761                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16762                     "sdintr: sd_handle_request_sense\n");
16763                 sd_handle_request_sense(un, bp, xp, pktp);
16764                 goto exit;
16765         }
16766 
16767         /*
16768          * Check to see if the command successfully completed as requested;
16769          * this is the most common case (and also the hot performance path).
16770          *
16771          * Requirements for successful completion are:
16772          * pkt_reason is CMD_CMPLT and packet status is status good.
16773          * In addition:
16774          * - A residual of zero indicates successful completion no matter what
16775          *   the command is.
16776          * - If the residual is not zero and the command is not a read or
16777          *   write, then it's still defined as successful completion. In other
16778          *   words, if the command is a read or write the residual must be
16779          *   zero for successful completion.
16780          * - If the residual is not zero and the command is a read or
16781          *   write, and it's a USCSICMD, then it's still defined as
16782          *   successful completion.
16783          */
16784         if ((pktp->pkt_reason == CMD_CMPLT) &&
16785             (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) {
16786 
16787                 /*
16788                  * Since this command is returned with a good status, we
16789                  * can reset the count for Sonoma failover.
16790                  */
16791                 un->un_sonoma_failure_count = 0;
16792 
16793                 /*
16794                  * Return all USCSI commands on good status
16795                  */
16796                 if (pktp->pkt_resid == 0) {
16797                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16798                             "sdintr: returning command for resid == 0\n");
16799                 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) &&
16800                     ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) {
16801                         SD_UPDATE_B_RESID(bp, pktp);
16802                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16803                             "sdintr: returning command for resid != 0\n");
16804                 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
16805                         SD_UPDATE_B_RESID(bp, pktp);
16806                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16807                             "sdintr: returning uscsi command\n");
16808                 } else {
16809                         goto not_successful;
16810                 }
16811                 sd_return_command(un, bp);
16812 
16813                 /*
16814                  * Decrement counter to indicate that the callback routine
16815                  * is done.
16816                  */
16817                 un->un_in_callback--;
16818                 ASSERT(un->un_in_callback >= 0);
16819                 mutex_exit(SD_MUTEX(un));
16820 
16821                 return;
16822         }
16823 
16824 not_successful:
16825         /*
16826          * The following is based upon knowledge of the underlying transport
16827          * and its use of DMA resources.  This code should be removed when
16828          * PKT_DMA_PARTIAL support is taken out of the disk driver in favor
16829          * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf()
16830          * and sd_start_cmds().
16831          *
16832          * Free any DMA resources associated with this command if there
16833          * is a chance it could be retried or enqueued for later retry.
16834          * If we keep the DMA binding then mpxio cannot reissue the
16835          * command on another path whenever a path failure occurs.
16836          *
16837          * Note that when PKT_DMA_PARTIAL is used, free/reallocation
16838          * causes the *entire* transfer to start over again from the
16839          * beginning of the request, even for PARTIAL chunks that
16840          * have already transferred successfully.
16841          *
16842          * This is only done for non-uscsi commands (and also skipped for the
16843          * driver's internal RQS command). Also just do this for Fibre Channel
16844          * devices as these are the only ones that support mpxio.
16845          */
16846         if ((un->un_f_is_fibre == TRUE) &&
16847             ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16848             ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
16849                 scsi_dmafree(pktp);
16850                 xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16851         }
16852 
16853         /*
16854          * The command did not successfully complete as requested so check
16855          * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16856          * driver command that should not be retried so just return. If
16857          * FLAG_DIAGNOSE is not set the error will be processed below.
16858          */
16859         if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16860                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16861                     "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n");
16862                 /*
16863                  * Issue a request sense if a check condition caused the error
16864                  * (we handle the auto request sense case above), otherwise
16865                  * just fail the command.
16866                  */
16867                 if ((pktp->pkt_reason == CMD_CMPLT) &&
16868                     (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) {
16869                         sd_send_request_sense_command(un, bp,
16870                             SD_RETRIES_STANDARD, pktp);
16871                 } else {
16872                         sd_return_failed_command(un, bp, EIO);
16873                 }
16874                 goto exit;
16875         }
16876 
16877         /*
16878          * The command did not successfully complete as requested so process
16879          * the error, retry, and/or attempt recovery.
16880          */
16881         switch (pktp->pkt_reason) {
16882         case CMD_CMPLT:
16883                 switch (SD_GET_PKT_STATUS(pktp)) {
16884                 case STATUS_GOOD:
16885                         /*
16886                          * The command completed successfully with a non-zero
16887                          * residual
16888                          */
16889                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16890                             "sdintr: STATUS_GOOD \n");
16891                         sd_pkt_status_good(un, bp, xp, pktp);
16892                         break;
16893 
16894                 case STATUS_CHECK:
16895                 case STATUS_TERMINATED:
16896                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16897                             "sdintr: STATUS_TERMINATED | STATUS_CHECK\n");
16898                         sd_pkt_status_check_condition(un, bp, xp, pktp);
16899                         break;
16900 
16901                 case STATUS_BUSY:
16902                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16903                             "sdintr: STATUS_BUSY\n");
16904                         sd_pkt_status_busy(un, bp, xp, pktp);
16905                         break;
16906 
16907                 case STATUS_RESERVATION_CONFLICT:
16908                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16909                             "sdintr: STATUS_RESERVATION_CONFLICT\n");
16910                         sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
16911                         break;
16912 
16913                 case STATUS_QFULL:
16914                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16915                             "sdintr: STATUS_QFULL\n");
16916                         sd_pkt_status_qfull(un, bp, xp, pktp);
16917                         break;
16918 
16919                 case STATUS_MET:
16920                 case STATUS_INTERMEDIATE:
16921                 case STATUS_SCSI2:
16922                 case STATUS_INTERMEDIATE_MET:
16923                 case STATUS_ACA_ACTIVE:
16924                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16925                             "Unexpected SCSI status received: 0x%x\n",
16926                             SD_GET_PKT_STATUS(pktp));
16927                         /*
16928                          * Mark the ssc_flags when detected invalid status
16929                          * code for non-USCSI command.
16930                          */
16931                         if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
16932                                 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
16933                                     0, "stat-code");
16934                         }
16935                         sd_return_failed_command(un, bp, EIO);
16936                         break;
16937 
16938                 default:
16939                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16940                             "Invalid SCSI status received: 0x%x\n",
16941                             SD_GET_PKT_STATUS(pktp));
16942                         if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
16943                                 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
16944                                     0, "stat-code");
16945                         }
16946                         sd_return_failed_command(un, bp, EIO);
16947                         break;
16948 
16949                 }
16950                 break;
16951 
16952         case CMD_INCOMPLETE:
16953                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16954                     "sdintr:  CMD_INCOMPLETE\n");
16955                 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp);
16956                 break;
16957         case CMD_TRAN_ERR:
16958                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16959                     "sdintr: CMD_TRAN_ERR\n");
16960                 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp);
16961                 break;
16962         case CMD_RESET:
16963                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16964                     "sdintr: CMD_RESET \n");
16965                 sd_pkt_reason_cmd_reset(un, bp, xp, pktp);
16966                 break;
16967         case CMD_ABORTED:
16968                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16969                     "sdintr: CMD_ABORTED \n");
16970                 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp);
16971                 break;
16972         case CMD_TIMEOUT:
16973                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16974                     "sdintr: CMD_TIMEOUT\n");
16975                 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp);
16976                 break;
16977         case CMD_UNX_BUS_FREE:
16978                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16979                     "sdintr: CMD_UNX_BUS_FREE \n");
16980                 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp);
16981                 break;
16982         case CMD_TAG_REJECT:
16983                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16984                     "sdintr: CMD_TAG_REJECT\n");
16985                 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp);
16986                 break;
16987         default:
16988                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16989                     "sdintr: default\n");
16990                 /*
16991                  * Mark the ssc_flags for detecting invliad pkt_reason.
16992                  */
16993                 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
16994                         sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_PKT_REASON,
16995                             0, "pkt-reason");
16996                 }
16997                 sd_pkt_reason_default(un, bp, xp, pktp);
16998                 break;
16999         }
17000 
17001 exit:
17002         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n");
17003 
17004         /* Decrement counter to indicate that the callback routine is done. */
17005         un->un_in_callback--;
17006         ASSERT(un->un_in_callback >= 0);
17007 
17008         /*
17009          * At this point, the pkt has been dispatched, ie, it is either
17010          * being re-tried or has been returned to its caller and should
17011          * not be referenced.
17012          */
17013 
17014         mutex_exit(SD_MUTEX(un));
17015 }
17016 
17017 
17018 /*
17019  *    Function: sd_print_incomplete_msg
17020  *
17021  * Description: Prints the error message for a CMD_INCOMPLETE error.
17022  *
17023  *   Arguments: un - ptr to associated softstate for the device.
17024  *              bp - ptr to the buf(9S) for the command.
17025  *              arg - message string ptr
17026  *              code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED,
17027  *                      or SD_NO_RETRY_ISSUED.
17028  *
17029  *     Context: May be called under interrupt context
17030  */
17031 
17032 static void
17033 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17034 {
17035         struct scsi_pkt *pktp;
17036         char    *msgp;
17037         char    *cmdp = arg;
17038 
17039         ASSERT(un != NULL);
17040         ASSERT(mutex_owned(SD_MUTEX(un)));
17041         ASSERT(bp != NULL);
17042         ASSERT(arg != NULL);
17043         pktp = SD_GET_PKTP(bp);
17044         ASSERT(pktp != NULL);
17045 
17046         switch (code) {
17047         case SD_DELAYED_RETRY_ISSUED:
17048         case SD_IMMEDIATE_RETRY_ISSUED:
17049                 msgp = "retrying";
17050                 break;
17051         case SD_NO_RETRY_ISSUED:
17052         default:
17053                 msgp = "giving up";
17054                 break;
17055         }
17056 
17057         if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17058                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17059                     "incomplete %s- %s\n", cmdp, msgp);
17060         }
17061 }
17062 
17063 
17064 
17065 /*
17066  *    Function: sd_pkt_status_good
17067  *
17068  * Description: Processing for a STATUS_GOOD code in pkt_status.
17069  *
17070  *     Context: May be called under interrupt context
17071  */
17072 
17073 static void
17074 sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
17075     struct sd_xbuf *xp, struct scsi_pkt *pktp)
17076 {
17077         char    *cmdp;
17078 
17079         ASSERT(un != NULL);
17080         ASSERT(mutex_owned(SD_MUTEX(un)));
17081         ASSERT(bp != NULL);
17082         ASSERT(xp != NULL);
17083         ASSERT(pktp != NULL);
17084         ASSERT(pktp->pkt_reason == CMD_CMPLT);
17085         ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD);
17086         ASSERT(pktp->pkt_resid != 0);
17087 
17088         SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n");
17089 
17090         SD_UPDATE_ERRSTATS(un, sd_harderrs);
17091         switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) {
17092         case SCMD_READ:
17093                 cmdp = "read";
17094                 break;
17095         case SCMD_WRITE:
17096                 cmdp = "write";
17097                 break;
17098         default:
17099                 SD_UPDATE_B_RESID(bp, pktp);
17100                 sd_return_command(un, bp);
17101                 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17102                 return;
17103         }
17104 
17105         /*
17106          * See if we can retry the read/write, preferrably immediately.
17107          * If retries are exhaused, then sd_retry_command() will update
17108          * the b_resid count.
17109          */
17110         sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg,
17111             cmdp, EIO, (clock_t)0, NULL);
17112 
17113         SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17114 }
17115 
17116 
17117 
17118 
17119 
17120 /*
17121  *    Function: sd_handle_request_sense
17122  *
17123  * Description: Processing for non-auto Request Sense command.
17124  *
17125  *   Arguments: un - ptr to associated softstate
17126  *              sense_bp - ptr to buf(9S) for the RQS command
17127  *              sense_xp - ptr to the sd_xbuf for the RQS command
17128  *              sense_pktp - ptr to the scsi_pkt(9S) for the RQS command
17129  *
17130  *     Context: May be called under interrupt context
17131  */
17132 
17133 static void
17134 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp,
17135     struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp)
17136 {
17137         struct buf      *cmd_bp;        /* buf for the original command */
17138         struct sd_xbuf  *cmd_xp;        /* sd_xbuf for the original command */
17139         struct scsi_pkt *cmd_pktp;      /* pkt for the original command */
17140         size_t          actual_len;     /* actual sense data length */
17141 
17142         ASSERT(un != NULL);
17143         ASSERT(mutex_owned(SD_MUTEX(un)));
17144         ASSERT(sense_bp != NULL);
17145         ASSERT(sense_xp != NULL);
17146         ASSERT(sense_pktp != NULL);
17147 
17148         /*
17149          * Note the sense_bp, sense_xp, and sense_pktp here are for the
17150          * RQS command and not the original command.
17151          */
17152         ASSERT(sense_pktp == un->un_rqs_pktp);
17153         ASSERT(sense_bp   == un->un_rqs_bp);
17154         ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) ==
17155             (FLAG_SENSING | FLAG_HEAD));
17156         ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) &
17157             FLAG_SENSING) == FLAG_SENSING);
17158 
17159         /* These are the bp, xp, and pktp for the original command */
17160         cmd_bp = sense_xp->xb_sense_bp;
17161         cmd_xp = SD_GET_XBUF(cmd_bp);
17162         cmd_pktp = SD_GET_PKTP(cmd_bp);
17163 
17164         if (sense_pktp->pkt_reason != CMD_CMPLT) {
17165                 /*
17166                  * The REQUEST SENSE command failed.  Release the REQUEST
17167                  * SENSE command for re-use, get back the bp for the original
17168                  * command, and attempt to re-try the original command if
17169                  * FLAG_DIAGNOSE is not set in the original packet.
17170                  */
17171                 SD_UPDATE_ERRSTATS(un, sd_harderrs);
17172                 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17173                         cmd_bp = sd_mark_rqs_idle(un, sense_xp);
17174                         sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD,
17175                             NULL, NULL, EIO, (clock_t)0, NULL);
17176                         return;
17177                 }
17178         }
17179 
17180         /*
17181          * Save the relevant sense info into the xp for the original cmd.
17182          *
17183          * Note: if the request sense failed the state info will be zero
17184          * as set in sd_mark_rqs_busy()
17185          */
17186         cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp);
17187         cmd_xp->xb_sense_state  = sense_pktp->pkt_state;
17188         actual_len = MAX_SENSE_LENGTH - sense_pktp->pkt_resid;
17189         if ((cmd_xp->xb_pkt_flags & SD_XB_USCSICMD) &&
17190             (((struct uscsi_cmd *)cmd_xp->xb_pktinfo)->uscsi_rqlen >
17191             SENSE_LENGTH)) {
17192                 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17193                     MAX_SENSE_LENGTH);
17194                 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid;
17195         } else {
17196                 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17197                     SENSE_LENGTH);
17198                 if (actual_len < SENSE_LENGTH) {
17199                         cmd_xp->xb_sense_resid = SENSE_LENGTH - actual_len;
17200                 } else {
17201                         cmd_xp->xb_sense_resid = 0;
17202                 }
17203         }
17204 
17205         /*
17206          *  Free up the RQS command....
17207          *  NOTE:
17208          *      Must do this BEFORE calling sd_validate_sense_data!
17209          *      sd_validate_sense_data may return the original command in
17210          *      which case the pkt will be freed and the flags can no
17211          *      longer be touched.
17212          *      SD_MUTEX is held through this process until the command
17213          *      is dispatched based upon the sense data, so there are
17214          *      no race conditions.
17215          */
17216         (void) sd_mark_rqs_idle(un, sense_xp);
17217 
17218         /*
17219          * For a retryable command see if we have valid sense data, if so then
17220          * turn it over to sd_decode_sense() to figure out the right course of
17221          * action. Just fail a non-retryable command.
17222          */
17223         if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17224                 if (sd_validate_sense_data(un, cmd_bp, cmd_xp, actual_len) ==
17225                     SD_SENSE_DATA_IS_VALID) {
17226                         sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp);
17227                 }
17228         } else {
17229                 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB",
17230                     (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17231                 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data",
17232                     (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
17233                 sd_return_failed_command(un, cmd_bp, EIO);
17234         }
17235 }
17236 
17237 
17238 
17239 
17240 /*
17241  *    Function: sd_handle_auto_request_sense
17242  *
17243  * Description: Processing for auto-request sense information.
17244  *
17245  *   Arguments: un - ptr to associated softstate
17246  *              bp - ptr to buf(9S) for the command
17247  *              xp - ptr to the sd_xbuf for the command
17248  *              pktp - ptr to the scsi_pkt(9S) for the command
17249  *
17250  *     Context: May be called under interrupt context
17251  */
17252 
17253 static void
17254 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
17255     struct sd_xbuf *xp, struct scsi_pkt *pktp)
17256 {
17257         struct scsi_arq_status *asp;
17258         size_t actual_len;
17259 
17260         ASSERT(un != NULL);
17261         ASSERT(mutex_owned(SD_MUTEX(un)));
17262         ASSERT(bp != NULL);
17263         ASSERT(xp != NULL);
17264         ASSERT(pktp != NULL);
17265         ASSERT(pktp != un->un_rqs_pktp);
17266         ASSERT(bp   != un->un_rqs_bp);
17267 
17268         /*
17269          * For auto-request sense, we get a scsi_arq_status back from
17270          * the HBA, with the sense data in the sts_sensedata member.
17271          * The pkt_scbp of the packet points to this scsi_arq_status.
17272          */
17273         asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
17274 
17275         if (asp->sts_rqpkt_reason != CMD_CMPLT) {
17276                 /*
17277                  * The auto REQUEST SENSE failed; see if we can re-try
17278                  * the original command.
17279                  */
17280                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17281                     "auto request sense failed (reason=%s)\n",
17282                     scsi_rname(asp->sts_rqpkt_reason));
17283 
17284                 sd_reset_target(un, pktp);
17285 
17286                 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17287                     NULL, NULL, EIO, (clock_t)0, NULL);
17288                 return;
17289         }
17290 
17291         /* Save the relevant sense info into the xp for the original cmd. */
17292         xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status)));
17293         xp->xb_sense_state  = asp->sts_rqpkt_state;
17294         xp->xb_sense_resid  = asp->sts_rqpkt_resid;
17295         if (xp->xb_sense_state & STATE_XARQ_DONE) {
17296                 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17297                 bcopy(&asp->sts_sensedata, xp->xb_sense_data,
17298                     MAX_SENSE_LENGTH);
17299         } else {
17300                 if (xp->xb_sense_resid > SENSE_LENGTH) {
17301                         actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17302                 } else {
17303                         actual_len = SENSE_LENGTH - xp->xb_sense_resid;
17304                 }
17305                 if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
17306                         if ((((struct uscsi_cmd *)
17307                             (xp->xb_pktinfo))->uscsi_rqlen) > actual_len) {
17308                                 xp->xb_sense_resid = (((struct uscsi_cmd *)
17309                                     (xp->xb_pktinfo))->uscsi_rqlen) -
17310                                     actual_len;
17311                         } else {
17312                                 xp->xb_sense_resid = 0;
17313                         }
17314                 }
17315                 bcopy(&asp->sts_sensedata, xp->xb_sense_data, SENSE_LENGTH);
17316         }
17317 
17318         /*
17319          * See if we have valid sense data, if so then turn it over to
17320          * sd_decode_sense() to figure out the right course of action.
17321          */
17322         if (sd_validate_sense_data(un, bp, xp, actual_len) ==
17323             SD_SENSE_DATA_IS_VALID) {
17324                 sd_decode_sense(un, bp, xp, pktp);
17325         }
17326 }
17327 
17328 
17329 /*
17330  *    Function: sd_print_sense_failed_msg
17331  *
17332  * Description: Print log message when RQS has failed.
17333  *
17334  *   Arguments: un - ptr to associated softstate
17335  *              bp - ptr to buf(9S) for the command
17336  *              arg - generic message string ptr
17337  *              code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17338  *                      or SD_NO_RETRY_ISSUED
17339  *
17340  *     Context: May be called from interrupt context
17341  */
17342 
17343 static void
17344 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg,
17345     int code)
17346 {
17347         char    *msgp = arg;
17348 
17349         ASSERT(un != NULL);
17350         ASSERT(mutex_owned(SD_MUTEX(un)));
17351         ASSERT(bp != NULL);
17352 
17353         if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) {
17354                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp);
17355         }
17356 }
17357 
17358 
17359 /*
17360  *    Function: sd_validate_sense_data
17361  *
17362  * Description: Check the given sense data for validity.
17363  *              If the sense data is not valid, the command will
17364  *              be either failed or retried!
17365  *
17366  * Return Code: SD_SENSE_DATA_IS_INVALID
17367  *              SD_SENSE_DATA_IS_VALID
17368  *
17369  *     Context: May be called from interrupt context
17370  */
17371 
17372 static int
17373 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17374     size_t actual_len)
17375 {
17376         struct scsi_extended_sense *esp;
17377         struct  scsi_pkt *pktp;
17378         char    *msgp = NULL;
17379         sd_ssc_t *sscp;
17380 
17381         ASSERT(un != NULL);
17382         ASSERT(mutex_owned(SD_MUTEX(un)));
17383         ASSERT(bp != NULL);
17384         ASSERT(bp != un->un_rqs_bp);
17385         ASSERT(xp != NULL);
17386         ASSERT(un->un_fm_private != NULL);
17387 
17388         pktp = SD_GET_PKTP(bp);
17389         ASSERT(pktp != NULL);
17390 
17391         sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
17392         ASSERT(sscp != NULL);
17393 
17394         /*
17395          * Check the status of the RQS command (auto or manual).
17396          */
17397         switch (xp->xb_sense_status & STATUS_MASK) {
17398         case STATUS_GOOD:
17399                 break;
17400 
17401         case STATUS_RESERVATION_CONFLICT:
17402                 sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17403                 return (SD_SENSE_DATA_IS_INVALID);
17404 
17405         case STATUS_BUSY:
17406                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17407                     "Busy Status on REQUEST SENSE\n");
17408                 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL,
17409                     NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17410                 return (SD_SENSE_DATA_IS_INVALID);
17411 
17412         case STATUS_QFULL:
17413                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17414                     "QFULL Status on REQUEST SENSE\n");
17415                 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL,
17416                     NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17417                 return (SD_SENSE_DATA_IS_INVALID);
17418 
17419         case STATUS_CHECK:
17420         case STATUS_TERMINATED:
17421                 msgp = "Check Condition on REQUEST SENSE\n";
17422                 goto sense_failed;
17423 
17424         default:
17425                 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n";
17426                 goto sense_failed;
17427         }
17428 
17429         /*
17430          * See if we got the minimum required amount of sense data.
17431          * Note: We are assuming the returned sense data is SENSE_LENGTH bytes
17432          * or less.
17433          */
17434         if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) ||
17435             (actual_len == 0)) {
17436                 msgp = "Request Sense couldn't get sense data\n";
17437                 goto sense_failed;
17438         }
17439 
17440         if (actual_len < SUN_MIN_SENSE_LENGTH) {
17441                 msgp = "Not enough sense information\n";
17442                 /* Mark the ssc_flags for detecting invalid sense data */
17443                 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17444                         sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17445                             "sense-data");
17446                 }
17447                 goto sense_failed;
17448         }
17449 
17450         /*
17451          * We require the extended sense data
17452          */
17453         esp = (struct scsi_extended_sense *)xp->xb_sense_data;
17454         if (esp->es_class != CLASS_EXTENDED_SENSE) {
17455                 if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17456                         static char tmp[8];
17457                         static char buf[148];
17458                         char *p = (char *)(xp->xb_sense_data);
17459                         int i;
17460 
17461                         mutex_enter(&sd_sense_mutex);
17462                         (void) strcpy(buf, "undecodable sense information:");
17463                         for (i = 0; i < actual_len; i++) {
17464                                 (void) sprintf(tmp, " 0x%x", *(p++)&0xff);
17465                                 (void) strcpy(&buf[strlen(buf)], tmp);
17466                         }
17467                         i = strlen(buf);
17468                         (void) strcpy(&buf[i], "-(assumed fatal)\n");
17469 
17470                         if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
17471                                 scsi_log(SD_DEVINFO(un), sd_label,
17472                                     CE_WARN, buf);
17473                         }
17474                         mutex_exit(&sd_sense_mutex);
17475                 }
17476 
17477                 /* Mark the ssc_flags for detecting invalid sense data */
17478                 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17479                         sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17480                             "sense-data");
17481                 }
17482 
17483                 /* Note: Legacy behavior, fail the command with no retry */
17484                 sd_return_failed_command(un, bp, EIO);
17485                 return (SD_SENSE_DATA_IS_INVALID);
17486         }
17487 
17488         /*
17489          * Check that es_code is valid (es_class concatenated with es_code
17490          * make up the "response code" field.  es_class will always be 7, so
17491          * make sure es_code is 0, 1, 2, 3 or 0xf.  es_code will indicate the
17492          * format.
17493          */
17494         if ((esp->es_code != CODE_FMT_FIXED_CURRENT) &&
17495             (esp->es_code != CODE_FMT_FIXED_DEFERRED) &&
17496             (esp->es_code != CODE_FMT_DESCR_CURRENT) &&
17497             (esp->es_code != CODE_FMT_DESCR_DEFERRED) &&
17498             (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) {
17499                 /* Mark the ssc_flags for detecting invalid sense data */
17500                 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17501                         sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17502                             "sense-data");
17503                 }
17504                 goto sense_failed;
17505         }
17506 
17507         return (SD_SENSE_DATA_IS_VALID);
17508 
17509 sense_failed:
17510         /*
17511          * If the request sense failed (for whatever reason), attempt
17512          * to retry the original command.
17513          */
17514         sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17515             sd_print_sense_failed_msg, msgp, EIO,
17516             un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL);
17517 
17518         return (SD_SENSE_DATA_IS_INVALID);
17519 }
17520 
17521 /*
17522  *    Function: sd_decode_sense
17523  *
17524  * Description: Take recovery action(s) when SCSI Sense Data is received.
17525  *
17526  *     Context: Interrupt context.
17527  */
17528 
17529 static void
17530 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17531     struct scsi_pkt *pktp)
17532 {
17533         uint8_t sense_key;
17534 
17535         ASSERT(un != NULL);
17536         ASSERT(mutex_owned(SD_MUTEX(un)));
17537         ASSERT(bp != NULL);
17538         ASSERT(bp != un->un_rqs_bp);
17539         ASSERT(xp != NULL);
17540         ASSERT(pktp != NULL);
17541 
17542         sense_key = scsi_sense_key(xp->xb_sense_data);
17543 
17544         switch (sense_key) {
17545         case KEY_NO_SENSE:
17546                 sd_sense_key_no_sense(un, bp, xp, pktp);
17547                 break;
17548         case KEY_RECOVERABLE_ERROR:
17549                 sd_sense_key_recoverable_error(un, xp->xb_sense_data,
17550                     bp, xp, pktp);
17551                 break;
17552         case KEY_NOT_READY:
17553                 sd_sense_key_not_ready(un, xp->xb_sense_data,
17554                     bp, xp, pktp);
17555                 break;
17556         case KEY_MEDIUM_ERROR:
17557         case KEY_HARDWARE_ERROR:
17558                 sd_sense_key_medium_or_hardware_error(un,
17559                     xp->xb_sense_data, bp, xp, pktp);
17560                 break;
17561         case KEY_ILLEGAL_REQUEST:
17562                 sd_sense_key_illegal_request(un, bp, xp, pktp);
17563                 break;
17564         case KEY_UNIT_ATTENTION:
17565                 sd_sense_key_unit_attention(un, xp->xb_sense_data,
17566                     bp, xp, pktp);
17567                 break;
17568         case KEY_WRITE_PROTECT:
17569         case KEY_VOLUME_OVERFLOW:
17570         case KEY_MISCOMPARE:
17571                 sd_sense_key_fail_command(un, bp, xp, pktp);
17572                 break;
17573         case KEY_BLANK_CHECK:
17574                 sd_sense_key_blank_check(un, bp, xp, pktp);
17575                 break;
17576         case KEY_ABORTED_COMMAND:
17577                 sd_sense_key_aborted_command(un, bp, xp, pktp);
17578                 break;
17579         case KEY_VENDOR_UNIQUE:
17580         case KEY_COPY_ABORTED:
17581         case KEY_EQUAL:
17582         case KEY_RESERVED:
17583         default:
17584                 sd_sense_key_default(un, xp->xb_sense_data,
17585                     bp, xp, pktp);
17586                 break;
17587         }
17588 }
17589 
17590 
17591 /*
17592  *    Function: sd_dump_memory
17593  *
17594  * Description: Debug logging routine to print the contents of a user provided
17595  *              buffer. The output of the buffer is broken up into 256 byte
17596  *              segments due to a size constraint of the scsi_log.
17597  *              implementation.
17598  *
17599  *   Arguments: un - ptr to softstate
17600  *              comp - component mask
17601  *              title - "title" string to preceed data when printed
17602  *              data - ptr to data block to be printed
17603  *              len - size of data block to be printed
17604  *              fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c)
17605  *
17606  *     Context: May be called from interrupt context
17607  */
17608 
17609 #define SD_DUMP_MEMORY_BUF_SIZE 256
17610 
17611 static char *sd_dump_format_string[] = {
17612                 " 0x%02x",
17613                 " %c"
17614 };
17615 
17616 static void
17617 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data,
17618     int len, int fmt)
17619 {
17620         int     i, j;
17621         int     avail_count;
17622         int     start_offset;
17623         int     end_offset;
17624         size_t  entry_len;
17625         char    *bufp;
17626         char    *local_buf;
17627         char    *format_string;
17628 
17629         ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR));
17630 
17631         /*
17632          * In the debug version of the driver, this function is called from a
17633          * number of places which are NOPs in the release driver.
17634          * The debug driver therefore has additional methods of filtering
17635          * debug output.
17636          */
17637 #ifdef SDDEBUG
17638         /*
17639          * In the debug version of the driver we can reduce the amount of debug
17640          * messages by setting sd_error_level to something other than
17641          * SCSI_ERR_ALL and clearing bits in sd_level_mask and
17642          * sd_component_mask.
17643          */
17644         if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) ||
17645             (sd_error_level != SCSI_ERR_ALL)) {
17646                 return;
17647         }
17648         if (((sd_component_mask & comp) == 0) ||
17649             (sd_error_level != SCSI_ERR_ALL)) {
17650                 return;
17651         }
17652 #else
17653         if (sd_error_level != SCSI_ERR_ALL) {
17654                 return;
17655         }
17656 #endif
17657 
17658         local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP);
17659         bufp = local_buf;
17660         /*
17661          * Available length is the length of local_buf[], minus the
17662          * length of the title string, minus one for the ":", minus
17663          * one for the newline, minus one for the NULL terminator.
17664          * This gives the #bytes available for holding the printed
17665          * values from the given data buffer.
17666          */
17667         if (fmt == SD_LOG_HEX) {
17668                 format_string = sd_dump_format_string[0];
17669         } else /* SD_LOG_CHAR */ {
17670                 format_string = sd_dump_format_string[1];
17671         }
17672         /*
17673          * Available count is the number of elements from the given
17674          * data buffer that we can fit into the available length.
17675          * This is based upon the size of the format string used.
17676          * Make one entry and find it's size.
17677          */
17678         (void) sprintf(bufp, format_string, data[0]);
17679         entry_len = strlen(bufp);
17680         avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len;
17681 
17682         j = 0;
17683         while (j < len) {
17684                 bufp = local_buf;
17685                 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE);
17686                 start_offset = j;
17687 
17688                 end_offset = start_offset + avail_count;
17689 
17690                 (void) sprintf(bufp, "%s:", title);
17691                 bufp += strlen(bufp);
17692                 for (i = start_offset; ((i < end_offset) && (j < len));
17693                     i++, j++) {
17694                         (void) sprintf(bufp, format_string, data[i]);
17695                         bufp += entry_len;
17696                 }
17697                 (void) sprintf(bufp, "\n");
17698 
17699                 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf);
17700         }
17701         kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE);
17702 }
17703 
17704 /*
17705  *    Function: sd_print_sense_msg
17706  *
17707  * Description: Log a message based upon the given sense data.
17708  *
17709  *   Arguments: un - ptr to associated softstate
17710  *              bp - ptr to buf(9S) for the command
17711  *              arg - ptr to associate sd_sense_info struct
17712  *              code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17713  *                      or SD_NO_RETRY_ISSUED
17714  *
17715  *     Context: May be called from interrupt context
17716  */
17717 
17718 static void
17719 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17720 {
17721         struct sd_xbuf  *xp;
17722         struct scsi_pkt *pktp;
17723         uint8_t *sensep;
17724         daddr_t request_blkno;
17725         diskaddr_t err_blkno;
17726         int severity;
17727         int pfa_flag;
17728         extern struct scsi_key_strings scsi_cmds[];
17729 
17730         ASSERT(un != NULL);
17731         ASSERT(mutex_owned(SD_MUTEX(un)));
17732         ASSERT(bp != NULL);
17733         xp = SD_GET_XBUF(bp);
17734         ASSERT(xp != NULL);
17735         pktp = SD_GET_PKTP(bp);
17736         ASSERT(pktp != NULL);
17737         ASSERT(arg != NULL);
17738 
17739         severity = ((struct sd_sense_info *)(arg))->ssi_severity;
17740         pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag;
17741 
17742         if ((code == SD_DELAYED_RETRY_ISSUED) ||
17743             (code == SD_IMMEDIATE_RETRY_ISSUED)) {
17744                 severity = SCSI_ERR_RETRYABLE;
17745         }
17746 
17747         /* Use absolute block number for the request block number */
17748         request_blkno = xp->xb_blkno;
17749 
17750         /*
17751          * Now try to get the error block number from the sense data
17752          */
17753         sensep = xp->xb_sense_data;
17754 
17755         if (scsi_sense_info_uint64(sensep, SENSE_LENGTH,
17756             (uint64_t *)&err_blkno)) {
17757                 /*
17758                  * We retrieved the error block number from the information
17759                  * portion of the sense data.
17760                  *
17761                  * For USCSI commands we are better off using the error
17762                  * block no. as the requested block no. (This is the best
17763                  * we can estimate.)
17764                  */
17765                 if ((SD_IS_BUFIO(xp) == FALSE) &&
17766                     ((pktp->pkt_flags & FLAG_SILENT) == 0)) {
17767                         request_blkno = err_blkno;
17768                 }
17769         } else {
17770                 /*
17771                  * Without the es_valid bit set (for fixed format) or an
17772                  * information descriptor (for descriptor format) we cannot
17773                  * be certain of the error blkno, so just use the
17774                  * request_blkno.
17775                  */
17776                 err_blkno = (diskaddr_t)request_blkno;
17777         }
17778 
17779         /*
17780          * The following will log the buffer contents for the release driver
17781          * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error
17782          * level is set to verbose.
17783          */
17784         sd_dump_memory(un, SD_LOG_IO, "Failed CDB",
17785             (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17786         sd_dump_memory(un, SD_LOG_IO, "Sense Data",
17787             (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX);
17788 
17789         if (pfa_flag == FALSE) {
17790                 /* This is normally only set for USCSI */
17791                 if ((pktp->pkt_flags & FLAG_SILENT) != 0) {
17792                         return;
17793                 }
17794 
17795                 if ((SD_IS_BUFIO(xp) == TRUE) &&
17796                     (((sd_level_mask & SD_LOGMASK_DIAG) == 0) &&
17797                     (severity < sd_error_level))) {
17798                         return;
17799                 }
17800         }
17801         /*
17802          * Check for Sonoma Failover and keep a count of how many failed I/O's
17803          */
17804         if ((SD_IS_LSI(un)) &&
17805             (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) &&
17806             (scsi_sense_asc(sensep) == 0x94) &&
17807             (scsi_sense_ascq(sensep) == 0x01)) {
17808                 un->un_sonoma_failure_count++;
17809                 if (un->un_sonoma_failure_count > 1) {
17810                         return;
17811                 }
17812         }
17813 
17814         if (SD_FM_LOG(un) == SD_FM_LOG_NSUP ||
17815             ((scsi_sense_key(sensep) == KEY_RECOVERABLE_ERROR) &&
17816             (pktp->pkt_resid == 0))) {
17817                 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity,
17818                     request_blkno, err_blkno, scsi_cmds,
17819                     (struct scsi_extended_sense *)sensep,
17820                     un->un_additional_codes, NULL);
17821         }
17822 }
17823 
17824 /*
17825  *    Function: sd_sense_key_no_sense
17826  *
17827  * Description: Recovery action when sense data was not received.
17828  *
17829  *     Context: May be called from interrupt context
17830  */
17831 
17832 static void
17833 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17834     struct scsi_pkt *pktp)
17835 {
17836         struct sd_sense_info    si;
17837 
17838         ASSERT(un != NULL);
17839         ASSERT(mutex_owned(SD_MUTEX(un)));
17840         ASSERT(bp != NULL);
17841         ASSERT(xp != NULL);
17842         ASSERT(pktp != NULL);
17843 
17844         si.ssi_severity = SCSI_ERR_FATAL;
17845         si.ssi_pfa_flag = FALSE;
17846 
17847         SD_UPDATE_ERRSTATS(un, sd_softerrs);
17848 
17849         sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17850             &si, EIO, (clock_t)0, NULL);
17851 }
17852 
17853 
17854 /*
17855  *    Function: sd_sense_key_recoverable_error
17856  *
17857  * Description: Recovery actions for a SCSI "Recovered Error" sense key.
17858  *
17859  *     Context: May be called from interrupt context
17860  */
17861 
17862 static void
17863 sd_sense_key_recoverable_error(struct sd_lun *un, uint8_t *sense_datap,
17864     struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17865 {
17866         struct sd_sense_info    si;
17867         uint8_t asc = scsi_sense_asc(sense_datap);
17868         uint8_t ascq = scsi_sense_ascq(sense_datap);
17869 
17870         ASSERT(un != NULL);
17871         ASSERT(mutex_owned(SD_MUTEX(un)));
17872         ASSERT(bp != NULL);
17873         ASSERT(xp != NULL);
17874         ASSERT(pktp != NULL);
17875 
17876         /*
17877          * 0x00, 0x1D: ATA PASSTHROUGH INFORMATION AVAILABLE
17878          */
17879         if (asc == 0x00 && ascq == 0x1D) {
17880                 sd_return_command(un, bp);
17881                 return;
17882         }
17883 
17884         /*
17885          * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED
17886          */
17887         if ((asc == 0x5D) && (sd_report_pfa != 0)) {
17888                 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
17889                 si.ssi_severity = SCSI_ERR_INFO;
17890                 si.ssi_pfa_flag = TRUE;
17891         } else {
17892                 SD_UPDATE_ERRSTATS(un, sd_softerrs);
17893                 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err);
17894                 si.ssi_severity = SCSI_ERR_RECOVERED;
17895                 si.ssi_pfa_flag = FALSE;
17896         }
17897 
17898         if (pktp->pkt_resid == 0) {
17899                 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17900                 sd_return_command(un, bp);
17901                 return;
17902         }
17903 
17904         sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17905             &si, EIO, (clock_t)0, NULL);
17906 }
17907 
17908 
17909 
17910 
17911 /*
17912  *    Function: sd_sense_key_not_ready
17913  *
17914  * Description: Recovery actions for a SCSI "Not Ready" sense key.
17915  *
17916  *     Context: May be called from interrupt context
17917  */
17918 
17919 static void
17920 sd_sense_key_not_ready(struct sd_lun *un, uint8_t *sense_datap, struct buf *bp,
17921     struct sd_xbuf *xp, struct scsi_pkt *pktp)
17922 {
17923         struct sd_sense_info    si;
17924         uint8_t asc = scsi_sense_asc(sense_datap);
17925         uint8_t ascq = scsi_sense_ascq(sense_datap);
17926 
17927         ASSERT(un != NULL);
17928         ASSERT(mutex_owned(SD_MUTEX(un)));
17929         ASSERT(bp != NULL);
17930         ASSERT(xp != NULL);
17931         ASSERT(pktp != NULL);
17932 
17933         si.ssi_severity = SCSI_ERR_FATAL;
17934         si.ssi_pfa_flag = FALSE;
17935 
17936         /*
17937          * Update error stats after first NOT READY error. Disks may have
17938          * been powered down and may need to be restarted.  For CDROMs,
17939          * report NOT READY errors only if media is present.
17940          */
17941         if ((ISCD(un) && (asc == 0x3A)) ||
17942             (xp->xb_nr_retry_count > 0)) {
17943                 SD_UPDATE_ERRSTATS(un, sd_harderrs);
17944                 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err);
17945         }
17946 
17947         /*
17948          * Just fail if the "not ready" retry limit has been reached.
17949          */
17950         if (xp->xb_nr_retry_count >= un->un_notready_retry_count) {
17951                 /* Special check for error message printing for removables. */
17952                 if (un->un_f_has_removable_media && (asc == 0x04) &&
17953                     (ascq >= 0x04)) {
17954                         si.ssi_severity = SCSI_ERR_ALL;
17955                 }
17956                 goto fail_command;
17957         }
17958 
17959         /*
17960          * Check the ASC and ASCQ in the sense data as needed, to determine
17961          * what to do.
17962          */
17963         switch (asc) {
17964         case 0x04:      /* LOGICAL UNIT NOT READY */
17965                 /*
17966                  * disk drives that don't spin up result in a very long delay
17967                  * in format without warning messages. We will log a message
17968                  * if the error level is set to verbose.
17969                  */
17970                 if (sd_error_level < SCSI_ERR_RETRYABLE) {
17971                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17972                             "logical unit not ready, resetting disk\n");
17973                 }
17974 
17975                 /*
17976                  * There are different requirements for CDROMs and disks for
17977                  * the number of retries.  If a CD-ROM is giving this, it is
17978                  * probably reading TOC and is in the process of getting
17979                  * ready, so we should keep on trying for a long time to make
17980                  * sure that all types of media are taken in account (for
17981                  * some media the drive takes a long time to read TOC).  For
17982                  * disks we do not want to retry this too many times as this
17983                  * can cause a long hang in format when the drive refuses to
17984                  * spin up (a very common failure).
17985                  */
17986                 switch (ascq) {
17987                 case 0x00:  /* LUN NOT READY, CAUSE NOT REPORTABLE */
17988                         /*
17989                          * Disk drives frequently refuse to spin up which
17990                          * results in a very long hang in format without
17991                          * warning messages.
17992                          *
17993                          * Note: This code preserves the legacy behavior of
17994                          * comparing xb_nr_retry_count against zero for fibre
17995                          * channel targets instead of comparing against the
17996                          * un_reset_retry_count value.  The reason for this
17997                          * discrepancy has been so utterly lost beneath the
17998                          * Sands of Time that even Indiana Jones could not
17999                          * find it.
18000                          */
18001                         if (un->un_f_is_fibre == TRUE) {
18002                                 if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18003                                     (xp->xb_nr_retry_count > 0)) &&
18004                                     (un->un_startstop_timeid == NULL)) {
18005                                         scsi_log(SD_DEVINFO(un), sd_label,
18006                                             CE_WARN, "logical unit not ready, "
18007                                             "resetting disk\n");
18008                                         sd_reset_target(un, pktp);
18009                                 }
18010                         } else {
18011                                 if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18012                                     (xp->xb_nr_retry_count >
18013                                     un->un_reset_retry_count)) &&
18014                                     (un->un_startstop_timeid == NULL)) {
18015                                         scsi_log(SD_DEVINFO(un), sd_label,
18016                                             CE_WARN, "logical unit not ready, "
18017                                             "resetting disk\n");
18018                                         sd_reset_target(un, pktp);
18019                                 }
18020                         }
18021                         break;
18022 
18023                 case 0x01:  /* LUN IS IN PROCESS OF BECOMING READY */
18024                         /*
18025                          * If the target is in the process of becoming
18026                          * ready, just proceed with the retry. This can
18027                          * happen with CD-ROMs that take a long time to
18028                          * read TOC after a power cycle or reset.
18029                          */
18030                         goto do_retry;
18031 
18032                 case 0x02:  /* LUN NOT READY, INITITIALIZING CMD REQUIRED */
18033                         break;
18034 
18035                 case 0x03:  /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */
18036                         /*
18037                          * Retries cannot help here so just fail right away.
18038                          */
18039                         goto fail_command;
18040 
18041                 case 0x88:
18042                         /*
18043                          * Vendor-unique code for T3/T4: it indicates a
18044                          * path problem in a mutipathed config, but as far as
18045                          * the target driver is concerned it equates to a fatal
18046                          * error, so we should just fail the command right away
18047                          * (without printing anything to the console). If this
18048                          * is not a T3/T4, fall thru to the default recovery
18049                          * action.
18050                          * T3/T4 is FC only, don't need to check is_fibre
18051                          */
18052                         if (SD_IS_T3(un) || SD_IS_T4(un)) {
18053                                 sd_return_failed_command(un, bp, EIO);
18054                                 return;
18055                         }
18056                         /* FALLTHRU */
18057 
18058                 case 0x04:  /* LUN NOT READY, FORMAT IN PROGRESS */
18059                 case 0x05:  /* LUN NOT READY, REBUILD IN PROGRESS */
18060                 case 0x06:  /* LUN NOT READY, RECALCULATION IN PROGRESS */
18061                 case 0x07:  /* LUN NOT READY, OPERATION IN PROGRESS */
18062                 case 0x08:  /* LUN NOT READY, LONG WRITE IN PROGRESS */
18063                 default:    /* Possible future codes in SCSI spec? */
18064                         /*
18065                          * For removable-media devices, do not retry if
18066                          * ASCQ > 2 as these result mostly from USCSI commands
18067                          * on MMC devices issued to check status of an
18068                          * operation initiated in immediate mode.  Also for
18069                          * ASCQ >= 4 do not print console messages as these
18070                          * mainly represent a user-initiated operation
18071                          * instead of a system failure.
18072                          */
18073                         if (un->un_f_has_removable_media) {
18074                                 si.ssi_severity = SCSI_ERR_ALL;
18075                                 goto fail_command;
18076                         }
18077                         break;
18078                 }
18079 
18080                 /*
18081                  * As part of our recovery attempt for the NOT READY
18082                  * condition, we issue a START STOP UNIT command. However
18083                  * we want to wait for a short delay before attempting this
18084                  * as there may still be more commands coming back from the
18085                  * target with the check condition. To do this we use
18086                  * timeout(9F) to call sd_start_stop_unit_callback() after
18087                  * the delay interval expires. (sd_start_stop_unit_callback()
18088                  * dispatches sd_start_stop_unit_task(), which will issue
18089                  * the actual START STOP UNIT command. The delay interval
18090                  * is one-half of the delay that we will use to retry the
18091                  * command that generated the NOT READY condition.
18092                  *
18093                  * Note that we could just dispatch sd_start_stop_unit_task()
18094                  * from here and allow it to sleep for the delay interval,
18095                  * but then we would be tying up the taskq thread
18096                  * uncesessarily for the duration of the delay.
18097                  *
18098                  * Do not issue the START STOP UNIT if the current command
18099                  * is already a START STOP UNIT.
18100                  */
18101                 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) {
18102                         break;
18103                 }
18104 
18105                 /*
18106                  * Do not schedule the timeout if one is already pending.
18107                  */
18108                 if (un->un_startstop_timeid != NULL) {
18109                         SD_INFO(SD_LOG_ERROR, un,
18110                             "sd_sense_key_not_ready: restart already issued to"
18111                             " %s%d\n", ddi_driver_name(SD_DEVINFO(un)),
18112                             ddi_get_instance(SD_DEVINFO(un)));
18113                         break;
18114                 }
18115 
18116                 /*
18117                  * Schedule the START STOP UNIT command, then queue the command
18118                  * for a retry.
18119                  *
18120                  * Note: A timeout is not scheduled for this retry because we
18121                  * want the retry to be serial with the START_STOP_UNIT. The
18122                  * retry will be started when the START_STOP_UNIT is completed
18123                  * in sd_start_stop_unit_task.
18124                  */
18125                 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback,
18126                     un, un->un_busy_timeout / 2);
18127                 xp->xb_nr_retry_count++;
18128                 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter);
18129                 return;
18130 
18131         case 0x05:      /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */
18132                 if (sd_error_level < SCSI_ERR_RETRYABLE) {
18133                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18134                             "unit does not respond to selection\n");
18135                 }
18136                 break;
18137 
18138         case 0x3A:      /* MEDIUM NOT PRESENT */
18139                 if (sd_error_level >= SCSI_ERR_FATAL) {
18140                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18141                             "Caddy not inserted in drive\n");
18142                 }
18143 
18144                 sr_ejected(un);
18145                 un->un_mediastate = DKIO_EJECTED;
18146                 /* The state has changed, inform the media watch routines */
18147                 cv_broadcast(&un->un_state_cv);
18148                 /* Just fail if no media is present in the drive. */
18149                 goto fail_command;
18150 
18151         default:
18152                 if (sd_error_level < SCSI_ERR_RETRYABLE) {
18153                         scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
18154                             "Unit not Ready. Additional sense code 0x%x\n",
18155                             asc);
18156                 }
18157                 break;
18158         }
18159 
18160 do_retry:
18161 
18162         /*
18163          * Retry the command, as some targets may report NOT READY for
18164          * several seconds after being reset.
18165          */
18166         xp->xb_nr_retry_count++;
18167         si.ssi_severity = SCSI_ERR_RETRYABLE;
18168         sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
18169             &si, EIO, un->un_busy_timeout, NULL);
18170 
18171         return;
18172 
18173 fail_command:
18174         sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18175         sd_return_failed_command(un, bp, EIO);
18176 }
18177 
18178 
18179 
18180 /*
18181  *    Function: sd_sense_key_medium_or_hardware_error
18182  *
18183  * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error"
18184  *              sense key.
18185  *
18186  *     Context: May be called from interrupt context
18187  */
18188 
18189 static void
18190 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, uint8_t *sense_datap,
18191     struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18192 {
18193         struct sd_sense_info    si;
18194         uint8_t sense_key = scsi_sense_key(sense_datap);
18195         uint8_t asc = scsi_sense_asc(sense_datap);
18196 
18197         ASSERT(un != NULL);
18198         ASSERT(mutex_owned(SD_MUTEX(un)));
18199         ASSERT(bp != NULL);
18200         ASSERT(xp != NULL);
18201         ASSERT(pktp != NULL);
18202 
18203         si.ssi_severity = SCSI_ERR_FATAL;
18204         si.ssi_pfa_flag = FALSE;
18205 
18206         if (sense_key == KEY_MEDIUM_ERROR) {
18207                 SD_UPDATE_ERRSTATS(un, sd_rq_media_err);
18208         }
18209 
18210         SD_UPDATE_ERRSTATS(un, sd_harderrs);
18211 
18212         if ((un->un_reset_retry_count != 0) &&
18213             (xp->xb_retry_count == un->un_reset_retry_count)) {
18214                 mutex_exit(SD_MUTEX(un));
18215                 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */
18216                 if (un->un_f_allow_bus_device_reset == TRUE) {
18217 
18218                         boolean_t try_resetting_target = B_TRUE;
18219 
18220                         /*
18221                          * We need to be able to handle specific ASC when we are
18222                          * handling a KEY_HARDWARE_ERROR. In particular
18223                          * taking the default action of resetting the target may
18224                          * not be the appropriate way to attempt recovery.
18225                          * Resetting a target because of a single LUN failure
18226                          * victimizes all LUNs on that target.
18227                          *
18228                          * This is true for the LSI arrays, if an LSI
18229                          * array controller returns an ASC of 0x84 (LUN Dead) we
18230                          * should trust it.
18231                          */
18232 
18233                         if (sense_key == KEY_HARDWARE_ERROR) {
18234                                 switch (asc) {
18235                                 case 0x84:
18236                                         if (SD_IS_LSI(un)) {
18237                                                 try_resetting_target = B_FALSE;
18238                                         }
18239                                         break;
18240                                 default:
18241                                         break;
18242                                 }
18243                         }
18244 
18245                         if (try_resetting_target == B_TRUE) {
18246                                 int reset_retval = 0;
18247                                 if (un->un_f_lun_reset_enabled == TRUE) {
18248                                         SD_TRACE(SD_LOG_IO_CORE, un,
18249                                             "sd_sense_key_medium_or_hardware_"
18250                                             "error: issuing RESET_LUN\n");
18251                                         reset_retval =
18252                                             scsi_reset(SD_ADDRESS(un),
18253                                             RESET_LUN);
18254                                 }
18255                                 if (reset_retval == 0) {
18256                                         SD_TRACE(SD_LOG_IO_CORE, un,
18257                                             "sd_sense_key_medium_or_hardware_"
18258                                             "error: issuing RESET_TARGET\n");
18259                                         (void) scsi_reset(SD_ADDRESS(un),
18260                                             RESET_TARGET);
18261                                 }
18262                         }
18263                 }
18264                 mutex_enter(SD_MUTEX(un));
18265         }
18266 
18267         /*
18268          * This really ought to be a fatal error, but we will retry anyway
18269          * as some drives report this as a spurious error.
18270          */
18271         sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18272             &si, EIO, (clock_t)0, NULL);
18273 }
18274 
18275 
18276 
18277 /*
18278  *    Function: sd_sense_key_illegal_request
18279  *
18280  * Description: Recovery actions for a SCSI "Illegal Request" sense key.
18281  *
18282  *     Context: May be called from interrupt context
18283  */
18284 
18285 static void
18286 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
18287     struct sd_xbuf *xp, struct scsi_pkt *pktp)
18288 {
18289         struct sd_sense_info    si;
18290 
18291         ASSERT(un != NULL);
18292         ASSERT(mutex_owned(SD_MUTEX(un)));
18293         ASSERT(bp != NULL);
18294         ASSERT(xp != NULL);
18295         ASSERT(pktp != NULL);
18296 
18297         SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err);
18298 
18299         si.ssi_severity = SCSI_ERR_INFO;
18300         si.ssi_pfa_flag = FALSE;
18301 
18302         /* Pointless to retry if the target thinks it's an illegal request */
18303         sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18304         sd_return_failed_command(un, bp, EIO);
18305 }
18306 
18307 
18308 
18309 
18310 /*
18311  *    Function: sd_sense_key_unit_attention
18312  *
18313  * Description: Recovery actions for a SCSI "Unit Attention" sense key.
18314  *
18315  *     Context: May be called from interrupt context
18316  */
18317 
18318 static void
18319 sd_sense_key_unit_attention(struct sd_lun *un,  uint8_t *sense_datap,
18320     struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18321 {
18322         /*
18323          * For UNIT ATTENTION we allow retries for one minute. Devices
18324          * like Sonoma can return UNIT ATTENTION close to a minute
18325          * under certain conditions.
18326          */
18327         int     retry_check_flag = SD_RETRIES_UA;
18328         boolean_t       kstat_updated = B_FALSE;
18329         struct  sd_sense_info           si;
18330         uint8_t asc = scsi_sense_asc(sense_datap);
18331         uint8_t ascq = scsi_sense_ascq(sense_datap);
18332 
18333         ASSERT(un != NULL);
18334         ASSERT(mutex_owned(SD_MUTEX(un)));
18335         ASSERT(bp != NULL);
18336         ASSERT(xp != NULL);
18337         ASSERT(pktp != NULL);
18338 
18339         si.ssi_severity = SCSI_ERR_INFO;
18340         si.ssi_pfa_flag = FALSE;
18341 
18342 
18343         switch (asc) {
18344         case 0x5D:  /* FAILURE PREDICTION THRESHOLD EXCEEDED */
18345                 if (sd_report_pfa != 0) {
18346                         SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18347                         si.ssi_pfa_flag = TRUE;
18348                         retry_check_flag = SD_RETRIES_STANDARD;
18349                         goto do_retry;
18350                 }
18351 
18352                 break;
18353 
18354         case 0x29:  /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
18355                 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
18356                         un->un_resvd_status |=
18357                             (SD_LOST_RESERVE | SD_WANT_RESERVE);
18358                 }
18359 #ifdef _LP64
18360                 if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) {
18361                         if (taskq_dispatch(sd_tq, sd_reenable_dsense_task,
18362                             un, KM_NOSLEEP) == 0) {
18363                                 /*
18364                                  * If we can't dispatch the task we'll just
18365                                  * live without descriptor sense.  We can
18366                                  * try again on the next "unit attention"
18367                                  */
18368                                 SD_ERROR(SD_LOG_ERROR, un,
18369                                     "sd_sense_key_unit_attention: "
18370                                     "Could not dispatch "
18371                                     "sd_reenable_dsense_task\n");
18372                         }
18373                 }
18374 #endif /* _LP64 */
18375                 /* FALLTHRU */
18376 
18377         case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
18378                 if (!un->un_f_has_removable_media) {
18379                         break;
18380                 }
18381 
18382                 /*
18383                  * When we get a unit attention from a removable-media device,
18384                  * it may be in a state that will take a long time to recover
18385                  * (e.g., from a reset).  Since we are executing in interrupt
18386                  * context here, we cannot wait around for the device to come
18387                  * back. So hand this command off to sd_media_change_task()
18388                  * for deferred processing under taskq thread context. (Note
18389                  * that the command still may be failed if a problem is
18390                  * encountered at a later time.)
18391                  */
18392                 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp,
18393                     KM_NOSLEEP) == 0) {
18394                         /*
18395                          * Cannot dispatch the request so fail the command.
18396                          */
18397                         SD_UPDATE_ERRSTATS(un, sd_harderrs);
18398                         SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18399                         si.ssi_severity = SCSI_ERR_FATAL;
18400                         sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18401                         sd_return_failed_command(un, bp, EIO);
18402                 }
18403 
18404                 /*
18405                  * If failed to dispatch sd_media_change_task(), we already
18406                  * updated kstat. If succeed to dispatch sd_media_change_task(),
18407                  * we should update kstat later if it encounters an error. So,
18408                  * we update kstat_updated flag here.
18409                  */
18410                 kstat_updated = B_TRUE;
18411 
18412                 /*
18413                  * Either the command has been successfully dispatched to a
18414                  * task Q for retrying, or the dispatch failed. In either case
18415                  * do NOT retry again by calling sd_retry_command. This sets up
18416                  * two retries of the same command and when one completes and
18417                  * frees the resources the other will access freed memory,
18418                  * a bad thing.
18419                  */
18420                 return;
18421 
18422         default:
18423                 break;
18424         }
18425 
18426         /*
18427          * ASC  ASCQ
18428          *  2A   09     Capacity data has changed
18429          *  2A   01     Mode parameters changed
18430          *  3F   0E     Reported luns data has changed
18431          * Arrays that support logical unit expansion should report
18432          * capacity changes(2Ah/09). Mode parameters changed and
18433          * reported luns data has changed are the approximation.
18434          */
18435         if (((asc == 0x2a) && (ascq == 0x09)) ||
18436             ((asc == 0x2a) && (ascq == 0x01)) ||
18437             ((asc == 0x3f) && (ascq == 0x0e))) {
18438                 if (taskq_dispatch(sd_tq, sd_target_change_task, un,
18439                     KM_NOSLEEP) == 0) {
18440                         SD_ERROR(SD_LOG_ERROR, un,
18441                             "sd_sense_key_unit_attention: "
18442                             "Could not dispatch sd_target_change_task\n");
18443                 }
18444         }
18445 
18446         /*
18447          * Update kstat if we haven't done that.
18448          */
18449         if (!kstat_updated) {
18450                 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18451                 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18452         }
18453 
18454 do_retry:
18455         sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si,
18456             EIO, SD_UA_RETRY_DELAY, NULL);
18457 }
18458 
18459 
18460 
18461 /*
18462  *    Function: sd_sense_key_fail_command
18463  *
18464  * Description: Use to fail a command when we don't like the sense key that
18465  *              was returned.
18466  *
18467  *     Context: May be called from interrupt context
18468  */
18469 
18470 static void
18471 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18472     struct scsi_pkt *pktp)
18473 {
18474         struct sd_sense_info    si;
18475 
18476         ASSERT(un != NULL);
18477         ASSERT(mutex_owned(SD_MUTEX(un)));
18478         ASSERT(bp != NULL);
18479         ASSERT(xp != NULL);
18480         ASSERT(pktp != NULL);
18481 
18482         si.ssi_severity = SCSI_ERR_FATAL;
18483         si.ssi_pfa_flag = FALSE;
18484 
18485         sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18486         sd_return_failed_command(un, bp, EIO);
18487 }
18488 
18489 
18490 
18491 /*
18492  *    Function: sd_sense_key_blank_check
18493  *
18494  * Description: Recovery actions for a SCSI "Blank Check" sense key.
18495  *              Has no monetary connotation.
18496  *
18497  *     Context: May be called from interrupt context
18498  */
18499 
18500 static void
18501 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18502     struct scsi_pkt *pktp)
18503 {
18504         struct sd_sense_info    si;
18505 
18506         ASSERT(un != NULL);
18507         ASSERT(mutex_owned(SD_MUTEX(un)));
18508         ASSERT(bp != NULL);
18509         ASSERT(xp != NULL);
18510         ASSERT(pktp != NULL);
18511 
18512         /*
18513          * Blank check is not fatal for removable devices, therefore
18514          * it does not require a console message.
18515          */
18516         si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL :
18517             SCSI_ERR_FATAL;
18518         si.ssi_pfa_flag = FALSE;
18519 
18520         sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18521         sd_return_failed_command(un, bp, EIO);
18522 }
18523 
18524 
18525 
18526 
18527 /*
18528  *    Function: sd_sense_key_aborted_command
18529  *
18530  * Description: Recovery actions for a SCSI "Aborted Command" sense key.
18531  *
18532  *     Context: May be called from interrupt context
18533  */
18534 
18535 static void
18536 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
18537     struct sd_xbuf *xp, struct scsi_pkt *pktp)
18538 {
18539         struct sd_sense_info    si;
18540 
18541         ASSERT(un != NULL);
18542         ASSERT(mutex_owned(SD_MUTEX(un)));
18543         ASSERT(bp != NULL);
18544         ASSERT(xp != NULL);
18545         ASSERT(pktp != NULL);
18546 
18547         si.ssi_severity = SCSI_ERR_FATAL;
18548         si.ssi_pfa_flag = FALSE;
18549 
18550         SD_UPDATE_ERRSTATS(un, sd_harderrs);
18551 
18552         /*
18553          * This really ought to be a fatal error, but we will retry anyway
18554          * as some drives report this as a spurious error.
18555          */
18556         sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18557             &si, EIO, drv_usectohz(100000), NULL);
18558 }
18559 
18560 
18561 
18562 /*
18563  *    Function: sd_sense_key_default
18564  *
18565  * Description: Default recovery action for several SCSI sense keys (basically
18566  *              attempts a retry).
18567  *
18568  *     Context: May be called from interrupt context
18569  */
18570 
18571 static void
18572 sd_sense_key_default(struct sd_lun *un, uint8_t *sense_datap, struct buf *bp,
18573     struct sd_xbuf *xp, struct scsi_pkt *pktp)
18574 {
18575         struct sd_sense_info    si;
18576         uint8_t sense_key = scsi_sense_key(sense_datap);
18577 
18578         ASSERT(un != NULL);
18579         ASSERT(mutex_owned(SD_MUTEX(un)));
18580         ASSERT(bp != NULL);
18581         ASSERT(xp != NULL);
18582         ASSERT(pktp != NULL);
18583 
18584         SD_UPDATE_ERRSTATS(un, sd_harderrs);
18585 
18586         /*
18587          * Undecoded sense key. Attempt retries and hope that will fix
18588          * the problem.  Otherwise, we're dead.
18589          */
18590         if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
18591                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18592                     "Unhandled Sense Key '%s'\n", sense_keys[sense_key]);
18593         }
18594 
18595         si.ssi_severity = SCSI_ERR_FATAL;
18596         si.ssi_pfa_flag = FALSE;
18597 
18598         sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18599             &si, EIO, (clock_t)0, NULL);
18600 }
18601 
18602 
18603 
18604 /*
18605  *    Function: sd_print_retry_msg
18606  *
18607  * Description: Print a message indicating the retry action being taken.
18608  *
18609  *   Arguments: un - ptr to associated softstate
18610  *              bp - ptr to buf(9S) for the command
18611  *              arg - not used.
18612  *              flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18613  *                      or SD_NO_RETRY_ISSUED
18614  *
18615  *     Context: May be called from interrupt context
18616  */
18617 /* ARGSUSED */
18618 static void
18619 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag)
18620 {
18621         struct sd_xbuf  *xp;
18622         struct scsi_pkt *pktp;
18623         char *reasonp;
18624         char *msgp;
18625 
18626         ASSERT(un != NULL);
18627         ASSERT(mutex_owned(SD_MUTEX(un)));
18628         ASSERT(bp != NULL);
18629         pktp = SD_GET_PKTP(bp);
18630         ASSERT(pktp != NULL);
18631         xp = SD_GET_XBUF(bp);
18632         ASSERT(xp != NULL);
18633 
18634         ASSERT(!mutex_owned(&un->un_pm_mutex));
18635         mutex_enter(&un->un_pm_mutex);
18636         if ((un->un_state == SD_STATE_SUSPENDED) ||
18637             (SD_DEVICE_IS_IN_LOW_POWER(un)) ||
18638             (pktp->pkt_flags & FLAG_SILENT)) {
18639                 mutex_exit(&un->un_pm_mutex);
18640                 goto update_pkt_reason;
18641         }
18642         mutex_exit(&un->un_pm_mutex);
18643 
18644         /*
18645          * Suppress messages if they are all the same pkt_reason; with
18646          * TQ, many (up to 256) are returned with the same pkt_reason.
18647          * If we are in panic, then suppress the retry messages.
18648          */
18649         switch (flag) {
18650         case SD_NO_RETRY_ISSUED:
18651                 msgp = "giving up";
18652                 break;
18653         case SD_IMMEDIATE_RETRY_ISSUED:
18654         case SD_DELAYED_RETRY_ISSUED:
18655                 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) ||
18656                     ((pktp->pkt_reason == un->un_last_pkt_reason) &&
18657                     (sd_error_level != SCSI_ERR_ALL))) {
18658                         return;
18659                 }
18660                 msgp = "retrying command";
18661                 break;
18662         default:
18663                 goto update_pkt_reason;
18664         }
18665 
18666         reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" :
18667             scsi_rname(pktp->pkt_reason));
18668 
18669         if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
18670                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18671                     "SCSI transport failed: reason '%s': %s\n", reasonp, msgp);
18672         }
18673 
18674 update_pkt_reason:
18675         /*
18676          * Update un->un_last_pkt_reason with the value in pktp->pkt_reason.
18677          * This is to prevent multiple console messages for the same failure
18678          * condition.  Note that un->un_last_pkt_reason is NOT restored if &
18679          * when the command is retried successfully because there still may be
18680          * more commands coming back with the same value of pktp->pkt_reason.
18681          */
18682         if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) {
18683                 un->un_last_pkt_reason = pktp->pkt_reason;
18684         }
18685 }
18686 
18687 
18688 /*
18689  *    Function: sd_print_cmd_incomplete_msg
18690  *
18691  * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason.
18692  *
18693  *   Arguments: un - ptr to associated softstate
18694  *              bp - ptr to buf(9S) for the command
18695  *              arg - passed to sd_print_retry_msg()
18696  *              code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18697  *                      or SD_NO_RETRY_ISSUED
18698  *
18699  *     Context: May be called from interrupt context
18700  */
18701 
18702 static void
18703 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg,
18704     int code)
18705 {
18706         dev_info_t      *dip;
18707 
18708         ASSERT(un != NULL);
18709         ASSERT(mutex_owned(SD_MUTEX(un)));
18710         ASSERT(bp != NULL);
18711 
18712         switch (code) {
18713         case SD_NO_RETRY_ISSUED:
18714                 /* Command was failed. Someone turned off this target? */
18715                 if (un->un_state != SD_STATE_OFFLINE) {
18716                         /*
18717                          * Suppress message if we are detaching and
18718                          * device has been disconnected
18719                          * Note that DEVI_IS_DEVICE_REMOVED is a consolidation
18720                          * private interface and not part of the DDI
18721                          */
18722                         dip = un->un_sd->sd_dev;
18723                         if (!(DEVI_IS_DETACHING(dip) &&
18724                             DEVI_IS_DEVICE_REMOVED(dip))) {
18725                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18726                                 "disk not responding to selection\n");
18727                         }
18728                         New_state(un, SD_STATE_OFFLINE);
18729                 }
18730                 break;
18731 
18732         case SD_DELAYED_RETRY_ISSUED:
18733         case SD_IMMEDIATE_RETRY_ISSUED:
18734         default:
18735                 /* Command was successfully queued for retry */
18736                 sd_print_retry_msg(un, bp, arg, code);
18737                 break;
18738         }
18739 }
18740 
18741 
18742 /*
18743  *    Function: sd_pkt_reason_cmd_incomplete
18744  *
18745  * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason.
18746  *
18747  *     Context: May be called from interrupt context
18748  */
18749 
18750 static void
18751 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
18752     struct sd_xbuf *xp, struct scsi_pkt *pktp)
18753 {
18754         int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE;
18755 
18756         ASSERT(un != NULL);
18757         ASSERT(mutex_owned(SD_MUTEX(un)));
18758         ASSERT(bp != NULL);
18759         ASSERT(xp != NULL);
18760         ASSERT(pktp != NULL);
18761 
18762         /* Do not do a reset if selection did not complete */
18763         /* Note: Should this not just check the bit? */
18764         if (pktp->pkt_state != STATE_GOT_BUS) {
18765                 SD_UPDATE_ERRSTATS(un, sd_transerrs);
18766                 sd_reset_target(un, pktp);
18767         }
18768 
18769         /*
18770          * If the target was not successfully selected, then set
18771          * SD_RETRIES_FAILFAST to indicate that we lost communication
18772          * with the target, and further retries and/or commands are
18773          * likely to take a long time.
18774          */
18775         if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) {
18776                 flag |= SD_RETRIES_FAILFAST;
18777         }
18778 
18779         SD_UPDATE_RESERVATION_STATUS(un, pktp);
18780 
18781         sd_retry_command(un, bp, flag,
18782             sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18783 }
18784 
18785 
18786 
18787 /*
18788  *    Function: sd_pkt_reason_cmd_tran_err
18789  *
18790  * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason.
18791  *
18792  *     Context: May be called from interrupt context
18793  */
18794 
18795 static void
18796 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
18797     struct sd_xbuf *xp, struct scsi_pkt *pktp)
18798 {
18799         ASSERT(un != NULL);
18800         ASSERT(mutex_owned(SD_MUTEX(un)));
18801         ASSERT(bp != NULL);
18802         ASSERT(xp != NULL);
18803         ASSERT(pktp != NULL);
18804 
18805         /*
18806          * Do not reset if we got a parity error, or if
18807          * selection did not complete.
18808          */
18809         SD_UPDATE_ERRSTATS(un, sd_harderrs);
18810         /* Note: Should this not just check the bit for pkt_state? */
18811         if (((pktp->pkt_statistics & STAT_PERR) == 0) &&
18812             (pktp->pkt_state != STATE_GOT_BUS)) {
18813                 SD_UPDATE_ERRSTATS(un, sd_transerrs);
18814                 sd_reset_target(un, pktp);
18815         }
18816 
18817         SD_UPDATE_RESERVATION_STATUS(un, pktp);
18818 
18819         sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
18820             sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18821 }
18822 
18823 
18824 
18825 /*
18826  *    Function: sd_pkt_reason_cmd_reset
18827  *
18828  * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason.
18829  *
18830  *     Context: May be called from interrupt context
18831  */
18832 
18833 static void
18834 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18835     struct scsi_pkt *pktp)
18836 {
18837         ASSERT(un != NULL);
18838         ASSERT(mutex_owned(SD_MUTEX(un)));
18839         ASSERT(bp != NULL);
18840         ASSERT(xp != NULL);
18841         ASSERT(pktp != NULL);
18842 
18843         /* The target may still be running the command, so try to reset. */
18844         SD_UPDATE_ERRSTATS(un, sd_transerrs);
18845         sd_reset_target(un, pktp);
18846 
18847         SD_UPDATE_RESERVATION_STATUS(un, pktp);
18848 
18849         /*
18850          * If pkt_reason is CMD_RESET chances are that this pkt got
18851          * reset because another target on this bus caused it. The target
18852          * that caused it should get CMD_TIMEOUT with pkt_statistics
18853          * of STAT_TIMEOUT/STAT_DEV_RESET.
18854          */
18855 
18856         sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
18857             sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18858 }
18859 
18860 
18861 
18862 
18863 /*
18864  *    Function: sd_pkt_reason_cmd_aborted
18865  *
18866  * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason.
18867  *
18868  *     Context: May be called from interrupt context
18869  */
18870 
18871 static void
18872 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18873     struct scsi_pkt *pktp)
18874 {
18875         ASSERT(un != NULL);
18876         ASSERT(mutex_owned(SD_MUTEX(un)));
18877         ASSERT(bp != NULL);
18878         ASSERT(xp != NULL);
18879         ASSERT(pktp != NULL);
18880 
18881         /* The target may still be running the command, so try to reset. */
18882         SD_UPDATE_ERRSTATS(un, sd_transerrs);
18883         sd_reset_target(un, pktp);
18884 
18885         SD_UPDATE_RESERVATION_STATUS(un, pktp);
18886 
18887         /*
18888          * If pkt_reason is CMD_ABORTED chances are that this pkt got
18889          * aborted because another target on this bus caused it. The target
18890          * that caused it should get CMD_TIMEOUT with pkt_statistics
18891          * of STAT_TIMEOUT/STAT_DEV_RESET.
18892          */
18893 
18894         sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
18895             sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18896 }
18897 
18898 
18899 
18900 /*
18901  *    Function: sd_pkt_reason_cmd_timeout
18902  *
18903  * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason.
18904  *
18905  *     Context: May be called from interrupt context
18906  */
18907 
18908 static void
18909 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18910     struct scsi_pkt *pktp)
18911 {
18912         ASSERT(un != NULL);
18913         ASSERT(mutex_owned(SD_MUTEX(un)));
18914         ASSERT(bp != NULL);
18915         ASSERT(xp != NULL);
18916         ASSERT(pktp != NULL);
18917 
18918 
18919         SD_UPDATE_ERRSTATS(un, sd_transerrs);
18920         sd_reset_target(un, pktp);
18921 
18922         SD_UPDATE_RESERVATION_STATUS(un, pktp);
18923 
18924         /*
18925          * A command timeout indicates that we could not establish
18926          * communication with the target, so set SD_RETRIES_FAILFAST
18927          * as further retries/commands are likely to take a long time.
18928          */
18929         sd_retry_command(un, bp,
18930             (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST),
18931             sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18932 }
18933 
18934 
18935 
18936 /*
18937  *    Function: sd_pkt_reason_cmd_unx_bus_free
18938  *
18939  * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason.
18940  *
18941  *     Context: May be called from interrupt context
18942  */
18943 
18944 static void
18945 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
18946     struct sd_xbuf *xp, struct scsi_pkt *pktp)
18947 {
18948         void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code);
18949 
18950         ASSERT(un != NULL);
18951         ASSERT(mutex_owned(SD_MUTEX(un)));
18952         ASSERT(bp != NULL);
18953         ASSERT(xp != NULL);
18954         ASSERT(pktp != NULL);
18955 
18956         SD_UPDATE_ERRSTATS(un, sd_harderrs);
18957         SD_UPDATE_RESERVATION_STATUS(un, pktp);
18958 
18959         funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ?
18960             sd_print_retry_msg : NULL;
18961 
18962         sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
18963             funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18964 }
18965 
18966 
18967 /*
18968  *    Function: sd_pkt_reason_cmd_tag_reject
18969  *
18970  * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason.
18971  *
18972  *     Context: May be called from interrupt context
18973  */
18974 
18975 static void
18976 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
18977     struct sd_xbuf *xp, struct scsi_pkt *pktp)
18978 {
18979         ASSERT(un != NULL);
18980         ASSERT(mutex_owned(SD_MUTEX(un)));
18981         ASSERT(bp != NULL);
18982         ASSERT(xp != NULL);
18983         ASSERT(pktp != NULL);
18984 
18985         SD_UPDATE_ERRSTATS(un, sd_harderrs);
18986         pktp->pkt_flags = 0;
18987         un->un_tagflags = 0;
18988         if (un->un_f_opt_queueing == TRUE) {
18989                 un->un_throttle = min(un->un_throttle, 3);
18990         } else {
18991                 un->un_throttle = 1;
18992         }
18993         mutex_exit(SD_MUTEX(un));
18994         (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
18995         mutex_enter(SD_MUTEX(un));
18996 
18997         SD_UPDATE_RESERVATION_STATUS(un, pktp);
18998 
18999         /* Legacy behavior not to check retry counts here. */
19000         sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE),
19001             sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19002 }
19003 
19004 
19005 /*
19006  *    Function: sd_pkt_reason_default
19007  *
19008  * Description: Default recovery actions for SCSA pkt_reason values that
19009  *              do not have more explicit recovery actions.
19010  *
19011  *     Context: May be called from interrupt context
19012  */
19013 
19014 static void
19015 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19016     struct scsi_pkt *pktp)
19017 {
19018         ASSERT(un != NULL);
19019         ASSERT(mutex_owned(SD_MUTEX(un)));
19020         ASSERT(bp != NULL);
19021         ASSERT(xp != NULL);
19022         ASSERT(pktp != NULL);
19023 
19024         SD_UPDATE_ERRSTATS(un, sd_transerrs);
19025         sd_reset_target(un, pktp);
19026 
19027         SD_UPDATE_RESERVATION_STATUS(un, pktp);
19028 
19029         sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19030             sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19031 }
19032 
19033 
19034 
19035 /*
19036  *    Function: sd_pkt_status_check_condition
19037  *
19038  * Description: Recovery actions for a "STATUS_CHECK" SCSI command status.
19039  *
19040  *     Context: May be called from interrupt context
19041  */
19042 
19043 static void
19044 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
19045     struct sd_xbuf *xp, struct scsi_pkt *pktp)
19046 {
19047         ASSERT(un != NULL);
19048         ASSERT(mutex_owned(SD_MUTEX(un)));
19049         ASSERT(bp != NULL);
19050         ASSERT(xp != NULL);
19051         ASSERT(pktp != NULL);
19052 
19053         SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: "
19054             "entry: buf:0x%p xp:0x%p\n", bp, xp);
19055 
19056         /*
19057          * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the
19058          * command will be retried after the request sense). Otherwise, retry
19059          * the command. Note: we are issuing the request sense even though the
19060          * retry limit may have been reached for the failed command.
19061          */
19062         if (un->un_f_arq_enabled == FALSE) {
19063                 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19064                     "no ARQ, sending request sense command\n");
19065                 sd_send_request_sense_command(un, bp, SD_RETRIES_STANDARD,
19066                     pktp);
19067         } else {
19068                 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19069                     "ARQ,retrying request sense command\n");
19070                 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19071                     un->un_f_is_fibre ? drv_usectohz(100000) : (clock_t)0,
19072                     NULL);
19073         }
19074 
19075         SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n");
19076 }
19077 
19078 
19079 /*
19080  *    Function: sd_pkt_status_busy
19081  *
19082  * Description: Recovery actions for a "STATUS_BUSY" SCSI command status.
19083  *
19084  *     Context: May be called from interrupt context
19085  */
19086 
19087 static void
19088 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19089     struct scsi_pkt *pktp)
19090 {
19091         ASSERT(un != NULL);
19092         ASSERT(mutex_owned(SD_MUTEX(un)));
19093         ASSERT(bp != NULL);
19094         ASSERT(xp != NULL);
19095         ASSERT(pktp != NULL);
19096 
19097         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19098             "sd_pkt_status_busy: entry\n");
19099 
19100         /* If retries are exhausted, just fail the command. */
19101         if (xp->xb_retry_count >= un->un_busy_retry_count) {
19102                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
19103                     "device busy too long\n");
19104                 sd_return_failed_command(un, bp, EIO);
19105                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19106                     "sd_pkt_status_busy: exit\n");
19107                 return;
19108         }
19109         xp->xb_retry_count++;
19110 
19111         /*
19112          * Try to reset the target. However, we do not want to perform
19113          * more than one reset if the device continues to fail. The reset
19114          * will be performed when the retry count reaches the reset
19115          * threshold.  This threshold should be set such that at least
19116          * one retry is issued before the reset is performed.
19117          */
19118         if (xp->xb_retry_count ==
19119             ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) {
19120                 int rval = 0;
19121                 mutex_exit(SD_MUTEX(un));
19122                 if (un->un_f_allow_bus_device_reset == TRUE) {
19123                         /*
19124                          * First try to reset the LUN; if we cannot then
19125                          * try to reset the target.
19126                          */
19127                         if (un->un_f_lun_reset_enabled == TRUE) {
19128                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19129                                     "sd_pkt_status_busy: RESET_LUN\n");
19130                                 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19131                         }
19132                         if (rval == 0) {
19133                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19134                                     "sd_pkt_status_busy: RESET_TARGET\n");
19135                                 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19136                         }
19137                 }
19138                 if (rval == 0) {
19139                         /*
19140                          * If the RESET_LUN and/or RESET_TARGET failed,
19141                          * try RESET_ALL
19142                          */
19143                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19144                             "sd_pkt_status_busy: RESET_ALL\n");
19145                         rval = scsi_reset(SD_ADDRESS(un), RESET_ALL);
19146                 }
19147                 mutex_enter(SD_MUTEX(un));
19148                 if (rval == 0) {
19149                         /*
19150                          * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed.
19151                          * At this point we give up & fail the command.
19152                          */
19153                         sd_return_failed_command(un, bp, EIO);
19154                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19155                             "sd_pkt_status_busy: exit (failed cmd)\n");
19156                         return;
19157                 }
19158         }
19159 
19160         /*
19161          * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as
19162          * we have already checked the retry counts above.
19163          */
19164         sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL,
19165             EIO, un->un_busy_timeout, NULL);
19166 
19167         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19168             "sd_pkt_status_busy: exit\n");
19169 }
19170 
19171 
19172 /*
19173  *    Function: sd_pkt_status_reservation_conflict
19174  *
19175  * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI
19176  *              command status.
19177  *
19178  *     Context: May be called from interrupt context
19179  */
19180 
19181 static void
19182 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp,
19183     struct sd_xbuf *xp, struct scsi_pkt *pktp)
19184 {
19185         ASSERT(un != NULL);
19186         ASSERT(mutex_owned(SD_MUTEX(un)));
19187         ASSERT(bp != NULL);
19188         ASSERT(xp != NULL);
19189         ASSERT(pktp != NULL);
19190 
19191         /*
19192          * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation
19193          * conflict could be due to various reasons like incorrect keys, not
19194          * registered or not reserved etc. So, we return EACCES to the caller.
19195          */
19196         if (un->un_reservation_type == SD_SCSI3_RESERVATION) {
19197                 int cmd = SD_GET_PKT_OPCODE(pktp);
19198                 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) ||
19199                     (cmd == SCMD_PERSISTENT_RESERVE_OUT)) {
19200                         sd_return_failed_command(un, bp, EACCES);
19201                         return;
19202                 }
19203         }
19204 
19205         un->un_resvd_status |= SD_RESERVATION_CONFLICT;
19206 
19207         if ((un->un_resvd_status & SD_FAILFAST) != 0) {
19208                 if (sd_failfast_enable != 0) {
19209                         /* By definition, we must panic here.... */
19210                         sd_panic_for_res_conflict(un);
19211                         /*NOTREACHED*/
19212                 }
19213                 SD_ERROR(SD_LOG_IO, un,
19214                     "sd_handle_resv_conflict: Disk Reserved\n");
19215                 sd_return_failed_command(un, bp, EACCES);
19216                 return;
19217         }
19218 
19219         /*
19220          * 1147670: retry only if sd_retry_on_reservation_conflict
19221          * property is set (default is 1). Retries will not succeed
19222          * on a disk reserved by another initiator. HA systems
19223          * may reset this via sd.conf to avoid these retries.
19224          *
19225          * Note: The legacy return code for this failure is EIO, however EACCES
19226          * seems more appropriate for a reservation conflict.
19227          */
19228         if (sd_retry_on_reservation_conflict == 0) {
19229                 SD_ERROR(SD_LOG_IO, un,
19230                     "sd_handle_resv_conflict: Device Reserved\n");
19231                 sd_return_failed_command(un, bp, EIO);
19232                 return;
19233         }
19234 
19235         /*
19236          * Retry the command if we can.
19237          *
19238          * Note: The legacy return code for this failure is EIO, however EACCES
19239          * seems more appropriate for a reservation conflict.
19240          */
19241         sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19242             (clock_t)2, NULL);
19243 }
19244 
19245 
19246 
19247 /*
19248  *    Function: sd_pkt_status_qfull
19249  *
19250  * Description: Handle a QUEUE FULL condition from the target.  This can
19251  *              occur if the HBA does not handle the queue full condition.
19252  *              (Basically this means third-party HBAs as Sun HBAs will
19253  *              handle the queue full condition.)  Note that if there are
19254  *              some commands already in the transport, then the queue full
19255  *              has occurred because the queue for this nexus is actually
19256  *              full. If there are no commands in the transport, then the
19257  *              queue full is resulting from some other initiator or lun
19258  *              consuming all the resources at the target.
19259  *
19260  *     Context: May be called from interrupt context
19261  */
19262 
19263 static void
19264 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19265     struct scsi_pkt *pktp)
19266 {
19267         ASSERT(un != NULL);
19268         ASSERT(mutex_owned(SD_MUTEX(un)));
19269         ASSERT(bp != NULL);
19270         ASSERT(xp != NULL);
19271         ASSERT(pktp != NULL);
19272 
19273         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19274             "sd_pkt_status_qfull: entry\n");
19275 
19276         /*
19277          * Just lower the QFULL throttle and retry the command.  Note that
19278          * we do not limit the number of retries here.
19279          */
19280         sd_reduce_throttle(un, SD_THROTTLE_QFULL);
19281         sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0,
19282             SD_RESTART_TIMEOUT, NULL);
19283 
19284         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19285             "sd_pkt_status_qfull: exit\n");
19286 }
19287 
19288 
19289 /*
19290  *    Function: sd_reset_target
19291  *
19292  * Description: Issue a scsi_reset(9F), with either RESET_LUN,
19293  *              RESET_TARGET, or RESET_ALL.
19294  *
19295  *     Context: May be called under interrupt context.
19296  */
19297 
19298 static void
19299 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp)
19300 {
19301         int rval = 0;
19302 
19303         ASSERT(un != NULL);
19304         ASSERT(mutex_owned(SD_MUTEX(un)));
19305         ASSERT(pktp != NULL);
19306 
19307         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n");
19308 
19309         /*
19310          * No need to reset if the transport layer has already done so.
19311          */
19312         if ((pktp->pkt_statistics &
19313             (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) {
19314                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19315                     "sd_reset_target: no reset\n");
19316                 return;
19317         }
19318 
19319         mutex_exit(SD_MUTEX(un));
19320 
19321         if (un->un_f_allow_bus_device_reset == TRUE) {
19322                 if (un->un_f_lun_reset_enabled == TRUE) {
19323                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19324                             "sd_reset_target: RESET_LUN\n");
19325                         rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19326                 }
19327                 if (rval == 0) {
19328                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19329                             "sd_reset_target: RESET_TARGET\n");
19330                         rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19331                 }
19332         }
19333 
19334         if (rval == 0) {
19335                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19336                     "sd_reset_target: RESET_ALL\n");
19337                 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
19338         }
19339 
19340         mutex_enter(SD_MUTEX(un));
19341 
19342         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n");
19343 }
19344 
19345 /*
19346  *    Function: sd_target_change_task
19347  *
19348  * Description: Handle dynamic target change
19349  *
19350  *     Context: Executes in a taskq() thread context
19351  */
19352 static void
19353 sd_target_change_task(void *arg)
19354 {
19355         struct sd_lun           *un = arg;
19356         uint64_t                capacity;
19357         diskaddr_t              label_cap;
19358         uint_t                  lbasize;
19359         sd_ssc_t                *ssc;
19360 
19361         ASSERT(un != NULL);
19362         ASSERT(!mutex_owned(SD_MUTEX(un)));
19363 
19364         if ((un->un_f_blockcount_is_valid == FALSE) ||
19365             (un->un_f_tgt_blocksize_is_valid == FALSE)) {
19366                 return;
19367         }
19368 
19369         ssc = sd_ssc_init(un);
19370 
19371         if (sd_send_scsi_READ_CAPACITY(ssc, &capacity,
19372             &lbasize, SD_PATH_DIRECT) != 0) {
19373                 SD_ERROR(SD_LOG_ERROR, un,
19374                     "sd_target_change_task: fail to read capacity\n");
19375                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19376                 goto task_exit;
19377         }
19378 
19379         mutex_enter(SD_MUTEX(un));
19380         if (capacity <= un->un_blockcount) {
19381                 mutex_exit(SD_MUTEX(un));
19382                 goto task_exit;
19383         }
19384 
19385         sd_update_block_info(un, lbasize, capacity);
19386         mutex_exit(SD_MUTEX(un));
19387 
19388         /*
19389          * If lun is EFI labeled and lun capacity is greater than the
19390          * capacity contained in the label, log a sys event.
19391          */
19392         if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
19393             (void*)SD_PATH_DIRECT) == 0) {
19394                 mutex_enter(SD_MUTEX(un));
19395                 if (un->un_f_blockcount_is_valid &&
19396                     un->un_blockcount > label_cap) {
19397                         mutex_exit(SD_MUTEX(un));
19398                         sd_log_lun_expansion_event(un, KM_SLEEP);
19399                 } else {
19400                         mutex_exit(SD_MUTEX(un));
19401                 }
19402         }
19403 
19404 task_exit:
19405         sd_ssc_fini(ssc);
19406 }
19407 
19408 
19409 /*
19410  *    Function: sd_log_dev_status_event
19411  *
19412  * Description: Log EC_dev_status sysevent
19413  *
19414  *     Context: Never called from interrupt context
19415  */
19416 static void
19417 sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag)
19418 {
19419         int err;
19420         char                    *path;
19421         nvlist_t                *attr_list;
19422 
19423         /* Allocate and build sysevent attribute list */
19424         err = nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, km_flag);
19425         if (err != 0) {
19426                 SD_ERROR(SD_LOG_ERROR, un,
19427                     "sd_log_dev_status_event: fail to allocate space\n");
19428                 return;
19429         }
19430 
19431         path = kmem_alloc(MAXPATHLEN, km_flag);
19432         if (path == NULL) {
19433                 nvlist_free(attr_list);
19434                 SD_ERROR(SD_LOG_ERROR, un,
19435                     "sd_log_dev_status_event: fail to allocate space\n");
19436                 return;
19437         }
19438         /*
19439          * Add path attribute to identify the lun.
19440          * We are using minor node 'a' as the sysevent attribute.
19441          */
19442         (void) snprintf(path, MAXPATHLEN, "/devices");
19443         (void) ddi_pathname(SD_DEVINFO(un), path + strlen(path));
19444         (void) snprintf(path + strlen(path), MAXPATHLEN - strlen(path),
19445             ":a");
19446 
19447         err = nvlist_add_string(attr_list, DEV_PHYS_PATH, path);
19448         if (err != 0) {
19449                 nvlist_free(attr_list);
19450                 kmem_free(path, MAXPATHLEN);
19451                 SD_ERROR(SD_LOG_ERROR, un,
19452                     "sd_log_dev_status_event: fail to add attribute\n");
19453                 return;
19454         }
19455 
19456         /* Log dynamic lun expansion sysevent */
19457         err = ddi_log_sysevent(SD_DEVINFO(un), SUNW_VENDOR, EC_DEV_STATUS,
19458             esc, attr_list, NULL, km_flag);
19459         if (err != DDI_SUCCESS) {
19460                 SD_ERROR(SD_LOG_ERROR, un,
19461                     "sd_log_dev_status_event: fail to log sysevent\n");
19462         }
19463 
19464         nvlist_free(attr_list);
19465         kmem_free(path, MAXPATHLEN);
19466 }
19467 
19468 
19469 /*
19470  *    Function: sd_log_lun_expansion_event
19471  *
19472  * Description: Log lun expansion sys event
19473  *
19474  *     Context: Never called from interrupt context
19475  */
19476 static void
19477 sd_log_lun_expansion_event(struct sd_lun *un, int km_flag)
19478 {
19479         sd_log_dev_status_event(un, ESC_DEV_DLE, km_flag);
19480 }
19481 
19482 
19483 /*
19484  *    Function: sd_log_eject_request_event
19485  *
19486  * Description: Log eject request sysevent
19487  *
19488  *     Context: Never called from interrupt context
19489  */
19490 static void
19491 sd_log_eject_request_event(struct sd_lun *un, int km_flag)
19492 {
19493         sd_log_dev_status_event(un, ESC_DEV_EJECT_REQUEST, km_flag);
19494 }
19495 
19496 
19497 /*
19498  *    Function: sd_media_change_task
19499  *
19500  * Description: Recovery action for CDROM to become available.
19501  *
19502  *     Context: Executes in a taskq() thread context
19503  */
19504 
19505 static void
19506 sd_media_change_task(void *arg)
19507 {
19508         struct  scsi_pkt        *pktp = arg;
19509         struct  sd_lun          *un;
19510         struct  buf             *bp;
19511         struct  sd_xbuf         *xp;
19512         int     err             = 0;
19513         int     retry_count     = 0;
19514         int     retry_limit     = SD_UNIT_ATTENTION_RETRY/10;
19515         struct  sd_sense_info   si;
19516 
19517         ASSERT(pktp != NULL);
19518         bp = (struct buf *)pktp->pkt_private;
19519         ASSERT(bp != NULL);
19520         xp = SD_GET_XBUF(bp);
19521         ASSERT(xp != NULL);
19522         un = SD_GET_UN(bp);
19523         ASSERT(un != NULL);
19524         ASSERT(!mutex_owned(SD_MUTEX(un)));
19525         ASSERT(un->un_f_monitor_media_state);
19526 
19527         si.ssi_severity = SCSI_ERR_INFO;
19528         si.ssi_pfa_flag = FALSE;
19529 
19530         /*
19531          * When a reset is issued on a CDROM, it takes a long time to
19532          * recover. First few attempts to read capacity and other things
19533          * related to handling unit attention fail (with a ASC 0x4 and
19534          * ASCQ 0x1). In that case we want to do enough retries and we want
19535          * to limit the retries in other cases of genuine failures like
19536          * no media in drive.
19537          */
19538         while (retry_count++ < retry_limit) {
19539                 if ((err = sd_handle_mchange(un)) == 0) {
19540                         break;
19541                 }
19542                 if (err == EAGAIN) {
19543                         retry_limit = SD_UNIT_ATTENTION_RETRY;
19544                 }
19545                 /* Sleep for 0.5 sec. & try again */
19546                 delay(drv_usectohz(500000));
19547         }
19548 
19549         /*
19550          * Dispatch (retry or fail) the original command here,
19551          * along with appropriate console messages....
19552          *
19553          * Must grab the mutex before calling sd_retry_command,
19554          * sd_print_sense_msg and sd_return_failed_command.
19555          */
19556         mutex_enter(SD_MUTEX(un));
19557         if (err != SD_CMD_SUCCESS) {
19558                 SD_UPDATE_ERRSTATS(un, sd_harderrs);
19559                 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
19560                 si.ssi_severity = SCSI_ERR_FATAL;
19561                 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
19562                 sd_return_failed_command(un, bp, EIO);
19563         } else {
19564                 sd_retry_command(un, bp, SD_RETRIES_UA, sd_print_sense_msg,
19565                     &si, EIO, (clock_t)0, NULL);
19566         }
19567         mutex_exit(SD_MUTEX(un));
19568 }
19569 
19570 
19571 
19572 /*
19573  *    Function: sd_handle_mchange
19574  *
19575  * Description: Perform geometry validation & other recovery when CDROM
19576  *              has been removed from drive.
19577  *
19578  * Return Code: 0 for success
19579  *              errno-type return code of either sd_send_scsi_DOORLOCK() or
19580  *              sd_send_scsi_READ_CAPACITY()
19581  *
19582  *     Context: Executes in a taskq() thread context
19583  */
19584 
19585 static int
19586 sd_handle_mchange(struct sd_lun *un)
19587 {
19588         uint64_t        capacity;
19589         uint32_t        lbasize;
19590         int             rval;
19591         sd_ssc_t        *ssc;
19592 
19593         ASSERT(!mutex_owned(SD_MUTEX(un)));
19594         ASSERT(un->un_f_monitor_media_state);
19595 
19596         ssc = sd_ssc_init(un);
19597         rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
19598             SD_PATH_DIRECT_PRIORITY);
19599 
19600         if (rval != 0)
19601                 goto failed;
19602 
19603         mutex_enter(SD_MUTEX(un));
19604         sd_update_block_info(un, lbasize, capacity);
19605 
19606         if (un->un_errstats != NULL) {
19607                 struct  sd_errstats *stp =
19608                     (struct sd_errstats *)un->un_errstats->ks_data;
19609                 stp->sd_capacity.value.ui64 = (uint64_t)
19610                     ((uint64_t)un->un_blockcount *
19611                     (uint64_t)un->un_tgt_blocksize);
19612         }
19613 
19614         /*
19615          * Check if the media in the device is writable or not
19616          */
19617         if (ISCD(un)) {
19618                 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT_PRIORITY);
19619         }
19620 
19621         /*
19622          * Note: Maybe let the strategy/partitioning chain worry about getting
19623          * valid geometry.
19624          */
19625         mutex_exit(SD_MUTEX(un));
19626         cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
19627 
19628 
19629         if (cmlb_validate(un->un_cmlbhandle, 0,
19630             (void *)SD_PATH_DIRECT_PRIORITY) != 0) {
19631                 sd_ssc_fini(ssc);
19632                 return (EIO);
19633         } else {
19634                 if (un->un_f_pkstats_enabled) {
19635                         sd_set_pstats(un);
19636                         SD_TRACE(SD_LOG_IO_PARTITION, un,
19637                             "sd_handle_mchange: un:0x%p pstats created and "
19638                             "set\n", un);
19639                 }
19640         }
19641 
19642         /*
19643          * Try to lock the door
19644          */
19645         rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
19646             SD_PATH_DIRECT_PRIORITY);
19647 failed:
19648         if (rval != 0)
19649                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19650         sd_ssc_fini(ssc);
19651         return (rval);
19652 }
19653 
19654 
19655 /*
19656  *    Function: sd_send_scsi_DOORLOCK
19657  *
19658  * Description: Issue the scsi DOOR LOCK command
19659  *
19660  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
19661  *                      structure for this target.
19662  *              flag  - SD_REMOVAL_ALLOW
19663  *                      SD_REMOVAL_PREVENT
19664  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19665  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19666  *                      to use the USCSI "direct" chain and bypass the normal
19667  *                      command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19668  *                      command is issued as part of an error recovery action.
19669  *
19670  * Return Code: 0   - Success
19671  *              errno return code from sd_ssc_send()
19672  *
19673  *     Context: Can sleep.
19674  */
19675 
19676 static int
19677 sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag)
19678 {
19679         struct scsi_extended_sense      sense_buf;
19680         union scsi_cdb          cdb;
19681         struct uscsi_cmd        ucmd_buf;
19682         int                     status;
19683         struct sd_lun           *un;
19684 
19685         ASSERT(ssc != NULL);
19686         un = ssc->ssc_un;
19687         ASSERT(un != NULL);
19688         ASSERT(!mutex_owned(SD_MUTEX(un)));
19689 
19690         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un);
19691 
19692         /* already determined doorlock is not supported, fake success */
19693         if (un->un_f_doorlock_supported == FALSE) {
19694                 return (0);
19695         }
19696 
19697         /*
19698          * If we are ejecting and see an SD_REMOVAL_PREVENT
19699          * ignore the command so we can complete the eject
19700          * operation.
19701          */
19702         if (flag == SD_REMOVAL_PREVENT) {
19703                 mutex_enter(SD_MUTEX(un));
19704                 if (un->un_f_ejecting == TRUE) {
19705                         mutex_exit(SD_MUTEX(un));
19706                         return (EAGAIN);
19707                 }
19708                 mutex_exit(SD_MUTEX(un));
19709         }
19710 
19711         bzero(&cdb, sizeof (cdb));
19712         bzero(&ucmd_buf, sizeof (ucmd_buf));
19713 
19714         cdb.scc_cmd = SCMD_DOORLOCK;
19715         cdb.cdb_opaque[4] = (uchar_t)flag;
19716 
19717         ucmd_buf.uscsi_cdb      = (char *)&cdb;
19718         ucmd_buf.uscsi_cdblen   = CDB_GROUP0;
19719         ucmd_buf.uscsi_bufaddr  = NULL;
19720         ucmd_buf.uscsi_buflen   = 0;
19721         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
19722         ucmd_buf.uscsi_rqlen    = sizeof (sense_buf);
19723         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_SILENT;
19724         ucmd_buf.uscsi_timeout  = 15;
19725 
19726         SD_TRACE(SD_LOG_IO, un,
19727             "sd_send_scsi_DOORLOCK: returning sd_ssc_send\n");
19728 
19729         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
19730             UIO_SYSSPACE, path_flag);
19731 
19732         if (status == 0)
19733                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
19734 
19735         if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) &&
19736             (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19737             (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) {
19738                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19739 
19740                 /* fake success and skip subsequent doorlock commands */
19741                 un->un_f_doorlock_supported = FALSE;
19742                 return (0);
19743         }
19744 
19745         return (status);
19746 }
19747 
19748 /*
19749  *    Function: sd_send_scsi_READ_CAPACITY
19750  *
19751  * Description: This routine uses the scsi READ CAPACITY command to determine
19752  *              the device capacity in number of blocks and the device native
19753  *              block size. If this function returns a failure, then the
19754  *              values in *capp and *lbap are undefined.  If the capacity
19755  *              returned is 0xffffffff then the lun is too large for a
19756  *              normal READ CAPACITY command and the results of a
19757  *              READ CAPACITY 16 will be used instead.
19758  *
19759  *   Arguments: ssc   - ssc contains ptr to soft state struct for the target
19760  *              capp - ptr to unsigned 64-bit variable to receive the
19761  *                      capacity value from the command.
19762  *              lbap - ptr to unsigned 32-bit varaible to receive the
19763  *                      block size value from the command
19764  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19765  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19766  *                      to use the USCSI "direct" chain and bypass the normal
19767  *                      command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19768  *                      command is issued as part of an error recovery action.
19769  *
19770  * Return Code: 0   - Success
19771  *              EIO - IO error
19772  *              EACCES - Reservation conflict detected
19773  *              EAGAIN - Device is becoming ready
19774  *              errno return code from sd_ssc_send()
19775  *
19776  *     Context: Can sleep.  Blocks until command completes.
19777  */
19778 
19779 #define SD_CAPACITY_SIZE        sizeof (struct scsi_capacity)
19780 
19781 static int
19782 sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap,
19783     int path_flag)
19784 {
19785         struct  scsi_extended_sense     sense_buf;
19786         struct  uscsi_cmd       ucmd_buf;
19787         union   scsi_cdb        cdb;
19788         uint32_t                *capacity_buf;
19789         uint64_t                capacity;
19790         uint32_t                lbasize;
19791         uint32_t                pbsize;
19792         int                     status;
19793         struct sd_lun           *un;
19794 
19795         ASSERT(ssc != NULL);
19796 
19797         un = ssc->ssc_un;
19798         ASSERT(un != NULL);
19799         ASSERT(!mutex_owned(SD_MUTEX(un)));
19800         ASSERT(capp != NULL);
19801         ASSERT(lbap != NULL);
19802 
19803         SD_TRACE(SD_LOG_IO, un,
19804             "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
19805 
19806         /*
19807          * First send a READ_CAPACITY command to the target.
19808          * (This command is mandatory under SCSI-2.)
19809          *
19810          * Set up the CDB for the READ_CAPACITY command.  The Partial
19811          * Medium Indicator bit is cleared.  The address field must be
19812          * zero if the PMI bit is zero.
19813          */
19814         bzero(&cdb, sizeof (cdb));
19815         bzero(&ucmd_buf, sizeof (ucmd_buf));
19816 
19817         capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP);
19818 
19819         cdb.scc_cmd = SCMD_READ_CAPACITY;
19820 
19821         ucmd_buf.uscsi_cdb      = (char *)&cdb;
19822         ucmd_buf.uscsi_cdblen   = CDB_GROUP1;
19823         ucmd_buf.uscsi_bufaddr  = (caddr_t)capacity_buf;
19824         ucmd_buf.uscsi_buflen   = SD_CAPACITY_SIZE;
19825         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
19826         ucmd_buf.uscsi_rqlen    = sizeof (sense_buf);
19827         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
19828         ucmd_buf.uscsi_timeout  = un->un_uscsi_timeout;
19829 
19830         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
19831             UIO_SYSSPACE, path_flag);
19832 
19833         switch (status) {
19834         case 0:
19835                 /* Return failure if we did not get valid capacity data. */
19836                 if (ucmd_buf.uscsi_resid != 0) {
19837                         sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
19838                             "sd_send_scsi_READ_CAPACITY received invalid "
19839                             "capacity data");
19840                         kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19841                         return (EIO);
19842                 }
19843                 /*
19844                  * Read capacity and block size from the READ CAPACITY 10 data.
19845                  * This data may be adjusted later due to device specific
19846                  * issues.
19847                  *
19848                  * According to the SCSI spec, the READ CAPACITY 10
19849                  * command returns the following:
19850                  *
19851                  *  bytes 0-3: Maximum logical block address available.
19852                  *              (MSB in byte:0 & LSB in byte:3)
19853                  *
19854                  *  bytes 4-7: Block length in bytes
19855                  *              (MSB in byte:4 & LSB in byte:7)
19856                  *
19857                  */
19858                 capacity = BE_32(capacity_buf[0]);
19859                 lbasize = BE_32(capacity_buf[1]);
19860 
19861                 /*
19862                  * Done with capacity_buf
19863                  */
19864                 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19865 
19866                 /*
19867                  * if the reported capacity is set to all 0xf's, then
19868                  * this disk is too large and requires SBC-2 commands.
19869                  * Reissue the request using READ CAPACITY 16.
19870                  */
19871                 if (capacity == 0xffffffff) {
19872                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19873                         status = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity,
19874                             &lbasize, &pbsize, path_flag);
19875                         if (status != 0) {
19876                                 return (status);
19877                         } else {
19878                                 goto rc16_done;
19879                         }
19880                 }
19881                 break;  /* Success! */
19882         case EIO:
19883                 switch (ucmd_buf.uscsi_status) {
19884                 case STATUS_RESERVATION_CONFLICT:
19885                         status = EACCES;
19886                         break;
19887                 case STATUS_CHECK:
19888                         /*
19889                          * Check condition; look for ASC/ASCQ of 0x04/0x01
19890                          * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
19891                          */
19892                         if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19893                             (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
19894                             (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
19895                                 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19896                                 return (EAGAIN);
19897                         }
19898                         break;
19899                 default:
19900                         break;
19901                 }
19902                 /* FALLTHRU */
19903         default:
19904                 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19905                 return (status);
19906         }
19907 
19908         /*
19909          * Some ATAPI CD-ROM drives report inaccurate LBA size values
19910          * (2352 and 0 are common) so for these devices always force the value
19911          * to 2048 as required by the ATAPI specs.
19912          */
19913         if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
19914                 lbasize = 2048;
19915         }
19916 
19917         /*
19918          * Get the maximum LBA value from the READ CAPACITY data.
19919          * Here we assume that the Partial Medium Indicator (PMI) bit
19920          * was cleared when issuing the command. This means that the LBA
19921          * returned from the device is the LBA of the last logical block
19922          * on the logical unit.  The actual logical block count will be
19923          * this value plus one.
19924          */
19925         capacity += 1;
19926 
19927         /*
19928          * Currently, for removable media, the capacity is saved in terms
19929          * of un->un_sys_blocksize, so scale the capacity value to reflect this.
19930          */
19931         if (un->un_f_has_removable_media)
19932                 capacity *= (lbasize / un->un_sys_blocksize);
19933 
19934 rc16_done:
19935 
19936         /*
19937          * Copy the values from the READ CAPACITY command into the space
19938          * provided by the caller.
19939          */
19940         *capp = capacity;
19941         *lbap = lbasize;
19942 
19943         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: "
19944             "capacity:0x%llx  lbasize:0x%x\n", capacity, lbasize);
19945 
19946         /*
19947          * Both the lbasize and capacity from the device must be nonzero,
19948          * otherwise we assume that the values are not valid and return
19949          * failure to the caller. (4203735)
19950          */
19951         if ((capacity == 0) || (lbasize == 0)) {
19952                 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
19953                     "sd_send_scsi_READ_CAPACITY received invalid value "
19954                     "capacity %llu lbasize %d", capacity, lbasize);
19955                 return (EIO);
19956         }
19957         sd_ssc_assessment(ssc, SD_FMT_STANDARD);
19958         return (0);
19959 }
19960 
19961 /*
19962  *    Function: sd_send_scsi_READ_CAPACITY_16
19963  *
19964  * Description: This routine uses the scsi READ CAPACITY 16 command to
19965  *              determine the device capacity in number of blocks and the
19966  *              device native block size.  If this function returns a failure,
19967  *              then the values in *capp and *lbap are undefined.
19968  *              This routine should be called by sd_send_scsi_READ_CAPACITY
19969  *              which will apply any device specific adjustments to capacity
19970  *              and lbasize. One exception is it is also called by
19971  *              sd_get_media_info_ext. In that function, there is no need to
19972  *              adjust the capacity and lbasize.
19973  *
19974  *   Arguments: ssc   - ssc contains ptr to soft state struct for the target
19975  *              capp - ptr to unsigned 64-bit variable to receive the
19976  *                      capacity value from the command.
19977  *              lbap - ptr to unsigned 32-bit varaible to receive the
19978  *                      block size value from the command
19979  *              psp  - ptr to unsigned 32-bit variable to receive the
19980  *                      physical block size value from the command
19981  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19982  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19983  *                      to use the USCSI "direct" chain and bypass the normal
19984  *                      command waitq. SD_PATH_DIRECT_PRIORITY is used when
19985  *                      this command is issued as part of an error recovery
19986  *                      action.
19987  *
19988  * Return Code: 0   - Success
19989  *              EIO - IO error
19990  *              EACCES - Reservation conflict detected
19991  *              EAGAIN - Device is becoming ready
19992  *              errno return code from sd_ssc_send()
19993  *
19994  *     Context: Can sleep.  Blocks until command completes.
19995  */
19996 
19997 #define SD_CAPACITY_16_SIZE     sizeof (struct scsi_capacity_16)
19998 
19999 static int
20000 sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap,
20001     uint32_t *psp, int path_flag)
20002 {
20003         struct  scsi_extended_sense     sense_buf;
20004         struct  uscsi_cmd       ucmd_buf;
20005         union   scsi_cdb        cdb;
20006         uint64_t                *capacity16_buf;
20007         uint64_t                capacity;
20008         uint32_t                lbasize;
20009         uint32_t                pbsize;
20010         uint32_t                lbpb_exp;
20011         int                     status;
20012         struct sd_lun           *un;
20013 
20014         ASSERT(ssc != NULL);
20015 
20016         un = ssc->ssc_un;
20017         ASSERT(un != NULL);
20018         ASSERT(!mutex_owned(SD_MUTEX(un)));
20019         ASSERT(capp != NULL);
20020         ASSERT(lbap != NULL);
20021 
20022         SD_TRACE(SD_LOG_IO, un,
20023             "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
20024 
20025         /*
20026          * First send a READ_CAPACITY_16 command to the target.
20027          *
20028          * Set up the CDB for the READ_CAPACITY_16 command.  The Partial
20029          * Medium Indicator bit is cleared.  The address field must be
20030          * zero if the PMI bit is zero.
20031          */
20032         bzero(&cdb, sizeof (cdb));
20033         bzero(&ucmd_buf, sizeof (ucmd_buf));
20034 
20035         capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP);
20036 
20037         ucmd_buf.uscsi_cdb      = (char *)&cdb;
20038         ucmd_buf.uscsi_cdblen   = CDB_GROUP4;
20039         ucmd_buf.uscsi_bufaddr  = (caddr_t)capacity16_buf;
20040         ucmd_buf.uscsi_buflen   = SD_CAPACITY_16_SIZE;
20041         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
20042         ucmd_buf.uscsi_rqlen    = sizeof (sense_buf);
20043         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20044         ucmd_buf.uscsi_timeout  = un->un_uscsi_timeout;
20045 
20046         /*
20047          * Read Capacity (16) is a Service Action In command.  One
20048          * command byte (0x9E) is overloaded for multiple operations,
20049          * with the second CDB byte specifying the desired operation
20050          */
20051         cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4;
20052         cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4;
20053 
20054         /*
20055          * Fill in allocation length field
20056          */
20057         FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen);
20058 
20059         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20060             UIO_SYSSPACE, path_flag);
20061 
20062         switch (status) {
20063         case 0:
20064                 /* Return failure if we did not get valid capacity data. */
20065                 if (ucmd_buf.uscsi_resid > 20) {
20066                         sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20067                             "sd_send_scsi_READ_CAPACITY_16 received invalid "
20068                             "capacity data");
20069                         kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20070                         return (EIO);
20071                 }
20072 
20073                 /*
20074                  * Read capacity and block size from the READ CAPACITY 16 data.
20075                  * This data may be adjusted later due to device specific
20076                  * issues.
20077                  *
20078                  * According to the SCSI spec, the READ CAPACITY 16
20079                  * command returns the following:
20080                  *
20081                  *  bytes 0-7: Maximum logical block address available.
20082                  *              (MSB in byte:0 & LSB in byte:7)
20083                  *
20084                  *  bytes 8-11: Block length in bytes
20085                  *              (MSB in byte:8 & LSB in byte:11)
20086                  *
20087                  *  byte 13: LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT
20088                  *
20089                  *  byte 14:
20090                  *      bit 7: Thin-Provisioning Enabled
20091                  *      bit 6: Thin-Provisioning Read Zeros
20092                  */
20093                 capacity = BE_64(capacity16_buf[0]);
20094                 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]);
20095                 lbpb_exp = (BE_64(capacity16_buf[1]) >> 16) & 0x0f;
20096 
20097                 un->un_thin_flags = 0;
20098                 if (((uint8_t *)capacity16_buf)[14] & (1 << 7))
20099                         un->un_thin_flags |= SD_THIN_PROV_ENABLED;
20100                 if (((uint8_t *)capacity16_buf)[14] & (1 << 6))
20101                         un->un_thin_flags |= SD_THIN_PROV_READ_ZEROS;
20102 
20103                 pbsize = lbasize << lbpb_exp;
20104 
20105                 /*
20106                  * Done with capacity16_buf
20107                  */
20108                 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20109 
20110                 /*
20111                  * if the reported capacity is set to all 0xf's, then
20112                  * this disk is too large.  This could only happen with
20113                  * a device that supports LBAs larger than 64 bits which
20114                  * are not defined by any current T10 standards.
20115                  */
20116                 if (capacity == 0xffffffffffffffff) {
20117                         sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20118                             "disk is too large");
20119                         return (EIO);
20120                 }
20121                 break;  /* Success! */
20122         case EIO:
20123                 switch (ucmd_buf.uscsi_status) {
20124                 case STATUS_RESERVATION_CONFLICT:
20125                         status = EACCES;
20126                         break;
20127                 case STATUS_CHECK:
20128                         /*
20129                          * Check condition; look for ASC/ASCQ of 0x04/0x01
20130                          * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
20131                          */
20132                         if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20133                             (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
20134                             (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
20135                                 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20136                                 return (EAGAIN);
20137                         }
20138                         break;
20139                 default:
20140                         break;
20141                 }
20142                 /* FALLTHRU */
20143         default:
20144                 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20145                 return (status);
20146         }
20147 
20148         /*
20149          * Some ATAPI CD-ROM drives report inaccurate LBA size values
20150          * (2352 and 0 are common) so for these devices always force the value
20151          * to 2048 as required by the ATAPI specs.
20152          */
20153         if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
20154                 lbasize = 2048;
20155         }
20156 
20157         /*
20158          * Get the maximum LBA value from the READ CAPACITY 16 data.
20159          * Here we assume that the Partial Medium Indicator (PMI) bit
20160          * was cleared when issuing the command. This means that the LBA
20161          * returned from the device is the LBA of the last logical block
20162          * on the logical unit.  The actual logical block count will be
20163          * this value plus one.
20164          */
20165         capacity += 1;
20166 
20167         /*
20168          * Currently, for removable media, the capacity is saved in terms
20169          * of un->un_sys_blocksize, so scale the capacity value to reflect this.
20170          */
20171         if (un->un_f_has_removable_media)
20172                 capacity *= (lbasize / un->un_sys_blocksize);
20173 
20174         *capp = capacity;
20175         *lbap = lbasize;
20176         *psp = pbsize;
20177 
20178         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: "
20179             "capacity:0x%llx  lbasize:0x%x, pbsize: 0x%x\n",
20180             capacity, lbasize, pbsize);
20181 
20182         if ((capacity == 0) || (lbasize == 0) || (pbsize == 0)) {
20183                 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20184                     "sd_send_scsi_READ_CAPACITY_16 received invalid value "
20185                     "capacity %llu lbasize %d pbsize %d", capacity, lbasize);
20186                 return (EIO);
20187         }
20188 
20189         sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20190         return (0);
20191 }
20192 
20193 
20194 /*
20195  *    Function: sd_send_scsi_START_STOP_UNIT
20196  *
20197  * Description: Issue a scsi START STOP UNIT command to the target.
20198  *
20199  *   Arguments: ssc    - ssc contatins pointer to driver soft state (unit)
20200  *                       structure for this target.
20201  *      pc_flag - SD_POWER_CONDITION
20202  *                SD_START_STOP
20203  *              flag  - SD_TARGET_START
20204  *                      SD_TARGET_STOP
20205  *                      SD_TARGET_EJECT
20206  *                      SD_TARGET_CLOSE
20207  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20208  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20209  *                      to use the USCSI "direct" chain and bypass the normal
20210  *                      command waitq. SD_PATH_DIRECT_PRIORITY is used when this
20211  *                      command is issued as part of an error recovery action.
20212  *
20213  * Return Code: 0   - Success
20214  *              EIO - IO error
20215  *              EACCES - Reservation conflict detected
20216  *              ENXIO  - Not Ready, medium not present
20217  *              errno return code from sd_ssc_send()
20218  *
20219  *     Context: Can sleep.
20220  */
20221 
20222 static int
20223 sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag, int flag,
20224     int path_flag)
20225 {
20226         struct  scsi_extended_sense     sense_buf;
20227         union scsi_cdb          cdb;
20228         struct uscsi_cmd        ucmd_buf;
20229         int                     status;
20230         struct sd_lun           *un;
20231 
20232         ASSERT(ssc != NULL);
20233         un = ssc->ssc_un;
20234         ASSERT(un != NULL);
20235         ASSERT(!mutex_owned(SD_MUTEX(un)));
20236 
20237         SD_TRACE(SD_LOG_IO, un,
20238             "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un);
20239 
20240         if (un->un_f_check_start_stop &&
20241             (pc_flag == SD_START_STOP) &&
20242             ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) &&
20243             (un->un_f_start_stop_supported != TRUE)) {
20244                 return (0);
20245         }
20246 
20247         /*
20248          * If we are performing an eject operation and
20249          * we receive any command other than SD_TARGET_EJECT
20250          * we should immediately return.
20251          */
20252         if (flag != SD_TARGET_EJECT) {
20253                 mutex_enter(SD_MUTEX(un));
20254                 if (un->un_f_ejecting == TRUE) {
20255                         mutex_exit(SD_MUTEX(un));
20256                         return (EAGAIN);
20257                 }
20258                 mutex_exit(SD_MUTEX(un));
20259         }
20260 
20261         bzero(&cdb, sizeof (cdb));
20262         bzero(&ucmd_buf, sizeof (ucmd_buf));
20263         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20264 
20265         cdb.scc_cmd = SCMD_START_STOP;
20266         cdb.cdb_opaque[4] = (pc_flag == SD_POWER_CONDITION) ?
20267             (uchar_t)(flag << 4) : (uchar_t)flag;
20268 
20269         ucmd_buf.uscsi_cdb      = (char *)&cdb;
20270         ucmd_buf.uscsi_cdblen   = CDB_GROUP0;
20271         ucmd_buf.uscsi_bufaddr  = NULL;
20272         ucmd_buf.uscsi_buflen   = 0;
20273         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
20274         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
20275         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_SILENT;
20276         ucmd_buf.uscsi_timeout  = 3 * un->un_uscsi_timeout;
20277 
20278         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20279             UIO_SYSSPACE, path_flag);
20280 
20281         switch (status) {
20282         case 0:
20283                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20284                 break;  /* Success! */
20285         case EIO:
20286                 switch (ucmd_buf.uscsi_status) {
20287                 case STATUS_RESERVATION_CONFLICT:
20288                         status = EACCES;
20289                         break;
20290                 case STATUS_CHECK:
20291                         if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) {
20292                                 switch (scsi_sense_key(
20293                                     (uint8_t *)&sense_buf)) {
20294                                 case KEY_ILLEGAL_REQUEST:
20295                                         status = ENOTSUP;
20296                                         break;
20297                                 case KEY_NOT_READY:
20298                                         if (scsi_sense_asc(
20299                                             (uint8_t *)&sense_buf)
20300                                             == 0x3A) {
20301                                                 status = ENXIO;
20302                                         }
20303                                         break;
20304                                 default:
20305                                         break;
20306                                 }
20307                         }
20308                         break;
20309                 default:
20310                         break;
20311                 }
20312                 break;
20313         default:
20314                 break;
20315         }
20316 
20317         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n");
20318 
20319         return (status);
20320 }
20321 
20322 
20323 /*
20324  *    Function: sd_start_stop_unit_callback
20325  *
20326  * Description: timeout(9F) callback to begin recovery process for a
20327  *              device that has spun down.
20328  *
20329  *   Arguments: arg - pointer to associated softstate struct.
20330  *
20331  *     Context: Executes in a timeout(9F) thread context
20332  */
20333 
20334 static void
20335 sd_start_stop_unit_callback(void *arg)
20336 {
20337         struct sd_lun   *un = arg;
20338         ASSERT(un != NULL);
20339         ASSERT(!mutex_owned(SD_MUTEX(un)));
20340 
20341         SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n");
20342 
20343         (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP);
20344 }
20345 
20346 
20347 /*
20348  *    Function: sd_start_stop_unit_task
20349  *
20350  * Description: Recovery procedure when a drive is spun down.
20351  *
20352  *   Arguments: arg - pointer to associated softstate struct.
20353  *
20354  *     Context: Executes in a taskq() thread context
20355  */
20356 
20357 static void
20358 sd_start_stop_unit_task(void *arg)
20359 {
20360         struct sd_lun   *un = arg;
20361         sd_ssc_t        *ssc;
20362         int             power_level;
20363         int             rval;
20364 
20365         ASSERT(un != NULL);
20366         ASSERT(!mutex_owned(SD_MUTEX(un)));
20367 
20368         SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n");
20369 
20370         /*
20371          * Some unformatted drives report not ready error, no need to
20372          * restart if format has been initiated.
20373          */
20374         mutex_enter(SD_MUTEX(un));
20375         if (un->un_f_format_in_progress == TRUE) {
20376                 mutex_exit(SD_MUTEX(un));
20377                 return;
20378         }
20379         mutex_exit(SD_MUTEX(un));
20380 
20381         ssc = sd_ssc_init(un);
20382         /*
20383          * When a START STOP command is issued from here, it is part of a
20384          * failure recovery operation and must be issued before any other
20385          * commands, including any pending retries. Thus it must be sent
20386          * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up
20387          * succeeds or not, we will start I/O after the attempt.
20388          * If power condition is supported and the current power level
20389          * is capable of performing I/O, we should set the power condition
20390          * to that level. Otherwise, set the power condition to ACTIVE.
20391          */
20392         if (un->un_f_power_condition_supported) {
20393                 mutex_enter(SD_MUTEX(un));
20394                 ASSERT(SD_PM_IS_LEVEL_VALID(un, un->un_power_level));
20395                 power_level = sd_pwr_pc.ran_perf[un->un_power_level]
20396                     > 0 ? un->un_power_level : SD_SPINDLE_ACTIVE;
20397                 mutex_exit(SD_MUTEX(un));
20398                 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
20399                     sd_pl2pc[power_level], SD_PATH_DIRECT_PRIORITY);
20400         } else {
20401                 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
20402                     SD_TARGET_START, SD_PATH_DIRECT_PRIORITY);
20403         }
20404 
20405         if (rval != 0)
20406                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20407         sd_ssc_fini(ssc);
20408         /*
20409          * The above call blocks until the START_STOP_UNIT command completes.
20410          * Now that it has completed, we must re-try the original IO that
20411          * received the NOT READY condition in the first place. There are
20412          * three possible conditions here:
20413          *
20414          *  (1) The original IO is on un_retry_bp.
20415          *  (2) The original IO is on the regular wait queue, and un_retry_bp
20416          *      is NULL.
20417          *  (3) The original IO is on the regular wait queue, and un_retry_bp
20418          *      points to some other, unrelated bp.
20419          *
20420          * For each case, we must call sd_start_cmds() with un_retry_bp
20421          * as the argument. If un_retry_bp is NULL, this will initiate
20422          * processing of the regular wait queue.  If un_retry_bp is not NULL,
20423          * then this will process the bp on un_retry_bp. That may or may not
20424          * be the original IO, but that does not matter: the important thing
20425          * is to keep the IO processing going at this point.
20426          *
20427          * Note: This is a very specific error recovery sequence associated
20428          * with a drive that is not spun up. We attempt a START_STOP_UNIT and
20429          * serialize the I/O with completion of the spin-up.
20430          */
20431         mutex_enter(SD_MUTEX(un));
20432         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
20433             "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n",
20434             un, un->un_retry_bp);
20435         un->un_startstop_timeid = NULL;      /* Timeout is no longer pending */
20436         sd_start_cmds(un, un->un_retry_bp);
20437         mutex_exit(SD_MUTEX(un));
20438 
20439         SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n");
20440 }
20441 
20442 
20443 /*
20444  *    Function: sd_send_scsi_INQUIRY
20445  *
20446  * Description: Issue the scsi INQUIRY command.
20447  *
20448  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20449  *                      structure for this target.
20450  *              bufaddr
20451  *              buflen
20452  *              evpd
20453  *              page_code
20454  *              page_length
20455  *
20456  * Return Code: 0   - Success
20457  *              errno return code from sd_ssc_send()
20458  *
20459  *     Context: Can sleep. Does not return until command is completed.
20460  */
20461 
20462 static int
20463 sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr, size_t buflen,
20464     uchar_t evpd, uchar_t page_code, size_t *residp)
20465 {
20466         union scsi_cdb          cdb;
20467         struct uscsi_cmd        ucmd_buf;
20468         int                     status;
20469         struct sd_lun           *un;
20470 
20471         ASSERT(ssc != NULL);
20472         un = ssc->ssc_un;
20473         ASSERT(un != NULL);
20474         ASSERT(!mutex_owned(SD_MUTEX(un)));
20475         ASSERT(bufaddr != NULL);
20476 
20477         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un);
20478 
20479         bzero(&cdb, sizeof (cdb));
20480         bzero(&ucmd_buf, sizeof (ucmd_buf));
20481         bzero(bufaddr, buflen);
20482 
20483         cdb.scc_cmd = SCMD_INQUIRY;
20484         cdb.cdb_opaque[1] = evpd;
20485         cdb.cdb_opaque[2] = page_code;
20486         FORMG0COUNT(&cdb, buflen);
20487 
20488         ucmd_buf.uscsi_cdb      = (char *)&cdb;
20489         ucmd_buf.uscsi_cdblen   = CDB_GROUP0;
20490         ucmd_buf.uscsi_bufaddr  = (caddr_t)bufaddr;
20491         ucmd_buf.uscsi_buflen   = buflen;
20492         ucmd_buf.uscsi_rqbuf    = NULL;
20493         ucmd_buf.uscsi_rqlen    = 0;
20494         ucmd_buf.uscsi_flags    = USCSI_READ | USCSI_SILENT;
20495         ucmd_buf.uscsi_timeout  = 2 * un->un_uscsi_timeout;
20496 
20497         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20498             UIO_SYSSPACE, SD_PATH_DIRECT);
20499 
20500         /*
20501          * Only handle status == 0, the upper-level caller
20502          * will put different assessment based on the context.
20503          */
20504         if (status == 0)
20505                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20506 
20507         if ((status == 0) && (residp != NULL)) {
20508                 *residp = ucmd_buf.uscsi_resid;
20509         }
20510 
20511         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n");
20512 
20513         return (status);
20514 }
20515 
20516 
20517 /*
20518  *    Function: sd_send_scsi_TEST_UNIT_READY
20519  *
20520  * Description: Issue the scsi TEST UNIT READY command.
20521  *              This routine can be told to set the flag USCSI_DIAGNOSE to
20522  *              prevent retrying failed commands. Use this when the intent
20523  *              is either to check for device readiness, to clear a Unit
20524  *              Attention, or to clear any outstanding sense data.
20525  *              However under specific conditions the expected behavior
20526  *              is for retries to bring a device ready, so use the flag
20527  *              with caution.
20528  *
20529  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20530  *                      structure for this target.
20531  *              flag:   SD_CHECK_FOR_MEDIA: return ENXIO if no media present
20532  *                      SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE.
20533  *                      0: dont check for media present, do retries on cmd.
20534  *
20535  * Return Code: 0   - Success
20536  *              EIO - IO error
20537  *              EACCES - Reservation conflict detected
20538  *              ENXIO  - Not Ready, medium not present
20539  *              errno return code from sd_ssc_send()
20540  *
20541  *     Context: Can sleep. Does not return until command is completed.
20542  */
20543 
20544 static int
20545 sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag)
20546 {
20547         struct  scsi_extended_sense     sense_buf;
20548         union scsi_cdb          cdb;
20549         struct uscsi_cmd        ucmd_buf;
20550         int                     status;
20551         struct sd_lun           *un;
20552 
20553         ASSERT(ssc != NULL);
20554         un = ssc->ssc_un;
20555         ASSERT(un != NULL);
20556         ASSERT(!mutex_owned(SD_MUTEX(un)));
20557 
20558         SD_TRACE(SD_LOG_IO, un,
20559             "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un);
20560 
20561         /*
20562          * Some Seagate elite1 TQ devices get hung with disconnect/reconnect
20563          * timeouts when they receive a TUR and the queue is not empty. Check
20564          * the configuration flag set during attach (indicating the drive has
20565          * this firmware bug) and un_ncmds_in_transport before issuing the
20566          * TUR. If there are
20567          * pending commands return success, this is a bit arbitrary but is ok
20568          * for non-removables (i.e. the eliteI disks) and non-clustering
20569          * configurations.
20570          */
20571         if (un->un_f_cfg_tur_check == TRUE) {
20572                 mutex_enter(SD_MUTEX(un));
20573                 if (un->un_ncmds_in_transport != 0) {
20574                         mutex_exit(SD_MUTEX(un));
20575                         return (0);
20576                 }
20577                 mutex_exit(SD_MUTEX(un));
20578         }
20579 
20580         bzero(&cdb, sizeof (cdb));
20581         bzero(&ucmd_buf, sizeof (ucmd_buf));
20582         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20583 
20584         cdb.scc_cmd = SCMD_TEST_UNIT_READY;
20585 
20586         ucmd_buf.uscsi_cdb      = (char *)&cdb;
20587         ucmd_buf.uscsi_cdblen   = CDB_GROUP0;
20588         ucmd_buf.uscsi_bufaddr  = NULL;
20589         ucmd_buf.uscsi_buflen   = 0;
20590         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
20591         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
20592         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_SILENT;
20593 
20594         /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */
20595         if ((flag & SD_DONT_RETRY_TUR) != 0) {
20596                 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE;
20597         }
20598         ucmd_buf.uscsi_timeout  = un->un_uscsi_timeout;
20599 
20600         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20601             UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT :
20602             SD_PATH_STANDARD));
20603 
20604         switch (status) {
20605         case 0:
20606                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20607                 break;  /* Success! */
20608         case EIO:
20609                 switch (ucmd_buf.uscsi_status) {
20610                 case STATUS_RESERVATION_CONFLICT:
20611                         status = EACCES;
20612                         break;
20613                 case STATUS_CHECK:
20614                         if ((flag & SD_CHECK_FOR_MEDIA) == 0) {
20615                                 break;
20616                         }
20617                         if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20618                             (scsi_sense_key((uint8_t *)&sense_buf) ==
20619                             KEY_NOT_READY) &&
20620                             (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) {
20621                                 status = ENXIO;
20622                         }
20623                         break;
20624                 default:
20625                         break;
20626                 }
20627                 break;
20628         default:
20629                 break;
20630         }
20631 
20632         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n");
20633 
20634         return (status);
20635 }
20636 
20637 /*
20638  *    Function: sd_send_scsi_PERSISTENT_RESERVE_IN
20639  *
20640  * Description: Issue the scsi PERSISTENT RESERVE IN command.
20641  *
20642  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20643  *                      structure for this target.
20644  *
20645  * Return Code: 0   - Success
20646  *              EACCES
20647  *              ENOTSUP
20648  *              errno return code from sd_ssc_send()
20649  *
20650  *     Context: Can sleep. Does not return until command is completed.
20651  */
20652 
20653 static int
20654 sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc, uchar_t usr_cmd,
20655     uint16_t data_len, uchar_t *data_bufp)
20656 {
20657         struct scsi_extended_sense      sense_buf;
20658         union scsi_cdb          cdb;
20659         struct uscsi_cmd        ucmd_buf;
20660         int                     status;
20661         int                     no_caller_buf = FALSE;
20662         struct sd_lun           *un;
20663 
20664         ASSERT(ssc != NULL);
20665         un = ssc->ssc_un;
20666         ASSERT(un != NULL);
20667         ASSERT(!mutex_owned(SD_MUTEX(un)));
20668         ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV));
20669 
20670         SD_TRACE(SD_LOG_IO, un,
20671             "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un);
20672 
20673         bzero(&cdb, sizeof (cdb));
20674         bzero(&ucmd_buf, sizeof (ucmd_buf));
20675         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20676         if (data_bufp == NULL) {
20677                 /* Allocate a default buf if the caller did not give one */
20678                 ASSERT(data_len == 0);
20679                 data_len  = MHIOC_RESV_KEY_SIZE;
20680                 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP);
20681                 no_caller_buf = TRUE;
20682         }
20683 
20684         cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
20685         cdb.cdb_opaque[1] = usr_cmd;
20686         FORMG1COUNT(&cdb, data_len);
20687 
20688         ucmd_buf.uscsi_cdb      = (char *)&cdb;
20689         ucmd_buf.uscsi_cdblen   = CDB_GROUP1;
20690         ucmd_buf.uscsi_bufaddr  = (caddr_t)data_bufp;
20691         ucmd_buf.uscsi_buflen   = data_len;
20692         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
20693         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
20694         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20695         ucmd_buf.uscsi_timeout  = un->un_uscsi_timeout;
20696 
20697         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20698             UIO_SYSSPACE, SD_PATH_STANDARD);
20699 
20700         switch (status) {
20701         case 0:
20702                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20703 
20704                 break;  /* Success! */
20705         case EIO:
20706                 switch (ucmd_buf.uscsi_status) {
20707                 case STATUS_RESERVATION_CONFLICT:
20708                         status = EACCES;
20709                         break;
20710                 case STATUS_CHECK:
20711                         if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20712                             (scsi_sense_key((uint8_t *)&sense_buf) ==
20713                             KEY_ILLEGAL_REQUEST)) {
20714                                 status = ENOTSUP;
20715                         }
20716                         break;
20717                 default:
20718                         break;
20719                 }
20720                 break;
20721         default:
20722                 break;
20723         }
20724 
20725         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n");
20726 
20727         if (no_caller_buf == TRUE) {
20728                 kmem_free(data_bufp, data_len);
20729         }
20730 
20731         return (status);
20732 }
20733 
20734 
20735 /*
20736  *    Function: sd_send_scsi_PERSISTENT_RESERVE_OUT
20737  *
20738  * Description: This routine is the driver entry point for handling CD-ROM
20739  *              multi-host persistent reservation requests (MHIOCGRP_INKEYS,
20740  *              MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the
20741  *              device.
20742  *
20743  *   Arguments: ssc  -  ssc contains un - pointer to soft state struct
20744  *                      for the target.
20745  *              usr_cmd SCSI-3 reservation facility command (one of
20746  *                      SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE,
20747  *                      SD_SCSI3_PREEMPTANDABORT, SD_SCSI3_CLEAR)
20748  *              usr_bufp - user provided pointer register, reserve descriptor or
20749  *                      preempt and abort structure (mhioc_register_t,
20750  *                      mhioc_resv_desc_t, mhioc_preemptandabort_t)
20751  *
20752  * Return Code: 0   - Success
20753  *              EACCES
20754  *              ENOTSUP
20755  *              errno return code from sd_ssc_send()
20756  *
20757  *     Context: Can sleep. Does not return until command is completed.
20758  */
20759 
20760 static int
20761 sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc, uchar_t usr_cmd,
20762     uchar_t *usr_bufp)
20763 {
20764         struct scsi_extended_sense      sense_buf;
20765         union scsi_cdb          cdb;
20766         struct uscsi_cmd        ucmd_buf;
20767         int                     status;
20768         uchar_t                 data_len = sizeof (sd_prout_t);
20769         sd_prout_t              *prp;
20770         struct sd_lun           *un;
20771 
20772         ASSERT(ssc != NULL);
20773         un = ssc->ssc_un;
20774         ASSERT(un != NULL);
20775         ASSERT(!mutex_owned(SD_MUTEX(un)));
20776         ASSERT(data_len == 24); /* required by scsi spec */
20777 
20778         SD_TRACE(SD_LOG_IO, un,
20779             "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un);
20780 
20781         if (usr_bufp == NULL) {
20782                 return (EINVAL);
20783         }
20784 
20785         bzero(&cdb, sizeof (cdb));
20786         bzero(&ucmd_buf, sizeof (ucmd_buf));
20787         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20788         prp = kmem_zalloc(data_len, KM_SLEEP);
20789 
20790         cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
20791         cdb.cdb_opaque[1] = usr_cmd;
20792         FORMG1COUNT(&cdb, data_len);
20793 
20794         ucmd_buf.uscsi_cdb      = (char *)&cdb;
20795         ucmd_buf.uscsi_cdblen   = CDB_GROUP1;
20796         ucmd_buf.uscsi_bufaddr  = (caddr_t)prp;
20797         ucmd_buf.uscsi_buflen   = data_len;
20798         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
20799         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
20800         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
20801         ucmd_buf.uscsi_timeout  = un->un_uscsi_timeout;
20802 
20803         switch (usr_cmd) {
20804         case SD_SCSI3_REGISTER: {
20805                 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp;
20806 
20807                 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20808                 bcopy(ptr->newkey.key, prp->service_key,
20809                     MHIOC_RESV_KEY_SIZE);
20810                 prp->aptpl = ptr->aptpl;
20811                 break;
20812         }
20813         case SD_SCSI3_CLEAR: {
20814                 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
20815 
20816                 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20817                 break;
20818         }
20819         case SD_SCSI3_RESERVE:
20820         case SD_SCSI3_RELEASE: {
20821                 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
20822 
20823                 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20824                 prp->scope_address = BE_32(ptr->scope_specific_addr);
20825                 cdb.cdb_opaque[2] = ptr->type;
20826                 break;
20827         }
20828         case SD_SCSI3_PREEMPTANDABORT: {
20829                 mhioc_preemptandabort_t *ptr =
20830                     (mhioc_preemptandabort_t *)usr_bufp;
20831 
20832                 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20833                 bcopy(ptr->victim_key.key, prp->service_key,
20834                     MHIOC_RESV_KEY_SIZE);
20835                 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr);
20836                 cdb.cdb_opaque[2] = ptr->resvdesc.type;
20837                 ucmd_buf.uscsi_flags |= USCSI_HEAD;
20838                 break;
20839         }
20840         case SD_SCSI3_REGISTERANDIGNOREKEY:
20841         {
20842                 mhioc_registerandignorekey_t *ptr;
20843                 ptr = (mhioc_registerandignorekey_t *)usr_bufp;
20844                 bcopy(ptr->newkey.key,
20845                     prp->service_key, MHIOC_RESV_KEY_SIZE);
20846                 prp->aptpl = ptr->aptpl;
20847                 break;
20848         }
20849         default:
20850                 ASSERT(FALSE);
20851                 break;
20852         }
20853 
20854         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20855             UIO_SYSSPACE, SD_PATH_STANDARD);
20856 
20857         switch (status) {
20858         case 0:
20859                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20860                 break;  /* Success! */
20861         case EIO:
20862                 switch (ucmd_buf.uscsi_status) {
20863                 case STATUS_RESERVATION_CONFLICT:
20864                         status = EACCES;
20865                         break;
20866                 case STATUS_CHECK:
20867                         if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20868                             (scsi_sense_key((uint8_t *)&sense_buf) ==
20869                             KEY_ILLEGAL_REQUEST)) {
20870                                 status = ENOTSUP;
20871                         }
20872                         break;
20873                 default:
20874                         break;
20875                 }
20876                 break;
20877         default:
20878                 break;
20879         }
20880 
20881         kmem_free(prp, data_len);
20882         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n");
20883         return (status);
20884 }
20885 
20886 
20887 /*
20888  *    Function: sd_send_scsi_SYNCHRONIZE_CACHE
20889  *
20890  * Description: Issues a scsi SYNCHRONIZE CACHE command to the target
20891  *
20892  *   Arguments: un - pointer to the target's soft state struct
20893  *              dkc - pointer to the callback structure
20894  *
20895  * Return Code: 0 - success
20896  *              errno-type error code
20897  *
20898  *     Context: kernel thread context only.
20899  *
20900  *  _______________________________________________________________
20901  * | dkc_flag &   | dkc_callback | DKIOCFLUSHWRITECACHE            |
20902  * |FLUSH_VOLATILE|              | operation                       |
20903  * |______________|______________|_________________________________|
20904  * | 0            | NULL         | Synchronous flush on both       |
20905  * |              |              | volatile and non-volatile cache |
20906  * |______________|______________|_________________________________|
20907  * | 1            | NULL         | Synchronous flush on volatile   |
20908  * |              |              | cache; disk drivers may suppress|
20909  * |              |              | flush if disk table indicates   |
20910  * |              |              | non-volatile cache              |
20911  * |______________|______________|_________________________________|
20912  * | 0            | !NULL        | Asynchronous flush on both      |
20913  * |              |              | volatile and non-volatile cache;|
20914  * |______________|______________|_________________________________|
20915  * | 1            | !NULL        | Asynchronous flush on volatile  |
20916  * |              |              | cache; disk drivers may suppress|
20917  * |              |              | flush if disk table indicates   |
20918  * |              |              | non-volatile cache              |
20919  * |______________|______________|_________________________________|
20920  *
20921  */
20922 
20923 static int
20924 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc)
20925 {
20926         struct sd_uscsi_info    *uip;
20927         struct uscsi_cmd        *uscmd;
20928         union scsi_cdb          *cdb;
20929         struct buf              *bp;
20930         int                     rval = 0;
20931         int                     is_async;
20932 
20933         SD_TRACE(SD_LOG_IO, un,
20934             "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un);
20935 
20936         ASSERT(un != NULL);
20937         ASSERT(!mutex_owned(SD_MUTEX(un)));
20938 
20939         if (dkc == NULL || dkc->dkc_callback == NULL) {
20940                 is_async = FALSE;
20941         } else {
20942                 is_async = TRUE;
20943         }
20944 
20945         mutex_enter(SD_MUTEX(un));
20946         /* check whether cache flush should be suppressed */
20947         if (un->un_f_suppress_cache_flush == TRUE) {
20948                 mutex_exit(SD_MUTEX(un));
20949                 /*
20950                  * suppress the cache flush if the device is told to do
20951                  * so by sd.conf or disk table
20952                  */
20953                 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: \
20954                     skip the cache flush since suppress_cache_flush is %d!\n",
20955                     un->un_f_suppress_cache_flush);
20956 
20957                 if (is_async == TRUE) {
20958                         /* invoke callback for asynchronous flush */
20959                         (*dkc->dkc_callback)(dkc->dkc_cookie, 0);
20960                 }
20961                 return (rval);
20962         }
20963         mutex_exit(SD_MUTEX(un));
20964 
20965         /*
20966          * check dkc_flag & FLUSH_VOLATILE so SYNC_NV bit can be
20967          * set properly
20968          */
20969         cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP);
20970         cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE;
20971 
20972         mutex_enter(SD_MUTEX(un));
20973         if (dkc != NULL && un->un_f_sync_nv_supported &&
20974             (dkc->dkc_flag & FLUSH_VOLATILE)) {
20975                 /*
20976                  * if the device supports SYNC_NV bit, turn on
20977                  * the SYNC_NV bit to only flush volatile cache
20978                  */
20979                 cdb->cdb_un.tag |= SD_SYNC_NV_BIT;
20980         }
20981         mutex_exit(SD_MUTEX(un));
20982 
20983         /*
20984          * First get some memory for the uscsi_cmd struct and cdb
20985          * and initialize for SYNCHRONIZE_CACHE cmd.
20986          */
20987         uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
20988         uscmd->uscsi_cdblen = CDB_GROUP1;
20989         uscmd->uscsi_cdb = (caddr_t)cdb;
20990         uscmd->uscsi_bufaddr = NULL;
20991         uscmd->uscsi_buflen = 0;
20992         uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
20993         uscmd->uscsi_rqlen = SENSE_LENGTH;
20994         uscmd->uscsi_rqresid = SENSE_LENGTH;
20995         uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
20996         uscmd->uscsi_timeout = un->un_cmd_timeout;
20997 
20998         /*
20999          * Allocate an sd_uscsi_info struct and fill it with the info
21000          * needed by sd_initpkt_for_uscsi().  Then put the pointer into
21001          * b_private in the buf for sd_initpkt_for_uscsi().  Note that
21002          * since we allocate the buf here in this function, we do not
21003          * need to preserve the prior contents of b_private.
21004          * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
21005          */
21006         uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
21007         uip->ui_flags = SD_PATH_DIRECT;
21008         uip->ui_cmdp  = uscmd;
21009 
21010         bp = getrbuf(KM_SLEEP);
21011         bp->b_private = uip;
21012 
21013         /*
21014          * Setup buffer to carry uscsi request.
21015          */
21016         bp->b_flags  = B_BUSY;
21017         bp->b_bcount = 0;
21018         bp->b_blkno  = 0;
21019 
21020         if (is_async == TRUE) {
21021                 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone;
21022                 uip->ui_dkc = *dkc;
21023         }
21024 
21025         bp->b_edev = SD_GET_DEV(un);
21026         bp->b_dev = cmpdev(bp->b_edev);   /* maybe unnecessary? */
21027 
21028         /*
21029          * Unset un_f_sync_cache_required flag
21030          */
21031         mutex_enter(SD_MUTEX(un));
21032         un->un_f_sync_cache_required = FALSE;
21033         mutex_exit(SD_MUTEX(un));
21034 
21035         (void) sd_uscsi_strategy(bp);
21036 
21037         /*
21038          * If synchronous request, wait for completion
21039          * If async just return and let b_iodone callback
21040          * cleanup.
21041          * NOTE: On return, u_ncmds_in_driver will be decremented,
21042          * but it was also incremented in sd_uscsi_strategy(), so
21043          * we should be ok.
21044          */
21045         if (is_async == FALSE) {
21046                 (void) biowait(bp);
21047                 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp);
21048         }
21049 
21050         return (rval);
21051 }
21052 
21053 
21054 static int
21055 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp)
21056 {
21057         struct sd_uscsi_info *uip;
21058         struct uscsi_cmd *uscmd;
21059         uint8_t *sense_buf;
21060         struct sd_lun *un;
21061         int status;
21062         union scsi_cdb *cdb;
21063 
21064         uip = (struct sd_uscsi_info *)(bp->b_private);
21065         ASSERT(uip != NULL);
21066 
21067         uscmd = uip->ui_cmdp;
21068         ASSERT(uscmd != NULL);
21069 
21070         sense_buf = (uint8_t *)uscmd->uscsi_rqbuf;
21071         ASSERT(sense_buf != NULL);
21072 
21073         un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
21074         ASSERT(un != NULL);
21075 
21076         cdb = (union scsi_cdb *)uscmd->uscsi_cdb;
21077 
21078         status = geterror(bp);
21079         switch (status) {
21080         case 0:
21081                 break;  /* Success! */
21082         case EIO:
21083                 switch (uscmd->uscsi_status) {
21084                 case STATUS_RESERVATION_CONFLICT:
21085                         /* Ignore reservation conflict */
21086                         status = 0;
21087                         goto done;
21088 
21089                 case STATUS_CHECK:
21090                         if ((uscmd->uscsi_rqstatus == STATUS_GOOD) &&
21091                             (scsi_sense_key(sense_buf) ==
21092                             KEY_ILLEGAL_REQUEST)) {
21093                                 /* Ignore Illegal Request error */
21094                                 if (cdb->cdb_un.tag&SD_SYNC_NV_BIT) {
21095                                         mutex_enter(SD_MUTEX(un));
21096                                         un->un_f_sync_nv_supported = FALSE;
21097                                         mutex_exit(SD_MUTEX(un));
21098                                         status = 0;
21099                                         SD_TRACE(SD_LOG_IO, un,
21100                                             "un_f_sync_nv_supported \
21101                                             is set to false.\n");
21102                                         goto done;
21103                                 }
21104 
21105                                 mutex_enter(SD_MUTEX(un));
21106                                 un->un_f_sync_cache_supported = FALSE;
21107                                 mutex_exit(SD_MUTEX(un));
21108                                 SD_TRACE(SD_LOG_IO, un,
21109                                     "sd_send_scsi_SYNCHRONIZE_CACHE_biodone: \
21110                                     un_f_sync_cache_supported set to false \
21111                                     with asc = %x, ascq = %x\n",
21112                                     scsi_sense_asc(sense_buf),
21113                                     scsi_sense_ascq(sense_buf));
21114                                 status = ENOTSUP;
21115                                 goto done;
21116                         }
21117                         break;
21118                 default:
21119                         break;
21120                 }
21121                 /* FALLTHRU */
21122         default:
21123                 /*
21124                  * Turn on the un_f_sync_cache_required flag
21125                  * since the SYNC CACHE command failed
21126                  */
21127                 mutex_enter(SD_MUTEX(un));
21128                 un->un_f_sync_cache_required = TRUE;
21129                 mutex_exit(SD_MUTEX(un));
21130 
21131                 /*
21132                  * Don't log an error message if this device
21133                  * has removable media.
21134                  */
21135                 if (!un->un_f_has_removable_media) {
21136                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
21137                             "SYNCHRONIZE CACHE command failed (%d)\n", status);
21138                 }
21139                 break;
21140         }
21141 
21142 done:
21143         if (uip->ui_dkc.dkc_callback != NULL)
21144                 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status);
21145 
21146         ASSERT((bp->b_flags & B_REMAPPED) == 0);
21147         freerbuf(bp);
21148         kmem_free(uip, sizeof (struct sd_uscsi_info));
21149         kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
21150         kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
21151         kmem_free(uscmd, sizeof (struct uscsi_cmd));
21152 
21153         return (status);
21154 }
21155 
21156 /*
21157  * Issues a single SCSI UNMAP command with a prepared UNMAP parameter list.
21158  * Returns zero on success, or the non-zero command error code on failure.
21159  */
21160 static int
21161 sd_send_scsi_UNMAP_issue_one(sd_ssc_t *ssc, unmap_param_hdr_t *uph,
21162     uint64_t num_descr, uint64_t bytes)
21163 {
21164         struct sd_lun           *un = ssc->ssc_un;
21165         struct scsi_extended_sense      sense_buf;
21166         union scsi_cdb          cdb;
21167         struct uscsi_cmd        ucmd_buf;
21168         int                     status;
21169         const uint64_t          param_size = sizeof (unmap_param_hdr_t) +
21170             num_descr * sizeof (unmap_blk_descr_t);
21171 
21172         uph->uph_data_len = BE_16(param_size - 2);
21173         uph->uph_descr_data_len = BE_16(param_size - 8);
21174 
21175         bzero(&cdb, sizeof (cdb));
21176         bzero(&ucmd_buf, sizeof (ucmd_buf));
21177         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21178 
21179         cdb.scc_cmd = SCMD_UNMAP;
21180         FORMG1COUNT(&cdb, param_size);
21181 
21182         ucmd_buf.uscsi_cdb      = (char *)&cdb;
21183         ucmd_buf.uscsi_cdblen   = (uchar_t)CDB_GROUP1;
21184         ucmd_buf.uscsi_bufaddr  = (caddr_t)uph;
21185         ucmd_buf.uscsi_buflen   = param_size;
21186         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
21187         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
21188         ucmd_buf.uscsi_flags    = USCSI_WRITE | USCSI_RQENABLE | USCSI_SILENT;
21189         ucmd_buf.uscsi_timeout  = un->un_cmd_timeout;
21190 
21191         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, UIO_SYSSPACE,
21192             SD_PATH_STANDARD);
21193 
21194         switch (status) {
21195         case 0:
21196                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21197 
21198                 if (un->un_unmapstats) {
21199                         atomic_inc_64(&un->un_unmapstats->us_cmds.value.ui64);
21200                         atomic_add_64(&un->un_unmapstats->us_extents.value.ui64,
21201                             num_descr);
21202                         atomic_add_64(&un->un_unmapstats->us_bytes.value.ui64,
21203                             bytes);
21204                 }
21205                 break;  /* Success! */
21206         case EIO:
21207                 if (un->un_unmapstats)
21208                         atomic_inc_64(&un->un_unmapstats->us_errs.value.ui64);
21209                 switch (ucmd_buf.uscsi_status) {
21210                 case STATUS_RESERVATION_CONFLICT:
21211                         status = EACCES;
21212                         break;
21213                 default:
21214                         break;
21215                 }
21216                 break;
21217         default:
21218                 if (un->un_unmapstats)
21219                         atomic_inc_64(&un->un_unmapstats->us_errs.value.ui64);
21220                 break;
21221         }
21222 
21223         return (status);
21224 }
21225 
21226 /*
21227  * Returns a pointer to the i'th block descriptor inside an UNMAP param list.
21228  */
21229 static inline unmap_blk_descr_t *
21230 UNMAP_blk_descr_i(void *buf, uint64_t i)
21231 {
21232         return ((unmap_blk_descr_t *)((uint8_t *)buf +
21233             sizeof (unmap_param_hdr_t) + (i * sizeof (unmap_blk_descr_t))));
21234 }
21235 
21236 /*
21237  * Takes the list of extents from sd_send_scsi_UNMAP, chops it up, prepares
21238  * UNMAP block descriptors and issues individual SCSI UNMAP commands. While
21239  * doing so we consult the block limits to determine at most how many
21240  * extents and LBAs we can UNMAP in one command.
21241  * If a command fails for whatever, reason, extent list processing is aborted
21242  * and the failed command's status is returned. Otherwise returns 0 on
21243  * success.
21244  */
21245 static int
21246 sd_send_scsi_UNMAP_issue(dev_t dev, sd_ssc_t *ssc, const dkioc_free_list_t *dfl)
21247 {
21248         struct sd_lun           *un = ssc->ssc_un;
21249         unmap_param_hdr_t       *uph;
21250         sd_blk_limits_t         *lim = &un->un_blk_lim;
21251         int                     rval = 0;
21252         int                     partition;
21253         /* partition offset & length in system blocks */
21254         diskaddr_t              part_off_sysblks = 0, part_len_sysblks = 0;
21255         uint64_t                part_off, part_len;
21256         uint64_t                descr_cnt_lim, byte_cnt_lim;
21257         uint64_t                descr_issued = 0, bytes_issued = 0;
21258 
21259         uph = kmem_zalloc(SD_UNMAP_PARAM_LIST_MAXSZ, KM_SLEEP);
21260 
21261         partition = SDPART(dev);
21262         (void) cmlb_partinfo(un->un_cmlbhandle, partition, &part_len_sysblks,
21263             &part_off_sysblks, NULL, NULL, (void *)SD_PATH_DIRECT);
21264         part_off = SD_SYSBLOCKS2BYTES(part_off_sysblks);
21265         part_len = SD_SYSBLOCKS2BYTES(part_len_sysblks);
21266 
21267         ASSERT(un->un_blk_lim.lim_max_unmap_lba_cnt != 0);
21268         ASSERT(un->un_blk_lim.lim_max_unmap_descr_cnt != 0);
21269         /* Spec says 0xffffffff are special values, so compute maximums. */
21270         byte_cnt_lim = lim->lim_max_unmap_lba_cnt < UINT32_MAX ?
21271             (uint64_t)lim->lim_max_unmap_lba_cnt * un->un_tgt_blocksize :
21272             UINT64_MAX;
21273         descr_cnt_lim = MIN(lim->lim_max_unmap_descr_cnt, SD_UNMAP_MAX_DESCR);
21274 
21275         for (size_t i = 0; i < dfl->dfl_num_exts; i++) {
21276                 const dkioc_free_list_ext_t *ext = &dfl->dfl_exts[i];
21277                 uint64_t ext_start = ext->dfle_start;
21278                 uint64_t ext_length = ext->dfle_length;
21279 
21280                 while (ext_length > 0) {
21281                         unmap_blk_descr_t *ubd;
21282                         /* Respect device limit on LBA count per command */
21283                         uint64_t len = MIN(MIN(ext_length, byte_cnt_lim -
21284                             bytes_issued), SD_TGTBLOCKS2BYTES(un, UINT32_MAX));
21285 
21286                         /* check partition limits */
21287                         if (ext_start + len > part_len) {
21288                                 rval = SET_ERROR(EINVAL);
21289                                 goto out;
21290                         }
21291 #ifdef  DEBUG
21292                         if (dfl->dfl_ck_func)
21293                                 dfl->dfl_ck_func(dfl->dfl_offset + ext_start,
21294                                     len, dfl->dfl_ck_arg);
21295 #endif
21296                         ASSERT3U(descr_issued, <, descr_cnt_lim);
21297                         ASSERT3U(bytes_issued, <, byte_cnt_lim);
21298                         ubd = UNMAP_blk_descr_i(uph, descr_issued);
21299 
21300                         /* adjust in-partition addresses to be device-global */
21301                         ubd->ubd_lba = BE_64(SD_BYTES2TGTBLOCKS(un,
21302                             dfl->dfl_offset + ext_start + part_off));
21303                         ubd->ubd_lba_cnt = BE_32(SD_BYTES2TGTBLOCKS(un, len));
21304 
21305                         descr_issued++;
21306                         bytes_issued += len;
21307 
21308                         /* Issue command when device limits reached */
21309                         if (descr_issued == descr_cnt_lim ||
21310                             bytes_issued == byte_cnt_lim) {
21311                                 rval = sd_send_scsi_UNMAP_issue_one(ssc, uph,
21312                                     descr_issued, bytes_issued);
21313                                 if (rval != 0)
21314                                         goto out;
21315                                 descr_issued = 0;
21316                                 bytes_issued = 0;
21317                         }
21318 
21319                         ext_start += len;
21320                         ext_length -= len;
21321                 }
21322         }
21323 
21324         if (descr_issued > 0) {
21325                 /* issue last command */
21326                 rval = sd_send_scsi_UNMAP_issue_one(ssc, uph, descr_issued,
21327                     bytes_issued);
21328         }
21329 
21330 out:
21331         kmem_free(uph, SD_UNMAP_PARAM_LIST_MAXSZ);
21332         return (rval);
21333 }
21334 
21335 /*
21336  * Issues one or several UNMAP commands based on a list of extents to be
21337  * unmapped. The internal multi-command processing is hidden, as the exact
21338  * number of commands and extents per command is limited by both SCSI
21339  * command syntax and device limits (as expressed in the SCSI Block Limits
21340  * VPD page and un_blk_lim in struct sd_lun).
21341  * Returns zero on success, or the error code of the first failed SCSI UNMAP
21342  * command.
21343  */
21344 static int
21345 sd_send_scsi_UNMAP(dev_t dev, sd_ssc_t *ssc, dkioc_free_list_t *dfl, int flag)
21346 {
21347         struct sd_lun           *un = ssc->ssc_un;
21348         int                     rval = 0;
21349 
21350         ASSERT(!mutex_owned(SD_MUTEX(un)));
21351         ASSERT(dfl != NULL);
21352 
21353         /* Per spec, any of these conditions signals lack of UNMAP support. */
21354         if (!(un->un_thin_flags & SD_THIN_PROV_ENABLED) ||
21355             un->un_blk_lim.lim_max_unmap_descr_cnt == 0 ||
21356             un->un_blk_lim.lim_max_unmap_lba_cnt == 0) {
21357                 return (SET_ERROR(ENOTSUP));
21358         }
21359 
21360         /* For userspace calls we must copy in. */
21361         if (!(flag & FKIOCTL) && (dfl = dfl_copyin(dfl, flag, KM_SLEEP)) ==
21362             NULL)
21363                 return (SET_ERROR(EFAULT));
21364 
21365         rval = sd_send_scsi_UNMAP_issue(dev, ssc, dfl);
21366 
21367         if (!(flag & FKIOCTL)) {
21368                 dfl_free(dfl);
21369                 dfl = NULL;
21370         }
21371 
21372         return (rval);
21373 }
21374 
21375 /*
21376  *    Function: sd_send_scsi_GET_CONFIGURATION
21377  *
21378  * Description: Issues the get configuration command to the device.
21379  *              Called from sd_check_for_writable_cd & sd_get_media_info
21380  *              caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN
21381  *   Arguments: ssc
21382  *              ucmdbuf
21383  *              rqbuf
21384  *              rqbuflen
21385  *              bufaddr
21386  *              buflen
21387  *              path_flag
21388  *
21389  * Return Code: 0   - Success
21390  *              errno return code from sd_ssc_send()
21391  *
21392  *     Context: Can sleep. Does not return until command is completed.
21393  *
21394  */
21395 
21396 static int
21397 sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf,
21398     uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen,
21399     int path_flag)
21400 {
21401         char    cdb[CDB_GROUP1];
21402         int     status;
21403         struct sd_lun   *un;
21404 
21405         ASSERT(ssc != NULL);
21406         un = ssc->ssc_un;
21407         ASSERT(un != NULL);
21408         ASSERT(!mutex_owned(SD_MUTEX(un)));
21409         ASSERT(bufaddr != NULL);
21410         ASSERT(ucmdbuf != NULL);
21411         ASSERT(rqbuf != NULL);
21412 
21413         SD_TRACE(SD_LOG_IO, un,
21414             "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un);
21415 
21416         bzero(cdb, sizeof (cdb));
21417         bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21418         bzero(rqbuf, rqbuflen);
21419         bzero(bufaddr, buflen);
21420 
21421         /*
21422          * Set up cdb field for the get configuration command.
21423          */
21424         cdb[0] = SCMD_GET_CONFIGURATION;
21425         cdb[1] = 0x02;  /* Requested Type */
21426         cdb[8] = SD_PROFILE_HEADER_LEN;
21427         ucmdbuf->uscsi_cdb = cdb;
21428         ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21429         ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21430         ucmdbuf->uscsi_buflen = buflen;
21431         ucmdbuf->uscsi_timeout = un->un_uscsi_timeout;
21432         ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21433         ucmdbuf->uscsi_rqlen = rqbuflen;
21434         ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21435 
21436         status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21437             UIO_SYSSPACE, path_flag);
21438 
21439         switch (status) {
21440         case 0:
21441                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21442                 break;  /* Success! */
21443         case EIO:
21444                 switch (ucmdbuf->uscsi_status) {
21445                 case STATUS_RESERVATION_CONFLICT:
21446                         status = EACCES;
21447                         break;
21448                 default:
21449                         break;
21450                 }
21451                 break;
21452         default:
21453                 break;
21454         }
21455 
21456         if (status == 0) {
21457                 SD_DUMP_MEMORY(un, SD_LOG_IO,
21458                     "sd_send_scsi_GET_CONFIGURATION: data",
21459                     (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21460         }
21461 
21462         SD_TRACE(SD_LOG_IO, un,
21463             "sd_send_scsi_GET_CONFIGURATION: exit\n");
21464 
21465         return (status);
21466 }
21467 
21468 /*
21469  *    Function: sd_send_scsi_feature_GET_CONFIGURATION
21470  *
21471  * Description: Issues the get configuration command to the device to
21472  *              retrieve a specific feature. Called from
21473  *              sd_check_for_writable_cd & sd_set_mmc_caps.
21474  *   Arguments: ssc
21475  *              ucmdbuf
21476  *              rqbuf
21477  *              rqbuflen
21478  *              bufaddr
21479  *              buflen
21480  *              feature
21481  *
21482  * Return Code: 0   - Success
21483  *              errno return code from sd_ssc_send()
21484  *
21485  *     Context: Can sleep. Does not return until command is completed.
21486  *
21487  */
21488 static int
21489 sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf,
21490     uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen,
21491     char feature, int path_flag)
21492 {
21493         char    cdb[CDB_GROUP1];
21494         int     status;
21495         struct sd_lun   *un;
21496 
21497         ASSERT(ssc != NULL);
21498         un = ssc->ssc_un;
21499         ASSERT(un != NULL);
21500         ASSERT(!mutex_owned(SD_MUTEX(un)));
21501         ASSERT(bufaddr != NULL);
21502         ASSERT(ucmdbuf != NULL);
21503         ASSERT(rqbuf != NULL);
21504 
21505         SD_TRACE(SD_LOG_IO, un,
21506             "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un);
21507 
21508         bzero(cdb, sizeof (cdb));
21509         bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21510         bzero(rqbuf, rqbuflen);
21511         bzero(bufaddr, buflen);
21512 
21513         /*
21514          * Set up cdb field for the get configuration command.
21515          */
21516         cdb[0] = SCMD_GET_CONFIGURATION;
21517         cdb[1] = 0x02;  /* Requested Type */
21518         cdb[3] = feature;
21519         cdb[8] = buflen;
21520         ucmdbuf->uscsi_cdb = cdb;
21521         ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21522         ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21523         ucmdbuf->uscsi_buflen = buflen;
21524         ucmdbuf->uscsi_timeout = un->un_uscsi_timeout;
21525         ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21526         ucmdbuf->uscsi_rqlen = rqbuflen;
21527         ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21528 
21529         status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21530             UIO_SYSSPACE, path_flag);
21531 
21532         switch (status) {
21533         case 0:
21534 
21535                 break;  /* Success! */
21536         case EIO:
21537                 switch (ucmdbuf->uscsi_status) {
21538                 case STATUS_RESERVATION_CONFLICT:
21539                         status = EACCES;
21540                         break;
21541                 default:
21542                         break;
21543                 }
21544                 break;
21545         default:
21546                 break;
21547         }
21548 
21549         if (status == 0) {
21550                 SD_DUMP_MEMORY(un, SD_LOG_IO,
21551                     "sd_send_scsi_feature_GET_CONFIGURATION: data",
21552                     (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21553         }
21554 
21555         SD_TRACE(SD_LOG_IO, un,
21556             "sd_send_scsi_feature_GET_CONFIGURATION: exit\n");
21557 
21558         return (status);
21559 }
21560 
21561 
21562 /*
21563  *    Function: sd_send_scsi_MODE_SENSE
21564  *
21565  * Description: Utility function for issuing a scsi MODE SENSE command.
21566  *              Note: This routine uses a consistent implementation for Group0,
21567  *              Group1, and Group2 commands across all platforms. ATAPI devices
21568  *              use Group 1 Read/Write commands and Group 2 Mode Sense/Select
21569  *
21570  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21571  *                      structure for this target.
21572  *              cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
21573  *                        CDB_GROUP[1|2] (10 byte).
21574  *              bufaddr - buffer for page data retrieved from the target.
21575  *              buflen - size of page to be retrieved.
21576  *              page_code - page code of data to be retrieved from the target.
21577  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21578  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21579  *                      to use the USCSI "direct" chain and bypass the normal
21580  *                      command waitq.
21581  *
21582  * Return Code: 0   - Success
21583  *              errno return code from sd_ssc_send()
21584  *
21585  *     Context: Can sleep. Does not return until command is completed.
21586  */
21587 
21588 static int
21589 sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
21590     size_t buflen,  uchar_t page_code, int path_flag)
21591 {
21592         struct  scsi_extended_sense     sense_buf;
21593         union scsi_cdb          cdb;
21594         struct uscsi_cmd        ucmd_buf;
21595         int                     status;
21596         int                     headlen;
21597         struct sd_lun           *un;
21598 
21599         ASSERT(ssc != NULL);
21600         un = ssc->ssc_un;
21601         ASSERT(un != NULL);
21602         ASSERT(!mutex_owned(SD_MUTEX(un)));
21603         ASSERT(bufaddr != NULL);
21604         ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
21605             (cdbsize == CDB_GROUP2));
21606 
21607         SD_TRACE(SD_LOG_IO, un,
21608             "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un);
21609 
21610         bzero(&cdb, sizeof (cdb));
21611         bzero(&ucmd_buf, sizeof (ucmd_buf));
21612         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21613         bzero(bufaddr, buflen);
21614 
21615         if (cdbsize == CDB_GROUP0) {
21616                 cdb.scc_cmd = SCMD_MODE_SENSE;
21617                 cdb.cdb_opaque[2] = page_code;
21618                 FORMG0COUNT(&cdb, buflen);
21619                 headlen = MODE_HEADER_LENGTH;
21620         } else {
21621                 cdb.scc_cmd = SCMD_MODE_SENSE_G1;
21622                 cdb.cdb_opaque[2] = page_code;
21623                 FORMG1COUNT(&cdb, buflen);
21624                 headlen = MODE_HEADER_LENGTH_GRP2;
21625         }
21626 
21627         ASSERT(headlen <= buflen);
21628         SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21629 
21630         ucmd_buf.uscsi_cdb      = (char *)&cdb;
21631         ucmd_buf.uscsi_cdblen   = (uchar_t)cdbsize;
21632         ucmd_buf.uscsi_bufaddr  = (caddr_t)bufaddr;
21633         ucmd_buf.uscsi_buflen   = buflen;
21634         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
21635         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
21636         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21637         ucmd_buf.uscsi_timeout  = un->un_uscsi_timeout;
21638 
21639         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21640             UIO_SYSSPACE, path_flag);
21641 
21642         switch (status) {
21643         case 0:
21644                 /*
21645                  * sr_check_wp() uses 0x3f page code and check the header of
21646                  * mode page to determine if target device is write-protected.
21647                  * But some USB devices return 0 bytes for 0x3f page code. For
21648                  * this case, make sure that mode page header is returned at
21649                  * least.
21650                  */
21651                 if (buflen - ucmd_buf.uscsi_resid <  headlen) {
21652                         status = EIO;
21653                         sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
21654                             "mode page header is not returned");
21655                 }
21656                 break;  /* Success! */
21657         case EIO:
21658                 switch (ucmd_buf.uscsi_status) {
21659                 case STATUS_RESERVATION_CONFLICT:
21660                         status = EACCES;
21661                         break;
21662                 default:
21663                         break;
21664                 }
21665                 break;
21666         default:
21667                 break;
21668         }
21669 
21670         if (status == 0) {
21671                 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data",
21672                     (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21673         }
21674         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n");
21675 
21676         return (status);
21677 }
21678 
21679 
21680 /*
21681  *    Function: sd_send_scsi_MODE_SELECT
21682  *
21683  * Description: Utility function for issuing a scsi MODE SELECT command.
21684  *              Note: This routine uses a consistent implementation for Group0,
21685  *              Group1, and Group2 commands across all platforms. ATAPI devices
21686  *              use Group 1 Read/Write commands and Group 2 Mode Sense/Select
21687  *
21688  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21689  *                      structure for this target.
21690  *              cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
21691  *                        CDB_GROUP[1|2] (10 byte).
21692  *              bufaddr - buffer for page data retrieved from the target.
21693  *              buflen - size of page to be retrieved.
21694  *              save_page - boolean to determin if SP bit should be set.
21695  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21696  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21697  *                      to use the USCSI "direct" chain and bypass the normal
21698  *                      command waitq.
21699  *
21700  * Return Code: 0   - Success
21701  *              errno return code from sd_ssc_send()
21702  *
21703  *     Context: Can sleep. Does not return until command is completed.
21704  */
21705 
21706 static int
21707 sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
21708     size_t buflen,  uchar_t save_page, int path_flag)
21709 {
21710         struct  scsi_extended_sense     sense_buf;
21711         union scsi_cdb          cdb;
21712         struct uscsi_cmd        ucmd_buf;
21713         int                     status;
21714         struct sd_lun           *un;
21715 
21716         ASSERT(ssc != NULL);
21717         un = ssc->ssc_un;
21718         ASSERT(un != NULL);
21719         ASSERT(!mutex_owned(SD_MUTEX(un)));
21720         ASSERT(bufaddr != NULL);
21721         ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
21722             (cdbsize == CDB_GROUP2));
21723 
21724         SD_TRACE(SD_LOG_IO, un,
21725             "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un);
21726 
21727         bzero(&cdb, sizeof (cdb));
21728         bzero(&ucmd_buf, sizeof (ucmd_buf));
21729         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21730 
21731         /* Set the PF bit for many third party drives */
21732         cdb.cdb_opaque[1] = 0x10;
21733 
21734         /* Set the savepage(SP) bit if given */
21735         if (save_page == SD_SAVE_PAGE) {
21736                 cdb.cdb_opaque[1] |= 0x01;
21737         }
21738 
21739         if (cdbsize == CDB_GROUP0) {
21740                 cdb.scc_cmd = SCMD_MODE_SELECT;
21741                 FORMG0COUNT(&cdb, buflen);
21742         } else {
21743                 cdb.scc_cmd = SCMD_MODE_SELECT_G1;
21744                 FORMG1COUNT(&cdb, buflen);
21745         }
21746 
21747         SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21748 
21749         ucmd_buf.uscsi_cdb      = (char *)&cdb;
21750         ucmd_buf.uscsi_cdblen   = (uchar_t)cdbsize;
21751         ucmd_buf.uscsi_bufaddr  = (caddr_t)bufaddr;
21752         ucmd_buf.uscsi_buflen   = buflen;
21753         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
21754         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
21755         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
21756         ucmd_buf.uscsi_timeout  = un->un_uscsi_timeout;
21757 
21758         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21759             UIO_SYSSPACE, path_flag);
21760 
21761         switch (status) {
21762         case 0:
21763                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21764                 break;  /* Success! */
21765         case EIO:
21766                 switch (ucmd_buf.uscsi_status) {
21767                 case STATUS_RESERVATION_CONFLICT:
21768                         status = EACCES;
21769                         break;
21770                 default:
21771                         break;
21772                 }
21773                 break;
21774         default:
21775                 break;
21776         }
21777 
21778         if (status == 0) {
21779                 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data",
21780                     (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21781         }
21782         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n");
21783 
21784         return (status);
21785 }
21786 
21787 
21788 /*
21789  *    Function: sd_send_scsi_RDWR
21790  *
21791  * Description: Issue a scsi READ or WRITE command with the given parameters.
21792  *
21793  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21794  *                      structure for this target.
21795  *              cmd:     SCMD_READ or SCMD_WRITE
21796  *              bufaddr: Address of caller's buffer to receive the RDWR data
21797  *              buflen:  Length of caller's buffer receive the RDWR data.
21798  *              start_block: Block number for the start of the RDWR operation.
21799  *                       (Assumes target-native block size.)
21800  *              residp:  Pointer to variable to receive the redisual of the
21801  *                       RDWR operation (may be NULL of no residual requested).
21802  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21803  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21804  *                      to use the USCSI "direct" chain and bypass the normal
21805  *                      command waitq.
21806  *
21807  * Return Code: 0   - Success
21808  *              errno return code from sd_ssc_send()
21809  *
21810  *     Context: Can sleep. Does not return until command is completed.
21811  */
21812 
21813 static int
21814 sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
21815     size_t buflen, daddr_t start_block, int path_flag)
21816 {
21817         struct  scsi_extended_sense     sense_buf;
21818         union scsi_cdb          cdb;
21819         struct uscsi_cmd        ucmd_buf;
21820         uint32_t                block_count;
21821         int                     status;
21822         int                     cdbsize;
21823         uchar_t                 flag;
21824         struct sd_lun           *un;
21825 
21826         ASSERT(ssc != NULL);
21827         un = ssc->ssc_un;
21828         ASSERT(un != NULL);
21829         ASSERT(!mutex_owned(SD_MUTEX(un)));
21830         ASSERT(bufaddr != NULL);
21831         ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE));
21832 
21833         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un);
21834 
21835         if (un->un_f_tgt_blocksize_is_valid != TRUE) {
21836                 return (EINVAL);
21837         }
21838 
21839         mutex_enter(SD_MUTEX(un));
21840         block_count = SD_BYTES2TGTBLOCKS(un, buflen);
21841         mutex_exit(SD_MUTEX(un));
21842 
21843         flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE;
21844 
21845         SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: "
21846             "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n",
21847             bufaddr, buflen, start_block, block_count);
21848 
21849         bzero(&cdb, sizeof (cdb));
21850         bzero(&ucmd_buf, sizeof (ucmd_buf));
21851         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21852 
21853         /* Compute CDB size to use */
21854         if (start_block > 0xffffffff)
21855                 cdbsize = CDB_GROUP4;
21856         else if ((start_block & 0xFFE00000) ||
21857             (un->un_f_cfg_is_atapi == TRUE))
21858                 cdbsize = CDB_GROUP1;
21859         else
21860                 cdbsize = CDB_GROUP0;
21861 
21862         switch (cdbsize) {
21863         case CDB_GROUP0:        /* 6-byte CDBs */
21864                 cdb.scc_cmd = cmd;
21865                 FORMG0ADDR(&cdb, start_block);
21866                 FORMG0COUNT(&cdb, block_count);
21867                 break;
21868         case CDB_GROUP1:        /* 10-byte CDBs */
21869                 cdb.scc_cmd = cmd | SCMD_GROUP1;
21870                 FORMG1ADDR(&cdb, start_block);
21871                 FORMG1COUNT(&cdb, block_count);
21872                 break;
21873         case CDB_GROUP4:        /* 16-byte CDBs */
21874                 cdb.scc_cmd = cmd | SCMD_GROUP4;
21875                 FORMG4LONGADDR(&cdb, (uint64_t)start_block);
21876                 FORMG4COUNT(&cdb, block_count);
21877                 break;
21878         case CDB_GROUP5:        /* 12-byte CDBs (currently unsupported) */
21879         default:
21880                 /* All others reserved */
21881                 return (EINVAL);
21882         }
21883 
21884         /* Set LUN bit(s) in CDB if this is a SCSI-1 device */
21885         SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21886 
21887         ucmd_buf.uscsi_cdb      = (char *)&cdb;
21888         ucmd_buf.uscsi_cdblen   = (uchar_t)cdbsize;
21889         ucmd_buf.uscsi_bufaddr  = bufaddr;
21890         ucmd_buf.uscsi_buflen   = buflen;
21891         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
21892         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
21893         ucmd_buf.uscsi_flags    = flag | USCSI_RQENABLE | USCSI_SILENT;
21894         ucmd_buf.uscsi_timeout  = un->un_cmd_timeout;
21895         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21896             UIO_SYSSPACE, path_flag);
21897 
21898         switch (status) {
21899         case 0:
21900                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21901                 break;  /* Success! */
21902         case EIO:
21903                 switch (ucmd_buf.uscsi_status) {
21904                 case STATUS_RESERVATION_CONFLICT:
21905                         status = EACCES;
21906                         break;
21907                 default:
21908                         break;
21909                 }
21910                 break;
21911         default:
21912                 break;
21913         }
21914 
21915         if (status == 0) {
21916                 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data",
21917                     (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21918         }
21919 
21920         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n");
21921 
21922         return (status);
21923 }
21924 
21925 
21926 /*
21927  *    Function: sd_send_scsi_LOG_SENSE
21928  *
21929  * Description: Issue a scsi LOG_SENSE command with the given parameters.
21930  *
21931  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21932  *                      structure for this target.
21933  *
21934  * Return Code: 0   - Success
21935  *              errno return code from sd_ssc_send()
21936  *
21937  *     Context: Can sleep. Does not return until command is completed.
21938  */
21939 
21940 static int
21941 sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr, uint16_t buflen,
21942     uchar_t page_code, uchar_t page_control, uint16_t param_ptr, int path_flag)
21943 {
21944         struct scsi_extended_sense      sense_buf;
21945         union scsi_cdb          cdb;
21946         struct uscsi_cmd        ucmd_buf;
21947         int                     status;
21948         struct sd_lun           *un;
21949 
21950         ASSERT(ssc != NULL);
21951         un = ssc->ssc_un;
21952         ASSERT(un != NULL);
21953         ASSERT(!mutex_owned(SD_MUTEX(un)));
21954 
21955         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un);
21956 
21957         bzero(&cdb, sizeof (cdb));
21958         bzero(&ucmd_buf, sizeof (ucmd_buf));
21959         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21960 
21961         cdb.scc_cmd = SCMD_LOG_SENSE_G1;
21962         cdb.cdb_opaque[2] = (page_control << 6) | page_code;
21963         cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8);
21964         cdb.cdb_opaque[6] = (uchar_t)(param_ptr  & 0x00FF);
21965         FORMG1COUNT(&cdb, buflen);
21966 
21967         ucmd_buf.uscsi_cdb      = (char *)&cdb;
21968         ucmd_buf.uscsi_cdblen   = CDB_GROUP1;
21969         ucmd_buf.uscsi_bufaddr  = (caddr_t)bufaddr;
21970         ucmd_buf.uscsi_buflen   = buflen;
21971         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
21972         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
21973         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21974         ucmd_buf.uscsi_timeout  = un->un_uscsi_timeout;
21975 
21976         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21977             UIO_SYSSPACE, path_flag);
21978 
21979         switch (status) {
21980         case 0:
21981                 break;
21982         case EIO:
21983                 switch (ucmd_buf.uscsi_status) {
21984                 case STATUS_RESERVATION_CONFLICT:
21985                         status = EACCES;
21986                         break;
21987                 case STATUS_CHECK:
21988                         if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
21989                             (scsi_sense_key((uint8_t *)&sense_buf) ==
21990                             KEY_ILLEGAL_REQUEST) &&
21991                             (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) {
21992                                 /*
21993                                  * ASC 0x24: INVALID FIELD IN CDB
21994                                  */
21995                                 switch (page_code) {
21996                                 case START_STOP_CYCLE_PAGE:
21997                                         /*
21998                                          * The start stop cycle counter is
21999                                          * implemented as page 0x31 in earlier
22000                                          * generation disks. In new generation
22001                                          * disks the start stop cycle counter is
22002                                          * implemented as page 0xE. To properly
22003                                          * handle this case if an attempt for
22004                                          * log page 0xE is made and fails we
22005                                          * will try again using page 0x31.
22006                                          *
22007                                          * Network storage BU committed to
22008                                          * maintain the page 0x31 for this
22009                                          * purpose and will not have any other
22010                                          * page implemented with page code 0x31
22011                                          * until all disks transition to the
22012                                          * standard page.
22013                                          */
22014                                         mutex_enter(SD_MUTEX(un));
22015                                         un->un_start_stop_cycle_page =
22016                                             START_STOP_CYCLE_VU_PAGE;
22017                                         cdb.cdb_opaque[2] =
22018                                             (char)(page_control << 6) |
22019                                             un->un_start_stop_cycle_page;
22020                                         mutex_exit(SD_MUTEX(un));
22021                                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
22022                                         status = sd_ssc_send(
22023                                             ssc, &ucmd_buf, FKIOCTL,
22024                                             UIO_SYSSPACE, path_flag);
22025 
22026                                         break;
22027                                 case TEMPERATURE_PAGE:
22028                                         status = ENOTTY;
22029                                         break;
22030                                 default:
22031                                         break;
22032                                 }
22033                         }
22034                         break;
22035                 default:
22036                         break;
22037                 }
22038                 break;
22039         default:
22040                 break;
22041         }
22042 
22043         if (status == 0) {
22044                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22045                 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data",
22046                     (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
22047         }
22048 
22049         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n");
22050 
22051         return (status);
22052 }
22053 
22054 
22055 /*
22056  *    Function: sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
22057  *
22058  * Description: Issue the scsi GET EVENT STATUS NOTIFICATION command.
22059  *
22060  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
22061  *                      structure for this target.
22062  *              bufaddr
22063  *              buflen
22064  *              class_req
22065  *
22066  * Return Code: 0   - Success
22067  *              errno return code from sd_ssc_send()
22068  *
22069  *     Context: Can sleep. Does not return until command is completed.
22070  */
22071 
22072 static int
22073 sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc, uchar_t *bufaddr,
22074     size_t buflen, uchar_t class_req)
22075 {
22076         union scsi_cdb          cdb;
22077         struct uscsi_cmd        ucmd_buf;
22078         int                     status;
22079         struct sd_lun           *un;
22080 
22081         ASSERT(ssc != NULL);
22082         un = ssc->ssc_un;
22083         ASSERT(un != NULL);
22084         ASSERT(!mutex_owned(SD_MUTEX(un)));
22085         ASSERT(bufaddr != NULL);
22086 
22087         SD_TRACE(SD_LOG_IO, un,
22088             "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: entry: un:0x%p\n", un);
22089 
22090         bzero(&cdb, sizeof (cdb));
22091         bzero(&ucmd_buf, sizeof (ucmd_buf));
22092         bzero(bufaddr, buflen);
22093 
22094         cdb.scc_cmd = SCMD_GET_EVENT_STATUS_NOTIFICATION;
22095         cdb.cdb_opaque[1] = 1; /* polled */
22096         cdb.cdb_opaque[4] = class_req;
22097         FORMG1COUNT(&cdb, buflen);
22098 
22099         ucmd_buf.uscsi_cdb      = (char *)&cdb;
22100         ucmd_buf.uscsi_cdblen   = CDB_GROUP1;
22101         ucmd_buf.uscsi_bufaddr  = (caddr_t)bufaddr;
22102         ucmd_buf.uscsi_buflen   = buflen;
22103         ucmd_buf.uscsi_rqbuf    = NULL;
22104         ucmd_buf.uscsi_rqlen    = 0;
22105         ucmd_buf.uscsi_flags    = USCSI_READ | USCSI_SILENT;
22106         ucmd_buf.uscsi_timeout  = un->un_uscsi_timeout;
22107 
22108         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
22109             UIO_SYSSPACE, SD_PATH_DIRECT);
22110 
22111         /*
22112          * Only handle status == 0, the upper-level caller
22113          * will put different assessment based on the context.
22114          */
22115         if (status == 0) {
22116                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22117 
22118                 if (ucmd_buf.uscsi_resid != 0) {
22119                         status = EIO;
22120                 }
22121         }
22122 
22123         SD_TRACE(SD_LOG_IO, un,
22124             "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: exit\n");
22125 
22126         return (status);
22127 }
22128 
22129 
22130 static boolean_t
22131 sd_gesn_media_data_valid(uchar_t *data)
22132 {
22133         uint16_t                        len;
22134 
22135         len = (data[1] << 8) | data[0];
22136         return ((len >= 6) &&
22137             ((data[2] & SD_GESN_HEADER_NEA) == 0) &&
22138             ((data[2] & SD_GESN_HEADER_CLASS) == SD_GESN_MEDIA_CLASS) &&
22139             ((data[3] & (1 << SD_GESN_MEDIA_CLASS)) != 0));
22140 }
22141 
22142 
22143 /*
22144  *    Function: sdioctl
22145  *
22146  * Description: Driver's ioctl(9e) entry point function.
22147  *
22148  *   Arguments: dev     - device number
22149  *              cmd     - ioctl operation to be performed
22150  *              arg     - user argument, contains data to be set or reference
22151  *                        parameter for get
22152  *              flag    - bit flag, indicating open settings, 32/64 bit type
22153  *              cred_p  - user credential pointer
22154  *              rval_p  - calling process return value (OPT)
22155  *
22156  * Return Code: EINVAL
22157  *              ENOTTY
22158  *              ENXIO
22159  *              EIO
22160  *              EFAULT
22161  *              ENOTSUP
22162  *              EPERM
22163  *
22164  *     Context: Called from the device switch at normal priority.
22165  */
22166 
22167 static int
22168 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p)
22169 {
22170         struct sd_lun   *un = NULL;
22171         int             err = 0;
22172         int             i = 0;
22173         cred_t          *cr;
22174         int             tmprval = EINVAL;
22175         boolean_t       is_valid;
22176         sd_ssc_t        *ssc;
22177 
22178         /*
22179          * All device accesses go thru sdstrategy where we check on suspend
22180          * status
22181          */
22182         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22183                 return (ENXIO);
22184         }
22185 
22186         ASSERT(!mutex_owned(SD_MUTEX(un)));
22187 
22188         /*
22189          * Moved this wait from sd_uscsi_strategy to here for
22190          * reasons of deadlock prevention. Internal driver commands,
22191          * specifically those to change a devices power level, result
22192          * in a call to sd_uscsi_strategy.
22193          */
22194         mutex_enter(SD_MUTEX(un));
22195         while ((un->un_state == SD_STATE_SUSPENDED) ||
22196             (un->un_state == SD_STATE_PM_CHANGING) ||
22197             (un->un_state == SD_STATE_ATTACHING)) {
22198                 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
22199         }
22200 
22201         if (un->un_state == SD_STATE_ATTACH_FAILED) {
22202                 mutex_exit(SD_MUTEX(un));
22203                 SD_ERROR(SD_LOG_READ_WRITE, un,
22204                     "sdioctl: attach failed\n");
22205                 return (EIO);
22206         }
22207 
22208         /*
22209          * Twiddling the counter here protects commands from now
22210          * through to the top of sd_uscsi_strategy. Without the
22211          * counter inc. a power down, for example, could get in
22212          * after the above check for state is made and before
22213          * execution gets to the top of sd_uscsi_strategy.
22214          * That would cause problems.
22215          */
22216         un->un_ncmds_in_driver++;
22217         mutex_exit(SD_MUTEX(un));
22218 
22219         /* Initialize sd_ssc_t for internal uscsi commands */
22220         ssc = sd_ssc_init(un);
22221 
22222         is_valid = SD_IS_VALID_LABEL(un);
22223 
22224         mutex_enter(SD_MUTEX(un));
22225 
22226         if (!is_valid &&
22227             (flag & (FNDELAY | FNONBLOCK))) {
22228                 switch (cmd) {
22229                 case DKIOCGGEOM:        /* SD_PATH_DIRECT */
22230                 case DKIOCGVTOC:
22231                 case DKIOCGEXTVTOC:
22232                 case DKIOCGAPART:
22233                 case DKIOCPARTINFO:
22234                 case DKIOCEXTPARTINFO:
22235                 case DKIOCSGEOM:
22236                 case DKIOCSAPART:
22237                 case DKIOCGETEFI:
22238                 case DKIOCPARTITION:
22239                 case DKIOCSVTOC:
22240                 case DKIOCSEXTVTOC:
22241                 case DKIOCSETEFI:
22242                 case DKIOCGMBOOT:
22243                 case DKIOCSMBOOT:
22244                 case DKIOCG_PHYGEOM:
22245                 case DKIOCG_VIRTGEOM:
22246                 case DKIOCSETEXTPART:
22247                         /* let cmlb handle it */
22248                         goto skip_ready_valid;
22249                 case CDROMPAUSE:
22250                 case CDROMRESUME:
22251                 case CDROMPLAYMSF:
22252                 case CDROMPLAYTRKIND:
22253                 case CDROMREADTOCHDR:
22254                 case CDROMREADTOCENTRY:
22255                 case CDROMSTOP:
22256                 case CDROMSTART:
22257                 case CDROMVOLCTRL:
22258                 case CDROMSUBCHNL:
22259                 case CDROMREADMODE2:
22260                 case CDROMREADMODE1:
22261                 case CDROMREADOFFSET:
22262                 case CDROMSBLKMODE:
22263                 case CDROMGBLKMODE:
22264                 case CDROMGDRVSPEED:
22265                 case CDROMSDRVSPEED:
22266                 case CDROMCDDA:
22267                 case CDROMCDXA:
22268                 case CDROMSUBCODE:
22269                         if (!ISCD(un)) {
22270                                 un->un_ncmds_in_driver--;
22271                                 ASSERT(un->un_ncmds_in_driver >= 0);
22272                                 if (un->un_f_detach_waiting)
22273                                         cv_signal(&un->un_detach_cv);
22274                                 mutex_exit(SD_MUTEX(un));
22275                                 err = ENOTTY;
22276                                 goto done_without_assess;
22277                         }
22278                         break;
22279                 case FDEJECT:
22280                 case DKIOCEJECT:
22281                 case CDROMEJECT:
22282                         if (!un->un_f_eject_media_supported) {
22283                                 un->un_ncmds_in_driver--;
22284                                 ASSERT(un->un_ncmds_in_driver >= 0);
22285                                 if (un->un_f_detach_waiting)
22286                                         cv_signal(&un->un_detach_cv);
22287                                 mutex_exit(SD_MUTEX(un));
22288                                 err = ENOTTY;
22289                                 goto done_without_assess;
22290                         }
22291                         break;
22292                 case DKIOCFLUSHWRITECACHE:
22293                         mutex_exit(SD_MUTEX(un));
22294                         err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22295                         if (err != 0) {
22296                                 mutex_enter(SD_MUTEX(un));
22297                                 un->un_ncmds_in_driver--;
22298                                 ASSERT(un->un_ncmds_in_driver >= 0);
22299                                 if (un->un_f_detach_waiting)
22300                                         cv_signal(&un->un_detach_cv);
22301                                 mutex_exit(SD_MUTEX(un));
22302                                 err = EIO;
22303                                 goto done_quick_assess;
22304                         }
22305                         mutex_enter(SD_MUTEX(un));
22306                         /* FALLTHROUGH */
22307                 case DKIOCREMOVABLE:
22308                 case DKIOCHOTPLUGGABLE:
22309                 case DKIOCINFO:
22310                 case DKIOCGMEDIAINFO:
22311                 case DKIOCGMEDIAINFOEXT:
22312                 case DKIOCSOLIDSTATE:
22313                 case MHIOCENFAILFAST:
22314                 case MHIOCSTATUS:
22315                 case MHIOCTKOWN:
22316                 case MHIOCRELEASE:
22317                 case MHIOCGRP_INKEYS:
22318                 case MHIOCGRP_INRESV:
22319                 case MHIOCGRP_REGISTER:
22320                 case MHIOCGRP_CLEAR:
22321                 case MHIOCGRP_RESERVE:
22322                 case MHIOCGRP_PREEMPTANDABORT:
22323                 case MHIOCGRP_REGISTERANDIGNOREKEY:
22324                 case CDROMCLOSETRAY:
22325                 case USCSICMD:
22326                         goto skip_ready_valid;
22327                 default:
22328                         break;
22329                 }
22330 
22331                 mutex_exit(SD_MUTEX(un));
22332                 err = sd_ready_and_valid(ssc, SDPART(dev));
22333                 mutex_enter(SD_MUTEX(un));
22334 
22335                 if (err != SD_READY_VALID) {
22336                         switch (cmd) {
22337                         case DKIOCSTATE:
22338                         case CDROMGDRVSPEED:
22339                         case CDROMSDRVSPEED:
22340                         case FDEJECT:   /* for eject command */
22341                         case DKIOCEJECT:
22342                         case CDROMEJECT:
22343                         case DKIOCREMOVABLE:
22344                         case DKIOCHOTPLUGGABLE:
22345                                 break;
22346                         default:
22347                                 if (un->un_f_has_removable_media) {
22348                                         err = ENXIO;
22349                                 } else {
22350                                 /* Do not map SD_RESERVED_BY_OTHERS to EIO */
22351                                         if (err == SD_RESERVED_BY_OTHERS) {
22352                                                 err = EACCES;
22353                                         } else {
22354                                                 err = EIO;
22355                                         }
22356                                 }
22357                                 un->un_ncmds_in_driver--;
22358                                 ASSERT(un->un_ncmds_in_driver >= 0);
22359                                 if (un->un_f_detach_waiting)
22360                                         cv_signal(&un->un_detach_cv);
22361                                 mutex_exit(SD_MUTEX(un));
22362 
22363                                 goto done_without_assess;
22364                         }
22365                 }
22366         }
22367 
22368 skip_ready_valid:
22369         mutex_exit(SD_MUTEX(un));
22370 
22371         switch (cmd) {
22372         case DKIOCINFO:
22373                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n");
22374                 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag);
22375                 break;
22376 
22377         case DKIOCGMEDIAINFO:
22378                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n");
22379                 err = sd_get_media_info(dev, (caddr_t)arg, flag);
22380                 break;
22381 
22382         case DKIOCGMEDIAINFOEXT:
22383                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFOEXT\n");
22384                 err = sd_get_media_info_ext(dev, (caddr_t)arg, flag);
22385                 break;
22386 
22387         case DKIOCGGEOM:
22388         case DKIOCGVTOC:
22389         case DKIOCGEXTVTOC:
22390         case DKIOCGAPART:
22391         case DKIOCPARTINFO:
22392         case DKIOCEXTPARTINFO:
22393         case DKIOCSGEOM:
22394         case DKIOCSAPART:
22395         case DKIOCGETEFI:
22396         case DKIOCPARTITION:
22397         case DKIOCSVTOC:
22398         case DKIOCSEXTVTOC:
22399         case DKIOCSETEFI:
22400         case DKIOCGMBOOT:
22401         case DKIOCSMBOOT:
22402         case DKIOCG_PHYGEOM:
22403         case DKIOCG_VIRTGEOM:
22404         case DKIOCSETEXTPART:
22405                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd);
22406 
22407                 /* TUR should spin up */
22408 
22409                 if (un->un_f_has_removable_media)
22410                         err = sd_send_scsi_TEST_UNIT_READY(ssc,
22411                             SD_CHECK_FOR_MEDIA);
22412 
22413                 else
22414                         err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22415 
22416                 if (err != 0)
22417                         goto done_with_assess;
22418 
22419                 err = cmlb_ioctl(un->un_cmlbhandle, dev,
22420                     cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT);
22421 
22422                 if ((err == 0) &&
22423                     ((cmd == DKIOCSETEFI) ||
22424                     ((un->un_f_pkstats_enabled) &&
22425                     (cmd == DKIOCSAPART || cmd == DKIOCSVTOC ||
22426                     cmd == DKIOCSEXTVTOC)))) {
22427 
22428                         tmprval = cmlb_validate(un->un_cmlbhandle, CMLB_SILENT,
22429                             (void *)SD_PATH_DIRECT);
22430                         if ((tmprval == 0) && un->un_f_pkstats_enabled) {
22431                                 sd_set_pstats(un);
22432                                 SD_TRACE(SD_LOG_IO_PARTITION, un,
22433                                     "sd_ioctl: un:0x%p pstats created and "
22434                                     "set\n", un);
22435                         }
22436                 }
22437 
22438                 if ((cmd == DKIOCSVTOC || cmd == DKIOCSEXTVTOC) ||
22439                     ((cmd == DKIOCSETEFI) && (tmprval == 0))) {
22440 
22441                         mutex_enter(SD_MUTEX(un));
22442                         if (un->un_f_devid_supported &&
22443                             (un->un_f_opt_fab_devid == TRUE)) {
22444                                 if (un->un_devid == NULL) {
22445                                         sd_register_devid(ssc, SD_DEVINFO(un),
22446                                             SD_TARGET_IS_UNRESERVED);
22447                                 } else {
22448                                         /*
22449                                          * The device id for this disk
22450                                          * has been fabricated. The
22451                                          * device id must be preserved
22452                                          * by writing it back out to
22453                                          * disk.
22454                                          */
22455                                         if (sd_write_deviceid(ssc) != 0) {
22456                                                 ddi_devid_free(un->un_devid);
22457                                                 un->un_devid = NULL;
22458                                         }
22459                                 }
22460                         }
22461                         mutex_exit(SD_MUTEX(un));
22462                 }
22463 
22464                 break;
22465 
22466         case DKIOCLOCK:
22467                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n");
22468                 err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
22469                     SD_PATH_STANDARD);
22470                 goto done_with_assess;
22471 
22472         case DKIOCUNLOCK:
22473                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n");
22474                 err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
22475                     SD_PATH_STANDARD);
22476                 goto done_with_assess;
22477 
22478         case DKIOCSTATE: {
22479                 enum dkio_state         state;
22480                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n");
22481 
22482                 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) {
22483                         err = EFAULT;
22484                 } else {
22485                         err = sd_check_media(dev, state);
22486                         if (err == 0) {
22487                                 if (ddi_copyout(&un->un_mediastate, (void *)arg,
22488                                     sizeof (int), flag) != 0)
22489                                         err = EFAULT;
22490                         }
22491                 }
22492                 break;
22493         }
22494 
22495         case DKIOCREMOVABLE:
22496                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n");
22497                 i = un->un_f_has_removable_media ? 1 : 0;
22498                 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22499                         err = EFAULT;
22500                 } else {
22501                         err = 0;
22502                 }
22503                 break;
22504 
22505         case DKIOCSOLIDSTATE:
22506                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSOLIDSTATE\n");
22507                 i = un->un_f_is_solid_state ? 1 : 0;
22508                 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22509                         err = EFAULT;
22510                 } else {
22511                         err = 0;
22512                 }
22513                 break;
22514 
22515         case DKIOCHOTPLUGGABLE:
22516                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n");
22517                 i = un->un_f_is_hotpluggable ? 1 : 0;
22518                 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22519                         err = EFAULT;
22520                 } else {
22521                         err = 0;
22522                 }
22523                 break;
22524 
22525         case DKIOCREADONLY:
22526                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREADONLY\n");
22527                 i = 0;
22528                 if ((ISCD(un) && !un->un_f_mmc_writable_media) ||
22529                     (sr_check_wp(dev) != 0)) {
22530                         i = 1;
22531                 }
22532                 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22533                         err = EFAULT;
22534                 } else {
22535                         err = 0;
22536                 }
22537                 break;
22538 
22539         case DKIOCGTEMPERATURE:
22540                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n");
22541                 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag);
22542                 break;
22543 
22544         case MHIOCENFAILFAST:
22545                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n");
22546                 if ((err = drv_priv(cred_p)) == 0) {
22547                         err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag);
22548                 }
22549                 break;
22550 
22551         case MHIOCTKOWN:
22552                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n");
22553                 if ((err = drv_priv(cred_p)) == 0) {
22554                         err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag);
22555                 }
22556                 break;
22557 
22558         case MHIOCRELEASE:
22559                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n");
22560                 if ((err = drv_priv(cred_p)) == 0) {
22561                         err = sd_mhdioc_release(dev);
22562                 }
22563                 break;
22564 
22565         case MHIOCSTATUS:
22566                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n");
22567                 if ((err = drv_priv(cred_p)) == 0) {
22568                         switch (sd_send_scsi_TEST_UNIT_READY(ssc, 0)) {
22569                         case 0:
22570                                 err = 0;
22571                                 break;
22572                         case EACCES:
22573                                 *rval_p = 1;
22574                                 err = 0;
22575                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
22576                                 break;
22577                         default:
22578                                 err = EIO;
22579                                 goto done_with_assess;
22580                         }
22581                 }
22582                 break;
22583 
22584         case MHIOCQRESERVE:
22585                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n");
22586                 if ((err = drv_priv(cred_p)) == 0) {
22587                         err = sd_reserve_release(dev, SD_RESERVE);
22588                 }
22589                 break;
22590 
22591         case MHIOCREREGISTERDEVID:
22592                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n");
22593                 if (drv_priv(cred_p) == EPERM) {
22594                         err = EPERM;
22595                 } else if (!un->un_f_devid_supported) {
22596                         err = ENOTTY;
22597                 } else {
22598                         err = sd_mhdioc_register_devid(dev);
22599                 }
22600                 break;
22601 
22602         case MHIOCGRP_INKEYS:
22603                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n");
22604                 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
22605                         if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22606                                 err = ENOTSUP;
22607                         } else {
22608                                 err = sd_mhdioc_inkeys(dev, (caddr_t)arg,
22609                                     flag);
22610                         }
22611                 }
22612                 break;
22613 
22614         case MHIOCGRP_INRESV:
22615                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n");
22616                 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
22617                         if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22618                                 err = ENOTSUP;
22619                         } else {
22620                                 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag);
22621                         }
22622                 }
22623                 break;
22624 
22625         case MHIOCGRP_REGISTER:
22626                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n");
22627                 if ((err = drv_priv(cred_p)) != EPERM) {
22628                         if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22629                                 err = ENOTSUP;
22630                         } else if (arg != NULL) {
22631                                 mhioc_register_t reg;
22632                                 if (ddi_copyin((void *)arg, &reg,
22633                                     sizeof (mhioc_register_t), flag) != 0) {
22634                                         err = EFAULT;
22635                                 } else {
22636                                         err =
22637                                             sd_send_scsi_PERSISTENT_RESERVE_OUT(
22638                                             ssc, SD_SCSI3_REGISTER,
22639                                             (uchar_t *)&reg);
22640                                         if (err != 0)
22641                                                 goto done_with_assess;
22642                                 }
22643                         }
22644                 }
22645                 break;
22646 
22647         case MHIOCGRP_CLEAR:
22648                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_CLEAR\n");
22649                 if ((err = drv_priv(cred_p)) != EPERM) {
22650                         if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22651                                 err = ENOTSUP;
22652                         } else if (arg != NULL) {
22653                                 mhioc_register_t reg;
22654                                 if (ddi_copyin((void *)arg, &reg,
22655                                     sizeof (mhioc_register_t), flag) != 0) {
22656                                         err = EFAULT;
22657                                 } else {
22658                                         err =
22659                                             sd_send_scsi_PERSISTENT_RESERVE_OUT(
22660                                             ssc, SD_SCSI3_CLEAR,
22661                                             (uchar_t *)&reg);
22662                                         if (err != 0)
22663                                                 goto done_with_assess;
22664                                 }
22665                         }
22666                 }
22667                 break;
22668 
22669         case MHIOCGRP_RESERVE:
22670                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n");
22671                 if ((err = drv_priv(cred_p)) != EPERM) {
22672                         if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22673                                 err = ENOTSUP;
22674                         } else if (arg != NULL) {
22675                                 mhioc_resv_desc_t resv_desc;
22676                                 if (ddi_copyin((void *)arg, &resv_desc,
22677                                     sizeof (mhioc_resv_desc_t), flag) != 0) {
22678                                         err = EFAULT;
22679                                 } else {
22680                                         err =
22681                                             sd_send_scsi_PERSISTENT_RESERVE_OUT(
22682                                             ssc, SD_SCSI3_RESERVE,
22683                                             (uchar_t *)&resv_desc);
22684                                         if (err != 0)
22685                                                 goto done_with_assess;
22686                                 }
22687                         }
22688                 }
22689                 break;
22690 
22691         case MHIOCGRP_PREEMPTANDABORT:
22692                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
22693                 if ((err = drv_priv(cred_p)) != EPERM) {
22694                         if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22695                                 err = ENOTSUP;
22696                         } else if (arg != NULL) {
22697                                 mhioc_preemptandabort_t preempt_abort;
22698                                 if (ddi_copyin((void *)arg, &preempt_abort,
22699                                     sizeof (mhioc_preemptandabort_t),
22700                                     flag) != 0) {
22701                                         err = EFAULT;
22702                                 } else {
22703                                         err =
22704                                             sd_send_scsi_PERSISTENT_RESERVE_OUT(
22705                                             ssc, SD_SCSI3_PREEMPTANDABORT,
22706                                             (uchar_t *)&preempt_abort);
22707                                         if (err != 0)
22708                                                 goto done_with_assess;
22709                                 }
22710                         }
22711                 }
22712                 break;
22713 
22714         case MHIOCGRP_REGISTERANDIGNOREKEY:
22715                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTERANDIGNOREKEY\n");
22716                 if ((err = drv_priv(cred_p)) != EPERM) {
22717                         if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22718                                 err = ENOTSUP;
22719                         } else if (arg != NULL) {
22720                                 mhioc_registerandignorekey_t r_and_i;
22721                                 if (ddi_copyin((void *)arg, (void *)&r_and_i,
22722                                     sizeof (mhioc_registerandignorekey_t),
22723                                     flag) != 0) {
22724                                         err = EFAULT;
22725                                 } else {
22726                                         err =
22727                                             sd_send_scsi_PERSISTENT_RESERVE_OUT(
22728                                             ssc, SD_SCSI3_REGISTERANDIGNOREKEY,
22729                                             (uchar_t *)&r_and_i);
22730                                         if (err != 0)
22731                                                 goto done_with_assess;
22732                                 }
22733                         }
22734                 }
22735                 break;
22736 
22737         case USCSICMD:
22738                 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n");
22739                 cr = ddi_get_cred();
22740                 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) {
22741                         err = EPERM;
22742                 } else {
22743                         enum uio_seg    uioseg;
22744 
22745                         uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE :
22746                             UIO_USERSPACE;
22747                         if (un->un_f_format_in_progress == TRUE) {
22748                                 err = EAGAIN;
22749                                 break;
22750                         }
22751 
22752                         err = sd_ssc_send(ssc,
22753                             (struct uscsi_cmd *)arg,
22754                             flag, uioseg, SD_PATH_STANDARD);
22755                         if (err != 0)
22756                                 goto done_with_assess;
22757                         else
22758                                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22759                 }
22760                 break;
22761 
22762         case CDROMPAUSE:
22763         case CDROMRESUME:
22764                 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n");
22765                 if (!ISCD(un)) {
22766                         err = ENOTTY;
22767                 } else {
22768                         err = sr_pause_resume(dev, cmd);
22769                 }
22770                 break;
22771 
22772         case CDROMPLAYMSF:
22773                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n");
22774                 if (!ISCD(un)) {
22775                         err = ENOTTY;
22776                 } else {
22777                         err = sr_play_msf(dev, (caddr_t)arg, flag);
22778                 }
22779                 break;
22780 
22781         case CDROMPLAYTRKIND:
22782                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n");
22783                 /*
22784                  * not supported on ATAPI CD drives, use CDROMPLAYMSF instead
22785                  */
22786                 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22787                         err = ENOTTY;
22788                 } else {
22789                         err = sr_play_trkind(dev, (caddr_t)arg, flag);
22790                 }
22791                 break;
22792 
22793         case CDROMREADTOCHDR:
22794                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n");
22795                 if (!ISCD(un)) {
22796                         err = ENOTTY;
22797                 } else {
22798                         err = sr_read_tochdr(dev, (caddr_t)arg, flag);
22799                 }
22800                 break;
22801 
22802         case CDROMREADTOCENTRY:
22803                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n");
22804                 if (!ISCD(un)) {
22805                         err = ENOTTY;
22806                 } else {
22807                         err = sr_read_tocentry(dev, (caddr_t)arg, flag);
22808                 }
22809                 break;
22810 
22811         case CDROMSTOP:
22812                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n");
22813                 if (!ISCD(un)) {
22814                         err = ENOTTY;
22815                 } else {
22816                         err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22817                             SD_TARGET_STOP, SD_PATH_STANDARD);
22818                         goto done_with_assess;
22819                 }
22820                 break;
22821 
22822         case CDROMSTART:
22823                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n");
22824                 if (!ISCD(un)) {
22825                         err = ENOTTY;
22826                 } else {
22827                         err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22828                             SD_TARGET_START, SD_PATH_STANDARD);
22829                         goto done_with_assess;
22830                 }
22831                 break;
22832 
22833         case CDROMCLOSETRAY:
22834                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n");
22835                 if (!ISCD(un)) {
22836                         err = ENOTTY;
22837                 } else {
22838                         err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22839                             SD_TARGET_CLOSE, SD_PATH_STANDARD);
22840                         goto done_with_assess;
22841                 }
22842                 break;
22843 
22844         case FDEJECT:   /* for eject command */
22845         case DKIOCEJECT:
22846         case CDROMEJECT:
22847                 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n");
22848                 if (!un->un_f_eject_media_supported) {
22849                         err = ENOTTY;
22850                 } else {
22851                         err = sr_eject(dev);
22852                 }
22853                 break;
22854 
22855         case CDROMVOLCTRL:
22856                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n");
22857                 if (!ISCD(un)) {
22858                         err = ENOTTY;
22859                 } else {
22860                         err = sr_volume_ctrl(dev, (caddr_t)arg, flag);
22861                 }
22862                 break;
22863 
22864         case CDROMSUBCHNL:
22865                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n");
22866                 if (!ISCD(un)) {
22867                         err = ENOTTY;
22868                 } else {
22869                         err = sr_read_subchannel(dev, (caddr_t)arg, flag);
22870                 }
22871                 break;
22872 
22873         case CDROMREADMODE2:
22874                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n");
22875                 if (!ISCD(un)) {
22876                         err = ENOTTY;
22877                 } else if (un->un_f_cfg_is_atapi == TRUE) {
22878                         /*
22879                          * If the drive supports READ CD, use that instead of
22880                          * switching the LBA size via a MODE SELECT
22881                          * Block Descriptor
22882                          */
22883                         err = sr_read_cd_mode2(dev, (caddr_t)arg, flag);
22884                 } else {
22885                         err = sr_read_mode2(dev, (caddr_t)arg, flag);
22886                 }
22887                 break;
22888 
22889         case CDROMREADMODE1:
22890                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n");
22891                 if (!ISCD(un)) {
22892                         err = ENOTTY;
22893                 } else {
22894                         err = sr_read_mode1(dev, (caddr_t)arg, flag);
22895                 }
22896                 break;
22897 
22898         case CDROMREADOFFSET:
22899                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n");
22900                 if (!ISCD(un)) {
22901                         err = ENOTTY;
22902                 } else {
22903                         err = sr_read_sony_session_offset(dev, (caddr_t)arg,
22904                             flag);
22905                 }
22906                 break;
22907 
22908         case CDROMSBLKMODE:
22909                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n");
22910                 /*
22911                  * There is no means of changing block size in case of atapi
22912                  * drives, thus return ENOTTY if drive type is atapi
22913                  */
22914                 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22915                         err = ENOTTY;
22916                 } else if (un->un_f_mmc_cap == TRUE) {
22917 
22918                         /*
22919                          * MMC Devices do not support changing the
22920                          * logical block size
22921                          *
22922                          * Note: EINVAL is being returned instead of ENOTTY to
22923                          * maintain consistancy with the original mmc
22924                          * driver update.
22925                          */
22926                         err = EINVAL;
22927                 } else {
22928                         mutex_enter(SD_MUTEX(un));
22929                         if ((!(un->un_exclopen & (1<<SDPART(dev)))) ||
22930                             (un->un_ncmds_in_transport > 0)) {
22931                                 mutex_exit(SD_MUTEX(un));
22932                                 err = EINVAL;
22933                         } else {
22934                                 mutex_exit(SD_MUTEX(un));
22935                                 err = sr_change_blkmode(dev, cmd, arg, flag);
22936                         }
22937                 }
22938                 break;
22939 
22940         case CDROMGBLKMODE:
22941                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n");
22942                 if (!ISCD(un)) {
22943                         err = ENOTTY;
22944                 } else if ((un->un_f_cfg_is_atapi != FALSE) &&
22945                     (un->un_f_blockcount_is_valid != FALSE)) {
22946                         /*
22947                          * Drive is an ATAPI drive so return target block
22948                          * size for ATAPI drives since we cannot change the
22949                          * blocksize on ATAPI drives. Used primarily to detect
22950                          * if an ATAPI cdrom is present.
22951                          */
22952                         if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg,
22953                             sizeof (int), flag) != 0) {
22954                                 err = EFAULT;
22955                         } else {
22956                                 err = 0;
22957                         }
22958 
22959                 } else {
22960                         /*
22961                          * Drive supports changing block sizes via a Mode
22962                          * Select.
22963                          */
22964                         err = sr_change_blkmode(dev, cmd, arg, flag);
22965                 }
22966                 break;
22967 
22968         case CDROMGDRVSPEED:
22969         case CDROMSDRVSPEED:
22970                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n");
22971                 if (!ISCD(un)) {
22972                         err = ENOTTY;
22973                 } else if (un->un_f_mmc_cap == TRUE) {
22974                         /*
22975                          * Note: In the future the driver implementation
22976                          * for getting and
22977                          * setting cd speed should entail:
22978                          * 1) If non-mmc try the Toshiba mode page
22979                          *    (sr_change_speed)
22980                          * 2) If mmc but no support for Real Time Streaming try
22981                          *    the SET CD SPEED (0xBB) command
22982                          *   (sr_atapi_change_speed)
22983                          * 3) If mmc and support for Real Time Streaming
22984                          *    try the GET PERFORMANCE and SET STREAMING
22985                          *    commands (not yet implemented, 4380808)
22986                          */
22987                         /*
22988                          * As per recent MMC spec, CD-ROM speed is variable
22989                          * and changes with LBA. Since there is no such
22990                          * things as drive speed now, fail this ioctl.
22991                          *
22992                          * Note: EINVAL is returned for consistancy of original
22993                          * implementation which included support for getting
22994                          * the drive speed of mmc devices but not setting
22995                          * the drive speed. Thus EINVAL would be returned
22996                          * if a set request was made for an mmc device.
22997                          * We no longer support get or set speed for
22998                          * mmc but need to remain consistent with regard
22999                          * to the error code returned.
23000                          */
23001                         err = EINVAL;
23002                 } else if (un->un_f_cfg_is_atapi == TRUE) {
23003                         err = sr_atapi_change_speed(dev, cmd, arg, flag);
23004                 } else {
23005                         err = sr_change_speed(dev, cmd, arg, flag);
23006                 }
23007                 break;
23008 
23009         case CDROMCDDA:
23010                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n");
23011                 if (!ISCD(un)) {
23012                         err = ENOTTY;
23013                 } else {
23014                         err = sr_read_cdda(dev, (void *)arg, flag);
23015                 }
23016                 break;
23017 
23018         case CDROMCDXA:
23019                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n");
23020                 if (!ISCD(un)) {
23021                         err = ENOTTY;
23022                 } else {
23023                         err = sr_read_cdxa(dev, (caddr_t)arg, flag);
23024                 }
23025                 break;
23026 
23027         case CDROMSUBCODE:
23028                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n");
23029                 if (!ISCD(un)) {
23030                         err = ENOTTY;
23031                 } else {
23032                         err = sr_read_all_subcodes(dev, (caddr_t)arg, flag);
23033                 }
23034                 break;
23035 
23036 
23037 #ifdef SDDEBUG
23038 /* RESET/ABORTS testing ioctls */
23039         case DKIOCRESET: {
23040                 int     reset_level;
23041 
23042                 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) {
23043                         err = EFAULT;
23044                 } else {
23045                         SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: "
23046                             "reset_level = 0x%lx\n", reset_level);
23047                         if (scsi_reset(SD_ADDRESS(un), reset_level)) {
23048                                 err = 0;
23049                         } else {
23050                                 err = EIO;
23051                         }
23052                 }
23053                 break;
23054         }
23055 
23056         case DKIOCABORT:
23057                 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n");
23058                 if (scsi_abort(SD_ADDRESS(un), NULL)) {
23059                         err = 0;
23060                 } else {
23061                         err = EIO;
23062                 }
23063                 break;
23064 #endif
23065 
23066 #ifdef SD_FAULT_INJECTION
23067 /* SDIOC FaultInjection testing ioctls */
23068         case SDIOCSTART:
23069         case SDIOCSTOP:
23070         case SDIOCINSERTPKT:
23071         case SDIOCINSERTXB:
23072         case SDIOCINSERTUN:
23073         case SDIOCINSERTARQ:
23074         case SDIOCPUSH:
23075         case SDIOCRETRIEVE:
23076         case SDIOCRUN:
23077         case SDIOCINSERTTRAN:
23078                 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:"
23079                     "SDIOC detected cmd:0x%X:\n", cmd);
23080                 /* call error generator */
23081                 err = sd_faultinjection_ioctl(cmd, arg, un);
23082                 break;
23083 
23084 #endif /* SD_FAULT_INJECTION */
23085 
23086         case DKIOCFLUSHWRITECACHE:
23087                 {
23088                         struct dk_callback *dkc = (struct dk_callback *)arg;
23089 
23090                         mutex_enter(SD_MUTEX(un));
23091                         if (!un->un_f_sync_cache_supported ||
23092                             !un->un_f_write_cache_enabled) {
23093                                 err = un->un_f_sync_cache_supported ?
23094                                     0 : ENOTSUP;
23095                                 mutex_exit(SD_MUTEX(un));
23096                                 if ((flag & FKIOCTL) && dkc != NULL &&
23097                                     dkc->dkc_callback != NULL) {
23098                                         (*dkc->dkc_callback)(dkc->dkc_cookie,
23099                                             err);
23100                                         /*
23101                                          * Did callback and reported error.
23102                                          * Since we did a callback, ioctl
23103                                          * should return 0.
23104                                          */
23105                                         err = 0;
23106                                 }
23107                                 break;
23108                         }
23109                         mutex_exit(SD_MUTEX(un));
23110 
23111                         if ((flag & FKIOCTL) && dkc != NULL &&
23112                             dkc->dkc_callback != NULL) {
23113                                 /* async SYNC CACHE request */
23114                                 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
23115                         } else {
23116                                 /* synchronous SYNC CACHE request */
23117                                 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
23118                         }
23119                 }
23120                 break;
23121 
23122         case DKIOCFREE:
23123                 {
23124                         dkioc_free_list_t *dfl = (dkioc_free_list_t *)arg;
23125 
23126                         /* bad userspace ioctls shouldn't panic */
23127                         if (dfl == NULL && !(flag & FKIOCTL)) {
23128                                 err = SET_ERROR(EINVAL);
23129                                 break;
23130                         }
23131                         /* synchronous UNMAP request */
23132                         err = sd_send_scsi_UNMAP(dev, ssc, dfl, flag);
23133                 }
23134                 break;
23135 
23136         case DKIOCGETWCE: {
23137 
23138                 int wce;
23139 
23140                 if ((err = sd_get_write_cache_enabled(ssc, &wce)) != 0) {
23141                         break;
23142                 }
23143 
23144                 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) {
23145                         err = EFAULT;
23146                 }
23147                 break;
23148         }
23149 
23150         case DKIOCSETWCE: {
23151 
23152                 int wce, sync_supported;
23153                 int cur_wce = 0;
23154 
23155                 if (!un->un_f_cache_mode_changeable) {
23156                         err = EINVAL;
23157                         break;
23158                 }
23159 
23160                 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) {
23161                         err = EFAULT;
23162                         break;
23163                 }
23164 
23165                 /*
23166                  * Synchronize multiple threads trying to enable
23167                  * or disable the cache via the un_f_wcc_cv
23168                  * condition variable.
23169                  */
23170                 mutex_enter(SD_MUTEX(un));
23171 
23172                 /*
23173                  * Don't allow the cache to be enabled if the
23174                  * config file has it disabled.
23175                  */
23176                 if (un->un_f_opt_disable_cache && wce) {
23177                         mutex_exit(SD_MUTEX(un));
23178                         err = EINVAL;
23179                         break;
23180                 }
23181 
23182                 /*
23183                  * Wait for write cache change in progress
23184                  * bit to be clear before proceeding.
23185                  */
23186                 while (un->un_f_wcc_inprog)
23187                         cv_wait(&un->un_wcc_cv, SD_MUTEX(un));
23188 
23189                 un->un_f_wcc_inprog = 1;
23190 
23191                 mutex_exit(SD_MUTEX(un));
23192 
23193                 /*
23194                  * Get the current write cache state
23195                  */
23196                 if ((err = sd_get_write_cache_enabled(ssc, &cur_wce)) != 0) {
23197                         mutex_enter(SD_MUTEX(un));
23198                         un->un_f_wcc_inprog = 0;
23199                         cv_broadcast(&un->un_wcc_cv);
23200                         mutex_exit(SD_MUTEX(un));
23201                         break;
23202                 }
23203 
23204                 mutex_enter(SD_MUTEX(un));
23205                 un->un_f_write_cache_enabled = (cur_wce != 0);
23206 
23207                 if (un->un_f_write_cache_enabled && wce == 0) {
23208                         /*
23209                          * Disable the write cache.  Don't clear
23210                          * un_f_write_cache_enabled until after
23211                          * the mode select and flush are complete.
23212                          */
23213                         sync_supported = un->un_f_sync_cache_supported;
23214 
23215                         /*
23216                          * If cache flush is suppressed, we assume that the
23217                          * controller firmware will take care of managing the
23218                          * write cache for us: no need to explicitly
23219                          * disable it.
23220                          */
23221                         if (!un->un_f_suppress_cache_flush) {
23222                                 mutex_exit(SD_MUTEX(un));
23223                                 if ((err = sd_cache_control(ssc,
23224                                     SD_CACHE_NOCHANGE,
23225                                     SD_CACHE_DISABLE)) == 0 &&
23226                                     sync_supported) {
23227                                         err = sd_send_scsi_SYNCHRONIZE_CACHE(un,
23228                                             NULL);
23229                                 }
23230                         } else {
23231                                 mutex_exit(SD_MUTEX(un));
23232                         }
23233 
23234                         mutex_enter(SD_MUTEX(un));
23235                         if (err == 0) {
23236                                 un->un_f_write_cache_enabled = 0;
23237                         }
23238 
23239                 } else if (!un->un_f_write_cache_enabled && wce != 0) {
23240                         /*
23241                          * Set un_f_write_cache_enabled first, so there is
23242                          * no window where the cache is enabled, but the
23243                          * bit says it isn't.
23244                          */
23245                         un->un_f_write_cache_enabled = 1;
23246 
23247                         /*
23248                          * If cache flush is suppressed, we assume that the
23249                          * controller firmware will take care of managing the
23250                          * write cache for us: no need to explicitly
23251                          * enable it.
23252                          */
23253                         if (!un->un_f_suppress_cache_flush) {
23254                                 mutex_exit(SD_MUTEX(un));
23255                                 err = sd_cache_control(ssc, SD_CACHE_NOCHANGE,
23256                                     SD_CACHE_ENABLE);
23257                         } else {
23258                                 mutex_exit(SD_MUTEX(un));
23259                         }
23260 
23261                         mutex_enter(SD_MUTEX(un));
23262 
23263                         if (err) {
23264                                 un->un_f_write_cache_enabled = 0;
23265                         }
23266                 }
23267 
23268                 un->un_f_wcc_inprog = 0;
23269                 cv_broadcast(&un->un_wcc_cv);
23270                 mutex_exit(SD_MUTEX(un));
23271                 break;
23272         }
23273 
23274         default:
23275                 err = ENOTTY;
23276                 break;
23277         }
23278         mutex_enter(SD_MUTEX(un));
23279         un->un_ncmds_in_driver--;
23280         ASSERT(un->un_ncmds_in_driver >= 0);
23281         if (un->un_f_detach_waiting)
23282                 cv_signal(&un->un_detach_cv);
23283         mutex_exit(SD_MUTEX(un));
23284 
23285 
23286 done_without_assess:
23287         sd_ssc_fini(ssc);
23288 
23289         SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23290         return (err);
23291 
23292 done_with_assess:
23293         mutex_enter(SD_MUTEX(un));
23294         un->un_ncmds_in_driver--;
23295         ASSERT(un->un_ncmds_in_driver >= 0);
23296         if (un->un_f_detach_waiting)
23297                 cv_signal(&un->un_detach_cv);
23298         mutex_exit(SD_MUTEX(un));
23299 
23300 done_quick_assess:
23301         if (err != 0)
23302                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23303         /* Uninitialize sd_ssc_t pointer */
23304         sd_ssc_fini(ssc);
23305 
23306         SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23307         return (err);
23308 }
23309 
23310 
23311 /*
23312  *    Function: sd_dkio_ctrl_info
23313  *
23314  * Description: This routine is the driver entry point for handling controller
23315  *              information ioctl requests (DKIOCINFO).
23316  *
23317  *   Arguments: dev  - the device number
23318  *              arg  - pointer to user provided dk_cinfo structure
23319  *                     specifying the controller type and attributes.
23320  *              flag - this argument is a pass through to ddi_copyxxx()
23321  *                     directly from the mode argument of ioctl().
23322  *
23323  * Return Code: 0
23324  *              EFAULT
23325  *              ENXIO
23326  */
23327 
23328 static int
23329 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag)
23330 {
23331         struct sd_lun   *un = NULL;
23332         struct dk_cinfo *info;
23333         dev_info_t      *pdip;
23334         int             lun, tgt;
23335 
23336         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23337                 return (ENXIO);
23338         }
23339 
23340         info = (struct dk_cinfo *)
23341             kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
23342 
23343         switch (un->un_ctype) {
23344         case CTYPE_CDROM:
23345                 info->dki_ctype = DKC_CDROM;
23346                 break;
23347         default:
23348                 info->dki_ctype = DKC_SCSI_CCS;
23349                 break;
23350         }
23351         pdip = ddi_get_parent(SD_DEVINFO(un));
23352         info->dki_cnum = ddi_get_instance(pdip);
23353         if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) {
23354                 (void) strcpy(info->dki_cname, ddi_get_name(pdip));
23355         } else {
23356                 (void) strncpy(info->dki_cname, ddi_node_name(pdip),
23357                     DK_DEVLEN - 1);
23358         }
23359 
23360         lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23361             DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0);
23362         tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23363             DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0);
23364 
23365         /* Unit Information */
23366         info->dki_unit = ddi_get_instance(SD_DEVINFO(un));
23367         info->dki_slave = ((tgt << 3) | lun);
23368         (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)),
23369             DK_DEVLEN - 1);
23370         info->dki_flags = DKI_FMTVOL;
23371         info->dki_partition = SDPART(dev);
23372 
23373         /* Max Transfer size of this device in blocks */
23374         info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize;
23375         info->dki_addr = 0;
23376         info->dki_space = 0;
23377         info->dki_prio = 0;
23378         info->dki_vec = 0;
23379 
23380         if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) {
23381                 kmem_free(info, sizeof (struct dk_cinfo));
23382                 return (EFAULT);
23383         } else {
23384                 kmem_free(info, sizeof (struct dk_cinfo));
23385                 return (0);
23386         }
23387 }
23388 
23389 /*
23390  *    Function: sd_get_media_info_com
23391  *
23392  * Description: This routine returns the information required to populate
23393  *              the fields for the dk_minfo/dk_minfo_ext structures.
23394  *
23395  *   Arguments: dev             - the device number
23396  *              dki_media_type  - media_type
23397  *              dki_lbsize      - logical block size
23398  *              dki_capacity    - capacity in blocks
23399  *              dki_pbsize      - physical block size (if requested)
23400  *
23401  * Return Code: 0
23402  *              EACCES
23403  *              EFAULT
23404  *              ENXIO
23405  *              EIO
23406  */
23407 static int
23408 sd_get_media_info_com(dev_t dev, uint_t *dki_media_type, uint_t *dki_lbsize,
23409     diskaddr_t *dki_capacity, uint_t *dki_pbsize)
23410 {
23411         struct sd_lun           *un = NULL;
23412         struct uscsi_cmd        com;
23413         struct scsi_inquiry     *sinq;
23414         u_longlong_t            media_capacity;
23415         uint64_t                capacity;
23416         uint_t                  lbasize;
23417         uint_t                  pbsize;
23418         uchar_t                 *out_data;
23419         uchar_t                 *rqbuf;
23420         int                     rval = 0;
23421         int                     rtn;
23422         sd_ssc_t                *ssc;
23423 
23424         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
23425             (un->un_state == SD_STATE_OFFLINE)) {
23426                 return (ENXIO);
23427         }
23428 
23429         SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info_com: entry\n");
23430 
23431         out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
23432         rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
23433         ssc = sd_ssc_init(un);
23434 
23435         /* Issue a TUR to determine if the drive is ready with media present */
23436         rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_CHECK_FOR_MEDIA);
23437         if (rval == ENXIO) {
23438                 goto done;
23439         } else if (rval != 0) {
23440                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23441         }
23442 
23443         /* Now get configuration data */
23444         if (ISCD(un)) {
23445                 *dki_media_type = DK_CDROM;
23446 
23447                 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */
23448                 if (un->un_f_mmc_cap == TRUE) {
23449                         rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf,
23450                             SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN,
23451                             SD_PATH_STANDARD);
23452 
23453                         if (rtn) {
23454                                 /*
23455                                  * We ignore all failures for CD and need to
23456                                  * put the assessment before processing code
23457                                  * to avoid missing assessment for FMA.
23458                                  */
23459                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23460                                 /*
23461                                  * Failed for other than an illegal request
23462                                  * or command not supported
23463                                  */
23464                                 if ((com.uscsi_status == STATUS_CHECK) &&
23465                                     (com.uscsi_rqstatus == STATUS_GOOD)) {
23466                                         if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) ||
23467                                             (rqbuf[12] != 0x20)) {
23468                                                 rval = EIO;
23469                                                 goto no_assessment;
23470                                         }
23471                                 }
23472                         } else {
23473                                 /*
23474                                  * The GET CONFIGURATION command succeeded
23475                                  * so set the media type according to the
23476                                  * returned data
23477                                  */
23478                                 *dki_media_type = out_data[6];
23479                                 *dki_media_type <<= 8;
23480                                 *dki_media_type |= out_data[7];
23481                         }
23482                 }
23483         } else {
23484                 /*
23485                  * The profile list is not available, so we attempt to identify
23486                  * the media type based on the inquiry data
23487                  */
23488                 sinq = un->un_sd->sd_inq;
23489                 if ((sinq->inq_dtype == DTYPE_DIRECT) ||
23490                     (sinq->inq_dtype == DTYPE_OPTICAL)) {
23491                         /* This is a direct access device  or optical disk */
23492                         *dki_media_type = DK_FIXED_DISK;
23493 
23494                         if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) ||
23495                             (bcmp(sinq->inq_vid, "iomega", 6) == 0)) {
23496                                 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) {
23497                                         *dki_media_type = DK_ZIP;
23498                                 } else if (
23499                                     (bcmp(sinq->inq_pid, "jaz", 3) == 0)) {
23500                                         *dki_media_type = DK_JAZ;
23501                                 }
23502                         }
23503                 } else {
23504                         /*
23505                          * Not a CD, direct access or optical disk so return
23506                          * unknown media
23507                          */
23508                         *dki_media_type = DK_UNKNOWN;
23509                 }
23510         }
23511 
23512         /*
23513          * Now read the capacity so we can provide the lbasize,
23514          * pbsize and capacity.
23515          */
23516         if (dki_pbsize && un->un_f_descr_format_supported) {
23517                 rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
23518                     &pbsize, SD_PATH_DIRECT);
23519 
23520                 if (un->un_f_sdconf_phy_blocksize) /* keep sd.conf's pbs */
23521                         pbsize = un->un_phy_blocksize;
23522                 else /* override the pbs if the instance has a larger value */
23523                         pbsize = MAX(pbsize, un->un_phy_blocksize);
23524         }
23525 
23526         if (dki_pbsize == NULL || rval != 0 ||
23527             !un->un_f_descr_format_supported) {
23528                 rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
23529                     SD_PATH_DIRECT);
23530 
23531                 switch (rval) {
23532                 case 0:
23533                         if (un->un_f_enable_rmw &&
23534                             un->un_phy_blocksize != 0) {
23535                                 pbsize = un->un_phy_blocksize;
23536                         } else {
23537                                 pbsize = lbasize;
23538                         }
23539                         media_capacity = capacity;
23540 
23541                         /*
23542                          * sd_send_scsi_READ_CAPACITY() reports capacity in
23543                          * un->un_sys_blocksize chunks. So we need to convert
23544                          * it into cap.lbsize chunks.
23545                          */
23546                         if (un->un_f_has_removable_media) {
23547                                 media_capacity *= un->un_sys_blocksize;
23548                                 media_capacity /= lbasize;
23549                         }
23550                         break;
23551                 case EACCES:
23552                         rval = EACCES;
23553                         goto done;
23554                 default:
23555                         rval = EIO;
23556                         goto done;
23557                 }
23558         } else {
23559                 if (un->un_f_enable_rmw &&
23560                     !ISP2(pbsize % DEV_BSIZE)) {
23561                         pbsize = SSD_SECSIZE;
23562                 } else if (!ISP2(lbasize % DEV_BSIZE) ||
23563                     !ISP2(pbsize % DEV_BSIZE)) {
23564                         pbsize = lbasize = DEV_BSIZE;
23565                 }
23566                 media_capacity = capacity;
23567         }
23568 
23569         /*
23570          * If lun is expanded dynamically, update the un structure.
23571          */
23572         mutex_enter(SD_MUTEX(un));
23573         if ((un->un_f_blockcount_is_valid == TRUE) &&
23574             (un->un_f_tgt_blocksize_is_valid == TRUE) &&
23575             (capacity > un->un_blockcount)) {
23576                 un->un_f_expnevent = B_FALSE;
23577                 sd_update_block_info(un, lbasize, capacity);
23578         }
23579         mutex_exit(SD_MUTEX(un));
23580 
23581         *dki_lbsize = lbasize;
23582         *dki_capacity = media_capacity;
23583         if (dki_pbsize)
23584                 *dki_pbsize = pbsize;
23585 
23586 done:
23587         if (rval != 0) {
23588                 if (rval == EIO)
23589                         sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23590                 else
23591                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23592         }
23593 no_assessment:
23594         sd_ssc_fini(ssc);
23595         kmem_free(out_data, SD_PROFILE_HEADER_LEN);
23596         kmem_free(rqbuf, SENSE_LENGTH);
23597         return (rval);
23598 }
23599 
23600 /*
23601  *    Function: sd_get_media_info
23602  *
23603  * Description: This routine is the driver entry point for handling ioctl
23604  *              requests for the media type or command set profile used by the
23605  *              drive to operate on the media (DKIOCGMEDIAINFO).
23606  *
23607  *   Arguments: dev     - the device number
23608  *              arg     - pointer to user provided dk_minfo structure
23609  *                        specifying the media type, logical block size and
23610  *                        drive capacity.
23611  *              flag    - this argument is a pass through to ddi_copyxxx()
23612  *                        directly from the mode argument of ioctl().
23613  *
23614  * Return Code: returns the value from sd_get_media_info_com
23615  */
23616 static int
23617 sd_get_media_info(dev_t dev, caddr_t arg, int flag)
23618 {
23619         struct dk_minfo         mi;
23620         int                     rval;
23621 
23622         rval = sd_get_media_info_com(dev, &mi.dki_media_type,
23623             &mi.dki_lbsize, &mi.dki_capacity, NULL);
23624 
23625         if (rval)
23626                 return (rval);
23627         if (ddi_copyout(&mi, arg, sizeof (struct dk_minfo), flag))
23628                 rval = EFAULT;
23629         return (rval);
23630 }
23631 
23632 /*
23633  *    Function: sd_get_media_info_ext
23634  *
23635  * Description: This routine is the driver entry point for handling ioctl
23636  *              requests for the media type or command set profile used by the
23637  *              drive to operate on the media (DKIOCGMEDIAINFOEXT). The
23638  *              difference this ioctl and DKIOCGMEDIAINFO is the return value
23639  *              of this ioctl contains both logical block size and physical
23640  *              block size.
23641  *
23642  *
23643  *   Arguments: dev     - the device number
23644  *              arg     - pointer to user provided dk_minfo_ext structure
23645  *                        specifying the media type, logical block size,
23646  *                        physical block size and disk capacity.
23647  *              flag    - this argument is a pass through to ddi_copyxxx()
23648  *                        directly from the mode argument of ioctl().
23649  *
23650  * Return Code: returns the value from sd_get_media_info_com
23651  */
23652 static int
23653 sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag)
23654 {
23655         struct dk_minfo_ext     mie;
23656         int                     rval = 0;
23657 
23658         rval = sd_get_media_info_com(dev, &mie.dki_media_type,
23659             &mie.dki_lbsize, &mie.dki_capacity, &mie.dki_pbsize);
23660 
23661         if (rval)
23662                 return (rval);
23663         if (ddi_copyout(&mie, arg, sizeof (struct dk_minfo_ext), flag))
23664                 rval = EFAULT;
23665         return (rval);
23666 
23667 }
23668 
23669 /*
23670  *    Function: sd_watch_request_submit
23671  *
23672  * Description: Call scsi_watch_request_submit or scsi_mmc_watch_request_submit
23673  *              depending on which is supported by device.
23674  */
23675 static opaque_t
23676 sd_watch_request_submit(struct sd_lun *un)
23677 {
23678         dev_t                   dev;
23679 
23680         /* All submissions are unified to use same device number */
23681         dev = sd_make_device(SD_DEVINFO(un));
23682 
23683         if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
23684                 return (scsi_mmc_watch_request_submit(SD_SCSI_DEVP(un),
23685                     sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
23686                     (caddr_t)dev));
23687         } else {
23688                 return (scsi_watch_request_submit(SD_SCSI_DEVP(un),
23689                     sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
23690                     (caddr_t)dev));
23691         }
23692 }
23693 
23694 
23695 /*
23696  *    Function: sd_check_media
23697  *
23698  * Description: This utility routine implements the functionality for the
23699  *              DKIOCSTATE ioctl. This ioctl blocks the user thread until the
23700  *              driver state changes from that specified by the user
23701  *              (inserted or ejected). For example, if the user specifies
23702  *              DKIO_EJECTED and the current media state is inserted this
23703  *              routine will immediately return DKIO_INSERTED. However, if the
23704  *              current media state is not inserted the user thread will be
23705  *              blocked until the drive state changes. If DKIO_NONE is specified
23706  *              the user thread will block until a drive state change occurs.
23707  *
23708  *   Arguments: dev  - the device number
23709  *              state  - user pointer to a dkio_state, updated with the current
23710  *                      drive state at return.
23711  *
23712  * Return Code: ENXIO
23713  *              EIO
23714  *              EAGAIN
23715  *              EINTR
23716  */
23717 
23718 static int
23719 sd_check_media(dev_t dev, enum dkio_state state)
23720 {
23721         struct sd_lun           *un = NULL;
23722         enum dkio_state         prev_state;
23723         opaque_t                token = NULL;
23724         int                     rval = 0;
23725         sd_ssc_t                *ssc;
23726 
23727         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23728                 return (ENXIO);
23729         }
23730 
23731         SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n");
23732 
23733         ssc = sd_ssc_init(un);
23734 
23735         mutex_enter(SD_MUTEX(un));
23736 
23737         SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: "
23738             "state=%x, mediastate=%x\n", state, un->un_mediastate);
23739 
23740         prev_state = un->un_mediastate;
23741 
23742         /* is there anything to do? */
23743         if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) {
23744                 /*
23745                  * submit the request to the scsi_watch service;
23746                  * scsi_media_watch_cb() does the real work
23747                  */
23748                 mutex_exit(SD_MUTEX(un));
23749 
23750                 /*
23751                  * This change handles the case where a scsi watch request is
23752                  * added to a device that is powered down. To accomplish this
23753                  * we power up the device before adding the scsi watch request,
23754                  * since the scsi watch sends a TUR directly to the device
23755                  * which the device cannot handle if it is powered down.
23756                  */
23757                 if (sd_pm_entry(un) != DDI_SUCCESS) {
23758                         mutex_enter(SD_MUTEX(un));
23759                         goto done;
23760                 }
23761 
23762                 token = sd_watch_request_submit(un);
23763 
23764                 sd_pm_exit(un);
23765 
23766                 mutex_enter(SD_MUTEX(un));
23767                 if (token == NULL) {
23768                         rval = EAGAIN;
23769                         goto done;
23770                 }
23771 
23772                 /*
23773                  * This is a special case IOCTL that doesn't return
23774                  * until the media state changes. Routine sdpower
23775                  * knows about and handles this so don't count it
23776                  * as an active cmd in the driver, which would
23777                  * keep the device busy to the pm framework.
23778                  * If the count isn't decremented the device can't
23779                  * be powered down.
23780                  */
23781                 un->un_ncmds_in_driver--;
23782                 ASSERT(un->un_ncmds_in_driver >= 0);
23783                 if (un->un_f_detach_waiting)
23784                         cv_signal(&un->un_detach_cv);
23785 
23786                 /*
23787                  * if a prior request had been made, this will be the same
23788                  * token, as scsi_watch was designed that way.
23789                  */
23790                 un->un_swr_token = token;
23791                 un->un_specified_mediastate = state;
23792 
23793                 /*
23794                  * now wait for media change
23795                  * we will not be signalled unless mediastate == state but it is
23796                  * still better to test for this condition, since there is a
23797                  * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED
23798                  */
23799                 SD_TRACE(SD_LOG_COMMON, un,
23800                     "sd_check_media: waiting for media state change\n");
23801                 while (un->un_mediastate == state) {
23802                         if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) {
23803                                 SD_TRACE(SD_LOG_COMMON, un,
23804                                     "sd_check_media: waiting for media state "
23805                                     "was interrupted\n");
23806                                 un->un_ncmds_in_driver++;
23807                                 rval = EINTR;
23808                                 goto done;
23809                         }
23810                         SD_TRACE(SD_LOG_COMMON, un,
23811                             "sd_check_media: received signal, state=%x\n",
23812                             un->un_mediastate);
23813                 }
23814                 /*
23815                  * Inc the counter to indicate the device once again
23816                  * has an active outstanding cmd.
23817                  */
23818                 un->un_ncmds_in_driver++;
23819         }
23820 
23821         /* invalidate geometry */
23822         if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) {
23823                 sr_ejected(un);
23824         }
23825 
23826         if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) {
23827                 uint64_t        capacity;
23828                 uint_t          lbasize;
23829 
23830                 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n");
23831                 mutex_exit(SD_MUTEX(un));
23832                 /*
23833                  * Since the following routines use SD_PATH_DIRECT, we must
23834                  * call PM directly before the upcoming disk accesses. This
23835                  * may cause the disk to be power/spin up.
23836                  */
23837 
23838                 if (sd_pm_entry(un) == DDI_SUCCESS) {
23839                         rval = sd_send_scsi_READ_CAPACITY(ssc,
23840                             &capacity, &lbasize, SD_PATH_DIRECT);
23841                         if (rval != 0) {
23842                                 sd_pm_exit(un);
23843                                 if (rval == EIO)
23844                                         sd_ssc_assessment(ssc,
23845                                             SD_FMT_STATUS_CHECK);
23846                                 else
23847                                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23848                                 mutex_enter(SD_MUTEX(un));
23849                                 goto done;
23850                         }
23851                 } else {
23852                         rval = EIO;
23853                         mutex_enter(SD_MUTEX(un));
23854                         goto done;
23855                 }
23856                 mutex_enter(SD_MUTEX(un));
23857 
23858                 sd_update_block_info(un, lbasize, capacity);
23859 
23860                 /*
23861                  *  Check if the media in the device is writable or not
23862                  */
23863                 if (ISCD(un)) {
23864                         sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
23865                 }
23866 
23867                 mutex_exit(SD_MUTEX(un));
23868                 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
23869                 if ((cmlb_validate(un->un_cmlbhandle, 0,
23870                     (void *)SD_PATH_DIRECT) == 0) && un->un_f_pkstats_enabled) {
23871                         sd_set_pstats(un);
23872                         SD_TRACE(SD_LOG_IO_PARTITION, un,
23873                             "sd_check_media: un:0x%p pstats created and "
23874                             "set\n", un);
23875                 }
23876 
23877                 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
23878                     SD_PATH_DIRECT);
23879 
23880                 sd_pm_exit(un);
23881 
23882                 if (rval != 0) {
23883                         if (rval == EIO)
23884                                 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23885                         else
23886                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23887                 }
23888 
23889                 mutex_enter(SD_MUTEX(un));
23890         }
23891 done:
23892         sd_ssc_fini(ssc);
23893         un->un_f_watcht_stopped = FALSE;
23894         if (token != NULL && un->un_swr_token != NULL) {
23895                 /*
23896                  * Use of this local token and the mutex ensures that we avoid
23897                  * some race conditions associated with terminating the
23898                  * scsi watch.
23899                  */
23900                 token = un->un_swr_token;
23901                 mutex_exit(SD_MUTEX(un));
23902                 (void) scsi_watch_request_terminate(token,
23903                     SCSI_WATCH_TERMINATE_WAIT);
23904                 if (scsi_watch_get_ref_count(token) == 0) {
23905                         mutex_enter(SD_MUTEX(un));
23906                         un->un_swr_token = (opaque_t)NULL;
23907                 } else {
23908                         mutex_enter(SD_MUTEX(un));
23909                 }
23910         }
23911 
23912         /*
23913          * Update the capacity kstat value, if no media previously
23914          * (capacity kstat is 0) and a media has been inserted
23915          * (un_f_blockcount_is_valid == TRUE)
23916          */
23917         if (un->un_errstats) {
23918                 struct sd_errstats      *stp = NULL;
23919 
23920                 stp = (struct sd_errstats *)un->un_errstats->ks_data;
23921                 if ((stp->sd_capacity.value.ui64 == 0) &&
23922                     (un->un_f_blockcount_is_valid == TRUE)) {
23923                         stp->sd_capacity.value.ui64 =
23924                             (uint64_t)((uint64_t)un->un_blockcount *
23925                             un->un_sys_blocksize);
23926                 }
23927         }
23928         mutex_exit(SD_MUTEX(un));
23929         SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n");
23930         return (rval);
23931 }
23932 
23933 
23934 /*
23935  *    Function: sd_delayed_cv_broadcast
23936  *
23937  * Description: Delayed cv_broadcast to allow for target to recover from media
23938  *              insertion.
23939  *
23940  *   Arguments: arg - driver soft state (unit) structure
23941  */
23942 
23943 static void
23944 sd_delayed_cv_broadcast(void *arg)
23945 {
23946         struct sd_lun *un = arg;
23947 
23948         SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n");
23949 
23950         mutex_enter(SD_MUTEX(un));
23951         un->un_dcvb_timeid = NULL;
23952         cv_broadcast(&un->un_state_cv);
23953         mutex_exit(SD_MUTEX(un));
23954 }
23955 
23956 
23957 /*
23958  *    Function: sd_media_watch_cb
23959  *
23960  * Description: Callback routine used for support of the DKIOCSTATE ioctl. This
23961  *              routine processes the TUR sense data and updates the driver
23962  *              state if a transition has occurred. The user thread
23963  *              (sd_check_media) is then signalled.
23964  *
23965  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
23966  *                      among multiple watches that share this callback function
23967  *              resultp - scsi watch facility result packet containing scsi
23968  *                        packet, status byte and sense data
23969  *
23970  * Return Code: 0 for success, -1 for failure
23971  */
23972 
23973 static int
23974 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
23975 {
23976         struct sd_lun                   *un;
23977         struct scsi_status              *statusp = resultp->statusp;
23978         uint8_t                         *sensep = (uint8_t *)resultp->sensep;
23979         enum dkio_state                 state = DKIO_NONE;
23980         dev_t                           dev = (dev_t)arg;
23981         uchar_t                         actual_sense_length;
23982         uint8_t                         skey, asc, ascq;
23983 
23984         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23985                 return (-1);
23986         }
23987         actual_sense_length = resultp->actual_sense_length;
23988 
23989         mutex_enter(SD_MUTEX(un));
23990         SD_TRACE(SD_LOG_COMMON, un,
23991             "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n",
23992             *((char *)statusp), (void *)sensep, actual_sense_length);
23993 
23994         if (resultp->pkt->pkt_reason == CMD_DEV_GONE) {
23995                 un->un_mediastate = DKIO_DEV_GONE;
23996                 cv_broadcast(&un->un_state_cv);
23997                 mutex_exit(SD_MUTEX(un));
23998 
23999                 return (0);
24000         }
24001 
24002         if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
24003                 if (sd_gesn_media_data_valid(resultp->mmc_data)) {
24004                         if ((resultp->mmc_data[5] &
24005                             SD_GESN_MEDIA_EVENT_STATUS_PRESENT) != 0) {
24006                                 state = DKIO_INSERTED;
24007                         } else {
24008                                 state = DKIO_EJECTED;
24009                         }
24010                         if ((resultp->mmc_data[4] & SD_GESN_MEDIA_EVENT_CODE) ==
24011                             SD_GESN_MEDIA_EVENT_EJECTREQUEST) {
24012                                 sd_log_eject_request_event(un, KM_NOSLEEP);
24013                         }
24014                 }
24015         } else if (sensep != NULL) {
24016                 /*
24017                  * If there was a check condition then sensep points to valid
24018                  * sense data. If status was not a check condition but a
24019                  * reservation or busy status then the new state is DKIO_NONE.
24020                  */
24021                 skey = scsi_sense_key(sensep);
24022                 asc = scsi_sense_asc(sensep);
24023                 ascq = scsi_sense_ascq(sensep);
24024 
24025                 SD_INFO(SD_LOG_COMMON, un,
24026                     "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n",
24027                     skey, asc, ascq);
24028                 /* This routine only uses up to 13 bytes of sense data. */
24029                 if (actual_sense_length >= 13) {
24030                         if (skey == KEY_UNIT_ATTENTION) {
24031                                 if (asc == 0x28) {
24032                                         state = DKIO_INSERTED;
24033                                 }
24034                         } else if (skey == KEY_NOT_READY) {
24035                                 /*
24036                                  * Sense data of 02/06/00 means that the
24037                                  * drive could not read the media (No
24038                                  * reference position found). In this case
24039                                  * to prevent a hang on the DKIOCSTATE IOCTL
24040                                  * we set the media state to DKIO_INSERTED.
24041                                  */
24042                                 if (asc == 0x06 && ascq == 0x00)
24043                                         state = DKIO_INSERTED;
24044 
24045                                 /*
24046                                  * if 02/04/02  means that the host
24047                                  * should send start command. Explicitly
24048                                  * leave the media state as is
24049                                  * (inserted) as the media is inserted
24050                                  * and host has stopped device for PM
24051                                  * reasons. Upon next true read/write
24052                                  * to this media will bring the
24053                                  * device to the right state good for
24054                                  * media access.
24055                                  */
24056                                 if (asc == 0x3a) {
24057                                         state = DKIO_EJECTED;
24058                                 } else {
24059                                         /*
24060                                          * If the drive is busy with an
24061                                          * operation or long write, keep the
24062                                          * media in an inserted state.
24063                                          */
24064 
24065                                         if ((asc == 0x04) &&
24066                                             ((ascq == 0x02) ||
24067                                             (ascq == 0x07) ||
24068                                             (ascq == 0x08))) {
24069                                                 state = DKIO_INSERTED;
24070                                         }
24071                                 }
24072                         } else if (skey == KEY_NO_SENSE) {
24073                                 if ((asc == 0x00) && (ascq == 0x00)) {
24074                                         /*
24075                                          * Sense Data 00/00/00 does not provide
24076                                          * any information about the state of
24077                                          * the media. Ignore it.
24078                                          */
24079                                         mutex_exit(SD_MUTEX(un));
24080                                         return (0);
24081                                 }
24082                         }
24083                 }
24084         } else if ((*((char *)statusp) == STATUS_GOOD) &&
24085             (resultp->pkt->pkt_reason == CMD_CMPLT)) {
24086                 state = DKIO_INSERTED;
24087         }
24088 
24089         SD_TRACE(SD_LOG_COMMON, un,
24090             "sd_media_watch_cb: state=%x, specified=%x\n",
24091             state, un->un_specified_mediastate);
24092 
24093         /*
24094          * now signal the waiting thread if this is *not* the specified state;
24095          * delay the signal if the state is DKIO_INSERTED to allow the target
24096          * to recover
24097          */
24098         if (state != un->un_specified_mediastate) {
24099                 un->un_mediastate = state;
24100                 if (state == DKIO_INSERTED) {
24101                         /*
24102                          * delay the signal to give the drive a chance
24103                          * to do what it apparently needs to do
24104                          */
24105                         SD_TRACE(SD_LOG_COMMON, un,
24106                             "sd_media_watch_cb: delayed cv_broadcast\n");
24107                         if (un->un_dcvb_timeid == NULL) {
24108                                 un->un_dcvb_timeid =
24109                                     timeout(sd_delayed_cv_broadcast, un,
24110                                     drv_usectohz((clock_t)MEDIA_ACCESS_DELAY));
24111                         }
24112                 } else {
24113                         SD_TRACE(SD_LOG_COMMON, un,
24114                             "sd_media_watch_cb: immediate cv_broadcast\n");
24115                         cv_broadcast(&un->un_state_cv);
24116                 }
24117         }
24118         mutex_exit(SD_MUTEX(un));
24119         return (0);
24120 }
24121 
24122 
24123 /*
24124  *    Function: sd_dkio_get_temp
24125  *
24126  * Description: This routine is the driver entry point for handling ioctl
24127  *              requests to get the disk temperature.
24128  *
24129  *   Arguments: dev  - the device number
24130  *              arg  - pointer to user provided dk_temperature structure.
24131  *              flag - this argument is a pass through to ddi_copyxxx()
24132  *                     directly from the mode argument of ioctl().
24133  *
24134  * Return Code: 0
24135  *              EFAULT
24136  *              ENXIO
24137  *              EAGAIN
24138  */
24139 
24140 static int
24141 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag)
24142 {
24143         struct sd_lun           *un = NULL;
24144         struct dk_temperature   *dktemp = NULL;
24145         uchar_t                 *temperature_page;
24146         int                     rval = 0;
24147         int                     path_flag = SD_PATH_STANDARD;
24148         sd_ssc_t                *ssc;
24149 
24150         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24151                 return (ENXIO);
24152         }
24153 
24154         ssc = sd_ssc_init(un);
24155         dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP);
24156 
24157         /* copyin the disk temp argument to get the user flags */
24158         if (ddi_copyin((void *)arg, dktemp,
24159             sizeof (struct dk_temperature), flag) != 0) {
24160                 rval = EFAULT;
24161                 goto done;
24162         }
24163 
24164         /* Initialize the temperature to invalid. */
24165         dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24166         dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24167 
24168         /*
24169          * Note: Investigate removing the "bypass pm" semantic.
24170          * Can we just bypass PM always?
24171          */
24172         if (dktemp->dkt_flags & DKT_BYPASS_PM) {
24173                 path_flag = SD_PATH_DIRECT;
24174                 ASSERT(!mutex_owned(&un->un_pm_mutex));
24175                 mutex_enter(&un->un_pm_mutex);
24176                 if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
24177                         /*
24178                          * If DKT_BYPASS_PM is set, and the drive happens to be
24179                          * in low power mode, we can not wake it up, Need to
24180                          * return EAGAIN.
24181                          */
24182                         mutex_exit(&un->un_pm_mutex);
24183                         rval = EAGAIN;
24184                         goto done;
24185                 } else {
24186                         /*
24187                          * Indicate to PM the device is busy. This is required
24188                          * to avoid a race - i.e. the ioctl is issuing a
24189                          * command and the pm framework brings down the device
24190                          * to low power mode (possible power cut-off on some
24191                          * platforms).
24192                          */
24193                         mutex_exit(&un->un_pm_mutex);
24194                         if (sd_pm_entry(un) != DDI_SUCCESS) {
24195                                 rval = EAGAIN;
24196                                 goto done;
24197                         }
24198                 }
24199         }
24200 
24201         temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP);
24202 
24203         rval = sd_send_scsi_LOG_SENSE(ssc, temperature_page,
24204             TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag);
24205         if (rval != 0)
24206                 goto done2;
24207 
24208         /*
24209          * For the current temperature verify that the parameter length is 0x02
24210          * and the parameter code is 0x00
24211          */
24212         if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) &&
24213             (temperature_page[5] == 0x00)) {
24214                 if (temperature_page[9] == 0xFF) {
24215                         dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24216                 } else {
24217                         dktemp->dkt_cur_temp = (short)(temperature_page[9]);
24218                 }
24219         }
24220 
24221         /*
24222          * For the reference temperature verify that the parameter
24223          * length is 0x02 and the parameter code is 0x01
24224          */
24225         if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) &&
24226             (temperature_page[11] == 0x01)) {
24227                 if (temperature_page[15] == 0xFF) {
24228                         dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24229                 } else {
24230                         dktemp->dkt_ref_temp = (short)(temperature_page[15]);
24231                 }
24232         }
24233 
24234         /* Do the copyout regardless of the temperature commands status. */
24235         if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature),
24236             flag) != 0) {
24237                 rval = EFAULT;
24238                 goto done1;
24239         }
24240 
24241 done2:
24242         if (rval != 0) {
24243                 if (rval == EIO)
24244                         sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24245                 else
24246                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24247         }
24248 done1:
24249         if (path_flag == SD_PATH_DIRECT) {
24250                 sd_pm_exit(un);
24251         }
24252 
24253         kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE);
24254 done:
24255         sd_ssc_fini(ssc);
24256         if (dktemp != NULL) {
24257                 kmem_free(dktemp, sizeof (struct dk_temperature));
24258         }
24259 
24260         return (rval);
24261 }
24262 
24263 
24264 /*
24265  *    Function: sd_log_page_supported
24266  *
24267  * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of
24268  *              supported log pages.
24269  *
24270  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
24271  *                      structure for this target.
24272  *              log_page -
24273  *
24274  * Return Code: -1 - on error (log sense is optional and may not be supported).
24275  *              0  - log page not found.
24276  *              1  - log page found.
24277  */
24278 #ifdef notyet
24279 static int
24280 sd_log_page_supported(sd_ssc_t *ssc, int log_page)
24281 {
24282         uchar_t *log_page_data;
24283         int     i;
24284         int     match = 0;
24285         int     log_size;
24286         int     status = 0;
24287         struct sd_lun   *un;
24288 
24289         ASSERT(ssc != NULL);
24290         un = ssc->ssc_un;
24291         ASSERT(un != NULL);
24292 
24293         log_page_data = kmem_zalloc(0xFF, KM_SLEEP);
24294 
24295         status = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 0xFF, 0, 0x01, 0,
24296             SD_PATH_DIRECT);
24297 
24298         if (status != 0) {
24299                 if (status == EIO) {
24300                         /*
24301                          * Some disks do not support log sense, we
24302                          * should ignore this kind of error(sense key is
24303                          * 0x5 - illegal request).
24304                          */
24305                         uint8_t *sensep;
24306                         int senlen;
24307 
24308                         sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
24309                         senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
24310                             ssc->ssc_uscsi_cmd->uscsi_rqresid);
24311 
24312                         if (senlen > 0 &&
24313                             scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
24314                                 sd_ssc_assessment(ssc,
24315                                     SD_FMT_IGNORE_COMPROMISE);
24316                         } else {
24317                                 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24318                         }
24319                 } else {
24320                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24321                 }
24322 
24323                 SD_ERROR(SD_LOG_COMMON, un,
24324                     "sd_log_page_supported: failed log page retrieval\n");
24325                 kmem_free(log_page_data, 0xFF);
24326                 return (-1);
24327         }
24328 
24329         log_size = log_page_data[3];
24330 
24331         /*
24332          * The list of supported log pages start from the fourth byte. Check
24333          * until we run out of log pages or a match is found.
24334          */
24335         for (i = 4; (i < (log_size + 4)) && !match; i++) {
24336                 if (log_page_data[i] == log_page) {
24337                         match++;
24338                 }
24339         }
24340         kmem_free(log_page_data, 0xFF);
24341         return (match);
24342 }
24343 #endif
24344 
24345 /*
24346  *    Function: sd_mhdioc_failfast
24347  *
24348  * Description: This routine is the driver entry point for handling ioctl
24349  *              requests to enable/disable the multihost failfast option.
24350  *              (MHIOCENFAILFAST)
24351  *
24352  *   Arguments: dev     - the device number
24353  *              arg     - user specified probing interval.
24354  *              flag    - this argument is a pass through to ddi_copyxxx()
24355  *                        directly from the mode argument of ioctl().
24356  *
24357  * Return Code: 0
24358  *              EFAULT
24359  *              ENXIO
24360  */
24361 
24362 static int
24363 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag)
24364 {
24365         struct sd_lun   *un = NULL;
24366         int             mh_time;
24367         int             rval = 0;
24368 
24369         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24370                 return (ENXIO);
24371         }
24372 
24373         if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag))
24374                 return (EFAULT);
24375 
24376         if (mh_time) {
24377                 mutex_enter(SD_MUTEX(un));
24378                 un->un_resvd_status |= SD_FAILFAST;
24379                 mutex_exit(SD_MUTEX(un));
24380                 /*
24381                  * If mh_time is INT_MAX, then this ioctl is being used for
24382                  * SCSI-3 PGR purposes, and we don't need to spawn watch thread.
24383                  */
24384                 if (mh_time != INT_MAX) {
24385                         rval = sd_check_mhd(dev, mh_time);
24386                 }
24387         } else {
24388                 (void) sd_check_mhd(dev, 0);
24389                 mutex_enter(SD_MUTEX(un));
24390                 un->un_resvd_status &= ~SD_FAILFAST;
24391                 mutex_exit(SD_MUTEX(un));
24392         }
24393         return (rval);
24394 }
24395 
24396 
24397 /*
24398  *    Function: sd_mhdioc_takeown
24399  *
24400  * Description: This routine is the driver entry point for handling ioctl
24401  *              requests to forcefully acquire exclusive access rights to the
24402  *              multihost disk (MHIOCTKOWN).
24403  *
24404  *   Arguments: dev     - the device number
24405  *              arg     - user provided structure specifying the delay
24406  *                        parameters in milliseconds
24407  *              flag    - this argument is a pass through to ddi_copyxxx()
24408  *                        directly from the mode argument of ioctl().
24409  *
24410  * Return Code: 0
24411  *              EFAULT
24412  *              ENXIO
24413  */
24414 
24415 static int
24416 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag)
24417 {
24418         struct sd_lun           *un = NULL;
24419         struct mhioctkown       *tkown = NULL;
24420         int                     rval = 0;
24421 
24422         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24423                 return (ENXIO);
24424         }
24425 
24426         if (arg != NULL) {
24427                 tkown = (struct mhioctkown *)
24428                     kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP);
24429                 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag);
24430                 if (rval != 0) {
24431                         rval = EFAULT;
24432                         goto error;
24433                 }
24434         }
24435 
24436         rval = sd_take_ownership(dev, tkown);
24437         mutex_enter(SD_MUTEX(un));
24438         if (rval == 0) {
24439                 un->un_resvd_status |= SD_RESERVE;
24440                 if (tkown != NULL && tkown->reinstate_resv_delay != 0) {
24441                         sd_reinstate_resv_delay =
24442                             tkown->reinstate_resv_delay * 1000;
24443                 } else {
24444                         sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
24445                 }
24446                 /*
24447                  * Give the scsi_watch routine interval set by
24448                  * the MHIOCENFAILFAST ioctl precedence here.
24449                  */
24450                 if ((un->un_resvd_status & SD_FAILFAST) == 0) {
24451                         mutex_exit(SD_MUTEX(un));
24452                         (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000);
24453                         SD_TRACE(SD_LOG_IOCTL_MHD, un,
24454                             "sd_mhdioc_takeown : %d\n",
24455                             sd_reinstate_resv_delay);
24456                 } else {
24457                         mutex_exit(SD_MUTEX(un));
24458                 }
24459                 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY,
24460                     sd_mhd_reset_notify_cb, (caddr_t)un);
24461         } else {
24462                 un->un_resvd_status &= ~SD_RESERVE;
24463                 mutex_exit(SD_MUTEX(un));
24464         }
24465 
24466 error:
24467         if (tkown != NULL) {
24468                 kmem_free(tkown, sizeof (struct mhioctkown));
24469         }
24470         return (rval);
24471 }
24472 
24473 
24474 /*
24475  *    Function: sd_mhdioc_release
24476  *
24477  * Description: This routine is the driver entry point for handling ioctl
24478  *              requests to release exclusive access rights to the multihost
24479  *              disk (MHIOCRELEASE).
24480  *
24481  *   Arguments: dev     - the device number
24482  *
24483  * Return Code: 0
24484  *              ENXIO
24485  */
24486 
24487 static int
24488 sd_mhdioc_release(dev_t dev)
24489 {
24490         struct sd_lun           *un = NULL;
24491         timeout_id_t            resvd_timeid_save;
24492         int                     resvd_status_save;
24493         int                     rval = 0;
24494 
24495         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24496                 return (ENXIO);
24497         }
24498 
24499         mutex_enter(SD_MUTEX(un));
24500         resvd_status_save = un->un_resvd_status;
24501         un->un_resvd_status &=
24502             ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE);
24503         if (un->un_resvd_timeid) {
24504                 resvd_timeid_save = un->un_resvd_timeid;
24505                 un->un_resvd_timeid = NULL;
24506                 mutex_exit(SD_MUTEX(un));
24507                 (void) untimeout(resvd_timeid_save);
24508         } else {
24509                 mutex_exit(SD_MUTEX(un));
24510         }
24511 
24512         /*
24513          * destroy any pending timeout thread that may be attempting to
24514          * reinstate reservation on this device.
24515          */
24516         sd_rmv_resv_reclaim_req(dev);
24517 
24518         if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) {
24519                 mutex_enter(SD_MUTEX(un));
24520                 if ((un->un_mhd_token) &&
24521                     ((un->un_resvd_status & SD_FAILFAST) == 0)) {
24522                         mutex_exit(SD_MUTEX(un));
24523                         (void) sd_check_mhd(dev, 0);
24524                 } else {
24525                         mutex_exit(SD_MUTEX(un));
24526                 }
24527                 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
24528                     sd_mhd_reset_notify_cb, (caddr_t)un);
24529         } else {
24530                 /*
24531                  * sd_mhd_watch_cb will restart the resvd recover timeout thread
24532                  */
24533                 mutex_enter(SD_MUTEX(un));
24534                 un->un_resvd_status = resvd_status_save;
24535                 mutex_exit(SD_MUTEX(un));
24536         }
24537         return (rval);
24538 }
24539 
24540 
24541 /*
24542  *    Function: sd_mhdioc_register_devid
24543  *
24544  * Description: This routine is the driver entry point for handling ioctl
24545  *              requests to register the device id (MHIOCREREGISTERDEVID).
24546  *
24547  *              Note: The implementation for this ioctl has been updated to
24548  *              be consistent with the original PSARC case (1999/357)
24549  *              (4375899, 4241671, 4220005)
24550  *
24551  *   Arguments: dev     - the device number
24552  *
24553  * Return Code: 0
24554  *              ENXIO
24555  */
24556 
24557 static int
24558 sd_mhdioc_register_devid(dev_t dev)
24559 {
24560         struct sd_lun   *un = NULL;
24561         int             rval = 0;
24562         sd_ssc_t        *ssc;
24563 
24564         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24565                 return (ENXIO);
24566         }
24567 
24568         ASSERT(!mutex_owned(SD_MUTEX(un)));
24569 
24570         mutex_enter(SD_MUTEX(un));
24571 
24572         /* If a devid already exists, de-register it */
24573         if (un->un_devid != NULL) {
24574                 ddi_devid_unregister(SD_DEVINFO(un));
24575                 /*
24576                  * After unregister devid, needs to free devid memory
24577                  */
24578                 ddi_devid_free(un->un_devid);
24579                 un->un_devid = NULL;
24580         }
24581 
24582         /* Check for reservation conflict */
24583         mutex_exit(SD_MUTEX(un));
24584         ssc = sd_ssc_init(un);
24585         rval = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
24586         mutex_enter(SD_MUTEX(un));
24587 
24588         switch (rval) {
24589         case 0:
24590                 sd_register_devid(ssc, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED);
24591                 break;
24592         case EACCES:
24593                 break;
24594         default:
24595                 rval = EIO;
24596         }
24597 
24598         mutex_exit(SD_MUTEX(un));
24599         if (rval != 0) {
24600                 if (rval == EIO)
24601                         sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24602                 else
24603                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24604         }
24605         sd_ssc_fini(ssc);
24606         return (rval);
24607 }
24608 
24609 
24610 /*
24611  *    Function: sd_mhdioc_inkeys
24612  *
24613  * Description: This routine is the driver entry point for handling ioctl
24614  *              requests to issue the SCSI-3 Persistent In Read Keys command
24615  *              to the device (MHIOCGRP_INKEYS).
24616  *
24617  *   Arguments: dev     - the device number
24618  *              arg     - user provided in_keys structure
24619  *              flag    - this argument is a pass through to ddi_copyxxx()
24620  *                        directly from the mode argument of ioctl().
24621  *
24622  * Return Code: code returned by sd_persistent_reservation_in_read_keys()
24623  *              ENXIO
24624  *              EFAULT
24625  */
24626 
24627 static int
24628 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag)
24629 {
24630         struct sd_lun           *un;
24631         mhioc_inkeys_t          inkeys;
24632         int                     rval = 0;
24633 
24634         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24635                 return (ENXIO);
24636         }
24637 
24638 #ifdef _MULTI_DATAMODEL
24639         switch (ddi_model_convert_from(flag & FMODELS)) {
24640         case DDI_MODEL_ILP32: {
24641                 struct mhioc_inkeys32   inkeys32;
24642 
24643                 if (ddi_copyin(arg, &inkeys32,
24644                     sizeof (struct mhioc_inkeys32), flag) != 0) {
24645                         return (EFAULT);
24646                 }
24647                 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li;
24648                 if ((rval = sd_persistent_reservation_in_read_keys(un,
24649                     &inkeys, flag)) != 0) {
24650                         return (rval);
24651                 }
24652                 inkeys32.generation = inkeys.generation;
24653                 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32),
24654                     flag) != 0) {
24655                         return (EFAULT);
24656                 }
24657                 break;
24658         }
24659         case DDI_MODEL_NONE:
24660                 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t),
24661                     flag) != 0) {
24662                         return (EFAULT);
24663                 }
24664                 if ((rval = sd_persistent_reservation_in_read_keys(un,
24665                     &inkeys, flag)) != 0) {
24666                         return (rval);
24667                 }
24668                 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t),
24669                     flag) != 0) {
24670                         return (EFAULT);
24671                 }
24672                 break;
24673         }
24674 
24675 #else /* ! _MULTI_DATAMODEL */
24676 
24677         if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) {
24678                 return (EFAULT);
24679         }
24680         rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag);
24681         if (rval != 0) {
24682                 return (rval);
24683         }
24684         if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) {
24685                 return (EFAULT);
24686         }
24687 
24688 #endif /* _MULTI_DATAMODEL */
24689 
24690         return (rval);
24691 }
24692 
24693 
24694 /*
24695  *    Function: sd_mhdioc_inresv
24696  *
24697  * Description: This routine is the driver entry point for handling ioctl
24698  *              requests to issue the SCSI-3 Persistent In Read Reservations
24699  *              command to the device (MHIOCGRP_INKEYS).
24700  *
24701  *   Arguments: dev     - the device number
24702  *              arg     - user provided in_resv structure
24703  *              flag    - this argument is a pass through to ddi_copyxxx()
24704  *                        directly from the mode argument of ioctl().
24705  *
24706  * Return Code: code returned by sd_persistent_reservation_in_read_resv()
24707  *              ENXIO
24708  *              EFAULT
24709  */
24710 
24711 static int
24712 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag)
24713 {
24714         struct sd_lun           *un;
24715         mhioc_inresvs_t         inresvs;
24716         int                     rval = 0;
24717 
24718         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24719                 return (ENXIO);
24720         }
24721 
24722 #ifdef _MULTI_DATAMODEL
24723 
24724         switch (ddi_model_convert_from(flag & FMODELS)) {
24725         case DDI_MODEL_ILP32: {
24726                 struct mhioc_inresvs32  inresvs32;
24727 
24728                 if (ddi_copyin(arg, &inresvs32,
24729                     sizeof (struct mhioc_inresvs32), flag) != 0) {
24730                         return (EFAULT);
24731                 }
24732                 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li;
24733                 if ((rval = sd_persistent_reservation_in_read_resv(un,
24734                     &inresvs, flag)) != 0) {
24735                         return (rval);
24736                 }
24737                 inresvs32.generation = inresvs.generation;
24738                 if (ddi_copyout(&inresvs32, arg,
24739                     sizeof (struct mhioc_inresvs32), flag) != 0) {
24740                         return (EFAULT);
24741                 }
24742                 break;
24743         }
24744         case DDI_MODEL_NONE:
24745                 if (ddi_copyin(arg, &inresvs,
24746                     sizeof (mhioc_inresvs_t), flag) != 0) {
24747                         return (EFAULT);
24748                 }
24749                 if ((rval = sd_persistent_reservation_in_read_resv(un,
24750                     &inresvs, flag)) != 0) {
24751                         return (rval);
24752                 }
24753                 if (ddi_copyout(&inresvs, arg,
24754                     sizeof (mhioc_inresvs_t), flag) != 0) {
24755                         return (EFAULT);
24756                 }
24757                 break;
24758         }
24759 
24760 #else /* ! _MULTI_DATAMODEL */
24761 
24762         if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) {
24763                 return (EFAULT);
24764         }
24765         rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag);
24766         if (rval != 0) {
24767                 return (rval);
24768         }
24769         if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) {
24770                 return (EFAULT);
24771         }
24772 
24773 #endif /* ! _MULTI_DATAMODEL */
24774 
24775         return (rval);
24776 }
24777 
24778 
24779 /*
24780  * The following routines support the clustering functionality described below
24781  * and implement lost reservation reclaim functionality.
24782  *
24783  * Clustering
24784  * ----------
24785  * The clustering code uses two different, independent forms of SCSI
24786  * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3
24787  * Persistent Group Reservations. For any particular disk, it will use either
24788  * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk.
24789  *
24790  * SCSI-2
24791  * The cluster software takes ownership of a multi-hosted disk by issuing the
24792  * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the
24793  * MHIOCRELEASE ioctl.  Closely related is the MHIOCENFAILFAST ioctl -- a
24794  * cluster, just after taking ownership of the disk with the MHIOCTKOWN ioctl
24795  * then issues the MHIOCENFAILFAST ioctl.  This ioctl "enables failfast" in the
24796  * driver. The meaning of failfast is that if the driver (on this host) ever
24797  * encounters the scsi error return code RESERVATION_CONFLICT from the device,
24798  * it should immediately panic the host. The motivation for this ioctl is that
24799  * if this host does encounter reservation conflict, the underlying cause is
24800  * that some other host of the cluster has decided that this host is no longer
24801  * in the cluster and has seized control of the disks for itself. Since this
24802  * host is no longer in the cluster, it ought to panic itself. The
24803  * MHIOCENFAILFAST ioctl does two things:
24804  *      (a) it sets a flag that will cause any returned RESERVATION_CONFLICT
24805  *      error to panic the host
24806  *      (b) it sets up a periodic timer to test whether this host still has
24807  *      "access" (in that no other host has reserved the device):  if the
24808  *      periodic timer gets RESERVATION_CONFLICT, the host is panicked. The
24809  *      purpose of that periodic timer is to handle scenarios where the host is
24810  *      otherwise temporarily quiescent, temporarily doing no real i/o.
24811  * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host,
24812  * by issuing a SCSI Bus Device Reset.  It will then issue a SCSI Reserve for
24813  * the device itself.
24814  *
24815  * SCSI-3 PGR
24816  * A direct semantic implementation of the SCSI-3 Persistent Reservation
24817  * facility is supported through the shared multihost disk ioctls
24818  * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE,
24819  * MHIOCGRP_PREEMPTANDABORT, MHIOCGRP_CLEAR)
24820  *
24821  * Reservation Reclaim:
24822  * --------------------
24823  * To support the lost reservation reclaim operations this driver creates a
24824  * single thread to handle reinstating reservations on all devices that have
24825  * lost reservations sd_resv_reclaim_requests are logged for all devices that
24826  * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb
24827  * and the reservation reclaim thread loops through the requests to regain the
24828  * lost reservations.
24829  */
24830 
24831 /*
24832  *    Function: sd_check_mhd()
24833  *
24834  * Description: This function sets up and submits a scsi watch request or
24835  *              terminates an existing watch request. This routine is used in
24836  *              support of reservation reclaim.
24837  *
24838  *   Arguments: dev    - the device 'dev_t' is used for context to discriminate
24839  *                       among multiple watches that share the callback function
24840  *              interval - the number of microseconds specifying the watch
24841  *                         interval for issuing TEST UNIT READY commands. If
24842  *                         set to 0 the watch should be terminated. If the
24843  *                         interval is set to 0 and if the device is required
24844  *                         to hold reservation while disabling failfast, the
24845  *                         watch is restarted with an interval of
24846  *                         reinstate_resv_delay.
24847  *
24848  * Return Code: 0          - Successful submit/terminate of scsi watch request
24849  *              ENXIO      - Indicates an invalid device was specified
24850  *              EAGAIN     - Unable to submit the scsi watch request
24851  */
24852 
24853 static int
24854 sd_check_mhd(dev_t dev, int interval)
24855 {
24856         struct sd_lun   *un;
24857         opaque_t        token;
24858 
24859         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24860                 return (ENXIO);
24861         }
24862 
24863         /* is this a watch termination request? */
24864         if (interval == 0) {
24865                 mutex_enter(SD_MUTEX(un));
24866                 /* if there is an existing watch task then terminate it */
24867                 if (un->un_mhd_token) {
24868                         token = un->un_mhd_token;
24869                         un->un_mhd_token = NULL;
24870                         mutex_exit(SD_MUTEX(un));
24871                         (void) scsi_watch_request_terminate(token,
24872                             SCSI_WATCH_TERMINATE_ALL_WAIT);
24873                         mutex_enter(SD_MUTEX(un));
24874                 } else {
24875                         mutex_exit(SD_MUTEX(un));
24876                         /*
24877                          * Note: If we return here we don't check for the
24878                          * failfast case. This is the original legacy
24879                          * implementation but perhaps we should be checking
24880                          * the failfast case.
24881                          */
24882                         return (0);
24883                 }
24884                 /*
24885                  * If the device is required to hold reservation while
24886                  * disabling failfast, we need to restart the scsi_watch
24887                  * routine with an interval of reinstate_resv_delay.
24888                  */
24889                 if (un->un_resvd_status & SD_RESERVE) {
24890                         interval = sd_reinstate_resv_delay/1000;
24891                 } else {
24892                         /* no failfast so bail */
24893                         mutex_exit(SD_MUTEX(un));
24894                         return (0);
24895                 }
24896                 mutex_exit(SD_MUTEX(un));
24897         }
24898 
24899         /*
24900          * adjust minimum time interval to 1 second,
24901          * and convert from msecs to usecs
24902          */
24903         if (interval > 0 && interval < 1000) {
24904                 interval = 1000;
24905         }
24906         interval *= 1000;
24907 
24908         /*
24909          * submit the request to the scsi_watch service
24910          */
24911         token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval,
24912             SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev);
24913         if (token == NULL) {
24914                 return (EAGAIN);
24915         }
24916 
24917         /*
24918          * save token for termination later on
24919          */
24920         mutex_enter(SD_MUTEX(un));
24921         un->un_mhd_token = token;
24922         mutex_exit(SD_MUTEX(un));
24923         return (0);
24924 }
24925 
24926 
24927 /*
24928  *    Function: sd_mhd_watch_cb()
24929  *
24930  * Description: This function is the call back function used by the scsi watch
24931  *              facility. The scsi watch facility sends the "Test Unit Ready"
24932  *              and processes the status. If applicable (i.e. a "Unit Attention"
24933  *              status and automatic "Request Sense" not used) the scsi watch
24934  *              facility will send a "Request Sense" and retrieve the sense data
24935  *              to be passed to this callback function. In either case the
24936  *              automatic "Request Sense" or the facility submitting one, this
24937  *              callback is passed the status and sense data.
24938  *
24939  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
24940  *                      among multiple watches that share this callback function
24941  *              resultp - scsi watch facility result packet containing scsi
24942  *                        packet, status byte and sense data
24943  *
24944  * Return Code: 0 - continue the watch task
24945  *              non-zero - terminate the watch task
24946  */
24947 
24948 static int
24949 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
24950 {
24951         struct sd_lun                   *un;
24952         struct scsi_status              *statusp;
24953         uint8_t                         *sensep;
24954         struct scsi_pkt                 *pkt;
24955         uchar_t                         actual_sense_length;
24956         dev_t                           dev = (dev_t)arg;
24957 
24958         ASSERT(resultp != NULL);
24959         statusp                 = resultp->statusp;
24960         sensep                  = (uint8_t *)resultp->sensep;
24961         pkt                     = resultp->pkt;
24962         actual_sense_length     = resultp->actual_sense_length;
24963 
24964         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24965                 return (ENXIO);
24966         }
24967 
24968         SD_TRACE(SD_LOG_IOCTL_MHD, un,
24969             "sd_mhd_watch_cb: reason '%s', status '%s'\n",
24970             scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp)));
24971 
24972         /* Begin processing of the status and/or sense data */
24973         if (pkt->pkt_reason != CMD_CMPLT) {
24974                 /* Handle the incomplete packet */
24975                 sd_mhd_watch_incomplete(un, pkt);
24976                 return (0);
24977         } else if (*((unsigned char *)statusp) != STATUS_GOOD) {
24978                 if (*((unsigned char *)statusp)
24979                     == STATUS_RESERVATION_CONFLICT) {
24980                         /*
24981                          * Handle a reservation conflict by panicking if
24982                          * configured for failfast or by logging the conflict
24983                          * and updating the reservation status
24984                          */
24985                         mutex_enter(SD_MUTEX(un));
24986                         if ((un->un_resvd_status & SD_FAILFAST) &&
24987                             (sd_failfast_enable)) {
24988                                 sd_panic_for_res_conflict(un);
24989                                 /*NOTREACHED*/
24990                         }
24991                         SD_INFO(SD_LOG_IOCTL_MHD, un,
24992                             "sd_mhd_watch_cb: Reservation Conflict\n");
24993                         un->un_resvd_status |= SD_RESERVATION_CONFLICT;
24994                         mutex_exit(SD_MUTEX(un));
24995                 }
24996         }
24997 
24998         if (sensep != NULL) {
24999                 if (actual_sense_length >= (SENSE_LENGTH - 2)) {
25000                         mutex_enter(SD_MUTEX(un));
25001                         if ((scsi_sense_asc(sensep) ==
25002                             SD_SCSI_RESET_SENSE_CODE) &&
25003                             (un->un_resvd_status & SD_RESERVE)) {
25004                                 /*
25005                                  * The additional sense code indicates a power
25006                                  * on or bus device reset has occurred; update
25007                                  * the reservation status.
25008                                  */
25009                                 un->un_resvd_status |=
25010                                     (SD_LOST_RESERVE | SD_WANT_RESERVE);
25011                                 SD_INFO(SD_LOG_IOCTL_MHD, un,
25012                                     "sd_mhd_watch_cb: Lost Reservation\n");
25013                         }
25014                 } else {
25015                         return (0);
25016                 }
25017         } else {
25018                 mutex_enter(SD_MUTEX(un));
25019         }
25020 
25021         if ((un->un_resvd_status & SD_RESERVE) &&
25022             (un->un_resvd_status & SD_LOST_RESERVE)) {
25023                 if (un->un_resvd_status & SD_WANT_RESERVE) {
25024                         /*
25025                          * A reset occurred in between the last probe and this
25026                          * one so if a timeout is pending cancel it.
25027                          */
25028                         if (un->un_resvd_timeid) {
25029                                 timeout_id_t temp_id = un->un_resvd_timeid;
25030                                 un->un_resvd_timeid = NULL;
25031                                 mutex_exit(SD_MUTEX(un));
25032                                 (void) untimeout(temp_id);
25033                                 mutex_enter(SD_MUTEX(un));
25034                         }
25035                         un->un_resvd_status &= ~SD_WANT_RESERVE;
25036                 }
25037                 if (un->un_resvd_timeid == 0) {
25038                         /* Schedule a timeout to handle the lost reservation */
25039                         un->un_resvd_timeid = timeout(sd_mhd_resvd_recover,
25040                             (void *)dev,
25041                             drv_usectohz(sd_reinstate_resv_delay));
25042                 }
25043         }
25044         mutex_exit(SD_MUTEX(un));
25045         return (0);
25046 }
25047 
25048 
25049 /*
25050  *    Function: sd_mhd_watch_incomplete()
25051  *
25052  * Description: This function is used to find out why a scsi pkt sent by the
25053  *              scsi watch facility was not completed. Under some scenarios this
25054  *              routine will return. Otherwise it will send a bus reset to see
25055  *              if the drive is still online.
25056  *
25057  *   Arguments: un  - driver soft state (unit) structure
25058  *              pkt - incomplete scsi pkt
25059  */
25060 
25061 static void
25062 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt)
25063 {
25064         int     be_chatty;
25065         int     perr;
25066 
25067         ASSERT(pkt != NULL);
25068         ASSERT(un != NULL);
25069         be_chatty       = (!(pkt->pkt_flags & FLAG_SILENT));
25070         perr            = (pkt->pkt_statistics & STAT_PERR);
25071 
25072         mutex_enter(SD_MUTEX(un));
25073         if (un->un_state == SD_STATE_DUMPING) {
25074                 mutex_exit(SD_MUTEX(un));
25075                 return;
25076         }
25077 
25078         switch (pkt->pkt_reason) {
25079         case CMD_UNX_BUS_FREE:
25080                 /*
25081                  * If we had a parity error that caused the target to drop BSY*,
25082                  * don't be chatty about it.
25083                  */
25084                 if (perr && be_chatty) {
25085                         be_chatty = 0;
25086                 }
25087                 break;
25088         case CMD_TAG_REJECT:
25089                 /*
25090                  * The SCSI-2 spec states that a tag reject will be sent by the
25091                  * target if tagged queuing is not supported. A tag reject may
25092                  * also be sent during certain initialization periods or to
25093                  * control internal resources. For the latter case the target
25094                  * may also return Queue Full.
25095                  *
25096                  * If this driver receives a tag reject from a target that is
25097                  * going through an init period or controlling internal
25098                  * resources tagged queuing will be disabled. This is a less
25099                  * than optimal behavior but the driver is unable to determine
25100                  * the target state and assumes tagged queueing is not supported
25101                  */
25102                 pkt->pkt_flags = 0;
25103                 un->un_tagflags = 0;
25104 
25105                 if (un->un_f_opt_queueing == TRUE) {
25106                         un->un_throttle = min(un->un_throttle, 3);
25107                 } else {
25108                         un->un_throttle = 1;
25109                 }
25110                 mutex_exit(SD_MUTEX(un));
25111                 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
25112                 mutex_enter(SD_MUTEX(un));
25113                 break;
25114         case CMD_INCOMPLETE:
25115                 /*
25116                  * The transport stopped with an abnormal state, fallthrough and
25117                  * reset the target and/or bus unless selection did not complete
25118                  * (indicated by STATE_GOT_BUS) in which case we don't want to
25119                  * go through a target/bus reset
25120                  */
25121                 if (pkt->pkt_state == STATE_GOT_BUS) {
25122                         break;
25123                 }
25124                 /*FALLTHROUGH*/
25125 
25126         case CMD_TIMEOUT:
25127         default:
25128                 /*
25129                  * The lun may still be running the command, so a lun reset
25130                  * should be attempted. If the lun reset fails or cannot be
25131                  * issued, than try a target reset. Lastly try a bus reset.
25132                  */
25133                 if ((pkt->pkt_statistics &
25134                     (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) {
25135                         int reset_retval = 0;
25136                         mutex_exit(SD_MUTEX(un));
25137                         if (un->un_f_allow_bus_device_reset == TRUE) {
25138                                 if (un->un_f_lun_reset_enabled == TRUE) {
25139                                         reset_retval =
25140                                             scsi_reset(SD_ADDRESS(un),
25141                                             RESET_LUN);
25142                                 }
25143                                 if (reset_retval == 0) {
25144                                         reset_retval =
25145                                             scsi_reset(SD_ADDRESS(un),
25146                                             RESET_TARGET);
25147                                 }
25148                         }
25149                         if (reset_retval == 0) {
25150                                 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
25151                         }
25152                         mutex_enter(SD_MUTEX(un));
25153                 }
25154                 break;
25155         }
25156 
25157         /* A device/bus reset has occurred; update the reservation status. */
25158         if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics &
25159             (STAT_BUS_RESET | STAT_DEV_RESET))) {
25160                 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25161                         un->un_resvd_status |=
25162                             (SD_LOST_RESERVE | SD_WANT_RESERVE);
25163                         SD_INFO(SD_LOG_IOCTL_MHD, un,
25164                             "sd_mhd_watch_incomplete: Lost Reservation\n");
25165                 }
25166         }
25167 
25168         /*
25169          * The disk has been turned off; Update the device state.
25170          *
25171          * Note: Should we be offlining the disk here?
25172          */
25173         if (pkt->pkt_state == STATE_GOT_BUS) {
25174                 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: "
25175                     "Disk not responding to selection\n");
25176                 if (un->un_state != SD_STATE_OFFLINE) {
25177                         New_state(un, SD_STATE_OFFLINE);
25178                 }
25179         } else if (be_chatty) {
25180                 /*
25181                  * suppress messages if they are all the same pkt reason;
25182                  * with TQ, many (up to 256) are returned with the same
25183                  * pkt_reason
25184                  */
25185                 if (pkt->pkt_reason != un->un_last_pkt_reason) {
25186                         SD_ERROR(SD_LOG_IOCTL_MHD, un,
25187                             "sd_mhd_watch_incomplete: "
25188                             "SCSI transport failed: reason '%s'\n",
25189                             scsi_rname(pkt->pkt_reason));
25190                 }
25191         }
25192         un->un_last_pkt_reason = pkt->pkt_reason;
25193         mutex_exit(SD_MUTEX(un));
25194 }
25195 
25196 
25197 /*
25198  *    Function: sd_sname()
25199  *
25200  * Description: This is a simple little routine to return a string containing
25201  *              a printable description of command status byte for use in
25202  *              logging.
25203  *
25204  *   Arguments: status - pointer to a status byte
25205  *
25206  * Return Code: char * - string containing status description.
25207  */
25208 
25209 static char *
25210 sd_sname(uchar_t status)
25211 {
25212         switch (status & STATUS_MASK) {
25213         case STATUS_GOOD:
25214                 return ("good status");
25215         case STATUS_CHECK:
25216                 return ("check condition");
25217         case STATUS_MET:
25218                 return ("condition met");
25219         case STATUS_BUSY:
25220                 return ("busy");
25221         case STATUS_INTERMEDIATE:
25222                 return ("intermediate");
25223         case STATUS_INTERMEDIATE_MET:
25224                 return ("intermediate - condition met");
25225         case STATUS_RESERVATION_CONFLICT:
25226                 return ("reservation_conflict");
25227         case STATUS_TERMINATED:
25228                 return ("command terminated");
25229         case STATUS_QFULL:
25230                 return ("queue full");
25231         default:
25232                 return ("<unknown status>");
25233         }
25234 }
25235 
25236 
25237 /*
25238  *    Function: sd_mhd_resvd_recover()
25239  *
25240  * Description: This function adds a reservation entry to the
25241  *              sd_resv_reclaim_request list and signals the reservation
25242  *              reclaim thread that there is work pending. If the reservation
25243  *              reclaim thread has not been previously created this function
25244  *              will kick it off.
25245  *
25246  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
25247  *                      among multiple watches that share this callback function
25248  *
25249  *     Context: This routine is called by timeout() and is run in interrupt
25250  *              context. It must not sleep or call other functions which may
25251  *              sleep.
25252  */
25253 
25254 static void
25255 sd_mhd_resvd_recover(void *arg)
25256 {
25257         dev_t                   dev = (dev_t)arg;
25258         struct sd_lun           *un;
25259         struct sd_thr_request   *sd_treq = NULL;
25260         struct sd_thr_request   *sd_cur = NULL;
25261         struct sd_thr_request   *sd_prev = NULL;
25262         int                     already_there = 0;
25263 
25264         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25265                 return;
25266         }
25267 
25268         mutex_enter(SD_MUTEX(un));
25269         un->un_resvd_timeid = NULL;
25270         if (un->un_resvd_status & SD_WANT_RESERVE) {
25271                 /*
25272                  * There was a reset so don't issue the reserve, allow the
25273                  * sd_mhd_watch_cb callback function to notice this and
25274                  * reschedule the timeout for reservation.
25275                  */
25276                 mutex_exit(SD_MUTEX(un));
25277                 return;
25278         }
25279         mutex_exit(SD_MUTEX(un));
25280 
25281         /*
25282          * Add this device to the sd_resv_reclaim_request list and the
25283          * sd_resv_reclaim_thread should take care of the rest.
25284          *
25285          * Note: We can't sleep in this context so if the memory allocation
25286          * fails allow the sd_mhd_watch_cb callback function to notice this and
25287          * reschedule the timeout for reservation.  (4378460)
25288          */
25289         sd_treq = (struct sd_thr_request *)
25290             kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP);
25291         if (sd_treq == NULL) {
25292                 return;
25293         }
25294 
25295         sd_treq->sd_thr_req_next = NULL;
25296         sd_treq->dev = dev;
25297         mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25298         if (sd_tr.srq_thr_req_head == NULL) {
25299                 sd_tr.srq_thr_req_head = sd_treq;
25300         } else {
25301                 sd_cur = sd_prev = sd_tr.srq_thr_req_head;
25302                 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) {
25303                         if (sd_cur->dev == dev) {
25304                                 /*
25305                                  * already in Queue so don't log
25306                                  * another request for the device
25307                                  */
25308                                 already_there = 1;
25309                                 break;
25310                         }
25311                         sd_prev = sd_cur;
25312                 }
25313                 if (!already_there) {
25314                         SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: "
25315                             "logging request for %lx\n", dev);
25316                         sd_prev->sd_thr_req_next = sd_treq;
25317                 } else {
25318                         kmem_free(sd_treq, sizeof (struct sd_thr_request));
25319                 }
25320         }
25321 
25322         /*
25323          * Create a kernel thread to do the reservation reclaim and free up this
25324          * thread. We cannot block this thread while we go away to do the
25325          * reservation reclaim
25326          */
25327         if (sd_tr.srq_resv_reclaim_thread == NULL)
25328                 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0,
25329                     sd_resv_reclaim_thread, NULL,
25330                     0, &p0, TS_RUN, v.v_maxsyspri - 2);
25331 
25332         /* Tell the reservation reclaim thread that it has work to do */
25333         cv_signal(&sd_tr.srq_resv_reclaim_cv);
25334         mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25335 }
25336 
25337 /*
25338  *    Function: sd_resv_reclaim_thread()
25339  *
25340  * Description: This function implements the reservation reclaim operations
25341  *
25342  *   Arguments: arg - the device 'dev_t' is used for context to discriminate
25343  *                    among multiple watches that share this callback function
25344  */
25345 
25346 static void
25347 sd_resv_reclaim_thread()
25348 {
25349         struct sd_lun           *un;
25350         struct sd_thr_request   *sd_mhreq;
25351 
25352         /* Wait for work */
25353         mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25354         if (sd_tr.srq_thr_req_head == NULL) {
25355                 cv_wait(&sd_tr.srq_resv_reclaim_cv,
25356                     &sd_tr.srq_resv_reclaim_mutex);
25357         }
25358 
25359         /* Loop while we have work */
25360         while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) {
25361                 un = ddi_get_soft_state(sd_state,
25362                     SDUNIT(sd_tr.srq_thr_cur_req->dev));
25363                 if (un == NULL) {
25364                         /*
25365                          * softstate structure is NULL so just
25366                          * dequeue the request and continue
25367                          */
25368                         sd_tr.srq_thr_req_head =
25369                             sd_tr.srq_thr_cur_req->sd_thr_req_next;
25370                         kmem_free(sd_tr.srq_thr_cur_req,
25371                             sizeof (struct sd_thr_request));
25372                         continue;
25373                 }
25374 
25375                 /* dequeue the request */
25376                 sd_mhreq = sd_tr.srq_thr_cur_req;
25377                 sd_tr.srq_thr_req_head =
25378                     sd_tr.srq_thr_cur_req->sd_thr_req_next;
25379                 mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25380 
25381                 /*
25382                  * Reclaim reservation only if SD_RESERVE is still set. There
25383                  * may have been a call to MHIOCRELEASE before we got here.
25384                  */
25385                 mutex_enter(SD_MUTEX(un));
25386                 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25387                         /*
25388                          * Note: The SD_LOST_RESERVE flag is cleared before
25389                          * reclaiming the reservation. If this is done after the
25390                          * call to sd_reserve_release a reservation loss in the
25391                          * window between pkt completion of reserve cmd and
25392                          * mutex_enter below may not be recognized
25393                          */
25394                         un->un_resvd_status &= ~SD_LOST_RESERVE;
25395                         mutex_exit(SD_MUTEX(un));
25396 
25397                         if (sd_reserve_release(sd_mhreq->dev,
25398                             SD_RESERVE) == 0) {
25399                                 mutex_enter(SD_MUTEX(un));
25400                                 un->un_resvd_status |= SD_RESERVE;
25401                                 mutex_exit(SD_MUTEX(un));
25402                                 SD_INFO(SD_LOG_IOCTL_MHD, un,
25403                                     "sd_resv_reclaim_thread: "
25404                                     "Reservation Recovered\n");
25405                         } else {
25406                                 mutex_enter(SD_MUTEX(un));
25407                                 un->un_resvd_status |= SD_LOST_RESERVE;
25408                                 mutex_exit(SD_MUTEX(un));
25409                                 SD_INFO(SD_LOG_IOCTL_MHD, un,
25410                                     "sd_resv_reclaim_thread: Failed "
25411                                     "Reservation Recovery\n");
25412                         }
25413                 } else {
25414                         mutex_exit(SD_MUTEX(un));
25415                 }
25416                 mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25417                 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req);
25418                 kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25419                 sd_mhreq = sd_tr.srq_thr_cur_req = NULL;
25420                 /*
25421                  * wakeup the destroy thread if anyone is waiting on
25422                  * us to complete.
25423                  */
25424                 cv_signal(&sd_tr.srq_inprocess_cv);
25425                 SD_TRACE(SD_LOG_IOCTL_MHD, un,
25426                     "sd_resv_reclaim_thread: cv_signalling current request \n");
25427         }
25428 
25429         /*
25430          * cleanup the sd_tr structure now that this thread will not exist
25431          */
25432         ASSERT(sd_tr.srq_thr_req_head == NULL);
25433         ASSERT(sd_tr.srq_thr_cur_req == NULL);
25434         sd_tr.srq_resv_reclaim_thread = NULL;
25435         mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25436         thread_exit();
25437 }
25438 
25439 
25440 /*
25441  *    Function: sd_rmv_resv_reclaim_req()
25442  *
25443  * Description: This function removes any pending reservation reclaim requests
25444  *              for the specified device.
25445  *
25446  *   Arguments: dev - the device 'dev_t'
25447  */
25448 
25449 static void
25450 sd_rmv_resv_reclaim_req(dev_t dev)
25451 {
25452         struct sd_thr_request *sd_mhreq;
25453         struct sd_thr_request *sd_prev;
25454 
25455         /* Remove a reservation reclaim request from the list */
25456         mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25457         if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) {
25458                 /*
25459                  * We are attempting to reinstate reservation for
25460                  * this device. We wait for sd_reserve_release()
25461                  * to return before we return.
25462                  */
25463                 cv_wait(&sd_tr.srq_inprocess_cv,
25464                     &sd_tr.srq_resv_reclaim_mutex);
25465         } else {
25466                 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head;
25467                 if (sd_mhreq && sd_mhreq->dev == dev) {
25468                         sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next;
25469                         kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25470                         mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25471                         return;
25472                 }
25473                 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) {
25474                         if (sd_mhreq && sd_mhreq->dev == dev) {
25475                                 break;
25476                         }
25477                         sd_prev = sd_mhreq;
25478                 }
25479                 if (sd_mhreq != NULL) {
25480                         sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next;
25481                         kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25482                 }
25483         }
25484         mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25485 }
25486 
25487 
25488 /*
25489  *    Function: sd_mhd_reset_notify_cb()
25490  *
25491  * Description: This is a call back function for scsi_reset_notify. This
25492  *              function updates the softstate reserved status and logs the
25493  *              reset. The driver scsi watch facility callback function
25494  *              (sd_mhd_watch_cb) and reservation reclaim thread functionality
25495  *              will reclaim the reservation.
25496  *
25497  *   Arguments: arg  - driver soft state (unit) structure
25498  */
25499 
25500 static void
25501 sd_mhd_reset_notify_cb(caddr_t arg)
25502 {
25503         struct sd_lun *un = (struct sd_lun *)arg;
25504 
25505         mutex_enter(SD_MUTEX(un));
25506         if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25507                 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE);
25508                 SD_INFO(SD_LOG_IOCTL_MHD, un,
25509                     "sd_mhd_reset_notify_cb: Lost Reservation\n");
25510         }
25511         mutex_exit(SD_MUTEX(un));
25512 }
25513 
25514 
25515 /*
25516  *    Function: sd_take_ownership()
25517  *
25518  * Description: This routine implements an algorithm to achieve a stable
25519  *              reservation on disks which don't implement priority reserve,
25520  *              and makes sure that other host lose re-reservation attempts.
25521  *              This algorithm contains of a loop that keeps issuing the RESERVE
25522  *              for some period of time (min_ownership_delay, default 6 seconds)
25523  *              During that loop, it looks to see if there has been a bus device
25524  *              reset or bus reset (both of which cause an existing reservation
25525  *              to be lost). If the reservation is lost issue RESERVE until a
25526  *              period of min_ownership_delay with no resets has gone by, or
25527  *              until max_ownership_delay has expired. This loop ensures that
25528  *              the host really did manage to reserve the device, in spite of
25529  *              resets. The looping for min_ownership_delay (default six
25530  *              seconds) is important to early generation clustering products,
25531  *              Solstice HA 1.x and Sun Cluster 2.x. Those products use an
25532  *              MHIOCENFAILFAST periodic timer of two seconds. By having
25533  *              MHIOCTKOWN issue Reserves in a loop for six seconds, and having
25534  *              MHIOCENFAILFAST poll every two seconds, the idea is that by the
25535  *              time the MHIOCTKOWN ioctl returns, the other host (if any) will
25536  *              have already noticed, via the MHIOCENFAILFAST polling, that it
25537  *              no longer "owns" the disk and will have panicked itself.  Thus,
25538  *              the host issuing the MHIOCTKOWN is assured (with timing
25539  *              dependencies) that by the time it actually starts to use the
25540  *              disk for real work, the old owner is no longer accessing it.
25541  *
25542  *              min_ownership_delay is the minimum amount of time for which the
25543  *              disk must be reserved continuously devoid of resets before the
25544  *              MHIOCTKOWN ioctl will return success.
25545  *
25546  *              max_ownership_delay indicates the amount of time by which the
25547  *              take ownership should succeed or timeout with an error.
25548  *
25549  *   Arguments: dev - the device 'dev_t'
25550  *              *p  - struct containing timing info.
25551  *
25552  * Return Code: 0 for success or error code
25553  */
25554 
25555 static int
25556 sd_take_ownership(dev_t dev, struct mhioctkown *p)
25557 {
25558         struct sd_lun   *un;
25559         int             rval;
25560         int             err;
25561         int             reservation_count   = 0;
25562         int             min_ownership_delay =  6000000; /* in usec */
25563         int             max_ownership_delay = 30000000; /* in usec */
25564         clock_t         start_time;     /* starting time of this algorithm */
25565         clock_t         end_time;       /* time limit for giving up */
25566         clock_t         ownership_time; /* time limit for stable ownership */
25567         clock_t         current_time;
25568         clock_t         previous_current_time;
25569 
25570         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25571                 return (ENXIO);
25572         }
25573 
25574         /*
25575          * Attempt a device reservation. A priority reservation is requested.
25576          */
25577         if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE))
25578             != SD_SUCCESS) {
25579                 SD_ERROR(SD_LOG_IOCTL_MHD, un,
25580                     "sd_take_ownership: return(1)=%d\n", rval);
25581                 return (rval);
25582         }
25583 
25584         /* Update the softstate reserved status to indicate the reservation */
25585         mutex_enter(SD_MUTEX(un));
25586         un->un_resvd_status |= SD_RESERVE;
25587         un->un_resvd_status &=
25588             ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT);
25589         mutex_exit(SD_MUTEX(un));
25590 
25591         if (p != NULL) {
25592                 if (p->min_ownership_delay != 0) {
25593                         min_ownership_delay = p->min_ownership_delay * 1000;
25594                 }
25595                 if (p->max_ownership_delay != 0) {
25596                         max_ownership_delay = p->max_ownership_delay * 1000;
25597                 }
25598         }
25599         SD_INFO(SD_LOG_IOCTL_MHD, un,
25600             "sd_take_ownership: min, max delays: %d, %d\n",
25601             min_ownership_delay, max_ownership_delay);
25602 
25603         start_time = ddi_get_lbolt();
25604         current_time    = start_time;
25605         ownership_time  = current_time + drv_usectohz(min_ownership_delay);
25606         end_time        = start_time + drv_usectohz(max_ownership_delay);
25607 
25608         while (current_time - end_time < 0) {
25609                 delay(drv_usectohz(500000));
25610 
25611                 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) {
25612                         if ((sd_reserve_release(dev, SD_RESERVE)) != 0) {
25613                                 mutex_enter(SD_MUTEX(un));
25614                                 rval = (un->un_resvd_status &
25615                                     SD_RESERVATION_CONFLICT) ? EACCES : EIO;
25616                                 mutex_exit(SD_MUTEX(un));
25617                                 break;
25618                         }
25619                 }
25620                 previous_current_time = current_time;
25621                 current_time = ddi_get_lbolt();
25622                 mutex_enter(SD_MUTEX(un));
25623                 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) {
25624                         ownership_time = ddi_get_lbolt() +
25625                             drv_usectohz(min_ownership_delay);
25626                         reservation_count = 0;
25627                 } else {
25628                         reservation_count++;
25629                 }
25630                 un->un_resvd_status |= SD_RESERVE;
25631                 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE);
25632                 mutex_exit(SD_MUTEX(un));
25633 
25634                 SD_INFO(SD_LOG_IOCTL_MHD, un,
25635                     "sd_take_ownership: ticks for loop iteration=%ld, "
25636                     "reservation=%s\n", (current_time - previous_current_time),
25637                     reservation_count ? "ok" : "reclaimed");
25638 
25639                 if (current_time - ownership_time >= 0 &&
25640                     reservation_count >= 4) {
25641                         rval = 0; /* Achieved a stable ownership */
25642                         break;
25643                 }
25644                 if (current_time - end_time >= 0) {
25645                         rval = EACCES; /* No ownership in max possible time */
25646                         break;
25647                 }
25648         }
25649         SD_TRACE(SD_LOG_IOCTL_MHD, un,
25650             "sd_take_ownership: return(2)=%d\n", rval);
25651         return (rval);
25652 }
25653 
25654 
25655 /*
25656  *    Function: sd_reserve_release()
25657  *
25658  * Description: This function builds and sends scsi RESERVE, RELEASE, and
25659  *              PRIORITY RESERVE commands based on a user specified command type
25660  *
25661  *   Arguments: dev - the device 'dev_t'
25662  *              cmd - user specified command type; one of SD_PRIORITY_RESERVE,
25663  *                    SD_RESERVE, SD_RELEASE
25664  *
25665  * Return Code: 0 or Error Code
25666  */
25667 
25668 static int
25669 sd_reserve_release(dev_t dev, int cmd)
25670 {
25671         struct uscsi_cmd        *com = NULL;
25672         struct sd_lun           *un = NULL;
25673         char                    cdb[CDB_GROUP0];
25674         int                     rval;
25675 
25676         ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) ||
25677             (cmd == SD_PRIORITY_RESERVE));
25678 
25679         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25680                 return (ENXIO);
25681         }
25682 
25683         /* instantiate and initialize the command and cdb */
25684         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
25685         bzero(cdb, CDB_GROUP0);
25686         com->uscsi_flags   = USCSI_SILENT;
25687         com->uscsi_timeout = un->un_reserve_release_time;
25688         com->uscsi_cdblen  = CDB_GROUP0;
25689         com->uscsi_cdb          = cdb;
25690         if (cmd == SD_RELEASE) {
25691                 cdb[0] = SCMD_RELEASE;
25692         } else {
25693                 cdb[0] = SCMD_RESERVE;
25694         }
25695 
25696         /* Send the command. */
25697         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
25698             SD_PATH_STANDARD);
25699 
25700         /*
25701          * "break" a reservation that is held by another host, by issuing a
25702          * reset if priority reserve is desired, and we could not get the
25703          * device.
25704          */
25705         if ((cmd == SD_PRIORITY_RESERVE) &&
25706             (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25707                 /*
25708                  * First try to reset the LUN. If we cannot, then try a target
25709                  * reset, followed by a bus reset if the target reset fails.
25710                  */
25711                 int reset_retval = 0;
25712                 if (un->un_f_lun_reset_enabled == TRUE) {
25713                         reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
25714                 }
25715                 if (reset_retval == 0) {
25716                         /* The LUN reset either failed or was not issued */
25717                         reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
25718                 }
25719                 if ((reset_retval == 0) &&
25720                     (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) {
25721                         rval = EIO;
25722                         kmem_free(com, sizeof (*com));
25723                         return (rval);
25724                 }
25725 
25726                 bzero(com, sizeof (struct uscsi_cmd));
25727                 com->uscsi_flags   = USCSI_SILENT;
25728                 com->uscsi_cdb          = cdb;
25729                 com->uscsi_cdblen  = CDB_GROUP0;
25730                 com->uscsi_timeout = 5;
25731 
25732                 /*
25733                  * Reissue the last reserve command, this time without request
25734                  * sense.  Assume that it is just a regular reserve command.
25735                  */
25736                 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
25737                     SD_PATH_STANDARD);
25738         }
25739 
25740         /* Return an error if still getting a reservation conflict. */
25741         if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25742                 rval = EACCES;
25743         }
25744 
25745         kmem_free(com, sizeof (*com));
25746         return (rval);
25747 }
25748 
25749 
25750 #define SD_NDUMP_RETRIES        12
25751 /*
25752  *      System Crash Dump routine
25753  */
25754 
25755 static int
25756 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
25757 {
25758         int             instance;
25759         int             partition;
25760         int             i;
25761         int             err;
25762         struct sd_lun   *un;
25763         struct scsi_pkt *wr_pktp;
25764         struct buf      *wr_bp;
25765         struct buf      wr_buf;
25766         daddr_t         tgt_byte_offset; /* rmw - byte offset for target */
25767         daddr_t         tgt_blkno;      /* rmw - blkno for target */
25768         size_t          tgt_byte_count; /* rmw -  # of bytes to xfer */
25769         size_t          tgt_nblk; /* rmw -  # of tgt blks to xfer */
25770         size_t          io_start_offset;
25771         int             doing_rmw = FALSE;
25772         int             rval;
25773         ssize_t         dma_resid;
25774         daddr_t         oblkno;
25775         diskaddr_t      nblks = 0;
25776         diskaddr_t      start_block;
25777 
25778         instance = SDUNIT(dev);
25779         if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
25780             (un->un_state == SD_STATE_ATTACHING) ||
25781             (un->un_state == SD_STATE_ATTACH_FAILED) ||
25782             !SD_IS_VALID_LABEL(un) || ISCD(un)) {
25783                 return (ENXIO);
25784         }
25785 
25786         SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n");
25787 
25788         partition = SDPART(dev);
25789         SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition);
25790 
25791         if (!(NOT_DEVBSIZE(un))) {
25792                 int secmask = 0;
25793                 int blknomask = 0;
25794 
25795                 blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
25796                 secmask = un->un_tgt_blocksize - 1;
25797 
25798                 if (blkno & blknomask) {
25799                         SD_TRACE(SD_LOG_DUMP, un,
25800                             "sddump: dump start block not modulo %d\n",
25801                             un->un_tgt_blocksize);
25802                         return (EINVAL);
25803                 }
25804 
25805                 if ((nblk * DEV_BSIZE) & secmask) {
25806                         SD_TRACE(SD_LOG_DUMP, un,
25807                             "sddump: dump length not modulo %d\n",
25808                             un->un_tgt_blocksize);
25809                         return (EINVAL);
25810                 }
25811 
25812         }
25813 
25814         /* Validate blocks to dump at against partition size. */
25815 
25816         (void) cmlb_partinfo(un->un_cmlbhandle, partition,
25817             &nblks, &start_block, NULL, NULL, (void *)SD_PATH_DIRECT);
25818 
25819         if (NOT_DEVBSIZE(un)) {
25820                 if ((blkno + nblk) > nblks) {
25821                         SD_TRACE(SD_LOG_DUMP, un,
25822                             "sddump: dump range larger than partition: "
25823                             "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
25824                             blkno, nblk, nblks);
25825                         return (EINVAL);
25826                 }
25827         } else {
25828                 if (((blkno / (un->un_tgt_blocksize / DEV_BSIZE)) +
25829                     (nblk / (un->un_tgt_blocksize / DEV_BSIZE))) > nblks) {
25830                         SD_TRACE(SD_LOG_DUMP, un,
25831                             "sddump: dump range larger than partition: "
25832                             "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
25833                             blkno, nblk, nblks);
25834                         return (EINVAL);
25835                 }
25836         }
25837 
25838         mutex_enter(&un->un_pm_mutex);
25839         if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
25840                 struct scsi_pkt *start_pktp;
25841 
25842                 mutex_exit(&un->un_pm_mutex);
25843 
25844                 /*
25845                  * use pm framework to power on HBA 1st
25846                  */
25847                 (void) pm_raise_power(SD_DEVINFO(un), 0,
25848                     SD_PM_STATE_ACTIVE(un));
25849 
25850                 /*
25851                  * Dump no long uses sdpower to power on a device, it's
25852                  * in-line here so it can be done in polled mode.
25853                  */
25854 
25855                 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n");
25856 
25857                 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL,
25858                     CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL);
25859 
25860                 if (start_pktp == NULL) {
25861                         /* We were not given a SCSI packet, fail. */
25862                         return (EIO);
25863                 }
25864                 bzero(start_pktp->pkt_cdbp, CDB_GROUP0);
25865                 start_pktp->pkt_cdbp[0] = SCMD_START_STOP;
25866                 start_pktp->pkt_cdbp[4] = SD_TARGET_START;
25867                 start_pktp->pkt_flags = FLAG_NOINTR;
25868 
25869                 mutex_enter(SD_MUTEX(un));
25870                 SD_FILL_SCSI1_LUN(un, start_pktp);
25871                 mutex_exit(SD_MUTEX(un));
25872                 /*
25873                  * Scsi_poll returns 0 (success) if the command completes and
25874                  * the status block is STATUS_GOOD.
25875                  */
25876                 if (sd_scsi_poll(un, start_pktp) != 0) {
25877                         scsi_destroy_pkt(start_pktp);
25878                         return (EIO);
25879                 }
25880                 scsi_destroy_pkt(start_pktp);
25881                 (void) sd_pm_state_change(un, SD_PM_STATE_ACTIVE(un),
25882                     SD_PM_STATE_CHANGE);
25883         } else {
25884                 mutex_exit(&un->un_pm_mutex);
25885         }
25886 
25887         mutex_enter(SD_MUTEX(un));
25888         un->un_throttle = 0;
25889 
25890         /*
25891          * The first time through, reset the specific target device.
25892          * However, when cpr calls sddump we know that sd is in a
25893          * a good state so no bus reset is required.
25894          * Clear sense data via Request Sense cmd.
25895          * In sddump we don't care about allow_bus_device_reset anymore
25896          */
25897 
25898         if ((un->un_state != SD_STATE_SUSPENDED) &&
25899             (un->un_state != SD_STATE_DUMPING)) {
25900 
25901                 New_state(un, SD_STATE_DUMPING);
25902 
25903                 if (un->un_f_is_fibre == FALSE) {
25904                         mutex_exit(SD_MUTEX(un));
25905                         /*
25906                          * Attempt a bus reset for parallel scsi.
25907                          *
25908                          * Note: A bus reset is required because on some host
25909                          * systems (i.e. E420R) a bus device reset is
25910                          * insufficient to reset the state of the target.
25911                          *
25912                          * Note: Don't issue the reset for fibre-channel,
25913                          * because this tends to hang the bus (loop) for
25914                          * too long while everyone is logging out and in
25915                          * and the deadman timer for dumping will fire
25916                          * before the dump is complete.
25917                          */
25918                         if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) {
25919                                 mutex_enter(SD_MUTEX(un));
25920                                 Restore_state(un);
25921                                 mutex_exit(SD_MUTEX(un));
25922                                 return (EIO);
25923                         }
25924 
25925                         /* Delay to give the device some recovery time. */
25926                         drv_usecwait(10000);
25927 
25928                         if (sd_send_polled_RQS(un) == SD_FAILURE) {
25929                                 SD_INFO(SD_LOG_DUMP, un,
25930                                     "sddump: sd_send_polled_RQS failed\n");
25931                         }
25932                         mutex_enter(SD_MUTEX(un));
25933                 }
25934         }
25935 
25936         /*
25937          * Convert the partition-relative block number to a
25938          * disk physical block number.
25939          */
25940         if (NOT_DEVBSIZE(un)) {
25941                 blkno += start_block;
25942         } else {
25943                 blkno = blkno / (un->un_tgt_blocksize / DEV_BSIZE);
25944                 blkno += start_block;
25945         }
25946 
25947         SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno);
25948 
25949 
25950         /*
25951          * Check if the device has a non-512 block size.
25952          */
25953         wr_bp = NULL;
25954         if (NOT_DEVBSIZE(un)) {
25955                 tgt_byte_offset = blkno * un->un_sys_blocksize;
25956                 tgt_byte_count = nblk * un->un_sys_blocksize;
25957                 if ((tgt_byte_offset % un->un_tgt_blocksize) ||
25958                     (tgt_byte_count % un->un_tgt_blocksize)) {
25959                         doing_rmw = TRUE;
25960                         /*
25961                          * Calculate the block number and number of block
25962                          * in terms of the media block size.
25963                          */
25964                         tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
25965                         tgt_nblk =
25966                             ((tgt_byte_offset + tgt_byte_count +
25967                             (un->un_tgt_blocksize - 1)) /
25968                             un->un_tgt_blocksize) - tgt_blkno;
25969 
25970                         /*
25971                          * Invoke the routine which is going to do read part
25972                          * of read-modify-write.
25973                          * Note that this routine returns a pointer to
25974                          * a valid bp in wr_bp.
25975                          */
25976                         err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk,
25977                             &wr_bp);
25978                         if (err) {
25979                                 mutex_exit(SD_MUTEX(un));
25980                                 return (err);
25981                         }
25982                         /*
25983                          * Offset is being calculated as -
25984                          * (original block # * system block size) -
25985                          * (new block # * target block size)
25986                          */
25987                         io_start_offset =
25988                             ((uint64_t)(blkno * un->un_sys_blocksize)) -
25989                             ((uint64_t)(tgt_blkno * un->un_tgt_blocksize));
25990 
25991                         ASSERT(io_start_offset < un->un_tgt_blocksize);
25992                         /*
25993                          * Do the modify portion of read modify write.
25994                          */
25995                         bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset],
25996                             (size_t)nblk * un->un_sys_blocksize);
25997                 } else {
25998                         doing_rmw = FALSE;
25999                         tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
26000                         tgt_nblk = tgt_byte_count / un->un_tgt_blocksize;
26001                 }
26002 
26003                 /* Convert blkno and nblk to target blocks */
26004                 blkno = tgt_blkno;
26005                 nblk = tgt_nblk;
26006         } else {
26007                 wr_bp = &wr_buf;
26008                 bzero(wr_bp, sizeof (struct buf));
26009                 wr_bp->b_flags               = B_BUSY;
26010                 wr_bp->b_un.b_addr   = addr;
26011                 wr_bp->b_bcount              = nblk << DEV_BSHIFT;
26012                 wr_bp->b_resid               = 0;
26013         }
26014 
26015         mutex_exit(SD_MUTEX(un));
26016 
26017         /*
26018          * Obtain a SCSI packet for the write command.
26019          * It should be safe to call the allocator here without
26020          * worrying about being locked for DVMA mapping because
26021          * the address we're passed is already a DVMA mapping
26022          *
26023          * We are also not going to worry about semaphore ownership
26024          * in the dump buffer. Dumping is single threaded at present.
26025          */
26026 
26027         wr_pktp = NULL;
26028 
26029         dma_resid = wr_bp->b_bcount;
26030         oblkno = blkno;
26031 
26032         if (!(NOT_DEVBSIZE(un))) {
26033                 nblk = nblk / (un->un_tgt_blocksize / DEV_BSIZE);
26034         }
26035 
26036         while (dma_resid != 0) {
26037 
26038         for (i = 0; i < SD_NDUMP_RETRIES; i++) {
26039                 wr_bp->b_flags &= ~B_ERROR;
26040 
26041                 if (un->un_partial_dma_supported == 1) {
26042                         blkno = oblkno +
26043                             ((wr_bp->b_bcount - dma_resid) /
26044                             un->un_tgt_blocksize);
26045                         nblk = dma_resid / un->un_tgt_blocksize;
26046 
26047                         if (wr_pktp) {
26048                                 /*
26049                                  * Partial DMA transfers after initial transfer
26050                                  */
26051                                 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp,
26052                                     blkno, nblk);
26053                         } else {
26054                                 /* Initial transfer */
26055                                 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26056                                     un->un_pkt_flags, NULL_FUNC, NULL,
26057                                     blkno, nblk);
26058                         }
26059                 } else {
26060                         rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26061                             0, NULL_FUNC, NULL, blkno, nblk);
26062                 }
26063 
26064                 if (rval == 0) {
26065                         /* We were given a SCSI packet, continue. */
26066                         break;
26067                 }
26068 
26069                 if (i == 0) {
26070                         if (wr_bp->b_flags & B_ERROR) {
26071                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26072                                     "no resources for dumping; "
26073                                     "error code: 0x%x, retrying",
26074                                     geterror(wr_bp));
26075                         } else {
26076                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26077                                     "no resources for dumping; retrying");
26078                         }
26079                 } else if (i != (SD_NDUMP_RETRIES - 1)) {
26080                         if (wr_bp->b_flags & B_ERROR) {
26081                                 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26082                                     "no resources for dumping; error code: "
26083                                     "0x%x, retrying\n", geterror(wr_bp));
26084                         }
26085                 } else {
26086                         if (wr_bp->b_flags & B_ERROR) {
26087                                 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26088                                     "no resources for dumping; "
26089                                     "error code: 0x%x, retries failed, "
26090                                     "giving up.\n", geterror(wr_bp));
26091                         } else {
26092                                 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26093                                     "no resources for dumping; "
26094                                     "retries failed, giving up.\n");
26095                         }
26096                         mutex_enter(SD_MUTEX(un));
26097                         Restore_state(un);
26098                         if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) {
26099                                 mutex_exit(SD_MUTEX(un));
26100                                 scsi_free_consistent_buf(wr_bp);
26101                         } else {
26102                                 mutex_exit(SD_MUTEX(un));
26103                         }
26104                         return (EIO);
26105                 }
26106                 drv_usecwait(10000);
26107         }
26108 
26109         if (un->un_partial_dma_supported == 1) {
26110                 /*
26111                  * save the resid from PARTIAL_DMA
26112                  */
26113                 dma_resid = wr_pktp->pkt_resid;
26114                 if (dma_resid != 0)
26115                         nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid);
26116                 wr_pktp->pkt_resid = 0;
26117         } else {
26118                 dma_resid = 0;
26119         }
26120 
26121         /* SunBug 1222170 */
26122         wr_pktp->pkt_flags = FLAG_NOINTR;
26123 
26124         err = EIO;
26125         for (i = 0; i < SD_NDUMP_RETRIES; i++) {
26126 
26127                 /*
26128                  * Scsi_poll returns 0 (success) if the command completes and
26129                  * the status block is STATUS_GOOD.  We should only check
26130                  * errors if this condition is not true.  Even then we should
26131                  * send our own request sense packet only if we have a check
26132                  * condition and auto request sense has not been performed by
26133                  * the hba.
26134                  */
26135                 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n");
26136 
26137                 if ((sd_scsi_poll(un, wr_pktp) == 0) &&
26138                     (wr_pktp->pkt_resid == 0)) {
26139                         err = SD_SUCCESS;
26140                         break;
26141                 }
26142 
26143                 /*
26144                  * Check CMD_DEV_GONE 1st, give up if device is gone.
26145                  */
26146                 if (wr_pktp->pkt_reason == CMD_DEV_GONE) {
26147                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26148                             "Error while dumping state...Device is gone\n");
26149                         break;
26150                 }
26151 
26152                 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) {
26153                         SD_INFO(SD_LOG_DUMP, un,
26154                             "sddump: write failed with CHECK, try # %d\n", i);
26155                         if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) {
26156                                 (void) sd_send_polled_RQS(un);
26157                         }
26158 
26159                         continue;
26160                 }
26161 
26162                 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) {
26163                         int reset_retval = 0;
26164 
26165                         SD_INFO(SD_LOG_DUMP, un,
26166                             "sddump: write failed with BUSY, try # %d\n", i);
26167 
26168                         if (un->un_f_lun_reset_enabled == TRUE) {
26169                                 reset_retval = scsi_reset(SD_ADDRESS(un),
26170                                     RESET_LUN);
26171                         }
26172                         if (reset_retval == 0) {
26173                                 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
26174                         }
26175                         (void) sd_send_polled_RQS(un);
26176 
26177                 } else {
26178                         SD_INFO(SD_LOG_DUMP, un,
26179                             "sddump: write failed with 0x%x, try # %d\n",
26180                             SD_GET_PKT_STATUS(wr_pktp), i);
26181                         mutex_enter(SD_MUTEX(un));
26182                         sd_reset_target(un, wr_pktp);
26183                         mutex_exit(SD_MUTEX(un));
26184                 }
26185 
26186                 /*
26187                  * If we are not getting anywhere with lun/target resets,
26188                  * let's reset the bus.
26189                  */
26190                 if (i == SD_NDUMP_RETRIES/2) {
26191                         (void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
26192                         (void) sd_send_polled_RQS(un);
26193                 }
26194         }
26195         }
26196 
26197         scsi_destroy_pkt(wr_pktp);
26198         mutex_enter(SD_MUTEX(un));
26199         if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) {
26200                 mutex_exit(SD_MUTEX(un));
26201                 scsi_free_consistent_buf(wr_bp);
26202         } else {
26203                 mutex_exit(SD_MUTEX(un));
26204         }
26205         SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err);
26206         return (err);
26207 }
26208 
26209 /*
26210  *    Function: sd_scsi_poll()
26211  *
26212  * Description: This is a wrapper for the scsi_poll call.
26213  *
26214  *   Arguments: sd_lun - The unit structure
26215  *              scsi_pkt - The scsi packet being sent to the device.
26216  *
26217  * Return Code: 0 - Command completed successfully with good status
26218  *             -1 - Command failed.  This could indicate a check condition
26219  *                  or other status value requiring recovery action.
26220  *
26221  * NOTE: This code is only called off sddump().
26222  */
26223 
26224 static int
26225 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp)
26226 {
26227         int status;
26228 
26229         ASSERT(un != NULL);
26230         ASSERT(!mutex_owned(SD_MUTEX(un)));
26231         ASSERT(pktp != NULL);
26232 
26233         status = SD_SUCCESS;
26234 
26235         if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) {
26236                 pktp->pkt_flags |= un->un_tagflags;
26237                 pktp->pkt_flags &= ~FLAG_NODISCON;
26238         }
26239 
26240         status = sd_ddi_scsi_poll(pktp);
26241         /*
26242          * Scsi_poll returns 0 (success) if the command completes and the
26243          * status block is STATUS_GOOD.  We should only check errors if this
26244          * condition is not true.  Even then we should send our own request
26245          * sense packet only if we have a check condition and auto
26246          * request sense has not been performed by the hba.
26247          * Don't get RQS data if pkt_reason is CMD_DEV_GONE.
26248          */
26249         if ((status != SD_SUCCESS) &&
26250             (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) &&
26251             (pktp->pkt_state & STATE_ARQ_DONE) == 0 &&
26252             (pktp->pkt_reason != CMD_DEV_GONE))
26253                 (void) sd_send_polled_RQS(un);
26254 
26255         return (status);
26256 }
26257 
26258 /*
26259  *    Function: sd_send_polled_RQS()
26260  *
26261  * Description: This sends the request sense command to a device.
26262  *
26263  *   Arguments: sd_lun - The unit structure
26264  *
26265  * Return Code: 0 - Command completed successfully with good status
26266  *             -1 - Command failed.
26267  *
26268  */
26269 
26270 static int
26271 sd_send_polled_RQS(struct sd_lun *un)
26272 {
26273         int     ret_val;
26274         struct  scsi_pkt        *rqs_pktp;
26275         struct  buf             *rqs_bp;
26276 
26277         ASSERT(un != NULL);
26278         ASSERT(!mutex_owned(SD_MUTEX(un)));
26279 
26280         ret_val = SD_SUCCESS;
26281 
26282         rqs_pktp = un->un_rqs_pktp;
26283         rqs_bp   = un->un_rqs_bp;
26284 
26285         mutex_enter(SD_MUTEX(un));
26286 
26287         if (un->un_sense_isbusy) {
26288                 ret_val = SD_FAILURE;
26289                 mutex_exit(SD_MUTEX(un));
26290                 return (ret_val);
26291         }
26292 
26293         /*
26294          * If the request sense buffer (and packet) is not in use,
26295          * let's set the un_sense_isbusy and send our packet
26296          */
26297         un->un_sense_isbusy  = 1;
26298         rqs_pktp->pkt_resid          = 0;
26299         rqs_pktp->pkt_reason         = 0;
26300         rqs_pktp->pkt_flags |= FLAG_NOINTR;
26301         bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH);
26302 
26303         mutex_exit(SD_MUTEX(un));
26304 
26305         SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at"
26306             " 0x%p\n", rqs_bp->b_un.b_addr);
26307 
26308         /*
26309          * Can't send this to sd_scsi_poll, we wrap ourselves around the
26310          * axle - it has a call into us!
26311          */
26312         if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) {
26313                 SD_INFO(SD_LOG_COMMON, un,
26314                     "sd_send_polled_RQS: RQS failed\n");
26315         }
26316 
26317         SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:",
26318             (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX);
26319 
26320         mutex_enter(SD_MUTEX(un));
26321         un->un_sense_isbusy = 0;
26322         mutex_exit(SD_MUTEX(un));
26323 
26324         return (ret_val);
26325 }
26326 
26327 /*
26328  * Defines needed for localized version of the scsi_poll routine.
26329  */
26330 #define CSEC            10000                   /* usecs */
26331 #define SEC_TO_CSEC     (1000000/CSEC)
26332 
26333 /*
26334  *    Function: sd_ddi_scsi_poll()
26335  *
26336  * Description: Localized version of the scsi_poll routine.  The purpose is to
26337  *              send a scsi_pkt to a device as a polled command.  This version
26338  *              is to ensure more robust handling of transport errors.
26339  *              Specifically this routine cures not ready, coming ready
26340  *              transition for power up and reset of sonoma's.  This can take
26341  *              up to 45 seconds for power-on and 20 seconds for reset of a
26342  *              sonoma lun.
26343  *
26344  *   Arguments: scsi_pkt - The scsi_pkt being sent to a device
26345  *
26346  * Return Code: 0 - Command completed successfully with good status
26347  *             -1 - Command failed.
26348  *
26349  * NOTE: This code is almost identical to scsi_poll, however before 6668774 can
26350  * be fixed (removing this code), we need to determine how to handle the
26351  * KEY_UNIT_ATTENTION condition below in conditions not as limited as sddump().
26352  *
26353  * NOTE: This code is only called off sddump().
26354  */
26355 static int
26356 sd_ddi_scsi_poll(struct scsi_pkt *pkt)
26357 {
26358         int                     rval = -1;
26359         int                     savef;
26360         long                    savet;
26361         void                    (*savec)();
26362         int                     timeout;
26363         int                     busy_count;
26364         int                     poll_delay;
26365         int                     rc;
26366         uint8_t                 *sensep;
26367         struct scsi_arq_status  *arqstat;
26368         extern int              do_polled_io;
26369 
26370         ASSERT(pkt->pkt_scbp);
26371 
26372         /*
26373          * save old flags..
26374          */
26375         savef = pkt->pkt_flags;
26376         savec = pkt->pkt_comp;
26377         savet = pkt->pkt_time;
26378 
26379         pkt->pkt_flags |= FLAG_NOINTR;
26380 
26381         /*
26382          * XXX there is nothing in the SCSA spec that states that we should not
26383          * do a callback for polled cmds; however, removing this will break sd
26384          * and probably other target drivers
26385          */
26386         pkt->pkt_comp = NULL;
26387 
26388         /*
26389          * we don't like a polled command without timeout.
26390          * 60 seconds seems long enough.
26391          */
26392         if (pkt->pkt_time == 0)
26393                 pkt->pkt_time = SCSI_POLL_TIMEOUT;
26394 
26395         /*
26396          * Send polled cmd.
26397          *
26398          * We do some error recovery for various errors.  Tran_busy,
26399          * queue full, and non-dispatched commands are retried every 10 msec.
26400          * as they are typically transient failures.  Busy status and Not
26401          * Ready are retried every second as this status takes a while to
26402          * change.
26403          */
26404         timeout = pkt->pkt_time * SEC_TO_CSEC;
26405 
26406         for (busy_count = 0; busy_count < timeout; busy_count++) {
26407                 /*
26408                  * Initialize pkt status variables.
26409                  */
26410                 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0;
26411 
26412                 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) {
26413                         if (rc != TRAN_BUSY) {
26414                                 /* Transport failed - give up. */
26415                                 break;
26416                         } else {
26417                                 /* Transport busy - try again. */
26418                                 poll_delay = 1 * CSEC;          /* 10 msec. */
26419                         }
26420                 } else {
26421                         /*
26422                          * Transport accepted - check pkt status.
26423                          */
26424                         rc = (*pkt->pkt_scbp) & STATUS_MASK;
26425                         if ((pkt->pkt_reason == CMD_CMPLT) &&
26426                             (rc == STATUS_CHECK) &&
26427                             (pkt->pkt_state & STATE_ARQ_DONE)) {
26428                                 arqstat =
26429                                     (struct scsi_arq_status *)(pkt->pkt_scbp);
26430                                 sensep = (uint8_t *)&arqstat->sts_sensedata;
26431                         } else {
26432                                 sensep = NULL;
26433                         }
26434 
26435                         if ((pkt->pkt_reason == CMD_CMPLT) &&
26436                             (rc == STATUS_GOOD)) {
26437                                 /* No error - we're done */
26438                                 rval = 0;
26439                                 break;
26440 
26441                         } else if (pkt->pkt_reason == CMD_DEV_GONE) {
26442                                 /* Lost connection - give up */
26443                                 break;
26444 
26445                         } else if ((pkt->pkt_reason == CMD_INCOMPLETE) &&
26446                             (pkt->pkt_state == 0)) {
26447                                 /* Pkt not dispatched - try again. */
26448                                 poll_delay = 1 * CSEC;          /* 10 msec. */
26449 
26450                         } else if ((pkt->pkt_reason == CMD_CMPLT) &&
26451                             (rc == STATUS_QFULL)) {
26452                                 /* Queue full - try again. */
26453                                 poll_delay = 1 * CSEC;          /* 10 msec. */
26454 
26455                         } else if ((pkt->pkt_reason == CMD_CMPLT) &&
26456                             (rc == STATUS_BUSY)) {
26457                                 /* Busy - try again. */
26458                                 poll_delay = 100 * CSEC;        /* 1 sec. */
26459                                 busy_count += (SEC_TO_CSEC - 1);
26460 
26461                         } else if ((sensep != NULL) &&
26462                             (scsi_sense_key(sensep) == KEY_UNIT_ATTENTION)) {
26463                                 /*
26464                                  * Unit Attention - try again.
26465                                  * Pretend it took 1 sec.
26466                                  * NOTE: 'continue' avoids poll_delay
26467                                  */
26468                                 busy_count += (SEC_TO_CSEC - 1);
26469                                 continue;
26470 
26471                         } else if ((sensep != NULL) &&
26472                             (scsi_sense_key(sensep) == KEY_NOT_READY) &&
26473                             (scsi_sense_asc(sensep) == 0x04) &&
26474                             (scsi_sense_ascq(sensep) == 0x01)) {
26475                                 /*
26476                                  * Not ready -> ready - try again.
26477                                  * 04h/01h: LUN IS IN PROCESS OF BECOMING READY
26478                                  * ...same as STATUS_BUSY
26479                                  */
26480                                 poll_delay = 100 * CSEC;        /* 1 sec. */
26481                                 busy_count += (SEC_TO_CSEC - 1);
26482 
26483                         } else {
26484                                 /* BAD status - give up. */
26485                                 break;
26486                         }
26487                 }
26488 
26489                 if (((curthread->t_flag & T_INTR_THREAD) == 0) &&
26490                     !do_polled_io) {
26491                         delay(drv_usectohz(poll_delay));
26492                 } else {
26493                         /* we busy wait during cpr_dump or interrupt threads */
26494                         drv_usecwait(poll_delay);
26495                 }
26496         }
26497 
26498         pkt->pkt_flags = savef;
26499         pkt->pkt_comp = savec;
26500         pkt->pkt_time = savet;
26501 
26502         /* return on error */
26503         if (rval)
26504                 return (rval);
26505 
26506         /*
26507          * This is not a performance critical code path.
26508          *
26509          * As an accommodation for scsi_poll callers, to avoid ddi_dma_sync()
26510          * issues associated with looking at DMA memory prior to
26511          * scsi_pkt_destroy(), we scsi_sync_pkt() prior to return.
26512          */
26513         scsi_sync_pkt(pkt);
26514         return (0);
26515 }
26516 
26517 
26518 
26519 /*
26520  *    Function: sd_persistent_reservation_in_read_keys
26521  *
26522  * Description: This routine is the driver entry point for handling CD-ROM
26523  *              multi-host persistent reservation requests (MHIOCGRP_INKEYS)
26524  *              by sending the SCSI-3 PRIN commands to the device.
26525  *              Processes the read keys command response by copying the
26526  *              reservation key information into the user provided buffer.
26527  *              Support for the 32/64 bit _MULTI_DATAMODEL is implemented.
26528  *
26529  *   Arguments: un   -  Pointer to soft state struct for the target.
26530  *              usrp -  user provided pointer to multihost Persistent In Read
26531  *                      Keys structure (mhioc_inkeys_t)
26532  *              flag -  this argument is a pass through to ddi_copyxxx()
26533  *                      directly from the mode argument of ioctl().
26534  *
26535  * Return Code: 0   - Success
26536  *              EACCES
26537  *              ENOTSUP
26538  *              errno return code from sd_send_scsi_cmd()
26539  *
26540  *     Context: Can sleep. Does not return until command is completed.
26541  */
26542 
26543 static int
26544 sd_persistent_reservation_in_read_keys(struct sd_lun *un,
26545     mhioc_inkeys_t *usrp, int flag)
26546 {
26547 #ifdef _MULTI_DATAMODEL
26548         struct mhioc_key_list32 li32;
26549 #endif
26550         sd_prin_readkeys_t      *in;
26551         mhioc_inkeys_t          *ptr;
26552         mhioc_key_list_t        li;
26553         uchar_t                 *data_bufp = NULL;
26554         int                     data_len = 0;
26555         int                     rval = 0;
26556         size_t                  copysz = 0;
26557         sd_ssc_t                *ssc;
26558 
26559         if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) {
26560                 return (EINVAL);
26561         }
26562         bzero(&li, sizeof (mhioc_key_list_t));
26563 
26564         ssc = sd_ssc_init(un);
26565 
26566         /*
26567          * Get the listsize from user
26568          */
26569 #ifdef _MULTI_DATAMODEL
26570         switch (ddi_model_convert_from(flag & FMODELS)) {
26571         case DDI_MODEL_ILP32:
26572                 copysz = sizeof (struct mhioc_key_list32);
26573                 if (ddi_copyin(ptr->li, &li32, copysz, flag)) {
26574                         SD_ERROR(SD_LOG_IOCTL_MHD, un,
26575                             "sd_persistent_reservation_in_read_keys: "
26576                             "failed ddi_copyin: mhioc_key_list32_t\n");
26577                         rval = EFAULT;
26578                         goto done;
26579                 }
26580                 li.listsize = li32.listsize;
26581                 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list;
26582                 break;
26583 
26584         case DDI_MODEL_NONE:
26585                 copysz = sizeof (mhioc_key_list_t);
26586                 if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26587                         SD_ERROR(SD_LOG_IOCTL_MHD, un,
26588                             "sd_persistent_reservation_in_read_keys: "
26589                             "failed ddi_copyin: mhioc_key_list_t\n");
26590                         rval = EFAULT;
26591                         goto done;
26592                 }
26593                 break;
26594         }
26595 
26596 #else /* ! _MULTI_DATAMODEL */
26597         copysz = sizeof (mhioc_key_list_t);
26598         if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26599                 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26600                     "sd_persistent_reservation_in_read_keys: "
26601                     "failed ddi_copyin: mhioc_key_list_t\n");
26602                 rval = EFAULT;
26603                 goto done;
26604         }
26605 #endif
26606 
26607         data_len  = li.listsize * MHIOC_RESV_KEY_SIZE;
26608         data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t));
26609         data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26610 
26611         rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS,
26612             data_len, data_bufp);
26613         if (rval != 0) {
26614                 if (rval == EIO)
26615                         sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
26616                 else
26617                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
26618                 goto done;
26619         }
26620         in = (sd_prin_readkeys_t *)data_bufp;
26621         ptr->generation = BE_32(in->generation);
26622         li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE;
26623 
26624         /*
26625          * Return the min(listsize, listlen) keys
26626          */
26627 #ifdef _MULTI_DATAMODEL
26628 
26629         switch (ddi_model_convert_from(flag & FMODELS)) {
26630         case DDI_MODEL_ILP32:
26631                 li32.listlen = li.listlen;
26632                 if (ddi_copyout(&li32, ptr->li, copysz, flag)) {
26633                         SD_ERROR(SD_LOG_IOCTL_MHD, un,
26634                             "sd_persistent_reservation_in_read_keys: "
26635                             "failed ddi_copyout: mhioc_key_list32_t\n");
26636                         rval = EFAULT;
26637                         goto done;
26638                 }
26639                 break;
26640 
26641         case DDI_MODEL_NONE:
26642                 if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26643                         SD_ERROR(SD_LOG_IOCTL_MHD, un,
26644                             "sd_persistent_reservation_in_read_keys: "
26645                             "failed ddi_copyout: mhioc_key_list_t\n");
26646                         rval = EFAULT;
26647                         goto done;
26648                 }
26649                 break;
26650         }
26651 
26652 #else /* ! _MULTI_DATAMODEL */
26653 
26654         if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26655                 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26656                     "sd_persistent_reservation_in_read_keys: "
26657                     "failed ddi_copyout: mhioc_key_list_t\n");
26658                 rval = EFAULT;
26659                 goto done;
26660         }
26661 
26662 #endif /* _MULTI_DATAMODEL */
26663 
26664         copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE,
26665             li.listsize * MHIOC_RESV_KEY_SIZE);
26666         if (ddi_copyout(&in->keylist, li.list, copysz, flag)) {
26667                 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26668                     "sd_persistent_reservation_in_read_keys: "
26669                     "failed ddi_copyout: keylist\n");
26670                 rval = EFAULT;
26671         }
26672 done:
26673         sd_ssc_fini(ssc);
26674         kmem_free(data_bufp, data_len);
26675         return (rval);
26676 }
26677 
26678 
26679 /*
26680  *    Function: sd_persistent_reservation_in_read_resv
26681  *
26682  * Description: This routine is the driver entry point for handling CD-ROM
26683  *              multi-host persistent reservation requests (MHIOCGRP_INRESV)
26684  *              by sending the SCSI-3 PRIN commands to the device.
26685  *              Process the read persistent reservations command response by
26686  *              copying the reservation information into the user provided
26687  *              buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented.
26688  *
26689  *   Arguments: un   -  Pointer to soft state struct for the target.
26690  *              usrp -  user provided pointer to multihost Persistent In Read
26691  *                      Keys structure (mhioc_inkeys_t)
26692  *              flag -  this argument is a pass through to ddi_copyxxx()
26693  *                      directly from the mode argument of ioctl().
26694  *
26695  * Return Code: 0   - Success
26696  *              EACCES
26697  *              ENOTSUP
26698  *              errno return code from sd_send_scsi_cmd()
26699  *
26700  *     Context: Can sleep. Does not return until command is completed.
26701  */
26702 
26703 static int
26704 sd_persistent_reservation_in_read_resv(struct sd_lun *un,
26705     mhioc_inresvs_t *usrp, int flag)
26706 {
26707 #ifdef _MULTI_DATAMODEL
26708         struct mhioc_resv_desc_list32 resvlist32;
26709 #endif
26710         sd_prin_readresv_t      *in;
26711         mhioc_inresvs_t         *ptr;
26712         sd_readresv_desc_t      *readresv_ptr;
26713         mhioc_resv_desc_list_t  resvlist;
26714         mhioc_resv_desc_t       resvdesc;
26715         uchar_t                 *data_bufp = NULL;
26716         int                     data_len;
26717         int                     rval = 0;
26718         int                     i;
26719         size_t                  copysz = 0;
26720         mhioc_resv_desc_t       *bufp;
26721         sd_ssc_t                *ssc;
26722 
26723         if ((ptr = usrp) == NULL) {
26724                 return (EINVAL);
26725         }
26726 
26727         ssc = sd_ssc_init(un);
26728 
26729         /*
26730          * Get the listsize from user
26731          */
26732 #ifdef _MULTI_DATAMODEL
26733         switch (ddi_model_convert_from(flag & FMODELS)) {
26734         case DDI_MODEL_ILP32:
26735                 copysz = sizeof (struct mhioc_resv_desc_list32);
26736                 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) {
26737                         SD_ERROR(SD_LOG_IOCTL_MHD, un,
26738                             "sd_persistent_reservation_in_read_resv: "
26739                             "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26740                         rval = EFAULT;
26741                         goto done;
26742                 }
26743                 resvlist.listsize = resvlist32.listsize;
26744                 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list;
26745                 break;
26746 
26747         case DDI_MODEL_NONE:
26748                 copysz = sizeof (mhioc_resv_desc_list_t);
26749                 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26750                         SD_ERROR(SD_LOG_IOCTL_MHD, un,
26751                             "sd_persistent_reservation_in_read_resv: "
26752                             "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26753                         rval = EFAULT;
26754                         goto done;
26755                 }
26756                 break;
26757         }
26758 #else /* ! _MULTI_DATAMODEL */
26759         copysz = sizeof (mhioc_resv_desc_list_t);
26760         if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26761                 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26762                     "sd_persistent_reservation_in_read_resv: "
26763                     "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26764                 rval = EFAULT;
26765                 goto done;
26766         }
26767 #endif /* ! _MULTI_DATAMODEL */
26768 
26769         data_len  = resvlist.listsize * SCSI3_RESV_DESC_LEN;
26770         data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t));
26771         data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26772 
26773         rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_RESV,
26774             data_len, data_bufp);
26775         if (rval != 0) {
26776                 if (rval == EIO)
26777                         sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
26778                 else
26779                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
26780                 goto done;
26781         }
26782         in = (sd_prin_readresv_t *)data_bufp;
26783         ptr->generation = BE_32(in->generation);
26784         resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN;
26785 
26786         /*
26787          * Return the min(listsize, listlen( keys
26788          */
26789 #ifdef _MULTI_DATAMODEL
26790 
26791         switch (ddi_model_convert_from(flag & FMODELS)) {
26792         case DDI_MODEL_ILP32:
26793                 resvlist32.listlen = resvlist.listlen;
26794                 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) {
26795                         SD_ERROR(SD_LOG_IOCTL_MHD, un,
26796                             "sd_persistent_reservation_in_read_resv: "
26797                             "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26798                         rval = EFAULT;
26799                         goto done;
26800                 }
26801                 break;
26802 
26803         case DDI_MODEL_NONE:
26804                 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26805                         SD_ERROR(SD_LOG_IOCTL_MHD, un,
26806                             "sd_persistent_reservation_in_read_resv: "
26807                             "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26808                         rval = EFAULT;
26809                         goto done;
26810                 }
26811                 break;
26812         }
26813 
26814 #else /* ! _MULTI_DATAMODEL */
26815 
26816         if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26817                 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26818                     "sd_persistent_reservation_in_read_resv: "
26819                     "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26820                 rval = EFAULT;
26821                 goto done;
26822         }
26823 
26824 #endif /* ! _MULTI_DATAMODEL */
26825 
26826         readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc;
26827         bufp = resvlist.list;
26828         copysz = sizeof (mhioc_resv_desc_t);
26829         for (i = 0; i < min(resvlist.listlen, resvlist.listsize);
26830             i++, readresv_ptr++, bufp++) {
26831 
26832                 bcopy(&readresv_ptr->resvkey, &resvdesc.key,
26833                     MHIOC_RESV_KEY_SIZE);
26834                 resvdesc.type  = readresv_ptr->type;
26835                 resvdesc.scope = readresv_ptr->scope;
26836                 resvdesc.scope_specific_addr =
26837                     BE_32(readresv_ptr->scope_specific_addr);
26838 
26839                 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) {
26840                         SD_ERROR(SD_LOG_IOCTL_MHD, un,
26841                             "sd_persistent_reservation_in_read_resv: "
26842                             "failed ddi_copyout: resvlist\n");
26843                         rval = EFAULT;
26844                         goto done;
26845                 }
26846         }
26847 done:
26848         sd_ssc_fini(ssc);
26849         /* only if data_bufp is allocated, we need to free it */
26850         if (data_bufp) {
26851                 kmem_free(data_bufp, data_len);
26852         }
26853         return (rval);
26854 }
26855 
26856 
26857 /*
26858  *    Function: sr_change_blkmode()
26859  *
26860  * Description: This routine is the driver entry point for handling CD-ROM
26861  *              block mode ioctl requests. Support for returning and changing
26862  *              the current block size in use by the device is implemented. The
26863  *              LBA size is changed via a MODE SELECT Block Descriptor.
26864  *
26865  *              This routine issues a mode sense with an allocation length of
26866  *              12 bytes for the mode page header and a single block descriptor.
26867  *
26868  *   Arguments: dev - the device 'dev_t'
26869  *              cmd - the request type; one of CDROMGBLKMODE (get) or
26870  *                    CDROMSBLKMODE (set)
26871  *              data - current block size or requested block size
26872  *              flag - this argument is a pass through to ddi_copyxxx() directly
26873  *                     from the mode argument of ioctl().
26874  *
26875  * Return Code: the code returned by sd_send_scsi_cmd()
26876  *              EINVAL if invalid arguments are provided
26877  *              EFAULT if ddi_copyxxx() fails
26878  *              ENXIO if fail ddi_get_soft_state
26879  *              EIO if invalid mode sense block descriptor length
26880  *
26881  */
26882 
26883 static int
26884 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag)
26885 {
26886         struct sd_lun                   *un = NULL;
26887         struct mode_header              *sense_mhp, *select_mhp;
26888         struct block_descriptor         *sense_desc, *select_desc;
26889         int                             current_bsize;
26890         int                             rval = EINVAL;
26891         uchar_t                         *sense = NULL;
26892         uchar_t                         *select = NULL;
26893         sd_ssc_t                        *ssc;
26894 
26895         ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE));
26896 
26897         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26898                 return (ENXIO);
26899         }
26900 
26901         /*
26902          * The block length is changed via the Mode Select block descriptor, the
26903          * "Read/Write Error Recovery" mode page (0x1) contents are not actually
26904          * required as part of this routine. Therefore the mode sense allocation
26905          * length is specified to be the length of a mode page header and a
26906          * block descriptor.
26907          */
26908         sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
26909 
26910         ssc = sd_ssc_init(un);
26911         rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
26912             BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD);
26913         sd_ssc_fini(ssc);
26914         if (rval != 0) {
26915                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26916                     "sr_change_blkmode: Mode Sense Failed\n");
26917                 kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26918                 return (rval);
26919         }
26920 
26921         /* Check the block descriptor len to handle only 1 block descriptor */
26922         sense_mhp = (struct mode_header *)sense;
26923         if ((sense_mhp->bdesc_length == 0) ||
26924             (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) {
26925                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26926                     "sr_change_blkmode: Mode Sense returned invalid block"
26927                     " descriptor length\n");
26928                 kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26929                 return (EIO);
26930         }
26931         sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH);
26932         current_bsize = ((sense_desc->blksize_hi << 16) |
26933             (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo);
26934 
26935         /* Process command */
26936         switch (cmd) {
26937         case CDROMGBLKMODE:
26938                 /* Return the block size obtained during the mode sense */
26939                 if (ddi_copyout(&current_bsize, (void *)data,
26940                     sizeof (int), flag) != 0)
26941                         rval = EFAULT;
26942                 break;
26943         case CDROMSBLKMODE:
26944                 /* Validate the requested block size */
26945                 switch (data) {
26946                 case CDROM_BLK_512:
26947                 case CDROM_BLK_1024:
26948                 case CDROM_BLK_2048:
26949                 case CDROM_BLK_2056:
26950                 case CDROM_BLK_2336:
26951                 case CDROM_BLK_2340:
26952                 case CDROM_BLK_2352:
26953                 case CDROM_BLK_2368:
26954                 case CDROM_BLK_2448:
26955                 case CDROM_BLK_2646:
26956                 case CDROM_BLK_2647:
26957                         break;
26958                 default:
26959                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26960                             "sr_change_blkmode: "
26961                             "Block Size '%ld' Not Supported\n", data);
26962                         kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26963                         return (EINVAL);
26964                 }
26965 
26966                 /*
26967                  * The current block size matches the requested block size so
26968                  * there is no need to send the mode select to change the size
26969                  */
26970                 if (current_bsize == data) {
26971                         break;
26972                 }
26973 
26974                 /* Build the select data for the requested block size */
26975                 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
26976                 select_mhp = (struct mode_header *)select;
26977                 select_desc =
26978                     (struct block_descriptor *)(select + MODE_HEADER_LENGTH);
26979                 /*
26980                  * The LBA size is changed via the block descriptor, so the
26981                  * descriptor is built according to the user data
26982                  */
26983                 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH;
26984                 select_desc->blksize_hi  = (char)(((data) & 0x00ff0000) >> 16);
26985                 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8);
26986                 select_desc->blksize_lo  = (char)((data) & 0x000000ff);
26987 
26988                 /* Send the mode select for the requested block size */
26989                 ssc = sd_ssc_init(un);
26990                 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0,
26991                     select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
26992                     SD_PATH_STANDARD);
26993                 sd_ssc_fini(ssc);
26994                 if (rval != 0) {
26995                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26996                             "sr_change_blkmode: Mode Select Failed\n");
26997                         /*
26998                          * The mode select failed for the requested block size,
26999                          * so reset the data for the original block size and
27000                          * send it to the target. The error is indicated by the
27001                          * return value for the failed mode select.
27002                          */
27003                         select_desc->blksize_hi  = sense_desc->blksize_hi;
27004                         select_desc->blksize_mid = sense_desc->blksize_mid;
27005                         select_desc->blksize_lo  = sense_desc->blksize_lo;
27006                         ssc = sd_ssc_init(un);
27007                         (void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0,
27008                             select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
27009                             SD_PATH_STANDARD);
27010                         sd_ssc_fini(ssc);
27011                 } else {
27012                         ASSERT(!mutex_owned(SD_MUTEX(un)));
27013                         mutex_enter(SD_MUTEX(un));
27014                         sd_update_block_info(un, (uint32_t)data, 0);
27015                         mutex_exit(SD_MUTEX(un));
27016                 }
27017                 break;
27018         default:
27019                 /* should not reach here, but check anyway */
27020                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27021                     "sr_change_blkmode: Command '%x' Not Supported\n", cmd);
27022                 rval = EINVAL;
27023                 break;
27024         }
27025 
27026         if (select) {
27027                 kmem_free(select, BUFLEN_CHG_BLK_MODE);
27028         }
27029         if (sense) {
27030                 kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27031         }
27032         return (rval);
27033 }
27034 
27035 
27036 /*
27037  * Note: The following sr_change_speed() and sr_atapi_change_speed() routines
27038  * implement driver support for getting and setting the CD speed. The command
27039  * set used will be based on the device type. If the device has not been
27040  * identified as MMC the Toshiba vendor specific mode page will be used. If
27041  * the device is MMC but does not support the Real Time Streaming feature
27042  * the SET CD SPEED command will be used to set speed and mode page 0x2A will
27043  * be used to read the speed.
27044  */
27045 
27046 /*
27047  *    Function: sr_change_speed()
27048  *
27049  * Description: This routine is the driver entry point for handling CD-ROM
27050  *              drive speed ioctl requests for devices supporting the Toshiba
27051  *              vendor specific drive speed mode page. Support for returning
27052  *              and changing the current drive speed in use by the device is
27053  *              implemented.
27054  *
27055  *   Arguments: dev - the device 'dev_t'
27056  *              cmd - the request type; one of CDROMGDRVSPEED (get) or
27057  *                    CDROMSDRVSPEED (set)
27058  *              data - current drive speed or requested drive speed
27059  *              flag - this argument is a pass through to ddi_copyxxx() directly
27060  *                     from the mode argument of ioctl().
27061  *
27062  * Return Code: the code returned by sd_send_scsi_cmd()
27063  *              EINVAL if invalid arguments are provided
27064  *              EFAULT if ddi_copyxxx() fails
27065  *              ENXIO if fail ddi_get_soft_state
27066  *              EIO if invalid mode sense block descriptor length
27067  */
27068 
27069 static int
27070 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27071 {
27072         struct sd_lun                   *un = NULL;
27073         struct mode_header              *sense_mhp, *select_mhp;
27074         struct mode_speed               *sense_page, *select_page;
27075         int                             current_speed;
27076         int                             rval = EINVAL;
27077         int                             bd_len;
27078         uchar_t                         *sense = NULL;
27079         uchar_t                         *select = NULL;
27080         sd_ssc_t                        *ssc;
27081 
27082         ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27083         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27084                 return (ENXIO);
27085         }
27086 
27087         /*
27088          * Note: The drive speed is being modified here according to a Toshiba
27089          * vendor specific mode page (0x31).
27090          */
27091         sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27092 
27093         ssc = sd_ssc_init(un);
27094         rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
27095             BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED,
27096             SD_PATH_STANDARD);
27097         sd_ssc_fini(ssc);
27098         if (rval != 0) {
27099                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27100                     "sr_change_speed: Mode Sense Failed\n");
27101                 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27102                 return (rval);
27103         }
27104         sense_mhp  = (struct mode_header *)sense;
27105 
27106         /* Check the block descriptor len to handle only 1 block descriptor */
27107         bd_len = sense_mhp->bdesc_length;
27108         if (bd_len > MODE_BLK_DESC_LENGTH) {
27109                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27110                     "sr_change_speed: Mode Sense returned invalid block "
27111                     "descriptor length\n");
27112                 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27113                 return (EIO);
27114         }
27115 
27116         sense_page = (struct mode_speed *)
27117             (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
27118         current_speed = sense_page->speed;
27119 
27120         /* Process command */
27121         switch (cmd) {
27122         case CDROMGDRVSPEED:
27123                 /* Return the drive speed obtained during the mode sense */
27124                 if (current_speed == 0x2) {
27125                         current_speed = CDROM_TWELVE_SPEED;
27126                 }
27127                 if (ddi_copyout(&current_speed, (void *)data,
27128                     sizeof (int), flag) != 0) {
27129                         rval = EFAULT;
27130                 }
27131                 break;
27132         case CDROMSDRVSPEED:
27133                 /* Validate the requested drive speed */
27134                 switch ((uchar_t)data) {
27135                 case CDROM_TWELVE_SPEED:
27136                         data = 0x2;
27137                         /*FALLTHROUGH*/
27138                 case CDROM_NORMAL_SPEED:
27139                 case CDROM_DOUBLE_SPEED:
27140                 case CDROM_QUAD_SPEED:
27141                 case CDROM_MAXIMUM_SPEED:
27142                         break;
27143                 default:
27144                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27145                             "sr_change_speed: "
27146                             "Drive Speed '%d' Not Supported\n", (uchar_t)data);
27147                         kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27148                         return (EINVAL);
27149                 }
27150 
27151                 /*
27152                  * The current drive speed matches the requested drive speed so
27153                  * there is no need to send the mode select to change the speed
27154                  */
27155                 if (current_speed == data) {
27156                         break;
27157                 }
27158 
27159                 /* Build the select data for the requested drive speed */
27160                 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27161                 select_mhp = (struct mode_header *)select;
27162                 select_mhp->bdesc_length = 0;
27163                 select_page =
27164                     (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27165                 select_page =
27166                     (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27167                 select_page->mode_page.code = CDROM_MODE_SPEED;
27168                 select_page->mode_page.length = 2;
27169                 select_page->speed = (uchar_t)data;
27170 
27171                 /* Send the mode select for the requested block size */
27172                 ssc = sd_ssc_init(un);
27173                 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
27174                     MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27175                     SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27176                 sd_ssc_fini(ssc);
27177                 if (rval != 0) {
27178                         /*
27179                          * The mode select failed for the requested drive speed,
27180                          * so reset the data for the original drive speed and
27181                          * send it to the target. The error is indicated by the
27182                          * return value for the failed mode select.
27183                          */
27184                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27185                             "sr_drive_speed: Mode Select Failed\n");
27186                         select_page->speed = sense_page->speed;
27187                         ssc = sd_ssc_init(un);
27188                         (void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
27189                             MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27190                             SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27191                         sd_ssc_fini(ssc);
27192                 }
27193                 break;
27194         default:
27195                 /* should not reach here, but check anyway */
27196                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27197                     "sr_change_speed: Command '%x' Not Supported\n", cmd);
27198                 rval = EINVAL;
27199                 break;
27200         }
27201 
27202         if (select) {
27203                 kmem_free(select, BUFLEN_MODE_CDROM_SPEED);
27204         }
27205         if (sense) {
27206                 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27207         }
27208 
27209         return (rval);
27210 }
27211 
27212 
27213 /*
27214  *    Function: sr_atapi_change_speed()
27215  *
27216  * Description: This routine is the driver entry point for handling CD-ROM
27217  *              drive speed ioctl requests for MMC devices that do not support
27218  *              the Real Time Streaming feature (0x107).
27219  *
27220  *              Note: This routine will use the SET SPEED command which may not
27221  *              be supported by all devices.
27222  *
27223  *   Arguments: dev- the device 'dev_t'
27224  *              cmd- the request type; one of CDROMGDRVSPEED (get) or
27225  *                   CDROMSDRVSPEED (set)
27226  *              data- current drive speed or requested drive speed
27227  *              flag- this argument is a pass through to ddi_copyxxx() directly
27228  *                    from the mode argument of ioctl().
27229  *
27230  * Return Code: the code returned by sd_send_scsi_cmd()
27231  *              EINVAL if invalid arguments are provided
27232  *              EFAULT if ddi_copyxxx() fails
27233  *              ENXIO if fail ddi_get_soft_state
27234  *              EIO if invalid mode sense block descriptor length
27235  */
27236 
27237 static int
27238 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27239 {
27240         struct sd_lun                   *un;
27241         struct uscsi_cmd                *com = NULL;
27242         struct mode_header_grp2         *sense_mhp;
27243         uchar_t                         *sense_page;
27244         uchar_t                         *sense = NULL;
27245         char                            cdb[CDB_GROUP5];
27246         int                             bd_len;
27247         int                             current_speed = 0;
27248         int                             max_speed = 0;
27249         int                             rval;
27250         sd_ssc_t                        *ssc;
27251 
27252         ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27253 
27254         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27255                 return (ENXIO);
27256         }
27257 
27258         sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
27259 
27260         ssc = sd_ssc_init(un);
27261         rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
27262             BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP,
27263             SD_PATH_STANDARD);
27264         sd_ssc_fini(ssc);
27265         if (rval != 0) {
27266                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27267                     "sr_atapi_change_speed: Mode Sense Failed\n");
27268                 kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27269                 return (rval);
27270         }
27271 
27272         /* Check the block descriptor len to handle only 1 block descriptor */
27273         sense_mhp = (struct mode_header_grp2 *)sense;
27274         bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
27275         if (bd_len > MODE_BLK_DESC_LENGTH) {
27276                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27277                     "sr_atapi_change_speed: Mode Sense returned invalid "
27278                     "block descriptor length\n");
27279                 kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27280                 return (EIO);
27281         }
27282 
27283         /* Calculate the current and maximum drive speeds */
27284         sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
27285         current_speed = (sense_page[14] << 8) | sense_page[15];
27286         max_speed = (sense_page[8] << 8) | sense_page[9];
27287 
27288         /* Process the command */
27289         switch (cmd) {
27290         case CDROMGDRVSPEED:
27291                 current_speed /= SD_SPEED_1X;
27292                 if (ddi_copyout(&current_speed, (void *)data,
27293                     sizeof (int), flag) != 0)
27294                         rval = EFAULT;
27295                 break;
27296         case CDROMSDRVSPEED:
27297                 /* Convert the speed code to KB/sec */
27298                 switch ((uchar_t)data) {
27299                 case CDROM_NORMAL_SPEED:
27300                         current_speed = SD_SPEED_1X;
27301                         break;
27302                 case CDROM_DOUBLE_SPEED:
27303                         current_speed = 2 * SD_SPEED_1X;
27304                         break;
27305                 case CDROM_QUAD_SPEED:
27306                         current_speed = 4 * SD_SPEED_1X;
27307                         break;
27308                 case CDROM_TWELVE_SPEED:
27309                         current_speed = 12 * SD_SPEED_1X;
27310                         break;
27311                 case CDROM_MAXIMUM_SPEED:
27312                         current_speed = 0xffff;
27313                         break;
27314                 default:
27315                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27316                             "sr_atapi_change_speed: invalid drive speed %d\n",
27317                             (uchar_t)data);
27318                         kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27319                         return (EINVAL);
27320                 }
27321 
27322                 /* Check the request against the drive's max speed. */
27323                 if (current_speed != 0xffff) {
27324                         if (current_speed > max_speed) {
27325                                 kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27326                                 return (EINVAL);
27327                         }
27328                 }
27329 
27330                 /*
27331                  * Build and send the SET SPEED command
27332                  *
27333                  * Note: The SET SPEED (0xBB) command used in this routine is
27334                  * obsolete per the SCSI MMC spec but still supported in the
27335                  * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27336                  * therefore the command is still implemented in this routine.
27337                  */
27338                 bzero(cdb, sizeof (cdb));
27339                 cdb[0] = (char)SCMD_SET_CDROM_SPEED;
27340                 cdb[2] = (uchar_t)(current_speed >> 8);
27341                 cdb[3] = (uchar_t)current_speed;
27342                 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27343                 com->uscsi_cdb          = (caddr_t)cdb;
27344                 com->uscsi_cdblen  = CDB_GROUP5;
27345                 com->uscsi_bufaddr = NULL;
27346                 com->uscsi_buflen  = 0;
27347                 com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT;
27348                 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD);
27349                 break;
27350         default:
27351                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27352                     "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd);
27353                 rval = EINVAL;
27354         }
27355 
27356         if (sense) {
27357                 kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27358         }
27359         if (com) {
27360                 kmem_free(com, sizeof (*com));
27361         }
27362         return (rval);
27363 }
27364 
27365 
27366 /*
27367  *    Function: sr_pause_resume()
27368  *
27369  * Description: This routine is the driver entry point for handling CD-ROM
27370  *              pause/resume ioctl requests. This only affects the audio play
27371  *              operation.
27372  *
27373  *   Arguments: dev - the device 'dev_t'
27374  *              cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used
27375  *                    for setting the resume bit of the cdb.
27376  *
27377  * Return Code: the code returned by sd_send_scsi_cmd()
27378  *              EINVAL if invalid mode specified
27379  *
27380  */
27381 
27382 static int
27383 sr_pause_resume(dev_t dev, int cmd)
27384 {
27385         struct sd_lun           *un;
27386         struct uscsi_cmd        *com;
27387         char                    cdb[CDB_GROUP1];
27388         int                     rval;
27389 
27390         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27391                 return (ENXIO);
27392         }
27393 
27394         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27395         bzero(cdb, CDB_GROUP1);
27396         cdb[0] = SCMD_PAUSE_RESUME;
27397         switch (cmd) {
27398         case CDROMRESUME:
27399                 cdb[8] = 1;
27400                 break;
27401         case CDROMPAUSE:
27402                 cdb[8] = 0;
27403                 break;
27404         default:
27405                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:"
27406                     " Command '%x' Not Supported\n", cmd);
27407                 rval = EINVAL;
27408                 goto done;
27409         }
27410 
27411         com->uscsi_cdb    = cdb;
27412         com->uscsi_cdblen = CDB_GROUP1;
27413         com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27414 
27415         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27416             SD_PATH_STANDARD);
27417 
27418 done:
27419         kmem_free(com, sizeof (*com));
27420         return (rval);
27421 }
27422 
27423 
27424 /*
27425  *    Function: sr_play_msf()
27426  *
27427  * Description: This routine is the driver entry point for handling CD-ROM
27428  *              ioctl requests to output the audio signals at the specified
27429  *              starting address and continue the audio play until the specified
27430  *              ending address (CDROMPLAYMSF) The address is in Minute Second
27431  *              Frame (MSF) format.
27432  *
27433  *   Arguments: dev     - the device 'dev_t'
27434  *              data    - pointer to user provided audio msf structure,
27435  *                        specifying start/end addresses.
27436  *              flag    - this argument is a pass through to ddi_copyxxx()
27437  *                        directly from the mode argument of ioctl().
27438  *
27439  * Return Code: the code returned by sd_send_scsi_cmd()
27440  *              EFAULT if ddi_copyxxx() fails
27441  *              ENXIO if fail ddi_get_soft_state
27442  *              EINVAL if data pointer is NULL
27443  */
27444 
27445 static int
27446 sr_play_msf(dev_t dev, caddr_t data, int flag)
27447 {
27448         struct sd_lun           *un;
27449         struct uscsi_cmd        *com;
27450         struct cdrom_msf        msf_struct;
27451         struct cdrom_msf        *msf = &msf_struct;
27452         char                    cdb[CDB_GROUP1];
27453         int                     rval;
27454 
27455         if (data == NULL) {
27456                 return (EINVAL);
27457         }
27458 
27459         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27460                 return (ENXIO);
27461         }
27462 
27463         if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) {
27464                 return (EFAULT);
27465         }
27466 
27467         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27468         bzero(cdb, CDB_GROUP1);
27469         cdb[0] = SCMD_PLAYAUDIO_MSF;
27470         if (un->un_f_cfg_playmsf_bcd == TRUE) {
27471                 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0);
27472                 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0);
27473                 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0);
27474                 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1);
27475                 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1);
27476                 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1);
27477         } else {
27478                 cdb[3] = msf->cdmsf_min0;
27479                 cdb[4] = msf->cdmsf_sec0;
27480                 cdb[5] = msf->cdmsf_frame0;
27481                 cdb[6] = msf->cdmsf_min1;
27482                 cdb[7] = msf->cdmsf_sec1;
27483                 cdb[8] = msf->cdmsf_frame1;
27484         }
27485         com->uscsi_cdb    = cdb;
27486         com->uscsi_cdblen = CDB_GROUP1;
27487         com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27488         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27489             SD_PATH_STANDARD);
27490         kmem_free(com, sizeof (*com));
27491         return (rval);
27492 }
27493 
27494 
27495 /*
27496  *    Function: sr_play_trkind()
27497  *
27498  * Description: This routine is the driver entry point for handling CD-ROM
27499  *              ioctl requests to output the audio signals at the specified
27500  *              starting address and continue the audio play until the specified
27501  *              ending address (CDROMPLAYTRKIND). The address is in Track Index
27502  *              format.
27503  *
27504  *   Arguments: dev     - the device 'dev_t'
27505  *              data    - pointer to user provided audio track/index structure,
27506  *                        specifying start/end addresses.
27507  *              flag    - this argument is a pass through to ddi_copyxxx()
27508  *                        directly from the mode argument of ioctl().
27509  *
27510  * Return Code: the code returned by sd_send_scsi_cmd()
27511  *              EFAULT if ddi_copyxxx() fails
27512  *              ENXIO if fail ddi_get_soft_state
27513  *              EINVAL if data pointer is NULL
27514  */
27515 
27516 static int
27517 sr_play_trkind(dev_t dev, caddr_t data, int flag)
27518 {
27519         struct cdrom_ti         ti_struct;
27520         struct cdrom_ti         *ti = &ti_struct;
27521         struct uscsi_cmd        *com = NULL;
27522         char                    cdb[CDB_GROUP1];
27523         int                     rval;
27524 
27525         if (data == NULL) {
27526                 return (EINVAL);
27527         }
27528 
27529         if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) {
27530                 return (EFAULT);
27531         }
27532 
27533         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27534         bzero(cdb, CDB_GROUP1);
27535         cdb[0] = SCMD_PLAYAUDIO_TI;
27536         cdb[4] = ti->cdti_trk0;
27537         cdb[5] = ti->cdti_ind0;
27538         cdb[7] = ti->cdti_trk1;
27539         cdb[8] = ti->cdti_ind1;
27540         com->uscsi_cdb    = cdb;
27541         com->uscsi_cdblen = CDB_GROUP1;
27542         com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27543         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27544             SD_PATH_STANDARD);
27545         kmem_free(com, sizeof (*com));
27546         return (rval);
27547 }
27548 
27549 
27550 /*
27551  *    Function: sr_read_all_subcodes()
27552  *
27553  * Description: This routine is the driver entry point for handling CD-ROM
27554  *              ioctl requests to return raw subcode data while the target is
27555  *              playing audio (CDROMSUBCODE).
27556  *
27557  *   Arguments: dev     - the device 'dev_t'
27558  *              data    - pointer to user provided cdrom subcode structure,
27559  *                        specifying the transfer length and address.
27560  *              flag    - this argument is a pass through to ddi_copyxxx()
27561  *                        directly from the mode argument of ioctl().
27562  *
27563  * Return Code: the code returned by sd_send_scsi_cmd()
27564  *              EFAULT if ddi_copyxxx() fails
27565  *              ENXIO if fail ddi_get_soft_state
27566  *              EINVAL if data pointer is NULL
27567  */
27568 
27569 static int
27570 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag)
27571 {
27572         struct sd_lun           *un = NULL;
27573         struct uscsi_cmd        *com = NULL;
27574         struct cdrom_subcode    *subcode = NULL;
27575         int                     rval;
27576         size_t                  buflen;
27577         char                    cdb[CDB_GROUP5];
27578 
27579 #ifdef _MULTI_DATAMODEL
27580         /* To support ILP32 applications in an LP64 world */
27581         struct cdrom_subcode32          cdrom_subcode32;
27582         struct cdrom_subcode32          *cdsc32 = &cdrom_subcode32;
27583 #endif
27584         if (data == NULL) {
27585                 return (EINVAL);
27586         }
27587 
27588         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27589                 return (ENXIO);
27590         }
27591 
27592         subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP);
27593 
27594 #ifdef _MULTI_DATAMODEL
27595         switch (ddi_model_convert_from(flag & FMODELS)) {
27596         case DDI_MODEL_ILP32:
27597                 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) {
27598                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27599                             "sr_read_all_subcodes: ddi_copyin Failed\n");
27600                         kmem_free(subcode, sizeof (struct cdrom_subcode));
27601                         return (EFAULT);
27602                 }
27603                 /* Convert the ILP32 uscsi data from the application to LP64 */
27604                 cdrom_subcode32tocdrom_subcode(cdsc32, subcode);
27605                 break;
27606         case DDI_MODEL_NONE:
27607                 if (ddi_copyin(data, subcode,
27608                     sizeof (struct cdrom_subcode), flag)) {
27609                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27610                             "sr_read_all_subcodes: ddi_copyin Failed\n");
27611                         kmem_free(subcode, sizeof (struct cdrom_subcode));
27612                         return (EFAULT);
27613                 }
27614                 break;
27615         }
27616 #else /* ! _MULTI_DATAMODEL */
27617         if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) {
27618                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27619                     "sr_read_all_subcodes: ddi_copyin Failed\n");
27620                 kmem_free(subcode, sizeof (struct cdrom_subcode));
27621                 return (EFAULT);
27622         }
27623 #endif /* _MULTI_DATAMODEL */
27624 
27625         /*
27626          * Since MMC-2 expects max 3 bytes for length, check if the
27627          * length input is greater than 3 bytes
27628          */
27629         if ((subcode->cdsc_length & 0xFF000000) != 0) {
27630                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27631                     "sr_read_all_subcodes: "
27632                     "cdrom transfer length too large: %d (limit %d)\n",
27633                     subcode->cdsc_length, 0xFFFFFF);
27634                 kmem_free(subcode, sizeof (struct cdrom_subcode));
27635                 return (EINVAL);
27636         }
27637 
27638         buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length;
27639         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27640         bzero(cdb, CDB_GROUP5);
27641 
27642         if (un->un_f_mmc_cap == TRUE) {
27643                 cdb[0] = (char)SCMD_READ_CD;
27644                 cdb[2] = (char)0xff;
27645                 cdb[3] = (char)0xff;
27646                 cdb[4] = (char)0xff;
27647                 cdb[5] = (char)0xff;
27648                 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27649                 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27650                 cdb[8] = ((subcode->cdsc_length) & 0x000000ff);
27651                 cdb[10] = 1;
27652         } else {
27653                 /*
27654                  * Note: A vendor specific command (0xDF) is being used here to
27655                  * request a read of all subcodes.
27656                  */
27657                 cdb[0] = (char)SCMD_READ_ALL_SUBCODES;
27658                 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24);
27659                 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27660                 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27661                 cdb[9] = ((subcode->cdsc_length) & 0x000000ff);
27662         }
27663         com->uscsi_cdb          = cdb;
27664         com->uscsi_cdblen  = CDB_GROUP5;
27665         com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr;
27666         com->uscsi_buflen  = buflen;
27667         com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27668         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
27669             SD_PATH_STANDARD);
27670         kmem_free(subcode, sizeof (struct cdrom_subcode));
27671         kmem_free(com, sizeof (*com));
27672         return (rval);
27673 }
27674 
27675 
27676 /*
27677  *    Function: sr_read_subchannel()
27678  *
27679  * Description: This routine is the driver entry point for handling CD-ROM
27680  *              ioctl requests to return the Q sub-channel data of the CD
27681  *              current position block. (CDROMSUBCHNL) The data includes the
27682  *              track number, index number, absolute CD-ROM address (LBA or MSF
27683  *              format per the user) , track relative CD-ROM address (LBA or MSF
27684  *              format per the user), control data and audio status.
27685  *
27686  *   Arguments: dev     - the device 'dev_t'
27687  *              data    - pointer to user provided cdrom sub-channel structure
27688  *              flag    - this argument is a pass through to ddi_copyxxx()
27689  *                        directly from the mode argument of ioctl().
27690  *
27691  * Return Code: the code returned by sd_send_scsi_cmd()
27692  *              EFAULT if ddi_copyxxx() fails
27693  *              ENXIO if fail ddi_get_soft_state
27694  *              EINVAL if data pointer is NULL
27695  */
27696 
27697 static int
27698 sr_read_subchannel(dev_t dev, caddr_t data, int flag)
27699 {
27700         struct sd_lun           *un;
27701         struct uscsi_cmd        *com;
27702         struct cdrom_subchnl    subchanel;
27703         struct cdrom_subchnl    *subchnl = &subchanel;
27704         char                    cdb[CDB_GROUP1];
27705         caddr_t                 buffer;
27706         int                     rval;
27707 
27708         if (data == NULL) {
27709                 return (EINVAL);
27710         }
27711 
27712         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27713             (un->un_state == SD_STATE_OFFLINE)) {
27714                 return (ENXIO);
27715         }
27716 
27717         if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) {
27718                 return (EFAULT);
27719         }
27720 
27721         buffer = kmem_zalloc((size_t)16, KM_SLEEP);
27722         bzero(cdb, CDB_GROUP1);
27723         cdb[0] = SCMD_READ_SUBCHANNEL;
27724         /* Set the MSF bit based on the user requested address format */
27725         cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02;
27726         /*
27727          * Set the Q bit in byte 2 to indicate that Q sub-channel data be
27728          * returned
27729          */
27730         cdb[2] = 0x40;
27731         /*
27732          * Set byte 3 to specify the return data format. A value of 0x01
27733          * indicates that the CD-ROM current position should be returned.
27734          */
27735         cdb[3] = 0x01;
27736         cdb[8] = 0x10;
27737         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27738         com->uscsi_cdb          = cdb;
27739         com->uscsi_cdblen  = CDB_GROUP1;
27740         com->uscsi_bufaddr = buffer;
27741         com->uscsi_buflen  = 16;
27742         com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27743         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27744             SD_PATH_STANDARD);
27745         if (rval != 0) {
27746                 kmem_free(buffer, 16);
27747                 kmem_free(com, sizeof (*com));
27748                 return (rval);
27749         }
27750 
27751         /* Process the returned Q sub-channel data */
27752         subchnl->cdsc_audiostatus = buffer[1];
27753         subchnl->cdsc_adr    = (buffer[5] & 0xF0) >> 4;
27754         subchnl->cdsc_ctrl   = (buffer[5] & 0x0F);
27755         subchnl->cdsc_trk    = buffer[6];
27756         subchnl->cdsc_ind    = buffer[7];
27757         if (subchnl->cdsc_format & CDROM_LBA) {
27758                 subchnl->cdsc_absaddr.lba =
27759                     ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27760                     ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27761                 subchnl->cdsc_reladdr.lba =
27762                     ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) +
27763                     ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]);
27764         } else if (un->un_f_cfg_readsub_bcd == TRUE) {
27765                 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]);
27766                 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]);
27767                 subchnl->cdsc_absaddr.msf.frame  = BCD_TO_BYTE(buffer[11]);
27768                 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]);
27769                 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]);
27770                 subchnl->cdsc_reladdr.msf.frame  = BCD_TO_BYTE(buffer[15]);
27771         } else {
27772                 subchnl->cdsc_absaddr.msf.minute = buffer[9];
27773                 subchnl->cdsc_absaddr.msf.second = buffer[10];
27774                 subchnl->cdsc_absaddr.msf.frame  = buffer[11];
27775                 subchnl->cdsc_reladdr.msf.minute = buffer[13];
27776                 subchnl->cdsc_reladdr.msf.second = buffer[14];
27777                 subchnl->cdsc_reladdr.msf.frame  = buffer[15];
27778         }
27779         kmem_free(buffer, 16);
27780         kmem_free(com, sizeof (*com));
27781         if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag)
27782             != 0) {
27783                 return (EFAULT);
27784         }
27785         return (rval);
27786 }
27787 
27788 
27789 /*
27790  *    Function: sr_read_tocentry()
27791  *
27792  * Description: This routine is the driver entry point for handling CD-ROM
27793  *              ioctl requests to read from the Table of Contents (TOC)
27794  *              (CDROMREADTOCENTRY). This routine provides the ADR and CTRL
27795  *              fields, the starting address (LBA or MSF format per the user)
27796  *              and the data mode if the user specified track is a data track.
27797  *
27798  *              Note: The READ HEADER (0x44) command used in this routine is
27799  *              obsolete per the SCSI MMC spec but still supported in the
27800  *              MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27801  *              therefore the command is still implemented in this routine.
27802  *
27803  *   Arguments: dev     - the device 'dev_t'
27804  *              data    - pointer to user provided toc entry structure,
27805  *                        specifying the track # and the address format
27806  *                        (LBA or MSF).
27807  *              flag    - this argument is a pass through to ddi_copyxxx()
27808  *                        directly from the mode argument of ioctl().
27809  *
27810  * Return Code: the code returned by sd_send_scsi_cmd()
27811  *              EFAULT if ddi_copyxxx() fails
27812  *              ENXIO if fail ddi_get_soft_state
27813  *              EINVAL if data pointer is NULL
27814  */
27815 
27816 static int
27817 sr_read_tocentry(dev_t dev, caddr_t data, int flag)
27818 {
27819         struct sd_lun           *un = NULL;
27820         struct uscsi_cmd        *com;
27821         struct cdrom_tocentry   toc_entry;
27822         struct cdrom_tocentry   *entry = &toc_entry;
27823         caddr_t                 buffer;
27824         int                     rval;
27825         char                    cdb[CDB_GROUP1];
27826 
27827         if (data == NULL) {
27828                 return (EINVAL);
27829         }
27830 
27831         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27832             (un->un_state == SD_STATE_OFFLINE)) {
27833                 return (ENXIO);
27834         }
27835 
27836         if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) {
27837                 return (EFAULT);
27838         }
27839 
27840         /* Validate the requested track and address format */
27841         if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) {
27842                 return (EINVAL);
27843         }
27844 
27845         if (entry->cdte_track == 0) {
27846                 return (EINVAL);
27847         }
27848 
27849         buffer = kmem_zalloc((size_t)12, KM_SLEEP);
27850         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27851         bzero(cdb, CDB_GROUP1);
27852 
27853         cdb[0] = SCMD_READ_TOC;
27854         /* Set the MSF bit based on the user requested address format  */
27855         cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2);
27856         if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
27857                 cdb[6] = BYTE_TO_BCD(entry->cdte_track);
27858         } else {
27859                 cdb[6] = entry->cdte_track;
27860         }
27861 
27862         /*
27863          * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
27864          * (4 byte TOC response header + 8 byte track descriptor)
27865          */
27866         cdb[8] = 12;
27867         com->uscsi_cdb          = cdb;
27868         com->uscsi_cdblen  = CDB_GROUP1;
27869         com->uscsi_bufaddr = buffer;
27870         com->uscsi_buflen  = 0x0C;
27871         com->uscsi_flags   = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ);
27872         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27873             SD_PATH_STANDARD);
27874         if (rval != 0) {
27875                 kmem_free(buffer, 12);
27876                 kmem_free(com, sizeof (*com));
27877                 return (rval);
27878         }
27879 
27880         /* Process the toc entry */
27881         entry->cdte_adr              = (buffer[5] & 0xF0) >> 4;
27882         entry->cdte_ctrl     = (buffer[5] & 0x0F);
27883         if (entry->cdte_format & CDROM_LBA) {
27884                 entry->cdte_addr.lba =
27885                     ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27886                     ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27887         } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) {
27888                 entry->cdte_addr.msf.minute  = BCD_TO_BYTE(buffer[9]);
27889                 entry->cdte_addr.msf.second  = BCD_TO_BYTE(buffer[10]);
27890                 entry->cdte_addr.msf.frame   = BCD_TO_BYTE(buffer[11]);
27891                 /*
27892                  * Send a READ TOC command using the LBA address format to get
27893                  * the LBA for the track requested so it can be used in the
27894                  * READ HEADER request
27895                  *
27896                  * Note: The MSF bit of the READ HEADER command specifies the
27897                  * output format. The block address specified in that command
27898                  * must be in LBA format.
27899                  */
27900                 cdb[1] = 0;
27901                 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27902                     SD_PATH_STANDARD);
27903                 if (rval != 0) {
27904                         kmem_free(buffer, 12);
27905                         kmem_free(com, sizeof (*com));
27906                         return (rval);
27907                 }
27908         } else {
27909                 entry->cdte_addr.msf.minute  = buffer[9];
27910                 entry->cdte_addr.msf.second  = buffer[10];
27911                 entry->cdte_addr.msf.frame   = buffer[11];
27912                 /*
27913                  * Send a READ TOC command using the LBA address format to get
27914                  * the LBA for the track requested so it can be used in the
27915                  * READ HEADER request
27916                  *
27917                  * Note: The MSF bit of the READ HEADER command specifies the
27918                  * output format. The block address specified in that command
27919                  * must be in LBA format.
27920                  */
27921                 cdb[1] = 0;
27922                 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27923                     SD_PATH_STANDARD);
27924                 if (rval != 0) {
27925                         kmem_free(buffer, 12);
27926                         kmem_free(com, sizeof (*com));
27927                         return (rval);
27928                 }
27929         }
27930 
27931         /*
27932          * Build and send the READ HEADER command to determine the data mode of
27933          * the user specified track.
27934          */
27935         if ((entry->cdte_ctrl & CDROM_DATA_TRACK) &&
27936             (entry->cdte_track != CDROM_LEADOUT)) {
27937                 bzero(cdb, CDB_GROUP1);
27938                 cdb[0] = SCMD_READ_HEADER;
27939                 cdb[2] = buffer[8];
27940                 cdb[3] = buffer[9];
27941                 cdb[4] = buffer[10];
27942                 cdb[5] = buffer[11];
27943                 cdb[8] = 0x08;
27944                 com->uscsi_buflen = 0x08;
27945                 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27946                     SD_PATH_STANDARD);
27947                 if (rval == 0) {
27948                         entry->cdte_datamode = buffer[0];
27949                 } else {
27950                         /*
27951                          * READ HEADER command failed, since this is
27952                          * obsoleted in one spec, its better to return
27953                          * -1 for an invlid track so that we can still
27954                          * receive the rest of the TOC data.
27955                          */
27956                         entry->cdte_datamode = (uchar_t)-1;
27957                 }
27958         } else {
27959                 entry->cdte_datamode = (uchar_t)-1;
27960         }
27961 
27962         kmem_free(buffer, 12);
27963         kmem_free(com, sizeof (*com));
27964         if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0)
27965                 return (EFAULT);
27966 
27967         return (rval);
27968 }
27969 
27970 
27971 /*
27972  *    Function: sr_read_tochdr()
27973  *
27974  * Description: This routine is the driver entry point for handling CD-ROM
27975  *              ioctl requests to read the Table of Contents (TOC) header
27976  *              (CDROMREADTOHDR). The TOC header consists of the disk starting
27977  *              and ending track numbers
27978  *
27979  *   Arguments: dev     - the device 'dev_t'
27980  *              data    - pointer to user provided toc header structure,
27981  *                        specifying the starting and ending track numbers.
27982  *              flag    - this argument is a pass through to ddi_copyxxx()
27983  *                        directly from the mode argument of ioctl().
27984  *
27985  * Return Code: the code returned by sd_send_scsi_cmd()
27986  *              EFAULT if ddi_copyxxx() fails
27987  *              ENXIO if fail ddi_get_soft_state
27988  *              EINVAL if data pointer is NULL
27989  */
27990 
27991 static int
27992 sr_read_tochdr(dev_t dev, caddr_t data, int flag)
27993 {
27994         struct sd_lun           *un;
27995         struct uscsi_cmd        *com;
27996         struct cdrom_tochdr     toc_header;
27997         struct cdrom_tochdr     *hdr = &toc_header;
27998         char                    cdb[CDB_GROUP1];
27999         int                     rval;
28000         caddr_t                 buffer;
28001 
28002         if (data == NULL) {
28003                 return (EINVAL);
28004         }
28005 
28006         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28007             (un->un_state == SD_STATE_OFFLINE)) {
28008                 return (ENXIO);
28009         }
28010 
28011         buffer = kmem_zalloc(4, KM_SLEEP);
28012         bzero(cdb, CDB_GROUP1);
28013         cdb[0] = SCMD_READ_TOC;
28014         /*
28015          * Specifying a track number of 0x00 in the READ TOC command indicates
28016          * that the TOC header should be returned
28017          */
28018         cdb[6] = 0x00;
28019         /*
28020          * Bytes 7 & 8 are the 4 byte allocation length for TOC header.
28021          * (2 byte data len + 1 byte starting track # + 1 byte ending track #)
28022          */
28023         cdb[8] = 0x04;
28024         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28025         com->uscsi_cdb          = cdb;
28026         com->uscsi_cdblen  = CDB_GROUP1;
28027         com->uscsi_bufaddr = buffer;
28028         com->uscsi_buflen  = 0x04;
28029         com->uscsi_timeout = 3 * un->un_cmd_timeout;
28030         com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28031 
28032         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
28033             SD_PATH_STANDARD);
28034         if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
28035                 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]);
28036                 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]);
28037         } else {
28038                 hdr->cdth_trk0 = buffer[2];
28039                 hdr->cdth_trk1 = buffer[3];
28040         }
28041         kmem_free(buffer, 4);
28042         kmem_free(com, sizeof (*com));
28043         if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) {
28044                 return (EFAULT);
28045         }
28046         return (rval);
28047 }
28048 
28049 
28050 /*
28051  * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(),
28052  * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for
28053  * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data,
28054  * digital audio and extended architecture digital audio. These modes are
28055  * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3
28056  * MMC specs.
28057  *
28058  * In addition to support for the various data formats these routines also
28059  * include support for devices that implement only the direct access READ
28060  * commands (0x08, 0x28), devices that implement the READ_CD commands
28061  * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and
28062  * READ CDXA commands (0xD8, 0xDB)
28063  */
28064 
28065 /*
28066  *    Function: sr_read_mode1()
28067  *
28068  * Description: This routine is the driver entry point for handling CD-ROM
28069  *              ioctl read mode1 requests (CDROMREADMODE1).
28070  *
28071  *   Arguments: dev     - the device 'dev_t'
28072  *              data    - pointer to user provided cd read structure specifying
28073  *                        the lba buffer address and length.
28074  *              flag    - this argument is a pass through to ddi_copyxxx()
28075  *                        directly from the mode argument of ioctl().
28076  *
28077  * Return Code: the code returned by sd_send_scsi_cmd()
28078  *              EFAULT if ddi_copyxxx() fails
28079  *              ENXIO if fail ddi_get_soft_state
28080  *              EINVAL if data pointer is NULL
28081  */
28082 
28083 static int
28084 sr_read_mode1(dev_t dev, caddr_t data, int flag)
28085 {
28086         struct sd_lun           *un;
28087         struct cdrom_read       mode1_struct;
28088         struct cdrom_read       *mode1 = &mode1_struct;
28089         int                     rval;
28090         sd_ssc_t                *ssc;
28091 
28092 #ifdef _MULTI_DATAMODEL
28093         /* To support ILP32 applications in an LP64 world */
28094         struct cdrom_read32     cdrom_read32;
28095         struct cdrom_read32     *cdrd32 = &cdrom_read32;
28096 #endif /* _MULTI_DATAMODEL */
28097 
28098         if (data == NULL) {
28099                 return (EINVAL);
28100         }
28101 
28102         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28103             (un->un_state == SD_STATE_OFFLINE)) {
28104                 return (ENXIO);
28105         }
28106 
28107         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28108             "sd_read_mode1: entry: un:0x%p\n", un);
28109 
28110 #ifdef _MULTI_DATAMODEL
28111         switch (ddi_model_convert_from(flag & FMODELS)) {
28112         case DDI_MODEL_ILP32:
28113                 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28114                         return (EFAULT);
28115                 }
28116                 /* Convert the ILP32 uscsi data from the application to LP64 */
28117                 cdrom_read32tocdrom_read(cdrd32, mode1);
28118                 break;
28119         case DDI_MODEL_NONE:
28120                 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28121                         return (EFAULT);
28122                 }
28123         }
28124 #else /* ! _MULTI_DATAMODEL */
28125         if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28126                 return (EFAULT);
28127         }
28128 #endif /* _MULTI_DATAMODEL */
28129 
28130         ssc = sd_ssc_init(un);
28131         rval = sd_send_scsi_READ(ssc, mode1->cdread_bufaddr,
28132             mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD);
28133         sd_ssc_fini(ssc);
28134 
28135         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28136             "sd_read_mode1: exit: un:0x%p\n", un);
28137 
28138         return (rval);
28139 }
28140 
28141 
28142 /*
28143  *    Function: sr_read_cd_mode2()
28144  *
28145  * Description: This routine is the driver entry point for handling CD-ROM
28146  *              ioctl read mode2 requests (CDROMREADMODE2) for devices that
28147  *              support the READ CD (0xBE) command or the 1st generation
28148  *              READ CD (0xD4) command.
28149  *
28150  *   Arguments: dev     - the device 'dev_t'
28151  *              data    - pointer to user provided cd read structure specifying
28152  *                        the lba buffer address and length.
28153  *              flag    - this argument is a pass through to ddi_copyxxx()
28154  *                        directly from the mode argument of ioctl().
28155  *
28156  * Return Code: the code returned by sd_send_scsi_cmd()
28157  *              EFAULT if ddi_copyxxx() fails
28158  *              ENXIO if fail ddi_get_soft_state
28159  *              EINVAL if data pointer is NULL
28160  */
28161 
28162 static int
28163 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag)
28164 {
28165         struct sd_lun           *un;
28166         struct uscsi_cmd        *com;
28167         struct cdrom_read       mode2_struct;
28168         struct cdrom_read       *mode2 = &mode2_struct;
28169         uchar_t                 cdb[CDB_GROUP5];
28170         int                     nblocks;
28171         int                     rval;
28172 #ifdef _MULTI_DATAMODEL
28173         /*  To support ILP32 applications in an LP64 world */
28174         struct cdrom_read32     cdrom_read32;
28175         struct cdrom_read32     *cdrd32 = &cdrom_read32;
28176 #endif /* _MULTI_DATAMODEL */
28177 
28178         if (data == NULL) {
28179                 return (EINVAL);
28180         }
28181 
28182         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28183             (un->un_state == SD_STATE_OFFLINE)) {
28184                 return (ENXIO);
28185         }
28186 
28187 #ifdef _MULTI_DATAMODEL
28188         switch (ddi_model_convert_from(flag & FMODELS)) {
28189         case DDI_MODEL_ILP32:
28190                 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28191                         return (EFAULT);
28192                 }
28193                 /* Convert the ILP32 uscsi data from the application to LP64 */
28194                 cdrom_read32tocdrom_read(cdrd32, mode2);
28195                 break;
28196         case DDI_MODEL_NONE:
28197                 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28198                         return (EFAULT);
28199                 }
28200                 break;
28201         }
28202 
28203 #else /* ! _MULTI_DATAMODEL */
28204         if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28205                 return (EFAULT);
28206         }
28207 #endif /* _MULTI_DATAMODEL */
28208 
28209         bzero(cdb, sizeof (cdb));
28210         if (un->un_f_cfg_read_cd_xd4 == TRUE) {
28211                 /* Read command supported by 1st generation atapi drives */
28212                 cdb[0] = SCMD_READ_CDD4;
28213         } else {
28214                 /* Universal CD Access Command */
28215                 cdb[0] = SCMD_READ_CD;
28216         }
28217 
28218         /*
28219          * Set expected sector type to: 2336s byte, Mode 2 Yellow Book
28220          */
28221         cdb[1] = CDROM_SECTOR_TYPE_MODE2;
28222 
28223         /* set the start address */
28224         cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF);
28225         cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF);
28226         cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28227         cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF);
28228 
28229         /* set the transfer length */
28230         nblocks = mode2->cdread_buflen / 2336;
28231         cdb[6] = (uchar_t)(nblocks >> 16);
28232         cdb[7] = (uchar_t)(nblocks >> 8);
28233         cdb[8] = (uchar_t)nblocks;
28234 
28235         /* set the filter bits */
28236         cdb[9] = CDROM_READ_CD_USERDATA;
28237 
28238         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28239         com->uscsi_cdb = (caddr_t)cdb;
28240         com->uscsi_cdblen = sizeof (cdb);
28241         com->uscsi_bufaddr = mode2->cdread_bufaddr;
28242         com->uscsi_buflen = mode2->cdread_buflen;
28243         com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28244 
28245         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28246             SD_PATH_STANDARD);
28247         kmem_free(com, sizeof (*com));
28248         return (rval);
28249 }
28250 
28251 
28252 /*
28253  *    Function: sr_read_mode2()
28254  *
28255  * Description: This routine is the driver entry point for handling CD-ROM
28256  *              ioctl read mode2 requests (CDROMREADMODE2) for devices that
28257  *              do not support the READ CD (0xBE) command.
28258  *
28259  *   Arguments: dev     - the device 'dev_t'
28260  *              data    - pointer to user provided cd read structure specifying
28261  *                        the lba buffer address and length.
28262  *              flag    - this argument is a pass through to ddi_copyxxx()
28263  *                        directly from the mode argument of ioctl().
28264  *
28265  * Return Code: the code returned by sd_send_scsi_cmd()
28266  *              EFAULT if ddi_copyxxx() fails
28267  *              ENXIO if fail ddi_get_soft_state
28268  *              EINVAL if data pointer is NULL
28269  *              EIO if fail to reset block size
28270  *              EAGAIN if commands are in progress in the driver
28271  */
28272 
28273 static int
28274 sr_read_mode2(dev_t dev, caddr_t data, int flag)
28275 {
28276         struct sd_lun           *un;
28277         struct cdrom_read       mode2_struct;
28278         struct cdrom_read       *mode2 = &mode2_struct;
28279         int                     rval;
28280         uint32_t                restore_blksize;
28281         struct uscsi_cmd        *com;
28282         uchar_t                 cdb[CDB_GROUP0];
28283         int                     nblocks;
28284 
28285 #ifdef _MULTI_DATAMODEL
28286         /* To support ILP32 applications in an LP64 world */
28287         struct cdrom_read32     cdrom_read32;
28288         struct cdrom_read32     *cdrd32 = &cdrom_read32;
28289 #endif /* _MULTI_DATAMODEL */
28290 
28291         if (data == NULL) {
28292                 return (EINVAL);
28293         }
28294 
28295         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28296             (un->un_state == SD_STATE_OFFLINE)) {
28297                 return (ENXIO);
28298         }
28299 
28300         /*
28301          * Because this routine will update the device and driver block size
28302          * being used we want to make sure there are no commands in progress.
28303          * If commands are in progress the user will have to try again.
28304          *
28305          * We check for 1 instead of 0 because we increment un_ncmds_in_driver
28306          * in sdioctl to protect commands from sdioctl through to the top of
28307          * sd_uscsi_strategy. See sdioctl for details.
28308          */
28309         mutex_enter(SD_MUTEX(un));
28310         if (un->un_ncmds_in_driver != 1) {
28311                 mutex_exit(SD_MUTEX(un));
28312                 return (EAGAIN);
28313         }
28314         mutex_exit(SD_MUTEX(un));
28315 
28316         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28317             "sd_read_mode2: entry: un:0x%p\n", un);
28318 
28319 #ifdef _MULTI_DATAMODEL
28320         switch (ddi_model_convert_from(flag & FMODELS)) {
28321         case DDI_MODEL_ILP32:
28322                 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28323                         return (EFAULT);
28324                 }
28325                 /* Convert the ILP32 uscsi data from the application to LP64 */
28326                 cdrom_read32tocdrom_read(cdrd32, mode2);
28327                 break;
28328         case DDI_MODEL_NONE:
28329                 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28330                         return (EFAULT);
28331                 }
28332                 break;
28333         }
28334 #else /* ! _MULTI_DATAMODEL */
28335         if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) {
28336                 return (EFAULT);
28337         }
28338 #endif /* _MULTI_DATAMODEL */
28339 
28340         /* Store the current target block size for restoration later */
28341         restore_blksize = un->un_tgt_blocksize;
28342 
28343         /* Change the device and soft state target block size to 2336 */
28344         if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) {
28345                 rval = EIO;
28346                 goto done;
28347         }
28348 
28349 
28350         bzero(cdb, sizeof (cdb));
28351 
28352         /* set READ operation */
28353         cdb[0] = SCMD_READ;
28354 
28355         /* adjust lba for 2kbyte blocks from 512 byte blocks */
28356         mode2->cdread_lba >>= 2;
28357 
28358         /* set the start address */
28359         cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F);
28360         cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28361         cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF);
28362 
28363         /* set the transfer length */
28364         nblocks = mode2->cdread_buflen / 2336;
28365         cdb[4] = (uchar_t)nblocks & 0xFF;
28366 
28367         /* build command */
28368         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28369         com->uscsi_cdb = (caddr_t)cdb;
28370         com->uscsi_cdblen = sizeof (cdb);
28371         com->uscsi_bufaddr = mode2->cdread_bufaddr;
28372         com->uscsi_buflen = mode2->cdread_buflen;
28373         com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28374 
28375         /*
28376          * Issue SCSI command with user space address for read buffer.
28377          *
28378          * This sends the command through main channel in the driver.
28379          *
28380          * Since this is accessed via an IOCTL call, we go through the
28381          * standard path, so that if the device was powered down, then
28382          * it would be 'awakened' to handle the command.
28383          */
28384         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28385             SD_PATH_STANDARD);
28386 
28387         kmem_free(com, sizeof (*com));
28388 
28389         /* Restore the device and soft state target block size */
28390         if (sr_sector_mode(dev, restore_blksize) != 0) {
28391                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28392                     "can't do switch back to mode 1\n");
28393                 /*
28394                  * If sd_send_scsi_READ succeeded we still need to report
28395                  * an error because we failed to reset the block size
28396                  */
28397                 if (rval == 0) {
28398                         rval = EIO;
28399                 }
28400         }
28401 
28402 done:
28403         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28404             "sd_read_mode2: exit: un:0x%p\n", un);
28405 
28406         return (rval);
28407 }
28408 
28409 
28410 /*
28411  *    Function: sr_sector_mode()
28412  *
28413  * Description: This utility function is used by sr_read_mode2 to set the target
28414  *              block size based on the user specified size. This is a legacy
28415  *              implementation based upon a vendor specific mode page
28416  *
28417  *   Arguments: dev     - the device 'dev_t'
28418  *              data    - flag indicating if block size is being set to 2336 or
28419  *                        512.
28420  *
28421  * Return Code: the code returned by sd_send_scsi_cmd()
28422  *              EFAULT if ddi_copyxxx() fails
28423  *              ENXIO if fail ddi_get_soft_state
28424  *              EINVAL if data pointer is NULL
28425  */
28426 
28427 static int
28428 sr_sector_mode(dev_t dev, uint32_t blksize)
28429 {
28430         struct sd_lun   *un;
28431         uchar_t         *sense;
28432         uchar_t         *select;
28433         int             rval;
28434         sd_ssc_t        *ssc;
28435 
28436         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28437             (un->un_state == SD_STATE_OFFLINE)) {
28438                 return (ENXIO);
28439         }
28440 
28441         sense = kmem_zalloc(20, KM_SLEEP);
28442 
28443         /* Note: This is a vendor specific mode page (0x81) */
28444         ssc = sd_ssc_init(un);
28445         rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 20, 0x81,
28446             SD_PATH_STANDARD);
28447         sd_ssc_fini(ssc);
28448         if (rval != 0) {
28449                 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28450                     "sr_sector_mode: Mode Sense failed\n");
28451                 kmem_free(sense, 20);
28452                 return (rval);
28453         }
28454         select = kmem_zalloc(20, KM_SLEEP);
28455         select[3] = 0x08;
28456         select[10] = ((blksize >> 8) & 0xff);
28457         select[11] = (blksize & 0xff);
28458         select[12] = 0x01;
28459         select[13] = 0x06;
28460         select[14] = sense[14];
28461         select[15] = sense[15];
28462         if (blksize == SD_MODE2_BLKSIZE) {
28463                 select[14] |= 0x01;
28464         }
28465 
28466         ssc = sd_ssc_init(un);
28467         rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 20,
28468             SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
28469         sd_ssc_fini(ssc);
28470         if (rval != 0) {
28471                 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28472                     "sr_sector_mode: Mode Select failed\n");
28473         } else {
28474                 /*
28475                  * Only update the softstate block size if we successfully
28476                  * changed the device block mode.
28477                  */
28478                 mutex_enter(SD_MUTEX(un));
28479                 sd_update_block_info(un, blksize, 0);
28480                 mutex_exit(SD_MUTEX(un));
28481         }
28482         kmem_free(sense, 20);
28483         kmem_free(select, 20);
28484         return (rval);
28485 }
28486 
28487 
28488 /*
28489  *    Function: sr_read_cdda()
28490  *
28491  * Description: This routine is the driver entry point for handling CD-ROM
28492  *              ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If
28493  *              the target supports CDDA these requests are handled via a vendor
28494  *              specific command (0xD8) If the target does not support CDDA
28495  *              these requests are handled via the READ CD command (0xBE).
28496  *
28497  *   Arguments: dev     - the device 'dev_t'
28498  *              data    - pointer to user provided CD-DA structure specifying
28499  *                        the track starting address, transfer length, and
28500  *                        subcode options.
28501  *              flag    - this argument is a pass through to ddi_copyxxx()
28502  *                        directly from the mode argument of ioctl().
28503  *
28504  * Return Code: the code returned by sd_send_scsi_cmd()
28505  *              EFAULT if ddi_copyxxx() fails
28506  *              ENXIO if fail ddi_get_soft_state
28507  *              EINVAL if invalid arguments are provided
28508  *              ENOTTY
28509  */
28510 
28511 static int
28512 sr_read_cdda(dev_t dev, caddr_t data, int flag)
28513 {
28514         struct sd_lun                   *un;
28515         struct uscsi_cmd                *com;
28516         struct cdrom_cdda               *cdda;
28517         int                             rval;
28518         size_t                          buflen;
28519         char                            cdb[CDB_GROUP5];
28520 
28521 #ifdef _MULTI_DATAMODEL
28522         /* To support ILP32 applications in an LP64 world */
28523         struct cdrom_cdda32     cdrom_cdda32;
28524         struct cdrom_cdda32     *cdda32 = &cdrom_cdda32;
28525 #endif /* _MULTI_DATAMODEL */
28526 
28527         if (data == NULL) {
28528                 return (EINVAL);
28529         }
28530 
28531         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28532                 return (ENXIO);
28533         }
28534 
28535         cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP);
28536 
28537 #ifdef _MULTI_DATAMODEL
28538         switch (ddi_model_convert_from(flag & FMODELS)) {
28539         case DDI_MODEL_ILP32:
28540                 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) {
28541                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28542                             "sr_read_cdda: ddi_copyin Failed\n");
28543                         kmem_free(cdda, sizeof (struct cdrom_cdda));
28544                         return (EFAULT);
28545                 }
28546                 /* Convert the ILP32 uscsi data from the application to LP64 */
28547                 cdrom_cdda32tocdrom_cdda(cdda32, cdda);
28548                 break;
28549         case DDI_MODEL_NONE:
28550                 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28551                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28552                             "sr_read_cdda: ddi_copyin Failed\n");
28553                         kmem_free(cdda, sizeof (struct cdrom_cdda));
28554                         return (EFAULT);
28555                 }
28556                 break;
28557         }
28558 #else /* ! _MULTI_DATAMODEL */
28559         if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28560                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28561                     "sr_read_cdda: ddi_copyin Failed\n");
28562                 kmem_free(cdda, sizeof (struct cdrom_cdda));
28563                 return (EFAULT);
28564         }
28565 #endif /* _MULTI_DATAMODEL */
28566 
28567         /*
28568          * Since MMC-2 expects max 3 bytes for length, check if the
28569          * length input is greater than 3 bytes
28570          */
28571         if ((cdda->cdda_length & 0xFF000000) != 0) {
28572                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: "
28573                     "cdrom transfer length too large: %d (limit %d)\n",
28574                     cdda->cdda_length, 0xFFFFFF);
28575                 kmem_free(cdda, sizeof (struct cdrom_cdda));
28576                 return (EINVAL);
28577         }
28578 
28579         switch (cdda->cdda_subcode) {
28580         case CDROM_DA_NO_SUBCODE:
28581                 buflen = CDROM_BLK_2352 * cdda->cdda_length;
28582                 break;
28583         case CDROM_DA_SUBQ:
28584                 buflen = CDROM_BLK_2368 * cdda->cdda_length;
28585                 break;
28586         case CDROM_DA_ALL_SUBCODE:
28587                 buflen = CDROM_BLK_2448 * cdda->cdda_length;
28588                 break;
28589         case CDROM_DA_SUBCODE_ONLY:
28590                 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length;
28591                 break;
28592         default:
28593                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28594                     "sr_read_cdda: Subcode '0x%x' Not Supported\n",
28595                     cdda->cdda_subcode);
28596                 kmem_free(cdda, sizeof (struct cdrom_cdda));
28597                 return (EINVAL);
28598         }
28599 
28600         /* Build and send the command */
28601         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28602         bzero(cdb, CDB_GROUP5);
28603 
28604         if (un->un_f_cfg_cdda == TRUE) {
28605                 cdb[0] = (char)SCMD_READ_CD;
28606                 cdb[1] = 0x04;
28607                 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28608                 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28609                 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28610                 cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28611                 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28612                 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28613                 cdb[8] = ((cdda->cdda_length) & 0x000000ff);
28614                 cdb[9] = 0x10;
28615                 switch (cdda->cdda_subcode) {
28616                 case CDROM_DA_NO_SUBCODE :
28617                         cdb[10] = 0x0;
28618                         break;
28619                 case CDROM_DA_SUBQ :
28620                         cdb[10] = 0x2;
28621                         break;
28622                 case CDROM_DA_ALL_SUBCODE :
28623                         cdb[10] = 0x1;
28624                         break;
28625                 case CDROM_DA_SUBCODE_ONLY :
28626                         /* FALLTHROUGH */
28627                 default :
28628                         kmem_free(cdda, sizeof (struct cdrom_cdda));
28629                         kmem_free(com, sizeof (*com));
28630                         return (ENOTTY);
28631                 }
28632         } else {
28633                 cdb[0] = (char)SCMD_READ_CDDA;
28634                 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28635                 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28636                 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28637                 cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28638                 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24);
28639                 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28640                 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28641                 cdb[9] = ((cdda->cdda_length) & 0x000000ff);
28642                 cdb[10] = cdda->cdda_subcode;
28643         }
28644 
28645         com->uscsi_cdb = cdb;
28646         com->uscsi_cdblen = CDB_GROUP5;
28647         com->uscsi_bufaddr = (caddr_t)cdda->cdda_data;
28648         com->uscsi_buflen = buflen;
28649         com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28650 
28651         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28652             SD_PATH_STANDARD);
28653 
28654         kmem_free(cdda, sizeof (struct cdrom_cdda));
28655         kmem_free(com, sizeof (*com));
28656         return (rval);
28657 }
28658 
28659 
28660 /*
28661  *    Function: sr_read_cdxa()
28662  *
28663  * Description: This routine is the driver entry point for handling CD-ROM
28664  *              ioctl requests to return CD-XA (Extended Architecture) data.
28665  *              (CDROMCDXA).
28666  *
28667  *   Arguments: dev     - the device 'dev_t'
28668  *              data    - pointer to user provided CD-XA structure specifying
28669  *                        the data starting address, transfer length, and format
28670  *              flag    - this argument is a pass through to ddi_copyxxx()
28671  *                        directly from the mode argument of ioctl().
28672  *
28673  * Return Code: the code returned by sd_send_scsi_cmd()
28674  *              EFAULT if ddi_copyxxx() fails
28675  *              ENXIO if fail ddi_get_soft_state
28676  *              EINVAL if data pointer is NULL
28677  */
28678 
28679 static int
28680 sr_read_cdxa(dev_t dev, caddr_t data, int flag)
28681 {
28682         struct sd_lun           *un;
28683         struct uscsi_cmd        *com;
28684         struct cdrom_cdxa       *cdxa;
28685         int                     rval;
28686         size_t                  buflen;
28687         char                    cdb[CDB_GROUP5];
28688         uchar_t                 read_flags;
28689 
28690 #ifdef _MULTI_DATAMODEL
28691         /* To support ILP32 applications in an LP64 world */
28692         struct cdrom_cdxa32             cdrom_cdxa32;
28693         struct cdrom_cdxa32             *cdxa32 = &cdrom_cdxa32;
28694 #endif /* _MULTI_DATAMODEL */
28695 
28696         if (data == NULL) {
28697                 return (EINVAL);
28698         }
28699 
28700         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28701                 return (ENXIO);
28702         }
28703 
28704         cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP);
28705 
28706 #ifdef _MULTI_DATAMODEL
28707         switch (ddi_model_convert_from(flag & FMODELS)) {
28708         case DDI_MODEL_ILP32:
28709                 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) {
28710                         kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28711                         return (EFAULT);
28712                 }
28713                 /*
28714                  * Convert the ILP32 uscsi data from the
28715                  * application to LP64 for internal use.
28716                  */
28717                 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa);
28718                 break;
28719         case DDI_MODEL_NONE:
28720                 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28721                         kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28722                         return (EFAULT);
28723                 }
28724                 break;
28725         }
28726 #else /* ! _MULTI_DATAMODEL */
28727         if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28728                 kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28729                 return (EFAULT);
28730         }
28731 #endif /* _MULTI_DATAMODEL */
28732 
28733         /*
28734          * Since MMC-2 expects max 3 bytes for length, check if the
28735          * length input is greater than 3 bytes
28736          */
28737         if ((cdxa->cdxa_length & 0xFF000000) != 0) {
28738                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: "
28739                     "cdrom transfer length too large: %d (limit %d)\n",
28740                     cdxa->cdxa_length, 0xFFFFFF);
28741                 kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28742                 return (EINVAL);
28743         }
28744 
28745         switch (cdxa->cdxa_format) {
28746         case CDROM_XA_DATA:
28747                 buflen = CDROM_BLK_2048 * cdxa->cdxa_length;
28748                 read_flags = 0x10;
28749                 break;
28750         case CDROM_XA_SECTOR_DATA:
28751                 buflen = CDROM_BLK_2352 * cdxa->cdxa_length;
28752                 read_flags = 0xf8;
28753                 break;
28754         case CDROM_XA_DATA_W_ERROR:
28755                 buflen = CDROM_BLK_2646 * cdxa->cdxa_length;
28756                 read_flags = 0xfc;
28757                 break;
28758         default:
28759                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28760                     "sr_read_cdxa: Format '0x%x' Not Supported\n",
28761                     cdxa->cdxa_format);
28762                 kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28763                 return (EINVAL);
28764         }
28765 
28766         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28767         bzero(cdb, CDB_GROUP5);
28768         if (un->un_f_mmc_cap == TRUE) {
28769                 cdb[0] = (char)SCMD_READ_CD;
28770                 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28771                 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28772                 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28773                 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28774                 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28775                 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28776                 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff);
28777                 cdb[9] = (char)read_flags;
28778         } else {
28779                 /*
28780                  * Note: A vendor specific command (0xDB) is being used her to
28781                  * request a read of all subcodes.
28782                  */
28783                 cdb[0] = (char)SCMD_READ_CDXA;
28784                 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28785                 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28786                 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28787                 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28788                 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24);
28789                 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28790                 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28791                 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff);
28792                 cdb[10] = cdxa->cdxa_format;
28793         }
28794         com->uscsi_cdb          = cdb;
28795         com->uscsi_cdblen  = CDB_GROUP5;
28796         com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data;
28797         com->uscsi_buflen  = buflen;
28798         com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28799         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28800             SD_PATH_STANDARD);
28801         kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28802         kmem_free(com, sizeof (*com));
28803         return (rval);
28804 }
28805 
28806 
28807 /*
28808  *    Function: sr_eject()
28809  *
28810  * Description: This routine is the driver entry point for handling CD-ROM
28811  *              eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT)
28812  *
28813  *   Arguments: dev     - the device 'dev_t'
28814  *
28815  * Return Code: the code returned by sd_send_scsi_cmd()
28816  */
28817 
28818 static int
28819 sr_eject(dev_t dev)
28820 {
28821         struct sd_lun   *un;
28822         int             rval;
28823         sd_ssc_t        *ssc;
28824 
28825         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28826             (un->un_state == SD_STATE_OFFLINE)) {
28827                 return (ENXIO);
28828         }
28829 
28830         /*
28831          * To prevent race conditions with the eject
28832          * command, keep track of an eject command as
28833          * it progresses. If we are already handling
28834          * an eject command in the driver for the given
28835          * unit and another request to eject is received
28836          * immediately return EAGAIN so we don't lose
28837          * the command if the current eject command fails.
28838          */
28839         mutex_enter(SD_MUTEX(un));
28840         if (un->un_f_ejecting == TRUE) {
28841                 mutex_exit(SD_MUTEX(un));
28842                 return (EAGAIN);
28843         }
28844         un->un_f_ejecting = TRUE;
28845         mutex_exit(SD_MUTEX(un));
28846 
28847         ssc = sd_ssc_init(un);
28848         rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
28849             SD_PATH_STANDARD);
28850         sd_ssc_fini(ssc);
28851 
28852         if (rval != 0) {
28853                 mutex_enter(SD_MUTEX(un));
28854                 un->un_f_ejecting = FALSE;
28855                 mutex_exit(SD_MUTEX(un));
28856                 return (rval);
28857         }
28858 
28859         ssc = sd_ssc_init(un);
28860         rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
28861             SD_TARGET_EJECT, SD_PATH_STANDARD);
28862         sd_ssc_fini(ssc);
28863 
28864         if (rval == 0) {
28865                 mutex_enter(SD_MUTEX(un));
28866                 sr_ejected(un);
28867                 un->un_mediastate = DKIO_EJECTED;
28868                 un->un_f_ejecting = FALSE;
28869                 cv_broadcast(&un->un_state_cv);
28870                 mutex_exit(SD_MUTEX(un));
28871         } else {
28872                 mutex_enter(SD_MUTEX(un));
28873                 un->un_f_ejecting = FALSE;
28874                 mutex_exit(SD_MUTEX(un));
28875         }
28876         return (rval);
28877 }
28878 
28879 
28880 /*
28881  *    Function: sr_ejected()
28882  *
28883  * Description: This routine updates the soft state structure to invalidate the
28884  *              geometry information after the media has been ejected or a
28885  *              media eject has been detected.
28886  *
28887  *   Arguments: un - driver soft state (unit) structure
28888  */
28889 
28890 static void
28891 sr_ejected(struct sd_lun *un)
28892 {
28893         struct sd_errstats *stp;
28894 
28895         ASSERT(un != NULL);
28896         ASSERT(mutex_owned(SD_MUTEX(un)));
28897 
28898         un->un_f_blockcount_is_valid = FALSE;
28899         un->un_f_tgt_blocksize_is_valid      = FALSE;
28900         mutex_exit(SD_MUTEX(un));
28901         cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
28902         mutex_enter(SD_MUTEX(un));
28903 
28904         if (un->un_errstats != NULL) {
28905                 stp = (struct sd_errstats *)un->un_errstats->ks_data;
28906                 stp->sd_capacity.value.ui64 = 0;
28907         }
28908 }
28909 
28910 
28911 /*
28912  *    Function: sr_check_wp()
28913  *
28914  * Description: This routine checks the write protection of a removable
28915  *      media disk and hotpluggable devices via the write protect bit of
28916  *      the Mode Page Header device specific field. Some devices choke
28917  *      on unsupported mode page. In order to workaround this issue,
28918  *      this routine has been implemented to use 0x3f mode page(request
28919  *      for all pages) for all device types.
28920  *
28921  *   Arguments: dev             - the device 'dev_t'
28922  *
28923  * Return Code: int indicating if the device is write protected (1) or not (0)
28924  *
28925  *     Context: Kernel thread.
28926  *
28927  */
28928 
28929 static int
28930 sr_check_wp(dev_t dev)
28931 {
28932         struct sd_lun   *un;
28933         uchar_t         device_specific;
28934         uchar_t         *sense;
28935         int             hdrlen;
28936         int             rval = FALSE;
28937         int             status;
28938         sd_ssc_t        *ssc;
28939 
28940         /*
28941          * Note: The return codes for this routine should be reworked to
28942          * properly handle the case of a NULL softstate.
28943          */
28944         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28945                 return (FALSE);
28946         }
28947 
28948         if (un->un_f_cfg_is_atapi == TRUE) {
28949                 /*
28950                  * The mode page contents are not required; set the allocation
28951                  * length for the mode page header only
28952                  */
28953                 hdrlen = MODE_HEADER_LENGTH_GRP2;
28954                 sense = kmem_zalloc(hdrlen, KM_SLEEP);
28955                 ssc = sd_ssc_init(un);
28956                 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, hdrlen,
28957                     MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
28958                 sd_ssc_fini(ssc);
28959                 if (status != 0)
28960                         goto err_exit;
28961                 device_specific =
28962                     ((struct mode_header_grp2 *)sense)->device_specific;
28963         } else {
28964                 hdrlen = MODE_HEADER_LENGTH;
28965                 sense = kmem_zalloc(hdrlen, KM_SLEEP);
28966                 ssc = sd_ssc_init(un);
28967                 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, hdrlen,
28968                     MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
28969                 sd_ssc_fini(ssc);
28970                 if (status != 0)
28971                         goto err_exit;
28972                 device_specific =
28973                     ((struct mode_header *)sense)->device_specific;
28974         }
28975 
28976 
28977         /*
28978          * Write protect mode sense failed; not all disks
28979          * understand this query. Return FALSE assuming that
28980          * these devices are not writable.
28981          */
28982         if (device_specific & WRITE_PROTECT) {
28983                 rval = TRUE;
28984         }
28985 
28986 err_exit:
28987         kmem_free(sense, hdrlen);
28988         return (rval);
28989 }
28990 
28991 /*
28992  *    Function: sr_volume_ctrl()
28993  *
28994  * Description: This routine is the driver entry point for handling CD-ROM
28995  *              audio output volume ioctl requests. (CDROMVOLCTRL)
28996  *
28997  *   Arguments: dev     - the device 'dev_t'
28998  *              data    - pointer to user audio volume control structure
28999  *              flag    - this argument is a pass through to ddi_copyxxx()
29000  *                        directly from the mode argument of ioctl().
29001  *
29002  * Return Code: the code returned by sd_send_scsi_cmd()
29003  *              EFAULT if ddi_copyxxx() fails
29004  *              ENXIO if fail ddi_get_soft_state
29005  *              EINVAL if data pointer is NULL
29006  *
29007  */
29008 
29009 static int
29010 sr_volume_ctrl(dev_t dev, caddr_t data, int flag)
29011 {
29012         struct sd_lun           *un;
29013         struct cdrom_volctrl    volume;
29014         struct cdrom_volctrl    *vol = &volume;
29015         uchar_t                 *sense_page;
29016         uchar_t                 *select_page;
29017         uchar_t                 *sense;
29018         uchar_t                 *select;
29019         int                     sense_buflen;
29020         int                     select_buflen;
29021         int                     rval;
29022         sd_ssc_t                *ssc;
29023 
29024         if (data == NULL) {
29025                 return (EINVAL);
29026         }
29027 
29028         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29029             (un->un_state == SD_STATE_OFFLINE)) {
29030                 return (ENXIO);
29031         }
29032 
29033         if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) {
29034                 return (EFAULT);
29035         }
29036 
29037         if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29038                 struct mode_header_grp2         *sense_mhp;
29039                 struct mode_header_grp2         *select_mhp;
29040                 int                             bd_len;
29041 
29042                 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN;
29043                 select_buflen = MODE_HEADER_LENGTH_GRP2 +
29044                     MODEPAGE_AUDIO_CTRL_LEN;
29045                 sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
29046                 select = kmem_zalloc(select_buflen, KM_SLEEP);
29047                 ssc = sd_ssc_init(un);
29048                 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
29049                     sense_buflen, MODEPAGE_AUDIO_CTRL,
29050                     SD_PATH_STANDARD);
29051                 sd_ssc_fini(ssc);
29052 
29053                 if (rval != 0) {
29054                         SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
29055                             "sr_volume_ctrl: Mode Sense Failed\n");
29056                         kmem_free(sense, sense_buflen);
29057                         kmem_free(select, select_buflen);
29058                         return (rval);
29059                 }
29060                 sense_mhp = (struct mode_header_grp2 *)sense;
29061                 select_mhp = (struct mode_header_grp2 *)select;
29062                 bd_len = (sense_mhp->bdesc_length_hi << 8) |
29063                     sense_mhp->bdesc_length_lo;
29064                 if (bd_len > MODE_BLK_DESC_LENGTH) {
29065                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29066                             "sr_volume_ctrl: Mode Sense returned invalid "
29067                             "block descriptor length\n");
29068                         kmem_free(sense, sense_buflen);
29069                         kmem_free(select, select_buflen);
29070                         return (EIO);
29071                 }
29072                 sense_page = (uchar_t *)
29073                     (sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
29074                 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2);
29075                 select_mhp->length_msb = 0;
29076                 select_mhp->length_lsb = 0;
29077                 select_mhp->bdesc_length_hi = 0;
29078                 select_mhp->bdesc_length_lo = 0;
29079         } else {
29080                 struct mode_header              *sense_mhp, *select_mhp;
29081 
29082                 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29083                 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29084                 sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
29085                 select = kmem_zalloc(select_buflen, KM_SLEEP);
29086                 ssc = sd_ssc_init(un);
29087                 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
29088                     sense_buflen, MODEPAGE_AUDIO_CTRL,
29089                     SD_PATH_STANDARD);
29090                 sd_ssc_fini(ssc);
29091 
29092                 if (rval != 0) {
29093                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29094                             "sr_volume_ctrl: Mode Sense Failed\n");
29095                         kmem_free(sense, sense_buflen);
29096                         kmem_free(select, select_buflen);
29097                         return (rval);
29098                 }
29099                 sense_mhp  = (struct mode_header *)sense;
29100                 select_mhp = (struct mode_header *)select;
29101                 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) {
29102                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29103                             "sr_volume_ctrl: Mode Sense returned invalid "
29104                             "block descriptor length\n");
29105                         kmem_free(sense, sense_buflen);
29106                         kmem_free(select, select_buflen);
29107                         return (EIO);
29108                 }
29109                 sense_page = (uchar_t *)
29110                     (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
29111                 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH);
29112                 select_mhp->length = 0;
29113                 select_mhp->bdesc_length = 0;
29114         }
29115         /*
29116          * Note: An audio control data structure could be created and overlayed
29117          * on the following in place of the array indexing method implemented.
29118          */
29119 
29120         /* Build the select data for the user volume data */
29121         select_page[0] = MODEPAGE_AUDIO_CTRL;
29122         select_page[1] = 0xE;
29123         /* Set the immediate bit */
29124         select_page[2] = 0x04;
29125         /* Zero out reserved fields */
29126         select_page[3] = 0x00;
29127         select_page[4] = 0x00;
29128         /* Return sense data for fields not to be modified */
29129         select_page[5] = sense_page[5];
29130         select_page[6] = sense_page[6];
29131         select_page[7] = sense_page[7];
29132         /* Set the user specified volume levels for channel 0 and 1 */
29133         select_page[8] = 0x01;
29134         select_page[9] = vol->channel0;
29135         select_page[10] = 0x02;
29136         select_page[11] = vol->channel1;
29137         /* Channel 2 and 3 are currently unsupported so return the sense data */
29138         select_page[12] = sense_page[12];
29139         select_page[13] = sense_page[13];
29140         select_page[14] = sense_page[14];
29141         select_page[15] = sense_page[15];
29142 
29143         ssc = sd_ssc_init(un);
29144         if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29145                 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, select,
29146                     select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29147         } else {
29148                 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
29149                     select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29150         }
29151         sd_ssc_fini(ssc);
29152 
29153         kmem_free(sense, sense_buflen);
29154         kmem_free(select, select_buflen);
29155         return (rval);
29156 }
29157 
29158 
29159 /*
29160  *    Function: sr_read_sony_session_offset()
29161  *
29162  * Description: This routine is the driver entry point for handling CD-ROM
29163  *              ioctl requests for session offset information. (CDROMREADOFFSET)
29164  *              The address of the first track in the last session of a
29165  *              multi-session CD-ROM is returned
29166  *
29167  *              Note: This routine uses a vendor specific key value in the
29168  *              command control field without implementing any vendor check here
29169  *              or in the ioctl routine.
29170  *
29171  *   Arguments: dev     - the device 'dev_t'
29172  *              data    - pointer to an int to hold the requested address
29173  *              flag    - this argument is a pass through to ddi_copyxxx()
29174  *                        directly from the mode argument of ioctl().
29175  *
29176  * Return Code: the code returned by sd_send_scsi_cmd()
29177  *              EFAULT if ddi_copyxxx() fails
29178  *              ENXIO if fail ddi_get_soft_state
29179  *              EINVAL if data pointer is NULL
29180  */
29181 
29182 static int
29183 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag)
29184 {
29185         struct sd_lun           *un;
29186         struct uscsi_cmd        *com;
29187         caddr_t                 buffer;
29188         char                    cdb[CDB_GROUP1];
29189         int                     session_offset = 0;
29190         int                     rval;
29191 
29192         if (data == NULL) {
29193                 return (EINVAL);
29194         }
29195 
29196         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29197             (un->un_state == SD_STATE_OFFLINE)) {
29198                 return (ENXIO);
29199         }
29200 
29201         buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP);
29202         bzero(cdb, CDB_GROUP1);
29203         cdb[0] = SCMD_READ_TOC;
29204         /*
29205          * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
29206          * (4 byte TOC response header + 8 byte response data)
29207          */
29208         cdb[8] = SONY_SESSION_OFFSET_LEN;
29209         /* Byte 9 is the control byte. A vendor specific value is used */
29210         cdb[9] = SONY_SESSION_OFFSET_KEY;
29211         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
29212         com->uscsi_cdb = cdb;
29213         com->uscsi_cdblen = CDB_GROUP1;
29214         com->uscsi_bufaddr = buffer;
29215         com->uscsi_buflen = SONY_SESSION_OFFSET_LEN;
29216         com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
29217 
29218         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
29219             SD_PATH_STANDARD);
29220         if (rval != 0) {
29221                 kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29222                 kmem_free(com, sizeof (*com));
29223                 return (rval);
29224         }
29225         if (buffer[1] == SONY_SESSION_OFFSET_VALID) {
29226                 session_offset =
29227                     ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
29228                     ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
29229                 /*
29230                  * Offset returned offset in current lbasize block's. Convert to
29231                  * 2k block's to return to the user
29232                  */
29233                 if (un->un_tgt_blocksize == CDROM_BLK_512) {
29234                         session_offset >>= 2;
29235                 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) {
29236                         session_offset >>= 1;
29237                 }
29238         }
29239 
29240         if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) {
29241                 rval = EFAULT;
29242         }
29243 
29244         kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29245         kmem_free(com, sizeof (*com));
29246         return (rval);
29247 }
29248 
29249 
29250 /*
29251  *    Function: sd_wm_cache_constructor()
29252  *
29253  * Description: Cache Constructor for the wmap cache for the read/modify/write
29254  *              devices.
29255  *
29256  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29257  *              un      - sd_lun structure for the device.
29258  *              flag    - the km flags passed to constructor
29259  *
29260  * Return Code: 0 on success.
29261  *              -1 on failure.
29262  */
29263 
29264 /*ARGSUSED*/
29265 static int
29266 sd_wm_cache_constructor(void *wm, void *un, int flags)
29267 {
29268         bzero(wm, sizeof (struct sd_w_map));
29269         cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL);
29270         return (0);
29271 }
29272 
29273 
29274 /*
29275  *    Function: sd_wm_cache_destructor()
29276  *
29277  * Description: Cache destructor for the wmap cache for the read/modify/write
29278  *              devices.
29279  *
29280  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29281  *              un      - sd_lun structure for the device.
29282  */
29283 /*ARGSUSED*/
29284 static void
29285 sd_wm_cache_destructor(void *wm, void *un)
29286 {
29287         cv_destroy(&((struct sd_w_map *)wm)->wm_avail);
29288 }
29289 
29290 
29291 /*
29292  *    Function: sd_range_lock()
29293  *
29294  * Description: Lock the range of blocks specified as parameter to ensure
29295  *              that read, modify write is atomic and no other i/o writes
29296  *              to the same location. The range is specified in terms
29297  *              of start and end blocks. Block numbers are the actual
29298  *              media block numbers and not system.
29299  *
29300  *   Arguments: un      - sd_lun structure for the device.
29301  *              startb - The starting block number
29302  *              endb - The end block number
29303  *              typ - type of i/o - simple/read_modify_write
29304  *
29305  * Return Code: wm  - pointer to the wmap structure.
29306  *
29307  *     Context: This routine can sleep.
29308  */
29309 
29310 static struct sd_w_map *
29311 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ)
29312 {
29313         struct sd_w_map *wmp = NULL;
29314         struct sd_w_map *sl_wmp = NULL;
29315         struct sd_w_map *tmp_wmp;
29316         wm_state state = SD_WM_CHK_LIST;
29317 
29318 
29319         ASSERT(un != NULL);
29320         ASSERT(!mutex_owned(SD_MUTEX(un)));
29321 
29322         mutex_enter(SD_MUTEX(un));
29323 
29324         while (state != SD_WM_DONE) {
29325 
29326                 switch (state) {
29327                 case SD_WM_CHK_LIST:
29328                         /*
29329                          * This is the starting state. Check the wmap list
29330                          * to see if the range is currently available.
29331                          */
29332                         if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) {
29333                                 /*
29334                                  * If this is a simple write and no rmw
29335                                  * i/o is pending then try to lock the
29336                                  * range as the range should be available.
29337                                  */
29338                                 state = SD_WM_LOCK_RANGE;
29339                         } else {
29340                                 tmp_wmp = sd_get_range(un, startb, endb);
29341                                 if (tmp_wmp != NULL) {
29342                                         if ((wmp != NULL) && ONLIST(un, wmp)) {
29343                                                 /*
29344                                                  * Should not keep onlist wmps
29345                                                  * while waiting this macro
29346                                                  * will also do wmp = NULL;
29347                                                  */
29348                                                 FREE_ONLIST_WMAP(un, wmp);
29349                                         }
29350                                         /*
29351                                          * sl_wmp is the wmap on which wait
29352                                          * is done, since the tmp_wmp points
29353                                          * to the inuse wmap, set sl_wmp to
29354                                          * tmp_wmp and change the state to sleep
29355                                          */
29356                                         sl_wmp = tmp_wmp;
29357                                         state = SD_WM_WAIT_MAP;
29358                                 } else {
29359                                         state = SD_WM_LOCK_RANGE;
29360                                 }
29361 
29362                         }
29363                         break;
29364 
29365                 case SD_WM_LOCK_RANGE:
29366                         ASSERT(un->un_wm_cache);
29367                         /*
29368                          * The range need to be locked, try to get a wmap.
29369                          * First attempt it with NO_SLEEP, want to avoid a sleep
29370                          * if possible as we will have to release the sd mutex
29371                          * if we have to sleep.
29372                          */
29373                         if (wmp == NULL)
29374                                 wmp = kmem_cache_alloc(un->un_wm_cache,
29375                                     KM_NOSLEEP);
29376                         if (wmp == NULL) {
29377                                 mutex_exit(SD_MUTEX(un));
29378                                 wmp = kmem_cache_alloc(un->un_wm_cache,
29379                                     KM_SLEEP);
29380                                 mutex_enter(SD_MUTEX(un));
29381                                 /*
29382                                  * we released the mutex so recheck and go to
29383                                  * check list state.
29384                                  */
29385                                 state = SD_WM_CHK_LIST;
29386                         } else {
29387                                 /*
29388                                  * We exit out of state machine since we
29389                                  * have the wmap. Do the housekeeping first.
29390                                  * place the wmap on the wmap list if it is not
29391                                  * on it already and then set the state to done.
29392                                  */
29393                                 wmp->wm_start = startb;
29394                                 wmp->wm_end = endb;
29395                                 wmp->wm_flags = typ | SD_WM_BUSY;
29396                                 if (typ & SD_WTYPE_RMW) {
29397                                         un->un_rmw_count++;
29398                                 }
29399                                 /*
29400                                  * If not already on the list then link
29401                                  */
29402                                 if (!ONLIST(un, wmp)) {
29403                                         wmp->wm_next = un->un_wm;
29404                                         wmp->wm_prev = NULL;
29405                                         if (wmp->wm_next)
29406                                                 wmp->wm_next->wm_prev = wmp;
29407                                         un->un_wm = wmp;
29408                                 }
29409                                 state = SD_WM_DONE;
29410                         }
29411                         break;
29412 
29413                 case SD_WM_WAIT_MAP:
29414                         ASSERT(sl_wmp->wm_flags & SD_WM_BUSY);
29415                         /*
29416                          * Wait is done on sl_wmp, which is set in the
29417                          * check_list state.
29418                          */
29419                         sl_wmp->wm_wanted_count++;
29420                         cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un));
29421                         sl_wmp->wm_wanted_count--;
29422                         /*
29423                          * We can reuse the memory from the completed sl_wmp
29424                          * lock range for our new lock, but only if noone is
29425                          * waiting for it.
29426                          */
29427                         ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY));
29428                         if (sl_wmp->wm_wanted_count == 0) {
29429                                 if (wmp != NULL) {
29430                                         CHK_N_FREEWMP(un, wmp);
29431                                 }
29432                                 wmp = sl_wmp;
29433                         }
29434                         sl_wmp = NULL;
29435                         /*
29436                          * After waking up, need to recheck for availability of
29437                          * range.
29438                          */
29439                         state = SD_WM_CHK_LIST;
29440                         break;
29441 
29442                 default:
29443                         panic("sd_range_lock: "
29444                             "Unknown state %d in sd_range_lock", state);
29445                         /*NOTREACHED*/
29446                 } /* switch(state) */
29447 
29448         } /* while(state != SD_WM_DONE) */
29449 
29450         mutex_exit(SD_MUTEX(un));
29451 
29452         ASSERT(wmp != NULL);
29453 
29454         return (wmp);
29455 }
29456 
29457 
29458 /*
29459  *    Function: sd_get_range()
29460  *
29461  * Description: Find if there any overlapping I/O to this one
29462  *              Returns the write-map of 1st such I/O, NULL otherwise.
29463  *
29464  *   Arguments: un      - sd_lun structure for the device.
29465  *              startb - The starting block number
29466  *              endb - The end block number
29467  *
29468  * Return Code: wm  - pointer to the wmap structure.
29469  */
29470 
29471 static struct sd_w_map *
29472 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb)
29473 {
29474         struct sd_w_map *wmp;
29475 
29476         ASSERT(un != NULL);
29477 
29478         for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) {
29479                 if (!(wmp->wm_flags & SD_WM_BUSY)) {
29480                         continue;
29481                 }
29482                 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) {
29483                         break;
29484                 }
29485                 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) {
29486                         break;
29487                 }
29488         }
29489 
29490         return (wmp);
29491 }
29492 
29493 
29494 /*
29495  *    Function: sd_free_inlist_wmap()
29496  *
29497  * Description: Unlink and free a write map struct.
29498  *
29499  *   Arguments: un      - sd_lun structure for the device.
29500  *              wmp     - sd_w_map which needs to be unlinked.
29501  */
29502 
29503 static void
29504 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp)
29505 {
29506         ASSERT(un != NULL);
29507 
29508         if (un->un_wm == wmp) {
29509                 un->un_wm = wmp->wm_next;
29510         } else {
29511                 wmp->wm_prev->wm_next = wmp->wm_next;
29512         }
29513 
29514         if (wmp->wm_next) {
29515                 wmp->wm_next->wm_prev = wmp->wm_prev;
29516         }
29517 
29518         wmp->wm_next = wmp->wm_prev = NULL;
29519 
29520         kmem_cache_free(un->un_wm_cache, wmp);
29521 }
29522 
29523 
29524 /*
29525  *    Function: sd_range_unlock()
29526  *
29527  * Description: Unlock the range locked by wm.
29528  *              Free write map if nobody else is waiting on it.
29529  *
29530  *   Arguments: un      - sd_lun structure for the device.
29531  *              wmp     - sd_w_map which needs to be unlinked.
29532  */
29533 
29534 static void
29535 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm)
29536 {
29537         ASSERT(un != NULL);
29538         ASSERT(wm != NULL);
29539         ASSERT(!mutex_owned(SD_MUTEX(un)));
29540 
29541         mutex_enter(SD_MUTEX(un));
29542 
29543         if (wm->wm_flags & SD_WTYPE_RMW) {
29544                 un->un_rmw_count--;
29545         }
29546 
29547         if (wm->wm_wanted_count) {
29548                 wm->wm_flags = 0;
29549                 /*
29550                  * Broadcast that the wmap is available now.
29551                  */
29552                 cv_broadcast(&wm->wm_avail);
29553         } else {
29554                 /*
29555                  * If no one is waiting on the map, it should be free'ed.
29556                  */
29557                 sd_free_inlist_wmap(un, wm);
29558         }
29559 
29560         mutex_exit(SD_MUTEX(un));
29561 }
29562 
29563 
29564 /*
29565  *    Function: sd_read_modify_write_task
29566  *
29567  * Description: Called from a taskq thread to initiate the write phase of
29568  *              a read-modify-write request.  This is used for targets where
29569  *              un->un_sys_blocksize != un->un_tgt_blocksize.
29570  *
29571  *   Arguments: arg - a pointer to the buf(9S) struct for the write command.
29572  *
29573  *     Context: Called under taskq thread context.
29574  */
29575 
29576 static void
29577 sd_read_modify_write_task(void *arg)
29578 {
29579         struct sd_mapblocksize_info     *bsp;
29580         struct buf      *bp;
29581         struct sd_xbuf  *xp;
29582         struct sd_lun   *un;
29583 
29584         bp = arg;       /* The bp is given in arg */
29585         ASSERT(bp != NULL);
29586 
29587         /* Get the pointer to the layer-private data struct */
29588         xp = SD_GET_XBUF(bp);
29589         ASSERT(xp != NULL);
29590         bsp = xp->xb_private;
29591         ASSERT(bsp != NULL);
29592 
29593         un = SD_GET_UN(bp);
29594         ASSERT(un != NULL);
29595         ASSERT(!mutex_owned(SD_MUTEX(un)));
29596 
29597         SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29598             "sd_read_modify_write_task: entry: buf:0x%p\n", bp);
29599 
29600         /*
29601          * This is the write phase of a read-modify-write request, called
29602          * under the context of a taskq thread in response to the completion
29603          * of the read portion of the rmw request completing under interrupt
29604          * context. The write request must be sent from here down the iostart
29605          * chain as if it were being sent from sd_mapblocksize_iostart(), so
29606          * we use the layer index saved in the layer-private data area.
29607          */
29608         SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp);
29609 
29610         SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29611             "sd_read_modify_write_task: exit: buf:0x%p\n", bp);
29612 }
29613 
29614 
29615 /*
29616  *    Function: sddump_do_read_of_rmw()
29617  *
29618  * Description: This routine will be called from sddump, If sddump is called
29619  *              with an I/O which not aligned on device blocksize boundary
29620  *              then the write has to be converted to read-modify-write.
29621  *              Do the read part here in order to keep sddump simple.
29622  *              Note - That the sd_mutex is held across the call to this
29623  *              routine.
29624  *
29625  *   Arguments: un      - sd_lun
29626  *              blkno   - block number in terms of media block size.
29627  *              nblk    - number of blocks.
29628  *              bpp     - pointer to pointer to the buf structure. On return
29629  *                      from this function, *bpp points to the valid buffer
29630  *                      to which the write has to be done.
29631  *
29632  * Return Code: 0 for success or errno-type return code
29633  */
29634 
29635 static int
29636 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
29637     struct buf **bpp)
29638 {
29639         int err;
29640         int i;
29641         int rval;
29642         struct buf *bp;
29643         struct scsi_pkt *pkt = NULL;
29644         uint32_t target_blocksize;
29645 
29646         ASSERT(un != NULL);
29647         ASSERT(mutex_owned(SD_MUTEX(un)));
29648 
29649         target_blocksize = un->un_tgt_blocksize;
29650 
29651         mutex_exit(SD_MUTEX(un));
29652 
29653         bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL,
29654             (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL);
29655         if (bp == NULL) {
29656                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29657                     "no resources for dumping; giving up");
29658                 err = ENOMEM;
29659                 goto done;
29660         }
29661 
29662         rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL,
29663             blkno, nblk);
29664         if (rval != 0) {
29665                 scsi_free_consistent_buf(bp);
29666                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29667                     "no resources for dumping; giving up");
29668                 err = ENOMEM;
29669                 goto done;
29670         }
29671 
29672         pkt->pkt_flags |= FLAG_NOINTR;
29673 
29674         err = EIO;
29675         for (i = 0; i < SD_NDUMP_RETRIES; i++) {
29676 
29677                 /*
29678                  * Scsi_poll returns 0 (success) if the command completes and
29679                  * the status block is STATUS_GOOD.  We should only check
29680                  * errors if this condition is not true.  Even then we should
29681                  * send our own request sense packet only if we have a check
29682                  * condition and auto request sense has not been performed by
29683                  * the hba.
29684                  */
29685                 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n");
29686 
29687                 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) {
29688                         err = 0;
29689                         break;
29690                 }
29691 
29692                 /*
29693                  * Check CMD_DEV_GONE 1st, give up if device is gone,
29694                  * no need to read RQS data.
29695                  */
29696                 if (pkt->pkt_reason == CMD_DEV_GONE) {
29697                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29698                             "Error while dumping state with rmw..."
29699                             "Device is gone\n");
29700                         break;
29701                 }
29702 
29703                 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) {
29704                         SD_INFO(SD_LOG_DUMP, un,
29705                             "sddump: read failed with CHECK, try # %d\n", i);
29706                         if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) {
29707                                 (void) sd_send_polled_RQS(un);
29708                         }
29709 
29710                         continue;
29711                 }
29712 
29713                 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) {
29714                         int reset_retval = 0;
29715 
29716                         SD_INFO(SD_LOG_DUMP, un,
29717                             "sddump: read failed with BUSY, try # %d\n", i);
29718 
29719                         if (un->un_f_lun_reset_enabled == TRUE) {
29720                                 reset_retval = scsi_reset(SD_ADDRESS(un),
29721                                     RESET_LUN);
29722                         }
29723                         if (reset_retval == 0) {
29724                                 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
29725                         }
29726                         (void) sd_send_polled_RQS(un);
29727 
29728                 } else {
29729                         SD_INFO(SD_LOG_DUMP, un,
29730                             "sddump: read failed with 0x%x, try # %d\n",
29731                             SD_GET_PKT_STATUS(pkt), i);
29732                         mutex_enter(SD_MUTEX(un));
29733                         sd_reset_target(un, pkt);
29734                         mutex_exit(SD_MUTEX(un));
29735                 }
29736 
29737                 /*
29738                  * If we are not getting anywhere with lun/target resets,
29739                  * let's reset the bus.
29740                  */
29741                 if (i > SD_NDUMP_RETRIES/2) {
29742                         (void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
29743                         (void) sd_send_polled_RQS(un);
29744                 }
29745 
29746         }
29747         scsi_destroy_pkt(pkt);
29748 
29749         if (err != 0) {
29750                 scsi_free_consistent_buf(bp);
29751                 *bpp = NULL;
29752         } else {
29753                 *bpp = bp;
29754         }
29755 
29756 done:
29757         mutex_enter(SD_MUTEX(un));
29758         return (err);
29759 }
29760 
29761 
29762 /*
29763  *    Function: sd_failfast_flushq
29764  *
29765  * Description: Take all bp's on the wait queue that have B_FAILFAST set
29766  *              in b_flags and move them onto the failfast queue, then kick
29767  *              off a thread to return all bp's on the failfast queue to
29768  *              their owners with an error set.
29769  *
29770  *   Arguments: un - pointer to the soft state struct for the instance.
29771  *
29772  *     Context: may execute in interrupt context.
29773  */
29774 
29775 static void
29776 sd_failfast_flushq(struct sd_lun *un, boolean_t flush_all)
29777 {
29778         struct buf *bp;
29779         struct buf *next_waitq_bp;
29780         struct buf *prev_waitq_bp = NULL;
29781 
29782         ASSERT(un != NULL);
29783         ASSERT(mutex_owned(SD_MUTEX(un)));
29784         ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE);
29785         ASSERT(un->un_failfast_bp == NULL);
29786 
29787         SD_TRACE(SD_LOG_IO_FAILFAST, un,
29788             "sd_failfast_flushq: entry: un:0x%p\n", un);
29789 
29790         /*
29791          * Check if we should flush all bufs when entering failfast state, or
29792          * just those with B_FAILFAST set.
29793          */
29794         if ((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) ||
29795             flush_all) {
29796                 /*
29797                  * Move *all* bp's on the wait queue to the failfast flush
29798                  * queue, including those that do NOT have B_FAILFAST set.
29799                  */
29800                 if (un->un_failfast_headp == NULL) {
29801                         ASSERT(un->un_failfast_tailp == NULL);
29802                         un->un_failfast_headp = un->un_waitq_headp;
29803                 } else {
29804                         ASSERT(un->un_failfast_tailp != NULL);
29805                         un->un_failfast_tailp->av_forw = un->un_waitq_headp;
29806                 }
29807 
29808                 un->un_failfast_tailp = un->un_waitq_tailp;
29809 
29810                 /* update kstat for each bp moved out of the waitq */
29811                 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) {
29812                         SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29813                 }
29814 
29815                 /* empty the waitq */
29816                 un->un_waitq_headp = un->un_waitq_tailp = NULL;
29817 
29818         } else {
29819                 /*
29820                  * Go thru the wait queue, pick off all entries with
29821                  * B_FAILFAST set, and move these onto the failfast queue.
29822                  */
29823                 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) {
29824                         /*
29825                          * Save the pointer to the next bp on the wait queue,
29826                          * so we get to it on the next iteration of this loop.
29827                          */
29828                         next_waitq_bp = bp->av_forw;
29829 
29830                         /*
29831                          * If this bp from the wait queue does NOT have
29832                          * B_FAILFAST set, just move on to the next element
29833                          * in the wait queue. Note, this is the only place
29834                          * where it is correct to set prev_waitq_bp.
29835                          */
29836                         if ((bp->b_flags & B_FAILFAST) == 0) {
29837                                 prev_waitq_bp = bp;
29838                                 continue;
29839                         }
29840 
29841                         /*
29842                          * Remove the bp from the wait queue.
29843                          */
29844                         if (bp == un->un_waitq_headp) {
29845                                 /* The bp is the first element of the waitq. */
29846                                 un->un_waitq_headp = next_waitq_bp;
29847                                 if (un->un_waitq_headp == NULL) {
29848                                         /* The wait queue is now empty */
29849                                         un->un_waitq_tailp = NULL;
29850                                 }
29851                         } else {
29852                                 /*
29853                                  * The bp is either somewhere in the middle
29854                                  * or at the end of the wait queue.
29855                                  */
29856                                 ASSERT(un->un_waitq_headp != NULL);
29857                                 ASSERT(prev_waitq_bp != NULL);
29858                                 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST)
29859                                     == 0);
29860                                 if (bp == un->un_waitq_tailp) {
29861                                         /* bp is the last entry on the waitq. */
29862                                         ASSERT(next_waitq_bp == NULL);
29863                                         un->un_waitq_tailp = prev_waitq_bp;
29864                                 }
29865                                 prev_waitq_bp->av_forw = next_waitq_bp;
29866                         }
29867                         bp->av_forw = NULL;
29868 
29869                         /*
29870                          * update kstat since the bp is moved out of
29871                          * the waitq
29872                          */
29873                         SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29874 
29875                         /*
29876                          * Now put the bp onto the failfast queue.
29877                          */
29878                         if (un->un_failfast_headp == NULL) {
29879                                 /* failfast queue is currently empty */
29880                                 ASSERT(un->un_failfast_tailp == NULL);
29881                                 un->un_failfast_headp =
29882                                     un->un_failfast_tailp = bp;
29883                         } else {
29884                                 /* Add the bp to the end of the failfast q */
29885                                 ASSERT(un->un_failfast_tailp != NULL);
29886                                 ASSERT(un->un_failfast_tailp->b_flags &
29887                                     B_FAILFAST);
29888                                 un->un_failfast_tailp->av_forw = bp;
29889                                 un->un_failfast_tailp = bp;
29890                         }
29891                 }
29892         }
29893 
29894         /*
29895          * Now return all bp's on the failfast queue to their owners.
29896          */
29897         while ((bp = un->un_failfast_headp) != NULL) {
29898 
29899                 un->un_failfast_headp = bp->av_forw;
29900                 if (un->un_failfast_headp == NULL) {
29901                         un->un_failfast_tailp = NULL;
29902                 }
29903 
29904                 /*
29905                  * We want to return the bp with a failure error code, but
29906                  * we do not want a call to sd_start_cmds() to occur here,
29907                  * so use sd_return_failed_command_no_restart() instead of
29908                  * sd_return_failed_command().
29909                  */
29910                 sd_return_failed_command_no_restart(un, bp, EIO);
29911         }
29912 
29913         /* Flush the xbuf queues if required. */
29914         if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) {
29915                 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback);
29916         }
29917 
29918         SD_TRACE(SD_LOG_IO_FAILFAST, un,
29919             "sd_failfast_flushq: exit: un:0x%p\n", un);
29920 }
29921 
29922 
29923 /*
29924  *    Function: sd_failfast_flushq_callback
29925  *
29926  * Description: Return TRUE if the given bp meets the criteria for failfast
29927  *              flushing. Used with ddi_xbuf_flushq(9F).
29928  *
29929  *   Arguments: bp - ptr to buf struct to be examined.
29930  *
29931  *     Context: Any
29932  */
29933 
29934 static int
29935 sd_failfast_flushq_callback(struct buf *bp)
29936 {
29937         /*
29938          * Return TRUE if (1) we want to flush ALL bufs when the failfast
29939          * state is entered; OR (2) the given bp has B_FAILFAST set.
29940          */
29941         return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) ||
29942             (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE);
29943 }
29944 
29945 
29946 
29947 /*
29948  * Function: sd_setup_next_xfer
29949  *
29950  * Description: Prepare next I/O operation using DMA_PARTIAL
29951  *
29952  */
29953 
29954 static int
29955 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
29956     struct scsi_pkt *pkt, struct sd_xbuf *xp)
29957 {
29958         ssize_t num_blks_not_xfered;
29959         daddr_t strt_blk_num;
29960         ssize_t bytes_not_xfered;
29961         int     rval;
29962 
29963         ASSERT(pkt->pkt_resid == 0);
29964 
29965         /*
29966          * Calculate next block number and amount to be transferred.
29967          *
29968          * How much data NOT transfered to the HBA yet.
29969          */
29970         bytes_not_xfered = xp->xb_dma_resid;
29971 
29972         /*
29973          * figure how many blocks NOT transfered to the HBA yet.
29974          */
29975         num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered);
29976 
29977         /*
29978          * set starting block number to the end of what WAS transfered.
29979          */
29980         strt_blk_num = xp->xb_blkno +
29981             SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered);
29982 
29983         /*
29984          * Move pkt to the next portion of the xfer.  sd_setup_next_rw_pkt
29985          * will call scsi_initpkt with NULL_FUNC so we do not have to release
29986          * the disk mutex here.
29987          */
29988         rval = sd_setup_next_rw_pkt(un, pkt, bp,
29989             strt_blk_num, num_blks_not_xfered);
29990 
29991         if (rval == 0) {
29992 
29993                 /*
29994                  * Success.
29995                  *
29996                  * Adjust things if there are still more blocks to be
29997                  * transfered.
29998                  */
29999                 xp->xb_dma_resid = pkt->pkt_resid;
30000                 pkt->pkt_resid = 0;
30001 
30002                 return (1);
30003         }
30004 
30005         /*
30006          * There's really only one possible return value from
30007          * sd_setup_next_rw_pkt which occurs when scsi_init_pkt
30008          * returns NULL.
30009          */
30010         ASSERT(rval == SD_PKT_ALLOC_FAILURE);
30011 
30012         bp->b_resid = bp->b_bcount;
30013         bp->b_flags |= B_ERROR;
30014 
30015         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
30016             "Error setting up next portion of DMA transfer\n");
30017 
30018         return (0);
30019 }
30020 
30021 /*
30022  *    Function: sd_panic_for_res_conflict
30023  *
30024  * Description: Call panic with a string formatted with "Reservation Conflict"
30025  *              and a human readable identifier indicating the SD instance
30026  *              that experienced the reservation conflict.
30027  *
30028  *   Arguments: un - pointer to the soft state struct for the instance.
30029  *
30030  *     Context: may execute in interrupt context.
30031  */
30032 
30033 #define SD_RESV_CONFLICT_FMT_LEN 40
30034 void
30035 sd_panic_for_res_conflict(struct sd_lun *un)
30036 {
30037         char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN];
30038         char path_str[MAXPATHLEN];
30039 
30040         (void) snprintf(panic_str, sizeof (panic_str),
30041             "Reservation Conflict\nDisk: %s",
30042             ddi_pathname(SD_DEVINFO(un), path_str));
30043 
30044         panic(panic_str);
30045 }
30046 
30047 /*
30048  * Note: The following sd_faultinjection_ioctl( ) routines implement
30049  * driver support for handling fault injection for error analysis
30050  * causing faults in multiple layers of the driver.
30051  *
30052  */
30053 
30054 #ifdef SD_FAULT_INJECTION
30055 
30056 /*
30057  *    Function: sd_faultinjection_ioctl()
30058  *
30059  * Description: This routine is the driver entry point for handling
30060  *              faultinjection ioctls to inject errors into the
30061  *              layer model
30062  *
30063  *   Arguments: cmd     - the ioctl cmd received
30064  *              arg     - the arguments from user and returns
30065  */
30066 
30067 static int
30068 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un)
30069 {
30070         uint_t i = 0;
30071         uint_t rval;
30072         int ret = 0;
30073 
30074         SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n");
30075 
30076         mutex_enter(SD_MUTEX(un));
30077 
30078         switch (cmd) {
30079         case SDIOCRUN:
30080                 /* Allow pushed faults to be injected */
30081                 SD_INFO(SD_LOG_SDTEST, un,
30082                     "sd_faultinjection_ioctl: Injecting Fault Run\n");
30083 
30084                 sd_fault_injection_on = 1;
30085 
30086                 SD_INFO(SD_LOG_IOERR, un,
30087                     "sd_faultinjection_ioctl: run finished\n");
30088                 break;
30089 
30090         case SDIOCSTART:
30091                 /* Start Injection Session */
30092                 SD_INFO(SD_LOG_SDTEST, un,
30093                     "sd_faultinjection_ioctl: Injecting Fault Start\n");
30094 
30095                 sd_fault_injection_on = 0;
30096                 un->sd_injection_mask = 0xFFFFFFFF;
30097                 for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30098                         un->sd_fi_fifo_pkt[i] = NULL;
30099                         un->sd_fi_fifo_xb[i] = NULL;
30100                         un->sd_fi_fifo_un[i] = NULL;
30101                         un->sd_fi_fifo_arq[i] = NULL;
30102                         un->sd_fi_fifo_tran[i] = NULL;
30103                 }
30104                 un->sd_fi_fifo_start = 0;
30105                 un->sd_fi_fifo_end = 0;
30106 
30107                 mutex_enter(&(un->un_fi_mutex));
30108                 un->sd_fi_log[0] = '\0';
30109                 un->sd_fi_buf_len = 0;
30110                 mutex_exit(&(un->un_fi_mutex));
30111 
30112                 SD_INFO(SD_LOG_IOERR, un,
30113                     "sd_faultinjection_ioctl: start finished\n");
30114                 break;
30115 
30116         case SDIOCSTOP:
30117                 /* Stop Injection Session */
30118                 SD_INFO(SD_LOG_SDTEST, un,
30119                     "sd_faultinjection_ioctl: Injecting Fault Stop\n");
30120                 sd_fault_injection_on = 0;
30121                 un->sd_injection_mask = 0x0;
30122 
30123                 /* Empty stray or unuseds structs from fifo */
30124                 for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30125                         if (un->sd_fi_fifo_pkt[i] != NULL) {
30126                                 kmem_free(un->sd_fi_fifo_pkt[i],
30127                                     sizeof (struct sd_fi_pkt));
30128                         }
30129                         if (un->sd_fi_fifo_xb[i] != NULL) {
30130                                 kmem_free(un->sd_fi_fifo_xb[i],
30131                                     sizeof (struct sd_fi_xb));
30132                         }
30133                         if (un->sd_fi_fifo_un[i] != NULL) {
30134                                 kmem_free(un->sd_fi_fifo_un[i],
30135                                     sizeof (struct sd_fi_un));
30136                         }
30137                         if (un->sd_fi_fifo_arq[i] != NULL) {
30138                                 kmem_free(un->sd_fi_fifo_arq[i],
30139                                     sizeof (struct sd_fi_arq));
30140                         }
30141                         if (un->sd_fi_fifo_tran[i] != NULL) {
30142                                 kmem_free(un->sd_fi_fifo_tran[i],
30143                                     sizeof (struct sd_fi_tran));
30144                         }
30145                         un->sd_fi_fifo_pkt[i] = NULL;
30146                         un->sd_fi_fifo_un[i] = NULL;
30147                         un->sd_fi_fifo_xb[i] = NULL;
30148                         un->sd_fi_fifo_arq[i] = NULL;
30149                         un->sd_fi_fifo_tran[i] = NULL;
30150                 }
30151                 un->sd_fi_fifo_start = 0;
30152                 un->sd_fi_fifo_end = 0;
30153 
30154                 SD_INFO(SD_LOG_IOERR, un,
30155                     "sd_faultinjection_ioctl: stop finished\n");
30156                 break;
30157 
30158         case SDIOCINSERTPKT:
30159                 /* Store a packet struct to be pushed onto fifo */
30160                 SD_INFO(SD_LOG_SDTEST, un,
30161                     "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n");
30162 
30163                 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30164 
30165                 if (un->sd_fi_fifo_tran[i] != NULL) {
30166                         ret = EBUSY;
30167                         break;
30168                 }
30169 
30170                 sd_fault_injection_on = 0;
30171 
30172                 /* No more that SD_FI_MAX_ERROR allowed in Queue */
30173                 if (un->sd_fi_fifo_pkt[i] != NULL) {
30174                         kmem_free(un->sd_fi_fifo_pkt[i],
30175                             sizeof (struct sd_fi_pkt));
30176                 }
30177                 if (arg != NULL) {
30178                         un->sd_fi_fifo_pkt[i] =
30179                             kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP);
30180                         if (un->sd_fi_fifo_pkt[i] == NULL) {
30181                                 /* Alloc failed don't store anything */
30182                                 ret = ENOMEM;
30183                                 break;
30184                         }
30185                         rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i],
30186                             sizeof (struct sd_fi_pkt), 0);
30187                         if (rval == -1) {
30188                                 kmem_free(un->sd_fi_fifo_pkt[i],
30189                                     sizeof (struct sd_fi_pkt));
30190                                 un->sd_fi_fifo_pkt[i] = NULL;
30191                                 ret = EFAULT;
30192                                 break;
30193                         }
30194                 } else {
30195                         SD_INFO(SD_LOG_IOERR, un,
30196                             "sd_faultinjection_ioctl: pkt null\n");
30197                 }
30198                 break;
30199 
30200         case SDIOCINSERTTRAN:
30201                 /* Store a tran packet struct to be pushed onto fifo. */
30202                 SD_INFO(SD_LOG_SDTEST, un,
30203                     "sd_faultinjection_ioctl: Injecting Fault Insert TRAN\n");
30204                 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30205 
30206                 /*
30207                  * HBA-related fault injections can't be mixed with target-level
30208                  * fault injections.
30209                  */
30210                 if (un->sd_fi_fifo_pkt[i] != NULL ||
30211                     un->sd_fi_fifo_xb[i] != NULL ||
30212                     un->sd_fi_fifo_un[i] != NULL ||
30213                     un->sd_fi_fifo_arq[i] != NULL) {
30214                         ret = EBUSY;
30215                         break;
30216                 }
30217 
30218                 sd_fault_injection_on = 0;
30219 
30220                 if (un->sd_fi_fifo_tran[i] != NULL) {
30221                         kmem_free(un->sd_fi_fifo_tran[i],
30222                             sizeof (struct sd_fi_tran));
30223                         un->sd_fi_fifo_tran[i] = NULL;
30224                 }
30225                 if (arg != NULL) {
30226                         un->sd_fi_fifo_tran[i] =
30227                             kmem_alloc(sizeof (struct sd_fi_tran), KM_NOSLEEP);
30228                         if (un->sd_fi_fifo_tran[i] == NULL) {
30229                                 /* Alloc failed don't store anything */
30230                                 ret = ENOMEM;
30231                                 break;
30232                         }
30233                         rval = ddi_copyin((void *)arg, un->sd_fi_fifo_tran[i],
30234                             sizeof (struct sd_fi_tran), 0);
30235 
30236                         if (rval == 0) {
30237                                 switch (un->sd_fi_fifo_tran[i]->tran_cmd) {
30238                                         case SD_FLTINJ_CMD_BUSY:
30239                                         case SD_FLTINJ_CMD_TIMEOUT:
30240                                                 break;
30241                                         default:
30242                                                 ret = EINVAL;
30243                                                 break;
30244                                 }
30245                         } else {
30246                                 ret = EFAULT;
30247                         }
30248 
30249                         if (ret != 0) {
30250                                 kmem_free(un->sd_fi_fifo_tran[i],
30251                                     sizeof (struct sd_fi_tran));
30252                                 un->sd_fi_fifo_tran[i] = NULL;
30253                                 break;
30254                         }
30255                 } else {
30256                         SD_INFO(SD_LOG_IOERR, un,
30257                             "sd_faultinjection_ioctl: tran null\n");
30258                 }
30259                 break;
30260 
30261         case SDIOCINSERTXB:
30262                 /* Store a xb struct to be pushed onto fifo */
30263                 SD_INFO(SD_LOG_SDTEST, un,
30264                     "sd_faultinjection_ioctl: Injecting Fault Insert XB\n");
30265 
30266                 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30267 
30268                 if (un->sd_fi_fifo_tran[i] != NULL) {
30269                         ret = EBUSY;
30270                         break;
30271                 }
30272 
30273                 sd_fault_injection_on = 0;
30274 
30275                 if (un->sd_fi_fifo_xb[i] != NULL) {
30276                         kmem_free(un->sd_fi_fifo_xb[i],
30277                             sizeof (struct sd_fi_xb));
30278                         un->sd_fi_fifo_xb[i] = NULL;
30279                 }
30280                 if (arg != NULL) {
30281                         un->sd_fi_fifo_xb[i] =
30282                             kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP);
30283                         if (un->sd_fi_fifo_xb[i] == NULL) {
30284                                 /* Alloc failed don't store anything */
30285                                 ret = ENOMEM;
30286                                 break;
30287                         }
30288                         rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i],
30289                             sizeof (struct sd_fi_xb), 0);
30290 
30291                         if (rval == -1) {
30292                                 kmem_free(un->sd_fi_fifo_xb[i],
30293                                     sizeof (struct sd_fi_xb));
30294                                 un->sd_fi_fifo_xb[i] = NULL;
30295                                 ret = EFAULT;
30296                                 break;
30297                         }
30298                 } else {
30299                         SD_INFO(SD_LOG_IOERR, un,
30300                             "sd_faultinjection_ioctl: xb null\n");
30301                 }
30302                 break;
30303 
30304         case SDIOCINSERTUN:
30305                 /* Store a un struct to be pushed onto fifo */
30306                 SD_INFO(SD_LOG_SDTEST, un,
30307                     "sd_faultinjection_ioctl: Injecting Fault Insert UN\n");
30308 
30309                 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30310                 if (un->sd_fi_fifo_tran[i] != NULL) {
30311                         ret = EBUSY;
30312                         break;
30313                 }
30314 
30315                 sd_fault_injection_on = 0;
30316 
30317                 if (un->sd_fi_fifo_un[i] != NULL) {
30318                         kmem_free(un->sd_fi_fifo_un[i],
30319                             sizeof (struct sd_fi_un));
30320                         un->sd_fi_fifo_un[i] = NULL;
30321                 }
30322                 if (arg != NULL) {
30323                         un->sd_fi_fifo_un[i] =
30324                             kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP);
30325                         if (un->sd_fi_fifo_un[i] == NULL) {
30326                                 /* Alloc failed don't store anything */
30327                                 ret = ENOMEM;
30328                                 break;
30329                         }
30330                         rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i],
30331                             sizeof (struct sd_fi_un), 0);
30332                         if (rval == -1) {
30333                                 kmem_free(un->sd_fi_fifo_un[i],
30334                                     sizeof (struct sd_fi_un));
30335                                 un->sd_fi_fifo_un[i] = NULL;
30336                                 ret = EFAULT;
30337                                 break;
30338                         }
30339 
30340                 } else {
30341                         SD_INFO(SD_LOG_IOERR, un,
30342                             "sd_faultinjection_ioctl: un null\n");
30343                 }
30344 
30345                 break;
30346 
30347         case SDIOCINSERTARQ:
30348                 /* Store a arq struct to be pushed onto fifo */
30349                 SD_INFO(SD_LOG_SDTEST, un,
30350                     "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n");
30351                 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30352                 if (un->sd_fi_fifo_tran[i] != NULL) {
30353                         ret = EBUSY;
30354                         break;
30355                 }
30356 
30357                 sd_fault_injection_on = 0;
30358 
30359                 if (un->sd_fi_fifo_arq[i] != NULL) {
30360                         kmem_free(un->sd_fi_fifo_arq[i],
30361                             sizeof (struct sd_fi_arq));
30362                         un->sd_fi_fifo_arq[i] = NULL;
30363                 }
30364                 if (arg != NULL) {
30365                         un->sd_fi_fifo_arq[i] =
30366                             kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP);
30367                         if (un->sd_fi_fifo_arq[i] == NULL) {
30368                                 /* Alloc failed don't store anything */
30369                                 ret = ENOMEM;
30370                                 break;
30371                         }
30372                         rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i],
30373                             sizeof (struct sd_fi_arq), 0);
30374                         if (rval == -1) {
30375                                 kmem_free(un->sd_fi_fifo_arq[i],
30376                                     sizeof (struct sd_fi_arq));
30377                                 un->sd_fi_fifo_arq[i] = NULL;
30378                                 ret = EFAULT;
30379                                 break;
30380                         }
30381 
30382                 } else {
30383                         SD_INFO(SD_LOG_IOERR, un,
30384                             "sd_faultinjection_ioctl: arq null\n");
30385                 }
30386 
30387                 break;
30388 
30389         case SDIOCPUSH:
30390                 /* Push stored xb, pkt, un, arq and tran onto fifo */
30391                 sd_fault_injection_on = 0;
30392 
30393                 if (arg != NULL) {
30394                         rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0);
30395                         if (rval != -1 &&
30396                             un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30397                                 un->sd_fi_fifo_end += i;
30398                         }
30399                 } else {
30400                         SD_INFO(SD_LOG_IOERR, un,
30401                             "sd_faultinjection_ioctl: push arg null\n");
30402                         if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30403                                 un->sd_fi_fifo_end++;
30404                         }
30405                 }
30406                 SD_INFO(SD_LOG_IOERR, un,
30407                     "sd_faultinjection_ioctl: push to end=%d\n",
30408                     un->sd_fi_fifo_end);
30409                 break;
30410 
30411         case SDIOCRETRIEVE:
30412                 /* Return buffer of log from Injection session */
30413                 SD_INFO(SD_LOG_SDTEST, un,
30414                     "sd_faultinjection_ioctl: Injecting Fault Retreive");
30415 
30416                 sd_fault_injection_on = 0;
30417 
30418                 mutex_enter(&(un->un_fi_mutex));
30419                 rval = ddi_copyout(un->sd_fi_log, (void *)arg,
30420                     un->sd_fi_buf_len+1, 0);
30421                 mutex_exit(&(un->un_fi_mutex));
30422 
30423                 if (rval == -1) {
30424                         /*
30425                          * arg is possibly invalid setting
30426                          * it to NULL for return
30427                          */
30428                         arg = NULL;
30429                 }
30430                 break;
30431         }
30432 
30433         mutex_exit(SD_MUTEX(un));
30434         SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: exit\n");
30435         return (ret);
30436 }
30437 
30438 
30439 /*
30440  *    Function: sd_injection_log()
30441  *
30442  * Description: This routine adds buff to the already existing injection log
30443  *              for retrieval via faultinjection_ioctl for use in fault
30444  *              detection and recovery
30445  *
30446  *   Arguments: buf - the string to add to the log
30447  */
30448 
30449 static void
30450 sd_injection_log(char *buf, struct sd_lun *un)
30451 {
30452         uint_t len;
30453 
30454         ASSERT(un != NULL);
30455         ASSERT(buf != NULL);
30456 
30457         mutex_enter(&(un->un_fi_mutex));
30458 
30459         len = min(strlen(buf), 255);
30460         /* Add logged value to Injection log to be returned later */
30461         if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) {
30462                 uint_t  offset = strlen((char *)un->sd_fi_log);
30463                 char *destp = (char *)un->sd_fi_log + offset;
30464                 int i;
30465                 for (i = 0; i < len; i++) {
30466                         *destp++ = *buf++;
30467                 }
30468                 un->sd_fi_buf_len += len;
30469                 un->sd_fi_log[un->sd_fi_buf_len] = '\0';
30470         }
30471 
30472         mutex_exit(&(un->un_fi_mutex));
30473 }
30474 
30475 /*
30476  * This function is called just before sending the packet to the HBA.
30477  * Caller must hold per-LUN mutex. Mutex is held locked upon return.
30478  */
30479 static void
30480 sd_prefaultinjection(struct scsi_pkt *pktp)
30481 {
30482         uint_t i;
30483         struct buf *bp;
30484         struct sd_lun *un;
30485         struct sd_fi_tran *fi_tran;
30486 
30487         ASSERT(pktp != NULL);
30488 
30489         /* pull bp and un from pktp */
30490         bp = (struct buf *)pktp->pkt_private;
30491         un = SD_GET_UN(bp);
30492 
30493         /* if injection is off return */
30494         if (sd_fault_injection_on == 0 ||
30495             un->sd_fi_fifo_start == un->sd_fi_fifo_end) {
30496                 return;
30497         }
30498 
30499         ASSERT(un != NULL);
30500         ASSERT(mutex_owned(SD_MUTEX(un)));
30501 
30502         /* take next set off fifo */
30503         i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR;
30504 
30505         fi_tran = un->sd_fi_fifo_tran[i];
30506         if (fi_tran != NULL) {
30507                 switch (fi_tran->tran_cmd) {
30508                         case SD_FLTINJ_CMD_BUSY:
30509                                 pktp->pkt_flags |= FLAG_PKT_BUSY;
30510                                 break;
30511                         case SD_FLTINJ_CMD_TIMEOUT:
30512                                 pktp->pkt_flags |= FLAG_PKT_TIMEOUT;
30513                                 break;
30514                         default:
30515                                 return;
30516                 }
30517         }
30518         /*
30519          * We don't deallocate any data here - it will be deallocated after
30520          * the packet has been processed by the HBA.
30521          */
30522 }
30523 
30524 
30525 /*
30526  *    Function: sd_faultinjection()
30527  *
30528  * Description: This routine takes the pkt and changes its
30529  *              content based on error injection scenerio.
30530  *
30531  *   Arguments: pktp    - packet to be changed
30532  */
30533 
30534 static void
30535 sd_faultinjection(struct scsi_pkt *pktp)
30536 {
30537         uint_t i;
30538         struct sd_fi_pkt *fi_pkt;
30539         struct sd_fi_xb *fi_xb;
30540         struct sd_fi_un *fi_un;
30541         struct sd_fi_arq *fi_arq;
30542         struct buf *bp;
30543         struct sd_xbuf *xb;
30544         struct sd_lun *un;
30545 
30546         ASSERT(pktp != NULL);
30547 
30548         /* pull bp xb and un from pktp */
30549         bp = (struct buf *)pktp->pkt_private;
30550         xb = SD_GET_XBUF(bp);
30551         un = SD_GET_UN(bp);
30552 
30553         ASSERT(un != NULL);
30554 
30555         mutex_enter(SD_MUTEX(un));
30556 
30557         SD_TRACE(SD_LOG_SDTEST, un,
30558             "sd_faultinjection: entry Injection from sdintr\n");
30559 
30560         /* if injection is off return */
30561         if (sd_fault_injection_on == 0 ||
30562             un->sd_fi_fifo_start == un->sd_fi_fifo_end) {
30563                 mutex_exit(SD_MUTEX(un));
30564                 return;
30565         }
30566 
30567         SD_INFO(SD_LOG_SDTEST, un,
30568             "sd_faultinjection: is working for copying\n");
30569 
30570         /* take next set off fifo */
30571         i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR;
30572 
30573         fi_pkt = un->sd_fi_fifo_pkt[i];
30574         fi_xb = un->sd_fi_fifo_xb[i];
30575         fi_un = un->sd_fi_fifo_un[i];
30576         fi_arq = un->sd_fi_fifo_arq[i];
30577 
30578 
30579         /* set variables accordingly */
30580         /* set pkt if it was on fifo */
30581         if (fi_pkt != NULL) {
30582                 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags");
30583                 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp");
30584                 if (fi_pkt->pkt_cdbp != 0xff)
30585                         SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp");
30586                 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state");
30587                 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics");
30588                 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason");
30589 
30590         }
30591         /* set xb if it was on fifo */
30592         if (fi_xb != NULL) {
30593                 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno");
30594                 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid");
30595                 if (fi_xb->xb_retry_count != 0)
30596                         SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count");
30597                 SD_CONDSET(xb, xb, xb_victim_retry_count,
30598                     "xb_victim_retry_count");
30599                 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status");
30600                 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state");
30601                 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid");
30602 
30603                 /* copy in block data from sense */
30604                 /*
30605                  * if (fi_xb->xb_sense_data[0] != -1) {
30606                  *      bcopy(fi_xb->xb_sense_data, xb->xb_sense_data,
30607                  *      SENSE_LENGTH);
30608                  * }
30609                  */
30610                 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, SENSE_LENGTH);
30611 
30612                 /* copy in extended sense codes */
30613                 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30614                     xb, es_code, "es_code");
30615                 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30616                     xb, es_key, "es_key");
30617                 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30618                     xb, es_add_code, "es_add_code");
30619                 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30620                     xb, es_qual_code, "es_qual_code");
30621                 struct scsi_extended_sense *esp;
30622                 esp = (struct scsi_extended_sense *)xb->xb_sense_data;
30623                 esp->es_class = CLASS_EXTENDED_SENSE;
30624         }
30625 
30626         /* set un if it was on fifo */
30627         if (fi_un != NULL) {
30628                 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb");
30629                 SD_CONDSET(un, un, un_ctype, "un_ctype");
30630                 SD_CONDSET(un, un, un_reset_retry_count,
30631                     "un_reset_retry_count");
30632                 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type");
30633                 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status");
30634                 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled");
30635                 SD_CONDSET(un, un, un_f_allow_bus_device_reset,
30636                     "un_f_allow_bus_device_reset");
30637                 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing");
30638 
30639         }
30640 
30641         /* copy in auto request sense if it was on fifo */
30642         if (fi_arq != NULL) {
30643                 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq));
30644         }
30645 
30646         /* free structs */
30647         if (un->sd_fi_fifo_pkt[i] != NULL) {
30648                 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt));
30649         }
30650         if (un->sd_fi_fifo_xb[i] != NULL) {
30651                 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb));
30652         }
30653         if (un->sd_fi_fifo_un[i] != NULL) {
30654                 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un));
30655         }
30656         if (un->sd_fi_fifo_arq[i] != NULL) {
30657                 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq));
30658         }
30659 
30660         /*
30661          * kmem_free does not gurantee to set to NULL
30662          * since we uses these to determine if we set
30663          * values or not lets confirm they are always
30664          * NULL after free
30665          */
30666         un->sd_fi_fifo_pkt[i] = NULL;
30667         un->sd_fi_fifo_un[i] = NULL;
30668         un->sd_fi_fifo_xb[i] = NULL;
30669         un->sd_fi_fifo_arq[i] = NULL;
30670 
30671         un->sd_fi_fifo_start++;
30672 
30673         mutex_exit(SD_MUTEX(un));
30674 
30675         SD_INFO(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n");
30676 }
30677 
30678 #endif /* SD_FAULT_INJECTION */
30679 
30680 /*
30681  * This routine is invoked in sd_unit_attach(). Before calling it, the
30682  * properties in conf file should be processed already, and "hotpluggable"
30683  * property was processed also.
30684  *
30685  * The sd driver distinguishes 3 different type of devices: removable media,
30686  * non-removable media, and hotpluggable. Below the differences are defined:
30687  *
30688  * 1. Device ID
30689  *
30690  *     The device ID of a device is used to identify this device. Refer to
30691  *     ddi_devid_register(9F).
30692  *
30693  *     For a non-removable media disk device which can provide 0x80 or 0x83
30694  *     VPD page (refer to INQUIRY command of SCSI SPC specification), a unique
30695  *     device ID is created to identify this device. For other non-removable
30696  *     media devices, a default device ID is created only if this device has
30697  *     at least 2 alter cylinders. Otherwise, this device has no devid.
30698  *
30699  *     -------------------------------------------------------
30700  *     removable media   hotpluggable  | Can Have Device ID
30701  *     -------------------------------------------------------
30702  *         false             false     |     Yes
30703  *         false             true      |     Yes
30704  *         true                x       |     No
30705  *     ------------------------------------------------------
30706  *
30707  *
30708  * 2. SCSI group 4 commands
30709  *
30710  *     In SCSI specs, only some commands in group 4 command set can use
30711  *     8-byte addresses that can be used to access >2TB storage spaces.
30712  *     Other commands have no such capability. Without supporting group4,
30713  *     it is impossible to make full use of storage spaces of a disk with
30714  *     capacity larger than 2TB.
30715  *
30716  *     -----------------------------------------------
30717  *     removable media   hotpluggable   LP64  |  Group
30718  *     -----------------------------------------------
30719  *           false          false       false |   1
30720  *           false          false       true  |   4
30721  *           false          true        false |   1
30722  *           false          true        true  |   4
30723  *           true             x           x   |   5
30724  *     -----------------------------------------------
30725  *
30726  *
30727  * 3. Check for VTOC Label
30728  *
30729  *     If a direct-access disk has no EFI label, sd will check if it has a
30730  *     valid VTOC label. Now, sd also does that check for removable media
30731  *     and hotpluggable devices.
30732  *
30733  *     --------------------------------------------------------------
30734  *     Direct-Access   removable media    hotpluggable |  Check Label
30735  *     -------------------------------------------------------------
30736  *         false          false           false        |   No
30737  *         false          false           true         |   No
30738  *         false          true            false        |   Yes
30739  *         false          true            true         |   Yes
30740  *         true            x                x          |   Yes
30741  *     --------------------------------------------------------------
30742  *
30743  *
30744  * 4. Building default VTOC label
30745  *
30746  *     As section 3 says, sd checks if some kinds of devices have VTOC label.
30747  *     If those devices have no valid VTOC label, sd(7d) will attempt to
30748  *     create default VTOC for them. Currently sd creates default VTOC label
30749  *     for all devices on x86 platform (VTOC_16).
30750  *
30751  *     -----------------------------------------------------------
30752  *       removable media hotpluggable platform   |   Default Label
30753  *     -----------------------------------------------------------
30754  *             false          true      x86      |     Yes
30755  *             true             x        x       |     Yes
30756  *     ----------------------------------------------------------
30757  *
30758  *
30759  * 5. Supported blocksizes of target devices
30760  *
30761  *     Sd supports non-512-byte blocksize for removable media devices only.
30762  *     For other devices, only 512-byte blocksize is supported. This may be
30763  *     changed in near future because some RAID devices require non-512-byte
30764  *     blocksize
30765  *
30766  *     -----------------------------------------------------------
30767  *     removable media    hotpluggable    | non-512-byte blocksize
30768  *     -----------------------------------------------------------
30769  *           false          false         |   No
30770  *           false          true          |   No
30771  *           true             x           |   Yes
30772  *     -----------------------------------------------------------
30773  *
30774  *
30775  * 6. Automatic mount & unmount
30776  *
30777  *     Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query
30778  *     if a device is removable media device. It return 1 for removable media
30779  *     devices, and 0 for others.
30780  *
30781  *     The automatic mounting subsystem should distinguish between the types
30782  *     of devices and apply automounting policies to each.
30783  *
30784  *
30785  * 7. fdisk partition management
30786  *
30787  *     Fdisk is traditional partition method on x86 platform. sd(7D) driver
30788  *     just supports fdisk partitions on x86 platform.
30789  *
30790  *     -----------------------------------------------------------
30791  *       platform   removable media  USB/1394  |  fdisk supported
30792  *     -----------------------------------------------------------
30793  *        x86         X               X        |       true
30794  *     -----------------------------------------------------------
30795  *
30796  *
30797  * 8. MBOOT/MBR
30798  *
30799  *     -----------------------------------------------------------
30800  *       platform   removable media  USB/1394  |  mboot supported
30801  *     -----------------------------------------------------------
30802  *        x86         X               X        |       true
30803  *     -----------------------------------------------------------
30804  *
30805  *
30806  * 9.  error handling during opening device
30807  *
30808  *     If failed to open a disk device, an errno is returned. For some kinds
30809  *     of errors, different errno is returned depending on if this device is
30810  *     a removable media device. This brings USB/1394 hard disks in line with
30811  *     expected hard disk behavior. It is not expected that this breaks any
30812  *     application.
30813  *
30814  *     ------------------------------------------------------
30815  *       removable media    hotpluggable   |  errno
30816  *     ------------------------------------------------------
30817  *             false          false        |   EIO
30818  *             false          true         |   EIO
30819  *             true             x          |   ENXIO
30820  *     ------------------------------------------------------
30821  *
30822  *
30823  * 11. ioctls: DKIOCEJECT, CDROMEJECT
30824  *
30825  *     These IOCTLs are applicable only to removable media devices.
30826  *
30827  *     -----------------------------------------------------------
30828  *       removable media    hotpluggable   |DKIOCEJECT, CDROMEJECT
30829  *     -----------------------------------------------------------
30830  *             false          false        |     No
30831  *             false          true         |     No
30832  *             true            x           |     Yes
30833  *     -----------------------------------------------------------
30834  *
30835  *
30836  * 12. Kstats for partitions
30837  *
30838  *     sd creates partition kstat for non-removable media devices. USB and
30839  *     Firewire hard disks now have partition kstats
30840  *
30841  *      ------------------------------------------------------
30842  *       removable media    hotpluggable   |   kstat
30843  *      ------------------------------------------------------
30844  *             false          false        |    Yes
30845  *             false          true         |    Yes
30846  *             true             x          |    No
30847  *       ------------------------------------------------------
30848  *
30849  *
30850  * 13. Removable media & hotpluggable properties
30851  *
30852  *     Sd driver creates a "removable-media" property for removable media
30853  *     devices. Parent nexus drivers create a "hotpluggable" property if
30854  *     it supports hotplugging.
30855  *
30856  *     ---------------------------------------------------------------------
30857  *     removable media   hotpluggable |  "removable-media"   " hotpluggable"
30858  *     ---------------------------------------------------------------------
30859  *       false            false       |    No                   No
30860  *       false            true        |    No                   Yes
30861  *       true             false       |    Yes                  No
30862  *       true             true        |    Yes                  Yes
30863  *     ---------------------------------------------------------------------
30864  *
30865  *
30866  * 14. Power Management
30867  *
30868  *     sd only power manages removable media devices or devices that support
30869  *     LOG_SENSE or have a "pm-capable" property  (PSARC/2002/250)
30870  *
30871  *     A parent nexus that supports hotplugging can also set "pm-capable"
30872  *     if the disk can be power managed.
30873  *
30874  *     ------------------------------------------------------------
30875  *       removable media hotpluggable pm-capable  |   power manage
30876  *     ------------------------------------------------------------
30877  *             false          false     false     |     No
30878  *             false          false     true      |     Yes
30879  *             false          true      false     |     No
30880  *             false          true      true      |     Yes
30881  *             true             x        x        |     Yes
30882  *     ------------------------------------------------------------
30883  *
30884  *      USB and firewire hard disks can now be power managed independently
30885  *      of the framebuffer
30886  *
30887  *
30888  * 15. Support for USB disks with capacity larger than 1TB
30889  *
30890  *     Currently, sd doesn't permit a fixed disk device with capacity
30891  *     larger than 1TB to be used in a 32-bit operating system environment.
30892  *     However, sd doesn't do that for removable media devices. Instead, it
30893  *     assumes that removable media devices cannot have a capacity larger
30894  *     than 1TB. Therefore, using those devices on 32-bit system is partially
30895  *     supported, which can cause some unexpected results.
30896  *
30897  *     ---------------------------------------------------------------------
30898  *       removable media    USB/1394 | Capacity > 1TB |   Used in 32-bit env
30899  *     ---------------------------------------------------------------------
30900  *             false          false  |   true         |     no
30901  *             false          true   |   true         |     no
30902  *             true           false  |   true         |     Yes
30903  *             true           true   |   true         |     Yes
30904  *     ---------------------------------------------------------------------
30905  *
30906  *
30907  * 16. Check write-protection at open time
30908  *
30909  *     When a removable media device is being opened for writing without NDELAY
30910  *     flag, sd will check if this device is writable. If attempting to open
30911  *     without NDELAY flag a write-protected device, this operation will abort.
30912  *
30913  *     ------------------------------------------------------------
30914  *       removable media    USB/1394   |   WP Check
30915  *     ------------------------------------------------------------
30916  *             false          false    |     No
30917  *             false          true     |     No
30918  *             true           false    |     Yes
30919  *             true           true     |     Yes
30920  *     ------------------------------------------------------------
30921  *
30922  *
30923  * 17. syslog when corrupted VTOC is encountered
30924  *
30925  *      Currently, if an invalid VTOC is encountered, sd only print syslog
30926  *      for fixed SCSI disks.
30927  *     ------------------------------------------------------------
30928  *       removable media    USB/1394   |   print syslog
30929  *     ------------------------------------------------------------
30930  *             false          false    |     Yes
30931  *             false          true     |     No
30932  *             true           false    |     No
30933  *             true           true     |     No
30934  *     ------------------------------------------------------------
30935  */
30936 static void
30937 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi)
30938 {
30939         int     pm_cap;
30940 
30941         ASSERT(un->un_sd);
30942         ASSERT(un->un_sd->sd_inq);
30943 
30944         /*
30945          * Enable SYNC CACHE support for all devices.
30946          */
30947         un->un_f_sync_cache_supported = TRUE;
30948 
30949         /*
30950          * Set the sync cache required flag to false.
30951          * This would ensure that there is no SYNC CACHE
30952          * sent when there are no writes
30953          */
30954         un->un_f_sync_cache_required = FALSE;
30955 
30956         if (un->un_sd->sd_inq->inq_rmb) {
30957                 /*
30958                  * The media of this device is removable. And for this kind
30959                  * of devices, it is possible to change medium after opening
30960                  * devices. Thus we should support this operation.
30961                  */
30962                 un->un_f_has_removable_media = TRUE;
30963 
30964                 /*
30965                  * support non-512-byte blocksize of removable media devices
30966                  */
30967                 un->un_f_non_devbsize_supported = TRUE;
30968 
30969                 /*
30970                  * Assume that all removable media devices support DOOR_LOCK
30971                  */
30972                 un->un_f_doorlock_supported = TRUE;
30973 
30974                 /*
30975                  * For a removable media device, it is possible to be opened
30976                  * with NDELAY flag when there is no media in drive, in this
30977                  * case we don't care if device is writable. But if without
30978                  * NDELAY flag, we need to check if media is write-protected.
30979                  */
30980                 un->un_f_chk_wp_open = TRUE;
30981 
30982                 /*
30983                  * need to start a SCSI watch thread to monitor media state,
30984                  * when media is being inserted or ejected, notify syseventd.
30985                  */
30986                 un->un_f_monitor_media_state = TRUE;
30987 
30988                 /*
30989                  * Some devices don't support START_STOP_UNIT command.
30990                  * Therefore, we'd better check if a device supports it
30991                  * before sending it.
30992                  */
30993                 un->un_f_check_start_stop = TRUE;
30994 
30995                 /*
30996                  * support eject media ioctl:
30997                  *              FDEJECT, DKIOCEJECT, CDROMEJECT
30998                  */
30999                 un->un_f_eject_media_supported = TRUE;
31000 
31001                 /*
31002                  * Because many removable-media devices don't support
31003                  * LOG_SENSE, we couldn't use this command to check if
31004                  * a removable media device support power-management.
31005                  * We assume that they support power-management via
31006                  * START_STOP_UNIT command and can be spun up and down
31007                  * without limitations.
31008                  */
31009                 un->un_f_pm_supported = TRUE;
31010 
31011                 /*
31012                  * Need to create a zero length (Boolean) property
31013                  * removable-media for the removable media devices.
31014                  * Note that the return value of the property is not being
31015                  * checked, since if unable to create the property
31016                  * then do not want the attach to fail altogether. Consistent
31017                  * with other property creation in attach.
31018                  */
31019                 (void) ddi_prop_create(DDI_DEV_T_NONE, devi,
31020                     DDI_PROP_CANSLEEP, "removable-media", NULL, 0);
31021 
31022         } else {
31023                 /*
31024                  * create device ID for device
31025                  */
31026                 un->un_f_devid_supported = TRUE;
31027 
31028                 /*
31029                  * Spin up non-removable-media devices once it is attached
31030                  */
31031                 un->un_f_attach_spinup = TRUE;
31032 
31033                 /*
31034                  * According to SCSI specification, Sense data has two kinds of
31035                  * format: fixed format, and descriptor format. At present, we
31036                  * don't support descriptor format sense data for removable
31037                  * media.
31038                  */
31039                 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) {
31040                         un->un_f_descr_format_supported = TRUE;
31041                 }
31042 
31043                 /*
31044                  * kstats are created only for non-removable media devices.
31045                  *
31046                  * Set this in sd.conf to 0 in order to disable kstats.  The
31047                  * default is 1, so they are enabled by default.
31048                  */
31049                 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY,
31050                     SD_DEVINFO(un), DDI_PROP_DONTPASS,
31051                     "enable-partition-kstats", 1));
31052 
31053                 /*
31054                  * Check if HBA has set the "pm-capable" property.
31055                  * If "pm-capable" exists and is non-zero then we can
31056                  * power manage the device without checking the start/stop
31057                  * cycle count log sense page.
31058                  *
31059                  * If "pm-capable" exists and is set to be false (0),
31060                  * then we should not power manage the device.
31061                  *
31062                  * If "pm-capable" doesn't exist then pm_cap will
31063                  * be set to SD_PM_CAPABLE_UNDEFINED (-1).  In this case,
31064                  * sd will check the start/stop cycle count log sense page
31065                  * and power manage the device if the cycle count limit has
31066                  * not been exceeded.
31067                  */
31068                 pm_cap = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
31069                     DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED);
31070                 if (SD_PM_CAPABLE_IS_UNDEFINED(pm_cap)) {
31071                         un->un_f_log_sense_supported = TRUE;
31072                         if (!un->un_f_power_condition_disabled &&
31073                             SD_INQUIRY(un)->inq_ansi == 6) {
31074                                 un->un_f_power_condition_supported = TRUE;
31075                         }
31076                 } else {
31077                         /*
31078                          * pm-capable property exists.
31079                          *
31080                          * Convert "TRUE" values for pm_cap to
31081                          * SD_PM_CAPABLE_IS_TRUE to make it easier to check
31082                          * later. "TRUE" values are any values defined in
31083                          * inquiry.h.
31084                          */
31085                         if (SD_PM_CAPABLE_IS_FALSE(pm_cap)) {
31086                                 un->un_f_log_sense_supported = FALSE;
31087                         } else {
31088                                 /* SD_PM_CAPABLE_IS_TRUE case */
31089                                 un->un_f_pm_supported = TRUE;
31090                                 if (!un->un_f_power_condition_disabled &&
31091                                     SD_PM_CAPABLE_IS_SPC_4(pm_cap)) {
31092                                         un->un_f_power_condition_supported =
31093                                             TRUE;
31094                                 }
31095                                 if (SD_PM_CAP_LOG_SUPPORTED(pm_cap)) {
31096                                         un->un_f_log_sense_supported = TRUE;
31097                                         un->un_f_pm_log_sense_smart =
31098                                             SD_PM_CAP_SMART_LOG(pm_cap);
31099                                 }
31100                         }
31101 
31102                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
31103                             "sd_unit_attach: un:0x%p pm-capable "
31104                             "property set to %d.\n", un, un->un_f_pm_supported);
31105                 }
31106         }
31107 
31108         if (un->un_f_is_hotpluggable) {
31109 
31110                 /*
31111                  * Have to watch hotpluggable devices as well, since
31112                  * that's the only way for userland applications to
31113                  * detect hot removal while device is busy/mounted.
31114                  */
31115                 un->un_f_monitor_media_state = TRUE;
31116 
31117                 un->un_f_check_start_stop = TRUE;
31118 
31119         }
31120 }
31121 
31122 /*
31123  * sd_tg_rdwr:
31124  * Provides rdwr access for cmlb via sd_tgops. The start_block is
31125  * in sys block size, req_length in bytes.
31126  *
31127  */
31128 static int
31129 sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
31130     diskaddr_t start_block, size_t reqlength, void *tg_cookie)
31131 {
31132         struct sd_lun *un;
31133         int path_flag = (int)(uintptr_t)tg_cookie;
31134         char *dkl = NULL;
31135         diskaddr_t real_addr = start_block;
31136         diskaddr_t first_byte, end_block;
31137 
31138         size_t  buffer_size = reqlength;
31139         int rval = 0;
31140         diskaddr_t      cap;
31141         uint32_t        lbasize;
31142         sd_ssc_t        *ssc;
31143 
31144         un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
31145         if (un == NULL)
31146                 return (ENXIO);
31147 
31148         if (cmd != TG_READ && cmd != TG_WRITE)
31149                 return (EINVAL);
31150 
31151         ssc = sd_ssc_init(un);
31152         mutex_enter(SD_MUTEX(un));
31153         if (un->un_f_tgt_blocksize_is_valid == FALSE) {
31154                 mutex_exit(SD_MUTEX(un));
31155                 rval = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
31156                     &lbasize, path_flag);
31157                 if (rval != 0)
31158                         goto done1;
31159                 mutex_enter(SD_MUTEX(un));
31160                 sd_update_block_info(un, lbasize, cap);
31161                 if ((un->un_f_tgt_blocksize_is_valid == FALSE)) {
31162                         mutex_exit(SD_MUTEX(un));
31163                         rval = EIO;
31164                         goto done;
31165                 }
31166         }
31167 
31168         if (NOT_DEVBSIZE(un)) {
31169                 /*
31170                  * sys_blocksize != tgt_blocksize, need to re-adjust
31171                  * blkno and save the index to beginning of dk_label
31172                  */
31173                 first_byte  = SD_SYSBLOCKS2BYTES(start_block);
31174                 real_addr = first_byte / un->un_tgt_blocksize;
31175 
31176                 end_block = (first_byte + reqlength +
31177                     un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
31178 
31179                 /* round up buffer size to multiple of target block size */
31180                 buffer_size = (end_block - real_addr) * un->un_tgt_blocksize;
31181 
31182                 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_tg_rdwr",
31183                     "label_addr: 0x%x allocation size: 0x%x\n",
31184                     real_addr, buffer_size);
31185 
31186                 if (((first_byte % un->un_tgt_blocksize) != 0) ||
31187                     (reqlength % un->un_tgt_blocksize) != 0)
31188                         /* the request is not aligned */
31189                         dkl = kmem_zalloc(buffer_size, KM_SLEEP);
31190         }
31191 
31192         /*
31193          * The MMC standard allows READ CAPACITY to be
31194          * inaccurate by a bounded amount (in the interest of
31195          * response latency).  As a result, failed READs are
31196          * commonplace (due to the reading of metadata and not
31197          * data). Depending on the per-Vendor/drive Sense data,
31198          * the failed READ can cause many (unnecessary) retries.
31199          */
31200 
31201         if (ISCD(un) && (cmd == TG_READ) &&
31202             (un->un_f_blockcount_is_valid == TRUE) &&
31203             ((start_block == (un->un_blockcount - 1))||
31204             (start_block == (un->un_blockcount - 2)))) {
31205                         path_flag = SD_PATH_DIRECT_PRIORITY;
31206         }
31207 
31208         mutex_exit(SD_MUTEX(un));
31209         if (cmd == TG_READ) {
31210                 rval = sd_send_scsi_READ(ssc, (dkl != NULL)? dkl: bufaddr,
31211                     buffer_size, real_addr, path_flag);
31212                 if (dkl != NULL)
31213                         bcopy(dkl + SD_TGTBYTEOFFSET(un, start_block,
31214                             real_addr), bufaddr, reqlength);
31215         } else {
31216                 if (dkl) {
31217                         rval = sd_send_scsi_READ(ssc, dkl, buffer_size,
31218                             real_addr, path_flag);
31219                         if (rval) {
31220                                 goto done1;
31221                         }
31222                         bcopy(bufaddr, dkl + SD_TGTBYTEOFFSET(un, start_block,
31223                             real_addr), reqlength);
31224                 }
31225                 rval = sd_send_scsi_WRITE(ssc, (dkl != NULL)? dkl: bufaddr,
31226                     buffer_size, real_addr, path_flag);
31227         }
31228 
31229 done1:
31230         if (dkl != NULL)
31231                 kmem_free(dkl, buffer_size);
31232 
31233         if (rval != 0) {
31234                 if (rval == EIO)
31235                         sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
31236                 else
31237                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
31238         }
31239 done:
31240         sd_ssc_fini(ssc);
31241         return (rval);
31242 }
31243 
31244 
31245 static int
31246 sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie)
31247 {
31248 
31249         struct sd_lun *un;
31250         diskaddr_t      cap;
31251         uint32_t        lbasize;
31252         int             path_flag = (int)(uintptr_t)tg_cookie;
31253         int             ret = 0;
31254 
31255         un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
31256         if (un == NULL)
31257                 return (ENXIO);
31258 
31259         switch (cmd) {
31260         case TG_GETPHYGEOM:
31261         case TG_GETVIRTGEOM:
31262         case TG_GETCAPACITY:
31263         case TG_GETBLOCKSIZE:
31264                 mutex_enter(SD_MUTEX(un));
31265 
31266                 if ((un->un_f_blockcount_is_valid == TRUE) &&
31267                     (un->un_f_tgt_blocksize_is_valid == TRUE)) {
31268                         cap = un->un_blockcount;
31269                         lbasize = un->un_tgt_blocksize;
31270                         mutex_exit(SD_MUTEX(un));
31271                 } else {
31272                         sd_ssc_t        *ssc;
31273                         mutex_exit(SD_MUTEX(un));
31274                         ssc = sd_ssc_init(un);
31275                         ret = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
31276                             &lbasize, path_flag);
31277                         if (ret != 0) {
31278                                 if (ret == EIO)
31279                                         sd_ssc_assessment(ssc,
31280                                             SD_FMT_STATUS_CHECK);
31281                                 else
31282                                         sd_ssc_assessment(ssc,
31283                                             SD_FMT_IGNORE);
31284                                 sd_ssc_fini(ssc);
31285                                 return (ret);
31286                         }
31287                         sd_ssc_fini(ssc);
31288                         mutex_enter(SD_MUTEX(un));
31289                         sd_update_block_info(un, lbasize, cap);
31290                         if ((un->un_f_blockcount_is_valid == FALSE) ||
31291                             (un->un_f_tgt_blocksize_is_valid == FALSE)) {
31292                                 mutex_exit(SD_MUTEX(un));
31293                                 return (EIO);
31294                         }
31295                         mutex_exit(SD_MUTEX(un));
31296                 }
31297 
31298                 if (cmd == TG_GETCAPACITY) {
31299                         *(diskaddr_t *)arg = cap;
31300                         return (0);
31301                 }
31302 
31303                 if (cmd == TG_GETBLOCKSIZE) {
31304                         *(uint32_t *)arg = lbasize;
31305                         return (0);
31306                 }
31307 
31308                 if (cmd == TG_GETPHYGEOM)
31309                         ret = sd_get_physical_geometry(un, (cmlb_geom_t *)arg,
31310                             cap, lbasize, path_flag);
31311                 else
31312                         /* TG_GETVIRTGEOM */
31313                         ret = sd_get_virtual_geometry(un,
31314                             (cmlb_geom_t *)arg, cap, lbasize);
31315 
31316                 return (ret);
31317 
31318         case TG_GETATTR:
31319                 mutex_enter(SD_MUTEX(un));
31320                 ((tg_attribute_t *)arg)->media_is_writable =
31321                     un->un_f_mmc_writable_media;
31322                 ((tg_attribute_t *)arg)->media_is_solid_state =
31323                     un->un_f_is_solid_state;
31324                 ((tg_attribute_t *)arg)->media_is_rotational =
31325                     un->un_f_is_rotational;
31326                 mutex_exit(SD_MUTEX(un));
31327                 return (0);
31328         default:
31329                 return (ENOTTY);
31330 
31331         }
31332 }
31333 
31334 /*
31335  *    Function: sd_ssc_ereport_post
31336  *
31337  * Description: Will be called when SD driver need to post an ereport.
31338  *
31339  *    Context: Kernel thread or interrupt context.
31340  */
31341 static void
31342 sd_ssc_ereport_post(sd_ssc_t *ssc, enum sd_driver_assessment drv_assess)
31343 {
31344         uchar_t uscsi_pkt_reason;
31345         uint32_t uscsi_pkt_state;
31346         uint32_t uscsi_pkt_statistics;
31347         uint64_t uscsi_ena;
31348         uchar_t op_code;
31349         uint8_t *sensep;
31350         union scsi_cdb *cdbp;
31351         uint_t cdblen = 0;
31352         uint_t senlen = 0;
31353         struct sd_lun *un;
31354         dev_info_t *dip;
31355         char *devid;
31356         int ssc_invalid_flags = SSC_FLAGS_INVALID_PKT_REASON |
31357             SSC_FLAGS_INVALID_STATUS |
31358             SSC_FLAGS_INVALID_SENSE |
31359             SSC_FLAGS_INVALID_DATA;
31360         char assessment[16];
31361 
31362         ASSERT(ssc != NULL);
31363         ASSERT(ssc->ssc_uscsi_cmd != NULL);
31364         ASSERT(ssc->ssc_uscsi_info != NULL);
31365 
31366         un = ssc->ssc_un;
31367         ASSERT(un != NULL);
31368 
31369         dip = un->un_sd->sd_dev;
31370 
31371         /*
31372          * Get the devid:
31373          *      devid will only be passed to non-transport error reports.
31374          */
31375         devid = DEVI(dip)->devi_devid_str;
31376 
31377         /*
31378          * If we are syncing or dumping, the command will not be executed
31379          * so we bypass this situation.
31380          */
31381         if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
31382             (un->un_state == SD_STATE_DUMPING))
31383                 return;
31384 
31385         uscsi_pkt_reason = ssc->ssc_uscsi_info->ui_pkt_reason;
31386         uscsi_pkt_state = ssc->ssc_uscsi_info->ui_pkt_state;
31387         uscsi_pkt_statistics = ssc->ssc_uscsi_info->ui_pkt_statistics;
31388         uscsi_ena = ssc->ssc_uscsi_info->ui_ena;
31389 
31390         sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
31391         cdbp = (union scsi_cdb *)ssc->ssc_uscsi_cmd->uscsi_cdb;
31392 
31393         /* In rare cases, EG:DOORLOCK, the cdb could be NULL */
31394         if (cdbp == NULL) {
31395                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
31396                     "sd_ssc_ereport_post meet empty cdb\n");
31397                 return;
31398         }
31399 
31400         op_code = cdbp->scc_cmd;
31401 
31402         cdblen = (int)ssc->ssc_uscsi_cmd->uscsi_cdblen;
31403         senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
31404             ssc->ssc_uscsi_cmd->uscsi_rqresid);
31405 
31406         if (senlen > 0)
31407                 ASSERT(sensep != NULL);
31408 
31409         /*
31410          * Initialize drv_assess to corresponding values.
31411          * SD_FM_DRV_FATAL will be mapped to "fail" or "fatal" depending
31412          * on the sense-key returned back.
31413          */
31414         switch (drv_assess) {
31415                 case SD_FM_DRV_RECOVERY:
31416                         (void) sprintf(assessment, "%s", "recovered");
31417                         break;
31418                 case SD_FM_DRV_RETRY:
31419                         (void) sprintf(assessment, "%s", "retry");
31420                         break;
31421                 case SD_FM_DRV_NOTICE:
31422                         (void) sprintf(assessment, "%s", "info");
31423                         break;
31424                 case SD_FM_DRV_FATAL:
31425                 default:
31426                         (void) sprintf(assessment, "%s", "unknown");
31427         }
31428         /*
31429          * If drv_assess == SD_FM_DRV_RECOVERY, this should be a recovered
31430          * command, we will post ereport.io.scsi.cmd.disk.recovered.
31431          * driver-assessment will always be "recovered" here.
31432          */
31433         if (drv_assess == SD_FM_DRV_RECOVERY) {
31434                 scsi_fm_ereport_post(un->un_sd, 0, NULL,
31435                     "cmd.disk.recovered", uscsi_ena, devid, NULL,
31436                     DDI_NOSLEEP, NULL,
31437                     FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31438                     DEVID_IF_KNOWN(devid),
31439                     "driver-assessment", DATA_TYPE_STRING, assessment,
31440                     "op-code", DATA_TYPE_UINT8, op_code,
31441                     "cdb", DATA_TYPE_UINT8_ARRAY,
31442                     cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31443                     "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31444                     "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31445                     "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31446                     NULL);
31447                 return;
31448         }
31449 
31450         /*
31451          * If there is un-expected/un-decodable data, we should post
31452          * ereport.io.scsi.cmd.disk.dev.uderr.
31453          * driver-assessment will be set based on parameter drv_assess.
31454          * SSC_FLAGS_INVALID_SENSE - invalid sense data sent back.
31455          * SSC_FLAGS_INVALID_PKT_REASON - invalid pkt-reason encountered.
31456          * SSC_FLAGS_INVALID_STATUS - invalid stat-code encountered.
31457          * SSC_FLAGS_INVALID_DATA - invalid data sent back.
31458          */
31459         if (ssc->ssc_flags & ssc_invalid_flags) {
31460                 if (ssc->ssc_flags & SSC_FLAGS_INVALID_SENSE) {
31461                         scsi_fm_ereport_post(un->un_sd, 0,
31462                             NULL, "cmd.disk.dev.uderr", uscsi_ena, devid,
31463                             NULL, DDI_NOSLEEP, NULL,
31464                             FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31465                             DEVID_IF_KNOWN(devid),
31466                             "driver-assessment", DATA_TYPE_STRING,
31467                             drv_assess == SD_FM_DRV_FATAL ?
31468                             "fail" : assessment,
31469                             "op-code", DATA_TYPE_UINT8, op_code,
31470                             "cdb", DATA_TYPE_UINT8_ARRAY,
31471                             cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31472                             "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31473                             "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31474                             "pkt-stats", DATA_TYPE_UINT32,
31475                             uscsi_pkt_statistics,
31476                             "stat-code", DATA_TYPE_UINT8,
31477                             ssc->ssc_uscsi_cmd->uscsi_status,
31478                             "un-decode-info", DATA_TYPE_STRING,
31479                             ssc->ssc_info,
31480                             "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31481                             senlen, sensep,
31482                             NULL);
31483                 } else {
31484                         /*
31485                          * For other type of invalid data, the
31486                          * un-decode-value field would be empty because the
31487                          * un-decodable content could be seen from upper
31488                          * level payload or inside un-decode-info.
31489                          */
31490                         scsi_fm_ereport_post(un->un_sd, 0,
31491                             NULL,
31492                             "cmd.disk.dev.uderr", uscsi_ena, devid,
31493                             NULL, DDI_NOSLEEP, NULL,
31494                             FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31495                             DEVID_IF_KNOWN(devid),
31496                             "driver-assessment", DATA_TYPE_STRING,
31497                             drv_assess == SD_FM_DRV_FATAL ?
31498                             "fail" : assessment,
31499                             "op-code", DATA_TYPE_UINT8, op_code,
31500                             "cdb", DATA_TYPE_UINT8_ARRAY,
31501                             cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31502                             "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31503                             "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31504                             "pkt-stats", DATA_TYPE_UINT32,
31505                             uscsi_pkt_statistics,
31506                             "stat-code", DATA_TYPE_UINT8,
31507                             ssc->ssc_uscsi_cmd->uscsi_status,
31508                             "un-decode-info", DATA_TYPE_STRING,
31509                             ssc->ssc_info,
31510                             "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31511                             0, NULL,
31512                             NULL);
31513                 }
31514                 ssc->ssc_flags &= ~ssc_invalid_flags;
31515                 return;
31516         }
31517 
31518         if (uscsi_pkt_reason != CMD_CMPLT ||
31519             (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)) {
31520                 /*
31521                  * pkt-reason != CMD_CMPLT or SSC_FLAGS_TRAN_ABORT was
31522                  * set inside sd_start_cmds due to errors(bad packet or
31523                  * fatal transport error), we should take it as a
31524                  * transport error, so we post ereport.io.scsi.cmd.disk.tran.
31525                  * driver-assessment will be set based on drv_assess.
31526                  * We will set devid to NULL because it is a transport
31527                  * error.
31528                  */
31529                 if (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)
31530                         ssc->ssc_flags &= ~SSC_FLAGS_TRAN_ABORT;
31531 
31532                 scsi_fm_ereport_post(un->un_sd, 0, NULL,
31533                     "cmd.disk.tran", uscsi_ena, NULL, NULL, DDI_NOSLEEP, NULL,
31534                     FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31535                     DEVID_IF_KNOWN(devid),
31536                     "driver-assessment", DATA_TYPE_STRING,
31537                     drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31538                     "op-code", DATA_TYPE_UINT8, op_code,
31539                     "cdb", DATA_TYPE_UINT8_ARRAY,
31540                     cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31541                     "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31542                     "pkt-state", DATA_TYPE_UINT8, uscsi_pkt_state,
31543                     "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31544                     NULL);
31545         } else {
31546                 /*
31547                  * If we got here, we have a completed command, and we need
31548                  * to further investigate the sense data to see what kind
31549                  * of ereport we should post.
31550                  * No ereport is needed if sense-key is KEY_RECOVERABLE_ERROR
31551                  * and asc/ascq is "ATA PASS-THROUGH INFORMATION AVAILABLE".
31552                  * Post ereport.io.scsi.cmd.disk.dev.rqs.merr if sense-key is
31553                  * KEY_MEDIUM_ERROR.
31554                  * Post ereport.io.scsi.cmd.disk.dev.rqs.derr otherwise.
31555                  * driver-assessment will be set based on the parameter
31556                  * drv_assess.
31557                  */
31558                 if (senlen > 0) {
31559                         /*
31560                          * Here we have sense data available.
31561                          */
31562                         uint8_t sense_key = scsi_sense_key(sensep);
31563                         uint8_t sense_asc = scsi_sense_asc(sensep);
31564                         uint8_t sense_ascq = scsi_sense_ascq(sensep);
31565 
31566                         if (sense_key == KEY_RECOVERABLE_ERROR &&
31567                             sense_asc == 0x00 && sense_ascq == 0x1d)
31568                                 return;
31569 
31570                         if (sense_key == KEY_MEDIUM_ERROR) {
31571                                 /*
31572                                  * driver-assessment should be "fatal" if
31573                                  * drv_assess is SD_FM_DRV_FATAL.
31574                                  */
31575                                 scsi_fm_ereport_post(un->un_sd,
31576                                     0, NULL,
31577                                     "cmd.disk.dev.rqs.merr",
31578                                     uscsi_ena, devid, NULL, DDI_NOSLEEP, NULL,
31579                                     FM_VERSION, DATA_TYPE_UINT8,
31580                                     FM_EREPORT_VERS0,
31581                                     DEVID_IF_KNOWN(devid),
31582                                     "driver-assessment",
31583                                     DATA_TYPE_STRING,
31584                                     drv_assess == SD_FM_DRV_FATAL ?
31585                                     "fatal" : assessment,
31586                                     "op-code",
31587                                     DATA_TYPE_UINT8, op_code,
31588                                     "cdb",
31589                                     DATA_TYPE_UINT8_ARRAY, cdblen,
31590                                     ssc->ssc_uscsi_cmd->uscsi_cdb,
31591                                     "pkt-reason",
31592                                     DATA_TYPE_UINT8, uscsi_pkt_reason,
31593                                     "pkt-state",
31594                                     DATA_TYPE_UINT8, uscsi_pkt_state,
31595                                     "pkt-stats",
31596                                     DATA_TYPE_UINT32,
31597                                     uscsi_pkt_statistics,
31598                                     "stat-code",
31599                                     DATA_TYPE_UINT8,
31600                                     ssc->ssc_uscsi_cmd->uscsi_status,
31601                                     "key",
31602                                     DATA_TYPE_UINT8,
31603                                     scsi_sense_key(sensep),
31604                                     "asc",
31605                                     DATA_TYPE_UINT8,
31606                                     scsi_sense_asc(sensep),
31607                                     "ascq",
31608                                     DATA_TYPE_UINT8,
31609                                     scsi_sense_ascq(sensep),
31610                                     "sense-data",
31611                                     DATA_TYPE_UINT8_ARRAY,
31612                                     senlen, sensep,
31613                                     "lba",
31614                                     DATA_TYPE_UINT64,
31615                                     ssc->ssc_uscsi_info->ui_lba,
31616                                     NULL);
31617                         } else {
31618                                 /*
31619                                  * if sense-key == 0x4(hardware
31620                                  * error), driver-assessment should
31621                                  * be "fatal" if drv_assess is
31622                                  * SD_FM_DRV_FATAL.
31623                                  */
31624                                 scsi_fm_ereport_post(un->un_sd,
31625                                     0, NULL,
31626                                     "cmd.disk.dev.rqs.derr",
31627                                     uscsi_ena, devid,
31628                                     NULL, DDI_NOSLEEP, NULL,
31629                                     FM_VERSION,
31630                                     DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31631                                     DEVID_IF_KNOWN(devid),
31632                                     "driver-assessment",
31633                                     DATA_TYPE_STRING,
31634                                     drv_assess == SD_FM_DRV_FATAL ?
31635                                     (sense_key == 0x4 ?
31636                                     "fatal" : "fail") : assessment,
31637                                     "op-code",
31638                                     DATA_TYPE_UINT8, op_code,
31639                                     "cdb",
31640                                     DATA_TYPE_UINT8_ARRAY, cdblen,
31641                                     ssc->ssc_uscsi_cmd->uscsi_cdb,
31642                                     "pkt-reason",
31643                                     DATA_TYPE_UINT8, uscsi_pkt_reason,
31644                                     "pkt-state",
31645                                     DATA_TYPE_UINT8, uscsi_pkt_state,
31646                                     "pkt-stats",
31647                                     DATA_TYPE_UINT32,
31648                                     uscsi_pkt_statistics,
31649                                     "stat-code",
31650                                     DATA_TYPE_UINT8,
31651                                     ssc->ssc_uscsi_cmd->uscsi_status,
31652                                     "key",
31653                                     DATA_TYPE_UINT8,
31654                                     scsi_sense_key(sensep),
31655                                     "asc",
31656                                     DATA_TYPE_UINT8,
31657                                     scsi_sense_asc(sensep),
31658                                     "ascq",
31659                                     DATA_TYPE_UINT8,
31660                                     scsi_sense_ascq(sensep),
31661                                     "sense-data",
31662                                     DATA_TYPE_UINT8_ARRAY,
31663                                     senlen, sensep,
31664                                     NULL);
31665                         }
31666                 } else {
31667                         /*
31668                          * For stat_code == STATUS_GOOD, this is not a
31669                          * hardware error.
31670                          */
31671                         if (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD)
31672                                 return;
31673 
31674                         /*
31675                          * Post ereport.io.scsi.cmd.disk.dev.serr if we got the
31676                          * stat-code but with sense data unavailable.
31677                          * driver-assessment will be set based on parameter
31678                          * drv_assess.
31679                          */
31680                         scsi_fm_ereport_post(un->un_sd, 0,
31681                             NULL,
31682                             "cmd.disk.dev.serr", uscsi_ena,
31683                             devid, NULL, DDI_NOSLEEP, NULL,
31684                             FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31685                             DEVID_IF_KNOWN(devid),
31686                             "driver-assessment", DATA_TYPE_STRING,
31687                             drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31688                             "op-code", DATA_TYPE_UINT8, op_code,
31689                             "cdb",
31690                             DATA_TYPE_UINT8_ARRAY,
31691                             cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31692                             "pkt-reason",
31693                             DATA_TYPE_UINT8, uscsi_pkt_reason,
31694                             "pkt-state",
31695                             DATA_TYPE_UINT8, uscsi_pkt_state,
31696                             "pkt-stats",
31697                             DATA_TYPE_UINT32, uscsi_pkt_statistics,
31698                             "stat-code",
31699                             DATA_TYPE_UINT8,
31700                             ssc->ssc_uscsi_cmd->uscsi_status,
31701                             NULL);
31702                 }
31703         }
31704 }
31705 
31706 /*
31707  *     Function: sd_ssc_extract_info
31708  *
31709  * Description: Extract information available to help generate ereport.
31710  *
31711  *     Context: Kernel thread or interrupt context.
31712  */
31713 static void
31714 sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un, struct scsi_pkt *pktp,
31715     struct buf *bp, struct sd_xbuf *xp)
31716 {
31717         size_t senlen = 0;
31718         union scsi_cdb *cdbp;
31719         int path_instance;
31720         /*
31721          * Need scsi_cdb_size array to determine the cdb length.
31722          */
31723         extern uchar_t  scsi_cdb_size[];
31724 
31725         ASSERT(un != NULL);
31726         ASSERT(pktp != NULL);
31727         ASSERT(bp != NULL);
31728         ASSERT(xp != NULL);
31729         ASSERT(ssc != NULL);
31730         ASSERT(mutex_owned(SD_MUTEX(un)));
31731 
31732         /*
31733          * Transfer the cdb buffer pointer here.
31734          */
31735         cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
31736 
31737         ssc->ssc_uscsi_cmd->uscsi_cdblen = scsi_cdb_size[GETGROUP(cdbp)];
31738         ssc->ssc_uscsi_cmd->uscsi_cdb = (caddr_t)cdbp;
31739 
31740         /*
31741          * Transfer the sense data buffer pointer if sense data is available,
31742          * calculate the sense data length first.
31743          */
31744         if ((xp->xb_sense_state & STATE_XARQ_DONE) ||
31745             (xp->xb_sense_state & STATE_ARQ_DONE)) {
31746                 /*
31747                  * For arq case, we will enter here.
31748                  */
31749                 if (xp->xb_sense_state & STATE_XARQ_DONE) {
31750                         senlen = MAX_SENSE_LENGTH - xp->xb_sense_resid;
31751                 } else {
31752                         senlen = SENSE_LENGTH;
31753                 }
31754         } else {
31755                 /*
31756                  * For non-arq case, we will enter this branch.
31757                  */
31758                 if (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK &&
31759                     (xp->xb_sense_state & STATE_XFERRED_DATA)) {
31760                         senlen = SENSE_LENGTH - xp->xb_sense_resid;
31761                 }
31762 
31763         }
31764 
31765         ssc->ssc_uscsi_cmd->uscsi_rqlen = (senlen & 0xff);
31766         ssc->ssc_uscsi_cmd->uscsi_rqresid = 0;
31767         ssc->ssc_uscsi_cmd->uscsi_rqbuf = (caddr_t)xp->xb_sense_data;
31768 
31769         ssc->ssc_uscsi_cmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
31770 
31771         /*
31772          * Only transfer path_instance when scsi_pkt was properly allocated.
31773          */
31774         path_instance = pktp->pkt_path_instance;
31775         if (scsi_pkt_allocated_correctly(pktp) && path_instance)
31776                 ssc->ssc_uscsi_cmd->uscsi_path_instance = path_instance;
31777         else
31778                 ssc->ssc_uscsi_cmd->uscsi_path_instance = 0;
31779 
31780         /*
31781          * Copy in the other fields we may need when posting ereport.
31782          */
31783         ssc->ssc_uscsi_info->ui_pkt_reason = pktp->pkt_reason;
31784         ssc->ssc_uscsi_info->ui_pkt_state = pktp->pkt_state;
31785         ssc->ssc_uscsi_info->ui_pkt_statistics = pktp->pkt_statistics;
31786         ssc->ssc_uscsi_info->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
31787 
31788         /*
31789          * For partially read/write command, we will not create ena
31790          * in case of a successful command be reconized as recovered.
31791          */
31792         if ((pktp->pkt_reason == CMD_CMPLT) &&
31793             (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD) &&
31794             (senlen == 0)) {
31795                 return;
31796         }
31797 
31798         /*
31799          * To associate ereports of a single command execution flow, we
31800          * need a shared ena for a specific command.
31801          */
31802         if (xp->xb_ena == 0)
31803                 xp->xb_ena = fm_ena_generate(0, FM_ENA_FMT1);
31804         ssc->ssc_uscsi_info->ui_ena = xp->xb_ena;
31805 }
31806 
31807 
31808 /*
31809  *     Function: sd_check_bdc_vpd
31810  *
31811  * Description: Query the optional INQUIRY VPD page 0xb1. If the device
31812  *              supports VPD page 0xb1, sd examines the MEDIUM ROTATION
31813  *              RATE.
31814  *
31815  *              Set the following based on RPM value:
31816  *              = 0     device is not solid state, non-rotational
31817  *              = 1     device is solid state, non-rotational
31818  *              > 1  device is not solid state, rotational
31819  *
31820  *     Context: Kernel thread or interrupt context.
31821  */
31822 
31823 static void
31824 sd_check_bdc_vpd(sd_ssc_t *ssc)
31825 {
31826         int             rval            = 0;
31827         uchar_t         *inqb1          = NULL;
31828         size_t          inqb1_len       = MAX_INQUIRY_SIZE;
31829         size_t          inqb1_resid     = 0;
31830         struct sd_lun   *un;
31831 
31832         ASSERT(ssc != NULL);
31833         un = ssc->ssc_un;
31834         ASSERT(un != NULL);
31835         ASSERT(!mutex_owned(SD_MUTEX(un)));
31836 
31837         mutex_enter(SD_MUTEX(un));
31838         un->un_f_is_rotational = TRUE;
31839         un->un_f_is_solid_state = FALSE;
31840 
31841         if (ISCD(un)) {
31842                 mutex_exit(SD_MUTEX(un));
31843                 return;
31844         }
31845 
31846         if (sd_check_vpd_page_support(ssc) == 0 &&
31847             un->un_vpd_page_mask & SD_VPD_DEV_CHARACTER_PG) {
31848                 mutex_exit(SD_MUTEX(un));
31849                 /* collect page b1 data */
31850                 inqb1 = kmem_zalloc(inqb1_len, KM_SLEEP);
31851 
31852                 rval = sd_send_scsi_INQUIRY(ssc, inqb1, inqb1_len,
31853                     0x01, 0xB1, &inqb1_resid);
31854 
31855                 if (rval == 0 && (inqb1_len - inqb1_resid > 5)) {
31856                         SD_TRACE(SD_LOG_COMMON, un,
31857                             "sd_check_bdc_vpd: \
31858                             successfully get VPD page: %x \
31859                             PAGE LENGTH: %x BYTE 4: %x \
31860                             BYTE 5: %x", inqb1[1], inqb1[3], inqb1[4],
31861                             inqb1[5]);
31862 
31863                         mutex_enter(SD_MUTEX(un));
31864                         /*
31865                          * Check the MEDIUM ROTATION RATE.
31866                          */
31867                         if (inqb1[4] == 0) {
31868                                 if (inqb1[5] == 0) {
31869                                         un->un_f_is_rotational = FALSE;
31870                                 } else if (inqb1[5] == 1) {
31871                                         un->un_f_is_rotational = FALSE;
31872                                         un->un_f_is_solid_state = TRUE;
31873                                         /*
31874                                          * Solid state drives don't need
31875                                          * disksort.
31876                                          */
31877                                         un->un_f_disksort_disabled = TRUE;
31878                                 }
31879                         }
31880                         mutex_exit(SD_MUTEX(un));
31881                 } else if (rval != 0) {
31882                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
31883                 }
31884 
31885                 kmem_free(inqb1, inqb1_len);
31886         } else {
31887                 mutex_exit(SD_MUTEX(un));
31888         }
31889 }
31890 
31891 /*
31892  *      Function: sd_check_emulation_mode
31893  *
31894  *   Description: Check whether the SSD is at emulation mode
31895  *                by issuing READ_CAPACITY_16 to see whether
31896  *                we can get physical block size of the drive.
31897  *
31898  *       Context: Kernel thread or interrupt context.
31899  */
31900 
31901 static void
31902 sd_check_emulation_mode(sd_ssc_t *ssc)
31903 {
31904         int             rval = 0;
31905         uint64_t        capacity;
31906         uint_t          lbasize;
31907         uint_t          pbsize;
31908         int             i;
31909         int             devid_len;
31910         struct sd_lun   *un;
31911 
31912         ASSERT(ssc != NULL);
31913         un = ssc->ssc_un;
31914         ASSERT(un != NULL);
31915         ASSERT(!mutex_owned(SD_MUTEX(un)));
31916 
31917         mutex_enter(SD_MUTEX(un));
31918         if (ISCD(un)) {
31919                 mutex_exit(SD_MUTEX(un));
31920                 return;
31921         }
31922 
31923         if (un->un_f_descr_format_supported) {
31924                 mutex_exit(SD_MUTEX(un));
31925                 rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
31926                     &pbsize, SD_PATH_DIRECT);
31927                 mutex_enter(SD_MUTEX(un));
31928 
31929                 if (rval != 0) {
31930                         un->un_phy_blocksize = DEV_BSIZE;
31931                 } else {
31932                         if (!ISP2(pbsize % DEV_BSIZE) || pbsize == 0) {
31933                                 un->un_phy_blocksize = DEV_BSIZE;
31934                         } else if (pbsize > un->un_phy_blocksize &&
31935                             !un->un_f_sdconf_phy_blocksize) {
31936                                 /*
31937                                  * Reset the physical block size
31938                                  * if we've detected a larger value and
31939                                  * we didn't already set the physical
31940                                  * block size in sd.conf
31941                                  */
31942                                 un->un_phy_blocksize = pbsize;
31943                         }
31944                 }
31945         }
31946 
31947         for (i = 0; i < sd_flash_dev_table_size; i++) {
31948                 devid_len = (int)strlen(sd_flash_dev_table[i]);
31949                 if (sd_sdconf_id_match(un, sd_flash_dev_table[i], devid_len)
31950                     == SD_SUCCESS) {
31951                         un->un_phy_blocksize = SSD_SECSIZE;
31952                         if (un->un_f_is_solid_state &&
31953                             un->un_phy_blocksize != un->un_tgt_blocksize)
31954                                 un->un_f_enable_rmw = TRUE;
31955                 }
31956         }
31957 
31958         mutex_exit(SD_MUTEX(un));
31959 }