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  * Copyright (c) 2011 Bayard G. Bell.  All rights reserved.
  27  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
  28  * Copyright 2012 DEY Storage Systems, Inc.  All rights reserved.
  29  * Copyright 2017 Nexenta Systems, Inc.
  30  */
  31 /*
  32  * Copyright 2011 cyril.galibern@opensvc.com
  33  */
  34 
  35 /*
  36  * SCSI disk target driver.
  37  */
  38 #include <sys/scsi/scsi.h>
  39 #include <sys/dkbad.h>
  40 #include <sys/dklabel.h>
  41 #include <sys/dkio.h>
  42 #include <sys/fdio.h>
  43 #include <sys/cdio.h>
  44 #include <sys/mhd.h>
  45 #include <sys/vtoc.h>
  46 #include <sys/dktp/fdisk.h>
  47 #include <sys/kstat.h>
  48 #include <sys/vtrace.h>
  49 #include <sys/note.h>
  50 #include <sys/thread.h>
  51 #include <sys/proc.h>
  52 #include <sys/efi_partition.h>
  53 #include <sys/var.h>
  54 #include <sys/aio_req.h>
  55 
  56 #ifdef __lock_lint
  57 #define _LP64
  58 #define __amd64
  59 #endif
  60 
  61 #if (defined(__fibre))
  62 /* Note: is there a leadville version of the following? */
  63 #include <sys/fc4/fcal_linkapp.h>
  64 #endif
  65 #include <sys/taskq.h>
  66 #include <sys/uuid.h>
  67 #include <sys/byteorder.h>
  68 #include <sys/sdt.h>
  69 
  70 #include "sd_xbuf.h"
  71 
  72 #include <sys/scsi/targets/sddef.h>
  73 #include <sys/cmlb.h>
  74 #include <sys/sysevent/eventdefs.h>
  75 #include <sys/sysevent/dev.h>
  76 
  77 #include <sys/fm/protocol.h>
  78 
  79 /*
  80  * Loadable module info.
  81  */
  82 #if (defined(__fibre))
  83 #define SD_MODULE_NAME  "SCSI SSA/FCAL Disk Driver"
  84 #else /* !__fibre */
  85 #define SD_MODULE_NAME  "SCSI Disk Driver"
  86 #endif /* !__fibre */
  87 
  88 /*
  89  * Define the interconnect type, to allow the driver to distinguish
  90  * between parallel SCSI (sd) and fibre channel (ssd) behaviors.
  91  *
  92  * This is really for backward compatibility. In the future, the driver
  93  * should actually check the "interconnect-type" property as reported by
  94  * the HBA; however at present this property is not defined by all HBAs,
  95  * so we will use this #define (1) to permit the driver to run in
  96  * backward-compatibility mode; and (2) to print a notification message
  97  * if an FC HBA does not support the "interconnect-type" property.  The
  98  * behavior of the driver will be to assume parallel SCSI behaviors unless
  99  * the "interconnect-type" property is defined by the HBA **AND** has a
 100  * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or
 101  * INTERCONNECT_FABRIC, in which case the driver will assume Fibre
 102  * Channel behaviors (as per the old ssd).  (Note that the
 103  * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and
 104  * will result in the driver assuming parallel SCSI behaviors.)
 105  *
 106  * (see common/sys/scsi/impl/services.h)
 107  *
 108  * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default
 109  * since some FC HBAs may already support that, and there is some code in
 110  * the driver that already looks for it.  Using INTERCONNECT_FABRIC as the
 111  * default would confuse that code, and besides things should work fine
 112  * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the
 113  * "interconnect_type" property.
 114  *
 115  */
 116 #if (defined(__fibre))
 117 #define SD_DEFAULT_INTERCONNECT_TYPE    SD_INTERCONNECT_FIBRE
 118 #else
 119 #define SD_DEFAULT_INTERCONNECT_TYPE    SD_INTERCONNECT_PARALLEL
 120 #endif
 121 
 122 /*
 123  * The name of the driver, established from the module name in _init.
 124  */
 125 static  char *sd_label                  = NULL;
 126 
 127 /*
 128  * Driver name is unfortunately prefixed on some driver.conf properties.
 129  */
 130 #if (defined(__fibre))
 131 #define sd_max_xfer_size                ssd_max_xfer_size
 132 #define sd_config_list                  ssd_config_list
 133 static  char *sd_max_xfer_size          = "ssd_max_xfer_size";
 134 static  char *sd_config_list            = "ssd-config-list";
 135 #else
 136 static  char *sd_max_xfer_size          = "sd_max_xfer_size";
 137 static  char *sd_config_list            = "sd-config-list";
 138 #endif
 139 
 140 /*
 141  * Driver global variables
 142  */
 143 
 144 #if (defined(__fibre))
 145 /*
 146  * These #defines are to avoid namespace collisions that occur because this
 147  * code is currently used to compile two separate driver modules: sd and ssd.
 148  * All global variables need to be treated this way (even if declared static)
 149  * in order to allow the debugger to resolve the names properly.
 150  * It is anticipated that in the near future the ssd module will be obsoleted,
 151  * at which time this namespace issue should go away.
 152  */
 153 #define sd_state                        ssd_state
 154 #define sd_io_time                      ssd_io_time
 155 #define sd_failfast_enable              ssd_failfast_enable
 156 #define sd_ua_retry_count               ssd_ua_retry_count
 157 #define sd_report_pfa                   ssd_report_pfa
 158 #define sd_max_throttle                 ssd_max_throttle
 159 #define sd_min_throttle                 ssd_min_throttle
 160 #define sd_rot_delay                    ssd_rot_delay
 161 
 162 #define sd_retry_on_reservation_conflict        \
 163                                         ssd_retry_on_reservation_conflict
 164 #define sd_reinstate_resv_delay         ssd_reinstate_resv_delay
 165 #define sd_resv_conflict_name           ssd_resv_conflict_name
 166 
 167 #define sd_component_mask               ssd_component_mask
 168 #define sd_level_mask                   ssd_level_mask
 169 #define sd_debug_un                     ssd_debug_un
 170 #define sd_error_level                  ssd_error_level
 171 
 172 #define sd_xbuf_active_limit            ssd_xbuf_active_limit
 173 #define sd_xbuf_reserve_limit           ssd_xbuf_reserve_limit
 174 
 175 #define sd_tr                           ssd_tr
 176 #define sd_reset_throttle_timeout       ssd_reset_throttle_timeout
 177 #define sd_qfull_throttle_timeout       ssd_qfull_throttle_timeout
 178 #define sd_qfull_throttle_enable        ssd_qfull_throttle_enable
 179 #define sd_check_media_time             ssd_check_media_time
 180 #define sd_wait_cmds_complete           ssd_wait_cmds_complete
 181 #define sd_label_mutex                  ssd_label_mutex
 182 #define sd_detach_mutex                 ssd_detach_mutex
 183 #define sd_log_buf                      ssd_log_buf
 184 #define sd_log_mutex                    ssd_log_mutex
 185 
 186 #define sd_disk_table                   ssd_disk_table
 187 #define sd_disk_table_size              ssd_disk_table_size
 188 #define sd_sense_mutex                  ssd_sense_mutex
 189 #define sd_cdbtab                       ssd_cdbtab
 190 
 191 #define sd_cb_ops                       ssd_cb_ops
 192 #define sd_ops                          ssd_ops
 193 #define sd_additional_codes             ssd_additional_codes
 194 #define sd_tgops                        ssd_tgops
 195 
 196 #define sd_minor_data                   ssd_minor_data
 197 #define sd_minor_data_efi               ssd_minor_data_efi
 198 
 199 #define sd_tq                           ssd_tq
 200 #define sd_wmr_tq                       ssd_wmr_tq
 201 #define sd_taskq_name                   ssd_taskq_name
 202 #define sd_wmr_taskq_name               ssd_wmr_taskq_name
 203 #define sd_taskq_minalloc               ssd_taskq_minalloc
 204 #define sd_taskq_maxalloc               ssd_taskq_maxalloc
 205 
 206 #define sd_dump_format_string           ssd_dump_format_string
 207 
 208 #define sd_iostart_chain                ssd_iostart_chain
 209 #define sd_iodone_chain                 ssd_iodone_chain
 210 
 211 #define sd_pm_idletime                  ssd_pm_idletime
 212 
 213 #define sd_force_pm_supported           ssd_force_pm_supported
 214 
 215 #define sd_dtype_optical_bind           ssd_dtype_optical_bind
 216 
 217 #define sd_ssc_init                     ssd_ssc_init
 218 #define sd_ssc_send                     ssd_ssc_send
 219 #define sd_ssc_fini                     ssd_ssc_fini
 220 #define sd_ssc_assessment               ssd_ssc_assessment
 221 #define sd_ssc_post                     ssd_ssc_post
 222 #define sd_ssc_print                    ssd_ssc_print
 223 #define sd_ssc_ereport_post             ssd_ssc_ereport_post
 224 #define sd_ssc_set_info                 ssd_ssc_set_info
 225 #define sd_ssc_extract_info             ssd_ssc_extract_info
 226 
 227 #endif
 228 
 229 #ifdef  SDDEBUG
 230 int     sd_force_pm_supported           = 0;
 231 #endif  /* SDDEBUG */
 232 
 233 void *sd_state                          = NULL;
 234 int sd_io_time                          = SD_IO_TIME;
 235 int sd_failfast_enable                  = 1;
 236 int sd_ua_retry_count                   = SD_UA_RETRY_COUNT;
 237 int sd_report_pfa                       = 1;
 238 int sd_max_throttle                     = SD_MAX_THROTTLE;
 239 int sd_min_throttle                     = SD_MIN_THROTTLE;
 240 int sd_rot_delay                        = 4; /* Default 4ms Rotation delay */
 241 int sd_qfull_throttle_enable            = TRUE;
 242 
 243 int sd_retry_on_reservation_conflict    = 1;
 244 int sd_reinstate_resv_delay             = SD_REINSTATE_RESV_DELAY;
 245 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay))
 246 
 247 static int sd_dtype_optical_bind        = -1;
 248 
 249 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */
 250 static  char *sd_resv_conflict_name     = "sd_retry_on_reservation_conflict";
 251 
 252 /*
 253  * Global data for debug logging. To enable debug printing, sd_component_mask
 254  * and sd_level_mask should be set to the desired bit patterns as outlined in
 255  * sddef.h.
 256  */
 257 uint_t  sd_component_mask               = 0x0;
 258 uint_t  sd_level_mask                   = 0x0;
 259 struct  sd_lun *sd_debug_un             = NULL;
 260 uint_t  sd_error_level                  = SCSI_ERR_RETRYABLE;
 261 
 262 /* Note: these may go away in the future... */
 263 static uint32_t sd_xbuf_active_limit    = 512;
 264 static uint32_t sd_xbuf_reserve_limit   = 16;
 265 
 266 static struct sd_resv_reclaim_request   sd_tr = { NULL, NULL, NULL, 0, 0, 0 };
 267 
 268 /*
 269  * Timer value used to reset the throttle after it has been reduced
 270  * (typically in response to TRAN_BUSY or STATUS_QFULL)
 271  */
 272 static int sd_reset_throttle_timeout    = SD_RESET_THROTTLE_TIMEOUT;
 273 static int sd_qfull_throttle_timeout    = SD_QFULL_THROTTLE_TIMEOUT;
 274 
 275 /*
 276  * Interval value associated with the media change scsi watch.
 277  */
 278 static int sd_check_media_time          = 3000000;
 279 
 280 /*
 281  * Wait value used for in progress operations during a DDI_SUSPEND
 282  */
 283 static int sd_wait_cmds_complete        = SD_WAIT_CMDS_COMPLETE;
 284 
 285 /*
 286  * sd_label_mutex protects a static buffer used in the disk label
 287  * component of the driver
 288  */
 289 static kmutex_t sd_label_mutex;
 290 
 291 /*
 292  * sd_detach_mutex protects un_layer_count, un_detach_count, and
 293  * un_opens_in_progress in the sd_lun structure.
 294  */
 295 static kmutex_t sd_detach_mutex;
 296 
 297 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex,
 298         sd_lun::{un_layer_count un_detach_count un_opens_in_progress}))
 299 
 300 /*
 301  * Global buffer and mutex for debug logging
 302  */
 303 static char     sd_log_buf[1024];
 304 static kmutex_t sd_log_mutex;
 305 
 306 /*
 307  * Structs and globals for recording attached lun information.
 308  * This maintains a chain. Each node in the chain represents a SCSI controller.
 309  * The structure records the number of luns attached to each target connected
 310  * with the controller.
 311  * For parallel scsi device only.
 312  */
 313 struct sd_scsi_hba_tgt_lun {
 314         struct sd_scsi_hba_tgt_lun      *next;
 315         dev_info_t                      *pdip;
 316         int                             nlun[NTARGETS_WIDE];
 317 };
 318 
 319 /*
 320  * Flag to indicate the lun is attached or detached
 321  */
 322 #define SD_SCSI_LUN_ATTACH      0
 323 #define SD_SCSI_LUN_DETACH      1
 324 
 325 static kmutex_t sd_scsi_target_lun_mutex;
 326 static struct sd_scsi_hba_tgt_lun       *sd_scsi_target_lun_head = NULL;
 327 
 328 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
 329     sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip))
 330 
 331 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
 332     sd_scsi_target_lun_head))
 333 
 334 /*
 335  * "Smart" Probe Caching structs, globals, #defines, etc.
 336  * For parallel scsi and non-self-identify device only.
 337  */
 338 
 339 /*
 340  * The following resources and routines are implemented to support
 341  * "smart" probing, which caches the scsi_probe() results in an array,
 342  * in order to help avoid long probe times.
 343  */
 344 struct sd_scsi_probe_cache {
 345         struct  sd_scsi_probe_cache     *next;
 346         dev_info_t      *pdip;
 347         int             cache[NTARGETS_WIDE];
 348 };
 349 
 350 static kmutex_t sd_scsi_probe_cache_mutex;
 351 static struct   sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL;
 352 
 353 /*
 354  * Really we only need protection on the head of the linked list, but
 355  * better safe than sorry.
 356  */
 357 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
 358     sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip))
 359 
 360 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
 361     sd_scsi_probe_cache_head))
 362 
 363 /*
 364  * Power attribute table
 365  */
 366 static sd_power_attr_ss sd_pwr_ss = {
 367         { "NAME=spindle-motor", "0=off", "1=on", NULL },
 368         {0, 100},
 369         {30, 0},
 370         {20000, 0}
 371 };
 372 
 373 static sd_power_attr_pc sd_pwr_pc = {
 374         { "NAME=spindle-motor", "0=stopped", "1=standby", "2=idle",
 375                 "3=active", NULL },
 376         {0, 0, 0, 100},
 377         {90, 90, 20, 0},
 378         {15000, 15000, 1000, 0}
 379 };
 380 
 381 /*
 382  * Power level to power condition
 383  */
 384 static int sd_pl2pc[] = {
 385         SD_TARGET_START_VALID,
 386         SD_TARGET_STANDBY,
 387         SD_TARGET_IDLE,
 388         SD_TARGET_ACTIVE
 389 };
 390 
 391 /*
 392  * Vendor specific data name property declarations
 393  */
 394 
 395 #if defined(__fibre) || defined(__i386) ||defined(__amd64)
 396 
 397 static sd_tunables seagate_properties = {
 398         SEAGATE_THROTTLE_VALUE,
 399         0,
 400         0,
 401         0,
 402         0,
 403         0,
 404         0,
 405         0,
 406         0
 407 };
 408 
 409 
 410 static sd_tunables fujitsu_properties = {
 411         FUJITSU_THROTTLE_VALUE,
 412         0,
 413         0,
 414         0,
 415         0,
 416         0,
 417         0,
 418         0,
 419         0
 420 };
 421 
 422 static sd_tunables ibm_properties = {
 423         IBM_THROTTLE_VALUE,
 424         0,
 425         0,
 426         0,
 427         0,
 428         0,
 429         0,
 430         0,
 431         0
 432 };
 433 
 434 static sd_tunables purple_properties = {
 435         PURPLE_THROTTLE_VALUE,
 436         0,
 437         0,
 438         PURPLE_BUSY_RETRIES,
 439         PURPLE_RESET_RETRY_COUNT,
 440         PURPLE_RESERVE_RELEASE_TIME,
 441         0,
 442         0,
 443         0
 444 };
 445 
 446 static sd_tunables sve_properties = {
 447         SVE_THROTTLE_VALUE,
 448         0,
 449         0,
 450         SVE_BUSY_RETRIES,
 451         SVE_RESET_RETRY_COUNT,
 452         SVE_RESERVE_RELEASE_TIME,
 453         SVE_MIN_THROTTLE_VALUE,
 454         SVE_DISKSORT_DISABLED_FLAG,
 455         0
 456 };
 457 
 458 static sd_tunables maserati_properties = {
 459         0,
 460         0,
 461         0,
 462         0,
 463         0,
 464         0,
 465         0,
 466         MASERATI_DISKSORT_DISABLED_FLAG,
 467         MASERATI_LUN_RESET_ENABLED_FLAG
 468 };
 469 
 470 static sd_tunables pirus_properties = {
 471         PIRUS_THROTTLE_VALUE,
 472         0,
 473         PIRUS_NRR_COUNT,
 474         PIRUS_BUSY_RETRIES,
 475         PIRUS_RESET_RETRY_COUNT,
 476         0,
 477         PIRUS_MIN_THROTTLE_VALUE,
 478         PIRUS_DISKSORT_DISABLED_FLAG,
 479         PIRUS_LUN_RESET_ENABLED_FLAG
 480 };
 481 
 482 #endif
 483 
 484 #if (defined(__sparc) && !defined(__fibre)) || \
 485         (defined(__i386) || defined(__amd64))
 486 
 487 
 488 static sd_tunables elite_properties = {
 489         ELITE_THROTTLE_VALUE,
 490         0,
 491         0,
 492         0,
 493         0,
 494         0,
 495         0,
 496         0,
 497         0
 498 };
 499 
 500 static sd_tunables st31200n_properties = {
 501         ST31200N_THROTTLE_VALUE,
 502         0,
 503         0,
 504         0,
 505         0,
 506         0,
 507         0,
 508         0,
 509         0
 510 };
 511 
 512 #endif /* Fibre or not */
 513 
 514 static sd_tunables lsi_properties_scsi = {
 515         LSI_THROTTLE_VALUE,
 516         0,
 517         LSI_NOTREADY_RETRIES,
 518         0,
 519         0,
 520         0,
 521         0,
 522         0,
 523         0
 524 };
 525 
 526 static sd_tunables symbios_properties = {
 527         SYMBIOS_THROTTLE_VALUE,
 528         0,
 529         SYMBIOS_NOTREADY_RETRIES,
 530         0,
 531         0,
 532         0,
 533         0,
 534         0,
 535         0
 536 };
 537 
 538 static sd_tunables lsi_properties = {
 539         0,
 540         0,
 541         LSI_NOTREADY_RETRIES,
 542         0,
 543         0,
 544         0,
 545         0,
 546         0,
 547         0
 548 };
 549 
 550 static sd_tunables lsi_oem_properties = {
 551         0,
 552         0,
 553         LSI_OEM_NOTREADY_RETRIES,
 554         0,
 555         0,
 556         0,
 557         0,
 558         0,
 559         0,
 560         1
 561 };
 562 
 563 
 564 
 565 #if (defined(SD_PROP_TST))
 566 
 567 #define SD_TST_CTYPE_VAL        CTYPE_CDROM
 568 #define SD_TST_THROTTLE_VAL     16
 569 #define SD_TST_NOTREADY_VAL     12
 570 #define SD_TST_BUSY_VAL         60
 571 #define SD_TST_RST_RETRY_VAL    36
 572 #define SD_TST_RSV_REL_TIME     60
 573 
 574 static sd_tunables tst_properties = {
 575         SD_TST_THROTTLE_VAL,
 576         SD_TST_CTYPE_VAL,
 577         SD_TST_NOTREADY_VAL,
 578         SD_TST_BUSY_VAL,
 579         SD_TST_RST_RETRY_VAL,
 580         SD_TST_RSV_REL_TIME,
 581         0,
 582         0,
 583         0
 584 };
 585 #endif
 586 
 587 /* This is similar to the ANSI toupper implementation */
 588 #define SD_TOUPPER(C)   (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C))
 589 
 590 /*
 591  * Static Driver Configuration Table
 592  *
 593  * This is the table of disks which need throttle adjustment (or, perhaps
 594  * something else as defined by the flags at a future time.)  device_id
 595  * is a string consisting of concatenated vid (vendor), pid (product/model)
 596  * and revision strings as defined in the scsi_inquiry structure.  Offsets of
 597  * the parts of the string are as defined by the sizes in the scsi_inquiry
 598  * structure.  Device type is searched as far as the device_id string is
 599  * defined.  Flags defines which values are to be set in the driver from the
 600  * properties list.
 601  *
 602  * Entries below which begin and end with a "*" are a special case.
 603  * These do not have a specific vendor, and the string which follows
 604  * can appear anywhere in the 16 byte PID portion of the inquiry data.
 605  *
 606  * Entries below which begin and end with a " " (blank) are a special
 607  * case. The comparison function will treat multiple consecutive blanks
 608  * as equivalent to a single blank. For example, this causes a
 609  * sd_disk_table entry of " NEC CDROM " to match a device's id string
 610  * of  "NEC       CDROM".
 611  *
 612  * Note: The MD21 controller type has been obsoleted.
 613  *       ST318202F is a Legacy device
 614  *       MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been
 615  *       made with an FC connection. The entries here are a legacy.
 616  */
 617 static sd_disk_config_t sd_disk_table[] = {
 618 #if defined(__fibre) || defined(__i386) || defined(__amd64)
 619         { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
 620         { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
 621         { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
 622         { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
 623         { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties },
 624         { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties },
 625         { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties },
 626         { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties },
 627         { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties },
 628         { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties },
 629         { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties },
 630         { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties },
 631         { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties },
 632         { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties },
 633         { "FUJITSU MAG3091F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
 634         { "FUJITSU MAG3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
 635         { "FUJITSU MAA3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
 636         { "FUJITSU MAF3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
 637         { "FUJITSU MAL3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
 638         { "FUJITSU MAL3738F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
 639         { "FUJITSU MAM3182FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
 640         { "FUJITSU MAM3364FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
 641         { "FUJITSU MAM3738FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
 642         { "IBM     DDYFT1835",  SD_CONF_BSET_THROTTLE, &ibm_properties },
 643         { "IBM     DDYFT3695",  SD_CONF_BSET_THROTTLE, &ibm_properties },
 644         { "IBM     IC35LF2D2",  SD_CONF_BSET_THROTTLE, &ibm_properties },
 645         { "IBM     IC35LF2PR",  SD_CONF_BSET_THROTTLE, &ibm_properties },
 646         { "IBM     1724-100",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 647         { "IBM     1726-2xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 648         { "IBM     1726-22x",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 649         { "IBM     1726-4xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 650         { "IBM     1726-42x",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 651         { "IBM     1726-3xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 652         { "IBM     3526",       SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 653         { "IBM     3542",       SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 654         { "IBM     3552",       SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 655         { "IBM     1722",       SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 656         { "IBM     1742",       SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 657         { "IBM     1815",       SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 658         { "IBM     FAStT",      SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 659         { "IBM     1814",       SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 660         { "IBM     1814-200",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 661         { "IBM     1818",       SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 662         { "DELL    MD3000",     SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 663         { "DELL    MD3000i",    SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 664         { "LSI     INF",        SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 665         { "ENGENIO INF",        SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 666         { "SGI     TP",         SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 667         { "SGI     IS",         SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 668         { "*CSM100_*",          SD_CONF_BSET_NRR_COUNT |
 669                         SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
 670         { "*CSM200_*",          SD_CONF_BSET_NRR_COUNT |
 671                         SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
 672         { "Fujitsu SX300",      SD_CONF_BSET_THROTTLE,  &lsi_oem_properties },
 673         { "LSI",                SD_CONF_BSET_NRR_COUNT, &lsi_properties },
 674         { "SUN     T3", SD_CONF_BSET_THROTTLE |
 675                         SD_CONF_BSET_BSY_RETRY_COUNT|
 676                         SD_CONF_BSET_RST_RETRIES|
 677                         SD_CONF_BSET_RSV_REL_TIME,
 678                 &purple_properties },
 679         { "SUN     SESS01", SD_CONF_BSET_THROTTLE |
 680                 SD_CONF_BSET_BSY_RETRY_COUNT|
 681                 SD_CONF_BSET_RST_RETRIES|
 682                 SD_CONF_BSET_RSV_REL_TIME|
 683                 SD_CONF_BSET_MIN_THROTTLE|
 684                 SD_CONF_BSET_DISKSORT_DISABLED,
 685                 &sve_properties },
 686         { "SUN     T4", SD_CONF_BSET_THROTTLE |
 687                         SD_CONF_BSET_BSY_RETRY_COUNT|
 688                         SD_CONF_BSET_RST_RETRIES|
 689                         SD_CONF_BSET_RSV_REL_TIME,
 690                 &purple_properties },
 691         { "SUN     SVE01", SD_CONF_BSET_DISKSORT_DISABLED |
 692                 SD_CONF_BSET_LUN_RESET_ENABLED,
 693                 &maserati_properties },
 694         { "SUN     SE6920", SD_CONF_BSET_THROTTLE |
 695                 SD_CONF_BSET_NRR_COUNT|
 696                 SD_CONF_BSET_BSY_RETRY_COUNT|
 697                 SD_CONF_BSET_RST_RETRIES|
 698                 SD_CONF_BSET_MIN_THROTTLE|
 699                 SD_CONF_BSET_DISKSORT_DISABLED|
 700                 SD_CONF_BSET_LUN_RESET_ENABLED,
 701                 &pirus_properties },
 702         { "SUN     SE6940", SD_CONF_BSET_THROTTLE |
 703                 SD_CONF_BSET_NRR_COUNT|
 704                 SD_CONF_BSET_BSY_RETRY_COUNT|
 705                 SD_CONF_BSET_RST_RETRIES|
 706                 SD_CONF_BSET_MIN_THROTTLE|
 707                 SD_CONF_BSET_DISKSORT_DISABLED|
 708                 SD_CONF_BSET_LUN_RESET_ENABLED,
 709                 &pirus_properties },
 710         { "SUN     StorageTek 6920", SD_CONF_BSET_THROTTLE |
 711                 SD_CONF_BSET_NRR_COUNT|
 712                 SD_CONF_BSET_BSY_RETRY_COUNT|
 713                 SD_CONF_BSET_RST_RETRIES|
 714                 SD_CONF_BSET_MIN_THROTTLE|
 715                 SD_CONF_BSET_DISKSORT_DISABLED|
 716                 SD_CONF_BSET_LUN_RESET_ENABLED,
 717                 &pirus_properties },
 718         { "SUN     StorageTek 6940", SD_CONF_BSET_THROTTLE |
 719                 SD_CONF_BSET_NRR_COUNT|
 720                 SD_CONF_BSET_BSY_RETRY_COUNT|
 721                 SD_CONF_BSET_RST_RETRIES|
 722                 SD_CONF_BSET_MIN_THROTTLE|
 723                 SD_CONF_BSET_DISKSORT_DISABLED|
 724                 SD_CONF_BSET_LUN_RESET_ENABLED,
 725                 &pirus_properties },
 726         { "SUN     PSX1000", SD_CONF_BSET_THROTTLE |
 727                 SD_CONF_BSET_NRR_COUNT|
 728                 SD_CONF_BSET_BSY_RETRY_COUNT|
 729                 SD_CONF_BSET_RST_RETRIES|
 730                 SD_CONF_BSET_MIN_THROTTLE|
 731                 SD_CONF_BSET_DISKSORT_DISABLED|
 732                 SD_CONF_BSET_LUN_RESET_ENABLED,
 733                 &pirus_properties },
 734         { "SUN     SE6330", SD_CONF_BSET_THROTTLE |
 735                 SD_CONF_BSET_NRR_COUNT|
 736                 SD_CONF_BSET_BSY_RETRY_COUNT|
 737                 SD_CONF_BSET_RST_RETRIES|
 738                 SD_CONF_BSET_MIN_THROTTLE|
 739                 SD_CONF_BSET_DISKSORT_DISABLED|
 740                 SD_CONF_BSET_LUN_RESET_ENABLED,
 741                 &pirus_properties },
 742         { "SUN     STK6580_6780", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 743         { "SUN     SUN_6180", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 744         { "STK     OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 745         { "STK     OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 746         { "STK     BladeCtlr",  SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 747         { "STK     FLEXLINE",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
 748         { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties },
 749 #endif /* fibre or NON-sparc platforms */
 750 #if ((defined(__sparc) && !defined(__fibre)) ||\
 751         (defined(__i386) || defined(__amd64)))
 752         { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties },
 753         { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties },
 754         { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL },
 755         { "CONNER  CP30540",  SD_CONF_BSET_NOCACHE,  NULL },
 756         { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL },
 757         { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL },
 758         { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL },
 759         { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL },
 760         { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL },
 761         { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL },
 762         { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL },
 763         { "SYMBIOS INF-01-00       ", SD_CONF_BSET_FAB_DEVID, NULL },
 764         { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT,
 765             &symbios_properties },
 766         { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT,
 767             &lsi_properties_scsi },
 768 #if defined(__i386) || defined(__amd64)
 769         { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD
 770                                     | SD_CONF_BSET_READSUB_BCD
 771                                     | SD_CONF_BSET_READ_TOC_ADDR_BCD
 772                                     | SD_CONF_BSET_NO_READ_HEADER
 773                                     | SD_CONF_BSET_READ_CD_XD4), NULL },
 774 
 775         { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD
 776                                     | SD_CONF_BSET_READSUB_BCD
 777                                     | SD_CONF_BSET_READ_TOC_ADDR_BCD
 778                                     | SD_CONF_BSET_NO_READ_HEADER
 779                                     | SD_CONF_BSET_READ_CD_XD4), NULL },
 780 #endif /* __i386 || __amd64 */
 781 #endif /* sparc NON-fibre or NON-sparc platforms */
 782 
 783 #if (defined(SD_PROP_TST))
 784         { "VENDOR  PRODUCT ", (SD_CONF_BSET_THROTTLE
 785                                 | SD_CONF_BSET_CTYPE
 786                                 | SD_CONF_BSET_NRR_COUNT
 787                                 | SD_CONF_BSET_FAB_DEVID
 788                                 | SD_CONF_BSET_NOCACHE
 789                                 | SD_CONF_BSET_BSY_RETRY_COUNT
 790                                 | SD_CONF_BSET_PLAYMSF_BCD
 791                                 | SD_CONF_BSET_READSUB_BCD
 792                                 | SD_CONF_BSET_READ_TOC_TRK_BCD
 793                                 | SD_CONF_BSET_READ_TOC_ADDR_BCD
 794                                 | SD_CONF_BSET_NO_READ_HEADER
 795                                 | SD_CONF_BSET_READ_CD_XD4
 796                                 | SD_CONF_BSET_RST_RETRIES
 797                                 | SD_CONF_BSET_RSV_REL_TIME
 798                                 | SD_CONF_BSET_TUR_CHECK), &tst_properties},
 799 #endif
 800 };
 801 
 802 static const int sd_disk_table_size =
 803         sizeof (sd_disk_table)/ sizeof (sd_disk_config_t);
 804 
 805 /*
 806  * Emulation mode disk drive VID/PID table
 807  */
 808 static char sd_flash_dev_table[][25] = {
 809         "ATA     MARVELL SD88SA02",
 810         "MARVELL SD88SA02",
 811         "TOSHIBA THNSNV05",
 812 };
 813 
 814 static const int sd_flash_dev_table_size =
 815         sizeof (sd_flash_dev_table) / sizeof (sd_flash_dev_table[0]);
 816 
 817 #define SD_INTERCONNECT_PARALLEL        0
 818 #define SD_INTERCONNECT_FABRIC          1
 819 #define SD_INTERCONNECT_FIBRE           2
 820 #define SD_INTERCONNECT_SSA             3
 821 #define SD_INTERCONNECT_SATA            4
 822 #define SD_INTERCONNECT_SAS             5
 823 
 824 #define SD_IS_PARALLEL_SCSI(un)         \
 825         ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL)
 826 #define SD_IS_SERIAL(un)                \
 827         (((un)->un_interconnect_type == SD_INTERCONNECT_SATA) ||\
 828         ((un)->un_interconnect_type == SD_INTERCONNECT_SAS))
 829 
 830 /*
 831  * Definitions used by device id registration routines
 832  */
 833 #define VPD_HEAD_OFFSET         3       /* size of head for vpd page */
 834 #define VPD_PAGE_LENGTH         3       /* offset for pge length data */
 835 #define VPD_MODE_PAGE           1       /* offset into vpd pg for "page code" */
 836 
 837 static kmutex_t sd_sense_mutex = {0};
 838 
 839 /*
 840  * Macros for updates of the driver state
 841  */
 842 #define New_state(un, s)        \
 843         (un)->un_last_state = (un)->un_state, (un)->un_state = (s)
 844 #define Restore_state(un)       \
 845         { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); }
 846 
 847 static struct sd_cdbinfo sd_cdbtab[] = {
 848         { CDB_GROUP0, 0x00,        0x1FFFFF,   0xFF,        },
 849         { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF,      },
 850         { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF,  },
 851         { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, },
 852 };
 853 
 854 /*
 855  * Specifies the number of seconds that must have elapsed since the last
 856  * cmd. has completed for a device to be declared idle to the PM framework.
 857  */
 858 static int sd_pm_idletime = 1;
 859 
 860 /*
 861  * Internal function prototypes
 862  */
 863 
 864 #if (defined(__fibre))
 865 /*
 866  * These #defines are to avoid namespace collisions that occur because this
 867  * code is currently used to compile two separate driver modules: sd and ssd.
 868  * All function names need to be treated this way (even if declared static)
 869  * in order to allow the debugger to resolve the names properly.
 870  * It is anticipated that in the near future the ssd module will be obsoleted,
 871  * at which time this ugliness should go away.
 872  */
 873 #define sd_log_trace                    ssd_log_trace
 874 #define sd_log_info                     ssd_log_info
 875 #define sd_log_err                      ssd_log_err
 876 #define sdprobe                         ssdprobe
 877 #define sdinfo                          ssdinfo
 878 #define sd_prop_op                      ssd_prop_op
 879 #define sd_scsi_probe_cache_init        ssd_scsi_probe_cache_init
 880 #define sd_scsi_probe_cache_fini        ssd_scsi_probe_cache_fini
 881 #define sd_scsi_clear_probe_cache       ssd_scsi_clear_probe_cache
 882 #define sd_scsi_probe_with_cache        ssd_scsi_probe_with_cache
 883 #define sd_scsi_target_lun_init         ssd_scsi_target_lun_init
 884 #define sd_scsi_target_lun_fini         ssd_scsi_target_lun_fini
 885 #define sd_scsi_get_target_lun_count    ssd_scsi_get_target_lun_count
 886 #define sd_scsi_update_lun_on_target    ssd_scsi_update_lun_on_target
 887 #define sd_spin_up_unit                 ssd_spin_up_unit
 888 #define sd_enable_descr_sense           ssd_enable_descr_sense
 889 #define sd_reenable_dsense_task         ssd_reenable_dsense_task
 890 #define sd_set_mmc_caps                 ssd_set_mmc_caps
 891 #define sd_read_unit_properties         ssd_read_unit_properties
 892 #define sd_process_sdconf_file          ssd_process_sdconf_file
 893 #define sd_process_sdconf_table         ssd_process_sdconf_table
 894 #define sd_sdconf_id_match              ssd_sdconf_id_match
 895 #define sd_blank_cmp                    ssd_blank_cmp
 896 #define sd_chk_vers1_data               ssd_chk_vers1_data
 897 #define sd_set_vers1_properties         ssd_set_vers1_properties
 898 #define sd_check_bdc_vpd                ssd_check_bdc_vpd
 899 #define sd_check_emulation_mode         ssd_check_emulation_mode
 900 
 901 #define sd_get_physical_geometry        ssd_get_physical_geometry
 902 #define sd_get_virtual_geometry         ssd_get_virtual_geometry
 903 #define sd_update_block_info            ssd_update_block_info
 904 #define sd_register_devid               ssd_register_devid
 905 #define sd_get_devid                    ssd_get_devid
 906 #define sd_create_devid                 ssd_create_devid
 907 #define sd_write_deviceid               ssd_write_deviceid
 908 #define sd_check_vpd_page_support       ssd_check_vpd_page_support
 909 #define sd_setup_pm                     ssd_setup_pm
 910 #define sd_create_pm_components         ssd_create_pm_components
 911 #define sd_ddi_suspend                  ssd_ddi_suspend
 912 #define sd_ddi_resume                   ssd_ddi_resume
 913 #define sd_pm_state_change              ssd_pm_state_change
 914 #define sdpower                         ssdpower
 915 #define sdattach                        ssdattach
 916 #define sddetach                        ssddetach
 917 #define sd_unit_attach                  ssd_unit_attach
 918 #define sd_unit_detach                  ssd_unit_detach
 919 #define sd_set_unit_attributes          ssd_set_unit_attributes
 920 #define sd_create_errstats              ssd_create_errstats
 921 #define sd_set_errstats                 ssd_set_errstats
 922 #define sd_set_pstats                   ssd_set_pstats
 923 #define sddump                          ssddump
 924 #define sd_scsi_poll                    ssd_scsi_poll
 925 #define sd_send_polled_RQS              ssd_send_polled_RQS
 926 #define sd_ddi_scsi_poll                ssd_ddi_scsi_poll
 927 #define sd_init_event_callbacks         ssd_init_event_callbacks
 928 #define sd_event_callback               ssd_event_callback
 929 #define sd_cache_control                ssd_cache_control
 930 #define sd_get_write_cache_enabled      ssd_get_write_cache_enabled
 931 #define sd_get_write_cache_changeable   ssd_get_write_cache_changeable
 932 #define sd_get_nv_sup                   ssd_get_nv_sup
 933 #define sd_make_device                  ssd_make_device
 934 #define sdopen                          ssdopen
 935 #define sdclose                         ssdclose
 936 #define sd_ready_and_valid              ssd_ready_and_valid
 937 #define sdmin                           ssdmin
 938 #define sdread                          ssdread
 939 #define sdwrite                         ssdwrite
 940 #define sdaread                         ssdaread
 941 #define sdawrite                        ssdawrite
 942 #define sdstrategy                      ssdstrategy
 943 #define sdioctl                         ssdioctl
 944 #define sd_mapblockaddr_iostart         ssd_mapblockaddr_iostart
 945 #define sd_mapblocksize_iostart         ssd_mapblocksize_iostart
 946 #define sd_checksum_iostart             ssd_checksum_iostart
 947 #define sd_checksum_uscsi_iostart       ssd_checksum_uscsi_iostart
 948 #define sd_pm_iostart                   ssd_pm_iostart
 949 #define sd_core_iostart                 ssd_core_iostart
 950 #define sd_mapblockaddr_iodone          ssd_mapblockaddr_iodone
 951 #define sd_mapblocksize_iodone          ssd_mapblocksize_iodone
 952 #define sd_checksum_iodone              ssd_checksum_iodone
 953 #define sd_checksum_uscsi_iodone        ssd_checksum_uscsi_iodone
 954 #define sd_pm_iodone                    ssd_pm_iodone
 955 #define sd_initpkt_for_buf              ssd_initpkt_for_buf
 956 #define sd_destroypkt_for_buf           ssd_destroypkt_for_buf
 957 #define sd_setup_rw_pkt                 ssd_setup_rw_pkt
 958 #define sd_setup_next_rw_pkt            ssd_setup_next_rw_pkt
 959 #define sd_buf_iodone                   ssd_buf_iodone
 960 #define sd_uscsi_strategy               ssd_uscsi_strategy
 961 #define sd_initpkt_for_uscsi            ssd_initpkt_for_uscsi
 962 #define sd_destroypkt_for_uscsi         ssd_destroypkt_for_uscsi
 963 #define sd_uscsi_iodone                 ssd_uscsi_iodone
 964 #define sd_xbuf_strategy                ssd_xbuf_strategy
 965 #define sd_xbuf_init                    ssd_xbuf_init
 966 #define sd_pm_entry                     ssd_pm_entry
 967 #define sd_pm_exit                      ssd_pm_exit
 968 
 969 #define sd_pm_idletimeout_handler       ssd_pm_idletimeout_handler
 970 #define sd_pm_timeout_handler           ssd_pm_timeout_handler
 971 
 972 #define sd_add_buf_to_waitq             ssd_add_buf_to_waitq
 973 #define sdintr                          ssdintr
 974 #define sd_start_cmds                   ssd_start_cmds
 975 #define sd_send_scsi_cmd                ssd_send_scsi_cmd
 976 #define sd_bioclone_alloc               ssd_bioclone_alloc
 977 #define sd_bioclone_free                ssd_bioclone_free
 978 #define sd_shadow_buf_alloc             ssd_shadow_buf_alloc
 979 #define sd_shadow_buf_free              ssd_shadow_buf_free
 980 #define sd_print_transport_rejected_message     \
 981                                         ssd_print_transport_rejected_message
 982 #define sd_retry_command                ssd_retry_command
 983 #define sd_set_retry_bp                 ssd_set_retry_bp
 984 #define sd_send_request_sense_command   ssd_send_request_sense_command
 985 #define sd_start_retry_command          ssd_start_retry_command
 986 #define sd_start_direct_priority_command        \
 987                                         ssd_start_direct_priority_command
 988 #define sd_return_failed_command        ssd_return_failed_command
 989 #define sd_return_failed_command_no_restart     \
 990                                         ssd_return_failed_command_no_restart
 991 #define sd_return_command               ssd_return_command
 992 #define sd_sync_with_callback           ssd_sync_with_callback
 993 #define sdrunout                        ssdrunout
 994 #define sd_mark_rqs_busy                ssd_mark_rqs_busy
 995 #define sd_mark_rqs_idle                ssd_mark_rqs_idle
 996 #define sd_reduce_throttle              ssd_reduce_throttle
 997 #define sd_restore_throttle             ssd_restore_throttle
 998 #define sd_print_incomplete_msg         ssd_print_incomplete_msg
 999 #define sd_init_cdb_limits              ssd_init_cdb_limits
1000 #define sd_pkt_status_good              ssd_pkt_status_good
1001 #define sd_pkt_status_check_condition   ssd_pkt_status_check_condition
1002 #define sd_pkt_status_busy              ssd_pkt_status_busy
1003 #define sd_pkt_status_reservation_conflict      \
1004                                         ssd_pkt_status_reservation_conflict
1005 #define sd_pkt_status_qfull             ssd_pkt_status_qfull
1006 #define sd_handle_request_sense         ssd_handle_request_sense
1007 #define sd_handle_auto_request_sense    ssd_handle_auto_request_sense
1008 #define sd_print_sense_failed_msg       ssd_print_sense_failed_msg
1009 #define sd_validate_sense_data          ssd_validate_sense_data
1010 #define sd_decode_sense                 ssd_decode_sense
1011 #define sd_print_sense_msg              ssd_print_sense_msg
1012 #define sd_sense_key_no_sense           ssd_sense_key_no_sense
1013 #define sd_sense_key_recoverable_error  ssd_sense_key_recoverable_error
1014 #define sd_sense_key_not_ready          ssd_sense_key_not_ready
1015 #define sd_sense_key_medium_or_hardware_error   \
1016                                         ssd_sense_key_medium_or_hardware_error
1017 #define sd_sense_key_illegal_request    ssd_sense_key_illegal_request
1018 #define sd_sense_key_unit_attention     ssd_sense_key_unit_attention
1019 #define sd_sense_key_fail_command       ssd_sense_key_fail_command
1020 #define sd_sense_key_blank_check        ssd_sense_key_blank_check
1021 #define sd_sense_key_aborted_command    ssd_sense_key_aborted_command
1022 #define sd_sense_key_default            ssd_sense_key_default
1023 #define sd_print_retry_msg              ssd_print_retry_msg
1024 #define sd_print_cmd_incomplete_msg     ssd_print_cmd_incomplete_msg
1025 #define sd_pkt_reason_cmd_incomplete    ssd_pkt_reason_cmd_incomplete
1026 #define sd_pkt_reason_cmd_tran_err      ssd_pkt_reason_cmd_tran_err
1027 #define sd_pkt_reason_cmd_reset         ssd_pkt_reason_cmd_reset
1028 #define sd_pkt_reason_cmd_aborted       ssd_pkt_reason_cmd_aborted
1029 #define sd_pkt_reason_cmd_timeout       ssd_pkt_reason_cmd_timeout
1030 #define sd_pkt_reason_cmd_unx_bus_free  ssd_pkt_reason_cmd_unx_bus_free
1031 #define sd_pkt_reason_cmd_tag_reject    ssd_pkt_reason_cmd_tag_reject
1032 #define sd_pkt_reason_default           ssd_pkt_reason_default
1033 #define sd_reset_target                 ssd_reset_target
1034 #define sd_start_stop_unit_callback     ssd_start_stop_unit_callback
1035 #define sd_start_stop_unit_task         ssd_start_stop_unit_task
1036 #define sd_taskq_create                 ssd_taskq_create
1037 #define sd_taskq_delete                 ssd_taskq_delete
1038 #define sd_target_change_task           ssd_target_change_task
1039 #define sd_log_dev_status_event         ssd_log_dev_status_event
1040 #define sd_log_lun_expansion_event      ssd_log_lun_expansion_event
1041 #define sd_log_eject_request_event      ssd_log_eject_request_event
1042 #define sd_media_change_task            ssd_media_change_task
1043 #define sd_handle_mchange               ssd_handle_mchange
1044 #define sd_send_scsi_DOORLOCK           ssd_send_scsi_DOORLOCK
1045 #define sd_send_scsi_READ_CAPACITY      ssd_send_scsi_READ_CAPACITY
1046 #define sd_send_scsi_READ_CAPACITY_16   ssd_send_scsi_READ_CAPACITY_16
1047 #define sd_send_scsi_GET_CONFIGURATION  ssd_send_scsi_GET_CONFIGURATION
1048 #define sd_send_scsi_feature_GET_CONFIGURATION  \
1049                                         sd_send_scsi_feature_GET_CONFIGURATION
1050 #define sd_send_scsi_START_STOP_UNIT    ssd_send_scsi_START_STOP_UNIT
1051 #define sd_send_scsi_INQUIRY            ssd_send_scsi_INQUIRY
1052 #define sd_send_scsi_TEST_UNIT_READY    ssd_send_scsi_TEST_UNIT_READY
1053 #define sd_send_scsi_PERSISTENT_RESERVE_IN      \
1054                                         ssd_send_scsi_PERSISTENT_RESERVE_IN
1055 #define sd_send_scsi_PERSISTENT_RESERVE_OUT     \
1056                                         ssd_send_scsi_PERSISTENT_RESERVE_OUT
1057 #define sd_send_scsi_SYNCHRONIZE_CACHE  ssd_send_scsi_SYNCHRONIZE_CACHE
1058 #define sd_send_scsi_SYNCHRONIZE_CACHE_biodone  \
1059                                         ssd_send_scsi_SYNCHRONIZE_CACHE_biodone
1060 #define sd_send_scsi_MODE_SENSE         ssd_send_scsi_MODE_SENSE
1061 #define sd_send_scsi_MODE_SELECT        ssd_send_scsi_MODE_SELECT
1062 #define sd_send_scsi_RDWR               ssd_send_scsi_RDWR
1063 #define sd_send_scsi_LOG_SENSE          ssd_send_scsi_LOG_SENSE
1064 #define sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION      \
1065                                 ssd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
1066 #define sd_gesn_media_data_valid        ssd_gesn_media_data_valid
1067 #define sd_alloc_rqs                    ssd_alloc_rqs
1068 #define sd_free_rqs                     ssd_free_rqs
1069 #define sd_dump_memory                  ssd_dump_memory
1070 #define sd_get_media_info_com           ssd_get_media_info_com
1071 #define sd_get_media_info               ssd_get_media_info
1072 #define sd_get_media_info_ext           ssd_get_media_info_ext
1073 #define sd_dkio_ctrl_info               ssd_dkio_ctrl_info
1074 #define sd_nvpair_str_decode            ssd_nvpair_str_decode
1075 #define sd_strtok_r                     ssd_strtok_r
1076 #define sd_set_properties               ssd_set_properties
1077 #define sd_get_tunables_from_conf       ssd_get_tunables_from_conf
1078 #define sd_setup_next_xfer              ssd_setup_next_xfer
1079 #define sd_dkio_get_temp                ssd_dkio_get_temp
1080 #define sd_check_mhd                    ssd_check_mhd
1081 #define sd_mhd_watch_cb                 ssd_mhd_watch_cb
1082 #define sd_mhd_watch_incomplete         ssd_mhd_watch_incomplete
1083 #define sd_sname                        ssd_sname
1084 #define sd_mhd_resvd_recover            ssd_mhd_resvd_recover
1085 #define sd_resv_reclaim_thread          ssd_resv_reclaim_thread
1086 #define sd_take_ownership               ssd_take_ownership
1087 #define sd_reserve_release              ssd_reserve_release
1088 #define sd_rmv_resv_reclaim_req         ssd_rmv_resv_reclaim_req
1089 #define sd_mhd_reset_notify_cb          ssd_mhd_reset_notify_cb
1090 #define sd_persistent_reservation_in_read_keys  \
1091                                         ssd_persistent_reservation_in_read_keys
1092 #define sd_persistent_reservation_in_read_resv  \
1093                                         ssd_persistent_reservation_in_read_resv
1094 #define sd_mhdioc_takeown               ssd_mhdioc_takeown
1095 #define sd_mhdioc_failfast              ssd_mhdioc_failfast
1096 #define sd_mhdioc_release               ssd_mhdioc_release
1097 #define sd_mhdioc_register_devid        ssd_mhdioc_register_devid
1098 #define sd_mhdioc_inkeys                ssd_mhdioc_inkeys
1099 #define sd_mhdioc_inresv                ssd_mhdioc_inresv
1100 #define sr_change_blkmode               ssr_change_blkmode
1101 #define sr_change_speed                 ssr_change_speed
1102 #define sr_atapi_change_speed           ssr_atapi_change_speed
1103 #define sr_pause_resume                 ssr_pause_resume
1104 #define sr_play_msf                     ssr_play_msf
1105 #define sr_play_trkind                  ssr_play_trkind
1106 #define sr_read_all_subcodes            ssr_read_all_subcodes
1107 #define sr_read_subchannel              ssr_read_subchannel
1108 #define sr_read_tocentry                ssr_read_tocentry
1109 #define sr_read_tochdr                  ssr_read_tochdr
1110 #define sr_read_cdda                    ssr_read_cdda
1111 #define sr_read_cdxa                    ssr_read_cdxa
1112 #define sr_read_mode1                   ssr_read_mode1
1113 #define sr_read_mode2                   ssr_read_mode2
1114 #define sr_read_cd_mode2                ssr_read_cd_mode2
1115 #define sr_sector_mode                  ssr_sector_mode
1116 #define sr_eject                        ssr_eject
1117 #define sr_ejected                      ssr_ejected
1118 #define sr_check_wp                     ssr_check_wp
1119 #define sd_watch_request_submit         ssd_watch_request_submit
1120 #define sd_check_media                  ssd_check_media
1121 #define sd_media_watch_cb               ssd_media_watch_cb
1122 #define sd_delayed_cv_broadcast         ssd_delayed_cv_broadcast
1123 #define sr_volume_ctrl                  ssr_volume_ctrl
1124 #define sr_read_sony_session_offset     ssr_read_sony_session_offset
1125 #define sd_log_page_supported           ssd_log_page_supported
1126 #define sd_check_for_writable_cd        ssd_check_for_writable_cd
1127 #define sd_wm_cache_constructor         ssd_wm_cache_constructor
1128 #define sd_wm_cache_destructor          ssd_wm_cache_destructor
1129 #define sd_range_lock                   ssd_range_lock
1130 #define sd_get_range                    ssd_get_range
1131 #define sd_free_inlist_wmap             ssd_free_inlist_wmap
1132 #define sd_range_unlock                 ssd_range_unlock
1133 #define sd_read_modify_write_task       ssd_read_modify_write_task
1134 #define sddump_do_read_of_rmw           ssddump_do_read_of_rmw
1135 
1136 #define sd_iostart_chain                ssd_iostart_chain
1137 #define sd_iodone_chain                 ssd_iodone_chain
1138 #define sd_initpkt_map                  ssd_initpkt_map
1139 #define sd_destroypkt_map               ssd_destroypkt_map
1140 #define sd_chain_type_map               ssd_chain_type_map
1141 #define sd_chain_index_map              ssd_chain_index_map
1142 
1143 #define sd_failfast_flushctl            ssd_failfast_flushctl
1144 #define sd_failfast_flushq              ssd_failfast_flushq
1145 #define sd_failfast_flushq_callback     ssd_failfast_flushq_callback
1146 
1147 #define sd_is_lsi                       ssd_is_lsi
1148 #define sd_tg_rdwr                      ssd_tg_rdwr
1149 #define sd_tg_getinfo                   ssd_tg_getinfo
1150 #define sd_rmw_msg_print_handler        ssd_rmw_msg_print_handler
1151 
1152 #endif  /* #if (defined(__fibre)) */
1153 
1154 
1155 int _init(void);
1156 int _fini(void);
1157 int _info(struct modinfo *modinfop);
1158 
1159 /*PRINTFLIKE3*/
1160 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1161 /*PRINTFLIKE3*/
1162 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1163 /*PRINTFLIKE3*/
1164 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1165 
1166 static int sdprobe(dev_info_t *devi);
1167 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1168     void **result);
1169 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1170     int mod_flags, char *name, caddr_t valuep, int *lengthp);
1171 
1172 /*
1173  * Smart probe for parallel scsi
1174  */
1175 static void sd_scsi_probe_cache_init(void);
1176 static void sd_scsi_probe_cache_fini(void);
1177 static void sd_scsi_clear_probe_cache(void);
1178 static int  sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)());
1179 
1180 /*
1181  * Attached luns on target for parallel scsi
1182  */
1183 static void sd_scsi_target_lun_init(void);
1184 static void sd_scsi_target_lun_fini(void);
1185 static int  sd_scsi_get_target_lun_count(dev_info_t *dip, int target);
1186 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag);
1187 
1188 static int      sd_spin_up_unit(sd_ssc_t *ssc);
1189 
1190 /*
1191  * Using sd_ssc_init to establish sd_ssc_t struct
1192  * Using sd_ssc_send to send uscsi internal command
1193  * Using sd_ssc_fini to free sd_ssc_t struct
1194  */
1195 static sd_ssc_t *sd_ssc_init(struct sd_lun *un);
1196 static int sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd,
1197     int flag, enum uio_seg dataspace, int path_flag);
1198 static void sd_ssc_fini(sd_ssc_t *ssc);
1199 
1200 /*
1201  * Using sd_ssc_assessment to set correct type-of-assessment
1202  * Using sd_ssc_post to post ereport & system log
1203  *       sd_ssc_post will call sd_ssc_print to print system log
1204  *       sd_ssc_post will call sd_ssd_ereport_post to post ereport
1205  */
1206 static void sd_ssc_assessment(sd_ssc_t *ssc,
1207     enum sd_type_assessment tp_assess);
1208 
1209 static void sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess);
1210 static void sd_ssc_print(sd_ssc_t *ssc, int sd_severity);
1211 static void sd_ssc_ereport_post(sd_ssc_t *ssc,
1212     enum sd_driver_assessment drv_assess);
1213 
1214 /*
1215  * Using sd_ssc_set_info to mark an un-decodable-data error.
1216  * Using sd_ssc_extract_info to transfer information from internal
1217  *       data structures to sd_ssc_t.
1218  */
1219 static void sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp,
1220     const char *fmt, ...);
1221 static void sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un,
1222     struct scsi_pkt *pktp, struct buf *bp, struct sd_xbuf *xp);
1223 
1224 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1225     enum uio_seg dataspace, int path_flag);
1226 
1227 #ifdef _LP64
1228 static void     sd_enable_descr_sense(sd_ssc_t *ssc);
1229 static void     sd_reenable_dsense_task(void *arg);
1230 #endif /* _LP64 */
1231 
1232 static void     sd_set_mmc_caps(sd_ssc_t *ssc);
1233 
1234 static void sd_read_unit_properties(struct sd_lun *un);
1235 static int  sd_process_sdconf_file(struct sd_lun *un);
1236 static void sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str);
1237 static char *sd_strtok_r(char *string, const char *sepset, char **lasts);
1238 static void sd_set_properties(struct sd_lun *un, char *name, char *value);
1239 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags,
1240     int *data_list, sd_tunables *values);
1241 static void sd_process_sdconf_table(struct sd_lun *un);
1242 static int  sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen);
1243 static int  sd_blank_cmp(struct sd_lun *un, char *id, int idlen);
1244 static int  sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
1245         int list_len, char *dataname_ptr);
1246 static void sd_set_vers1_properties(struct sd_lun *un, int flags,
1247     sd_tunables *prop_list);
1248 
1249 static void sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi,
1250     int reservation_flag);
1251 static int  sd_get_devid(sd_ssc_t *ssc);
1252 static ddi_devid_t sd_create_devid(sd_ssc_t *ssc);
1253 static int  sd_write_deviceid(sd_ssc_t *ssc);
1254 static int  sd_check_vpd_page_support(sd_ssc_t *ssc);
1255 
1256 static void sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi);
1257 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un);
1258 
1259 static int  sd_ddi_suspend(dev_info_t *devi);
1260 static int  sd_ddi_resume(dev_info_t *devi);
1261 static int  sd_pm_state_change(struct sd_lun *un, int level, int flag);
1262 static int  sdpower(dev_info_t *devi, int component, int level);
1263 
1264 static int  sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd);
1265 static int  sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd);
1266 static int  sd_unit_attach(dev_info_t *devi);
1267 static int  sd_unit_detach(dev_info_t *devi);
1268 
1269 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi);
1270 static void sd_create_errstats(struct sd_lun *un, int instance);
1271 static void sd_set_errstats(struct sd_lun *un);
1272 static void sd_set_pstats(struct sd_lun *un);
1273 
1274 static int  sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
1275 static int  sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt);
1276 static int  sd_send_polled_RQS(struct sd_lun *un);
1277 static int  sd_ddi_scsi_poll(struct scsi_pkt *pkt);
1278 
1279 #if (defined(__fibre))
1280 /*
1281  * Event callbacks (photon)
1282  */
1283 static void sd_init_event_callbacks(struct sd_lun *un);
1284 static void  sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *);
1285 #endif
1286 
1287 /*
1288  * Defines for sd_cache_control
1289  */
1290 
1291 #define SD_CACHE_ENABLE         1
1292 #define SD_CACHE_DISABLE        0
1293 #define SD_CACHE_NOCHANGE       -1
1294 
1295 static int   sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag);
1296 static int   sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled);
1297 static void  sd_get_write_cache_changeable(sd_ssc_t *ssc, int *is_changeable);
1298 static void  sd_get_nv_sup(sd_ssc_t *ssc);
1299 static dev_t sd_make_device(dev_info_t *devi);
1300 static void  sd_check_bdc_vpd(sd_ssc_t *ssc);
1301 static void  sd_check_emulation_mode(sd_ssc_t *ssc);
1302 static void  sd_update_block_info(struct sd_lun *un, uint32_t lbasize,
1303         uint64_t capacity);
1304 
1305 /*
1306  * Driver entry point functions.
1307  */
1308 static int  sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
1309 static int  sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p);
1310 static int  sd_ready_and_valid(sd_ssc_t *ssc, int part);
1311 
1312 static void sdmin(struct buf *bp);
1313 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p);
1314 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p);
1315 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1316 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1317 
1318 static int sdstrategy(struct buf *bp);
1319 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *);
1320 
1321 /*
1322  * Function prototypes for layering functions in the iostart chain.
1323  */
1324 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un,
1325         struct buf *bp);
1326 static void sd_mapblocksize_iostart(int index, struct sd_lun *un,
1327         struct buf *bp);
1328 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp);
1329 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un,
1330         struct buf *bp);
1331 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp);
1332 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp);
1333 
1334 /*
1335  * Function prototypes for layering functions in the iodone chain.
1336  */
1337 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp);
1338 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp);
1339 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un,
1340         struct buf *bp);
1341 static void sd_mapblocksize_iodone(int index, struct sd_lun *un,
1342         struct buf *bp);
1343 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp);
1344 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un,
1345         struct buf *bp);
1346 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp);
1347 
1348 /*
1349  * Prototypes for functions to support buf(9S) based IO.
1350  */
1351 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg);
1352 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **);
1353 static void sd_destroypkt_for_buf(struct buf *);
1354 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp,
1355         struct buf *bp, int flags,
1356         int (*callback)(caddr_t), caddr_t callback_arg,
1357         diskaddr_t lba, uint32_t blockcount);
1358 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp,
1359         struct buf *bp, diskaddr_t lba, uint32_t blockcount);
1360 
1361 /*
1362  * Prototypes for functions to support USCSI IO.
1363  */
1364 static int sd_uscsi_strategy(struct buf *bp);
1365 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **);
1366 static void sd_destroypkt_for_uscsi(struct buf *);
1367 
1368 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
1369         uchar_t chain_type, void *pktinfop);
1370 
1371 static int  sd_pm_entry(struct sd_lun *un);
1372 static void sd_pm_exit(struct sd_lun *un);
1373 
1374 static void sd_pm_idletimeout_handler(void *arg);
1375 
1376 /*
1377  * sd_core internal functions (used at the sd_core_io layer).
1378  */
1379 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp);
1380 static void sdintr(struct scsi_pkt *pktp);
1381 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp);
1382 
1383 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1384         enum uio_seg dataspace, int path_flag);
1385 
1386 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen,
1387         daddr_t blkno, int (*func)(struct buf *));
1388 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen,
1389         uint_t bflags, daddr_t blkno, int (*func)(struct buf *));
1390 static void sd_bioclone_free(struct buf *bp);
1391 static void sd_shadow_buf_free(struct buf *bp);
1392 
1393 static void sd_print_transport_rejected_message(struct sd_lun *un,
1394         struct sd_xbuf *xp, int code);
1395 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp,
1396     void *arg, int code);
1397 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp,
1398     void *arg, int code);
1399 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp,
1400     void *arg, int code);
1401 
1402 static void sd_retry_command(struct sd_lun *un, struct buf *bp,
1403         int retry_check_flag,
1404         void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp,
1405                 int c),
1406         void *user_arg, int failure_code,  clock_t retry_delay,
1407         void (*statp)(kstat_io_t *));
1408 
1409 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp,
1410         clock_t retry_delay, void (*statp)(kstat_io_t *));
1411 
1412 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
1413         struct scsi_pkt *pktp);
1414 static void sd_start_retry_command(void *arg);
1415 static void sd_start_direct_priority_command(void *arg);
1416 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp,
1417         int errcode);
1418 static void sd_return_failed_command_no_restart(struct sd_lun *un,
1419         struct buf *bp, int errcode);
1420 static void sd_return_command(struct sd_lun *un, struct buf *bp);
1421 static void sd_sync_with_callback(struct sd_lun *un);
1422 static int sdrunout(caddr_t arg);
1423 
1424 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp);
1425 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp);
1426 
1427 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type);
1428 static void sd_restore_throttle(void *arg);
1429 
1430 static void sd_init_cdb_limits(struct sd_lun *un);
1431 
1432 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
1433         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1434 
1435 /*
1436  * Error handling functions
1437  */
1438 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
1439         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1440 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp,
1441         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1442 static void sd_pkt_status_reservation_conflict(struct sd_lun *un,
1443         struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1444 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
1445         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1446 
1447 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp,
1448         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1449 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
1450         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1451 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp,
1452         struct sd_xbuf *xp, size_t actual_len);
1453 static void sd_decode_sense(struct sd_lun *un, struct buf *bp,
1454         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1455 
1456 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp,
1457         void *arg, int code);
1458 
1459 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
1460         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1461 static void sd_sense_key_recoverable_error(struct sd_lun *un,
1462         uint8_t *sense_datap,
1463         struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1464 static void sd_sense_key_not_ready(struct sd_lun *un,
1465         uint8_t *sense_datap,
1466         struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1467 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
1468         uint8_t *sense_datap,
1469         struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1470 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
1471         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1472 static void sd_sense_key_unit_attention(struct sd_lun *un,
1473         uint8_t *sense_datap,
1474         struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1475 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
1476         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1477 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
1478         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1479 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
1480         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1481 static void sd_sense_key_default(struct sd_lun *un,
1482         uint8_t *sense_datap,
1483         struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1484 
1485 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp,
1486         void *arg, int flag);
1487 
1488 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
1489         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1490 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
1491         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1492 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
1493         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1494 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
1495         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1496 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
1497         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1498 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
1499         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1500 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
1501         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1502 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
1503         struct sd_xbuf *xp, struct scsi_pkt *pktp);
1504 
1505 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp);
1506 
1507 static void sd_start_stop_unit_callback(void *arg);
1508 static void sd_start_stop_unit_task(void *arg);
1509 
1510 static void sd_taskq_create(void);
1511 static void sd_taskq_delete(void);
1512 static void sd_target_change_task(void *arg);
1513 static void sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag);
1514 static void sd_log_lun_expansion_event(struct sd_lun *un, int km_flag);
1515 static void sd_log_eject_request_event(struct sd_lun *un, int km_flag);
1516 static void sd_media_change_task(void *arg);
1517 
1518 static int sd_handle_mchange(struct sd_lun *un);
1519 static int sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag);
1520 static int sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp,
1521         uint32_t *lbap, int path_flag);
1522 static int sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
1523         uint32_t *lbap, uint32_t *psp, int path_flag);
1524 static int sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag,
1525         int flag, int path_flag);
1526 static int sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr,
1527         size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp);
1528 static int sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag);
1529 static int sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc,
1530         uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp);
1531 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc,
1532         uchar_t usr_cmd, uchar_t *usr_bufp);
1533 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un,
1534         struct dk_callback *dkc);
1535 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp);
1536 static int sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc,
1537         struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1538         uchar_t *bufaddr, uint_t buflen, int path_flag);
1539 static int sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
1540         struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1541         uchar_t *bufaddr, uint_t buflen, char feature, int path_flag);
1542 static int sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize,
1543         uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag);
1544 static int sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize,
1545         uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag);
1546 static int sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
1547         size_t buflen, daddr_t start_block, int path_flag);
1548 #define sd_send_scsi_READ(ssc, bufaddr, buflen, start_block, path_flag) \
1549         sd_send_scsi_RDWR(ssc, SCMD_READ, bufaddr, buflen, start_block, \
1550         path_flag)
1551 #define sd_send_scsi_WRITE(ssc, bufaddr, buflen, start_block, path_flag)\
1552         sd_send_scsi_RDWR(ssc, SCMD_WRITE, bufaddr, buflen, start_block,\
1553         path_flag)
1554 
1555 static int sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr,
1556         uint16_t buflen, uchar_t page_code, uchar_t page_control,
1557         uint16_t param_ptr, int path_flag);
1558 static int sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc,
1559         uchar_t *bufaddr, size_t buflen, uchar_t class_req);
1560 static boolean_t sd_gesn_media_data_valid(uchar_t *data);
1561 
1562 static int  sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un);
1563 static void sd_free_rqs(struct sd_lun *un);
1564 
1565 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title,
1566         uchar_t *data, int len, int fmt);
1567 static void sd_panic_for_res_conflict(struct sd_lun *un);
1568 
1569 /*
1570  * Disk Ioctl Function Prototypes
1571  */
1572 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag);
1573 static int sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag);
1574 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag);
1575 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag);
1576 
1577 /*
1578  * Multi-host Ioctl Prototypes
1579  */
1580 static int sd_check_mhd(dev_t dev, int interval);
1581 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1582 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt);
1583 static char *sd_sname(uchar_t status);
1584 static void sd_mhd_resvd_recover(void *arg);
1585 static void sd_resv_reclaim_thread();
1586 static int sd_take_ownership(dev_t dev, struct mhioctkown *p);
1587 static int sd_reserve_release(dev_t dev, int cmd);
1588 static void sd_rmv_resv_reclaim_req(dev_t dev);
1589 static void sd_mhd_reset_notify_cb(caddr_t arg);
1590 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un,
1591         mhioc_inkeys_t *usrp, int flag);
1592 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un,
1593         mhioc_inresvs_t *usrp, int flag);
1594 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag);
1595 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag);
1596 static int sd_mhdioc_release(dev_t dev);
1597 static int sd_mhdioc_register_devid(dev_t dev);
1598 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag);
1599 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag);
1600 
1601 /*
1602  * SCSI removable prototypes
1603  */
1604 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag);
1605 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1606 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1607 static int sr_pause_resume(dev_t dev, int mode);
1608 static int sr_play_msf(dev_t dev, caddr_t data, int flag);
1609 static int sr_play_trkind(dev_t dev, caddr_t data, int flag);
1610 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag);
1611 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag);
1612 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag);
1613 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag);
1614 static int sr_read_cdda(dev_t dev, caddr_t data, int flag);
1615 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag);
1616 static int sr_read_mode1(dev_t dev, caddr_t data, int flag);
1617 static int sr_read_mode2(dev_t dev, caddr_t data, int flag);
1618 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag);
1619 static int sr_sector_mode(dev_t dev, uint32_t blksize);
1620 static int sr_eject(dev_t dev);
1621 static void sr_ejected(register struct sd_lun *un);
1622 static int sr_check_wp(dev_t dev);
1623 static opaque_t sd_watch_request_submit(struct sd_lun *un);
1624 static int sd_check_media(dev_t dev, enum dkio_state state);
1625 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1626 static void sd_delayed_cv_broadcast(void *arg);
1627 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag);
1628 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag);
1629 
1630 static int sd_log_page_supported(sd_ssc_t *ssc, int log_page);
1631 
1632 /*
1633  * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions.
1634  */
1635 static void sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag);
1636 static int sd_wm_cache_constructor(void *wm, void *un, int flags);
1637 static void sd_wm_cache_destructor(void *wm, void *un);
1638 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb,
1639     daddr_t endb, ushort_t typ);
1640 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb,
1641     daddr_t endb);
1642 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp);
1643 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm);
1644 static void sd_read_modify_write_task(void * arg);
1645 static int
1646 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
1647     struct buf **bpp);
1648 
1649 
1650 /*
1651  * Function prototypes for failfast support.
1652  */
1653 static void sd_failfast_flushq(struct sd_lun *un);
1654 static int sd_failfast_flushq_callback(struct buf *bp);
1655 
1656 /*
1657  * Function prototypes to check for lsi devices
1658  */
1659 static void sd_is_lsi(struct sd_lun *un);
1660 
1661 /*
1662  * Function prototypes for partial DMA support
1663  */
1664 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
1665                 struct scsi_pkt *pkt, struct sd_xbuf *xp);
1666 
1667 
1668 /* Function prototypes for cmlb */
1669 static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
1670     diskaddr_t start_block, size_t reqlength, void *tg_cookie);
1671 
1672 static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie);
1673 
1674 /*
1675  * For printing RMW warning message timely
1676  */
1677 static void sd_rmw_msg_print_handler(void *arg);
1678 
1679 /*
1680  * Constants for failfast support:
1681  *
1682  * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO
1683  * failfast processing being performed.
1684  *
1685  * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing
1686  * failfast processing on all bufs with B_FAILFAST set.
1687  */
1688 
1689 #define SD_FAILFAST_INACTIVE            0
1690 #define SD_FAILFAST_ACTIVE              1
1691 
1692 /*
1693  * Bitmask to control behavior of buf(9S) flushes when a transition to
1694  * the failfast state occurs. Optional bits include:
1695  *
1696  * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that
1697  * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will
1698  * be flushed.
1699  *
1700  * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the
1701  * driver, in addition to the regular wait queue. This includes the xbuf
1702  * queues. When clear, only the driver's wait queue will be flushed.
1703  */
1704 #define SD_FAILFAST_FLUSH_ALL_BUFS      0x01
1705 #define SD_FAILFAST_FLUSH_ALL_QUEUES    0x02
1706 
1707 /*
1708  * The default behavior is to only flush bufs that have B_FAILFAST set, but
1709  * to flush all queues within the driver.
1710  */
1711 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES;
1712 
1713 
1714 /*
1715  * SD Testing Fault Injection
1716  */
1717 #ifdef SD_FAULT_INJECTION
1718 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un);
1719 static void sd_faultinjection(struct scsi_pkt *pktp);
1720 static void sd_injection_log(char *buf, struct sd_lun *un);
1721 #endif
1722 
1723 /*
1724  * Device driver ops vector
1725  */
1726 static struct cb_ops sd_cb_ops = {
1727         sdopen,                 /* open */
1728         sdclose,                /* close */
1729         sdstrategy,             /* strategy */
1730         nodev,                  /* print */
1731         sddump,                 /* dump */
1732         sdread,                 /* read */
1733         sdwrite,                /* write */
1734         sdioctl,                /* ioctl */
1735         nodev,                  /* devmap */
1736         nodev,                  /* mmap */
1737         nodev,                  /* segmap */
1738         nochpoll,               /* poll */
1739         sd_prop_op,             /* cb_prop_op */
1740         0,                      /* streamtab  */
1741         D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */
1742         CB_REV,                 /* cb_rev */
1743         sdaread,                /* async I/O read entry point */
1744         sdawrite                /* async I/O write entry point */
1745 };
1746 
1747 struct dev_ops sd_ops = {
1748         DEVO_REV,               /* devo_rev, */
1749         0,                      /* refcnt  */
1750         sdinfo,                 /* info */
1751         nulldev,                /* identify */
1752         sdprobe,                /* probe */
1753         sdattach,               /* attach */
1754         sddetach,               /* detach */
1755         nodev,                  /* reset */
1756         &sd_cb_ops,         /* driver operations */
1757         NULL,                   /* bus operations */
1758         sdpower,                /* power */
1759         ddi_quiesce_not_needed,         /* quiesce */
1760 };
1761 
1762 /*
1763  * This is the loadable module wrapper.
1764  */
1765 #include <sys/modctl.h>
1766 
1767 static struct modldrv modldrv = {
1768         &mod_driverops,             /* Type of module. This one is a driver */
1769         SD_MODULE_NAME,         /* Module name. */
1770         &sd_ops                     /* driver ops */
1771 };
1772 
1773 static struct modlinkage modlinkage = {
1774         MODREV_1, &modldrv, NULL
1775 };
1776 
1777 static cmlb_tg_ops_t sd_tgops = {
1778         TG_DK_OPS_VERSION_1,
1779         sd_tg_rdwr,
1780         sd_tg_getinfo
1781 };
1782 
1783 static struct scsi_asq_key_strings sd_additional_codes[] = {
1784         0x81, 0, "Logical Unit is Reserved",
1785         0x85, 0, "Audio Address Not Valid",
1786         0xb6, 0, "Media Load Mechanism Failed",
1787         0xB9, 0, "Audio Play Operation Aborted",
1788         0xbf, 0, "Buffer Overflow for Read All Subcodes Command",
1789         0x53, 2, "Medium removal prevented",
1790         0x6f, 0, "Authentication failed during key exchange",
1791         0x6f, 1, "Key not present",
1792         0x6f, 2, "Key not established",
1793         0x6f, 3, "Read without proper authentication",
1794         0x6f, 4, "Mismatched region to this logical unit",
1795         0x6f, 5, "Region reset count error",
1796         0xffff, 0x0, NULL
1797 };
1798 
1799 
1800 /*
1801  * Struct for passing printing information for sense data messages
1802  */
1803 struct sd_sense_info {
1804         int     ssi_severity;
1805         int     ssi_pfa_flag;
1806 };
1807 
1808 /*
1809  * Table of function pointers for iostart-side routines. Separate "chains"
1810  * of layered function calls are formed by placing the function pointers
1811  * sequentially in the desired order. Functions are called according to an
1812  * incrementing table index ordering. The last function in each chain must
1813  * be sd_core_iostart(). The corresponding iodone-side routines are expected
1814  * in the sd_iodone_chain[] array.
1815  *
1816  * Note: It may seem more natural to organize both the iostart and iodone
1817  * functions together, into an array of structures (or some similar
1818  * organization) with a common index, rather than two separate arrays which
1819  * must be maintained in synchronization. The purpose of this division is
1820  * to achieve improved performance: individual arrays allows for more
1821  * effective cache line utilization on certain platforms.
1822  */
1823 
1824 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp);
1825 
1826 
1827 static sd_chain_t sd_iostart_chain[] = {
1828 
1829         /* Chain for buf IO for disk drive targets (PM enabled) */
1830         sd_mapblockaddr_iostart,        /* Index: 0 */
1831         sd_pm_iostart,                  /* Index: 1 */
1832         sd_core_iostart,                /* Index: 2 */
1833 
1834         /* Chain for buf IO for disk drive targets (PM disabled) */
1835         sd_mapblockaddr_iostart,        /* Index: 3 */
1836         sd_core_iostart,                /* Index: 4 */
1837 
1838         /*
1839          * Chain for buf IO for removable-media or large sector size
1840          * disk drive targets with RMW needed (PM enabled)
1841          */
1842         sd_mapblockaddr_iostart,        /* Index: 5 */
1843         sd_mapblocksize_iostart,        /* Index: 6 */
1844         sd_pm_iostart,                  /* Index: 7 */
1845         sd_core_iostart,                /* Index: 8 */
1846 
1847         /*
1848          * Chain for buf IO for removable-media or large sector size
1849          * disk drive targets with RMW needed (PM disabled)
1850          */
1851         sd_mapblockaddr_iostart,        /* Index: 9 */
1852         sd_mapblocksize_iostart,        /* Index: 10 */
1853         sd_core_iostart,                /* Index: 11 */
1854 
1855         /* Chain for buf IO for disk drives with checksumming (PM enabled) */
1856         sd_mapblockaddr_iostart,        /* Index: 12 */
1857         sd_checksum_iostart,            /* Index: 13 */
1858         sd_pm_iostart,                  /* Index: 14 */
1859         sd_core_iostart,                /* Index: 15 */
1860 
1861         /* Chain for buf IO for disk drives with checksumming (PM disabled) */
1862         sd_mapblockaddr_iostart,        /* Index: 16 */
1863         sd_checksum_iostart,            /* Index: 17 */
1864         sd_core_iostart,                /* Index: 18 */
1865 
1866         /* Chain for USCSI commands (all targets) */
1867         sd_pm_iostart,                  /* Index: 19 */
1868         sd_core_iostart,                /* Index: 20 */
1869 
1870         /* Chain for checksumming USCSI commands (all targets) */
1871         sd_checksum_uscsi_iostart,      /* Index: 21 */
1872         sd_pm_iostart,                  /* Index: 22 */
1873         sd_core_iostart,                /* Index: 23 */
1874 
1875         /* Chain for "direct" USCSI commands (all targets) */
1876         sd_core_iostart,                /* Index: 24 */
1877 
1878         /* Chain for "direct priority" USCSI commands (all targets) */
1879         sd_core_iostart,                /* Index: 25 */
1880 
1881         /*
1882          * Chain for buf IO for large sector size disk drive targets
1883          * with RMW needed with checksumming (PM enabled)
1884          */
1885         sd_mapblockaddr_iostart,        /* Index: 26 */
1886         sd_mapblocksize_iostart,        /* Index: 27 */
1887         sd_checksum_iostart,            /* Index: 28 */
1888         sd_pm_iostart,                  /* Index: 29 */
1889         sd_core_iostart,                /* Index: 30 */
1890 
1891         /*
1892          * Chain for buf IO for large sector size disk drive targets
1893          * with RMW needed with checksumming (PM disabled)
1894          */
1895         sd_mapblockaddr_iostart,        /* Index: 31 */
1896         sd_mapblocksize_iostart,        /* Index: 32 */
1897         sd_checksum_iostart,            /* Index: 33 */
1898         sd_core_iostart,                /* Index: 34 */
1899 
1900 };
1901 
1902 /*
1903  * Macros to locate the first function of each iostart chain in the
1904  * sd_iostart_chain[] array. These are located by the index in the array.
1905  */
1906 #define SD_CHAIN_DISK_IOSTART                   0
1907 #define SD_CHAIN_DISK_IOSTART_NO_PM             3
1908 #define SD_CHAIN_MSS_DISK_IOSTART               5
1909 #define SD_CHAIN_RMMEDIA_IOSTART                5
1910 #define SD_CHAIN_MSS_DISK_IOSTART_NO_PM         9
1911 #define SD_CHAIN_RMMEDIA_IOSTART_NO_PM          9
1912 #define SD_CHAIN_CHKSUM_IOSTART                 12
1913 #define SD_CHAIN_CHKSUM_IOSTART_NO_PM           16
1914 #define SD_CHAIN_USCSI_CMD_IOSTART              19
1915 #define SD_CHAIN_USCSI_CHKSUM_IOSTART           21
1916 #define SD_CHAIN_DIRECT_CMD_IOSTART             24
1917 #define SD_CHAIN_PRIORITY_CMD_IOSTART           25
1918 #define SD_CHAIN_MSS_CHKSUM_IOSTART             26
1919 #define SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM       31
1920 
1921 
1922 /*
1923  * Table of function pointers for the iodone-side routines for the driver-
1924  * internal layering mechanism.  The calling sequence for iodone routines
1925  * uses a decrementing table index, so the last routine called in a chain
1926  * must be at the lowest array index location for that chain.  The last
1927  * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs)
1928  * or sd_uscsi_iodone() (for uscsi IOs).  Other than this, the ordering
1929  * of the functions in an iodone side chain must correspond to the ordering
1930  * of the iostart routines for that chain.  Note that there is no iodone
1931  * side routine that corresponds to sd_core_iostart(), so there is no
1932  * entry in the table for this.
1933  */
1934 
1935 static sd_chain_t sd_iodone_chain[] = {
1936 
1937         /* Chain for buf IO for disk drive targets (PM enabled) */
1938         sd_buf_iodone,                  /* Index: 0 */
1939         sd_mapblockaddr_iodone,         /* Index: 1 */
1940         sd_pm_iodone,                   /* Index: 2 */
1941 
1942         /* Chain for buf IO for disk drive targets (PM disabled) */
1943         sd_buf_iodone,                  /* Index: 3 */
1944         sd_mapblockaddr_iodone,         /* Index: 4 */
1945 
1946         /*
1947          * Chain for buf IO for removable-media or large sector size
1948          * disk drive targets with RMW needed (PM enabled)
1949          */
1950         sd_buf_iodone,                  /* Index: 5 */
1951         sd_mapblockaddr_iodone,         /* Index: 6 */
1952         sd_mapblocksize_iodone,         /* Index: 7 */
1953         sd_pm_iodone,                   /* Index: 8 */
1954 
1955         /*
1956          * Chain for buf IO for removable-media or large sector size
1957          * disk drive targets with RMW needed (PM disabled)
1958          */
1959         sd_buf_iodone,                  /* Index: 9 */
1960         sd_mapblockaddr_iodone,         /* Index: 10 */
1961         sd_mapblocksize_iodone,         /* Index: 11 */
1962 
1963         /* Chain for buf IO for disk drives with checksumming (PM enabled) */
1964         sd_buf_iodone,                  /* Index: 12 */
1965         sd_mapblockaddr_iodone,         /* Index: 13 */
1966         sd_checksum_iodone,             /* Index: 14 */
1967         sd_pm_iodone,                   /* Index: 15 */
1968 
1969         /* Chain for buf IO for disk drives with checksumming (PM disabled) */
1970         sd_buf_iodone,                  /* Index: 16 */
1971         sd_mapblockaddr_iodone,         /* Index: 17 */
1972         sd_checksum_iodone,             /* Index: 18 */
1973 
1974         /* Chain for USCSI commands (non-checksum targets) */
1975         sd_uscsi_iodone,                /* Index: 19 */
1976         sd_pm_iodone,                   /* Index: 20 */
1977 
1978         /* Chain for USCSI commands (checksum targets) */
1979         sd_uscsi_iodone,                /* Index: 21 */
1980         sd_checksum_uscsi_iodone,       /* Index: 22 */
1981         sd_pm_iodone,                   /* Index: 22 */
1982 
1983         /* Chain for "direct" USCSI commands (all targets) */
1984         sd_uscsi_iodone,                /* Index: 24 */
1985 
1986         /* Chain for "direct priority" USCSI commands (all targets) */
1987         sd_uscsi_iodone,                /* Index: 25 */
1988 
1989         /*
1990          * Chain for buf IO for large sector size disk drive targets
1991          * with checksumming (PM enabled)
1992          */
1993         sd_buf_iodone,                  /* Index: 26 */
1994         sd_mapblockaddr_iodone,         /* Index: 27 */
1995         sd_mapblocksize_iodone,         /* Index: 28 */
1996         sd_checksum_iodone,             /* Index: 29 */
1997         sd_pm_iodone,                   /* Index: 30 */
1998 
1999         /*
2000          * Chain for buf IO for large sector size disk drive targets
2001          * with checksumming (PM disabled)
2002          */
2003         sd_buf_iodone,                  /* Index: 31 */
2004         sd_mapblockaddr_iodone,         /* Index: 32 */
2005         sd_mapblocksize_iodone,         /* Index: 33 */
2006         sd_checksum_iodone,             /* Index: 34 */
2007 };
2008 
2009 
2010 /*
2011  * Macros to locate the "first" function in the sd_iodone_chain[] array for
2012  * each iodone-side chain. These are located by the array index, but as the
2013  * iodone side functions are called in a decrementing-index order, the
2014  * highest index number in each chain must be specified (as these correspond
2015  * to the first function in the iodone chain that will be called by the core
2016  * at IO completion time).
2017  */
2018 
2019 #define SD_CHAIN_DISK_IODONE                    2
2020 #define SD_CHAIN_DISK_IODONE_NO_PM              4
2021 #define SD_CHAIN_RMMEDIA_IODONE                 8
2022 #define SD_CHAIN_MSS_DISK_IODONE                8
2023 #define SD_CHAIN_RMMEDIA_IODONE_NO_PM           11
2024 #define SD_CHAIN_MSS_DISK_IODONE_NO_PM          11
2025 #define SD_CHAIN_CHKSUM_IODONE                  15
2026 #define SD_CHAIN_CHKSUM_IODONE_NO_PM            18
2027 #define SD_CHAIN_USCSI_CMD_IODONE               20
2028 #define SD_CHAIN_USCSI_CHKSUM_IODONE            22
2029 #define SD_CHAIN_DIRECT_CMD_IODONE              24
2030 #define SD_CHAIN_PRIORITY_CMD_IODONE            25
2031 #define SD_CHAIN_MSS_CHKSUM_IODONE              30
2032 #define SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM        34
2033 
2034 
2035 
2036 /*
2037  * Array to map a layering chain index to the appropriate initpkt routine.
2038  * The redundant entries are present so that the index used for accessing
2039  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2040  * with this table as well.
2041  */
2042 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **);
2043 
2044 static sd_initpkt_t     sd_initpkt_map[] = {
2045 
2046         /* Chain for buf IO for disk drive targets (PM enabled) */
2047         sd_initpkt_for_buf,             /* Index: 0 */
2048         sd_initpkt_for_buf,             /* Index: 1 */
2049         sd_initpkt_for_buf,             /* Index: 2 */
2050 
2051         /* Chain for buf IO for disk drive targets (PM disabled) */
2052         sd_initpkt_for_buf,             /* Index: 3 */
2053         sd_initpkt_for_buf,             /* Index: 4 */
2054 
2055         /*
2056          * Chain for buf IO for removable-media or large sector size
2057          * disk drive targets (PM enabled)
2058          */
2059         sd_initpkt_for_buf,             /* Index: 5 */
2060         sd_initpkt_for_buf,             /* Index: 6 */
2061         sd_initpkt_for_buf,             /* Index: 7 */
2062         sd_initpkt_for_buf,             /* Index: 8 */
2063 
2064         /*
2065          * Chain for buf IO for removable-media or large sector size
2066          * disk drive targets (PM disabled)
2067          */
2068         sd_initpkt_for_buf,             /* Index: 9 */
2069         sd_initpkt_for_buf,             /* Index: 10 */
2070         sd_initpkt_for_buf,             /* Index: 11 */
2071 
2072         /* Chain for buf IO for disk drives with checksumming (PM enabled) */
2073         sd_initpkt_for_buf,             /* Index: 12 */
2074         sd_initpkt_for_buf,             /* Index: 13 */
2075         sd_initpkt_for_buf,             /* Index: 14 */
2076         sd_initpkt_for_buf,             /* Index: 15 */
2077 
2078         /* Chain for buf IO for disk drives with checksumming (PM disabled) */
2079         sd_initpkt_for_buf,             /* Index: 16 */
2080         sd_initpkt_for_buf,             /* Index: 17 */
2081         sd_initpkt_for_buf,             /* Index: 18 */
2082 
2083         /* Chain for USCSI commands (non-checksum targets) */
2084         sd_initpkt_for_uscsi,           /* Index: 19 */
2085         sd_initpkt_for_uscsi,           /* Index: 20 */
2086 
2087         /* Chain for USCSI commands (checksum targets) */
2088         sd_initpkt_for_uscsi,           /* Index: 21 */
2089         sd_initpkt_for_uscsi,           /* Index: 22 */
2090         sd_initpkt_for_uscsi,           /* Index: 22 */
2091 
2092         /* Chain for "direct" USCSI commands (all targets) */
2093         sd_initpkt_for_uscsi,           /* Index: 24 */
2094 
2095         /* Chain for "direct priority" USCSI commands (all targets) */
2096         sd_initpkt_for_uscsi,           /* Index: 25 */
2097 
2098         /*
2099          * Chain for buf IO for large sector size disk drive targets
2100          * with checksumming (PM enabled)
2101          */
2102         sd_initpkt_for_buf,             /* Index: 26 */
2103         sd_initpkt_for_buf,             /* Index: 27 */
2104         sd_initpkt_for_buf,             /* Index: 28 */
2105         sd_initpkt_for_buf,             /* Index: 29 */
2106         sd_initpkt_for_buf,             /* Index: 30 */
2107 
2108         /*
2109          * Chain for buf IO for large sector size disk drive targets
2110          * with checksumming (PM disabled)
2111          */
2112         sd_initpkt_for_buf,             /* Index: 31 */
2113         sd_initpkt_for_buf,             /* Index: 32 */
2114         sd_initpkt_for_buf,             /* Index: 33 */
2115         sd_initpkt_for_buf,             /* Index: 34 */
2116 };
2117 
2118 
2119 /*
2120  * Array to map a layering chain index to the appropriate destroypktpkt routine.
2121  * The redundant entries are present so that the index used for accessing
2122  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2123  * with this table as well.
2124  */
2125 typedef void (*sd_destroypkt_t)(struct buf *);
2126 
2127 static sd_destroypkt_t  sd_destroypkt_map[] = {
2128 
2129         /* Chain for buf IO for disk drive targets (PM enabled) */
2130         sd_destroypkt_for_buf,          /* Index: 0 */
2131         sd_destroypkt_for_buf,          /* Index: 1 */
2132         sd_destroypkt_for_buf,          /* Index: 2 */
2133 
2134         /* Chain for buf IO for disk drive targets (PM disabled) */
2135         sd_destroypkt_for_buf,          /* Index: 3 */
2136         sd_destroypkt_for_buf,          /* Index: 4 */
2137 
2138         /*
2139          * Chain for buf IO for removable-media or large sector size
2140          * disk drive targets (PM enabled)
2141          */
2142         sd_destroypkt_for_buf,          /* Index: 5 */
2143         sd_destroypkt_for_buf,          /* Index: 6 */
2144         sd_destroypkt_for_buf,          /* Index: 7 */
2145         sd_destroypkt_for_buf,          /* Index: 8 */
2146 
2147         /*
2148          * Chain for buf IO for removable-media or large sector size
2149          * disk drive targets (PM disabled)
2150          */
2151         sd_destroypkt_for_buf,          /* Index: 9 */
2152         sd_destroypkt_for_buf,          /* Index: 10 */
2153         sd_destroypkt_for_buf,          /* Index: 11 */
2154 
2155         /* Chain for buf IO for disk drives with checksumming (PM enabled) */
2156         sd_destroypkt_for_buf,          /* Index: 12 */
2157         sd_destroypkt_for_buf,          /* Index: 13 */
2158         sd_destroypkt_for_buf,          /* Index: 14 */
2159         sd_destroypkt_for_buf,          /* Index: 15 */
2160 
2161         /* Chain for buf IO for disk drives with checksumming (PM disabled) */
2162         sd_destroypkt_for_buf,          /* Index: 16 */
2163         sd_destroypkt_for_buf,          /* Index: 17 */
2164         sd_destroypkt_for_buf,          /* Index: 18 */
2165 
2166         /* Chain for USCSI commands (non-checksum targets) */
2167         sd_destroypkt_for_uscsi,        /* Index: 19 */
2168         sd_destroypkt_for_uscsi,        /* Index: 20 */
2169 
2170         /* Chain for USCSI commands (checksum targets) */
2171         sd_destroypkt_for_uscsi,        /* Index: 21 */
2172         sd_destroypkt_for_uscsi,        /* Index: 22 */
2173         sd_destroypkt_for_uscsi,        /* Index: 22 */
2174 
2175         /* Chain for "direct" USCSI commands (all targets) */
2176         sd_destroypkt_for_uscsi,        /* Index: 24 */
2177 
2178         /* Chain for "direct priority" USCSI commands (all targets) */
2179         sd_destroypkt_for_uscsi,        /* Index: 25 */
2180 
2181         /*
2182          * Chain for buf IO for large sector size disk drive targets
2183          * with checksumming (PM disabled)
2184          */
2185         sd_destroypkt_for_buf,          /* Index: 26 */
2186         sd_destroypkt_for_buf,          /* Index: 27 */
2187         sd_destroypkt_for_buf,          /* Index: 28 */
2188         sd_destroypkt_for_buf,          /* Index: 29 */
2189         sd_destroypkt_for_buf,          /* Index: 30 */
2190 
2191         /*
2192          * Chain for buf IO for large sector size disk drive targets
2193          * with checksumming (PM enabled)
2194          */
2195         sd_destroypkt_for_buf,          /* Index: 31 */
2196         sd_destroypkt_for_buf,          /* Index: 32 */
2197         sd_destroypkt_for_buf,          /* Index: 33 */
2198         sd_destroypkt_for_buf,          /* Index: 34 */
2199 };
2200 
2201 
2202 
2203 /*
2204  * Array to map a layering chain index to the appropriate chain "type".
2205  * The chain type indicates a specific property/usage of the chain.
2206  * The redundant entries are present so that the index used for accessing
2207  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2208  * with this table as well.
2209  */
2210 
2211 #define SD_CHAIN_NULL                   0       /* for the special RQS cmd */
2212 #define SD_CHAIN_BUFIO                  1       /* regular buf IO */
2213 #define SD_CHAIN_USCSI                  2       /* regular USCSI commands */
2214 #define SD_CHAIN_DIRECT                 3       /* uscsi, w/ bypass power mgt */
2215 #define SD_CHAIN_DIRECT_PRIORITY        4       /* uscsi, w/ bypass power mgt */
2216                                                 /* (for error recovery) */
2217 
2218 static int sd_chain_type_map[] = {
2219 
2220         /* Chain for buf IO for disk drive targets (PM enabled) */
2221         SD_CHAIN_BUFIO,                 /* Index: 0 */
2222         SD_CHAIN_BUFIO,                 /* Index: 1 */
2223         SD_CHAIN_BUFIO,                 /* Index: 2 */
2224 
2225         /* Chain for buf IO for disk drive targets (PM disabled) */
2226         SD_CHAIN_BUFIO,                 /* Index: 3 */
2227         SD_CHAIN_BUFIO,                 /* Index: 4 */
2228 
2229         /*
2230          * Chain for buf IO for removable-media or large sector size
2231          * disk drive targets (PM enabled)
2232          */
2233         SD_CHAIN_BUFIO,                 /* Index: 5 */
2234         SD_CHAIN_BUFIO,                 /* Index: 6 */
2235         SD_CHAIN_BUFIO,                 /* Index: 7 */
2236         SD_CHAIN_BUFIO,                 /* Index: 8 */
2237 
2238         /*
2239          * Chain for buf IO for removable-media or large sector size
2240          * disk drive targets (PM disabled)
2241          */
2242         SD_CHAIN_BUFIO,                 /* Index: 9 */
2243         SD_CHAIN_BUFIO,                 /* Index: 10 */
2244         SD_CHAIN_BUFIO,                 /* Index: 11 */
2245 
2246         /* Chain for buf IO for disk drives with checksumming (PM enabled) */
2247         SD_CHAIN_BUFIO,                 /* Index: 12 */
2248         SD_CHAIN_BUFIO,                 /* Index: 13 */
2249         SD_CHAIN_BUFIO,                 /* Index: 14 */
2250         SD_CHAIN_BUFIO,                 /* Index: 15 */
2251 
2252         /* Chain for buf IO for disk drives with checksumming (PM disabled) */
2253         SD_CHAIN_BUFIO,                 /* Index: 16 */
2254         SD_CHAIN_BUFIO,                 /* Index: 17 */
2255         SD_CHAIN_BUFIO,                 /* Index: 18 */
2256 
2257         /* Chain for USCSI commands (non-checksum targets) */
2258         SD_CHAIN_USCSI,                 /* Index: 19 */
2259         SD_CHAIN_USCSI,                 /* Index: 20 */
2260 
2261         /* Chain for USCSI commands (checksum targets) */
2262         SD_CHAIN_USCSI,                 /* Index: 21 */
2263         SD_CHAIN_USCSI,                 /* Index: 22 */
2264         SD_CHAIN_USCSI,                 /* Index: 23 */
2265 
2266         /* Chain for "direct" USCSI commands (all targets) */
2267         SD_CHAIN_DIRECT,                /* Index: 24 */
2268 
2269         /* Chain for "direct priority" USCSI commands (all targets) */
2270         SD_CHAIN_DIRECT_PRIORITY,       /* Index: 25 */
2271 
2272         /*
2273          * Chain for buf IO for large sector size disk drive targets
2274          * with checksumming (PM enabled)
2275          */
2276         SD_CHAIN_BUFIO,                 /* Index: 26 */
2277         SD_CHAIN_BUFIO,                 /* Index: 27 */
2278         SD_CHAIN_BUFIO,                 /* Index: 28 */
2279         SD_CHAIN_BUFIO,                 /* Index: 29 */
2280         SD_CHAIN_BUFIO,                 /* Index: 30 */
2281 
2282         /*
2283          * Chain for buf IO for large sector size disk drive targets
2284          * with checksumming (PM disabled)
2285          */
2286         SD_CHAIN_BUFIO,                 /* Index: 31 */
2287         SD_CHAIN_BUFIO,                 /* Index: 32 */
2288         SD_CHAIN_BUFIO,                 /* Index: 33 */
2289         SD_CHAIN_BUFIO,                 /* Index: 34 */
2290 };
2291 
2292 
2293 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */
2294 #define SD_IS_BUFIO(xp)                 \
2295         (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO)
2296 
2297 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */
2298 #define SD_IS_DIRECT_PRIORITY(xp)       \
2299         (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY)
2300 
2301 
2302 
2303 /*
2304  * Struct, array, and macros to map a specific chain to the appropriate
2305  * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays.
2306  *
2307  * The sd_chain_index_map[] array is used at attach time to set the various
2308  * un_xxx_chain type members of the sd_lun softstate to the specific layering
2309  * chain to be used with the instance. This allows different instances to use
2310  * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart
2311  * and xb_chain_iodone index values in the sd_xbuf are initialized to these
2312  * values at sd_xbuf init time, this allows (1) layering chains may be changed
2313  * dynamically & without the use of locking; and (2) a layer may update the
2314  * xb_chain_io[start|done] member in a given xbuf with its current index value,
2315  * to allow for deferred processing of an IO within the same chain from a
2316  * different execution context.
2317  */
2318 
2319 struct sd_chain_index {
2320         int     sci_iostart_index;
2321         int     sci_iodone_index;
2322 };
2323 
2324 static struct sd_chain_index    sd_chain_index_map[] = {
2325         { SD_CHAIN_DISK_IOSTART,                SD_CHAIN_DISK_IODONE },
2326         { SD_CHAIN_DISK_IOSTART_NO_PM,          SD_CHAIN_DISK_IODONE_NO_PM },
2327         { SD_CHAIN_RMMEDIA_IOSTART,             SD_CHAIN_RMMEDIA_IODONE },
2328         { SD_CHAIN_RMMEDIA_IOSTART_NO_PM,       SD_CHAIN_RMMEDIA_IODONE_NO_PM },
2329         { SD_CHAIN_CHKSUM_IOSTART,              SD_CHAIN_CHKSUM_IODONE },
2330         { SD_CHAIN_CHKSUM_IOSTART_NO_PM,        SD_CHAIN_CHKSUM_IODONE_NO_PM },
2331         { SD_CHAIN_USCSI_CMD_IOSTART,           SD_CHAIN_USCSI_CMD_IODONE },
2332         { SD_CHAIN_USCSI_CHKSUM_IOSTART,        SD_CHAIN_USCSI_CHKSUM_IODONE },
2333         { SD_CHAIN_DIRECT_CMD_IOSTART,          SD_CHAIN_DIRECT_CMD_IODONE },
2334         { SD_CHAIN_PRIORITY_CMD_IOSTART,        SD_CHAIN_PRIORITY_CMD_IODONE },
2335         { SD_CHAIN_MSS_CHKSUM_IOSTART,          SD_CHAIN_MSS_CHKSUM_IODONE },
2336         { SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM, SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM },
2337 
2338 };
2339 
2340 
2341 /*
2342  * The following are indexes into the sd_chain_index_map[] array.
2343  */
2344 
2345 /* un->un_buf_chain_type must be set to one of these */
2346 #define SD_CHAIN_INFO_DISK              0
2347 #define SD_CHAIN_INFO_DISK_NO_PM        1
2348 #define SD_CHAIN_INFO_RMMEDIA           2
2349 #define SD_CHAIN_INFO_MSS_DISK          2
2350 #define SD_CHAIN_INFO_RMMEDIA_NO_PM     3
2351 #define SD_CHAIN_INFO_MSS_DSK_NO_PM     3
2352 #define SD_CHAIN_INFO_CHKSUM            4
2353 #define SD_CHAIN_INFO_CHKSUM_NO_PM      5
2354 #define SD_CHAIN_INFO_MSS_DISK_CHKSUM   10
2355 #define SD_CHAIN_INFO_MSS_DISK_CHKSUM_NO_PM     11
2356 
2357 /* un->un_uscsi_chain_type must be set to one of these */
2358 #define SD_CHAIN_INFO_USCSI_CMD         6
2359 /* USCSI with PM disabled is the same as DIRECT */
2360 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM   8
2361 #define SD_CHAIN_INFO_USCSI_CHKSUM      7
2362 
2363 /* un->un_direct_chain_type must be set to one of these */
2364 #define SD_CHAIN_INFO_DIRECT_CMD        8
2365 
2366 /* un->un_priority_chain_type must be set to one of these */
2367 #define SD_CHAIN_INFO_PRIORITY_CMD      9
2368 
2369 /* size for devid inquiries */
2370 #define MAX_INQUIRY_SIZE                0xF0
2371 
2372 /*
2373  * Macros used by functions to pass a given buf(9S) struct along to the
2374  * next function in the layering chain for further processing.
2375  *
2376  * In the following macros, passing more than three arguments to the called
2377  * routines causes the optimizer for the SPARC compiler to stop doing tail
2378  * call elimination which results in significant performance degradation.
2379  */
2380 #define SD_BEGIN_IOSTART(index, un, bp) \
2381         ((*(sd_iostart_chain[index]))(index, un, bp))
2382 
2383 #define SD_BEGIN_IODONE(index, un, bp)  \
2384         ((*(sd_iodone_chain[index]))(index, un, bp))
2385 
2386 #define SD_NEXT_IOSTART(index, un, bp)                          \
2387         ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp))
2388 
2389 #define SD_NEXT_IODONE(index, un, bp)                           \
2390         ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp))
2391 
2392 /*
2393  *    Function: _init
2394  *
2395  * Description: This is the driver _init(9E) entry point.
2396  *
2397  * Return Code: Returns the value from mod_install(9F) or
2398  *              ddi_soft_state_init(9F) as appropriate.
2399  *
2400  *     Context: Called when driver module loaded.
2401  */
2402 
2403 int
2404 _init(void)
2405 {
2406         int     err;
2407 
2408         /* establish driver name from module name */
2409         sd_label = (char *)mod_modname(&modlinkage);
2410 
2411         err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun),
2412             SD_MAXUNIT);
2413         if (err != 0) {
2414                 return (err);
2415         }
2416 
2417         mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL);
2418         mutex_init(&sd_log_mutex,    NULL, MUTEX_DRIVER, NULL);
2419         mutex_init(&sd_label_mutex,  NULL, MUTEX_DRIVER, NULL);
2420 
2421         mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL);
2422         cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL);
2423         cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL);
2424 
2425         /*
2426          * it's ok to init here even for fibre device
2427          */
2428         sd_scsi_probe_cache_init();
2429 
2430         sd_scsi_target_lun_init();
2431 
2432         /*
2433          * Creating taskq before mod_install ensures that all callers (threads)
2434          * that enter the module after a successful mod_install encounter
2435          * a valid taskq.
2436          */
2437         sd_taskq_create();
2438 
2439         err = mod_install(&modlinkage);
2440         if (err != 0) {
2441                 /* delete taskq if install fails */
2442                 sd_taskq_delete();
2443 
2444                 mutex_destroy(&sd_detach_mutex);
2445                 mutex_destroy(&sd_log_mutex);
2446                 mutex_destroy(&sd_label_mutex);
2447 
2448                 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2449                 cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2450                 cv_destroy(&sd_tr.srq_inprocess_cv);
2451 
2452                 sd_scsi_probe_cache_fini();
2453 
2454                 sd_scsi_target_lun_fini();
2455 
2456                 ddi_soft_state_fini(&sd_state);
2457 
2458                 return (err);
2459         }
2460 
2461         return (err);
2462 }
2463 
2464 
2465 /*
2466  *    Function: _fini
2467  *
2468  * Description: This is the driver _fini(9E) entry point.
2469  *
2470  * Return Code: Returns the value from mod_remove(9F)
2471  *
2472  *     Context: Called when driver module is unloaded.
2473  */
2474 
2475 int
2476 _fini(void)
2477 {
2478         int err;
2479 
2480         if ((err = mod_remove(&modlinkage)) != 0) {
2481                 return (err);
2482         }
2483 
2484         sd_taskq_delete();
2485 
2486         mutex_destroy(&sd_detach_mutex);
2487         mutex_destroy(&sd_log_mutex);
2488         mutex_destroy(&sd_label_mutex);
2489         mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2490 
2491         sd_scsi_probe_cache_fini();
2492 
2493         sd_scsi_target_lun_fini();
2494 
2495         cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2496         cv_destroy(&sd_tr.srq_inprocess_cv);
2497 
2498         ddi_soft_state_fini(&sd_state);
2499 
2500         return (err);
2501 }
2502 
2503 
2504 /*
2505  *    Function: _info
2506  *
2507  * Description: This is the driver _info(9E) entry point.
2508  *
2509  *   Arguments: modinfop - pointer to the driver modinfo structure
2510  *
2511  * Return Code: Returns the value from mod_info(9F).
2512  *
2513  *     Context: Kernel thread context
2514  */
2515 
2516 int
2517 _info(struct modinfo *modinfop)
2518 {
2519         return (mod_info(&modlinkage, modinfop));
2520 }
2521 
2522 
2523 /*
2524  * The following routines implement the driver message logging facility.
2525  * They provide component- and level- based debug output filtering.
2526  * Output may also be restricted to messages for a single instance by
2527  * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set
2528  * to NULL, then messages for all instances are printed.
2529  *
2530  * These routines have been cloned from each other due to the language
2531  * constraints of macros and variable argument list processing.
2532  */
2533 
2534 
2535 /*
2536  *    Function: sd_log_err
2537  *
2538  * Description: This routine is called by the SD_ERROR macro for debug
2539  *              logging of error conditions.
2540  *
2541  *   Arguments: comp - driver component being logged
2542  *              dev  - pointer to driver info structure
2543  *              fmt  - error string and format to be logged
2544  */
2545 
2546 static void
2547 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...)
2548 {
2549         va_list         ap;
2550         dev_info_t      *dev;
2551 
2552         ASSERT(un != NULL);
2553         dev = SD_DEVINFO(un);
2554         ASSERT(dev != NULL);
2555 
2556         /*
2557          * Filter messages based on the global component and level masks.
2558          * Also print if un matches the value of sd_debug_un, or if
2559          * sd_debug_un is set to NULL.
2560          */
2561         if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) &&
2562             ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2563                 mutex_enter(&sd_log_mutex);
2564                 va_start(ap, fmt);
2565                 (void) vsprintf(sd_log_buf, fmt, ap);
2566                 va_end(ap);
2567                 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2568                 mutex_exit(&sd_log_mutex);
2569         }
2570 #ifdef SD_FAULT_INJECTION
2571         _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2572         if (un->sd_injection_mask & comp) {
2573                 mutex_enter(&sd_log_mutex);
2574                 va_start(ap, fmt);
2575                 (void) vsprintf(sd_log_buf, fmt, ap);
2576                 va_end(ap);
2577                 sd_injection_log(sd_log_buf, un);
2578                 mutex_exit(&sd_log_mutex);
2579         }
2580 #endif
2581 }
2582 
2583 
2584 /*
2585  *    Function: sd_log_info
2586  *
2587  * Description: This routine is called by the SD_INFO macro for debug
2588  *              logging of general purpose informational conditions.
2589  *
2590  *   Arguments: comp - driver component being logged
2591  *              dev  - pointer to driver info structure
2592  *              fmt  - info string and format to be logged
2593  */
2594 
2595 static void
2596 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...)
2597 {
2598         va_list         ap;
2599         dev_info_t      *dev;
2600 
2601         ASSERT(un != NULL);
2602         dev = SD_DEVINFO(un);
2603         ASSERT(dev != NULL);
2604 
2605         /*
2606          * Filter messages based on the global component and level masks.
2607          * Also print if un matches the value of sd_debug_un, or if
2608          * sd_debug_un is set to NULL.
2609          */
2610         if ((sd_component_mask & component) &&
2611             (sd_level_mask & SD_LOGMASK_INFO) &&
2612             ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2613                 mutex_enter(&sd_log_mutex);
2614                 va_start(ap, fmt);
2615                 (void) vsprintf(sd_log_buf, fmt, ap);
2616                 va_end(ap);
2617                 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2618                 mutex_exit(&sd_log_mutex);
2619         }
2620 #ifdef SD_FAULT_INJECTION
2621         _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2622         if (un->sd_injection_mask & component) {
2623                 mutex_enter(&sd_log_mutex);
2624                 va_start(ap, fmt);
2625                 (void) vsprintf(sd_log_buf, fmt, ap);
2626                 va_end(ap);
2627                 sd_injection_log(sd_log_buf, un);
2628                 mutex_exit(&sd_log_mutex);
2629         }
2630 #endif
2631 }
2632 
2633 
2634 /*
2635  *    Function: sd_log_trace
2636  *
2637  * Description: This routine is called by the SD_TRACE macro for debug
2638  *              logging of trace conditions (i.e. function entry/exit).
2639  *
2640  *   Arguments: comp - driver component being logged
2641  *              dev  - pointer to driver info structure
2642  *              fmt  - trace string and format to be logged
2643  */
2644 
2645 static void
2646 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...)
2647 {
2648         va_list         ap;
2649         dev_info_t      *dev;
2650 
2651         ASSERT(un != NULL);
2652         dev = SD_DEVINFO(un);
2653         ASSERT(dev != NULL);
2654 
2655         /*
2656          * Filter messages based on the global component and level masks.
2657          * Also print if un matches the value of sd_debug_un, or if
2658          * sd_debug_un is set to NULL.
2659          */
2660         if ((sd_component_mask & component) &&
2661             (sd_level_mask & SD_LOGMASK_TRACE) &&
2662             ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2663                 mutex_enter(&sd_log_mutex);
2664                 va_start(ap, fmt);
2665                 (void) vsprintf(sd_log_buf, fmt, ap);
2666                 va_end(ap);
2667                 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2668                 mutex_exit(&sd_log_mutex);
2669         }
2670 #ifdef SD_FAULT_INJECTION
2671         _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2672         if (un->sd_injection_mask & component) {
2673                 mutex_enter(&sd_log_mutex);
2674                 va_start(ap, fmt);
2675                 (void) vsprintf(sd_log_buf, fmt, ap);
2676                 va_end(ap);
2677                 sd_injection_log(sd_log_buf, un);
2678                 mutex_exit(&sd_log_mutex);
2679         }
2680 #endif
2681 }
2682 
2683 
2684 /*
2685  *    Function: sdprobe
2686  *
2687  * Description: This is the driver probe(9e) entry point function.
2688  *
2689  *   Arguments: devi - opaque device info handle
2690  *
2691  * Return Code: DDI_PROBE_SUCCESS: If the probe was successful.
2692  *              DDI_PROBE_FAILURE: If the probe failed.
2693  *              DDI_PROBE_PARTIAL: If the instance is not present now,
2694  *                                 but may be present in the future.
2695  */
2696 
2697 static int
2698 sdprobe(dev_info_t *devi)
2699 {
2700         struct scsi_device      *devp;
2701         int                     rval;
2702         int                     instance = ddi_get_instance(devi);
2703 
2704         /*
2705          * if it wasn't for pln, sdprobe could actually be nulldev
2706          * in the "__fibre" case.
2707          */
2708         if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
2709                 return (DDI_PROBE_DONTCARE);
2710         }
2711 
2712         devp = ddi_get_driver_private(devi);
2713 
2714         if (devp == NULL) {
2715                 /* Ooops... nexus driver is mis-configured... */
2716                 return (DDI_PROBE_FAILURE);
2717         }
2718 
2719         if (ddi_get_soft_state(sd_state, instance) != NULL) {
2720                 return (DDI_PROBE_PARTIAL);
2721         }
2722 
2723         /*
2724          * Call the SCSA utility probe routine to see if we actually
2725          * have a target at this SCSI nexus.
2726          */
2727         switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) {
2728         case SCSIPROBE_EXISTS:
2729                 switch (devp->sd_inq->inq_dtype) {
2730                 case DTYPE_DIRECT:
2731                         rval = DDI_PROBE_SUCCESS;
2732                         break;
2733                 case DTYPE_RODIRECT:
2734                         /* CDs etc. Can be removable media */
2735                         rval = DDI_PROBE_SUCCESS;
2736                         break;
2737                 case DTYPE_OPTICAL:
2738                         /*
2739                          * Rewritable optical driver HP115AA
2740                          * Can also be removable media
2741                          */
2742 
2743                         /*
2744                          * Do not attempt to bind to  DTYPE_OPTICAL if
2745                          * pre solaris 9 sparc sd behavior is required
2746                          *
2747                          * If first time through and sd_dtype_optical_bind
2748                          * has not been set in /etc/system check properties
2749                          */
2750 
2751                         if (sd_dtype_optical_bind  < 0) {
2752                                 sd_dtype_optical_bind = ddi_prop_get_int
2753                                     (DDI_DEV_T_ANY, devi, 0,
2754                                     "optical-device-bind", 1);
2755                         }
2756 
2757                         if (sd_dtype_optical_bind == 0) {
2758                                 rval = DDI_PROBE_FAILURE;
2759                         } else {
2760                                 rval = DDI_PROBE_SUCCESS;
2761                         }
2762                         break;
2763 
2764                 case DTYPE_NOTPRESENT:
2765                 default:
2766                         rval = DDI_PROBE_FAILURE;
2767                         break;
2768                 }
2769                 break;
2770         default:
2771                 rval = DDI_PROBE_PARTIAL;
2772                 break;
2773         }
2774 
2775         /*
2776          * This routine checks for resource allocation prior to freeing,
2777          * so it will take care of the "smart probing" case where a
2778          * scsi_probe() may or may not have been issued and will *not*
2779          * free previously-freed resources.
2780          */
2781         scsi_unprobe(devp);
2782         return (rval);
2783 }
2784 
2785 
2786 /*
2787  *    Function: sdinfo
2788  *
2789  * Description: This is the driver getinfo(9e) entry point function.
2790  *              Given the device number, return the devinfo pointer from
2791  *              the scsi_device structure or the instance number
2792  *              associated with the dev_t.
2793  *
2794  *   Arguments: dip     - pointer to device info structure
2795  *              infocmd - command argument (DDI_INFO_DEVT2DEVINFO,
2796  *                        DDI_INFO_DEVT2INSTANCE)
2797  *              arg     - driver dev_t
2798  *              resultp - user buffer for request response
2799  *
2800  * Return Code: DDI_SUCCESS
2801  *              DDI_FAILURE
2802  */
2803 /* ARGSUSED */
2804 static int
2805 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2806 {
2807         struct sd_lun   *un;
2808         dev_t           dev;
2809         int             instance;
2810         int             error;
2811 
2812         switch (infocmd) {
2813         case DDI_INFO_DEVT2DEVINFO:
2814                 dev = (dev_t)arg;
2815                 instance = SDUNIT(dev);
2816                 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
2817                         return (DDI_FAILURE);
2818                 }
2819                 *result = (void *) SD_DEVINFO(un);
2820                 error = DDI_SUCCESS;
2821                 break;
2822         case DDI_INFO_DEVT2INSTANCE:
2823                 dev = (dev_t)arg;
2824                 instance = SDUNIT(dev);
2825                 *result = (void *)(uintptr_t)instance;
2826                 error = DDI_SUCCESS;
2827                 break;
2828         default:
2829                 error = DDI_FAILURE;
2830         }
2831         return (error);
2832 }
2833 
2834 /*
2835  *    Function: sd_prop_op
2836  *
2837  * Description: This is the driver prop_op(9e) entry point function.
2838  *              Return the number of blocks for the partition in question
2839  *              or forward the request to the property facilities.
2840  *
2841  *   Arguments: dev       - device number
2842  *              dip       - pointer to device info structure
2843  *              prop_op   - property operator
2844  *              mod_flags - DDI_PROP_DONTPASS, don't pass to parent
2845  *              name      - pointer to property name
2846  *              valuep    - pointer or address of the user buffer
2847  *              lengthp   - property length
2848  *
2849  * Return Code: DDI_PROP_SUCCESS
2850  *              DDI_PROP_NOT_FOUND
2851  *              DDI_PROP_UNDEFINED
2852  *              DDI_PROP_NO_MEMORY
2853  *              DDI_PROP_BUF_TOO_SMALL
2854  */
2855 
2856 static int
2857 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
2858     char *name, caddr_t valuep, int *lengthp)
2859 {
2860         struct sd_lun   *un;
2861 
2862         if ((un = ddi_get_soft_state(sd_state, ddi_get_instance(dip))) == NULL)
2863                 return (ddi_prop_op(dev, dip, prop_op, mod_flags,
2864                     name, valuep, lengthp));
2865 
2866         return (cmlb_prop_op(un->un_cmlbhandle,
2867             dev, dip, prop_op, mod_flags, name, valuep, lengthp,
2868             SDPART(dev), (void *)SD_PATH_DIRECT));
2869 }
2870 
2871 /*
2872  * The following functions are for smart probing:
2873  * sd_scsi_probe_cache_init()
2874  * sd_scsi_probe_cache_fini()
2875  * sd_scsi_clear_probe_cache()
2876  * sd_scsi_probe_with_cache()
2877  */
2878 
2879 /*
2880  *    Function: sd_scsi_probe_cache_init
2881  *
2882  * Description: Initializes the probe response cache mutex and head pointer.
2883  *
2884  *     Context: Kernel thread context
2885  */
2886 
2887 static void
2888 sd_scsi_probe_cache_init(void)
2889 {
2890         mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL);
2891         sd_scsi_probe_cache_head = NULL;
2892 }
2893 
2894 
2895 /*
2896  *    Function: sd_scsi_probe_cache_fini
2897  *
2898  * Description: Frees all resources associated with the probe response cache.
2899  *
2900  *     Context: Kernel thread context
2901  */
2902 
2903 static void
2904 sd_scsi_probe_cache_fini(void)
2905 {
2906         struct sd_scsi_probe_cache *cp;
2907         struct sd_scsi_probe_cache *ncp;
2908 
2909         /* Clean up our smart probing linked list */
2910         for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) {
2911                 ncp = cp->next;
2912                 kmem_free(cp, sizeof (struct sd_scsi_probe_cache));
2913         }
2914         sd_scsi_probe_cache_head = NULL;
2915         mutex_destroy(&sd_scsi_probe_cache_mutex);
2916 }
2917 
2918 
2919 /*
2920  *    Function: sd_scsi_clear_probe_cache
2921  *
2922  * Description: This routine clears the probe response cache. This is
2923  *              done when open() returns ENXIO so that when deferred
2924  *              attach is attempted (possibly after a device has been
2925  *              turned on) we will retry the probe. Since we don't know
2926  *              which target we failed to open, we just clear the
2927  *              entire cache.
2928  *
2929  *     Context: Kernel thread context
2930  */
2931 
2932 static void
2933 sd_scsi_clear_probe_cache(void)
2934 {
2935         struct sd_scsi_probe_cache      *cp;
2936         int                             i;
2937 
2938         mutex_enter(&sd_scsi_probe_cache_mutex);
2939         for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2940                 /*
2941                  * Reset all entries to SCSIPROBE_EXISTS.  This will
2942                  * force probing to be performed the next time
2943                  * sd_scsi_probe_with_cache is called.
2944                  */
2945                 for (i = 0; i < NTARGETS_WIDE; i++) {
2946                         cp->cache[i] = SCSIPROBE_EXISTS;
2947                 }
2948         }
2949         mutex_exit(&sd_scsi_probe_cache_mutex);
2950 }
2951 
2952 
2953 /*
2954  *    Function: sd_scsi_probe_with_cache
2955  *
2956  * Description: This routine implements support for a scsi device probe
2957  *              with cache. The driver maintains a cache of the target
2958  *              responses to scsi probes. If we get no response from a
2959  *              target during a probe inquiry, we remember that, and we
2960  *              avoid additional calls to scsi_probe on non-zero LUNs
2961  *              on the same target until the cache is cleared. By doing
2962  *              so we avoid the 1/4 sec selection timeout for nonzero
2963  *              LUNs. lun0 of a target is always probed.
2964  *
2965  *   Arguments: devp     - Pointer to a scsi_device(9S) structure
2966  *              waitfunc - indicates what the allocator routines should
2967  *                         do when resources are not available. This value
2968  *                         is passed on to scsi_probe() when that routine
2969  *                         is called.
2970  *
2971  * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache;
2972  *              otherwise the value returned by scsi_probe(9F).
2973  *
2974  *     Context: Kernel thread context
2975  */
2976 
2977 static int
2978 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)())
2979 {
2980         struct sd_scsi_probe_cache      *cp;
2981         dev_info_t      *pdip = ddi_get_parent(devp->sd_dev);
2982         int             lun, tgt;
2983 
2984         lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
2985             SCSI_ADDR_PROP_LUN, 0);
2986         tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
2987             SCSI_ADDR_PROP_TARGET, -1);
2988 
2989         /* Make sure caching enabled and target in range */
2990         if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) {
2991                 /* do it the old way (no cache) */
2992                 return (scsi_probe(devp, waitfn));
2993         }
2994 
2995         mutex_enter(&sd_scsi_probe_cache_mutex);
2996 
2997         /* Find the cache for this scsi bus instance */
2998         for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2999                 if (cp->pdip == pdip) {
3000                         break;
3001                 }
3002         }
3003 
3004         /* If we can't find a cache for this pdip, create one */
3005         if (cp == NULL) {
3006                 int i;
3007 
3008                 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache),
3009                     KM_SLEEP);
3010                 cp->pdip = pdip;
3011                 cp->next = sd_scsi_probe_cache_head;
3012                 sd_scsi_probe_cache_head = cp;
3013                 for (i = 0; i < NTARGETS_WIDE; i++) {
3014                         cp->cache[i] = SCSIPROBE_EXISTS;
3015                 }
3016         }
3017 
3018         mutex_exit(&sd_scsi_probe_cache_mutex);
3019 
3020         /* Recompute the cache for this target if LUN zero */
3021         if (lun == 0) {
3022                 cp->cache[tgt] = SCSIPROBE_EXISTS;
3023         }
3024 
3025         /* Don't probe if cache remembers a NORESP from a previous LUN. */
3026         if (cp->cache[tgt] != SCSIPROBE_EXISTS) {
3027                 return (SCSIPROBE_NORESP);
3028         }
3029 
3030         /* Do the actual probe; save & return the result */
3031         return (cp->cache[tgt] = scsi_probe(devp, waitfn));
3032 }
3033 
3034 
3035 /*
3036  *    Function: sd_scsi_target_lun_init
3037  *
3038  * Description: Initializes the attached lun chain mutex and head pointer.
3039  *
3040  *     Context: Kernel thread context
3041  */
3042 
3043 static void
3044 sd_scsi_target_lun_init(void)
3045 {
3046         mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL);
3047         sd_scsi_target_lun_head = NULL;
3048 }
3049 
3050 
3051 /*
3052  *    Function: sd_scsi_target_lun_fini
3053  *
3054  * Description: Frees all resources associated with the attached lun
3055  *              chain
3056  *
3057  *     Context: Kernel thread context
3058  */
3059 
3060 static void
3061 sd_scsi_target_lun_fini(void)
3062 {
3063         struct sd_scsi_hba_tgt_lun      *cp;
3064         struct sd_scsi_hba_tgt_lun      *ncp;
3065 
3066         for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) {
3067                 ncp = cp->next;
3068                 kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun));
3069         }
3070         sd_scsi_target_lun_head = NULL;
3071         mutex_destroy(&sd_scsi_target_lun_mutex);
3072 }
3073 
3074 
3075 /*
3076  *    Function: sd_scsi_get_target_lun_count
3077  *
3078  * Description: This routine will check in the attached lun chain to see
3079  *              how many luns are attached on the required SCSI controller
3080  *              and target. Currently, some capabilities like tagged queue
3081  *              are supported per target based by HBA. So all luns in a
3082  *              target have the same capabilities. Based on this assumption,
3083  *              sd should only set these capabilities once per target. This
3084  *              function is called when sd needs to decide how many luns
3085  *              already attached on a target.
3086  *
3087  *   Arguments: dip     - Pointer to the system's dev_info_t for the SCSI
3088  *                        controller device.
3089  *              target  - The target ID on the controller's SCSI bus.
3090  *
3091  * Return Code: The number of luns attached on the required target and
3092  *              controller.
3093  *              -1 if target ID is not in parallel SCSI scope or the given
3094  *              dip is not in the chain.
3095  *
3096  *     Context: Kernel thread context
3097  */
3098 
3099 static int
3100 sd_scsi_get_target_lun_count(dev_info_t *dip, int target)
3101 {
3102         struct sd_scsi_hba_tgt_lun      *cp;
3103 
3104         if ((target < 0) || (target >= NTARGETS_WIDE)) {
3105                 return (-1);
3106         }
3107 
3108         mutex_enter(&sd_scsi_target_lun_mutex);
3109 
3110         for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3111                 if (cp->pdip == dip) {
3112                         break;
3113                 }
3114         }
3115 
3116         mutex_exit(&sd_scsi_target_lun_mutex);
3117 
3118         if (cp == NULL) {
3119                 return (-1);
3120         }
3121 
3122         return (cp->nlun[target]);
3123 }
3124 
3125 
3126 /*
3127  *    Function: sd_scsi_update_lun_on_target
3128  *
3129  * Description: This routine is used to update the attached lun chain when a
3130  *              lun is attached or detached on a target.
3131  *
3132  *   Arguments: dip     - Pointer to the system's dev_info_t for the SCSI
3133  *                        controller device.
3134  *              target  - The target ID on the controller's SCSI bus.
3135  *              flag    - Indicate the lun is attached or detached.
3136  *
3137  *     Context: Kernel thread context
3138  */
3139 
3140 static void
3141 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag)
3142 {
3143         struct sd_scsi_hba_tgt_lun      *cp;
3144 
3145         mutex_enter(&sd_scsi_target_lun_mutex);
3146 
3147         for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3148                 if (cp->pdip == dip) {
3149                         break;
3150                 }
3151         }
3152 
3153         if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) {
3154                 cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun),
3155                     KM_SLEEP);
3156                 cp->pdip = dip;
3157                 cp->next = sd_scsi_target_lun_head;
3158                 sd_scsi_target_lun_head = cp;
3159         }
3160 
3161         mutex_exit(&sd_scsi_target_lun_mutex);
3162 
3163         if (cp != NULL) {
3164                 if (flag == SD_SCSI_LUN_ATTACH) {
3165                         cp->nlun[target] ++;
3166                 } else {
3167                         cp->nlun[target] --;
3168                 }
3169         }
3170 }
3171 
3172 
3173 /*
3174  *    Function: sd_spin_up_unit
3175  *
3176  * Description: Issues the following commands to spin-up the device:
3177  *              START STOP UNIT, and INQUIRY.
3178  *
3179  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3180  *                      structure for this target.
3181  *
3182  * Return Code: 0 - success
3183  *              EIO - failure
3184  *              EACCES - reservation conflict
3185  *
3186  *     Context: Kernel thread context
3187  */
3188 
3189 static int
3190 sd_spin_up_unit(sd_ssc_t *ssc)
3191 {
3192         size_t  resid           = 0;
3193         int     has_conflict    = FALSE;
3194         uchar_t *bufaddr;
3195         int     status;
3196         struct sd_lun   *un;
3197 
3198         ASSERT(ssc != NULL);
3199         un = ssc->ssc_un;
3200         ASSERT(un != NULL);
3201 
3202         /*
3203          * Send a throwaway START UNIT command.
3204          *
3205          * If we fail on this, we don't care presently what precisely
3206          * is wrong.  EMC's arrays will also fail this with a check
3207          * condition (0x2/0x4/0x3) if the device is "inactive," but
3208          * we don't want to fail the attach because it may become
3209          * "active" later.
3210          * We don't know if power condition is supported or not at
3211          * this stage, use START STOP bit.
3212          */
3213         status = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
3214             SD_TARGET_START, SD_PATH_DIRECT);
3215 
3216         if (status != 0) {
3217                 if (status == EACCES)
3218                         has_conflict = TRUE;
3219                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3220         }
3221 
3222         /*
3223          * Send another INQUIRY command to the target. This is necessary for
3224          * non-removable media direct access devices because their INQUIRY data
3225          * may not be fully qualified until they are spun up (perhaps via the
3226          * START command above).  Note: This seems to be needed for some
3227          * legacy devices only.) The INQUIRY command should succeed even if a
3228          * Reservation Conflict is present.
3229          */
3230         bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP);
3231 
3232         if (sd_send_scsi_INQUIRY(ssc, bufaddr, SUN_INQSIZE, 0, 0, &resid)
3233             != 0) {
3234                 kmem_free(bufaddr, SUN_INQSIZE);
3235                 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
3236                 return (EIO);
3237         }
3238 
3239         /*
3240          * If we got enough INQUIRY data, copy it over the old INQUIRY data.
3241          * Note that this routine does not return a failure here even if the
3242          * INQUIRY command did not return any data.  This is a legacy behavior.
3243          */
3244         if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) {
3245                 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE);
3246         }
3247 
3248         kmem_free(bufaddr, SUN_INQSIZE);
3249 
3250         /* If we hit a reservation conflict above, tell the caller. */
3251         if (has_conflict == TRUE) {
3252                 return (EACCES);
3253         }
3254 
3255         return (0);
3256 }
3257 
3258 #ifdef _LP64
3259 /*
3260  *    Function: sd_enable_descr_sense
3261  *
3262  * Description: This routine attempts to select descriptor sense format
3263  *              using the Control mode page.  Devices that support 64 bit
3264  *              LBAs (for >2TB luns) should also implement descriptor
3265  *              sense data so we will call this function whenever we see
3266  *              a lun larger than 2TB.  If for some reason the device
3267  *              supports 64 bit LBAs but doesn't support descriptor sense
3268  *              presumably the mode select will fail.  Everything will
3269  *              continue to work normally except that we will not get
3270  *              complete sense data for commands that fail with an LBA
3271  *              larger than 32 bits.
3272  *
3273  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3274  *                      structure for this target.
3275  *
3276  *     Context: Kernel thread context only
3277  */
3278 
3279 static void
3280 sd_enable_descr_sense(sd_ssc_t *ssc)
3281 {
3282         uchar_t                 *header;
3283         struct mode_control_scsi3 *ctrl_bufp;
3284         size_t                  buflen;
3285         size_t                  bd_len;
3286         int                     status;
3287         struct sd_lun           *un;
3288 
3289         ASSERT(ssc != NULL);
3290         un = ssc->ssc_un;
3291         ASSERT(un != NULL);
3292 
3293         /*
3294          * Read MODE SENSE page 0xA, Control Mode Page
3295          */
3296         buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH +
3297             sizeof (struct mode_control_scsi3);
3298         header = kmem_zalloc(buflen, KM_SLEEP);
3299 
3300         status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
3301             MODEPAGE_CTRL_MODE, SD_PATH_DIRECT);
3302 
3303         if (status != 0) {
3304                 SD_ERROR(SD_LOG_COMMON, un,
3305                     "sd_enable_descr_sense: mode sense ctrl page failed\n");
3306                 goto eds_exit;
3307         }
3308 
3309         /*
3310          * Determine size of Block Descriptors in order to locate
3311          * the mode page data. ATAPI devices return 0, SCSI devices
3312          * should return MODE_BLK_DESC_LENGTH.
3313          */
3314         bd_len  = ((struct mode_header *)header)->bdesc_length;
3315 
3316         /* Clear the mode data length field for MODE SELECT */
3317         ((struct mode_header *)header)->length = 0;
3318 
3319         ctrl_bufp = (struct mode_control_scsi3 *)
3320             (header + MODE_HEADER_LENGTH + bd_len);
3321 
3322         /*
3323          * If the page length is smaller than the expected value,
3324          * the target device doesn't support D_SENSE. Bail out here.
3325          */
3326         if (ctrl_bufp->mode_page.length <
3327             sizeof (struct mode_control_scsi3) - 2) {
3328                 SD_ERROR(SD_LOG_COMMON, un,
3329                     "sd_enable_descr_sense: enable D_SENSE failed\n");
3330                 goto eds_exit;
3331         }
3332 
3333         /*
3334          * Clear PS bit for MODE SELECT
3335          */
3336         ctrl_bufp->mode_page.ps = 0;
3337 
3338         /*
3339          * Set D_SENSE to enable descriptor sense format.
3340          */
3341         ctrl_bufp->d_sense = 1;
3342 
3343         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3344 
3345         /*
3346          * Use MODE SELECT to commit the change to the D_SENSE bit
3347          */
3348         status = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header,
3349             buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT);
3350 
3351         if (status != 0) {
3352                 SD_INFO(SD_LOG_COMMON, un,
3353                     "sd_enable_descr_sense: mode select ctrl page failed\n");
3354         } else {
3355                 kmem_free(header, buflen);
3356                 return;
3357         }
3358 
3359 eds_exit:
3360         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3361         kmem_free(header, buflen);
3362 }
3363 
3364 /*
3365  *    Function: sd_reenable_dsense_task
3366  *
3367  * Description: Re-enable descriptor sense after device or bus reset
3368  *
3369  *     Context: Executes in a taskq() thread context
3370  */
3371 static void
3372 sd_reenable_dsense_task(void *arg)
3373 {
3374         struct  sd_lun  *un = arg;
3375         sd_ssc_t        *ssc;
3376 
3377         ASSERT(un != NULL);
3378 
3379         ssc = sd_ssc_init(un);
3380         sd_enable_descr_sense(ssc);
3381         sd_ssc_fini(ssc);
3382 }
3383 #endif /* _LP64 */
3384 
3385 /*
3386  *    Function: sd_set_mmc_caps
3387  *
3388  * Description: This routine determines if the device is MMC compliant and if
3389  *              the device supports CDDA via a mode sense of the CDVD
3390  *              capabilities mode page. Also checks if the device is a
3391  *              dvdram writable device.
3392  *
3393  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3394  *                      structure for this target.
3395  *
3396  *     Context: Kernel thread context only
3397  */
3398 
3399 static void
3400 sd_set_mmc_caps(sd_ssc_t *ssc)
3401 {
3402         struct mode_header_grp2         *sense_mhp;
3403         uchar_t                         *sense_page;
3404         caddr_t                         buf;
3405         int                             bd_len;
3406         int                             status;
3407         struct uscsi_cmd                com;
3408         int                             rtn;
3409         uchar_t                         *out_data_rw, *out_data_hd;
3410         uchar_t                         *rqbuf_rw, *rqbuf_hd;
3411         uchar_t                         *out_data_gesn;
3412         int                             gesn_len;
3413         struct sd_lun                   *un;
3414 
3415         ASSERT(ssc != NULL);
3416         un = ssc->ssc_un;
3417         ASSERT(un != NULL);
3418 
3419         /*
3420          * The flags which will be set in this function are - mmc compliant,
3421          * dvdram writable device, cdda support. Initialize them to FALSE
3422          * and if a capability is detected - it will be set to TRUE.
3423          */
3424         un->un_f_mmc_cap = FALSE;
3425         un->un_f_dvdram_writable_device = FALSE;
3426         un->un_f_cfg_cdda = FALSE;
3427 
3428         buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3429         status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3430             BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
3431 
3432         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3433 
3434         if (status != 0) {
3435                 /* command failed; just return */
3436                 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3437                 return;
3438         }
3439         /*
3440          * If the mode sense request for the CDROM CAPABILITIES
3441          * page (0x2A) succeeds the device is assumed to be MMC.
3442          */
3443         un->un_f_mmc_cap = TRUE;
3444 
3445         /* See if GET STATUS EVENT NOTIFICATION is supported */
3446         if (un->un_f_mmc_gesn_polling) {
3447                 gesn_len = SD_GESN_HEADER_LEN + SD_GESN_MEDIA_DATA_LEN;
3448                 out_data_gesn = kmem_zalloc(gesn_len, KM_SLEEP);
3449 
3450                 rtn = sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(ssc,
3451                     out_data_gesn, gesn_len, 1 << SD_GESN_MEDIA_CLASS);
3452 
3453                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3454 
3455                 if ((rtn != 0) || !sd_gesn_media_data_valid(out_data_gesn)) {
3456                         un->un_f_mmc_gesn_polling = FALSE;
3457                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
3458                             "sd_set_mmc_caps: gesn not supported "
3459                             "%d %x %x %x %x\n", rtn,
3460                             out_data_gesn[0], out_data_gesn[1],
3461                             out_data_gesn[2], out_data_gesn[3]);
3462                 }
3463 
3464                 kmem_free(out_data_gesn, gesn_len);
3465         }
3466 
3467         /* Get to the page data */
3468         sense_mhp = (struct mode_header_grp2 *)buf;
3469         bd_len = (sense_mhp->bdesc_length_hi << 8) |
3470             sense_mhp->bdesc_length_lo;
3471         if (bd_len > MODE_BLK_DESC_LENGTH) {
3472                 /*
3473                  * We did not get back the expected block descriptor
3474                  * length so we cannot determine if the device supports
3475                  * CDDA. However, we still indicate the device is MMC
3476                  * according to the successful response to the page
3477                  * 0x2A mode sense request.
3478                  */
3479                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3480                     "sd_set_mmc_caps: Mode Sense returned "
3481                     "invalid block descriptor length\n");
3482                 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3483                 return;
3484         }
3485 
3486         /* See if read CDDA is supported */
3487         sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 +
3488             bd_len);
3489         un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE;
3490 
3491         /* See if writing DVD RAM is supported. */
3492         un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE;
3493         if (un->un_f_dvdram_writable_device == TRUE) {
3494                 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3495                 return;
3496         }
3497 
3498         /*
3499          * If the device presents DVD or CD capabilities in the mode
3500          * page, we can return here since a RRD will not have
3501          * these capabilities.
3502          */
3503         if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3504                 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3505                 return;
3506         }
3507         kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3508 
3509         /*
3510          * If un->un_f_dvdram_writable_device is still FALSE,
3511          * check for a Removable Rigid Disk (RRD).  A RRD
3512          * device is identified by the features RANDOM_WRITABLE and
3513          * HARDWARE_DEFECT_MANAGEMENT.
3514          */
3515         out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3516         rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3517 
3518         rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3519             SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3520             RANDOM_WRITABLE, SD_PATH_STANDARD);
3521 
3522         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3523 
3524         if (rtn != 0) {
3525                 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3526                 kmem_free(rqbuf_rw, SENSE_LENGTH);
3527                 return;
3528         }
3529 
3530         out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3531         rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3532 
3533         rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3534             SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3535             HARDWARE_DEFECT_MANAGEMENT, SD_PATH_STANDARD);
3536 
3537         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3538 
3539         if (rtn == 0) {
3540                 /*
3541                  * We have good information, check for random writable
3542                  * and hardware defect features.
3543                  */
3544                 if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3545                     (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) {
3546                         un->un_f_dvdram_writable_device = TRUE;
3547                 }
3548         }
3549 
3550         kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3551         kmem_free(rqbuf_rw, SENSE_LENGTH);
3552         kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3553         kmem_free(rqbuf_hd, SENSE_LENGTH);
3554 }
3555 
3556 /*
3557  *    Function: sd_check_for_writable_cd
3558  *
3559  * Description: This routine determines if the media in the device is
3560  *              writable or not. It uses the get configuration command (0x46)
3561  *              to determine if the media is writable
3562  *
3563  *   Arguments: un - driver soft state (unit) structure
3564  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct"
3565  *                           chain and the normal command waitq, or
3566  *                           SD_PATH_DIRECT_PRIORITY to use the USCSI
3567  *                           "direct" chain and bypass the normal command
3568  *                           waitq.
3569  *
3570  *     Context: Never called at interrupt context.
3571  */
3572 
3573 static void
3574 sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag)
3575 {
3576         struct uscsi_cmd                com;
3577         uchar_t                         *out_data;
3578         uchar_t                         *rqbuf;
3579         int                             rtn;
3580         uchar_t                         *out_data_rw, *out_data_hd;
3581         uchar_t                         *rqbuf_rw, *rqbuf_hd;
3582         struct mode_header_grp2         *sense_mhp;
3583         uchar_t                         *sense_page;
3584         caddr_t                         buf;
3585         int                             bd_len;
3586         int                             status;
3587         struct sd_lun                   *un;
3588 
3589         ASSERT(ssc != NULL);
3590         un = ssc->ssc_un;
3591         ASSERT(un != NULL);
3592         ASSERT(mutex_owned(SD_MUTEX(un)));
3593 
3594         /*
3595          * Initialize the writable media to false, if configuration info.
3596          * tells us otherwise then only we will set it.
3597          */
3598         un->un_f_mmc_writable_media = FALSE;
3599         mutex_exit(SD_MUTEX(un));
3600 
3601         out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
3602         rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3603 
3604         rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, SENSE_LENGTH,
3605             out_data, SD_PROFILE_HEADER_LEN, path_flag);
3606 
3607         if (rtn != 0)
3608                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3609 
3610         mutex_enter(SD_MUTEX(un));
3611         if (rtn == 0) {
3612                 /*
3613                  * We have good information, check for writable DVD.
3614                  */
3615                 if ((out_data[6] == 0) && (out_data[7] == 0x12)) {
3616                         un->un_f_mmc_writable_media = TRUE;
3617                         kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3618                         kmem_free(rqbuf, SENSE_LENGTH);
3619                         return;
3620                 }
3621         }
3622 
3623         kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3624         kmem_free(rqbuf, SENSE_LENGTH);
3625 
3626         /*
3627          * Determine if this is a RRD type device.
3628          */
3629         mutex_exit(SD_MUTEX(un));
3630         buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3631         status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3632             BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, path_flag);
3633 
3634         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3635 
3636         mutex_enter(SD_MUTEX(un));
3637         if (status != 0) {
3638                 /* command failed; just return */
3639                 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3640                 return;
3641         }
3642 
3643         /* Get to the page data */
3644         sense_mhp = (struct mode_header_grp2 *)buf;
3645         bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
3646         if (bd_len > MODE_BLK_DESC_LENGTH) {
3647                 /*
3648                  * We did not get back the expected block descriptor length so
3649                  * we cannot check the mode page.
3650                  */
3651                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3652                     "sd_check_for_writable_cd: Mode Sense returned "
3653                     "invalid block descriptor length\n");
3654                 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3655                 return;
3656         }
3657 
3658         /*
3659          * If the device presents DVD or CD capabilities in the mode
3660          * page, we can return here since a RRD device will not have
3661          * these capabilities.
3662          */
3663         sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len);
3664         if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3665                 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3666                 return;
3667         }
3668         kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3669 
3670         /*
3671          * If un->un_f_mmc_writable_media is still FALSE,
3672          * check for RRD type media.  A RRD device is identified
3673          * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT.
3674          */
3675         mutex_exit(SD_MUTEX(un));
3676         out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3677         rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3678 
3679         rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3680             SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3681             RANDOM_WRITABLE, path_flag);
3682 
3683         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3684         if (rtn != 0) {
3685                 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3686                 kmem_free(rqbuf_rw, SENSE_LENGTH);
3687                 mutex_enter(SD_MUTEX(un));
3688                 return;
3689         }
3690 
3691         out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3692         rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3693 
3694         rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3695             SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3696             HARDWARE_DEFECT_MANAGEMENT, path_flag);
3697 
3698         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3699         mutex_enter(SD_MUTEX(un));
3700         if (rtn == 0) {
3701                 /*
3702                  * We have good information, check for random writable
3703                  * and hardware defect features as current.
3704                  */
3705                 if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3706                     (out_data_rw[10] & 0x1) &&
3707                     (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) &&
3708                     (out_data_hd[10] & 0x1)) {
3709                         un->un_f_mmc_writable_media = TRUE;
3710                 }
3711         }
3712 
3713         kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3714         kmem_free(rqbuf_rw, SENSE_LENGTH);
3715         kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3716         kmem_free(rqbuf_hd, SENSE_LENGTH);
3717 }
3718 
3719 /*
3720  *    Function: sd_read_unit_properties
3721  *
3722  * Description: The following implements a property lookup mechanism.
3723  *              Properties for particular disks (keyed on vendor, model
3724  *              and rev numbers) are sought in the sd.conf file via
3725  *              sd_process_sdconf_file(), and if not found there, are
3726  *              looked for in a list hardcoded in this driver via
3727  *              sd_process_sdconf_table() Once located the properties
3728  *              are used to update the driver unit structure.
3729  *
3730  *   Arguments: un - driver soft state (unit) structure
3731  */
3732 
3733 static void
3734 sd_read_unit_properties(struct sd_lun *un)
3735 {
3736         /*
3737          * sd_process_sdconf_file returns SD_FAILURE if it cannot find
3738          * the "sd-config-list" property (from the sd.conf file) or if
3739          * there was not a match for the inquiry vid/pid. If this event
3740          * occurs the static driver configuration table is searched for
3741          * a match.
3742          */
3743         ASSERT(un != NULL);
3744         if (sd_process_sdconf_file(un) == SD_FAILURE) {
3745                 sd_process_sdconf_table(un);
3746         }
3747 
3748         /* check for LSI device */
3749         sd_is_lsi(un);
3750 
3751 
3752 }
3753 
3754 
3755 /*
3756  *    Function: sd_process_sdconf_file
3757  *
3758  * Description: Use ddi_prop_lookup(9F) to obtain the properties from the
3759  *              driver's config file (ie, sd.conf) and update the driver
3760  *              soft state structure accordingly.
3761  *
3762  *   Arguments: un - driver soft state (unit) structure
3763  *
3764  * Return Code: SD_SUCCESS - The properties were successfully set according
3765  *                           to the driver configuration file.
3766  *              SD_FAILURE - The driver config list was not obtained or
3767  *                           there was no vid/pid match. This indicates that
3768  *                           the static config table should be used.
3769  *
3770  * The config file has a property, "sd-config-list". Currently we support
3771  * two kinds of formats. For both formats, the value of this property
3772  * is a list of duplets:
3773  *
3774  *  sd-config-list=
3775  *      <duplet>,
3776  *      [,<duplet>]*;
3777  *
3778  * For the improved format, where
3779  *
3780  *     <duplet>:= "<vid+pid>","<tunable-list>"
3781  *
3782  * and
3783  *
3784  *     <tunable-list>:=   <tunable> [, <tunable> ]*;
3785  *     <tunable> =        <name> : <value>
3786  *
3787  * The <vid+pid> is the string that is returned by the target device on a
3788  * SCSI inquiry command, the <tunable-list> contains one or more tunables
3789  * to apply to all target devices with the specified <vid+pid>.
3790  *
3791  * Each <tunable> is a "<name> : <value>" pair.
3792  *
3793  * For the old format, the structure of each duplet is as follows:
3794  *
3795  *  <duplet>:= "<vid+pid>","<data-property-name_list>"
3796  *
3797  * The first entry of the duplet is the device ID string (the concatenated
3798  * vid & pid; not to be confused with a device_id).  This is defined in
3799  * the same way as in the sd_disk_table.
3800  *
3801  * The second part of the duplet is a string that identifies a
3802  * data-property-name-list. The data-property-name-list is defined as
3803  * follows:
3804  *
3805  *  <data-property-name-list>:=<data-property-name> [<data-property-name>]
3806  *
3807  * The syntax of <data-property-name> depends on the <version> field.
3808  *
3809  * If version = SD_CONF_VERSION_1 we have the following syntax:
3810  *
3811  *      <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
3812  *
3813  * where the prop0 value will be used to set prop0 if bit0 set in the
3814  * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1
3815  *
3816  */
3817 
3818 static int
3819 sd_process_sdconf_file(struct sd_lun *un)
3820 {
3821         char    **config_list = NULL;
3822         uint_t  nelements;
3823         char    *vidptr;
3824         int     vidlen;
3825         char    *dnlist_ptr;
3826         char    *dataname_ptr;
3827         char    *dataname_lasts;
3828         int     *data_list = NULL;
3829         uint_t  data_list_len;
3830         int     rval = SD_FAILURE;
3831         int     i;
3832 
3833         ASSERT(un != NULL);
3834 
3835         /* Obtain the configuration list associated with the .conf file */
3836         if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, SD_DEVINFO(un),
3837             DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, sd_config_list,
3838             &config_list, &nelements) != DDI_PROP_SUCCESS) {
3839                 return (SD_FAILURE);
3840         }
3841 
3842         /*
3843          * Compare vids in each duplet to the inquiry vid - if a match is
3844          * made, get the data value and update the soft state structure
3845          * accordingly.
3846          *
3847          * Each duplet should show as a pair of strings, return SD_FAILURE
3848          * otherwise.
3849          */
3850         if (nelements & 1) {
3851                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3852                     "sd-config-list should show as pairs of strings.\n");
3853                 if (config_list)
3854                         ddi_prop_free(config_list);
3855                 return (SD_FAILURE);
3856         }
3857 
3858         for (i = 0; i < nelements; i += 2) {
3859                 /*
3860                  * Note: The assumption here is that each vid entry is on
3861                  * a unique line from its associated duplet.
3862                  */
3863                 vidptr = config_list[i];
3864                 vidlen = (int)strlen(vidptr);
3865                 if (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS) {
3866                         continue;
3867                 }
3868 
3869                 /*
3870                  * dnlist contains 1 or more blank separated
3871                  * data-property-name entries
3872                  */
3873                 dnlist_ptr = config_list[i + 1];
3874 
3875                 if (strchr(dnlist_ptr, ':') != NULL) {
3876                         /*
3877                          * Decode the improved format sd-config-list.
3878                          */
3879                         sd_nvpair_str_decode(un, dnlist_ptr);
3880                 } else {
3881                         /*
3882                          * The old format sd-config-list, loop through all
3883                          * data-property-name entries in the
3884                          * data-property-name-list
3885                          * setting the properties for each.
3886                          */
3887                         for (dataname_ptr = sd_strtok_r(dnlist_ptr, " \t",
3888                             &dataname_lasts); dataname_ptr != NULL;
3889                             dataname_ptr = sd_strtok_r(NULL, " \t",
3890                             &dataname_lasts)) {
3891                                 int version;
3892 
3893                                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3894                                     "sd_process_sdconf_file: disk:%s, "
3895                                     "data:%s\n", vidptr, dataname_ptr);
3896 
3897                                 /* Get the data list */
3898                                 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
3899                                     SD_DEVINFO(un), 0, dataname_ptr, &data_list,
3900                                     &data_list_len) != DDI_PROP_SUCCESS) {
3901                                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
3902                                             "sd_process_sdconf_file: data "
3903                                             "property (%s) has no value\n",
3904                                             dataname_ptr);
3905                                         continue;
3906                                 }
3907 
3908                                 version = data_list[0];
3909 
3910                                 if (version == SD_CONF_VERSION_1) {
3911                                         sd_tunables values;
3912 
3913                                         /* Set the properties */
3914                                         if (sd_chk_vers1_data(un, data_list[1],
3915                                             &data_list[2], data_list_len,
3916                                             dataname_ptr) == SD_SUCCESS) {
3917                                                 sd_get_tunables_from_conf(un,
3918                                                     data_list[1], &data_list[2],
3919                                                     &values);
3920                                                 sd_set_vers1_properties(un,
3921                                                     data_list[1], &values);
3922                                                 rval = SD_SUCCESS;
3923                                         } else {
3924                                                 rval = SD_FAILURE;
3925                                         }
3926                                 } else {
3927                                         scsi_log(SD_DEVINFO(un), sd_label,
3928                                             CE_WARN, "data property %s version "
3929                                             "0x%x is invalid.",
3930                                             dataname_ptr, version);
3931                                         rval = SD_FAILURE;
3932                                 }
3933                                 if (data_list)
3934                                         ddi_prop_free(data_list);
3935                         }
3936                 }
3937         }
3938 
3939         /* free up the memory allocated by ddi_prop_lookup_string_array(). */
3940         if (config_list) {
3941                 ddi_prop_free(config_list);
3942         }
3943 
3944         return (rval);
3945 }
3946 
3947 /*
3948  *    Function: sd_nvpair_str_decode()
3949  *
3950  * Description: Parse the improved format sd-config-list to get
3951  *    each entry of tunable, which includes a name-value pair.
3952  *    Then call sd_set_properties() to set the property.
3953  *
3954  *   Arguments: un - driver soft state (unit) structure
3955  *    nvpair_str - the tunable list
3956  */
3957 static void
3958 sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str)
3959 {
3960         char    *nv, *name, *value, *token;
3961         char    *nv_lasts, *v_lasts, *x_lasts;
3962 
3963         for (nv = sd_strtok_r(nvpair_str, ",", &nv_lasts); nv != NULL;
3964             nv = sd_strtok_r(NULL, ",", &nv_lasts)) {
3965                 token = sd_strtok_r(nv, ":", &v_lasts);
3966                 name  = sd_strtok_r(token, " \t", &x_lasts);
3967                 token = sd_strtok_r(NULL, ":", &v_lasts);
3968                 value = sd_strtok_r(token, " \t", &x_lasts);
3969                 if (name == NULL || value == NULL) {
3970                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
3971                             "sd_nvpair_str_decode: "
3972                             "name or value is not valid!\n");
3973                 } else {
3974                         sd_set_properties(un, name, value);
3975                 }
3976         }
3977 }
3978 
3979 /*
3980  *    Function: sd_strtok_r()
3981  *
3982  * Description: This function uses strpbrk and strspn to break
3983  *    string into tokens on sequentially subsequent calls. Return
3984  *    NULL when no non-separator characters remain. The first
3985  *    argument is NULL for subsequent calls.
3986  */
3987 static char *
3988 sd_strtok_r(char *string, const char *sepset, char **lasts)
3989 {
3990         char    *q, *r;
3991 
3992         /* First or subsequent call */
3993         if (string == NULL)
3994                 string = *lasts;
3995 
3996         if (string == NULL)
3997                 return (NULL);
3998 
3999         /* Skip leading separators */
4000         q = string + strspn(string, sepset);
4001 
4002         if (*q == '\0')
4003                 return (NULL);
4004 
4005         if ((r = strpbrk(q, sepset)) == NULL)
4006                 *lasts = NULL;
4007         else {
4008                 *r = '\0';
4009                 *lasts = r + 1;
4010         }
4011         return (q);
4012 }
4013 
4014 /*
4015  *    Function: sd_set_properties()
4016  *
4017  * Description: Set device properties based on the improved
4018  *    format sd-config-list.
4019  *
4020  *   Arguments: un - driver soft state (unit) structure
4021  *    name  - supported tunable name
4022  *    value - tunable value
4023  */
4024 static void
4025 sd_set_properties(struct sd_lun *un, char *name, char *value)
4026 {
4027         char    *endptr = NULL;
4028         long    val = 0;
4029 
4030         if (strcasecmp(name, "cache-nonvolatile") == 0) {
4031                 if (strcasecmp(value, "true") == 0) {
4032                         un->un_f_suppress_cache_flush = TRUE;
4033                 } else if (strcasecmp(value, "false") == 0) {
4034                         un->un_f_suppress_cache_flush = FALSE;
4035                 } else {
4036                         goto value_invalid;
4037                 }
4038                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4039                     "suppress_cache_flush flag set to %d\n",
4040                     un->un_f_suppress_cache_flush);
4041                 return;
4042         }
4043 
4044         if (strcasecmp(name, "controller-type") == 0) {
4045                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4046                         un->un_ctype = val;
4047                 } else {
4048                         goto value_invalid;
4049                 }
4050                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4051                     "ctype set to %d\n", un->un_ctype);
4052                 return;
4053         }
4054 
4055         if (strcasecmp(name, "delay-busy") == 0) {
4056                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4057                         un->un_busy_timeout = drv_usectohz(val / 1000);
4058                 } else {
4059                         goto value_invalid;
4060                 }
4061                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4062                     "busy_timeout set to %d\n", un->un_busy_timeout);
4063                 return;
4064         }
4065 
4066         if (strcasecmp(name, "disksort") == 0) {
4067                 if (strcasecmp(value, "true") == 0) {
4068                         un->un_f_disksort_disabled = FALSE;
4069                 } else if (strcasecmp(value, "false") == 0) {
4070                         un->un_f_disksort_disabled = TRUE;
4071                 } else {
4072                         goto value_invalid;
4073                 }
4074                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4075                     "disksort disabled flag set to %d\n",
4076                     un->un_f_disksort_disabled);
4077                 return;
4078         }
4079 
4080         if (strcasecmp(name, "power-condition") == 0) {
4081                 if (strcasecmp(value, "true") == 0) {
4082                         un->un_f_power_condition_disabled = FALSE;
4083                 } else if (strcasecmp(value, "false") == 0) {
4084                         un->un_f_power_condition_disabled = TRUE;
4085                 } else {
4086                         goto value_invalid;
4087                 }
4088                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4089                     "power condition disabled flag set to %d\n",
4090                     un->un_f_power_condition_disabled);
4091                 return;
4092         }
4093 
4094         if (strcasecmp(name, "timeout-releasereservation") == 0) {
4095                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4096                         un->un_reserve_release_time = val;
4097                 } else {
4098                         goto value_invalid;
4099                 }
4100                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4101                     "reservation release timeout set to %d\n",
4102                     un->un_reserve_release_time);
4103                 return;
4104         }
4105 
4106         if (strcasecmp(name, "reset-lun") == 0) {
4107                 if (strcasecmp(value, "true") == 0) {
4108                         un->un_f_lun_reset_enabled = TRUE;
4109                 } else if (strcasecmp(value, "false") == 0) {
4110                         un->un_f_lun_reset_enabled = FALSE;
4111                 } else {
4112                         goto value_invalid;
4113                 }
4114                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4115                     "lun reset enabled flag set to %d\n",
4116                     un->un_f_lun_reset_enabled);
4117                 return;
4118         }
4119 
4120         if (strcasecmp(name, "retries-busy") == 0) {
4121                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4122                         un->un_busy_retry_count = val;
4123                 } else {
4124                         goto value_invalid;
4125                 }
4126                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4127                     "busy retry count set to %d\n", un->un_busy_retry_count);
4128                 return;
4129         }
4130 
4131         if (strcasecmp(name, "retries-timeout") == 0) {
4132                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4133                         un->un_retry_count = val;
4134                 } else {
4135                         goto value_invalid;
4136                 }
4137                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4138                     "timeout retry count set to %d\n", un->un_retry_count);
4139                 return;
4140         }
4141 
4142         if (strcasecmp(name, "retries-notready") == 0) {
4143                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4144                         un->un_notready_retry_count = val;
4145                 } else {
4146                         goto value_invalid;
4147                 }
4148                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4149                     "notready retry count set to %d\n",
4150                     un->un_notready_retry_count);
4151                 return;
4152         }
4153 
4154         if (strcasecmp(name, "retries-reset") == 0) {
4155                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4156                         un->un_reset_retry_count = val;
4157                 } else {
4158                         goto value_invalid;
4159                 }
4160                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4161                     "reset retry count set to %d\n",
4162                     un->un_reset_retry_count);
4163                 return;
4164         }
4165 
4166         if (strcasecmp(name, "throttle-max") == 0) {
4167                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4168                         un->un_saved_throttle = un->un_throttle = val;
4169                 } else {
4170                         goto value_invalid;
4171                 }
4172                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4173                     "throttle set to %d\n", un->un_throttle);
4174         }
4175 
4176         if (strcasecmp(name, "throttle-min") == 0) {
4177                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4178                         un->un_min_throttle = val;
4179                 } else {
4180                         goto value_invalid;
4181                 }
4182                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4183                     "min throttle set to %d\n", un->un_min_throttle);
4184         }
4185 
4186         if (strcasecmp(name, "rmw-type") == 0) {
4187                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4188                         un->un_f_rmw_type = val;
4189                 } else {
4190                         goto value_invalid;
4191                 }
4192                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4193                     "RMW type set to %d\n", un->un_f_rmw_type);
4194         }
4195 
4196         if (strcasecmp(name, "physical-block-size") == 0) {
4197                 if (ddi_strtol(value, &endptr, 0, &val) == 0 &&
4198                     ISP2(val) && val >= un->un_tgt_blocksize &&
4199                     val >= un->un_sys_blocksize) {
4200                         un->un_phy_blocksize = val;
4201                 } else {
4202                         goto value_invalid;
4203                 }
4204                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4205                     "physical block size set to %d\n", un->un_phy_blocksize);
4206         }
4207 
4208         if (strcasecmp(name, "retries-victim") == 0) {
4209                 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4210                         un->un_victim_retry_count = val;
4211                 } else {
4212                         goto value_invalid;
4213                 }
4214                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4215                     "victim retry count set to %d\n",
4216                     un->un_victim_retry_count);
4217                 return;
4218         }
4219 
4220         /*
4221          * Validate the throttle values.
4222          * If any of the numbers are invalid, set everything to defaults.
4223          */
4224         if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4225             (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4226             (un->un_min_throttle > un->un_throttle)) {
4227                 un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4228                 un->un_min_throttle = sd_min_throttle;
4229         }
4230 
4231         if (strcasecmp(name, "mmc-gesn-polling") == 0) {
4232                 if (strcasecmp(value, "true") == 0) {
4233                         un->un_f_mmc_gesn_polling = TRUE;
4234                 } else if (strcasecmp(value, "false") == 0) {
4235                         un->un_f_mmc_gesn_polling = FALSE;
4236                 } else {
4237                         goto value_invalid;
4238                 }
4239                 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4240                     "mmc-gesn-polling set to %d\n",
4241                     un->un_f_mmc_gesn_polling);
4242         }
4243 
4244         return;
4245 
4246 value_invalid:
4247         SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4248             "value of prop %s is invalid\n", name);
4249 }
4250 
4251 /*
4252  *    Function: sd_get_tunables_from_conf()
4253  *
4254  *
4255  *    This function reads the data list from the sd.conf file and pulls
4256  *    the values that can have numeric values as arguments and places
4257  *    the values in the appropriate sd_tunables member.
4258  *    Since the order of the data list members varies across platforms
4259  *    This function reads them from the data list in a platform specific
4260  *    order and places them into the correct sd_tunable member that is
4261  *    consistent across all platforms.
4262  */
4263 static void
4264 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list,
4265     sd_tunables *values)
4266 {
4267         int i;
4268         int mask;
4269 
4270         bzero(values, sizeof (sd_tunables));
4271 
4272         for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4273 
4274                 mask = 1 << i;
4275                 if (mask > flags) {
4276                         break;
4277                 }
4278 
4279                 switch (mask & flags) {
4280                 case 0: /* This mask bit not set in flags */
4281                         continue;
4282                 case SD_CONF_BSET_THROTTLE:
4283                         values->sdt_throttle = data_list[i];
4284                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
4285                             "sd_get_tunables_from_conf: throttle = %d\n",
4286                             values->sdt_throttle);
4287                         break;
4288                 case SD_CONF_BSET_CTYPE:
4289                         values->sdt_ctype = data_list[i];
4290                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
4291                             "sd_get_tunables_from_conf: ctype = %d\n",
4292                             values->sdt_ctype);
4293                         break;
4294                 case SD_CONF_BSET_NRR_COUNT:
4295                         values->sdt_not_rdy_retries = data_list[i];
4296                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
4297                             "sd_get_tunables_from_conf: not_rdy_retries = %d\n",
4298                             values->sdt_not_rdy_retries);
4299                         break;
4300                 case SD_CONF_BSET_BSY_RETRY_COUNT:
4301                         values->sdt_busy_retries = data_list[i];
4302                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
4303                             "sd_get_tunables_from_conf: busy_retries = %d\n",
4304                             values->sdt_busy_retries);
4305                         break;
4306                 case SD_CONF_BSET_RST_RETRIES:
4307                         values->sdt_reset_retries = data_list[i];
4308                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
4309                             "sd_get_tunables_from_conf: reset_retries = %d\n",
4310                             values->sdt_reset_retries);
4311                         break;
4312                 case SD_CONF_BSET_RSV_REL_TIME:
4313                         values->sdt_reserv_rel_time = data_list[i];
4314                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
4315                             "sd_get_tunables_from_conf: reserv_rel_time = %d\n",
4316                             values->sdt_reserv_rel_time);
4317                         break;
4318                 case SD_CONF_BSET_MIN_THROTTLE:
4319                         values->sdt_min_throttle = data_list[i];
4320                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
4321                             "sd_get_tunables_from_conf: min_throttle = %d\n",
4322                             values->sdt_min_throttle);
4323                         break;
4324                 case SD_CONF_BSET_DISKSORT_DISABLED:
4325                         values->sdt_disk_sort_dis = data_list[i];
4326                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
4327                             "sd_get_tunables_from_conf: disk_sort_dis = %d\n",
4328                             values->sdt_disk_sort_dis);
4329                         break;
4330                 case SD_CONF_BSET_LUN_RESET_ENABLED:
4331                         values->sdt_lun_reset_enable = data_list[i];
4332                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
4333                             "sd_get_tunables_from_conf: lun_reset_enable = %d"
4334                             "\n", values->sdt_lun_reset_enable);
4335                         break;
4336                 case SD_CONF_BSET_CACHE_IS_NV:
4337                         values->sdt_suppress_cache_flush = data_list[i];
4338                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
4339                             "sd_get_tunables_from_conf: \
4340                             suppress_cache_flush = %d"
4341                             "\n", values->sdt_suppress_cache_flush);
4342                         break;
4343                 case SD_CONF_BSET_PC_DISABLED:
4344                         values->sdt_disk_sort_dis = data_list[i];
4345                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
4346                             "sd_get_tunables_from_conf: power_condition_dis = "
4347                             "%d\n", values->sdt_power_condition_dis);
4348                         break;
4349                 }
4350         }
4351 }
4352 
4353 /*
4354  *    Function: sd_process_sdconf_table
4355  *
4356  * Description: Search the static configuration table for a match on the
4357  *              inquiry vid/pid and update the driver soft state structure
4358  *              according to the table property values for the device.
4359  *
4360  *              The form of a configuration table entry is:
4361  *                <vid+pid>,<flags>,<property-data>
4362  *                "SEAGATE ST42400N",1,0x40000,
4363  *                0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1;
4364  *
4365  *   Arguments: un - driver soft state (unit) structure
4366  */
4367 
4368 static void
4369 sd_process_sdconf_table(struct sd_lun *un)
4370 {
4371         char    *id = NULL;
4372         int     table_index;
4373         int     idlen;
4374 
4375         ASSERT(un != NULL);
4376         for (table_index = 0; table_index < sd_disk_table_size;
4377             table_index++) {
4378                 id = sd_disk_table[table_index].device_id;
4379                 idlen = strlen(id);
4380 
4381                 /*
4382                  * The static configuration table currently does not
4383                  * implement version 10 properties. Additionally,
4384                  * multiple data-property-name entries are not
4385                  * implemented in the static configuration table.
4386                  */
4387                 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4388                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
4389                             "sd_process_sdconf_table: disk %s\n", id);
4390                         sd_set_vers1_properties(un,
4391                             sd_disk_table[table_index].flags,
4392                             sd_disk_table[table_index].properties);
4393                         break;
4394                 }
4395         }
4396 }
4397 
4398 
4399 /*
4400  *    Function: sd_sdconf_id_match
4401  *
4402  * Description: This local function implements a case sensitive vid/pid
4403  *              comparison as well as the boundary cases of wild card and
4404  *              multiple blanks.
4405  *
4406  *              Note: An implicit assumption made here is that the scsi
4407  *              inquiry structure will always keep the vid, pid and
4408  *              revision strings in consecutive sequence, so they can be
4409  *              read as a single string. If this assumption is not the
4410  *              case, a separate string, to be used for the check, needs
4411  *              to be built with these strings concatenated.
4412  *
4413  *   Arguments: un - driver soft state (unit) structure
4414  *              id - table or config file vid/pid
4415  *              idlen  - length of the vid/pid (bytes)
4416  *
4417  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4418  *              SD_FAILURE - Indicates no match with the inquiry vid/pid
4419  */
4420 
4421 static int
4422 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen)
4423 {
4424         struct scsi_inquiry     *sd_inq;
4425         int                     rval = SD_SUCCESS;
4426 
4427         ASSERT(un != NULL);
4428         sd_inq = un->un_sd->sd_inq;
4429         ASSERT(id != NULL);
4430 
4431         /*
4432          * We use the inq_vid as a pointer to a buffer containing the
4433          * vid and pid and use the entire vid/pid length of the table
4434          * entry for the comparison. This works because the inq_pid
4435          * data member follows inq_vid in the scsi_inquiry structure.
4436          */
4437         if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) {
4438                 /*
4439                  * The user id string is compared to the inquiry vid/pid
4440                  * using a case insensitive comparison and ignoring
4441                  * multiple spaces.
4442                  */
4443                 rval = sd_blank_cmp(un, id, idlen);
4444                 if (rval != SD_SUCCESS) {
4445                         /*
4446                          * User id strings that start and end with a "*"
4447                          * are a special case. These do not have a
4448                          * specific vendor, and the product string can
4449                          * appear anywhere in the 16 byte PID portion of
4450                          * the inquiry data. This is a simple strstr()
4451                          * type search for the user id in the inquiry data.
4452                          */
4453                         if ((id[0] == '*') && (id[idlen - 1] == '*')) {
4454                                 char    *pidptr = &id[1];
4455                                 int     i;
4456                                 int     j;
4457                                 int     pidstrlen = idlen - 2;
4458                                 j = sizeof (SD_INQUIRY(un)->inq_pid) -
4459                                     pidstrlen;
4460 
4461                                 if (j < 0) {
4462                                         return (SD_FAILURE);
4463                                 }
4464                                 for (i = 0; i < j; i++) {
4465                                         if (bcmp(&SD_INQUIRY(un)->inq_pid[i],
4466                                             pidptr, pidstrlen) == 0) {
4467                                                 rval = SD_SUCCESS;
4468                                                 break;
4469                                         }
4470                                 }
4471                         }
4472                 }
4473         }
4474         return (rval);
4475 }
4476 
4477 
4478 /*
4479  *    Function: sd_blank_cmp
4480  *
4481  * Description: If the id string starts and ends with a space, treat
4482  *              multiple consecutive spaces as equivalent to a single
4483  *              space. For example, this causes a sd_disk_table entry
4484  *              of " NEC CDROM " to match a device's id string of
4485  *              "NEC       CDROM".
4486  *
4487  *              Note: The success exit condition for this routine is if
4488  *              the pointer to the table entry is '\0' and the cnt of
4489  *              the inquiry length is zero. This will happen if the inquiry
4490  *              string returned by the device is padded with spaces to be
4491  *              exactly 24 bytes in length (8 byte vid + 16 byte pid). The
4492  *              SCSI spec states that the inquiry string is to be padded with
4493  *              spaces.
4494  *
4495  *   Arguments: un - driver soft state (unit) structure
4496  *              id - table or config file vid/pid
4497  *              idlen  - length of the vid/pid (bytes)
4498  *
4499  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4500  *              SD_FAILURE - Indicates no match with the inquiry vid/pid
4501  */
4502 
4503 static int
4504 sd_blank_cmp(struct sd_lun *un, char *id, int idlen)
4505 {
4506         char            *p1;
4507         char            *p2;
4508         int             cnt;
4509         cnt = sizeof (SD_INQUIRY(un)->inq_vid) +
4510             sizeof (SD_INQUIRY(un)->inq_pid);
4511 
4512         ASSERT(un != NULL);
4513         p2 = un->un_sd->sd_inq->inq_vid;
4514         ASSERT(id != NULL);
4515         p1 = id;
4516 
4517         if ((id[0] == ' ') && (id[idlen - 1] == ' ')) {
4518                 /*
4519                  * Note: string p1 is terminated by a NUL but string p2
4520                  * isn't.  The end of p2 is determined by cnt.
4521                  */
4522                 for (;;) {
4523                         /* skip over any extra blanks in both strings */
4524                         while ((*p1 != '\0') && (*p1 == ' ')) {
4525                                 p1++;
4526                         }
4527                         while ((cnt != 0) && (*p2 == ' ')) {
4528                                 p2++;
4529                                 cnt--;
4530                         }
4531 
4532                         /* compare the two strings */
4533                         if ((cnt == 0) ||
4534                             (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) {
4535                                 break;
4536                         }
4537                         while ((cnt > 0) &&
4538                             (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) {
4539                                 p1++;
4540                                 p2++;
4541                                 cnt--;
4542                         }
4543                 }
4544         }
4545 
4546         /* return SD_SUCCESS if both strings match */
4547         return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE);
4548 }
4549 
4550 
4551 /*
4552  *    Function: sd_chk_vers1_data
4553  *
4554  * Description: Verify the version 1 device properties provided by the
4555  *              user via the configuration file
4556  *
4557  *   Arguments: un           - driver soft state (unit) structure
4558  *              flags        - integer mask indicating properties to be set
4559  *              prop_list    - integer list of property values
4560  *              list_len     - number of the elements
4561  *
4562  * Return Code: SD_SUCCESS - Indicates the user provided data is valid
4563  *              SD_FAILURE - Indicates the user provided data is invalid
4564  */
4565 
4566 static int
4567 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
4568     int list_len, char *dataname_ptr)
4569 {
4570         int i;
4571         int mask = 1;
4572         int index = 0;
4573 
4574         ASSERT(un != NULL);
4575 
4576         /* Check for a NULL property name and list */
4577         if (dataname_ptr == NULL) {
4578                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4579                     "sd_chk_vers1_data: NULL data property name.");
4580                 return (SD_FAILURE);
4581         }
4582         if (prop_list == NULL) {
4583                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4584                     "sd_chk_vers1_data: %s NULL data property list.",
4585                     dataname_ptr);
4586                 return (SD_FAILURE);
4587         }
4588 
4589         /* Display a warning if undefined bits are set in the flags */
4590         if (flags & ~SD_CONF_BIT_MASK) {
4591                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4592                     "sd_chk_vers1_data: invalid bits 0x%x in data list %s. "
4593                     "Properties not set.",
4594                     (flags & ~SD_CONF_BIT_MASK), dataname_ptr);
4595                 return (SD_FAILURE);
4596         }
4597 
4598         /*
4599          * Verify the length of the list by identifying the highest bit set
4600          * in the flags and validating that the property list has a length
4601          * up to the index of this bit.
4602          */
4603         for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4604                 if (flags & mask) {
4605                         index++;
4606                 }
4607                 mask = 1 << i;
4608         }
4609         if (list_len < (index + 2)) {
4610                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4611                     "sd_chk_vers1_data: "
4612                     "Data property list %s size is incorrect. "
4613                     "Properties not set.", dataname_ptr);
4614                 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: "
4615                     "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS);
4616                 return (SD_FAILURE);
4617         }
4618         return (SD_SUCCESS);
4619 }
4620 
4621 
4622 /*
4623  *    Function: sd_set_vers1_properties
4624  *
4625  * Description: Set version 1 device properties based on a property list
4626  *              retrieved from the driver configuration file or static
4627  *              configuration table. Version 1 properties have the format:
4628  *
4629  *      <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
4630  *
4631  *              where the prop0 value will be used to set prop0 if bit0
4632  *              is set in the flags
4633  *
4634  *   Arguments: un           - driver soft state (unit) structure
4635  *              flags        - integer mask indicating properties to be set
4636  *              prop_list    - integer list of property values
4637  */
4638 
4639 static void
4640 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list)
4641 {
4642         ASSERT(un != NULL);
4643 
4644         /*
4645          * Set the flag to indicate cache is to be disabled. An attempt
4646          * to disable the cache via sd_cache_control() will be made
4647          * later during attach once the basic initialization is complete.
4648          */
4649         if (flags & SD_CONF_BSET_NOCACHE) {
4650                 un->un_f_opt_disable_cache = TRUE;
4651                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4652                     "sd_set_vers1_properties: caching disabled flag set\n");
4653         }
4654 
4655         /* CD-specific configuration parameters */
4656         if (flags & SD_CONF_BSET_PLAYMSF_BCD) {
4657                 un->un_f_cfg_playmsf_bcd = TRUE;
4658                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4659                     "sd_set_vers1_properties: playmsf_bcd set\n");
4660         }
4661         if (flags & SD_CONF_BSET_READSUB_BCD) {
4662                 un->un_f_cfg_readsub_bcd = TRUE;
4663                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4664                     "sd_set_vers1_properties: readsub_bcd set\n");
4665         }
4666         if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) {
4667                 un->un_f_cfg_read_toc_trk_bcd = TRUE;
4668                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4669                     "sd_set_vers1_properties: read_toc_trk_bcd set\n");
4670         }
4671         if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) {
4672                 un->un_f_cfg_read_toc_addr_bcd = TRUE;
4673                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4674                     "sd_set_vers1_properties: read_toc_addr_bcd set\n");
4675         }
4676         if (flags & SD_CONF_BSET_NO_READ_HEADER) {
4677                 un->un_f_cfg_no_read_header = TRUE;
4678                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4679                     "sd_set_vers1_properties: no_read_header set\n");
4680         }
4681         if (flags & SD_CONF_BSET_READ_CD_XD4) {
4682                 un->un_f_cfg_read_cd_xd4 = TRUE;
4683                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4684                     "sd_set_vers1_properties: read_cd_xd4 set\n");
4685         }
4686 
4687         /* Support for devices which do not have valid/unique serial numbers */
4688         if (flags & SD_CONF_BSET_FAB_DEVID) {
4689                 un->un_f_opt_fab_devid = TRUE;
4690                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4691                     "sd_set_vers1_properties: fab_devid bit set\n");
4692         }
4693 
4694         /* Support for user throttle configuration */
4695         if (flags & SD_CONF_BSET_THROTTLE) {
4696                 ASSERT(prop_list != NULL);
4697                 un->un_saved_throttle = un->un_throttle =
4698                     prop_list->sdt_throttle;
4699                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4700                     "sd_set_vers1_properties: throttle set to %d\n",
4701                     prop_list->sdt_throttle);
4702         }
4703 
4704         /* Set the per disk retry count according to the conf file or table. */
4705         if (flags & SD_CONF_BSET_NRR_COUNT) {
4706                 ASSERT(prop_list != NULL);
4707                 if (prop_list->sdt_not_rdy_retries) {
4708                         un->un_notready_retry_count =
4709                             prop_list->sdt_not_rdy_retries;
4710                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
4711                             "sd_set_vers1_properties: not ready retry count"
4712                             " set to %d\n", un->un_notready_retry_count);
4713                 }
4714         }
4715 
4716         /* The controller type is reported for generic disk driver ioctls */
4717         if (flags & SD_CONF_BSET_CTYPE) {
4718                 ASSERT(prop_list != NULL);
4719                 switch (prop_list->sdt_ctype) {
4720                 case CTYPE_CDROM:
4721                         un->un_ctype = prop_list->sdt_ctype;
4722                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
4723                             "sd_set_vers1_properties: ctype set to "
4724                             "CTYPE_CDROM\n");
4725                         break;
4726                 case CTYPE_CCS:
4727                         un->un_ctype = prop_list->sdt_ctype;
4728                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
4729                             "sd_set_vers1_properties: ctype set to "
4730                             "CTYPE_CCS\n");
4731                         break;
4732                 case CTYPE_ROD:         /* RW optical */
4733                         un->un_ctype = prop_list->sdt_ctype;
4734                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
4735                             "sd_set_vers1_properties: ctype set to "
4736                             "CTYPE_ROD\n");
4737                         break;
4738                 default:
4739                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4740                             "sd_set_vers1_properties: Could not set "
4741                             "invalid ctype value (%d)",
4742                             prop_list->sdt_ctype);
4743                 }
4744         }
4745 
4746         /* Purple failover timeout */
4747         if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) {
4748                 ASSERT(prop_list != NULL);
4749                 un->un_busy_retry_count =
4750                     prop_list->sdt_busy_retries;
4751                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4752                     "sd_set_vers1_properties: "
4753                     "busy retry count set to %d\n",
4754                     un->un_busy_retry_count);
4755         }
4756 
4757         /* Purple reset retry count */
4758         if (flags & SD_CONF_BSET_RST_RETRIES) {
4759                 ASSERT(prop_list != NULL);
4760                 un->un_reset_retry_count =
4761                     prop_list->sdt_reset_retries;
4762                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4763                     "sd_set_vers1_properties: "
4764                     "reset retry count set to %d\n",
4765                     un->un_reset_retry_count);
4766         }
4767 
4768         /* Purple reservation release timeout */
4769         if (flags & SD_CONF_BSET_RSV_REL_TIME) {
4770                 ASSERT(prop_list != NULL);
4771                 un->un_reserve_release_time =
4772                     prop_list->sdt_reserv_rel_time;
4773                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4774                     "sd_set_vers1_properties: "
4775                     "reservation release timeout set to %d\n",
4776                     un->un_reserve_release_time);
4777         }
4778 
4779         /*
4780          * Driver flag telling the driver to verify that no commands are pending
4781          * for a device before issuing a Test Unit Ready. This is a workaround
4782          * for a firmware bug in some Seagate eliteI drives.
4783          */
4784         if (flags & SD_CONF_BSET_TUR_CHECK) {
4785                 un->un_f_cfg_tur_check = TRUE;
4786                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4787                     "sd_set_vers1_properties: tur queue check set\n");
4788         }
4789 
4790         if (flags & SD_CONF_BSET_MIN_THROTTLE) {
4791                 un->un_min_throttle = prop_list->sdt_min_throttle;
4792                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4793                     "sd_set_vers1_properties: min throttle set to %d\n",
4794                     un->un_min_throttle);
4795         }
4796 
4797         if (flags & SD_CONF_BSET_DISKSORT_DISABLED) {
4798                 un->un_f_disksort_disabled =
4799                     (prop_list->sdt_disk_sort_dis != 0) ?
4800                     TRUE : FALSE;
4801                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4802                     "sd_set_vers1_properties: disksort disabled "
4803                     "flag set to %d\n",
4804                     prop_list->sdt_disk_sort_dis);
4805         }
4806 
4807         if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) {
4808                 un->un_f_lun_reset_enabled =
4809                     (prop_list->sdt_lun_reset_enable != 0) ?
4810                     TRUE : FALSE;
4811                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4812                     "sd_set_vers1_properties: lun reset enabled "
4813                     "flag set to %d\n",
4814                     prop_list->sdt_lun_reset_enable);
4815         }
4816 
4817         if (flags & SD_CONF_BSET_CACHE_IS_NV) {
4818                 un->un_f_suppress_cache_flush =
4819                     (prop_list->sdt_suppress_cache_flush != 0) ?
4820                     TRUE : FALSE;
4821                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4822                     "sd_set_vers1_properties: suppress_cache_flush "
4823                     "flag set to %d\n",
4824                     prop_list->sdt_suppress_cache_flush);
4825         }
4826 
4827         if (flags & SD_CONF_BSET_PC_DISABLED) {
4828                 un->un_f_power_condition_disabled =
4829                     (prop_list->sdt_power_condition_dis != 0) ?
4830                     TRUE : FALSE;
4831                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4832                     "sd_set_vers1_properties: power_condition_disabled "
4833                     "flag set to %d\n",
4834                     prop_list->sdt_power_condition_dis);
4835         }
4836 
4837         /*
4838          * Validate the throttle values.
4839          * If any of the numbers are invalid, set everything to defaults.
4840          */
4841         if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4842             (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4843             (un->un_min_throttle > un->un_throttle)) {
4844                 un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4845                 un->un_min_throttle = sd_min_throttle;
4846         }
4847 }
4848 
4849 /*
4850  *   Function: sd_is_lsi()
4851  *
4852  *   Description: Check for lsi devices, step through the static device
4853  *      table to match vid/pid.
4854  *
4855  *   Args: un - ptr to sd_lun
4856  *
4857  *   Notes:  When creating new LSI property, need to add the new LSI property
4858  *              to this function.
4859  */
4860 static void
4861 sd_is_lsi(struct sd_lun *un)
4862 {
4863         char    *id = NULL;
4864         int     table_index;
4865         int     idlen;
4866         void    *prop;
4867 
4868         ASSERT(un != NULL);
4869         for (table_index = 0; table_index < sd_disk_table_size;
4870             table_index++) {
4871                 id = sd_disk_table[table_index].device_id;
4872                 idlen = strlen(id);
4873                 if (idlen == 0) {
4874                         continue;
4875                 }
4876 
4877                 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4878                         prop = sd_disk_table[table_index].properties;
4879                         if (prop == &lsi_properties ||
4880                             prop == &lsi_oem_properties ||
4881                             prop == &lsi_properties_scsi ||
4882                             prop == &symbios_properties) {
4883                                 un->un_f_cfg_is_lsi = TRUE;
4884                         }
4885                         break;
4886                 }
4887         }
4888 }
4889 
4890 /*
4891  *    Function: sd_get_physical_geometry
4892  *
4893  * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and
4894  *              MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the
4895  *              target, and use this information to initialize the physical
4896  *              geometry cache specified by pgeom_p.
4897  *
4898  *              MODE SENSE is an optional command, so failure in this case
4899  *              does not necessarily denote an error. We want to use the
4900  *              MODE SENSE commands to derive the physical geometry of the
4901  *              device, but if either command fails, the logical geometry is
4902  *              used as the fallback for disk label geometry in cmlb.
4903  *
4904  *              This requires that un->un_blockcount and un->un_tgt_blocksize
4905  *              have already been initialized for the current target and
4906  *              that the current values be passed as args so that we don't
4907  *              end up ever trying to use -1 as a valid value. This could
4908  *              happen if either value is reset while we're not holding
4909  *              the mutex.
4910  *
4911  *   Arguments: un - driver soft state (unit) structure
4912  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4913  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4914  *                      to use the USCSI "direct" chain and bypass the normal
4915  *                      command waitq.
4916  *
4917  *     Context: Kernel thread only (can sleep).
4918  */
4919 
4920 static int
4921 sd_get_physical_geometry(struct sd_lun *un, cmlb_geom_t *pgeom_p,
4922     diskaddr_t capacity, int lbasize, int path_flag)
4923 {
4924         struct  mode_format     *page3p;
4925         struct  mode_geometry   *page4p;
4926         struct  mode_header     *headerp;
4927         int     sector_size;
4928         int     nsect;
4929         int     nhead;
4930         int     ncyl;
4931         int     intrlv;
4932         int     spc;
4933         diskaddr_t      modesense_capacity;
4934         int     rpm;
4935         int     bd_len;
4936         int     mode_header_length;
4937         uchar_t *p3bufp;
4938         uchar_t *p4bufp;
4939         int     cdbsize;
4940         int     ret = EIO;
4941         sd_ssc_t *ssc;
4942         int     status;
4943 
4944         ASSERT(un != NULL);
4945 
4946         if (lbasize == 0) {
4947                 if (ISCD(un)) {
4948                         lbasize = 2048;
4949                 } else {
4950                         lbasize = un->un_sys_blocksize;
4951                 }
4952         }
4953         pgeom_p->g_secsize = (unsigned short)lbasize;
4954 
4955         /*
4956          * If the unit is a cd/dvd drive MODE SENSE page three
4957          * and MODE SENSE page four are reserved (see SBC spec
4958          * and MMC spec). To prevent soft errors just return
4959          * using the default LBA size.
4960          *
4961          * Since SATA MODE SENSE function (sata_txlt_mode_sense()) does not
4962          * implement support for mode pages 3 and 4 return here to prevent
4963          * illegal requests on SATA drives.
4964          *
4965          * These pages are also reserved in SBC-2 and later.  We assume SBC-2
4966          * or later for a direct-attached block device if the SCSI version is
4967          * at least SPC-3.
4968          */
4969 
4970         if (ISCD(un) ||
4971             un->un_interconnect_type == SD_INTERCONNECT_SATA ||
4972             (un->un_ctype == CTYPE_CCS && SD_INQUIRY(un)->inq_ansi >= 5))
4973                 return (ret);
4974 
4975         cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0;
4976 
4977         /*
4978          * Retrieve MODE SENSE page 3 - Format Device Page
4979          */
4980         p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP);
4981         ssc = sd_ssc_init(un);
4982         status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p3bufp,
4983             SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag);
4984         if (status != 0) {
4985                 SD_ERROR(SD_LOG_COMMON, un,
4986                     "sd_get_physical_geometry: mode sense page 3 failed\n");
4987                 goto page3_exit;
4988         }
4989 
4990         /*
4991          * Determine size of Block Descriptors in order to locate the mode
4992          * page data.  ATAPI devices return 0, SCSI devices should return
4993          * MODE_BLK_DESC_LENGTH.
4994          */
4995         headerp = (struct mode_header *)p3bufp;
4996         if (un->un_f_cfg_is_atapi == TRUE) {
4997                 struct mode_header_grp2 *mhp =
4998                     (struct mode_header_grp2 *)headerp;
4999                 mode_header_length = MODE_HEADER_LENGTH_GRP2;
5000                 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
5001         } else {
5002                 mode_header_length = MODE_HEADER_LENGTH;
5003                 bd_len = ((struct mode_header *)headerp)->bdesc_length;
5004         }
5005 
5006         if (bd_len > MODE_BLK_DESC_LENGTH) {
5007                 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5008                     "sd_get_physical_geometry: received unexpected bd_len "
5009                     "of %d, page3\n", bd_len);
5010                 status = EIO;
5011                 goto page3_exit;
5012         }
5013 
5014         page3p = (struct mode_format *)
5015             ((caddr_t)headerp + mode_header_length + bd_len);
5016 
5017         if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) {
5018                 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5019                     "sd_get_physical_geometry: mode sense pg3 code mismatch "
5020                     "%d\n", page3p->mode_page.code);
5021                 status = EIO;
5022                 goto page3_exit;
5023         }
5024 
5025         /*
5026          * Use this physical geometry data only if BOTH MODE SENSE commands
5027          * complete successfully; otherwise, revert to the logical geometry.
5028          * So, we need to save everything in temporary variables.
5029          */
5030         sector_size = BE_16(page3p->data_bytes_sect);
5031 
5032         /*
5033          * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size
5034          */
5035         if (sector_size == 0) {
5036                 sector_size = un->un_sys_blocksize;
5037         } else {
5038                 sector_size &= ~(un->un_sys_blocksize - 1);
5039         }
5040 
5041         nsect  = BE_16(page3p->sect_track);
5042         intrlv = BE_16(page3p->interleave);
5043 
5044         SD_INFO(SD_LOG_COMMON, un,
5045             "sd_get_physical_geometry: Format Parameters (page 3)\n");
5046         SD_INFO(SD_LOG_COMMON, un,
5047             "   mode page: %d; nsect: %d; sector size: %d;\n",
5048             page3p->mode_page.code, nsect, sector_size);
5049         SD_INFO(SD_LOG_COMMON, un,
5050             "   interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv,
5051             BE_16(page3p->track_skew),
5052             BE_16(page3p->cylinder_skew));
5053 
5054         sd_ssc_assessment(ssc, SD_FMT_STANDARD);
5055 
5056         /*
5057          * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page
5058          */
5059         p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP);
5060         status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p4bufp,
5061             SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag);
5062         if (status != 0) {
5063                 SD_ERROR(SD_LOG_COMMON, un,
5064                     "sd_get_physical_geometry: mode sense page 4 failed\n");
5065                 goto page4_exit;
5066         }
5067 
5068         /*
5069          * Determine size of Block Descriptors in order to locate the mode
5070          * page data.  ATAPI devices return 0, SCSI devices should return
5071          * MODE_BLK_DESC_LENGTH.
5072          */
5073         headerp = (struct mode_header *)p4bufp;
5074         if (un->un_f_cfg_is_atapi == TRUE) {
5075                 struct mode_header_grp2 *mhp =
5076                     (struct mode_header_grp2 *)headerp;
5077                 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
5078         } else {
5079                 bd_len = ((struct mode_header *)headerp)->bdesc_length;
5080         }
5081 
5082         if (bd_len > MODE_BLK_DESC_LENGTH) {
5083                 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5084                     "sd_get_physical_geometry: received unexpected bd_len of "
5085                     "%d, page4\n", bd_len);
5086                 status = EIO;
5087                 goto page4_exit;
5088         }
5089 
5090         page4p = (struct mode_geometry *)
5091             ((caddr_t)headerp + mode_header_length + bd_len);
5092 
5093         if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) {
5094                 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5095                     "sd_get_physical_geometry: mode sense pg4 code mismatch "
5096                     "%d\n", page4p->mode_page.code);
5097                 status = EIO;
5098                 goto page4_exit;
5099         }
5100 
5101         /*
5102          * Stash the data now, after we know that both commands completed.
5103          */
5104 
5105 
5106         nhead = (int)page4p->heads;  /* uchar, so no conversion needed */
5107         spc   = nhead * nsect;
5108         ncyl  = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb;
5109         rpm   = BE_16(page4p->rpm);
5110 
5111         modesense_capacity = spc * ncyl;
5112 
5113         SD_INFO(SD_LOG_COMMON, un,
5114             "sd_get_physical_geometry: Geometry Parameters (page 4)\n");
5115         SD_INFO(SD_LOG_COMMON, un,
5116             "   cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm);
5117         SD_INFO(SD_LOG_COMMON, un,
5118             "   computed capacity(h*s*c): %d;\n", modesense_capacity);
5119         SD_INFO(SD_LOG_COMMON, un, "   pgeom_p: %p; read cap: %d\n",
5120             (void *)pgeom_p, capacity);
5121 
5122         /*
5123          * Compensate if the drive's geometry is not rectangular, i.e.,
5124          * the product of C * H * S returned by MODE SENSE >= that returned
5125          * by read capacity. This is an idiosyncrasy of the original x86
5126          * disk subsystem.
5127          */
5128         if (modesense_capacity >= capacity) {
5129                 SD_INFO(SD_LOG_COMMON, un,
5130                     "sd_get_physical_geometry: adjusting acyl; "
5131                     "old: %d; new: %d\n", pgeom_p->g_acyl,
5132                     (modesense_capacity - capacity + spc - 1) / spc);
5133                 if (sector_size != 0) {
5134                         /* 1243403: NEC D38x7 drives don't support sec size */
5135                         pgeom_p->g_secsize = (unsigned short)sector_size;
5136                 }
5137                 pgeom_p->g_nsect    = (unsigned short)nsect;
5138                 pgeom_p->g_nhead    = (unsigned short)nhead;
5139                 pgeom_p->g_capacity = capacity;
5140                 pgeom_p->g_acyl          =
5141                     (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc;
5142                 pgeom_p->g_ncyl          = ncyl - pgeom_p->g_acyl;
5143         }
5144 
5145         pgeom_p->g_rpm    = (unsigned short)rpm;
5146         pgeom_p->g_intrlv = (unsigned short)intrlv;
5147         ret = 0;
5148 
5149         SD_INFO(SD_LOG_COMMON, un,
5150             "sd_get_physical_geometry: mode sense geometry:\n");
5151         SD_INFO(SD_LOG_COMMON, un,
5152             "   nsect: %d; sector size: %d; interlv: %d\n",
5153             nsect, sector_size, intrlv);
5154         SD_INFO(SD_LOG_COMMON, un,
5155             "   nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n",
5156             nhead, ncyl, rpm, modesense_capacity);
5157         SD_INFO(SD_LOG_COMMON, un,
5158             "sd_get_physical_geometry: (cached)\n");
5159         SD_INFO(SD_LOG_COMMON, un,
5160             "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
5161             pgeom_p->g_ncyl,  pgeom_p->g_acyl,
5162             pgeom_p->g_nhead, pgeom_p->g_nsect);
5163         SD_INFO(SD_LOG_COMMON, un,
5164             "   lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n",
5165             pgeom_p->g_secsize, pgeom_p->g_capacity,
5166             pgeom_p->g_intrlv, pgeom_p->g_rpm);
5167         sd_ssc_assessment(ssc, SD_FMT_STANDARD);
5168 
5169 page4_exit:
5170         kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH);
5171 
5172 page3_exit:
5173         kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH);
5174 
5175         if (status != 0) {
5176                 if (status == EIO) {
5177                         /*
5178                          * Some disks do not support mode sense(6), we
5179                          * should ignore this kind of error(sense key is
5180                          * 0x5 - illegal request).
5181                          */
5182                         uint8_t *sensep;
5183                         int senlen;
5184 
5185                         sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
5186                         senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
5187                             ssc->ssc_uscsi_cmd->uscsi_rqresid);
5188 
5189                         if (senlen > 0 &&
5190                             scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
5191                                 sd_ssc_assessment(ssc,
5192                                     SD_FMT_IGNORE_COMPROMISE);
5193                         } else {
5194                                 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
5195                         }
5196                 } else {
5197                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5198                 }
5199         }
5200         sd_ssc_fini(ssc);
5201         return (ret);
5202 }
5203 
5204 /*
5205  *    Function: sd_get_virtual_geometry
5206  *
5207  * Description: Ask the controller to tell us about the target device.
5208  *
5209  *   Arguments: un - pointer to softstate
5210  *              capacity - disk capacity in #blocks
5211  *              lbasize - disk block size in bytes
5212  *
5213  *     Context: Kernel thread only
5214  */
5215 
5216 static int
5217 sd_get_virtual_geometry(struct sd_lun *un, cmlb_geom_t *lgeom_p,
5218     diskaddr_t capacity, int lbasize)
5219 {
5220         uint_t  geombuf;
5221         int     spc;
5222 
5223         ASSERT(un != NULL);
5224 
5225         /* Set sector size, and total number of sectors */
5226         (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size",   lbasize,  1);
5227         (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1);
5228 
5229         /* Let the HBA tell us its geometry */
5230         geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1);
5231 
5232         /* A value of -1 indicates an undefined "geometry" property */
5233         if (geombuf == (-1)) {
5234                 return (EINVAL);
5235         }
5236 
5237         /* Initialize the logical geometry cache. */
5238         lgeom_p->g_nhead   = (geombuf >> 16) & 0xffff;
5239         lgeom_p->g_nsect   = geombuf & 0xffff;
5240         lgeom_p->g_secsize = un->un_sys_blocksize;
5241 
5242         spc = lgeom_p->g_nhead * lgeom_p->g_nsect;
5243 
5244         /*
5245          * Note: The driver originally converted the capacity value from
5246          * target blocks to system blocks. However, the capacity value passed
5247          * to this routine is already in terms of system blocks (this scaling
5248          * is done when the READ CAPACITY command is issued and processed).
5249          * This 'error' may have gone undetected because the usage of g_ncyl
5250          * (which is based upon g_capacity) is very limited within the driver
5251          */
5252         lgeom_p->g_capacity = capacity;
5253 
5254         /*
5255          * Set ncyl to zero if the hba returned a zero nhead or nsect value. The
5256          * hba may return zero values if the device has been removed.
5257          */
5258         if (spc == 0) {
5259                 lgeom_p->g_ncyl = 0;
5260         } else {
5261                 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc;
5262         }
5263         lgeom_p->g_acyl = 0;
5264 
5265         SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n");
5266         return (0);
5267 
5268 }
5269 /*
5270  *    Function: sd_update_block_info
5271  *
5272  * Description: Calculate a byte count to sector count bitshift value
5273  *              from sector size.
5274  *
5275  *   Arguments: un: unit struct.
5276  *              lbasize: new target sector size
5277  *              capacity: new target capacity, ie. block count
5278  *
5279  *     Context: Kernel thread context
5280  */
5281 
5282 static void
5283 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity)
5284 {
5285         if (lbasize != 0) {
5286                 un->un_tgt_blocksize = lbasize;
5287                 un->un_f_tgt_blocksize_is_valid = TRUE;
5288                 if (!un->un_f_has_removable_media) {
5289                         un->un_sys_blocksize = lbasize;
5290                 }
5291         }
5292 
5293         if (capacity != 0) {
5294                 un->un_blockcount            = capacity;
5295                 un->un_f_blockcount_is_valid = TRUE;
5296 
5297                 /*
5298                  * The capacity has changed so update the errstats.
5299                  */
5300                 if (un->un_errstats != NULL) {
5301                         struct sd_errstats *stp;
5302 
5303                         capacity *= un->un_sys_blocksize;
5304                         stp = (struct sd_errstats *)un->un_errstats->ks_data;
5305                         if (stp->sd_capacity.value.ui64 < capacity)
5306                                 stp->sd_capacity.value.ui64 = capacity;
5307                 }
5308         }
5309 }
5310 
5311 
5312 /*
5313  *    Function: sd_register_devid
5314  *
5315  * Description: This routine will obtain the device id information from the
5316  *              target, obtain the serial number, and register the device
5317  *              id with the ddi framework.
5318  *
5319  *   Arguments: devi - the system's dev_info_t for the device.
5320  *              un - driver soft state (unit) structure
5321  *              reservation_flag - indicates if a reservation conflict
5322  *              occurred during attach
5323  *
5324  *     Context: Kernel Thread
5325  */
5326 static void
5327 sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, int reservation_flag)
5328 {
5329         int             rval            = 0;
5330         uchar_t         *inq80          = NULL;
5331         size_t          inq80_len       = MAX_INQUIRY_SIZE;
5332         size_t          inq80_resid     = 0;
5333         uchar_t         *inq83          = NULL;
5334         size_t          inq83_len       = MAX_INQUIRY_SIZE;
5335         size_t          inq83_resid     = 0;
5336         int             dlen, len;
5337         char            *sn;
5338         struct sd_lun   *un;
5339 
5340         ASSERT(ssc != NULL);
5341         un = ssc->ssc_un;
5342         ASSERT(un != NULL);
5343         ASSERT(mutex_owned(SD_MUTEX(un)));
5344         ASSERT((SD_DEVINFO(un)) == devi);
5345 
5346 
5347         /*
5348          * We check the availability of the World Wide Name (0x83) and Unit
5349          * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using
5350          * un_vpd_page_mask from them, we decide which way to get the WWN.  If
5351          * 0x83 is available, that is the best choice.  Our next choice is
5352          * 0x80.  If neither are available, we munge the devid from the device
5353          * vid/pid/serial # for Sun qualified disks, or use the ddi framework
5354          * to fabricate a devid for non-Sun qualified disks.
5355          */
5356         if (sd_check_vpd_page_support(ssc) == 0) {
5357                 /* collect page 80 data if available */
5358                 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) {
5359 
5360                         mutex_exit(SD_MUTEX(un));
5361                         inq80 = kmem_zalloc(inq80_len, KM_SLEEP);
5362 
5363                         rval = sd_send_scsi_INQUIRY(ssc, inq80, inq80_len,
5364                             0x01, 0x80, &inq80_resid);
5365 
5366                         if (rval != 0) {
5367                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5368                                 kmem_free(inq80, inq80_len);
5369                                 inq80 = NULL;
5370                                 inq80_len = 0;
5371                         } else if (ddi_prop_exists(
5372                             DDI_DEV_T_NONE, SD_DEVINFO(un),
5373                             DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
5374                             INQUIRY_SERIAL_NO) == 0) {
5375                                 /*
5376                                  * If we don't already have a serial number
5377                                  * property, do quick verify of data returned
5378                                  * and define property.
5379                                  */
5380                                 dlen = inq80_len - inq80_resid;
5381                                 len = (size_t)inq80[3];
5382                                 if ((dlen >= 4) && ((len + 4) <= dlen)) {
5383                                         /*
5384                                          * Ensure sn termination, skip leading
5385                                          * blanks, and create property
5386                                          * 'inquiry-serial-no'.
5387                                          */
5388                                         sn = (char *)&inq80[4];
5389                                         sn[len] = 0;
5390                                         while (*sn && (*sn == ' '))
5391                                                 sn++;
5392                                         if (*sn) {
5393                                                 (void) ddi_prop_update_string(
5394                                                     DDI_DEV_T_NONE,
5395                                                     SD_DEVINFO(un),
5396                                                     INQUIRY_SERIAL_NO, sn);
5397                                         }
5398                                 }
5399                         }
5400                         mutex_enter(SD_MUTEX(un));
5401                 }
5402 
5403                 /* collect page 83 data if available */
5404                 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) {
5405                         mutex_exit(SD_MUTEX(un));
5406                         inq83 = kmem_zalloc(inq83_len, KM_SLEEP);
5407 
5408                         rval = sd_send_scsi_INQUIRY(ssc, inq83, inq83_len,
5409                             0x01, 0x83, &inq83_resid);
5410 
5411                         if (rval != 0) {
5412                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5413                                 kmem_free(inq83, inq83_len);
5414                                 inq83 = NULL;
5415                                 inq83_len = 0;
5416                         }
5417                         mutex_enter(SD_MUTEX(un));
5418                 }
5419         }
5420 
5421         /*
5422          * If transport has already registered a devid for this target
5423          * then that takes precedence over the driver's determination
5424          * of the devid.
5425          *
5426          * NOTE: The reason this check is done here instead of at the beginning
5427          * of the function is to allow the code above to create the
5428          * 'inquiry-serial-no' property.
5429          */
5430         if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) {
5431                 ASSERT(un->un_devid);
5432                 un->un_f_devid_transport_defined = TRUE;
5433                 goto cleanup; /* use devid registered by the transport */
5434         }
5435 
5436         /*
5437          * This is the case of antiquated Sun disk drives that have the
5438          * FAB_DEVID property set in the disk_table.  These drives
5439          * manage the devid's by storing them in last 2 available sectors
5440          * on the drive and have them fabricated by the ddi layer by calling
5441          * ddi_devid_init and passing the DEVID_FAB flag.
5442          */
5443         if (un->un_f_opt_fab_devid == TRUE) {
5444                 /*
5445                  * Depending on EINVAL isn't reliable, since a reserved disk
5446                  * may result in invalid geometry, so check to make sure a
5447                  * reservation conflict did not occur during attach.
5448                  */
5449                 if ((sd_get_devid(ssc) == EINVAL) &&
5450                     (reservation_flag != SD_TARGET_IS_RESERVED)) {
5451                         /*
5452                          * The devid is invalid AND there is no reservation
5453                          * conflict.  Fabricate a new devid.
5454                          */
5455                         (void) sd_create_devid(ssc);
5456                 }
5457 
5458                 /* Register the devid if it exists */
5459                 if (un->un_devid != NULL) {
5460                         (void) ddi_devid_register(SD_DEVINFO(un),
5461                             un->un_devid);
5462                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
5463                             "sd_register_devid: Devid Fabricated\n");
5464                 }
5465                 goto cleanup;
5466         }
5467 
5468         /* encode best devid possible based on data available */
5469         if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST,
5470             (char *)ddi_driver_name(SD_DEVINFO(un)),
5471             (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)),
5472             inq80, inq80_len - inq80_resid, inq83, inq83_len -
5473             inq83_resid, &un->un_devid) == DDI_SUCCESS) {
5474 
5475                 /* devid successfully encoded, register devid */
5476                 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid);
5477 
5478         } else {
5479                 /*
5480                  * Unable to encode a devid based on data available.
5481                  * This is not a Sun qualified disk.  Older Sun disk
5482                  * drives that have the SD_FAB_DEVID property
5483                  * set in the disk_table and non Sun qualified
5484                  * disks are treated in the same manner.  These
5485                  * drives manage the devid's by storing them in
5486                  * last 2 available sectors on the drive and
5487                  * have them fabricated by the ddi layer by
5488                  * calling ddi_devid_init and passing the
5489                  * DEVID_FAB flag.
5490                  * Create a fabricate devid only if there's no
5491                  * fabricate devid existed.
5492                  */
5493                 if (sd_get_devid(ssc) == EINVAL) {
5494                         (void) sd_create_devid(ssc);
5495                 }
5496                 un->un_f_opt_fab_devid = TRUE;
5497 
5498                 /* Register the devid if it exists */
5499                 if (un->un_devid != NULL) {
5500                         (void) ddi_devid_register(SD_DEVINFO(un),
5501                             un->un_devid);
5502                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
5503                             "sd_register_devid: devid fabricated using "
5504                             "ddi framework\n");
5505                 }
5506         }
5507 
5508 cleanup:
5509         /* clean up resources */
5510         if (inq80 != NULL) {
5511                 kmem_free(inq80, inq80_len);
5512         }
5513         if (inq83 != NULL) {
5514                 kmem_free(inq83, inq83_len);
5515         }
5516 }
5517 
5518 
5519 
5520 /*
5521  *    Function: sd_get_devid
5522  *
5523  * Description: This routine will return 0 if a valid device id has been
5524  *              obtained from the target and stored in the soft state. If a
5525  *              valid device id has not been previously read and stored, a
5526  *              read attempt will be made.
5527  *
5528  *   Arguments: un - driver soft state (unit) structure
5529  *
5530  * Return Code: 0 if we successfully get the device id
5531  *
5532  *     Context: Kernel Thread
5533  */
5534 
5535 static int
5536 sd_get_devid(sd_ssc_t *ssc)
5537 {
5538         struct dk_devid         *dkdevid;
5539         ddi_devid_t             tmpid;
5540         uint_t                  *ip;
5541         size_t                  sz;
5542         diskaddr_t              blk;
5543         int                     status;
5544         int                     chksum;
5545         int                     i;
5546         size_t                  buffer_size;
5547         struct sd_lun           *un;
5548 
5549         ASSERT(ssc != NULL);
5550         un = ssc->ssc_un;
5551         ASSERT(un != NULL);
5552         ASSERT(mutex_owned(SD_MUTEX(un)));
5553 
5554         SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n",
5555             un);
5556 
5557         if (un->un_devid != NULL) {
5558                 return (0);
5559         }
5560 
5561         mutex_exit(SD_MUTEX(un));
5562         if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5563             (void *)SD_PATH_DIRECT) != 0) {
5564                 mutex_enter(SD_MUTEX(un));
5565                 return (EINVAL);
5566         }
5567 
5568         /*
5569          * Read and verify device id, stored in the reserved cylinders at the
5570          * end of the disk. Backup label is on the odd sectors of the last
5571          * track of the last cylinder. Device id will be on track of the next
5572          * to last cylinder.
5573          */
5574         mutex_enter(SD_MUTEX(un));
5575         buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid));
5576         mutex_exit(SD_MUTEX(un));
5577         dkdevid = kmem_alloc(buffer_size, KM_SLEEP);
5578         status = sd_send_scsi_READ(ssc, dkdevid, buffer_size, blk,
5579             SD_PATH_DIRECT);
5580 
5581         if (status != 0) {
5582                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5583                 goto error;
5584         }
5585 
5586         /* Validate the revision */
5587         if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
5588             (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
5589                 status = EINVAL;
5590                 goto error;
5591         }
5592 
5593         /* Calculate the checksum */
5594         chksum = 0;
5595         ip = (uint_t *)dkdevid;
5596         for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5597             i++) {
5598                 chksum ^= ip[i];
5599         }
5600 
5601         /* Compare the checksums */
5602         if (DKD_GETCHKSUM(dkdevid) != chksum) {
5603                 status = EINVAL;
5604                 goto error;
5605         }
5606 
5607         /* Validate the device id */
5608         if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
5609                 status = EINVAL;
5610                 goto error;
5611         }
5612 
5613         /*
5614          * Store the device id in the driver soft state
5615          */
5616         sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
5617         tmpid = kmem_alloc(sz, KM_SLEEP);
5618 
5619         mutex_enter(SD_MUTEX(un));
5620 
5621         un->un_devid = tmpid;
5622         bcopy(&dkdevid->dkd_devid, un->un_devid, sz);
5623 
5624         kmem_free(dkdevid, buffer_size);
5625 
5626         SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un);
5627 
5628         return (status);
5629 error:
5630         mutex_enter(SD_MUTEX(un));
5631         kmem_free(dkdevid, buffer_size);
5632         return (status);
5633 }
5634 
5635 
5636 /*
5637  *    Function: sd_create_devid
5638  *
5639  * Description: This routine will fabricate the device id and write it
5640  *              to the disk.
5641  *
5642  *   Arguments: un - driver soft state (unit) structure
5643  *
5644  * Return Code: value of the fabricated device id
5645  *
5646  *     Context: Kernel Thread
5647  */
5648 
5649 static ddi_devid_t
5650 sd_create_devid(sd_ssc_t *ssc)
5651 {
5652         struct sd_lun   *un;
5653 
5654         ASSERT(ssc != NULL);
5655         un = ssc->ssc_un;
5656         ASSERT(un != NULL);
5657 
5658         /* Fabricate the devid */
5659         if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid)
5660             == DDI_FAILURE) {
5661                 return (NULL);
5662         }
5663 
5664         /* Write the devid to disk */
5665         if (sd_write_deviceid(ssc) != 0) {
5666                 ddi_devid_free(un->un_devid);
5667                 un->un_devid = NULL;
5668         }
5669 
5670         return (un->un_devid);
5671 }
5672 
5673 
5674 /*
5675  *    Function: sd_write_deviceid
5676  *
5677  * Description: This routine will write the device id to the disk
5678  *              reserved sector.
5679  *
5680  *   Arguments: un - driver soft state (unit) structure
5681  *
5682  * Return Code: EINVAL
5683  *              value returned by sd_send_scsi_cmd
5684  *
5685  *     Context: Kernel Thread
5686  */
5687 
5688 static int
5689 sd_write_deviceid(sd_ssc_t *ssc)
5690 {
5691         struct dk_devid         *dkdevid;
5692         uchar_t                 *buf;
5693         diskaddr_t              blk;
5694         uint_t                  *ip, chksum;
5695         int                     status;
5696         int                     i;
5697         struct sd_lun           *un;
5698 
5699         ASSERT(ssc != NULL);
5700         un = ssc->ssc_un;
5701         ASSERT(un != NULL);
5702         ASSERT(mutex_owned(SD_MUTEX(un)));
5703 
5704         mutex_exit(SD_MUTEX(un));
5705         if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5706             (void *)SD_PATH_DIRECT) != 0) {
5707                 mutex_enter(SD_MUTEX(un));
5708                 return (-1);
5709         }
5710 
5711 
5712         /* Allocate the buffer */
5713         buf = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP);
5714         dkdevid = (struct dk_devid *)buf;
5715 
5716         /* Fill in the revision */
5717         dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
5718         dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
5719 
5720         /* Copy in the device id */
5721         mutex_enter(SD_MUTEX(un));
5722         bcopy(un->un_devid, &dkdevid->dkd_devid,
5723             ddi_devid_sizeof(un->un_devid));
5724         mutex_exit(SD_MUTEX(un));
5725 
5726         /* Calculate the checksum */
5727         chksum = 0;
5728         ip = (uint_t *)dkdevid;
5729         for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5730             i++) {
5731                 chksum ^= ip[i];
5732         }
5733 
5734         /* Fill-in checksum */
5735         DKD_FORMCHKSUM(chksum, dkdevid);
5736 
5737         /* Write the reserved sector */
5738         status = sd_send_scsi_WRITE(ssc, buf, un->un_sys_blocksize, blk,
5739             SD_PATH_DIRECT);
5740         if (status != 0)
5741                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5742 
5743         kmem_free(buf, un->un_sys_blocksize);
5744 
5745         mutex_enter(SD_MUTEX(un));
5746         return (status);
5747 }
5748 
5749 
5750 /*
5751  *    Function: sd_check_vpd_page_support
5752  *
5753  * Description: This routine sends an inquiry command with the EVPD bit set and
5754  *              a page code of 0x00 to the device. It is used to determine which
5755  *              vital product pages are available to find the devid. We are
5756  *              looking for pages 0x83 0x80 or 0xB1.  If we return a negative 1,
5757  *              the device does not support that command.
5758  *
5759  *   Arguments: un  - driver soft state (unit) structure
5760  *
5761  * Return Code: 0 - success
5762  *              1 - check condition
5763  *
5764  *     Context: This routine can sleep.
5765  */
5766 
5767 static int
5768 sd_check_vpd_page_support(sd_ssc_t *ssc)
5769 {
5770         uchar_t *page_list      = NULL;
5771         uchar_t page_length     = 0xff; /* Use max possible length */
5772         uchar_t evpd            = 0x01; /* Set the EVPD bit */
5773         uchar_t page_code       = 0x00; /* Supported VPD Pages */
5774         int     rval            = 0;
5775         int     counter;
5776         struct sd_lun           *un;
5777 
5778         ASSERT(ssc != NULL);
5779         un = ssc->ssc_un;
5780         ASSERT(un != NULL);
5781         ASSERT(mutex_owned(SD_MUTEX(un)));
5782 
5783         mutex_exit(SD_MUTEX(un));
5784 
5785         /*
5786          * We'll set the page length to the maximum to save figuring it out
5787          * with an additional call.
5788          */
5789         page_list =  kmem_zalloc(page_length, KM_SLEEP);
5790 
5791         rval = sd_send_scsi_INQUIRY(ssc, page_list, page_length, evpd,
5792             page_code, NULL);
5793 
5794         if (rval != 0)
5795                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5796 
5797         mutex_enter(SD_MUTEX(un));
5798 
5799         /*
5800          * Now we must validate that the device accepted the command, as some
5801          * drives do not support it.  If the drive does support it, we will
5802          * return 0, and the supported pages will be in un_vpd_page_mask.  If
5803          * not, we return -1.
5804          */
5805         if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) {
5806                 /* Loop to find one of the 2 pages we need */
5807                 counter = 4;  /* Supported pages start at byte 4, with 0x00 */
5808 
5809                 /*
5810                  * Pages are returned in ascending order, and 0x83 is what we
5811                  * are hoping for.
5812                  */
5813                 while ((page_list[counter] <= 0xB1) &&
5814                     (counter <= (page_list[VPD_PAGE_LENGTH] +
5815                     VPD_HEAD_OFFSET))) {
5816                         /*
5817                          * Add 3 because page_list[3] is the number of
5818                          * pages minus 3
5819                          */
5820 
5821                         switch (page_list[counter]) {
5822                         case 0x00:
5823                                 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG;
5824                                 break;
5825                         case 0x80:
5826                                 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG;
5827                                 break;
5828                         case 0x81:
5829                                 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG;
5830                                 break;
5831                         case 0x82:
5832                                 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG;
5833                                 break;
5834                         case 0x83:
5835                                 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG;
5836                                 break;
5837                         case 0x86:
5838                                 un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG;
5839                                 break;
5840                         case 0xB1:
5841                                 un->un_vpd_page_mask |= SD_VPD_DEV_CHARACTER_PG;
5842                                 break;
5843                         }
5844                         counter++;
5845                 }
5846 
5847         } else {
5848                 rval = -1;
5849 
5850                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
5851                     "sd_check_vpd_page_support: This drive does not implement "
5852                     "VPD pages.\n");
5853         }
5854 
5855         kmem_free(page_list, page_length);
5856 
5857         return (rval);
5858 }
5859 
5860 
5861 /*
5862  *    Function: sd_setup_pm
5863  *
5864  * Description: Initialize Power Management on the device
5865  *
5866  *     Context: Kernel Thread
5867  */
5868 
5869 static void
5870 sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi)
5871 {
5872         uint_t          log_page_size;
5873         uchar_t         *log_page_data;
5874         int             rval = 0;
5875         struct sd_lun   *un;
5876 
5877         ASSERT(ssc != NULL);
5878         un = ssc->ssc_un;
5879         ASSERT(un != NULL);
5880 
5881         /*
5882          * Since we are called from attach, holding a mutex for
5883          * un is unnecessary. Because some of the routines called
5884          * from here require SD_MUTEX to not be held, assert this
5885          * right up front.
5886          */
5887         ASSERT(!mutex_owned(SD_MUTEX(un)));
5888         /*
5889          * Since the sd device does not have the 'reg' property,
5890          * cpr will not call its DDI_SUSPEND/DDI_RESUME entries.
5891          * The following code is to tell cpr that this device
5892          * DOES need to be suspended and resumed.
5893          */
5894         (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
5895             "pm-hardware-state", "needs-suspend-resume");
5896 
5897         /*
5898          * This complies with the new power management framework
5899          * for certain desktop machines. Create the pm_components
5900          * property as a string array property.
5901          * If un_f_pm_supported is TRUE, that means the disk
5902          * attached HBA has set the "pm-capable" property and
5903          * the value of this property is bigger than 0.
5904          */
5905         if (un->un_f_pm_supported) {
5906                 /*
5907                  * not all devices have a motor, try it first.
5908                  * some devices may return ILLEGAL REQUEST, some
5909                  * will hang
5910                  * The following START_STOP_UNIT is used to check if target
5911                  * device has a motor.
5912                  */
5913                 un->un_f_start_stop_supported = TRUE;
5914 
5915                 if (un->un_f_power_condition_supported) {
5916                         rval = sd_send_scsi_START_STOP_UNIT(ssc,
5917                             SD_POWER_CONDITION, SD_TARGET_ACTIVE,
5918                             SD_PATH_DIRECT);
5919                         if (rval != 0) {
5920                                 un->un_f_power_condition_supported = FALSE;
5921                         }
5922                 }
5923                 if (!un->un_f_power_condition_supported) {
5924                         rval = sd_send_scsi_START_STOP_UNIT(ssc,
5925                             SD_START_STOP, SD_TARGET_START, SD_PATH_DIRECT);
5926                 }
5927                 if (rval != 0) {
5928                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5929                         un->un_f_start_stop_supported = FALSE;
5930                 }
5931 
5932                 /*
5933                  * create pm properties anyways otherwise the parent can't
5934                  * go to sleep
5935                  */
5936                 un->un_f_pm_is_enabled = TRUE;
5937                 (void) sd_create_pm_components(devi, un);
5938 
5939                 /*
5940                  * If it claims that log sense is supported, check it out.
5941                  */
5942                 if (un->un_f_log_sense_supported) {
5943                         rval = sd_log_page_supported(ssc,
5944                             START_STOP_CYCLE_PAGE);
5945                         if (rval == 1) {
5946                                 /* Page found, use it. */
5947                                 un->un_start_stop_cycle_page =
5948                                     START_STOP_CYCLE_PAGE;
5949                         } else {
5950                                 /*
5951                                  * Page not found or log sense is not
5952                                  * supported.
5953                                  * Notice we do not check the old style
5954                                  * START_STOP_CYCLE_VU_PAGE because this
5955                                  * code path does not apply to old disks.
5956                                  */
5957                                 un->un_f_log_sense_supported = FALSE;
5958                                 un->un_f_pm_log_sense_smart = FALSE;
5959                         }
5960                 }
5961 
5962                 return;
5963         }
5964 
5965         /*
5966          * For the disk whose attached HBA has not set the "pm-capable"
5967          * property, check if it supports the power management.
5968          */
5969         if (!un->un_f_log_sense_supported) {
5970                 un->un_power_level = SD_SPINDLE_ON;
5971                 un->un_f_pm_is_enabled = FALSE;
5972                 return;
5973         }
5974 
5975         rval = sd_log_page_supported(ssc, START_STOP_CYCLE_PAGE);
5976 
5977 #ifdef  SDDEBUG
5978         if (sd_force_pm_supported) {
5979                 /* Force a successful result */
5980                 rval = 1;
5981         }
5982 #endif
5983 
5984         /*
5985          * If the start-stop cycle counter log page is not supported
5986          * or if the pm-capable property is set to be false (0),
5987          * then we should not create the pm_components property.
5988          */
5989         if (rval == -1) {
5990                 /*
5991                  * Error.
5992                  * Reading log sense failed, most likely this is
5993                  * an older drive that does not support log sense.
5994                  * If this fails auto-pm is not supported.
5995                  */
5996                 un->un_power_level = SD_SPINDLE_ON;
5997                 un->un_f_pm_is_enabled = FALSE;
5998 
5999         } else if (rval == 0) {
6000                 /*
6001                  * Page not found.
6002                  * The start stop cycle counter is implemented as page
6003                  * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For
6004                  * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE).
6005                  */
6006                 if (sd_log_page_supported(ssc, START_STOP_CYCLE_VU_PAGE) == 1) {
6007                         /*
6008                          * Page found, use this one.
6009                          */
6010                         un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE;
6011                         un->un_f_pm_is_enabled = TRUE;
6012                 } else {
6013                         /*
6014                          * Error or page not found.
6015                          * auto-pm is not supported for this device.
6016                          */
6017                         un->un_power_level = SD_SPINDLE_ON;
6018                         un->un_f_pm_is_enabled = FALSE;
6019                 }
6020         } else {
6021                 /*
6022                  * Page found, use it.
6023                  */
6024                 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE;
6025                 un->un_f_pm_is_enabled = TRUE;
6026         }
6027 
6028 
6029         if (un->un_f_pm_is_enabled == TRUE) {
6030                 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6031                 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6032 
6033                 rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
6034                     log_page_size, un->un_start_stop_cycle_page,
6035                     0x01, 0, SD_PATH_DIRECT);
6036 
6037                 if (rval != 0) {
6038                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6039                 }
6040 
6041 #ifdef  SDDEBUG
6042                 if (sd_force_pm_supported) {
6043                         /* Force a successful result */
6044                         rval = 0;
6045                 }
6046 #endif
6047 
6048                 /*
6049                  * If the Log sense for Page( Start/stop cycle counter page)
6050                  * succeeds, then power management is supported and we can
6051                  * enable auto-pm.
6052                  */
6053                 if (rval == 0)  {
6054                         (void) sd_create_pm_components(devi, un);
6055                 } else {
6056                         un->un_power_level = SD_SPINDLE_ON;
6057                         un->un_f_pm_is_enabled = FALSE;
6058                 }
6059 
6060                 kmem_free(log_page_data, log_page_size);
6061         }
6062 }
6063 
6064 
6065 /*
6066  *    Function: sd_create_pm_components
6067  *
6068  * Description: Initialize PM property.
6069  *
6070  *     Context: Kernel thread context
6071  */
6072 
6073 static void
6074 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un)
6075 {
6076         ASSERT(!mutex_owned(SD_MUTEX(un)));
6077 
6078         if (un->un_f_power_condition_supported) {
6079                 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6080                     "pm-components", sd_pwr_pc.pm_comp, 5)
6081                     != DDI_PROP_SUCCESS) {
6082                         un->un_power_level = SD_SPINDLE_ACTIVE;
6083                         un->un_f_pm_is_enabled = FALSE;
6084                         return;
6085                 }
6086         } else {
6087                 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6088                     "pm-components", sd_pwr_ss.pm_comp, 3)
6089                     != DDI_PROP_SUCCESS) {
6090                         un->un_power_level = SD_SPINDLE_ON;
6091                         un->un_f_pm_is_enabled = FALSE;
6092                         return;
6093                 }
6094         }
6095         /*
6096          * When components are initially created they are idle,
6097          * power up any non-removables.
6098          * Note: the return value of pm_raise_power can't be used
6099          * for determining if PM should be enabled for this device.
6100          * Even if you check the return values and remove this
6101          * property created above, the PM framework will not honor the
6102          * change after the first call to pm_raise_power. Hence,
6103          * removal of that property does not help if pm_raise_power
6104          * fails. In the case of removable media, the start/stop
6105          * will fail if the media is not present.
6106          */
6107         if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0,
6108             SD_PM_STATE_ACTIVE(un)) == DDI_SUCCESS)) {
6109                 mutex_enter(SD_MUTEX(un));
6110                 un->un_power_level = SD_PM_STATE_ACTIVE(un);
6111                 mutex_enter(&un->un_pm_mutex);
6112                 /* Set to on and not busy. */
6113                 un->un_pm_count = 0;
6114         } else {
6115                 mutex_enter(SD_MUTEX(un));
6116                 un->un_power_level = SD_PM_STATE_STOPPED(un);
6117                 mutex_enter(&un->un_pm_mutex);
6118                 /* Set to off. */
6119                 un->un_pm_count = -1;
6120         }
6121         mutex_exit(&un->un_pm_mutex);
6122         mutex_exit(SD_MUTEX(un));
6123 }
6124 
6125 
6126 /*
6127  *    Function: sd_ddi_suspend
6128  *
6129  * Description: Performs system power-down operations. This includes
6130  *              setting the drive state to indicate its suspended so
6131  *              that no new commands will be accepted. Also, wait for
6132  *              all commands that are in transport or queued to a timer
6133  *              for retry to complete. All timeout threads are cancelled.
6134  *
6135  * Return Code: DDI_FAILURE or DDI_SUCCESS
6136  *
6137  *     Context: Kernel thread context
6138  */
6139 
6140 static int
6141 sd_ddi_suspend(dev_info_t *devi)
6142 {
6143         struct  sd_lun  *un;
6144         clock_t         wait_cmds_complete;
6145 
6146         un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6147         if (un == NULL) {
6148                 return (DDI_FAILURE);
6149         }
6150 
6151         SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n");
6152 
6153         mutex_enter(SD_MUTEX(un));
6154 
6155         /* Return success if the device is already suspended. */
6156         if (un->un_state == SD_STATE_SUSPENDED) {
6157                 mutex_exit(SD_MUTEX(un));
6158                 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6159                     "device already suspended, exiting\n");
6160                 return (DDI_SUCCESS);
6161         }
6162 
6163         /* Return failure if the device is being used by HA */
6164         if (un->un_resvd_status &
6165             (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) {
6166                 mutex_exit(SD_MUTEX(un));
6167                 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6168                     "device in use by HA, exiting\n");
6169                 return (DDI_FAILURE);
6170         }
6171 
6172         /*
6173          * Return failure if the device is in a resource wait
6174          * or power changing state.
6175          */
6176         if ((un->un_state == SD_STATE_RWAIT) ||
6177             (un->un_state == SD_STATE_PM_CHANGING)) {
6178                 mutex_exit(SD_MUTEX(un));
6179                 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6180                     "device in resource wait state, exiting\n");
6181                 return (DDI_FAILURE);
6182         }
6183 
6184 
6185         un->un_save_state = un->un_last_state;
6186         New_state(un, SD_STATE_SUSPENDED);
6187 
6188         /*
6189          * Wait for all commands that are in transport or queued to a timer
6190          * for retry to complete.
6191          *
6192          * While waiting, no new commands will be accepted or sent because of
6193          * the new state we set above.
6194          *
6195          * Wait till current operation has completed. If we are in the resource
6196          * wait state (with an intr outstanding) then we need to wait till the
6197          * intr completes and starts the next cmd. We want to wait for
6198          * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND.
6199          */
6200         wait_cmds_complete = ddi_get_lbolt() +
6201             (sd_wait_cmds_complete * drv_usectohz(1000000));
6202 
6203         while (un->un_ncmds_in_transport != 0) {
6204                 /*
6205                  * Fail if commands do not finish in the specified time.
6206                  */
6207                 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un),
6208                     wait_cmds_complete) == -1) {
6209                         /*
6210                          * Undo the state changes made above. Everything
6211                          * must go back to it's original value.
6212                          */
6213                         Restore_state(un);
6214                         un->un_last_state = un->un_save_state;
6215                         /* Wake up any threads that might be waiting. */
6216                         cv_broadcast(&un->un_suspend_cv);
6217                         mutex_exit(SD_MUTEX(un));
6218                         SD_ERROR(SD_LOG_IO_PM, un,
6219                             "sd_ddi_suspend: failed due to outstanding cmds\n");
6220                         SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n");
6221                         return (DDI_FAILURE);
6222                 }
6223         }
6224 
6225         /*
6226          * Cancel SCSI watch thread and timeouts, if any are active
6227          */
6228 
6229         if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) {
6230                 opaque_t temp_token = un->un_swr_token;
6231                 mutex_exit(SD_MUTEX(un));
6232                 scsi_watch_suspend(temp_token);
6233                 mutex_enter(SD_MUTEX(un));
6234         }
6235 
6236         if (un->un_reset_throttle_timeid != NULL) {
6237                 timeout_id_t temp_id = un->un_reset_throttle_timeid;
6238                 un->un_reset_throttle_timeid = NULL;
6239                 mutex_exit(SD_MUTEX(un));
6240                 (void) untimeout(temp_id);
6241                 mutex_enter(SD_MUTEX(un));
6242         }
6243 
6244         if (un->un_dcvb_timeid != NULL) {
6245                 timeout_id_t temp_id = un->un_dcvb_timeid;
6246                 un->un_dcvb_timeid = NULL;
6247                 mutex_exit(SD_MUTEX(un));
6248                 (void) untimeout(temp_id);
6249                 mutex_enter(SD_MUTEX(un));
6250         }
6251 
6252         mutex_enter(&un->un_pm_mutex);
6253         if (un->un_pm_timeid != NULL) {
6254                 timeout_id_t temp_id = un->un_pm_timeid;
6255                 un->un_pm_timeid = NULL;
6256                 mutex_exit(&un->un_pm_mutex);
6257                 mutex_exit(SD_MUTEX(un));
6258                 (void) untimeout(temp_id);
6259                 mutex_enter(SD_MUTEX(un));
6260         } else {
6261                 mutex_exit(&un->un_pm_mutex);
6262         }
6263 
6264         if (un->un_rmw_msg_timeid != NULL) {
6265                 timeout_id_t temp_id = un->un_rmw_msg_timeid;
6266                 un->un_rmw_msg_timeid = NULL;
6267                 mutex_exit(SD_MUTEX(un));
6268                 (void) untimeout(temp_id);
6269                 mutex_enter(SD_MUTEX(un));
6270         }
6271 
6272         if (un->un_retry_timeid != NULL) {
6273                 timeout_id_t temp_id = un->un_retry_timeid;
6274                 un->un_retry_timeid = NULL;
6275                 mutex_exit(SD_MUTEX(un));
6276                 (void) untimeout(temp_id);
6277                 mutex_enter(SD_MUTEX(un));
6278 
6279                 if (un->un_retry_bp != NULL) {
6280                         un->un_retry_bp->av_forw = un->un_waitq_headp;
6281                         un->un_waitq_headp = un->un_retry_bp;
6282                         if (un->un_waitq_tailp == NULL) {
6283                                 un->un_waitq_tailp = un->un_retry_bp;
6284                         }
6285                         un->un_retry_bp = NULL;
6286                         un->un_retry_statp = NULL;
6287                 }
6288         }
6289 
6290         if (un->un_direct_priority_timeid != NULL) {
6291                 timeout_id_t temp_id = un->un_direct_priority_timeid;
6292                 un->un_direct_priority_timeid = NULL;
6293                 mutex_exit(SD_MUTEX(un));
6294                 (void) untimeout(temp_id);
6295                 mutex_enter(SD_MUTEX(un));
6296         }
6297 
6298         if (un->un_f_is_fibre == TRUE) {
6299                 /*
6300                  * Remove callbacks for insert and remove events
6301                  */
6302                 if (un->un_insert_event != NULL) {
6303                         mutex_exit(SD_MUTEX(un));
6304                         (void) ddi_remove_event_handler(un->un_insert_cb_id);
6305                         mutex_enter(SD_MUTEX(un));
6306                         un->un_insert_event = NULL;
6307                 }
6308 
6309                 if (un->un_remove_event != NULL) {
6310                         mutex_exit(SD_MUTEX(un));
6311                         (void) ddi_remove_event_handler(un->un_remove_cb_id);
6312                         mutex_enter(SD_MUTEX(un));
6313                         un->un_remove_event = NULL;
6314                 }
6315         }
6316 
6317         mutex_exit(SD_MUTEX(un));
6318 
6319         SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n");
6320 
6321         return (DDI_SUCCESS);
6322 }
6323 
6324 
6325 /*
6326  *    Function: sd_ddi_resume
6327  *
6328  * Description: Performs system power-up operations..
6329  *
6330  * Return Code: DDI_SUCCESS
6331  *              DDI_FAILURE
6332  *
6333  *     Context: Kernel thread context
6334  */
6335 
6336 static int
6337 sd_ddi_resume(dev_info_t *devi)
6338 {
6339         struct  sd_lun  *un;
6340 
6341         un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6342         if (un == NULL) {
6343                 return (DDI_FAILURE);
6344         }
6345 
6346         SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n");
6347 
6348         mutex_enter(SD_MUTEX(un));
6349         Restore_state(un);
6350 
6351         /*
6352          * Restore the state which was saved to give the
6353          * the right state in un_last_state
6354          */
6355         un->un_last_state = un->un_save_state;
6356         /*
6357          * Note: throttle comes back at full.
6358          * Also note: this MUST be done before calling pm_raise_power
6359          * otherwise the system can get hung in biowait. The scenario where
6360          * this'll happen is under cpr suspend. Writing of the system
6361          * state goes through sddump, which writes 0 to un_throttle. If
6362          * writing the system state then fails, example if the partition is
6363          * too small, then cpr attempts a resume. If throttle isn't restored
6364          * from the saved value until after calling pm_raise_power then
6365          * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs
6366          * in biowait.
6367          */
6368         un->un_throttle = un->un_saved_throttle;
6369 
6370         /*
6371          * The chance of failure is very rare as the only command done in power
6372          * entry point is START command when you transition from 0->1 or
6373          * unknown->1. Put it to SPINDLE ON state irrespective of the state at
6374          * which suspend was done. Ignore the return value as the resume should
6375          * not be failed. In the case of removable media the media need not be
6376          * inserted and hence there is a chance that raise power will fail with
6377          * media not present.
6378          */
6379         if (un->un_f_attach_spinup) {
6380                 mutex_exit(SD_MUTEX(un));
6381                 (void) pm_raise_power(SD_DEVINFO(un), 0,
6382                     SD_PM_STATE_ACTIVE(un));
6383                 mutex_enter(SD_MUTEX(un));
6384         }
6385 
6386         /*
6387          * Don't broadcast to the suspend cv and therefore possibly
6388          * start I/O until after power has been restored.
6389          */
6390         cv_broadcast(&un->un_suspend_cv);
6391         cv_broadcast(&un->un_state_cv);
6392 
6393         /* restart thread */
6394         if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) {
6395                 scsi_watch_resume(un->un_swr_token);
6396         }
6397 
6398 #if (defined(__fibre))
6399         if (un->un_f_is_fibre == TRUE) {
6400                 /*
6401                  * Add callbacks for insert and remove events
6402                  */
6403                 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
6404                         sd_init_event_callbacks(un);
6405                 }
6406         }
6407 #endif
6408 
6409         /*
6410          * Transport any pending commands to the target.
6411          *
6412          * If this is a low-activity device commands in queue will have to wait
6413          * until new commands come in, which may take awhile. Also, we
6414          * specifically don't check un_ncmds_in_transport because we know that
6415          * there really are no commands in progress after the unit was
6416          * suspended and we could have reached the throttle level, been
6417          * suspended, and have no new commands coming in for awhile. Highly
6418          * unlikely, but so is the low-activity disk scenario.
6419          */
6420         ddi_xbuf_dispatch(un->un_xbuf_attr);
6421 
6422         sd_start_cmds(un, NULL);
6423         mutex_exit(SD_MUTEX(un));
6424 
6425         SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n");
6426 
6427         return (DDI_SUCCESS);
6428 }
6429 
6430 
6431 /*
6432  *    Function: sd_pm_state_change
6433  *
6434  * Description: Change the driver power state.
6435  *              Someone else is required to actually change the driver
6436  *              power level.
6437  *
6438  *   Arguments: un - driver soft state (unit) structure
6439  *              level - the power level that is changed to
6440  *              flag - to decide how to change the power state
6441  *
6442  * Return Code: DDI_SUCCESS
6443  *
6444  *     Context: Kernel thread context
6445  */
6446 static int
6447 sd_pm_state_change(struct sd_lun *un, int level, int flag)
6448 {
6449         ASSERT(un != NULL);
6450         SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: entry\n");
6451 
6452         ASSERT(!mutex_owned(SD_MUTEX(un)));
6453         mutex_enter(SD_MUTEX(un));
6454 
6455         if (flag == SD_PM_STATE_ROLLBACK || SD_PM_IS_IO_CAPABLE(un, level)) {
6456                 un->un_power_level = level;
6457                 ASSERT(!mutex_owned(&un->un_pm_mutex));
6458                 mutex_enter(&un->un_pm_mutex);
6459                 if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
6460                         un->un_pm_count++;
6461                         ASSERT(un->un_pm_count == 0);
6462                 }
6463                 mutex_exit(&un->un_pm_mutex);
6464         } else {
6465                 /*
6466                  * Exit if power management is not enabled for this device,
6467                  * or if the device is being used by HA.
6468                  */
6469                 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status &
6470                     (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) {
6471                         mutex_exit(SD_MUTEX(un));
6472                         SD_TRACE(SD_LOG_POWER, un,
6473                             "sd_pm_state_change: exiting\n");
6474                         return (DDI_FAILURE);
6475                 }
6476 
6477                 SD_INFO(SD_LOG_POWER, un, "sd_pm_state_change: "
6478                     "un_ncmds_in_driver=%ld\n", un->un_ncmds_in_driver);
6479 
6480                 /*
6481                  * See if the device is not busy, ie.:
6482                  *    - we have no commands in the driver for this device
6483                  *    - not waiting for resources
6484                  */
6485                 if ((un->un_ncmds_in_driver == 0) &&
6486                     (un->un_state != SD_STATE_RWAIT)) {
6487                         /*
6488                          * The device is not busy, so it is OK to go to low
6489                          * power state. Indicate low power, but rely on someone
6490                          * else to actually change it.
6491                          */
6492                         mutex_enter(&un->un_pm_mutex);
6493                         un->un_pm_count = -1;
6494                         mutex_exit(&un->un_pm_mutex);
6495                         un->un_power_level = level;
6496                 }
6497         }
6498 
6499         mutex_exit(SD_MUTEX(un));
6500 
6501         SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: exit\n");
6502 
6503         return (DDI_SUCCESS);
6504 }
6505 
6506 
6507 /*
6508  *    Function: sd_pm_idletimeout_handler
6509  *
6510  * Description: A timer routine that's active only while a device is busy.
6511  *              The purpose is to extend slightly the pm framework's busy
6512  *              view of the device to prevent busy/idle thrashing for
6513  *              back-to-back commands. Do this by comparing the current time
6514  *              to the time at which the last command completed and when the
6515  *              difference is greater than sd_pm_idletime, call
6516  *              pm_idle_component. In addition to indicating idle to the pm
6517  *              framework, update the chain type to again use the internal pm
6518  *              layers of the driver.
6519  *
6520  *   Arguments: arg - driver soft state (unit) structure
6521  *
6522  *     Context: Executes in a timeout(9F) thread context
6523  */
6524 
6525 static void
6526 sd_pm_idletimeout_handler(void *arg)
6527 {
6528         const hrtime_t idletime = sd_pm_idletime * NANOSEC;
6529         struct sd_lun *un = arg;
6530 
6531         mutex_enter(&sd_detach_mutex);
6532         if (un->un_detach_count != 0) {
6533                 /* Abort if the instance is detaching */
6534                 mutex_exit(&sd_detach_mutex);
6535                 return;
6536         }
6537         mutex_exit(&sd_detach_mutex);
6538 
6539         /*
6540          * Grab both mutexes, in the proper order, since we're accessing
6541          * both PM and softstate variables.
6542          */
6543         mutex_enter(SD_MUTEX(un));
6544         mutex_enter(&un->un_pm_mutex);
6545         if (((gethrtime() - un->un_pm_idle_time) > idletime) &&
6546             (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) {
6547                 /*
6548                  * Update the chain types.
6549                  * This takes affect on the next new command received.
6550                  */
6551                 if (un->un_f_non_devbsize_supported) {
6552                         un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
6553                 } else {
6554                         un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
6555                 }
6556                 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD;
6557 
6558                 SD_TRACE(SD_LOG_IO_PM, un,
6559                     "sd_pm_idletimeout_handler: idling device\n");
6560                 (void) pm_idle_component(SD_DEVINFO(un), 0);
6561                 un->un_pm_idle_timeid = NULL;
6562         } else {
6563                 un->un_pm_idle_timeid =
6564                     timeout(sd_pm_idletimeout_handler, un,
6565                     (drv_usectohz((clock_t)300000))); /* 300 ms. */
6566         }
6567         mutex_exit(&un->un_pm_mutex);
6568         mutex_exit(SD_MUTEX(un));
6569 }
6570 
6571 
6572 /*
6573  *    Function: sd_pm_timeout_handler
6574  *
6575  * Description: Callback to tell framework we are idle.
6576  *
6577  *     Context: timeout(9f) thread context.
6578  */
6579 
6580 static void
6581 sd_pm_timeout_handler(void *arg)
6582 {
6583         struct sd_lun *un = arg;
6584 
6585         (void) pm_idle_component(SD_DEVINFO(un), 0);
6586         mutex_enter(&un->un_pm_mutex);
6587         un->un_pm_timeid = NULL;
6588         mutex_exit(&un->un_pm_mutex);
6589 }
6590 
6591 
6592 /*
6593  *    Function: sdpower
6594  *
6595  * Description: PM entry point.
6596  *
6597  * Return Code: DDI_SUCCESS
6598  *              DDI_FAILURE
6599  *
6600  *     Context: Kernel thread context
6601  */
6602 
6603 static int
6604 sdpower(dev_info_t *devi, int component, int level)
6605 {
6606         struct sd_lun   *un;
6607         int             instance;
6608         int             rval = DDI_SUCCESS;
6609         uint_t          i, log_page_size, maxcycles, ncycles;
6610         uchar_t         *log_page_data;
6611         int             log_sense_page;
6612         int             medium_present;
6613         time_t          intvlp;
6614         struct pm_trans_data    sd_pm_tran_data;
6615         uchar_t         save_state = SD_STATE_NORMAL;
6616         int             sval;
6617         uchar_t         state_before_pm;
6618         int             got_semaphore_here;
6619         sd_ssc_t        *ssc;
6620         int     last_power_level = SD_SPINDLE_UNINIT;
6621 
6622         instance = ddi_get_instance(devi);
6623 
6624         if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
6625             !SD_PM_IS_LEVEL_VALID(un, level) || component != 0) {
6626                 return (DDI_FAILURE);
6627         }
6628 
6629         ssc = sd_ssc_init(un);
6630 
6631         SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level);
6632 
6633         /*
6634          * Must synchronize power down with close.
6635          * Attempt to decrement/acquire the open/close semaphore,
6636          * but do NOT wait on it. If it's not greater than zero,
6637          * ie. it can't be decremented without waiting, then
6638          * someone else, either open or close, already has it
6639          * and the try returns 0. Use that knowledge here to determine
6640          * if it's OK to change the device power level.
6641          * Also, only increment it on exit if it was decremented, ie. gotten,
6642          * here.
6643          */
6644         got_semaphore_here = sema_tryp(&un->un_semoclose);
6645 
6646         mutex_enter(SD_MUTEX(un));
6647 
6648         SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n",
6649             un->un_ncmds_in_driver);
6650 
6651         /*
6652          * If un_ncmds_in_driver is non-zero it indicates commands are
6653          * already being processed in the driver, or if the semaphore was
6654          * not gotten here it indicates an open or close is being processed.
6655          * At the same time somebody is requesting to go to a lower power
6656          * that can't perform I/O, which can't happen, therefore we need to
6657          * return failure.
6658          */
6659         if ((!SD_PM_IS_IO_CAPABLE(un, level)) &&
6660             ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) {
6661                 mutex_exit(SD_MUTEX(un));
6662 
6663                 if (got_semaphore_here != 0) {
6664                         sema_v(&un->un_semoclose);
6665                 }
6666                 SD_TRACE(SD_LOG_IO_PM, un,
6667                     "sdpower: exit, device has queued cmds.\n");
6668 
6669                 goto sdpower_failed;
6670         }
6671 
6672         /*
6673          * if it is OFFLINE that means the disk is completely dead
6674          * in our case we have to put the disk in on or off by sending commands
6675          * Of course that will fail anyway so return back here.
6676          *
6677          * Power changes to a device that's OFFLINE or SUSPENDED
6678          * are not allowed.
6679          */
6680         if ((un->un_state == SD_STATE_OFFLINE) ||
6681             (un->un_state == SD_STATE_SUSPENDED)) {
6682                 mutex_exit(SD_MUTEX(un));
6683 
6684                 if (got_semaphore_here != 0) {
6685                         sema_v(&un->un_semoclose);
6686                 }
6687                 SD_TRACE(SD_LOG_IO_PM, un,
6688                     "sdpower: exit, device is off-line.\n");
6689 
6690                 goto sdpower_failed;
6691         }
6692 
6693         /*
6694          * Change the device's state to indicate it's power level
6695          * is being changed. Do this to prevent a power off in the
6696          * middle of commands, which is especially bad on devices
6697          * that are really powered off instead of just spun down.
6698          */
6699         state_before_pm = un->un_state;
6700         un->un_state = SD_STATE_PM_CHANGING;
6701 
6702         mutex_exit(SD_MUTEX(un));
6703 
6704         /*
6705          * If log sense command is not supported, bypass the
6706          * following checking, otherwise, check the log sense
6707          * information for this device.
6708          */
6709         if (SD_PM_STOP_MOTOR_NEEDED(un, level) &&
6710             un->un_f_log_sense_supported) {
6711                 /*
6712                  * Get the log sense information to understand whether the
6713                  * the powercycle counts have gone beyond the threshhold.
6714                  */
6715                 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6716                 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6717 
6718                 mutex_enter(SD_MUTEX(un));
6719                 log_sense_page = un->un_start_stop_cycle_page;
6720                 mutex_exit(SD_MUTEX(un));
6721 
6722                 rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
6723                     log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT);
6724 
6725                 if (rval != 0) {
6726                         if (rval == EIO)
6727                                 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
6728                         else
6729                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6730                 }
6731 
6732 #ifdef  SDDEBUG
6733                 if (sd_force_pm_supported) {
6734                         /* Force a successful result */
6735                         rval = 0;
6736                 }
6737 #endif
6738                 if (rval != 0) {
6739                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
6740                             "Log Sense Failed\n");
6741 
6742                         kmem_free(log_page_data, log_page_size);
6743                         /* Cannot support power management on those drives */
6744 
6745                         if (got_semaphore_here != 0) {
6746                                 sema_v(&un->un_semoclose);
6747                         }
6748                         /*
6749                          * On exit put the state back to it's original value
6750                          * and broadcast to anyone waiting for the power
6751                          * change completion.
6752                          */
6753                         mutex_enter(SD_MUTEX(un));
6754                         un->un_state = state_before_pm;
6755                         cv_broadcast(&un->un_suspend_cv);
6756                         mutex_exit(SD_MUTEX(un));
6757                         SD_TRACE(SD_LOG_IO_PM, un,
6758                             "sdpower: exit, Log Sense Failed.\n");
6759 
6760                         goto sdpower_failed;
6761                 }
6762 
6763                 /*
6764                  * From the page data - Convert the essential information to
6765                  * pm_trans_data
6766                  */
6767                 maxcycles =
6768                     (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) |
6769                     (log_page_data[0x1E] << 8)  | log_page_data[0x1F];
6770 
6771                 ncycles =
6772                     (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) |
6773                     (log_page_data[0x26] << 8)  | log_page_data[0x27];
6774 
6775                 if (un->un_f_pm_log_sense_smart) {
6776                         sd_pm_tran_data.un.smart_count.allowed = maxcycles;
6777                         sd_pm_tran_data.un.smart_count.consumed = ncycles;
6778                         sd_pm_tran_data.un.smart_count.flag = 0;
6779                         sd_pm_tran_data.format = DC_SMART_FORMAT;
6780                 } else {
6781                         sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles;
6782                         sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles;
6783                         for (i = 0; i < DC_SCSI_MFR_LEN; i++) {
6784                                 sd_pm_tran_data.un.scsi_cycles.svc_date[i] =
6785                                     log_page_data[8+i];
6786                         }
6787                         sd_pm_tran_data.un.scsi_cycles.flag = 0;
6788                         sd_pm_tran_data.format = DC_SCSI_FORMAT;
6789                 }
6790 
6791                 kmem_free(log_page_data, log_page_size);
6792 
6793                 /*
6794                  * Call pm_trans_check routine to get the Ok from
6795                  * the global policy
6796                  */
6797                 rval = pm_trans_check(&sd_pm_tran_data, &intvlp);
6798 #ifdef  SDDEBUG
6799                 if (sd_force_pm_supported) {
6800                         /* Force a successful result */
6801                         rval = 1;
6802                 }
6803 #endif
6804                 switch (rval) {
6805                 case 0:
6806                         /*
6807                          * Not Ok to Power cycle or error in parameters passed
6808                          * Would have given the advised time to consider power
6809                          * cycle. Based on the new intvlp parameter we are
6810                          * supposed to pretend we are busy so that pm framework
6811                          * will never call our power entry point. Because of
6812                          * that install a timeout handler and wait for the
6813                          * recommended time to elapse so that power management
6814                          * can be effective again.
6815                          *
6816                          * To effect this behavior, call pm_busy_component to
6817                          * indicate to the framework this device is busy.
6818                          * By not adjusting un_pm_count the rest of PM in
6819                          * the driver will function normally, and independent
6820                          * of this but because the framework is told the device
6821                          * is busy it won't attempt powering down until it gets
6822                          * a matching idle. The timeout handler sends this.
6823                          * Note: sd_pm_entry can't be called here to do this
6824                          * because sdpower may have been called as a result
6825                          * of a call to pm_raise_power from within sd_pm_entry.
6826                          *
6827                          * If a timeout handler is already active then
6828                          * don't install another.
6829                          */
6830                         mutex_enter(&un->un_pm_mutex);
6831                         if (un->un_pm_timeid == NULL) {
6832                                 un->un_pm_timeid =
6833                                     timeout(sd_pm_timeout_handler,
6834                                     un, intvlp * drv_usectohz(1000000));
6835                                 mutex_exit(&un->un_pm_mutex);
6836                                 (void) pm_busy_component(SD_DEVINFO(un), 0);
6837                         } else {
6838                                 mutex_exit(&un->un_pm_mutex);
6839                         }
6840                         if (got_semaphore_here != 0) {
6841                                 sema_v(&un->un_semoclose);
6842                         }
6843                         /*
6844                          * On exit put the state back to it's original value
6845                          * and broadcast to anyone waiting for the power
6846                          * change completion.
6847                          */
6848                         mutex_enter(SD_MUTEX(un));
6849                         un->un_state = state_before_pm;
6850                         cv_broadcast(&un->un_suspend_cv);
6851                         mutex_exit(SD_MUTEX(un));
6852 
6853                         SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, "
6854                             "trans check Failed, not ok to power cycle.\n");
6855 
6856                         goto sdpower_failed;
6857                 case -1:
6858                         if (got_semaphore_here != 0) {
6859                                 sema_v(&un->un_semoclose);
6860                         }
6861                         /*
6862                          * On exit put the state back to it's original value
6863                          * and broadcast to anyone waiting for the power
6864                          * change completion.
6865                          */
6866                         mutex_enter(SD_MUTEX(un));
6867                         un->un_state = state_before_pm;
6868                         cv_broadcast(&un->un_suspend_cv);
6869                         mutex_exit(SD_MUTEX(un));
6870                         SD_TRACE(SD_LOG_IO_PM, un,
6871                             "sdpower: exit, trans check command Failed.\n");
6872 
6873                         goto sdpower_failed;
6874                 }
6875         }
6876 
6877         if (!SD_PM_IS_IO_CAPABLE(un, level)) {
6878                 /*
6879                  * Save the last state... if the STOP FAILS we need it
6880                  * for restoring
6881                  */
6882                 mutex_enter(SD_MUTEX(un));
6883                 save_state = un->un_last_state;
6884                 last_power_level = un->un_power_level;
6885                 /*
6886                  * There must not be any cmds. getting processed
6887                  * in the driver when we get here. Power to the
6888                  * device is potentially going off.
6889                  */
6890                 ASSERT(un->un_ncmds_in_driver == 0);
6891                 mutex_exit(SD_MUTEX(un));
6892 
6893                 /*
6894                  * For now PM suspend the device completely before spindle is
6895                  * turned off
6896                  */
6897                 if ((rval = sd_pm_state_change(un, level, SD_PM_STATE_CHANGE))
6898                     == DDI_FAILURE) {
6899                         if (got_semaphore_here != 0) {
6900                                 sema_v(&un->un_semoclose);
6901                         }
6902                         /*
6903                          * On exit put the state back to it's original value
6904                          * and broadcast to anyone waiting for the power
6905                          * change completion.
6906                          */
6907                         mutex_enter(SD_MUTEX(un));
6908                         un->un_state = state_before_pm;
6909                         un->un_power_level = last_power_level;
6910                         cv_broadcast(&un->un_suspend_cv);
6911                         mutex_exit(SD_MUTEX(un));
6912                         SD_TRACE(SD_LOG_IO_PM, un,
6913                             "sdpower: exit, PM suspend Failed.\n");
6914 
6915                         goto sdpower_failed;
6916                 }
6917         }
6918 
6919         /*
6920          * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open,
6921          * close, or strategy. Dump no long uses this routine, it uses it's
6922          * own code so it can be done in polled mode.
6923          */
6924 
6925         medium_present = TRUE;
6926 
6927         /*
6928          * When powering up, issue a TUR in case the device is at unit
6929          * attention.  Don't do retries. Bypass the PM layer, otherwise
6930          * a deadlock on un_pm_busy_cv will occur.
6931          */
6932         if (SD_PM_IS_IO_CAPABLE(un, level)) {
6933                 sval = sd_send_scsi_TEST_UNIT_READY(ssc,
6934                     SD_DONT_RETRY_TUR | SD_BYPASS_PM);
6935                 if (sval != 0)
6936                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6937         }
6938 
6939         if (un->un_f_power_condition_supported) {
6940                 char *pm_condition_name[] = {"STOPPED", "STANDBY",
6941                     "IDLE", "ACTIVE"};
6942                 SD_TRACE(SD_LOG_IO_PM, un,
6943                     "sdpower: sending \'%s\' power condition",
6944                     pm_condition_name[level]);
6945                 sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
6946                     sd_pl2pc[level], SD_PATH_DIRECT);
6947         } else {
6948                 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n",
6949                     ((level == SD_SPINDLE_ON) ? "START" : "STOP"));
6950                 sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
6951                     ((level == SD_SPINDLE_ON) ? SD_TARGET_START :
6952                     SD_TARGET_STOP), SD_PATH_DIRECT);
6953         }
6954         if (sval != 0) {
6955                 if (sval == EIO)
6956                         sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
6957                 else
6958                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6959         }
6960 
6961         /* Command failed, check for media present. */
6962         if ((sval == ENXIO) && un->un_f_has_removable_media) {
6963                 medium_present = FALSE;
6964         }
6965 
6966         /*
6967          * The conditions of interest here are:
6968          *   if a spindle off with media present fails,
6969          *      then restore the state and return an error.
6970          *   else if a spindle on fails,
6971          *      then return an error (there's no state to restore).
6972          * In all other cases we setup for the new state
6973          * and return success.
6974          */
6975         if (!SD_PM_IS_IO_CAPABLE(un, level)) {
6976                 if ((medium_present == TRUE) && (sval != 0)) {
6977                         /* The stop command from above failed */
6978                         rval = DDI_FAILURE;
6979                         /*
6980                          * The stop command failed, and we have media
6981                          * present. Put the level back by calling the
6982                          * sd_pm_resume() and set the state back to
6983                          * it's previous value.
6984                          */
6985                         (void) sd_pm_state_change(un, last_power_level,
6986                             SD_PM_STATE_ROLLBACK);
6987                         mutex_enter(SD_MUTEX(un));
6988                         un->un_last_state = save_state;
6989                         mutex_exit(SD_MUTEX(un));
6990                 } else if (un->un_f_monitor_media_state) {
6991                         /*
6992                          * The stop command from above succeeded.
6993                          * Terminate watch thread in case of removable media
6994                          * devices going into low power state. This is as per
6995                          * the requirements of pm framework, otherwise commands
6996                          * will be generated for the device (through watch
6997                          * thread), even when the device is in low power state.
6998                          */
6999                         mutex_enter(SD_MUTEX(un));
7000                         un->un_f_watcht_stopped = FALSE;
7001                         if (un->un_swr_token != NULL) {
7002                                 opaque_t temp_token = un->un_swr_token;
7003                                 un->un_f_watcht_stopped = TRUE;
7004                                 un->un_swr_token = NULL;
7005                                 mutex_exit(SD_MUTEX(un));
7006                                 (void) scsi_watch_request_terminate(temp_token,
7007                                     SCSI_WATCH_TERMINATE_ALL_WAIT);
7008                         } else {
7009                                 mutex_exit(SD_MUTEX(un));
7010                         }
7011                 }
7012         } else {
7013                 /*
7014                  * The level requested is I/O capable.
7015                  * Legacy behavior: return success on a failed spinup
7016                  * if there is no media in the drive.
7017                  * Do this by looking at medium_present here.
7018                  */
7019                 if ((sval != 0) && medium_present) {
7020                         /* The start command from above failed */
7021                         rval = DDI_FAILURE;
7022                 } else {
7023                         /*
7024                          * The start command from above succeeded
7025                          * PM resume the devices now that we have
7026                          * started the disks
7027                          */
7028                         (void) sd_pm_state_change(un, level,
7029                             SD_PM_STATE_CHANGE);
7030 
7031                         /*
7032                          * Resume the watch thread since it was suspended
7033                          * when the device went into low power mode.
7034                          */
7035                         if (un->un_f_monitor_media_state) {
7036                                 mutex_enter(SD_MUTEX(un));
7037                                 if (un->un_f_watcht_stopped == TRUE) {
7038                                         opaque_t temp_token;
7039 
7040                                         un->un_f_watcht_stopped = FALSE;
7041                                         mutex_exit(SD_MUTEX(un));
7042                                         temp_token =
7043                                             sd_watch_request_submit(un);
7044                                         mutex_enter(SD_MUTEX(un));
7045                                         un->un_swr_token = temp_token;
7046                                 }
7047                                 mutex_exit(SD_MUTEX(un));
7048                         }
7049                 }
7050         }
7051 
7052         if (got_semaphore_here != 0) {
7053                 sema_v(&un->un_semoclose);
7054         }
7055         /*
7056          * On exit put the state back to it's original value
7057          * and broadcast to anyone waiting for the power
7058          * change completion.
7059          */
7060         mutex_enter(SD_MUTEX(un));
7061         un->un_state = state_before_pm;
7062         cv_broadcast(&un->un_suspend_cv);
7063         mutex_exit(SD_MUTEX(un));
7064 
7065         SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval);
7066 
7067         sd_ssc_fini(ssc);
7068         return (rval);
7069 
7070 sdpower_failed:
7071 
7072         sd_ssc_fini(ssc);
7073         return (DDI_FAILURE);
7074 }
7075 
7076 
7077 
7078 /*
7079  *    Function: sdattach
7080  *
7081  * Description: Driver's attach(9e) entry point function.
7082  *
7083  *   Arguments: devi - opaque device info handle
7084  *              cmd  - attach  type
7085  *
7086  * Return Code: DDI_SUCCESS
7087  *              DDI_FAILURE
7088  *
7089  *     Context: Kernel thread context
7090  */
7091 
7092 static int
7093 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
7094 {
7095         switch (cmd) {
7096         case DDI_ATTACH:
7097                 return (sd_unit_attach(devi));
7098         case DDI_RESUME:
7099                 return (sd_ddi_resume(devi));
7100         default:
7101                 break;
7102         }
7103         return (DDI_FAILURE);
7104 }
7105 
7106 
7107 /*
7108  *    Function: sddetach
7109  *
7110  * Description: Driver's detach(9E) entry point function.
7111  *
7112  *   Arguments: devi - opaque device info handle
7113  *              cmd  - detach  type
7114  *
7115  * Return Code: DDI_SUCCESS
7116  *              DDI_FAILURE
7117  *
7118  *     Context: Kernel thread context
7119  */
7120 
7121 static int
7122 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
7123 {
7124         switch (cmd) {
7125         case DDI_DETACH:
7126                 return (sd_unit_detach(devi));
7127         case DDI_SUSPEND:
7128                 return (sd_ddi_suspend(devi));
7129         default:
7130                 break;
7131         }
7132         return (DDI_FAILURE);
7133 }
7134 
7135 
7136 /*
7137  *     Function: sd_sync_with_callback
7138  *
7139  *  Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft
7140  *               state while the callback routine is active.
7141  *
7142  *    Arguments: un: softstate structure for the instance
7143  *
7144  *      Context: Kernel thread context
7145  */
7146 
7147 static void
7148 sd_sync_with_callback(struct sd_lun *un)
7149 {
7150         ASSERT(un != NULL);
7151 
7152         mutex_enter(SD_MUTEX(un));
7153 
7154         ASSERT(un->un_in_callback >= 0);
7155 
7156         while (un->un_in_callback > 0) {
7157                 mutex_exit(SD_MUTEX(un));
7158                 delay(2);
7159                 mutex_enter(SD_MUTEX(un));
7160         }
7161 
7162         mutex_exit(SD_MUTEX(un));
7163 }
7164 
7165 /*
7166  *    Function: sd_unit_attach
7167  *
7168  * Description: Performs DDI_ATTACH processing for sdattach(). Allocates
7169  *              the soft state structure for the device and performs
7170  *              all necessary structure and device initializations.
7171  *
7172  *   Arguments: devi: the system's dev_info_t for the device.
7173  *
7174  * Return Code: DDI_SUCCESS if attach is successful.
7175  *              DDI_FAILURE if any part of the attach fails.
7176  *
7177  *     Context: Called at attach(9e) time for the DDI_ATTACH flag.
7178  *              Kernel thread context only.  Can sleep.
7179  */
7180 
7181 static int
7182 sd_unit_attach(dev_info_t *devi)
7183 {
7184         struct  scsi_device     *devp;
7185         struct  sd_lun          *un;
7186         char                    *variantp;
7187         char                    name_str[48];
7188         int     reservation_flag = SD_TARGET_IS_UNRESERVED;
7189         int     instance;
7190         int     rval;
7191         int     wc_enabled;
7192         int     wc_changeable;
7193         int     tgt;
7194         uint64_t        capacity;
7195         uint_t          lbasize = 0;
7196         dev_info_t      *pdip = ddi_get_parent(devi);
7197         int             offbyone = 0;
7198         int             geom_label_valid = 0;
7199         sd_ssc_t        *ssc;
7200         int             status;
7201         struct sd_fm_internal   *sfip = NULL;
7202         int             max_xfer_size;
7203 
7204         /*
7205          * Retrieve the target driver's private data area. This was set
7206          * up by the HBA.
7207          */
7208         devp = ddi_get_driver_private(devi);
7209 
7210         /*
7211          * Retrieve the target ID of the device.
7212          */
7213         tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7214             SCSI_ADDR_PROP_TARGET, -1);
7215 
7216         /*
7217          * Since we have no idea what state things were left in by the last
7218          * user of the device, set up some 'default' settings, ie. turn 'em
7219          * off. The scsi_ifsetcap calls force re-negotiations with the drive.
7220          * Do this before the scsi_probe, which sends an inquiry.
7221          * This is a fix for bug (4430280).
7222          * Of special importance is wide-xfer. The drive could have been left
7223          * in wide transfer mode by the last driver to communicate with it,
7224          * this includes us. If that's the case, and if the following is not
7225          * setup properly or we don't re-negotiate with the drive prior to
7226          * transferring data to/from the drive, it causes bus parity errors,
7227          * data overruns, and unexpected interrupts. This first occurred when
7228          * the fix for bug (4378686) was made.
7229          */
7230         (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1);
7231         (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1);
7232         (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1);
7233 
7234         /*
7235          * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs
7236          * on a target. Setting it per lun instance actually sets the
7237          * capability of this target, which affects those luns already
7238          * attached on the same target. So during attach, we can only disable
7239          * this capability only when no other lun has been attached on this
7240          * target. By doing this, we assume a target has the same tagged-qing
7241          * capability for every lun. The condition can be removed when HBA
7242          * is changed to support per lun based tagged-qing capability.
7243          */
7244         if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
7245                 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1);
7246         }
7247 
7248         /*
7249          * Use scsi_probe() to issue an INQUIRY command to the device.
7250          * This call will allocate and fill in the scsi_inquiry structure
7251          * and point the sd_inq member of the scsi_device structure to it.
7252          * If the attach succeeds, then this memory will not be de-allocated
7253          * (via scsi_unprobe()) until the instance is detached.
7254          */
7255         if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) {
7256                 goto probe_failed;
7257         }
7258 
7259         /*
7260          * Check the device type as specified in the inquiry data and
7261          * claim it if it is of a type that we support.
7262          */
7263         switch (devp->sd_inq->inq_dtype) {
7264         case DTYPE_DIRECT:
7265                 break;
7266         case DTYPE_RODIRECT:
7267                 break;
7268         case DTYPE_OPTICAL:
7269                 break;
7270         case DTYPE_NOTPRESENT:
7271         default:
7272                 /* Unsupported device type; fail the attach. */
7273                 goto probe_failed;
7274         }
7275 
7276         /*
7277          * Allocate the soft state structure for this unit.
7278          *
7279          * We rely upon this memory being set to all zeroes by
7280          * ddi_soft_state_zalloc().  We assume that any member of the
7281          * soft state structure that is not explicitly initialized by
7282          * this routine will have a value of zero.
7283          */
7284         instance = ddi_get_instance(devp->sd_dev);
7285         if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) {
7286                 goto probe_failed;
7287         }
7288 
7289         /*
7290          * Retrieve a pointer to the newly-allocated soft state.
7291          *
7292          * This should NEVER fail if the ddi_soft_state_zalloc() call above
7293          * was successful, unless something has gone horribly wrong and the
7294          * ddi's soft state internals are corrupt (in which case it is
7295          * probably better to halt here than just fail the attach....)
7296          */
7297         if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
7298                 panic("sd_unit_attach: NULL soft state on instance:0x%x",
7299                     instance);
7300                 /*NOTREACHED*/
7301         }
7302 
7303         /*
7304          * Link the back ptr of the driver soft state to the scsi_device
7305          * struct for this lun.
7306          * Save a pointer to the softstate in the driver-private area of
7307          * the scsi_device struct.
7308          * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until
7309          * we first set un->un_sd below.
7310          */
7311         un->un_sd = devp;
7312         devp->sd_private = (opaque_t)un;
7313 
7314         /*
7315          * The following must be after devp is stored in the soft state struct.
7316          */
7317 #ifdef SDDEBUG
7318         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7319             "%s_unit_attach: un:0x%p instance:%d\n",
7320             ddi_driver_name(devi), un, instance);
7321 #endif
7322 
7323         /*
7324          * Set up the device type and node type (for the minor nodes).
7325          * By default we assume that the device can at least support the
7326          * Common Command Set. Call it a CD-ROM if it reports itself
7327          * as a RODIRECT device.
7328          */
7329         switch (devp->sd_inq->inq_dtype) {
7330         case DTYPE_RODIRECT:
7331                 un->un_node_type = DDI_NT_CD_CHAN;
7332                 un->un_ctype  = CTYPE_CDROM;
7333                 break;
7334         case DTYPE_OPTICAL:
7335                 un->un_node_type = DDI_NT_BLOCK_CHAN;
7336                 un->un_ctype  = CTYPE_ROD;
7337                 break;
7338         default:
7339                 un->un_node_type = DDI_NT_BLOCK_CHAN;
7340                 un->un_ctype  = CTYPE_CCS;
7341                 break;
7342         }
7343 
7344         /*
7345          * Try to read the interconnect type from the HBA.
7346          *
7347          * Note: This driver is currently compiled as two binaries, a parallel
7348          * scsi version (sd) and a fibre channel version (ssd). All functional
7349          * differences are determined at compile time. In the future a single
7350          * binary will be provided and the interconnect type will be used to
7351          * differentiate between fibre and parallel scsi behaviors. At that time
7352          * it will be necessary for all fibre channel HBAs to support this
7353          * property.
7354          *
7355          * set un_f_is_fiber to TRUE ( default fiber )
7356          */
7357         un->un_f_is_fibre = TRUE;
7358         switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) {
7359         case INTERCONNECT_SSA:
7360                 un->un_interconnect_type = SD_INTERCONNECT_SSA;
7361                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7362                     "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un);
7363                 break;
7364         case INTERCONNECT_PARALLEL:
7365                 un->un_f_is_fibre = FALSE;
7366                 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7367                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7368                     "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un);
7369                 break;
7370         case INTERCONNECT_SAS:
7371                 un->un_f_is_fibre = FALSE;
7372                 un->un_interconnect_type = SD_INTERCONNECT_SAS;
7373                 un->un_node_type = DDI_NT_BLOCK_SAS;
7374                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7375                     "sd_unit_attach: un:0x%p SD_INTERCONNECT_SAS\n", un);
7376                 break;
7377         case INTERCONNECT_SATA:
7378                 un->un_f_is_fibre = FALSE;
7379                 un->un_interconnect_type = SD_INTERCONNECT_SATA;
7380                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7381                     "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un);
7382                 break;
7383         case INTERCONNECT_FIBRE:
7384                 un->un_interconnect_type = SD_INTERCONNECT_FIBRE;
7385                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7386                     "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un);
7387                 break;
7388         case INTERCONNECT_FABRIC:
7389                 un->un_interconnect_type = SD_INTERCONNECT_FABRIC;
7390                 un->un_node_type = DDI_NT_BLOCK_FABRIC;
7391                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7392                     "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un);
7393                 break;
7394         default:
7395 #ifdef SD_DEFAULT_INTERCONNECT_TYPE
7396                 /*
7397                  * The HBA does not support the "interconnect-type" property
7398                  * (or did not provide a recognized type).
7399                  *
7400                  * Note: This will be obsoleted when a single fibre channel
7401                  * and parallel scsi driver is delivered. In the meantime the
7402                  * interconnect type will be set to the platform default.If that
7403                  * type is not parallel SCSI, it means that we should be
7404                  * assuming "ssd" semantics. However, here this also means that
7405                  * the FC HBA is not supporting the "interconnect-type" property
7406                  * like we expect it to, so log this occurrence.
7407                  */
7408                 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE;
7409                 if (!SD_IS_PARALLEL_SCSI(un)) {
7410                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
7411                             "sd_unit_attach: un:0x%p Assuming "
7412                             "INTERCONNECT_FIBRE\n", un);
7413                 } else {
7414                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
7415                             "sd_unit_attach: un:0x%p Assuming "
7416                             "INTERCONNECT_PARALLEL\n", un);
7417                         un->un_f_is_fibre = FALSE;
7418                 }
7419 #else
7420                 /*
7421                  * Note: This source will be implemented when a single fibre
7422                  * channel and parallel scsi driver is delivered. The default
7423                  * will be to assume that if a device does not support the
7424                  * "interconnect-type" property it is a parallel SCSI HBA and
7425                  * we will set the interconnect type for parallel scsi.
7426                  */
7427                 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7428                 un->un_f_is_fibre = FALSE;
7429 #endif
7430                 break;
7431         }
7432 
7433         if (un->un_f_is_fibre == TRUE) {
7434                 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) ==
7435                     SCSI_VERSION_3) {
7436                         switch (un->un_interconnect_type) {
7437                         case SD_INTERCONNECT_FIBRE:
7438                         case SD_INTERCONNECT_SSA:
7439                                 un->un_node_type = DDI_NT_BLOCK_WWN;
7440                                 break;
7441                         default:
7442                                 break;
7443                         }
7444                 }
7445         }
7446 
7447         /*
7448          * Initialize the Request Sense command for the target
7449          */
7450         if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) {
7451                 goto alloc_rqs_failed;
7452         }
7453 
7454         /*
7455          * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc
7456          * with separate binary for sd and ssd.
7457          *
7458          * x86 has 1 binary, un_retry_count is set base on connection type.
7459          * The hardcoded values will go away when Sparc uses 1 binary
7460          * for sd and ssd.  This hardcoded values need to match
7461          * SD_RETRY_COUNT in sddef.h
7462          * The value used is base on interconnect type.
7463          * fibre = 3, parallel = 5
7464          */
7465 #if defined(__i386) || defined(__amd64)
7466         un->un_retry_count = un->un_f_is_fibre ? 3 : 5;
7467 #else
7468         un->un_retry_count = SD_RETRY_COUNT;
7469 #endif
7470 
7471         /*
7472          * Set the per disk retry count to the default number of retries
7473          * for disks and CDROMs. This value can be overridden by the
7474          * disk property list or an entry in sd.conf.
7475          */
7476         un->un_notready_retry_count =
7477             ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un)
7478             : DISK_NOT_READY_RETRY_COUNT(un);
7479 
7480         /*
7481          * Set the busy retry count to the default value of un_retry_count.
7482          * This can be overridden by entries in sd.conf or the device
7483          * config table.
7484          */
7485         un->un_busy_retry_count = un->un_retry_count;
7486 
7487         /*
7488          * Init the reset threshold for retries.  This number determines
7489          * how many retries must be performed before a reset can be issued
7490          * (for certain error conditions). This can be overridden by entries
7491          * in sd.conf or the device config table.
7492          */
7493         un->un_reset_retry_count = (un->un_retry_count / 2);
7494 
7495         /*
7496          * Set the victim_retry_count to the default un_retry_count
7497          */
7498         un->un_victim_retry_count = (2 * un->un_retry_count);
7499 
7500         /*
7501          * Set the reservation release timeout to the default value of
7502          * 5 seconds. This can be overridden by entries in ssd.conf or the
7503          * device config table.
7504          */
7505         un->un_reserve_release_time = 5;
7506 
7507         /*
7508          * Set up the default maximum transfer size. Note that this may
7509          * get updated later in the attach, when setting up default wide
7510          * operations for disks.
7511          */
7512 #if defined(__i386) || defined(__amd64)
7513         un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE;
7514         un->un_partial_dma_supported = 1;
7515 #else
7516         un->un_max_xfer_size = (uint_t)maxphys;
7517 #endif
7518 
7519         /*
7520          * Get "allow bus device reset" property (defaults to "enabled" if
7521          * the property was not defined). This is to disable bus resets for
7522          * certain kinds of error recovery. Note: In the future when a run-time
7523          * fibre check is available the soft state flag should default to
7524          * enabled.
7525          */
7526         if (un->un_f_is_fibre == TRUE) {
7527                 un->un_f_allow_bus_device_reset = TRUE;
7528         } else {
7529                 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7530                     "allow-bus-device-reset", 1) != 0) {
7531                         un->un_f_allow_bus_device_reset = TRUE;
7532                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
7533                             "sd_unit_attach: un:0x%p Bus device reset "
7534                             "enabled\n", un);
7535                 } else {
7536                         un->un_f_allow_bus_device_reset = FALSE;
7537                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
7538                             "sd_unit_attach: un:0x%p Bus device reset "
7539                             "disabled\n", un);
7540                 }
7541         }
7542 
7543         /*
7544          * Check if this is an ATAPI device. ATAPI devices use Group 1
7545          * Read/Write commands and Group 2 Mode Sense/Select commands.
7546          *
7547          * Note: The "obsolete" way of doing this is to check for the "atapi"
7548          * property. The new "variant" property with a value of "atapi" has been
7549          * introduced so that future 'variants' of standard SCSI behavior (like
7550          * atapi) could be specified by the underlying HBA drivers by supplying
7551          * a new value for the "variant" property, instead of having to define a
7552          * new property.
7553          */
7554         if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) {
7555                 un->un_f_cfg_is_atapi = TRUE;
7556                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7557                     "sd_unit_attach: un:0x%p Atapi device\n", un);
7558         }
7559         if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant",
7560             &variantp) == DDI_PROP_SUCCESS) {
7561                 if (strcmp(variantp, "atapi") == 0) {
7562                         un->un_f_cfg_is_atapi = TRUE;
7563                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
7564                             "sd_unit_attach: un:0x%p Atapi device\n", un);
7565                 }
7566                 ddi_prop_free(variantp);
7567         }
7568 
7569         un->un_cmd_timeout   = SD_IO_TIME;
7570 
7571         un->un_busy_timeout  = SD_BSY_TIMEOUT;
7572 
7573         /* Info on current states, statuses, etc. (Updated frequently) */
7574         un->un_state         = SD_STATE_NORMAL;
7575         un->un_last_state    = SD_STATE_NORMAL;
7576 
7577         /* Control & status info for command throttling */
7578         un->un_throttle              = sd_max_throttle;
7579         un->un_saved_throttle        = sd_max_throttle;
7580         un->un_min_throttle  = sd_min_throttle;
7581 
7582         if (un->un_f_is_fibre == TRUE) {
7583                 un->un_f_use_adaptive_throttle = TRUE;
7584         } else {
7585                 un->un_f_use_adaptive_throttle = FALSE;
7586         }
7587 
7588         /* Removable media support. */
7589         cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL);
7590         un->un_mediastate            = DKIO_NONE;
7591         un->un_specified_mediastate  = DKIO_NONE;
7592 
7593         /* CVs for suspend/resume (PM or DR) */
7594         cv_init(&un->un_suspend_cv,   NULL, CV_DRIVER, NULL);
7595         cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL);
7596 
7597         /* Power management support. */
7598         un->un_power_level = SD_SPINDLE_UNINIT;
7599 
7600         cv_init(&un->un_wcc_cv,   NULL, CV_DRIVER, NULL);
7601         un->un_f_wcc_inprog = 0;
7602 
7603         /*
7604          * The open/close semaphore is used to serialize threads executing
7605          * in the driver's open & close entry point routines for a given
7606          * instance.
7607          */
7608         (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL);
7609 
7610         /*
7611          * The conf file entry and softstate variable is a forceful override,
7612          * meaning a non-zero value must be entered to change the default.
7613          */
7614         un->un_f_disksort_disabled = FALSE;
7615         un->un_f_rmw_type = SD_RMW_TYPE_DEFAULT;
7616         un->un_f_enable_rmw = FALSE;
7617 
7618         /*
7619          * GET EVENT STATUS NOTIFICATION media polling enabled by default, but
7620          * can be overridden via [s]sd-config-list "mmc-gesn-polling" property.
7621          */
7622         un->un_f_mmc_gesn_polling = TRUE;
7623 
7624         /*
7625          * physical sector size defaults to DEV_BSIZE currently. We can
7626          * override this value via the driver configuration file so we must
7627          * set it before calling sd_read_unit_properties().
7628          */
7629         un->un_phy_blocksize = DEV_BSIZE;
7630 
7631         /*
7632          * Retrieve the properties from the static driver table or the driver
7633          * configuration file (.conf) for this unit and update the soft state
7634          * for the device as needed for the indicated properties.
7635          * Note: the property configuration needs to occur here as some of the
7636          * following routines may have dependencies on soft state flags set
7637          * as part of the driver property configuration.
7638          */
7639         sd_read_unit_properties(un);
7640         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7641             "sd_unit_attach: un:0x%p property configuration complete.\n", un);
7642 
7643         /*
7644          * Only if a device has "hotpluggable" property, it is
7645          * treated as hotpluggable device. Otherwise, it is
7646          * regarded as non-hotpluggable one.
7647          */
7648         if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable",
7649             -1) != -1) {
7650                 un->un_f_is_hotpluggable = TRUE;
7651         }
7652 
7653         /*
7654          * set unit's attributes(flags) according to "hotpluggable" and
7655          * RMB bit in INQUIRY data.
7656          */
7657         sd_set_unit_attributes(un, devi);
7658 
7659         /*
7660          * By default, we mark the capacity, lbasize, and geometry
7661          * as invalid. Only if we successfully read a valid capacity
7662          * will we update the un_blockcount and un_tgt_blocksize with the
7663          * valid values (the geometry will be validated later).
7664          */
7665         un->un_f_blockcount_is_valid = FALSE;
7666         un->un_f_tgt_blocksize_is_valid      = FALSE;
7667 
7668         /*
7669          * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine
7670          * otherwise.
7671          */
7672         un->un_tgt_blocksize  = un->un_sys_blocksize  = DEV_BSIZE;
7673         un->un_blockcount = 0;
7674 
7675         /*
7676          * Set up the per-instance info needed to determine the correct
7677          * CDBs and other info for issuing commands to the target.
7678          */
7679         sd_init_cdb_limits(un);
7680 
7681         /*
7682          * Set up the IO chains to use, based upon the target type.
7683          */
7684         if (un->un_f_non_devbsize_supported) {
7685                 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
7686         } else {
7687                 un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
7688         }
7689         un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD;
7690         un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD;
7691         un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD;
7692 
7693         un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf),
7694             sd_xbuf_strategy, un, sd_xbuf_active_limit,  sd_xbuf_reserve_limit,
7695             ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER);
7696         ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi);
7697 
7698 
7699         if (ISCD(un)) {
7700                 un->un_additional_codes = sd_additional_codes;
7701         } else {
7702                 un->un_additional_codes = NULL;
7703         }
7704 
7705         /*
7706          * Create the kstats here so they can be available for attach-time
7707          * routines that send commands to the unit (either polled or via
7708          * sd_send_scsi_cmd).
7709          *
7710          * Note: This is a critical sequence that needs to be maintained:
7711          *      1) Instantiate the kstats here, before any routines using the
7712          *         iopath (i.e. sd_send_scsi_cmd).
7713          *      2) Instantiate and initialize the partition stats
7714          *         (sd_set_pstats).
7715          *      3) Initialize the error stats (sd_set_errstats), following
7716          *         sd_validate_geometry(),sd_register_devid(),
7717          *         and sd_cache_control().
7718          */
7719 
7720         un->un_stats = kstat_create(sd_label, instance,
7721             NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
7722         if (un->un_stats != NULL) {
7723                 un->un_stats->ks_lock = SD_MUTEX(un);
7724                 kstat_install(un->un_stats);
7725         }
7726         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7727             "sd_unit_attach: un:0x%p un_stats created\n", un);
7728 
7729         sd_create_errstats(un, instance);
7730         if (un->un_errstats == NULL) {
7731                 goto create_errstats_failed;
7732         }
7733         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7734             "sd_unit_attach: un:0x%p errstats created\n", un);
7735 
7736         /*
7737          * The following if/else code was relocated here from below as part
7738          * of the fix for bug (4430280). However with the default setup added
7739          * on entry to this routine, it's no longer absolutely necessary for
7740          * this to be before the call to sd_spin_up_unit.
7741          */
7742         if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) {
7743                 int tq_trigger_flag = (((devp->sd_inq->inq_ansi == 4) ||
7744                     (devp->sd_inq->inq_ansi == 5)) &&
7745                     devp->sd_inq->inq_bque) || devp->sd_inq->inq_cmdque;
7746 
7747                 /*
7748                  * If tagged queueing is supported by the target
7749                  * and by the host adapter then we will enable it
7750                  */
7751                 un->un_tagflags = 0;
7752                 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && tq_trigger_flag &&
7753                     (un->un_f_arq_enabled == TRUE)) {
7754                         if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing",
7755                             1, 1) == 1) {
7756                                 un->un_tagflags = FLAG_STAG;
7757                                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7758                                     "sd_unit_attach: un:0x%p tag queueing "
7759                                     "enabled\n", un);
7760                         } else if (scsi_ifgetcap(SD_ADDRESS(un),
7761                             "untagged-qing", 0) == 1) {
7762                                 un->un_f_opt_queueing = TRUE;
7763                                 un->un_saved_throttle = un->un_throttle =
7764                                     min(un->un_throttle, 3);
7765                         } else {
7766                                 un->un_f_opt_queueing = FALSE;
7767                                 un->un_saved_throttle = un->un_throttle = 1;
7768                         }
7769                 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0)
7770                     == 1) && (un->un_f_arq_enabled == TRUE)) {
7771                         /* The Host Adapter supports internal queueing. */
7772                         un->un_f_opt_queueing = TRUE;
7773                         un->un_saved_throttle = un->un_throttle =
7774                             min(un->un_throttle, 3);
7775                 } else {
7776                         un->un_f_opt_queueing = FALSE;
7777                         un->un_saved_throttle = un->un_throttle = 1;
7778                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
7779                             "sd_unit_attach: un:0x%p no tag queueing\n", un);
7780                 }
7781 
7782                 /*
7783                  * Enable large transfers for SATA/SAS drives
7784                  */
7785                 if (SD_IS_SERIAL(un)) {
7786                         un->un_max_xfer_size =
7787                             ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7788                             sd_max_xfer_size, SD_MAX_XFER_SIZE);
7789                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
7790                             "sd_unit_attach: un:0x%p max transfer "
7791                             "size=0x%x\n", un, un->un_max_xfer_size);
7792 
7793                 }
7794 
7795                 /* Setup or tear down default wide operations for disks */
7796 
7797                 /*
7798                  * Note: Legacy: it may be possible for both "sd_max_xfer_size"
7799                  * and "ssd_max_xfer_size" to exist simultaneously on the same
7800                  * system and be set to different values. In the future this
7801                  * code may need to be updated when the ssd module is
7802                  * obsoleted and removed from the system. (4299588)
7803                  */
7804                 if (SD_IS_PARALLEL_SCSI(un) &&
7805                     (devp->sd_inq->inq_rdf == RDF_SCSI2) &&
7806                     (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) {
7807                         if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7808                             1, 1) == 1) {
7809                                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7810                                     "sd_unit_attach: un:0x%p Wide Transfer "
7811                                     "enabled\n", un);
7812                         }
7813 
7814                         /*
7815                          * If tagged queuing has also been enabled, then
7816                          * enable large xfers
7817                          */
7818                         if (un->un_saved_throttle == sd_max_throttle) {
7819                                 un->un_max_xfer_size =
7820                                     ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7821                                     sd_max_xfer_size, SD_MAX_XFER_SIZE);
7822                                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7823                                     "sd_unit_attach: un:0x%p max transfer "
7824                                     "size=0x%x\n", un, un->un_max_xfer_size);
7825                         }
7826                 } else {
7827                         if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7828                             0, 1) == 1) {
7829                                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7830                                     "sd_unit_attach: un:0x%p "
7831                                     "Wide Transfer disabled\n", un);
7832                         }
7833                 }
7834         } else {
7835                 un->un_tagflags = FLAG_STAG;
7836                 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY,
7837                     devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE);
7838         }
7839 
7840         /*
7841          * If this target supports LUN reset, try to enable it.
7842          */
7843         if (un->un_f_lun_reset_enabled) {
7844                 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) {
7845                         SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7846                             "un:0x%p lun_reset capability set\n", un);
7847                 } else {
7848                         SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7849                             "un:0x%p lun-reset capability not set\n", un);
7850                 }
7851         }
7852 
7853         /*
7854          * Adjust the maximum transfer size. This is to fix
7855          * the problem of partial DMA support on SPARC. Some
7856          * HBA driver, like aac, has very small dma_attr_maxxfer
7857          * size, which requires partial DMA support on SPARC.
7858          * In the future the SPARC pci nexus driver may solve
7859          * the problem instead of this fix.
7860          */
7861         max_xfer_size = scsi_ifgetcap(SD_ADDRESS(un), "dma-max", 1);
7862         if ((max_xfer_size > 0) && (max_xfer_size < un->un_max_xfer_size)) {
7863                 /* We need DMA partial even on sparc to ensure sddump() works */
7864                 un->un_max_xfer_size = max_xfer_size;
7865                 if (un->un_partial_dma_supported == 0)
7866                         un->un_partial_dma_supported = 1;
7867         }
7868         if (ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7869             DDI_PROP_DONTPASS, "buf_break", 0) == 1) {
7870                 if (ddi_xbuf_attr_setup_brk(un->un_xbuf_attr,
7871                     un->un_max_xfer_size) == 1) {
7872                         un->un_buf_breakup_supported = 1;
7873                         SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7874                             "un:0x%p Buf breakup enabled\n", un);
7875                 }
7876         }
7877 
7878         /*
7879          * Set PKT_DMA_PARTIAL flag.
7880          */
7881         if (un->un_partial_dma_supported == 1) {
7882                 un->un_pkt_flags = PKT_DMA_PARTIAL;
7883         } else {
7884                 un->un_pkt_flags = 0;
7885         }
7886 
7887         /* Initialize sd_ssc_t for internal uscsi commands */
7888         ssc = sd_ssc_init(un);
7889         scsi_fm_init(devp);
7890 
7891         /*
7892          * Allocate memory for SCSI FMA stuffs.
7893          */
7894         un->un_fm_private =
7895             kmem_zalloc(sizeof (struct sd_fm_internal), KM_SLEEP);
7896         sfip = (struct sd_fm_internal *)un->un_fm_private;
7897         sfip->fm_ssc.ssc_uscsi_cmd = &sfip->fm_ucmd;
7898         sfip->fm_ssc.ssc_uscsi_info = &sfip->fm_uinfo;
7899         sfip->fm_ssc.ssc_un = un;
7900 
7901         if (ISCD(un) ||
7902             un->un_f_has_removable_media ||
7903             devp->sd_fm_capable == DDI_FM_NOT_CAPABLE) {
7904                 /*
7905                  * We don't touch CDROM or the DDI_FM_NOT_CAPABLE device.
7906                  * Their log are unchanged.
7907                  */
7908                 sfip->fm_log_level = SD_FM_LOG_NSUP;
7909         } else {
7910                 /*
7911                  * If enter here, it should be non-CDROM and FM-capable
7912                  * device, and it will not keep the old scsi_log as before
7913                  * in /var/adm/messages. However, the property
7914                  * "fm-scsi-log" will control whether the FM telemetry will
7915                  * be logged in /var/adm/messages.
7916                  */
7917                 int fm_scsi_log;
7918                 fm_scsi_log = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7919                     DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "fm-scsi-log", 0);
7920 
7921                 if (fm_scsi_log)
7922                         sfip->fm_log_level = SD_FM_LOG_EREPORT;
7923                 else
7924                         sfip->fm_log_level = SD_FM_LOG_SILENT;
7925         }
7926 
7927         /*
7928          * At this point in the attach, we have enough info in the
7929          * soft state to be able to issue commands to the target.
7930          *
7931          * All command paths used below MUST issue their commands as
7932          * SD_PATH_DIRECT. This is important as intermediate layers
7933          * are not all initialized yet (such as PM).
7934          */
7935 
7936         /*
7937          * Send a TEST UNIT READY command to the device. This should clear
7938          * any outstanding UNIT ATTENTION that may be present.
7939          *
7940          * Note: Don't check for success, just track if there is a reservation,
7941          * this is a throw away command to clear any unit attentions.
7942          *
7943          * Note: This MUST be the first command issued to the target during
7944          * attach to ensure power on UNIT ATTENTIONS are cleared.
7945          * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated
7946          * with attempts at spinning up a device with no media.
7947          */
7948         status = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
7949         if (status != 0) {
7950                 if (status == EACCES)
7951                         reservation_flag = SD_TARGET_IS_RESERVED;
7952                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
7953         }
7954 
7955         /*
7956          * If the device is NOT a removable media device, attempt to spin
7957          * it up (using the START_STOP_UNIT command) and read its capacity
7958          * (using the READ CAPACITY command).  Note, however, that either
7959          * of these could fail and in some cases we would continue with
7960          * the attach despite the failure (see below).
7961          */
7962         if (un->un_f_descr_format_supported) {
7963 
7964                 switch (sd_spin_up_unit(ssc)) {
7965                 case 0:
7966                         /*
7967                          * Spin-up was successful; now try to read the
7968                          * capacity.  If successful then save the results
7969                          * and mark the capacity & lbasize as valid.
7970                          */
7971                         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7972                             "sd_unit_attach: un:0x%p spin-up successful\n", un);
7973 
7974                         status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
7975                             &lbasize, SD_PATH_DIRECT);
7976 
7977                         switch (status) {
7978                         case 0: {
7979                                 if (capacity > DK_MAX_BLOCKS) {
7980 #ifdef _LP64
7981                                         if ((capacity + 1) >
7982                                             SD_GROUP1_MAX_ADDRESS) {
7983                                                 /*
7984                                                  * Enable descriptor format
7985                                                  * sense data so that we can
7986                                                  * get 64 bit sense data
7987                                                  * fields.
7988                                                  */
7989                                                 sd_enable_descr_sense(ssc);
7990                                         }
7991 #else
7992                                         /* 32-bit kernels can't handle this */
7993                                         scsi_log(SD_DEVINFO(un),
7994                                             sd_label, CE_WARN,
7995                                             "disk has %llu blocks, which "
7996                                             "is too large for a 32-bit "
7997                                             "kernel", capacity);
7998 
7999 #if defined(__i386) || defined(__amd64)
8000                                         /*
8001                                          * 1TB disk was treated as (1T - 512)B
8002                                          * in the past, so that it might have
8003                                          * valid VTOC and solaris partitions,
8004                                          * we have to allow it to continue to
8005                                          * work.
8006                                          */
8007                                         if (capacity -1 > DK_MAX_BLOCKS)
8008 #endif
8009                                         goto spinup_failed;
8010 #endif
8011                                 }
8012 
8013                                 /*
8014                                  * Here it's not necessary to check the case:
8015                                  * the capacity of the device is bigger than
8016                                  * what the max hba cdb can support. Because
8017                                  * sd_send_scsi_READ_CAPACITY will retrieve
8018                                  * the capacity by sending USCSI command, which
8019                                  * is constrained by the max hba cdb. Actually,
8020                                  * sd_send_scsi_READ_CAPACITY will return
8021                                  * EINVAL when using bigger cdb than required
8022                                  * cdb length. Will handle this case in
8023                                  * "case EINVAL".
8024                                  */
8025 
8026                                 /*
8027                                  * The following relies on
8028                                  * sd_send_scsi_READ_CAPACITY never
8029                                  * returning 0 for capacity and/or lbasize.
8030                                  */
8031                                 sd_update_block_info(un, lbasize, capacity);
8032 
8033                                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8034                                     "sd_unit_attach: un:0x%p capacity = %ld "
8035                                     "blocks; lbasize= %ld.\n", un,
8036                                     un->un_blockcount, un->un_tgt_blocksize);
8037 
8038                                 break;
8039                         }
8040                         case EINVAL:
8041                                 /*
8042                                  * In the case where the max-cdb-length property
8043                                  * is smaller than the required CDB length for
8044                                  * a SCSI device, a target driver can fail to
8045                                  * attach to that device.
8046                                  */
8047                                 scsi_log(SD_DEVINFO(un),
8048                                     sd_label, CE_WARN,
8049                                     "disk capacity is too large "
8050                                     "for current cdb length");
8051                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8052 
8053                                 goto spinup_failed;
8054                         case EACCES:
8055                                 /*
8056                                  * Should never get here if the spin-up
8057                                  * succeeded, but code it in anyway.
8058                                  * From here, just continue with the attach...
8059                                  */
8060                                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8061                                     "sd_unit_attach: un:0x%p "
8062                                     "sd_send_scsi_READ_CAPACITY "
8063                                     "returned reservation conflict\n", un);
8064                                 reservation_flag = SD_TARGET_IS_RESERVED;
8065                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8066                                 break;
8067                         default:
8068                                 /*
8069                                  * Likewise, should never get here if the
8070                                  * spin-up succeeded. Just continue with
8071                                  * the attach...
8072                                  */
8073                                 if (status == EIO)
8074                                         sd_ssc_assessment(ssc,
8075                                             SD_FMT_STATUS_CHECK);
8076                                 else
8077                                         sd_ssc_assessment(ssc,
8078                                             SD_FMT_IGNORE);
8079                                 break;
8080                         }
8081                         break;
8082                 case EACCES:
8083                         /*
8084                          * Device is reserved by another host.  In this case
8085                          * we could not spin it up or read the capacity, but
8086                          * we continue with the attach anyway.
8087                          */
8088                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
8089                             "sd_unit_attach: un:0x%p spin-up reservation "
8090                             "conflict.\n", un);
8091                         reservation_flag = SD_TARGET_IS_RESERVED;
8092                         break;
8093                 default:
8094                         /* Fail the attach if the spin-up failed. */
8095                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
8096                             "sd_unit_attach: un:0x%p spin-up failed.", un);
8097                         goto spinup_failed;
8098                 }
8099 
8100         }
8101 
8102         /*
8103          * Check to see if this is a MMC drive
8104          */
8105         if (ISCD(un)) {
8106                 sd_set_mmc_caps(ssc);
8107         }
8108 
8109         /*
8110          * Add a zero-length attribute to tell the world we support
8111          * kernel ioctls (for layered drivers)
8112          */
8113         (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8114             DDI_KERNEL_IOCTL, NULL, 0);
8115 
8116         /*
8117          * Add a boolean property to tell the world we support
8118          * the B_FAILFAST flag (for layered drivers)
8119          */
8120         (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8121             "ddi-failfast-supported", NULL, 0);
8122 
8123         /*
8124          * Initialize power management
8125          */
8126         mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL);
8127         cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL);
8128         sd_setup_pm(ssc, devi);
8129         if (un->un_f_pm_is_enabled == FALSE) {
8130                 /*
8131                  * For performance, point to a jump table that does
8132                  * not include pm.
8133                  * The direct and priority chains don't change with PM.
8134                  *
8135                  * Note: this is currently done based on individual device
8136                  * capabilities. When an interface for determining system
8137                  * power enabled state becomes available, or when additional
8138                  * layers are added to the command chain, these values will
8139                  * have to be re-evaluated for correctness.
8140                  */
8141                 if (un->un_f_non_devbsize_supported) {
8142                         un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM;
8143                 } else {
8144                         un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM;
8145                 }
8146                 un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
8147         }
8148 
8149         /*
8150          * This property is set to 0 by HA software to avoid retries
8151          * on a reserved disk. (The preferred property name is
8152          * "retry-on-reservation-conflict") (1189689)
8153          *
8154          * Note: The use of a global here can have unintended consequences. A
8155          * per instance variable is preferable to match the capabilities of
8156          * different underlying hba's (4402600)
8157          */
8158         sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi,
8159             DDI_PROP_DONTPASS, "retry-on-reservation-conflict",
8160             sd_retry_on_reservation_conflict);
8161         if (sd_retry_on_reservation_conflict != 0) {
8162                 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY,
8163                     devi, DDI_PROP_DONTPASS, sd_resv_conflict_name,
8164                     sd_retry_on_reservation_conflict);
8165         }
8166 
8167         /* Set up options for QFULL handling. */
8168         if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8169             "qfull-retries", -1)) != -1) {
8170                 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries",
8171                     rval, 1);
8172         }
8173         if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8174             "qfull-retry-interval", -1)) != -1) {
8175                 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval",
8176                     rval, 1);
8177         }
8178 
8179         /*
8180          * This just prints a message that announces the existence of the
8181          * device. The message is always printed in the system logfile, but
8182          * only appears on the console if the system is booted with the
8183          * -v (verbose) argument.
8184          */
8185         ddi_report_dev(devi);
8186 
8187         un->un_mediastate = DKIO_NONE;
8188 
8189         /*
8190          * Check Block Device Characteristics VPD.
8191          */
8192         sd_check_bdc_vpd(ssc);
8193 
8194         /*
8195          * Check whether the drive is in emulation mode.
8196          */
8197         sd_check_emulation_mode(ssc);
8198 
8199         cmlb_alloc_handle(&un->un_cmlbhandle);
8200 
8201 #if defined(__i386) || defined(__amd64)
8202         /*
8203          * On x86, compensate for off-by-1 legacy error
8204          */
8205         if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable &&
8206             (lbasize == un->un_sys_blocksize))
8207                 offbyone = CMLB_OFF_BY_ONE;
8208 #endif
8209 
8210         if (cmlb_attach(devi, &sd_tgops, (int)devp->sd_inq->inq_dtype,
8211             VOID2BOOLEAN(un->un_f_has_removable_media != 0),
8212             VOID2BOOLEAN(un->un_f_is_hotpluggable != 0),
8213             un->un_node_type, offbyone, un->un_cmlbhandle,
8214             (void *)SD_PATH_DIRECT) != 0) {
8215                 goto cmlb_attach_failed;
8216         }
8217 
8218 
8219         /*
8220          * Read and validate the device's geometry (ie, disk label)
8221          * A new unformatted drive will not have a valid geometry, but
8222          * the driver needs to successfully attach to this device so
8223          * the drive can be formatted via ioctls.
8224          */
8225         geom_label_valid = (cmlb_validate(un->un_cmlbhandle, 0,
8226             (void *)SD_PATH_DIRECT) == 0) ? 1: 0;
8227 
8228         mutex_enter(SD_MUTEX(un));
8229 
8230         /*
8231          * Read and initialize the devid for the unit.
8232          */
8233         if (un->un_f_devid_supported) {
8234                 sd_register_devid(ssc, devi, reservation_flag);
8235         }
8236         mutex_exit(SD_MUTEX(un));
8237 
8238 #if (defined(__fibre))
8239         /*
8240          * Register callbacks for fibre only.  You can't do this solely
8241          * on the basis of the devid_type because this is hba specific.
8242          * We need to query our hba capabilities to find out whether to
8243          * register or not.
8244          */
8245         if (un->un_f_is_fibre) {
8246                 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
8247                         sd_init_event_callbacks(un);
8248                         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8249                             "sd_unit_attach: un:0x%p event callbacks inserted",
8250                             un);
8251                 }
8252         }
8253 #endif
8254 
8255         if (un->un_f_opt_disable_cache == TRUE) {
8256                 /*
8257                  * Disable both read cache and write cache.  This is
8258                  * the historic behavior of the keywords in the config file.
8259                  */
8260                 if (sd_cache_control(ssc, SD_CACHE_DISABLE, SD_CACHE_DISABLE) !=
8261                     0) {
8262                         SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8263                             "sd_unit_attach: un:0x%p Could not disable "
8264                             "caching", un);
8265                         goto devid_failed;
8266                 }
8267         }
8268 
8269         /*
8270          * Check the value of the WCE bit and if it's allowed to be changed,
8271          * set un_f_write_cache_enabled and un_f_cache_mode_changeable
8272          * accordingly.
8273          */
8274         (void) sd_get_write_cache_enabled(ssc, &wc_enabled);
8275         sd_get_write_cache_changeable(ssc, &wc_changeable);
8276         mutex_enter(SD_MUTEX(un));
8277         un->un_f_write_cache_enabled = (wc_enabled != 0);
8278         un->un_f_cache_mode_changeable = (wc_changeable != 0);
8279         mutex_exit(SD_MUTEX(un));
8280 
8281         if ((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR &&
8282             un->un_tgt_blocksize != DEV_BSIZE) ||
8283             un->un_f_enable_rmw) {
8284                 if (!(un->un_wm_cache)) {
8285                         (void) snprintf(name_str, sizeof (name_str),
8286                             "%s%d_cache",
8287                             ddi_driver_name(SD_DEVINFO(un)),
8288                             ddi_get_instance(SD_DEVINFO(un)));
8289                         un->un_wm_cache = kmem_cache_create(
8290                             name_str, sizeof (struct sd_w_map),
8291                             8, sd_wm_cache_constructor,
8292                             sd_wm_cache_destructor, NULL,
8293                             (void *)un, NULL, 0);
8294                         if (!(un->un_wm_cache)) {
8295                                 goto wm_cache_failed;
8296                         }
8297                 }
8298         }
8299 
8300         /*
8301          * Check the value of the NV_SUP bit and set
8302          * un_f_suppress_cache_flush accordingly.
8303          */
8304         sd_get_nv_sup(ssc);
8305 
8306         /*
8307          * Find out what type of reservation this disk supports.
8308          */
8309         status = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS, 0, NULL);
8310 
8311         switch (status) {
8312         case 0:
8313                 /*
8314                  * SCSI-3 reservations are supported.
8315                  */
8316                 un->un_reservation_type = SD_SCSI3_RESERVATION;
8317                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8318                     "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un);
8319                 break;
8320         case ENOTSUP:
8321                 /*
8322                  * The PERSISTENT RESERVE IN command would not be recognized by
8323                  * a SCSI-2 device, so assume the reservation type is SCSI-2.
8324                  */
8325                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8326                     "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un);
8327                 un->un_reservation_type = SD_SCSI2_RESERVATION;
8328 
8329                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8330                 break;
8331         default:
8332                 /*
8333                  * default to SCSI-3 reservations
8334                  */
8335                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8336                     "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un);
8337                 un->un_reservation_type = SD_SCSI3_RESERVATION;
8338 
8339                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8340                 break;
8341         }
8342 
8343         /*
8344          * Set the pstat and error stat values here, so data obtained during the
8345          * previous attach-time routines is available.
8346          *
8347          * Note: This is a critical sequence that needs to be maintained:
8348          *      1) Instantiate the kstats before any routines using the iopath
8349          *         (i.e. sd_send_scsi_cmd).
8350          *      2) Initialize the error stats (sd_set_errstats) and partition
8351          *         stats (sd_set_pstats)here, following
8352          *         cmlb_validate_geometry(), sd_register_devid(), and
8353          *         sd_cache_control().
8354          */
8355 
8356         if (un->un_f_pkstats_enabled && geom_label_valid) {
8357                 sd_set_pstats(un);
8358                 SD_TRACE(SD_LOG_IO_PARTITION, un,
8359                     "sd_unit_attach: un:0x%p pstats created and set\n", un);
8360         }
8361 
8362         sd_set_errstats(un);
8363         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8364             "sd_unit_attach: un:0x%p errstats set\n", un);
8365 
8366 
8367         /*
8368          * After successfully attaching an instance, we record the information
8369          * of how many luns have been attached on the relative target and
8370          * controller for parallel SCSI. This information is used when sd tries
8371          * to set the tagged queuing capability in HBA.
8372          */
8373         if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) {
8374                 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH);
8375         }
8376 
8377         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8378             "sd_unit_attach: un:0x%p exit success\n", un);
8379 
8380         /* Uninitialize sd_ssc_t pointer */
8381         sd_ssc_fini(ssc);
8382 
8383         return (DDI_SUCCESS);
8384 
8385         /*
8386          * An error occurred during the attach; clean up & return failure.
8387          */
8388 wm_cache_failed:
8389 devid_failed:
8390         ddi_remove_minor_node(devi, NULL);
8391 
8392 cmlb_attach_failed:
8393         /*
8394          * Cleanup from the scsi_ifsetcap() calls (437868)
8395          */
8396         (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8397         (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8398 
8399         /*
8400          * Refer to the comments of setting tagged-qing in the beginning of
8401          * sd_unit_attach. We can only disable tagged queuing when there is
8402          * no lun attached on the target.
8403          */
8404         if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
8405                 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8406         }
8407 
8408         if (un->un_f_is_fibre == FALSE) {
8409                 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8410         }
8411 
8412 spinup_failed:
8413 
8414         /* Uninitialize sd_ssc_t pointer */
8415         sd_ssc_fini(ssc);
8416 
8417         mutex_enter(SD_MUTEX(un));
8418 
8419         /* Deallocate SCSI FMA memory spaces */
8420         kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
8421 
8422         /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */
8423         if (un->un_direct_priority_timeid != NULL) {
8424                 timeout_id_t temp_id = un->un_direct_priority_timeid;
8425                 un->un_direct_priority_timeid = NULL;
8426                 mutex_exit(SD_MUTEX(un));
8427                 (void) untimeout(temp_id);
8428                 mutex_enter(SD_MUTEX(un));
8429         }
8430 
8431         /* Cancel any pending start/stop timeouts */
8432         if (un->un_startstop_timeid != NULL) {
8433                 timeout_id_t temp_id = un->un_startstop_timeid;
8434                 un->un_startstop_timeid = NULL;
8435                 mutex_exit(SD_MUTEX(un));
8436                 (void) untimeout(temp_id);
8437                 mutex_enter(SD_MUTEX(un));
8438         }
8439 
8440         /* Cancel any pending reset-throttle timeouts */
8441         if (un->un_reset_throttle_timeid != NULL) {
8442                 timeout_id_t temp_id = un->un_reset_throttle_timeid;
8443                 un->un_reset_throttle_timeid = NULL;
8444                 mutex_exit(SD_MUTEX(un));
8445                 (void) untimeout(temp_id);
8446                 mutex_enter(SD_MUTEX(un));
8447         }
8448 
8449         /* Cancel rmw warning message timeouts */
8450         if (un->un_rmw_msg_timeid != NULL) {
8451                 timeout_id_t temp_id = un->un_rmw_msg_timeid;
8452                 un->un_rmw_msg_timeid = NULL;
8453                 mutex_exit(SD_MUTEX(un));
8454                 (void) untimeout(temp_id);
8455                 mutex_enter(SD_MUTEX(un));
8456         }
8457 
8458         /* Cancel any pending retry timeouts */
8459         if (un->un_retry_timeid != NULL) {
8460                 timeout_id_t temp_id = un->un_retry_timeid;
8461                 un->un_retry_timeid = NULL;
8462                 mutex_exit(SD_MUTEX(un));
8463                 (void) untimeout(temp_id);
8464                 mutex_enter(SD_MUTEX(un));
8465         }
8466 
8467         /* Cancel any pending delayed cv broadcast timeouts */
8468         if (un->un_dcvb_timeid != NULL) {
8469                 timeout_id_t temp_id = un->un_dcvb_timeid;
8470                 un->un_dcvb_timeid = NULL;
8471                 mutex_exit(SD_MUTEX(un));
8472                 (void) untimeout(temp_id);
8473                 mutex_enter(SD_MUTEX(un));
8474         }
8475 
8476         mutex_exit(SD_MUTEX(un));
8477 
8478         /* There should not be any in-progress I/O so ASSERT this check */
8479         ASSERT(un->un_ncmds_in_transport == 0);
8480         ASSERT(un->un_ncmds_in_driver == 0);
8481 
8482         /* Do not free the softstate if the callback routine is active */
8483         sd_sync_with_callback(un);
8484 
8485         /*
8486          * Partition stats apparently are not used with removables. These would
8487          * not have been created during attach, so no need to clean them up...
8488          */
8489         if (un->un_errstats != NULL) {
8490                 kstat_delete(un->un_errstats);
8491                 un->un_errstats = NULL;
8492         }
8493 
8494 create_errstats_failed:
8495 
8496         if (un->un_stats != NULL) {
8497                 kstat_delete(un->un_stats);
8498                 un->un_stats = NULL;
8499         }
8500 
8501         ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8502         ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8503 
8504         ddi_prop_remove_all(devi);
8505         sema_destroy(&un->un_semoclose);
8506         cv_destroy(&un->un_state_cv);
8507 
8508         sd_free_rqs(un);
8509 
8510 alloc_rqs_failed:
8511 
8512         devp->sd_private = NULL;
8513         bzero(un, sizeof (struct sd_lun));      /* Clear any stale data! */
8514 
8515         /*
8516          * Note: the man pages are unclear as to whether or not doing a
8517          * ddi_soft_state_free(sd_state, instance) is the right way to
8518          * clean up after the ddi_soft_state_zalloc() if the subsequent
8519          * ddi_get_soft_state() fails.  The implication seems to be
8520          * that the get_soft_state cannot fail if the zalloc succeeds.
8521          */
8522 #ifndef XPV_HVM_DRIVER
8523         ddi_soft_state_free(sd_state, instance);
8524 #endif /* !XPV_HVM_DRIVER */
8525 
8526 probe_failed:
8527         scsi_unprobe(devp);
8528 
8529         return (DDI_FAILURE);
8530 }
8531 
8532 
8533 /*
8534  *    Function: sd_unit_detach
8535  *
8536  * Description: Performs DDI_DETACH processing for sddetach().
8537  *
8538  * Return Code: DDI_SUCCESS
8539  *              DDI_FAILURE
8540  *
8541  *     Context: Kernel thread context
8542  */
8543 
8544 static int
8545 sd_unit_detach(dev_info_t *devi)
8546 {
8547         struct scsi_device      *devp;
8548         struct sd_lun           *un;
8549         int                     i;
8550         int                     tgt;
8551         dev_t                   dev;
8552         dev_info_t              *pdip = ddi_get_parent(devi);
8553         int                     instance = ddi_get_instance(devi);
8554 
8555         mutex_enter(&sd_detach_mutex);
8556 
8557         /*
8558          * Fail the detach for any of the following:
8559          *  - Unable to get the sd_lun struct for the instance
8560          *  - A layered driver has an outstanding open on the instance
8561          *  - Another thread is already detaching this instance
8562          *  - Another thread is currently performing an open
8563          */
8564         devp = ddi_get_driver_private(devi);
8565         if ((devp == NULL) ||
8566             ((un = (struct sd_lun *)devp->sd_private) == NULL) ||
8567             (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) ||
8568             (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) {
8569                 mutex_exit(&sd_detach_mutex);
8570                 return (DDI_FAILURE);
8571         }
8572 
8573         SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un);
8574 
8575         /*
8576          * Mark this instance as currently in a detach, to inhibit any
8577          * opens from a layered driver.
8578          */
8579         un->un_detach_count++;
8580         mutex_exit(&sd_detach_mutex);
8581 
8582         tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
8583             SCSI_ADDR_PROP_TARGET, -1);
8584 
8585         dev = sd_make_device(SD_DEVINFO(un));
8586 
8587 #ifndef lint
8588         _NOTE(COMPETING_THREADS_NOW);
8589 #endif
8590 
8591         mutex_enter(SD_MUTEX(un));
8592 
8593         /*
8594          * Fail the detach if there are any outstanding layered
8595          * opens on this device.
8596          */
8597         for (i = 0; i < NDKMAP; i++) {
8598                 if (un->un_ocmap.lyropen[i] != 0) {
8599                         goto err_notclosed;
8600                 }
8601         }
8602 
8603         /*
8604          * Verify there are NO outstanding commands issued to this device.
8605          * ie, un_ncmds_in_transport == 0.
8606          * It's possible to have outstanding commands through the physio
8607          * code path, even though everything's closed.
8608          */
8609         if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) ||
8610             (un->un_direct_priority_timeid != NULL) ||
8611             (un->un_state == SD_STATE_RWAIT)) {
8612                 mutex_exit(SD_MUTEX(un));
8613                 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8614                     "sd_dr_detach: Detach failure due to outstanding cmds\n");
8615                 goto err_stillbusy;
8616         }
8617 
8618         /*
8619          * If we have the device reserved, release the reservation.
8620          */
8621         if ((un->un_resvd_status & SD_RESERVE) &&
8622             !(un->un_resvd_status & SD_LOST_RESERVE)) {
8623                 mutex_exit(SD_MUTEX(un));
8624                 /*
8625                  * Note: sd_reserve_release sends a command to the device
8626                  * via the sd_ioctlcmd() path, and can sleep.
8627                  */
8628                 if (sd_reserve_release(dev, SD_RELEASE) != 0) {
8629                         SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8630                             "sd_dr_detach: Cannot release reservation \n");
8631                 }
8632         } else {
8633                 mutex_exit(SD_MUTEX(un));
8634         }
8635 
8636         /*
8637          * Untimeout any reserve recover, throttle reset, restart unit
8638          * and delayed broadcast timeout threads. Protect the timeout pointer
8639          * from getting nulled by their callback functions.
8640          */
8641         mutex_enter(SD_MUTEX(un));
8642         if (un->un_resvd_timeid != NULL) {
8643                 timeout_id_t temp_id = un->un_resvd_timeid;
8644                 un->un_resvd_timeid = NULL;
8645                 mutex_exit(SD_MUTEX(un));
8646                 (void) untimeout(temp_id);
8647                 mutex_enter(SD_MUTEX(un));
8648         }
8649 
8650         if (un->un_reset_throttle_timeid != NULL) {
8651                 timeout_id_t temp_id = un->un_reset_throttle_timeid;
8652                 un->un_reset_throttle_timeid = NULL;
8653                 mutex_exit(SD_MUTEX(un));
8654                 (void) untimeout(temp_id);
8655                 mutex_enter(SD_MUTEX(un));
8656         }
8657 
8658         if (un->un_startstop_timeid != NULL) {
8659                 timeout_id_t temp_id = un->un_startstop_timeid;
8660                 un->un_startstop_timeid = NULL;
8661                 mutex_exit(SD_MUTEX(un));
8662                 (void) untimeout(temp_id);
8663                 mutex_enter(SD_MUTEX(un));
8664         }
8665 
8666         if (un->un_rmw_msg_timeid != NULL) {
8667                 timeout_id_t temp_id = un->un_rmw_msg_timeid;
8668                 un->un_rmw_msg_timeid = NULL;
8669                 mutex_exit(SD_MUTEX(un));
8670                 (void) untimeout(temp_id);
8671                 mutex_enter(SD_MUTEX(un));
8672         }
8673 
8674         if (un->un_dcvb_timeid != NULL) {
8675                 timeout_id_t temp_id = un->un_dcvb_timeid;
8676                 un->un_dcvb_timeid = NULL;
8677                 mutex_exit(SD_MUTEX(un));
8678                 (void) untimeout(temp_id);
8679         } else {
8680                 mutex_exit(SD_MUTEX(un));
8681         }
8682 
8683         /* Remove any pending reservation reclaim requests for this device */
8684         sd_rmv_resv_reclaim_req(dev);
8685 
8686         mutex_enter(SD_MUTEX(un));
8687 
8688         /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */
8689         if (un->un_direct_priority_timeid != NULL) {
8690                 timeout_id_t temp_id = un->un_direct_priority_timeid;
8691                 un->un_direct_priority_timeid = NULL;
8692                 mutex_exit(SD_MUTEX(un));
8693                 (void) untimeout(temp_id);
8694                 mutex_enter(SD_MUTEX(un));
8695         }
8696 
8697         /* Cancel any active multi-host disk watch thread requests */
8698         if (un->un_mhd_token != NULL) {
8699                 mutex_exit(SD_MUTEX(un));
8700                  _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token));
8701                 if (scsi_watch_request_terminate(un->un_mhd_token,
8702                     SCSI_WATCH_TERMINATE_NOWAIT)) {
8703                         SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8704                             "sd_dr_detach: Cannot cancel mhd watch request\n");
8705                         /*
8706                          * Note: We are returning here after having removed
8707                          * some driver timeouts above. This is consistent with
8708                          * the legacy implementation but perhaps the watch
8709                          * terminate call should be made with the wait flag set.
8710                          */
8711                         goto err_stillbusy;
8712                 }
8713                 mutex_enter(SD_MUTEX(un));
8714                 un->un_mhd_token = NULL;
8715         }
8716 
8717         if (un->un_swr_token != NULL) {
8718                 mutex_exit(SD_MUTEX(un));
8719                 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token));
8720                 if (scsi_watch_request_terminate(un->un_swr_token,
8721                     SCSI_WATCH_TERMINATE_NOWAIT)) {
8722                         SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8723                             "sd_dr_detach: Cannot cancel swr watch request\n");
8724                         /*
8725                          * Note: We are returning here after having removed
8726                          * some driver timeouts above. This is consistent with
8727                          * the legacy implementation but perhaps the watch
8728                          * terminate call should be made with the wait flag set.
8729                          */
8730                         goto err_stillbusy;
8731                 }
8732                 mutex_enter(SD_MUTEX(un));
8733                 un->un_swr_token = NULL;
8734         }
8735 
8736         mutex_exit(SD_MUTEX(un));
8737 
8738         /*
8739          * Clear any scsi_reset_notifies. We clear the reset notifies
8740          * if we have not registered one.
8741          * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX!
8742          */
8743         (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
8744             sd_mhd_reset_notify_cb, (caddr_t)un);
8745 
8746         /*
8747          * protect the timeout pointers from getting nulled by
8748          * their callback functions during the cancellation process.
8749          * In such a scenario untimeout can be invoked with a null value.
8750          */
8751         _NOTE(NO_COMPETING_THREADS_NOW);
8752 
8753         mutex_enter(&un->un_pm_mutex);
8754         if (un->un_pm_idle_timeid != NULL) {
8755                 timeout_id_t temp_id = un->un_pm_idle_timeid;
8756                 un->un_pm_idle_timeid = NULL;
8757                 mutex_exit(&un->un_pm_mutex);
8758 
8759                 /*
8760                  * Timeout is active; cancel it.
8761                  * Note that it'll never be active on a device
8762                  * that does not support PM therefore we don't
8763                  * have to check before calling pm_idle_component.
8764                  */
8765                 (void) untimeout(temp_id);
8766                 (void) pm_idle_component(SD_DEVINFO(un), 0);
8767                 mutex_enter(&un->un_pm_mutex);
8768         }
8769 
8770         /*
8771          * Check whether there is already a timeout scheduled for power
8772          * management. If yes then don't lower the power here, that's.
8773          * the timeout handler's job.
8774          */
8775         if (un->un_pm_timeid != NULL) {
8776                 timeout_id_t temp_id = un->un_pm_timeid;
8777                 un->un_pm_timeid = NULL;
8778                 mutex_exit(&un->un_pm_mutex);
8779                 /*
8780                  * Timeout is active; cancel it.
8781                  * Note that it'll never be active on a device
8782                  * that does not support PM therefore we don't
8783                  * have to check before calling pm_idle_component.
8784                  */
8785                 (void) untimeout(temp_id);
8786                 (void) pm_idle_component(SD_DEVINFO(un), 0);
8787 
8788         } else {
8789                 mutex_exit(&un->un_pm_mutex);
8790                 if ((un->un_f_pm_is_enabled == TRUE) &&
8791                     (pm_lower_power(SD_DEVINFO(un), 0, SD_PM_STATE_STOPPED(un))
8792                     != DDI_SUCCESS)) {
8793                         SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8794                     "sd_dr_detach: Lower power request failed, ignoring.\n");
8795                         /*
8796                          * Fix for bug: 4297749, item # 13
8797                          * The above test now includes a check to see if PM is
8798                          * supported by this device before call
8799                          * pm_lower_power().
8800                          * Note, the following is not dead code. The call to
8801                          * pm_lower_power above will generate a call back into
8802                          * our sdpower routine which might result in a timeout
8803                          * handler getting activated. Therefore the following
8804                          * code is valid and necessary.
8805                          */
8806                         mutex_enter(&un->un_pm_mutex);
8807                         if (un->un_pm_timeid != NULL) {
8808                                 timeout_id_t temp_id = un->un_pm_timeid;
8809                                 un->un_pm_timeid = NULL;
8810                                 mutex_exit(&un->un_pm_mutex);
8811                                 (void) untimeout(temp_id);
8812                                 (void) pm_idle_component(SD_DEVINFO(un), 0);
8813                         } else {
8814                                 mutex_exit(&un->un_pm_mutex);
8815                         }
8816                 }
8817         }
8818 
8819         /*
8820          * Cleanup from the scsi_ifsetcap() calls (437868)
8821          * Relocated here from above to be after the call to
8822          * pm_lower_power, which was getting errors.
8823          */
8824         (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8825         (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8826 
8827         /*
8828          * Currently, tagged queuing is supported per target based by HBA.
8829          * Setting this per lun instance actually sets the capability of this
8830          * target in HBA, which affects those luns already attached on the
8831          * same target. So during detach, we can only disable this capability
8832          * only when this is the only lun left on this target. By doing
8833          * this, we assume a target has the same tagged queuing capability
8834          * for every lun. The condition can be removed when HBA is changed to
8835          * support per lun based tagged queuing capability.
8836          */
8837         if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) {
8838                 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8839         }
8840 
8841         if (un->un_f_is_fibre == FALSE) {
8842                 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8843         }
8844 
8845         /*
8846          * Remove any event callbacks, fibre only
8847          */
8848         if (un->un_f_is_fibre == TRUE) {
8849                 if ((un->un_insert_event != NULL) &&
8850                     (ddi_remove_event_handler(un->un_insert_cb_id) !=
8851                     DDI_SUCCESS)) {
8852                         /*
8853                          * Note: We are returning here after having done
8854                          * substantial cleanup above. This is consistent
8855                          * with the legacy implementation but this may not
8856                          * be the right thing to do.
8857                          */
8858                         SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8859                             "sd_dr_detach: Cannot cancel insert event\n");
8860                         goto err_remove_event;
8861                 }
8862                 un->un_insert_event = NULL;
8863 
8864                 if ((un->un_remove_event != NULL) &&
8865                     (ddi_remove_event_handler(un->un_remove_cb_id) !=
8866                     DDI_SUCCESS)) {
8867                         /*
8868                          * Note: We are returning here after having done
8869                          * substantial cleanup above. This is consistent
8870                          * with the legacy implementation but this may not
8871                          * be the right thing to do.
8872                          */
8873                         SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8874                             "sd_dr_detach: Cannot cancel remove event\n");
8875                         goto err_remove_event;
8876                 }
8877                 un->un_remove_event = NULL;
8878         }
8879 
8880         /* Do not free the softstate if the callback routine is active */
8881         sd_sync_with_callback(un);
8882 
8883         cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
8884         cmlb_free_handle(&un->un_cmlbhandle);
8885 
8886         /*
8887          * Hold the detach mutex here, to make sure that no other threads ever
8888          * can access a (partially) freed soft state structure.
8889          */
8890         mutex_enter(&sd_detach_mutex);
8891 
8892         /*
8893          * Clean up the soft state struct.
8894          * Cleanup is done in reverse order of allocs/inits.
8895          * At this point there should be no competing threads anymore.
8896          */
8897 
8898         scsi_fm_fini(devp);
8899 
8900         /*
8901          * Deallocate memory for SCSI FMA.
8902          */
8903         kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
8904 
8905         /*
8906          * Unregister and free device id if it was not registered
8907          * by the transport.
8908          */
8909         if (un->un_f_devid_transport_defined == FALSE)
8910                 ddi_devid_unregister(devi);
8911 
8912         /*
8913          * free the devid structure if allocated before (by ddi_devid_init()
8914          * or ddi_devid_get()).
8915          */
8916         if (un->un_devid) {
8917                 ddi_devid_free(un->un_devid);
8918                 un->un_devid = NULL;
8919         }
8920 
8921         /*
8922          * Destroy wmap cache if it exists.
8923          */
8924         if (un->un_wm_cache != NULL) {
8925                 kmem_cache_destroy(un->un_wm_cache);
8926                 un->un_wm_cache = NULL;
8927         }
8928 
8929         /*
8930          * kstat cleanup is done in detach for all device types (4363169).
8931          * We do not want to fail detach if the device kstats are not deleted
8932          * since there is a confusion about the devo_refcnt for the device.
8933          * We just delete the kstats and let detach complete successfully.
8934          */
8935         if (un->un_stats != NULL) {
8936                 kstat_delete(un->un_stats);
8937                 un->un_stats = NULL;
8938         }
8939         if (un->un_errstats != NULL) {
8940                 kstat_delete(un->un_errstats);
8941                 un->un_errstats = NULL;
8942         }
8943 
8944         /* Remove partition stats */
8945         if (un->un_f_pkstats_enabled) {
8946                 for (i = 0; i < NSDMAP; i++) {
8947                         if (un->un_pstats[i] != NULL) {
8948                                 kstat_delete(un->un_pstats[i]);
8949                                 un->un_pstats[i] = NULL;
8950                         }
8951                 }
8952         }
8953 
8954         /* Remove xbuf registration */
8955         ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8956         ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8957 
8958         /* Remove driver properties */
8959         ddi_prop_remove_all(devi);
8960 
8961         mutex_destroy(&un->un_pm_mutex);
8962         cv_destroy(&un->un_pm_busy_cv);
8963 
8964         cv_destroy(&un->un_wcc_cv);
8965 
8966         /* Open/close semaphore */
8967         sema_destroy(&un->un_semoclose);
8968 
8969         /* Removable media condvar. */
8970         cv_destroy(&un->un_state_cv);
8971 
8972         /* Suspend/resume condvar. */
8973         cv_destroy(&un->un_suspend_cv);
8974         cv_destroy(&un->un_disk_busy_cv);
8975 
8976         sd_free_rqs(un);
8977 
8978         /* Free up soft state */
8979         devp->sd_private = NULL;
8980 
8981         bzero(un, sizeof (struct sd_lun));
8982 
8983         ddi_soft_state_free(sd_state, instance);
8984 
8985         mutex_exit(&sd_detach_mutex);
8986 
8987         /* This frees up the INQUIRY data associated with the device. */
8988         scsi_unprobe(devp);
8989 
8990         /*
8991          * After successfully detaching an instance, we update the information
8992          * of how many luns have been attached in the relative target and
8993          * controller for parallel SCSI. This information is used when sd tries
8994          * to set the tagged queuing capability in HBA.
8995          * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to
8996          * check if the device is parallel SCSI. However, we don't need to
8997          * check here because we've already checked during attach. No device
8998          * that is not parallel SCSI is in the chain.
8999          */
9000         if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) {
9001                 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH);
9002         }
9003 
9004         return (DDI_SUCCESS);
9005 
9006 err_notclosed:
9007         mutex_exit(SD_MUTEX(un));
9008 
9009 err_stillbusy:
9010         _NOTE(NO_COMPETING_THREADS_NOW);
9011 
9012 err_remove_event:
9013         mutex_enter(&sd_detach_mutex);
9014         un->un_detach_count--;
9015         mutex_exit(&sd_detach_mutex);
9016 
9017         SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n");
9018         return (DDI_FAILURE);
9019 }
9020 
9021 
9022 /*
9023  *    Function: sd_create_errstats
9024  *
9025  * Description: This routine instantiates the device error stats.
9026  *
9027  *              Note: During attach the stats are instantiated first so they are
9028  *              available for attach-time routines that utilize the driver
9029  *              iopath to send commands to the device. The stats are initialized
9030  *              separately so data obtained during some attach-time routines is
9031  *              available. (4362483)
9032  *
9033  *   Arguments: un - driver soft state (unit) structure
9034  *              instance - driver instance
9035  *
9036  *     Context: Kernel thread context
9037  */
9038 
9039 static void
9040 sd_create_errstats(struct sd_lun *un, int instance)
9041 {
9042         struct  sd_errstats     *stp;
9043         char    kstatmodule_err[KSTAT_STRLEN];
9044         char    kstatname[KSTAT_STRLEN];
9045         int     ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t));
9046 
9047         ASSERT(un != NULL);
9048 
9049         if (un->un_errstats != NULL) {
9050                 return;
9051         }
9052 
9053         (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
9054             "%serr", sd_label);
9055         (void) snprintf(kstatname, sizeof (kstatname),
9056             "%s%d,err", sd_label, instance);
9057 
9058         un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname,
9059             "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
9060 
9061         if (un->un_errstats == NULL) {
9062                 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9063                     "sd_create_errstats: Failed kstat_create\n");
9064                 return;
9065         }
9066 
9067         stp = (struct sd_errstats *)un->un_errstats->ks_data;
9068         kstat_named_init(&stp->sd_softerrs,      "Soft Errors",
9069             KSTAT_DATA_UINT32);
9070         kstat_named_init(&stp->sd_harderrs,      "Hard Errors",
9071             KSTAT_DATA_UINT32);
9072         kstat_named_init(&stp->sd_transerrs,     "Transport Errors",
9073             KSTAT_DATA_UINT32);
9074         kstat_named_init(&stp->sd_vid,           "Vendor",
9075             KSTAT_DATA_CHAR);
9076         kstat_named_init(&stp->sd_pid,           "Product",
9077             KSTAT_DATA_CHAR);
9078         kstat_named_init(&stp->sd_revision,      "Revision",
9079             KSTAT_DATA_CHAR);
9080         kstat_named_init(&stp->sd_serial,        "Serial No",
9081             KSTAT_DATA_CHAR);
9082         kstat_named_init(&stp->sd_capacity,      "Size",
9083             KSTAT_DATA_ULONGLONG);
9084         kstat_named_init(&stp->sd_rq_media_err,  "Media Error",
9085             KSTAT_DATA_UINT32);
9086         kstat_named_init(&stp->sd_rq_ntrdy_err,  "Device Not Ready",
9087             KSTAT_DATA_UINT32);
9088         kstat_named_init(&stp->sd_rq_nodev_err,  "No Device",
9089             KSTAT_DATA_UINT32);
9090         kstat_named_init(&stp->sd_rq_recov_err,  "Recoverable",
9091             KSTAT_DATA_UINT32);
9092         kstat_named_init(&stp->sd_rq_illrq_err,  "Illegal Request",
9093             KSTAT_DATA_UINT32);
9094         kstat_named_init(&stp->sd_rq_pfa_err,    "Predictive Failure Analysis",
9095             KSTAT_DATA_UINT32);
9096 
9097         un->un_errstats->ks_private = un;
9098         un->un_errstats->ks_update  = nulldev;
9099 
9100         kstat_install(un->un_errstats);
9101 }
9102 
9103 
9104 /*
9105  *    Function: sd_set_errstats
9106  *
9107  * Description: This routine sets the value of the vendor id, product id,
9108  *              revision, serial number, and capacity device error stats.
9109  *
9110  *              Note: During attach the stats are instantiated first so they are
9111  *              available for attach-time routines that utilize the driver
9112  *              iopath to send commands to the device. The stats are initialized
9113  *              separately so data obtained during some attach-time routines is
9114  *              available. (4362483)
9115  *
9116  *   Arguments: un - driver soft state (unit) structure
9117  *
9118  *     Context: Kernel thread context
9119  */
9120 
9121 static void
9122 sd_set_errstats(struct sd_lun *un)
9123 {
9124         struct  sd_errstats     *stp;
9125         char                    *sn;
9126 
9127         ASSERT(un != NULL);
9128         ASSERT(un->un_errstats != NULL);
9129         stp = (struct sd_errstats *)un->un_errstats->ks_data;
9130         ASSERT(stp != NULL);
9131         (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8);
9132         (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16);
9133         (void) strncpy(stp->sd_revision.value.c,
9134             un->un_sd->sd_inq->inq_revision, 4);
9135 
9136         /*
9137          * All the errstats are persistent across detach/attach,
9138          * so reset all the errstats here in case of the hot
9139          * replacement of disk drives, except for not changed
9140          * Sun qualified drives.
9141          */
9142         if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) ||
9143             (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9144             sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) {
9145                 stp->sd_softerrs.value.ui32 = 0;
9146                 stp->sd_harderrs.value.ui32 = 0;
9147                 stp->sd_transerrs.value.ui32 = 0;
9148                 stp->sd_rq_media_err.value.ui32 = 0;
9149                 stp->sd_rq_ntrdy_err.value.ui32 = 0;
9150                 stp->sd_rq_nodev_err.value.ui32 = 0;
9151                 stp->sd_rq_recov_err.value.ui32 = 0;
9152                 stp->sd_rq_illrq_err.value.ui32 = 0;
9153                 stp->sd_rq_pfa_err.value.ui32 = 0;
9154         }
9155 
9156         /*
9157          * Set the "Serial No" kstat for Sun qualified drives (indicated by
9158          * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid)
9159          * (4376302))
9160          */
9161         if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) {
9162                 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9163                     sizeof (SD_INQUIRY(un)->inq_serial));
9164         } else {
9165                 /*
9166                  * Set the "Serial No" kstat for non-Sun qualified drives
9167                  */
9168                 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, SD_DEVINFO(un),
9169                     DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
9170                     INQUIRY_SERIAL_NO, &sn) == DDI_SUCCESS) {
9171                         (void) strlcpy(stp->sd_serial.value.c, sn,
9172                             sizeof (stp->sd_serial.value.c));
9173                         ddi_prop_free(sn);
9174                 }
9175         }
9176 
9177         if (un->un_f_blockcount_is_valid != TRUE) {
9178                 /*
9179                  * Set capacity error stat to 0 for no media. This ensures
9180                  * a valid capacity is displayed in response to 'iostat -E'
9181                  * when no media is present in the device.
9182                  */
9183                 stp->sd_capacity.value.ui64 = 0;
9184         } else {
9185                 /*
9186                  * Multiply un_blockcount by un->un_sys_blocksize to get
9187                  * capacity.
9188                  *
9189                  * Note: for non-512 blocksize devices "un_blockcount" has been
9190                  * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by
9191                  * (un_tgt_blocksize / un->un_sys_blocksize).
9192                  */
9193                 stp->sd_capacity.value.ui64 = (uint64_t)
9194                     ((uint64_t)un->un_blockcount * un->un_sys_blocksize);
9195         }
9196 }
9197 
9198 
9199 /*
9200  *    Function: sd_set_pstats
9201  *
9202  * Description: This routine instantiates and initializes the partition
9203  *              stats for each partition with more than zero blocks.
9204  *              (4363169)
9205  *
9206  *   Arguments: un - driver soft state (unit) structure
9207  *
9208  *     Context: Kernel thread context
9209  */
9210 
9211 static void
9212 sd_set_pstats(struct sd_lun *un)
9213 {
9214         char    kstatname[KSTAT_STRLEN];
9215         int     instance;
9216         int     i;
9217         diskaddr_t      nblks = 0;
9218         char    *partname = NULL;
9219 
9220         ASSERT(un != NULL);
9221 
9222         instance = ddi_get_instance(SD_DEVINFO(un));
9223 
9224         /* Note:x86: is this a VTOC8/VTOC16 difference? */
9225         for (i = 0; i < NSDMAP; i++) {
9226 
9227                 if (cmlb_partinfo(un->un_cmlbhandle, i,
9228                     &nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0)
9229                         continue;
9230                 mutex_enter(SD_MUTEX(un));
9231 
9232                 if ((un->un_pstats[i] == NULL) &&
9233                     (nblks != 0)) {
9234 
9235                         (void) snprintf(kstatname, sizeof (kstatname),
9236                             "%s%d,%s", sd_label, instance,
9237                             partname);
9238 
9239                         un->un_pstats[i] = kstat_create(sd_label,
9240                             instance, kstatname, "partition", KSTAT_TYPE_IO,
9241                             1, KSTAT_FLAG_PERSISTENT);
9242                         if (un->un_pstats[i] != NULL) {
9243                                 un->un_pstats[i]->ks_lock = SD_MUTEX(un);
9244                                 kstat_install(un->un_pstats[i]);
9245                         }
9246                 }
9247                 mutex_exit(SD_MUTEX(un));
9248         }
9249 }
9250 
9251 
9252 #if (defined(__fibre))
9253 /*
9254  *    Function: sd_init_event_callbacks
9255  *
9256  * Description: This routine initializes the insertion and removal event
9257  *              callbacks. (fibre only)
9258  *
9259  *   Arguments: un - driver soft state (unit) structure
9260  *
9261  *     Context: Kernel thread context
9262  */
9263 
9264 static void
9265 sd_init_event_callbacks(struct sd_lun *un)
9266 {
9267         ASSERT(un != NULL);
9268 
9269         if ((un->un_insert_event == NULL) &&
9270             (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT,
9271             &un->un_insert_event) == DDI_SUCCESS)) {
9272                 /*
9273                  * Add the callback for an insertion event
9274                  */
9275                 (void) ddi_add_event_handler(SD_DEVINFO(un),
9276                     un->un_insert_event, sd_event_callback, (void *)un,
9277                     &(un->un_insert_cb_id));
9278         }
9279 
9280         if ((un->un_remove_event == NULL) &&
9281             (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT,
9282             &un->un_remove_event) == DDI_SUCCESS)) {
9283                 /*
9284                  * Add the callback for a removal event
9285                  */
9286                 (void) ddi_add_event_handler(SD_DEVINFO(un),
9287                     un->un_remove_event, sd_event_callback, (void *)un,
9288                     &(un->un_remove_cb_id));
9289         }
9290 }
9291 
9292 
9293 /*
9294  *    Function: sd_event_callback
9295  *
9296  * Description: This routine handles insert/remove events (photon). The
9297  *              state is changed to OFFLINE which can be used to supress
9298  *              error msgs. (fibre only)
9299  *
9300  *   Arguments: un - driver soft state (unit) structure
9301  *
9302  *     Context: Callout thread context
9303  */
9304 /* ARGSUSED */
9305 static void
9306 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg,
9307     void *bus_impldata)
9308 {
9309         struct sd_lun *un = (struct sd_lun *)arg;
9310 
9311         _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event));
9312         if (event == un->un_insert_event) {
9313                 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event");
9314                 mutex_enter(SD_MUTEX(un));
9315                 if (un->un_state == SD_STATE_OFFLINE) {
9316                         if (un->un_last_state != SD_STATE_SUSPENDED) {
9317                                 un->un_state = un->un_last_state;
9318                         } else {
9319                                 /*
9320                                  * We have gone through SUSPEND/RESUME while
9321                                  * we were offline. Restore the last state
9322                                  */
9323                                 un->un_state = un->un_save_state;
9324                         }
9325                 }
9326                 mutex_exit(SD_MUTEX(un));
9327 
9328         _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event));
9329         } else if (event == un->un_remove_event) {
9330                 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event");
9331                 mutex_enter(SD_MUTEX(un));
9332                 /*
9333                  * We need to handle an event callback that occurs during
9334                  * the suspend operation, since we don't prevent it.
9335                  */
9336                 if (un->un_state != SD_STATE_OFFLINE) {
9337                         if (un->un_state != SD_STATE_SUSPENDED) {
9338                                 New_state(un, SD_STATE_OFFLINE);
9339                         } else {
9340                                 un->un_last_state = SD_STATE_OFFLINE;
9341                         }
9342                 }
9343                 mutex_exit(SD_MUTEX(un));
9344         } else {
9345                 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
9346                     "!Unknown event\n");
9347         }
9348 
9349 }
9350 #endif
9351 
9352 /*
9353  * Values related to caching mode page depending on whether the unit is ATAPI.
9354  */
9355 #define SDC_CDB_GROUP(un) ((un->un_f_cfg_is_atapi == TRUE) ? \
9356         CDB_GROUP1 : CDB_GROUP0)
9357 #define SDC_HDRLEN(un) ((un->un_f_cfg_is_atapi == TRUE) ? \
9358         MODE_HEADER_LENGTH_GRP2 : MODE_HEADER_LENGTH)
9359 /*
9360  * Use mode_cache_scsi3 to ensure we get all of the mode sense data, otherwise
9361  * the mode select will fail (mode_cache_scsi3 is a superset of mode_caching).
9362  */
9363 #define SDC_BUFLEN(un) (SDC_HDRLEN(un) + MODE_BLK_DESC_LENGTH + \
9364         sizeof (struct mode_cache_scsi3))
9365 
9366 static int
9367 sd_get_caching_mode_page(sd_ssc_t *ssc, uchar_t page_control, uchar_t **header,
9368     int *bdlen)
9369 {
9370         struct sd_lun   *un = ssc->ssc_un;
9371         struct mode_caching *mode_caching_page;
9372         size_t          buflen = SDC_BUFLEN(un);
9373         int             hdrlen = SDC_HDRLEN(un);
9374         int             rval;
9375 
9376         /*
9377          * Do a test unit ready, otherwise a mode sense may not work if this
9378          * is the first command sent to the device after boot.
9379          */
9380         if (sd_send_scsi_TEST_UNIT_READY(ssc, 0) != 0)
9381                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9382 
9383         /*
9384          * Allocate memory for the retrieved mode page and its headers.  Set
9385          * a pointer to the page itself.
9386          */
9387         *header = kmem_zalloc(buflen, KM_SLEEP);
9388 
9389         /* Get the information from the device */
9390         rval = sd_send_scsi_MODE_SENSE(ssc, SDC_CDB_GROUP(un), *header, buflen,
9391             page_control | MODEPAGE_CACHING, SD_PATH_DIRECT);
9392         if (rval != 0) {
9393                 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un, "%s: Mode Sense Failed\n",
9394                     __func__);
9395                 goto mode_sense_failed;
9396         }
9397 
9398         /*
9399          * Determine size of Block Descriptors in order to locate
9400          * the mode page data. ATAPI devices return 0, SCSI devices
9401          * should return MODE_BLK_DESC_LENGTH.
9402          */
9403         if (un->un_f_cfg_is_atapi == TRUE) {
9404                 struct mode_header_grp2 *mhp =
9405                     (struct mode_header_grp2 *)(*header);
9406                 *bdlen = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
9407         } else {
9408                 *bdlen = ((struct mode_header *)(*header))->bdesc_length;
9409         }
9410 
9411         if (*bdlen > MODE_BLK_DESC_LENGTH) {
9412                 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0,
9413                     "%s: Mode Sense returned invalid block descriptor length\n",
9414                     __func__);
9415                 rval = EIO;
9416                 goto mode_sense_failed;
9417         }
9418 
9419         mode_caching_page = (struct mode_caching *)(*header + hdrlen + *bdlen);
9420         if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) {
9421                 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
9422                     "%s: Mode Sense caching page code mismatch %d\n",
9423                     __func__, mode_caching_page->mode_page.code);
9424                 rval = EIO;
9425         }
9426 
9427 mode_sense_failed:
9428         if (rval != 0) {
9429                 kmem_free(*header, buflen);
9430                 *header = NULL;
9431                 *bdlen = 0;
9432         }
9433         return (rval);
9434 }
9435 
9436 /*
9437  *    Function: sd_cache_control()
9438  *
9439  * Description: This routine is the driver entry point for setting
9440  *              read and write caching by modifying the WCE (write cache
9441  *              enable) and RCD (read cache disable) bits of mode
9442  *              page 8 (MODEPAGE_CACHING).
9443  *
9444  *   Arguments: ssc             - ssc contains pointer to driver soft state
9445  *                                (unit) structure for this target.
9446  *              rcd_flag        - flag for controlling the read cache
9447  *              wce_flag        - flag for controlling the write cache
9448  *
9449  * Return Code: EIO
9450  *              code returned by sd_send_scsi_MODE_SENSE and
9451  *              sd_send_scsi_MODE_SELECT
9452  *
9453  *     Context: Kernel Thread
9454  */
9455 
9456 static int
9457 sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag)
9458 {
9459         struct sd_lun   *un = ssc->ssc_un;
9460         struct mode_caching *mode_caching_page;
9461         uchar_t         *header;
9462         size_t          buflen = SDC_BUFLEN(un);
9463         int             hdrlen = SDC_HDRLEN(un);
9464         int             bdlen;
9465         int             rval;
9466 
9467         rval = sd_get_caching_mode_page(ssc, MODEPAGE_CURRENT, &header, &bdlen);
9468         switch (rval) {
9469         case 0:
9470                 /* Check the relevant bits on successful mode sense */
9471                 mode_caching_page = (struct mode_caching *)(header + hdrlen +
9472                     bdlen);
9473                 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) ||
9474                     (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) ||
9475                     (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) ||
9476                     (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) {
9477                         size_t sbuflen;
9478                         uchar_t save_pg;
9479 
9480                         /*
9481                          * Construct select buffer length based on the
9482                          * length of the sense data returned.
9483                          */
9484                         sbuflen = hdrlen + bdlen + sizeof (struct mode_page) +
9485                             (int)mode_caching_page->mode_page.length;
9486 
9487                         /* Set the caching bits as requested */
9488                         if (rcd_flag == SD_CACHE_ENABLE)
9489                                 mode_caching_page->rcd = 0;
9490                         else if (rcd_flag == SD_CACHE_DISABLE)
9491                                 mode_caching_page->rcd = 1;
9492 
9493                         if (wce_flag == SD_CACHE_ENABLE)
9494                                 mode_caching_page->wce = 1;
9495                         else if (wce_flag == SD_CACHE_DISABLE)
9496                                 mode_caching_page->wce = 0;
9497 
9498                         /*
9499                          * Save the page if the mode sense says the
9500                          * drive supports it.
9501                          */
9502                         save_pg = mode_caching_page->mode_page.ps ?
9503                             SD_SAVE_PAGE : SD_DONTSAVE_PAGE;
9504 
9505                         /* Clear reserved bits before mode select */
9506                         mode_caching_page->mode_page.ps = 0;
9507 
9508                         /*
9509                          * Clear out mode header for mode select.
9510                          * The rest of the retrieved page will be reused.
9511                          */
9512                         bzero(header, hdrlen);
9513 
9514                         if (un->un_f_cfg_is_atapi == TRUE) {
9515                                 struct mode_header_grp2 *mhp =
9516                                     (struct mode_header_grp2 *)header;
9517                                 mhp->bdesc_length_hi = bdlen >> 8;
9518                                 mhp->bdesc_length_lo = (uchar_t)bdlen & 0xff;
9519                         } else {
9520                                 ((struct mode_header *)header)->bdesc_length =
9521                                     bdlen;
9522                         }
9523 
9524                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9525 
9526                         /* Issue mode select to change the cache settings */
9527                         rval = sd_send_scsi_MODE_SELECT(ssc, SDC_CDB_GROUP(un),
9528                             header, sbuflen, save_pg, SD_PATH_DIRECT);
9529                 }
9530                 kmem_free(header, buflen);
9531                 break;
9532         case EIO:
9533                 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9534                 break;
9535         default:
9536                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9537                 break;
9538         }
9539 
9540         return (rval);
9541 }
9542 
9543 
9544 /*
9545  *    Function: sd_get_write_cache_enabled()
9546  *
9547  * Description: This routine is the driver entry point for determining if write
9548  *              caching is enabled.  It examines the WCE (write cache enable)
9549  *              bits of mode page 8 (MODEPAGE_CACHING) with Page Control field
9550  *              bits set to MODEPAGE_CURRENT.
9551  *
9552  *   Arguments: ssc             - ssc contains pointer to driver soft state
9553  *                                (unit) structure for this target.
9554  *              is_enabled      - pointer to int where write cache enabled state
9555  *                                is returned (non-zero -> write cache enabled)
9556  *
9557  * Return Code: EIO
9558  *              code returned by sd_send_scsi_MODE_SENSE
9559  *
9560  *     Context: Kernel Thread
9561  *
9562  * NOTE: If ioctl is added to disable write cache, this sequence should
9563  * be followed so that no locking is required for accesses to
9564  * un->un_f_write_cache_enabled:
9565  *      do mode select to clear wce
9566  *      do synchronize cache to flush cache
9567  *      set un->un_f_write_cache_enabled = FALSE
9568  *
9569  * Conversely, an ioctl to enable the write cache should be done
9570  * in this order:
9571  *      set un->un_f_write_cache_enabled = TRUE
9572  *      do mode select to set wce
9573  */
9574 
9575 static int
9576 sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled)
9577 {
9578         struct sd_lun   *un = ssc->ssc_un;
9579         struct mode_caching *mode_caching_page;
9580         uchar_t         *header;
9581         size_t          buflen = SDC_BUFLEN(un);
9582         int             hdrlen = SDC_HDRLEN(un);
9583         int             bdlen;
9584         int             rval;
9585 
9586         /* In case of error, flag as enabled */
9587         *is_enabled = TRUE;
9588 
9589         rval = sd_get_caching_mode_page(ssc, MODEPAGE_CURRENT, &header, &bdlen);
9590         switch (rval) {
9591         case 0:
9592                 mode_caching_page = (struct mode_caching *)(header + hdrlen +
9593                     bdlen);
9594                 *is_enabled = mode_caching_page->wce;
9595                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
9596                 kmem_free(header, buflen);
9597                 break;
9598         case EIO: {
9599                 /*
9600                  * Some disks do not support Mode Sense(6), we
9601                  * should ignore this kind of error (sense key is
9602                  * 0x5 - illegal request).
9603                  */
9604                 uint8_t *sensep;
9605                 int senlen;
9606 
9607                 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
9608                 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
9609                     ssc->ssc_uscsi_cmd->uscsi_rqresid);
9610 
9611                 if (senlen > 0 &&
9612                     scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
9613                         sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
9614                 } else {
9615                         sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9616                 }
9617                 break;
9618         }
9619         default:
9620                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9621                 break;
9622         }
9623 
9624         return (rval);
9625 }
9626 
9627 /*
9628  *    Function: sd_get_write_cache_changeable()
9629  *
9630  * Description: This routine is the driver entry point for determining if write
9631  *              caching is changeable.  It examines the WCE (write cache enable)
9632  *              bits of mode page 8 (MODEPAGE_CACHING) with Page Control field
9633  *              bits set to MODEPAGE_CHANGEABLE.
9634  *
9635  *   Arguments: ssc             - ssc contains pointer to driver soft state
9636  *                                (unit) structure for this target.
9637  *              is_changeable   - pointer to int where write cache changeable
9638  *                                state is returned (non-zero -> write cache
9639  *                                changeable)
9640  *
9641  *     Context: Kernel Thread
9642  */
9643 
9644 static void
9645 sd_get_write_cache_changeable(sd_ssc_t *ssc, int *is_changeable)
9646 {
9647         struct sd_lun   *un = ssc->ssc_un;
9648         struct mode_caching *mode_caching_page;
9649         uchar_t         *header;
9650         size_t          buflen = SDC_BUFLEN(un);
9651         int             hdrlen = SDC_HDRLEN(un);
9652         int             bdlen;
9653         int             rval;
9654 
9655         /* In case of error, flag as enabled */
9656         *is_changeable = TRUE;
9657 
9658         rval = sd_get_caching_mode_page(ssc, MODEPAGE_CHANGEABLE, &header,
9659             &bdlen);
9660         switch (rval) {
9661         case 0:
9662                 mode_caching_page = (struct mode_caching *)(header + hdrlen +
9663                     bdlen);
9664                 *is_changeable = mode_caching_page->wce;
9665                 kmem_free(header, buflen);
9666                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
9667                 break;
9668         case EIO:
9669                 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9670                 break;
9671         default:
9672                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9673                 break;
9674         }
9675 }
9676 
9677 /*
9678  *    Function: sd_get_nv_sup()
9679  *
9680  * Description: This routine is the driver entry point for
9681  * determining whether non-volatile cache is supported. This
9682  * determination process works as follows:
9683  *
9684  * 1. sd first queries sd.conf on whether
9685  * suppress_cache_flush bit is set for this device.
9686  *
9687  * 2. if not there, then queries the internal disk table.
9688  *
9689  * 3. if either sd.conf or internal disk table specifies
9690  * cache flush be suppressed, we don't bother checking
9691  * NV_SUP bit.
9692  *
9693  * If SUPPRESS_CACHE_FLUSH bit is not set to 1, sd queries
9694  * the optional INQUIRY VPD page 0x86. If the device
9695  * supports VPD page 0x86, sd examines the NV_SUP
9696  * (non-volatile cache support) bit in the INQUIRY VPD page
9697  * 0x86:
9698  *   o If NV_SUP bit is set, sd assumes the device has a
9699  *   non-volatile cache and set the
9700  *   un_f_sync_nv_supported to TRUE.
9701  *   o Otherwise cache is not non-volatile,
9702  *   un_f_sync_nv_supported is set to FALSE.
9703  *
9704  * Arguments: un - driver soft state (unit) structure
9705  *
9706  * Return Code:
9707  *
9708  *     Context: Kernel Thread
9709  */
9710 
9711 static void
9712 sd_get_nv_sup(sd_ssc_t *ssc)
9713 {
9714         int             rval            = 0;
9715         uchar_t         *inq86          = NULL;
9716         size_t          inq86_len       = MAX_INQUIRY_SIZE;
9717         size_t          inq86_resid     = 0;
9718         struct          dk_callback *dkc;
9719         struct sd_lun   *un;
9720 
9721         ASSERT(ssc != NULL);
9722         un = ssc->ssc_un;
9723         ASSERT(un != NULL);
9724 
9725         mutex_enter(SD_MUTEX(un));
9726 
9727         /*
9728          * Be conservative on the device's support of
9729          * SYNC_NV bit: un_f_sync_nv_supported is
9730          * initialized to be false.
9731          */
9732         un->un_f_sync_nv_supported = FALSE;
9733 
9734         /*
9735          * If either sd.conf or internal disk table
9736          * specifies cache flush be suppressed, then
9737          * we don't bother checking NV_SUP bit.
9738          */
9739         if (un->un_f_suppress_cache_flush == TRUE) {
9740                 mutex_exit(SD_MUTEX(un));
9741                 return;
9742         }
9743 
9744         if (sd_check_vpd_page_support(ssc) == 0 &&
9745             un->un_vpd_page_mask & SD_VPD_EXTENDED_DATA_PG) {
9746                 mutex_exit(SD_MUTEX(un));
9747                 /* collect page 86 data if available */
9748                 inq86 = kmem_zalloc(inq86_len, KM_SLEEP);
9749 
9750                 rval = sd_send_scsi_INQUIRY(ssc, inq86, inq86_len,
9751                     0x01, 0x86, &inq86_resid);
9752 
9753                 if (rval == 0 && (inq86_len - inq86_resid > 6)) {
9754                         SD_TRACE(SD_LOG_COMMON, un,
9755                             "sd_get_nv_sup: \
9756                             successfully get VPD page: %x \
9757                             PAGE LENGTH: %x BYTE 6: %x\n",
9758                             inq86[1], inq86[3], inq86[6]);
9759 
9760                         mutex_enter(SD_MUTEX(un));
9761                         /*
9762                          * check the value of NV_SUP bit: only if the device
9763                          * reports NV_SUP bit to be 1, the
9764                          * un_f_sync_nv_supported bit will be set to true.
9765                          */
9766                         if (inq86[6] & SD_VPD_NV_SUP) {
9767                                 un->un_f_sync_nv_supported = TRUE;
9768                         }
9769                         mutex_exit(SD_MUTEX(un));
9770                 } else if (rval != 0) {
9771                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9772                 }
9773 
9774                 kmem_free(inq86, inq86_len);
9775         } else {
9776                 mutex_exit(SD_MUTEX(un));
9777         }
9778 
9779         /*
9780          * Send a SYNC CACHE command to check whether
9781          * SYNC_NV bit is supported. This command should have
9782          * un_f_sync_nv_supported set to correct value.
9783          */
9784         mutex_enter(SD_MUTEX(un));
9785         if (un->un_f_sync_nv_supported) {
9786                 mutex_exit(SD_MUTEX(un));
9787                 dkc = kmem_zalloc(sizeof (struct dk_callback), KM_SLEEP);
9788                 dkc->dkc_flag = FLUSH_VOLATILE;
9789                 (void) sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
9790 
9791                 /*
9792                  * Send a TEST UNIT READY command to the device. This should
9793                  * clear any outstanding UNIT ATTENTION that may be present.
9794                  */
9795                 rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
9796                 if (rval != 0)
9797                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9798 
9799                 kmem_free(dkc, sizeof (struct dk_callback));
9800         } else {
9801                 mutex_exit(SD_MUTEX(un));
9802         }
9803 
9804         SD_TRACE(SD_LOG_COMMON, un, "sd_get_nv_sup: \
9805             un_f_suppress_cache_flush is set to %d\n",
9806             un->un_f_suppress_cache_flush);
9807 }
9808 
9809 /*
9810  *    Function: sd_make_device
9811  *
9812  * Description: Utility routine to return the Solaris device number from
9813  *              the data in the device's dev_info structure.
9814  *
9815  * Return Code: The Solaris device number
9816  *
9817  *     Context: Any
9818  */
9819 
9820 static dev_t
9821 sd_make_device(dev_info_t *devi)
9822 {
9823         return (makedevice(ddi_driver_major(devi),
9824             ddi_get_instance(devi) << SDUNIT_SHIFT));
9825 }
9826 
9827 
9828 /*
9829  *    Function: sd_pm_entry
9830  *
9831  * Description: Called at the start of a new command to manage power
9832  *              and busy status of a device. This includes determining whether
9833  *              the current power state of the device is sufficient for
9834  *              performing the command or whether it must be changed.
9835  *              The PM framework is notified appropriately.
9836  *              Only with a return status of DDI_SUCCESS will the
9837  *              component be busy to the framework.
9838  *
9839  *              All callers of sd_pm_entry must check the return status
9840  *              and only call sd_pm_exit it it was DDI_SUCCESS. A status
9841  *              of DDI_FAILURE indicates the device failed to power up.
9842  *              In this case un_pm_count has been adjusted so the result
9843  *              on exit is still powered down, ie. count is less than 0.
9844  *              Calling sd_pm_exit with this count value hits an ASSERT.
9845  *
9846  * Return Code: DDI_SUCCESS or DDI_FAILURE
9847  *
9848  *     Context: Kernel thread context.
9849  */
9850 
9851 static int
9852 sd_pm_entry(struct sd_lun *un)
9853 {
9854         int return_status = DDI_SUCCESS;
9855 
9856         ASSERT(!mutex_owned(SD_MUTEX(un)));
9857         ASSERT(!mutex_owned(&un->un_pm_mutex));
9858 
9859         SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n");
9860 
9861         if (un->un_f_pm_is_enabled == FALSE) {
9862                 SD_TRACE(SD_LOG_IO_PM, un,
9863                     "sd_pm_entry: exiting, PM not enabled\n");
9864                 return (return_status);
9865         }
9866 
9867         /*
9868          * Just increment a counter if PM is enabled. On the transition from
9869          * 0 ==> 1, mark the device as busy.  The iodone side will decrement
9870          * the count with each IO and mark the device as idle when the count
9871          * hits 0.
9872          *
9873          * If the count is less than 0 the device is powered down. If a powered
9874          * down device is successfully powered up then the count must be
9875          * incremented to reflect the power up. Note that it'll get incremented
9876          * a second time to become busy.
9877          *
9878          * Because the following has the potential to change the device state
9879          * and must release the un_pm_mutex to do so, only one thread can be
9880          * allowed through at a time.
9881          */
9882 
9883         mutex_enter(&un->un_pm_mutex);
9884         while (un->un_pm_busy == TRUE) {
9885                 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex);
9886         }
9887         un->un_pm_busy = TRUE;
9888 
9889         if (un->un_pm_count < 1) {
9890 
9891                 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n");
9892 
9893                 /*
9894                  * Indicate we are now busy so the framework won't attempt to
9895                  * power down the device. This call will only fail if either
9896                  * we passed a bad component number or the device has no
9897                  * components. Neither of these should ever happen.
9898                  */
9899                 mutex_exit(&un->un_pm_mutex);
9900                 return_status = pm_busy_component(SD_DEVINFO(un), 0);
9901                 ASSERT(return_status == DDI_SUCCESS);
9902 
9903                 mutex_enter(&un->un_pm_mutex);
9904 
9905                 if (un->un_pm_count < 0) {
9906                         mutex_exit(&un->un_pm_mutex);
9907 
9908                         SD_TRACE(SD_LOG_IO_PM, un,
9909                             "sd_pm_entry: power up component\n");
9910 
9911                         /*
9912                          * pm_raise_power will cause sdpower to be called
9913                          * which brings the device power level to the
9914                          * desired state, If successful, un_pm_count and
9915                          * un_power_level will be updated appropriately.
9916                          */
9917                         return_status = pm_raise_power(SD_DEVINFO(un), 0,
9918                             SD_PM_STATE_ACTIVE(un));
9919 
9920                         mutex_enter(&un->un_pm_mutex);
9921 
9922                         if (return_status != DDI_SUCCESS) {
9923                                 /*
9924                                  * Power up failed.
9925                                  * Idle the device and adjust the count
9926                                  * so the result on exit is that we're
9927                                  * still powered down, ie. count is less than 0.
9928                                  */
9929                                 SD_TRACE(SD_LOG_IO_PM, un,
9930                                     "sd_pm_entry: power up failed,"
9931                                     " idle the component\n");
9932 
9933                                 (void) pm_idle_component(SD_DEVINFO(un), 0);
9934                                 un->un_pm_count--;
9935                         } else {
9936                                 /*
9937                                  * Device is powered up, verify the
9938                                  * count is non-negative.
9939                                  * This is debug only.
9940                                  */
9941                                 ASSERT(un->un_pm_count == 0);
9942                         }
9943                 }
9944 
9945                 if (return_status == DDI_SUCCESS) {
9946                         /*
9947                          * For performance, now that the device has been tagged
9948                          * as busy, and it's known to be powered up, update the
9949                          * chain types to use jump tables that do not include
9950                          * pm. This significantly lowers the overhead and
9951                          * therefore improves performance.
9952                          */
9953 
9954                         mutex_exit(&un->un_pm_mutex);
9955                         mutex_enter(SD_MUTEX(un));
9956                         SD_TRACE(SD_LOG_IO_PM, un,
9957                             "sd_pm_entry: changing uscsi_chain_type from %d\n",
9958                             un->un_uscsi_chain_type);
9959 
9960                         if (un->un_f_non_devbsize_supported) {
9961                                 un->un_buf_chain_type =
9962                                     SD_CHAIN_INFO_RMMEDIA_NO_PM;
9963                         } else {
9964                                 un->un_buf_chain_type =
9965                                     SD_CHAIN_INFO_DISK_NO_PM;
9966                         }
9967                         un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
9968 
9969                         SD_TRACE(SD_LOG_IO_PM, un,
9970                             "             changed  uscsi_chain_type to   %d\n",
9971                             un->un_uscsi_chain_type);
9972                         mutex_exit(SD_MUTEX(un));
9973                         mutex_enter(&un->un_pm_mutex);
9974 
9975                         if (un->un_pm_idle_timeid == NULL) {
9976                                 /* 300 ms. */
9977                                 un->un_pm_idle_timeid =
9978                                     timeout(sd_pm_idletimeout_handler, un,
9979                                     (drv_usectohz((clock_t)300000)));
9980                                 /*
9981                                  * Include an extra call to busy which keeps the
9982                                  * device busy with-respect-to the PM layer
9983                                  * until the timer fires, at which time it'll
9984                                  * get the extra idle call.
9985                                  */
9986                                 (void) pm_busy_component(SD_DEVINFO(un), 0);
9987                         }
9988                 }
9989         }
9990         un->un_pm_busy = FALSE;
9991         /* Next... */
9992         cv_signal(&un->un_pm_busy_cv);
9993 
9994         un->un_pm_count++;
9995 
9996         SD_TRACE(SD_LOG_IO_PM, un,
9997             "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count);
9998 
9999         mutex_exit(&un->un_pm_mutex);
10000 
10001         return (return_status);
10002 }
10003 
10004 
10005 /*
10006  *    Function: sd_pm_exit
10007  *
10008  * Description: Called at the completion of a command to manage busy
10009  *              status for the device. If the device becomes idle the
10010  *              PM framework is notified.
10011  *
10012  *     Context: Kernel thread context
10013  */
10014 
10015 static void
10016 sd_pm_exit(struct sd_lun *un)
10017 {
10018         ASSERT(!mutex_owned(SD_MUTEX(un)));
10019         ASSERT(!mutex_owned(&un->un_pm_mutex));
10020 
10021         SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n");
10022 
10023         /*
10024          * After attach the following flag is only read, so don't
10025          * take the penalty of acquiring a mutex for it.
10026          */
10027         if (un->un_f_pm_is_enabled == TRUE) {
10028 
10029                 mutex_enter(&un->un_pm_mutex);
10030                 un->un_pm_count--;
10031 
10032                 SD_TRACE(SD_LOG_IO_PM, un,
10033                     "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count);
10034 
10035                 ASSERT(un->un_pm_count >= 0);
10036                 if (un->un_pm_count == 0) {
10037                         mutex_exit(&un->un_pm_mutex);
10038 
10039                         SD_TRACE(SD_LOG_IO_PM, un,
10040                             "sd_pm_exit: idle component\n");
10041 
10042                         (void) pm_idle_component(SD_DEVINFO(un), 0);
10043 
10044                 } else {
10045                         mutex_exit(&un->un_pm_mutex);
10046                 }
10047         }
10048 
10049         SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n");
10050 }
10051 
10052 
10053 /*
10054  *    Function: sdopen
10055  *
10056  * Description: Driver's open(9e) entry point function.
10057  *
10058  *   Arguments: dev_i   - pointer to device number
10059  *              flag    - how to open file (FEXCL, FNDELAY, FREAD, FWRITE)
10060  *              otyp    - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10061  *              cred_p  - user credential pointer
10062  *
10063  * Return Code: EINVAL
10064  *              ENXIO
10065  *              EIO
10066  *              EROFS
10067  *              EBUSY
10068  *
10069  *     Context: Kernel thread context
10070  */
10071 /* ARGSUSED */
10072 static int
10073 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
10074 {
10075         struct sd_lun   *un;
10076         int             nodelay;
10077         int             part;
10078         uint64_t        partmask;
10079         int             instance;
10080         dev_t           dev;
10081         int             rval = EIO;
10082         diskaddr_t      nblks = 0;
10083         diskaddr_t      label_cap;
10084 
10085         /* Validate the open type */
10086         if (otyp >= OTYPCNT) {
10087                 return (EINVAL);
10088         }
10089 
10090         dev = *dev_p;
10091         instance = SDUNIT(dev);
10092         mutex_enter(&sd_detach_mutex);
10093 
10094         /*
10095          * Fail the open if there is no softstate for the instance, or
10096          * if another thread somewhere is trying to detach the instance.
10097          */
10098         if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
10099             (un->un_detach_count != 0)) {
10100                 mutex_exit(&sd_detach_mutex);
10101                 /*
10102                  * The probe cache only needs to be cleared when open (9e) fails
10103                  * with ENXIO (4238046).
10104                  */
10105                 /*
10106                  * un-conditionally clearing probe cache is ok with
10107                  * separate sd/ssd binaries
10108                  * x86 platform can be an issue with both parallel
10109                  * and fibre in 1 binary
10110                  */
10111                 sd_scsi_clear_probe_cache();
10112                 return (ENXIO);
10113         }
10114 
10115         /*
10116          * The un_layer_count is to prevent another thread in specfs from
10117          * trying to detach the instance, which can happen when we are
10118          * called from a higher-layer driver instead of thru specfs.
10119          * This will not be needed when DDI provides a layered driver
10120          * interface that allows specfs to know that an instance is in
10121          * use by a layered driver & should not be detached.
10122          *
10123          * Note: the semantics for layered driver opens are exactly one
10124          * close for every open.
10125          */
10126         if (otyp == OTYP_LYR) {
10127                 un->un_layer_count++;
10128         }
10129 
10130         /*
10131          * Keep a count of the current # of opens in progress. This is because
10132          * some layered drivers try to call us as a regular open. This can
10133          * cause problems that we cannot prevent, however by keeping this count
10134          * we can at least keep our open and detach routines from racing against
10135          * each other under such conditions.
10136          */
10137         un->un_opens_in_progress++;
10138         mutex_exit(&sd_detach_mutex);
10139 
10140         nodelay  = (flag & (FNDELAY | FNONBLOCK));
10141         part     = SDPART(dev);
10142         partmask = 1 << part;
10143 
10144         /*
10145          * We use a semaphore here in order to serialize
10146          * open and close requests on the device.
10147          */
10148         sema_p(&un->un_semoclose);
10149 
10150         mutex_enter(SD_MUTEX(un));
10151 
10152         /*
10153          * All device accesses go thru sdstrategy() where we check
10154          * on suspend status but there could be a scsi_poll command,
10155          * which bypasses sdstrategy(), so we need to check pm
10156          * status.
10157          */
10158 
10159         if (!nodelay) {
10160                 while ((un->un_state == SD_STATE_SUSPENDED) ||
10161                     (un->un_state == SD_STATE_PM_CHANGING)) {
10162                         cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10163                 }
10164 
10165                 mutex_exit(SD_MUTEX(un));
10166                 if (sd_pm_entry(un) != DDI_SUCCESS) {
10167                         rval = EIO;
10168                         SD_ERROR(SD_LOG_OPEN_CLOSE, un,
10169                             "sdopen: sd_pm_entry failed\n");
10170                         goto open_failed_with_pm;
10171                 }
10172                 mutex_enter(SD_MUTEX(un));
10173         }
10174 
10175         /* check for previous exclusive open */
10176         SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un);
10177         SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10178             "sdopen: exclopen=%x, flag=%x, regopen=%x\n",
10179             un->un_exclopen, flag, un->un_ocmap.regopen[otyp]);
10180 
10181         if (un->un_exclopen & (partmask)) {
10182                 goto excl_open_fail;
10183         }
10184 
10185         if (flag & FEXCL) {
10186                 int i;
10187                 if (un->un_ocmap.lyropen[part]) {
10188                         goto excl_open_fail;
10189                 }
10190                 for (i = 0; i < (OTYPCNT - 1); i++) {
10191                         if (un->un_ocmap.regopen[i] & (partmask)) {
10192                                 goto excl_open_fail;
10193                         }
10194                 }
10195         }
10196 
10197         /*
10198          * Check the write permission if this is a removable media device,
10199          * NDELAY has not been set, and writable permission is requested.
10200          *
10201          * Note: If NDELAY was set and this is write-protected media the WRITE
10202          * attempt will fail with EIO as part of the I/O processing. This is a
10203          * more permissive implementation that allows the open to succeed and
10204          * WRITE attempts to fail when appropriate.
10205          */
10206         if (un->un_f_chk_wp_open) {
10207                 if ((flag & FWRITE) && (!nodelay)) {
10208                         mutex_exit(SD_MUTEX(un));
10209                         /*
10210                          * Defer the check for write permission on writable
10211                          * DVD drive till sdstrategy and will not fail open even
10212                          * if FWRITE is set as the device can be writable
10213                          * depending upon the media and the media can change
10214                          * after the call to open().
10215                          */
10216                         if (un->un_f_dvdram_writable_device == FALSE) {
10217                                 if (ISCD(un) || sr_check_wp(dev)) {
10218                                 rval = EROFS;
10219                                 mutex_enter(SD_MUTEX(un));
10220                                 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10221                                     "write to cd or write protected media\n");
10222                                 goto open_fail;
10223                                 }
10224                         }
10225                         mutex_enter(SD_MUTEX(un));
10226                 }
10227         }
10228 
10229         /*
10230          * If opening in NDELAY/NONBLOCK mode, just return.
10231          * Check if disk is ready and has a valid geometry later.
10232          */
10233         if (!nodelay) {
10234                 sd_ssc_t        *ssc;
10235 
10236                 mutex_exit(SD_MUTEX(un));
10237                 ssc = sd_ssc_init(un);
10238                 rval = sd_ready_and_valid(ssc, part);
10239                 sd_ssc_fini(ssc);
10240                 mutex_enter(SD_MUTEX(un));
10241                 /*
10242                  * Fail if device is not ready or if the number of disk
10243                  * blocks is zero or negative for non CD devices.
10244                  */
10245 
10246                 nblks = 0;
10247 
10248                 if (rval == SD_READY_VALID && (!ISCD(un))) {
10249                         /* if cmlb_partinfo fails, nblks remains 0 */
10250                         mutex_exit(SD_MUTEX(un));
10251                         (void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks,
10252                             NULL, NULL, NULL, (void *)SD_PATH_DIRECT);
10253                         mutex_enter(SD_MUTEX(un));
10254                 }
10255 
10256                 if ((rval != SD_READY_VALID) ||
10257                     (!ISCD(un) && nblks <= 0)) {
10258                         rval = un->un_f_has_removable_media ? ENXIO : EIO;
10259                         SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10260                             "device not ready or invalid disk block value\n");
10261                         goto open_fail;
10262                 }
10263 #if defined(__i386) || defined(__amd64)
10264         } else {
10265                 uchar_t *cp;
10266                 /*
10267                  * x86 requires special nodelay handling, so that p0 is
10268                  * always defined and accessible.
10269                  * Invalidate geometry only if device is not already open.
10270                  */
10271                 cp = &un->un_ocmap.chkd[0];
10272                 while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10273                         if (*cp != (uchar_t)0) {
10274                                 break;
10275                         }
10276                         cp++;
10277                 }
10278                 if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10279                         mutex_exit(SD_MUTEX(un));
10280                         cmlb_invalidate(un->un_cmlbhandle,
10281                             (void *)SD_PATH_DIRECT);
10282                         mutex_enter(SD_MUTEX(un));
10283                 }
10284 
10285 #endif
10286         }
10287 
10288         if (otyp == OTYP_LYR) {
10289                 un->un_ocmap.lyropen[part]++;
10290         } else {
10291                 un->un_ocmap.regopen[otyp] |= partmask;
10292         }
10293 
10294         /* Set up open and exclusive open flags */
10295         if (flag & FEXCL) {
10296                 un->un_exclopen |= (partmask);
10297         }
10298 
10299         /*
10300          * If the lun is EFI labeled and lun capacity is greater than the
10301          * capacity contained in the label, log a sys-event to notify the
10302          * interested module.
10303          * To avoid an infinite loop of logging sys-event, we only log the
10304          * event when the lun is not opened in NDELAY mode. The event handler
10305          * should open the lun in NDELAY mode.
10306          */
10307         if (!nodelay) {
10308                 mutex_exit(SD_MUTEX(un));
10309                 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
10310                     (void*)SD_PATH_DIRECT) == 0) {
10311                         mutex_enter(SD_MUTEX(un));
10312                         if (un->un_f_blockcount_is_valid &&
10313                             un->un_blockcount > label_cap &&
10314                             un->un_f_expnevent == B_FALSE) {
10315                                 un->un_f_expnevent = B_TRUE;
10316                                 mutex_exit(SD_MUTEX(un));
10317                                 sd_log_lun_expansion_event(un,
10318                                     (nodelay ? KM_NOSLEEP : KM_SLEEP));
10319                                 mutex_enter(SD_MUTEX(un));
10320                         }
10321                 } else {
10322                         mutex_enter(SD_MUTEX(un));
10323                 }
10324         }
10325 
10326         SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10327             "open of part %d type %d\n", part, otyp);
10328 
10329         mutex_exit(SD_MUTEX(un));
10330         if (!nodelay) {
10331                 sd_pm_exit(un);
10332         }
10333 
10334         sema_v(&un->un_semoclose);
10335 
10336         mutex_enter(&sd_detach_mutex);
10337         un->un_opens_in_progress--;
10338         mutex_exit(&sd_detach_mutex);
10339 
10340         SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n");
10341         return (DDI_SUCCESS);
10342 
10343 excl_open_fail:
10344         SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n");
10345         rval = EBUSY;
10346 
10347 open_fail:
10348         mutex_exit(SD_MUTEX(un));
10349 
10350         /*
10351          * On a failed open we must exit the pm management.
10352          */
10353         if (!nodelay) {
10354                 sd_pm_exit(un);
10355         }
10356 open_failed_with_pm:
10357         sema_v(&un->un_semoclose);
10358 
10359         mutex_enter(&sd_detach_mutex);
10360         un->un_opens_in_progress--;
10361         if (otyp == OTYP_LYR) {
10362                 un->un_layer_count--;
10363         }
10364         mutex_exit(&sd_detach_mutex);
10365 
10366         return (rval);
10367 }
10368 
10369 
10370 /*
10371  *    Function: sdclose
10372  *
10373  * Description: Driver's close(9e) entry point function.
10374  *
10375  *   Arguments: dev    - device number
10376  *              flag   - file status flag, informational only
10377  *              otyp   - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10378  *              cred_p - user credential pointer
10379  *
10380  * Return Code: ENXIO
10381  *
10382  *     Context: Kernel thread context
10383  */
10384 /* ARGSUSED */
10385 static int
10386 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p)
10387 {
10388         struct sd_lun   *un;
10389         uchar_t         *cp;
10390         int             part;
10391         int             nodelay;
10392         int             rval = 0;
10393 
10394         /* Validate the open type */
10395         if (otyp >= OTYPCNT) {
10396                 return (ENXIO);
10397         }
10398 
10399         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10400                 return (ENXIO);
10401         }
10402 
10403         part = SDPART(dev);
10404         nodelay = flag & (FNDELAY | FNONBLOCK);
10405 
10406         SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10407             "sdclose: close of part %d type %d\n", part, otyp);
10408 
10409         /*
10410          * We use a semaphore here in order to serialize
10411          * open and close requests on the device.
10412          */
10413         sema_p(&un->un_semoclose);
10414 
10415         mutex_enter(SD_MUTEX(un));
10416 
10417         /* Don't proceed if power is being changed. */
10418         while (un->un_state == SD_STATE_PM_CHANGING) {
10419                 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10420         }
10421 
10422         if (un->un_exclopen & (1 << part)) {
10423                 un->un_exclopen &= ~(1 << part);
10424         }
10425 
10426         /* Update the open partition map */
10427         if (otyp == OTYP_LYR) {
10428                 un->un_ocmap.lyropen[part] -= 1;
10429         } else {
10430                 un->un_ocmap.regopen[otyp] &= ~(1 << part);
10431         }
10432 
10433         cp = &un->un_ocmap.chkd[0];
10434         while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10435                 if (*cp != NULL) {
10436                         break;
10437                 }
10438                 cp++;
10439         }
10440 
10441         if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10442                 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n");
10443 
10444                 /*
10445                  * We avoid persistance upon the last close, and set
10446                  * the throttle back to the maximum.
10447                  */
10448                 un->un_throttle = un->un_saved_throttle;
10449 
10450                 if (un->un_state == SD_STATE_OFFLINE) {
10451                         if (un->un_f_is_fibre == FALSE) {
10452                                 scsi_log(SD_DEVINFO(un), sd_label,
10453                                     CE_WARN, "offline\n");
10454                         }
10455                         mutex_exit(SD_MUTEX(un));
10456                         cmlb_invalidate(un->un_cmlbhandle,
10457                             (void *)SD_PATH_DIRECT);
10458                         mutex_enter(SD_MUTEX(un));
10459 
10460                 } else {
10461                         /*
10462                          * Flush any outstanding writes in NVRAM cache.
10463                          * Note: SYNCHRONIZE CACHE is an optional SCSI-2
10464                          * cmd, it may not work for non-Pluto devices.
10465                          * SYNCHRONIZE CACHE is not required for removables,
10466                          * except DVD-RAM drives.
10467                          *
10468                          * Also note: because SYNCHRONIZE CACHE is currently
10469                          * the only command issued here that requires the
10470                          * drive be powered up, only do the power up before
10471                          * sending the Sync Cache command. If additional
10472                          * commands are added which require a powered up
10473                          * drive, the following sequence may have to change.
10474                          *
10475                          * And finally, note that parallel SCSI on SPARC
10476                          * only issues a Sync Cache to DVD-RAM, a newly
10477                          * supported device.
10478                          */
10479 #if defined(__i386) || defined(__amd64)
10480                         if ((un->un_f_sync_cache_supported &&
10481                             un->un_f_sync_cache_required) ||
10482                             un->un_f_dvdram_writable_device == TRUE) {
10483 #else
10484                         if (un->un_f_dvdram_writable_device == TRUE) {
10485 #endif
10486                                 mutex_exit(SD_MUTEX(un));
10487                                 if (sd_pm_entry(un) == DDI_SUCCESS) {
10488                                         rval =
10489                                             sd_send_scsi_SYNCHRONIZE_CACHE(un,
10490                                             NULL);
10491                                         /* ignore error if not supported */
10492                                         if (rval == ENOTSUP) {
10493                                                 rval = 0;
10494                                         } else if (rval != 0) {
10495                                                 rval = EIO;
10496                                         }
10497                                         sd_pm_exit(un);
10498                                 } else {
10499                                         rval = EIO;
10500                                 }
10501                                 mutex_enter(SD_MUTEX(un));
10502                         }
10503 
10504                         /*
10505                          * For devices which supports DOOR_LOCK, send an ALLOW
10506                          * MEDIA REMOVAL command, but don't get upset if it
10507                          * fails. We need to raise the power of the drive before
10508                          * we can call sd_send_scsi_DOORLOCK()
10509                          */
10510                         if (un->un_f_doorlock_supported) {
10511                                 mutex_exit(SD_MUTEX(un));
10512                                 if (sd_pm_entry(un) == DDI_SUCCESS) {
10513                                         sd_ssc_t        *ssc;
10514 
10515                                         ssc = sd_ssc_init(un);
10516                                         rval = sd_send_scsi_DOORLOCK(ssc,
10517                                             SD_REMOVAL_ALLOW, SD_PATH_DIRECT);
10518                                         if (rval != 0)
10519                                                 sd_ssc_assessment(ssc,
10520                                                     SD_FMT_IGNORE);
10521                                         sd_ssc_fini(ssc);
10522 
10523                                         sd_pm_exit(un);
10524                                         if (ISCD(un) && (rval != 0) &&
10525                                             (nodelay != 0)) {
10526                                                 rval = ENXIO;
10527                                         }
10528                                 } else {
10529                                         rval = EIO;
10530                                 }
10531                                 mutex_enter(SD_MUTEX(un));
10532                         }
10533 
10534                         /*
10535                          * If a device has removable media, invalidate all
10536                          * parameters related to media, such as geometry,
10537                          * blocksize, and blockcount.
10538                          */
10539                         if (un->un_f_has_removable_media) {
10540                                 sr_ejected(un);
10541                         }
10542 
10543                         /*
10544                          * Destroy the cache (if it exists) which was
10545                          * allocated for the write maps since this is
10546                          * the last close for this media.
10547                          */
10548                         if (un->un_wm_cache) {
10549                                 /*
10550                                  * Check if there are pending commands.
10551                                  * and if there are give a warning and
10552                                  * do not destroy the cache.
10553                                  */
10554                                 if (un->un_ncmds_in_driver > 0) {
10555                                         scsi_log(SD_DEVINFO(un),
10556                                             sd_label, CE_WARN,
10557                                             "Unable to clean up memory "
10558                                             "because of pending I/O\n");
10559                                 } else {
10560                                         kmem_cache_destroy(
10561                                             un->un_wm_cache);
10562                                         un->un_wm_cache = NULL;
10563                                 }
10564                         }
10565                 }
10566         }
10567 
10568         mutex_exit(SD_MUTEX(un));
10569         sema_v(&un->un_semoclose);
10570 
10571         if (otyp == OTYP_LYR) {
10572                 mutex_enter(&sd_detach_mutex);
10573                 /*
10574                  * The detach routine may run when the layer count
10575                  * drops to zero.
10576                  */
10577                 un->un_layer_count--;
10578                 mutex_exit(&sd_detach_mutex);
10579         }
10580 
10581         return (rval);
10582 }
10583 
10584 
10585 /*
10586  *    Function: sd_ready_and_valid
10587  *
10588  * Description: Test if device is ready and has a valid geometry.
10589  *
10590  *   Arguments: ssc - sd_ssc_t will contain un
10591  *              un  - driver soft state (unit) structure
10592  *
10593  * Return Code: SD_READY_VALID          ready and valid label
10594  *              SD_NOT_READY_VALID      not ready, no label
10595  *              SD_RESERVED_BY_OTHERS   reservation conflict
10596  *
10597  *     Context: Never called at interrupt context.
10598  */
10599 
10600 static int
10601 sd_ready_and_valid(sd_ssc_t *ssc, int part)
10602 {
10603         struct sd_errstats      *stp;
10604         uint64_t                capacity;
10605         uint_t                  lbasize;
10606         int                     rval = SD_READY_VALID;
10607         char                    name_str[48];
10608         boolean_t               is_valid;
10609         struct sd_lun           *un;
10610         int                     status;
10611 
10612         ASSERT(ssc != NULL);
10613         un = ssc->ssc_un;
10614         ASSERT(un != NULL);
10615         ASSERT(!mutex_owned(SD_MUTEX(un)));
10616 
10617         mutex_enter(SD_MUTEX(un));
10618         /*
10619          * If a device has removable media, we must check if media is
10620          * ready when checking if this device is ready and valid.
10621          */
10622         if (un->un_f_has_removable_media) {
10623                 mutex_exit(SD_MUTEX(un));
10624                 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10625 
10626                 if (status != 0) {
10627                         rval = SD_NOT_READY_VALID;
10628                         mutex_enter(SD_MUTEX(un));
10629 
10630                         /* Ignore all failed status for removalbe media */
10631                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10632 
10633                         goto done;
10634                 }
10635 
10636                 is_valid = SD_IS_VALID_LABEL(un);
10637                 mutex_enter(SD_MUTEX(un));
10638                 if (!is_valid ||
10639                     (un->un_f_blockcount_is_valid == FALSE) ||
10640                     (un->un_f_tgt_blocksize_is_valid == FALSE)) {
10641 
10642                         /* capacity has to be read every open. */
10643                         mutex_exit(SD_MUTEX(un));
10644                         status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
10645                             &lbasize, SD_PATH_DIRECT);
10646 
10647                         if (status != 0) {
10648                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10649 
10650                                 cmlb_invalidate(un->un_cmlbhandle,
10651                                     (void *)SD_PATH_DIRECT);
10652                                 mutex_enter(SD_MUTEX(un));
10653                                 rval = SD_NOT_READY_VALID;
10654 
10655                                 goto done;
10656                         } else {
10657                                 mutex_enter(SD_MUTEX(un));
10658                                 sd_update_block_info(un, lbasize, capacity);
10659                         }
10660                 }
10661 
10662                 /*
10663                  * Check if the media in the device is writable or not.
10664                  */
10665                 if (!is_valid && ISCD(un)) {
10666                         sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
10667                 }
10668 
10669         } else {
10670                 /*
10671                  * Do a test unit ready to clear any unit attention from non-cd
10672                  * devices.
10673                  */
10674                 mutex_exit(SD_MUTEX(un));
10675 
10676                 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10677                 if (status != 0) {
10678                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10679                 }
10680 
10681                 mutex_enter(SD_MUTEX(un));
10682         }
10683 
10684 
10685         /*
10686          * If this is a non 512 block device, allocate space for
10687          * the wmap cache. This is being done here since every time
10688          * a media is changed this routine will be called and the
10689          * block size is a function of media rather than device.
10690          */
10691         if (((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR ||
10692             un->un_f_non_devbsize_supported) &&
10693             un->un_tgt_blocksize != DEV_BSIZE) ||
10694             un->un_f_enable_rmw) {
10695                 if (!(un->un_wm_cache)) {
10696                         (void) snprintf(name_str, sizeof (name_str),
10697                             "%s%d_cache",
10698                             ddi_driver_name(SD_DEVINFO(un)),
10699                             ddi_get_instance(SD_DEVINFO(un)));
10700                         un->un_wm_cache = kmem_cache_create(
10701                             name_str, sizeof (struct sd_w_map),
10702                             8, sd_wm_cache_constructor,
10703                             sd_wm_cache_destructor, NULL,
10704                             (void *)un, NULL, 0);
10705                         if (!(un->un_wm_cache)) {
10706                                 rval = ENOMEM;
10707                                 goto done;
10708                         }
10709                 }
10710         }
10711 
10712         if (un->un_state == SD_STATE_NORMAL) {
10713                 /*
10714                  * If the target is not yet ready here (defined by a TUR
10715                  * failure), invalidate the geometry and print an 'offline'
10716                  * message. This is a legacy message, as the state of the
10717                  * target is not actually changed to SD_STATE_OFFLINE.
10718                  *
10719                  * If the TUR fails for EACCES (Reservation Conflict),
10720                  * SD_RESERVED_BY_OTHERS will be returned to indicate
10721                  * reservation conflict. If the TUR fails for other
10722                  * reasons, SD_NOT_READY_VALID will be returned.
10723                  */
10724                 int err;
10725 
10726                 mutex_exit(SD_MUTEX(un));
10727                 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10728                 mutex_enter(SD_MUTEX(un));
10729 
10730                 if (err != 0) {
10731                         mutex_exit(SD_MUTEX(un));
10732                         cmlb_invalidate(un->un_cmlbhandle,
10733                             (void *)SD_PATH_DIRECT);
10734                         mutex_enter(SD_MUTEX(un));
10735                         if (err == EACCES) {
10736                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10737                                     "reservation conflict\n");
10738                                 rval = SD_RESERVED_BY_OTHERS;
10739                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10740                         } else {
10741                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10742                                     "drive offline\n");
10743                                 rval = SD_NOT_READY_VALID;
10744                                 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
10745                         }
10746                         goto done;
10747                 }
10748         }
10749 
10750         if (un->un_f_format_in_progress == FALSE) {
10751                 mutex_exit(SD_MUTEX(un));
10752 
10753                 (void) cmlb_validate(un->un_cmlbhandle, 0,
10754                     (void *)SD_PATH_DIRECT);
10755                 if (cmlb_partinfo(un->un_cmlbhandle, part, NULL, NULL, NULL,
10756                     NULL, (void *) SD_PATH_DIRECT) != 0) {
10757                         rval = SD_NOT_READY_VALID;
10758                         mutex_enter(SD_MUTEX(un));
10759 
10760                         goto done;
10761                 }
10762                 if (un->un_f_pkstats_enabled) {
10763                         sd_set_pstats(un);
10764                         SD_TRACE(SD_LOG_IO_PARTITION, un,
10765                             "sd_ready_and_valid: un:0x%p pstats created and "
10766                             "set\n", un);
10767                 }
10768                 mutex_enter(SD_MUTEX(un));
10769         }
10770 
10771         /*
10772          * If this device supports DOOR_LOCK command, try and send
10773          * this command to PREVENT MEDIA REMOVAL, but don't get upset
10774          * if it fails. For a CD, however, it is an error
10775          */
10776         if (un->un_f_doorlock_supported) {
10777                 mutex_exit(SD_MUTEX(un));
10778                 status = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
10779                     SD_PATH_DIRECT);
10780 
10781                 if ((status != 0) && ISCD(un)) {
10782                         rval = SD_NOT_READY_VALID;
10783                         mutex_enter(SD_MUTEX(un));
10784 
10785                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10786 
10787                         goto done;
10788                 } else if (status != 0)
10789                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10790                 mutex_enter(SD_MUTEX(un));
10791         }
10792 
10793         /* The state has changed, inform the media watch routines */
10794         un->un_mediastate = DKIO_INSERTED;
10795         cv_broadcast(&un->un_state_cv);
10796         rval = SD_READY_VALID;
10797 
10798 done:
10799 
10800         /*
10801          * Initialize the capacity kstat value, if no media previously
10802          * (capacity kstat is 0) and a media has been inserted
10803          * (un_blockcount > 0).
10804          */
10805         if (un->un_errstats != NULL) {
10806                 stp = (struct sd_errstats *)un->un_errstats->ks_data;
10807                 if ((stp->sd_capacity.value.ui64 == 0) &&
10808                     (un->un_f_blockcount_is_valid == TRUE)) {
10809                         stp->sd_capacity.value.ui64 =
10810                             (uint64_t)((uint64_t)un->un_blockcount *
10811                             un->un_sys_blocksize);
10812                 }
10813         }
10814 
10815         mutex_exit(SD_MUTEX(un));
10816         return (rval);
10817 }
10818 
10819 
10820 /*
10821  *    Function: sdmin
10822  *
10823  * Description: Routine to limit the size of a data transfer. Used in
10824  *              conjunction with physio(9F).
10825  *
10826  *   Arguments: bp - pointer to the indicated buf(9S) struct.
10827  *
10828  *     Context: Kernel thread context.
10829  */
10830 
10831 static void
10832 sdmin(struct buf *bp)
10833 {
10834         struct sd_lun   *un;
10835         int             instance;
10836 
10837         instance = SDUNIT(bp->b_edev);
10838 
10839         un = ddi_get_soft_state(sd_state, instance);
10840         ASSERT(un != NULL);
10841 
10842         /*
10843          * We depend on buf breakup to restrict
10844          * IO size if it is enabled.
10845          */
10846         if (un->un_buf_breakup_supported) {
10847                 return;
10848         }
10849 
10850         if (bp->b_bcount > un->un_max_xfer_size) {
10851                 bp->b_bcount = un->un_max_xfer_size;
10852         }
10853 }
10854 
10855 
10856 /*
10857  *    Function: sdread
10858  *
10859  * Description: Driver's read(9e) entry point function.
10860  *
10861  *   Arguments: dev   - device number
10862  *              uio   - structure pointer describing where data is to be stored
10863  *                      in user's space
10864  *              cred_p  - user credential pointer
10865  *
10866  * Return Code: ENXIO
10867  *              EIO
10868  *              EINVAL
10869  *              value returned by physio
10870  *
10871  *     Context: Kernel thread context.
10872  */
10873 /* ARGSUSED */
10874 static int
10875 sdread(dev_t dev, struct uio *uio, cred_t *cred_p)
10876 {
10877         struct sd_lun   *un = NULL;
10878         int             secmask;
10879         int             err = 0;
10880         sd_ssc_t        *ssc;
10881 
10882         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10883                 return (ENXIO);
10884         }
10885 
10886         ASSERT(!mutex_owned(SD_MUTEX(un)));
10887 
10888 
10889         if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10890                 mutex_enter(SD_MUTEX(un));
10891                 /*
10892                  * Because the call to sd_ready_and_valid will issue I/O we
10893                  * must wait here if either the device is suspended or
10894                  * if it's power level is changing.
10895                  */
10896                 while ((un->un_state == SD_STATE_SUSPENDED) ||
10897                     (un->un_state == SD_STATE_PM_CHANGING)) {
10898                         cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10899                 }
10900                 un->un_ncmds_in_driver++;
10901                 mutex_exit(SD_MUTEX(un));
10902 
10903                 /* Initialize sd_ssc_t for internal uscsi commands */
10904                 ssc = sd_ssc_init(un);
10905                 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10906                         err = EIO;
10907                 } else {
10908                         err = 0;
10909                 }
10910                 sd_ssc_fini(ssc);
10911 
10912                 mutex_enter(SD_MUTEX(un));
10913                 un->un_ncmds_in_driver--;
10914                 ASSERT(un->un_ncmds_in_driver >= 0);
10915                 mutex_exit(SD_MUTEX(un));
10916                 if (err != 0)
10917                         return (err);
10918         }
10919 
10920         /*
10921          * Read requests are restricted to multiples of the system block size.
10922          */
10923         if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
10924             !un->un_f_enable_rmw)
10925                 secmask = un->un_tgt_blocksize - 1;
10926         else
10927                 secmask = DEV_BSIZE - 1;
10928 
10929         if (uio->uio_loffset & ((offset_t)(secmask))) {
10930                 SD_ERROR(SD_LOG_READ_WRITE, un,
10931                     "sdread: file offset not modulo %d\n",
10932                     secmask + 1);
10933                 err = EINVAL;
10934         } else if (uio->uio_iov->iov_len & (secmask)) {
10935                 SD_ERROR(SD_LOG_READ_WRITE, un,
10936                     "sdread: transfer length not modulo %d\n",
10937                     secmask + 1);
10938                 err = EINVAL;
10939         } else {
10940                 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio);
10941         }
10942 
10943         return (err);
10944 }
10945 
10946 
10947 /*
10948  *    Function: sdwrite
10949  *
10950  * Description: Driver's write(9e) entry point function.
10951  *
10952  *   Arguments: dev   - device number
10953  *              uio   - structure pointer describing where data is stored in
10954  *                      user's space
10955  *              cred_p  - user credential pointer
10956  *
10957  * Return Code: ENXIO
10958  *              EIO
10959  *              EINVAL
10960  *              value returned by physio
10961  *
10962  *     Context: Kernel thread context.
10963  */
10964 /* ARGSUSED */
10965 static int
10966 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p)
10967 {
10968         struct sd_lun   *un = NULL;
10969         int             secmask;
10970         int             err = 0;
10971         sd_ssc_t        *ssc;
10972 
10973         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10974                 return (ENXIO);
10975         }
10976 
10977         ASSERT(!mutex_owned(SD_MUTEX(un)));
10978 
10979         if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10980                 mutex_enter(SD_MUTEX(un));
10981                 /*
10982                  * Because the call to sd_ready_and_valid will issue I/O we
10983                  * must wait here if either the device is suspended or
10984                  * if it's power level is changing.
10985                  */
10986                 while ((un->un_state == SD_STATE_SUSPENDED) ||
10987                     (un->un_state == SD_STATE_PM_CHANGING)) {
10988                         cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10989                 }
10990                 un->un_ncmds_in_driver++;
10991                 mutex_exit(SD_MUTEX(un));
10992 
10993                 /* Initialize sd_ssc_t for internal uscsi commands */
10994                 ssc = sd_ssc_init(un);
10995                 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10996                         err = EIO;
10997                 } else {
10998                         err = 0;
10999                 }
11000                 sd_ssc_fini(ssc);
11001 
11002                 mutex_enter(SD_MUTEX(un));
11003                 un->un_ncmds_in_driver--;
11004                 ASSERT(un->un_ncmds_in_driver >= 0);
11005                 mutex_exit(SD_MUTEX(un));
11006                 if (err != 0)
11007                         return (err);
11008         }
11009 
11010         /*
11011          * Write requests are restricted to multiples of the system block size.
11012          */
11013         if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11014             !un->un_f_enable_rmw)
11015                 secmask = un->un_tgt_blocksize - 1;
11016         else
11017                 secmask = DEV_BSIZE - 1;
11018 
11019         if (uio->uio_loffset & ((offset_t)(secmask))) {
11020                 SD_ERROR(SD_LOG_READ_WRITE, un,
11021                     "sdwrite: file offset not modulo %d\n",
11022                     secmask + 1);
11023                 err = EINVAL;
11024         } else if (uio->uio_iov->iov_len & (secmask)) {
11025                 SD_ERROR(SD_LOG_READ_WRITE, un,
11026                     "sdwrite: transfer length not modulo %d\n",
11027                     secmask + 1);
11028                 err = EINVAL;
11029         } else {
11030                 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio);
11031         }
11032 
11033         return (err);
11034 }
11035 
11036 
11037 /*
11038  *    Function: sdaread
11039  *
11040  * Description: Driver's aread(9e) entry point function.
11041  *
11042  *   Arguments: dev   - device number
11043  *              aio   - structure pointer describing where data is to be stored
11044  *              cred_p  - user credential pointer
11045  *
11046  * Return Code: ENXIO
11047  *              EIO
11048  *              EINVAL
11049  *              value returned by aphysio
11050  *
11051  *     Context: Kernel thread context.
11052  */
11053 /* ARGSUSED */
11054 static int
11055 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11056 {
11057         struct sd_lun   *un = NULL;
11058         struct uio      *uio = aio->aio_uio;
11059         int             secmask;
11060         int             err = 0;
11061         sd_ssc_t        *ssc;
11062 
11063         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11064                 return (ENXIO);
11065         }
11066 
11067         ASSERT(!mutex_owned(SD_MUTEX(un)));
11068 
11069         if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11070                 mutex_enter(SD_MUTEX(un));
11071                 /*
11072                  * Because the call to sd_ready_and_valid will issue I/O we
11073                  * must wait here if either the device is suspended or
11074                  * if it's power level is changing.
11075                  */
11076                 while ((un->un_state == SD_STATE_SUSPENDED) ||
11077                     (un->un_state == SD_STATE_PM_CHANGING)) {
11078                         cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11079                 }
11080                 un->un_ncmds_in_driver++;
11081                 mutex_exit(SD_MUTEX(un));
11082 
11083                 /* Initialize sd_ssc_t for internal uscsi commands */
11084                 ssc = sd_ssc_init(un);
11085                 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11086                         err = EIO;
11087                 } else {
11088                         err = 0;
11089                 }
11090                 sd_ssc_fini(ssc);
11091 
11092                 mutex_enter(SD_MUTEX(un));
11093                 un->un_ncmds_in_driver--;
11094                 ASSERT(un->un_ncmds_in_driver >= 0);
11095                 mutex_exit(SD_MUTEX(un));
11096                 if (err != 0)
11097                         return (err);
11098         }
11099 
11100         /*
11101          * Read requests are restricted to multiples of the system block size.
11102          */
11103         if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11104             !un->un_f_enable_rmw)
11105                 secmask = un->un_tgt_blocksize - 1;
11106         else
11107                 secmask = DEV_BSIZE - 1;
11108 
11109         if (uio->uio_loffset & ((offset_t)(secmask))) {
11110                 SD_ERROR(SD_LOG_READ_WRITE, un,
11111                     "sdaread: file offset not modulo %d\n",
11112                     secmask + 1);
11113                 err = EINVAL;
11114         } else if (uio->uio_iov->iov_len & (secmask)) {
11115                 SD_ERROR(SD_LOG_READ_WRITE, un,
11116                     "sdaread: transfer length not modulo %d\n",
11117                     secmask + 1);
11118                 err = EINVAL;
11119         } else {
11120                 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio);
11121         }
11122 
11123         return (err);
11124 }
11125 
11126 
11127 /*
11128  *    Function: sdawrite
11129  *
11130  * Description: Driver's awrite(9e) entry point function.
11131  *
11132  *   Arguments: dev   - device number
11133  *              aio   - structure pointer describing where data is stored
11134  *              cred_p  - user credential pointer
11135  *
11136  * Return Code: ENXIO
11137  *              EIO
11138  *              EINVAL
11139  *              value returned by aphysio
11140  *
11141  *     Context: Kernel thread context.
11142  */
11143 /* ARGSUSED */
11144 static int
11145 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11146 {
11147         struct sd_lun   *un = NULL;
11148         struct uio      *uio = aio->aio_uio;
11149         int             secmask;
11150         int             err = 0;
11151         sd_ssc_t        *ssc;
11152 
11153         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11154                 return (ENXIO);
11155         }
11156 
11157         ASSERT(!mutex_owned(SD_MUTEX(un)));
11158 
11159         if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11160                 mutex_enter(SD_MUTEX(un));
11161                 /*
11162                  * Because the call to sd_ready_and_valid will issue I/O we
11163                  * must wait here if either the device is suspended or
11164                  * if it's power level is changing.
11165                  */
11166                 while ((un->un_state == SD_STATE_SUSPENDED) ||
11167                     (un->un_state == SD_STATE_PM_CHANGING)) {
11168                         cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11169                 }
11170                 un->un_ncmds_in_driver++;
11171                 mutex_exit(SD_MUTEX(un));
11172 
11173                 /* Initialize sd_ssc_t for internal uscsi commands */
11174                 ssc = sd_ssc_init(un);
11175                 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11176                         err = EIO;
11177                 } else {
11178                         err = 0;
11179                 }
11180                 sd_ssc_fini(ssc);
11181 
11182                 mutex_enter(SD_MUTEX(un));
11183                 un->un_ncmds_in_driver--;
11184                 ASSERT(un->un_ncmds_in_driver >= 0);
11185                 mutex_exit(SD_MUTEX(un));
11186                 if (err != 0)
11187                         return (err);
11188         }
11189 
11190         /*
11191          * Write requests are restricted to multiples of the system block size.
11192          */
11193         if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11194             !un->un_f_enable_rmw)
11195                 secmask = un->un_tgt_blocksize - 1;
11196         else
11197                 secmask = DEV_BSIZE - 1;
11198 
11199         if (uio->uio_loffset & ((offset_t)(secmask))) {
11200                 SD_ERROR(SD_LOG_READ_WRITE, un,
11201                     "sdawrite: file offset not modulo %d\n",
11202                     secmask + 1);
11203                 err = EINVAL;
11204         } else if (uio->uio_iov->iov_len & (secmask)) {
11205                 SD_ERROR(SD_LOG_READ_WRITE, un,
11206                     "sdawrite: transfer length not modulo %d\n",
11207                     secmask + 1);
11208                 err = EINVAL;
11209         } else {
11210                 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio);
11211         }
11212 
11213         return (err);
11214 }
11215 
11216 
11217 
11218 
11219 
11220 /*
11221  * Driver IO processing follows the following sequence:
11222  *
11223  *     sdioctl(9E)     sdstrategy(9E)         biodone(9F)
11224  *         |                |                     ^
11225  *         v                v                     |
11226  * sd_send_scsi_cmd()  ddi_xbuf_qstrategy()       +-------------------+
11227  *         |                |                     |                   |
11228  *         v                |                     |                   |
11229  * sd_uscsi_strategy() sd_xbuf_strategy()   sd_buf_iodone()   sd_uscsi_iodone()
11230  *         |                |                     ^                   ^
11231  *         v                v                     |                   |
11232  * SD_BEGIN_IOSTART()  SD_BEGIN_IOSTART()         |                   |
11233  *         |                |                     |                   |
11234  *     +---+                |                     +------------+      +-------+
11235  *     |                    |                                  |              |
11236  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11237  *     |                    v                                  |              |
11238  *     |         sd_mapblockaddr_iostart()           sd_mapblockaddr_iodone() |
11239  *     |                    |                                  ^              |
11240  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11241  *     |                    v                                  |              |
11242  *     |         sd_mapblocksize_iostart()           sd_mapblocksize_iodone() |
11243  *     |                    |                                  ^              |
11244  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11245  *     |                    v                                  |              |
11246  *     |           sd_checksum_iostart()               sd_checksum_iodone()   |
11247  *     |                    |                                  ^              |
11248  *     +-> SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()+------------->+
11249  *     |                    v                                  |              |
11250  *     |              sd_pm_iostart()                     sd_pm_iodone()      |
11251  *     |                    |                                  ^              |
11252  *     |                    |                                  |              |
11253  *     +-> SD_NEXT_IOSTART()|               SD_BEGIN_IODONE()--+--------------+
11254  *                          |                           ^
11255  *                          v                           |
11256  *                   sd_core_iostart()                  |
11257  *                          |                           |
11258  *                          |                           +------>(*destroypkt)()
11259  *                          +-> sd_start_cmds() <-+     |           |
11260  *                          |                     |     |           v
11261  *                          |                     |     |  scsi_destroy_pkt(9F)
11262  *                          |                     |     |
11263  *                          +->(*initpkt)()       +- sdintr()
11264  *                          |  |                        |  |
11265  *                          |  +-> scsi_init_pkt(9F)    |  +-> sd_handle_xxx()
11266  *                          |  +-> scsi_setup_cdb(9F)   |
11267  *                          |                           |
11268  *                          +--> scsi_transport(9F)     |
11269  *                                     |                |
11270  *                                     +----> SCSA ---->+
11271  *
11272  *
11273  * This code is based upon the following presumptions:
11274  *
11275  *   - iostart and iodone functions operate on buf(9S) structures. These
11276  *     functions perform the necessary operations on the buf(9S) and pass
11277  *     them along to the next function in the chain by using the macros
11278  *     SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE()
11279  *     (for iodone side functions).
11280  *
11281  *   - The iostart side functions may sleep. The iodone side functions
11282  *     are called under interrupt context and may NOT sleep. Therefore
11283  *     iodone side functions also may not call iostart side functions.
11284  *     (NOTE: iostart side functions should NOT sleep for memory, as
11285  *     this could result in deadlock.)
11286  *
11287  *   - An iostart side function may call its corresponding iodone side
11288  *     function directly (if necessary).
11289  *
11290  *   - In the event of an error, an iostart side function can return a buf(9S)
11291  *     to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and
11292  *     b_error in the usual way of course).
11293  *
11294  *   - The taskq mechanism may be used by the iodone side functions to dispatch
11295  *     requests to the iostart side functions.  The iostart side functions in
11296  *     this case would be called under the context of a taskq thread, so it's
11297  *     OK for them to block/sleep/spin in this case.
11298  *
11299  *   - iostart side functions may allocate "shadow" buf(9S) structs and
11300  *     pass them along to the next function in the chain.  The corresponding
11301  *     iodone side functions must coalesce the "shadow" bufs and return
11302  *     the "original" buf to the next higher layer.
11303  *
11304  *   - The b_private field of the buf(9S) struct holds a pointer to
11305  *     an sd_xbuf struct, which contains information needed to
11306  *     construct the scsi_pkt for the command.
11307  *
11308  *   - The SD_MUTEX(un) is NOT held across calls to the next layer. Each
11309  *     layer must acquire & release the SD_MUTEX(un) as needed.
11310  */
11311 
11312 
11313 /*
11314  * Create taskq for all targets in the system. This is created at
11315  * _init(9E) and destroyed at _fini(9E).
11316  *
11317  * Note: here we set the minalloc to a reasonably high number to ensure that
11318  * we will have an adequate supply of task entries available at interrupt time.
11319  * This is used in conjunction with the TASKQ_PREPOPULATE flag in
11320  * sd_create_taskq().  Since we do not want to sleep for allocations at
11321  * interrupt time, set maxalloc equal to minalloc. That way we will just fail
11322  * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq
11323  * requests any one instant in time.
11324  */
11325 #define SD_TASKQ_NUMTHREADS     8
11326 #define SD_TASKQ_MINALLOC       256
11327 #define SD_TASKQ_MAXALLOC       256
11328 
11329 static taskq_t  *sd_tq = NULL;
11330 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq))
11331 
11332 static int      sd_taskq_minalloc = SD_TASKQ_MINALLOC;
11333 static int      sd_taskq_maxalloc = SD_TASKQ_MAXALLOC;
11334 
11335 /*
11336  * The following task queue is being created for the write part of
11337  * read-modify-write of non-512 block size devices.
11338  * Limit the number of threads to 1 for now. This number has been chosen
11339  * considering the fact that it applies only to dvd ram drives/MO drives
11340  * currently. Performance for which is not main criteria at this stage.
11341  * Note: It needs to be explored if we can use a single taskq in future
11342  */
11343 #define SD_WMR_TASKQ_NUMTHREADS 1
11344 static taskq_t  *sd_wmr_tq = NULL;
11345 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq))
11346 
11347 /*
11348  *    Function: sd_taskq_create
11349  *
11350  * Description: Create taskq thread(s) and preallocate task entries
11351  *
11352  * Return Code: Returns a pointer to the allocated taskq_t.
11353  *
11354  *     Context: Can sleep. Requires blockable context.
11355  *
11356  *       Notes: - The taskq() facility currently is NOT part of the DDI.
11357  *                (definitely NOT recommeded for 3rd-party drivers!) :-)
11358  *              - taskq_create() will block for memory, also it will panic
11359  *                if it cannot create the requested number of threads.
11360  *              - Currently taskq_create() creates threads that cannot be
11361  *                swapped.
11362  *              - We use TASKQ_PREPOPULATE to ensure we have an adequate
11363  *                supply of taskq entries at interrupt time (ie, so that we
11364  *                do not have to sleep for memory)
11365  */
11366 
11367 static void
11368 sd_taskq_create(void)
11369 {
11370         char    taskq_name[TASKQ_NAMELEN];
11371 
11372         ASSERT(sd_tq == NULL);
11373         ASSERT(sd_wmr_tq == NULL);
11374 
11375         (void) snprintf(taskq_name, sizeof (taskq_name),
11376             "%s_drv_taskq", sd_label);
11377         sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS,
11378             (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11379             TASKQ_PREPOPULATE));
11380 
11381         (void) snprintf(taskq_name, sizeof (taskq_name),
11382             "%s_rmw_taskq", sd_label);
11383         sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS,
11384             (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11385             TASKQ_PREPOPULATE));
11386 }
11387 
11388 
11389 /*
11390  *    Function: sd_taskq_delete
11391  *
11392  * Description: Complementary cleanup routine for sd_taskq_create().
11393  *
11394  *     Context: Kernel thread context.
11395  */
11396 
11397 static void
11398 sd_taskq_delete(void)
11399 {
11400         ASSERT(sd_tq != NULL);
11401         ASSERT(sd_wmr_tq != NULL);
11402         taskq_destroy(sd_tq);
11403         taskq_destroy(sd_wmr_tq);
11404         sd_tq = NULL;
11405         sd_wmr_tq = NULL;
11406 }
11407 
11408 
11409 /*
11410  *    Function: sdstrategy
11411  *
11412  * Description: Driver's strategy (9E) entry point function.
11413  *
11414  *   Arguments: bp - pointer to buf(9S)
11415  *
11416  * Return Code: Always returns zero
11417  *
11418  *     Context: Kernel thread context.
11419  */
11420 
11421 static int
11422 sdstrategy(struct buf *bp)
11423 {
11424         struct sd_lun *un;
11425 
11426         un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11427         if (un == NULL) {
11428                 bioerror(bp, EIO);
11429                 bp->b_resid = bp->b_bcount;
11430                 biodone(bp);
11431                 return (0);
11432         }
11433 
11434         /* As was done in the past, fail new cmds. if state is dumping. */
11435         if (un->un_state == SD_STATE_DUMPING) {
11436                 bioerror(bp, ENXIO);
11437                 bp->b_resid = bp->b_bcount;
11438                 biodone(bp);
11439                 return (0);
11440         }
11441 
11442         ASSERT(!mutex_owned(SD_MUTEX(un)));
11443 
11444         /*
11445          * Commands may sneak in while we released the mutex in
11446          * DDI_SUSPEND, we should block new commands. However, old
11447          * commands that are still in the driver at this point should
11448          * still be allowed to drain.
11449          */
11450         mutex_enter(SD_MUTEX(un));
11451         /*
11452          * Must wait here if either the device is suspended or
11453          * if it's power level is changing.
11454          */
11455         while ((un->un_state == SD_STATE_SUSPENDED) ||
11456             (un->un_state == SD_STATE_PM_CHANGING)) {
11457                 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11458         }
11459 
11460         un->un_ncmds_in_driver++;
11461 
11462         /*
11463          * atapi: Since we are running the CD for now in PIO mode we need to
11464          * call bp_mapin here to avoid bp_mapin called interrupt context under
11465          * the HBA's init_pkt routine.
11466          */
11467         if (un->un_f_cfg_is_atapi == TRUE) {
11468                 mutex_exit(SD_MUTEX(un));
11469                 bp_mapin(bp);
11470                 mutex_enter(SD_MUTEX(un));
11471         }
11472         SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n",
11473             un->un_ncmds_in_driver);
11474 
11475         if (bp->b_flags & B_WRITE)
11476                 un->un_f_sync_cache_required = TRUE;
11477 
11478         mutex_exit(SD_MUTEX(un));
11479 
11480         /*
11481          * This will (eventually) allocate the sd_xbuf area and
11482          * call sd_xbuf_strategy().  We just want to return the
11483          * result of ddi_xbuf_qstrategy so that we have an opt-
11484          * imized tail call which saves us a stack frame.
11485          */
11486         return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr));
11487 }
11488 
11489 
11490 /*
11491  *    Function: sd_xbuf_strategy
11492  *
11493  * Description: Function for initiating IO operations via the
11494  *              ddi_xbuf_qstrategy() mechanism.
11495  *
11496  *     Context: Kernel thread context.
11497  */
11498 
11499 static void
11500 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg)
11501 {
11502         struct sd_lun *un = arg;
11503 
11504         ASSERT(bp != NULL);
11505         ASSERT(xp != NULL);
11506         ASSERT(un != NULL);
11507         ASSERT(!mutex_owned(SD_MUTEX(un)));
11508 
11509         /*
11510          * Initialize the fields in the xbuf and save a pointer to the
11511          * xbuf in bp->b_private.
11512          */
11513         sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL);
11514 
11515         /* Send the buf down the iostart chain */
11516         SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp);
11517 }
11518 
11519 
11520 /*
11521  *    Function: sd_xbuf_init
11522  *
11523  * Description: Prepare the given sd_xbuf struct for use.
11524  *
11525  *   Arguments: un - ptr to softstate
11526  *              bp - ptr to associated buf(9S)
11527  *              xp - ptr to associated sd_xbuf
11528  *              chain_type - IO chain type to use:
11529  *                      SD_CHAIN_NULL
11530  *                      SD_CHAIN_BUFIO
11531  *                      SD_CHAIN_USCSI
11532  *                      SD_CHAIN_DIRECT
11533  *                      SD_CHAIN_DIRECT_PRIORITY
11534  *              pktinfop - ptr to private data struct for scsi_pkt(9S)
11535  *                      initialization; may be NULL if none.
11536  *
11537  *     Context: Kernel thread context
11538  */
11539 
11540 static void
11541 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
11542     uchar_t chain_type, void *pktinfop)
11543 {
11544         int index;
11545 
11546         ASSERT(un != NULL);
11547         ASSERT(bp != NULL);
11548         ASSERT(xp != NULL);
11549 
11550         SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n",
11551             bp, chain_type);
11552 
11553         xp->xb_un    = un;
11554         xp->xb_pktp  = NULL;
11555         xp->xb_pktinfo       = pktinfop;
11556         xp->xb_private       = bp->b_private;
11557         xp->xb_blkno = (daddr_t)bp->b_blkno;
11558 
11559         /*
11560          * Set up the iostart and iodone chain indexes in the xbuf, based
11561          * upon the specified chain type to use.
11562          */
11563         switch (chain_type) {
11564         case SD_CHAIN_NULL:
11565                 /*
11566                  * Fall thru to just use the values for the buf type, even
11567                  * tho for the NULL chain these values will never be used.
11568                  */
11569                 /* FALLTHRU */
11570         case SD_CHAIN_BUFIO:
11571                 index = un->un_buf_chain_type;
11572                 if ((!un->un_f_has_removable_media) &&
11573                     (un->un_tgt_blocksize != 0) &&
11574                     (un->un_tgt_blocksize != DEV_BSIZE ||
11575                     un->un_f_enable_rmw)) {
11576                         int secmask = 0, blknomask = 0;
11577                         if (un->un_f_enable_rmw) {
11578                                 blknomask =
11579                                     (un->un_phy_blocksize / DEV_BSIZE) - 1;
11580                                 secmask = un->un_phy_blocksize - 1;
11581                         } else {
11582                                 blknomask =
11583                                     (un->un_tgt_blocksize / DEV_BSIZE) - 1;
11584                                 secmask = un->un_tgt_blocksize - 1;
11585                         }
11586 
11587                         if ((bp->b_lblkno & (blknomask)) ||
11588                             (bp->b_bcount & (secmask))) {
11589                                 if ((un->un_f_rmw_type !=
11590                                     SD_RMW_TYPE_RETURN_ERROR) ||
11591                                     un->un_f_enable_rmw) {
11592                                         if (un->un_f_pm_is_enabled == FALSE)
11593                                                 index =
11594                                                     SD_CHAIN_INFO_MSS_DSK_NO_PM;
11595                                         else
11596                                                 index =
11597                                                     SD_CHAIN_INFO_MSS_DISK;
11598                                 }
11599                         }
11600                 }
11601                 break;
11602         case SD_CHAIN_USCSI:
11603                 index = un->un_uscsi_chain_type;
11604                 break;
11605         case SD_CHAIN_DIRECT:
11606                 index = un->un_direct_chain_type;
11607                 break;
11608         case SD_CHAIN_DIRECT_PRIORITY:
11609                 index = un->un_priority_chain_type;
11610                 break;
11611         default:
11612                 /* We're really broken if we ever get here... */
11613                 panic("sd_xbuf_init: illegal chain type!");
11614                 /*NOTREACHED*/
11615         }
11616 
11617         xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index;
11618         xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index;
11619 
11620         /*
11621          * It might be a bit easier to simply bzero the entire xbuf above,
11622          * but it turns out that since we init a fair number of members anyway,
11623          * we save a fair number cycles by doing explicit assignment of zero.
11624          */
11625         xp->xb_pkt_flags     = 0;
11626         xp->xb_dma_resid     = 0;
11627         xp->xb_retry_count   = 0;
11628         xp->xb_victim_retry_count = 0;
11629         xp->xb_ua_retry_count        = 0;
11630         xp->xb_nr_retry_count        = 0;
11631         xp->xb_sense_bp              = NULL;
11632         xp->xb_sense_status  = 0;
11633         xp->xb_sense_state   = 0;
11634         xp->xb_sense_resid   = 0;
11635         xp->xb_ena           = 0;
11636 
11637         bp->b_private        = xp;
11638         bp->b_flags  &= ~(B_DONE | B_ERROR);
11639         bp->b_resid  = 0;
11640         bp->av_forw  = NULL;
11641         bp->av_back  = NULL;
11642         bioerror(bp, 0);
11643 
11644         SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n");
11645 }
11646 
11647 
11648 /*
11649  *    Function: sd_uscsi_strategy
11650  *
11651  * Description: Wrapper for calling into the USCSI chain via physio(9F)
11652  *
11653  *   Arguments: bp - buf struct ptr
11654  *
11655  * Return Code: Always returns 0
11656  *
11657  *     Context: Kernel thread context
11658  */
11659 
11660 static int
11661 sd_uscsi_strategy(struct buf *bp)
11662 {
11663         struct sd_lun           *un;
11664         struct sd_uscsi_info    *uip;
11665         struct sd_xbuf          *xp;
11666         uchar_t                 chain_type;
11667         uchar_t                 cmd;
11668 
11669         ASSERT(bp != NULL);
11670 
11671         un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11672         if (un == NULL) {
11673                 bioerror(bp, EIO);
11674                 bp->b_resid = bp->b_bcount;
11675                 biodone(bp);
11676                 return (0);
11677         }
11678 
11679         ASSERT(!mutex_owned(SD_MUTEX(un)));
11680 
11681         SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp);
11682 
11683         /*
11684          * A pointer to a struct sd_uscsi_info is expected in bp->b_private
11685          */
11686         ASSERT(bp->b_private != NULL);
11687         uip = (struct sd_uscsi_info *)bp->b_private;
11688         cmd = ((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_cdb[0];
11689 
11690         mutex_enter(SD_MUTEX(un));
11691         /*
11692          * atapi: Since we are running the CD for now in PIO mode we need to
11693          * call bp_mapin here to avoid bp_mapin called interrupt context under
11694          * the HBA's init_pkt routine.
11695          */
11696         if (un->un_f_cfg_is_atapi == TRUE) {
11697                 mutex_exit(SD_MUTEX(un));
11698                 bp_mapin(bp);
11699                 mutex_enter(SD_MUTEX(un));
11700         }
11701         un->un_ncmds_in_driver++;
11702         SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n",
11703             un->un_ncmds_in_driver);
11704 
11705         if ((bp->b_flags & B_WRITE) && (bp->b_bcount != 0) &&
11706             (cmd != SCMD_MODE_SELECT) && (cmd != SCMD_MODE_SELECT_G1))
11707                 un->un_f_sync_cache_required = TRUE;
11708 
11709         mutex_exit(SD_MUTEX(un));
11710 
11711         switch (uip->ui_flags) {
11712         case SD_PATH_DIRECT:
11713                 chain_type = SD_CHAIN_DIRECT;
11714                 break;
11715         case SD_PATH_DIRECT_PRIORITY:
11716                 chain_type = SD_CHAIN_DIRECT_PRIORITY;
11717                 break;
11718         default:
11719                 chain_type = SD_CHAIN_USCSI;
11720                 break;
11721         }
11722 
11723         /*
11724          * We may allocate extra buf for external USCSI commands. If the
11725          * application asks for bigger than 20-byte sense data via USCSI,
11726          * SCSA layer will allocate 252 bytes sense buf for that command.
11727          */
11728         if (((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_rqlen >
11729             SENSE_LENGTH) {
11730                 xp = kmem_zalloc(sizeof (struct sd_xbuf) - SENSE_LENGTH +
11731                     MAX_SENSE_LENGTH, KM_SLEEP);
11732         } else {
11733                 xp = kmem_zalloc(sizeof (struct sd_xbuf), KM_SLEEP);
11734         }
11735 
11736         sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp);
11737 
11738         /* Use the index obtained within xbuf_init */
11739         SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp);
11740 
11741         SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp);
11742 
11743         return (0);
11744 }
11745 
11746 /*
11747  *    Function: sd_send_scsi_cmd
11748  *
11749  * Description: Runs a USCSI command for user (when called thru sdioctl),
11750  *              or for the driver
11751  *
11752  *   Arguments: dev - the dev_t for the device
11753  *              incmd - ptr to a valid uscsi_cmd struct
11754  *              flag - bit flag, indicating open settings, 32/64 bit type
11755  *              dataspace - UIO_USERSPACE or UIO_SYSSPACE
11756  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11757  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11758  *                      to use the USCSI "direct" chain and bypass the normal
11759  *                      command waitq.
11760  *
11761  * Return Code: 0 -  successful completion of the given command
11762  *              EIO - scsi_uscsi_handle_command() failed
11763  *              ENXIO  - soft state not found for specified dev
11764  *              EINVAL
11765  *              EFAULT - copyin/copyout error
11766  *              return code of scsi_uscsi_handle_command():
11767  *                      EIO
11768  *                      ENXIO
11769  *                      EACCES
11770  *
11771  *     Context: Waits for command to complete. Can sleep.
11772  */
11773 
11774 static int
11775 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
11776     enum uio_seg dataspace, int path_flag)
11777 {
11778         struct sd_lun   *un;
11779         sd_ssc_t        *ssc;
11780         int             rval;
11781 
11782         un = ddi_get_soft_state(sd_state, SDUNIT(dev));
11783         if (un == NULL) {
11784                 return (ENXIO);
11785         }
11786 
11787         /*
11788          * Using sd_ssc_send to handle uscsi cmd
11789          */
11790         ssc = sd_ssc_init(un);
11791         rval = sd_ssc_send(ssc, incmd, flag, dataspace, path_flag);
11792         sd_ssc_fini(ssc);
11793 
11794         return (rval);
11795 }
11796 
11797 /*
11798  *    Function: sd_ssc_init
11799  *
11800  * Description: Uscsi end-user call this function to initialize necessary
11801  *              fields, such as uscsi_cmd and sd_uscsi_info struct.
11802  *
11803  *              The return value of sd_send_scsi_cmd will be treated as a
11804  *              fault in various conditions. Even it is not Zero, some
11805  *              callers may ignore the return value. That is to say, we can
11806  *              not make an accurate assessment in sdintr, since if a
11807  *              command is failed in sdintr it does not mean the caller of
11808  *              sd_send_scsi_cmd will treat it as a real failure.
11809  *
11810  *              To avoid printing too many error logs for a failed uscsi
11811  *              packet that the caller may not treat it as a failure, the
11812  *              sd will keep silent for handling all uscsi commands.
11813  *
11814  *              During detach->attach and attach-open, for some types of
11815  *              problems, the driver should be providing information about
11816  *              the problem encountered. Device use USCSI_SILENT, which
11817  *              suppresses all driver information. The result is that no
11818  *              information about the problem is available. Being
11819  *              completely silent during this time is inappropriate. The
11820  *              driver needs a more selective filter than USCSI_SILENT, so
11821  *              that information related to faults is provided.
11822  *
11823  *              To make the accurate accessment, the caller  of
11824  *              sd_send_scsi_USCSI_CMD should take the ownership and
11825  *              get necessary information to print error messages.
11826  *
11827  *              If we want to print necessary info of uscsi command, we need to
11828  *              keep the uscsi_cmd and sd_uscsi_info till we can make the
11829  *              assessment. We use sd_ssc_init to alloc necessary
11830  *              structs for sending an uscsi command and we are also
11831  *              responsible for free the memory by calling
11832  *              sd_ssc_fini.
11833  *
11834  *              The calling secquences will look like:
11835  *              sd_ssc_init->
11836  *
11837  *                  ...
11838  *
11839  *                  sd_send_scsi_USCSI_CMD->
11840  *                      sd_ssc_send-> - - - sdintr
11841  *                  ...
11842  *
11843  *                  if we think the return value should be treated as a
11844  *                  failure, we make the accessment here and print out
11845  *                  necessary by retrieving uscsi_cmd and sd_uscsi_info'
11846  *
11847  *                  ...
11848  *
11849  *              sd_ssc_fini
11850  *
11851  *
11852  *   Arguments: un - pointer to driver soft state (unit) structure for this
11853  *                   target.
11854  *
11855  * Return code: sd_ssc_t - pointer to allocated sd_ssc_t struct, it contains
11856  *                         uscsi_cmd and sd_uscsi_info.
11857  *                  NULL - if can not alloc memory for sd_ssc_t struct
11858  *
11859  *     Context: Kernel Thread.
11860  */
11861 static sd_ssc_t *
11862 sd_ssc_init(struct sd_lun *un)
11863 {
11864         sd_ssc_t                *ssc;
11865         struct uscsi_cmd        *ucmdp;
11866         struct sd_uscsi_info    *uip;
11867 
11868         ASSERT(un != NULL);
11869         ASSERT(!mutex_owned(SD_MUTEX(un)));
11870 
11871         /*
11872          * Allocate sd_ssc_t structure
11873          */
11874         ssc = kmem_zalloc(sizeof (sd_ssc_t), KM_SLEEP);
11875 
11876         /*
11877          * Allocate uscsi_cmd by calling scsi_uscsi_alloc common routine
11878          */
11879         ucmdp = scsi_uscsi_alloc();
11880 
11881         /*
11882          * Allocate sd_uscsi_info structure
11883          */
11884         uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
11885 
11886         ssc->ssc_uscsi_cmd = ucmdp;
11887         ssc->ssc_uscsi_info = uip;
11888         ssc->ssc_un = un;
11889 
11890         return (ssc);
11891 }
11892 
11893 /*
11894  * Function: sd_ssc_fini
11895  *
11896  * Description: To free sd_ssc_t and it's hanging off
11897  *
11898  * Arguments: ssc - struct pointer of sd_ssc_t.
11899  */
11900 static void
11901 sd_ssc_fini(sd_ssc_t *ssc)
11902 {
11903         scsi_uscsi_free(ssc->ssc_uscsi_cmd);
11904 
11905         if (ssc->ssc_uscsi_info != NULL) {
11906                 kmem_free(ssc->ssc_uscsi_info, sizeof (struct sd_uscsi_info));
11907                 ssc->ssc_uscsi_info = NULL;
11908         }
11909 
11910         kmem_free(ssc, sizeof (sd_ssc_t));
11911         ssc = NULL;
11912 }
11913 
11914 /*
11915  * Function: sd_ssc_send
11916  *
11917  * Description: Runs a USCSI command for user when called through sdioctl,
11918  *              or for the driver.
11919  *
11920  *   Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
11921  *                    sd_uscsi_info in.
11922  *              incmd - ptr to a valid uscsi_cmd struct
11923  *              flag - bit flag, indicating open settings, 32/64 bit type
11924  *              dataspace - UIO_USERSPACE or UIO_SYSSPACE
11925  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11926  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11927  *                      to use the USCSI "direct" chain and bypass the normal
11928  *                      command waitq.
11929  *
11930  * Return Code: 0 -  successful completion of the given command
11931  *              EIO - scsi_uscsi_handle_command() failed
11932  *              ENXIO  - soft state not found for specified dev
11933  *              ECANCELED - command cancelled due to low power
11934  *              EINVAL
11935  *              EFAULT - copyin/copyout error
11936  *              return code of scsi_uscsi_handle_command():
11937  *                      EIO
11938  *                      ENXIO
11939  *                      EACCES
11940  *
11941  *     Context: Kernel Thread;
11942  *              Waits for command to complete. Can sleep.
11943  */
11944 static int
11945 sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, int flag,
11946     enum uio_seg dataspace, int path_flag)
11947 {
11948         struct sd_uscsi_info    *uip;
11949         struct uscsi_cmd        *uscmd;
11950         struct sd_lun           *un;
11951         dev_t                   dev;
11952 
11953         int     format = 0;
11954         int     rval;
11955 
11956         ASSERT(ssc != NULL);
11957         un = ssc->ssc_un;
11958         ASSERT(un != NULL);
11959         uscmd = ssc->ssc_uscsi_cmd;
11960         ASSERT(uscmd != NULL);
11961         ASSERT(!mutex_owned(SD_MUTEX(un)));
11962         if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
11963                 /*
11964                  * If enter here, it indicates that the previous uscsi
11965                  * command has not been processed by sd_ssc_assessment.
11966                  * This is violating our rules of FMA telemetry processing.
11967                  * We should print out this message and the last undisposed
11968                  * uscsi command.
11969                  */
11970                 if (uscmd->uscsi_cdb != NULL) {
11971                         SD_INFO(SD_LOG_SDTEST, un,
11972                             "sd_ssc_send is missing the alternative "
11973                             "sd_ssc_assessment when running command 0x%x.\n",
11974                             uscmd->uscsi_cdb[0]);
11975                 }
11976                 /*
11977                  * Set the ssc_flags to SSC_FLAGS_UNKNOWN, which should be
11978                  * the initial status.
11979                  */
11980                 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
11981         }
11982 
11983         /*
11984          * We need to make sure sd_ssc_send will have sd_ssc_assessment
11985          * followed to avoid missing FMA telemetries.
11986          */
11987         ssc->ssc_flags |= SSC_FLAGS_NEED_ASSESSMENT;
11988 
11989         /*
11990          * if USCSI_PMFAILFAST is set and un is in low power, fail the
11991          * command immediately.
11992          */
11993         mutex_enter(SD_MUTEX(un));
11994         mutex_enter(&un->un_pm_mutex);
11995         if ((uscmd->uscsi_flags & USCSI_PMFAILFAST) &&
11996             SD_DEVICE_IS_IN_LOW_POWER(un)) {
11997                 SD_TRACE(SD_LOG_IO, un, "sd_ssc_send:"
11998                     "un:0x%p is in low power\n", un);
11999                 mutex_exit(&un->un_pm_mutex);
12000                 mutex_exit(SD_MUTEX(un));
12001                 return (ECANCELED);
12002         }
12003         mutex_exit(&un->un_pm_mutex);
12004         mutex_exit(SD_MUTEX(un));
12005 
12006 #ifdef SDDEBUG
12007         switch (dataspace) {
12008         case UIO_USERSPACE:
12009                 SD_TRACE(SD_LOG_IO, un,
12010                     "sd_ssc_send: entry: un:0x%p UIO_USERSPACE\n", un);
12011                 break;
12012         case UIO_SYSSPACE:
12013                 SD_TRACE(SD_LOG_IO, un,
12014                     "sd_ssc_send: entry: un:0x%p UIO_SYSSPACE\n", un);
12015                 break;
12016         default:
12017                 SD_TRACE(SD_LOG_IO, un,
12018                     "sd_ssc_send: entry: un:0x%p UNEXPECTED SPACE\n", un);
12019                 break;
12020         }
12021 #endif
12022 
12023         rval = scsi_uscsi_copyin((intptr_t)incmd, flag,
12024             SD_ADDRESS(un), &uscmd);
12025         if (rval != 0) {
12026                 SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: "
12027                     "scsi_uscsi_alloc_and_copyin failed\n", un);
12028                 return (rval);
12029         }
12030 
12031         if ((uscmd->uscsi_cdb != NULL) &&
12032             (uscmd->uscsi_cdb[0] == SCMD_FORMAT)) {
12033                 mutex_enter(SD_MUTEX(un));
12034                 un->un_f_format_in_progress = TRUE;
12035                 mutex_exit(SD_MUTEX(un));
12036                 format = 1;
12037         }
12038 
12039         /*
12040          * Allocate an sd_uscsi_info struct and fill it with the info
12041          * needed by sd_initpkt_for_uscsi().  Then put the pointer into
12042          * b_private in the buf for sd_initpkt_for_uscsi().  Note that
12043          * since we allocate the buf here in this function, we do not
12044          * need to preserve the prior contents of b_private.
12045          * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
12046          */
12047         uip = ssc->ssc_uscsi_info;
12048         uip->ui_flags = path_flag;
12049         uip->ui_cmdp = uscmd;
12050 
12051         /*
12052          * Commands sent with priority are intended for error recovery
12053          * situations, and do not have retries performed.
12054          */
12055         if (path_flag == SD_PATH_DIRECT_PRIORITY) {
12056                 uscmd->uscsi_flags |= USCSI_DIAGNOSE;
12057         }
12058         uscmd->uscsi_flags &= ~USCSI_NOINTR;
12059 
12060         dev = SD_GET_DEV(un);
12061         rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd,
12062             sd_uscsi_strategy, NULL, uip);
12063 
12064         /*
12065          * mark ssc_flags right after handle_cmd to make sure
12066          * the uscsi has been sent
12067          */
12068         ssc->ssc_flags |= SSC_FLAGS_CMD_ISSUED;
12069 
12070 #ifdef SDDEBUG
12071         SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
12072             "uscsi_status: 0x%02x  uscsi_resid:0x%x\n",
12073             uscmd->uscsi_status, uscmd->uscsi_resid);
12074         if (uscmd->uscsi_bufaddr != NULL) {
12075                 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
12076                     "uscmd->uscsi_bufaddr: 0x%p  uscmd->uscsi_buflen:%d\n",
12077                     uscmd->uscsi_bufaddr, uscmd->uscsi_buflen);
12078                 if (dataspace == UIO_SYSSPACE) {
12079                         SD_DUMP_MEMORY(un, SD_LOG_IO,
12080                             "data", (uchar_t *)uscmd->uscsi_bufaddr,
12081                             uscmd->uscsi_buflen, SD_LOG_HEX);
12082                 }
12083         }
12084 #endif
12085 
12086         if (format == 1) {
12087                 mutex_enter(SD_MUTEX(un));
12088                 un->un_f_format_in_progress = FALSE;
12089                 mutex_exit(SD_MUTEX(un));
12090         }
12091 
12092         (void) scsi_uscsi_copyout((intptr_t)incmd, uscmd);
12093 
12094         return (rval);
12095 }
12096 
12097 /*
12098  *     Function: sd_ssc_print
12099  *
12100  * Description: Print information available to the console.
12101  *
12102  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12103  *                    sd_uscsi_info in.
12104  *            sd_severity - log level.
12105  *     Context: Kernel thread or interrupt context.
12106  */
12107 static void
12108 sd_ssc_print(sd_ssc_t *ssc, int sd_severity)
12109 {
12110         struct uscsi_cmd        *ucmdp;
12111         struct scsi_device      *devp;
12112         dev_info_t              *devinfo;
12113         uchar_t                 *sensep;
12114         int                     senlen;
12115         union scsi_cdb          *cdbp;
12116         uchar_t                 com;
12117         extern struct scsi_key_strings scsi_cmds[];
12118 
12119         ASSERT(ssc != NULL);
12120         ASSERT(ssc->ssc_un != NULL);
12121 
12122         if (SD_FM_LOG(ssc->ssc_un) != SD_FM_LOG_EREPORT)
12123                 return;
12124         ucmdp = ssc->ssc_uscsi_cmd;
12125         devp = SD_SCSI_DEVP(ssc->ssc_un);
12126         devinfo = SD_DEVINFO(ssc->ssc_un);
12127         ASSERT(ucmdp != NULL);
12128         ASSERT(devp != NULL);
12129         ASSERT(devinfo != NULL);
12130         sensep = (uint8_t *)ucmdp->uscsi_rqbuf;
12131         senlen = ucmdp->uscsi_rqlen - ucmdp->uscsi_rqresid;
12132         cdbp = (union scsi_cdb *)ucmdp->uscsi_cdb;
12133 
12134         /* In certain case (like DOORLOCK), the cdb could be NULL. */
12135         if (cdbp == NULL)
12136                 return;
12137         /* We don't print log if no sense data available. */
12138         if (senlen == 0)
12139                 sensep = NULL;
12140         com = cdbp->scc_cmd;
12141         scsi_generic_errmsg(devp, sd_label, sd_severity, 0, 0, com,
12142             scsi_cmds, sensep, ssc->ssc_un->un_additional_codes, NULL);
12143 }
12144 
12145 /*
12146  *     Function: sd_ssc_assessment
12147  *
12148  * Description: We use this function to make an assessment at the point
12149  *              where SD driver may encounter a potential error.
12150  *
12151  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12152  *                  sd_uscsi_info in.
12153  *            tp_assess - a hint of strategy for ereport posting.
12154  *            Possible values of tp_assess include:
12155  *                SD_FMT_IGNORE - we don't post any ereport because we're
12156  *                sure that it is ok to ignore the underlying problems.
12157  *                SD_FMT_IGNORE_COMPROMISE - we don't post any ereport for now
12158  *                but it might be not correct to ignore the underlying hardware
12159  *                error.
12160  *                SD_FMT_STATUS_CHECK - we will post an ereport with the
12161  *                payload driver-assessment of value "fail" or
12162  *                "fatal"(depending on what information we have here). This
12163  *                assessment value is usually set when SD driver think there
12164  *                is a potential error occurred(Typically, when return value
12165  *                of the SCSI command is EIO).
12166  *                SD_FMT_STANDARD - we will post an ereport with the payload
12167  *                driver-assessment of value "info". This assessment value is
12168  *                set when the SCSI command returned successfully and with
12169  *                sense data sent back.
12170  *
12171  *     Context: Kernel thread.
12172  */
12173 static void
12174 sd_ssc_assessment(sd_ssc_t *ssc, enum sd_type_assessment tp_assess)
12175 {
12176         int senlen = 0;
12177         struct uscsi_cmd *ucmdp = NULL;
12178         struct sd_lun *un;
12179 
12180         ASSERT(ssc != NULL);
12181         un = ssc->ssc_un;
12182         ASSERT(un != NULL);
12183         ucmdp = ssc->ssc_uscsi_cmd;
12184         ASSERT(ucmdp != NULL);
12185 
12186         if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
12187                 ssc->ssc_flags &= ~SSC_FLAGS_NEED_ASSESSMENT;
12188         } else {
12189                 /*
12190                  * If enter here, it indicates that we have a wrong
12191                  * calling sequence of sd_ssc_send and sd_ssc_assessment,
12192                  * both of which should be called in a pair in case of
12193                  * loss of FMA telemetries.
12194                  */
12195                 if (ucmdp->uscsi_cdb != NULL) {
12196                         SD_INFO(SD_LOG_SDTEST, un,
12197                             "sd_ssc_assessment is missing the "
12198                             "alternative sd_ssc_send when running 0x%x, "
12199                             "or there are superfluous sd_ssc_assessment for "
12200                             "the same sd_ssc_send.\n",
12201                             ucmdp->uscsi_cdb[0]);
12202                 }
12203                 /*
12204                  * Set the ssc_flags to the initial value to avoid passing
12205                  * down dirty flags to the following sd_ssc_send function.
12206                  */
12207                 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12208                 return;
12209         }
12210 
12211         /*
12212          * Only handle an issued command which is waiting for assessment.
12213          * A command which is not issued will not have
12214          * SSC_FLAGS_INVALID_DATA set, so it'ok we just return here.
12215          */
12216         if (!(ssc->ssc_flags & SSC_FLAGS_CMD_ISSUED)) {
12217                 sd_ssc_print(ssc, SCSI_ERR_INFO);
12218                 return;
12219         } else {
12220                 /*
12221                  * For an issued command, we should clear this flag in
12222                  * order to make the sd_ssc_t structure be used off
12223                  * multiple uscsi commands.
12224                  */
12225                 ssc->ssc_flags &= ~SSC_FLAGS_CMD_ISSUED;
12226         }
12227 
12228         /*
12229          * We will not deal with non-retryable(flag USCSI_DIAGNOSE set)
12230          * commands here. And we should clear the ssc_flags before return.
12231          */
12232         if (ucmdp->uscsi_flags & USCSI_DIAGNOSE) {
12233                 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12234                 return;
12235         }
12236 
12237         switch (tp_assess) {
12238         case SD_FMT_IGNORE:
12239         case SD_FMT_IGNORE_COMPROMISE:
12240                 break;
12241         case SD_FMT_STATUS_CHECK:
12242                 /*
12243                  * For a failed command(including the succeeded command
12244                  * with invalid data sent back).
12245                  */
12246                 sd_ssc_post(ssc, SD_FM_DRV_FATAL);
12247                 break;
12248         case SD_FMT_STANDARD:
12249                 /*
12250                  * Always for the succeeded commands probably with sense
12251                  * data sent back.
12252                  * Limitation:
12253                  *      We can only handle a succeeded command with sense
12254                  *      data sent back when auto-request-sense is enabled.
12255                  */
12256                 senlen = ssc->ssc_uscsi_cmd->uscsi_rqlen -
12257                     ssc->ssc_uscsi_cmd->uscsi_rqresid;
12258                 if ((ssc->ssc_uscsi_info->ui_pkt_state & STATE_ARQ_DONE) &&
12259                     (un->un_f_arq_enabled == TRUE) &&
12260                     senlen > 0 &&
12261                     ssc->ssc_uscsi_cmd->uscsi_rqbuf != NULL) {
12262                         sd_ssc_post(ssc, SD_FM_DRV_NOTICE);
12263                 }
12264                 break;
12265         default:
12266                 /*
12267                  * Should not have other type of assessment.
12268                  */
12269                 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
12270                     "sd_ssc_assessment got wrong "
12271                     "sd_type_assessment %d.\n", tp_assess);
12272                 break;
12273         }
12274         /*
12275          * Clear up the ssc_flags before return.
12276          */
12277         ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12278 }
12279 
12280 /*
12281  *    Function: sd_ssc_post
12282  *
12283  * Description: 1. read the driver property to get fm-scsi-log flag.
12284  *              2. print log if fm_log_capable is non-zero.
12285  *              3. call sd_ssc_ereport_post to post ereport if possible.
12286  *
12287  *    Context: May be called from kernel thread or interrupt context.
12288  */
12289 static void
12290 sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess)
12291 {
12292         struct sd_lun   *un;
12293         int             sd_severity;
12294 
12295         ASSERT(ssc != NULL);
12296         un = ssc->ssc_un;
12297         ASSERT(un != NULL);
12298 
12299         /*
12300          * We may enter here from sd_ssc_assessment(for USCSI command) or
12301          * by directly called from sdintr context.
12302          * We don't handle a non-disk drive(CD-ROM, removable media).
12303          * Clear the ssc_flags before return in case we've set
12304          * SSC_FLAGS_INVALID_XXX which should be skipped for a non-disk
12305          * driver.
12306          */
12307         if (ISCD(un) || un->un_f_has_removable_media) {
12308                 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12309                 return;
12310         }
12311 
12312         switch (sd_assess) {
12313                 case SD_FM_DRV_FATAL:
12314                         sd_severity = SCSI_ERR_FATAL;
12315                         break;
12316                 case SD_FM_DRV_RECOVERY:
12317                         sd_severity = SCSI_ERR_RECOVERED;
12318                         break;
12319                 case SD_FM_DRV_RETRY:
12320                         sd_severity = SCSI_ERR_RETRYABLE;
12321                         break;
12322                 case SD_FM_DRV_NOTICE:
12323                         sd_severity = SCSI_ERR_INFO;
12324                         break;
12325                 default:
12326                         sd_severity = SCSI_ERR_UNKNOWN;
12327         }
12328         /* print log */
12329         sd_ssc_print(ssc, sd_severity);
12330 
12331         /* always post ereport */
12332         sd_ssc_ereport_post(ssc, sd_assess);
12333 }
12334 
12335 /*
12336  *    Function: sd_ssc_set_info
12337  *
12338  * Description: Mark ssc_flags and set ssc_info which would be the
12339  *              payload of uderr ereport. This function will cause
12340  *              sd_ssc_ereport_post to post uderr ereport only.
12341  *              Besides, when ssc_flags == SSC_FLAGS_INVALID_DATA(USCSI),
12342  *              the function will also call SD_ERROR or scsi_log for a
12343  *              CDROM/removable-media/DDI_FM_NOT_CAPABLE device.
12344  *
12345  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12346  *                  sd_uscsi_info in.
12347  *            ssc_flags - indicate the sub-category of a uderr.
12348  *            comp - this argument is meaningful only when
12349  *                   ssc_flags == SSC_FLAGS_INVALID_DATA, and its possible
12350  *                   values include:
12351  *                   > 0, SD_ERROR is used with comp as the driver logging
12352  *                   component;
12353  *                   = 0, scsi-log is used to log error telemetries;
12354  *                   < 0, no log available for this telemetry.
12355  *
12356  *    Context: Kernel thread or interrupt context
12357  */
12358 static void
12359 sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp, const char *fmt, ...)
12360 {
12361         va_list ap;
12362 
12363         ASSERT(ssc != NULL);
12364         ASSERT(ssc->ssc_un != NULL);
12365 
12366         ssc->ssc_flags |= ssc_flags;
12367         va_start(ap, fmt);
12368         (void) vsnprintf(ssc->ssc_info, sizeof (ssc->ssc_info), fmt, ap);
12369         va_end(ap);
12370 
12371         /*
12372          * If SSC_FLAGS_INVALID_DATA is set, it should be a uscsi command
12373          * with invalid data sent back. For non-uscsi command, the
12374          * following code will be bypassed.
12375          */
12376         if (ssc_flags & SSC_FLAGS_INVALID_DATA) {
12377                 if (SD_FM_LOG(ssc->ssc_un) == SD_FM_LOG_NSUP) {
12378                         /*
12379                          * If the error belong to certain component and we
12380                          * do not want it to show up on the console, we
12381                          * will use SD_ERROR, otherwise scsi_log is
12382                          * preferred.
12383                          */
12384                         if (comp > 0) {
12385                                 SD_ERROR(comp, ssc->ssc_un, ssc->ssc_info);
12386                         } else if (comp == 0) {
12387                                 scsi_log(SD_DEVINFO(ssc->ssc_un), sd_label,
12388                                     CE_WARN, ssc->ssc_info);
12389                         }
12390                 }
12391         }
12392 }
12393 
12394 /*
12395  *    Function: sd_buf_iodone
12396  *
12397  * Description: Frees the sd_xbuf & returns the buf to its originator.
12398  *
12399  *     Context: May be called from interrupt context.
12400  */
12401 /* ARGSUSED */
12402 static void
12403 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp)
12404 {
12405         struct sd_xbuf *xp;
12406 
12407         ASSERT(un != NULL);
12408         ASSERT(bp != NULL);
12409         ASSERT(!mutex_owned(SD_MUTEX(un)));
12410 
12411         SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n");
12412 
12413         xp = SD_GET_XBUF(bp);
12414         ASSERT(xp != NULL);
12415 
12416         /* xbuf is gone after this */
12417         if (ddi_xbuf_done(bp, un->un_xbuf_attr)) {
12418                 mutex_enter(SD_MUTEX(un));
12419 
12420                 /*
12421                  * Grab time when the cmd completed.
12422                  * This is used for determining if the system has been
12423                  * idle long enough to make it idle to the PM framework.
12424                  * This is for lowering the overhead, and therefore improving
12425                  * performance per I/O operation.
12426                  */
12427                 un->un_pm_idle_time = gethrtime();
12428 
12429                 un->un_ncmds_in_driver--;
12430                 ASSERT(un->un_ncmds_in_driver >= 0);
12431                 SD_INFO(SD_LOG_IO, un,
12432                     "sd_buf_iodone: un_ncmds_in_driver = %ld\n",
12433                     un->un_ncmds_in_driver);
12434 
12435                 mutex_exit(SD_MUTEX(un));
12436         }
12437 
12438         biodone(bp);                            /* bp is gone after this */
12439 
12440         SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n");
12441 }
12442 
12443 
12444 /*
12445  *    Function: sd_uscsi_iodone
12446  *
12447  * Description: Frees the sd_xbuf & returns the buf to its originator.
12448  *
12449  *     Context: May be called from interrupt context.
12450  */
12451 /* ARGSUSED */
12452 static void
12453 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
12454 {
12455         struct sd_xbuf *xp;
12456 
12457         ASSERT(un != NULL);
12458         ASSERT(bp != NULL);
12459 
12460         xp = SD_GET_XBUF(bp);
12461         ASSERT(xp != NULL);
12462         ASSERT(!mutex_owned(SD_MUTEX(un)));
12463 
12464         SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n");
12465 
12466         bp->b_private = xp->xb_private;
12467 
12468         mutex_enter(SD_MUTEX(un));
12469 
12470         /*
12471          * Grab time when the cmd completed.
12472          * This is used for determining if the system has been
12473          * idle long enough to make it idle to the PM framework.
12474          * This is for lowering the overhead, and therefore improving
12475          * performance per I/O operation.
12476          */
12477         un->un_pm_idle_time = gethrtime();
12478 
12479         un->un_ncmds_in_driver--;
12480         ASSERT(un->un_ncmds_in_driver >= 0);
12481         SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n",
12482             un->un_ncmds_in_driver);
12483 
12484         mutex_exit(SD_MUTEX(un));
12485 
12486         if (((struct uscsi_cmd *)(xp->xb_pktinfo))->uscsi_rqlen >
12487             SENSE_LENGTH) {
12488                 kmem_free(xp, sizeof (struct sd_xbuf) - SENSE_LENGTH +
12489                     MAX_SENSE_LENGTH);
12490         } else {
12491                 kmem_free(xp, sizeof (struct sd_xbuf));
12492         }
12493 
12494         biodone(bp);
12495 
12496         SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n");
12497 }
12498 
12499 
12500 /*
12501  *    Function: sd_mapblockaddr_iostart
12502  *
12503  * Description: Verify request lies within the partition limits for
12504  *              the indicated minor device.  Issue "overrun" buf if
12505  *              request would exceed partition range.  Converts
12506  *              partition-relative block address to absolute.
12507  *
12508  *              Upon exit of this function:
12509  *              1.I/O is aligned
12510  *                 xp->xb_blkno represents the absolute sector address
12511  *              2.I/O is misaligned
12512  *                 xp->xb_blkno represents the absolute logical block address
12513  *                 based on DEV_BSIZE. The logical block address will be
12514  *                 converted to physical sector address in sd_mapblocksize_\
12515  *                 iostart.
12516  *              3.I/O is misaligned but is aligned in "overrun" buf
12517  *                 xp->xb_blkno represents the absolute logical block address
12518  *                 based on DEV_BSIZE. The logical block address will be
12519  *                 converted to physical sector address in sd_mapblocksize_\
12520  *                 iostart. But no RMW will be issued in this case.
12521  *
12522  *     Context: Can sleep
12523  *
12524  *      Issues: This follows what the old code did, in terms of accessing
12525  *              some of the partition info in the unit struct without holding
12526  *              the mutext.  This is a general issue, if the partition info
12527  *              can be altered while IO is in progress... as soon as we send
12528  *              a buf, its partitioning can be invalid before it gets to the
12529  *              device.  Probably the right fix is to move partitioning out
12530  *              of the driver entirely.
12531  */
12532 
12533 static void
12534 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp)
12535 {
12536         diskaddr_t      nblocks;        /* #blocks in the given partition */
12537         daddr_t blocknum;       /* Block number specified by the buf */
12538         size_t  requested_nblocks;
12539         size_t  available_nblocks;
12540         int     partition;
12541         diskaddr_t      partition_offset;
12542         struct sd_xbuf *xp;
12543         int secmask = 0, blknomask = 0;
12544         ushort_t is_aligned = TRUE;
12545 
12546         ASSERT(un != NULL);
12547         ASSERT(bp != NULL);
12548         ASSERT(!mutex_owned(SD_MUTEX(un)));
12549 
12550         SD_TRACE(SD_LOG_IO_PARTITION, un,
12551             "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp);
12552 
12553         xp = SD_GET_XBUF(bp);
12554         ASSERT(xp != NULL);
12555 
12556         /*
12557          * If the geometry is not indicated as valid, attempt to access
12558          * the unit & verify the geometry/label. This can be the case for
12559          * removable-media devices, of if the device was opened in
12560          * NDELAY/NONBLOCK mode.
12561          */
12562         partition = SDPART(bp->b_edev);
12563 
12564         if (!SD_IS_VALID_LABEL(un)) {
12565                 sd_ssc_t *ssc;
12566                 /*
12567                  * Initialize sd_ssc_t for internal uscsi commands
12568                  * In case of potential porformance issue, we need
12569                  * to alloc memory only if there is invalid label
12570                  */
12571                 ssc = sd_ssc_init(un);
12572 
12573                 if (sd_ready_and_valid(ssc, partition) != SD_READY_VALID) {
12574                         /*
12575                          * For removable devices it is possible to start an
12576                          * I/O without a media by opening the device in nodelay
12577                          * mode. Also for writable CDs there can be many
12578                          * scenarios where there is no geometry yet but volume
12579                          * manager is trying to issue a read() just because
12580                          * it can see TOC on the CD. So do not print a message
12581                          * for removables.
12582                          */
12583                         if (!un->un_f_has_removable_media) {
12584                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
12585                                     "i/o to invalid geometry\n");
12586                         }
12587                         bioerror(bp, EIO);
12588                         bp->b_resid = bp->b_bcount;
12589                         SD_BEGIN_IODONE(index, un, bp);
12590 
12591                         sd_ssc_fini(ssc);
12592                         return;
12593                 }
12594                 sd_ssc_fini(ssc);
12595         }
12596 
12597         nblocks = 0;
12598         (void) cmlb_partinfo(un->un_cmlbhandle, partition,
12599             &nblocks, &partition_offset, NULL, NULL, (void *)SD_PATH_DIRECT);
12600 
12601         if (un->un_f_enable_rmw) {
12602                 blknomask = (un->un_phy_blocksize / DEV_BSIZE) - 1;
12603                 secmask = un->un_phy_blocksize - 1;
12604         } else {
12605                 blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
12606                 secmask = un->un_tgt_blocksize - 1;
12607         }
12608 
12609         if ((bp->b_lblkno & (blknomask)) || (bp->b_bcount & (secmask))) {
12610                 is_aligned = FALSE;
12611         }
12612 
12613         if (!(NOT_DEVBSIZE(un)) || un->un_f_enable_rmw) {
12614                 /*
12615                  * If I/O is aligned, no need to involve RMW(Read Modify Write)
12616                  * Convert the logical block number to target's physical sector
12617                  * number.
12618                  */
12619                 if (is_aligned) {
12620                         xp->xb_blkno = SD_SYS2TGTBLOCK(un, xp->xb_blkno);
12621                 } else {
12622                         /*
12623                          * There is no RMW if we're just reading, so don't
12624                          * warn or error out because of it.
12625                          */
12626                         if (bp->b_flags & B_READ) {
12627                                 /*EMPTY*/
12628                         } else if (!un->un_f_enable_rmw &&
12629                             un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR) {
12630                                 bp->b_flags |= B_ERROR;
12631                                 goto error_exit;
12632                         } else if (un->un_f_rmw_type == SD_RMW_TYPE_DEFAULT) {
12633                                 mutex_enter(SD_MUTEX(un));
12634                                 if (!un->un_f_enable_rmw &&
12635                                     un->un_rmw_msg_timeid == NULL) {
12636                                         scsi_log(SD_DEVINFO(un), sd_label,
12637                                             CE_WARN, "I/O request is not "
12638                                             "aligned with %d disk sector size. "
12639                                             "It is handled through Read Modify "
12640                                             "Write but the performance is "
12641                                             "very low.\n",
12642                                             un->un_tgt_blocksize);
12643                                         un->un_rmw_msg_timeid =
12644                                             timeout(sd_rmw_msg_print_handler,
12645                                             un, SD_RMW_MSG_PRINT_TIMEOUT);
12646                                 } else {
12647                                         un->un_rmw_incre_count ++;
12648                                 }
12649                                 mutex_exit(SD_MUTEX(un));
12650                         }
12651 
12652                         nblocks = SD_TGT2SYSBLOCK(un, nblocks);
12653                         partition_offset = SD_TGT2SYSBLOCK(un,
12654                             partition_offset);
12655                 }
12656         }
12657 
12658         /*
12659          * blocknum is the starting block number of the request. At this
12660          * point it is still relative to the start of the minor device.
12661          */
12662         blocknum = xp->xb_blkno;
12663 
12664         /*
12665          * Legacy: If the starting block number is one past the last block
12666          * in the partition, do not set B_ERROR in the buf.
12667          */
12668         if (blocknum == nblocks)  {
12669                 goto error_exit;
12670         }
12671 
12672         /*
12673          * Confirm that the first block of the request lies within the
12674          * partition limits. Also the requested number of bytes must be
12675          * a multiple of the system block size.
12676          */
12677         if ((blocknum < 0) || (blocknum >= nblocks) ||
12678             ((bp->b_bcount & (DEV_BSIZE - 1)) != 0)) {
12679                 bp->b_flags |= B_ERROR;
12680                 goto error_exit;
12681         }
12682 
12683         /*
12684          * If the requsted # blocks exceeds the available # blocks, that
12685          * is an overrun of the partition.
12686          */
12687         if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12688                 requested_nblocks = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
12689         } else {
12690                 requested_nblocks = SD_BYTES2SYSBLOCKS(bp->b_bcount);
12691         }
12692 
12693         available_nblocks = (size_t)(nblocks - blocknum);
12694         ASSERT(nblocks >= blocknum);
12695 
12696         if (requested_nblocks > available_nblocks) {
12697                 size_t resid;
12698 
12699                 /*
12700                  * Allocate an "overrun" buf to allow the request to proceed
12701                  * for the amount of space available in the partition. The
12702                  * amount not transferred will be added into the b_resid
12703                  * when the operation is complete. The overrun buf
12704                  * replaces the original buf here, and the original buf
12705                  * is saved inside the overrun buf, for later use.
12706                  */
12707                 if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12708                         resid = SD_TGTBLOCKS2BYTES(un,
12709                             (offset_t)(requested_nblocks - available_nblocks));
12710                 } else {
12711                         resid = SD_SYSBLOCKS2BYTES(
12712                             (offset_t)(requested_nblocks - available_nblocks));
12713                 }
12714 
12715                 size_t count = bp->b_bcount - resid;
12716                 /*
12717                  * Note: count is an unsigned entity thus it'll NEVER
12718                  * be less than 0 so ASSERT the original values are
12719                  * correct.
12720                  */
12721                 ASSERT(bp->b_bcount >= resid);
12722 
12723                 bp = sd_bioclone_alloc(bp, count, blocknum,
12724                     (int (*)(struct buf *)) sd_mapblockaddr_iodone);
12725                 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */
12726                 ASSERT(xp != NULL);
12727         }
12728 
12729         /* At this point there should be no residual for this buf. */
12730         ASSERT(bp->b_resid == 0);
12731 
12732         /* Convert the block number to an absolute address. */
12733         xp->xb_blkno += partition_offset;
12734 
12735         SD_NEXT_IOSTART(index, un, bp);
12736 
12737         SD_TRACE(SD_LOG_IO_PARTITION, un,
12738             "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp);
12739 
12740         return;
12741 
12742 error_exit:
12743         bp->b_resid = bp->b_bcount;
12744         SD_BEGIN_IODONE(index, un, bp);
12745         SD_TRACE(SD_LOG_IO_PARTITION, un,
12746             "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp);
12747 }
12748 
12749 
12750 /*
12751  *    Function: sd_mapblockaddr_iodone
12752  *
12753  * Description: Completion-side processing for partition management.
12754  *
12755  *     Context: May be called under interrupt context
12756  */
12757 
12758 static void
12759 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp)
12760 {
12761         /* int  partition; */   /* Not used, see below. */
12762         ASSERT(un != NULL);
12763         ASSERT(bp != NULL);
12764         ASSERT(!mutex_owned(SD_MUTEX(un)));
12765 
12766         SD_TRACE(SD_LOG_IO_PARTITION, un,
12767             "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp);
12768 
12769         if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) {
12770                 /*
12771                  * We have an "overrun" buf to deal with...
12772                  */
12773                 struct sd_xbuf  *xp;
12774                 struct buf      *obp;   /* ptr to the original buf */
12775 
12776                 xp = SD_GET_XBUF(bp);
12777                 ASSERT(xp != NULL);
12778 
12779                 /* Retrieve the pointer to the original buf */
12780                 obp = (struct buf *)xp->xb_private;
12781                 ASSERT(obp != NULL);
12782 
12783                 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid);
12784                 bioerror(obp, bp->b_error);
12785 
12786                 sd_bioclone_free(bp);
12787 
12788                 /*
12789                  * Get back the original buf.
12790                  * Note that since the restoration of xb_blkno below
12791                  * was removed, the sd_xbuf is not needed.
12792                  */
12793                 bp = obp;
12794                 /*
12795                  * xp = SD_GET_XBUF(bp);
12796                  * ASSERT(xp != NULL);
12797                  */
12798         }
12799 
12800         /*
12801          * Convert sd->xb_blkno back to a minor-device relative value.
12802          * Note: this has been commented out, as it is not needed in the
12803          * current implementation of the driver (ie, since this function
12804          * is at the top of the layering chains, so the info will be
12805          * discarded) and it is in the "hot" IO path.
12806          *
12807          * partition = getminor(bp->b_edev) & SDPART_MASK;
12808          * xp->xb_blkno -= un->un_offset[partition];
12809          */
12810 
12811         SD_NEXT_IODONE(index, un, bp);
12812 
12813         SD_TRACE(SD_LOG_IO_PARTITION, un,
12814             "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp);
12815 }
12816 
12817 
12818 /*
12819  *    Function: sd_mapblocksize_iostart
12820  *
12821  * Description: Convert between system block size (un->un_sys_blocksize)
12822  *              and target block size (un->un_tgt_blocksize).
12823  *
12824  *     Context: Can sleep to allocate resources.
12825  *
12826  * Assumptions: A higher layer has already performed any partition validation,
12827  *              and converted the xp->xb_blkno to an absolute value relative
12828  *              to the start of the device.
12829  *
12830  *              It is also assumed that the higher layer has implemented
12831  *              an "overrun" mechanism for the case where the request would
12832  *              read/write beyond the end of a partition.  In this case we
12833  *              assume (and ASSERT) that bp->b_resid == 0.
12834  *
12835  *              Note: The implementation for this routine assumes the target
12836  *              block size remains constant between allocation and transport.
12837  */
12838 
12839 static void
12840 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp)
12841 {
12842         struct sd_mapblocksize_info     *bsp;
12843         struct sd_xbuf                  *xp;
12844         offset_t first_byte;
12845         daddr_t start_block, end_block;
12846         daddr_t request_bytes;
12847         ushort_t is_aligned = FALSE;
12848 
12849         ASSERT(un != NULL);
12850         ASSERT(bp != NULL);
12851         ASSERT(!mutex_owned(SD_MUTEX(un)));
12852         ASSERT(bp->b_resid == 0);
12853 
12854         SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12855             "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp);
12856 
12857         /*
12858          * For a non-writable CD, a write request is an error
12859          */
12860         if (ISCD(un) && ((bp->b_flags & B_READ) == 0) &&
12861             (un->un_f_mmc_writable_media == FALSE)) {
12862                 bioerror(bp, EIO);
12863                 bp->b_resid = bp->b_bcount;
12864                 SD_BEGIN_IODONE(index, un, bp);
12865                 return;
12866         }
12867 
12868         /*
12869          * We do not need a shadow buf if the device is using
12870          * un->un_sys_blocksize as its block size or if bcount == 0.
12871          * In this case there is no layer-private data block allocated.
12872          */
12873         if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
12874             (bp->b_bcount == 0)) {
12875                 goto done;
12876         }
12877 
12878 #if defined(__i386) || defined(__amd64)
12879         /* We do not support non-block-aligned transfers for ROD devices */
12880         ASSERT(!ISROD(un));
12881 #endif
12882 
12883         xp = SD_GET_XBUF(bp);
12884         ASSERT(xp != NULL);
12885 
12886         SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12887             "tgt_blocksize:0x%x sys_blocksize: 0x%x\n",
12888             un->un_tgt_blocksize, DEV_BSIZE);
12889         SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12890             "request start block:0x%x\n", xp->xb_blkno);
12891         SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12892             "request len:0x%x\n", bp->b_bcount);
12893 
12894         /*
12895          * Allocate the layer-private data area for the mapblocksize layer.
12896          * Layers are allowed to use the xp_private member of the sd_xbuf
12897          * struct to store the pointer to their layer-private data block, but
12898          * each layer also has the responsibility of restoring the prior
12899          * contents of xb_private before returning the buf/xbuf to the
12900          * higher layer that sent it.
12901          *
12902          * Here we save the prior contents of xp->xb_private into the
12903          * bsp->mbs_oprivate field of our layer-private data area. This value
12904          * is restored by sd_mapblocksize_iodone() just prior to freeing up
12905          * the layer-private area and returning the buf/xbuf to the layer
12906          * that sent it.
12907          *
12908          * Note that here we use kmem_zalloc for the allocation as there are
12909          * parts of the mapblocksize code that expect certain fields to be
12910          * zero unless explicitly set to a required value.
12911          */
12912         bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12913         bsp->mbs_oprivate = xp->xb_private;
12914         xp->xb_private = bsp;
12915 
12916         /*
12917          * This treats the data on the disk (target) as an array of bytes.
12918          * first_byte is the byte offset, from the beginning of the device,
12919          * to the location of the request. This is converted from a
12920          * un->un_sys_blocksize block address to a byte offset, and then back
12921          * to a block address based upon a un->un_tgt_blocksize block size.
12922          *
12923          * xp->xb_blkno should be absolute upon entry into this function,
12924          * but, but it is based upon partitions that use the "system"
12925          * block size. It must be adjusted to reflect the block size of
12926          * the target.
12927          *
12928          * Note that end_block is actually the block that follows the last
12929          * block of the request, but that's what is needed for the computation.
12930          */
12931         first_byte  = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
12932         if (un->un_f_enable_rmw) {
12933                 start_block = xp->xb_blkno =
12934                     (first_byte / un->un_phy_blocksize) *
12935                     (un->un_phy_blocksize / DEV_BSIZE);
12936                 end_block   = ((first_byte + bp->b_bcount +
12937                     un->un_phy_blocksize - 1) / un->un_phy_blocksize) *
12938                     (un->un_phy_blocksize / DEV_BSIZE);
12939         } else {
12940                 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize;
12941                 end_block   = (first_byte + bp->b_bcount +
12942                     un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
12943         }
12944 
12945         /* request_bytes is rounded up to a multiple of the target block size */
12946         request_bytes = (end_block - start_block) * un->un_tgt_blocksize;
12947 
12948         /*
12949          * See if the starting address of the request and the request
12950          * length are aligned on a un->un_tgt_blocksize boundary. If aligned
12951          * then we do not need to allocate a shadow buf to handle the request.
12952          */
12953         if (un->un_f_enable_rmw) {
12954                 if (((first_byte % un->un_phy_blocksize) == 0) &&
12955                     ((bp->b_bcount % un->un_phy_blocksize) == 0)) {
12956                         is_aligned = TRUE;
12957                 }
12958         } else {
12959                 if (((first_byte % un->un_tgt_blocksize) == 0) &&
12960                     ((bp->b_bcount % un->un_tgt_blocksize) == 0)) {
12961                         is_aligned = TRUE;
12962                 }
12963         }
12964 
12965         if ((bp->b_flags & B_READ) == 0) {
12966                 /*
12967                  * Lock the range for a write operation. An aligned request is
12968                  * considered a simple write; otherwise the request must be a
12969                  * read-modify-write.
12970                  */
12971                 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1,
12972                     (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW);
12973         }
12974 
12975         /*
12976          * Alloc a shadow buf if the request is not aligned. Also, this is
12977          * where the READ command is generated for a read-modify-write. (The
12978          * write phase is deferred until after the read completes.)
12979          */
12980         if (is_aligned == FALSE) {
12981 
12982                 struct sd_mapblocksize_info     *shadow_bsp;
12983                 struct sd_xbuf  *shadow_xp;
12984                 struct buf      *shadow_bp;
12985 
12986                 /*
12987                  * Allocate the shadow buf and it associated xbuf. Note that
12988                  * after this call the xb_blkno value in both the original
12989                  * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the
12990                  * same: absolute relative to the start of the device, and
12991                  * adjusted for the target block size. The b_blkno in the
12992                  * shadow buf will also be set to this value. We should never
12993                  * change b_blkno in the original bp however.
12994                  *
12995                  * Note also that the shadow buf will always need to be a
12996                  * READ command, regardless of whether the incoming command
12997                  * is a READ or a WRITE.
12998                  */
12999                 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ,
13000                     xp->xb_blkno,
13001                     (int (*)(struct buf *)) sd_mapblocksize_iodone);
13002 
13003                 shadow_xp = SD_GET_XBUF(shadow_bp);
13004 
13005                 /*
13006                  * Allocate the layer-private data for the shadow buf.
13007                  * (No need to preserve xb_private in the shadow xbuf.)
13008                  */
13009                 shadow_xp->xb_private = shadow_bsp =
13010                     kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
13011 
13012                 /*
13013                  * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone
13014                  * to figure out where the start of the user data is (based upon
13015                  * the system block size) in the data returned by the READ
13016                  * command (which will be based upon the target blocksize). Note
13017                  * that this is only really used if the request is unaligned.
13018                  */
13019                 if (un->un_f_enable_rmw) {
13020                         bsp->mbs_copy_offset = (ssize_t)(first_byte -
13021                             ((offset_t)xp->xb_blkno * un->un_sys_blocksize));
13022                         ASSERT((bsp->mbs_copy_offset >= 0) &&
13023                             (bsp->mbs_copy_offset < un->un_phy_blocksize));
13024                 } else {
13025                         bsp->mbs_copy_offset = (ssize_t)(first_byte -
13026                             ((offset_t)xp->xb_blkno * un->un_tgt_blocksize));
13027                         ASSERT((bsp->mbs_copy_offset >= 0) &&
13028                             (bsp->mbs_copy_offset < un->un_tgt_blocksize));
13029                 }
13030 
13031                 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset;
13032 
13033                 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index;
13034 
13035                 /* Transfer the wmap (if any) to the shadow buf */
13036                 shadow_bsp->mbs_wmp = bsp->mbs_wmp;
13037                 bsp->mbs_wmp = NULL;
13038 
13039                 /*
13040                  * The shadow buf goes on from here in place of the
13041                  * original buf.
13042                  */
13043                 shadow_bsp->mbs_orig_bp = bp;
13044                 bp = shadow_bp;
13045         }
13046 
13047         SD_INFO(SD_LOG_IO_RMMEDIA, un,
13048             "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno);
13049         SD_INFO(SD_LOG_IO_RMMEDIA, un,
13050             "sd_mapblocksize_iostart: tgt request len:0x%x\n",
13051             request_bytes);
13052         SD_INFO(SD_LOG_IO_RMMEDIA, un,
13053             "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp);
13054 
13055 done:
13056         SD_NEXT_IOSTART(index, un, bp);
13057 
13058         SD_TRACE(SD_LOG_IO_RMMEDIA, un,
13059             "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp);
13060 }
13061 
13062 
13063 /*
13064  *    Function: sd_mapblocksize_iodone
13065  *
13066  * Description: Completion side processing for block-size mapping.
13067  *
13068  *     Context: May be called under interrupt context
13069  */
13070 
13071 static void
13072 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp)
13073 {
13074         struct sd_mapblocksize_info     *bsp;
13075         struct sd_xbuf  *xp;
13076         struct sd_xbuf  *orig_xp;       /* sd_xbuf for the original buf */
13077         struct buf      *orig_bp;       /* ptr to the original buf */
13078         offset_t        shadow_end;
13079         offset_t        request_end;
13080         offset_t        shadow_start;
13081         ssize_t         copy_offset;
13082         size_t          copy_length;
13083         size_t          shortfall;
13084         uint_t          is_write;       /* TRUE if this bp is a WRITE */
13085         uint_t          has_wmap;       /* TRUE is this bp has a wmap */
13086 
13087         ASSERT(un != NULL);
13088         ASSERT(bp != NULL);
13089 
13090         SD_TRACE(SD_LOG_IO_RMMEDIA, un,
13091             "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp);
13092 
13093         /*
13094          * There is no shadow buf or layer-private data if the target is
13095          * using un->un_sys_blocksize as its block size or if bcount == 0.
13096          */
13097         if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
13098             (bp->b_bcount == 0)) {
13099                 goto exit;
13100         }
13101 
13102         xp = SD_GET_XBUF(bp);
13103         ASSERT(xp != NULL);
13104 
13105         /* Retrieve the pointer to the layer-private data area from the xbuf. */
13106         bsp = xp->xb_private;
13107 
13108         is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE;
13109         has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE;
13110 
13111         if (is_write) {
13112                 /*
13113                  * For a WRITE request we must free up the block range that
13114                  * we have locked up.  This holds regardless of whether this is
13115                  * an aligned write request or a read-modify-write request.
13116                  */
13117                 sd_range_unlock(un, bsp->mbs_wmp);
13118                 bsp->mbs_wmp = NULL;
13119         }
13120 
13121         if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) {
13122                 /*
13123                  * An aligned read or write command will have no shadow buf;
13124                  * there is not much else to do with it.
13125                  */
13126                 goto done;
13127         }
13128 
13129         orig_bp = bsp->mbs_orig_bp;
13130         ASSERT(orig_bp != NULL);
13131         orig_xp = SD_GET_XBUF(orig_bp);
13132         ASSERT(orig_xp != NULL);
13133         ASSERT(!mutex_owned(SD_MUTEX(un)));
13134 
13135         if (!is_write && has_wmap) {
13136                 /*
13137                  * A READ with a wmap means this is the READ phase of a
13138                  * read-modify-write. If an error occurred on the READ then
13139                  * we do not proceed with the WRITE phase or copy any data.
13140                  * Just release the write maps and return with an error.
13141                  */
13142                 if ((bp->b_resid != 0) || (bp->b_error != 0)) {
13143                         orig_bp->b_resid = orig_bp->b_bcount;
13144                         bioerror(orig_bp, bp->b_error);
13145                         sd_range_unlock(un, bsp->mbs_wmp);
13146                         goto freebuf_done;
13147                 }
13148         }
13149 
13150         /*
13151          * Here is where we set up to copy the data from the shadow buf
13152          * into the space associated with the original buf.
13153          *
13154          * To deal with the conversion between block sizes, these
13155          * computations treat the data as an array of bytes, with the
13156          * first byte (byte 0) corresponding to the first byte in the
13157          * first block on the disk.
13158          */
13159 
13160         /*
13161          * shadow_start and shadow_len indicate the location and size of
13162          * the data returned with the shadow IO request.
13163          */
13164         if (un->un_f_enable_rmw) {
13165                 shadow_start  = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
13166         } else {
13167                 shadow_start  = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
13168         }
13169         shadow_end    = shadow_start + bp->b_bcount - bp->b_resid;
13170 
13171         /*
13172          * copy_offset gives the offset (in bytes) from the start of the first
13173          * block of the READ request to the beginning of the data.  We retrieve
13174          * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved
13175          * there by sd_mapblockize_iostart(). copy_length gives the amount of
13176          * data to be copied (in bytes).
13177          */
13178         copy_offset  = bsp->mbs_copy_offset;
13179         if (un->un_f_enable_rmw) {
13180                 ASSERT((copy_offset >= 0) &&
13181                     (copy_offset < un->un_phy_blocksize));
13182         } else {
13183                 ASSERT((copy_offset >= 0) &&
13184                     (copy_offset < un->un_tgt_blocksize));
13185         }
13186 
13187         copy_length  = orig_bp->b_bcount;
13188         request_end  = shadow_start + copy_offset + orig_bp->b_bcount;
13189 
13190         /*
13191          * Set up the resid and error fields of orig_bp as appropriate.
13192          */
13193         if (shadow_end >= request_end) {
13194                 /* We got all the requested data; set resid to zero */
13195                 orig_bp->b_resid = 0;
13196         } else {
13197                 /*
13198                  * We failed to get enough data to fully satisfy the original
13199                  * request. Just copy back whatever data we got and set
13200                  * up the residual and error code as required.
13201                  *
13202                  * 'shortfall' is the amount by which the data received with the
13203                  * shadow buf has "fallen short" of the requested amount.
13204                  */
13205                 shortfall = (size_t)(request_end - shadow_end);
13206 
13207                 if (shortfall > orig_bp->b_bcount) {
13208                         /*
13209                          * We did not get enough data to even partially
13210                          * fulfill the original request.  The residual is
13211                          * equal to the amount requested.
13212                          */
13213                         orig_bp->b_resid = orig_bp->b_bcount;
13214                 } else {
13215                         /*
13216                          * We did not get all the data that we requested
13217                          * from the device, but we will try to return what
13218                          * portion we did get.
13219                          */
13220                         orig_bp->b_resid = shortfall;
13221                 }
13222                 ASSERT(copy_length >= orig_bp->b_resid);
13223                 copy_length  -= orig_bp->b_resid;
13224         }
13225 
13226         /* Propagate the error code from the shadow buf to the original buf */
13227         bioerror(orig_bp, bp->b_error);
13228 
13229         if (is_write) {
13230                 goto freebuf_done;      /* No data copying for a WRITE */
13231         }
13232 
13233         if (has_wmap) {
13234                 /*
13235                  * This is a READ command from the READ phase of a
13236                  * read-modify-write request. We have to copy the data given
13237                  * by the user OVER the data returned by the READ command,
13238                  * then convert the command from a READ to a WRITE and send
13239                  * it back to the target.
13240                  */
13241                 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset,
13242                     copy_length);
13243 
13244                 bp->b_flags &= ~((int)B_READ);   /* Convert to a WRITE */
13245 
13246                 /*
13247                  * Dispatch the WRITE command to the taskq thread, which
13248                  * will in turn send the command to the target. When the
13249                  * WRITE command completes, we (sd_mapblocksize_iodone())
13250                  * will get called again as part of the iodone chain
13251                  * processing for it. Note that we will still be dealing
13252                  * with the shadow buf at that point.
13253                  */
13254                 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp,
13255                     KM_NOSLEEP) != 0) {
13256                         /*
13257                          * Dispatch was successful so we are done. Return
13258                          * without going any higher up the iodone chain. Do
13259                          * not free up any layer-private data until after the
13260                          * WRITE completes.
13261                          */
13262                         return;
13263                 }
13264 
13265                 /*
13266                  * Dispatch of the WRITE command failed; set up the error
13267                  * condition and send this IO back up the iodone chain.
13268                  */
13269                 bioerror(orig_bp, EIO);
13270                 orig_bp->b_resid = orig_bp->b_bcount;
13271 
13272         } else {
13273                 /*
13274                  * This is a regular READ request (ie, not a RMW). Copy the
13275                  * data from the shadow buf into the original buf. The
13276                  * copy_offset compensates for any "misalignment" between the
13277                  * shadow buf (with its un->un_tgt_blocksize blocks) and the
13278                  * original buf (with its un->un_sys_blocksize blocks).
13279                  */
13280                 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr,
13281                     copy_length);
13282         }
13283 
13284 freebuf_done:
13285 
13286         /*
13287          * At this point we still have both the shadow buf AND the original
13288          * buf to deal with, as well as the layer-private data area in each.
13289          * Local variables are as follows:
13290          *
13291          * bp -- points to shadow buf
13292          * xp -- points to xbuf of shadow buf
13293          * bsp -- points to layer-private data area of shadow buf
13294          * orig_bp -- points to original buf
13295          *
13296          * First free the shadow buf and its associated xbuf, then free the
13297          * layer-private data area from the shadow buf. There is no need to
13298          * restore xb_private in the shadow xbuf.
13299          */
13300         sd_shadow_buf_free(bp);
13301         kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13302 
13303         /*
13304          * Now update the local variables to point to the original buf, xbuf,
13305          * and layer-private area.
13306          */
13307         bp = orig_bp;
13308         xp = SD_GET_XBUF(bp);
13309         ASSERT(xp != NULL);
13310         ASSERT(xp == orig_xp);
13311         bsp = xp->xb_private;
13312         ASSERT(bsp != NULL);
13313 
13314 done:
13315         /*
13316          * Restore xb_private to whatever it was set to by the next higher
13317          * layer in the chain, then free the layer-private data area.
13318          */
13319         xp->xb_private = bsp->mbs_oprivate;
13320         kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13321 
13322 exit:
13323         SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp),
13324             "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp);
13325 
13326         SD_NEXT_IODONE(index, un, bp);
13327 }
13328 
13329 
13330 /*
13331  *    Function: sd_checksum_iostart
13332  *
13333  * Description: A stub function for a layer that's currently not used.
13334  *              For now just a placeholder.
13335  *
13336  *     Context: Kernel thread context
13337  */
13338 
13339 static void
13340 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp)
13341 {
13342         ASSERT(un != NULL);
13343         ASSERT(bp != NULL);
13344         ASSERT(!mutex_owned(SD_MUTEX(un)));
13345         SD_NEXT_IOSTART(index, un, bp);
13346 }
13347 
13348 
13349 /*
13350  *    Function: sd_checksum_iodone
13351  *
13352  * Description: A stub function for a layer that's currently not used.
13353  *              For now just a placeholder.
13354  *
13355  *     Context: May be called under interrupt context
13356  */
13357 
13358 static void
13359 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp)
13360 {
13361         ASSERT(un != NULL);
13362         ASSERT(bp != NULL);
13363         ASSERT(!mutex_owned(SD_MUTEX(un)));
13364         SD_NEXT_IODONE(index, un, bp);
13365 }
13366 
13367 
13368 /*
13369  *    Function: sd_checksum_uscsi_iostart
13370  *
13371  * Description: A stub function for a layer that's currently not used.
13372  *              For now just a placeholder.
13373  *
13374  *     Context: Kernel thread context
13375  */
13376 
13377 static void
13378 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp)
13379 {
13380         ASSERT(un != NULL);
13381         ASSERT(bp != NULL);
13382         ASSERT(!mutex_owned(SD_MUTEX(un)));
13383         SD_NEXT_IOSTART(index, un, bp);
13384 }
13385 
13386 
13387 /*
13388  *    Function: sd_checksum_uscsi_iodone
13389  *
13390  * Description: A stub function for a layer that's currently not used.
13391  *              For now just a placeholder.
13392  *
13393  *     Context: May be called under interrupt context
13394  */
13395 
13396 static void
13397 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
13398 {
13399         ASSERT(un != NULL);
13400         ASSERT(bp != NULL);
13401         ASSERT(!mutex_owned(SD_MUTEX(un)));
13402         SD_NEXT_IODONE(index, un, bp);
13403 }
13404 
13405 
13406 /*
13407  *    Function: sd_pm_iostart
13408  *
13409  * Description: iostart-side routine for Power mangement.
13410  *
13411  *     Context: Kernel thread context
13412  */
13413 
13414 static void
13415 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp)
13416 {
13417         ASSERT(un != NULL);
13418         ASSERT(bp != NULL);
13419         ASSERT(!mutex_owned(SD_MUTEX(un)));
13420         ASSERT(!mutex_owned(&un->un_pm_mutex));
13421 
13422         SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n");
13423 
13424         if (sd_pm_entry(un) != DDI_SUCCESS) {
13425                 /*
13426                  * Set up to return the failed buf back up the 'iodone'
13427                  * side of the calling chain.
13428                  */
13429                 bioerror(bp, EIO);
13430                 bp->b_resid = bp->b_bcount;
13431 
13432                 SD_BEGIN_IODONE(index, un, bp);
13433 
13434                 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13435                 return;
13436         }
13437 
13438         SD_NEXT_IOSTART(index, un, bp);
13439 
13440         SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13441 }
13442 
13443 
13444 /*
13445  *    Function: sd_pm_iodone
13446  *
13447  * Description: iodone-side routine for power mangement.
13448  *
13449  *     Context: may be called from interrupt context
13450  */
13451 
13452 static void
13453 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp)
13454 {
13455         ASSERT(un != NULL);
13456         ASSERT(bp != NULL);
13457         ASSERT(!mutex_owned(&un->un_pm_mutex));
13458 
13459         SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n");
13460 
13461         /*
13462          * After attach the following flag is only read, so don't
13463          * take the penalty of acquiring a mutex for it.
13464          */
13465         if (un->un_f_pm_is_enabled == TRUE) {
13466                 sd_pm_exit(un);
13467         }
13468 
13469         SD_NEXT_IODONE(index, un, bp);
13470 
13471         SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n");
13472 }
13473 
13474 
13475 /*
13476  *    Function: sd_core_iostart
13477  *
13478  * Description: Primary driver function for enqueuing buf(9S) structs from
13479  *              the system and initiating IO to the target device
13480  *
13481  *     Context: Kernel thread context. Can sleep.
13482  *
13483  * Assumptions:  - The given xp->xb_blkno is absolute
13484  *                 (ie, relative to the start of the device).
13485  *               - The IO is to be done using the native blocksize of
13486  *                 the device, as specified in un->un_tgt_blocksize.
13487  */
13488 /* ARGSUSED */
13489 static void
13490 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp)
13491 {
13492         struct sd_xbuf *xp;
13493 
13494         ASSERT(un != NULL);
13495         ASSERT(bp != NULL);
13496         ASSERT(!mutex_owned(SD_MUTEX(un)));
13497         ASSERT(bp->b_resid == 0);
13498 
13499         SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp);
13500 
13501         xp = SD_GET_XBUF(bp);
13502         ASSERT(xp != NULL);
13503 
13504         mutex_enter(SD_MUTEX(un));
13505 
13506         /*
13507          * If we are currently in the failfast state, fail any new IO
13508          * that has B_FAILFAST set, then return.
13509          */
13510         if ((bp->b_flags & B_FAILFAST) &&
13511             (un->un_failfast_state == SD_FAILFAST_ACTIVE)) {
13512                 mutex_exit(SD_MUTEX(un));
13513                 bioerror(bp, EIO);
13514                 bp->b_resid = bp->b_bcount;
13515                 SD_BEGIN_IODONE(index, un, bp);
13516                 return;
13517         }
13518 
13519         if (SD_IS_DIRECT_PRIORITY(xp)) {
13520                 /*
13521                  * Priority command -- transport it immediately.
13522                  *
13523                  * Note: We may want to assert that USCSI_DIAGNOSE is set,
13524                  * because all direct priority commands should be associated
13525                  * with error recovery actions which we don't want to retry.
13526                  */
13527                 sd_start_cmds(un, bp);
13528         } else {
13529                 /*
13530                  * Normal command -- add it to the wait queue, then start
13531                  * transporting commands from the wait queue.
13532                  */
13533                 sd_add_buf_to_waitq(un, bp);
13534                 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
13535                 sd_start_cmds(un, NULL);
13536         }
13537 
13538         mutex_exit(SD_MUTEX(un));
13539 
13540         SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp);
13541 }
13542 
13543 
13544 /*
13545  *    Function: sd_init_cdb_limits
13546  *
13547  * Description: This is to handle scsi_pkt initialization differences
13548  *              between the driver platforms.
13549  *
13550  *              Legacy behaviors:
13551  *
13552  *              If the block number or the sector count exceeds the
13553  *              capabilities of a Group 0 command, shift over to a
13554  *              Group 1 command. We don't blindly use Group 1
13555  *              commands because a) some drives (CDC Wren IVs) get a
13556  *              bit confused, and b) there is probably a fair amount
13557  *              of speed difference for a target to receive and decode
13558  *              a 10 byte command instead of a 6 byte command.
13559  *
13560  *              The xfer time difference of 6 vs 10 byte CDBs is
13561  *              still significant so this code is still worthwhile.
13562  *              10 byte CDBs are very inefficient with the fas HBA driver
13563  *              and older disks. Each CDB byte took 1 usec with some
13564  *              popular disks.
13565  *
13566  *     Context: Must be called at attach time
13567  */
13568 
13569 static void
13570 sd_init_cdb_limits(struct sd_lun *un)
13571 {
13572         int hba_cdb_limit;
13573 
13574         /*
13575          * Use CDB_GROUP1 commands for most devices except for
13576          * parallel SCSI fixed drives in which case we get better
13577          * performance using CDB_GROUP0 commands (where applicable).
13578          */
13579         un->un_mincdb = SD_CDB_GROUP1;
13580 #if !defined(__fibre)
13581         if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) &&
13582             !un->un_f_has_removable_media) {
13583                 un->un_mincdb = SD_CDB_GROUP0;
13584         }
13585 #endif
13586 
13587         /*
13588          * Try to read the max-cdb-length supported by HBA.
13589          */
13590         un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1);
13591         if (0 >= un->un_max_hba_cdb) {
13592                 un->un_max_hba_cdb = CDB_GROUP4;
13593                 hba_cdb_limit = SD_CDB_GROUP4;
13594         } else if (0 < un->un_max_hba_cdb &&
13595             un->un_max_hba_cdb < CDB_GROUP1) {
13596                 hba_cdb_limit = SD_CDB_GROUP0;
13597         } else if (CDB_GROUP1 <= un->un_max_hba_cdb &&
13598             un->un_max_hba_cdb < CDB_GROUP5) {
13599                 hba_cdb_limit = SD_CDB_GROUP1;
13600         } else if (CDB_GROUP5 <= un->un_max_hba_cdb &&
13601             un->un_max_hba_cdb < CDB_GROUP4) {
13602                 hba_cdb_limit = SD_CDB_GROUP5;
13603         } else {
13604                 hba_cdb_limit = SD_CDB_GROUP4;
13605         }
13606 
13607         /*
13608          * Use CDB_GROUP5 commands for removable devices.  Use CDB_GROUP4
13609          * commands for fixed disks unless we are building for a 32 bit
13610          * kernel.
13611          */
13612 #ifdef _LP64
13613         un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13614             min(hba_cdb_limit, SD_CDB_GROUP4);
13615 #else
13616         un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13617             min(hba_cdb_limit, SD_CDB_GROUP1);
13618 #endif
13619 
13620         un->un_status_len = (int)((un->un_f_arq_enabled == TRUE)
13621             ? sizeof (struct scsi_arq_status) : 1);
13622         if (!ISCD(un))
13623                 un->un_cmd_timeout = (ushort_t)sd_io_time;
13624         un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout;
13625 }
13626 
13627 
13628 /*
13629  *    Function: sd_initpkt_for_buf
13630  *
13631  * Description: Allocate and initialize for transport a scsi_pkt struct,
13632  *              based upon the info specified in the given buf struct.
13633  *
13634  *              Assumes the xb_blkno in the request is absolute (ie,
13635  *              relative to the start of the device (NOT partition!).
13636  *              Also assumes that the request is using the native block
13637  *              size of the device (as returned by the READ CAPACITY
13638  *              command).
13639  *
13640  * Return Code: SD_PKT_ALLOC_SUCCESS
13641  *              SD_PKT_ALLOC_FAILURE
13642  *              SD_PKT_ALLOC_FAILURE_NO_DMA
13643  *              SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13644  *
13645  *     Context: Kernel thread and may be called from software interrupt context
13646  *              as part of a sdrunout callback. This function may not block or
13647  *              call routines that block
13648  */
13649 
13650 static int
13651 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp)
13652 {
13653         struct sd_xbuf  *xp;
13654         struct scsi_pkt *pktp = NULL;
13655         struct sd_lun   *un;
13656         size_t          blockcount;
13657         daddr_t         startblock;
13658         int             rval;
13659         int             cmd_flags;
13660 
13661         ASSERT(bp != NULL);
13662         ASSERT(pktpp != NULL);
13663         xp = SD_GET_XBUF(bp);
13664         ASSERT(xp != NULL);
13665         un = SD_GET_UN(bp);
13666         ASSERT(un != NULL);
13667         ASSERT(mutex_owned(SD_MUTEX(un)));
13668         ASSERT(bp->b_resid == 0);
13669 
13670         SD_TRACE(SD_LOG_IO_CORE, un,
13671             "sd_initpkt_for_buf: entry: buf:0x%p\n", bp);
13672 
13673         mutex_exit(SD_MUTEX(un));
13674 
13675 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
13676         if (xp->xb_pkt_flags & SD_XB_DMA_FREED) {
13677                 /*
13678                  * Already have a scsi_pkt -- just need DMA resources.
13679                  * We must recompute the CDB in case the mapping returns
13680                  * a nonzero pkt_resid.
13681                  * Note: if this is a portion of a PKT_DMA_PARTIAL transfer
13682                  * that is being retried, the unmap/remap of the DMA resouces
13683                  * will result in the entire transfer starting over again
13684                  * from the very first block.
13685                  */
13686                 ASSERT(xp->xb_pktp != NULL);
13687                 pktp = xp->xb_pktp;
13688         } else {
13689                 pktp = NULL;
13690         }
13691 #endif /* __i386 || __amd64 */
13692 
13693         startblock = xp->xb_blkno;   /* Absolute block num. */
13694         blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
13695 
13696         cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK);
13697 
13698         /*
13699          * sd_setup_rw_pkt will determine the appropriate CDB group to use,
13700          * call scsi_init_pkt, and build the CDB.
13701          */
13702         rval = sd_setup_rw_pkt(un, &pktp, bp,
13703             cmd_flags, sdrunout, (caddr_t)un,
13704             startblock, blockcount);
13705 
13706         if (rval == 0) {
13707                 /*
13708                  * Success.
13709                  *
13710                  * If partial DMA is being used and required for this transfer.
13711                  * set it up here.
13712                  */
13713                 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 &&
13714                     (pktp->pkt_resid != 0)) {
13715 
13716                         /*
13717                          * Save the CDB length and pkt_resid for the
13718                          * next xfer
13719                          */
13720                         xp->xb_dma_resid = pktp->pkt_resid;
13721 
13722                         /* rezero resid */
13723                         pktp->pkt_resid = 0;
13724 
13725                 } else {
13726                         xp->xb_dma_resid = 0;
13727                 }
13728 
13729                 pktp->pkt_flags = un->un_tagflags;
13730                 pktp->pkt_time  = un->un_cmd_timeout;
13731                 pktp->pkt_comp  = sdintr;
13732 
13733                 pktp->pkt_private = bp;
13734                 *pktpp = pktp;
13735 
13736                 SD_TRACE(SD_LOG_IO_CORE, un,
13737                     "sd_initpkt_for_buf: exit: buf:0x%p\n", bp);
13738 
13739 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
13740                 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED;
13741 #endif
13742 
13743                 mutex_enter(SD_MUTEX(un));
13744                 return (SD_PKT_ALLOC_SUCCESS);
13745 
13746         }
13747 
13748         /*
13749          * SD_PKT_ALLOC_FAILURE is the only expected failure code
13750          * from sd_setup_rw_pkt.
13751          */
13752         ASSERT(rval == SD_PKT_ALLOC_FAILURE);
13753 
13754         if (rval == SD_PKT_ALLOC_FAILURE) {
13755                 *pktpp = NULL;
13756                 /*
13757                  * Set the driver state to RWAIT to indicate the driver
13758                  * is waiting on resource allocations. The driver will not
13759                  * suspend, pm_suspend, or detatch while the state is RWAIT.
13760                  */
13761                 mutex_enter(SD_MUTEX(un));
13762                 New_state(un, SD_STATE_RWAIT);
13763 
13764                 SD_ERROR(SD_LOG_IO_CORE, un,
13765                     "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp);
13766 
13767                 if ((bp->b_flags & B_ERROR) != 0) {
13768                         return (SD_PKT_ALLOC_FAILURE_NO_DMA);
13769                 }
13770                 return (SD_PKT_ALLOC_FAILURE);
13771         } else {
13772                 /*
13773                  * PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13774                  *
13775                  * This should never happen.  Maybe someone messed with the
13776                  * kernel's minphys?
13777                  */
13778                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
13779                     "Request rejected: too large for CDB: "
13780                     "lba:0x%08lx  len:0x%08lx\n", startblock, blockcount);
13781                 SD_ERROR(SD_LOG_IO_CORE, un,
13782                     "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp);
13783                 mutex_enter(SD_MUTEX(un));
13784                 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13785 
13786         }
13787 }
13788 
13789 
13790 /*
13791  *    Function: sd_destroypkt_for_buf
13792  *
13793  * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing).
13794  *
13795  *     Context: Kernel thread or interrupt context
13796  */
13797 
13798 static void
13799 sd_destroypkt_for_buf(struct buf *bp)
13800 {
13801         ASSERT(bp != NULL);
13802         ASSERT(SD_GET_UN(bp) != NULL);
13803 
13804         SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13805             "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp);
13806 
13807         ASSERT(SD_GET_PKTP(bp) != NULL);
13808         scsi_destroy_pkt(SD_GET_PKTP(bp));
13809 
13810         SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13811             "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp);
13812 }
13813 
13814 /*
13815  *    Function: sd_setup_rw_pkt
13816  *
13817  * Description: Determines appropriate CDB group for the requested LBA
13818  *              and transfer length, calls scsi_init_pkt, and builds
13819  *              the CDB.  Do not use for partial DMA transfers except
13820  *              for the initial transfer since the CDB size must
13821  *              remain constant.
13822  *
13823  *     Context: Kernel thread and may be called from software interrupt
13824  *              context as part of a sdrunout callback. This function may not
13825  *              block or call routines that block
13826  */
13827 
13828 
13829 int
13830 sd_setup_rw_pkt(struct sd_lun *un,
13831     struct scsi_pkt **pktpp, struct buf *bp, int flags,
13832     int (*callback)(caddr_t), caddr_t callback_arg,
13833     diskaddr_t lba, uint32_t blockcount)
13834 {
13835         struct scsi_pkt *return_pktp;
13836         union scsi_cdb *cdbp;
13837         struct sd_cdbinfo *cp = NULL;
13838         int i;
13839 
13840         /*
13841          * See which size CDB to use, based upon the request.
13842          */
13843         for (i = un->un_mincdb; i <= un->un_maxcdb; i++) {
13844 
13845                 /*
13846                  * Check lba and block count against sd_cdbtab limits.
13847                  * In the partial DMA case, we have to use the same size
13848                  * CDB for all the transfers.  Check lba + blockcount
13849                  * against the max LBA so we know that segment of the
13850                  * transfer can use the CDB we select.
13851                  */
13852                 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) &&
13853                     (blockcount <= sd_cdbtab[i].sc_maxlen)) {
13854 
13855                         /*
13856                          * The command will fit into the CDB type
13857                          * specified by sd_cdbtab[i].
13858                          */
13859                         cp = sd_cdbtab + i;
13860 
13861                         /*
13862                          * Call scsi_init_pkt so we can fill in the
13863                          * CDB.
13864                          */
13865                         return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp,
13866                             bp, cp->sc_grpcode, un->un_status_len, 0,
13867                             flags, callback, callback_arg);
13868 
13869                         if (return_pktp != NULL) {
13870 
13871                                 /*
13872                                  * Return new value of pkt
13873                                  */
13874                                 *pktpp = return_pktp;
13875 
13876                                 /*
13877                                  * To be safe, zero the CDB insuring there is
13878                                  * no leftover data from a previous command.
13879                                  */
13880                                 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode);
13881 
13882                                 /*
13883                                  * Handle partial DMA mapping
13884                                  */
13885                                 if (return_pktp->pkt_resid != 0) {
13886 
13887                                         /*
13888                                          * Not going to xfer as many blocks as
13889                                          * originally expected
13890                                          */
13891                                         blockcount -=
13892                                             SD_BYTES2TGTBLOCKS(un,
13893                                             return_pktp->pkt_resid);
13894                                 }
13895 
13896                                 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp;
13897 
13898                                 /*
13899                                  * Set command byte based on the CDB
13900                                  * type we matched.
13901                                  */
13902                                 cdbp->scc_cmd = cp->sc_grpmask |
13903                                     ((bp->b_flags & B_READ) ?
13904                                     SCMD_READ : SCMD_WRITE);
13905 
13906                                 SD_FILL_SCSI1_LUN(un, return_pktp);
13907 
13908                                 /*
13909                                  * Fill in LBA and length
13910                                  */
13911                                 ASSERT((cp->sc_grpcode == CDB_GROUP1) ||
13912                                     (cp->sc_grpcode == CDB_GROUP4) ||
13913                                     (cp->sc_grpcode == CDB_GROUP0) ||
13914                                     (cp->sc_grpcode == CDB_GROUP5));
13915 
13916                                 if (cp->sc_grpcode == CDB_GROUP1) {
13917                                         FORMG1ADDR(cdbp, lba);
13918                                         FORMG1COUNT(cdbp, blockcount);
13919                                         return (0);
13920                                 } else if (cp->sc_grpcode == CDB_GROUP4) {
13921                                         FORMG4LONGADDR(cdbp, lba);
13922                                         FORMG4COUNT(cdbp, blockcount);
13923                                         return (0);
13924                                 } else if (cp->sc_grpcode == CDB_GROUP0) {
13925                                         FORMG0ADDR(cdbp, lba);
13926                                         FORMG0COUNT(cdbp, blockcount);
13927                                         return (0);
13928                                 } else if (cp->sc_grpcode == CDB_GROUP5) {
13929                                         FORMG5ADDR(cdbp, lba);
13930                                         FORMG5COUNT(cdbp, blockcount);
13931                                         return (0);
13932                                 }
13933 
13934                                 /*
13935                                  * It should be impossible to not match one
13936                                  * of the CDB types above, so we should never
13937                                  * reach this point.  Set the CDB command byte
13938                                  * to test-unit-ready to avoid writing
13939                                  * to somewhere we don't intend.
13940                                  */
13941                                 cdbp->scc_cmd = SCMD_TEST_UNIT_READY;
13942                                 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13943                         } else {
13944                                 /*
13945                                  * Couldn't get scsi_pkt
13946                                  */
13947                                 return (SD_PKT_ALLOC_FAILURE);
13948                         }
13949                 }
13950         }
13951 
13952         /*
13953          * None of the available CDB types were suitable.  This really
13954          * should never happen:  on a 64 bit system we support
13955          * READ16/WRITE16 which will hold an entire 64 bit disk address
13956          * and on a 32 bit system we will refuse to bind to a device
13957          * larger than 2TB so addresses will never be larger than 32 bits.
13958          */
13959         return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13960 }
13961 
13962 /*
13963  *    Function: sd_setup_next_rw_pkt
13964  *
13965  * Description: Setup packet for partial DMA transfers, except for the
13966  *              initial transfer.  sd_setup_rw_pkt should be used for
13967  *              the initial transfer.
13968  *
13969  *     Context: Kernel thread and may be called from interrupt context.
13970  */
13971 
13972 int
13973 sd_setup_next_rw_pkt(struct sd_lun *un,
13974     struct scsi_pkt *pktp, struct buf *bp,
13975     diskaddr_t lba, uint32_t blockcount)
13976 {
13977         uchar_t com;
13978         union scsi_cdb *cdbp;
13979         uchar_t cdb_group_id;
13980 
13981         ASSERT(pktp != NULL);
13982         ASSERT(pktp->pkt_cdbp != NULL);
13983 
13984         cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
13985         com = cdbp->scc_cmd;
13986         cdb_group_id = CDB_GROUPID(com);
13987 
13988         ASSERT((cdb_group_id == CDB_GROUPID_0) ||
13989             (cdb_group_id == CDB_GROUPID_1) ||
13990             (cdb_group_id == CDB_GROUPID_4) ||
13991             (cdb_group_id == CDB_GROUPID_5));
13992 
13993         /*
13994          * Move pkt to the next portion of the xfer.
13995          * func is NULL_FUNC so we do not have to release
13996          * the disk mutex here.
13997          */
13998         if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0,
13999             NULL_FUNC, NULL) == pktp) {
14000                 /* Success.  Handle partial DMA */
14001                 if (pktp->pkt_resid != 0) {
14002                         blockcount -=
14003                             SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid);
14004                 }
14005 
14006                 cdbp->scc_cmd = com;
14007                 SD_FILL_SCSI1_LUN(un, pktp);
14008                 if (cdb_group_id == CDB_GROUPID_1) {
14009                         FORMG1ADDR(cdbp, lba);
14010                         FORMG1COUNT(cdbp, blockcount);
14011                         return (0);
14012                 } else if (cdb_group_id == CDB_GROUPID_4) {
14013                         FORMG4LONGADDR(cdbp, lba);
14014                         FORMG4COUNT(cdbp, blockcount);
14015                         return (0);
14016                 } else if (cdb_group_id == CDB_GROUPID_0) {
14017                         FORMG0ADDR(cdbp, lba);
14018                         FORMG0COUNT(cdbp, blockcount);
14019                         return (0);
14020                 } else if (cdb_group_id == CDB_GROUPID_5) {
14021                         FORMG5ADDR(cdbp, lba);
14022                         FORMG5COUNT(cdbp, blockcount);
14023                         return (0);
14024                 }
14025 
14026                 /* Unreachable */
14027                 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
14028         }
14029 
14030         /*
14031          * Error setting up next portion of cmd transfer.
14032          * Something is definitely very wrong and this
14033          * should not happen.
14034          */
14035         return (SD_PKT_ALLOC_FAILURE);
14036 }
14037 
14038 /*
14039  *    Function: sd_initpkt_for_uscsi
14040  *
14041  * Description: Allocate and initialize for transport a scsi_pkt struct,
14042  *              based upon the info specified in the given uscsi_cmd struct.
14043  *
14044  * Return Code: SD_PKT_ALLOC_SUCCESS
14045  *              SD_PKT_ALLOC_FAILURE
14046  *              SD_PKT_ALLOC_FAILURE_NO_DMA
14047  *              SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
14048  *
14049  *     Context: Kernel thread and may be called from software interrupt context
14050  *              as part of a sdrunout callback. This function may not block or
14051  *              call routines that block
14052  */
14053 
14054 static int
14055 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp)
14056 {
14057         struct uscsi_cmd *uscmd;
14058         struct sd_xbuf  *xp;
14059         struct scsi_pkt *pktp;
14060         struct sd_lun   *un;
14061         uint32_t        flags = 0;
14062 
14063         ASSERT(bp != NULL);
14064         ASSERT(pktpp != NULL);
14065         xp = SD_GET_XBUF(bp);
14066         ASSERT(xp != NULL);
14067         un = SD_GET_UN(bp);
14068         ASSERT(un != NULL);
14069         ASSERT(mutex_owned(SD_MUTEX(un)));
14070 
14071         /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
14072         uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
14073         ASSERT(uscmd != NULL);
14074 
14075         SD_TRACE(SD_LOG_IO_CORE, un,
14076             "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp);
14077 
14078         /*
14079          * Allocate the scsi_pkt for the command.
14080          * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path
14081          *       during scsi_init_pkt time and will continue to use the
14082          *       same path as long as the same scsi_pkt is used without
14083          *       intervening scsi_dma_free(). Since uscsi command does
14084          *       not call scsi_dmafree() before retry failed command, it
14085          *       is necessary to make sure PKT_DMA_PARTIAL flag is NOT
14086          *       set such that scsi_vhci can use other available path for
14087          *       retry. Besides, ucsci command does not allow DMA breakup,
14088          *       so there is no need to set PKT_DMA_PARTIAL flag.
14089          */
14090         if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
14091                 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
14092                     ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
14093                     ((int)(uscmd->uscsi_rqlen) + sizeof (struct scsi_arq_status)
14094                     - sizeof (struct scsi_extended_sense)), 0,
14095                     (un->un_pkt_flags & ~PKT_DMA_PARTIAL) | PKT_XARQ,
14096                     sdrunout, (caddr_t)un);
14097         } else {
14098                 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
14099                     ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
14100                     sizeof (struct scsi_arq_status), 0,
14101                     (un->un_pkt_flags & ~PKT_DMA_PARTIAL),
14102                     sdrunout, (caddr_t)un);
14103         }
14104 
14105         if (pktp == NULL) {
14106                 *pktpp = NULL;
14107                 /*
14108                  * Set the driver state to RWAIT to indicate the driver
14109                  * is waiting on resource allocations. The driver will not
14110                  * suspend, pm_suspend, or detatch while the state is RWAIT.
14111                  */
14112                 New_state(un, SD_STATE_RWAIT);
14113 
14114                 SD_ERROR(SD_LOG_IO_CORE, un,
14115                     "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp);
14116 
14117                 if ((bp->b_flags & B_ERROR) != 0) {
14118                         return (SD_PKT_ALLOC_FAILURE_NO_DMA);
14119                 }
14120                 return (SD_PKT_ALLOC_FAILURE);
14121         }
14122 
14123         /*
14124          * We do not do DMA breakup for USCSI commands, so return failure
14125          * here if all the needed DMA resources were not allocated.
14126          */
14127         if ((un->un_pkt_flags & PKT_DMA_PARTIAL) &&
14128             (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) {
14129                 scsi_destroy_pkt(pktp);
14130                 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: "
14131                     "No partial DMA for USCSI. exit: buf:0x%p\n", bp);
14132                 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL);
14133         }
14134 
14135         /* Init the cdb from the given uscsi struct */
14136         (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp,
14137             uscmd->uscsi_cdb[0], 0, 0, 0);
14138 
14139         SD_FILL_SCSI1_LUN(un, pktp);
14140 
14141         /*
14142          * Set up the optional USCSI flags. See the uscsi (7I) man page
14143          * for listing of the supported flags.
14144          */
14145 
14146         if (uscmd->uscsi_flags & USCSI_SILENT) {
14147                 flags |= FLAG_SILENT;
14148         }
14149 
14150         if (uscmd->uscsi_flags & USCSI_DIAGNOSE) {
14151                 flags |= FLAG_DIAGNOSE;
14152         }
14153 
14154         if (uscmd->uscsi_flags & USCSI_ISOLATE) {
14155                 flags |= FLAG_ISOLATE;
14156         }
14157 
14158         if (un->un_f_is_fibre == FALSE) {
14159                 if (uscmd->uscsi_flags & USCSI_RENEGOT) {
14160                         flags |= FLAG_RENEGOTIATE_WIDE_SYNC;
14161                 }
14162         }
14163 
14164         /*
14165          * Set the pkt flags here so we save time later.
14166          * Note: These flags are NOT in the uscsi man page!!!
14167          */
14168         if (uscmd->uscsi_flags & USCSI_HEAD) {
14169                 flags |= FLAG_HEAD;
14170         }
14171 
14172         if (uscmd->uscsi_flags & USCSI_NOINTR) {
14173                 flags |= FLAG_NOINTR;
14174         }
14175 
14176         /*
14177          * For tagged queueing, things get a bit complicated.
14178          * Check first for head of queue and last for ordered queue.
14179          * If neither head nor order, use the default driver tag flags.
14180          */
14181         if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) {
14182                 if (uscmd->uscsi_flags & USCSI_HTAG) {
14183                         flags |= FLAG_HTAG;
14184                 } else if (uscmd->uscsi_flags & USCSI_OTAG) {
14185                         flags |= FLAG_OTAG;
14186                 } else {
14187                         flags |= un->un_tagflags & FLAG_TAGMASK;
14188                 }
14189         }
14190 
14191         if (uscmd->uscsi_flags & USCSI_NODISCON) {
14192                 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON;
14193         }
14194 
14195         pktp->pkt_flags = flags;
14196 
14197         /* Transfer uscsi information to scsi_pkt */
14198         (void) scsi_uscsi_pktinit(uscmd, pktp);
14199 
14200         /* Copy the caller's CDB into the pkt... */
14201         bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen);
14202 
14203         if (uscmd->uscsi_timeout == 0) {
14204                 pktp->pkt_time = un->un_uscsi_timeout;
14205         } else {
14206                 pktp->pkt_time = uscmd->uscsi_timeout;
14207         }
14208 
14209         /* need it later to identify USCSI request in sdintr */
14210         xp->xb_pkt_flags |= SD_XB_USCSICMD;
14211 
14212         xp->xb_sense_resid = uscmd->uscsi_rqresid;
14213 
14214         pktp->pkt_private = bp;
14215         pktp->pkt_comp = sdintr;
14216         *pktpp = pktp;
14217 
14218         SD_TRACE(SD_LOG_IO_CORE, un,
14219             "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp);
14220 
14221         return (SD_PKT_ALLOC_SUCCESS);
14222 }
14223 
14224 
14225 /*
14226  *    Function: sd_destroypkt_for_uscsi
14227  *
14228  * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi
14229  *              IOs.. Also saves relevant info into the associated uscsi_cmd
14230  *              struct.
14231  *
14232  *     Context: May be called under interrupt context
14233  */
14234 
14235 static void
14236 sd_destroypkt_for_uscsi(struct buf *bp)
14237 {
14238         struct uscsi_cmd *uscmd;
14239         struct sd_xbuf  *xp;
14240         struct scsi_pkt *pktp;
14241         struct sd_lun   *un;
14242         struct sd_uscsi_info *suip;
14243 
14244         ASSERT(bp != NULL);
14245         xp = SD_GET_XBUF(bp);
14246         ASSERT(xp != NULL);
14247         un = SD_GET_UN(bp);
14248         ASSERT(un != NULL);
14249         ASSERT(!mutex_owned(SD_MUTEX(un)));
14250         pktp = SD_GET_PKTP(bp);
14251         ASSERT(pktp != NULL);
14252 
14253         SD_TRACE(SD_LOG_IO_CORE, un,
14254             "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp);
14255 
14256         /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
14257         uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
14258         ASSERT(uscmd != NULL);
14259 
14260         /* Save the status and the residual into the uscsi_cmd struct */
14261         uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
14262         uscmd->uscsi_resid  = bp->b_resid;
14263 
14264         /* Transfer scsi_pkt information to uscsi */
14265         (void) scsi_uscsi_pktfini(pktp, uscmd);
14266 
14267         /*
14268          * If enabled, copy any saved sense data into the area specified
14269          * by the uscsi command.
14270          */
14271         if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
14272             (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
14273                 /*
14274                  * Note: uscmd->uscsi_rqbuf should always point to a buffer
14275                  * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd())
14276                  */
14277                 uscmd->uscsi_rqstatus = xp->xb_sense_status;
14278                 uscmd->uscsi_rqresid  = xp->xb_sense_resid;
14279                 if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
14280                         bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
14281                             MAX_SENSE_LENGTH);
14282                 } else {
14283                         bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
14284                             SENSE_LENGTH);
14285                 }
14286         }
14287         /*
14288          * The following assignments are for SCSI FMA.
14289          */
14290         ASSERT(xp->xb_private != NULL);
14291         suip = (struct sd_uscsi_info *)xp->xb_private;
14292         suip->ui_pkt_reason = pktp->pkt_reason;
14293         suip->ui_pkt_state = pktp->pkt_state;
14294         suip->ui_pkt_statistics = pktp->pkt_statistics;
14295         suip->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
14296 
14297         /* We are done with the scsi_pkt; free it now */
14298         ASSERT(SD_GET_PKTP(bp) != NULL);
14299         scsi_destroy_pkt(SD_GET_PKTP(bp));
14300 
14301         SD_TRACE(SD_LOG_IO_CORE, un,
14302             "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp);
14303 }
14304 
14305 
14306 /*
14307  *    Function: sd_bioclone_alloc
14308  *
14309  * Description: Allocate a buf(9S) and init it as per the given buf
14310  *              and the various arguments.  The associated sd_xbuf
14311  *              struct is (nearly) duplicated.  The struct buf *bp
14312  *              argument is saved in new_xp->xb_private.
14313  *
14314  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
14315  *              datalen - size of data area for the shadow bp
14316  *              blkno - starting LBA
14317  *              func - function pointer for b_iodone in the shadow buf. (May
14318  *                      be NULL if none.)
14319  *
14320  * Return Code: Pointer to allocates buf(9S) struct
14321  *
14322  *     Context: Can sleep.
14323  */
14324 
14325 static struct buf *
14326 sd_bioclone_alloc(struct buf *bp, size_t datalen, daddr_t blkno,
14327     int (*func)(struct buf *))
14328 {
14329         struct  sd_lun  *un;
14330         struct  sd_xbuf *xp;
14331         struct  sd_xbuf *new_xp;
14332         struct  buf     *new_bp;
14333 
14334         ASSERT(bp != NULL);
14335         xp = SD_GET_XBUF(bp);
14336         ASSERT(xp != NULL);
14337         un = SD_GET_UN(bp);
14338         ASSERT(un != NULL);
14339         ASSERT(!mutex_owned(SD_MUTEX(un)));
14340 
14341         new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func,
14342             NULL, KM_SLEEP);
14343 
14344         new_bp->b_lblkno     = blkno;
14345 
14346         /*
14347          * Allocate an xbuf for the shadow bp and copy the contents of the
14348          * original xbuf into it.
14349          */
14350         new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14351         bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14352 
14353         /*
14354          * The given bp is automatically saved in the xb_private member
14355          * of the new xbuf.  Callers are allowed to depend on this.
14356          */
14357         new_xp->xb_private = bp;
14358 
14359         new_bp->b_private  = new_xp;
14360 
14361         return (new_bp);
14362 }
14363 
14364 /*
14365  *    Function: sd_shadow_buf_alloc
14366  *
14367  * Description: Allocate a buf(9S) and init it as per the given buf
14368  *              and the various arguments.  The associated sd_xbuf
14369  *              struct is (nearly) duplicated.  The struct buf *bp
14370  *              argument is saved in new_xp->xb_private.
14371  *
14372  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
14373  *              datalen - size of data area for the shadow bp
14374  *              bflags - B_READ or B_WRITE (pseudo flag)
14375  *              blkno - starting LBA
14376  *              func - function pointer for b_iodone in the shadow buf. (May
14377  *                      be NULL if none.)
14378  *
14379  * Return Code: Pointer to allocates buf(9S) struct
14380  *
14381  *     Context: Can sleep.
14382  */
14383 
14384 static struct buf *
14385 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags,
14386     daddr_t blkno, int (*func)(struct buf *))
14387 {
14388         struct  sd_lun  *un;
14389         struct  sd_xbuf *xp;
14390         struct  sd_xbuf *new_xp;
14391         struct  buf     *new_bp;
14392 
14393         ASSERT(bp != NULL);
14394         xp = SD_GET_XBUF(bp);
14395         ASSERT(xp != NULL);
14396         un = SD_GET_UN(bp);
14397         ASSERT(un != NULL);
14398         ASSERT(!mutex_owned(SD_MUTEX(un)));
14399 
14400         if (bp->b_flags & (B_PAGEIO | B_PHYS)) {
14401                 bp_mapin(bp);
14402         }
14403 
14404         bflags &= (B_READ | B_WRITE);
14405 #if defined(__i386) || defined(__amd64)
14406         new_bp = getrbuf(KM_SLEEP);
14407         new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP);
14408         new_bp->b_bcount = datalen;
14409         new_bp->b_flags = bflags |
14410             (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW));
14411 #else
14412         new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL,
14413             datalen, bflags, SLEEP_FUNC, NULL);
14414 #endif
14415         new_bp->av_forw      = NULL;
14416         new_bp->av_back      = NULL;
14417         new_bp->b_dev        = bp->b_dev;
14418         new_bp->b_blkno      = blkno;
14419         new_bp->b_iodone = func;
14420         new_bp->b_edev       = bp->b_edev;
14421         new_bp->b_resid      = 0;
14422 
14423         /* We need to preserve the B_FAILFAST flag */
14424         if (bp->b_flags & B_FAILFAST) {
14425                 new_bp->b_flags |= B_FAILFAST;
14426         }
14427 
14428         /*
14429          * Allocate an xbuf for the shadow bp and copy the contents of the
14430          * original xbuf into it.
14431          */
14432         new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14433         bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14434 
14435         /* Need later to copy data between the shadow buf & original buf! */
14436         new_xp->xb_pkt_flags |= PKT_CONSISTENT;
14437 
14438         /*
14439          * The given bp is automatically saved in the xb_private member
14440          * of the new xbuf.  Callers are allowed to depend on this.
14441          */
14442         new_xp->xb_private = bp;
14443 
14444         new_bp->b_private  = new_xp;
14445 
14446         return (new_bp);
14447 }
14448 
14449 /*
14450  *    Function: sd_bioclone_free
14451  *
14452  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations
14453  *              in the larger than partition operation.
14454  *
14455  *     Context: May be called under interrupt context
14456  */
14457 
14458 static void
14459 sd_bioclone_free(struct buf *bp)
14460 {
14461         struct sd_xbuf  *xp;
14462 
14463         ASSERT(bp != NULL);
14464         xp = SD_GET_XBUF(bp);
14465         ASSERT(xp != NULL);
14466 
14467         /*
14468          * Call bp_mapout() before freeing the buf,  in case a lower
14469          * layer or HBA  had done a bp_mapin().  we must do this here
14470          * as we are the "originator" of the shadow buf.
14471          */
14472         bp_mapout(bp);
14473 
14474         /*
14475          * Null out b_iodone before freeing the bp, to ensure that the driver
14476          * never gets confused by a stale value in this field. (Just a little
14477          * extra defensiveness here.)
14478          */
14479         bp->b_iodone = NULL;
14480 
14481         freerbuf(bp);
14482 
14483         kmem_free(xp, sizeof (struct sd_xbuf));
14484 }
14485 
14486 /*
14487  *    Function: sd_shadow_buf_free
14488  *
14489  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations.
14490  *
14491  *     Context: May be called under interrupt context
14492  */
14493 
14494 static void
14495 sd_shadow_buf_free(struct buf *bp)
14496 {
14497         struct sd_xbuf  *xp;
14498 
14499         ASSERT(bp != NULL);
14500         xp = SD_GET_XBUF(bp);
14501         ASSERT(xp != NULL);
14502 
14503 #if defined(__sparc)
14504         /*
14505          * Call bp_mapout() before freeing the buf,  in case a lower
14506          * layer or HBA  had done a bp_mapin().  we must do this here
14507          * as we are the "originator" of the shadow buf.
14508          */
14509         bp_mapout(bp);
14510 #endif
14511 
14512         /*
14513          * Null out b_iodone before freeing the bp, to ensure that the driver
14514          * never gets confused by a stale value in this field. (Just a little
14515          * extra defensiveness here.)
14516          */
14517         bp->b_iodone = NULL;
14518 
14519 #if defined(__i386) || defined(__amd64)
14520         kmem_free(bp->b_un.b_addr, bp->b_bcount);
14521         freerbuf(bp);
14522 #else
14523         scsi_free_consistent_buf(bp);
14524 #endif
14525 
14526         kmem_free(xp, sizeof (struct sd_xbuf));
14527 }
14528 
14529 
14530 /*
14531  *    Function: sd_print_transport_rejected_message
14532  *
14533  * Description: This implements the ludicrously complex rules for printing
14534  *              a "transport rejected" message.  This is to address the
14535  *              specific problem of having a flood of this error message
14536  *              produced when a failover occurs.
14537  *
14538  *     Context: Any.
14539  */
14540 
14541 static void
14542 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp,
14543     int code)
14544 {
14545         ASSERT(un != NULL);
14546         ASSERT(mutex_owned(SD_MUTEX(un)));
14547         ASSERT(xp != NULL);
14548 
14549         /*
14550          * Print the "transport rejected" message under the following
14551          * conditions:
14552          *
14553          * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set
14554          * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR.
14555          * - If the error code IS a TRAN_FATAL_ERROR, then the message is
14556          *   printed the FIRST time a TRAN_FATAL_ERROR is returned from
14557          *   scsi_transport(9F) (which indicates that the target might have
14558          *   gone off-line).  This uses the un->un_tran_fatal_count
14559          *   count, which is incremented whenever a TRAN_FATAL_ERROR is
14560          *   received, and reset to zero whenver a TRAN_ACCEPT is returned
14561          *   from scsi_transport().
14562          *
14563          * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of
14564          * the preceeding cases in order for the message to be printed.
14565          */
14566         if (((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) &&
14567             (SD_FM_LOG(un) == SD_FM_LOG_NSUP)) {
14568                 if ((sd_level_mask & SD_LOGMASK_DIAG) ||
14569                     (code != TRAN_FATAL_ERROR) ||
14570                     (un->un_tran_fatal_count == 1)) {
14571                         switch (code) {
14572                         case TRAN_BADPKT:
14573                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14574                                     "transport rejected bad packet\n");
14575                                 break;
14576                         case TRAN_FATAL_ERROR:
14577                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14578                                     "transport rejected fatal error\n");
14579                                 break;
14580                         default:
14581                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14582                                     "transport rejected (%d)\n", code);
14583                                 break;
14584                         }
14585                 }
14586         }
14587 }
14588 
14589 
14590 /*
14591  *    Function: sd_add_buf_to_waitq
14592  *
14593  * Description: Add the given buf(9S) struct to the wait queue for the
14594  *              instance.  If sorting is enabled, then the buf is added
14595  *              to the queue via an elevator sort algorithm (a la
14596  *              disksort(9F)).  The SD_GET_BLKNO(bp) is used as the sort key.
14597  *              If sorting is not enabled, then the buf is just added
14598  *              to the end of the wait queue.
14599  *
14600  * Return Code: void
14601  *
14602  *     Context: Does not sleep/block, therefore technically can be called
14603  *              from any context.  However if sorting is enabled then the
14604  *              execution time is indeterminate, and may take long if
14605  *              the wait queue grows large.
14606  */
14607 
14608 static void
14609 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp)
14610 {
14611         struct buf *ap;
14612 
14613         ASSERT(bp != NULL);
14614         ASSERT(un != NULL);
14615         ASSERT(mutex_owned(SD_MUTEX(un)));
14616 
14617         /* If the queue is empty, add the buf as the only entry & return. */
14618         if (un->un_waitq_headp == NULL) {
14619                 ASSERT(un->un_waitq_tailp == NULL);
14620                 un->un_waitq_headp = un->un_waitq_tailp = bp;
14621                 bp->av_forw = NULL;
14622                 return;
14623         }
14624 
14625         ASSERT(un->un_waitq_tailp != NULL);
14626 
14627         /*
14628          * If sorting is disabled, just add the buf to the tail end of
14629          * the wait queue and return.
14630          */
14631         if (un->un_f_disksort_disabled || un->un_f_enable_rmw) {
14632                 un->un_waitq_tailp->av_forw = bp;
14633                 un->un_waitq_tailp = bp;
14634                 bp->av_forw = NULL;
14635                 return;
14636         }
14637 
14638         /*
14639          * Sort thru the list of requests currently on the wait queue
14640          * and add the new buf request at the appropriate position.
14641          *
14642          * The un->un_waitq_headp is an activity chain pointer on which
14643          * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The
14644          * first queue holds those requests which are positioned after
14645          * the current SD_GET_BLKNO() (in the first request); the second holds
14646          * requests which came in after their SD_GET_BLKNO() number was passed.
14647          * Thus we implement a one way scan, retracting after reaching
14648          * the end of the drive to the first request on the second
14649          * queue, at which time it becomes the first queue.
14650          * A one-way scan is natural because of the way UNIX read-ahead
14651          * blocks are allocated.
14652          *
14653          * If we lie after the first request, then we must locate the
14654          * second request list and add ourselves to it.
14655          */
14656         ap = un->un_waitq_headp;
14657         if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) {
14658                 while (ap->av_forw != NULL) {
14659                         /*
14660                          * Look for an "inversion" in the (normally
14661                          * ascending) block numbers. This indicates
14662                          * the start of the second request list.
14663                          */
14664                         if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) {
14665                                 /*
14666                                  * Search the second request list for the
14667                                  * first request at a larger block number.
14668                                  * We go before that; however if there is
14669                                  * no such request, we go at the end.
14670                                  */
14671                                 do {
14672                                         if (SD_GET_BLKNO(bp) <
14673                                             SD_GET_BLKNO(ap->av_forw)) {
14674                                                 goto insert;
14675                                         }
14676                                         ap = ap->av_forw;
14677                                 } while (ap->av_forw != NULL);
14678                                 goto insert;            /* after last */
14679                         }
14680                         ap = ap->av_forw;
14681                 }
14682 
14683                 /*
14684                  * No inversions... we will go after the last, and
14685                  * be the first request in the second request list.
14686                  */
14687                 goto insert;
14688         }
14689 
14690         /*
14691          * Request is at/after the current request...
14692          * sort in the first request list.
14693          */
14694         while (ap->av_forw != NULL) {
14695                 /*
14696                  * We want to go after the current request (1) if
14697                  * there is an inversion after it (i.e. it is the end
14698                  * of the first request list), or (2) if the next
14699                  * request is a larger block no. than our request.
14700                  */
14701                 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) ||
14702                     (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) {
14703                         goto insert;
14704                 }
14705                 ap = ap->av_forw;
14706         }
14707 
14708         /*
14709          * Neither a second list nor a larger request, therefore
14710          * we go at the end of the first list (which is the same
14711          * as the end of the whole schebang).
14712          */
14713 insert:
14714         bp->av_forw = ap->av_forw;
14715         ap->av_forw = bp;
14716 
14717         /*
14718          * If we inserted onto the tail end of the waitq, make sure the
14719          * tail pointer is updated.
14720          */
14721         if (ap == un->un_waitq_tailp) {
14722                 un->un_waitq_tailp = bp;
14723         }
14724 }
14725 
14726 
14727 /*
14728  *    Function: sd_start_cmds
14729  *
14730  * Description: Remove and transport cmds from the driver queues.
14731  *
14732  *   Arguments: un - pointer to the unit (soft state) struct for the target.
14733  *
14734  *              immed_bp - ptr to a buf to be transported immediately. Only
14735  *              the immed_bp is transported; bufs on the waitq are not
14736  *              processed and the un_retry_bp is not checked.  If immed_bp is
14737  *              NULL, then normal queue processing is performed.
14738  *
14739  *     Context: May be called from kernel thread context, interrupt context,
14740  *              or runout callback context. This function may not block or
14741  *              call routines that block.
14742  */
14743 
14744 static void
14745 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp)
14746 {
14747         struct  sd_xbuf *xp;
14748         struct  buf     *bp;
14749         void    (*statp)(kstat_io_t *);
14750 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14751         void    (*saved_statp)(kstat_io_t *);
14752 #endif
14753         int     rval;
14754         struct sd_fm_internal *sfip = NULL;
14755 
14756         ASSERT(un != NULL);
14757         ASSERT(mutex_owned(SD_MUTEX(un)));
14758         ASSERT(un->un_ncmds_in_transport >= 0);
14759         ASSERT(un->un_throttle >= 0);
14760 
14761         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n");
14762 
14763         do {
14764 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14765                 saved_statp = NULL;
14766 #endif
14767 
14768                 /*
14769                  * If we are syncing or dumping, fail the command to
14770                  * avoid recursively calling back into scsi_transport().
14771                  * The dump I/O itself uses a separate code path so this
14772                  * only prevents non-dump I/O from being sent while dumping.
14773                  * File system sync takes place before dumping begins.
14774                  * During panic, filesystem I/O is allowed provided
14775                  * un_in_callback is <= 1.  This is to prevent recursion
14776                  * such as sd_start_cmds -> scsi_transport -> sdintr ->
14777                  * sd_start_cmds and so on.  See panic.c for more information
14778                  * about the states the system can be in during panic.
14779                  */
14780                 if ((un->un_state == SD_STATE_DUMPING) ||
14781                     (ddi_in_panic() && (un->un_in_callback > 1))) {
14782                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14783                             "sd_start_cmds: panicking\n");
14784                         goto exit;
14785                 }
14786 
14787                 if ((bp = immed_bp) != NULL) {
14788                         /*
14789                          * We have a bp that must be transported immediately.
14790                          * It's OK to transport the immed_bp here without doing
14791                          * the throttle limit check because the immed_bp is
14792                          * always used in a retry/recovery case. This means
14793                          * that we know we are not at the throttle limit by
14794                          * virtue of the fact that to get here we must have
14795                          * already gotten a command back via sdintr(). This also
14796                          * relies on (1) the command on un_retry_bp preventing
14797                          * further commands from the waitq from being issued;
14798                          * and (2) the code in sd_retry_command checking the
14799                          * throttle limit before issuing a delayed or immediate
14800                          * retry. This holds even if the throttle limit is
14801                          * currently ratcheted down from its maximum value.
14802                          */
14803                         statp = kstat_runq_enter;
14804                         if (bp == un->un_retry_bp) {
14805                                 ASSERT((un->un_retry_statp == NULL) ||
14806                                     (un->un_retry_statp == kstat_waitq_enter) ||
14807                                     (un->un_retry_statp ==
14808                                     kstat_runq_back_to_waitq));
14809                                 /*
14810                                  * If the waitq kstat was incremented when
14811                                  * sd_set_retry_bp() queued this bp for a retry,
14812                                  * then we must set up statp so that the waitq
14813                                  * count will get decremented correctly below.
14814                                  * Also we must clear un->un_retry_statp to
14815                                  * ensure that we do not act on a stale value
14816                                  * in this field.
14817                                  */
14818                                 if ((un->un_retry_statp == kstat_waitq_enter) ||
14819                                     (un->un_retry_statp ==
14820                                     kstat_runq_back_to_waitq)) {
14821                                         statp = kstat_waitq_to_runq;
14822                                 }
14823 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14824                                 saved_statp = un->un_retry_statp;
14825 #endif
14826                                 un->un_retry_statp = NULL;
14827 
14828                                 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
14829                                     "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p "
14830                                     "un_throttle:%d un_ncmds_in_transport:%d\n",
14831                                     un, un->un_retry_bp, un->un_throttle,
14832                                     un->un_ncmds_in_transport);
14833                         } else {
14834                                 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: "
14835                                     "processing priority bp:0x%p\n", bp);
14836                         }
14837 
14838                 } else if ((bp = un->un_waitq_headp) != NULL) {
14839                         /*
14840                          * A command on the waitq is ready to go, but do not
14841                          * send it if:
14842                          *
14843                          * (1) the throttle limit has been reached, or
14844                          * (2) a retry is pending, or
14845                          * (3) a START_STOP_UNIT callback pending, or
14846                          * (4) a callback for a SD_PATH_DIRECT_PRIORITY
14847                          *      command is pending.
14848                          *
14849                          * For all of these conditions, IO processing will
14850                          * restart after the condition is cleared.
14851                          */
14852                         if (un->un_ncmds_in_transport >= un->un_throttle) {
14853                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14854                                     "sd_start_cmds: exiting, "
14855                                     "throttle limit reached!\n");
14856                                 goto exit;
14857                         }
14858                         if (un->un_retry_bp != NULL) {
14859                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14860                                     "sd_start_cmds: exiting, retry pending!\n");
14861                                 goto exit;
14862                         }
14863                         if (un->un_startstop_timeid != NULL) {
14864                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14865                                     "sd_start_cmds: exiting, "
14866                                     "START_STOP pending!\n");
14867                                 goto exit;
14868                         }
14869                         if (un->un_direct_priority_timeid != NULL) {
14870                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14871                                     "sd_start_cmds: exiting, "
14872                                     "SD_PATH_DIRECT_PRIORITY cmd. pending!\n");
14873                                 goto exit;
14874                         }
14875 
14876                         /* Dequeue the command */
14877                         un->un_waitq_headp = bp->av_forw;
14878                         if (un->un_waitq_headp == NULL) {
14879                                 un->un_waitq_tailp = NULL;
14880                         }
14881                         bp->av_forw = NULL;
14882                         statp = kstat_waitq_to_runq;
14883                         SD_TRACE(SD_LOG_IO_CORE, un,
14884                             "sd_start_cmds: processing waitq bp:0x%p\n", bp);
14885 
14886                 } else {
14887                         /* No work to do so bail out now */
14888                         SD_TRACE(SD_LOG_IO_CORE, un,
14889                             "sd_start_cmds: no more work, exiting!\n");
14890                         goto exit;
14891                 }
14892 
14893                 /*
14894                  * Reset the state to normal. This is the mechanism by which
14895                  * the state transitions from either SD_STATE_RWAIT or
14896                  * SD_STATE_OFFLINE to SD_STATE_NORMAL.
14897                  * If state is SD_STATE_PM_CHANGING then this command is
14898                  * part of the device power control and the state must
14899                  * not be put back to normal. Doing so would would
14900                  * allow new commands to proceed when they shouldn't,
14901                  * the device may be going off.
14902                  */
14903                 if ((un->un_state != SD_STATE_SUSPENDED) &&
14904                     (un->un_state != SD_STATE_PM_CHANGING)) {
14905                         New_state(un, SD_STATE_NORMAL);
14906                 }
14907 
14908                 xp = SD_GET_XBUF(bp);
14909                 ASSERT(xp != NULL);
14910 
14911 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14912                 /*
14913                  * Allocate the scsi_pkt if we need one, or attach DMA
14914                  * resources if we have a scsi_pkt that needs them. The
14915                  * latter should only occur for commands that are being
14916                  * retried.
14917                  */
14918                 if ((xp->xb_pktp == NULL) ||
14919                     ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) {
14920 #else
14921                 if (xp->xb_pktp == NULL) {
14922 #endif
14923                         /*
14924                          * There is no scsi_pkt allocated for this buf. Call
14925                          * the initpkt function to allocate & init one.
14926                          *
14927                          * The scsi_init_pkt runout callback functionality is
14928                          * implemented as follows:
14929                          *
14930                          * 1) The initpkt function always calls
14931                          *    scsi_init_pkt(9F) with sdrunout specified as the
14932                          *    callback routine.
14933                          * 2) A successful packet allocation is initialized and
14934                          *    the I/O is transported.
14935                          * 3) The I/O associated with an allocation resource
14936                          *    failure is left on its queue to be retried via
14937                          *    runout or the next I/O.
14938                          * 4) The I/O associated with a DMA error is removed
14939                          *    from the queue and failed with EIO. Processing of
14940                          *    the transport queues is also halted to be
14941                          *    restarted via runout or the next I/O.
14942                          * 5) The I/O associated with a CDB size or packet
14943                          *    size error is removed from the queue and failed
14944                          *    with EIO. Processing of the transport queues is
14945                          *    continued.
14946                          *
14947                          * Note: there is no interface for canceling a runout
14948                          * callback. To prevent the driver from detaching or
14949                          * suspending while a runout is pending the driver
14950                          * state is set to SD_STATE_RWAIT
14951                          *
14952                          * Note: using the scsi_init_pkt callback facility can
14953                          * result in an I/O request persisting at the head of
14954                          * the list which cannot be satisfied even after
14955                          * multiple retries. In the future the driver may
14956                          * implement some kind of maximum runout count before
14957                          * failing an I/O.
14958                          *
14959                          * Note: the use of funcp below may seem superfluous,
14960                          * but it helps warlock figure out the correct
14961                          * initpkt function calls (see [s]sd.wlcmd).
14962                          */
14963                         struct scsi_pkt *pktp;
14964                         int (*funcp)(struct buf *bp, struct scsi_pkt **pktp);
14965 
14966                         ASSERT(bp != un->un_rqs_bp);
14967 
14968                         funcp = sd_initpkt_map[xp->xb_chain_iostart];
14969                         switch ((*funcp)(bp, &pktp)) {
14970                         case  SD_PKT_ALLOC_SUCCESS:
14971                                 xp->xb_pktp = pktp;
14972                                 SD_TRACE(SD_LOG_IO_CORE, un,
14973                                     "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n",
14974                                     pktp);
14975                                 goto got_pkt;
14976 
14977                         case SD_PKT_ALLOC_FAILURE:
14978                                 /*
14979                                  * Temporary (hopefully) resource depletion.
14980                                  * Since retries and RQS commands always have a
14981                                  * scsi_pkt allocated, these cases should never
14982                                  * get here. So the only cases this needs to
14983                                  * handle is a bp from the waitq (which we put
14984                                  * back onto the waitq for sdrunout), or a bp
14985                                  * sent as an immed_bp (which we just fail).
14986                                  */
14987                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14988                                     "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n");
14989 
14990 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14991 
14992                                 if (bp == immed_bp) {
14993                                         /*
14994                                          * If SD_XB_DMA_FREED is clear, then
14995                                          * this is a failure to allocate a
14996                                          * scsi_pkt, and we must fail the
14997                                          * command.
14998                                          */
14999                                         if ((xp->xb_pkt_flags &
15000                                             SD_XB_DMA_FREED) == 0) {
15001                                                 break;
15002                                         }
15003 
15004                                         /*
15005                                          * If this immediate command is NOT our
15006                                          * un_retry_bp, then we must fail it.
15007                                          */
15008                                         if (bp != un->un_retry_bp) {
15009                                                 break;
15010                                         }
15011 
15012                                         /*
15013                                          * We get here if this cmd is our
15014                                          * un_retry_bp that was DMAFREED, but
15015                                          * scsi_init_pkt() failed to reallocate
15016                                          * DMA resources when we attempted to
15017                                          * retry it. This can happen when an
15018                                          * mpxio failover is in progress, but
15019                                          * we don't want to just fail the
15020                                          * command in this case.
15021                                          *
15022                                          * Use timeout(9F) to restart it after
15023                                          * a 100ms delay.  We don't want to
15024                                          * let sdrunout() restart it, because
15025                                          * sdrunout() is just supposed to start
15026                                          * commands that are sitting on the
15027                                          * wait queue.  The un_retry_bp stays
15028                                          * set until the command completes, but
15029                                          * sdrunout can be called many times
15030                                          * before that happens.  Since sdrunout
15031                                          * cannot tell if the un_retry_bp is
15032                                          * already in the transport, it could
15033                                          * end up calling scsi_transport() for
15034                                          * the un_retry_bp multiple times.
15035                                          *
15036                                          * Also: don't schedule the callback
15037                                          * if some other callback is already
15038                                          * pending.
15039                                          */
15040                                         if (un->un_retry_statp == NULL) {
15041                                                 /*
15042                                                  * restore the kstat pointer to
15043                                                  * keep kstat counts coherent
15044                                                  * when we do retry the command.
15045                                                  */
15046                                                 un->un_retry_statp =
15047                                                     saved_statp;
15048                                         }
15049 
15050                                         if ((un->un_startstop_timeid == NULL) &&
15051                                             (un->un_retry_timeid == NULL) &&
15052                                             (un->un_direct_priority_timeid ==
15053                                             NULL)) {
15054 
15055                                                 un->un_retry_timeid =
15056                                                     timeout(
15057                                                     sd_start_retry_command,
15058                                                     un, SD_RESTART_TIMEOUT);
15059                                         }
15060                                         goto exit;
15061                                 }
15062 
15063 #else
15064                                 if (bp == immed_bp) {
15065                                         break;  /* Just fail the command */
15066                                 }
15067 #endif
15068 
15069                                 /* Add the buf back to the head of the waitq */
15070                                 bp->av_forw = un->un_waitq_headp;
15071                                 un->un_waitq_headp = bp;
15072                                 if (un->un_waitq_tailp == NULL) {
15073                                         un->un_waitq_tailp = bp;
15074                                 }
15075                                 goto exit;
15076 
15077                         case SD_PKT_ALLOC_FAILURE_NO_DMA:
15078                                 /*
15079                                  * HBA DMA resource failure. Fail the command
15080                                  * and continue processing of the queues.
15081                                  */
15082                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15083                                     "sd_start_cmds: "
15084                                     "SD_PKT_ALLOC_FAILURE_NO_DMA\n");
15085                                 break;
15086 
15087                         case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL:
15088                                 /*
15089                                  * Note:x86: Partial DMA mapping not supported
15090                                  * for USCSI commands, and all the needed DMA
15091                                  * resources were not allocated.
15092                                  */
15093                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15094                                     "sd_start_cmds: "
15095                                     "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n");
15096                                 break;
15097 
15098                         case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL:
15099                                 /*
15100                                  * Note:x86: Request cannot fit into CDB based
15101                                  * on lba and len.
15102                                  */
15103                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15104                                     "sd_start_cmds: "
15105                                     "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n");
15106                                 break;
15107 
15108                         default:
15109                                 /* Should NEVER get here! */
15110                                 panic("scsi_initpkt error");
15111                                 /*NOTREACHED*/
15112                         }
15113 
15114                         /*
15115                          * Fatal error in allocating a scsi_pkt for this buf.
15116                          * Update kstats & return the buf with an error code.
15117                          * We must use sd_return_failed_command_no_restart() to
15118                          * avoid a recursive call back into sd_start_cmds().
15119                          * However this also means that we must keep processing
15120                          * the waitq here in order to avoid stalling.
15121                          */
15122                         if (statp == kstat_waitq_to_runq) {
15123                                 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
15124                         }
15125                         sd_return_failed_command_no_restart(un, bp, EIO);
15126                         if (bp == immed_bp) {
15127                                 /* immed_bp is gone by now, so clear this */
15128                                 immed_bp = NULL;
15129                         }
15130                         continue;
15131                 }
15132 got_pkt:
15133                 if (bp == immed_bp) {
15134                         /* goto the head of the class.... */
15135                         xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15136                 }
15137 
15138                 un->un_ncmds_in_transport++;
15139                 SD_UPDATE_KSTATS(un, statp, bp);
15140 
15141                 /*
15142                  * Call scsi_transport() to send the command to the target.
15143                  * According to SCSA architecture, we must drop the mutex here
15144                  * before calling scsi_transport() in order to avoid deadlock.
15145                  * Note that the scsi_pkt's completion routine can be executed
15146                  * (from interrupt context) even before the call to
15147                  * scsi_transport() returns.
15148                  */
15149                 SD_TRACE(SD_LOG_IO_CORE, un,
15150                     "sd_start_cmds: calling scsi_transport()\n");
15151                 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp);
15152 
15153                 mutex_exit(SD_MUTEX(un));
15154                 rval = scsi_transport(xp->xb_pktp);
15155                 mutex_enter(SD_MUTEX(un));
15156 
15157                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15158                     "sd_start_cmds: scsi_transport() returned %d\n", rval);
15159 
15160                 switch (rval) {
15161                 case TRAN_ACCEPT:
15162                         /* Clear this with every pkt accepted by the HBA */
15163                         un->un_tran_fatal_count = 0;
15164                         break;  /* Success; try the next cmd (if any) */
15165 
15166                 case TRAN_BUSY:
15167                         un->un_ncmds_in_transport--;
15168                         ASSERT(un->un_ncmds_in_transport >= 0);
15169 
15170                         /*
15171                          * Don't retry request sense, the sense data
15172                          * is lost when another request is sent.
15173                          * Free up the rqs buf and retry
15174                          * the original failed cmd.  Update kstat.
15175                          */
15176                         if (bp == un->un_rqs_bp) {
15177                                 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15178                                 bp = sd_mark_rqs_idle(un, xp);
15179                                 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
15180                                     NULL, NULL, EIO, un->un_busy_timeout / 500,
15181                                     kstat_waitq_enter);
15182                                 goto exit;
15183                         }
15184 
15185 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
15186                         /*
15187                          * Free the DMA resources for the  scsi_pkt. This will
15188                          * allow mpxio to select another path the next time
15189                          * we call scsi_transport() with this scsi_pkt.
15190                          * See sdintr() for the rationalization behind this.
15191                          */
15192                         if ((un->un_f_is_fibre == TRUE) &&
15193                             ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
15194                             ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) {
15195                                 scsi_dmafree(xp->xb_pktp);
15196                                 xp->xb_pkt_flags |= SD_XB_DMA_FREED;
15197                         }
15198 #endif
15199 
15200                         if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) {
15201                                 /*
15202                                  * Commands that are SD_PATH_DIRECT_PRIORITY
15203                                  * are for error recovery situations. These do
15204                                  * not use the normal command waitq, so if they
15205                                  * get a TRAN_BUSY we cannot put them back onto
15206                                  * the waitq for later retry. One possible
15207                                  * problem is that there could already be some
15208                                  * other command on un_retry_bp that is waiting
15209                                  * for this one to complete, so we would be
15210                                  * deadlocked if we put this command back onto
15211                                  * the waitq for later retry (since un_retry_bp
15212                                  * must complete before the driver gets back to
15213                                  * commands on the waitq).
15214                                  *
15215                                  * To avoid deadlock we must schedule a callback
15216                                  * that will restart this command after a set
15217                                  * interval.  This should keep retrying for as
15218                                  * long as the underlying transport keeps
15219                                  * returning TRAN_BUSY (just like for other
15220                                  * commands).  Use the same timeout interval as
15221                                  * for the ordinary TRAN_BUSY retry.
15222                                  */
15223                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15224                                     "sd_start_cmds: scsi_transport() returned "
15225                                     "TRAN_BUSY for DIRECT_PRIORITY cmd!\n");
15226 
15227                                 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15228                                 un->un_direct_priority_timeid =
15229                                     timeout(sd_start_direct_priority_command,
15230                                     bp, un->un_busy_timeout / 500);
15231 
15232                                 goto exit;
15233                         }
15234 
15235                         /*
15236                          * For TRAN_BUSY, we want to reduce the throttle value,
15237                          * unless we are retrying a command.
15238                          */
15239                         if (bp != un->un_retry_bp) {
15240                                 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY);
15241                         }
15242 
15243                         /*
15244                          * Set up the bp to be tried again 10 ms later.
15245                          * Note:x86: Is there a timeout value in the sd_lun
15246                          * for this condition?
15247                          */
15248                         sd_set_retry_bp(un, bp, un->un_busy_timeout / 500,
15249                             kstat_runq_back_to_waitq);
15250                         goto exit;
15251 
15252                 case TRAN_FATAL_ERROR:
15253                         un->un_tran_fatal_count++;
15254                         /* FALLTHRU */
15255 
15256                 case TRAN_BADPKT:
15257                 default:
15258                         un->un_ncmds_in_transport--;
15259                         ASSERT(un->un_ncmds_in_transport >= 0);
15260 
15261                         /*
15262                          * If this is our REQUEST SENSE command with a
15263                          * transport error, we must get back the pointers
15264                          * to the original buf, and mark the REQUEST
15265                          * SENSE command as "available".
15266                          */
15267                         if (bp == un->un_rqs_bp) {
15268                                 bp = sd_mark_rqs_idle(un, xp);
15269                                 xp = SD_GET_XBUF(bp);
15270                         } else {
15271                                 /*
15272                                  * Legacy behavior: do not update transport
15273                                  * error count for request sense commands.
15274                                  */
15275                                 SD_UPDATE_ERRSTATS(un, sd_transerrs);
15276                         }
15277 
15278                         SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15279                         sd_print_transport_rejected_message(un, xp, rval);
15280 
15281                         /*
15282                          * This command will be terminated by SD driver due
15283                          * to a fatal transport error. We should post
15284                          * ereport.io.scsi.cmd.disk.tran with driver-assessment
15285                          * of "fail" for any command to indicate this
15286                          * situation.
15287                          */
15288                         if (xp->xb_ena > 0) {
15289                                 ASSERT(un->un_fm_private != NULL);
15290                                 sfip = un->un_fm_private;
15291                                 sfip->fm_ssc.ssc_flags |= SSC_FLAGS_TRAN_ABORT;
15292                                 sd_ssc_extract_info(&sfip->fm_ssc, un,
15293                                     xp->xb_pktp, bp, xp);
15294                                 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
15295                         }
15296 
15297                         /*
15298                          * We must use sd_return_failed_command_no_restart() to
15299                          * avoid a recursive call back into sd_start_cmds().
15300                          * However this also means that we must keep processing
15301                          * the waitq here in order to avoid stalling.
15302                          */
15303                         sd_return_failed_command_no_restart(un, bp, EIO);
15304 
15305                         /*
15306                          * Notify any threads waiting in sd_ddi_suspend() that
15307                          * a command completion has occurred.
15308                          */
15309                         if (un->un_state == SD_STATE_SUSPENDED) {
15310                                 cv_broadcast(&un->un_disk_busy_cv);
15311                         }
15312 
15313                         if (bp == immed_bp) {
15314                                 /* immed_bp is gone by now, so clear this */
15315                                 immed_bp = NULL;
15316                         }
15317                         break;
15318                 }
15319 
15320         } while (immed_bp == NULL);
15321 
15322 exit:
15323         ASSERT(mutex_owned(SD_MUTEX(un)));
15324         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n");
15325 }
15326 
15327 
15328 /*
15329  *    Function: sd_return_command
15330  *
15331  * Description: Returns a command to its originator (with or without an
15332  *              error).  Also starts commands waiting to be transported
15333  *              to the target.
15334  *
15335  *     Context: May be called from interrupt, kernel, or timeout context
15336  */
15337 
15338 static void
15339 sd_return_command(struct sd_lun *un, struct buf *bp)
15340 {
15341         struct sd_xbuf *xp;
15342         struct scsi_pkt *pktp;
15343         struct sd_fm_internal *sfip;
15344 
15345         ASSERT(bp != NULL);
15346         ASSERT(un != NULL);
15347         ASSERT(mutex_owned(SD_MUTEX(un)));
15348         ASSERT(bp != un->un_rqs_bp);
15349         xp = SD_GET_XBUF(bp);
15350         ASSERT(xp != NULL);
15351 
15352         pktp = SD_GET_PKTP(bp);
15353         sfip = (struct sd_fm_internal *)un->un_fm_private;
15354         ASSERT(sfip != NULL);
15355 
15356         SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n");
15357 
15358         /*
15359          * Note: check for the "sdrestart failed" case.
15360          */
15361         if ((un->un_partial_dma_supported == 1) &&
15362             ((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) &&
15363             (geterror(bp) == 0) && (xp->xb_dma_resid != 0) &&
15364             (xp->xb_pktp->pkt_resid == 0)) {
15365 
15366                 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) {
15367                         /*
15368                          * Successfully set up next portion of cmd
15369                          * transfer, try sending it
15370                          */
15371                         sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
15372                             NULL, NULL, 0, (clock_t)0, NULL);
15373                         sd_start_cmds(un, NULL);
15374                         return; /* Note:x86: need a return here? */
15375                 }
15376         }
15377 
15378         /*
15379          * If this is the failfast bp, clear it from un_failfast_bp. This
15380          * can happen if upon being re-tried the failfast bp either
15381          * succeeded or encountered another error (possibly even a different
15382          * error than the one that precipitated the failfast state, but in
15383          * that case it would have had to exhaust retries as well). Regardless,
15384          * this should not occur whenever the instance is in the active
15385          * failfast state.
15386          */
15387         if (bp == un->un_failfast_bp) {
15388                 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15389                 un->un_failfast_bp = NULL;
15390         }
15391 
15392         /*
15393          * Clear the failfast state upon successful completion of ANY cmd.
15394          */
15395         if (bp->b_error == 0) {
15396                 un->un_failfast_state = SD_FAILFAST_INACTIVE;
15397                 /*
15398                  * If this is a successful command, but used to be retried,
15399                  * we will take it as a recovered command and post an
15400                  * ereport with driver-assessment of "recovered".
15401                  */
15402                 if (xp->xb_ena > 0) {
15403                         sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15404                         sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RECOVERY);
15405                 }
15406         } else {
15407                 /*
15408                  * If this is a failed non-USCSI command we will post an
15409                  * ereport with driver-assessment set accordingly("fail" or
15410                  * "fatal").
15411                  */
15412                 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
15413                         sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15414                         sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
15415                 }
15416         }
15417 
15418         /*
15419          * This is used if the command was retried one or more times. Show that
15420          * we are done with it, and allow processing of the waitq to resume.
15421          */
15422         if (bp == un->un_retry_bp) {
15423                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15424                     "sd_return_command: un:0x%p: "
15425                     "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15426                 un->un_retry_bp = NULL;
15427                 un->un_retry_statp = NULL;
15428         }
15429 
15430         SD_UPDATE_RDWR_STATS(un, bp);
15431         SD_UPDATE_PARTITION_STATS(un, bp);
15432 
15433         switch (un->un_state) {
15434         case SD_STATE_SUSPENDED:
15435                 /*
15436                  * Notify any threads waiting in sd_ddi_suspend() that
15437                  * a command completion has occurred.
15438                  */
15439                 cv_broadcast(&un->un_disk_busy_cv);
15440                 break;
15441         default:
15442                 sd_start_cmds(un, NULL);
15443                 break;
15444         }
15445 
15446         /* Return this command up the iodone chain to its originator. */
15447         mutex_exit(SD_MUTEX(un));
15448 
15449         (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15450         xp->xb_pktp = NULL;
15451 
15452         SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15453 
15454         ASSERT(!mutex_owned(SD_MUTEX(un)));
15455         mutex_enter(SD_MUTEX(un));
15456 
15457         SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n");
15458 }
15459 
15460 
15461 /*
15462  *    Function: sd_return_failed_command
15463  *
15464  * Description: Command completion when an error occurred.
15465  *
15466  *     Context: May be called from interrupt context
15467  */
15468 
15469 static void
15470 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode)
15471 {
15472         ASSERT(bp != NULL);
15473         ASSERT(un != NULL);
15474         ASSERT(mutex_owned(SD_MUTEX(un)));
15475 
15476         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15477             "sd_return_failed_command: entry\n");
15478 
15479         /*
15480          * b_resid could already be nonzero due to a partial data
15481          * transfer, so do not change it here.
15482          */
15483         SD_BIOERROR(bp, errcode);
15484 
15485         sd_return_command(un, bp);
15486         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15487             "sd_return_failed_command: exit\n");
15488 }
15489 
15490 
15491 /*
15492  *    Function: sd_return_failed_command_no_restart
15493  *
15494  * Description: Same as sd_return_failed_command, but ensures that no
15495  *              call back into sd_start_cmds will be issued.
15496  *
15497  *     Context: May be called from interrupt context
15498  */
15499 
15500 static void
15501 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp,
15502     int errcode)
15503 {
15504         struct sd_xbuf *xp;
15505 
15506         ASSERT(bp != NULL);
15507         ASSERT(un != NULL);
15508         ASSERT(mutex_owned(SD_MUTEX(un)));
15509         xp = SD_GET_XBUF(bp);
15510         ASSERT(xp != NULL);
15511         ASSERT(errcode != 0);
15512 
15513         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15514             "sd_return_failed_command_no_restart: entry\n");
15515 
15516         /*
15517          * b_resid could already be nonzero due to a partial data
15518          * transfer, so do not change it here.
15519          */
15520         SD_BIOERROR(bp, errcode);
15521 
15522         /*
15523          * If this is the failfast bp, clear it. This can happen if the
15524          * failfast bp encounterd a fatal error when we attempted to
15525          * re-try it (such as a scsi_transport(9F) failure).  However
15526          * we should NOT be in an active failfast state if the failfast
15527          * bp is not NULL.
15528          */
15529         if (bp == un->un_failfast_bp) {
15530                 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15531                 un->un_failfast_bp = NULL;
15532         }
15533 
15534         if (bp == un->un_retry_bp) {
15535                 /*
15536                  * This command was retried one or more times. Show that we are
15537                  * done with it, and allow processing of the waitq to resume.
15538                  */
15539                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15540                     "sd_return_failed_command_no_restart: "
15541                     " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15542                 un->un_retry_bp = NULL;
15543                 un->un_retry_statp = NULL;
15544         }
15545 
15546         SD_UPDATE_RDWR_STATS(un, bp);
15547         SD_UPDATE_PARTITION_STATS(un, bp);
15548 
15549         mutex_exit(SD_MUTEX(un));
15550 
15551         if (xp->xb_pktp != NULL) {
15552                 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15553                 xp->xb_pktp = NULL;
15554         }
15555 
15556         SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15557 
15558         mutex_enter(SD_MUTEX(un));
15559 
15560         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15561             "sd_return_failed_command_no_restart: exit\n");
15562 }
15563 
15564 
15565 /*
15566  *    Function: sd_retry_command
15567  *
15568  * Description: queue up a command for retry, or (optionally) fail it
15569  *              if retry counts are exhausted.
15570  *
15571  *   Arguments: un - Pointer to the sd_lun struct for the target.
15572  *
15573  *              bp - Pointer to the buf for the command to be retried.
15574  *
15575  *              retry_check_flag - Flag to see which (if any) of the retry
15576  *                 counts should be decremented/checked. If the indicated
15577  *                 retry count is exhausted, then the command will not be
15578  *                 retried; it will be failed instead. This should use a
15579  *                 value equal to one of the following:
15580  *
15581  *                      SD_RETRIES_NOCHECK
15582  *                      SD_RESD_RETRIES_STANDARD
15583  *                      SD_RETRIES_VICTIM
15584  *
15585  *                 Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE
15586  *                 if the check should be made to see of FLAG_ISOLATE is set
15587  *                 in the pkt. If FLAG_ISOLATE is set, then the command is
15588  *                 not retried, it is simply failed.
15589  *
15590  *              user_funcp - Ptr to function to call before dispatching the
15591  *                 command. May be NULL if no action needs to be performed.
15592  *                 (Primarily intended for printing messages.)
15593  *
15594  *              user_arg - Optional argument to be passed along to
15595  *                 the user_funcp call.
15596  *
15597  *              failure_code - errno return code to set in the bp if the
15598  *                 command is going to be failed.
15599  *
15600  *              retry_delay - Retry delay interval in (clock_t) units. May
15601  *                 be zero which indicates that the retry should be retried
15602  *                 immediately (ie, without an intervening delay).
15603  *
15604  *              statp - Ptr to kstat function to be updated if the command
15605  *                 is queued for a delayed retry. May be NULL if no kstat
15606  *                 update is desired.
15607  *
15608  *     Context: May be called from interrupt context.
15609  */
15610 
15611 static void
15612 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag,
15613     void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int code),
15614     void *user_arg, int failure_code, clock_t retry_delay,
15615     void (*statp)(kstat_io_t *))
15616 {
15617         struct sd_xbuf  *xp;
15618         struct scsi_pkt *pktp;
15619         struct sd_fm_internal *sfip;
15620 
15621         ASSERT(un != NULL);
15622         ASSERT(mutex_owned(SD_MUTEX(un)));
15623         ASSERT(bp != NULL);
15624         xp = SD_GET_XBUF(bp);
15625         ASSERT(xp != NULL);
15626         pktp = SD_GET_PKTP(bp);
15627         ASSERT(pktp != NULL);
15628 
15629         sfip = (struct sd_fm_internal *)un->un_fm_private;
15630         ASSERT(sfip != NULL);
15631 
15632         SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15633             "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp);
15634 
15635         /*
15636          * If we are syncing or dumping, fail the command to avoid
15637          * recursively calling back into scsi_transport().
15638          */
15639         if (ddi_in_panic()) {
15640                 goto fail_command_no_log;
15641         }
15642 
15643         /*
15644          * We should never be be retrying a command with FLAG_DIAGNOSE set, so
15645          * log an error and fail the command.
15646          */
15647         if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
15648                 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
15649                     "ERROR, retrying FLAG_DIAGNOSE command.\n");
15650                 sd_dump_memory(un, SD_LOG_IO, "CDB",
15651                     (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
15652                 sd_dump_memory(un, SD_LOG_IO, "Sense Data",
15653                     (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
15654                 goto fail_command;
15655         }
15656 
15657         /*
15658          * If we are suspended, then put the command onto head of the
15659          * wait queue since we don't want to start more commands, and
15660          * clear the un_retry_bp. Next time when we are resumed, will
15661          * handle the command in the wait queue.
15662          */
15663         switch (un->un_state) {
15664         case SD_STATE_SUSPENDED:
15665         case SD_STATE_DUMPING:
15666                 bp->av_forw = un->un_waitq_headp;
15667                 un->un_waitq_headp = bp;
15668                 if (un->un_waitq_tailp == NULL) {
15669                         un->un_waitq_tailp = bp;
15670                 }
15671                 if (bp == un->un_retry_bp) {
15672                         un->un_retry_bp = NULL;
15673                         un->un_retry_statp = NULL;
15674                 }
15675                 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
15676                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: "
15677                     "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp);
15678                 return;
15679         default:
15680                 break;
15681         }
15682 
15683         /*
15684          * If the caller wants us to check FLAG_ISOLATE, then see if that
15685          * is set; if it is then we do not want to retry the command.
15686          * Normally, FLAG_ISOLATE is only used with USCSI cmds.
15687          */
15688         if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) {
15689                 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) {
15690                         goto fail_command;
15691                 }
15692         }
15693 
15694 
15695         /*
15696          * If SD_RETRIES_FAILFAST is set, it indicates that either a
15697          * command timeout or a selection timeout has occurred. This means
15698          * that we were unable to establish an kind of communication with
15699          * the target, and subsequent retries and/or commands are likely
15700          * to encounter similar results and take a long time to complete.
15701          *
15702          * If this is a failfast error condition, we need to update the
15703          * failfast state, even if this bp does not have B_FAILFAST set.
15704          */
15705         if (retry_check_flag & SD_RETRIES_FAILFAST) {
15706                 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) {
15707                         ASSERT(un->un_failfast_bp == NULL);
15708                         /*
15709                          * If we are already in the active failfast state, and
15710                          * another failfast error condition has been detected,
15711                          * then fail this command if it has B_FAILFAST set.
15712                          * If B_FAILFAST is clear, then maintain the legacy
15713                          * behavior of retrying heroically, even tho this will
15714                          * take a lot more time to fail the command.
15715                          */
15716                         if (bp->b_flags & B_FAILFAST) {
15717                                 goto fail_command;
15718                         }
15719                 } else {
15720                         /*
15721                          * We're not in the active failfast state, but we
15722                          * have a failfast error condition, so we must begin
15723                          * transition to the next state. We do this regardless
15724                          * of whether or not this bp has B_FAILFAST set.
15725                          */
15726                         if (un->un_failfast_bp == NULL) {
15727                                 /*
15728                                  * This is the first bp to meet a failfast
15729                                  * condition so save it on un_failfast_bp &
15730                                  * do normal retry processing. Do not enter
15731                                  * active failfast state yet. This marks
15732                                  * entry into the "failfast pending" state.
15733                                  */
15734                                 un->un_failfast_bp = bp;
15735 
15736                         } else if (un->un_failfast_bp == bp) {
15737                                 /*
15738                                  * This is the second time *this* bp has
15739                                  * encountered a failfast error condition,
15740                                  * so enter active failfast state & flush
15741                                  * queues as appropriate.
15742                                  */
15743                                 un->un_failfast_state = SD_FAILFAST_ACTIVE;
15744                                 un->un_failfast_bp = NULL;
15745                                 sd_failfast_flushq(un);
15746 
15747                                 /*
15748                                  * Fail this bp now if B_FAILFAST set;
15749                                  * otherwise continue with retries. (It would
15750                                  * be pretty ironic if this bp succeeded on a
15751                                  * subsequent retry after we just flushed all
15752                                  * the queues).
15753                                  */
15754                                 if (bp->b_flags & B_FAILFAST) {
15755                                         goto fail_command;
15756                                 }
15757 
15758 #if !defined(lint) && !defined(__lint)
15759                         } else {
15760                                 /*
15761                                  * If neither of the preceeding conditionals
15762                                  * was true, it means that there is some
15763                                  * *other* bp that has met an inital failfast
15764                                  * condition and is currently either being
15765                                  * retried or is waiting to be retried. In
15766                                  * that case we should perform normal retry
15767                                  * processing on *this* bp, since there is a
15768                                  * chance that the current failfast condition
15769                                  * is transient and recoverable. If that does
15770                                  * not turn out to be the case, then retries
15771                                  * will be cleared when the wait queue is
15772                                  * flushed anyway.
15773                                  */
15774 #endif
15775                         }
15776                 }
15777         } else {
15778                 /*
15779                  * SD_RETRIES_FAILFAST is clear, which indicates that we
15780                  * likely were able to at least establish some level of
15781                  * communication with the target and subsequent commands
15782                  * and/or retries are likely to get through to the target,
15783                  * In this case we want to be aggressive about clearing
15784                  * the failfast state. Note that this does not affect
15785                  * the "failfast pending" condition.
15786                  */
15787                 un->un_failfast_state = SD_FAILFAST_INACTIVE;
15788         }
15789 
15790 
15791         /*
15792          * Check the specified retry count to see if we can still do
15793          * any retries with this pkt before we should fail it.
15794          */
15795         switch (retry_check_flag & SD_RETRIES_MASK) {
15796         case SD_RETRIES_VICTIM:
15797                 /*
15798                  * Check the victim retry count. If exhausted, then fall
15799                  * thru & check against the standard retry count.
15800                  */
15801                 if (xp->xb_victim_retry_count < un->un_victim_retry_count) {
15802                         /* Increment count & proceed with the retry */
15803                         xp->xb_victim_retry_count++;
15804                         break;
15805                 }
15806                 /* Victim retries exhausted, fall back to std. retries... */
15807                 /* FALLTHRU */
15808 
15809         case SD_RETRIES_STANDARD:
15810                 if (xp->xb_retry_count >= un->un_retry_count) {
15811                         /* Retries exhausted, fail the command */
15812                         SD_TRACE(SD_LOG_IO_CORE, un,
15813                             "sd_retry_command: retries exhausted!\n");
15814                         /*
15815                          * update b_resid for failed SCMD_READ & SCMD_WRITE
15816                          * commands with nonzero pkt_resid.
15817                          */
15818                         if ((pktp->pkt_reason == CMD_CMPLT) &&
15819                             (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) &&
15820                             (pktp->pkt_resid != 0)) {
15821                                 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F;
15822                                 if ((op == SCMD_READ) || (op == SCMD_WRITE)) {
15823                                         SD_UPDATE_B_RESID(bp, pktp);
15824                                 }
15825                         }
15826                         goto fail_command;
15827                 }
15828                 xp->xb_retry_count++;
15829                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15830                     "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15831                 break;
15832 
15833         case SD_RETRIES_UA:
15834                 if (xp->xb_ua_retry_count >= sd_ua_retry_count) {
15835                         /* Retries exhausted, fail the command */
15836                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
15837                             "Unit Attention retries exhausted. "
15838                             "Check the target.\n");
15839                         goto fail_command;
15840                 }
15841                 xp->xb_ua_retry_count++;
15842                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15843                     "sd_retry_command: retry count:%d\n",
15844                     xp->xb_ua_retry_count);
15845                 break;
15846 
15847         case SD_RETRIES_BUSY:
15848                 if (xp->xb_retry_count >= un->un_busy_retry_count) {
15849                         /* Retries exhausted, fail the command */
15850                         SD_TRACE(SD_LOG_IO_CORE, un,
15851                             "sd_retry_command: retries exhausted!\n");
15852                         goto fail_command;
15853                 }
15854                 xp->xb_retry_count++;
15855                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15856                     "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15857                 break;
15858 
15859         case SD_RETRIES_NOCHECK:
15860         default:
15861                 /* No retry count to check. Just proceed with the retry */
15862                 break;
15863         }
15864 
15865         xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15866 
15867         /*
15868          * If this is a non-USCSI command being retried
15869          * during execution last time, we should post an ereport with
15870          * driver-assessment of the value "retry".
15871          * For partial DMA, request sense and STATUS_QFULL, there are no
15872          * hardware errors, we bypass ereport posting.
15873          */
15874         if (failure_code != 0) {
15875                 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
15876                         sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15877                         sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RETRY);
15878                 }
15879         }
15880 
15881         /*
15882          * If we were given a zero timeout, we must attempt to retry the
15883          * command immediately (ie, without a delay).
15884          */
15885         if (retry_delay == 0) {
15886                 /*
15887                  * Check some limiting conditions to see if we can actually
15888                  * do the immediate retry.  If we cannot, then we must
15889                  * fall back to queueing up a delayed retry.
15890                  */
15891                 if (un->un_ncmds_in_transport >= un->un_throttle) {
15892                         /*
15893                          * We are at the throttle limit for the target,
15894                          * fall back to delayed retry.
15895                          */
15896                         retry_delay = un->un_busy_timeout;
15897                         statp = kstat_waitq_enter;
15898                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15899                             "sd_retry_command: immed. retry hit "
15900                             "throttle!\n");
15901                 } else {
15902                         /*
15903                          * We're clear to proceed with the immediate retry.
15904                          * First call the user-provided function (if any)
15905                          */
15906                         if (user_funcp != NULL) {
15907                                 (*user_funcp)(un, bp, user_arg,
15908                                     SD_IMMEDIATE_RETRY_ISSUED);
15909 #ifdef __lock_lint
15910                                 sd_print_incomplete_msg(un, bp, user_arg,
15911                                     SD_IMMEDIATE_RETRY_ISSUED);
15912                                 sd_print_cmd_incomplete_msg(un, bp, user_arg,
15913                                     SD_IMMEDIATE_RETRY_ISSUED);
15914                                 sd_print_sense_failed_msg(un, bp, user_arg,
15915                                     SD_IMMEDIATE_RETRY_ISSUED);
15916 #endif
15917                         }
15918 
15919                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15920                             "sd_retry_command: issuing immediate retry\n");
15921 
15922                         /*
15923                          * Call sd_start_cmds() to transport the command to
15924                          * the target.
15925                          */
15926                         sd_start_cmds(un, bp);
15927 
15928                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15929                             "sd_retry_command exit\n");
15930                         return;
15931                 }
15932         }
15933 
15934         /*
15935          * Set up to retry the command after a delay.
15936          * First call the user-provided function (if any)
15937          */
15938         if (user_funcp != NULL) {
15939                 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED);
15940         }
15941 
15942         sd_set_retry_bp(un, bp, retry_delay, statp);
15943 
15944         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15945         return;
15946 
15947 fail_command:
15948 
15949         if (user_funcp != NULL) {
15950                 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED);
15951         }
15952 
15953 fail_command_no_log:
15954 
15955         SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15956             "sd_retry_command: returning failed command\n");
15957 
15958         sd_return_failed_command(un, bp, failure_code);
15959 
15960         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15961 }
15962 
15963 
15964 /*
15965  *    Function: sd_set_retry_bp
15966  *
15967  * Description: Set up the given bp for retry.
15968  *
15969  *   Arguments: un - ptr to associated softstate
15970  *              bp - ptr to buf(9S) for the command
15971  *              retry_delay - time interval before issuing retry (may be 0)
15972  *              statp - optional pointer to kstat function
15973  *
15974  *     Context: May be called under interrupt context
15975  */
15976 
15977 static void
15978 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay,
15979     void (*statp)(kstat_io_t *))
15980 {
15981         ASSERT(un != NULL);
15982         ASSERT(mutex_owned(SD_MUTEX(un)));
15983         ASSERT(bp != NULL);
15984 
15985         SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15986             "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp);
15987 
15988         /*
15989          * Indicate that the command is being retried. This will not allow any
15990          * other commands on the wait queue to be transported to the target
15991          * until this command has been completed (success or failure). The
15992          * "retry command" is not transported to the target until the given
15993          * time delay expires, unless the user specified a 0 retry_delay.
15994          *
15995          * Note: the timeout(9F) callback routine is what actually calls
15996          * sd_start_cmds() to transport the command, with the exception of a
15997          * zero retry_delay. The only current implementor of a zero retry delay
15998          * is the case where a START_STOP_UNIT is sent to spin-up a device.
15999          */
16000         if (un->un_retry_bp == NULL) {
16001                 ASSERT(un->un_retry_statp == NULL);
16002                 un->un_retry_bp = bp;
16003 
16004                 /*
16005                  * If the user has not specified a delay the command should
16006                  * be queued and no timeout should be scheduled.
16007                  */
16008                 if (retry_delay == 0) {
16009                         /*
16010                          * Save the kstat pointer that will be used in the
16011                          * call to SD_UPDATE_KSTATS() below, so that
16012                          * sd_start_cmds() can correctly decrement the waitq
16013                          * count when it is time to transport this command.
16014                          */
16015                         un->un_retry_statp = statp;
16016                         goto done;
16017                 }
16018         }
16019 
16020         if (un->un_retry_bp == bp) {
16021                 /*
16022                  * Save the kstat pointer that will be used in the call to
16023                  * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can
16024                  * correctly decrement the waitq count when it is time to
16025                  * transport this command.
16026                  */
16027                 un->un_retry_statp = statp;
16028 
16029                 /*
16030                  * Schedule a timeout if:
16031                  *   1) The user has specified a delay.
16032                  *   2) There is not a START_STOP_UNIT callback pending.
16033                  *
16034                  * If no delay has been specified, then it is up to the caller
16035                  * to ensure that IO processing continues without stalling.
16036                  * Effectively, this means that the caller will issue the
16037                  * required call to sd_start_cmds(). The START_STOP_UNIT
16038                  * callback does this after the START STOP UNIT command has
16039                  * completed. In either of these cases we should not schedule
16040                  * a timeout callback here.  Also don't schedule the timeout if
16041                  * an SD_PATH_DIRECT_PRIORITY command is waiting to restart.
16042                  */
16043                 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) &&
16044                     (un->un_direct_priority_timeid == NULL)) {
16045                         un->un_retry_timeid =
16046                             timeout(sd_start_retry_command, un, retry_delay);
16047                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16048                             "sd_set_retry_bp: setting timeout: un: 0x%p"
16049                             " bp:0x%p un_retry_timeid:0x%p\n",
16050                             un, bp, un->un_retry_timeid);
16051                 }
16052         } else {
16053                 /*
16054                  * We only get in here if there is already another command
16055                  * waiting to be retried.  In this case, we just put the
16056                  * given command onto the wait queue, so it can be transported
16057                  * after the current retry command has completed.
16058                  *
16059                  * Also we have to make sure that if the command at the head
16060                  * of the wait queue is the un_failfast_bp, that we do not
16061                  * put ahead of it any other commands that are to be retried.
16062                  */
16063                 if ((un->un_failfast_bp != NULL) &&
16064                     (un->un_failfast_bp == un->un_waitq_headp)) {
16065                         /*
16066                          * Enqueue this command AFTER the first command on
16067                          * the wait queue (which is also un_failfast_bp).
16068                          */
16069                         bp->av_forw = un->un_waitq_headp->av_forw;
16070                         un->un_waitq_headp->av_forw = bp;
16071                         if (un->un_waitq_headp == un->un_waitq_tailp) {
16072                                 un->un_waitq_tailp = bp;
16073                         }
16074                 } else {
16075                         /* Enqueue this command at the head of the waitq. */
16076                         bp->av_forw = un->un_waitq_headp;
16077                         un->un_waitq_headp = bp;
16078                         if (un->un_waitq_tailp == NULL) {
16079                                 un->un_waitq_tailp = bp;
16080                         }
16081                 }
16082 
16083                 if (statp == NULL) {
16084                         statp = kstat_waitq_enter;
16085                 }
16086                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16087                     "sd_set_retry_bp: un:0x%p already delayed retry\n", un);
16088         }
16089 
16090 done:
16091         if (statp != NULL) {
16092                 SD_UPDATE_KSTATS(un, statp, bp);
16093         }
16094 
16095         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16096             "sd_set_retry_bp: exit un:0x%p\n", un);
16097 }
16098 
16099 
16100 /*
16101  *    Function: sd_start_retry_command
16102  *
16103  * Description: Start the command that has been waiting on the target's
16104  *              retry queue.  Called from timeout(9F) context after the
16105  *              retry delay interval has expired.
16106  *
16107  *   Arguments: arg - pointer to associated softstate for the device.
16108  *
16109  *     Context: timeout(9F) thread context.  May not sleep.
16110  */
16111 
16112 static void
16113 sd_start_retry_command(void *arg)
16114 {
16115         struct sd_lun *un = arg;
16116 
16117         ASSERT(un != NULL);
16118         ASSERT(!mutex_owned(SD_MUTEX(un)));
16119 
16120         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16121             "sd_start_retry_command: entry\n");
16122 
16123         mutex_enter(SD_MUTEX(un));
16124 
16125         un->un_retry_timeid = NULL;
16126 
16127         if (un->un_retry_bp != NULL) {
16128                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16129                     "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n",
16130                     un, un->un_retry_bp);
16131                 sd_start_cmds(un, un->un_retry_bp);
16132         }
16133 
16134         mutex_exit(SD_MUTEX(un));
16135 
16136         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16137             "sd_start_retry_command: exit\n");
16138 }
16139 
16140 /*
16141  *    Function: sd_rmw_msg_print_handler
16142  *
16143  * Description: If RMW mode is enabled and warning message is triggered
16144  *              print I/O count during a fixed interval.
16145  *
16146  *   Arguments: arg - pointer to associated softstate for the device.
16147  *
16148  *     Context: timeout(9F) thread context. May not sleep.
16149  */
16150 static void
16151 sd_rmw_msg_print_handler(void *arg)
16152 {
16153         struct sd_lun *un = arg;
16154 
16155         ASSERT(un != NULL);
16156         ASSERT(!mutex_owned(SD_MUTEX(un)));
16157 
16158         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16159             "sd_rmw_msg_print_handler: entry\n");
16160 
16161         mutex_enter(SD_MUTEX(un));
16162 
16163         if (un->un_rmw_incre_count > 0) {
16164                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16165                     "%"PRIu64" I/O requests are not aligned with %d disk "
16166                     "sector size in %ld seconds. They are handled through "
16167                     "Read Modify Write but the performance is very low!\n",
16168                     un->un_rmw_incre_count, un->un_tgt_blocksize,
16169                     drv_hztousec(SD_RMW_MSG_PRINT_TIMEOUT) / 1000000);
16170                 un->un_rmw_incre_count = 0;
16171                 un->un_rmw_msg_timeid = timeout(sd_rmw_msg_print_handler,
16172                     un, SD_RMW_MSG_PRINT_TIMEOUT);
16173         } else {
16174                 un->un_rmw_msg_timeid = NULL;
16175         }
16176 
16177         mutex_exit(SD_MUTEX(un));
16178 
16179         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16180             "sd_rmw_msg_print_handler: exit\n");
16181 }
16182 
16183 /*
16184  *    Function: sd_start_direct_priority_command
16185  *
16186  * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had
16187  *              received TRAN_BUSY when we called scsi_transport() to send it
16188  *              to the underlying HBA. This function is called from timeout(9F)
16189  *              context after the delay interval has expired.
16190  *
16191  *   Arguments: arg - pointer to associated buf(9S) to be restarted.
16192  *
16193  *     Context: timeout(9F) thread context.  May not sleep.
16194  */
16195 
16196 static void
16197 sd_start_direct_priority_command(void *arg)
16198 {
16199         struct buf      *priority_bp = arg;
16200         struct sd_lun   *un;
16201 
16202         ASSERT(priority_bp != NULL);
16203         un = SD_GET_UN(priority_bp);
16204         ASSERT(un != NULL);
16205         ASSERT(!mutex_owned(SD_MUTEX(un)));
16206 
16207         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16208             "sd_start_direct_priority_command: entry\n");
16209 
16210         mutex_enter(SD_MUTEX(un));
16211         un->un_direct_priority_timeid = NULL;
16212         sd_start_cmds(un, priority_bp);
16213         mutex_exit(SD_MUTEX(un));
16214 
16215         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16216             "sd_start_direct_priority_command: exit\n");
16217 }
16218 
16219 
16220 /*
16221  *    Function: sd_send_request_sense_command
16222  *
16223  * Description: Sends a REQUEST SENSE command to the target
16224  *
16225  *     Context: May be called from interrupt context.
16226  */
16227 
16228 static void
16229 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
16230     struct scsi_pkt *pktp)
16231 {
16232         ASSERT(bp != NULL);
16233         ASSERT(un != NULL);
16234         ASSERT(mutex_owned(SD_MUTEX(un)));
16235 
16236         SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: "
16237             "entry: buf:0x%p\n", bp);
16238 
16239         /*
16240          * If we are syncing or dumping, then fail the command to avoid a
16241          * recursive callback into scsi_transport(). Also fail the command
16242          * if we are suspended (legacy behavior).
16243          */
16244         if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
16245             (un->un_state == SD_STATE_DUMPING)) {
16246                 sd_return_failed_command(un, bp, EIO);
16247                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16248                     "sd_send_request_sense_command: syncing/dumping, exit\n");
16249                 return;
16250         }
16251 
16252         /*
16253          * Retry the failed command and don't issue the request sense if:
16254          *    1) the sense buf is busy
16255          *    2) we have 1 or more outstanding commands on the target
16256          *    (the sense data will be cleared or invalidated any way)
16257          *
16258          * Note: There could be an issue with not checking a retry limit here,
16259          * the problem is determining which retry limit to check.
16260          */
16261         if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) {
16262                 /* Don't retry if the command is flagged as non-retryable */
16263                 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16264                         sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
16265                             NULL, NULL, 0, un->un_busy_timeout,
16266                             kstat_waitq_enter);
16267                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16268                             "sd_send_request_sense_command: "
16269                             "at full throttle, retrying exit\n");
16270                 } else {
16271                         sd_return_failed_command(un, bp, EIO);
16272                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16273                             "sd_send_request_sense_command: "
16274                             "at full throttle, non-retryable exit\n");
16275                 }
16276                 return;
16277         }
16278 
16279         sd_mark_rqs_busy(un, bp);
16280         sd_start_cmds(un, un->un_rqs_bp);
16281 
16282         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16283             "sd_send_request_sense_command: exit\n");
16284 }
16285 
16286 
16287 /*
16288  *    Function: sd_mark_rqs_busy
16289  *
16290  * Description: Indicate that the request sense bp for this instance is
16291  *              in use.
16292  *
16293  *     Context: May be called under interrupt context
16294  */
16295 
16296 static void
16297 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp)
16298 {
16299         struct sd_xbuf  *sense_xp;
16300 
16301         ASSERT(un != NULL);
16302         ASSERT(bp != NULL);
16303         ASSERT(mutex_owned(SD_MUTEX(un)));
16304         ASSERT(un->un_sense_isbusy == 0);
16305 
16306         SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: "
16307             "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un);
16308 
16309         sense_xp = SD_GET_XBUF(un->un_rqs_bp);
16310         ASSERT(sense_xp != NULL);
16311 
16312         SD_INFO(SD_LOG_IO, un,
16313             "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp);
16314 
16315         ASSERT(sense_xp->xb_pktp != NULL);
16316         ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD))
16317             == (FLAG_SENSING | FLAG_HEAD));
16318 
16319         un->un_sense_isbusy = 1;
16320         un->un_rqs_bp->b_resid = 0;
16321         sense_xp->xb_pktp->pkt_resid  = 0;
16322         sense_xp->xb_pktp->pkt_reason = 0;
16323 
16324         /* So we can get back the bp at interrupt time! */
16325         sense_xp->xb_sense_bp = bp;
16326 
16327         bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH);
16328 
16329         /*
16330          * Mark this buf as awaiting sense data. (This is already set in
16331          * the pkt_flags for the RQS packet.)
16332          */
16333         ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING;
16334 
16335         /* Request sense down same path */
16336         if (scsi_pkt_allocated_correctly((SD_GET_XBUF(bp))->xb_pktp) &&
16337             ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance)
16338                 sense_xp->xb_pktp->pkt_path_instance =
16339                     ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance;
16340 
16341         sense_xp->xb_retry_count     = 0;
16342         sense_xp->xb_victim_retry_count = 0;
16343         sense_xp->xb_ua_retry_count  = 0;
16344         sense_xp->xb_nr_retry_count  = 0;
16345         sense_xp->xb_dma_resid  = 0;
16346 
16347         /* Clean up the fields for auto-request sense */
16348         sense_xp->xb_sense_status = 0;
16349         sense_xp->xb_sense_state  = 0;
16350         sense_xp->xb_sense_resid  = 0;
16351         bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data));
16352 
16353         SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n");
16354 }
16355 
16356 
16357 /*
16358  *    Function: sd_mark_rqs_idle
16359  *
16360  * Description: SD_MUTEX must be held continuously through this routine
16361  *              to prevent reuse of the rqs struct before the caller can
16362  *              complete it's processing.
16363  *
16364  * Return Code: Pointer to the RQS buf
16365  *
16366  *     Context: May be called under interrupt context
16367  */
16368 
16369 static struct buf *
16370 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp)
16371 {
16372         struct buf *bp;
16373         ASSERT(un != NULL);
16374         ASSERT(sense_xp != NULL);
16375         ASSERT(mutex_owned(SD_MUTEX(un)));
16376         ASSERT(un->un_sense_isbusy != 0);
16377 
16378         un->un_sense_isbusy = 0;
16379         bp = sense_xp->xb_sense_bp;
16380         sense_xp->xb_sense_bp = NULL;
16381 
16382         /* This pkt is no longer interested in getting sense data */
16383         ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING;
16384 
16385         return (bp);
16386 }
16387 
16388 
16389 
16390 /*
16391  *    Function: sd_alloc_rqs
16392  *
16393  * Description: Set up the unit to receive auto request sense data
16394  *
16395  * Return Code: DDI_SUCCESS or DDI_FAILURE
16396  *
16397  *     Context: Called under attach(9E) context
16398  */
16399 
16400 static int
16401 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un)
16402 {
16403         struct sd_xbuf *xp;
16404 
16405         ASSERT(un != NULL);
16406         ASSERT(!mutex_owned(SD_MUTEX(un)));
16407         ASSERT(un->un_rqs_bp == NULL);
16408         ASSERT(un->un_rqs_pktp == NULL);
16409 
16410         /*
16411          * First allocate the required buf and scsi_pkt structs, then set up
16412          * the CDB in the scsi_pkt for a REQUEST SENSE command.
16413          */
16414         un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL,
16415             MAX_SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL);
16416         if (un->un_rqs_bp == NULL) {
16417                 return (DDI_FAILURE);
16418         }
16419 
16420         un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp,
16421             CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL);
16422 
16423         if (un->un_rqs_pktp == NULL) {
16424                 sd_free_rqs(un);
16425                 return (DDI_FAILURE);
16426         }
16427 
16428         /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */
16429         (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp,
16430             SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0);
16431 
16432         SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp);
16433 
16434         /* Set up the other needed members in the ARQ scsi_pkt. */
16435         un->un_rqs_pktp->pkt_comp   = sdintr;
16436         un->un_rqs_pktp->pkt_time   = sd_io_time;
16437         un->un_rqs_pktp->pkt_flags |=
16438             (FLAG_SENSING | FLAG_HEAD); /* (1222170) */
16439 
16440         /*
16441          * Allocate  & init the sd_xbuf struct for the RQS command. Do not
16442          * provide any intpkt, destroypkt routines as we take care of
16443          * scsi_pkt allocation/freeing here and in sd_free_rqs().
16444          */
16445         xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
16446         sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL);
16447         xp->xb_pktp = un->un_rqs_pktp;
16448         SD_INFO(SD_LOG_ATTACH_DETACH, un,
16449             "sd_alloc_rqs: un 0x%p, rqs  xp 0x%p,  pkt 0x%p,  buf 0x%p\n",
16450             un, xp, un->un_rqs_pktp, un->un_rqs_bp);
16451 
16452         /*
16453          * Save the pointer to the request sense private bp so it can
16454          * be retrieved in sdintr.
16455          */
16456         un->un_rqs_pktp->pkt_private = un->un_rqs_bp;
16457         ASSERT(un->un_rqs_bp->b_private == xp);
16458 
16459         /*
16460          * See if the HBA supports auto-request sense for the specified
16461          * target/lun. If it does, then try to enable it (if not already
16462          * enabled).
16463          *
16464          * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return
16465          * failure, while for other HBAs (pln) scsi_ifsetcap will always
16466          * return success.  However, in both of these cases ARQ is always
16467          * enabled and scsi_ifgetcap will always return true. The best approach
16468          * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap().
16469          *
16470          * The 3rd case is the HBA (adp) always return enabled on
16471          * scsi_ifgetgetcap even when it's not enable, the best approach
16472          * is issue a scsi_ifsetcap then a scsi_ifgetcap
16473          * Note: this case is to circumvent the Adaptec bug. (x86 only)
16474          */
16475 
16476         if (un->un_f_is_fibre == TRUE) {
16477                 un->un_f_arq_enabled = TRUE;
16478         } else {
16479 #if defined(__i386) || defined(__amd64)
16480                 /*
16481                  * Circumvent the Adaptec bug, remove this code when
16482                  * the bug is fixed
16483                  */
16484                 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1);
16485 #endif
16486                 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) {
16487                 case 0:
16488                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
16489                             "sd_alloc_rqs: HBA supports ARQ\n");
16490                         /*
16491                          * ARQ is supported by this HBA but currently is not
16492                          * enabled. Attempt to enable it and if successful then
16493                          * mark this instance as ARQ enabled.
16494                          */
16495                         if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1)
16496                             == 1) {
16497                                 /* Successfully enabled ARQ in the HBA */
16498                                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16499                                     "sd_alloc_rqs: ARQ enabled\n");
16500                                 un->un_f_arq_enabled = TRUE;
16501                         } else {
16502                                 /* Could not enable ARQ in the HBA */
16503                                 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16504                                     "sd_alloc_rqs: failed ARQ enable\n");
16505                                 un->un_f_arq_enabled = FALSE;
16506                         }
16507                         break;
16508                 case 1:
16509                         /*
16510                          * ARQ is supported by this HBA and is already enabled.
16511                          * Just mark ARQ as enabled for this instance.
16512                          */
16513                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
16514                             "sd_alloc_rqs: ARQ already enabled\n");
16515                         un->un_f_arq_enabled = TRUE;
16516                         break;
16517                 default:
16518                         /*
16519                          * ARQ is not supported by this HBA; disable it for this
16520                          * instance.
16521                          */
16522                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
16523                             "sd_alloc_rqs: HBA does not support ARQ\n");
16524                         un->un_f_arq_enabled = FALSE;
16525                         break;
16526                 }
16527         }
16528 
16529         return (DDI_SUCCESS);
16530 }
16531 
16532 
16533 /*
16534  *    Function: sd_free_rqs
16535  *
16536  * Description: Cleanup for the pre-instance RQS command.
16537  *
16538  *     Context: Kernel thread context
16539  */
16540 
16541 static void
16542 sd_free_rqs(struct sd_lun *un)
16543 {
16544         ASSERT(un != NULL);
16545 
16546         SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n");
16547 
16548         /*
16549          * If consistent memory is bound to a scsi_pkt, the pkt
16550          * has to be destroyed *before* freeing the consistent memory.
16551          * Don't change the sequence of this operations.
16552          * scsi_destroy_pkt() might access memory, which isn't allowed,
16553          * after it was freed in scsi_free_consistent_buf().
16554          */
16555         if (un->un_rqs_pktp != NULL) {
16556                 scsi_destroy_pkt(un->un_rqs_pktp);
16557                 un->un_rqs_pktp = NULL;
16558         }
16559 
16560         if (un->un_rqs_bp != NULL) {
16561                 struct sd_xbuf *xp = SD_GET_XBUF(un->un_rqs_bp);
16562                 if (xp != NULL) {
16563                         kmem_free(xp, sizeof (struct sd_xbuf));
16564                 }
16565                 scsi_free_consistent_buf(un->un_rqs_bp);
16566                 un->un_rqs_bp = NULL;
16567         }
16568         SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n");
16569 }
16570 
16571 
16572 
16573 /*
16574  *    Function: sd_reduce_throttle
16575  *
16576  * Description: Reduces the maximum # of outstanding commands on a
16577  *              target to the current number of outstanding commands.
16578  *              Queues a tiemout(9F) callback to restore the limit
16579  *              after a specified interval has elapsed.
16580  *              Typically used when we get a TRAN_BUSY return code
16581  *              back from scsi_transport().
16582  *
16583  *   Arguments: un - ptr to the sd_lun softstate struct
16584  *              throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL
16585  *
16586  *     Context: May be called from interrupt context
16587  */
16588 
16589 static void
16590 sd_reduce_throttle(struct sd_lun *un, int throttle_type)
16591 {
16592         ASSERT(un != NULL);
16593         ASSERT(mutex_owned(SD_MUTEX(un)));
16594         ASSERT(un->un_ncmds_in_transport >= 0);
16595 
16596         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16597             "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n",
16598             un, un->un_throttle, un->un_ncmds_in_transport);
16599 
16600         if (un->un_throttle > 1) {
16601                 if (un->un_f_use_adaptive_throttle == TRUE) {
16602                         switch (throttle_type) {
16603                         case SD_THROTTLE_TRAN_BUSY:
16604                                 if (un->un_busy_throttle == 0) {
16605                                         un->un_busy_throttle = un->un_throttle;
16606                                 }
16607                                 break;
16608                         case SD_THROTTLE_QFULL:
16609                                 un->un_busy_throttle = 0;
16610                                 break;
16611                         default:
16612                                 ASSERT(FALSE);
16613                         }
16614 
16615                         if (un->un_ncmds_in_transport > 0) {
16616                                 un->un_throttle = un->un_ncmds_in_transport;
16617                         }
16618 
16619                 } else {
16620                         if (un->un_ncmds_in_transport == 0) {
16621                                 un->un_throttle = 1;
16622                         } else {
16623                                 un->un_throttle = un->un_ncmds_in_transport;
16624                         }
16625                 }
16626         }
16627 
16628         /* Reschedule the timeout if none is currently active */
16629         if (un->un_reset_throttle_timeid == NULL) {
16630                 un->un_reset_throttle_timeid = timeout(sd_restore_throttle,
16631                     un, SD_THROTTLE_RESET_INTERVAL);
16632                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16633                     "sd_reduce_throttle: timeout scheduled!\n");
16634         }
16635 
16636         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16637             "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16638 }
16639 
16640 
16641 
16642 /*
16643  *    Function: sd_restore_throttle
16644  *
16645  * Description: Callback function for timeout(9F).  Resets the current
16646  *              value of un->un_throttle to its default.
16647  *
16648  *   Arguments: arg - pointer to associated softstate for the device.
16649  *
16650  *     Context: May be called from interrupt context
16651  */
16652 
16653 static void
16654 sd_restore_throttle(void *arg)
16655 {
16656         struct sd_lun   *un = arg;
16657 
16658         ASSERT(un != NULL);
16659         ASSERT(!mutex_owned(SD_MUTEX(un)));
16660 
16661         mutex_enter(SD_MUTEX(un));
16662 
16663         SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16664             "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16665 
16666         un->un_reset_throttle_timeid = NULL;
16667 
16668         if (un->un_f_use_adaptive_throttle == TRUE) {
16669                 /*
16670                  * If un_busy_throttle is nonzero, then it contains the
16671                  * value that un_throttle was when we got a TRAN_BUSY back
16672                  * from scsi_transport(). We want to revert back to this
16673                  * value.
16674                  *
16675                  * In the QFULL case, the throttle limit will incrementally
16676                  * increase until it reaches max throttle.
16677                  */
16678                 if (un->un_busy_throttle > 0) {
16679                         un->un_throttle = un->un_busy_throttle;
16680                         un->un_busy_throttle = 0;
16681                 } else {
16682                         /*
16683                          * increase throttle by 10% open gate slowly, schedule
16684                          * another restore if saved throttle has not been
16685                          * reached
16686                          */
16687                         short throttle;
16688                         if (sd_qfull_throttle_enable) {
16689                                 throttle = un->un_throttle +
16690                                     max((un->un_throttle / 10), 1);
16691                                 un->un_throttle =
16692                                     (throttle < un->un_saved_throttle) ?
16693                                     throttle : un->un_saved_throttle;
16694                                 if (un->un_throttle < un->un_saved_throttle) {
16695                                         un->un_reset_throttle_timeid =
16696                                             timeout(sd_restore_throttle,
16697                                             un,
16698                                             SD_QFULL_THROTTLE_RESET_INTERVAL);
16699                                 }
16700                         }
16701                 }
16702 
16703                 /*
16704                  * If un_throttle has fallen below the low-water mark, we
16705                  * restore the maximum value here (and allow it to ratchet
16706                  * down again if necessary).
16707                  */
16708                 if (un->un_throttle < un->un_min_throttle) {
16709                         un->un_throttle = un->un_saved_throttle;
16710                 }
16711         } else {
16712                 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16713                     "restoring limit from 0x%x to 0x%x\n",
16714                     un->un_throttle, un->un_saved_throttle);
16715                 un->un_throttle = un->un_saved_throttle;
16716         }
16717 
16718         SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16719             "sd_restore_throttle: calling sd_start_cmds!\n");
16720 
16721         sd_start_cmds(un, NULL);
16722 
16723         SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16724             "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n",
16725             un, un->un_throttle);
16726 
16727         mutex_exit(SD_MUTEX(un));
16728 
16729         SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n");
16730 }
16731 
16732 /*
16733  *    Function: sdrunout
16734  *
16735  * Description: Callback routine for scsi_init_pkt when a resource allocation
16736  *              fails.
16737  *
16738  *   Arguments: arg - a pointer to the sd_lun unit struct for the particular
16739  *              soft state instance.
16740  *
16741  * Return Code: The scsi_init_pkt routine allows for the callback function to
16742  *              return a 0 indicating the callback should be rescheduled or a 1
16743  *              indicating not to reschedule. This routine always returns 1
16744  *              because the driver always provides a callback function to
16745  *              scsi_init_pkt. This results in a callback always being scheduled
16746  *              (via the scsi_init_pkt callback implementation) if a resource
16747  *              failure occurs.
16748  *
16749  *     Context: This callback function may not block or call routines that block
16750  *
16751  *        Note: Using the scsi_init_pkt callback facility can result in an I/O
16752  *              request persisting at the head of the list which cannot be
16753  *              satisfied even after multiple retries. In the future the driver
16754  *              may implement some time of maximum runout count before failing
16755  *              an I/O.
16756  */
16757 
16758 static int
16759 sdrunout(caddr_t arg)
16760 {
16761         struct sd_lun   *un = (struct sd_lun *)arg;
16762 
16763         ASSERT(un != NULL);
16764         ASSERT(!mutex_owned(SD_MUTEX(un)));
16765 
16766         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n");
16767 
16768         mutex_enter(SD_MUTEX(un));
16769         sd_start_cmds(un, NULL);
16770         mutex_exit(SD_MUTEX(un));
16771         /*
16772          * This callback routine always returns 1 (i.e. do not reschedule)
16773          * because we always specify sdrunout as the callback handler for
16774          * scsi_init_pkt inside the call to sd_start_cmds.
16775          */
16776         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n");
16777         return (1);
16778 }
16779 
16780 
16781 /*
16782  *    Function: sdintr
16783  *
16784  * Description: Completion callback routine for scsi_pkt(9S) structs
16785  *              sent to the HBA driver via scsi_transport(9F).
16786  *
16787  *     Context: Interrupt context
16788  */
16789 
16790 static void
16791 sdintr(struct scsi_pkt *pktp)
16792 {
16793         struct buf      *bp;
16794         struct sd_xbuf  *xp;
16795         struct sd_lun   *un;
16796         size_t          actual_len;
16797         sd_ssc_t        *sscp;
16798 
16799         ASSERT(pktp != NULL);
16800         bp = (struct buf *)pktp->pkt_private;
16801         ASSERT(bp != NULL);
16802         xp = SD_GET_XBUF(bp);
16803         ASSERT(xp != NULL);
16804         ASSERT(xp->xb_pktp != NULL);
16805         un = SD_GET_UN(bp);
16806         ASSERT(un != NULL);
16807         ASSERT(!mutex_owned(SD_MUTEX(un)));
16808 
16809 #ifdef SD_FAULT_INJECTION
16810 
16811         SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n");
16812         /* SD FaultInjection */
16813         sd_faultinjection(pktp);
16814 
16815 #endif /* SD_FAULT_INJECTION */
16816 
16817         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p,"
16818             " xp:0x%p, un:0x%p\n", bp, xp, un);
16819 
16820         mutex_enter(SD_MUTEX(un));
16821 
16822         ASSERT(un->un_fm_private != NULL);
16823         sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
16824         ASSERT(sscp != NULL);
16825 
16826         /* Reduce the count of the #commands currently in transport */
16827         un->un_ncmds_in_transport--;
16828         ASSERT(un->un_ncmds_in_transport >= 0);
16829 
16830         /* Increment counter to indicate that the callback routine is active */
16831         un->un_in_callback++;
16832 
16833         SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
16834 
16835 #ifdef  SDDEBUG
16836         if (bp == un->un_retry_bp) {
16837                 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: "
16838                     "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n",
16839                     un, un->un_retry_bp, un->un_ncmds_in_transport);
16840         }
16841 #endif
16842 
16843         /*
16844          * If pkt_reason is CMD_DEV_GONE, fail the command, and update the media
16845          * state if needed.
16846          */
16847         if (pktp->pkt_reason == CMD_DEV_GONE) {
16848                 /* Prevent multiple console messages for the same failure. */
16849                 if (un->un_last_pkt_reason != CMD_DEV_GONE) {
16850                         un->un_last_pkt_reason = CMD_DEV_GONE;
16851                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16852                             "Command failed to complete...Device is gone\n");
16853                 }
16854                 if (un->un_mediastate != DKIO_DEV_GONE) {
16855                         un->un_mediastate = DKIO_DEV_GONE;
16856                         cv_broadcast(&un->un_state_cv);
16857                 }
16858                 /*
16859                  * If the command happens to be the REQUEST SENSE command,
16860                  * free up the rqs buf and fail the original command.
16861                  */
16862                 if (bp == un->un_rqs_bp) {
16863                         bp = sd_mark_rqs_idle(un, xp);
16864                 }
16865                 sd_return_failed_command(un, bp, EIO);
16866                 goto exit;
16867         }
16868 
16869         if (pktp->pkt_state & STATE_XARQ_DONE) {
16870                 SD_TRACE(SD_LOG_COMMON, un,
16871                     "sdintr: extra sense data received. pkt=%p\n", pktp);
16872         }
16873 
16874         /*
16875          * First see if the pkt has auto-request sense data with it....
16876          * Look at the packet state first so we don't take a performance
16877          * hit looking at the arq enabled flag unless absolutely necessary.
16878          */
16879         if ((pktp->pkt_state & STATE_ARQ_DONE) &&
16880             (un->un_f_arq_enabled == TRUE)) {
16881                 /*
16882                  * The HBA did an auto request sense for this command so check
16883                  * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16884                  * driver command that should not be retried.
16885                  */
16886                 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16887                         /*
16888                          * Save the relevant sense info into the xp for the
16889                          * original cmd.
16890                          */
16891                         struct scsi_arq_status *asp;
16892                         asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
16893                         xp->xb_sense_status =
16894                             *((uchar_t *)(&(asp->sts_rqpkt_status)));
16895                         xp->xb_sense_state  = asp->sts_rqpkt_state;
16896                         xp->xb_sense_resid  = asp->sts_rqpkt_resid;
16897                         if (pktp->pkt_state & STATE_XARQ_DONE) {
16898                                 actual_len = MAX_SENSE_LENGTH -
16899                                     xp->xb_sense_resid;
16900                                 bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16901                                     MAX_SENSE_LENGTH);
16902                         } else {
16903                                 if (xp->xb_sense_resid > SENSE_LENGTH) {
16904                                         actual_len = MAX_SENSE_LENGTH -
16905                                             xp->xb_sense_resid;
16906                                 } else {
16907                                         actual_len = SENSE_LENGTH -
16908                                             xp->xb_sense_resid;
16909                                 }
16910                                 if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
16911                                         if ((((struct uscsi_cmd *)
16912                                             (xp->xb_pktinfo))->uscsi_rqlen) >
16913                                             actual_len) {
16914                                                 xp->xb_sense_resid =
16915                                                     (((struct uscsi_cmd *)
16916                                                     (xp->xb_pktinfo))->
16917                                                     uscsi_rqlen) - actual_len;
16918                                         } else {
16919                                                 xp->xb_sense_resid = 0;
16920                                         }
16921                                 }
16922                                 bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16923                                     SENSE_LENGTH);
16924                         }
16925 
16926                         /* fail the command */
16927                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16928                             "sdintr: arq done and FLAG_DIAGNOSE set\n");
16929                         sd_return_failed_command(un, bp, EIO);
16930                         goto exit;
16931                 }
16932 
16933 #if (defined(__i386) || defined(__amd64))       /* DMAFREE for x86 only */
16934                 /*
16935                  * We want to either retry or fail this command, so free
16936                  * the DMA resources here.  If we retry the command then
16937                  * the DMA resources will be reallocated in sd_start_cmds().
16938                  * Note that when PKT_DMA_PARTIAL is used, this reallocation
16939                  * causes the *entire* transfer to start over again from the
16940                  * beginning of the request, even for PARTIAL chunks that
16941                  * have already transferred successfully.
16942                  */
16943                 if ((un->un_f_is_fibre == TRUE) &&
16944                     ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16945                     ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
16946                         scsi_dmafree(pktp);
16947                         xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16948                 }
16949 #endif
16950 
16951                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16952                     "sdintr: arq done, sd_handle_auto_request_sense\n");
16953 
16954                 sd_handle_auto_request_sense(un, bp, xp, pktp);
16955                 goto exit;
16956         }
16957 
16958         /* Next see if this is the REQUEST SENSE pkt for the instance */
16959         if (pktp->pkt_flags & FLAG_SENSING)  {
16960                 /* This pktp is from the unit's REQUEST_SENSE command */
16961                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16962                     "sdintr: sd_handle_request_sense\n");
16963                 sd_handle_request_sense(un, bp, xp, pktp);
16964                 goto exit;
16965         }
16966 
16967         /*
16968          * Check to see if the command successfully completed as requested;
16969          * this is the most common case (and also the hot performance path).
16970          *
16971          * Requirements for successful completion are:
16972          * pkt_reason is CMD_CMPLT and packet status is status good.
16973          * In addition:
16974          * - A residual of zero indicates successful completion no matter what
16975          *   the command is.
16976          * - If the residual is not zero and the command is not a read or
16977          *   write, then it's still defined as successful completion. In other
16978          *   words, if the command is a read or write the residual must be
16979          *   zero for successful completion.
16980          * - If the residual is not zero and the command is a read or
16981          *   write, and it's a USCSICMD, then it's still defined as
16982          *   successful completion.
16983          */
16984         if ((pktp->pkt_reason == CMD_CMPLT) &&
16985             (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) {
16986 
16987                 /*
16988                  * Since this command is returned with a good status, we
16989                  * can reset the count for Sonoma failover.
16990                  */
16991                 un->un_sonoma_failure_count = 0;
16992 
16993                 /*
16994                  * Return all USCSI commands on good status
16995                  */
16996                 if (pktp->pkt_resid == 0) {
16997                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16998                             "sdintr: returning command for resid == 0\n");
16999                 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) &&
17000                     ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) {
17001                         SD_UPDATE_B_RESID(bp, pktp);
17002                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17003                             "sdintr: returning command for resid != 0\n");
17004                 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
17005                         SD_UPDATE_B_RESID(bp, pktp);
17006                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17007                             "sdintr: returning uscsi command\n");
17008                 } else {
17009                         goto not_successful;
17010                 }
17011                 sd_return_command(un, bp);
17012 
17013                 /*
17014                  * Decrement counter to indicate that the callback routine
17015                  * is done.
17016                  */
17017                 un->un_in_callback--;
17018                 ASSERT(un->un_in_callback >= 0);
17019                 mutex_exit(SD_MUTEX(un));
17020 
17021                 return;
17022         }
17023 
17024 not_successful:
17025 
17026 #if (defined(__i386) || defined(__amd64))       /* DMAFREE for x86 only */
17027         /*
17028          * The following is based upon knowledge of the underlying transport
17029          * and its use of DMA resources.  This code should be removed when
17030          * PKT_DMA_PARTIAL support is taken out of the disk driver in favor
17031          * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf()
17032          * and sd_start_cmds().
17033          *
17034          * Free any DMA resources associated with this command if there
17035          * is a chance it could be retried or enqueued for later retry.
17036          * If we keep the DMA binding then mpxio cannot reissue the
17037          * command on another path whenever a path failure occurs.
17038          *
17039          * Note that when PKT_DMA_PARTIAL is used, free/reallocation
17040          * causes the *entire* transfer to start over again from the
17041          * beginning of the request, even for PARTIAL chunks that
17042          * have already transferred successfully.
17043          *
17044          * This is only done for non-uscsi commands (and also skipped for the
17045          * driver's internal RQS command). Also just do this for Fibre Channel
17046          * devices as these are the only ones that support mpxio.
17047          */
17048         if ((un->un_f_is_fibre == TRUE) &&
17049             ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
17050             ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
17051                 scsi_dmafree(pktp);
17052                 xp->xb_pkt_flags |= SD_XB_DMA_FREED;
17053         }
17054 #endif
17055 
17056         /*
17057          * The command did not successfully complete as requested so check
17058          * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
17059          * driver command that should not be retried so just return. If
17060          * FLAG_DIAGNOSE is not set the error will be processed below.
17061          */
17062         if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
17063                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17064                     "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n");
17065                 /*
17066                  * Issue a request sense if a check condition caused the error
17067                  * (we handle the auto request sense case above), otherwise
17068                  * just fail the command.
17069                  */
17070                 if ((pktp->pkt_reason == CMD_CMPLT) &&
17071                     (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) {
17072                         sd_send_request_sense_command(un, bp, pktp);
17073                 } else {
17074                         sd_return_failed_command(un, bp, EIO);
17075                 }
17076                 goto exit;
17077         }
17078 
17079         /*
17080          * The command did not successfully complete as requested so process
17081          * the error, retry, and/or attempt recovery.
17082          */
17083         switch (pktp->pkt_reason) {
17084         case CMD_CMPLT:
17085                 switch (SD_GET_PKT_STATUS(pktp)) {
17086                 case STATUS_GOOD:
17087                         /*
17088                          * The command completed successfully with a non-zero
17089                          * residual
17090                          */
17091                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17092                             "sdintr: STATUS_GOOD \n");
17093                         sd_pkt_status_good(un, bp, xp, pktp);
17094                         break;
17095 
17096                 case STATUS_CHECK:
17097                 case STATUS_TERMINATED:
17098                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17099                             "sdintr: STATUS_TERMINATED | STATUS_CHECK\n");
17100                         sd_pkt_status_check_condition(un, bp, xp, pktp);
17101                         break;
17102 
17103                 case STATUS_BUSY:
17104                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17105                             "sdintr: STATUS_BUSY\n");
17106                         sd_pkt_status_busy(un, bp, xp, pktp);
17107                         break;
17108 
17109                 case STATUS_RESERVATION_CONFLICT:
17110                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17111                             "sdintr: STATUS_RESERVATION_CONFLICT\n");
17112                         sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17113                         break;
17114 
17115                 case STATUS_QFULL:
17116                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17117                             "sdintr: STATUS_QFULL\n");
17118                         sd_pkt_status_qfull(un, bp, xp, pktp);
17119                         break;
17120 
17121                 case STATUS_MET:
17122                 case STATUS_INTERMEDIATE:
17123                 case STATUS_SCSI2:
17124                 case STATUS_INTERMEDIATE_MET:
17125                 case STATUS_ACA_ACTIVE:
17126                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17127                             "Unexpected SCSI status received: 0x%x\n",
17128                             SD_GET_PKT_STATUS(pktp));
17129                         /*
17130                          * Mark the ssc_flags when detected invalid status
17131                          * code for non-USCSI command.
17132                          */
17133                         if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17134                                 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
17135                                     0, "stat-code");
17136                         }
17137                         sd_return_failed_command(un, bp, EIO);
17138                         break;
17139 
17140                 default:
17141                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17142                             "Invalid SCSI status received: 0x%x\n",
17143                             SD_GET_PKT_STATUS(pktp));
17144                         if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17145                                 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
17146                                     0, "stat-code");
17147                         }
17148                         sd_return_failed_command(un, bp, EIO);
17149                         break;
17150 
17151                 }
17152                 break;
17153 
17154         case CMD_INCOMPLETE:
17155                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17156                     "sdintr:  CMD_INCOMPLETE\n");
17157                 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp);
17158                 break;
17159         case CMD_TRAN_ERR:
17160                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17161                     "sdintr: CMD_TRAN_ERR\n");
17162                 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp);
17163                 break;
17164         case CMD_RESET:
17165                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17166                     "sdintr: CMD_RESET \n");
17167                 sd_pkt_reason_cmd_reset(un, bp, xp, pktp);
17168                 break;
17169         case CMD_ABORTED:
17170                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17171                     "sdintr: CMD_ABORTED \n");
17172                 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp);
17173                 break;
17174         case CMD_TIMEOUT:
17175                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17176                     "sdintr: CMD_TIMEOUT\n");
17177                 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp);
17178                 break;
17179         case CMD_UNX_BUS_FREE:
17180                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17181                     "sdintr: CMD_UNX_BUS_FREE \n");
17182                 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp);
17183                 break;
17184         case CMD_TAG_REJECT:
17185                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17186                     "sdintr: CMD_TAG_REJECT\n");
17187                 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp);
17188                 break;
17189         default:
17190                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17191                     "sdintr: default\n");
17192                 /*
17193                  * Mark the ssc_flags for detecting invliad pkt_reason.
17194                  */
17195                 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17196                         sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_PKT_REASON,
17197                             0, "pkt-reason");
17198                 }
17199                 sd_pkt_reason_default(un, bp, xp, pktp);
17200                 break;
17201         }
17202 
17203 exit:
17204         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n");
17205 
17206         /* Decrement counter to indicate that the callback routine is done. */
17207         un->un_in_callback--;
17208         ASSERT(un->un_in_callback >= 0);
17209 
17210         /*
17211          * At this point, the pkt has been dispatched, ie, it is either
17212          * being re-tried or has been returned to its caller and should
17213          * not be referenced.
17214          */
17215 
17216         mutex_exit(SD_MUTEX(un));
17217 }
17218 
17219 
17220 /*
17221  *    Function: sd_print_incomplete_msg
17222  *
17223  * Description: Prints the error message for a CMD_INCOMPLETE error.
17224  *
17225  *   Arguments: un - ptr to associated softstate for the device.
17226  *              bp - ptr to the buf(9S) for the command.
17227  *              arg - message string ptr
17228  *              code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED,
17229  *                      or SD_NO_RETRY_ISSUED.
17230  *
17231  *     Context: May be called under interrupt context
17232  */
17233 
17234 static void
17235 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17236 {
17237         struct scsi_pkt *pktp;
17238         char    *msgp;
17239         char    *cmdp = arg;
17240 
17241         ASSERT(un != NULL);
17242         ASSERT(mutex_owned(SD_MUTEX(un)));
17243         ASSERT(bp != NULL);
17244         ASSERT(arg != NULL);
17245         pktp = SD_GET_PKTP(bp);
17246         ASSERT(pktp != NULL);
17247 
17248         switch (code) {
17249         case SD_DELAYED_RETRY_ISSUED:
17250         case SD_IMMEDIATE_RETRY_ISSUED:
17251                 msgp = "retrying";
17252                 break;
17253         case SD_NO_RETRY_ISSUED:
17254         default:
17255                 msgp = "giving up";
17256                 break;
17257         }
17258 
17259         if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17260                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17261                     "incomplete %s- %s\n", cmdp, msgp);
17262         }
17263 }
17264 
17265 
17266 
17267 /*
17268  *    Function: sd_pkt_status_good
17269  *
17270  * Description: Processing for a STATUS_GOOD code in pkt_status.
17271  *
17272  *     Context: May be called under interrupt context
17273  */
17274 
17275 static void
17276 sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
17277     struct sd_xbuf *xp, struct scsi_pkt *pktp)
17278 {
17279         char    *cmdp;
17280 
17281         ASSERT(un != NULL);
17282         ASSERT(mutex_owned(SD_MUTEX(un)));
17283         ASSERT(bp != NULL);
17284         ASSERT(xp != NULL);
17285         ASSERT(pktp != NULL);
17286         ASSERT(pktp->pkt_reason == CMD_CMPLT);
17287         ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD);
17288         ASSERT(pktp->pkt_resid != 0);
17289 
17290         SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n");
17291 
17292         SD_UPDATE_ERRSTATS(un, sd_harderrs);
17293         switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) {
17294         case SCMD_READ:
17295                 cmdp = "read";
17296                 break;
17297         case SCMD_WRITE:
17298                 cmdp = "write";
17299                 break;
17300         default:
17301                 SD_UPDATE_B_RESID(bp, pktp);
17302                 sd_return_command(un, bp);
17303                 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17304                 return;
17305         }
17306 
17307         /*
17308          * See if we can retry the read/write, preferrably immediately.
17309          * If retries are exhaused, then sd_retry_command() will update
17310          * the b_resid count.
17311          */
17312         sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg,
17313             cmdp, EIO, (clock_t)0, NULL);
17314 
17315         SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17316 }
17317 
17318 
17319 
17320 
17321 
17322 /*
17323  *    Function: sd_handle_request_sense
17324  *
17325  * Description: Processing for non-auto Request Sense command.
17326  *
17327  *   Arguments: un - ptr to associated softstate
17328  *              sense_bp - ptr to buf(9S) for the RQS command
17329  *              sense_xp - ptr to the sd_xbuf for the RQS command
17330  *              sense_pktp - ptr to the scsi_pkt(9S) for the RQS command
17331  *
17332  *     Context: May be called under interrupt context
17333  */
17334 
17335 static void
17336 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp,
17337     struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp)
17338 {
17339         struct buf      *cmd_bp;        /* buf for the original command */
17340         struct sd_xbuf  *cmd_xp;        /* sd_xbuf for the original command */
17341         struct scsi_pkt *cmd_pktp;      /* pkt for the original command */
17342         size_t          actual_len;     /* actual sense data length */
17343 
17344         ASSERT(un != NULL);
17345         ASSERT(mutex_owned(SD_MUTEX(un)));
17346         ASSERT(sense_bp != NULL);
17347         ASSERT(sense_xp != NULL);
17348         ASSERT(sense_pktp != NULL);
17349 
17350         /*
17351          * Note the sense_bp, sense_xp, and sense_pktp here are for the
17352          * RQS command and not the original command.
17353          */
17354         ASSERT(sense_pktp == un->un_rqs_pktp);
17355         ASSERT(sense_bp   == un->un_rqs_bp);
17356         ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) ==
17357             (FLAG_SENSING | FLAG_HEAD));
17358         ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) &
17359             FLAG_SENSING) == FLAG_SENSING);
17360 
17361         /* These are the bp, xp, and pktp for the original command */
17362         cmd_bp = sense_xp->xb_sense_bp;
17363         cmd_xp = SD_GET_XBUF(cmd_bp);
17364         cmd_pktp = SD_GET_PKTP(cmd_bp);
17365 
17366         if (sense_pktp->pkt_reason != CMD_CMPLT) {
17367                 /*
17368                  * The REQUEST SENSE command failed.  Release the REQUEST
17369                  * SENSE command for re-use, get back the bp for the original
17370                  * command, and attempt to re-try the original command if
17371                  * FLAG_DIAGNOSE is not set in the original packet.
17372                  */
17373                 SD_UPDATE_ERRSTATS(un, sd_harderrs);
17374                 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17375                         cmd_bp = sd_mark_rqs_idle(un, sense_xp);
17376                         sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD,
17377                             NULL, NULL, EIO, (clock_t)0, NULL);
17378                         return;
17379                 }
17380         }
17381 
17382         /*
17383          * Save the relevant sense info into the xp for the original cmd.
17384          *
17385          * Note: if the request sense failed the state info will be zero
17386          * as set in sd_mark_rqs_busy()
17387          */
17388         cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp);
17389         cmd_xp->xb_sense_state  = sense_pktp->pkt_state;
17390         actual_len = MAX_SENSE_LENGTH - sense_pktp->pkt_resid;
17391         if ((cmd_xp->xb_pkt_flags & SD_XB_USCSICMD) &&
17392             (((struct uscsi_cmd *)cmd_xp->xb_pktinfo)->uscsi_rqlen >
17393             SENSE_LENGTH)) {
17394                 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17395                     MAX_SENSE_LENGTH);
17396                 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid;
17397         } else {
17398                 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17399                     SENSE_LENGTH);
17400                 if (actual_len < SENSE_LENGTH) {
17401                         cmd_xp->xb_sense_resid = SENSE_LENGTH - actual_len;
17402                 } else {
17403                         cmd_xp->xb_sense_resid = 0;
17404                 }
17405         }
17406 
17407         /*
17408          *  Free up the RQS command....
17409          *  NOTE:
17410          *      Must do this BEFORE calling sd_validate_sense_data!
17411          *      sd_validate_sense_data may return the original command in
17412          *      which case the pkt will be freed and the flags can no
17413          *      longer be touched.
17414          *      SD_MUTEX is held through this process until the command
17415          *      is dispatched based upon the sense data, so there are
17416          *      no race conditions.
17417          */
17418         (void) sd_mark_rqs_idle(un, sense_xp);
17419 
17420         /*
17421          * For a retryable command see if we have valid sense data, if so then
17422          * turn it over to sd_decode_sense() to figure out the right course of
17423          * action. Just fail a non-retryable command.
17424          */
17425         if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17426                 if (sd_validate_sense_data(un, cmd_bp, cmd_xp, actual_len) ==
17427                     SD_SENSE_DATA_IS_VALID) {
17428                         sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp);
17429                 }
17430         } else {
17431                 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB",
17432                     (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17433                 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data",
17434                     (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
17435                 sd_return_failed_command(un, cmd_bp, EIO);
17436         }
17437 }
17438 
17439 
17440 
17441 
17442 /*
17443  *    Function: sd_handle_auto_request_sense
17444  *
17445  * Description: Processing for auto-request sense information.
17446  *
17447  *   Arguments: un - ptr to associated softstate
17448  *              bp - ptr to buf(9S) for the command
17449  *              xp - ptr to the sd_xbuf for the command
17450  *              pktp - ptr to the scsi_pkt(9S) for the command
17451  *
17452  *     Context: May be called under interrupt context
17453  */
17454 
17455 static void
17456 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
17457     struct sd_xbuf *xp, struct scsi_pkt *pktp)
17458 {
17459         struct scsi_arq_status *asp;
17460         size_t actual_len;
17461 
17462         ASSERT(un != NULL);
17463         ASSERT(mutex_owned(SD_MUTEX(un)));
17464         ASSERT(bp != NULL);
17465         ASSERT(xp != NULL);
17466         ASSERT(pktp != NULL);
17467         ASSERT(pktp != un->un_rqs_pktp);
17468         ASSERT(bp   != un->un_rqs_bp);
17469 
17470         /*
17471          * For auto-request sense, we get a scsi_arq_status back from
17472          * the HBA, with the sense data in the sts_sensedata member.
17473          * The pkt_scbp of the packet points to this scsi_arq_status.
17474          */
17475         asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
17476 
17477         if (asp->sts_rqpkt_reason != CMD_CMPLT) {
17478                 /*
17479                  * The auto REQUEST SENSE failed; see if we can re-try
17480                  * the original command.
17481                  */
17482                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17483                     "auto request sense failed (reason=%s)\n",
17484                     scsi_rname(asp->sts_rqpkt_reason));
17485 
17486                 sd_reset_target(un, pktp);
17487 
17488                 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17489                     NULL, NULL, EIO, (clock_t)0, NULL);
17490                 return;
17491         }
17492 
17493         /* Save the relevant sense info into the xp for the original cmd. */
17494         xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status)));
17495         xp->xb_sense_state  = asp->sts_rqpkt_state;
17496         xp->xb_sense_resid  = asp->sts_rqpkt_resid;
17497         if (xp->xb_sense_state & STATE_XARQ_DONE) {
17498                 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17499                 bcopy(&asp->sts_sensedata, xp->xb_sense_data,
17500                     MAX_SENSE_LENGTH);
17501         } else {
17502                 if (xp->xb_sense_resid > SENSE_LENGTH) {
17503                         actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17504                 } else {
17505                         actual_len = SENSE_LENGTH - xp->xb_sense_resid;
17506                 }
17507                 if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
17508                         if ((((struct uscsi_cmd *)
17509                             (xp->xb_pktinfo))->uscsi_rqlen) > actual_len) {
17510                                 xp->xb_sense_resid = (((struct uscsi_cmd *)
17511                                     (xp->xb_pktinfo))->uscsi_rqlen) -
17512                                     actual_len;
17513                         } else {
17514                                 xp->xb_sense_resid = 0;
17515                         }
17516                 }
17517                 bcopy(&asp->sts_sensedata, xp->xb_sense_data, SENSE_LENGTH);
17518         }
17519 
17520         /*
17521          * See if we have valid sense data, if so then turn it over to
17522          * sd_decode_sense() to figure out the right course of action.
17523          */
17524         if (sd_validate_sense_data(un, bp, xp, actual_len) ==
17525             SD_SENSE_DATA_IS_VALID) {
17526                 sd_decode_sense(un, bp, xp, pktp);
17527         }
17528 }
17529 
17530 
17531 /*
17532  *    Function: sd_print_sense_failed_msg
17533  *
17534  * Description: Print log message when RQS has failed.
17535  *
17536  *   Arguments: un - ptr to associated softstate
17537  *              bp - ptr to buf(9S) for the command
17538  *              arg - generic message string ptr
17539  *              code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17540  *                      or SD_NO_RETRY_ISSUED
17541  *
17542  *     Context: May be called from interrupt context
17543  */
17544 
17545 static void
17546 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg,
17547     int code)
17548 {
17549         char    *msgp = arg;
17550 
17551         ASSERT(un != NULL);
17552         ASSERT(mutex_owned(SD_MUTEX(un)));
17553         ASSERT(bp != NULL);
17554 
17555         if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) {
17556                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp);
17557         }
17558 }
17559 
17560 
17561 /*
17562  *    Function: sd_validate_sense_data
17563  *
17564  * Description: Check the given sense data for validity.
17565  *              If the sense data is not valid, the command will
17566  *              be either failed or retried!
17567  *
17568  * Return Code: SD_SENSE_DATA_IS_INVALID
17569  *              SD_SENSE_DATA_IS_VALID
17570  *
17571  *     Context: May be called from interrupt context
17572  */
17573 
17574 static int
17575 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17576     size_t actual_len)
17577 {
17578         struct scsi_extended_sense *esp;
17579         struct  scsi_pkt *pktp;
17580         char    *msgp = NULL;
17581         sd_ssc_t *sscp;
17582 
17583         ASSERT(un != NULL);
17584         ASSERT(mutex_owned(SD_MUTEX(un)));
17585         ASSERT(bp != NULL);
17586         ASSERT(bp != un->un_rqs_bp);
17587         ASSERT(xp != NULL);
17588         ASSERT(un->un_fm_private != NULL);
17589 
17590         pktp = SD_GET_PKTP(bp);
17591         ASSERT(pktp != NULL);
17592 
17593         sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
17594         ASSERT(sscp != NULL);
17595 
17596         /*
17597          * Check the status of the RQS command (auto or manual).
17598          */
17599         switch (xp->xb_sense_status & STATUS_MASK) {
17600         case STATUS_GOOD:
17601                 break;
17602 
17603         case STATUS_RESERVATION_CONFLICT:
17604                 sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17605                 return (SD_SENSE_DATA_IS_INVALID);
17606 
17607         case STATUS_BUSY:
17608                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17609                     "Busy Status on REQUEST SENSE\n");
17610                 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL,
17611                     NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17612                 return (SD_SENSE_DATA_IS_INVALID);
17613 
17614         case STATUS_QFULL:
17615                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17616                     "QFULL Status on REQUEST SENSE\n");
17617                 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL,
17618                     NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17619                 return (SD_SENSE_DATA_IS_INVALID);
17620 
17621         case STATUS_CHECK:
17622         case STATUS_TERMINATED:
17623                 msgp = "Check Condition on REQUEST SENSE\n";
17624                 goto sense_failed;
17625 
17626         default:
17627                 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n";
17628                 goto sense_failed;
17629         }
17630 
17631         /*
17632          * See if we got the minimum required amount of sense data.
17633          * Note: We are assuming the returned sense data is SENSE_LENGTH bytes
17634          * or less.
17635          */
17636         if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) ||
17637             (actual_len == 0)) {
17638                 msgp = "Request Sense couldn't get sense data\n";
17639                 goto sense_failed;
17640         }
17641 
17642         if (actual_len < SUN_MIN_SENSE_LENGTH) {
17643                 msgp = "Not enough sense information\n";
17644                 /* Mark the ssc_flags for detecting invalid sense data */
17645                 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17646                         sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17647                             "sense-data");
17648                 }
17649                 goto sense_failed;
17650         }
17651 
17652         /*
17653          * We require the extended sense data
17654          */
17655         esp = (struct scsi_extended_sense *)xp->xb_sense_data;
17656         if (esp->es_class != CLASS_EXTENDED_SENSE) {
17657                 if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17658                         static char tmp[8];
17659                         static char buf[148];
17660                         char *p = (char *)(xp->xb_sense_data);
17661                         int i;
17662 
17663                         mutex_enter(&sd_sense_mutex);
17664                         (void) strcpy(buf, "undecodable sense information:");
17665                         for (i = 0; i < actual_len; i++) {
17666                                 (void) sprintf(tmp, " 0x%x", *(p++)&0xff);
17667                                 (void) strcpy(&buf[strlen(buf)], tmp);
17668                         }
17669                         i = strlen(buf);
17670                         (void) strcpy(&buf[i], "-(assumed fatal)\n");
17671 
17672                         if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
17673                                 scsi_log(SD_DEVINFO(un), sd_label,
17674                                     CE_WARN, buf);
17675                         }
17676                         mutex_exit(&sd_sense_mutex);
17677                 }
17678 
17679                 /* Mark the ssc_flags for detecting invalid sense data */
17680                 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17681                         sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17682                             "sense-data");
17683                 }
17684 
17685                 /* Note: Legacy behavior, fail the command with no retry */
17686                 sd_return_failed_command(un, bp, EIO);
17687                 return (SD_SENSE_DATA_IS_INVALID);
17688         }
17689 
17690         /*
17691          * Check that es_code is valid (es_class concatenated with es_code
17692          * make up the "response code" field.  es_class will always be 7, so
17693          * make sure es_code is 0, 1, 2, 3 or 0xf.  es_code will indicate the
17694          * format.
17695          */
17696         if ((esp->es_code != CODE_FMT_FIXED_CURRENT) &&
17697             (esp->es_code != CODE_FMT_FIXED_DEFERRED) &&
17698             (esp->es_code != CODE_FMT_DESCR_CURRENT) &&
17699             (esp->es_code != CODE_FMT_DESCR_DEFERRED) &&
17700             (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) {
17701                 /* Mark the ssc_flags for detecting invalid sense data */
17702                 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17703                         sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17704                             "sense-data");
17705                 }
17706                 goto sense_failed;
17707         }
17708 
17709         return (SD_SENSE_DATA_IS_VALID);
17710 
17711 sense_failed:
17712         /*
17713          * If the request sense failed (for whatever reason), attempt
17714          * to retry the original command.
17715          */
17716 #if defined(__i386) || defined(__amd64)
17717         /*
17718          * SD_RETRY_DELAY is conditionally compile (#if fibre) in
17719          * sddef.h for Sparc platform, and x86 uses 1 binary
17720          * for both SCSI/FC.
17721          * The SD_RETRY_DELAY value need to be adjusted here
17722          * when SD_RETRY_DELAY change in sddef.h
17723          */
17724         sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17725             sd_print_sense_failed_msg, msgp, EIO,
17726             un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL);
17727 #else
17728         sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17729             sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL);
17730 #endif
17731 
17732         return (SD_SENSE_DATA_IS_INVALID);
17733 }
17734 
17735 /*
17736  *    Function: sd_decode_sense
17737  *
17738  * Description: Take recovery action(s) when SCSI Sense Data is received.
17739  *
17740  *     Context: Interrupt context.
17741  */
17742 
17743 static void
17744 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17745     struct scsi_pkt *pktp)
17746 {
17747         uint8_t sense_key;
17748 
17749         ASSERT(un != NULL);
17750         ASSERT(mutex_owned(SD_MUTEX(un)));
17751         ASSERT(bp != NULL);
17752         ASSERT(bp != un->un_rqs_bp);
17753         ASSERT(xp != NULL);
17754         ASSERT(pktp != NULL);
17755 
17756         sense_key = scsi_sense_key(xp->xb_sense_data);
17757 
17758         switch (sense_key) {
17759         case KEY_NO_SENSE:
17760                 sd_sense_key_no_sense(un, bp, xp, pktp);
17761                 break;
17762         case KEY_RECOVERABLE_ERROR:
17763                 sd_sense_key_recoverable_error(un, xp->xb_sense_data,
17764                     bp, xp, pktp);
17765                 break;
17766         case KEY_NOT_READY:
17767                 sd_sense_key_not_ready(un, xp->xb_sense_data,
17768                     bp, xp, pktp);
17769                 break;
17770         case KEY_MEDIUM_ERROR:
17771         case KEY_HARDWARE_ERROR:
17772                 sd_sense_key_medium_or_hardware_error(un,
17773                     xp->xb_sense_data, bp, xp, pktp);
17774                 break;
17775         case KEY_ILLEGAL_REQUEST:
17776                 sd_sense_key_illegal_request(un, bp, xp, pktp);
17777                 break;
17778         case KEY_UNIT_ATTENTION:
17779                 sd_sense_key_unit_attention(un, xp->xb_sense_data,
17780                     bp, xp, pktp);
17781                 break;
17782         case KEY_WRITE_PROTECT:
17783         case KEY_VOLUME_OVERFLOW:
17784         case KEY_MISCOMPARE:
17785                 sd_sense_key_fail_command(un, bp, xp, pktp);
17786                 break;
17787         case KEY_BLANK_CHECK:
17788                 sd_sense_key_blank_check(un, bp, xp, pktp);
17789                 break;
17790         case KEY_ABORTED_COMMAND:
17791                 sd_sense_key_aborted_command(un, bp, xp, pktp);
17792                 break;
17793         case KEY_VENDOR_UNIQUE:
17794         case KEY_COPY_ABORTED:
17795         case KEY_EQUAL:
17796         case KEY_RESERVED:
17797         default:
17798                 sd_sense_key_default(un, xp->xb_sense_data,
17799                     bp, xp, pktp);
17800                 break;
17801         }
17802 }
17803 
17804 
17805 /*
17806  *    Function: sd_dump_memory
17807  *
17808  * Description: Debug logging routine to print the contents of a user provided
17809  *              buffer. The output of the buffer is broken up into 256 byte
17810  *              segments due to a size constraint of the scsi_log.
17811  *              implementation.
17812  *
17813  *   Arguments: un - ptr to softstate
17814  *              comp - component mask
17815  *              title - "title" string to preceed data when printed
17816  *              data - ptr to data block to be printed
17817  *              len - size of data block to be printed
17818  *              fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c)
17819  *
17820  *     Context: May be called from interrupt context
17821  */
17822 
17823 #define SD_DUMP_MEMORY_BUF_SIZE 256
17824 
17825 static char *sd_dump_format_string[] = {
17826                 " 0x%02x",
17827                 " %c"
17828 };
17829 
17830 static void
17831 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data,
17832     int len, int fmt)
17833 {
17834         int     i, j;
17835         int     avail_count;
17836         int     start_offset;
17837         int     end_offset;
17838         size_t  entry_len;
17839         char    *bufp;
17840         char    *local_buf;
17841         char    *format_string;
17842 
17843         ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR));
17844 
17845         /*
17846          * In the debug version of the driver, this function is called from a
17847          * number of places which are NOPs in the release driver.
17848          * The debug driver therefore has additional methods of filtering
17849          * debug output.
17850          */
17851 #ifdef SDDEBUG
17852         /*
17853          * In the debug version of the driver we can reduce the amount of debug
17854          * messages by setting sd_error_level to something other than
17855          * SCSI_ERR_ALL and clearing bits in sd_level_mask and
17856          * sd_component_mask.
17857          */
17858         if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) ||
17859             (sd_error_level != SCSI_ERR_ALL)) {
17860                 return;
17861         }
17862         if (((sd_component_mask & comp) == 0) ||
17863             (sd_error_level != SCSI_ERR_ALL)) {
17864                 return;
17865         }
17866 #else
17867         if (sd_error_level != SCSI_ERR_ALL) {
17868                 return;
17869         }
17870 #endif
17871 
17872         local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP);
17873         bufp = local_buf;
17874         /*
17875          * Available length is the length of local_buf[], minus the
17876          * length of the title string, minus one for the ":", minus
17877          * one for the newline, minus one for the NULL terminator.
17878          * This gives the #bytes available for holding the printed
17879          * values from the given data buffer.
17880          */
17881         if (fmt == SD_LOG_HEX) {
17882                 format_string = sd_dump_format_string[0];
17883         } else /* SD_LOG_CHAR */ {
17884                 format_string = sd_dump_format_string[1];
17885         }
17886         /*
17887          * Available count is the number of elements from the given
17888          * data buffer that we can fit into the available length.
17889          * This is based upon the size of the format string used.
17890          * Make one entry and find it's size.
17891          */
17892         (void) sprintf(bufp, format_string, data[0]);
17893         entry_len = strlen(bufp);
17894         avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len;
17895 
17896         j = 0;
17897         while (j < len) {
17898                 bufp = local_buf;
17899                 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE);
17900                 start_offset = j;
17901 
17902                 end_offset = start_offset + avail_count;
17903 
17904                 (void) sprintf(bufp, "%s:", title);
17905                 bufp += strlen(bufp);
17906                 for (i = start_offset; ((i < end_offset) && (j < len));
17907                     i++, j++) {
17908                         (void) sprintf(bufp, format_string, data[i]);
17909                         bufp += entry_len;
17910                 }
17911                 (void) sprintf(bufp, "\n");
17912 
17913                 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf);
17914         }
17915         kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE);
17916 }
17917 
17918 /*
17919  *    Function: sd_print_sense_msg
17920  *
17921  * Description: Log a message based upon the given sense data.
17922  *
17923  *   Arguments: un - ptr to associated softstate
17924  *              bp - ptr to buf(9S) for the command
17925  *              arg - ptr to associate sd_sense_info struct
17926  *              code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17927  *                      or SD_NO_RETRY_ISSUED
17928  *
17929  *     Context: May be called from interrupt context
17930  */
17931 
17932 static void
17933 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17934 {
17935         struct sd_xbuf  *xp;
17936         struct scsi_pkt *pktp;
17937         uint8_t *sensep;
17938         daddr_t request_blkno;
17939         diskaddr_t err_blkno;
17940         int severity;
17941         int pfa_flag;
17942         extern struct scsi_key_strings scsi_cmds[];
17943 
17944         ASSERT(un != NULL);
17945         ASSERT(mutex_owned(SD_MUTEX(un)));
17946         ASSERT(bp != NULL);
17947         xp = SD_GET_XBUF(bp);
17948         ASSERT(xp != NULL);
17949         pktp = SD_GET_PKTP(bp);
17950         ASSERT(pktp != NULL);
17951         ASSERT(arg != NULL);
17952 
17953         severity = ((struct sd_sense_info *)(arg))->ssi_severity;
17954         pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag;
17955 
17956         if ((code == SD_DELAYED_RETRY_ISSUED) ||
17957             (code == SD_IMMEDIATE_RETRY_ISSUED)) {
17958                 severity = SCSI_ERR_RETRYABLE;
17959         }
17960 
17961         /* Use absolute block number for the request block number */
17962         request_blkno = xp->xb_blkno;
17963 
17964         /*
17965          * Now try to get the error block number from the sense data
17966          */
17967         sensep = xp->xb_sense_data;
17968 
17969         if (scsi_sense_info_uint64(sensep, SENSE_LENGTH,
17970             (uint64_t *)&err_blkno)) {
17971                 /*
17972                  * We retrieved the error block number from the information
17973                  * portion of the sense data.
17974                  *
17975                  * For USCSI commands we are better off using the error
17976                  * block no. as the requested block no. (This is the best
17977                  * we can estimate.)
17978                  */
17979                 if ((SD_IS_BUFIO(xp) == FALSE) &&
17980                     ((pktp->pkt_flags & FLAG_SILENT) == 0)) {
17981                         request_blkno = err_blkno;
17982                 }
17983         } else {
17984                 /*
17985                  * Without the es_valid bit set (for fixed format) or an
17986                  * information descriptor (for descriptor format) we cannot
17987                  * be certain of the error blkno, so just use the
17988                  * request_blkno.
17989                  */
17990                 err_blkno = (diskaddr_t)request_blkno;
17991         }
17992 
17993         /*
17994          * The following will log the buffer contents for the release driver
17995          * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error
17996          * level is set to verbose.
17997          */
17998         sd_dump_memory(un, SD_LOG_IO, "Failed CDB",
17999             (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
18000         sd_dump_memory(un, SD_LOG_IO, "Sense Data",
18001             (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX);
18002 
18003         if (pfa_flag == FALSE) {
18004                 /* This is normally only set for USCSI */
18005                 if ((pktp->pkt_flags & FLAG_SILENT) != 0) {
18006                         return;
18007                 }
18008 
18009                 if ((SD_IS_BUFIO(xp) == TRUE) &&
18010                     (((sd_level_mask & SD_LOGMASK_DIAG) == 0) &&
18011                     (severity < sd_error_level))) {
18012                         return;
18013                 }
18014         }
18015         /*
18016          * Check for Sonoma Failover and keep a count of how many failed I/O's
18017          */
18018         if ((SD_IS_LSI(un)) &&
18019             (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) &&
18020             (scsi_sense_asc(sensep) == 0x94) &&
18021             (scsi_sense_ascq(sensep) == 0x01)) {
18022                 un->un_sonoma_failure_count++;
18023                 if (un->un_sonoma_failure_count > 1) {
18024                         return;
18025                 }
18026         }
18027 
18028         if (SD_FM_LOG(un) == SD_FM_LOG_NSUP ||
18029             ((scsi_sense_key(sensep) == KEY_RECOVERABLE_ERROR) &&
18030             (pktp->pkt_resid == 0))) {
18031                 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity,
18032                     request_blkno, err_blkno, scsi_cmds,
18033                     (struct scsi_extended_sense *)sensep,
18034                     un->un_additional_codes, NULL);
18035         }
18036 }
18037 
18038 /*
18039  *    Function: sd_sense_key_no_sense
18040  *
18041  * Description: Recovery action when sense data was not received.
18042  *
18043  *     Context: May be called from interrupt context
18044  */
18045 
18046 static void
18047 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18048     struct scsi_pkt *pktp)
18049 {
18050         struct sd_sense_info    si;
18051 
18052         ASSERT(un != NULL);
18053         ASSERT(mutex_owned(SD_MUTEX(un)));
18054         ASSERT(bp != NULL);
18055         ASSERT(xp != NULL);
18056         ASSERT(pktp != NULL);
18057 
18058         si.ssi_severity = SCSI_ERR_FATAL;
18059         si.ssi_pfa_flag = FALSE;
18060 
18061         SD_UPDATE_ERRSTATS(un, sd_softerrs);
18062 
18063         sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18064             &si, EIO, (clock_t)0, NULL);
18065 }
18066 
18067 
18068 /*
18069  *    Function: sd_sense_key_recoverable_error
18070  *
18071  * Description: Recovery actions for a SCSI "Recovered Error" sense key.
18072  *
18073  *     Context: May be called from interrupt context
18074  */
18075 
18076 static void
18077 sd_sense_key_recoverable_error(struct sd_lun *un, uint8_t *sense_datap,
18078     struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18079 {
18080         struct sd_sense_info    si;
18081         uint8_t asc = scsi_sense_asc(sense_datap);
18082         uint8_t ascq = scsi_sense_ascq(sense_datap);
18083 
18084         ASSERT(un != NULL);
18085         ASSERT(mutex_owned(SD_MUTEX(un)));
18086         ASSERT(bp != NULL);
18087         ASSERT(xp != NULL);
18088         ASSERT(pktp != NULL);
18089 
18090         /*
18091          * 0x00, 0x1D: ATA PASSTHROUGH INFORMATION AVAILABLE
18092          */
18093         if (asc == 0x00 && ascq == 0x1D) {
18094                 sd_return_command(un, bp);
18095                 return;
18096         }
18097 
18098         /*
18099          * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED
18100          */
18101         if ((asc == 0x5D) && (sd_report_pfa != 0)) {
18102                 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18103                 si.ssi_severity = SCSI_ERR_INFO;
18104                 si.ssi_pfa_flag = TRUE;
18105         } else {
18106                 SD_UPDATE_ERRSTATS(un, sd_softerrs);
18107                 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err);
18108                 si.ssi_severity = SCSI_ERR_RECOVERED;
18109                 si.ssi_pfa_flag = FALSE;
18110         }
18111 
18112         if (pktp->pkt_resid == 0) {
18113                 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18114                 sd_return_command(un, bp);
18115                 return;
18116         }
18117 
18118         sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18119             &si, EIO, (clock_t)0, NULL);
18120 }
18121 
18122 
18123 
18124 
18125 /*
18126  *    Function: sd_sense_key_not_ready
18127  *
18128  * Description: Recovery actions for a SCSI "Not Ready" sense key.
18129  *
18130  *     Context: May be called from interrupt context
18131  */
18132 
18133 static void
18134 sd_sense_key_not_ready(struct sd_lun *un, uint8_t *sense_datap, struct buf *bp,
18135     struct sd_xbuf *xp, struct scsi_pkt *pktp)
18136 {
18137         struct sd_sense_info    si;
18138         uint8_t asc = scsi_sense_asc(sense_datap);
18139         uint8_t ascq = scsi_sense_ascq(sense_datap);
18140 
18141         ASSERT(un != NULL);
18142         ASSERT(mutex_owned(SD_MUTEX(un)));
18143         ASSERT(bp != NULL);
18144         ASSERT(xp != NULL);
18145         ASSERT(pktp != NULL);
18146 
18147         si.ssi_severity = SCSI_ERR_FATAL;
18148         si.ssi_pfa_flag = FALSE;
18149 
18150         /*
18151          * Update error stats after first NOT READY error. Disks may have
18152          * been powered down and may need to be restarted.  For CDROMs,
18153          * report NOT READY errors only if media is present.
18154          */
18155         if ((ISCD(un) && (asc == 0x3A)) ||
18156             (xp->xb_nr_retry_count > 0)) {
18157                 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18158                 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err);
18159         }
18160 
18161         /*
18162          * Just fail if the "not ready" retry limit has been reached.
18163          */
18164         if (xp->xb_nr_retry_count >= un->un_notready_retry_count) {
18165                 /* Special check for error message printing for removables. */
18166                 if (un->un_f_has_removable_media && (asc == 0x04) &&
18167                     (ascq >= 0x04)) {
18168                         si.ssi_severity = SCSI_ERR_ALL;
18169                 }
18170                 goto fail_command;
18171         }
18172 
18173         /*
18174          * Check the ASC and ASCQ in the sense data as needed, to determine
18175          * what to do.
18176          */
18177         switch (asc) {
18178         case 0x04:      /* LOGICAL UNIT NOT READY */
18179                 /*
18180                  * disk drives that don't spin up result in a very long delay
18181                  * in format without warning messages. We will log a message
18182                  * if the error level is set to verbose.
18183                  */
18184                 if (sd_error_level < SCSI_ERR_RETRYABLE) {
18185                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18186                             "logical unit not ready, resetting disk\n");
18187                 }
18188 
18189                 /*
18190                  * There are different requirements for CDROMs and disks for
18191                  * the number of retries.  If a CD-ROM is giving this, it is
18192                  * probably reading TOC and is in the process of getting
18193                  * ready, so we should keep on trying for a long time to make
18194                  * sure that all types of media are taken in account (for
18195                  * some media the drive takes a long time to read TOC).  For
18196                  * disks we do not want to retry this too many times as this
18197                  * can cause a long hang in format when the drive refuses to
18198                  * spin up (a very common failure).
18199                  */
18200                 switch (ascq) {
18201                 case 0x00:  /* LUN NOT READY, CAUSE NOT REPORTABLE */
18202                         /*
18203                          * Disk drives frequently refuse to spin up which
18204                          * results in a very long hang in format without
18205                          * warning messages.
18206                          *
18207                          * Note: This code preserves the legacy behavior of
18208                          * comparing xb_nr_retry_count against zero for fibre
18209                          * channel targets instead of comparing against the
18210                          * un_reset_retry_count value.  The reason for this
18211                          * discrepancy has been so utterly lost beneath the
18212                          * Sands of Time that even Indiana Jones could not
18213                          * find it.
18214                          */
18215                         if (un->un_f_is_fibre == TRUE) {
18216                                 if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18217                                     (xp->xb_nr_retry_count > 0)) &&
18218                                     (un->un_startstop_timeid == NULL)) {
18219                                         scsi_log(SD_DEVINFO(un), sd_label,
18220                                             CE_WARN, "logical unit not ready, "
18221                                             "resetting disk\n");
18222                                         sd_reset_target(un, pktp);
18223                                 }
18224                         } else {
18225                                 if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18226                                     (xp->xb_nr_retry_count >
18227                                     un->un_reset_retry_count)) &&
18228                                     (un->un_startstop_timeid == NULL)) {
18229                                         scsi_log(SD_DEVINFO(un), sd_label,
18230                                             CE_WARN, "logical unit not ready, "
18231                                             "resetting disk\n");
18232                                         sd_reset_target(un, pktp);
18233                                 }
18234                         }
18235                         break;
18236 
18237                 case 0x01:  /* LUN IS IN PROCESS OF BECOMING READY */
18238                         /*
18239                          * If the target is in the process of becoming
18240                          * ready, just proceed with the retry. This can
18241                          * happen with CD-ROMs that take a long time to
18242                          * read TOC after a power cycle or reset.
18243                          */
18244                         goto do_retry;
18245 
18246                 case 0x02:  /* LUN NOT READY, INITITIALIZING CMD REQUIRED */
18247                         break;
18248 
18249                 case 0x03:  /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */
18250                         /*
18251                          * Retries cannot help here so just fail right away.
18252                          */
18253                         goto fail_command;
18254 
18255                 case 0x88:
18256                         /*
18257                          * Vendor-unique code for T3/T4: it indicates a
18258                          * path problem in a mutipathed config, but as far as
18259                          * the target driver is concerned it equates to a fatal
18260                          * error, so we should just fail the command right away
18261                          * (without printing anything to the console). If this
18262                          * is not a T3/T4, fall thru to the default recovery
18263                          * action.
18264                          * T3/T4 is FC only, don't need to check is_fibre
18265                          */
18266                         if (SD_IS_T3(un) || SD_IS_T4(un)) {
18267                                 sd_return_failed_command(un, bp, EIO);
18268                                 return;
18269                         }
18270                         /* FALLTHRU */
18271 
18272                 case 0x04:  /* LUN NOT READY, FORMAT IN PROGRESS */
18273                 case 0x05:  /* LUN NOT READY, REBUILD IN PROGRESS */
18274                 case 0x06:  /* LUN NOT READY, RECALCULATION IN PROGRESS */
18275                 case 0x07:  /* LUN NOT READY, OPERATION IN PROGRESS */
18276                 case 0x08:  /* LUN NOT READY, LONG WRITE IN PROGRESS */
18277                 default:    /* Possible future codes in SCSI spec? */
18278                         /*
18279                          * For removable-media devices, do not retry if
18280                          * ASCQ > 2 as these result mostly from USCSI commands
18281                          * on MMC devices issued to check status of an
18282                          * operation initiated in immediate mode.  Also for
18283                          * ASCQ >= 4 do not print console messages as these
18284                          * mainly represent a user-initiated operation
18285                          * instead of a system failure.
18286                          */
18287                         if (un->un_f_has_removable_media) {
18288                                 si.ssi_severity = SCSI_ERR_ALL;
18289                                 goto fail_command;
18290                         }
18291                         break;
18292                 }
18293 
18294                 /*
18295                  * As part of our recovery attempt for the NOT READY
18296                  * condition, we issue a START STOP UNIT command. However
18297                  * we want to wait for a short delay before attempting this
18298                  * as there may still be more commands coming back from the
18299                  * target with the check condition. To do this we use
18300                  * timeout(9F) to call sd_start_stop_unit_callback() after
18301                  * the delay interval expires. (sd_start_stop_unit_callback()
18302                  * dispatches sd_start_stop_unit_task(), which will issue
18303                  * the actual START STOP UNIT command. The delay interval
18304                  * is one-half of the delay that we will use to retry the
18305                  * command that generated the NOT READY condition.
18306                  *
18307                  * Note that we could just dispatch sd_start_stop_unit_task()
18308                  * from here and allow it to sleep for the delay interval,
18309                  * but then we would be tying up the taskq thread
18310                  * uncesessarily for the duration of the delay.
18311                  *
18312                  * Do not issue the START STOP UNIT if the current command
18313                  * is already a START STOP UNIT.
18314                  */
18315                 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) {
18316                         break;
18317                 }
18318 
18319                 /*
18320                  * Do not schedule the timeout if one is already pending.
18321                  */
18322                 if (un->un_startstop_timeid != NULL) {
18323                         SD_INFO(SD_LOG_ERROR, un,
18324                             "sd_sense_key_not_ready: restart already issued to"
18325                             " %s%d\n", ddi_driver_name(SD_DEVINFO(un)),
18326                             ddi_get_instance(SD_DEVINFO(un)));
18327                         break;
18328                 }
18329 
18330                 /*
18331                  * Schedule the START STOP UNIT command, then queue the command
18332                  * for a retry.
18333                  *
18334                  * Note: A timeout is not scheduled for this retry because we
18335                  * want the retry to be serial with the START_STOP_UNIT. The
18336                  * retry will be started when the START_STOP_UNIT is completed
18337                  * in sd_start_stop_unit_task.
18338                  */
18339                 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback,
18340                     un, un->un_busy_timeout / 2);
18341                 xp->xb_nr_retry_count++;
18342                 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter);
18343                 return;
18344 
18345         case 0x05:      /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */
18346                 if (sd_error_level < SCSI_ERR_RETRYABLE) {
18347                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18348                             "unit does not respond to selection\n");
18349                 }
18350                 break;
18351 
18352         case 0x3A:      /* MEDIUM NOT PRESENT */
18353                 if (sd_error_level >= SCSI_ERR_FATAL) {
18354                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18355                             "Caddy not inserted in drive\n");
18356                 }
18357 
18358                 sr_ejected(un);
18359                 un->un_mediastate = DKIO_EJECTED;
18360                 /* The state has changed, inform the media watch routines */
18361                 cv_broadcast(&un->un_state_cv);
18362                 /* Just fail if no media is present in the drive. */
18363                 goto fail_command;
18364 
18365         default:
18366                 if (sd_error_level < SCSI_ERR_RETRYABLE) {
18367                         scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
18368                             "Unit not Ready. Additional sense code 0x%x\n",
18369                             asc);
18370                 }
18371                 break;
18372         }
18373 
18374 do_retry:
18375 
18376         /*
18377          * Retry the command, as some targets may report NOT READY for
18378          * several seconds after being reset.
18379          */
18380         xp->xb_nr_retry_count++;
18381         si.ssi_severity = SCSI_ERR_RETRYABLE;
18382         sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
18383             &si, EIO, un->un_busy_timeout, NULL);
18384 
18385         return;
18386 
18387 fail_command:
18388         sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18389         sd_return_failed_command(un, bp, EIO);
18390 }
18391 
18392 
18393 
18394 /*
18395  *    Function: sd_sense_key_medium_or_hardware_error
18396  *
18397  * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error"
18398  *              sense key.
18399  *
18400  *     Context: May be called from interrupt context
18401  */
18402 
18403 static void
18404 sd_sense_key_medium_or_hardware_error(struct sd_lun *un, uint8_t *sense_datap,
18405     struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18406 {
18407         struct sd_sense_info    si;
18408         uint8_t sense_key = scsi_sense_key(sense_datap);
18409         uint8_t asc = scsi_sense_asc(sense_datap);
18410 
18411         ASSERT(un != NULL);
18412         ASSERT(mutex_owned(SD_MUTEX(un)));
18413         ASSERT(bp != NULL);
18414         ASSERT(xp != NULL);
18415         ASSERT(pktp != NULL);
18416 
18417         si.ssi_severity = SCSI_ERR_FATAL;
18418         si.ssi_pfa_flag = FALSE;
18419 
18420         if (sense_key == KEY_MEDIUM_ERROR) {
18421                 SD_UPDATE_ERRSTATS(un, sd_rq_media_err);
18422         }
18423 
18424         SD_UPDATE_ERRSTATS(un, sd_harderrs);
18425 
18426         if ((un->un_reset_retry_count != 0) &&
18427             (xp->xb_retry_count == un->un_reset_retry_count)) {
18428                 mutex_exit(SD_MUTEX(un));
18429                 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */
18430                 if (un->un_f_allow_bus_device_reset == TRUE) {
18431 
18432                         boolean_t try_resetting_target = B_TRUE;
18433 
18434                         /*
18435                          * We need to be able to handle specific ASC when we are
18436                          * handling a KEY_HARDWARE_ERROR. In particular
18437                          * taking the default action of resetting the target may
18438                          * not be the appropriate way to attempt recovery.
18439                          * Resetting a target because of a single LUN failure
18440                          * victimizes all LUNs on that target.
18441                          *
18442                          * This is true for the LSI arrays, if an LSI
18443                          * array controller returns an ASC of 0x84 (LUN Dead) we
18444                          * should trust it.
18445                          */
18446 
18447                         if (sense_key == KEY_HARDWARE_ERROR) {
18448                                 switch (asc) {
18449                                 case 0x84:
18450                                         if (SD_IS_LSI(un)) {
18451                                                 try_resetting_target = B_FALSE;
18452                                         }
18453                                         break;
18454                                 default:
18455                                         break;
18456                                 }
18457                         }
18458 
18459                         if (try_resetting_target == B_TRUE) {
18460                                 int reset_retval = 0;
18461                                 if (un->un_f_lun_reset_enabled == TRUE) {
18462                                         SD_TRACE(SD_LOG_IO_CORE, un,
18463                                             "sd_sense_key_medium_or_hardware_"
18464                                             "error: issuing RESET_LUN\n");
18465                                         reset_retval =
18466                                             scsi_reset(SD_ADDRESS(un),
18467                                             RESET_LUN);
18468                                 }
18469                                 if (reset_retval == 0) {
18470                                         SD_TRACE(SD_LOG_IO_CORE, un,
18471                                             "sd_sense_key_medium_or_hardware_"
18472                                             "error: issuing RESET_TARGET\n");
18473                                         (void) scsi_reset(SD_ADDRESS(un),
18474                                             RESET_TARGET);
18475                                 }
18476                         }
18477                 }
18478                 mutex_enter(SD_MUTEX(un));
18479         }
18480 
18481         /*
18482          * This really ought to be a fatal error, but we will retry anyway
18483          * as some drives report this as a spurious error.
18484          */
18485         sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18486             &si, EIO, (clock_t)0, NULL);
18487 }
18488 
18489 
18490 
18491 /*
18492  *    Function: sd_sense_key_illegal_request
18493  *
18494  * Description: Recovery actions for a SCSI "Illegal Request" sense key.
18495  *
18496  *     Context: May be called from interrupt context
18497  */
18498 
18499 static void
18500 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
18501     struct sd_xbuf *xp, struct scsi_pkt *pktp)
18502 {
18503         struct sd_sense_info    si;
18504 
18505         ASSERT(un != NULL);
18506         ASSERT(mutex_owned(SD_MUTEX(un)));
18507         ASSERT(bp != NULL);
18508         ASSERT(xp != NULL);
18509         ASSERT(pktp != NULL);
18510 
18511         SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err);
18512 
18513         si.ssi_severity = SCSI_ERR_INFO;
18514         si.ssi_pfa_flag = FALSE;
18515 
18516         /* Pointless to retry if the target thinks it's an illegal request */
18517         sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18518         sd_return_failed_command(un, bp, EIO);
18519 }
18520 
18521 
18522 
18523 
18524 /*
18525  *    Function: sd_sense_key_unit_attention
18526  *
18527  * Description: Recovery actions for a SCSI "Unit Attention" sense key.
18528  *
18529  *     Context: May be called from interrupt context
18530  */
18531 
18532 static void
18533 sd_sense_key_unit_attention(struct sd_lun *un,  uint8_t *sense_datap,
18534     struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18535 {
18536         /*
18537          * For UNIT ATTENTION we allow retries for one minute. Devices
18538          * like Sonoma can return UNIT ATTENTION close to a minute
18539          * under certain conditions.
18540          */
18541         int     retry_check_flag = SD_RETRIES_UA;
18542         boolean_t       kstat_updated = B_FALSE;
18543         struct  sd_sense_info           si;
18544         uint8_t asc = scsi_sense_asc(sense_datap);
18545         uint8_t ascq = scsi_sense_ascq(sense_datap);
18546 
18547         ASSERT(un != NULL);
18548         ASSERT(mutex_owned(SD_MUTEX(un)));
18549         ASSERT(bp != NULL);
18550         ASSERT(xp != NULL);
18551         ASSERT(pktp != NULL);
18552 
18553         si.ssi_severity = SCSI_ERR_INFO;
18554         si.ssi_pfa_flag = FALSE;
18555 
18556 
18557         switch (asc) {
18558         case 0x5D:  /* FAILURE PREDICTION THRESHOLD EXCEEDED */
18559                 if (sd_report_pfa != 0) {
18560                         SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18561                         si.ssi_pfa_flag = TRUE;
18562                         retry_check_flag = SD_RETRIES_STANDARD;
18563                         goto do_retry;
18564                 }
18565 
18566                 break;
18567 
18568         case 0x29:  /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
18569                 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
18570                         un->un_resvd_status |=
18571                             (SD_LOST_RESERVE | SD_WANT_RESERVE);
18572                 }
18573 #ifdef _LP64
18574                 if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) {
18575                         if (taskq_dispatch(sd_tq, sd_reenable_dsense_task,
18576                             un, KM_NOSLEEP) == 0) {
18577                                 /*
18578                                  * If we can't dispatch the task we'll just
18579                                  * live without descriptor sense.  We can
18580                                  * try again on the next "unit attention"
18581                                  */
18582                                 SD_ERROR(SD_LOG_ERROR, un,
18583                                     "sd_sense_key_unit_attention: "
18584                                     "Could not dispatch "
18585                                     "sd_reenable_dsense_task\n");
18586                         }
18587                 }
18588 #endif /* _LP64 */
18589                 /* FALLTHRU */
18590 
18591         case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
18592                 if (!un->un_f_has_removable_media) {
18593                         break;
18594                 }
18595 
18596                 /*
18597                  * When we get a unit attention from a removable-media device,
18598                  * it may be in a state that will take a long time to recover
18599                  * (e.g., from a reset).  Since we are executing in interrupt
18600                  * context here, we cannot wait around for the device to come
18601                  * back. So hand this command off to sd_media_change_task()
18602                  * for deferred processing under taskq thread context. (Note
18603                  * that the command still may be failed if a problem is
18604                  * encountered at a later time.)
18605                  */
18606                 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp,
18607                     KM_NOSLEEP) == 0) {
18608                         /*
18609                          * Cannot dispatch the request so fail the command.
18610                          */
18611                         SD_UPDATE_ERRSTATS(un, sd_harderrs);
18612                         SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18613                         si.ssi_severity = SCSI_ERR_FATAL;
18614                         sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18615                         sd_return_failed_command(un, bp, EIO);
18616                 }
18617 
18618                 /*
18619                  * If failed to dispatch sd_media_change_task(), we already
18620                  * updated kstat. If succeed to dispatch sd_media_change_task(),
18621                  * we should update kstat later if it encounters an error. So,
18622                  * we update kstat_updated flag here.
18623                  */
18624                 kstat_updated = B_TRUE;
18625 
18626                 /*
18627                  * Either the command has been successfully dispatched to a
18628                  * task Q for retrying, or the dispatch failed. In either case
18629                  * do NOT retry again by calling sd_retry_command. This sets up
18630                  * two retries of the same command and when one completes and
18631                  * frees the resources the other will access freed memory,
18632                  * a bad thing.
18633                  */
18634                 return;
18635 
18636         default:
18637                 break;
18638         }
18639 
18640         /*
18641          * ASC  ASCQ
18642          *  2A   09     Capacity data has changed
18643          *  2A   01     Mode parameters changed
18644          *  3F   0E     Reported luns data has changed
18645          * Arrays that support logical unit expansion should report
18646          * capacity changes(2Ah/09). Mode parameters changed and
18647          * reported luns data has changed are the approximation.
18648          */
18649         if (((asc == 0x2a) && (ascq == 0x09)) ||
18650             ((asc == 0x2a) && (ascq == 0x01)) ||
18651             ((asc == 0x3f) && (ascq == 0x0e))) {
18652                 if (taskq_dispatch(sd_tq, sd_target_change_task, un,
18653                     KM_NOSLEEP) == 0) {
18654                         SD_ERROR(SD_LOG_ERROR, un,
18655                             "sd_sense_key_unit_attention: "
18656                             "Could not dispatch sd_target_change_task\n");
18657                 }
18658         }
18659 
18660         /*
18661          * Update kstat if we haven't done that.
18662          */
18663         if (!kstat_updated) {
18664                 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18665                 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18666         }
18667 
18668 do_retry:
18669         sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si,
18670             EIO, SD_UA_RETRY_DELAY, NULL);
18671 }
18672 
18673 
18674 
18675 /*
18676  *    Function: sd_sense_key_fail_command
18677  *
18678  * Description: Use to fail a command when we don't like the sense key that
18679  *              was returned.
18680  *
18681  *     Context: May be called from interrupt context
18682  */
18683 
18684 static void
18685 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18686     struct scsi_pkt *pktp)
18687 {
18688         struct sd_sense_info    si;
18689 
18690         ASSERT(un != NULL);
18691         ASSERT(mutex_owned(SD_MUTEX(un)));
18692         ASSERT(bp != NULL);
18693         ASSERT(xp != NULL);
18694         ASSERT(pktp != NULL);
18695 
18696         si.ssi_severity = SCSI_ERR_FATAL;
18697         si.ssi_pfa_flag = FALSE;
18698 
18699         sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18700         sd_return_failed_command(un, bp, EIO);
18701 }
18702 
18703 
18704 
18705 /*
18706  *    Function: sd_sense_key_blank_check
18707  *
18708  * Description: Recovery actions for a SCSI "Blank Check" sense key.
18709  *              Has no monetary connotation.
18710  *
18711  *     Context: May be called from interrupt context
18712  */
18713 
18714 static void
18715 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18716     struct scsi_pkt *pktp)
18717 {
18718         struct sd_sense_info    si;
18719 
18720         ASSERT(un != NULL);
18721         ASSERT(mutex_owned(SD_MUTEX(un)));
18722         ASSERT(bp != NULL);
18723         ASSERT(xp != NULL);
18724         ASSERT(pktp != NULL);
18725 
18726         /*
18727          * Blank check is not fatal for removable devices, therefore
18728          * it does not require a console message.
18729          */
18730         si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL :
18731             SCSI_ERR_FATAL;
18732         si.ssi_pfa_flag = FALSE;
18733 
18734         sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18735         sd_return_failed_command(un, bp, EIO);
18736 }
18737 
18738 
18739 
18740 
18741 /*
18742  *    Function: sd_sense_key_aborted_command
18743  *
18744  * Description: Recovery actions for a SCSI "Aborted Command" sense key.
18745  *
18746  *     Context: May be called from interrupt context
18747  */
18748 
18749 static void
18750 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
18751     struct sd_xbuf *xp, struct scsi_pkt *pktp)
18752 {
18753         struct sd_sense_info    si;
18754 
18755         ASSERT(un != NULL);
18756         ASSERT(mutex_owned(SD_MUTEX(un)));
18757         ASSERT(bp != NULL);
18758         ASSERT(xp != NULL);
18759         ASSERT(pktp != NULL);
18760 
18761         si.ssi_severity = SCSI_ERR_FATAL;
18762         si.ssi_pfa_flag = FALSE;
18763 
18764         SD_UPDATE_ERRSTATS(un, sd_harderrs);
18765 
18766         /*
18767          * This really ought to be a fatal error, but we will retry anyway
18768          * as some drives report this as a spurious error.
18769          */
18770         sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18771             &si, EIO, drv_usectohz(100000), NULL);
18772 }
18773 
18774 
18775 
18776 /*
18777  *    Function: sd_sense_key_default
18778  *
18779  * Description: Default recovery action for several SCSI sense keys (basically
18780  *              attempts a retry).
18781  *
18782  *     Context: May be called from interrupt context
18783  */
18784 
18785 static void
18786 sd_sense_key_default(struct sd_lun *un, uint8_t *sense_datap, struct buf *bp,
18787     struct sd_xbuf *xp, struct scsi_pkt *pktp)
18788 {
18789         struct sd_sense_info    si;
18790         uint8_t sense_key = scsi_sense_key(sense_datap);
18791 
18792         ASSERT(un != NULL);
18793         ASSERT(mutex_owned(SD_MUTEX(un)));
18794         ASSERT(bp != NULL);
18795         ASSERT(xp != NULL);
18796         ASSERT(pktp != NULL);
18797 
18798         SD_UPDATE_ERRSTATS(un, sd_harderrs);
18799 
18800         /*
18801          * Undecoded sense key. Attempt retries and hope that will fix
18802          * the problem.  Otherwise, we're dead.
18803          */
18804         if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
18805                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18806                     "Unhandled Sense Key '%s'\n", sense_keys[sense_key]);
18807         }
18808 
18809         si.ssi_severity = SCSI_ERR_FATAL;
18810         si.ssi_pfa_flag = FALSE;
18811 
18812         sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18813             &si, EIO, (clock_t)0, NULL);
18814 }
18815 
18816 
18817 
18818 /*
18819  *    Function: sd_print_retry_msg
18820  *
18821  * Description: Print a message indicating the retry action being taken.
18822  *
18823  *   Arguments: un - ptr to associated softstate
18824  *              bp - ptr to buf(9S) for the command
18825  *              arg - not used.
18826  *              flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18827  *                      or SD_NO_RETRY_ISSUED
18828  *
18829  *     Context: May be called from interrupt context
18830  */
18831 /* ARGSUSED */
18832 static void
18833 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag)
18834 {
18835         struct sd_xbuf  *xp;
18836         struct scsi_pkt *pktp;
18837         char *reasonp;
18838         char *msgp;
18839 
18840         ASSERT(un != NULL);
18841         ASSERT(mutex_owned(SD_MUTEX(un)));
18842         ASSERT(bp != NULL);
18843         pktp = SD_GET_PKTP(bp);
18844         ASSERT(pktp != NULL);
18845         xp = SD_GET_XBUF(bp);
18846         ASSERT(xp != NULL);
18847 
18848         ASSERT(!mutex_owned(&un->un_pm_mutex));
18849         mutex_enter(&un->un_pm_mutex);
18850         if ((un->un_state == SD_STATE_SUSPENDED) ||
18851             (SD_DEVICE_IS_IN_LOW_POWER(un)) ||
18852             (pktp->pkt_flags & FLAG_SILENT)) {
18853                 mutex_exit(&un->un_pm_mutex);
18854                 goto update_pkt_reason;
18855         }
18856         mutex_exit(&un->un_pm_mutex);
18857 
18858         /*
18859          * Suppress messages if they are all the same pkt_reason; with
18860          * TQ, many (up to 256) are returned with the same pkt_reason.
18861          * If we are in panic, then suppress the retry messages.
18862          */
18863         switch (flag) {
18864         case SD_NO_RETRY_ISSUED:
18865                 msgp = "giving up";
18866                 break;
18867         case SD_IMMEDIATE_RETRY_ISSUED:
18868         case SD_DELAYED_RETRY_ISSUED:
18869                 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) ||
18870                     ((pktp->pkt_reason == un->un_last_pkt_reason) &&
18871                     (sd_error_level != SCSI_ERR_ALL))) {
18872                         return;
18873                 }
18874                 msgp = "retrying command";
18875                 break;
18876         default:
18877                 goto update_pkt_reason;
18878         }
18879 
18880         reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" :
18881             scsi_rname(pktp->pkt_reason));
18882 
18883         if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
18884                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18885                     "SCSI transport failed: reason '%s': %s\n", reasonp, msgp);
18886         }
18887 
18888 update_pkt_reason:
18889         /*
18890          * Update un->un_last_pkt_reason with the value in pktp->pkt_reason.
18891          * This is to prevent multiple console messages for the same failure
18892          * condition.  Note that un->un_last_pkt_reason is NOT restored if &
18893          * when the command is retried successfully because there still may be
18894          * more commands coming back with the same value of pktp->pkt_reason.
18895          */
18896         if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) {
18897                 un->un_last_pkt_reason = pktp->pkt_reason;
18898         }
18899 }
18900 
18901 
18902 /*
18903  *    Function: sd_print_cmd_incomplete_msg
18904  *
18905  * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason.
18906  *
18907  *   Arguments: un - ptr to associated softstate
18908  *              bp - ptr to buf(9S) for the command
18909  *              arg - passed to sd_print_retry_msg()
18910  *              code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18911  *                      or SD_NO_RETRY_ISSUED
18912  *
18913  *     Context: May be called from interrupt context
18914  */
18915 
18916 static void
18917 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg,
18918     int code)
18919 {
18920         dev_info_t      *dip;
18921 
18922         ASSERT(un != NULL);
18923         ASSERT(mutex_owned(SD_MUTEX(un)));
18924         ASSERT(bp != NULL);
18925 
18926         switch (code) {
18927         case SD_NO_RETRY_ISSUED:
18928                 /* Command was failed. Someone turned off this target? */
18929                 if (un->un_state != SD_STATE_OFFLINE) {
18930                         /*
18931                          * Suppress message if we are detaching and
18932                          * device has been disconnected
18933                          * Note that DEVI_IS_DEVICE_REMOVED is a consolidation
18934                          * private interface and not part of the DDI
18935                          */
18936                         dip = un->un_sd->sd_dev;
18937                         if (!(DEVI_IS_DETACHING(dip) &&
18938                             DEVI_IS_DEVICE_REMOVED(dip))) {
18939                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18940                                 "disk not responding to selection\n");
18941                         }
18942                         New_state(un, SD_STATE_OFFLINE);
18943                 }
18944                 break;
18945 
18946         case SD_DELAYED_RETRY_ISSUED:
18947         case SD_IMMEDIATE_RETRY_ISSUED:
18948         default:
18949                 /* Command was successfully queued for retry */
18950                 sd_print_retry_msg(un, bp, arg, code);
18951                 break;
18952         }
18953 }
18954 
18955 
18956 /*
18957  *    Function: sd_pkt_reason_cmd_incomplete
18958  *
18959  * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason.
18960  *
18961  *     Context: May be called from interrupt context
18962  */
18963 
18964 static void
18965 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
18966     struct sd_xbuf *xp, struct scsi_pkt *pktp)
18967 {
18968         int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE;
18969 
18970         ASSERT(un != NULL);
18971         ASSERT(mutex_owned(SD_MUTEX(un)));
18972         ASSERT(bp != NULL);
18973         ASSERT(xp != NULL);
18974         ASSERT(pktp != NULL);
18975 
18976         /* Do not do a reset if selection did not complete */
18977         /* Note: Should this not just check the bit? */
18978         if (pktp->pkt_state != STATE_GOT_BUS) {
18979                 SD_UPDATE_ERRSTATS(un, sd_transerrs);
18980                 sd_reset_target(un, pktp);
18981         }
18982 
18983         /*
18984          * If the target was not successfully selected, then set
18985          * SD_RETRIES_FAILFAST to indicate that we lost communication
18986          * with the target, and further retries and/or commands are
18987          * likely to take a long time.
18988          */
18989         if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) {
18990                 flag |= SD_RETRIES_FAILFAST;
18991         }
18992 
18993         SD_UPDATE_RESERVATION_STATUS(un, pktp);
18994 
18995         sd_retry_command(un, bp, flag,
18996             sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18997 }
18998 
18999 
19000 
19001 /*
19002  *    Function: sd_pkt_reason_cmd_tran_err
19003  *
19004  * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason.
19005  *
19006  *     Context: May be called from interrupt context
19007  */
19008 
19009 static void
19010 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
19011     struct sd_xbuf *xp, struct scsi_pkt *pktp)
19012 {
19013         ASSERT(un != NULL);
19014         ASSERT(mutex_owned(SD_MUTEX(un)));
19015         ASSERT(bp != NULL);
19016         ASSERT(xp != NULL);
19017         ASSERT(pktp != NULL);
19018 
19019         /*
19020          * Do not reset if we got a parity error, or if
19021          * selection did not complete.
19022          */
19023         SD_UPDATE_ERRSTATS(un, sd_harderrs);
19024         /* Note: Should this not just check the bit for pkt_state? */
19025         if (((pktp->pkt_statistics & STAT_PERR) == 0) &&
19026             (pktp->pkt_state != STATE_GOT_BUS)) {
19027                 SD_UPDATE_ERRSTATS(un, sd_transerrs);
19028                 sd_reset_target(un, pktp);
19029         }
19030 
19031         SD_UPDATE_RESERVATION_STATUS(un, pktp);
19032 
19033         sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19034             sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19035 }
19036 
19037 
19038 
19039 /*
19040  *    Function: sd_pkt_reason_cmd_reset
19041  *
19042  * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason.
19043  *
19044  *     Context: May be called from interrupt context
19045  */
19046 
19047 static void
19048 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19049     struct scsi_pkt *pktp)
19050 {
19051         ASSERT(un != NULL);
19052         ASSERT(mutex_owned(SD_MUTEX(un)));
19053         ASSERT(bp != NULL);
19054         ASSERT(xp != NULL);
19055         ASSERT(pktp != NULL);
19056 
19057         /* The target may still be running the command, so try to reset. */
19058         SD_UPDATE_ERRSTATS(un, sd_transerrs);
19059         sd_reset_target(un, pktp);
19060 
19061         SD_UPDATE_RESERVATION_STATUS(un, pktp);
19062 
19063         /*
19064          * If pkt_reason is CMD_RESET chances are that this pkt got
19065          * reset because another target on this bus caused it. The target
19066          * that caused it should get CMD_TIMEOUT with pkt_statistics
19067          * of STAT_TIMEOUT/STAT_DEV_RESET.
19068          */
19069 
19070         sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
19071             sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19072 }
19073 
19074 
19075 
19076 
19077 /*
19078  *    Function: sd_pkt_reason_cmd_aborted
19079  *
19080  * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason.
19081  *
19082  *     Context: May be called from interrupt context
19083  */
19084 
19085 static void
19086 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19087     struct scsi_pkt *pktp)
19088 {
19089         ASSERT(un != NULL);
19090         ASSERT(mutex_owned(SD_MUTEX(un)));
19091         ASSERT(bp != NULL);
19092         ASSERT(xp != NULL);
19093         ASSERT(pktp != NULL);
19094 
19095         /* The target may still be running the command, so try to reset. */
19096         SD_UPDATE_ERRSTATS(un, sd_transerrs);
19097         sd_reset_target(un, pktp);
19098 
19099         SD_UPDATE_RESERVATION_STATUS(un, pktp);
19100 
19101         /*
19102          * If pkt_reason is CMD_ABORTED chances are that this pkt got
19103          * aborted because another target on this bus caused it. The target
19104          * that caused it should get CMD_TIMEOUT with pkt_statistics
19105          * of STAT_TIMEOUT/STAT_DEV_RESET.
19106          */
19107 
19108         sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
19109             sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19110 }
19111 
19112 
19113 
19114 /*
19115  *    Function: sd_pkt_reason_cmd_timeout
19116  *
19117  * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason.
19118  *
19119  *     Context: May be called from interrupt context
19120  */
19121 
19122 static void
19123 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19124     struct scsi_pkt *pktp)
19125 {
19126         ASSERT(un != NULL);
19127         ASSERT(mutex_owned(SD_MUTEX(un)));
19128         ASSERT(bp != NULL);
19129         ASSERT(xp != NULL);
19130         ASSERT(pktp != NULL);
19131 
19132 
19133         SD_UPDATE_ERRSTATS(un, sd_transerrs);
19134         sd_reset_target(un, pktp);
19135 
19136         SD_UPDATE_RESERVATION_STATUS(un, pktp);
19137 
19138         /*
19139          * A command timeout indicates that we could not establish
19140          * communication with the target, so set SD_RETRIES_FAILFAST
19141          * as further retries/commands are likely to take a long time.
19142          */
19143         sd_retry_command(un, bp,
19144             (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST),
19145             sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19146 }
19147 
19148 
19149 
19150 /*
19151  *    Function: sd_pkt_reason_cmd_unx_bus_free
19152  *
19153  * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason.
19154  *
19155  *     Context: May be called from interrupt context
19156  */
19157 
19158 static void
19159 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
19160     struct sd_xbuf *xp, struct scsi_pkt *pktp)
19161 {
19162         void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code);
19163 
19164         ASSERT(un != NULL);
19165         ASSERT(mutex_owned(SD_MUTEX(un)));
19166         ASSERT(bp != NULL);
19167         ASSERT(xp != NULL);
19168         ASSERT(pktp != NULL);
19169 
19170         SD_UPDATE_ERRSTATS(un, sd_harderrs);
19171         SD_UPDATE_RESERVATION_STATUS(un, pktp);
19172 
19173         funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ?
19174             sd_print_retry_msg : NULL;
19175 
19176         sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19177             funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19178 }
19179 
19180 
19181 /*
19182  *    Function: sd_pkt_reason_cmd_tag_reject
19183  *
19184  * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason.
19185  *
19186  *     Context: May be called from interrupt context
19187  */
19188 
19189 static void
19190 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
19191     struct sd_xbuf *xp, struct scsi_pkt *pktp)
19192 {
19193         ASSERT(un != NULL);
19194         ASSERT(mutex_owned(SD_MUTEX(un)));
19195         ASSERT(bp != NULL);
19196         ASSERT(xp != NULL);
19197         ASSERT(pktp != NULL);
19198 
19199         SD_UPDATE_ERRSTATS(un, sd_harderrs);
19200         pktp->pkt_flags = 0;
19201         un->un_tagflags = 0;
19202         if (un->un_f_opt_queueing == TRUE) {
19203                 un->un_throttle = min(un->un_throttle, 3);
19204         } else {
19205                 un->un_throttle = 1;
19206         }
19207         mutex_exit(SD_MUTEX(un));
19208         (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
19209         mutex_enter(SD_MUTEX(un));
19210 
19211         SD_UPDATE_RESERVATION_STATUS(un, pktp);
19212 
19213         /* Legacy behavior not to check retry counts here. */
19214         sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE),
19215             sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19216 }
19217 
19218 
19219 /*
19220  *    Function: sd_pkt_reason_default
19221  *
19222  * Description: Default recovery actions for SCSA pkt_reason values that
19223  *              do not have more explicit recovery actions.
19224  *
19225  *     Context: May be called from interrupt context
19226  */
19227 
19228 static void
19229 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19230     struct scsi_pkt *pktp)
19231 {
19232         ASSERT(un != NULL);
19233         ASSERT(mutex_owned(SD_MUTEX(un)));
19234         ASSERT(bp != NULL);
19235         ASSERT(xp != NULL);
19236         ASSERT(pktp != NULL);
19237 
19238         SD_UPDATE_ERRSTATS(un, sd_transerrs);
19239         sd_reset_target(un, pktp);
19240 
19241         SD_UPDATE_RESERVATION_STATUS(un, pktp);
19242 
19243         sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19244             sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19245 }
19246 
19247 
19248 
19249 /*
19250  *    Function: sd_pkt_status_check_condition
19251  *
19252  * Description: Recovery actions for a "STATUS_CHECK" SCSI command status.
19253  *
19254  *     Context: May be called from interrupt context
19255  */
19256 
19257 static void
19258 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
19259     struct sd_xbuf *xp, struct scsi_pkt *pktp)
19260 {
19261         ASSERT(un != NULL);
19262         ASSERT(mutex_owned(SD_MUTEX(un)));
19263         ASSERT(bp != NULL);
19264         ASSERT(xp != NULL);
19265         ASSERT(pktp != NULL);
19266 
19267         SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: "
19268             "entry: buf:0x%p xp:0x%p\n", bp, xp);
19269 
19270         /*
19271          * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the
19272          * command will be retried after the request sense). Otherwise, retry
19273          * the command. Note: we are issuing the request sense even though the
19274          * retry limit may have been reached for the failed command.
19275          */
19276         if (un->un_f_arq_enabled == FALSE) {
19277                 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19278                     "no ARQ, sending request sense command\n");
19279                 sd_send_request_sense_command(un, bp, pktp);
19280         } else {
19281                 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19282                     "ARQ,retrying request sense command\n");
19283 #if defined(__i386) || defined(__amd64)
19284                 /*
19285                  * The SD_RETRY_DELAY value need to be adjusted here
19286                  * when SD_RETRY_DELAY change in sddef.h
19287                  */
19288                 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19289                     un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0,
19290                     NULL);
19291 #else
19292                 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL,
19293                     EIO, SD_RETRY_DELAY, NULL);
19294 #endif
19295         }
19296 
19297         SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n");
19298 }
19299 
19300 
19301 /*
19302  *    Function: sd_pkt_status_busy
19303  *
19304  * Description: Recovery actions for a "STATUS_BUSY" SCSI command status.
19305  *
19306  *     Context: May be called from interrupt context
19307  */
19308 
19309 static void
19310 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19311     struct scsi_pkt *pktp)
19312 {
19313         ASSERT(un != NULL);
19314         ASSERT(mutex_owned(SD_MUTEX(un)));
19315         ASSERT(bp != NULL);
19316         ASSERT(xp != NULL);
19317         ASSERT(pktp != NULL);
19318 
19319         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19320             "sd_pkt_status_busy: entry\n");
19321 
19322         /* If retries are exhausted, just fail the command. */
19323         if (xp->xb_retry_count >= un->un_busy_retry_count) {
19324                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
19325                     "device busy too long\n");
19326                 sd_return_failed_command(un, bp, EIO);
19327                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19328                     "sd_pkt_status_busy: exit\n");
19329                 return;
19330         }
19331         xp->xb_retry_count++;
19332 
19333         /*
19334          * Try to reset the target. However, we do not want to perform
19335          * more than one reset if the device continues to fail. The reset
19336          * will be performed when the retry count reaches the reset
19337          * threshold.  This threshold should be set such that at least
19338          * one retry is issued before the reset is performed.
19339          */
19340         if (xp->xb_retry_count ==
19341             ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) {
19342                 int rval = 0;
19343                 mutex_exit(SD_MUTEX(un));
19344                 if (un->un_f_allow_bus_device_reset == TRUE) {
19345                         /*
19346                          * First try to reset the LUN; if we cannot then
19347                          * try to reset the target.
19348                          */
19349                         if (un->un_f_lun_reset_enabled == TRUE) {
19350                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19351                                     "sd_pkt_status_busy: RESET_LUN\n");
19352                                 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19353                         }
19354                         if (rval == 0) {
19355                                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19356                                     "sd_pkt_status_busy: RESET_TARGET\n");
19357                                 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19358                         }
19359                 }
19360                 if (rval == 0) {
19361                         /*
19362                          * If the RESET_LUN and/or RESET_TARGET failed,
19363                          * try RESET_ALL
19364                          */
19365                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19366                             "sd_pkt_status_busy: RESET_ALL\n");
19367                         rval = scsi_reset(SD_ADDRESS(un), RESET_ALL);
19368                 }
19369                 mutex_enter(SD_MUTEX(un));
19370                 if (rval == 0) {
19371                         /*
19372                          * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed.
19373                          * At this point we give up & fail the command.
19374                          */
19375                         sd_return_failed_command(un, bp, EIO);
19376                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19377                             "sd_pkt_status_busy: exit (failed cmd)\n");
19378                         return;
19379                 }
19380         }
19381 
19382         /*
19383          * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as
19384          * we have already checked the retry counts above.
19385          */
19386         sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL,
19387             EIO, un->un_busy_timeout, NULL);
19388 
19389         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19390             "sd_pkt_status_busy: exit\n");
19391 }
19392 
19393 
19394 /*
19395  *    Function: sd_pkt_status_reservation_conflict
19396  *
19397  * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI
19398  *              command status.
19399  *
19400  *     Context: May be called from interrupt context
19401  */
19402 
19403 static void
19404 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp,
19405     struct sd_xbuf *xp, struct scsi_pkt *pktp)
19406 {
19407         ASSERT(un != NULL);
19408         ASSERT(mutex_owned(SD_MUTEX(un)));
19409         ASSERT(bp != NULL);
19410         ASSERT(xp != NULL);
19411         ASSERT(pktp != NULL);
19412 
19413         /*
19414          * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation
19415          * conflict could be due to various reasons like incorrect keys, not
19416          * registered or not reserved etc. So, we return EACCES to the caller.
19417          */
19418         if (un->un_reservation_type == SD_SCSI3_RESERVATION) {
19419                 int cmd = SD_GET_PKT_OPCODE(pktp);
19420                 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) ||
19421                     (cmd == SCMD_PERSISTENT_RESERVE_OUT)) {
19422                         sd_return_failed_command(un, bp, EACCES);
19423                         return;
19424                 }
19425         }
19426 
19427         un->un_resvd_status |= SD_RESERVATION_CONFLICT;
19428 
19429         if ((un->un_resvd_status & SD_FAILFAST) != 0) {
19430                 if (sd_failfast_enable != 0) {
19431                         /* By definition, we must panic here.... */
19432                         sd_panic_for_res_conflict(un);
19433                         /*NOTREACHED*/
19434                 }
19435                 SD_ERROR(SD_LOG_IO, un,
19436                     "sd_handle_resv_conflict: Disk Reserved\n");
19437                 sd_return_failed_command(un, bp, EACCES);
19438                 return;
19439         }
19440 
19441         /*
19442          * 1147670: retry only if sd_retry_on_reservation_conflict
19443          * property is set (default is 1). Retries will not succeed
19444          * on a disk reserved by another initiator. HA systems
19445          * may reset this via sd.conf to avoid these retries.
19446          *
19447          * Note: The legacy return code for this failure is EIO, however EACCES
19448          * seems more appropriate for a reservation conflict.
19449          */
19450         if (sd_retry_on_reservation_conflict == 0) {
19451                 SD_ERROR(SD_LOG_IO, un,
19452                     "sd_handle_resv_conflict: Device Reserved\n");
19453                 sd_return_failed_command(un, bp, EIO);
19454                 return;
19455         }
19456 
19457         /*
19458          * Retry the command if we can.
19459          *
19460          * Note: The legacy return code for this failure is EIO, however EACCES
19461          * seems more appropriate for a reservation conflict.
19462          */
19463         sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19464             (clock_t)2, NULL);
19465 }
19466 
19467 
19468 
19469 /*
19470  *    Function: sd_pkt_status_qfull
19471  *
19472  * Description: Handle a QUEUE FULL condition from the target.  This can
19473  *              occur if the HBA does not handle the queue full condition.
19474  *              (Basically this means third-party HBAs as Sun HBAs will
19475  *              handle the queue full condition.)  Note that if there are
19476  *              some commands already in the transport, then the queue full
19477  *              has occurred because the queue for this nexus is actually
19478  *              full. If there are no commands in the transport, then the
19479  *              queue full is resulting from some other initiator or lun
19480  *              consuming all the resources at the target.
19481  *
19482  *     Context: May be called from interrupt context
19483  */
19484 
19485 static void
19486 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19487     struct scsi_pkt *pktp)
19488 {
19489         ASSERT(un != NULL);
19490         ASSERT(mutex_owned(SD_MUTEX(un)));
19491         ASSERT(bp != NULL);
19492         ASSERT(xp != NULL);
19493         ASSERT(pktp != NULL);
19494 
19495         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19496             "sd_pkt_status_qfull: entry\n");
19497 
19498         /*
19499          * Just lower the QFULL throttle and retry the command.  Note that
19500          * we do not limit the number of retries here.
19501          */
19502         sd_reduce_throttle(un, SD_THROTTLE_QFULL);
19503         sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0,
19504             SD_RESTART_TIMEOUT, NULL);
19505 
19506         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19507             "sd_pkt_status_qfull: exit\n");
19508 }
19509 
19510 
19511 /*
19512  *    Function: sd_reset_target
19513  *
19514  * Description: Issue a scsi_reset(9F), with either RESET_LUN,
19515  *              RESET_TARGET, or RESET_ALL.
19516  *
19517  *     Context: May be called under interrupt context.
19518  */
19519 
19520 static void
19521 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp)
19522 {
19523         int rval = 0;
19524 
19525         ASSERT(un != NULL);
19526         ASSERT(mutex_owned(SD_MUTEX(un)));
19527         ASSERT(pktp != NULL);
19528 
19529         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n");
19530 
19531         /*
19532          * No need to reset if the transport layer has already done so.
19533          */
19534         if ((pktp->pkt_statistics &
19535             (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) {
19536                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19537                     "sd_reset_target: no reset\n");
19538                 return;
19539         }
19540 
19541         mutex_exit(SD_MUTEX(un));
19542 
19543         if (un->un_f_allow_bus_device_reset == TRUE) {
19544                 if (un->un_f_lun_reset_enabled == TRUE) {
19545                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19546                             "sd_reset_target: RESET_LUN\n");
19547                         rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19548                 }
19549                 if (rval == 0) {
19550                         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19551                             "sd_reset_target: RESET_TARGET\n");
19552                         rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19553                 }
19554         }
19555 
19556         if (rval == 0) {
19557                 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19558                     "sd_reset_target: RESET_ALL\n");
19559                 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
19560         }
19561 
19562         mutex_enter(SD_MUTEX(un));
19563 
19564         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n");
19565 }
19566 
19567 /*
19568  *    Function: sd_target_change_task
19569  *
19570  * Description: Handle dynamic target change
19571  *
19572  *     Context: Executes in a taskq() thread context
19573  */
19574 static void
19575 sd_target_change_task(void *arg)
19576 {
19577         struct sd_lun           *un = arg;
19578         uint64_t                capacity;
19579         diskaddr_t              label_cap;
19580         uint_t                  lbasize;
19581         sd_ssc_t                *ssc;
19582 
19583         ASSERT(un != NULL);
19584         ASSERT(!mutex_owned(SD_MUTEX(un)));
19585 
19586         if ((un->un_f_blockcount_is_valid == FALSE) ||
19587             (un->un_f_tgt_blocksize_is_valid == FALSE)) {
19588                 return;
19589         }
19590 
19591         ssc = sd_ssc_init(un);
19592 
19593         if (sd_send_scsi_READ_CAPACITY(ssc, &capacity,
19594             &lbasize, SD_PATH_DIRECT) != 0) {
19595                 SD_ERROR(SD_LOG_ERROR, un,
19596                     "sd_target_change_task: fail to read capacity\n");
19597                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19598                 goto task_exit;
19599         }
19600 
19601         mutex_enter(SD_MUTEX(un));
19602         if (capacity <= un->un_blockcount) {
19603                 mutex_exit(SD_MUTEX(un));
19604                 goto task_exit;
19605         }
19606 
19607         sd_update_block_info(un, lbasize, capacity);
19608         mutex_exit(SD_MUTEX(un));
19609 
19610         /*
19611          * If lun is EFI labeled and lun capacity is greater than the
19612          * capacity contained in the label, log a sys event.
19613          */
19614         if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
19615             (void*)SD_PATH_DIRECT) == 0) {
19616                 mutex_enter(SD_MUTEX(un));
19617                 if (un->un_f_blockcount_is_valid &&
19618                     un->un_blockcount > label_cap) {
19619                         mutex_exit(SD_MUTEX(un));
19620                         sd_log_lun_expansion_event(un, KM_SLEEP);
19621                 } else {
19622                         mutex_exit(SD_MUTEX(un));
19623                 }
19624         }
19625 
19626 task_exit:
19627         sd_ssc_fini(ssc);
19628 }
19629 
19630 
19631 /*
19632  *    Function: sd_log_dev_status_event
19633  *
19634  * Description: Log EC_dev_status sysevent
19635  *
19636  *     Context: Never called from interrupt context
19637  */
19638 static void
19639 sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag)
19640 {
19641         int err;
19642         char                    *path;
19643         nvlist_t                *attr_list;
19644 
19645         /* Allocate and build sysevent attribute list */
19646         err = nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, km_flag);
19647         if (err != 0) {
19648                 SD_ERROR(SD_LOG_ERROR, un,
19649                     "sd_log_dev_status_event: fail to allocate space\n");
19650                 return;
19651         }
19652 
19653         path = kmem_alloc(MAXPATHLEN, km_flag);
19654         if (path == NULL) {
19655                 nvlist_free(attr_list);
19656                 SD_ERROR(SD_LOG_ERROR, un,
19657                     "sd_log_dev_status_event: fail to allocate space\n");
19658                 return;
19659         }
19660         /*
19661          * Add path attribute to identify the lun.
19662          * We are using minor node 'a' as the sysevent attribute.
19663          */
19664         (void) snprintf(path, MAXPATHLEN, "/devices");
19665         (void) ddi_pathname(SD_DEVINFO(un), path + strlen(path));
19666         (void) snprintf(path + strlen(path), MAXPATHLEN - strlen(path),
19667             ":a");
19668 
19669         err = nvlist_add_string(attr_list, DEV_PHYS_PATH, path);
19670         if (err != 0) {
19671                 nvlist_free(attr_list);
19672                 kmem_free(path, MAXPATHLEN);
19673                 SD_ERROR(SD_LOG_ERROR, un,
19674                     "sd_log_dev_status_event: fail to add attribute\n");
19675                 return;
19676         }
19677 
19678         /* Log dynamic lun expansion sysevent */
19679         err = ddi_log_sysevent(SD_DEVINFO(un), SUNW_VENDOR, EC_DEV_STATUS,
19680             esc, attr_list, NULL, km_flag);
19681         if (err != DDI_SUCCESS) {
19682                 SD_ERROR(SD_LOG_ERROR, un,
19683                     "sd_log_dev_status_event: fail to log sysevent\n");
19684         }
19685 
19686         nvlist_free(attr_list);
19687         kmem_free(path, MAXPATHLEN);
19688 }
19689 
19690 
19691 /*
19692  *    Function: sd_log_lun_expansion_event
19693  *
19694  * Description: Log lun expansion sys event
19695  *
19696  *     Context: Never called from interrupt context
19697  */
19698 static void
19699 sd_log_lun_expansion_event(struct sd_lun *un, int km_flag)
19700 {
19701         sd_log_dev_status_event(un, ESC_DEV_DLE, km_flag);
19702 }
19703 
19704 
19705 /*
19706  *    Function: sd_log_eject_request_event
19707  *
19708  * Description: Log eject request sysevent
19709  *
19710  *     Context: Never called from interrupt context
19711  */
19712 static void
19713 sd_log_eject_request_event(struct sd_lun *un, int km_flag)
19714 {
19715         sd_log_dev_status_event(un, ESC_DEV_EJECT_REQUEST, km_flag);
19716 }
19717 
19718 
19719 /*
19720  *    Function: sd_media_change_task
19721  *
19722  * Description: Recovery action for CDROM to become available.
19723  *
19724  *     Context: Executes in a taskq() thread context
19725  */
19726 
19727 static void
19728 sd_media_change_task(void *arg)
19729 {
19730         struct  scsi_pkt        *pktp = arg;
19731         struct  sd_lun          *un;
19732         struct  buf             *bp;
19733         struct  sd_xbuf         *xp;
19734         int     err             = 0;
19735         int     retry_count     = 0;
19736         int     retry_limit     = SD_UNIT_ATTENTION_RETRY/10;
19737         struct  sd_sense_info   si;
19738 
19739         ASSERT(pktp != NULL);
19740         bp = (struct buf *)pktp->pkt_private;
19741         ASSERT(bp != NULL);
19742         xp = SD_GET_XBUF(bp);
19743         ASSERT(xp != NULL);
19744         un = SD_GET_UN(bp);
19745         ASSERT(un != NULL);
19746         ASSERT(!mutex_owned(SD_MUTEX(un)));
19747         ASSERT(un->un_f_monitor_media_state);
19748 
19749         si.ssi_severity = SCSI_ERR_INFO;
19750         si.ssi_pfa_flag = FALSE;
19751 
19752         /*
19753          * When a reset is issued on a CDROM, it takes a long time to
19754          * recover. First few attempts to read capacity and other things
19755          * related to handling unit attention fail (with a ASC 0x4 and
19756          * ASCQ 0x1). In that case we want to do enough retries and we want
19757          * to limit the retries in other cases of genuine failures like
19758          * no media in drive.
19759          */
19760         while (retry_count++ < retry_limit) {
19761                 if ((err = sd_handle_mchange(un)) == 0) {
19762                         break;
19763                 }
19764                 if (err == EAGAIN) {
19765                         retry_limit = SD_UNIT_ATTENTION_RETRY;
19766                 }
19767                 /* Sleep for 0.5 sec. & try again */
19768                 delay(drv_usectohz(500000));
19769         }
19770 
19771         /*
19772          * Dispatch (retry or fail) the original command here,
19773          * along with appropriate console messages....
19774          *
19775          * Must grab the mutex before calling sd_retry_command,
19776          * sd_print_sense_msg and sd_return_failed_command.
19777          */
19778         mutex_enter(SD_MUTEX(un));
19779         if (err != SD_CMD_SUCCESS) {
19780                 SD_UPDATE_ERRSTATS(un, sd_harderrs);
19781                 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
19782                 si.ssi_severity = SCSI_ERR_FATAL;
19783                 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
19784                 sd_return_failed_command(un, bp, EIO);
19785         } else {
19786                 sd_retry_command(un, bp, SD_RETRIES_UA, sd_print_sense_msg,
19787                     &si, EIO, (clock_t)0, NULL);
19788         }
19789         mutex_exit(SD_MUTEX(un));
19790 }
19791 
19792 
19793 
19794 /*
19795  *    Function: sd_handle_mchange
19796  *
19797  * Description: Perform geometry validation & other recovery when CDROM
19798  *              has been removed from drive.
19799  *
19800  * Return Code: 0 for success
19801  *              errno-type return code of either sd_send_scsi_DOORLOCK() or
19802  *              sd_send_scsi_READ_CAPACITY()
19803  *
19804  *     Context: Executes in a taskq() thread context
19805  */
19806 
19807 static int
19808 sd_handle_mchange(struct sd_lun *un)
19809 {
19810         uint64_t        capacity;
19811         uint32_t        lbasize;
19812         int             rval;
19813         sd_ssc_t        *ssc;
19814 
19815         ASSERT(!mutex_owned(SD_MUTEX(un)));
19816         ASSERT(un->un_f_monitor_media_state);
19817 
19818         ssc = sd_ssc_init(un);
19819         rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
19820             SD_PATH_DIRECT_PRIORITY);
19821 
19822         if (rval != 0)
19823                 goto failed;
19824 
19825         mutex_enter(SD_MUTEX(un));
19826         sd_update_block_info(un, lbasize, capacity);
19827 
19828         if (un->un_errstats != NULL) {
19829                 struct  sd_errstats *stp =
19830                     (struct sd_errstats *)un->un_errstats->ks_data;
19831                 stp->sd_capacity.value.ui64 = (uint64_t)
19832                     ((uint64_t)un->un_blockcount *
19833                     (uint64_t)un->un_tgt_blocksize);
19834         }
19835 
19836         /*
19837          * Check if the media in the device is writable or not
19838          */
19839         if (ISCD(un)) {
19840                 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT_PRIORITY);
19841         }
19842 
19843         /*
19844          * Note: Maybe let the strategy/partitioning chain worry about getting
19845          * valid geometry.
19846          */
19847         mutex_exit(SD_MUTEX(un));
19848         cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
19849 
19850 
19851         if (cmlb_validate(un->un_cmlbhandle, 0,
19852             (void *)SD_PATH_DIRECT_PRIORITY) != 0) {
19853                 sd_ssc_fini(ssc);
19854                 return (EIO);
19855         } else {
19856                 if (un->un_f_pkstats_enabled) {
19857                         sd_set_pstats(un);
19858                         SD_TRACE(SD_LOG_IO_PARTITION, un,
19859                             "sd_handle_mchange: un:0x%p pstats created and "
19860                             "set\n", un);
19861                 }
19862         }
19863 
19864         /*
19865          * Try to lock the door
19866          */
19867         rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
19868             SD_PATH_DIRECT_PRIORITY);
19869 failed:
19870         if (rval != 0)
19871                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19872         sd_ssc_fini(ssc);
19873         return (rval);
19874 }
19875 
19876 
19877 /*
19878  *    Function: sd_send_scsi_DOORLOCK
19879  *
19880  * Description: Issue the scsi DOOR LOCK command
19881  *
19882  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
19883  *                      structure for this target.
19884  *              flag  - SD_REMOVAL_ALLOW
19885  *                      SD_REMOVAL_PREVENT
19886  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19887  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19888  *                      to use the USCSI "direct" chain and bypass the normal
19889  *                      command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19890  *                      command is issued as part of an error recovery action.
19891  *
19892  * Return Code: 0   - Success
19893  *              errno return code from sd_ssc_send()
19894  *
19895  *     Context: Can sleep.
19896  */
19897 
19898 static int
19899 sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag)
19900 {
19901         struct scsi_extended_sense      sense_buf;
19902         union scsi_cdb          cdb;
19903         struct uscsi_cmd        ucmd_buf;
19904         int                     status;
19905         struct sd_lun           *un;
19906 
19907         ASSERT(ssc != NULL);
19908         un = ssc->ssc_un;
19909         ASSERT(un != NULL);
19910         ASSERT(!mutex_owned(SD_MUTEX(un)));
19911 
19912         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un);
19913 
19914         /* already determined doorlock is not supported, fake success */
19915         if (un->un_f_doorlock_supported == FALSE) {
19916                 return (0);
19917         }
19918 
19919         /*
19920          * If we are ejecting and see an SD_REMOVAL_PREVENT
19921          * ignore the command so we can complete the eject
19922          * operation.
19923          */
19924         if (flag == SD_REMOVAL_PREVENT) {
19925                 mutex_enter(SD_MUTEX(un));
19926                 if (un->un_f_ejecting == TRUE) {
19927                         mutex_exit(SD_MUTEX(un));
19928                         return (EAGAIN);
19929                 }
19930                 mutex_exit(SD_MUTEX(un));
19931         }
19932 
19933         bzero(&cdb, sizeof (cdb));
19934         bzero(&ucmd_buf, sizeof (ucmd_buf));
19935 
19936         cdb.scc_cmd = SCMD_DOORLOCK;
19937         cdb.cdb_opaque[4] = (uchar_t)flag;
19938 
19939         ucmd_buf.uscsi_cdb      = (char *)&cdb;
19940         ucmd_buf.uscsi_cdblen   = CDB_GROUP0;
19941         ucmd_buf.uscsi_bufaddr  = NULL;
19942         ucmd_buf.uscsi_buflen   = 0;
19943         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
19944         ucmd_buf.uscsi_rqlen    = sizeof (sense_buf);
19945         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_SILENT;
19946         ucmd_buf.uscsi_timeout  = 15;
19947 
19948         SD_TRACE(SD_LOG_IO, un,
19949             "sd_send_scsi_DOORLOCK: returning sd_ssc_send\n");
19950 
19951         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
19952             UIO_SYSSPACE, path_flag);
19953 
19954         if (status == 0)
19955                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
19956 
19957         if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) &&
19958             (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19959             (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) {
19960                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19961 
19962                 /* fake success and skip subsequent doorlock commands */
19963                 un->un_f_doorlock_supported = FALSE;
19964                 return (0);
19965         }
19966 
19967         return (status);
19968 }
19969 
19970 /*
19971  *    Function: sd_send_scsi_READ_CAPACITY
19972  *
19973  * Description: This routine uses the scsi READ CAPACITY command to determine
19974  *              the device capacity in number of blocks and the device native
19975  *              block size. If this function returns a failure, then the
19976  *              values in *capp and *lbap are undefined.  If the capacity
19977  *              returned is 0xffffffff then the lun is too large for a
19978  *              normal READ CAPACITY command and the results of a
19979  *              READ CAPACITY 16 will be used instead.
19980  *
19981  *   Arguments: ssc   - ssc contains ptr to soft state struct for the target
19982  *              capp - ptr to unsigned 64-bit variable to receive the
19983  *                      capacity value from the command.
19984  *              lbap - ptr to unsigned 32-bit varaible to receive the
19985  *                      block size value from the command
19986  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19987  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19988  *                      to use the USCSI "direct" chain and bypass the normal
19989  *                      command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19990  *                      command is issued as part of an error recovery action.
19991  *
19992  * Return Code: 0   - Success
19993  *              EIO - IO error
19994  *              EACCES - Reservation conflict detected
19995  *              EAGAIN - Device is becoming ready
19996  *              errno return code from sd_ssc_send()
19997  *
19998  *     Context: Can sleep.  Blocks until command completes.
19999  */
20000 
20001 #define SD_CAPACITY_SIZE        sizeof (struct scsi_capacity)
20002 
20003 static int
20004 sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap,
20005     int path_flag)
20006 {
20007         struct  scsi_extended_sense     sense_buf;
20008         struct  uscsi_cmd       ucmd_buf;
20009         union   scsi_cdb        cdb;
20010         uint32_t                *capacity_buf;
20011         uint64_t                capacity;
20012         uint32_t                lbasize;
20013         uint32_t                pbsize;
20014         int                     status;
20015         struct sd_lun           *un;
20016 
20017         ASSERT(ssc != NULL);
20018 
20019         un = ssc->ssc_un;
20020         ASSERT(un != NULL);
20021         ASSERT(!mutex_owned(SD_MUTEX(un)));
20022         ASSERT(capp != NULL);
20023         ASSERT(lbap != NULL);
20024 
20025         SD_TRACE(SD_LOG_IO, un,
20026             "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
20027 
20028         /*
20029          * First send a READ_CAPACITY command to the target.
20030          * (This command is mandatory under SCSI-2.)
20031          *
20032          * Set up the CDB for the READ_CAPACITY command.  The Partial
20033          * Medium Indicator bit is cleared.  The address field must be
20034          * zero if the PMI bit is zero.
20035          */
20036         bzero(&cdb, sizeof (cdb));
20037         bzero(&ucmd_buf, sizeof (ucmd_buf));
20038 
20039         capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP);
20040 
20041         cdb.scc_cmd = SCMD_READ_CAPACITY;
20042 
20043         ucmd_buf.uscsi_cdb      = (char *)&cdb;
20044         ucmd_buf.uscsi_cdblen   = CDB_GROUP1;
20045         ucmd_buf.uscsi_bufaddr  = (caddr_t)capacity_buf;
20046         ucmd_buf.uscsi_buflen   = SD_CAPACITY_SIZE;
20047         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
20048         ucmd_buf.uscsi_rqlen    = sizeof (sense_buf);
20049         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20050         ucmd_buf.uscsi_timeout  = 60;
20051 
20052         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20053             UIO_SYSSPACE, path_flag);
20054 
20055         switch (status) {
20056         case 0:
20057                 /* Return failure if we did not get valid capacity data. */
20058                 if (ucmd_buf.uscsi_resid != 0) {
20059                         sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20060                             "sd_send_scsi_READ_CAPACITY received invalid "
20061                             "capacity data");
20062                         kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20063                         return (EIO);
20064                 }
20065                 /*
20066                  * Read capacity and block size from the READ CAPACITY 10 data.
20067                  * This data may be adjusted later due to device specific
20068                  * issues.
20069                  *
20070                  * According to the SCSI spec, the READ CAPACITY 10
20071                  * command returns the following:
20072                  *
20073                  *  bytes 0-3: Maximum logical block address available.
20074                  *              (MSB in byte:0 & LSB in byte:3)
20075                  *
20076                  *  bytes 4-7: Block length in bytes
20077                  *              (MSB in byte:4 & LSB in byte:7)
20078                  *
20079                  */
20080                 capacity = BE_32(capacity_buf[0]);
20081                 lbasize = BE_32(capacity_buf[1]);
20082 
20083                 /*
20084                  * Done with capacity_buf
20085                  */
20086                 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20087 
20088                 /*
20089                  * if the reported capacity is set to all 0xf's, then
20090                  * this disk is too large and requires SBC-2 commands.
20091                  * Reissue the request using READ CAPACITY 16.
20092                  */
20093                 if (capacity == 0xffffffff) {
20094                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20095                         status = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity,
20096                             &lbasize, &pbsize, path_flag);
20097                         if (status != 0) {
20098                                 return (status);
20099                         } else {
20100                                 goto rc16_done;
20101                         }
20102                 }
20103                 break;  /* Success! */
20104         case EIO:
20105                 switch (ucmd_buf.uscsi_status) {
20106                 case STATUS_RESERVATION_CONFLICT:
20107                         status = EACCES;
20108                         break;
20109                 case STATUS_CHECK:
20110                         /*
20111                          * Check condition; look for ASC/ASCQ of 0x04/0x01
20112                          * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
20113                          */
20114                         if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20115                             (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
20116                             (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
20117                                 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20118                                 return (EAGAIN);
20119                         }
20120                         break;
20121                 default:
20122                         break;
20123                 }
20124                 /* FALLTHRU */
20125         default:
20126                 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20127                 return (status);
20128         }
20129 
20130         /*
20131          * Some ATAPI CD-ROM drives report inaccurate LBA size values
20132          * (2352 and 0 are common) so for these devices always force the value
20133          * to 2048 as required by the ATAPI specs.
20134          */
20135         if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
20136                 lbasize = 2048;
20137         }
20138 
20139         /*
20140          * Get the maximum LBA value from the READ CAPACITY data.
20141          * Here we assume that the Partial Medium Indicator (PMI) bit
20142          * was cleared when issuing the command. This means that the LBA
20143          * returned from the device is the LBA of the last logical block
20144          * on the logical unit.  The actual logical block count will be
20145          * this value plus one.
20146          */
20147         capacity += 1;
20148 
20149         /*
20150          * Currently, for removable media, the capacity is saved in terms
20151          * of un->un_sys_blocksize, so scale the capacity value to reflect this.
20152          */
20153         if (un->un_f_has_removable_media)
20154                 capacity *= (lbasize / un->un_sys_blocksize);
20155 
20156 rc16_done:
20157 
20158         /*
20159          * Copy the values from the READ CAPACITY command into the space
20160          * provided by the caller.
20161          */
20162         *capp = capacity;
20163         *lbap = lbasize;
20164 
20165         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: "
20166             "capacity:0x%llx  lbasize:0x%x\n", capacity, lbasize);
20167 
20168         /*
20169          * Both the lbasize and capacity from the device must be nonzero,
20170          * otherwise we assume that the values are not valid and return
20171          * failure to the caller. (4203735)
20172          */
20173         if ((capacity == 0) || (lbasize == 0)) {
20174                 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20175                     "sd_send_scsi_READ_CAPACITY received invalid value "
20176                     "capacity %llu lbasize %d", capacity, lbasize);
20177                 return (EIO);
20178         }
20179         sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20180         return (0);
20181 }
20182 
20183 /*
20184  *    Function: sd_send_scsi_READ_CAPACITY_16
20185  *
20186  * Description: This routine uses the scsi READ CAPACITY 16 command to
20187  *              determine the device capacity in number of blocks and the
20188  *              device native block size.  If this function returns a failure,
20189  *              then the values in *capp and *lbap are undefined.
20190  *              This routine should be called by sd_send_scsi_READ_CAPACITY
20191  *              which will apply any device specific adjustments to capacity
20192  *              and lbasize. One exception is it is also called by
20193  *              sd_get_media_info_ext. In that function, there is no need to
20194  *              adjust the capacity and lbasize.
20195  *
20196  *   Arguments: ssc   - ssc contains ptr to soft state struct for the target
20197  *              capp - ptr to unsigned 64-bit variable to receive the
20198  *                      capacity value from the command.
20199  *              lbap - ptr to unsigned 32-bit varaible to receive the
20200  *                      block size value from the command
20201  *              psp  - ptr to unsigned 32-bit variable to receive the
20202  *                      physical block size value from the command
20203  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20204  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20205  *                      to use the USCSI "direct" chain and bypass the normal
20206  *                      command waitq. SD_PATH_DIRECT_PRIORITY is used when
20207  *                      this command is issued as part of an error recovery
20208  *                      action.
20209  *
20210  * Return Code: 0   - Success
20211  *              EIO - IO error
20212  *              EACCES - Reservation conflict detected
20213  *              EAGAIN - Device is becoming ready
20214  *              errno return code from sd_ssc_send()
20215  *
20216  *     Context: Can sleep.  Blocks until command completes.
20217  */
20218 
20219 #define SD_CAPACITY_16_SIZE     sizeof (struct scsi_capacity_16)
20220 
20221 static int
20222 sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap,
20223     uint32_t *psp, int path_flag)
20224 {
20225         struct  scsi_extended_sense     sense_buf;
20226         struct  uscsi_cmd       ucmd_buf;
20227         union   scsi_cdb        cdb;
20228         uint64_t                *capacity16_buf;
20229         uint64_t                capacity;
20230         uint32_t                lbasize;
20231         uint32_t                pbsize;
20232         uint32_t                lbpb_exp;
20233         int                     status;
20234         struct sd_lun           *un;
20235 
20236         ASSERT(ssc != NULL);
20237 
20238         un = ssc->ssc_un;
20239         ASSERT(un != NULL);
20240         ASSERT(!mutex_owned(SD_MUTEX(un)));
20241         ASSERT(capp != NULL);
20242         ASSERT(lbap != NULL);
20243 
20244         SD_TRACE(SD_LOG_IO, un,
20245             "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
20246 
20247         /*
20248          * First send a READ_CAPACITY_16 command to the target.
20249          *
20250          * Set up the CDB for the READ_CAPACITY_16 command.  The Partial
20251          * Medium Indicator bit is cleared.  The address field must be
20252          * zero if the PMI bit is zero.
20253          */
20254         bzero(&cdb, sizeof (cdb));
20255         bzero(&ucmd_buf, sizeof (ucmd_buf));
20256 
20257         capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP);
20258 
20259         ucmd_buf.uscsi_cdb      = (char *)&cdb;
20260         ucmd_buf.uscsi_cdblen   = CDB_GROUP4;
20261         ucmd_buf.uscsi_bufaddr  = (caddr_t)capacity16_buf;
20262         ucmd_buf.uscsi_buflen   = SD_CAPACITY_16_SIZE;
20263         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
20264         ucmd_buf.uscsi_rqlen    = sizeof (sense_buf);
20265         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20266         ucmd_buf.uscsi_timeout  = 60;
20267 
20268         /*
20269          * Read Capacity (16) is a Service Action In command.  One
20270          * command byte (0x9E) is overloaded for multiple operations,
20271          * with the second CDB byte specifying the desired operation
20272          */
20273         cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4;
20274         cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4;
20275 
20276         /*
20277          * Fill in allocation length field
20278          */
20279         FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen);
20280 
20281         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20282             UIO_SYSSPACE, path_flag);
20283 
20284         switch (status) {
20285         case 0:
20286                 /* Return failure if we did not get valid capacity data. */
20287                 if (ucmd_buf.uscsi_resid > 20) {
20288                         sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20289                             "sd_send_scsi_READ_CAPACITY_16 received invalid "
20290                             "capacity data");
20291                         kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20292                         return (EIO);
20293                 }
20294 
20295                 /*
20296                  * Read capacity and block size from the READ CAPACITY 16 data.
20297                  * This data may be adjusted later due to device specific
20298                  * issues.
20299                  *
20300                  * According to the SCSI spec, the READ CAPACITY 16
20301                  * command returns the following:
20302                  *
20303                  *  bytes 0-7: Maximum logical block address available.
20304                  *              (MSB in byte:0 & LSB in byte:7)
20305                  *
20306                  *  bytes 8-11: Block length in bytes
20307                  *              (MSB in byte:8 & LSB in byte:11)
20308                  *
20309                  *  byte 13: LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT
20310                  */
20311                 capacity = BE_64(capacity16_buf[0]);
20312                 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]);
20313                 lbpb_exp = (BE_64(capacity16_buf[1]) >> 16) & 0x0f;
20314 
20315                 pbsize = lbasize << lbpb_exp;
20316 
20317                 /*
20318                  * Done with capacity16_buf
20319                  */
20320                 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20321 
20322                 /*
20323                  * if the reported capacity is set to all 0xf's, then
20324                  * this disk is too large.  This could only happen with
20325                  * a device that supports LBAs larger than 64 bits which
20326                  * are not defined by any current T10 standards.
20327                  */
20328                 if (capacity == 0xffffffffffffffff) {
20329                         sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20330                             "disk is too large");
20331                         return (EIO);
20332                 }
20333                 break;  /* Success! */
20334         case EIO:
20335                 switch (ucmd_buf.uscsi_status) {
20336                 case STATUS_RESERVATION_CONFLICT:
20337                         status = EACCES;
20338                         break;
20339                 case STATUS_CHECK:
20340                         /*
20341                          * Check condition; look for ASC/ASCQ of 0x04/0x01
20342                          * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
20343                          */
20344                         if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20345                             (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
20346                             (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
20347                                 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20348                                 return (EAGAIN);
20349                         }
20350                         break;
20351                 default:
20352                         break;
20353                 }
20354                 /* FALLTHRU */
20355         default:
20356                 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20357                 return (status);
20358         }
20359 
20360         /*
20361          * Some ATAPI CD-ROM drives report inaccurate LBA size values
20362          * (2352 and 0 are common) so for these devices always force the value
20363          * to 2048 as required by the ATAPI specs.
20364          */
20365         if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
20366                 lbasize = 2048;
20367         }
20368 
20369         /*
20370          * Get the maximum LBA value from the READ CAPACITY 16 data.
20371          * Here we assume that the Partial Medium Indicator (PMI) bit
20372          * was cleared when issuing the command. This means that the LBA
20373          * returned from the device is the LBA of the last logical block
20374          * on the logical unit.  The actual logical block count will be
20375          * this value plus one.
20376          */
20377         capacity += 1;
20378 
20379         /*
20380          * Currently, for removable media, the capacity is saved in terms
20381          * of un->un_sys_blocksize, so scale the capacity value to reflect this.
20382          */
20383         if (un->un_f_has_removable_media)
20384                 capacity *= (lbasize / un->un_sys_blocksize);
20385 
20386         *capp = capacity;
20387         *lbap = lbasize;
20388         *psp = pbsize;
20389 
20390         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: "
20391             "capacity:0x%llx  lbasize:0x%x, pbsize: 0x%x\n",
20392             capacity, lbasize, pbsize);
20393 
20394         if ((capacity == 0) || (lbasize == 0) || (pbsize == 0)) {
20395                 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20396                     "sd_send_scsi_READ_CAPACITY_16 received invalid value "
20397                     "capacity %llu lbasize %d pbsize %d", capacity, lbasize);
20398                 return (EIO);
20399         }
20400 
20401         sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20402         return (0);
20403 }
20404 
20405 
20406 /*
20407  *    Function: sd_send_scsi_START_STOP_UNIT
20408  *
20409  * Description: Issue a scsi START STOP UNIT command to the target.
20410  *
20411  *   Arguments: ssc    - ssc contatins pointer to driver soft state (unit)
20412  *                       structure for this target.
20413  *      pc_flag - SD_POWER_CONDITION
20414  *                SD_START_STOP
20415  *              flag  - SD_TARGET_START
20416  *                      SD_TARGET_STOP
20417  *                      SD_TARGET_EJECT
20418  *                      SD_TARGET_CLOSE
20419  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20420  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20421  *                      to use the USCSI "direct" chain and bypass the normal
20422  *                      command waitq. SD_PATH_DIRECT_PRIORITY is used when this
20423  *                      command is issued as part of an error recovery action.
20424  *
20425  * Return Code: 0   - Success
20426  *              EIO - IO error
20427  *              EACCES - Reservation conflict detected
20428  *              ENXIO  - Not Ready, medium not present
20429  *              errno return code from sd_ssc_send()
20430  *
20431  *     Context: Can sleep.
20432  */
20433 
20434 static int
20435 sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag, int flag,
20436     int path_flag)
20437 {
20438         struct  scsi_extended_sense     sense_buf;
20439         union scsi_cdb          cdb;
20440         struct uscsi_cmd        ucmd_buf;
20441         int                     status;
20442         struct sd_lun           *un;
20443 
20444         ASSERT(ssc != NULL);
20445         un = ssc->ssc_un;
20446         ASSERT(un != NULL);
20447         ASSERT(!mutex_owned(SD_MUTEX(un)));
20448 
20449         SD_TRACE(SD_LOG_IO, un,
20450             "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un);
20451 
20452         if (un->un_f_check_start_stop &&
20453             (pc_flag == SD_START_STOP) &&
20454             ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) &&
20455             (un->un_f_start_stop_supported != TRUE)) {
20456                 return (0);
20457         }
20458 
20459         /*
20460          * If we are performing an eject operation and
20461          * we receive any command other than SD_TARGET_EJECT
20462          * we should immediately return.
20463          */
20464         if (flag != SD_TARGET_EJECT) {
20465                 mutex_enter(SD_MUTEX(un));
20466                 if (un->un_f_ejecting == TRUE) {
20467                         mutex_exit(SD_MUTEX(un));
20468                         return (EAGAIN);
20469                 }
20470                 mutex_exit(SD_MUTEX(un));
20471         }
20472 
20473         bzero(&cdb, sizeof (cdb));
20474         bzero(&ucmd_buf, sizeof (ucmd_buf));
20475         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20476 
20477         cdb.scc_cmd = SCMD_START_STOP;
20478         cdb.cdb_opaque[4] = (pc_flag == SD_POWER_CONDITION) ?
20479             (uchar_t)(flag << 4) : (uchar_t)flag;
20480 
20481         ucmd_buf.uscsi_cdb      = (char *)&cdb;
20482         ucmd_buf.uscsi_cdblen   = CDB_GROUP0;
20483         ucmd_buf.uscsi_bufaddr  = NULL;
20484         ucmd_buf.uscsi_buflen   = 0;
20485         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
20486         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
20487         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_SILENT;
20488         ucmd_buf.uscsi_timeout  = 200;
20489 
20490         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20491             UIO_SYSSPACE, path_flag);
20492 
20493         switch (status) {
20494         case 0:
20495                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20496                 break;  /* Success! */
20497         case EIO:
20498                 switch (ucmd_buf.uscsi_status) {
20499                 case STATUS_RESERVATION_CONFLICT:
20500                         status = EACCES;
20501                         break;
20502                 case STATUS_CHECK:
20503                         if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) {
20504                                 switch (scsi_sense_key(
20505                                     (uint8_t *)&sense_buf)) {
20506                                 case KEY_ILLEGAL_REQUEST:
20507                                         status = ENOTSUP;
20508                                         break;
20509                                 case KEY_NOT_READY:
20510                                         if (scsi_sense_asc(
20511                                             (uint8_t *)&sense_buf)
20512                                             == 0x3A) {
20513                                                 status = ENXIO;
20514                                         }
20515                                         break;
20516                                 default:
20517                                         break;
20518                                 }
20519                         }
20520                         break;
20521                 default:
20522                         break;
20523                 }
20524                 break;
20525         default:
20526                 break;
20527         }
20528 
20529         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n");
20530 
20531         return (status);
20532 }
20533 
20534 
20535 /*
20536  *    Function: sd_start_stop_unit_callback
20537  *
20538  * Description: timeout(9F) callback to begin recovery process for a
20539  *              device that has spun down.
20540  *
20541  *   Arguments: arg - pointer to associated softstate struct.
20542  *
20543  *     Context: Executes in a timeout(9F) thread context
20544  */
20545 
20546 static void
20547 sd_start_stop_unit_callback(void *arg)
20548 {
20549         struct sd_lun   *un = arg;
20550         ASSERT(un != NULL);
20551         ASSERT(!mutex_owned(SD_MUTEX(un)));
20552 
20553         SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n");
20554 
20555         (void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP);
20556 }
20557 
20558 
20559 /*
20560  *    Function: sd_start_stop_unit_task
20561  *
20562  * Description: Recovery procedure when a drive is spun down.
20563  *
20564  *   Arguments: arg - pointer to associated softstate struct.
20565  *
20566  *     Context: Executes in a taskq() thread context
20567  */
20568 
20569 static void
20570 sd_start_stop_unit_task(void *arg)
20571 {
20572         struct sd_lun   *un = arg;
20573         sd_ssc_t        *ssc;
20574         int             power_level;
20575         int             rval;
20576 
20577         ASSERT(un != NULL);
20578         ASSERT(!mutex_owned(SD_MUTEX(un)));
20579 
20580         SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n");
20581 
20582         /*
20583          * Some unformatted drives report not ready error, no need to
20584          * restart if format has been initiated.
20585          */
20586         mutex_enter(SD_MUTEX(un));
20587         if (un->un_f_format_in_progress == TRUE) {
20588                 mutex_exit(SD_MUTEX(un));
20589                 return;
20590         }
20591         mutex_exit(SD_MUTEX(un));
20592 
20593         ssc = sd_ssc_init(un);
20594         /*
20595          * When a START STOP command is issued from here, it is part of a
20596          * failure recovery operation and must be issued before any other
20597          * commands, including any pending retries. Thus it must be sent
20598          * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up
20599          * succeeds or not, we will start I/O after the attempt.
20600          * If power condition is supported and the current power level
20601          * is capable of performing I/O, we should set the power condition
20602          * to that level. Otherwise, set the power condition to ACTIVE.
20603          */
20604         if (un->un_f_power_condition_supported) {
20605                 mutex_enter(SD_MUTEX(un));
20606                 ASSERT(SD_PM_IS_LEVEL_VALID(un, un->un_power_level));
20607                 power_level = sd_pwr_pc.ran_perf[un->un_power_level]
20608                     > 0 ? un->un_power_level : SD_SPINDLE_ACTIVE;
20609                 mutex_exit(SD_MUTEX(un));
20610                 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
20611                     sd_pl2pc[power_level], SD_PATH_DIRECT_PRIORITY);
20612         } else {
20613                 rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
20614                     SD_TARGET_START, SD_PATH_DIRECT_PRIORITY);
20615         }
20616 
20617         if (rval != 0)
20618                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20619         sd_ssc_fini(ssc);
20620         /*
20621          * The above call blocks until the START_STOP_UNIT command completes.
20622          * Now that it has completed, we must re-try the original IO that
20623          * received the NOT READY condition in the first place. There are
20624          * three possible conditions here:
20625          *
20626          *  (1) The original IO is on un_retry_bp.
20627          *  (2) The original IO is on the regular wait queue, and un_retry_bp
20628          *      is NULL.
20629          *  (3) The original IO is on the regular wait queue, and un_retry_bp
20630          *      points to some other, unrelated bp.
20631          *
20632          * For each case, we must call sd_start_cmds() with un_retry_bp
20633          * as the argument. If un_retry_bp is NULL, this will initiate
20634          * processing of the regular wait queue.  If un_retry_bp is not NULL,
20635          * then this will process the bp on un_retry_bp. That may or may not
20636          * be the original IO, but that does not matter: the important thing
20637          * is to keep the IO processing going at this point.
20638          *
20639          * Note: This is a very specific error recovery sequence associated
20640          * with a drive that is not spun up. We attempt a START_STOP_UNIT and
20641          * serialize the I/O with completion of the spin-up.
20642          */
20643         mutex_enter(SD_MUTEX(un));
20644         SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
20645             "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n",
20646             un, un->un_retry_bp);
20647         un->un_startstop_timeid = NULL;      /* Timeout is no longer pending */
20648         sd_start_cmds(un, un->un_retry_bp);
20649         mutex_exit(SD_MUTEX(un));
20650 
20651         SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n");
20652 }
20653 
20654 
20655 /*
20656  *    Function: sd_send_scsi_INQUIRY
20657  *
20658  * Description: Issue the scsi INQUIRY command.
20659  *
20660  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20661  *                      structure for this target.
20662  *              bufaddr
20663  *              buflen
20664  *              evpd
20665  *              page_code
20666  *              page_length
20667  *
20668  * Return Code: 0   - Success
20669  *              errno return code from sd_ssc_send()
20670  *
20671  *     Context: Can sleep. Does not return until command is completed.
20672  */
20673 
20674 static int
20675 sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr, size_t buflen,
20676     uchar_t evpd, uchar_t page_code, size_t *residp)
20677 {
20678         union scsi_cdb          cdb;
20679         struct uscsi_cmd        ucmd_buf;
20680         int                     status;
20681         struct sd_lun           *un;
20682 
20683         ASSERT(ssc != NULL);
20684         un = ssc->ssc_un;
20685         ASSERT(un != NULL);
20686         ASSERT(!mutex_owned(SD_MUTEX(un)));
20687         ASSERT(bufaddr != NULL);
20688 
20689         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un);
20690 
20691         bzero(&cdb, sizeof (cdb));
20692         bzero(&ucmd_buf, sizeof (ucmd_buf));
20693         bzero(bufaddr, buflen);
20694 
20695         cdb.scc_cmd = SCMD_INQUIRY;
20696         cdb.cdb_opaque[1] = evpd;
20697         cdb.cdb_opaque[2] = page_code;
20698         FORMG0COUNT(&cdb, buflen);
20699 
20700         ucmd_buf.uscsi_cdb      = (char *)&cdb;
20701         ucmd_buf.uscsi_cdblen   = CDB_GROUP0;
20702         ucmd_buf.uscsi_bufaddr  = (caddr_t)bufaddr;
20703         ucmd_buf.uscsi_buflen   = buflen;
20704         ucmd_buf.uscsi_rqbuf    = NULL;
20705         ucmd_buf.uscsi_rqlen    = 0;
20706         ucmd_buf.uscsi_flags    = USCSI_READ | USCSI_SILENT;
20707         ucmd_buf.uscsi_timeout  = 200;  /* Excessive legacy value */
20708 
20709         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20710             UIO_SYSSPACE, SD_PATH_DIRECT);
20711 
20712         /*
20713          * Only handle status == 0, the upper-level caller
20714          * will put different assessment based on the context.
20715          */
20716         if (status == 0)
20717                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20718 
20719         if ((status == 0) && (residp != NULL)) {
20720                 *residp = ucmd_buf.uscsi_resid;
20721         }
20722 
20723         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n");
20724 
20725         return (status);
20726 }
20727 
20728 
20729 /*
20730  *    Function: sd_send_scsi_TEST_UNIT_READY
20731  *
20732  * Description: Issue the scsi TEST UNIT READY command.
20733  *              This routine can be told to set the flag USCSI_DIAGNOSE to
20734  *              prevent retrying failed commands. Use this when the intent
20735  *              is either to check for device readiness, to clear a Unit
20736  *              Attention, or to clear any outstanding sense data.
20737  *              However under specific conditions the expected behavior
20738  *              is for retries to bring a device ready, so use the flag
20739  *              with caution.
20740  *
20741  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20742  *                      structure for this target.
20743  *              flag:   SD_CHECK_FOR_MEDIA: return ENXIO if no media present
20744  *                      SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE.
20745  *                      0: dont check for media present, do retries on cmd.
20746  *
20747  * Return Code: 0   - Success
20748  *              EIO - IO error
20749  *              EACCES - Reservation conflict detected
20750  *              ENXIO  - Not Ready, medium not present
20751  *              errno return code from sd_ssc_send()
20752  *
20753  *     Context: Can sleep. Does not return until command is completed.
20754  */
20755 
20756 static int
20757 sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag)
20758 {
20759         struct  scsi_extended_sense     sense_buf;
20760         union scsi_cdb          cdb;
20761         struct uscsi_cmd        ucmd_buf;
20762         int                     status;
20763         struct sd_lun           *un;
20764 
20765         ASSERT(ssc != NULL);
20766         un = ssc->ssc_un;
20767         ASSERT(un != NULL);
20768         ASSERT(!mutex_owned(SD_MUTEX(un)));
20769 
20770         SD_TRACE(SD_LOG_IO, un,
20771             "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un);
20772 
20773         /*
20774          * Some Seagate elite1 TQ devices get hung with disconnect/reconnect
20775          * timeouts when they receive a TUR and the queue is not empty. Check
20776          * the configuration flag set during attach (indicating the drive has
20777          * this firmware bug) and un_ncmds_in_transport before issuing the
20778          * TUR. If there are
20779          * pending commands return success, this is a bit arbitrary but is ok
20780          * for non-removables (i.e. the eliteI disks) and non-clustering
20781          * configurations.
20782          */
20783         if (un->un_f_cfg_tur_check == TRUE) {
20784                 mutex_enter(SD_MUTEX(un));
20785                 if (un->un_ncmds_in_transport != 0) {
20786                         mutex_exit(SD_MUTEX(un));
20787                         return (0);
20788                 }
20789                 mutex_exit(SD_MUTEX(un));
20790         }
20791 
20792         bzero(&cdb, sizeof (cdb));
20793         bzero(&ucmd_buf, sizeof (ucmd_buf));
20794         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20795 
20796         cdb.scc_cmd = SCMD_TEST_UNIT_READY;
20797 
20798         ucmd_buf.uscsi_cdb      = (char *)&cdb;
20799         ucmd_buf.uscsi_cdblen   = CDB_GROUP0;
20800         ucmd_buf.uscsi_bufaddr  = NULL;
20801         ucmd_buf.uscsi_buflen   = 0;
20802         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
20803         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
20804         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_SILENT;
20805 
20806         /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */
20807         if ((flag & SD_DONT_RETRY_TUR) != 0) {
20808                 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE;
20809         }
20810         ucmd_buf.uscsi_timeout  = 60;
20811 
20812         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20813             UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT :
20814             SD_PATH_STANDARD));
20815 
20816         switch (status) {
20817         case 0:
20818                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20819                 break;  /* Success! */
20820         case EIO:
20821                 switch (ucmd_buf.uscsi_status) {
20822                 case STATUS_RESERVATION_CONFLICT:
20823                         status = EACCES;
20824                         break;
20825                 case STATUS_CHECK:
20826                         if ((flag & SD_CHECK_FOR_MEDIA) == 0) {
20827                                 break;
20828                         }
20829                         if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20830                             (scsi_sense_key((uint8_t *)&sense_buf) ==
20831                             KEY_NOT_READY) &&
20832                             (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) {
20833                                 status = ENXIO;
20834                         }
20835                         break;
20836                 default:
20837                         break;
20838                 }
20839                 break;
20840         default:
20841                 break;
20842         }
20843 
20844         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n");
20845 
20846         return (status);
20847 }
20848 
20849 /*
20850  *    Function: sd_send_scsi_PERSISTENT_RESERVE_IN
20851  *
20852  * Description: Issue the scsi PERSISTENT RESERVE IN command.
20853  *
20854  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20855  *                      structure for this target.
20856  *
20857  * Return Code: 0   - Success
20858  *              EACCES
20859  *              ENOTSUP
20860  *              errno return code from sd_ssc_send()
20861  *
20862  *     Context: Can sleep. Does not return until command is completed.
20863  */
20864 
20865 static int
20866 sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc, uchar_t usr_cmd,
20867     uint16_t data_len, uchar_t *data_bufp)
20868 {
20869         struct scsi_extended_sense      sense_buf;
20870         union scsi_cdb          cdb;
20871         struct uscsi_cmd        ucmd_buf;
20872         int                     status;
20873         int                     no_caller_buf = FALSE;
20874         struct sd_lun           *un;
20875 
20876         ASSERT(ssc != NULL);
20877         un = ssc->ssc_un;
20878         ASSERT(un != NULL);
20879         ASSERT(!mutex_owned(SD_MUTEX(un)));
20880         ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV));
20881 
20882         SD_TRACE(SD_LOG_IO, un,
20883             "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un);
20884 
20885         bzero(&cdb, sizeof (cdb));
20886         bzero(&ucmd_buf, sizeof (ucmd_buf));
20887         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20888         if (data_bufp == NULL) {
20889                 /* Allocate a default buf if the caller did not give one */
20890                 ASSERT(data_len == 0);
20891                 data_len  = MHIOC_RESV_KEY_SIZE;
20892                 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP);
20893                 no_caller_buf = TRUE;
20894         }
20895 
20896         cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
20897         cdb.cdb_opaque[1] = usr_cmd;
20898         FORMG1COUNT(&cdb, data_len);
20899 
20900         ucmd_buf.uscsi_cdb      = (char *)&cdb;
20901         ucmd_buf.uscsi_cdblen   = CDB_GROUP1;
20902         ucmd_buf.uscsi_bufaddr  = (caddr_t)data_bufp;
20903         ucmd_buf.uscsi_buflen   = data_len;
20904         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
20905         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
20906         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20907         ucmd_buf.uscsi_timeout  = 60;
20908 
20909         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20910             UIO_SYSSPACE, SD_PATH_STANDARD);
20911 
20912         switch (status) {
20913         case 0:
20914                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20915 
20916                 break;  /* Success! */
20917         case EIO:
20918                 switch (ucmd_buf.uscsi_status) {
20919                 case STATUS_RESERVATION_CONFLICT:
20920                         status = EACCES;
20921                         break;
20922                 case STATUS_CHECK:
20923                         if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20924                             (scsi_sense_key((uint8_t *)&sense_buf) ==
20925                             KEY_ILLEGAL_REQUEST)) {
20926                                 status = ENOTSUP;
20927                         }
20928                         break;
20929                 default:
20930                         break;
20931                 }
20932                 break;
20933         default:
20934                 break;
20935         }
20936 
20937         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n");
20938 
20939         if (no_caller_buf == TRUE) {
20940                 kmem_free(data_bufp, data_len);
20941         }
20942 
20943         return (status);
20944 }
20945 
20946 
20947 /*
20948  *    Function: sd_send_scsi_PERSISTENT_RESERVE_OUT
20949  *
20950  * Description: This routine is the driver entry point for handling CD-ROM
20951  *              multi-host persistent reservation requests (MHIOCGRP_INKEYS,
20952  *              MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the
20953  *              device.
20954  *
20955  *   Arguments: ssc  -  ssc contains un - pointer to soft state struct
20956  *                      for the target.
20957  *              usr_cmd SCSI-3 reservation facility command (one of
20958  *                      SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE,
20959  *                      SD_SCSI3_PREEMPTANDABORT, SD_SCSI3_CLEAR)
20960  *              usr_bufp - user provided pointer register, reserve descriptor or
20961  *                      preempt and abort structure (mhioc_register_t,
20962  *                      mhioc_resv_desc_t, mhioc_preemptandabort_t)
20963  *
20964  * Return Code: 0   - Success
20965  *              EACCES
20966  *              ENOTSUP
20967  *              errno return code from sd_ssc_send()
20968  *
20969  *     Context: Can sleep. Does not return until command is completed.
20970  */
20971 
20972 static int
20973 sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc, uchar_t usr_cmd,
20974     uchar_t *usr_bufp)
20975 {
20976         struct scsi_extended_sense      sense_buf;
20977         union scsi_cdb          cdb;
20978         struct uscsi_cmd        ucmd_buf;
20979         int                     status;
20980         uchar_t                 data_len = sizeof (sd_prout_t);
20981         sd_prout_t              *prp;
20982         struct sd_lun           *un;
20983 
20984         ASSERT(ssc != NULL);
20985         un = ssc->ssc_un;
20986         ASSERT(un != NULL);
20987         ASSERT(!mutex_owned(SD_MUTEX(un)));
20988         ASSERT(data_len == 24); /* required by scsi spec */
20989 
20990         SD_TRACE(SD_LOG_IO, un,
20991             "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un);
20992 
20993         if (usr_bufp == NULL) {
20994                 return (EINVAL);
20995         }
20996 
20997         bzero(&cdb, sizeof (cdb));
20998         bzero(&ucmd_buf, sizeof (ucmd_buf));
20999         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21000         prp = kmem_zalloc(data_len, KM_SLEEP);
21001 
21002         cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
21003         cdb.cdb_opaque[1] = usr_cmd;
21004         FORMG1COUNT(&cdb, data_len);
21005 
21006         ucmd_buf.uscsi_cdb      = (char *)&cdb;
21007         ucmd_buf.uscsi_cdblen   = CDB_GROUP1;
21008         ucmd_buf.uscsi_bufaddr  = (caddr_t)prp;
21009         ucmd_buf.uscsi_buflen   = data_len;
21010         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
21011         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
21012         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
21013         ucmd_buf.uscsi_timeout  = 60;
21014 
21015         switch (usr_cmd) {
21016         case SD_SCSI3_REGISTER: {
21017                 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp;
21018 
21019                 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21020                 bcopy(ptr->newkey.key, prp->service_key,
21021                     MHIOC_RESV_KEY_SIZE);
21022                 prp->aptpl = ptr->aptpl;
21023                 break;
21024         }
21025         case SD_SCSI3_CLEAR: {
21026                 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
21027 
21028                 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21029                 break;
21030         }
21031         case SD_SCSI3_RESERVE:
21032         case SD_SCSI3_RELEASE: {
21033                 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
21034 
21035                 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21036                 prp->scope_address = BE_32(ptr->scope_specific_addr);
21037                 cdb.cdb_opaque[2] = ptr->type;
21038                 break;
21039         }
21040         case SD_SCSI3_PREEMPTANDABORT: {
21041                 mhioc_preemptandabort_t *ptr =
21042                     (mhioc_preemptandabort_t *)usr_bufp;
21043 
21044                 bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21045                 bcopy(ptr->victim_key.key, prp->service_key,
21046                     MHIOC_RESV_KEY_SIZE);
21047                 prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr);
21048                 cdb.cdb_opaque[2] = ptr->resvdesc.type;
21049                 ucmd_buf.uscsi_flags |= USCSI_HEAD;
21050                 break;
21051         }
21052         case SD_SCSI3_REGISTERANDIGNOREKEY:
21053         {
21054                 mhioc_registerandignorekey_t *ptr;
21055                 ptr = (mhioc_registerandignorekey_t *)usr_bufp;
21056                 bcopy(ptr->newkey.key,
21057                     prp->service_key, MHIOC_RESV_KEY_SIZE);
21058                 prp->aptpl = ptr->aptpl;
21059                 break;
21060         }
21061         default:
21062                 ASSERT(FALSE);
21063                 break;
21064         }
21065 
21066         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21067             UIO_SYSSPACE, SD_PATH_STANDARD);
21068 
21069         switch (status) {
21070         case 0:
21071                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21072                 break;  /* Success! */
21073         case EIO:
21074                 switch (ucmd_buf.uscsi_status) {
21075                 case STATUS_RESERVATION_CONFLICT:
21076                         status = EACCES;
21077                         break;
21078                 case STATUS_CHECK:
21079                         if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
21080                             (scsi_sense_key((uint8_t *)&sense_buf) ==
21081                             KEY_ILLEGAL_REQUEST)) {
21082                                 status = ENOTSUP;
21083                         }
21084                         break;
21085                 default:
21086                         break;
21087                 }
21088                 break;
21089         default:
21090                 break;
21091         }
21092 
21093         kmem_free(prp, data_len);
21094         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n");
21095         return (status);
21096 }
21097 
21098 
21099 /*
21100  *    Function: sd_send_scsi_SYNCHRONIZE_CACHE
21101  *
21102  * Description: Issues a scsi SYNCHRONIZE CACHE command to the target
21103  *
21104  *   Arguments: un - pointer to the target's soft state struct
21105  *              dkc - pointer to the callback structure
21106  *
21107  * Return Code: 0 - success
21108  *              errno-type error code
21109  *
21110  *     Context: kernel thread context only.
21111  *
21112  *  _______________________________________________________________
21113  * | dkc_flag &   | dkc_callback | DKIOCFLUSHWRITECACHE            |
21114  * |FLUSH_VOLATILE|              | operation                       |
21115  * |______________|______________|_________________________________|
21116  * | 0            | NULL         | Synchronous flush on both       |
21117  * |              |              | volatile and non-volatile cache |
21118  * |______________|______________|_________________________________|
21119  * | 1            | NULL         | Synchronous flush on volatile   |
21120  * |              |              | cache; disk drivers may suppress|
21121  * |              |              | flush if disk table indicates   |
21122  * |              |              | non-volatile cache              |
21123  * |______________|______________|_________________________________|
21124  * | 0            | !NULL        | Asynchronous flush on both      |
21125  * |              |              | volatile and non-volatile cache;|
21126  * |______________|______________|_________________________________|
21127  * | 1            | !NULL        | Asynchronous flush on volatile  |
21128  * |              |              | cache; disk drivers may suppress|
21129  * |              |              | flush if disk table indicates   |
21130  * |              |              | non-volatile cache              |
21131  * |______________|______________|_________________________________|
21132  *
21133  */
21134 
21135 static int
21136 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc)
21137 {
21138         struct sd_uscsi_info    *uip;
21139         struct uscsi_cmd        *uscmd;
21140         union scsi_cdb          *cdb;
21141         struct buf              *bp;
21142         int                     rval = 0;
21143         int                     is_async;
21144 
21145         SD_TRACE(SD_LOG_IO, un,
21146             "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un);
21147 
21148         ASSERT(un != NULL);
21149         ASSERT(!mutex_owned(SD_MUTEX(un)));
21150 
21151         if (dkc == NULL || dkc->dkc_callback == NULL) {
21152                 is_async = FALSE;
21153         } else {
21154                 is_async = TRUE;
21155         }
21156 
21157         mutex_enter(SD_MUTEX(un));
21158         /* check whether cache flush should be suppressed */
21159         if (un->un_f_suppress_cache_flush == TRUE) {
21160                 mutex_exit(SD_MUTEX(un));
21161                 /*
21162                  * suppress the cache flush if the device is told to do
21163                  * so by sd.conf or disk table
21164                  */
21165                 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: \
21166                     skip the cache flush since suppress_cache_flush is %d!\n",
21167                     un->un_f_suppress_cache_flush);
21168 
21169                 if (is_async == TRUE) {
21170                         /* invoke callback for asynchronous flush */
21171                         (*dkc->dkc_callback)(dkc->dkc_cookie, 0);
21172                 }
21173                 return (rval);
21174         }
21175         mutex_exit(SD_MUTEX(un));
21176 
21177         /*
21178          * check dkc_flag & FLUSH_VOLATILE so SYNC_NV bit can be
21179          * set properly
21180          */
21181         cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP);
21182         cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE;
21183 
21184         mutex_enter(SD_MUTEX(un));
21185         if (dkc != NULL && un->un_f_sync_nv_supported &&
21186             (dkc->dkc_flag & FLUSH_VOLATILE)) {
21187                 /*
21188                  * if the device supports SYNC_NV bit, turn on
21189                  * the SYNC_NV bit to only flush volatile cache
21190                  */
21191                 cdb->cdb_un.tag |= SD_SYNC_NV_BIT;
21192         }
21193         mutex_exit(SD_MUTEX(un));
21194 
21195         /*
21196          * First get some memory for the uscsi_cmd struct and cdb
21197          * and initialize for SYNCHRONIZE_CACHE cmd.
21198          */
21199         uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
21200         uscmd->uscsi_cdblen = CDB_GROUP1;
21201         uscmd->uscsi_cdb = (caddr_t)cdb;
21202         uscmd->uscsi_bufaddr = NULL;
21203         uscmd->uscsi_buflen = 0;
21204         uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
21205         uscmd->uscsi_rqlen = SENSE_LENGTH;
21206         uscmd->uscsi_rqresid = SENSE_LENGTH;
21207         uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
21208         uscmd->uscsi_timeout = sd_io_time;
21209 
21210         /*
21211          * Allocate an sd_uscsi_info struct and fill it with the info
21212          * needed by sd_initpkt_for_uscsi().  Then put the pointer into
21213          * b_private in the buf for sd_initpkt_for_uscsi().  Note that
21214          * since we allocate the buf here in this function, we do not
21215          * need to preserve the prior contents of b_private.
21216          * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
21217          */
21218         uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
21219         uip->ui_flags = SD_PATH_DIRECT;
21220         uip->ui_cmdp  = uscmd;
21221 
21222         bp = getrbuf(KM_SLEEP);
21223         bp->b_private = uip;
21224 
21225         /*
21226          * Setup buffer to carry uscsi request.
21227          */
21228         bp->b_flags  = B_BUSY;
21229         bp->b_bcount = 0;
21230         bp->b_blkno  = 0;
21231 
21232         if (is_async == TRUE) {
21233                 bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone;
21234                 uip->ui_dkc = *dkc;
21235         }
21236 
21237         bp->b_edev = SD_GET_DEV(un);
21238         bp->b_dev = cmpdev(bp->b_edev);   /* maybe unnecessary? */
21239 
21240         /*
21241          * Unset un_f_sync_cache_required flag
21242          */
21243         mutex_enter(SD_MUTEX(un));
21244         un->un_f_sync_cache_required = FALSE;
21245         mutex_exit(SD_MUTEX(un));
21246 
21247         (void) sd_uscsi_strategy(bp);
21248 
21249         /*
21250          * If synchronous request, wait for completion
21251          * If async just return and let b_iodone callback
21252          * cleanup.
21253          * NOTE: On return, u_ncmds_in_driver will be decremented,
21254          * but it was also incremented in sd_uscsi_strategy(), so
21255          * we should be ok.
21256          */
21257         if (is_async == FALSE) {
21258                 (void) biowait(bp);
21259                 rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp);
21260         }
21261 
21262         return (rval);
21263 }
21264 
21265 
21266 static int
21267 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp)
21268 {
21269         struct sd_uscsi_info *uip;
21270         struct uscsi_cmd *uscmd;
21271         uint8_t *sense_buf;
21272         struct sd_lun *un;
21273         int status;
21274         union scsi_cdb *cdb;
21275 
21276         uip = (struct sd_uscsi_info *)(bp->b_private);
21277         ASSERT(uip != NULL);
21278 
21279         uscmd = uip->ui_cmdp;
21280         ASSERT(uscmd != NULL);
21281 
21282         sense_buf = (uint8_t *)uscmd->uscsi_rqbuf;
21283         ASSERT(sense_buf != NULL);
21284 
21285         un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
21286         ASSERT(un != NULL);
21287 
21288         cdb = (union scsi_cdb *)uscmd->uscsi_cdb;
21289 
21290         status = geterror(bp);
21291         switch (status) {
21292         case 0:
21293                 break;  /* Success! */
21294         case EIO:
21295                 switch (uscmd->uscsi_status) {
21296                 case STATUS_RESERVATION_CONFLICT:
21297                         /* Ignore reservation conflict */
21298                         status = 0;
21299                         goto done;
21300 
21301                 case STATUS_CHECK:
21302                         if ((uscmd->uscsi_rqstatus == STATUS_GOOD) &&
21303                             (scsi_sense_key(sense_buf) ==
21304                             KEY_ILLEGAL_REQUEST)) {
21305                                 /* Ignore Illegal Request error */
21306                                 if (cdb->cdb_un.tag&SD_SYNC_NV_BIT) {
21307                                         mutex_enter(SD_MUTEX(un));
21308                                         un->un_f_sync_nv_supported = FALSE;
21309                                         mutex_exit(SD_MUTEX(un));
21310                                         status = 0;
21311                                         SD_TRACE(SD_LOG_IO, un,
21312                                             "un_f_sync_nv_supported \
21313                                             is set to false.\n");
21314                                         goto done;
21315                                 }
21316 
21317                                 mutex_enter(SD_MUTEX(un));
21318                                 un->un_f_sync_cache_supported = FALSE;
21319                                 mutex_exit(SD_MUTEX(un));
21320                                 SD_TRACE(SD_LOG_IO, un,
21321                                     "sd_send_scsi_SYNCHRONIZE_CACHE_biodone: \
21322                                     un_f_sync_cache_supported set to false \
21323                                     with asc = %x, ascq = %x\n",
21324                                     scsi_sense_asc(sense_buf),
21325                                     scsi_sense_ascq(sense_buf));
21326                                 status = ENOTSUP;
21327                                 goto done;
21328                         }
21329                         break;
21330                 default:
21331                         break;
21332                 }
21333                 /* FALLTHRU */
21334         default:
21335                 /*
21336                  * Turn on the un_f_sync_cache_required flag
21337                  * since the SYNC CACHE command failed
21338                  */
21339                 mutex_enter(SD_MUTEX(un));
21340                 un->un_f_sync_cache_required = TRUE;
21341                 mutex_exit(SD_MUTEX(un));
21342 
21343                 /*
21344                  * Don't log an error message if this device
21345                  * has removable media.
21346                  */
21347                 if (!un->un_f_has_removable_media) {
21348                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
21349                             "SYNCHRONIZE CACHE command failed (%d)\n", status);
21350                 }
21351                 break;
21352         }
21353 
21354 done:
21355         if (uip->ui_dkc.dkc_callback != NULL) {
21356                 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status);
21357         }
21358 
21359         ASSERT((bp->b_flags & B_REMAPPED) == 0);
21360         freerbuf(bp);
21361         kmem_free(uip, sizeof (struct sd_uscsi_info));
21362         kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
21363         kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
21364         kmem_free(uscmd, sizeof (struct uscsi_cmd));
21365 
21366         return (status);
21367 }
21368 
21369 
21370 /*
21371  *    Function: sd_send_scsi_GET_CONFIGURATION
21372  *
21373  * Description: Issues the get configuration command to the device.
21374  *              Called from sd_check_for_writable_cd & sd_get_media_info
21375  *              caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN
21376  *   Arguments: ssc
21377  *              ucmdbuf
21378  *              rqbuf
21379  *              rqbuflen
21380  *              bufaddr
21381  *              buflen
21382  *              path_flag
21383  *
21384  * Return Code: 0   - Success
21385  *              errno return code from sd_ssc_send()
21386  *
21387  *     Context: Can sleep. Does not return until command is completed.
21388  *
21389  */
21390 
21391 static int
21392 sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf,
21393     uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen,
21394     int path_flag)
21395 {
21396         char    cdb[CDB_GROUP1];
21397         int     status;
21398         struct sd_lun   *un;
21399 
21400         ASSERT(ssc != NULL);
21401         un = ssc->ssc_un;
21402         ASSERT(un != NULL);
21403         ASSERT(!mutex_owned(SD_MUTEX(un)));
21404         ASSERT(bufaddr != NULL);
21405         ASSERT(ucmdbuf != NULL);
21406         ASSERT(rqbuf != NULL);
21407 
21408         SD_TRACE(SD_LOG_IO, un,
21409             "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un);
21410 
21411         bzero(cdb, sizeof (cdb));
21412         bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21413         bzero(rqbuf, rqbuflen);
21414         bzero(bufaddr, buflen);
21415 
21416         /*
21417          * Set up cdb field for the get configuration command.
21418          */
21419         cdb[0] = SCMD_GET_CONFIGURATION;
21420         cdb[1] = 0x02;  /* Requested Type */
21421         cdb[8] = SD_PROFILE_HEADER_LEN;
21422         ucmdbuf->uscsi_cdb = cdb;
21423         ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21424         ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21425         ucmdbuf->uscsi_buflen = buflen;
21426         ucmdbuf->uscsi_timeout = sd_io_time;
21427         ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21428         ucmdbuf->uscsi_rqlen = rqbuflen;
21429         ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21430 
21431         status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21432             UIO_SYSSPACE, path_flag);
21433 
21434         switch (status) {
21435         case 0:
21436                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21437                 break;  /* Success! */
21438         case EIO:
21439                 switch (ucmdbuf->uscsi_status) {
21440                 case STATUS_RESERVATION_CONFLICT:
21441                         status = EACCES;
21442                         break;
21443                 default:
21444                         break;
21445                 }
21446                 break;
21447         default:
21448                 break;
21449         }
21450 
21451         if (status == 0) {
21452                 SD_DUMP_MEMORY(un, SD_LOG_IO,
21453                     "sd_send_scsi_GET_CONFIGURATION: data",
21454                     (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21455         }
21456 
21457         SD_TRACE(SD_LOG_IO, un,
21458             "sd_send_scsi_GET_CONFIGURATION: exit\n");
21459 
21460         return (status);
21461 }
21462 
21463 /*
21464  *    Function: sd_send_scsi_feature_GET_CONFIGURATION
21465  *
21466  * Description: Issues the get configuration command to the device to
21467  *              retrieve a specific feature. Called from
21468  *              sd_check_for_writable_cd & sd_set_mmc_caps.
21469  *   Arguments: ssc
21470  *              ucmdbuf
21471  *              rqbuf
21472  *              rqbuflen
21473  *              bufaddr
21474  *              buflen
21475  *              feature
21476  *
21477  * Return Code: 0   - Success
21478  *              errno return code from sd_ssc_send()
21479  *
21480  *     Context: Can sleep. Does not return until command is completed.
21481  *
21482  */
21483 static int
21484 sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf,
21485     uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen,
21486     char feature, int path_flag)
21487 {
21488         char    cdb[CDB_GROUP1];
21489         int     status;
21490         struct sd_lun   *un;
21491 
21492         ASSERT(ssc != NULL);
21493         un = ssc->ssc_un;
21494         ASSERT(un != NULL);
21495         ASSERT(!mutex_owned(SD_MUTEX(un)));
21496         ASSERT(bufaddr != NULL);
21497         ASSERT(ucmdbuf != NULL);
21498         ASSERT(rqbuf != NULL);
21499 
21500         SD_TRACE(SD_LOG_IO, un,
21501             "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un);
21502 
21503         bzero(cdb, sizeof (cdb));
21504         bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21505         bzero(rqbuf, rqbuflen);
21506         bzero(bufaddr, buflen);
21507 
21508         /*
21509          * Set up cdb field for the get configuration command.
21510          */
21511         cdb[0] = SCMD_GET_CONFIGURATION;
21512         cdb[1] = 0x02;  /* Requested Type */
21513         cdb[3] = feature;
21514         cdb[8] = buflen;
21515         ucmdbuf->uscsi_cdb = cdb;
21516         ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21517         ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21518         ucmdbuf->uscsi_buflen = buflen;
21519         ucmdbuf->uscsi_timeout = sd_io_time;
21520         ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21521         ucmdbuf->uscsi_rqlen = rqbuflen;
21522         ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21523 
21524         status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21525             UIO_SYSSPACE, path_flag);
21526 
21527         switch (status) {
21528         case 0:
21529 
21530                 break;  /* Success! */
21531         case EIO:
21532                 switch (ucmdbuf->uscsi_status) {
21533                 case STATUS_RESERVATION_CONFLICT:
21534                         status = EACCES;
21535                         break;
21536                 default:
21537                         break;
21538                 }
21539                 break;
21540         default:
21541                 break;
21542         }
21543 
21544         if (status == 0) {
21545                 SD_DUMP_MEMORY(un, SD_LOG_IO,
21546                     "sd_send_scsi_feature_GET_CONFIGURATION: data",
21547                     (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21548         }
21549 
21550         SD_TRACE(SD_LOG_IO, un,
21551             "sd_send_scsi_feature_GET_CONFIGURATION: exit\n");
21552 
21553         return (status);
21554 }
21555 
21556 
21557 /*
21558  *    Function: sd_send_scsi_MODE_SENSE
21559  *
21560  * Description: Utility function for issuing a scsi MODE SENSE command.
21561  *              Note: This routine uses a consistent implementation for Group0,
21562  *              Group1, and Group2 commands across all platforms. ATAPI devices
21563  *              use Group 1 Read/Write commands and Group 2 Mode Sense/Select
21564  *
21565  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21566  *                      structure for this target.
21567  *              cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
21568  *                        CDB_GROUP[1|2] (10 byte).
21569  *              bufaddr - buffer for page data retrieved from the target.
21570  *              buflen - size of page to be retrieved.
21571  *              page_code - page code of data to be retrieved from the target.
21572  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21573  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21574  *                      to use the USCSI "direct" chain and bypass the normal
21575  *                      command waitq.
21576  *
21577  * Return Code: 0   - Success
21578  *              errno return code from sd_ssc_send()
21579  *
21580  *     Context: Can sleep. Does not return until command is completed.
21581  */
21582 
21583 static int
21584 sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
21585     size_t buflen,  uchar_t page_code, int path_flag)
21586 {
21587         struct  scsi_extended_sense     sense_buf;
21588         union scsi_cdb          cdb;
21589         struct uscsi_cmd        ucmd_buf;
21590         int                     status;
21591         int                     headlen;
21592         struct sd_lun           *un;
21593 
21594         ASSERT(ssc != NULL);
21595         un = ssc->ssc_un;
21596         ASSERT(un != NULL);
21597         ASSERT(!mutex_owned(SD_MUTEX(un)));
21598         ASSERT(bufaddr != NULL);
21599         ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
21600             (cdbsize == CDB_GROUP2));
21601 
21602         SD_TRACE(SD_LOG_IO, un,
21603             "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un);
21604 
21605         bzero(&cdb, sizeof (cdb));
21606         bzero(&ucmd_buf, sizeof (ucmd_buf));
21607         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21608         bzero(bufaddr, buflen);
21609 
21610         if (cdbsize == CDB_GROUP0) {
21611                 cdb.scc_cmd = SCMD_MODE_SENSE;
21612                 cdb.cdb_opaque[2] = page_code;
21613                 FORMG0COUNT(&cdb, buflen);
21614                 headlen = MODE_HEADER_LENGTH;
21615         } else {
21616                 cdb.scc_cmd = SCMD_MODE_SENSE_G1;
21617                 cdb.cdb_opaque[2] = page_code;
21618                 FORMG1COUNT(&cdb, buflen);
21619                 headlen = MODE_HEADER_LENGTH_GRP2;
21620         }
21621 
21622         ASSERT(headlen <= buflen);
21623         SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21624 
21625         ucmd_buf.uscsi_cdb      = (char *)&cdb;
21626         ucmd_buf.uscsi_cdblen   = (uchar_t)cdbsize;
21627         ucmd_buf.uscsi_bufaddr  = (caddr_t)bufaddr;
21628         ucmd_buf.uscsi_buflen   = buflen;
21629         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
21630         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
21631         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21632         ucmd_buf.uscsi_timeout  = 60;
21633 
21634         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21635             UIO_SYSSPACE, path_flag);
21636 
21637         switch (status) {
21638         case 0:
21639                 /*
21640                  * sr_check_wp() uses 0x3f page code and check the header of
21641                  * mode page to determine if target device is write-protected.
21642                  * But some USB devices return 0 bytes for 0x3f page code. For
21643                  * this case, make sure that mode page header is returned at
21644                  * least.
21645                  */
21646                 if (buflen - ucmd_buf.uscsi_resid <  headlen) {
21647                         status = EIO;
21648                         sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
21649                             "mode page header is not returned");
21650                 }
21651                 break;  /* Success! */
21652         case EIO:
21653                 switch (ucmd_buf.uscsi_status) {
21654                 case STATUS_RESERVATION_CONFLICT:
21655                         status = EACCES;
21656                         break;
21657                 default:
21658                         break;
21659                 }
21660                 break;
21661         default:
21662                 break;
21663         }
21664 
21665         if (status == 0) {
21666                 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data",
21667                     (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21668         }
21669         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n");
21670 
21671         return (status);
21672 }
21673 
21674 
21675 /*
21676  *    Function: sd_send_scsi_MODE_SELECT
21677  *
21678  * Description: Utility function for issuing a scsi MODE SELECT command.
21679  *              Note: This routine uses a consistent implementation for Group0,
21680  *              Group1, and Group2 commands across all platforms. ATAPI devices
21681  *              use Group 1 Read/Write commands and Group 2 Mode Sense/Select
21682  *
21683  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21684  *                      structure for this target.
21685  *              cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
21686  *                        CDB_GROUP[1|2] (10 byte).
21687  *              bufaddr - buffer for page data retrieved from the target.
21688  *              buflen - size of page to be retrieved.
21689  *              save_page - boolean to determin if SP bit should be set.
21690  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21691  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21692  *                      to use the USCSI "direct" chain and bypass the normal
21693  *                      command waitq.
21694  *
21695  * Return Code: 0   - Success
21696  *              errno return code from sd_ssc_send()
21697  *
21698  *     Context: Can sleep. Does not return until command is completed.
21699  */
21700 
21701 static int
21702 sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
21703     size_t buflen,  uchar_t save_page, int path_flag)
21704 {
21705         struct  scsi_extended_sense     sense_buf;
21706         union scsi_cdb          cdb;
21707         struct uscsi_cmd        ucmd_buf;
21708         int                     status;
21709         struct sd_lun           *un;
21710 
21711         ASSERT(ssc != NULL);
21712         un = ssc->ssc_un;
21713         ASSERT(un != NULL);
21714         ASSERT(!mutex_owned(SD_MUTEX(un)));
21715         ASSERT(bufaddr != NULL);
21716         ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
21717             (cdbsize == CDB_GROUP2));
21718 
21719         SD_TRACE(SD_LOG_IO, un,
21720             "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un);
21721 
21722         bzero(&cdb, sizeof (cdb));
21723         bzero(&ucmd_buf, sizeof (ucmd_buf));
21724         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21725 
21726         /* Set the PF bit for many third party drives */
21727         cdb.cdb_opaque[1] = 0x10;
21728 
21729         /* Set the savepage(SP) bit if given */
21730         if (save_page == SD_SAVE_PAGE) {
21731                 cdb.cdb_opaque[1] |= 0x01;
21732         }
21733 
21734         if (cdbsize == CDB_GROUP0) {
21735                 cdb.scc_cmd = SCMD_MODE_SELECT;
21736                 FORMG0COUNT(&cdb, buflen);
21737         } else {
21738                 cdb.scc_cmd = SCMD_MODE_SELECT_G1;
21739                 FORMG1COUNT(&cdb, buflen);
21740         }
21741 
21742         SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21743 
21744         ucmd_buf.uscsi_cdb      = (char *)&cdb;
21745         ucmd_buf.uscsi_cdblen   = (uchar_t)cdbsize;
21746         ucmd_buf.uscsi_bufaddr  = (caddr_t)bufaddr;
21747         ucmd_buf.uscsi_buflen   = buflen;
21748         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
21749         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
21750         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
21751         ucmd_buf.uscsi_timeout  = 60;
21752 
21753         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21754             UIO_SYSSPACE, path_flag);
21755 
21756         switch (status) {
21757         case 0:
21758                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21759                 break;  /* Success! */
21760         case EIO:
21761                 switch (ucmd_buf.uscsi_status) {
21762                 case STATUS_RESERVATION_CONFLICT:
21763                         status = EACCES;
21764                         break;
21765                 default:
21766                         break;
21767                 }
21768                 break;
21769         default:
21770                 break;
21771         }
21772 
21773         if (status == 0) {
21774                 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data",
21775                     (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21776         }
21777         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n");
21778 
21779         return (status);
21780 }
21781 
21782 
21783 /*
21784  *    Function: sd_send_scsi_RDWR
21785  *
21786  * Description: Issue a scsi READ or WRITE command with the given parameters.
21787  *
21788  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21789  *                      structure for this target.
21790  *              cmd:     SCMD_READ or SCMD_WRITE
21791  *              bufaddr: Address of caller's buffer to receive the RDWR data
21792  *              buflen:  Length of caller's buffer receive the RDWR data.
21793  *              start_block: Block number for the start of the RDWR operation.
21794  *                       (Assumes target-native block size.)
21795  *              residp:  Pointer to variable to receive the redisual of the
21796  *                       RDWR operation (may be NULL of no residual requested).
21797  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21798  *                      the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21799  *                      to use the USCSI "direct" chain and bypass the normal
21800  *                      command waitq.
21801  *
21802  * Return Code: 0   - Success
21803  *              errno return code from sd_ssc_send()
21804  *
21805  *     Context: Can sleep. Does not return until command is completed.
21806  */
21807 
21808 static int
21809 sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
21810     size_t buflen, daddr_t start_block, int path_flag)
21811 {
21812         struct  scsi_extended_sense     sense_buf;
21813         union scsi_cdb          cdb;
21814         struct uscsi_cmd        ucmd_buf;
21815         uint32_t                block_count;
21816         int                     status;
21817         int                     cdbsize;
21818         uchar_t                 flag;
21819         struct sd_lun           *un;
21820 
21821         ASSERT(ssc != NULL);
21822         un = ssc->ssc_un;
21823         ASSERT(un != NULL);
21824         ASSERT(!mutex_owned(SD_MUTEX(un)));
21825         ASSERT(bufaddr != NULL);
21826         ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE));
21827 
21828         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un);
21829 
21830         if (un->un_f_tgt_blocksize_is_valid != TRUE) {
21831                 return (EINVAL);
21832         }
21833 
21834         mutex_enter(SD_MUTEX(un));
21835         block_count = SD_BYTES2TGTBLOCKS(un, buflen);
21836         mutex_exit(SD_MUTEX(un));
21837 
21838         flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE;
21839 
21840         SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: "
21841             "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n",
21842             bufaddr, buflen, start_block, block_count);
21843 
21844         bzero(&cdb, sizeof (cdb));
21845         bzero(&ucmd_buf, sizeof (ucmd_buf));
21846         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21847 
21848         /* Compute CDB size to use */
21849         if (start_block > 0xffffffff)
21850                 cdbsize = CDB_GROUP4;
21851         else if ((start_block & 0xFFE00000) ||
21852             (un->un_f_cfg_is_atapi == TRUE))
21853                 cdbsize = CDB_GROUP1;
21854         else
21855                 cdbsize = CDB_GROUP0;
21856 
21857         switch (cdbsize) {
21858         case CDB_GROUP0:        /* 6-byte CDBs */
21859                 cdb.scc_cmd = cmd;
21860                 FORMG0ADDR(&cdb, start_block);
21861                 FORMG0COUNT(&cdb, block_count);
21862                 break;
21863         case CDB_GROUP1:        /* 10-byte CDBs */
21864                 cdb.scc_cmd = cmd | SCMD_GROUP1;
21865                 FORMG1ADDR(&cdb, start_block);
21866                 FORMG1COUNT(&cdb, block_count);
21867                 break;
21868         case CDB_GROUP4:        /* 16-byte CDBs */
21869                 cdb.scc_cmd = cmd | SCMD_GROUP4;
21870                 FORMG4LONGADDR(&cdb, (uint64_t)start_block);
21871                 FORMG4COUNT(&cdb, block_count);
21872                 break;
21873         case CDB_GROUP5:        /* 12-byte CDBs (currently unsupported) */
21874         default:
21875                 /* All others reserved */
21876                 return (EINVAL);
21877         }
21878 
21879         /* Set LUN bit(s) in CDB if this is a SCSI-1 device */
21880         SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21881 
21882         ucmd_buf.uscsi_cdb      = (char *)&cdb;
21883         ucmd_buf.uscsi_cdblen   = (uchar_t)cdbsize;
21884         ucmd_buf.uscsi_bufaddr  = bufaddr;
21885         ucmd_buf.uscsi_buflen   = buflen;
21886         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
21887         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
21888         ucmd_buf.uscsi_flags    = flag | USCSI_RQENABLE | USCSI_SILENT;
21889         ucmd_buf.uscsi_timeout  = 60;
21890         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21891             UIO_SYSSPACE, path_flag);
21892 
21893         switch (status) {
21894         case 0:
21895                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21896                 break;  /* Success! */
21897         case EIO:
21898                 switch (ucmd_buf.uscsi_status) {
21899                 case STATUS_RESERVATION_CONFLICT:
21900                         status = EACCES;
21901                         break;
21902                 default:
21903                         break;
21904                 }
21905                 break;
21906         default:
21907                 break;
21908         }
21909 
21910         if (status == 0) {
21911                 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data",
21912                     (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21913         }
21914 
21915         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n");
21916 
21917         return (status);
21918 }
21919 
21920 
21921 /*
21922  *    Function: sd_send_scsi_LOG_SENSE
21923  *
21924  * Description: Issue a scsi LOG_SENSE command with the given parameters.
21925  *
21926  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21927  *                      structure for this target.
21928  *
21929  * Return Code: 0   - Success
21930  *              errno return code from sd_ssc_send()
21931  *
21932  *     Context: Can sleep. Does not return until command is completed.
21933  */
21934 
21935 static int
21936 sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr, uint16_t buflen,
21937     uchar_t page_code, uchar_t page_control, uint16_t param_ptr, int path_flag)
21938 {
21939         struct scsi_extended_sense      sense_buf;
21940         union scsi_cdb          cdb;
21941         struct uscsi_cmd        ucmd_buf;
21942         int                     status;
21943         struct sd_lun           *un;
21944 
21945         ASSERT(ssc != NULL);
21946         un = ssc->ssc_un;
21947         ASSERT(un != NULL);
21948         ASSERT(!mutex_owned(SD_MUTEX(un)));
21949 
21950         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un);
21951 
21952         bzero(&cdb, sizeof (cdb));
21953         bzero(&ucmd_buf, sizeof (ucmd_buf));
21954         bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21955 
21956         cdb.scc_cmd = SCMD_LOG_SENSE_G1;
21957         cdb.cdb_opaque[2] = (page_control << 6) | page_code;
21958         cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8);
21959         cdb.cdb_opaque[6] = (uchar_t)(param_ptr  & 0x00FF);
21960         FORMG1COUNT(&cdb, buflen);
21961 
21962         ucmd_buf.uscsi_cdb      = (char *)&cdb;
21963         ucmd_buf.uscsi_cdblen   = CDB_GROUP1;
21964         ucmd_buf.uscsi_bufaddr  = (caddr_t)bufaddr;
21965         ucmd_buf.uscsi_buflen   = buflen;
21966         ucmd_buf.uscsi_rqbuf    = (caddr_t)&sense_buf;
21967         ucmd_buf.uscsi_rqlen    = sizeof (struct scsi_extended_sense);
21968         ucmd_buf.uscsi_flags    = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21969         ucmd_buf.uscsi_timeout  = 60;
21970 
21971         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21972             UIO_SYSSPACE, path_flag);
21973 
21974         switch (status) {
21975         case 0:
21976                 break;
21977         case EIO:
21978                 switch (ucmd_buf.uscsi_status) {
21979                 case STATUS_RESERVATION_CONFLICT:
21980                         status = EACCES;
21981                         break;
21982                 case STATUS_CHECK:
21983                         if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
21984                             (scsi_sense_key((uint8_t *)&sense_buf) ==
21985                             KEY_ILLEGAL_REQUEST) &&
21986                             (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) {
21987                                 /*
21988                                  * ASC 0x24: INVALID FIELD IN CDB
21989                                  */
21990                                 switch (page_code) {
21991                                 case START_STOP_CYCLE_PAGE:
21992                                         /*
21993                                          * The start stop cycle counter is
21994                                          * implemented as page 0x31 in earlier
21995                                          * generation disks. In new generation
21996                                          * disks the start stop cycle counter is
21997                                          * implemented as page 0xE. To properly
21998                                          * handle this case if an attempt for
21999                                          * log page 0xE is made and fails we
22000                                          * will try again using page 0x31.
22001                                          *
22002                                          * Network storage BU committed to
22003                                          * maintain the page 0x31 for this
22004                                          * purpose and will not have any other
22005                                          * page implemented with page code 0x31
22006                                          * until all disks transition to the
22007                                          * standard page.
22008                                          */
22009                                         mutex_enter(SD_MUTEX(un));
22010                                         un->un_start_stop_cycle_page =
22011                                             START_STOP_CYCLE_VU_PAGE;
22012                                         cdb.cdb_opaque[2] =
22013                                             (char)(page_control << 6) |
22014                                             un->un_start_stop_cycle_page;
22015                                         mutex_exit(SD_MUTEX(un));
22016                                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
22017                                         status = sd_ssc_send(
22018                                             ssc, &ucmd_buf, FKIOCTL,
22019                                             UIO_SYSSPACE, path_flag);
22020 
22021                                         break;
22022                                 case TEMPERATURE_PAGE:
22023                                         status = ENOTTY;
22024                                         break;
22025                                 default:
22026                                         break;
22027                                 }
22028                         }
22029                         break;
22030                 default:
22031                         break;
22032                 }
22033                 break;
22034         default:
22035                 break;
22036         }
22037 
22038         if (status == 0) {
22039                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22040                 SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data",
22041                     (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
22042         }
22043 
22044         SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n");
22045 
22046         return (status);
22047 }
22048 
22049 
22050 /*
22051  *    Function: sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
22052  *
22053  * Description: Issue the scsi GET EVENT STATUS NOTIFICATION command.
22054  *
22055  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
22056  *                      structure for this target.
22057  *              bufaddr
22058  *              buflen
22059  *              class_req
22060  *
22061  * Return Code: 0   - Success
22062  *              errno return code from sd_ssc_send()
22063  *
22064  *     Context: Can sleep. Does not return until command is completed.
22065  */
22066 
22067 static int
22068 sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc, uchar_t *bufaddr,
22069     size_t buflen, uchar_t class_req)
22070 {
22071         union scsi_cdb          cdb;
22072         struct uscsi_cmd        ucmd_buf;
22073         int                     status;
22074         struct sd_lun           *un;
22075 
22076         ASSERT(ssc != NULL);
22077         un = ssc->ssc_un;
22078         ASSERT(un != NULL);
22079         ASSERT(!mutex_owned(SD_MUTEX(un)));
22080         ASSERT(bufaddr != NULL);
22081 
22082         SD_TRACE(SD_LOG_IO, un,
22083             "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: entry: un:0x%p\n", un);
22084 
22085         bzero(&cdb, sizeof (cdb));
22086         bzero(&ucmd_buf, sizeof (ucmd_buf));
22087         bzero(bufaddr, buflen);
22088 
22089         cdb.scc_cmd = SCMD_GET_EVENT_STATUS_NOTIFICATION;
22090         cdb.cdb_opaque[1] = 1; /* polled */
22091         cdb.cdb_opaque[4] = class_req;
22092         FORMG1COUNT(&cdb, buflen);
22093 
22094         ucmd_buf.uscsi_cdb      = (char *)&cdb;
22095         ucmd_buf.uscsi_cdblen   = CDB_GROUP1;
22096         ucmd_buf.uscsi_bufaddr  = (caddr_t)bufaddr;
22097         ucmd_buf.uscsi_buflen   = buflen;
22098         ucmd_buf.uscsi_rqbuf    = NULL;
22099         ucmd_buf.uscsi_rqlen    = 0;
22100         ucmd_buf.uscsi_flags    = USCSI_READ | USCSI_SILENT;
22101         ucmd_buf.uscsi_timeout  = 60;
22102 
22103         status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
22104             UIO_SYSSPACE, SD_PATH_DIRECT);
22105 
22106         /*
22107          * Only handle status == 0, the upper-level caller
22108          * will put different assessment based on the context.
22109          */
22110         if (status == 0) {
22111                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22112 
22113                 if (ucmd_buf.uscsi_resid != 0) {
22114                         status = EIO;
22115                 }
22116         }
22117 
22118         SD_TRACE(SD_LOG_IO, un,
22119             "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: exit\n");
22120 
22121         return (status);
22122 }
22123 
22124 
22125 static boolean_t
22126 sd_gesn_media_data_valid(uchar_t *data)
22127 {
22128         uint16_t                        len;
22129 
22130         len = (data[1] << 8) | data[0];
22131         return ((len >= 6) &&
22132             ((data[2] & SD_GESN_HEADER_NEA) == 0) &&
22133             ((data[2] & SD_GESN_HEADER_CLASS) == SD_GESN_MEDIA_CLASS) &&
22134             ((data[3] & (1 << SD_GESN_MEDIA_CLASS)) != 0));
22135 }
22136 
22137 
22138 /*
22139  *    Function: sdioctl
22140  *
22141  * Description: Driver's ioctl(9e) entry point function.
22142  *
22143  *   Arguments: dev     - device number
22144  *              cmd     - ioctl operation to be performed
22145  *              arg     - user argument, contains data to be set or reference
22146  *                        parameter for get
22147  *              flag    - bit flag, indicating open settings, 32/64 bit type
22148  *              cred_p  - user credential pointer
22149  *              rval_p  - calling process return value (OPT)
22150  *
22151  * Return Code: EINVAL
22152  *              ENOTTY
22153  *              ENXIO
22154  *              EIO
22155  *              EFAULT
22156  *              ENOTSUP
22157  *              EPERM
22158  *
22159  *     Context: Called from the device switch at normal priority.
22160  */
22161 
22162 static int
22163 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p)
22164 {
22165         struct sd_lun   *un = NULL;
22166         int             err = 0;
22167         int             i = 0;
22168         cred_t          *cr;
22169         int             tmprval = EINVAL;
22170         boolean_t       is_valid;
22171         sd_ssc_t        *ssc;
22172 
22173         /*
22174          * All device accesses go thru sdstrategy where we check on suspend
22175          * status
22176          */
22177         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22178                 return (ENXIO);
22179         }
22180 
22181         ASSERT(!mutex_owned(SD_MUTEX(un)));
22182 
22183         /* Initialize sd_ssc_t for internal uscsi commands */
22184         ssc = sd_ssc_init(un);
22185 
22186         is_valid = SD_IS_VALID_LABEL(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                 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
22198         }
22199         /*
22200          * Twiddling the counter here protects commands from now
22201          * through to the top of sd_uscsi_strategy. Without the
22202          * counter inc. a power down, for example, could get in
22203          * after the above check for state is made and before
22204          * execution gets to the top of sd_uscsi_strategy.
22205          * That would cause problems.
22206          */
22207         un->un_ncmds_in_driver++;
22208 
22209         if (!is_valid &&
22210             (flag & (FNDELAY | FNONBLOCK))) {
22211                 switch (cmd) {
22212                 case DKIOCGGEOM:        /* SD_PATH_DIRECT */
22213                 case DKIOCGVTOC:
22214                 case DKIOCGEXTVTOC:
22215                 case DKIOCGAPART:
22216                 case DKIOCPARTINFO:
22217                 case DKIOCEXTPARTINFO:
22218                 case DKIOCSGEOM:
22219                 case DKIOCSAPART:
22220                 case DKIOCGETEFI:
22221                 case DKIOCPARTITION:
22222                 case DKIOCSVTOC:
22223                 case DKIOCSEXTVTOC:
22224                 case DKIOCSETEFI:
22225                 case DKIOCGMBOOT:
22226                 case DKIOCSMBOOT:
22227                 case DKIOCG_PHYGEOM:
22228                 case DKIOCG_VIRTGEOM:
22229 #if defined(__i386) || defined(__amd64)
22230                 case DKIOCSETEXTPART:
22231 #endif
22232                         /* let cmlb handle it */
22233                         goto skip_ready_valid;
22234 
22235                 case CDROMPAUSE:
22236                 case CDROMRESUME:
22237                 case CDROMPLAYMSF:
22238                 case CDROMPLAYTRKIND:
22239                 case CDROMREADTOCHDR:
22240                 case CDROMREADTOCENTRY:
22241                 case CDROMSTOP:
22242                 case CDROMSTART:
22243                 case CDROMVOLCTRL:
22244                 case CDROMSUBCHNL:
22245                 case CDROMREADMODE2:
22246                 case CDROMREADMODE1:
22247                 case CDROMREADOFFSET:
22248                 case CDROMSBLKMODE:
22249                 case CDROMGBLKMODE:
22250                 case CDROMGDRVSPEED:
22251                 case CDROMSDRVSPEED:
22252                 case CDROMCDDA:
22253                 case CDROMCDXA:
22254                 case CDROMSUBCODE:
22255                         if (!ISCD(un)) {
22256                                 un->un_ncmds_in_driver--;
22257                                 ASSERT(un->un_ncmds_in_driver >= 0);
22258                                 mutex_exit(SD_MUTEX(un));
22259                                 err = ENOTTY;
22260                                 goto done_without_assess;
22261                         }
22262                         break;
22263                 case FDEJECT:
22264                 case DKIOCEJECT:
22265                 case CDROMEJECT:
22266                         if (!un->un_f_eject_media_supported) {
22267                                 un->un_ncmds_in_driver--;
22268                                 ASSERT(un->un_ncmds_in_driver >= 0);
22269                                 mutex_exit(SD_MUTEX(un));
22270                                 err = ENOTTY;
22271                                 goto done_without_assess;
22272                         }
22273                         break;
22274                 case DKIOCFLUSHWRITECACHE:
22275                         mutex_exit(SD_MUTEX(un));
22276                         err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22277                         if (err != 0) {
22278                                 mutex_enter(SD_MUTEX(un));
22279                                 un->un_ncmds_in_driver--;
22280                                 ASSERT(un->un_ncmds_in_driver >= 0);
22281                                 mutex_exit(SD_MUTEX(un));
22282                                 err = EIO;
22283                                 goto done_quick_assess;
22284                         }
22285                         mutex_enter(SD_MUTEX(un));
22286                         /* FALLTHROUGH */
22287                 case DKIOCREMOVABLE:
22288                 case DKIOCHOTPLUGGABLE:
22289                 case DKIOCINFO:
22290                 case DKIOCGMEDIAINFO:
22291                 case DKIOCGMEDIAINFOEXT:
22292                 case DKIOCSOLIDSTATE:
22293                 case MHIOCENFAILFAST:
22294                 case MHIOCSTATUS:
22295                 case MHIOCTKOWN:
22296                 case MHIOCRELEASE:
22297                 case MHIOCGRP_INKEYS:
22298                 case MHIOCGRP_INRESV:
22299                 case MHIOCGRP_REGISTER:
22300                 case MHIOCGRP_CLEAR:
22301                 case MHIOCGRP_RESERVE:
22302                 case MHIOCGRP_PREEMPTANDABORT:
22303                 case MHIOCGRP_REGISTERANDIGNOREKEY:
22304                 case CDROMCLOSETRAY:
22305                 case USCSICMD:
22306                         goto skip_ready_valid;
22307                 default:
22308                         break;
22309                 }
22310 
22311                 mutex_exit(SD_MUTEX(un));
22312                 err = sd_ready_and_valid(ssc, SDPART(dev));
22313                 mutex_enter(SD_MUTEX(un));
22314 
22315                 if (err != SD_READY_VALID) {
22316                         switch (cmd) {
22317                         case DKIOCSTATE:
22318                         case CDROMGDRVSPEED:
22319                         case CDROMSDRVSPEED:
22320                         case FDEJECT:   /* for eject command */
22321                         case DKIOCEJECT:
22322                         case CDROMEJECT:
22323                         case DKIOCREMOVABLE:
22324                         case DKIOCHOTPLUGGABLE:
22325                                 break;
22326                         default:
22327                                 if (un->un_f_has_removable_media) {
22328                                         err = ENXIO;
22329                                 } else {
22330                                 /* Do not map SD_RESERVED_BY_OTHERS to EIO */
22331                                         if (err == SD_RESERVED_BY_OTHERS) {
22332                                                 err = EACCES;
22333                                         } else {
22334                                                 err = EIO;
22335                                         }
22336                                 }
22337                                 un->un_ncmds_in_driver--;
22338                                 ASSERT(un->un_ncmds_in_driver >= 0);
22339                                 mutex_exit(SD_MUTEX(un));
22340 
22341                                 goto done_without_assess;
22342                         }
22343                 }
22344         }
22345 
22346 skip_ready_valid:
22347         mutex_exit(SD_MUTEX(un));
22348 
22349         switch (cmd) {
22350         case DKIOCINFO:
22351                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n");
22352                 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag);
22353                 break;
22354 
22355         case DKIOCGMEDIAINFO:
22356                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n");
22357                 err = sd_get_media_info(dev, (caddr_t)arg, flag);
22358                 break;
22359 
22360         case DKIOCGMEDIAINFOEXT:
22361                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFOEXT\n");
22362                 err = sd_get_media_info_ext(dev, (caddr_t)arg, flag);
22363                 break;
22364 
22365         case DKIOCGGEOM:
22366         case DKIOCGVTOC:
22367         case DKIOCGEXTVTOC:
22368         case DKIOCGAPART:
22369         case DKIOCPARTINFO:
22370         case DKIOCEXTPARTINFO:
22371         case DKIOCSGEOM:
22372         case DKIOCSAPART:
22373         case DKIOCGETEFI:
22374         case DKIOCPARTITION:
22375         case DKIOCSVTOC:
22376         case DKIOCSEXTVTOC:
22377         case DKIOCSETEFI:
22378         case DKIOCGMBOOT:
22379         case DKIOCSMBOOT:
22380         case DKIOCG_PHYGEOM:
22381         case DKIOCG_VIRTGEOM:
22382 #if defined(__i386) || defined(__amd64)
22383         case DKIOCSETEXTPART:
22384 #endif
22385                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd);
22386 
22387                 /* TUR should spin up */
22388 
22389                 if (un->un_f_has_removable_media)
22390                         err = sd_send_scsi_TEST_UNIT_READY(ssc,
22391                             SD_CHECK_FOR_MEDIA);
22392 
22393                 else
22394                         err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22395 
22396                 if (err != 0)
22397                         goto done_with_assess;
22398 
22399                 err = cmlb_ioctl(un->un_cmlbhandle, dev,
22400                     cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT);
22401 
22402                 if ((err == 0) &&
22403                     ((cmd == DKIOCSETEFI) ||
22404                     ((un->un_f_pkstats_enabled) &&
22405                     (cmd == DKIOCSAPART || cmd == DKIOCSVTOC ||
22406                     cmd == DKIOCSEXTVTOC)))) {
22407 
22408                         tmprval = cmlb_validate(un->un_cmlbhandle, CMLB_SILENT,
22409                             (void *)SD_PATH_DIRECT);
22410                         if ((tmprval == 0) && un->un_f_pkstats_enabled) {
22411                                 sd_set_pstats(un);
22412                                 SD_TRACE(SD_LOG_IO_PARTITION, un,
22413                                     "sd_ioctl: un:0x%p pstats created and "
22414                                     "set\n", un);
22415                         }
22416                 }
22417 
22418                 if ((cmd == DKIOCSVTOC || cmd == DKIOCSEXTVTOC) ||
22419                     ((cmd == DKIOCSETEFI) && (tmprval == 0))) {
22420 
22421                         mutex_enter(SD_MUTEX(un));
22422                         if (un->un_f_devid_supported &&
22423                             (un->un_f_opt_fab_devid == TRUE)) {
22424                                 if (un->un_devid == NULL) {
22425                                         sd_register_devid(ssc, SD_DEVINFO(un),
22426                                             SD_TARGET_IS_UNRESERVED);
22427                                 } else {
22428                                         /*
22429                                          * The device id for this disk
22430                                          * has been fabricated. The
22431                                          * device id must be preserved
22432                                          * by writing it back out to
22433                                          * disk.
22434                                          */
22435                                         if (sd_write_deviceid(ssc) != 0) {
22436                                                 ddi_devid_free(un->un_devid);
22437                                                 un->un_devid = NULL;
22438                                         }
22439                                 }
22440                         }
22441                         mutex_exit(SD_MUTEX(un));
22442                 }
22443 
22444                 break;
22445 
22446         case DKIOCLOCK:
22447                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n");
22448                 err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
22449                     SD_PATH_STANDARD);
22450                 goto done_with_assess;
22451 
22452         case DKIOCUNLOCK:
22453                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n");
22454                 err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
22455                     SD_PATH_STANDARD);
22456                 goto done_with_assess;
22457 
22458         case DKIOCSTATE: {
22459                 enum dkio_state         state;
22460                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n");
22461 
22462                 if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) {
22463                         err = EFAULT;
22464                 } else {
22465                         err = sd_check_media(dev, state);
22466                         if (err == 0) {
22467                                 if (ddi_copyout(&un->un_mediastate, (void *)arg,
22468                                     sizeof (int), flag) != 0)
22469                                         err = EFAULT;
22470                         }
22471                 }
22472                 break;
22473         }
22474 
22475         case DKIOCREMOVABLE:
22476                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n");
22477                 i = un->un_f_has_removable_media ? 1 : 0;
22478                 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22479                         err = EFAULT;
22480                 } else {
22481                         err = 0;
22482                 }
22483                 break;
22484 
22485         case DKIOCSOLIDSTATE:
22486                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSOLIDSTATE\n");
22487                 i = un->un_f_is_solid_state ? 1 : 0;
22488                 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22489                         err = EFAULT;
22490                 } else {
22491                         err = 0;
22492                 }
22493                 break;
22494 
22495         case DKIOCHOTPLUGGABLE:
22496                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n");
22497                 i = un->un_f_is_hotpluggable ? 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 DKIOCREADONLY:
22506                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREADONLY\n");
22507                 i = 0;
22508                 if ((ISCD(un) && !un->un_f_mmc_writable_media) ||
22509                     (sr_check_wp(dev) != 0)) {
22510                         i = 1;
22511                 }
22512                 if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22513                         err = EFAULT;
22514                 } else {
22515                         err = 0;
22516                 }
22517                 break;
22518 
22519         case DKIOCGTEMPERATURE:
22520                 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n");
22521                 err = sd_dkio_get_temp(dev, (caddr_t)arg, flag);
22522                 break;
22523 
22524         case MHIOCENFAILFAST:
22525                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n");
22526                 if ((err = drv_priv(cred_p)) == 0) {
22527                         err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag);
22528                 }
22529                 break;
22530 
22531         case MHIOCTKOWN:
22532                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n");
22533                 if ((err = drv_priv(cred_p)) == 0) {
22534                         err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag);
22535                 }
22536                 break;
22537 
22538         case MHIOCRELEASE:
22539                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n");
22540                 if ((err = drv_priv(cred_p)) == 0) {
22541                         err = sd_mhdioc_release(dev);
22542                 }
22543                 break;
22544 
22545         case MHIOCSTATUS:
22546                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n");
22547                 if ((err = drv_priv(cred_p)) == 0) {
22548                         switch (sd_send_scsi_TEST_UNIT_READY(ssc, 0)) {
22549                         case 0:
22550                                 err = 0;
22551                                 break;
22552                         case EACCES:
22553                                 *rval_p = 1;
22554                                 err = 0;
22555                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
22556                                 break;
22557                         default:
22558                                 err = EIO;
22559                                 goto done_with_assess;
22560                         }
22561                 }
22562                 break;
22563 
22564         case MHIOCQRESERVE:
22565                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n");
22566                 if ((err = drv_priv(cred_p)) == 0) {
22567                         err = sd_reserve_release(dev, SD_RESERVE);
22568                 }
22569                 break;
22570 
22571         case MHIOCREREGISTERDEVID:
22572                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n");
22573                 if (drv_priv(cred_p) == EPERM) {
22574                         err = EPERM;
22575                 } else if (!un->un_f_devid_supported) {
22576                         err = ENOTTY;
22577                 } else {
22578                         err = sd_mhdioc_register_devid(dev);
22579                 }
22580                 break;
22581 
22582         case MHIOCGRP_INKEYS:
22583                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n");
22584                 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
22585                         if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22586                                 err = ENOTSUP;
22587                         } else {
22588                                 err = sd_mhdioc_inkeys(dev, (caddr_t)arg,
22589                                     flag);
22590                         }
22591                 }
22592                 break;
22593 
22594         case MHIOCGRP_INRESV:
22595                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n");
22596                 if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
22597                         if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22598                                 err = ENOTSUP;
22599                         } else {
22600                                 err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag);
22601                         }
22602                 }
22603                 break;
22604 
22605         case MHIOCGRP_REGISTER:
22606                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n");
22607                 if ((err = drv_priv(cred_p)) != EPERM) {
22608                         if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22609                                 err = ENOTSUP;
22610                         } else if (arg != NULL) {
22611                                 mhioc_register_t reg;
22612                                 if (ddi_copyin((void *)arg, &reg,
22613                                     sizeof (mhioc_register_t), flag) != 0) {
22614                                         err = EFAULT;
22615                                 } else {
22616                                         err =
22617                                             sd_send_scsi_PERSISTENT_RESERVE_OUT(
22618                                             ssc, SD_SCSI3_REGISTER,
22619                                             (uchar_t *)&reg);
22620                                         if (err != 0)
22621                                                 goto done_with_assess;
22622                                 }
22623                         }
22624                 }
22625                 break;
22626 
22627         case MHIOCGRP_CLEAR:
22628                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_CLEAR\n");
22629                 if ((err = drv_priv(cred_p)) != EPERM) {
22630                         if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22631                                 err = ENOTSUP;
22632                         } else if (arg != NULL) {
22633                                 mhioc_register_t reg;
22634                                 if (ddi_copyin((void *)arg, &reg,
22635                                     sizeof (mhioc_register_t), flag) != 0) {
22636                                         err = EFAULT;
22637                                 } else {
22638                                         err =
22639                                             sd_send_scsi_PERSISTENT_RESERVE_OUT(
22640                                             ssc, SD_SCSI3_CLEAR,
22641                                             (uchar_t *)&reg);
22642                                         if (err != 0)
22643                                                 goto done_with_assess;
22644                                 }
22645                         }
22646                 }
22647                 break;
22648 
22649         case MHIOCGRP_RESERVE:
22650                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n");
22651                 if ((err = drv_priv(cred_p)) != EPERM) {
22652                         if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22653                                 err = ENOTSUP;
22654                         } else if (arg != NULL) {
22655                                 mhioc_resv_desc_t resv_desc;
22656                                 if (ddi_copyin((void *)arg, &resv_desc,
22657                                     sizeof (mhioc_resv_desc_t), flag) != 0) {
22658                                         err = EFAULT;
22659                                 } else {
22660                                         err =
22661                                             sd_send_scsi_PERSISTENT_RESERVE_OUT(
22662                                             ssc, SD_SCSI3_RESERVE,
22663                                             (uchar_t *)&resv_desc);
22664                                         if (err != 0)
22665                                                 goto done_with_assess;
22666                                 }
22667                         }
22668                 }
22669                 break;
22670 
22671         case MHIOCGRP_PREEMPTANDABORT:
22672                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
22673                 if ((err = drv_priv(cred_p)) != EPERM) {
22674                         if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22675                                 err = ENOTSUP;
22676                         } else if (arg != NULL) {
22677                                 mhioc_preemptandabort_t preempt_abort;
22678                                 if (ddi_copyin((void *)arg, &preempt_abort,
22679                                     sizeof (mhioc_preemptandabort_t),
22680                                     flag) != 0) {
22681                                         err = EFAULT;
22682                                 } else {
22683                                         err =
22684                                             sd_send_scsi_PERSISTENT_RESERVE_OUT(
22685                                             ssc, SD_SCSI3_PREEMPTANDABORT,
22686                                             (uchar_t *)&preempt_abort);
22687                                         if (err != 0)
22688                                                 goto done_with_assess;
22689                                 }
22690                         }
22691                 }
22692                 break;
22693 
22694         case MHIOCGRP_REGISTERANDIGNOREKEY:
22695                 SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTERANDIGNOREKEY\n");
22696                 if ((err = drv_priv(cred_p)) != EPERM) {
22697                         if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22698                                 err = ENOTSUP;
22699                         } else if (arg != NULL) {
22700                                 mhioc_registerandignorekey_t r_and_i;
22701                                 if (ddi_copyin((void *)arg, (void *)&r_and_i,
22702                                     sizeof (mhioc_registerandignorekey_t),
22703                                     flag) != 0) {
22704                                         err = EFAULT;
22705                                 } else {
22706                                         err =
22707                                             sd_send_scsi_PERSISTENT_RESERVE_OUT(
22708                                             ssc, SD_SCSI3_REGISTERANDIGNOREKEY,
22709                                             (uchar_t *)&r_and_i);
22710                                         if (err != 0)
22711                                                 goto done_with_assess;
22712                                 }
22713                         }
22714                 }
22715                 break;
22716 
22717         case USCSICMD:
22718                 SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n");
22719                 cr = ddi_get_cred();
22720                 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) {
22721                         err = EPERM;
22722                 } else {
22723                         enum uio_seg    uioseg;
22724 
22725                         uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE :
22726                             UIO_USERSPACE;
22727                         if (un->un_f_format_in_progress == TRUE) {
22728                                 err = EAGAIN;
22729                                 break;
22730                         }
22731 
22732                         err = sd_ssc_send(ssc,
22733                             (struct uscsi_cmd *)arg,
22734                             flag, uioseg, SD_PATH_STANDARD);
22735                         if (err != 0)
22736                                 goto done_with_assess;
22737                         else
22738                                 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22739                 }
22740                 break;
22741 
22742         case CDROMPAUSE:
22743         case CDROMRESUME:
22744                 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n");
22745                 if (!ISCD(un)) {
22746                         err = ENOTTY;
22747                 } else {
22748                         err = sr_pause_resume(dev, cmd);
22749                 }
22750                 break;
22751 
22752         case CDROMPLAYMSF:
22753                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n");
22754                 if (!ISCD(un)) {
22755                         err = ENOTTY;
22756                 } else {
22757                         err = sr_play_msf(dev, (caddr_t)arg, flag);
22758                 }
22759                 break;
22760 
22761         case CDROMPLAYTRKIND:
22762                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n");
22763 #if defined(__i386) || defined(__amd64)
22764                 /*
22765                  * not supported on ATAPI CD drives, use CDROMPLAYMSF instead
22766                  */
22767                 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22768 #else
22769                 if (!ISCD(un)) {
22770 #endif
22771                         err = ENOTTY;
22772                 } else {
22773                         err = sr_play_trkind(dev, (caddr_t)arg, flag);
22774                 }
22775                 break;
22776 
22777         case CDROMREADTOCHDR:
22778                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n");
22779                 if (!ISCD(un)) {
22780                         err = ENOTTY;
22781                 } else {
22782                         err = sr_read_tochdr(dev, (caddr_t)arg, flag);
22783                 }
22784                 break;
22785 
22786         case CDROMREADTOCENTRY:
22787                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n");
22788                 if (!ISCD(un)) {
22789                         err = ENOTTY;
22790                 } else {
22791                         err = sr_read_tocentry(dev, (caddr_t)arg, flag);
22792                 }
22793                 break;
22794 
22795         case CDROMSTOP:
22796                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n");
22797                 if (!ISCD(un)) {
22798                         err = ENOTTY;
22799                 } else {
22800                         err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22801                             SD_TARGET_STOP, SD_PATH_STANDARD);
22802                         goto done_with_assess;
22803                 }
22804                 break;
22805 
22806         case CDROMSTART:
22807                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n");
22808                 if (!ISCD(un)) {
22809                         err = ENOTTY;
22810                 } else {
22811                         err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22812                             SD_TARGET_START, SD_PATH_STANDARD);
22813                         goto done_with_assess;
22814                 }
22815                 break;
22816 
22817         case CDROMCLOSETRAY:
22818                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n");
22819                 if (!ISCD(un)) {
22820                         err = ENOTTY;
22821                 } else {
22822                         err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22823                             SD_TARGET_CLOSE, SD_PATH_STANDARD);
22824                         goto done_with_assess;
22825                 }
22826                 break;
22827 
22828         case FDEJECT:   /* for eject command */
22829         case DKIOCEJECT:
22830         case CDROMEJECT:
22831                 SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n");
22832                 if (!un->un_f_eject_media_supported) {
22833                         err = ENOTTY;
22834                 } else {
22835                         err = sr_eject(dev);
22836                 }
22837                 break;
22838 
22839         case CDROMVOLCTRL:
22840                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n");
22841                 if (!ISCD(un)) {
22842                         err = ENOTTY;
22843                 } else {
22844                         err = sr_volume_ctrl(dev, (caddr_t)arg, flag);
22845                 }
22846                 break;
22847 
22848         case CDROMSUBCHNL:
22849                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n");
22850                 if (!ISCD(un)) {
22851                         err = ENOTTY;
22852                 } else {
22853                         err = sr_read_subchannel(dev, (caddr_t)arg, flag);
22854                 }
22855                 break;
22856 
22857         case CDROMREADMODE2:
22858                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n");
22859                 if (!ISCD(un)) {
22860                         err = ENOTTY;
22861                 } else if (un->un_f_cfg_is_atapi == TRUE) {
22862                         /*
22863                          * If the drive supports READ CD, use that instead of
22864                          * switching the LBA size via a MODE SELECT
22865                          * Block Descriptor
22866                          */
22867                         err = sr_read_cd_mode2(dev, (caddr_t)arg, flag);
22868                 } else {
22869                         err = sr_read_mode2(dev, (caddr_t)arg, flag);
22870                 }
22871                 break;
22872 
22873         case CDROMREADMODE1:
22874                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n");
22875                 if (!ISCD(un)) {
22876                         err = ENOTTY;
22877                 } else {
22878                         err = sr_read_mode1(dev, (caddr_t)arg, flag);
22879                 }
22880                 break;
22881 
22882         case CDROMREADOFFSET:
22883                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n");
22884                 if (!ISCD(un)) {
22885                         err = ENOTTY;
22886                 } else {
22887                         err = sr_read_sony_session_offset(dev, (caddr_t)arg,
22888                             flag);
22889                 }
22890                 break;
22891 
22892         case CDROMSBLKMODE:
22893                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n");
22894                 /*
22895                  * There is no means of changing block size in case of atapi
22896                  * drives, thus return ENOTTY if drive type is atapi
22897                  */
22898                 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22899                         err = ENOTTY;
22900                 } else if (un->un_f_mmc_cap == TRUE) {
22901 
22902                         /*
22903                          * MMC Devices do not support changing the
22904                          * logical block size
22905                          *
22906                          * Note: EINVAL is being returned instead of ENOTTY to
22907                          * maintain consistancy with the original mmc
22908                          * driver update.
22909                          */
22910                         err = EINVAL;
22911                 } else {
22912                         mutex_enter(SD_MUTEX(un));
22913                         if ((!(un->un_exclopen & (1<<SDPART(dev)))) ||
22914                             (un->un_ncmds_in_transport > 0)) {
22915                                 mutex_exit(SD_MUTEX(un));
22916                                 err = EINVAL;
22917                         } else {
22918                                 mutex_exit(SD_MUTEX(un));
22919                                 err = sr_change_blkmode(dev, cmd, arg, flag);
22920                         }
22921                 }
22922                 break;
22923 
22924         case CDROMGBLKMODE:
22925                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n");
22926                 if (!ISCD(un)) {
22927                         err = ENOTTY;
22928                 } else if ((un->un_f_cfg_is_atapi != FALSE) &&
22929                     (un->un_f_blockcount_is_valid != FALSE)) {
22930                         /*
22931                          * Drive is an ATAPI drive so return target block
22932                          * size for ATAPI drives since we cannot change the
22933                          * blocksize on ATAPI drives. Used primarily to detect
22934                          * if an ATAPI cdrom is present.
22935                          */
22936                         if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg,
22937                             sizeof (int), flag) != 0) {
22938                                 err = EFAULT;
22939                         } else {
22940                                 err = 0;
22941                         }
22942 
22943                 } else {
22944                         /*
22945                          * Drive supports changing block sizes via a Mode
22946                          * Select.
22947                          */
22948                         err = sr_change_blkmode(dev, cmd, arg, flag);
22949                 }
22950                 break;
22951 
22952         case CDROMGDRVSPEED:
22953         case CDROMSDRVSPEED:
22954                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n");
22955                 if (!ISCD(un)) {
22956                         err = ENOTTY;
22957                 } else if (un->un_f_mmc_cap == TRUE) {
22958                         /*
22959                          * Note: In the future the driver implementation
22960                          * for getting and
22961                          * setting cd speed should entail:
22962                          * 1) If non-mmc try the Toshiba mode page
22963                          *    (sr_change_speed)
22964                          * 2) If mmc but no support for Real Time Streaming try
22965                          *    the SET CD SPEED (0xBB) command
22966                          *   (sr_atapi_change_speed)
22967                          * 3) If mmc and support for Real Time Streaming
22968                          *    try the GET PERFORMANCE and SET STREAMING
22969                          *    commands (not yet implemented, 4380808)
22970                          */
22971                         /*
22972                          * As per recent MMC spec, CD-ROM speed is variable
22973                          * and changes with LBA. Since there is no such
22974                          * things as drive speed now, fail this ioctl.
22975                          *
22976                          * Note: EINVAL is returned for consistancy of original
22977                          * implementation which included support for getting
22978                          * the drive speed of mmc devices but not setting
22979                          * the drive speed. Thus EINVAL would be returned
22980                          * if a set request was made for an mmc device.
22981                          * We no longer support get or set speed for
22982                          * mmc but need to remain consistent with regard
22983                          * to the error code returned.
22984                          */
22985                         err = EINVAL;
22986                 } else if (un->un_f_cfg_is_atapi == TRUE) {
22987                         err = sr_atapi_change_speed(dev, cmd, arg, flag);
22988                 } else {
22989                         err = sr_change_speed(dev, cmd, arg, flag);
22990                 }
22991                 break;
22992 
22993         case CDROMCDDA:
22994                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n");
22995                 if (!ISCD(un)) {
22996                         err = ENOTTY;
22997                 } else {
22998                         err = sr_read_cdda(dev, (void *)arg, flag);
22999                 }
23000                 break;
23001 
23002         case CDROMCDXA:
23003                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n");
23004                 if (!ISCD(un)) {
23005                         err = ENOTTY;
23006                 } else {
23007                         err = sr_read_cdxa(dev, (caddr_t)arg, flag);
23008                 }
23009                 break;
23010 
23011         case CDROMSUBCODE:
23012                 SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n");
23013                 if (!ISCD(un)) {
23014                         err = ENOTTY;
23015                 } else {
23016                         err = sr_read_all_subcodes(dev, (caddr_t)arg, flag);
23017                 }
23018                 break;
23019 
23020 
23021 #ifdef SDDEBUG
23022 /* RESET/ABORTS testing ioctls */
23023         case DKIOCRESET: {
23024                 int     reset_level;
23025 
23026                 if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) {
23027                         err = EFAULT;
23028                 } else {
23029                         SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: "
23030                             "reset_level = 0x%lx\n", reset_level);
23031                         if (scsi_reset(SD_ADDRESS(un), reset_level)) {
23032                                 err = 0;
23033                         } else {
23034                                 err = EIO;
23035                         }
23036                 }
23037                 break;
23038         }
23039 
23040         case DKIOCABORT:
23041                 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n");
23042                 if (scsi_abort(SD_ADDRESS(un), NULL)) {
23043                         err = 0;
23044                 } else {
23045                         err = EIO;
23046                 }
23047                 break;
23048 #endif
23049 
23050 #ifdef SD_FAULT_INJECTION
23051 /* SDIOC FaultInjection testing ioctls */
23052         case SDIOCSTART:
23053         case SDIOCSTOP:
23054         case SDIOCINSERTPKT:
23055         case SDIOCINSERTXB:
23056         case SDIOCINSERTUN:
23057         case SDIOCINSERTARQ:
23058         case SDIOCPUSH:
23059         case SDIOCRETRIEVE:
23060         case SDIOCRUN:
23061                 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:"
23062                     "SDIOC detected cmd:0x%X:\n", cmd);
23063                 /* call error generator */
23064                 sd_faultinjection_ioctl(cmd, arg, un);
23065                 err = 0;
23066                 break;
23067 
23068 #endif /* SD_FAULT_INJECTION */
23069 
23070         case DKIOCFLUSHWRITECACHE:
23071                 {
23072                         struct dk_callback *dkc = (struct dk_callback *)arg;
23073 
23074                         mutex_enter(SD_MUTEX(un));
23075                         if (!un->un_f_sync_cache_supported ||
23076                             !un->un_f_write_cache_enabled) {
23077                                 err = un->un_f_sync_cache_supported ?
23078                                     0 : ENOTSUP;
23079                                 mutex_exit(SD_MUTEX(un));
23080                                 if ((flag & FKIOCTL) && dkc != NULL &&
23081                                     dkc->dkc_callback != NULL) {
23082                                         (*dkc->dkc_callback)(dkc->dkc_cookie,
23083                                             err);
23084                                         /*
23085                                          * Did callback and reported error.
23086                                          * Since we did a callback, ioctl
23087                                          * should return 0.
23088                                          */
23089                                         err = 0;
23090                                 }
23091                                 break;
23092                         }
23093                         mutex_exit(SD_MUTEX(un));
23094 
23095                         if ((flag & FKIOCTL) && dkc != NULL &&
23096                             dkc->dkc_callback != NULL) {
23097                                 /* async SYNC CACHE request */
23098                                 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
23099                         } else {
23100                                 /* synchronous SYNC CACHE request */
23101                                 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
23102                         }
23103                 }
23104                 break;
23105 
23106         case DKIOCGETWCE: {
23107 
23108                 int wce;
23109 
23110                 if ((err = sd_get_write_cache_enabled(ssc, &wce)) != 0) {
23111                         break;
23112                 }
23113 
23114                 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) {
23115                         err = EFAULT;
23116                 }
23117                 break;
23118         }
23119 
23120         case DKIOCSETWCE: {
23121 
23122                 int wce, sync_supported;
23123                 int cur_wce = 0;
23124 
23125                 if (!un->un_f_cache_mode_changeable) {
23126                         err = EINVAL;
23127                         break;
23128                 }
23129 
23130                 if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) {
23131                         err = EFAULT;
23132                         break;
23133                 }
23134 
23135                 /*
23136                  * Synchronize multiple threads trying to enable
23137                  * or disable the cache via the un_f_wcc_cv
23138                  * condition variable.
23139                  */
23140                 mutex_enter(SD_MUTEX(un));
23141 
23142                 /*
23143                  * Don't allow the cache to be enabled if the
23144                  * config file has it disabled.
23145                  */
23146                 if (un->un_f_opt_disable_cache && wce) {
23147                         mutex_exit(SD_MUTEX(un));
23148                         err = EINVAL;
23149                         break;
23150                 }
23151 
23152                 /*
23153                  * Wait for write cache change in progress
23154                  * bit to be clear before proceeding.
23155                  */
23156                 while (un->un_f_wcc_inprog)
23157                         cv_wait(&un->un_wcc_cv, SD_MUTEX(un));
23158 
23159                 un->un_f_wcc_inprog = 1;
23160 
23161                 mutex_exit(SD_MUTEX(un));
23162 
23163                 /*
23164                  * Get the current write cache state
23165                  */
23166                 if ((err = sd_get_write_cache_enabled(ssc, &cur_wce)) != 0) {
23167                         mutex_enter(SD_MUTEX(un));
23168                         un->un_f_wcc_inprog = 0;
23169                         cv_broadcast(&un->un_wcc_cv);
23170                         mutex_exit(SD_MUTEX(un));
23171                         break;
23172                 }
23173 
23174                 mutex_enter(SD_MUTEX(un));
23175                 un->un_f_write_cache_enabled = (cur_wce != 0);
23176 
23177                 if (un->un_f_write_cache_enabled && wce == 0) {
23178                         /*
23179                          * Disable the write cache.  Don't clear
23180                          * un_f_write_cache_enabled until after
23181                          * the mode select and flush are complete.
23182                          */
23183                         sync_supported = un->un_f_sync_cache_supported;
23184 
23185                         /*
23186                          * If cache flush is suppressed, we assume that the
23187                          * controller firmware will take care of managing the
23188                          * write cache for us: no need to explicitly
23189                          * disable it.
23190                          */
23191                         if (!un->un_f_suppress_cache_flush) {
23192                                 mutex_exit(SD_MUTEX(un));
23193                                 if ((err = sd_cache_control(ssc,
23194                                     SD_CACHE_NOCHANGE,
23195                                     SD_CACHE_DISABLE)) == 0 &&
23196                                     sync_supported) {
23197                                         err = sd_send_scsi_SYNCHRONIZE_CACHE(un,
23198                                             NULL);
23199                                 }
23200                         } else {
23201                                 mutex_exit(SD_MUTEX(un));
23202                         }
23203 
23204                         mutex_enter(SD_MUTEX(un));
23205                         if (err == 0) {
23206                                 un->un_f_write_cache_enabled = 0;
23207                         }
23208 
23209                 } else if (!un->un_f_write_cache_enabled && wce != 0) {
23210                         /*
23211                          * Set un_f_write_cache_enabled first, so there is
23212                          * no window where the cache is enabled, but the
23213                          * bit says it isn't.
23214                          */
23215                         un->un_f_write_cache_enabled = 1;
23216 
23217                         /*
23218                          * If cache flush is suppressed, we assume that the
23219                          * controller firmware will take care of managing the
23220                          * write cache for us: no need to explicitly
23221                          * enable it.
23222                          */
23223                         if (!un->un_f_suppress_cache_flush) {
23224                                 mutex_exit(SD_MUTEX(un));
23225                                 err = sd_cache_control(ssc, SD_CACHE_NOCHANGE,
23226                                     SD_CACHE_ENABLE);
23227                         } else {
23228                                 mutex_exit(SD_MUTEX(un));
23229                         }
23230 
23231                         mutex_enter(SD_MUTEX(un));
23232 
23233                         if (err) {
23234                                 un->un_f_write_cache_enabled = 0;
23235                         }
23236                 }
23237 
23238                 un->un_f_wcc_inprog = 0;
23239                 cv_broadcast(&un->un_wcc_cv);
23240                 mutex_exit(SD_MUTEX(un));
23241                 break;
23242         }
23243 
23244         default:
23245                 err = ENOTTY;
23246                 break;
23247         }
23248         mutex_enter(SD_MUTEX(un));
23249         un->un_ncmds_in_driver--;
23250         ASSERT(un->un_ncmds_in_driver >= 0);
23251         mutex_exit(SD_MUTEX(un));
23252 
23253 
23254 done_without_assess:
23255         sd_ssc_fini(ssc);
23256 
23257         SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23258         return (err);
23259 
23260 done_with_assess:
23261         mutex_enter(SD_MUTEX(un));
23262         un->un_ncmds_in_driver--;
23263         ASSERT(un->un_ncmds_in_driver >= 0);
23264         mutex_exit(SD_MUTEX(un));
23265 
23266 done_quick_assess:
23267         if (err != 0)
23268                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23269         /* Uninitialize sd_ssc_t pointer */
23270         sd_ssc_fini(ssc);
23271 
23272         SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23273         return (err);
23274 }
23275 
23276 
23277 /*
23278  *    Function: sd_dkio_ctrl_info
23279  *
23280  * Description: This routine is the driver entry point for handling controller
23281  *              information ioctl requests (DKIOCINFO).
23282  *
23283  *   Arguments: dev  - the device number
23284  *              arg  - pointer to user provided dk_cinfo structure
23285  *                     specifying the controller type and attributes.
23286  *              flag - this argument is a pass through to ddi_copyxxx()
23287  *                     directly from the mode argument of ioctl().
23288  *
23289  * Return Code: 0
23290  *              EFAULT
23291  *              ENXIO
23292  */
23293 
23294 static int
23295 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag)
23296 {
23297         struct sd_lun   *un = NULL;
23298         struct dk_cinfo *info;
23299         dev_info_t      *pdip;
23300         int             lun, tgt;
23301 
23302         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23303                 return (ENXIO);
23304         }
23305 
23306         info = (struct dk_cinfo *)
23307             kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
23308 
23309         switch (un->un_ctype) {
23310         case CTYPE_CDROM:
23311                 info->dki_ctype = DKC_CDROM;
23312                 break;
23313         default:
23314                 info->dki_ctype = DKC_SCSI_CCS;
23315                 break;
23316         }
23317         pdip = ddi_get_parent(SD_DEVINFO(un));
23318         info->dki_cnum = ddi_get_instance(pdip);
23319         if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) {
23320                 (void) strcpy(info->dki_cname, ddi_get_name(pdip));
23321         } else {
23322                 (void) strncpy(info->dki_cname, ddi_node_name(pdip),
23323                     DK_DEVLEN - 1);
23324         }
23325 
23326         lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23327             DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0);
23328         tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23329             DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0);
23330 
23331         /* Unit Information */
23332         info->dki_unit = ddi_get_instance(SD_DEVINFO(un));
23333         info->dki_slave = ((tgt << 3) | lun);
23334         (void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)),
23335             DK_DEVLEN - 1);
23336         info->dki_flags = DKI_FMTVOL;
23337         info->dki_partition = SDPART(dev);
23338 
23339         /* Max Transfer size of this device in blocks */
23340         info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize;
23341         info->dki_addr = 0;
23342         info->dki_space = 0;
23343         info->dki_prio = 0;
23344         info->dki_vec = 0;
23345 
23346         if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) {
23347                 kmem_free(info, sizeof (struct dk_cinfo));
23348                 return (EFAULT);
23349         } else {
23350                 kmem_free(info, sizeof (struct dk_cinfo));
23351                 return (0);
23352         }
23353 }
23354 
23355 /*
23356  *    Function: sd_get_media_info_com
23357  *
23358  * Description: This routine returns the information required to populate
23359  *              the fields for the dk_minfo/dk_minfo_ext structures.
23360  *
23361  *   Arguments: dev             - the device number
23362  *              dki_media_type  - media_type
23363  *              dki_lbsize      - logical block size
23364  *              dki_capacity    - capacity in blocks
23365  *              dki_pbsize      - physical block size (if requested)
23366  *
23367  * Return Code: 0
23368  *              EACCESS
23369  *              EFAULT
23370  *              ENXIO
23371  *              EIO
23372  */
23373 static int
23374 sd_get_media_info_com(dev_t dev, uint_t *dki_media_type, uint_t *dki_lbsize,
23375     diskaddr_t *dki_capacity, uint_t *dki_pbsize)
23376 {
23377         struct sd_lun           *un = NULL;
23378         struct uscsi_cmd        com;
23379         struct scsi_inquiry     *sinq;
23380         u_longlong_t            media_capacity;
23381         uint64_t                capacity;
23382         uint_t                  lbasize;
23383         uint_t                  pbsize;
23384         uchar_t                 *out_data;
23385         uchar_t                 *rqbuf;
23386         int                     rval = 0;
23387         int                     rtn;
23388         sd_ssc_t                *ssc;
23389 
23390         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
23391             (un->un_state == SD_STATE_OFFLINE)) {
23392                 return (ENXIO);
23393         }
23394 
23395         SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info_com: entry\n");
23396 
23397         out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
23398         rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
23399         ssc = sd_ssc_init(un);
23400 
23401         /* Issue a TUR to determine if the drive is ready with media present */
23402         rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_CHECK_FOR_MEDIA);
23403         if (rval == ENXIO) {
23404                 goto done;
23405         } else if (rval != 0) {
23406                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23407         }
23408 
23409         /* Now get configuration data */
23410         if (ISCD(un)) {
23411                 *dki_media_type = DK_CDROM;
23412 
23413                 /* Allow SCMD_GET_CONFIGURATION to MMC devices only */
23414                 if (un->un_f_mmc_cap == TRUE) {
23415                         rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf,
23416                             SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN,
23417                             SD_PATH_STANDARD);
23418 
23419                         if (rtn) {
23420                                 /*
23421                                  * We ignore all failures for CD and need to
23422                                  * put the assessment before processing code
23423                                  * to avoid missing assessment for FMA.
23424                                  */
23425                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23426                                 /*
23427                                  * Failed for other than an illegal request
23428                                  * or command not supported
23429                                  */
23430                                 if ((com.uscsi_status == STATUS_CHECK) &&
23431                                     (com.uscsi_rqstatus == STATUS_GOOD)) {
23432                                         if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) ||
23433                                             (rqbuf[12] != 0x20)) {
23434                                                 rval = EIO;
23435                                                 goto no_assessment;
23436                                         }
23437                                 }
23438                         } else {
23439                                 /*
23440                                  * The GET CONFIGURATION command succeeded
23441                                  * so set the media type according to the
23442                                  * returned data
23443                                  */
23444                                 *dki_media_type = out_data[6];
23445                                 *dki_media_type <<= 8;
23446                                 *dki_media_type |= out_data[7];
23447                         }
23448                 }
23449         } else {
23450                 /*
23451                  * The profile list is not available, so we attempt to identify
23452                  * the media type based on the inquiry data
23453                  */
23454                 sinq = un->un_sd->sd_inq;
23455                 if ((sinq->inq_dtype == DTYPE_DIRECT) ||
23456                     (sinq->inq_dtype == DTYPE_OPTICAL)) {
23457                         /* This is a direct access device  or optical disk */
23458                         *dki_media_type = DK_FIXED_DISK;
23459 
23460                         if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) ||
23461                             (bcmp(sinq->inq_vid, "iomega", 6) == 0)) {
23462                                 if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) {
23463                                         *dki_media_type = DK_ZIP;
23464                                 } else if (
23465                                     (bcmp(sinq->inq_pid, "jaz", 3) == 0)) {
23466                                         *dki_media_type = DK_JAZ;
23467                                 }
23468                         }
23469                 } else {
23470                         /*
23471                          * Not a CD, direct access or optical disk so return
23472                          * unknown media
23473                          */
23474                         *dki_media_type = DK_UNKNOWN;
23475                 }
23476         }
23477 
23478         /*
23479          * Now read the capacity so we can provide the lbasize,
23480          * pbsize and capacity.
23481          */
23482         if (dki_pbsize && un->un_f_descr_format_supported) {
23483                 rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
23484                     &pbsize, SD_PATH_DIRECT);
23485 
23486                 /*
23487                  * Override the physical blocksize if the instance already
23488                  * has a larger value.
23489                  */
23490                 pbsize = MAX(pbsize, un->un_phy_blocksize);
23491         }
23492 
23493         if (dki_pbsize == NULL || rval != 0 ||
23494             !un->un_f_descr_format_supported) {
23495                 rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
23496                     SD_PATH_DIRECT);
23497 
23498                 switch (rval) {
23499                 case 0:
23500                         if (un->un_f_enable_rmw &&
23501                             un->un_phy_blocksize != 0) {
23502                                 pbsize = un->un_phy_blocksize;
23503                         } else {
23504                                 pbsize = lbasize;
23505                         }
23506                         media_capacity = capacity;
23507 
23508                         /*
23509                          * sd_send_scsi_READ_CAPACITY() reports capacity in
23510                          * un->un_sys_blocksize chunks. So we need to convert
23511                          * it into cap.lbsize chunks.
23512                          */
23513                         if (un->un_f_has_removable_media) {
23514                                 media_capacity *= un->un_sys_blocksize;
23515                                 media_capacity /= lbasize;
23516                         }
23517                         break;
23518                 case EACCES:
23519                         rval = EACCES;
23520                         goto done;
23521                 default:
23522                         rval = EIO;
23523                         goto done;
23524                 }
23525         } else {
23526                 if (un->un_f_enable_rmw &&
23527                     !ISP2(pbsize % DEV_BSIZE)) {
23528                         pbsize = SSD_SECSIZE;
23529                 } else if (!ISP2(lbasize % DEV_BSIZE) ||
23530                     !ISP2(pbsize % DEV_BSIZE)) {
23531                         pbsize = lbasize = DEV_BSIZE;
23532                 }
23533                 media_capacity = capacity;
23534         }
23535 
23536         /*
23537          * If lun is expanded dynamically, update the un structure.
23538          */
23539         mutex_enter(SD_MUTEX(un));
23540         if ((un->un_f_blockcount_is_valid == TRUE) &&
23541             (un->un_f_tgt_blocksize_is_valid == TRUE) &&
23542             (capacity > un->un_blockcount)) {
23543                 un->un_f_expnevent = B_FALSE;
23544                 sd_update_block_info(un, lbasize, capacity);
23545         }
23546         mutex_exit(SD_MUTEX(un));
23547 
23548         *dki_lbsize = lbasize;
23549         *dki_capacity = media_capacity;
23550         if (dki_pbsize)
23551                 *dki_pbsize = pbsize;
23552 
23553 done:
23554         if (rval != 0) {
23555                 if (rval == EIO)
23556                         sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23557                 else
23558                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23559         }
23560 no_assessment:
23561         sd_ssc_fini(ssc);
23562         kmem_free(out_data, SD_PROFILE_HEADER_LEN);
23563         kmem_free(rqbuf, SENSE_LENGTH);
23564         return (rval);
23565 }
23566 
23567 /*
23568  *    Function: sd_get_media_info
23569  *
23570  * Description: This routine is the driver entry point for handling ioctl
23571  *              requests for the media type or command set profile used by the
23572  *              drive to operate on the media (DKIOCGMEDIAINFO).
23573  *
23574  *   Arguments: dev     - the device number
23575  *              arg     - pointer to user provided dk_minfo structure
23576  *                        specifying the media type, logical block size and
23577  *                        drive capacity.
23578  *              flag    - this argument is a pass through to ddi_copyxxx()
23579  *                        directly from the mode argument of ioctl().
23580  *
23581  * Return Code: returns the value from sd_get_media_info_com
23582  */
23583 static int
23584 sd_get_media_info(dev_t dev, caddr_t arg, int flag)
23585 {
23586         struct dk_minfo         mi;
23587         int                     rval;
23588 
23589         rval = sd_get_media_info_com(dev, &mi.dki_media_type,
23590             &mi.dki_lbsize, &mi.dki_capacity, NULL);
23591 
23592         if (rval)
23593                 return (rval);
23594         if (ddi_copyout(&mi, arg, sizeof (struct dk_minfo), flag))
23595                 rval = EFAULT;
23596         return (rval);
23597 }
23598 
23599 /*
23600  *    Function: sd_get_media_info_ext
23601  *
23602  * Description: This routine is the driver entry point for handling ioctl
23603  *              requests for the media type or command set profile used by the
23604  *              drive to operate on the media (DKIOCGMEDIAINFOEXT). The
23605  *              difference this ioctl and DKIOCGMEDIAINFO is the return value
23606  *              of this ioctl contains both logical block size and physical
23607  *              block size.
23608  *
23609  *
23610  *   Arguments: dev     - the device number
23611  *              arg     - pointer to user provided dk_minfo_ext structure
23612  *                        specifying the media type, logical block size,
23613  *                        physical block size and disk capacity.
23614  *              flag    - this argument is a pass through to ddi_copyxxx()
23615  *                        directly from the mode argument of ioctl().
23616  *
23617  * Return Code: returns the value from sd_get_media_info_com
23618  */
23619 static int
23620 sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag)
23621 {
23622         struct dk_minfo_ext     mie;
23623         int                     rval = 0;
23624 
23625         rval = sd_get_media_info_com(dev, &mie.dki_media_type,
23626             &mie.dki_lbsize, &mie.dki_capacity, &mie.dki_pbsize);
23627 
23628         if (rval)
23629                 return (rval);
23630         if (ddi_copyout(&mie, arg, sizeof (struct dk_minfo_ext), flag))
23631                 rval = EFAULT;
23632         return (rval);
23633 
23634 }
23635 
23636 /*
23637  *    Function: sd_watch_request_submit
23638  *
23639  * Description: Call scsi_watch_request_submit or scsi_mmc_watch_request_submit
23640  *              depending on which is supported by device.
23641  */
23642 static opaque_t
23643 sd_watch_request_submit(struct sd_lun *un)
23644 {
23645         dev_t                   dev;
23646 
23647         /* All submissions are unified to use same device number */
23648         dev = sd_make_device(SD_DEVINFO(un));
23649 
23650         if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
23651                 return (scsi_mmc_watch_request_submit(SD_SCSI_DEVP(un),
23652                     sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
23653                     (caddr_t)dev));
23654         } else {
23655                 return (scsi_watch_request_submit(SD_SCSI_DEVP(un),
23656                     sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
23657                     (caddr_t)dev));
23658         }
23659 }
23660 
23661 
23662 /*
23663  *    Function: sd_check_media
23664  *
23665  * Description: This utility routine implements the functionality for the
23666  *              DKIOCSTATE ioctl. This ioctl blocks the user thread until the
23667  *              driver state changes from that specified by the user
23668  *              (inserted or ejected). For example, if the user specifies
23669  *              DKIO_EJECTED and the current media state is inserted this
23670  *              routine will immediately return DKIO_INSERTED. However, if the
23671  *              current media state is not inserted the user thread will be
23672  *              blocked until the drive state changes. If DKIO_NONE is specified
23673  *              the user thread will block until a drive state change occurs.
23674  *
23675  *   Arguments: dev  - the device number
23676  *              state  - user pointer to a dkio_state, updated with the current
23677  *                      drive state at return.
23678  *
23679  * Return Code: ENXIO
23680  *              EIO
23681  *              EAGAIN
23682  *              EINTR
23683  */
23684 
23685 static int
23686 sd_check_media(dev_t dev, enum dkio_state state)
23687 {
23688         struct sd_lun           *un = NULL;
23689         enum dkio_state         prev_state;
23690         opaque_t                token = NULL;
23691         int                     rval = 0;
23692         sd_ssc_t                *ssc;
23693 
23694         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23695                 return (ENXIO);
23696         }
23697 
23698         SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n");
23699 
23700         ssc = sd_ssc_init(un);
23701 
23702         mutex_enter(SD_MUTEX(un));
23703 
23704         SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: "
23705             "state=%x, mediastate=%x\n", state, un->un_mediastate);
23706 
23707         prev_state = un->un_mediastate;
23708 
23709         /* is there anything to do? */
23710         if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) {
23711                 /*
23712                  * submit the request to the scsi_watch service;
23713                  * scsi_media_watch_cb() does the real work
23714                  */
23715                 mutex_exit(SD_MUTEX(un));
23716 
23717                 /*
23718                  * This change handles the case where a scsi watch request is
23719                  * added to a device that is powered down. To accomplish this
23720                  * we power up the device before adding the scsi watch request,
23721                  * since the scsi watch sends a TUR directly to the device
23722                  * which the device cannot handle if it is powered down.
23723                  */
23724                 if (sd_pm_entry(un) != DDI_SUCCESS) {
23725                         mutex_enter(SD_MUTEX(un));
23726                         goto done;
23727                 }
23728 
23729                 token = sd_watch_request_submit(un);
23730 
23731                 sd_pm_exit(un);
23732 
23733                 mutex_enter(SD_MUTEX(un));
23734                 if (token == NULL) {
23735                         rval = EAGAIN;
23736                         goto done;
23737                 }
23738 
23739                 /*
23740                  * This is a special case IOCTL that doesn't return
23741                  * until the media state changes. Routine sdpower
23742                  * knows about and handles this so don't count it
23743                  * as an active cmd in the driver, which would
23744                  * keep the device busy to the pm framework.
23745                  * If the count isn't decremented the device can't
23746                  * be powered down.
23747                  */
23748                 un->un_ncmds_in_driver--;
23749                 ASSERT(un->un_ncmds_in_driver >= 0);
23750 
23751                 /*
23752                  * if a prior request had been made, this will be the same
23753                  * token, as scsi_watch was designed that way.
23754                  */
23755                 un->un_swr_token = token;
23756                 un->un_specified_mediastate = state;
23757 
23758                 /*
23759                  * now wait for media change
23760                  * we will not be signalled unless mediastate == state but it is
23761                  * still better to test for this condition, since there is a
23762                  * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED
23763                  */
23764                 SD_TRACE(SD_LOG_COMMON, un,
23765                     "sd_check_media: waiting for media state change\n");
23766                 while (un->un_mediastate == state) {
23767                         if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) {
23768                                 SD_TRACE(SD_LOG_COMMON, un,
23769                                     "sd_check_media: waiting for media state "
23770                                     "was interrupted\n");
23771                                 un->un_ncmds_in_driver++;
23772                                 rval = EINTR;
23773                                 goto done;
23774                         }
23775                         SD_TRACE(SD_LOG_COMMON, un,
23776                             "sd_check_media: received signal, state=%x\n",
23777                             un->un_mediastate);
23778                 }
23779                 /*
23780                  * Inc the counter to indicate the device once again
23781                  * has an active outstanding cmd.
23782                  */
23783                 un->un_ncmds_in_driver++;
23784         }
23785 
23786         /* invalidate geometry */
23787         if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) {
23788                 sr_ejected(un);
23789         }
23790 
23791         if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) {
23792                 uint64_t        capacity;
23793                 uint_t          lbasize;
23794 
23795                 SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n");
23796                 mutex_exit(SD_MUTEX(un));
23797                 /*
23798                  * Since the following routines use SD_PATH_DIRECT, we must
23799                  * call PM directly before the upcoming disk accesses. This
23800                  * may cause the disk to be power/spin up.
23801                  */
23802 
23803                 if (sd_pm_entry(un) == DDI_SUCCESS) {
23804                         rval = sd_send_scsi_READ_CAPACITY(ssc,
23805                             &capacity, &lbasize, SD_PATH_DIRECT);
23806                         if (rval != 0) {
23807                                 sd_pm_exit(un);
23808                                 if (rval == EIO)
23809                                         sd_ssc_assessment(ssc,
23810                                             SD_FMT_STATUS_CHECK);
23811                                 else
23812                                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23813                                 mutex_enter(SD_MUTEX(un));
23814                                 goto done;
23815                         }
23816                 } else {
23817                         rval = EIO;
23818                         mutex_enter(SD_MUTEX(un));
23819                         goto done;
23820                 }
23821                 mutex_enter(SD_MUTEX(un));
23822 
23823                 sd_update_block_info(un, lbasize, capacity);
23824 
23825                 /*
23826                  *  Check if the media in the device is writable or not
23827                  */
23828                 if (ISCD(un)) {
23829                         sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
23830                 }
23831 
23832                 mutex_exit(SD_MUTEX(un));
23833                 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
23834                 if ((cmlb_validate(un->un_cmlbhandle, 0,
23835                     (void *)SD_PATH_DIRECT) == 0) && un->un_f_pkstats_enabled) {
23836                         sd_set_pstats(un);
23837                         SD_TRACE(SD_LOG_IO_PARTITION, un,
23838                             "sd_check_media: un:0x%p pstats created and "
23839                             "set\n", un);
23840                 }
23841 
23842                 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
23843                     SD_PATH_DIRECT);
23844 
23845                 sd_pm_exit(un);
23846 
23847                 if (rval != 0) {
23848                         if (rval == EIO)
23849                                 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23850                         else
23851                                 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23852                 }
23853 
23854                 mutex_enter(SD_MUTEX(un));
23855         }
23856 done:
23857         sd_ssc_fini(ssc);
23858         un->un_f_watcht_stopped = FALSE;
23859         if (token != NULL && un->un_swr_token != NULL) {
23860                 /*
23861                  * Use of this local token and the mutex ensures that we avoid
23862                  * some race conditions associated with terminating the
23863                  * scsi watch.
23864                  */
23865                 token = un->un_swr_token;
23866                 mutex_exit(SD_MUTEX(un));
23867                 (void) scsi_watch_request_terminate(token,
23868                     SCSI_WATCH_TERMINATE_WAIT);
23869                 if (scsi_watch_get_ref_count(token) == 0) {
23870                         mutex_enter(SD_MUTEX(un));
23871                         un->un_swr_token = (opaque_t)NULL;
23872                 } else {
23873                         mutex_enter(SD_MUTEX(un));
23874                 }
23875         }
23876 
23877         /*
23878          * Update the capacity kstat value, if no media previously
23879          * (capacity kstat is 0) and a media has been inserted
23880          * (un_f_blockcount_is_valid == TRUE)
23881          */
23882         if (un->un_errstats) {
23883                 struct sd_errstats      *stp = NULL;
23884 
23885                 stp = (struct sd_errstats *)un->un_errstats->ks_data;
23886                 if ((stp->sd_capacity.value.ui64 == 0) &&
23887                     (un->un_f_blockcount_is_valid == TRUE)) {
23888                         stp->sd_capacity.value.ui64 =
23889                             (uint64_t)((uint64_t)un->un_blockcount *
23890                             un->un_sys_blocksize);
23891                 }
23892         }
23893         mutex_exit(SD_MUTEX(un));
23894         SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n");
23895         return (rval);
23896 }
23897 
23898 
23899 /*
23900  *    Function: sd_delayed_cv_broadcast
23901  *
23902  * Description: Delayed cv_broadcast to allow for target to recover from media
23903  *              insertion.
23904  *
23905  *   Arguments: arg - driver soft state (unit) structure
23906  */
23907 
23908 static void
23909 sd_delayed_cv_broadcast(void *arg)
23910 {
23911         struct sd_lun *un = arg;
23912 
23913         SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n");
23914 
23915         mutex_enter(SD_MUTEX(un));
23916         un->un_dcvb_timeid = NULL;
23917         cv_broadcast(&un->un_state_cv);
23918         mutex_exit(SD_MUTEX(un));
23919 }
23920 
23921 
23922 /*
23923  *    Function: sd_media_watch_cb
23924  *
23925  * Description: Callback routine used for support of the DKIOCSTATE ioctl. This
23926  *              routine processes the TUR sense data and updates the driver
23927  *              state if a transition has occurred. The user thread
23928  *              (sd_check_media) is then signalled.
23929  *
23930  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
23931  *                      among multiple watches that share this callback function
23932  *              resultp - scsi watch facility result packet containing scsi
23933  *                        packet, status byte and sense data
23934  *
23935  * Return Code: 0 for success, -1 for failure
23936  */
23937 
23938 static int
23939 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
23940 {
23941         struct sd_lun                   *un;
23942         struct scsi_status              *statusp = resultp->statusp;
23943         uint8_t                         *sensep = (uint8_t *)resultp->sensep;
23944         enum dkio_state                 state = DKIO_NONE;
23945         dev_t                           dev = (dev_t)arg;
23946         uchar_t                         actual_sense_length;
23947         uint8_t                         skey, asc, ascq;
23948 
23949         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23950                 return (-1);
23951         }
23952         actual_sense_length = resultp->actual_sense_length;
23953 
23954         mutex_enter(SD_MUTEX(un));
23955         SD_TRACE(SD_LOG_COMMON, un,
23956             "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n",
23957             *((char *)statusp), (void *)sensep, actual_sense_length);
23958 
23959         if (resultp->pkt->pkt_reason == CMD_DEV_GONE) {
23960                 un->un_mediastate = DKIO_DEV_GONE;
23961                 cv_broadcast(&un->un_state_cv);
23962                 mutex_exit(SD_MUTEX(un));
23963 
23964                 return (0);
23965         }
23966 
23967         if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
23968                 if (sd_gesn_media_data_valid(resultp->mmc_data)) {
23969                         if ((resultp->mmc_data[5] &
23970                             SD_GESN_MEDIA_EVENT_STATUS_PRESENT) != 0) {
23971                                 state = DKIO_INSERTED;
23972                         } else {
23973                                 state = DKIO_EJECTED;
23974                         }
23975                         if ((resultp->mmc_data[4] & SD_GESN_MEDIA_EVENT_CODE) ==
23976                             SD_GESN_MEDIA_EVENT_EJECTREQUEST) {
23977                                 sd_log_eject_request_event(un, KM_NOSLEEP);
23978                         }
23979                 }
23980         } else if (sensep != NULL) {
23981                 /*
23982                  * If there was a check condition then sensep points to valid
23983                  * sense data. If status was not a check condition but a
23984                  * reservation or busy status then the new state is DKIO_NONE.
23985                  */
23986                 skey = scsi_sense_key(sensep);
23987                 asc = scsi_sense_asc(sensep);
23988                 ascq = scsi_sense_ascq(sensep);
23989 
23990                 SD_INFO(SD_LOG_COMMON, un,
23991                     "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n",
23992                     skey, asc, ascq);
23993                 /* This routine only uses up to 13 bytes of sense data. */
23994                 if (actual_sense_length >= 13) {
23995                         if (skey == KEY_UNIT_ATTENTION) {
23996                                 if (asc == 0x28) {
23997                                         state = DKIO_INSERTED;
23998                                 }
23999                         } else if (skey == KEY_NOT_READY) {
24000                                 /*
24001                                  * Sense data of 02/06/00 means that the
24002                                  * drive could not read the media (No
24003                                  * reference position found). In this case
24004                                  * to prevent a hang on the DKIOCSTATE IOCTL
24005                                  * we set the media state to DKIO_INSERTED.
24006                                  */
24007                                 if (asc == 0x06 && ascq == 0x00)
24008                                         state = DKIO_INSERTED;
24009 
24010                                 /*
24011                                  * if 02/04/02  means that the host
24012                                  * should send start command. Explicitly
24013                                  * leave the media state as is
24014                                  * (inserted) as the media is inserted
24015                                  * and host has stopped device for PM
24016                                  * reasons. Upon next true read/write
24017                                  * to this media will bring the
24018                                  * device to the right state good for
24019                                  * media access.
24020                                  */
24021                                 if (asc == 0x3a) {
24022                                         state = DKIO_EJECTED;
24023                                 } else {
24024                                         /*
24025                                          * If the drive is busy with an
24026                                          * operation or long write, keep the
24027                                          * media in an inserted state.
24028                                          */
24029 
24030                                         if ((asc == 0x04) &&
24031                                             ((ascq == 0x02) ||
24032                                             (ascq == 0x07) ||
24033                                             (ascq == 0x08))) {
24034                                                 state = DKIO_INSERTED;
24035                                         }
24036                                 }
24037                         } else if (skey == KEY_NO_SENSE) {
24038                                 if ((asc == 0x00) && (ascq == 0x00)) {
24039                                         /*
24040                                          * Sense Data 00/00/00 does not provide
24041                                          * any information about the state of
24042                                          * the media. Ignore it.
24043                                          */
24044                                         mutex_exit(SD_MUTEX(un));
24045                                         return (0);
24046                                 }
24047                         }
24048                 }
24049         } else if ((*((char *)statusp) == STATUS_GOOD) &&
24050             (resultp->pkt->pkt_reason == CMD_CMPLT)) {
24051                 state = DKIO_INSERTED;
24052         }
24053 
24054         SD_TRACE(SD_LOG_COMMON, un,
24055             "sd_media_watch_cb: state=%x, specified=%x\n",
24056             state, un->un_specified_mediastate);
24057 
24058         /*
24059          * now signal the waiting thread if this is *not* the specified state;
24060          * delay the signal if the state is DKIO_INSERTED to allow the target
24061          * to recover
24062          */
24063         if (state != un->un_specified_mediastate) {
24064                 un->un_mediastate = state;
24065                 if (state == DKIO_INSERTED) {
24066                         /*
24067                          * delay the signal to give the drive a chance
24068                          * to do what it apparently needs to do
24069                          */
24070                         SD_TRACE(SD_LOG_COMMON, un,
24071                             "sd_media_watch_cb: delayed cv_broadcast\n");
24072                         if (un->un_dcvb_timeid == NULL) {
24073                                 un->un_dcvb_timeid =
24074                                     timeout(sd_delayed_cv_broadcast, un,
24075                                     drv_usectohz((clock_t)MEDIA_ACCESS_DELAY));
24076                         }
24077                 } else {
24078                         SD_TRACE(SD_LOG_COMMON, un,
24079                             "sd_media_watch_cb: immediate cv_broadcast\n");
24080                         cv_broadcast(&un->un_state_cv);
24081                 }
24082         }
24083         mutex_exit(SD_MUTEX(un));
24084         return (0);
24085 }
24086 
24087 
24088 /*
24089  *    Function: sd_dkio_get_temp
24090  *
24091  * Description: This routine is the driver entry point for handling ioctl
24092  *              requests to get the disk temperature.
24093  *
24094  *   Arguments: dev  - the device number
24095  *              arg  - pointer to user provided dk_temperature structure.
24096  *              flag - this argument is a pass through to ddi_copyxxx()
24097  *                     directly from the mode argument of ioctl().
24098  *
24099  * Return Code: 0
24100  *              EFAULT
24101  *              ENXIO
24102  *              EAGAIN
24103  */
24104 
24105 static int
24106 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag)
24107 {
24108         struct sd_lun           *un = NULL;
24109         struct dk_temperature   *dktemp = NULL;
24110         uchar_t                 *temperature_page;
24111         int                     rval = 0;
24112         int                     path_flag = SD_PATH_STANDARD;
24113         sd_ssc_t                *ssc;
24114 
24115         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24116                 return (ENXIO);
24117         }
24118 
24119         ssc = sd_ssc_init(un);
24120         dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP);
24121 
24122         /* copyin the disk temp argument to get the user flags */
24123         if (ddi_copyin((void *)arg, dktemp,
24124             sizeof (struct dk_temperature), flag) != 0) {
24125                 rval = EFAULT;
24126                 goto done;
24127         }
24128 
24129         /* Initialize the temperature to invalid. */
24130         dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24131         dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24132 
24133         /*
24134          * Note: Investigate removing the "bypass pm" semantic.
24135          * Can we just bypass PM always?
24136          */
24137         if (dktemp->dkt_flags & DKT_BYPASS_PM) {
24138                 path_flag = SD_PATH_DIRECT;
24139                 ASSERT(!mutex_owned(&un->un_pm_mutex));
24140                 mutex_enter(&un->un_pm_mutex);
24141                 if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
24142                         /*
24143                          * If DKT_BYPASS_PM is set, and the drive happens to be
24144                          * in low power mode, we can not wake it up, Need to
24145                          * return EAGAIN.
24146                          */
24147                         mutex_exit(&un->un_pm_mutex);
24148                         rval = EAGAIN;
24149                         goto done;
24150                 } else {
24151                         /*
24152                          * Indicate to PM the device is busy. This is required
24153                          * to avoid a race - i.e. the ioctl is issuing a
24154                          * command and the pm framework brings down the device
24155                          * to low power mode (possible power cut-off on some
24156                          * platforms).
24157                          */
24158                         mutex_exit(&un->un_pm_mutex);
24159                         if (sd_pm_entry(un) != DDI_SUCCESS) {
24160                                 rval = EAGAIN;
24161                                 goto done;
24162                         }
24163                 }
24164         }
24165 
24166         temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP);
24167 
24168         rval = sd_send_scsi_LOG_SENSE(ssc, temperature_page,
24169             TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag);
24170         if (rval != 0)
24171                 goto done2;
24172 
24173         /*
24174          * For the current temperature verify that the parameter length is 0x02
24175          * and the parameter code is 0x00
24176          */
24177         if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) &&
24178             (temperature_page[5] == 0x00)) {
24179                 if (temperature_page[9] == 0xFF) {
24180                         dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24181                 } else {
24182                         dktemp->dkt_cur_temp = (short)(temperature_page[9]);
24183                 }
24184         }
24185 
24186         /*
24187          * For the reference temperature verify that the parameter
24188          * length is 0x02 and the parameter code is 0x01
24189          */
24190         if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) &&
24191             (temperature_page[11] == 0x01)) {
24192                 if (temperature_page[15] == 0xFF) {
24193                         dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24194                 } else {
24195                         dktemp->dkt_ref_temp = (short)(temperature_page[15]);
24196                 }
24197         }
24198 
24199         /* Do the copyout regardless of the temperature commands status. */
24200         if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature),
24201             flag) != 0) {
24202                 rval = EFAULT;
24203                 goto done1;
24204         }
24205 
24206 done2:
24207         if (rval != 0) {
24208                 if (rval == EIO)
24209                         sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24210                 else
24211                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24212         }
24213 done1:
24214         if (path_flag == SD_PATH_DIRECT) {
24215                 sd_pm_exit(un);
24216         }
24217 
24218         kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE);
24219 done:
24220         sd_ssc_fini(ssc);
24221         if (dktemp != NULL) {
24222                 kmem_free(dktemp, sizeof (struct dk_temperature));
24223         }
24224 
24225         return (rval);
24226 }
24227 
24228 
24229 /*
24230  *    Function: sd_log_page_supported
24231  *
24232  * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of
24233  *              supported log pages.
24234  *
24235  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
24236  *                      structure for this target.
24237  *              log_page -
24238  *
24239  * Return Code: -1 - on error (log sense is optional and may not be supported).
24240  *              0  - log page not found.
24241  *              1  - log page found.
24242  */
24243 
24244 static int
24245 sd_log_page_supported(sd_ssc_t *ssc, int log_page)
24246 {
24247         uchar_t *log_page_data;
24248         int     i;
24249         int     match = 0;
24250         int     log_size;
24251         int     status = 0;
24252         struct sd_lun   *un;
24253 
24254         ASSERT(ssc != NULL);
24255         un = ssc->ssc_un;
24256         ASSERT(un != NULL);
24257 
24258         log_page_data = kmem_zalloc(0xFF, KM_SLEEP);
24259 
24260         status = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 0xFF, 0, 0x01, 0,
24261             SD_PATH_DIRECT);
24262 
24263         if (status != 0) {
24264                 if (status == EIO) {
24265                         /*
24266                          * Some disks do not support log sense, we
24267                          * should ignore this kind of error(sense key is
24268                          * 0x5 - illegal request).
24269                          */
24270                         uint8_t *sensep;
24271                         int senlen;
24272 
24273                         sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
24274                         senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
24275                             ssc->ssc_uscsi_cmd->uscsi_rqresid);
24276 
24277                         if (senlen > 0 &&
24278                             scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
24279                                 sd_ssc_assessment(ssc,
24280                                     SD_FMT_IGNORE_COMPROMISE);
24281                         } else {
24282                                 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24283                         }
24284                 } else {
24285                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24286                 }
24287 
24288                 SD_ERROR(SD_LOG_COMMON, un,
24289                     "sd_log_page_supported: failed log page retrieval\n");
24290                 kmem_free(log_page_data, 0xFF);
24291                 return (-1);
24292         }
24293 
24294         log_size = log_page_data[3];
24295 
24296         /*
24297          * The list of supported log pages start from the fourth byte. Check
24298          * until we run out of log pages or a match is found.
24299          */
24300         for (i = 4; (i < (log_size + 4)) && !match; i++) {
24301                 if (log_page_data[i] == log_page) {
24302                         match++;
24303                 }
24304         }
24305         kmem_free(log_page_data, 0xFF);
24306         return (match);
24307 }
24308 
24309 
24310 /*
24311  *    Function: sd_mhdioc_failfast
24312  *
24313  * Description: This routine is the driver entry point for handling ioctl
24314  *              requests to enable/disable the multihost failfast option.
24315  *              (MHIOCENFAILFAST)
24316  *
24317  *   Arguments: dev     - the device number
24318  *              arg     - user specified probing interval.
24319  *              flag    - this argument is a pass through to ddi_copyxxx()
24320  *                        directly from the mode argument of ioctl().
24321  *
24322  * Return Code: 0
24323  *              EFAULT
24324  *              ENXIO
24325  */
24326 
24327 static int
24328 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag)
24329 {
24330         struct sd_lun   *un = NULL;
24331         int             mh_time;
24332         int             rval = 0;
24333 
24334         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24335                 return (ENXIO);
24336         }
24337 
24338         if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag))
24339                 return (EFAULT);
24340 
24341         if (mh_time) {
24342                 mutex_enter(SD_MUTEX(un));
24343                 un->un_resvd_status |= SD_FAILFAST;
24344                 mutex_exit(SD_MUTEX(un));
24345                 /*
24346                  * If mh_time is INT_MAX, then this ioctl is being used for
24347                  * SCSI-3 PGR purposes, and we don't need to spawn watch thread.
24348                  */
24349                 if (mh_time != INT_MAX) {
24350                         rval = sd_check_mhd(dev, mh_time);
24351                 }
24352         } else {
24353                 (void) sd_check_mhd(dev, 0);
24354                 mutex_enter(SD_MUTEX(un));
24355                 un->un_resvd_status &= ~SD_FAILFAST;
24356                 mutex_exit(SD_MUTEX(un));
24357         }
24358         return (rval);
24359 }
24360 
24361 
24362 /*
24363  *    Function: sd_mhdioc_takeown
24364  *
24365  * Description: This routine is the driver entry point for handling ioctl
24366  *              requests to forcefully acquire exclusive access rights to the
24367  *              multihost disk (MHIOCTKOWN).
24368  *
24369  *   Arguments: dev     - the device number
24370  *              arg     - user provided structure specifying the delay
24371  *                        parameters in milliseconds
24372  *              flag    - this argument is a pass through to ddi_copyxxx()
24373  *                        directly from the mode argument of ioctl().
24374  *
24375  * Return Code: 0
24376  *              EFAULT
24377  *              ENXIO
24378  */
24379 
24380 static int
24381 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag)
24382 {
24383         struct sd_lun           *un = NULL;
24384         struct mhioctkown       *tkown = NULL;
24385         int                     rval = 0;
24386 
24387         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24388                 return (ENXIO);
24389         }
24390 
24391         if (arg != NULL) {
24392                 tkown = (struct mhioctkown *)
24393                     kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP);
24394                 rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag);
24395                 if (rval != 0) {
24396                         rval = EFAULT;
24397                         goto error;
24398                 }
24399         }
24400 
24401         rval = sd_take_ownership(dev, tkown);
24402         mutex_enter(SD_MUTEX(un));
24403         if (rval == 0) {
24404                 un->un_resvd_status |= SD_RESERVE;
24405                 if (tkown != NULL && tkown->reinstate_resv_delay != 0) {
24406                         sd_reinstate_resv_delay =
24407                             tkown->reinstate_resv_delay * 1000;
24408                 } else {
24409                         sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
24410                 }
24411                 /*
24412                  * Give the scsi_watch routine interval set by
24413                  * the MHIOCENFAILFAST ioctl precedence here.
24414                  */
24415                 if ((un->un_resvd_status & SD_FAILFAST) == 0) {
24416                         mutex_exit(SD_MUTEX(un));
24417                         (void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000);
24418                         SD_TRACE(SD_LOG_IOCTL_MHD, un,
24419                             "sd_mhdioc_takeown : %d\n",
24420                             sd_reinstate_resv_delay);
24421                 } else {
24422                         mutex_exit(SD_MUTEX(un));
24423                 }
24424                 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY,
24425                     sd_mhd_reset_notify_cb, (caddr_t)un);
24426         } else {
24427                 un->un_resvd_status &= ~SD_RESERVE;
24428                 mutex_exit(SD_MUTEX(un));
24429         }
24430 
24431 error:
24432         if (tkown != NULL) {
24433                 kmem_free(tkown, sizeof (struct mhioctkown));
24434         }
24435         return (rval);
24436 }
24437 
24438 
24439 /*
24440  *    Function: sd_mhdioc_release
24441  *
24442  * Description: This routine is the driver entry point for handling ioctl
24443  *              requests to release exclusive access rights to the multihost
24444  *              disk (MHIOCRELEASE).
24445  *
24446  *   Arguments: dev     - the device number
24447  *
24448  * Return Code: 0
24449  *              ENXIO
24450  */
24451 
24452 static int
24453 sd_mhdioc_release(dev_t dev)
24454 {
24455         struct sd_lun           *un = NULL;
24456         timeout_id_t            resvd_timeid_save;
24457         int                     resvd_status_save;
24458         int                     rval = 0;
24459 
24460         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24461                 return (ENXIO);
24462         }
24463 
24464         mutex_enter(SD_MUTEX(un));
24465         resvd_status_save = un->un_resvd_status;
24466         un->un_resvd_status &=
24467             ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE);
24468         if (un->un_resvd_timeid) {
24469                 resvd_timeid_save = un->un_resvd_timeid;
24470                 un->un_resvd_timeid = NULL;
24471                 mutex_exit(SD_MUTEX(un));
24472                 (void) untimeout(resvd_timeid_save);
24473         } else {
24474                 mutex_exit(SD_MUTEX(un));
24475         }
24476 
24477         /*
24478          * destroy any pending timeout thread that may be attempting to
24479          * reinstate reservation on this device.
24480          */
24481         sd_rmv_resv_reclaim_req(dev);
24482 
24483         if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) {
24484                 mutex_enter(SD_MUTEX(un));
24485                 if ((un->un_mhd_token) &&
24486                     ((un->un_resvd_status & SD_FAILFAST) == 0)) {
24487                         mutex_exit(SD_MUTEX(un));
24488                         (void) sd_check_mhd(dev, 0);
24489                 } else {
24490                         mutex_exit(SD_MUTEX(un));
24491                 }
24492                 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
24493                     sd_mhd_reset_notify_cb, (caddr_t)un);
24494         } else {
24495                 /*
24496                  * sd_mhd_watch_cb will restart the resvd recover timeout thread
24497                  */
24498                 mutex_enter(SD_MUTEX(un));
24499                 un->un_resvd_status = resvd_status_save;
24500                 mutex_exit(SD_MUTEX(un));
24501         }
24502         return (rval);
24503 }
24504 
24505 
24506 /*
24507  *    Function: sd_mhdioc_register_devid
24508  *
24509  * Description: This routine is the driver entry point for handling ioctl
24510  *              requests to register the device id (MHIOCREREGISTERDEVID).
24511  *
24512  *              Note: The implementation for this ioctl has been updated to
24513  *              be consistent with the original PSARC case (1999/357)
24514  *              (4375899, 4241671, 4220005)
24515  *
24516  *   Arguments: dev     - the device number
24517  *
24518  * Return Code: 0
24519  *              ENXIO
24520  */
24521 
24522 static int
24523 sd_mhdioc_register_devid(dev_t dev)
24524 {
24525         struct sd_lun   *un = NULL;
24526         int             rval = 0;
24527         sd_ssc_t        *ssc;
24528 
24529         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24530                 return (ENXIO);
24531         }
24532 
24533         ASSERT(!mutex_owned(SD_MUTEX(un)));
24534 
24535         mutex_enter(SD_MUTEX(un));
24536 
24537         /* If a devid already exists, de-register it */
24538         if (un->un_devid != NULL) {
24539                 ddi_devid_unregister(SD_DEVINFO(un));
24540                 /*
24541                  * After unregister devid, needs to free devid memory
24542                  */
24543                 ddi_devid_free(un->un_devid);
24544                 un->un_devid = NULL;
24545         }
24546 
24547         /* Check for reservation conflict */
24548         mutex_exit(SD_MUTEX(un));
24549         ssc = sd_ssc_init(un);
24550         rval = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
24551         mutex_enter(SD_MUTEX(un));
24552 
24553         switch (rval) {
24554         case 0:
24555                 sd_register_devid(ssc, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED);
24556                 break;
24557         case EACCES:
24558                 break;
24559         default:
24560                 rval = EIO;
24561         }
24562 
24563         mutex_exit(SD_MUTEX(un));
24564         if (rval != 0) {
24565                 if (rval == EIO)
24566                         sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24567                 else
24568                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24569         }
24570         sd_ssc_fini(ssc);
24571         return (rval);
24572 }
24573 
24574 
24575 /*
24576  *    Function: sd_mhdioc_inkeys
24577  *
24578  * Description: This routine is the driver entry point for handling ioctl
24579  *              requests to issue the SCSI-3 Persistent In Read Keys command
24580  *              to the device (MHIOCGRP_INKEYS).
24581  *
24582  *   Arguments: dev     - the device number
24583  *              arg     - user provided in_keys structure
24584  *              flag    - this argument is a pass through to ddi_copyxxx()
24585  *                        directly from the mode argument of ioctl().
24586  *
24587  * Return Code: code returned by sd_persistent_reservation_in_read_keys()
24588  *              ENXIO
24589  *              EFAULT
24590  */
24591 
24592 static int
24593 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag)
24594 {
24595         struct sd_lun           *un;
24596         mhioc_inkeys_t          inkeys;
24597         int                     rval = 0;
24598 
24599         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24600                 return (ENXIO);
24601         }
24602 
24603 #ifdef _MULTI_DATAMODEL
24604         switch (ddi_model_convert_from(flag & FMODELS)) {
24605         case DDI_MODEL_ILP32: {
24606                 struct mhioc_inkeys32   inkeys32;
24607 
24608                 if (ddi_copyin(arg, &inkeys32,
24609                     sizeof (struct mhioc_inkeys32), flag) != 0) {
24610                         return (EFAULT);
24611                 }
24612                 inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li;
24613                 if ((rval = sd_persistent_reservation_in_read_keys(un,
24614                     &inkeys, flag)) != 0) {
24615                         return (rval);
24616                 }
24617                 inkeys32.generation = inkeys.generation;
24618                 if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32),
24619                     flag) != 0) {
24620                         return (EFAULT);
24621                 }
24622                 break;
24623         }
24624         case DDI_MODEL_NONE:
24625                 if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t),
24626                     flag) != 0) {
24627                         return (EFAULT);
24628                 }
24629                 if ((rval = sd_persistent_reservation_in_read_keys(un,
24630                     &inkeys, flag)) != 0) {
24631                         return (rval);
24632                 }
24633                 if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t),
24634                     flag) != 0) {
24635                         return (EFAULT);
24636                 }
24637                 break;
24638         }
24639 
24640 #else /* ! _MULTI_DATAMODEL */
24641 
24642         if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) {
24643                 return (EFAULT);
24644         }
24645         rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag);
24646         if (rval != 0) {
24647                 return (rval);
24648         }
24649         if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) {
24650                 return (EFAULT);
24651         }
24652 
24653 #endif /* _MULTI_DATAMODEL */
24654 
24655         return (rval);
24656 }
24657 
24658 
24659 /*
24660  *    Function: sd_mhdioc_inresv
24661  *
24662  * Description: This routine is the driver entry point for handling ioctl
24663  *              requests to issue the SCSI-3 Persistent In Read Reservations
24664  *              command to the device (MHIOCGRP_INKEYS).
24665  *
24666  *   Arguments: dev     - the device number
24667  *              arg     - user provided in_resv structure
24668  *              flag    - this argument is a pass through to ddi_copyxxx()
24669  *                        directly from the mode argument of ioctl().
24670  *
24671  * Return Code: code returned by sd_persistent_reservation_in_read_resv()
24672  *              ENXIO
24673  *              EFAULT
24674  */
24675 
24676 static int
24677 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag)
24678 {
24679         struct sd_lun           *un;
24680         mhioc_inresvs_t         inresvs;
24681         int                     rval = 0;
24682 
24683         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24684                 return (ENXIO);
24685         }
24686 
24687 #ifdef _MULTI_DATAMODEL
24688 
24689         switch (ddi_model_convert_from(flag & FMODELS)) {
24690         case DDI_MODEL_ILP32: {
24691                 struct mhioc_inresvs32  inresvs32;
24692 
24693                 if (ddi_copyin(arg, &inresvs32,
24694                     sizeof (struct mhioc_inresvs32), flag) != 0) {
24695                         return (EFAULT);
24696                 }
24697                 inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li;
24698                 if ((rval = sd_persistent_reservation_in_read_resv(un,
24699                     &inresvs, flag)) != 0) {
24700                         return (rval);
24701                 }
24702                 inresvs32.generation = inresvs.generation;
24703                 if (ddi_copyout(&inresvs32, arg,
24704                     sizeof (struct mhioc_inresvs32), flag) != 0) {
24705                         return (EFAULT);
24706                 }
24707                 break;
24708         }
24709         case DDI_MODEL_NONE:
24710                 if (ddi_copyin(arg, &inresvs,
24711                     sizeof (mhioc_inresvs_t), flag) != 0) {
24712                         return (EFAULT);
24713                 }
24714                 if ((rval = sd_persistent_reservation_in_read_resv(un,
24715                     &inresvs, flag)) != 0) {
24716                         return (rval);
24717                 }
24718                 if (ddi_copyout(&inresvs, arg,
24719                     sizeof (mhioc_inresvs_t), flag) != 0) {
24720                         return (EFAULT);
24721                 }
24722                 break;
24723         }
24724 
24725 #else /* ! _MULTI_DATAMODEL */
24726 
24727         if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) {
24728                 return (EFAULT);
24729         }
24730         rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag);
24731         if (rval != 0) {
24732                 return (rval);
24733         }
24734         if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) {
24735                 return (EFAULT);
24736         }
24737 
24738 #endif /* ! _MULTI_DATAMODEL */
24739 
24740         return (rval);
24741 }
24742 
24743 
24744 /*
24745  * The following routines support the clustering functionality described below
24746  * and implement lost reservation reclaim functionality.
24747  *
24748  * Clustering
24749  * ----------
24750  * The clustering code uses two different, independent forms of SCSI
24751  * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3
24752  * Persistent Group Reservations. For any particular disk, it will use either
24753  * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk.
24754  *
24755  * SCSI-2
24756  * The cluster software takes ownership of a multi-hosted disk by issuing the
24757  * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the
24758  * MHIOCRELEASE ioctl.  Closely related is the MHIOCENFAILFAST ioctl -- a
24759  * cluster, just after taking ownership of the disk with the MHIOCTKOWN ioctl
24760  * then issues the MHIOCENFAILFAST ioctl.  This ioctl "enables failfast" in the
24761  * driver. The meaning of failfast is that if the driver (on this host) ever
24762  * encounters the scsi error return code RESERVATION_CONFLICT from the device,
24763  * it should immediately panic the host. The motivation for this ioctl is that
24764  * if this host does encounter reservation conflict, the underlying cause is
24765  * that some other host of the cluster has decided that this host is no longer
24766  * in the cluster and has seized control of the disks for itself. Since this
24767  * host is no longer in the cluster, it ought to panic itself. The
24768  * MHIOCENFAILFAST ioctl does two things:
24769  *      (a) it sets a flag that will cause any returned RESERVATION_CONFLICT
24770  *      error to panic the host
24771  *      (b) it sets up a periodic timer to test whether this host still has
24772  *      "access" (in that no other host has reserved the device):  if the
24773  *      periodic timer gets RESERVATION_CONFLICT, the host is panicked. The
24774  *      purpose of that periodic timer is to handle scenarios where the host is
24775  *      otherwise temporarily quiescent, temporarily doing no real i/o.
24776  * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host,
24777  * by issuing a SCSI Bus Device Reset.  It will then issue a SCSI Reserve for
24778  * the device itself.
24779  *
24780  * SCSI-3 PGR
24781  * A direct semantic implementation of the SCSI-3 Persistent Reservation
24782  * facility is supported through the shared multihost disk ioctls
24783  * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE,
24784  * MHIOCGRP_PREEMPTANDABORT, MHIOCGRP_CLEAR)
24785  *
24786  * Reservation Reclaim:
24787  * --------------------
24788  * To support the lost reservation reclaim operations this driver creates a
24789  * single thread to handle reinstating reservations on all devices that have
24790  * lost reservations sd_resv_reclaim_requests are logged for all devices that
24791  * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb
24792  * and the reservation reclaim thread loops through the requests to regain the
24793  * lost reservations.
24794  */
24795 
24796 /*
24797  *    Function: sd_check_mhd()
24798  *
24799  * Description: This function sets up and submits a scsi watch request or
24800  *              terminates an existing watch request. This routine is used in
24801  *              support of reservation reclaim.
24802  *
24803  *   Arguments: dev    - the device 'dev_t' is used for context to discriminate
24804  *                       among multiple watches that share the callback function
24805  *              interval - the number of microseconds specifying the watch
24806  *                         interval for issuing TEST UNIT READY commands. If
24807  *                         set to 0 the watch should be terminated. If the
24808  *                         interval is set to 0 and if the device is required
24809  *                         to hold reservation while disabling failfast, the
24810  *                         watch is restarted with an interval of
24811  *                         reinstate_resv_delay.
24812  *
24813  * Return Code: 0          - Successful submit/terminate of scsi watch request
24814  *              ENXIO      - Indicates an invalid device was specified
24815  *              EAGAIN     - Unable to submit the scsi watch request
24816  */
24817 
24818 static int
24819 sd_check_mhd(dev_t dev, int interval)
24820 {
24821         struct sd_lun   *un;
24822         opaque_t        token;
24823 
24824         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24825                 return (ENXIO);
24826         }
24827 
24828         /* is this a watch termination request? */
24829         if (interval == 0) {
24830                 mutex_enter(SD_MUTEX(un));
24831                 /* if there is an existing watch task then terminate it */
24832                 if (un->un_mhd_token) {
24833                         token = un->un_mhd_token;
24834                         un->un_mhd_token = NULL;
24835                         mutex_exit(SD_MUTEX(un));
24836                         (void) scsi_watch_request_terminate(token,
24837                             SCSI_WATCH_TERMINATE_ALL_WAIT);
24838                         mutex_enter(SD_MUTEX(un));
24839                 } else {
24840                         mutex_exit(SD_MUTEX(un));
24841                         /*
24842                          * Note: If we return here we don't check for the
24843                          * failfast case. This is the original legacy
24844                          * implementation but perhaps we should be checking
24845                          * the failfast case.
24846                          */
24847                         return (0);
24848                 }
24849                 /*
24850                  * If the device is required to hold reservation while
24851                  * disabling failfast, we need to restart the scsi_watch
24852                  * routine with an interval of reinstate_resv_delay.
24853                  */
24854                 if (un->un_resvd_status & SD_RESERVE) {
24855                         interval = sd_reinstate_resv_delay/1000;
24856                 } else {
24857                         /* no failfast so bail */
24858                         mutex_exit(SD_MUTEX(un));
24859                         return (0);
24860                 }
24861                 mutex_exit(SD_MUTEX(un));
24862         }
24863 
24864         /*
24865          * adjust minimum time interval to 1 second,
24866          * and convert from msecs to usecs
24867          */
24868         if (interval > 0 && interval < 1000) {
24869                 interval = 1000;
24870         }
24871         interval *= 1000;
24872 
24873         /*
24874          * submit the request to the scsi_watch service
24875          */
24876         token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval,
24877             SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev);
24878         if (token == NULL) {
24879                 return (EAGAIN);
24880         }
24881 
24882         /*
24883          * save token for termination later on
24884          */
24885         mutex_enter(SD_MUTEX(un));
24886         un->un_mhd_token = token;
24887         mutex_exit(SD_MUTEX(un));
24888         return (0);
24889 }
24890 
24891 
24892 /*
24893  *    Function: sd_mhd_watch_cb()
24894  *
24895  * Description: This function is the call back function used by the scsi watch
24896  *              facility. The scsi watch facility sends the "Test Unit Ready"
24897  *              and processes the status. If applicable (i.e. a "Unit Attention"
24898  *              status and automatic "Request Sense" not used) the scsi watch
24899  *              facility will send a "Request Sense" and retrieve the sense data
24900  *              to be passed to this callback function. In either case the
24901  *              automatic "Request Sense" or the facility submitting one, this
24902  *              callback is passed the status and sense data.
24903  *
24904  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
24905  *                      among multiple watches that share this callback function
24906  *              resultp - scsi watch facility result packet containing scsi
24907  *                        packet, status byte and sense data
24908  *
24909  * Return Code: 0 - continue the watch task
24910  *              non-zero - terminate the watch task
24911  */
24912 
24913 static int
24914 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
24915 {
24916         struct sd_lun                   *un;
24917         struct scsi_status              *statusp;
24918         uint8_t                         *sensep;
24919         struct scsi_pkt                 *pkt;
24920         uchar_t                         actual_sense_length;
24921         dev_t                           dev = (dev_t)arg;
24922 
24923         ASSERT(resultp != NULL);
24924         statusp                 = resultp->statusp;
24925         sensep                  = (uint8_t *)resultp->sensep;
24926         pkt                     = resultp->pkt;
24927         actual_sense_length     = resultp->actual_sense_length;
24928 
24929         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24930                 return (ENXIO);
24931         }
24932 
24933         SD_TRACE(SD_LOG_IOCTL_MHD, un,
24934             "sd_mhd_watch_cb: reason '%s', status '%s'\n",
24935             scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp)));
24936 
24937         /* Begin processing of the status and/or sense data */
24938         if (pkt->pkt_reason != CMD_CMPLT) {
24939                 /* Handle the incomplete packet */
24940                 sd_mhd_watch_incomplete(un, pkt);
24941                 return (0);
24942         } else if (*((unsigned char *)statusp) != STATUS_GOOD) {
24943                 if (*((unsigned char *)statusp)
24944                     == STATUS_RESERVATION_CONFLICT) {
24945                         /*
24946                          * Handle a reservation conflict by panicking if
24947                          * configured for failfast or by logging the conflict
24948                          * and updating the reservation status
24949                          */
24950                         mutex_enter(SD_MUTEX(un));
24951                         if ((un->un_resvd_status & SD_FAILFAST) &&
24952                             (sd_failfast_enable)) {
24953                                 sd_panic_for_res_conflict(un);
24954                                 /*NOTREACHED*/
24955                         }
24956                         SD_INFO(SD_LOG_IOCTL_MHD, un,
24957                             "sd_mhd_watch_cb: Reservation Conflict\n");
24958                         un->un_resvd_status |= SD_RESERVATION_CONFLICT;
24959                         mutex_exit(SD_MUTEX(un));
24960                 }
24961         }
24962 
24963         if (sensep != NULL) {
24964                 if (actual_sense_length >= (SENSE_LENGTH - 2)) {
24965                         mutex_enter(SD_MUTEX(un));
24966                         if ((scsi_sense_asc(sensep) ==
24967                             SD_SCSI_RESET_SENSE_CODE) &&
24968                             (un->un_resvd_status & SD_RESERVE)) {
24969                                 /*
24970                                  * The additional sense code indicates a power
24971                                  * on or bus device reset has occurred; update
24972                                  * the reservation status.
24973                                  */
24974                                 un->un_resvd_status |=
24975                                     (SD_LOST_RESERVE | SD_WANT_RESERVE);
24976                                 SD_INFO(SD_LOG_IOCTL_MHD, un,
24977                                     "sd_mhd_watch_cb: Lost Reservation\n");
24978                         }
24979                 } else {
24980                         return (0);
24981                 }
24982         } else {
24983                 mutex_enter(SD_MUTEX(un));
24984         }
24985 
24986         if ((un->un_resvd_status & SD_RESERVE) &&
24987             (un->un_resvd_status & SD_LOST_RESERVE)) {
24988                 if (un->un_resvd_status & SD_WANT_RESERVE) {
24989                         /*
24990                          * A reset occurred in between the last probe and this
24991                          * one so if a timeout is pending cancel it.
24992                          */
24993                         if (un->un_resvd_timeid) {
24994                                 timeout_id_t temp_id = un->un_resvd_timeid;
24995                                 un->un_resvd_timeid = NULL;
24996                                 mutex_exit(SD_MUTEX(un));
24997                                 (void) untimeout(temp_id);
24998                                 mutex_enter(SD_MUTEX(un));
24999                         }
25000                         un->un_resvd_status &= ~SD_WANT_RESERVE;
25001                 }
25002                 if (un->un_resvd_timeid == 0) {
25003                         /* Schedule a timeout to handle the lost reservation */
25004                         un->un_resvd_timeid = timeout(sd_mhd_resvd_recover,
25005                             (void *)dev,
25006                             drv_usectohz(sd_reinstate_resv_delay));
25007                 }
25008         }
25009         mutex_exit(SD_MUTEX(un));
25010         return (0);
25011 }
25012 
25013 
25014 /*
25015  *    Function: sd_mhd_watch_incomplete()
25016  *
25017  * Description: This function is used to find out why a scsi pkt sent by the
25018  *              scsi watch facility was not completed. Under some scenarios this
25019  *              routine will return. Otherwise it will send a bus reset to see
25020  *              if the drive is still online.
25021  *
25022  *   Arguments: un  - driver soft state (unit) structure
25023  *              pkt - incomplete scsi pkt
25024  */
25025 
25026 static void
25027 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt)
25028 {
25029         int     be_chatty;
25030         int     perr;
25031 
25032         ASSERT(pkt != NULL);
25033         ASSERT(un != NULL);
25034         be_chatty       = (!(pkt->pkt_flags & FLAG_SILENT));
25035         perr            = (pkt->pkt_statistics & STAT_PERR);
25036 
25037         mutex_enter(SD_MUTEX(un));
25038         if (un->un_state == SD_STATE_DUMPING) {
25039                 mutex_exit(SD_MUTEX(un));
25040                 return;
25041         }
25042 
25043         switch (pkt->pkt_reason) {
25044         case CMD_UNX_BUS_FREE:
25045                 /*
25046                  * If we had a parity error that caused the target to drop BSY*,
25047                  * don't be chatty about it.
25048                  */
25049                 if (perr && be_chatty) {
25050                         be_chatty = 0;
25051                 }
25052                 break;
25053         case CMD_TAG_REJECT:
25054                 /*
25055                  * The SCSI-2 spec states that a tag reject will be sent by the
25056                  * target if tagged queuing is not supported. A tag reject may
25057                  * also be sent during certain initialization periods or to
25058                  * control internal resources. For the latter case the target
25059                  * may also return Queue Full.
25060                  *
25061                  * If this driver receives a tag reject from a target that is
25062                  * going through an init period or controlling internal
25063                  * resources tagged queuing will be disabled. This is a less
25064                  * than optimal behavior but the driver is unable to determine
25065                  * the target state and assumes tagged queueing is not supported
25066                  */
25067                 pkt->pkt_flags = 0;
25068                 un->un_tagflags = 0;
25069 
25070                 if (un->un_f_opt_queueing == TRUE) {
25071                         un->un_throttle = min(un->un_throttle, 3);
25072                 } else {
25073                         un->un_throttle = 1;
25074                 }
25075                 mutex_exit(SD_MUTEX(un));
25076                 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
25077                 mutex_enter(SD_MUTEX(un));
25078                 break;
25079         case CMD_INCOMPLETE:
25080                 /*
25081                  * The transport stopped with an abnormal state, fallthrough and
25082                  * reset the target and/or bus unless selection did not complete
25083                  * (indicated by STATE_GOT_BUS) in which case we don't want to
25084                  * go through a target/bus reset
25085                  */
25086                 if (pkt->pkt_state == STATE_GOT_BUS) {
25087                         break;
25088                 }
25089                 /*FALLTHROUGH*/
25090 
25091         case CMD_TIMEOUT:
25092         default:
25093                 /*
25094                  * The lun may still be running the command, so a lun reset
25095                  * should be attempted. If the lun reset fails or cannot be
25096                  * issued, than try a target reset. Lastly try a bus reset.
25097                  */
25098                 if ((pkt->pkt_statistics &
25099                     (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) {
25100                         int reset_retval = 0;
25101                         mutex_exit(SD_MUTEX(un));
25102                         if (un->un_f_allow_bus_device_reset == TRUE) {
25103                                 if (un->un_f_lun_reset_enabled == TRUE) {
25104                                         reset_retval =
25105                                             scsi_reset(SD_ADDRESS(un),
25106                                             RESET_LUN);
25107                                 }
25108                                 if (reset_retval == 0) {
25109                                         reset_retval =
25110                                             scsi_reset(SD_ADDRESS(un),
25111                                             RESET_TARGET);
25112                                 }
25113                         }
25114                         if (reset_retval == 0) {
25115                                 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
25116                         }
25117                         mutex_enter(SD_MUTEX(un));
25118                 }
25119                 break;
25120         }
25121 
25122         /* A device/bus reset has occurred; update the reservation status. */
25123         if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics &
25124             (STAT_BUS_RESET | STAT_DEV_RESET))) {
25125                 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25126                         un->un_resvd_status |=
25127                             (SD_LOST_RESERVE | SD_WANT_RESERVE);
25128                         SD_INFO(SD_LOG_IOCTL_MHD, un,
25129                             "sd_mhd_watch_incomplete: Lost Reservation\n");
25130                 }
25131         }
25132 
25133         /*
25134          * The disk has been turned off; Update the device state.
25135          *
25136          * Note: Should we be offlining the disk here?
25137          */
25138         if (pkt->pkt_state == STATE_GOT_BUS) {
25139                 SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: "
25140                     "Disk not responding to selection\n");
25141                 if (un->un_state != SD_STATE_OFFLINE) {
25142                         New_state(un, SD_STATE_OFFLINE);
25143                 }
25144         } else if (be_chatty) {
25145                 /*
25146                  * suppress messages if they are all the same pkt reason;
25147                  * with TQ, many (up to 256) are returned with the same
25148                  * pkt_reason
25149                  */
25150                 if (pkt->pkt_reason != un->un_last_pkt_reason) {
25151                         SD_ERROR(SD_LOG_IOCTL_MHD, un,
25152                             "sd_mhd_watch_incomplete: "
25153                             "SCSI transport failed: reason '%s'\n",
25154                             scsi_rname(pkt->pkt_reason));
25155                 }
25156         }
25157         un->un_last_pkt_reason = pkt->pkt_reason;
25158         mutex_exit(SD_MUTEX(un));
25159 }
25160 
25161 
25162 /*
25163  *    Function: sd_sname()
25164  *
25165  * Description: This is a simple little routine to return a string containing
25166  *              a printable description of command status byte for use in
25167  *              logging.
25168  *
25169  *   Arguments: status - pointer to a status byte
25170  *
25171  * Return Code: char * - string containing status description.
25172  */
25173 
25174 static char *
25175 sd_sname(uchar_t status)
25176 {
25177         switch (status & STATUS_MASK) {
25178         case STATUS_GOOD:
25179                 return ("good status");
25180         case STATUS_CHECK:
25181                 return ("check condition");
25182         case STATUS_MET:
25183                 return ("condition met");
25184         case STATUS_BUSY:
25185                 return ("busy");
25186         case STATUS_INTERMEDIATE:
25187                 return ("intermediate");
25188         case STATUS_INTERMEDIATE_MET:
25189                 return ("intermediate - condition met");
25190         case STATUS_RESERVATION_CONFLICT:
25191                 return ("reservation_conflict");
25192         case STATUS_TERMINATED:
25193                 return ("command terminated");
25194         case STATUS_QFULL:
25195                 return ("queue full");
25196         default:
25197                 return ("<unknown status>");
25198         }
25199 }
25200 
25201 
25202 /*
25203  *    Function: sd_mhd_resvd_recover()
25204  *
25205  * Description: This function adds a reservation entry to the
25206  *              sd_resv_reclaim_request list and signals the reservation
25207  *              reclaim thread that there is work pending. If the reservation
25208  *              reclaim thread has not been previously created this function
25209  *              will kick it off.
25210  *
25211  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
25212  *                      among multiple watches that share this callback function
25213  *
25214  *     Context: This routine is called by timeout() and is run in interrupt
25215  *              context. It must not sleep or call other functions which may
25216  *              sleep.
25217  */
25218 
25219 static void
25220 sd_mhd_resvd_recover(void *arg)
25221 {
25222         dev_t                   dev = (dev_t)arg;
25223         struct sd_lun           *un;
25224         struct sd_thr_request   *sd_treq = NULL;
25225         struct sd_thr_request   *sd_cur = NULL;
25226         struct sd_thr_request   *sd_prev = NULL;
25227         int                     already_there = 0;
25228 
25229         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25230                 return;
25231         }
25232 
25233         mutex_enter(SD_MUTEX(un));
25234         un->un_resvd_timeid = NULL;
25235         if (un->un_resvd_status & SD_WANT_RESERVE) {
25236                 /*
25237                  * There was a reset so don't issue the reserve, allow the
25238                  * sd_mhd_watch_cb callback function to notice this and
25239                  * reschedule the timeout for reservation.
25240                  */
25241                 mutex_exit(SD_MUTEX(un));
25242                 return;
25243         }
25244         mutex_exit(SD_MUTEX(un));
25245 
25246         /*
25247          * Add this device to the sd_resv_reclaim_request list and the
25248          * sd_resv_reclaim_thread should take care of the rest.
25249          *
25250          * Note: We can't sleep in this context so if the memory allocation
25251          * fails allow the sd_mhd_watch_cb callback function to notice this and
25252          * reschedule the timeout for reservation.  (4378460)
25253          */
25254         sd_treq = (struct sd_thr_request *)
25255             kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP);
25256         if (sd_treq == NULL) {
25257                 return;
25258         }
25259 
25260         sd_treq->sd_thr_req_next = NULL;
25261         sd_treq->dev = dev;
25262         mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25263         if (sd_tr.srq_thr_req_head == NULL) {
25264                 sd_tr.srq_thr_req_head = sd_treq;
25265         } else {
25266                 sd_cur = sd_prev = sd_tr.srq_thr_req_head;
25267                 for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) {
25268                         if (sd_cur->dev == dev) {
25269                                 /*
25270                                  * already in Queue so don't log
25271                                  * another request for the device
25272                                  */
25273                                 already_there = 1;
25274                                 break;
25275                         }
25276                         sd_prev = sd_cur;
25277                 }
25278                 if (!already_there) {
25279                         SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: "
25280                             "logging request for %lx\n", dev);
25281                         sd_prev->sd_thr_req_next = sd_treq;
25282                 } else {
25283                         kmem_free(sd_treq, sizeof (struct sd_thr_request));
25284                 }
25285         }
25286 
25287         /*
25288          * Create a kernel thread to do the reservation reclaim and free up this
25289          * thread. We cannot block this thread while we go away to do the
25290          * reservation reclaim
25291          */
25292         if (sd_tr.srq_resv_reclaim_thread == NULL)
25293                 sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0,
25294                     sd_resv_reclaim_thread, NULL,
25295                     0, &p0, TS_RUN, v.v_maxsyspri - 2);
25296 
25297         /* Tell the reservation reclaim thread that it has work to do */
25298         cv_signal(&sd_tr.srq_resv_reclaim_cv);
25299         mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25300 }
25301 
25302 /*
25303  *    Function: sd_resv_reclaim_thread()
25304  *
25305  * Description: This function implements the reservation reclaim operations
25306  *
25307  *   Arguments: arg - the device 'dev_t' is used for context to discriminate
25308  *                    among multiple watches that share this callback function
25309  */
25310 
25311 static void
25312 sd_resv_reclaim_thread()
25313 {
25314         struct sd_lun           *un;
25315         struct sd_thr_request   *sd_mhreq;
25316 
25317         /* Wait for work */
25318         mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25319         if (sd_tr.srq_thr_req_head == NULL) {
25320                 cv_wait(&sd_tr.srq_resv_reclaim_cv,
25321                     &sd_tr.srq_resv_reclaim_mutex);
25322         }
25323 
25324         /* Loop while we have work */
25325         while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) {
25326                 un = ddi_get_soft_state(sd_state,
25327                     SDUNIT(sd_tr.srq_thr_cur_req->dev));
25328                 if (un == NULL) {
25329                         /*
25330                          * softstate structure is NULL so just
25331                          * dequeue the request and continue
25332                          */
25333                         sd_tr.srq_thr_req_head =
25334                             sd_tr.srq_thr_cur_req->sd_thr_req_next;
25335                         kmem_free(sd_tr.srq_thr_cur_req,
25336                             sizeof (struct sd_thr_request));
25337                         continue;
25338                 }
25339 
25340                 /* dequeue the request */
25341                 sd_mhreq = sd_tr.srq_thr_cur_req;
25342                 sd_tr.srq_thr_req_head =
25343                     sd_tr.srq_thr_cur_req->sd_thr_req_next;
25344                 mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25345 
25346                 /*
25347                  * Reclaim reservation only if SD_RESERVE is still set. There
25348                  * may have been a call to MHIOCRELEASE before we got here.
25349                  */
25350                 mutex_enter(SD_MUTEX(un));
25351                 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25352                         /*
25353                          * Note: The SD_LOST_RESERVE flag is cleared before
25354                          * reclaiming the reservation. If this is done after the
25355                          * call to sd_reserve_release a reservation loss in the
25356                          * window between pkt completion of reserve cmd and
25357                          * mutex_enter below may not be recognized
25358                          */
25359                         un->un_resvd_status &= ~SD_LOST_RESERVE;
25360                         mutex_exit(SD_MUTEX(un));
25361 
25362                         if (sd_reserve_release(sd_mhreq->dev,
25363                             SD_RESERVE) == 0) {
25364                                 mutex_enter(SD_MUTEX(un));
25365                                 un->un_resvd_status |= SD_RESERVE;
25366                                 mutex_exit(SD_MUTEX(un));
25367                                 SD_INFO(SD_LOG_IOCTL_MHD, un,
25368                                     "sd_resv_reclaim_thread: "
25369                                     "Reservation Recovered\n");
25370                         } else {
25371                                 mutex_enter(SD_MUTEX(un));
25372                                 un->un_resvd_status |= SD_LOST_RESERVE;
25373                                 mutex_exit(SD_MUTEX(un));
25374                                 SD_INFO(SD_LOG_IOCTL_MHD, un,
25375                                     "sd_resv_reclaim_thread: Failed "
25376                                     "Reservation Recovery\n");
25377                         }
25378                 } else {
25379                         mutex_exit(SD_MUTEX(un));
25380                 }
25381                 mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25382                 ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req);
25383                 kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25384                 sd_mhreq = sd_tr.srq_thr_cur_req = NULL;
25385                 /*
25386                  * wakeup the destroy thread if anyone is waiting on
25387                  * us to complete.
25388                  */
25389                 cv_signal(&sd_tr.srq_inprocess_cv);
25390                 SD_TRACE(SD_LOG_IOCTL_MHD, un,
25391                     "sd_resv_reclaim_thread: cv_signalling current request \n");
25392         }
25393 
25394         /*
25395          * cleanup the sd_tr structure now that this thread will not exist
25396          */
25397         ASSERT(sd_tr.srq_thr_req_head == NULL);
25398         ASSERT(sd_tr.srq_thr_cur_req == NULL);
25399         sd_tr.srq_resv_reclaim_thread = NULL;
25400         mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25401         thread_exit();
25402 }
25403 
25404 
25405 /*
25406  *    Function: sd_rmv_resv_reclaim_req()
25407  *
25408  * Description: This function removes any pending reservation reclaim requests
25409  *              for the specified device.
25410  *
25411  *   Arguments: dev - the device 'dev_t'
25412  */
25413 
25414 static void
25415 sd_rmv_resv_reclaim_req(dev_t dev)
25416 {
25417         struct sd_thr_request *sd_mhreq;
25418         struct sd_thr_request *sd_prev;
25419 
25420         /* Remove a reservation reclaim request from the list */
25421         mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25422         if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) {
25423                 /*
25424                  * We are attempting to reinstate reservation for
25425                  * this device. We wait for sd_reserve_release()
25426                  * to return before we return.
25427                  */
25428                 cv_wait(&sd_tr.srq_inprocess_cv,
25429                     &sd_tr.srq_resv_reclaim_mutex);
25430         } else {
25431                 sd_prev = sd_mhreq = sd_tr.srq_thr_req_head;
25432                 if (sd_mhreq && sd_mhreq->dev == dev) {
25433                         sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next;
25434                         kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25435                         mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25436                         return;
25437                 }
25438                 for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) {
25439                         if (sd_mhreq && sd_mhreq->dev == dev) {
25440                                 break;
25441                         }
25442                         sd_prev = sd_mhreq;
25443                 }
25444                 if (sd_mhreq != NULL) {
25445                         sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next;
25446                         kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25447                 }
25448         }
25449         mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25450 }
25451 
25452 
25453 /*
25454  *    Function: sd_mhd_reset_notify_cb()
25455  *
25456  * Description: This is a call back function for scsi_reset_notify. This
25457  *              function updates the softstate reserved status and logs the
25458  *              reset. The driver scsi watch facility callback function
25459  *              (sd_mhd_watch_cb) and reservation reclaim thread functionality
25460  *              will reclaim the reservation.
25461  *
25462  *   Arguments: arg  - driver soft state (unit) structure
25463  */
25464 
25465 static void
25466 sd_mhd_reset_notify_cb(caddr_t arg)
25467 {
25468         struct sd_lun *un = (struct sd_lun *)arg;
25469 
25470         mutex_enter(SD_MUTEX(un));
25471         if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25472                 un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE);
25473                 SD_INFO(SD_LOG_IOCTL_MHD, un,
25474                     "sd_mhd_reset_notify_cb: Lost Reservation\n");
25475         }
25476         mutex_exit(SD_MUTEX(un));
25477 }
25478 
25479 
25480 /*
25481  *    Function: sd_take_ownership()
25482  *
25483  * Description: This routine implements an algorithm to achieve a stable
25484  *              reservation on disks which don't implement priority reserve,
25485  *              and makes sure that other host lose re-reservation attempts.
25486  *              This algorithm contains of a loop that keeps issuing the RESERVE
25487  *              for some period of time (min_ownership_delay, default 6 seconds)
25488  *              During that loop, it looks to see if there has been a bus device
25489  *              reset or bus reset (both of which cause an existing reservation
25490  *              to be lost). If the reservation is lost issue RESERVE until a
25491  *              period of min_ownership_delay with no resets has gone by, or
25492  *              until max_ownership_delay has expired. This loop ensures that
25493  *              the host really did manage to reserve the device, in spite of
25494  *              resets. The looping for min_ownership_delay (default six
25495  *              seconds) is important to early generation clustering products,
25496  *              Solstice HA 1.x and Sun Cluster 2.x. Those products use an
25497  *              MHIOCENFAILFAST periodic timer of two seconds. By having
25498  *              MHIOCTKOWN issue Reserves in a loop for six seconds, and having
25499  *              MHIOCENFAILFAST poll every two seconds, the idea is that by the
25500  *              time the MHIOCTKOWN ioctl returns, the other host (if any) will
25501  *              have already noticed, via the MHIOCENFAILFAST polling, that it
25502  *              no longer "owns" the disk and will have panicked itself.  Thus,
25503  *              the host issuing the MHIOCTKOWN is assured (with timing
25504  *              dependencies) that by the time it actually starts to use the
25505  *              disk for real work, the old owner is no longer accessing it.
25506  *
25507  *              min_ownership_delay is the minimum amount of time for which the
25508  *              disk must be reserved continuously devoid of resets before the
25509  *              MHIOCTKOWN ioctl will return success.
25510  *
25511  *              max_ownership_delay indicates the amount of time by which the
25512  *              take ownership should succeed or timeout with an error.
25513  *
25514  *   Arguments: dev - the device 'dev_t'
25515  *              *p  - struct containing timing info.
25516  *
25517  * Return Code: 0 for success or error code
25518  */
25519 
25520 static int
25521 sd_take_ownership(dev_t dev, struct mhioctkown *p)
25522 {
25523         struct sd_lun   *un;
25524         int             rval;
25525         int             err;
25526         int             reservation_count   = 0;
25527         int             min_ownership_delay =  6000000; /* in usec */
25528         int             max_ownership_delay = 30000000; /* in usec */
25529         clock_t         start_time;     /* starting time of this algorithm */
25530         clock_t         end_time;       /* time limit for giving up */
25531         clock_t         ownership_time; /* time limit for stable ownership */
25532         clock_t         current_time;
25533         clock_t         previous_current_time;
25534 
25535         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25536                 return (ENXIO);
25537         }
25538 
25539         /*
25540          * Attempt a device reservation. A priority reservation is requested.
25541          */
25542         if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE))
25543             != SD_SUCCESS) {
25544                 SD_ERROR(SD_LOG_IOCTL_MHD, un,
25545                     "sd_take_ownership: return(1)=%d\n", rval);
25546                 return (rval);
25547         }
25548 
25549         /* Update the softstate reserved status to indicate the reservation */
25550         mutex_enter(SD_MUTEX(un));
25551         un->un_resvd_status |= SD_RESERVE;
25552         un->un_resvd_status &=
25553             ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT);
25554         mutex_exit(SD_MUTEX(un));
25555 
25556         if (p != NULL) {
25557                 if (p->min_ownership_delay != 0) {
25558                         min_ownership_delay = p->min_ownership_delay * 1000;
25559                 }
25560                 if (p->max_ownership_delay != 0) {
25561                         max_ownership_delay = p->max_ownership_delay * 1000;
25562                 }
25563         }
25564         SD_INFO(SD_LOG_IOCTL_MHD, un,
25565             "sd_take_ownership: min, max delays: %d, %d\n",
25566             min_ownership_delay, max_ownership_delay);
25567 
25568         start_time = ddi_get_lbolt();
25569         current_time    = start_time;
25570         ownership_time  = current_time + drv_usectohz(min_ownership_delay);
25571         end_time        = start_time + drv_usectohz(max_ownership_delay);
25572 
25573         while (current_time - end_time < 0) {
25574                 delay(drv_usectohz(500000));
25575 
25576                 if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) {
25577                         if ((sd_reserve_release(dev, SD_RESERVE)) != 0) {
25578                                 mutex_enter(SD_MUTEX(un));
25579                                 rval = (un->un_resvd_status &
25580                                     SD_RESERVATION_CONFLICT) ? EACCES : EIO;
25581                                 mutex_exit(SD_MUTEX(un));
25582                                 break;
25583                         }
25584                 }
25585                 previous_current_time = current_time;
25586                 current_time = ddi_get_lbolt();
25587                 mutex_enter(SD_MUTEX(un));
25588                 if (err || (un->un_resvd_status & SD_LOST_RESERVE)) {
25589                         ownership_time = ddi_get_lbolt() +
25590                             drv_usectohz(min_ownership_delay);
25591                         reservation_count = 0;
25592                 } else {
25593                         reservation_count++;
25594                 }
25595                 un->un_resvd_status |= SD_RESERVE;
25596                 un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE);
25597                 mutex_exit(SD_MUTEX(un));
25598 
25599                 SD_INFO(SD_LOG_IOCTL_MHD, un,
25600                     "sd_take_ownership: ticks for loop iteration=%ld, "
25601                     "reservation=%s\n", (current_time - previous_current_time),
25602                     reservation_count ? "ok" : "reclaimed");
25603 
25604                 if (current_time - ownership_time >= 0 &&
25605                     reservation_count >= 4) {
25606                         rval = 0; /* Achieved a stable ownership */
25607                         break;
25608                 }
25609                 if (current_time - end_time >= 0) {
25610                         rval = EACCES; /* No ownership in max possible time */
25611                         break;
25612                 }
25613         }
25614         SD_TRACE(SD_LOG_IOCTL_MHD, un,
25615             "sd_take_ownership: return(2)=%d\n", rval);
25616         return (rval);
25617 }
25618 
25619 
25620 /*
25621  *    Function: sd_reserve_release()
25622  *
25623  * Description: This function builds and sends scsi RESERVE, RELEASE, and
25624  *              PRIORITY RESERVE commands based on a user specified command type
25625  *
25626  *   Arguments: dev - the device 'dev_t'
25627  *              cmd - user specified command type; one of SD_PRIORITY_RESERVE,
25628  *                    SD_RESERVE, SD_RELEASE
25629  *
25630  * Return Code: 0 or Error Code
25631  */
25632 
25633 static int
25634 sd_reserve_release(dev_t dev, int cmd)
25635 {
25636         struct uscsi_cmd        *com = NULL;
25637         struct sd_lun           *un = NULL;
25638         char                    cdb[CDB_GROUP0];
25639         int                     rval;
25640 
25641         ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) ||
25642             (cmd == SD_PRIORITY_RESERVE));
25643 
25644         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25645                 return (ENXIO);
25646         }
25647 
25648         /* instantiate and initialize the command and cdb */
25649         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
25650         bzero(cdb, CDB_GROUP0);
25651         com->uscsi_flags   = USCSI_SILENT;
25652         com->uscsi_timeout = un->un_reserve_release_time;
25653         com->uscsi_cdblen  = CDB_GROUP0;
25654         com->uscsi_cdb          = cdb;
25655         if (cmd == SD_RELEASE) {
25656                 cdb[0] = SCMD_RELEASE;
25657         } else {
25658                 cdb[0] = SCMD_RESERVE;
25659         }
25660 
25661         /* Send the command. */
25662         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
25663             SD_PATH_STANDARD);
25664 
25665         /*
25666          * "break" a reservation that is held by another host, by issuing a
25667          * reset if priority reserve is desired, and we could not get the
25668          * device.
25669          */
25670         if ((cmd == SD_PRIORITY_RESERVE) &&
25671             (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25672                 /*
25673                  * First try to reset the LUN. If we cannot, then try a target
25674                  * reset, followed by a bus reset if the target reset fails.
25675                  */
25676                 int reset_retval = 0;
25677                 if (un->un_f_lun_reset_enabled == TRUE) {
25678                         reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
25679                 }
25680                 if (reset_retval == 0) {
25681                         /* The LUN reset either failed or was not issued */
25682                         reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
25683                 }
25684                 if ((reset_retval == 0) &&
25685                     (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) {
25686                         rval = EIO;
25687                         kmem_free(com, sizeof (*com));
25688                         return (rval);
25689                 }
25690 
25691                 bzero(com, sizeof (struct uscsi_cmd));
25692                 com->uscsi_flags   = USCSI_SILENT;
25693                 com->uscsi_cdb          = cdb;
25694                 com->uscsi_cdblen  = CDB_GROUP0;
25695                 com->uscsi_timeout = 5;
25696 
25697                 /*
25698                  * Reissue the last reserve command, this time without request
25699                  * sense.  Assume that it is just a regular reserve command.
25700                  */
25701                 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
25702                     SD_PATH_STANDARD);
25703         }
25704 
25705         /* Return an error if still getting a reservation conflict. */
25706         if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25707                 rval = EACCES;
25708         }
25709 
25710         kmem_free(com, sizeof (*com));
25711         return (rval);
25712 }
25713 
25714 
25715 #define SD_NDUMP_RETRIES        12
25716 /*
25717  *      System Crash Dump routine
25718  */
25719 
25720 static int
25721 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
25722 {
25723         int             instance;
25724         int             partition;
25725         int             i;
25726         int             err;
25727         struct sd_lun   *un;
25728         struct scsi_pkt *wr_pktp;
25729         struct buf      *wr_bp;
25730         struct buf      wr_buf;
25731         daddr_t         tgt_byte_offset; /* rmw - byte offset for target */
25732         daddr_t         tgt_blkno;      /* rmw - blkno for target */
25733         size_t          tgt_byte_count; /* rmw -  # of bytes to xfer */
25734         size_t          tgt_nblk; /* rmw -  # of tgt blks to xfer */
25735         size_t          io_start_offset;
25736         int             doing_rmw = FALSE;
25737         int             rval;
25738         ssize_t         dma_resid;
25739         daddr_t         oblkno;
25740         diskaddr_t      nblks = 0;
25741         diskaddr_t      start_block;
25742 
25743         instance = SDUNIT(dev);
25744         if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
25745             !SD_IS_VALID_LABEL(un) || ISCD(un)) {
25746                 return (ENXIO);
25747         }
25748 
25749         _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un))
25750 
25751         SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n");
25752 
25753         partition = SDPART(dev);
25754         SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition);
25755 
25756         if (!(NOT_DEVBSIZE(un))) {
25757                 int secmask = 0;
25758                 int blknomask = 0;
25759 
25760                 blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
25761                 secmask = un->un_tgt_blocksize - 1;
25762 
25763                 if (blkno & blknomask) {
25764                         SD_TRACE(SD_LOG_DUMP, un,
25765                             "sddump: dump start block not modulo %d\n",
25766                             un->un_tgt_blocksize);
25767                         return (EINVAL);
25768                 }
25769 
25770                 if ((nblk * DEV_BSIZE) & secmask) {
25771                         SD_TRACE(SD_LOG_DUMP, un,
25772                             "sddump: dump length not modulo %d\n",
25773                             un->un_tgt_blocksize);
25774                         return (EINVAL);
25775                 }
25776 
25777         }
25778 
25779         /* Validate blocks to dump at against partition size. */
25780 
25781         (void) cmlb_partinfo(un->un_cmlbhandle, partition,
25782             &nblks, &start_block, NULL, NULL, (void *)SD_PATH_DIRECT);
25783 
25784         if (NOT_DEVBSIZE(un)) {
25785                 if ((blkno + nblk) > nblks) {
25786                         SD_TRACE(SD_LOG_DUMP, un,
25787                             "sddump: dump range larger than partition: "
25788                             "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
25789                             blkno, nblk, nblks);
25790                         return (EINVAL);
25791                 }
25792         } else {
25793                 if (((blkno / (un->un_tgt_blocksize / DEV_BSIZE)) +
25794                     (nblk / (un->un_tgt_blocksize / DEV_BSIZE))) > nblks) {
25795                         SD_TRACE(SD_LOG_DUMP, un,
25796                             "sddump: dump range larger than partition: "
25797                             "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
25798                             blkno, nblk, nblks);
25799                         return (EINVAL);
25800                 }
25801         }
25802 
25803         mutex_enter(&un->un_pm_mutex);
25804         if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
25805                 struct scsi_pkt *start_pktp;
25806 
25807                 mutex_exit(&un->un_pm_mutex);
25808 
25809                 /*
25810                  * use pm framework to power on HBA 1st
25811                  */
25812                 (void) pm_raise_power(SD_DEVINFO(un), 0,
25813                     SD_PM_STATE_ACTIVE(un));
25814 
25815                 /*
25816                  * Dump no long uses sdpower to power on a device, it's
25817                  * in-line here so it can be done in polled mode.
25818                  */
25819 
25820                 SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n");
25821 
25822                 start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL,
25823                     CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL);
25824 
25825                 if (start_pktp == NULL) {
25826                         /* We were not given a SCSI packet, fail. */
25827                         return (EIO);
25828                 }
25829                 bzero(start_pktp->pkt_cdbp, CDB_GROUP0);
25830                 start_pktp->pkt_cdbp[0] = SCMD_START_STOP;
25831                 start_pktp->pkt_cdbp[4] = SD_TARGET_START;
25832                 start_pktp->pkt_flags = FLAG_NOINTR;
25833 
25834                 mutex_enter(SD_MUTEX(un));
25835                 SD_FILL_SCSI1_LUN(un, start_pktp);
25836                 mutex_exit(SD_MUTEX(un));
25837                 /*
25838                  * Scsi_poll returns 0 (success) if the command completes and
25839                  * the status block is STATUS_GOOD.
25840                  */
25841                 if (sd_scsi_poll(un, start_pktp) != 0) {
25842                         scsi_destroy_pkt(start_pktp);
25843                         return (EIO);
25844                 }
25845                 scsi_destroy_pkt(start_pktp);
25846                 (void) sd_pm_state_change(un, SD_PM_STATE_ACTIVE(un),
25847                     SD_PM_STATE_CHANGE);
25848         } else {
25849                 mutex_exit(&un->un_pm_mutex);
25850         }
25851 
25852         mutex_enter(SD_MUTEX(un));
25853         un->un_throttle = 0;
25854 
25855         /*
25856          * The first time through, reset the specific target device.
25857          * However, when cpr calls sddump we know that sd is in a
25858          * a good state so no bus reset is required.
25859          * Clear sense data via Request Sense cmd.
25860          * In sddump we don't care about allow_bus_device_reset anymore
25861          */
25862 
25863         if ((un->un_state != SD_STATE_SUSPENDED) &&
25864             (un->un_state != SD_STATE_DUMPING)) {
25865 
25866                 New_state(un, SD_STATE_DUMPING);
25867 
25868                 if (un->un_f_is_fibre == FALSE) {
25869                         mutex_exit(SD_MUTEX(un));
25870                         /*
25871                          * Attempt a bus reset for parallel scsi.
25872                          *
25873                          * Note: A bus reset is required because on some host
25874                          * systems (i.e. E420R) a bus device reset is
25875                          * insufficient to reset the state of the target.
25876                          *
25877                          * Note: Don't issue the reset for fibre-channel,
25878                          * because this tends to hang the bus (loop) for
25879                          * too long while everyone is logging out and in
25880                          * and the deadman timer for dumping will fire
25881                          * before the dump is complete.
25882                          */
25883                         if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) {
25884                                 mutex_enter(SD_MUTEX(un));
25885                                 Restore_state(un);
25886                                 mutex_exit(SD_MUTEX(un));
25887                                 return (EIO);
25888                         }
25889 
25890                         /* Delay to give the device some recovery time. */
25891                         drv_usecwait(10000);
25892 
25893                         if (sd_send_polled_RQS(un) == SD_FAILURE) {
25894                                 SD_INFO(SD_LOG_DUMP, un,
25895                                     "sddump: sd_send_polled_RQS failed\n");
25896                         }
25897                         mutex_enter(SD_MUTEX(un));
25898                 }
25899         }
25900 
25901         /*
25902          * Convert the partition-relative block number to a
25903          * disk physical block number.
25904          */
25905         if (NOT_DEVBSIZE(un)) {
25906                 blkno += start_block;
25907         } else {
25908                 blkno = blkno / (un->un_tgt_blocksize / DEV_BSIZE);
25909                 blkno += start_block;
25910         }
25911 
25912         SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno);
25913 
25914 
25915         /*
25916          * Check if the device has a non-512 block size.
25917          */
25918         wr_bp = NULL;
25919         if (NOT_DEVBSIZE(un)) {
25920                 tgt_byte_offset = blkno * un->un_sys_blocksize;
25921                 tgt_byte_count = nblk * un->un_sys_blocksize;
25922                 if ((tgt_byte_offset % un->un_tgt_blocksize) ||
25923                     (tgt_byte_count % un->un_tgt_blocksize)) {
25924                         doing_rmw = TRUE;
25925                         /*
25926                          * Calculate the block number and number of block
25927                          * in terms of the media block size.
25928                          */
25929                         tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
25930                         tgt_nblk =
25931                             ((tgt_byte_offset + tgt_byte_count +
25932                             (un->un_tgt_blocksize - 1)) /
25933                             un->un_tgt_blocksize) - tgt_blkno;
25934 
25935                         /*
25936                          * Invoke the routine which is going to do read part
25937                          * of read-modify-write.
25938                          * Note that this routine returns a pointer to
25939                          * a valid bp in wr_bp.
25940                          */
25941                         err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk,
25942                             &wr_bp);
25943                         if (err) {
25944                                 mutex_exit(SD_MUTEX(un));
25945                                 return (err);
25946                         }
25947                         /*
25948                          * Offset is being calculated as -
25949                          * (original block # * system block size) -
25950                          * (new block # * target block size)
25951                          */
25952                         io_start_offset =
25953                             ((uint64_t)(blkno * un->un_sys_blocksize)) -
25954                             ((uint64_t)(tgt_blkno * un->un_tgt_blocksize));
25955 
25956                         ASSERT(io_start_offset < un->un_tgt_blocksize);
25957                         /*
25958                          * Do the modify portion of read modify write.
25959                          */
25960                         bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset],
25961                             (size_t)nblk * un->un_sys_blocksize);
25962                 } else {
25963                         doing_rmw = FALSE;
25964                         tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
25965                         tgt_nblk = tgt_byte_count / un->un_tgt_blocksize;
25966                 }
25967 
25968                 /* Convert blkno and nblk to target blocks */
25969                 blkno = tgt_blkno;
25970                 nblk = tgt_nblk;
25971         } else {
25972                 wr_bp = &wr_buf;
25973                 bzero(wr_bp, sizeof (struct buf));
25974                 wr_bp->b_flags               = B_BUSY;
25975                 wr_bp->b_un.b_addr   = addr;
25976                 wr_bp->b_bcount              = nblk << DEV_BSHIFT;
25977                 wr_bp->b_resid               = 0;
25978         }
25979 
25980         mutex_exit(SD_MUTEX(un));
25981 
25982         /*
25983          * Obtain a SCSI packet for the write command.
25984          * It should be safe to call the allocator here without
25985          * worrying about being locked for DVMA mapping because
25986          * the address we're passed is already a DVMA mapping
25987          *
25988          * We are also not going to worry about semaphore ownership
25989          * in the dump buffer. Dumping is single threaded at present.
25990          */
25991 
25992         wr_pktp = NULL;
25993 
25994         dma_resid = wr_bp->b_bcount;
25995         oblkno = blkno;
25996 
25997         if (!(NOT_DEVBSIZE(un))) {
25998                 nblk = nblk / (un->un_tgt_blocksize / DEV_BSIZE);
25999         }
26000 
26001         while (dma_resid != 0) {
26002 
26003         for (i = 0; i < SD_NDUMP_RETRIES; i++) {
26004                 wr_bp->b_flags &= ~B_ERROR;
26005 
26006                 if (un->un_partial_dma_supported == 1) {
26007                         blkno = oblkno +
26008                             ((wr_bp->b_bcount - dma_resid) /
26009                             un->un_tgt_blocksize);
26010                         nblk = dma_resid / un->un_tgt_blocksize;
26011 
26012                         if (wr_pktp) {
26013                                 /*
26014                                  * Partial DMA transfers after initial transfer
26015                                  */
26016                                 rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp,
26017                                     blkno, nblk);
26018                         } else {
26019                                 /* Initial transfer */
26020                                 rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26021                                     un->un_pkt_flags, NULL_FUNC, NULL,
26022                                     blkno, nblk);
26023                         }
26024                 } else {
26025                         rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26026                             0, NULL_FUNC, NULL, blkno, nblk);
26027                 }
26028 
26029                 if (rval == 0) {
26030                         /* We were given a SCSI packet, continue. */
26031                         break;
26032                 }
26033 
26034                 if (i == 0) {
26035                         if (wr_bp->b_flags & B_ERROR) {
26036                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26037                                     "no resources for dumping; "
26038                                     "error code: 0x%x, retrying",
26039                                     geterror(wr_bp));
26040                         } else {
26041                                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26042                                     "no resources for dumping; retrying");
26043                         }
26044                 } else if (i != (SD_NDUMP_RETRIES - 1)) {
26045                         if (wr_bp->b_flags & B_ERROR) {
26046                                 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26047                                     "no resources for dumping; error code: "
26048                                     "0x%x, retrying\n", geterror(wr_bp));
26049                         }
26050                 } else {
26051                         if (wr_bp->b_flags & B_ERROR) {
26052                                 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26053                                     "no resources for dumping; "
26054                                     "error code: 0x%x, retries failed, "
26055                                     "giving up.\n", geterror(wr_bp));
26056                         } else {
26057                                 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26058                                     "no resources for dumping; "
26059                                     "retries failed, giving up.\n");
26060                         }
26061                         mutex_enter(SD_MUTEX(un));
26062                         Restore_state(un);
26063                         if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) {
26064                                 mutex_exit(SD_MUTEX(un));
26065                                 scsi_free_consistent_buf(wr_bp);
26066                         } else {
26067                                 mutex_exit(SD_MUTEX(un));
26068                         }
26069                         return (EIO);
26070                 }
26071                 drv_usecwait(10000);
26072         }
26073 
26074         if (un->un_partial_dma_supported == 1) {
26075                 /*
26076                  * save the resid from PARTIAL_DMA
26077                  */
26078                 dma_resid = wr_pktp->pkt_resid;
26079                 if (dma_resid != 0)
26080                         nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid);
26081                 wr_pktp->pkt_resid = 0;
26082         } else {
26083                 dma_resid = 0;
26084         }
26085 
26086         /* SunBug 1222170 */
26087         wr_pktp->pkt_flags = FLAG_NOINTR;
26088 
26089         err = EIO;
26090         for (i = 0; i < SD_NDUMP_RETRIES; i++) {
26091 
26092                 /*
26093                  * Scsi_poll returns 0 (success) if the command completes and
26094                  * the status block is STATUS_GOOD.  We should only check
26095                  * errors if this condition is not true.  Even then we should
26096                  * send our own request sense packet only if we have a check
26097                  * condition and auto request sense has not been performed by
26098                  * the hba.
26099                  */
26100                 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n");
26101 
26102                 if ((sd_scsi_poll(un, wr_pktp) == 0) &&
26103                     (wr_pktp->pkt_resid == 0)) {
26104                         err = SD_SUCCESS;
26105                         break;
26106                 }
26107 
26108                 /*
26109                  * Check CMD_DEV_GONE 1st, give up if device is gone.
26110                  */
26111                 if (wr_pktp->pkt_reason == CMD_DEV_GONE) {
26112                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26113                             "Error while dumping state...Device is gone\n");
26114                         break;
26115                 }
26116 
26117                 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) {
26118                         SD_INFO(SD_LOG_DUMP, un,
26119                             "sddump: write failed with CHECK, try # %d\n", i);
26120                         if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) {
26121                                 (void) sd_send_polled_RQS(un);
26122                         }
26123 
26124                         continue;
26125                 }
26126 
26127                 if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) {
26128                         int reset_retval = 0;
26129 
26130                         SD_INFO(SD_LOG_DUMP, un,
26131                             "sddump: write failed with BUSY, try # %d\n", i);
26132 
26133                         if (un->un_f_lun_reset_enabled == TRUE) {
26134                                 reset_retval = scsi_reset(SD_ADDRESS(un),
26135                                     RESET_LUN);
26136                         }
26137                         if (reset_retval == 0) {
26138                                 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
26139                         }
26140                         (void) sd_send_polled_RQS(un);
26141 
26142                 } else {
26143                         SD_INFO(SD_LOG_DUMP, un,
26144                             "sddump: write failed with 0x%x, try # %d\n",
26145                             SD_GET_PKT_STATUS(wr_pktp), i);
26146                         mutex_enter(SD_MUTEX(un));
26147                         sd_reset_target(un, wr_pktp);
26148                         mutex_exit(SD_MUTEX(un));
26149                 }
26150 
26151                 /*
26152                  * If we are not getting anywhere with lun/target resets,
26153                  * let's reset the bus.
26154                  */
26155                 if (i == SD_NDUMP_RETRIES/2) {
26156                         (void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
26157                         (void) sd_send_polled_RQS(un);
26158                 }
26159         }
26160         }
26161 
26162         scsi_destroy_pkt(wr_pktp);
26163         mutex_enter(SD_MUTEX(un));
26164         if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) {
26165                 mutex_exit(SD_MUTEX(un));
26166                 scsi_free_consistent_buf(wr_bp);
26167         } else {
26168                 mutex_exit(SD_MUTEX(un));
26169         }
26170         SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err);
26171         return (err);
26172 }
26173 
26174 /*
26175  *    Function: sd_scsi_poll()
26176  *
26177  * Description: This is a wrapper for the scsi_poll call.
26178  *
26179  *   Arguments: sd_lun - The unit structure
26180  *              scsi_pkt - The scsi packet being sent to the device.
26181  *
26182  * Return Code: 0 - Command completed successfully with good status
26183  *             -1 - Command failed.  This could indicate a check condition
26184  *                  or other status value requiring recovery action.
26185  *
26186  * NOTE: This code is only called off sddump().
26187  */
26188 
26189 static int
26190 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp)
26191 {
26192         int status;
26193 
26194         ASSERT(un != NULL);
26195         ASSERT(!mutex_owned(SD_MUTEX(un)));
26196         ASSERT(pktp != NULL);
26197 
26198         status = SD_SUCCESS;
26199 
26200         if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) {
26201                 pktp->pkt_flags |= un->un_tagflags;
26202                 pktp->pkt_flags &= ~FLAG_NODISCON;
26203         }
26204 
26205         status = sd_ddi_scsi_poll(pktp);
26206         /*
26207          * Scsi_poll returns 0 (success) if the command completes and the
26208          * status block is STATUS_GOOD.  We should only check errors if this
26209          * condition is not true.  Even then we should send our own request
26210          * sense packet only if we have a check condition and auto
26211          * request sense has not been performed by the hba.
26212          * Don't get RQS data if pkt_reason is CMD_DEV_GONE.
26213          */
26214         if ((status != SD_SUCCESS) &&
26215             (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) &&
26216             (pktp->pkt_state & STATE_ARQ_DONE) == 0 &&
26217             (pktp->pkt_reason != CMD_DEV_GONE))
26218                 (void) sd_send_polled_RQS(un);
26219 
26220         return (status);
26221 }
26222 
26223 /*
26224  *    Function: sd_send_polled_RQS()
26225  *
26226  * Description: This sends the request sense command to a device.
26227  *
26228  *   Arguments: sd_lun - The unit structure
26229  *
26230  * Return Code: 0 - Command completed successfully with good status
26231  *             -1 - Command failed.
26232  *
26233  */
26234 
26235 static int
26236 sd_send_polled_RQS(struct sd_lun *un)
26237 {
26238         int     ret_val;
26239         struct  scsi_pkt        *rqs_pktp;
26240         struct  buf             *rqs_bp;
26241 
26242         ASSERT(un != NULL);
26243         ASSERT(!mutex_owned(SD_MUTEX(un)));
26244 
26245         ret_val = SD_SUCCESS;
26246 
26247         rqs_pktp = un->un_rqs_pktp;
26248         rqs_bp   = un->un_rqs_bp;
26249 
26250         mutex_enter(SD_MUTEX(un));
26251 
26252         if (un->un_sense_isbusy) {
26253                 ret_val = SD_FAILURE;
26254                 mutex_exit(SD_MUTEX(un));
26255                 return (ret_val);
26256         }
26257 
26258         /*
26259          * If the request sense buffer (and packet) is not in use,
26260          * let's set the un_sense_isbusy and send our packet
26261          */
26262         un->un_sense_isbusy  = 1;
26263         rqs_pktp->pkt_resid          = 0;
26264         rqs_pktp->pkt_reason         = 0;
26265         rqs_pktp->pkt_flags |= FLAG_NOINTR;
26266         bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH);
26267 
26268         mutex_exit(SD_MUTEX(un));
26269 
26270         SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at"
26271             " 0x%p\n", rqs_bp->b_un.b_addr);
26272 
26273         /*
26274          * Can't send this to sd_scsi_poll, we wrap ourselves around the
26275          * axle - it has a call into us!
26276          */
26277         if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) {
26278                 SD_INFO(SD_LOG_COMMON, un,
26279                     "sd_send_polled_RQS: RQS failed\n");
26280         }
26281 
26282         SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:",
26283             (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX);
26284 
26285         mutex_enter(SD_MUTEX(un));
26286         un->un_sense_isbusy = 0;
26287         mutex_exit(SD_MUTEX(un));
26288 
26289         return (ret_val);
26290 }
26291 
26292 /*
26293  * Defines needed for localized version of the scsi_poll routine.
26294  */
26295 #define CSEC            10000                   /* usecs */
26296 #define SEC_TO_CSEC     (1000000/CSEC)
26297 
26298 /*
26299  *    Function: sd_ddi_scsi_poll()
26300  *
26301  * Description: Localized version of the scsi_poll routine.  The purpose is to
26302  *              send a scsi_pkt to a device as a polled command.  This version
26303  *              is to ensure more robust handling of transport errors.
26304  *              Specifically this routine cures not ready, coming ready
26305  *              transition for power up and reset of sonoma's.  This can take
26306  *              up to 45 seconds for power-on and 20 seconds for reset of a
26307  *              sonoma lun.
26308  *
26309  *   Arguments: scsi_pkt - The scsi_pkt being sent to a device
26310  *
26311  * Return Code: 0 - Command completed successfully with good status
26312  *             -1 - Command failed.
26313  *
26314  * NOTE: This code is almost identical to scsi_poll, however before 6668774 can
26315  * be fixed (removing this code), we need to determine how to handle the
26316  * KEY_UNIT_ATTENTION condition below in conditions not as limited as sddump().
26317  *
26318  * NOTE: This code is only called off sddump().
26319  */
26320 static int
26321 sd_ddi_scsi_poll(struct scsi_pkt *pkt)
26322 {
26323         int                     rval = -1;
26324         int                     savef;
26325         long                    savet;
26326         void                    (*savec)();
26327         int                     timeout;
26328         int                     busy_count;
26329         int                     poll_delay;
26330         int                     rc;
26331         uint8_t                 *sensep;
26332         struct scsi_arq_status  *arqstat;
26333         extern int              do_polled_io;
26334 
26335         ASSERT(pkt->pkt_scbp);
26336 
26337         /*
26338          * save old flags..
26339          */
26340         savef = pkt->pkt_flags;
26341         savec = pkt->pkt_comp;
26342         savet = pkt->pkt_time;
26343 
26344         pkt->pkt_flags |= FLAG_NOINTR;
26345 
26346         /*
26347          * XXX there is nothing in the SCSA spec that states that we should not
26348          * do a callback for polled cmds; however, removing this will break sd
26349          * and probably other target drivers
26350          */
26351         pkt->pkt_comp = NULL;
26352 
26353         /*
26354          * we don't like a polled command without timeout.
26355          * 60 seconds seems long enough.
26356          */
26357         if (pkt->pkt_time == 0)
26358                 pkt->pkt_time = SCSI_POLL_TIMEOUT;
26359 
26360         /*
26361          * Send polled cmd.
26362          *
26363          * We do some error recovery for various errors.  Tran_busy,
26364          * queue full, and non-dispatched commands are retried every 10 msec.
26365          * as they are typically transient failures.  Busy status and Not
26366          * Ready are retried every second as this status takes a while to
26367          * change.
26368          */
26369         timeout = pkt->pkt_time * SEC_TO_CSEC;
26370 
26371         for (busy_count = 0; busy_count < timeout; busy_count++) {
26372                 /*
26373                  * Initialize pkt status variables.
26374                  */
26375                 *pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0;
26376 
26377                 if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) {
26378                         if (rc != TRAN_BUSY) {
26379                                 /* Transport failed - give up. */
26380                                 break;
26381                         } else {
26382                                 /* Transport busy - try again. */
26383                                 poll_delay = 1 * CSEC;          /* 10 msec. */
26384                         }
26385                 } else {
26386                         /*
26387                          * Transport accepted - check pkt status.
26388                          */
26389                         rc = (*pkt->pkt_scbp) & STATUS_MASK;
26390                         if ((pkt->pkt_reason == CMD_CMPLT) &&
26391                             (rc == STATUS_CHECK) &&
26392                             (pkt->pkt_state & STATE_ARQ_DONE)) {
26393                                 arqstat =
26394                                     (struct scsi_arq_status *)(pkt->pkt_scbp);
26395                                 sensep = (uint8_t *)&arqstat->sts_sensedata;
26396                         } else {
26397                                 sensep = NULL;
26398                         }
26399 
26400                         if ((pkt->pkt_reason == CMD_CMPLT) &&
26401                             (rc == STATUS_GOOD)) {
26402                                 /* No error - we're done */
26403                                 rval = 0;
26404                                 break;
26405 
26406                         } else if (pkt->pkt_reason == CMD_DEV_GONE) {
26407                                 /* Lost connection - give up */
26408                                 break;
26409 
26410                         } else if ((pkt->pkt_reason == CMD_INCOMPLETE) &&
26411                             (pkt->pkt_state == 0)) {
26412                                 /* Pkt not dispatched - try again. */
26413                                 poll_delay = 1 * CSEC;          /* 10 msec. */
26414 
26415                         } else if ((pkt->pkt_reason == CMD_CMPLT) &&
26416                             (rc == STATUS_QFULL)) {
26417                                 /* Queue full - try again. */
26418                                 poll_delay = 1 * CSEC;          /* 10 msec. */
26419 
26420                         } else if ((pkt->pkt_reason == CMD_CMPLT) &&
26421                             (rc == STATUS_BUSY)) {
26422                                 /* Busy - try again. */
26423                                 poll_delay = 100 * CSEC;        /* 1 sec. */
26424                                 busy_count += (SEC_TO_CSEC - 1);
26425 
26426                         } else if ((sensep != NULL) &&
26427                             (scsi_sense_key(sensep) == KEY_UNIT_ATTENTION)) {
26428                                 /*
26429                                  * Unit Attention - try again.
26430                                  * Pretend it took 1 sec.
26431                                  * NOTE: 'continue' avoids poll_delay
26432                                  */
26433                                 busy_count += (SEC_TO_CSEC - 1);
26434                                 continue;
26435 
26436                         } else if ((sensep != NULL) &&
26437                             (scsi_sense_key(sensep) == KEY_NOT_READY) &&
26438                             (scsi_sense_asc(sensep) == 0x04) &&
26439                             (scsi_sense_ascq(sensep) == 0x01)) {
26440                                 /*
26441                                  * Not ready -> ready - try again.
26442                                  * 04h/01h: LUN IS IN PROCESS OF BECOMING READY
26443                                  * ...same as STATUS_BUSY
26444                                  */
26445                                 poll_delay = 100 * CSEC;        /* 1 sec. */
26446                                 busy_count += (SEC_TO_CSEC - 1);
26447 
26448                         } else {
26449                                 /* BAD status - give up. */
26450                                 break;
26451                         }
26452                 }
26453 
26454                 if (((curthread->t_flag & T_INTR_THREAD) == 0) &&
26455                     !do_polled_io) {
26456                         delay(drv_usectohz(poll_delay));
26457                 } else {
26458                         /* we busy wait during cpr_dump or interrupt threads */
26459                         drv_usecwait(poll_delay);
26460                 }
26461         }
26462 
26463         pkt->pkt_flags = savef;
26464         pkt->pkt_comp = savec;
26465         pkt->pkt_time = savet;
26466 
26467         /* return on error */
26468         if (rval)
26469                 return (rval);
26470 
26471         /*
26472          * This is not a performance critical code path.
26473          *
26474          * As an accommodation for scsi_poll callers, to avoid ddi_dma_sync()
26475          * issues associated with looking at DMA memory prior to
26476          * scsi_pkt_destroy(), we scsi_sync_pkt() prior to return.
26477          */
26478         scsi_sync_pkt(pkt);
26479         return (0);
26480 }
26481 
26482 
26483 
26484 /*
26485  *    Function: sd_persistent_reservation_in_read_keys
26486  *
26487  * Description: This routine is the driver entry point for handling CD-ROM
26488  *              multi-host persistent reservation requests (MHIOCGRP_INKEYS)
26489  *              by sending the SCSI-3 PRIN commands to the device.
26490  *              Processes the read keys command response by copying the
26491  *              reservation key information into the user provided buffer.
26492  *              Support for the 32/64 bit _MULTI_DATAMODEL is implemented.
26493  *
26494  *   Arguments: un   -  Pointer to soft state struct for the target.
26495  *              usrp -  user provided pointer to multihost Persistent In Read
26496  *                      Keys structure (mhioc_inkeys_t)
26497  *              flag -  this argument is a pass through to ddi_copyxxx()
26498  *                      directly from the mode argument of ioctl().
26499  *
26500  * Return Code: 0   - Success
26501  *              EACCES
26502  *              ENOTSUP
26503  *              errno return code from sd_send_scsi_cmd()
26504  *
26505  *     Context: Can sleep. Does not return until command is completed.
26506  */
26507 
26508 static int
26509 sd_persistent_reservation_in_read_keys(struct sd_lun *un,
26510     mhioc_inkeys_t *usrp, int flag)
26511 {
26512 #ifdef _MULTI_DATAMODEL
26513         struct mhioc_key_list32 li32;
26514 #endif
26515         sd_prin_readkeys_t      *in;
26516         mhioc_inkeys_t          *ptr;
26517         mhioc_key_list_t        li;
26518         uchar_t                 *data_bufp = NULL;
26519         int                     data_len = 0;
26520         int                     rval = 0;
26521         size_t                  copysz = 0;
26522         sd_ssc_t                *ssc;
26523 
26524         if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) {
26525                 return (EINVAL);
26526         }
26527         bzero(&li, sizeof (mhioc_key_list_t));
26528 
26529         ssc = sd_ssc_init(un);
26530 
26531         /*
26532          * Get the listsize from user
26533          */
26534 #ifdef _MULTI_DATAMODEL
26535         switch (ddi_model_convert_from(flag & FMODELS)) {
26536         case DDI_MODEL_ILP32:
26537                 copysz = sizeof (struct mhioc_key_list32);
26538                 if (ddi_copyin(ptr->li, &li32, copysz, flag)) {
26539                         SD_ERROR(SD_LOG_IOCTL_MHD, un,
26540                             "sd_persistent_reservation_in_read_keys: "
26541                             "failed ddi_copyin: mhioc_key_list32_t\n");
26542                         rval = EFAULT;
26543                         goto done;
26544                 }
26545                 li.listsize = li32.listsize;
26546                 li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list;
26547                 break;
26548 
26549         case DDI_MODEL_NONE:
26550                 copysz = sizeof (mhioc_key_list_t);
26551                 if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26552                         SD_ERROR(SD_LOG_IOCTL_MHD, un,
26553                             "sd_persistent_reservation_in_read_keys: "
26554                             "failed ddi_copyin: mhioc_key_list_t\n");
26555                         rval = EFAULT;
26556                         goto done;
26557                 }
26558                 break;
26559         }
26560 
26561 #else /* ! _MULTI_DATAMODEL */
26562         copysz = sizeof (mhioc_key_list_t);
26563         if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26564                 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26565                     "sd_persistent_reservation_in_read_keys: "
26566                     "failed ddi_copyin: mhioc_key_list_t\n");
26567                 rval = EFAULT;
26568                 goto done;
26569         }
26570 #endif
26571 
26572         data_len  = li.listsize * MHIOC_RESV_KEY_SIZE;
26573         data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t));
26574         data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26575 
26576         rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS,
26577             data_len, data_bufp);
26578         if (rval != 0) {
26579                 if (rval == EIO)
26580                         sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
26581                 else
26582                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
26583                 goto done;
26584         }
26585         in = (sd_prin_readkeys_t *)data_bufp;
26586         ptr->generation = BE_32(in->generation);
26587         li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE;
26588 
26589         /*
26590          * Return the min(listsize, listlen) keys
26591          */
26592 #ifdef _MULTI_DATAMODEL
26593 
26594         switch (ddi_model_convert_from(flag & FMODELS)) {
26595         case DDI_MODEL_ILP32:
26596                 li32.listlen = li.listlen;
26597                 if (ddi_copyout(&li32, ptr->li, copysz, flag)) {
26598                         SD_ERROR(SD_LOG_IOCTL_MHD, un,
26599                             "sd_persistent_reservation_in_read_keys: "
26600                             "failed ddi_copyout: mhioc_key_list32_t\n");
26601                         rval = EFAULT;
26602                         goto done;
26603                 }
26604                 break;
26605 
26606         case DDI_MODEL_NONE:
26607                 if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26608                         SD_ERROR(SD_LOG_IOCTL_MHD, un,
26609                             "sd_persistent_reservation_in_read_keys: "
26610                             "failed ddi_copyout: mhioc_key_list_t\n");
26611                         rval = EFAULT;
26612                         goto done;
26613                 }
26614                 break;
26615         }
26616 
26617 #else /* ! _MULTI_DATAMODEL */
26618 
26619         if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26620                 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26621                     "sd_persistent_reservation_in_read_keys: "
26622                     "failed ddi_copyout: mhioc_key_list_t\n");
26623                 rval = EFAULT;
26624                 goto done;
26625         }
26626 
26627 #endif /* _MULTI_DATAMODEL */
26628 
26629         copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE,
26630             li.listsize * MHIOC_RESV_KEY_SIZE);
26631         if (ddi_copyout(&in->keylist, li.list, copysz, flag)) {
26632                 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26633                     "sd_persistent_reservation_in_read_keys: "
26634                     "failed ddi_copyout: keylist\n");
26635                 rval = EFAULT;
26636         }
26637 done:
26638         sd_ssc_fini(ssc);
26639         kmem_free(data_bufp, data_len);
26640         return (rval);
26641 }
26642 
26643 
26644 /*
26645  *    Function: sd_persistent_reservation_in_read_resv
26646  *
26647  * Description: This routine is the driver entry point for handling CD-ROM
26648  *              multi-host persistent reservation requests (MHIOCGRP_INRESV)
26649  *              by sending the SCSI-3 PRIN commands to the device.
26650  *              Process the read persistent reservations command response by
26651  *              copying the reservation information into the user provided
26652  *              buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented.
26653  *
26654  *   Arguments: un   -  Pointer to soft state struct for the target.
26655  *              usrp -  user provided pointer to multihost Persistent In Read
26656  *                      Keys structure (mhioc_inkeys_t)
26657  *              flag -  this argument is a pass through to ddi_copyxxx()
26658  *                      directly from the mode argument of ioctl().
26659  *
26660  * Return Code: 0   - Success
26661  *              EACCES
26662  *              ENOTSUP
26663  *              errno return code from sd_send_scsi_cmd()
26664  *
26665  *     Context: Can sleep. Does not return until command is completed.
26666  */
26667 
26668 static int
26669 sd_persistent_reservation_in_read_resv(struct sd_lun *un,
26670     mhioc_inresvs_t *usrp, int flag)
26671 {
26672 #ifdef _MULTI_DATAMODEL
26673         struct mhioc_resv_desc_list32 resvlist32;
26674 #endif
26675         sd_prin_readresv_t      *in;
26676         mhioc_inresvs_t         *ptr;
26677         sd_readresv_desc_t      *readresv_ptr;
26678         mhioc_resv_desc_list_t  resvlist;
26679         mhioc_resv_desc_t       resvdesc;
26680         uchar_t                 *data_bufp = NULL;
26681         int                     data_len;
26682         int                     rval = 0;
26683         int                     i;
26684         size_t                  copysz = 0;
26685         mhioc_resv_desc_t       *bufp;
26686         sd_ssc_t                *ssc;
26687 
26688         if ((ptr = usrp) == NULL) {
26689                 return (EINVAL);
26690         }
26691 
26692         ssc = sd_ssc_init(un);
26693 
26694         /*
26695          * Get the listsize from user
26696          */
26697 #ifdef _MULTI_DATAMODEL
26698         switch (ddi_model_convert_from(flag & FMODELS)) {
26699         case DDI_MODEL_ILP32:
26700                 copysz = sizeof (struct mhioc_resv_desc_list32);
26701                 if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) {
26702                         SD_ERROR(SD_LOG_IOCTL_MHD, un,
26703                             "sd_persistent_reservation_in_read_resv: "
26704                             "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26705                         rval = EFAULT;
26706                         goto done;
26707                 }
26708                 resvlist.listsize = resvlist32.listsize;
26709                 resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list;
26710                 break;
26711 
26712         case DDI_MODEL_NONE:
26713                 copysz = sizeof (mhioc_resv_desc_list_t);
26714                 if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26715                         SD_ERROR(SD_LOG_IOCTL_MHD, un,
26716                             "sd_persistent_reservation_in_read_resv: "
26717                             "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26718                         rval = EFAULT;
26719                         goto done;
26720                 }
26721                 break;
26722         }
26723 #else /* ! _MULTI_DATAMODEL */
26724         copysz = sizeof (mhioc_resv_desc_list_t);
26725         if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26726                 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26727                     "sd_persistent_reservation_in_read_resv: "
26728                     "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26729                 rval = EFAULT;
26730                 goto done;
26731         }
26732 #endif /* ! _MULTI_DATAMODEL */
26733 
26734         data_len  = resvlist.listsize * SCSI3_RESV_DESC_LEN;
26735         data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t));
26736         data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26737 
26738         rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_RESV,
26739             data_len, data_bufp);
26740         if (rval != 0) {
26741                 if (rval == EIO)
26742                         sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
26743                 else
26744                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
26745                 goto done;
26746         }
26747         in = (sd_prin_readresv_t *)data_bufp;
26748         ptr->generation = BE_32(in->generation);
26749         resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN;
26750 
26751         /*
26752          * Return the min(listsize, listlen( keys
26753          */
26754 #ifdef _MULTI_DATAMODEL
26755 
26756         switch (ddi_model_convert_from(flag & FMODELS)) {
26757         case DDI_MODEL_ILP32:
26758                 resvlist32.listlen = resvlist.listlen;
26759                 if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) {
26760                         SD_ERROR(SD_LOG_IOCTL_MHD, un,
26761                             "sd_persistent_reservation_in_read_resv: "
26762                             "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26763                         rval = EFAULT;
26764                         goto done;
26765                 }
26766                 break;
26767 
26768         case DDI_MODEL_NONE:
26769                 if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26770                         SD_ERROR(SD_LOG_IOCTL_MHD, un,
26771                             "sd_persistent_reservation_in_read_resv: "
26772                             "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26773                         rval = EFAULT;
26774                         goto done;
26775                 }
26776                 break;
26777         }
26778 
26779 #else /* ! _MULTI_DATAMODEL */
26780 
26781         if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26782                 SD_ERROR(SD_LOG_IOCTL_MHD, un,
26783                     "sd_persistent_reservation_in_read_resv: "
26784                     "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26785                 rval = EFAULT;
26786                 goto done;
26787         }
26788 
26789 #endif /* ! _MULTI_DATAMODEL */
26790 
26791         readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc;
26792         bufp = resvlist.list;
26793         copysz = sizeof (mhioc_resv_desc_t);
26794         for (i = 0; i < min(resvlist.listlen, resvlist.listsize);
26795             i++, readresv_ptr++, bufp++) {
26796 
26797                 bcopy(&readresv_ptr->resvkey, &resvdesc.key,
26798                     MHIOC_RESV_KEY_SIZE);
26799                 resvdesc.type  = readresv_ptr->type;
26800                 resvdesc.scope = readresv_ptr->scope;
26801                 resvdesc.scope_specific_addr =
26802                     BE_32(readresv_ptr->scope_specific_addr);
26803 
26804                 if (ddi_copyout(&resvdesc, bufp, copysz, flag)) {
26805                         SD_ERROR(SD_LOG_IOCTL_MHD, un,
26806                             "sd_persistent_reservation_in_read_resv: "
26807                             "failed ddi_copyout: resvlist\n");
26808                         rval = EFAULT;
26809                         goto done;
26810                 }
26811         }
26812 done:
26813         sd_ssc_fini(ssc);
26814         /* only if data_bufp is allocated, we need to free it */
26815         if (data_bufp) {
26816                 kmem_free(data_bufp, data_len);
26817         }
26818         return (rval);
26819 }
26820 
26821 
26822 /*
26823  *    Function: sr_change_blkmode()
26824  *
26825  * Description: This routine is the driver entry point for handling CD-ROM
26826  *              block mode ioctl requests. Support for returning and changing
26827  *              the current block size in use by the device is implemented. The
26828  *              LBA size is changed via a MODE SELECT Block Descriptor.
26829  *
26830  *              This routine issues a mode sense with an allocation length of
26831  *              12 bytes for the mode page header and a single block descriptor.
26832  *
26833  *   Arguments: dev - the device 'dev_t'
26834  *              cmd - the request type; one of CDROMGBLKMODE (get) or
26835  *                    CDROMSBLKMODE (set)
26836  *              data - current block size or requested block size
26837  *              flag - this argument is a pass through to ddi_copyxxx() directly
26838  *                     from the mode argument of ioctl().
26839  *
26840  * Return Code: the code returned by sd_send_scsi_cmd()
26841  *              EINVAL if invalid arguments are provided
26842  *              EFAULT if ddi_copyxxx() fails
26843  *              ENXIO if fail ddi_get_soft_state
26844  *              EIO if invalid mode sense block descriptor length
26845  *
26846  */
26847 
26848 static int
26849 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag)
26850 {
26851         struct sd_lun                   *un = NULL;
26852         struct mode_header              *sense_mhp, *select_mhp;
26853         struct block_descriptor         *sense_desc, *select_desc;
26854         int                             current_bsize;
26855         int                             rval = EINVAL;
26856         uchar_t                         *sense = NULL;
26857         uchar_t                         *select = NULL;
26858         sd_ssc_t                        *ssc;
26859 
26860         ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE));
26861 
26862         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26863                 return (ENXIO);
26864         }
26865 
26866         /*
26867          * The block length is changed via the Mode Select block descriptor, the
26868          * "Read/Write Error Recovery" mode page (0x1) contents are not actually
26869          * required as part of this routine. Therefore the mode sense allocation
26870          * length is specified to be the length of a mode page header and a
26871          * block descriptor.
26872          */
26873         sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
26874 
26875         ssc = sd_ssc_init(un);
26876         rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
26877             BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD);
26878         sd_ssc_fini(ssc);
26879         if (rval != 0) {
26880                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26881                     "sr_change_blkmode: Mode Sense Failed\n");
26882                 kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26883                 return (rval);
26884         }
26885 
26886         /* Check the block descriptor len to handle only 1 block descriptor */
26887         sense_mhp = (struct mode_header *)sense;
26888         if ((sense_mhp->bdesc_length == 0) ||
26889             (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) {
26890                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26891                     "sr_change_blkmode: Mode Sense returned invalid block"
26892                     " descriptor length\n");
26893                 kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26894                 return (EIO);
26895         }
26896         sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH);
26897         current_bsize = ((sense_desc->blksize_hi << 16) |
26898             (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo);
26899 
26900         /* Process command */
26901         switch (cmd) {
26902         case CDROMGBLKMODE:
26903                 /* Return the block size obtained during the mode sense */
26904                 if (ddi_copyout(&current_bsize, (void *)data,
26905                     sizeof (int), flag) != 0)
26906                         rval = EFAULT;
26907                 break;
26908         case CDROMSBLKMODE:
26909                 /* Validate the requested block size */
26910                 switch (data) {
26911                 case CDROM_BLK_512:
26912                 case CDROM_BLK_1024:
26913                 case CDROM_BLK_2048:
26914                 case CDROM_BLK_2056:
26915                 case CDROM_BLK_2336:
26916                 case CDROM_BLK_2340:
26917                 case CDROM_BLK_2352:
26918                 case CDROM_BLK_2368:
26919                 case CDROM_BLK_2448:
26920                 case CDROM_BLK_2646:
26921                 case CDROM_BLK_2647:
26922                         break;
26923                 default:
26924                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26925                             "sr_change_blkmode: "
26926                             "Block Size '%ld' Not Supported\n", data);
26927                         kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26928                         return (EINVAL);
26929                 }
26930 
26931                 /*
26932                  * The current block size matches the requested block size so
26933                  * there is no need to send the mode select to change the size
26934                  */
26935                 if (current_bsize == data) {
26936                         break;
26937                 }
26938 
26939                 /* Build the select data for the requested block size */
26940                 select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
26941                 select_mhp = (struct mode_header *)select;
26942                 select_desc =
26943                     (struct block_descriptor *)(select + MODE_HEADER_LENGTH);
26944                 /*
26945                  * The LBA size is changed via the block descriptor, so the
26946                  * descriptor is built according to the user data
26947                  */
26948                 select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH;
26949                 select_desc->blksize_hi  = (char)(((data) & 0x00ff0000) >> 16);
26950                 select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8);
26951                 select_desc->blksize_lo  = (char)((data) & 0x000000ff);
26952 
26953                 /* Send the mode select for the requested block size */
26954                 ssc = sd_ssc_init(un);
26955                 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0,
26956                     select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
26957                     SD_PATH_STANDARD);
26958                 sd_ssc_fini(ssc);
26959                 if (rval != 0) {
26960                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26961                             "sr_change_blkmode: Mode Select Failed\n");
26962                         /*
26963                          * The mode select failed for the requested block size,
26964                          * so reset the data for the original block size and
26965                          * send it to the target. The error is indicated by the
26966                          * return value for the failed mode select.
26967                          */
26968                         select_desc->blksize_hi  = sense_desc->blksize_hi;
26969                         select_desc->blksize_mid = sense_desc->blksize_mid;
26970                         select_desc->blksize_lo  = sense_desc->blksize_lo;
26971                         ssc = sd_ssc_init(un);
26972                         (void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0,
26973                             select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
26974                             SD_PATH_STANDARD);
26975                         sd_ssc_fini(ssc);
26976                 } else {
26977                         ASSERT(!mutex_owned(SD_MUTEX(un)));
26978                         mutex_enter(SD_MUTEX(un));
26979                         sd_update_block_info(un, (uint32_t)data, 0);
26980                         mutex_exit(SD_MUTEX(un));
26981                 }
26982                 break;
26983         default:
26984                 /* should not reach here, but check anyway */
26985                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26986                     "sr_change_blkmode: Command '%x' Not Supported\n", cmd);
26987                 rval = EINVAL;
26988                 break;
26989         }
26990 
26991         if (select) {
26992                 kmem_free(select, BUFLEN_CHG_BLK_MODE);
26993         }
26994         if (sense) {
26995                 kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26996         }
26997         return (rval);
26998 }
26999 
27000 
27001 /*
27002  * Note: The following sr_change_speed() and sr_atapi_change_speed() routines
27003  * implement driver support for getting and setting the CD speed. The command
27004  * set used will be based on the device type. If the device has not been
27005  * identified as MMC the Toshiba vendor specific mode page will be used. If
27006  * the device is MMC but does not support the Real Time Streaming feature
27007  * the SET CD SPEED command will be used to set speed and mode page 0x2A will
27008  * be used to read the speed.
27009  */
27010 
27011 /*
27012  *    Function: sr_change_speed()
27013  *
27014  * Description: This routine is the driver entry point for handling CD-ROM
27015  *              drive speed ioctl requests for devices supporting the Toshiba
27016  *              vendor specific drive speed mode page. Support for returning
27017  *              and changing the current drive speed in use by the device is
27018  *              implemented.
27019  *
27020  *   Arguments: dev - the device 'dev_t'
27021  *              cmd - the request type; one of CDROMGDRVSPEED (get) or
27022  *                    CDROMSDRVSPEED (set)
27023  *              data - current drive speed or requested drive speed
27024  *              flag - this argument is a pass through to ddi_copyxxx() directly
27025  *                     from the mode argument of ioctl().
27026  *
27027  * Return Code: the code returned by sd_send_scsi_cmd()
27028  *              EINVAL if invalid arguments are provided
27029  *              EFAULT if ddi_copyxxx() fails
27030  *              ENXIO if fail ddi_get_soft_state
27031  *              EIO if invalid mode sense block descriptor length
27032  */
27033 
27034 static int
27035 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27036 {
27037         struct sd_lun                   *un = NULL;
27038         struct mode_header              *sense_mhp, *select_mhp;
27039         struct mode_speed               *sense_page, *select_page;
27040         int                             current_speed;
27041         int                             rval = EINVAL;
27042         int                             bd_len;
27043         uchar_t                         *sense = NULL;
27044         uchar_t                         *select = NULL;
27045         sd_ssc_t                        *ssc;
27046 
27047         ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27048         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27049                 return (ENXIO);
27050         }
27051 
27052         /*
27053          * Note: The drive speed is being modified here according to a Toshiba
27054          * vendor specific mode page (0x31).
27055          */
27056         sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27057 
27058         ssc = sd_ssc_init(un);
27059         rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
27060             BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED,
27061             SD_PATH_STANDARD);
27062         sd_ssc_fini(ssc);
27063         if (rval != 0) {
27064                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27065                     "sr_change_speed: Mode Sense Failed\n");
27066                 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27067                 return (rval);
27068         }
27069         sense_mhp  = (struct mode_header *)sense;
27070 
27071         /* Check the block descriptor len to handle only 1 block descriptor */
27072         bd_len = sense_mhp->bdesc_length;
27073         if (bd_len > MODE_BLK_DESC_LENGTH) {
27074                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27075                     "sr_change_speed: Mode Sense returned invalid block "
27076                     "descriptor length\n");
27077                 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27078                 return (EIO);
27079         }
27080 
27081         sense_page = (struct mode_speed *)
27082             (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
27083         current_speed = sense_page->speed;
27084 
27085         /* Process command */
27086         switch (cmd) {
27087         case CDROMGDRVSPEED:
27088                 /* Return the drive speed obtained during the mode sense */
27089                 if (current_speed == 0x2) {
27090                         current_speed = CDROM_TWELVE_SPEED;
27091                 }
27092                 if (ddi_copyout(&current_speed, (void *)data,
27093                     sizeof (int), flag) != 0) {
27094                         rval = EFAULT;
27095                 }
27096                 break;
27097         case CDROMSDRVSPEED:
27098                 /* Validate the requested drive speed */
27099                 switch ((uchar_t)data) {
27100                 case CDROM_TWELVE_SPEED:
27101                         data = 0x2;
27102                         /*FALLTHROUGH*/
27103                 case CDROM_NORMAL_SPEED:
27104                 case CDROM_DOUBLE_SPEED:
27105                 case CDROM_QUAD_SPEED:
27106                 case CDROM_MAXIMUM_SPEED:
27107                         break;
27108                 default:
27109                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27110                             "sr_change_speed: "
27111                             "Drive Speed '%d' Not Supported\n", (uchar_t)data);
27112                         kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27113                         return (EINVAL);
27114                 }
27115 
27116                 /*
27117                  * The current drive speed matches the requested drive speed so
27118                  * there is no need to send the mode select to change the speed
27119                  */
27120                 if (current_speed == data) {
27121                         break;
27122                 }
27123 
27124                 /* Build the select data for the requested drive speed */
27125                 select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27126                 select_mhp = (struct mode_header *)select;
27127                 select_mhp->bdesc_length = 0;
27128                 select_page =
27129                     (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27130                 select_page =
27131                     (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27132                 select_page->mode_page.code = CDROM_MODE_SPEED;
27133                 select_page->mode_page.length = 2;
27134                 select_page->speed = (uchar_t)data;
27135 
27136                 /* Send the mode select for the requested block size */
27137                 ssc = sd_ssc_init(un);
27138                 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
27139                     MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27140                     SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27141                 sd_ssc_fini(ssc);
27142                 if (rval != 0) {
27143                         /*
27144                          * The mode select failed for the requested drive speed,
27145                          * so reset the data for the original drive speed and
27146                          * send it to the target. The error is indicated by the
27147                          * return value for the failed mode select.
27148                          */
27149                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27150                             "sr_drive_speed: Mode Select Failed\n");
27151                         select_page->speed = sense_page->speed;
27152                         ssc = sd_ssc_init(un);
27153                         (void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
27154                             MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27155                             SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27156                         sd_ssc_fini(ssc);
27157                 }
27158                 break;
27159         default:
27160                 /* should not reach here, but check anyway */
27161                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27162                     "sr_change_speed: Command '%x' Not Supported\n", cmd);
27163                 rval = EINVAL;
27164                 break;
27165         }
27166 
27167         if (select) {
27168                 kmem_free(select, BUFLEN_MODE_CDROM_SPEED);
27169         }
27170         if (sense) {
27171                 kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27172         }
27173 
27174         return (rval);
27175 }
27176 
27177 
27178 /*
27179  *    Function: sr_atapi_change_speed()
27180  *
27181  * Description: This routine is the driver entry point for handling CD-ROM
27182  *              drive speed ioctl requests for MMC devices that do not support
27183  *              the Real Time Streaming feature (0x107).
27184  *
27185  *              Note: This routine will use the SET SPEED command which may not
27186  *              be supported by all devices.
27187  *
27188  *   Arguments: dev- the device 'dev_t'
27189  *              cmd- the request type; one of CDROMGDRVSPEED (get) or
27190  *                   CDROMSDRVSPEED (set)
27191  *              data- current drive speed or requested drive speed
27192  *              flag- this argument is a pass through to ddi_copyxxx() directly
27193  *                    from the mode argument of ioctl().
27194  *
27195  * Return Code: the code returned by sd_send_scsi_cmd()
27196  *              EINVAL if invalid arguments are provided
27197  *              EFAULT if ddi_copyxxx() fails
27198  *              ENXIO if fail ddi_get_soft_state
27199  *              EIO if invalid mode sense block descriptor length
27200  */
27201 
27202 static int
27203 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27204 {
27205         struct sd_lun                   *un;
27206         struct uscsi_cmd                *com = NULL;
27207         struct mode_header_grp2         *sense_mhp;
27208         uchar_t                         *sense_page;
27209         uchar_t                         *sense = NULL;
27210         char                            cdb[CDB_GROUP5];
27211         int                             bd_len;
27212         int                             current_speed = 0;
27213         int                             max_speed = 0;
27214         int                             rval;
27215         sd_ssc_t                        *ssc;
27216 
27217         ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27218 
27219         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27220                 return (ENXIO);
27221         }
27222 
27223         sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
27224 
27225         ssc = sd_ssc_init(un);
27226         rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
27227             BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP,
27228             SD_PATH_STANDARD);
27229         sd_ssc_fini(ssc);
27230         if (rval != 0) {
27231                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27232                     "sr_atapi_change_speed: Mode Sense Failed\n");
27233                 kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27234                 return (rval);
27235         }
27236 
27237         /* Check the block descriptor len to handle only 1 block descriptor */
27238         sense_mhp = (struct mode_header_grp2 *)sense;
27239         bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
27240         if (bd_len > MODE_BLK_DESC_LENGTH) {
27241                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27242                     "sr_atapi_change_speed: Mode Sense returned invalid "
27243                     "block descriptor length\n");
27244                 kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27245                 return (EIO);
27246         }
27247 
27248         /* Calculate the current and maximum drive speeds */
27249         sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
27250         current_speed = (sense_page[14] << 8) | sense_page[15];
27251         max_speed = (sense_page[8] << 8) | sense_page[9];
27252 
27253         /* Process the command */
27254         switch (cmd) {
27255         case CDROMGDRVSPEED:
27256                 current_speed /= SD_SPEED_1X;
27257                 if (ddi_copyout(&current_speed, (void *)data,
27258                     sizeof (int), flag) != 0)
27259                         rval = EFAULT;
27260                 break;
27261         case CDROMSDRVSPEED:
27262                 /* Convert the speed code to KB/sec */
27263                 switch ((uchar_t)data) {
27264                 case CDROM_NORMAL_SPEED:
27265                         current_speed = SD_SPEED_1X;
27266                         break;
27267                 case CDROM_DOUBLE_SPEED:
27268                         current_speed = 2 * SD_SPEED_1X;
27269                         break;
27270                 case CDROM_QUAD_SPEED:
27271                         current_speed = 4 * SD_SPEED_1X;
27272                         break;
27273                 case CDROM_TWELVE_SPEED:
27274                         current_speed = 12 * SD_SPEED_1X;
27275                         break;
27276                 case CDROM_MAXIMUM_SPEED:
27277                         current_speed = 0xffff;
27278                         break;
27279                 default:
27280                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27281                             "sr_atapi_change_speed: invalid drive speed %d\n",
27282                             (uchar_t)data);
27283                         kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27284                         return (EINVAL);
27285                 }
27286 
27287                 /* Check the request against the drive's max speed. */
27288                 if (current_speed != 0xffff) {
27289                         if (current_speed > max_speed) {
27290                                 kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27291                                 return (EINVAL);
27292                         }
27293                 }
27294 
27295                 /*
27296                  * Build and send the SET SPEED command
27297                  *
27298                  * Note: The SET SPEED (0xBB) command used in this routine is
27299                  * obsolete per the SCSI MMC spec but still supported in the
27300                  * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27301                  * therefore the command is still implemented in this routine.
27302                  */
27303                 bzero(cdb, sizeof (cdb));
27304                 cdb[0] = (char)SCMD_SET_CDROM_SPEED;
27305                 cdb[2] = (uchar_t)(current_speed >> 8);
27306                 cdb[3] = (uchar_t)current_speed;
27307                 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27308                 com->uscsi_cdb          = (caddr_t)cdb;
27309                 com->uscsi_cdblen  = CDB_GROUP5;
27310                 com->uscsi_bufaddr = NULL;
27311                 com->uscsi_buflen  = 0;
27312                 com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT;
27313                 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD);
27314                 break;
27315         default:
27316                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27317                     "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd);
27318                 rval = EINVAL;
27319         }
27320 
27321         if (sense) {
27322                 kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27323         }
27324         if (com) {
27325                 kmem_free(com, sizeof (*com));
27326         }
27327         return (rval);
27328 }
27329 
27330 
27331 /*
27332  *    Function: sr_pause_resume()
27333  *
27334  * Description: This routine is the driver entry point for handling CD-ROM
27335  *              pause/resume ioctl requests. This only affects the audio play
27336  *              operation.
27337  *
27338  *   Arguments: dev - the device 'dev_t'
27339  *              cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used
27340  *                    for setting the resume bit of the cdb.
27341  *
27342  * Return Code: the code returned by sd_send_scsi_cmd()
27343  *              EINVAL if invalid mode specified
27344  *
27345  */
27346 
27347 static int
27348 sr_pause_resume(dev_t dev, int cmd)
27349 {
27350         struct sd_lun           *un;
27351         struct uscsi_cmd        *com;
27352         char                    cdb[CDB_GROUP1];
27353         int                     rval;
27354 
27355         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27356                 return (ENXIO);
27357         }
27358 
27359         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27360         bzero(cdb, CDB_GROUP1);
27361         cdb[0] = SCMD_PAUSE_RESUME;
27362         switch (cmd) {
27363         case CDROMRESUME:
27364                 cdb[8] = 1;
27365                 break;
27366         case CDROMPAUSE:
27367                 cdb[8] = 0;
27368                 break;
27369         default:
27370                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:"
27371                     " Command '%x' Not Supported\n", cmd);
27372                 rval = EINVAL;
27373                 goto done;
27374         }
27375 
27376         com->uscsi_cdb    = cdb;
27377         com->uscsi_cdblen = CDB_GROUP1;
27378         com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27379 
27380         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27381             SD_PATH_STANDARD);
27382 
27383 done:
27384         kmem_free(com, sizeof (*com));
27385         return (rval);
27386 }
27387 
27388 
27389 /*
27390  *    Function: sr_play_msf()
27391  *
27392  * Description: This routine is the driver entry point for handling CD-ROM
27393  *              ioctl requests to output the audio signals at the specified
27394  *              starting address and continue the audio play until the specified
27395  *              ending address (CDROMPLAYMSF) The address is in Minute Second
27396  *              Frame (MSF) format.
27397  *
27398  *   Arguments: dev     - the device 'dev_t'
27399  *              data    - pointer to user provided audio msf structure,
27400  *                        specifying start/end addresses.
27401  *              flag    - this argument is a pass through to ddi_copyxxx()
27402  *                        directly from the mode argument of ioctl().
27403  *
27404  * Return Code: the code returned by sd_send_scsi_cmd()
27405  *              EFAULT if ddi_copyxxx() fails
27406  *              ENXIO if fail ddi_get_soft_state
27407  *              EINVAL if data pointer is NULL
27408  */
27409 
27410 static int
27411 sr_play_msf(dev_t dev, caddr_t data, int flag)
27412 {
27413         struct sd_lun           *un;
27414         struct uscsi_cmd        *com;
27415         struct cdrom_msf        msf_struct;
27416         struct cdrom_msf        *msf = &msf_struct;
27417         char                    cdb[CDB_GROUP1];
27418         int                     rval;
27419 
27420         if (data == NULL) {
27421                 return (EINVAL);
27422         }
27423 
27424         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27425                 return (ENXIO);
27426         }
27427 
27428         if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) {
27429                 return (EFAULT);
27430         }
27431 
27432         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27433         bzero(cdb, CDB_GROUP1);
27434         cdb[0] = SCMD_PLAYAUDIO_MSF;
27435         if (un->un_f_cfg_playmsf_bcd == TRUE) {
27436                 cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0);
27437                 cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0);
27438                 cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0);
27439                 cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1);
27440                 cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1);
27441                 cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1);
27442         } else {
27443                 cdb[3] = msf->cdmsf_min0;
27444                 cdb[4] = msf->cdmsf_sec0;
27445                 cdb[5] = msf->cdmsf_frame0;
27446                 cdb[6] = msf->cdmsf_min1;
27447                 cdb[7] = msf->cdmsf_sec1;
27448                 cdb[8] = msf->cdmsf_frame1;
27449         }
27450         com->uscsi_cdb    = cdb;
27451         com->uscsi_cdblen = CDB_GROUP1;
27452         com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27453         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27454             SD_PATH_STANDARD);
27455         kmem_free(com, sizeof (*com));
27456         return (rval);
27457 }
27458 
27459 
27460 /*
27461  *    Function: sr_play_trkind()
27462  *
27463  * Description: This routine is the driver entry point for handling CD-ROM
27464  *              ioctl requests to output the audio signals at the specified
27465  *              starting address and continue the audio play until the specified
27466  *              ending address (CDROMPLAYTRKIND). The address is in Track Index
27467  *              format.
27468  *
27469  *   Arguments: dev     - the device 'dev_t'
27470  *              data    - pointer to user provided audio track/index structure,
27471  *                        specifying start/end addresses.
27472  *              flag    - this argument is a pass through to ddi_copyxxx()
27473  *                        directly from the mode argument of ioctl().
27474  *
27475  * Return Code: the code returned by sd_send_scsi_cmd()
27476  *              EFAULT if ddi_copyxxx() fails
27477  *              ENXIO if fail ddi_get_soft_state
27478  *              EINVAL if data pointer is NULL
27479  */
27480 
27481 static int
27482 sr_play_trkind(dev_t dev, caddr_t data, int flag)
27483 {
27484         struct cdrom_ti         ti_struct;
27485         struct cdrom_ti         *ti = &ti_struct;
27486         struct uscsi_cmd        *com = NULL;
27487         char                    cdb[CDB_GROUP1];
27488         int                     rval;
27489 
27490         if (data == NULL) {
27491                 return (EINVAL);
27492         }
27493 
27494         if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) {
27495                 return (EFAULT);
27496         }
27497 
27498         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27499         bzero(cdb, CDB_GROUP1);
27500         cdb[0] = SCMD_PLAYAUDIO_TI;
27501         cdb[4] = ti->cdti_trk0;
27502         cdb[5] = ti->cdti_ind0;
27503         cdb[7] = ti->cdti_trk1;
27504         cdb[8] = ti->cdti_ind1;
27505         com->uscsi_cdb    = cdb;
27506         com->uscsi_cdblen = CDB_GROUP1;
27507         com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27508         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27509             SD_PATH_STANDARD);
27510         kmem_free(com, sizeof (*com));
27511         return (rval);
27512 }
27513 
27514 
27515 /*
27516  *    Function: sr_read_all_subcodes()
27517  *
27518  * Description: This routine is the driver entry point for handling CD-ROM
27519  *              ioctl requests to return raw subcode data while the target is
27520  *              playing audio (CDROMSUBCODE).
27521  *
27522  *   Arguments: dev     - the device 'dev_t'
27523  *              data    - pointer to user provided cdrom subcode structure,
27524  *                        specifying the transfer length and address.
27525  *              flag    - this argument is a pass through to ddi_copyxxx()
27526  *                        directly from the mode argument of ioctl().
27527  *
27528  * Return Code: the code returned by sd_send_scsi_cmd()
27529  *              EFAULT if ddi_copyxxx() fails
27530  *              ENXIO if fail ddi_get_soft_state
27531  *              EINVAL if data pointer is NULL
27532  */
27533 
27534 static int
27535 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag)
27536 {
27537         struct sd_lun           *un = NULL;
27538         struct uscsi_cmd        *com = NULL;
27539         struct cdrom_subcode    *subcode = NULL;
27540         int                     rval;
27541         size_t                  buflen;
27542         char                    cdb[CDB_GROUP5];
27543 
27544 #ifdef _MULTI_DATAMODEL
27545         /* To support ILP32 applications in an LP64 world */
27546         struct cdrom_subcode32          cdrom_subcode32;
27547         struct cdrom_subcode32          *cdsc32 = &cdrom_subcode32;
27548 #endif
27549         if (data == NULL) {
27550                 return (EINVAL);
27551         }
27552 
27553         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27554                 return (ENXIO);
27555         }
27556 
27557         subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP);
27558 
27559 #ifdef _MULTI_DATAMODEL
27560         switch (ddi_model_convert_from(flag & FMODELS)) {
27561         case DDI_MODEL_ILP32:
27562                 if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) {
27563                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27564                             "sr_read_all_subcodes: ddi_copyin Failed\n");
27565                         kmem_free(subcode, sizeof (struct cdrom_subcode));
27566                         return (EFAULT);
27567                 }
27568                 /* Convert the ILP32 uscsi data from the application to LP64 */
27569                 cdrom_subcode32tocdrom_subcode(cdsc32, subcode);
27570                 break;
27571         case DDI_MODEL_NONE:
27572                 if (ddi_copyin(data, subcode,
27573                     sizeof (struct cdrom_subcode), flag)) {
27574                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27575                             "sr_read_all_subcodes: ddi_copyin Failed\n");
27576                         kmem_free(subcode, sizeof (struct cdrom_subcode));
27577                         return (EFAULT);
27578                 }
27579                 break;
27580         }
27581 #else /* ! _MULTI_DATAMODEL */
27582         if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) {
27583                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27584                     "sr_read_all_subcodes: ddi_copyin Failed\n");
27585                 kmem_free(subcode, sizeof (struct cdrom_subcode));
27586                 return (EFAULT);
27587         }
27588 #endif /* _MULTI_DATAMODEL */
27589 
27590         /*
27591          * Since MMC-2 expects max 3 bytes for length, check if the
27592          * length input is greater than 3 bytes
27593          */
27594         if ((subcode->cdsc_length & 0xFF000000) != 0) {
27595                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27596                     "sr_read_all_subcodes: "
27597                     "cdrom transfer length too large: %d (limit %d)\n",
27598                     subcode->cdsc_length, 0xFFFFFF);
27599                 kmem_free(subcode, sizeof (struct cdrom_subcode));
27600                 return (EINVAL);
27601         }
27602 
27603         buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length;
27604         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27605         bzero(cdb, CDB_GROUP5);
27606 
27607         if (un->un_f_mmc_cap == TRUE) {
27608                 cdb[0] = (char)SCMD_READ_CD;
27609                 cdb[2] = (char)0xff;
27610                 cdb[3] = (char)0xff;
27611                 cdb[4] = (char)0xff;
27612                 cdb[5] = (char)0xff;
27613                 cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27614                 cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27615                 cdb[8] = ((subcode->cdsc_length) & 0x000000ff);
27616                 cdb[10] = 1;
27617         } else {
27618                 /*
27619                  * Note: A vendor specific command (0xDF) is being used here to
27620                  * request a read of all subcodes.
27621                  */
27622                 cdb[0] = (char)SCMD_READ_ALL_SUBCODES;
27623                 cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24);
27624                 cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27625                 cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27626                 cdb[9] = ((subcode->cdsc_length) & 0x000000ff);
27627         }
27628         com->uscsi_cdb          = cdb;
27629         com->uscsi_cdblen  = CDB_GROUP5;
27630         com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr;
27631         com->uscsi_buflen  = buflen;
27632         com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27633         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
27634             SD_PATH_STANDARD);
27635         kmem_free(subcode, sizeof (struct cdrom_subcode));
27636         kmem_free(com, sizeof (*com));
27637         return (rval);
27638 }
27639 
27640 
27641 /*
27642  *    Function: sr_read_subchannel()
27643  *
27644  * Description: This routine is the driver entry point for handling CD-ROM
27645  *              ioctl requests to return the Q sub-channel data of the CD
27646  *              current position block. (CDROMSUBCHNL) The data includes the
27647  *              track number, index number, absolute CD-ROM address (LBA or MSF
27648  *              format per the user) , track relative CD-ROM address (LBA or MSF
27649  *              format per the user), control data and audio status.
27650  *
27651  *   Arguments: dev     - the device 'dev_t'
27652  *              data    - pointer to user provided cdrom sub-channel structure
27653  *              flag    - this argument is a pass through to ddi_copyxxx()
27654  *                        directly from the mode argument of ioctl().
27655  *
27656  * Return Code: the code returned by sd_send_scsi_cmd()
27657  *              EFAULT if ddi_copyxxx() fails
27658  *              ENXIO if fail ddi_get_soft_state
27659  *              EINVAL if data pointer is NULL
27660  */
27661 
27662 static int
27663 sr_read_subchannel(dev_t dev, caddr_t data, int flag)
27664 {
27665         struct sd_lun           *un;
27666         struct uscsi_cmd        *com;
27667         struct cdrom_subchnl    subchanel;
27668         struct cdrom_subchnl    *subchnl = &subchanel;
27669         char                    cdb[CDB_GROUP1];
27670         caddr_t                 buffer;
27671         int                     rval;
27672 
27673         if (data == NULL) {
27674                 return (EINVAL);
27675         }
27676 
27677         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27678             (un->un_state == SD_STATE_OFFLINE)) {
27679                 return (ENXIO);
27680         }
27681 
27682         if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) {
27683                 return (EFAULT);
27684         }
27685 
27686         buffer = kmem_zalloc((size_t)16, KM_SLEEP);
27687         bzero(cdb, CDB_GROUP1);
27688         cdb[0] = SCMD_READ_SUBCHANNEL;
27689         /* Set the MSF bit based on the user requested address format */
27690         cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02;
27691         /*
27692          * Set the Q bit in byte 2 to indicate that Q sub-channel data be
27693          * returned
27694          */
27695         cdb[2] = 0x40;
27696         /*
27697          * Set byte 3 to specify the return data format. A value of 0x01
27698          * indicates that the CD-ROM current position should be returned.
27699          */
27700         cdb[3] = 0x01;
27701         cdb[8] = 0x10;
27702         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27703         com->uscsi_cdb          = cdb;
27704         com->uscsi_cdblen  = CDB_GROUP1;
27705         com->uscsi_bufaddr = buffer;
27706         com->uscsi_buflen  = 16;
27707         com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27708         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27709             SD_PATH_STANDARD);
27710         if (rval != 0) {
27711                 kmem_free(buffer, 16);
27712                 kmem_free(com, sizeof (*com));
27713                 return (rval);
27714         }
27715 
27716         /* Process the returned Q sub-channel data */
27717         subchnl->cdsc_audiostatus = buffer[1];
27718         subchnl->cdsc_adr    = (buffer[5] & 0xF0) >> 4;
27719         subchnl->cdsc_ctrl   = (buffer[5] & 0x0F);
27720         subchnl->cdsc_trk    = buffer[6];
27721         subchnl->cdsc_ind    = buffer[7];
27722         if (subchnl->cdsc_format & CDROM_LBA) {
27723                 subchnl->cdsc_absaddr.lba =
27724                     ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27725                     ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27726                 subchnl->cdsc_reladdr.lba =
27727                     ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) +
27728                     ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]);
27729         } else if (un->un_f_cfg_readsub_bcd == TRUE) {
27730                 subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]);
27731                 subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]);
27732                 subchnl->cdsc_absaddr.msf.frame  = BCD_TO_BYTE(buffer[11]);
27733                 subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]);
27734                 subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]);
27735                 subchnl->cdsc_reladdr.msf.frame  = BCD_TO_BYTE(buffer[15]);
27736         } else {
27737                 subchnl->cdsc_absaddr.msf.minute = buffer[9];
27738                 subchnl->cdsc_absaddr.msf.second = buffer[10];
27739                 subchnl->cdsc_absaddr.msf.frame  = buffer[11];
27740                 subchnl->cdsc_reladdr.msf.minute = buffer[13];
27741                 subchnl->cdsc_reladdr.msf.second = buffer[14];
27742                 subchnl->cdsc_reladdr.msf.frame  = buffer[15];
27743         }
27744         kmem_free(buffer, 16);
27745         kmem_free(com, sizeof (*com));
27746         if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag)
27747             != 0) {
27748                 return (EFAULT);
27749         }
27750         return (rval);
27751 }
27752 
27753 
27754 /*
27755  *    Function: sr_read_tocentry()
27756  *
27757  * Description: This routine is the driver entry point for handling CD-ROM
27758  *              ioctl requests to read from the Table of Contents (TOC)
27759  *              (CDROMREADTOCENTRY). This routine provides the ADR and CTRL
27760  *              fields, the starting address (LBA or MSF format per the user)
27761  *              and the data mode if the user specified track is a data track.
27762  *
27763  *              Note: The READ HEADER (0x44) command used in this routine is
27764  *              obsolete per the SCSI MMC spec but still supported in the
27765  *              MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27766  *              therefore the command is still implemented in this routine.
27767  *
27768  *   Arguments: dev     - the device 'dev_t'
27769  *              data    - pointer to user provided toc entry structure,
27770  *                        specifying the track # and the address format
27771  *                        (LBA or MSF).
27772  *              flag    - this argument is a pass through to ddi_copyxxx()
27773  *                        directly from the mode argument of ioctl().
27774  *
27775  * Return Code: the code returned by sd_send_scsi_cmd()
27776  *              EFAULT if ddi_copyxxx() fails
27777  *              ENXIO if fail ddi_get_soft_state
27778  *              EINVAL if data pointer is NULL
27779  */
27780 
27781 static int
27782 sr_read_tocentry(dev_t dev, caddr_t data, int flag)
27783 {
27784         struct sd_lun           *un = NULL;
27785         struct uscsi_cmd        *com;
27786         struct cdrom_tocentry   toc_entry;
27787         struct cdrom_tocentry   *entry = &toc_entry;
27788         caddr_t                 buffer;
27789         int                     rval;
27790         char                    cdb[CDB_GROUP1];
27791 
27792         if (data == NULL) {
27793                 return (EINVAL);
27794         }
27795 
27796         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27797             (un->un_state == SD_STATE_OFFLINE)) {
27798                 return (ENXIO);
27799         }
27800 
27801         if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) {
27802                 return (EFAULT);
27803         }
27804 
27805         /* Validate the requested track and address format */
27806         if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) {
27807                 return (EINVAL);
27808         }
27809 
27810         if (entry->cdte_track == 0) {
27811                 return (EINVAL);
27812         }
27813 
27814         buffer = kmem_zalloc((size_t)12, KM_SLEEP);
27815         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27816         bzero(cdb, CDB_GROUP1);
27817 
27818         cdb[0] = SCMD_READ_TOC;
27819         /* Set the MSF bit based on the user requested address format  */
27820         cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2);
27821         if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
27822                 cdb[6] = BYTE_TO_BCD(entry->cdte_track);
27823         } else {
27824                 cdb[6] = entry->cdte_track;
27825         }
27826 
27827         /*
27828          * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
27829          * (4 byte TOC response header + 8 byte track descriptor)
27830          */
27831         cdb[8] = 12;
27832         com->uscsi_cdb          = cdb;
27833         com->uscsi_cdblen  = CDB_GROUP1;
27834         com->uscsi_bufaddr = buffer;
27835         com->uscsi_buflen  = 0x0C;
27836         com->uscsi_flags   = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ);
27837         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27838             SD_PATH_STANDARD);
27839         if (rval != 0) {
27840                 kmem_free(buffer, 12);
27841                 kmem_free(com, sizeof (*com));
27842                 return (rval);
27843         }
27844 
27845         /* Process the toc entry */
27846         entry->cdte_adr              = (buffer[5] & 0xF0) >> 4;
27847         entry->cdte_ctrl     = (buffer[5] & 0x0F);
27848         if (entry->cdte_format & CDROM_LBA) {
27849                 entry->cdte_addr.lba =
27850                     ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27851                     ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27852         } else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) {
27853                 entry->cdte_addr.msf.minute  = BCD_TO_BYTE(buffer[9]);
27854                 entry->cdte_addr.msf.second  = BCD_TO_BYTE(buffer[10]);
27855                 entry->cdte_addr.msf.frame   = BCD_TO_BYTE(buffer[11]);
27856                 /*
27857                  * Send a READ TOC command using the LBA address format to get
27858                  * the LBA for the track requested so it can be used in the
27859                  * READ HEADER request
27860                  *
27861                  * Note: The MSF bit of the READ HEADER command specifies the
27862                  * output format. The block address specified in that command
27863                  * must be in LBA format.
27864                  */
27865                 cdb[1] = 0;
27866                 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27867                     SD_PATH_STANDARD);
27868                 if (rval != 0) {
27869                         kmem_free(buffer, 12);
27870                         kmem_free(com, sizeof (*com));
27871                         return (rval);
27872                 }
27873         } else {
27874                 entry->cdte_addr.msf.minute  = buffer[9];
27875                 entry->cdte_addr.msf.second  = buffer[10];
27876                 entry->cdte_addr.msf.frame   = buffer[11];
27877                 /*
27878                  * Send a READ TOC command using the LBA address format to get
27879                  * the LBA for the track requested so it can be used in the
27880                  * READ HEADER request
27881                  *
27882                  * Note: The MSF bit of the READ HEADER command specifies the
27883                  * output format. The block address specified in that command
27884                  * must be in LBA format.
27885                  */
27886                 cdb[1] = 0;
27887                 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27888                     SD_PATH_STANDARD);
27889                 if (rval != 0) {
27890                         kmem_free(buffer, 12);
27891                         kmem_free(com, sizeof (*com));
27892                         return (rval);
27893                 }
27894         }
27895 
27896         /*
27897          * Build and send the READ HEADER command to determine the data mode of
27898          * the user specified track.
27899          */
27900         if ((entry->cdte_ctrl & CDROM_DATA_TRACK) &&
27901             (entry->cdte_track != CDROM_LEADOUT)) {
27902                 bzero(cdb, CDB_GROUP1);
27903                 cdb[0] = SCMD_READ_HEADER;
27904                 cdb[2] = buffer[8];
27905                 cdb[3] = buffer[9];
27906                 cdb[4] = buffer[10];
27907                 cdb[5] = buffer[11];
27908                 cdb[8] = 0x08;
27909                 com->uscsi_buflen = 0x08;
27910                 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27911                     SD_PATH_STANDARD);
27912                 if (rval == 0) {
27913                         entry->cdte_datamode = buffer[0];
27914                 } else {
27915                         /*
27916                          * READ HEADER command failed, since this is
27917                          * obsoleted in one spec, its better to return
27918                          * -1 for an invlid track so that we can still
27919                          * receive the rest of the TOC data.
27920                          */
27921                         entry->cdte_datamode = (uchar_t)-1;
27922                 }
27923         } else {
27924                 entry->cdte_datamode = (uchar_t)-1;
27925         }
27926 
27927         kmem_free(buffer, 12);
27928         kmem_free(com, sizeof (*com));
27929         if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0)
27930                 return (EFAULT);
27931 
27932         return (rval);
27933 }
27934 
27935 
27936 /*
27937  *    Function: sr_read_tochdr()
27938  *
27939  * Description: This routine is the driver entry point for handling CD-ROM
27940  *              ioctl requests to read the Table of Contents (TOC) header
27941  *              (CDROMREADTOHDR). The TOC header consists of the disk starting
27942  *              and ending track numbers
27943  *
27944  *   Arguments: dev     - the device 'dev_t'
27945  *              data    - pointer to user provided toc header structure,
27946  *                        specifying the starting and ending track numbers.
27947  *              flag    - this argument is a pass through to ddi_copyxxx()
27948  *                        directly from the mode argument of ioctl().
27949  *
27950  * Return Code: the code returned by sd_send_scsi_cmd()
27951  *              EFAULT if ddi_copyxxx() fails
27952  *              ENXIO if fail ddi_get_soft_state
27953  *              EINVAL if data pointer is NULL
27954  */
27955 
27956 static int
27957 sr_read_tochdr(dev_t dev, caddr_t data, int flag)
27958 {
27959         struct sd_lun           *un;
27960         struct uscsi_cmd        *com;
27961         struct cdrom_tochdr     toc_header;
27962         struct cdrom_tochdr     *hdr = &toc_header;
27963         char                    cdb[CDB_GROUP1];
27964         int                     rval;
27965         caddr_t                 buffer;
27966 
27967         if (data == NULL) {
27968                 return (EINVAL);
27969         }
27970 
27971         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27972             (un->un_state == SD_STATE_OFFLINE)) {
27973                 return (ENXIO);
27974         }
27975 
27976         buffer = kmem_zalloc(4, KM_SLEEP);
27977         bzero(cdb, CDB_GROUP1);
27978         cdb[0] = SCMD_READ_TOC;
27979         /*
27980          * Specifying a track number of 0x00 in the READ TOC command indicates
27981          * that the TOC header should be returned
27982          */
27983         cdb[6] = 0x00;
27984         /*
27985          * Bytes 7 & 8 are the 4 byte allocation length for TOC header.
27986          * (2 byte data len + 1 byte starting track # + 1 byte ending track #)
27987          */
27988         cdb[8] = 0x04;
27989         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27990         com->uscsi_cdb          = cdb;
27991         com->uscsi_cdblen  = CDB_GROUP1;
27992         com->uscsi_bufaddr = buffer;
27993         com->uscsi_buflen  = 0x04;
27994         com->uscsi_timeout = 300;
27995         com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27996 
27997         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27998             SD_PATH_STANDARD);
27999         if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
28000                 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]);
28001                 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]);
28002         } else {
28003                 hdr->cdth_trk0 = buffer[2];
28004                 hdr->cdth_trk1 = buffer[3];
28005         }
28006         kmem_free(buffer, 4);
28007         kmem_free(com, sizeof (*com));
28008         if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) {
28009                 return (EFAULT);
28010         }
28011         return (rval);
28012 }
28013 
28014 
28015 /*
28016  * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(),
28017  * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for
28018  * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data,
28019  * digital audio and extended architecture digital audio. These modes are
28020  * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3
28021  * MMC specs.
28022  *
28023  * In addition to support for the various data formats these routines also
28024  * include support for devices that implement only the direct access READ
28025  * commands (0x08, 0x28), devices that implement the READ_CD commands
28026  * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and
28027  * READ CDXA commands (0xD8, 0xDB)
28028  */
28029 
28030 /*
28031  *    Function: sr_read_mode1()
28032  *
28033  * Description: This routine is the driver entry point for handling CD-ROM
28034  *              ioctl read mode1 requests (CDROMREADMODE1).
28035  *
28036  *   Arguments: dev     - the device 'dev_t'
28037  *              data    - pointer to user provided cd read structure specifying
28038  *                        the lba buffer address and length.
28039  *              flag    - this argument is a pass through to ddi_copyxxx()
28040  *                        directly from the mode argument of ioctl().
28041  *
28042  * Return Code: the code returned by sd_send_scsi_cmd()
28043  *              EFAULT if ddi_copyxxx() fails
28044  *              ENXIO if fail ddi_get_soft_state
28045  *              EINVAL if data pointer is NULL
28046  */
28047 
28048 static int
28049 sr_read_mode1(dev_t dev, caddr_t data, int flag)
28050 {
28051         struct sd_lun           *un;
28052         struct cdrom_read       mode1_struct;
28053         struct cdrom_read       *mode1 = &mode1_struct;
28054         int                     rval;
28055         sd_ssc_t                *ssc;
28056 
28057 #ifdef _MULTI_DATAMODEL
28058         /* To support ILP32 applications in an LP64 world */
28059         struct cdrom_read32     cdrom_read32;
28060         struct cdrom_read32     *cdrd32 = &cdrom_read32;
28061 #endif /* _MULTI_DATAMODEL */
28062 
28063         if (data == NULL) {
28064                 return (EINVAL);
28065         }
28066 
28067         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28068             (un->un_state == SD_STATE_OFFLINE)) {
28069                 return (ENXIO);
28070         }
28071 
28072         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28073             "sd_read_mode1: entry: un:0x%p\n", un);
28074 
28075 #ifdef _MULTI_DATAMODEL
28076         switch (ddi_model_convert_from(flag & FMODELS)) {
28077         case DDI_MODEL_ILP32:
28078                 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28079                         return (EFAULT);
28080                 }
28081                 /* Convert the ILP32 uscsi data from the application to LP64 */
28082                 cdrom_read32tocdrom_read(cdrd32, mode1);
28083                 break;
28084         case DDI_MODEL_NONE:
28085                 if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28086                         return (EFAULT);
28087                 }
28088         }
28089 #else /* ! _MULTI_DATAMODEL */
28090         if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28091                 return (EFAULT);
28092         }
28093 #endif /* _MULTI_DATAMODEL */
28094 
28095         ssc = sd_ssc_init(un);
28096         rval = sd_send_scsi_READ(ssc, mode1->cdread_bufaddr,
28097             mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD);
28098         sd_ssc_fini(ssc);
28099 
28100         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28101             "sd_read_mode1: exit: un:0x%p\n", un);
28102 
28103         return (rval);
28104 }
28105 
28106 
28107 /*
28108  *    Function: sr_read_cd_mode2()
28109  *
28110  * Description: This routine is the driver entry point for handling CD-ROM
28111  *              ioctl read mode2 requests (CDROMREADMODE2) for devices that
28112  *              support the READ CD (0xBE) command or the 1st generation
28113  *              READ CD (0xD4) command.
28114  *
28115  *   Arguments: dev     - the device 'dev_t'
28116  *              data    - pointer to user provided cd read structure specifying
28117  *                        the lba buffer address and length.
28118  *              flag    - this argument is a pass through to ddi_copyxxx()
28119  *                        directly from the mode argument of ioctl().
28120  *
28121  * Return Code: the code returned by sd_send_scsi_cmd()
28122  *              EFAULT if ddi_copyxxx() fails
28123  *              ENXIO if fail ddi_get_soft_state
28124  *              EINVAL if data pointer is NULL
28125  */
28126 
28127 static int
28128 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag)
28129 {
28130         struct sd_lun           *un;
28131         struct uscsi_cmd        *com;
28132         struct cdrom_read       mode2_struct;
28133         struct cdrom_read       *mode2 = &mode2_struct;
28134         uchar_t                 cdb[CDB_GROUP5];
28135         int                     nblocks;
28136         int                     rval;
28137 #ifdef _MULTI_DATAMODEL
28138         /*  To support ILP32 applications in an LP64 world */
28139         struct cdrom_read32     cdrom_read32;
28140         struct cdrom_read32     *cdrd32 = &cdrom_read32;
28141 #endif /* _MULTI_DATAMODEL */
28142 
28143         if (data == NULL) {
28144                 return (EINVAL);
28145         }
28146 
28147         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28148             (un->un_state == SD_STATE_OFFLINE)) {
28149                 return (ENXIO);
28150         }
28151 
28152 #ifdef _MULTI_DATAMODEL
28153         switch (ddi_model_convert_from(flag & FMODELS)) {
28154         case DDI_MODEL_ILP32:
28155                 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28156                         return (EFAULT);
28157                 }
28158                 /* Convert the ILP32 uscsi data from the application to LP64 */
28159                 cdrom_read32tocdrom_read(cdrd32, mode2);
28160                 break;
28161         case DDI_MODEL_NONE:
28162                 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28163                         return (EFAULT);
28164                 }
28165                 break;
28166         }
28167 
28168 #else /* ! _MULTI_DATAMODEL */
28169         if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28170                 return (EFAULT);
28171         }
28172 #endif /* _MULTI_DATAMODEL */
28173 
28174         bzero(cdb, sizeof (cdb));
28175         if (un->un_f_cfg_read_cd_xd4 == TRUE) {
28176                 /* Read command supported by 1st generation atapi drives */
28177                 cdb[0] = SCMD_READ_CDD4;
28178         } else {
28179                 /* Universal CD Access Command */
28180                 cdb[0] = SCMD_READ_CD;
28181         }
28182 
28183         /*
28184          * Set expected sector type to: 2336s byte, Mode 2 Yellow Book
28185          */
28186         cdb[1] = CDROM_SECTOR_TYPE_MODE2;
28187 
28188         /* set the start address */
28189         cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF);
28190         cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF);
28191         cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28192         cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF);
28193 
28194         /* set the transfer length */
28195         nblocks = mode2->cdread_buflen / 2336;
28196         cdb[6] = (uchar_t)(nblocks >> 16);
28197         cdb[7] = (uchar_t)(nblocks >> 8);
28198         cdb[8] = (uchar_t)nblocks;
28199 
28200         /* set the filter bits */
28201         cdb[9] = CDROM_READ_CD_USERDATA;
28202 
28203         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28204         com->uscsi_cdb = (caddr_t)cdb;
28205         com->uscsi_cdblen = sizeof (cdb);
28206         com->uscsi_bufaddr = mode2->cdread_bufaddr;
28207         com->uscsi_buflen = mode2->cdread_buflen;
28208         com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28209 
28210         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28211             SD_PATH_STANDARD);
28212         kmem_free(com, sizeof (*com));
28213         return (rval);
28214 }
28215 
28216 
28217 /*
28218  *    Function: sr_read_mode2()
28219  *
28220  * Description: This routine is the driver entry point for handling CD-ROM
28221  *              ioctl read mode2 requests (CDROMREADMODE2) for devices that
28222  *              do not support the READ CD (0xBE) command.
28223  *
28224  *   Arguments: dev     - the device 'dev_t'
28225  *              data    - pointer to user provided cd read structure specifying
28226  *                        the lba buffer address and length.
28227  *              flag    - this argument is a pass through to ddi_copyxxx()
28228  *                        directly from the mode argument of ioctl().
28229  *
28230  * Return Code: the code returned by sd_send_scsi_cmd()
28231  *              EFAULT if ddi_copyxxx() fails
28232  *              ENXIO if fail ddi_get_soft_state
28233  *              EINVAL if data pointer is NULL
28234  *              EIO if fail to reset block size
28235  *              EAGAIN if commands are in progress in the driver
28236  */
28237 
28238 static int
28239 sr_read_mode2(dev_t dev, caddr_t data, int flag)
28240 {
28241         struct sd_lun           *un;
28242         struct cdrom_read       mode2_struct;
28243         struct cdrom_read       *mode2 = &mode2_struct;
28244         int                     rval;
28245         uint32_t                restore_blksize;
28246         struct uscsi_cmd        *com;
28247         uchar_t                 cdb[CDB_GROUP0];
28248         int                     nblocks;
28249 
28250 #ifdef _MULTI_DATAMODEL
28251         /* To support ILP32 applications in an LP64 world */
28252         struct cdrom_read32     cdrom_read32;
28253         struct cdrom_read32     *cdrd32 = &cdrom_read32;
28254 #endif /* _MULTI_DATAMODEL */
28255 
28256         if (data == NULL) {
28257                 return (EINVAL);
28258         }
28259 
28260         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28261             (un->un_state == SD_STATE_OFFLINE)) {
28262                 return (ENXIO);
28263         }
28264 
28265         /*
28266          * Because this routine will update the device and driver block size
28267          * being used we want to make sure there are no commands in progress.
28268          * If commands are in progress the user will have to try again.
28269          *
28270          * We check for 1 instead of 0 because we increment un_ncmds_in_driver
28271          * in sdioctl to protect commands from sdioctl through to the top of
28272          * sd_uscsi_strategy. See sdioctl for details.
28273          */
28274         mutex_enter(SD_MUTEX(un));
28275         if (un->un_ncmds_in_driver != 1) {
28276                 mutex_exit(SD_MUTEX(un));
28277                 return (EAGAIN);
28278         }
28279         mutex_exit(SD_MUTEX(un));
28280 
28281         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28282             "sd_read_mode2: entry: un:0x%p\n", un);
28283 
28284 #ifdef _MULTI_DATAMODEL
28285         switch (ddi_model_convert_from(flag & FMODELS)) {
28286         case DDI_MODEL_ILP32:
28287                 if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28288                         return (EFAULT);
28289                 }
28290                 /* Convert the ILP32 uscsi data from the application to LP64 */
28291                 cdrom_read32tocdrom_read(cdrd32, mode2);
28292                 break;
28293         case DDI_MODEL_NONE:
28294                 if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28295                         return (EFAULT);
28296                 }
28297                 break;
28298         }
28299 #else /* ! _MULTI_DATAMODEL */
28300         if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) {
28301                 return (EFAULT);
28302         }
28303 #endif /* _MULTI_DATAMODEL */
28304 
28305         /* Store the current target block size for restoration later */
28306         restore_blksize = un->un_tgt_blocksize;
28307 
28308         /* Change the device and soft state target block size to 2336 */
28309         if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) {
28310                 rval = EIO;
28311                 goto done;
28312         }
28313 
28314 
28315         bzero(cdb, sizeof (cdb));
28316 
28317         /* set READ operation */
28318         cdb[0] = SCMD_READ;
28319 
28320         /* adjust lba for 2kbyte blocks from 512 byte blocks */
28321         mode2->cdread_lba >>= 2;
28322 
28323         /* set the start address */
28324         cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F);
28325         cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28326         cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF);
28327 
28328         /* set the transfer length */
28329         nblocks = mode2->cdread_buflen / 2336;
28330         cdb[4] = (uchar_t)nblocks & 0xFF;
28331 
28332         /* build command */
28333         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28334         com->uscsi_cdb = (caddr_t)cdb;
28335         com->uscsi_cdblen = sizeof (cdb);
28336         com->uscsi_bufaddr = mode2->cdread_bufaddr;
28337         com->uscsi_buflen = mode2->cdread_buflen;
28338         com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28339 
28340         /*
28341          * Issue SCSI command with user space address for read buffer.
28342          *
28343          * This sends the command through main channel in the driver.
28344          *
28345          * Since this is accessed via an IOCTL call, we go through the
28346          * standard path, so that if the device was powered down, then
28347          * it would be 'awakened' to handle the command.
28348          */
28349         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28350             SD_PATH_STANDARD);
28351 
28352         kmem_free(com, sizeof (*com));
28353 
28354         /* Restore the device and soft state target block size */
28355         if (sr_sector_mode(dev, restore_blksize) != 0) {
28356                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28357                     "can't do switch back to mode 1\n");
28358                 /*
28359                  * If sd_send_scsi_READ succeeded we still need to report
28360                  * an error because we failed to reset the block size
28361                  */
28362                 if (rval == 0) {
28363                         rval = EIO;
28364                 }
28365         }
28366 
28367 done:
28368         SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28369             "sd_read_mode2: exit: un:0x%p\n", un);
28370 
28371         return (rval);
28372 }
28373 
28374 
28375 /*
28376  *    Function: sr_sector_mode()
28377  *
28378  * Description: This utility function is used by sr_read_mode2 to set the target
28379  *              block size based on the user specified size. This is a legacy
28380  *              implementation based upon a vendor specific mode page
28381  *
28382  *   Arguments: dev     - the device 'dev_t'
28383  *              data    - flag indicating if block size is being set to 2336 or
28384  *                        512.
28385  *
28386  * Return Code: the code returned by sd_send_scsi_cmd()
28387  *              EFAULT if ddi_copyxxx() fails
28388  *              ENXIO if fail ddi_get_soft_state
28389  *              EINVAL if data pointer is NULL
28390  */
28391 
28392 static int
28393 sr_sector_mode(dev_t dev, uint32_t blksize)
28394 {
28395         struct sd_lun   *un;
28396         uchar_t         *sense;
28397         uchar_t         *select;
28398         int             rval;
28399         sd_ssc_t        *ssc;
28400 
28401         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28402             (un->un_state == SD_STATE_OFFLINE)) {
28403                 return (ENXIO);
28404         }
28405 
28406         sense = kmem_zalloc(20, KM_SLEEP);
28407 
28408         /* Note: This is a vendor specific mode page (0x81) */
28409         ssc = sd_ssc_init(un);
28410         rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 20, 0x81,
28411             SD_PATH_STANDARD);
28412         sd_ssc_fini(ssc);
28413         if (rval != 0) {
28414                 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28415                     "sr_sector_mode: Mode Sense failed\n");
28416                 kmem_free(sense, 20);
28417                 return (rval);
28418         }
28419         select = kmem_zalloc(20, KM_SLEEP);
28420         select[3] = 0x08;
28421         select[10] = ((blksize >> 8) & 0xff);
28422         select[11] = (blksize & 0xff);
28423         select[12] = 0x01;
28424         select[13] = 0x06;
28425         select[14] = sense[14];
28426         select[15] = sense[15];
28427         if (blksize == SD_MODE2_BLKSIZE) {
28428                 select[14] |= 0x01;
28429         }
28430 
28431         ssc = sd_ssc_init(un);
28432         rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 20,
28433             SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
28434         sd_ssc_fini(ssc);
28435         if (rval != 0) {
28436                 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28437                     "sr_sector_mode: Mode Select failed\n");
28438         } else {
28439                 /*
28440                  * Only update the softstate block size if we successfully
28441                  * changed the device block mode.
28442                  */
28443                 mutex_enter(SD_MUTEX(un));
28444                 sd_update_block_info(un, blksize, 0);
28445                 mutex_exit(SD_MUTEX(un));
28446         }
28447         kmem_free(sense, 20);
28448         kmem_free(select, 20);
28449         return (rval);
28450 }
28451 
28452 
28453 /*
28454  *    Function: sr_read_cdda()
28455  *
28456  * Description: This routine is the driver entry point for handling CD-ROM
28457  *              ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If
28458  *              the target supports CDDA these requests are handled via a vendor
28459  *              specific command (0xD8) If the target does not support CDDA
28460  *              these requests are handled via the READ CD command (0xBE).
28461  *
28462  *   Arguments: dev     - the device 'dev_t'
28463  *              data    - pointer to user provided CD-DA structure specifying
28464  *                        the track starting address, transfer length, and
28465  *                        subcode options.
28466  *              flag    - this argument is a pass through to ddi_copyxxx()
28467  *                        directly from the mode argument of ioctl().
28468  *
28469  * Return Code: the code returned by sd_send_scsi_cmd()
28470  *              EFAULT if ddi_copyxxx() fails
28471  *              ENXIO if fail ddi_get_soft_state
28472  *              EINVAL if invalid arguments are provided
28473  *              ENOTTY
28474  */
28475 
28476 static int
28477 sr_read_cdda(dev_t dev, caddr_t data, int flag)
28478 {
28479         struct sd_lun                   *un;
28480         struct uscsi_cmd                *com;
28481         struct cdrom_cdda               *cdda;
28482         int                             rval;
28483         size_t                          buflen;
28484         char                            cdb[CDB_GROUP5];
28485 
28486 #ifdef _MULTI_DATAMODEL
28487         /* To support ILP32 applications in an LP64 world */
28488         struct cdrom_cdda32     cdrom_cdda32;
28489         struct cdrom_cdda32     *cdda32 = &cdrom_cdda32;
28490 #endif /* _MULTI_DATAMODEL */
28491 
28492         if (data == NULL) {
28493                 return (EINVAL);
28494         }
28495 
28496         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28497                 return (ENXIO);
28498         }
28499 
28500         cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP);
28501 
28502 #ifdef _MULTI_DATAMODEL
28503         switch (ddi_model_convert_from(flag & FMODELS)) {
28504         case DDI_MODEL_ILP32:
28505                 if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) {
28506                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28507                             "sr_read_cdda: ddi_copyin Failed\n");
28508                         kmem_free(cdda, sizeof (struct cdrom_cdda));
28509                         return (EFAULT);
28510                 }
28511                 /* Convert the ILP32 uscsi data from the application to LP64 */
28512                 cdrom_cdda32tocdrom_cdda(cdda32, cdda);
28513                 break;
28514         case DDI_MODEL_NONE:
28515                 if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28516                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28517                             "sr_read_cdda: ddi_copyin Failed\n");
28518                         kmem_free(cdda, sizeof (struct cdrom_cdda));
28519                         return (EFAULT);
28520                 }
28521                 break;
28522         }
28523 #else /* ! _MULTI_DATAMODEL */
28524         if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28525                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28526                     "sr_read_cdda: ddi_copyin Failed\n");
28527                 kmem_free(cdda, sizeof (struct cdrom_cdda));
28528                 return (EFAULT);
28529         }
28530 #endif /* _MULTI_DATAMODEL */
28531 
28532         /*
28533          * Since MMC-2 expects max 3 bytes for length, check if the
28534          * length input is greater than 3 bytes
28535          */
28536         if ((cdda->cdda_length & 0xFF000000) != 0) {
28537                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: "
28538                     "cdrom transfer length too large: %d (limit %d)\n",
28539                     cdda->cdda_length, 0xFFFFFF);
28540                 kmem_free(cdda, sizeof (struct cdrom_cdda));
28541                 return (EINVAL);
28542         }
28543 
28544         switch (cdda->cdda_subcode) {
28545         case CDROM_DA_NO_SUBCODE:
28546                 buflen = CDROM_BLK_2352 * cdda->cdda_length;
28547                 break;
28548         case CDROM_DA_SUBQ:
28549                 buflen = CDROM_BLK_2368 * cdda->cdda_length;
28550                 break;
28551         case CDROM_DA_ALL_SUBCODE:
28552                 buflen = CDROM_BLK_2448 * cdda->cdda_length;
28553                 break;
28554         case CDROM_DA_SUBCODE_ONLY:
28555                 buflen = CDROM_BLK_SUBCODE * cdda->cdda_length;
28556                 break;
28557         default:
28558                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28559                     "sr_read_cdda: Subcode '0x%x' Not Supported\n",
28560                     cdda->cdda_subcode);
28561                 kmem_free(cdda, sizeof (struct cdrom_cdda));
28562                 return (EINVAL);
28563         }
28564 
28565         /* Build and send the command */
28566         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28567         bzero(cdb, CDB_GROUP5);
28568 
28569         if (un->un_f_cfg_cdda == TRUE) {
28570                 cdb[0] = (char)SCMD_READ_CD;
28571                 cdb[1] = 0x04;
28572                 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28573                 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28574                 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28575                 cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28576                 cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28577                 cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28578                 cdb[8] = ((cdda->cdda_length) & 0x000000ff);
28579                 cdb[9] = 0x10;
28580                 switch (cdda->cdda_subcode) {
28581                 case CDROM_DA_NO_SUBCODE :
28582                         cdb[10] = 0x0;
28583                         break;
28584                 case CDROM_DA_SUBQ :
28585                         cdb[10] = 0x2;
28586                         break;
28587                 case CDROM_DA_ALL_SUBCODE :
28588                         cdb[10] = 0x1;
28589                         break;
28590                 case CDROM_DA_SUBCODE_ONLY :
28591                         /* FALLTHROUGH */
28592                 default :
28593                         kmem_free(cdda, sizeof (struct cdrom_cdda));
28594                         kmem_free(com, sizeof (*com));
28595                         return (ENOTTY);
28596                 }
28597         } else {
28598                 cdb[0] = (char)SCMD_READ_CDDA;
28599                 cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28600                 cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28601                 cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28602                 cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28603                 cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24);
28604                 cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28605                 cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28606                 cdb[9] = ((cdda->cdda_length) & 0x000000ff);
28607                 cdb[10] = cdda->cdda_subcode;
28608         }
28609 
28610         com->uscsi_cdb = cdb;
28611         com->uscsi_cdblen = CDB_GROUP5;
28612         com->uscsi_bufaddr = (caddr_t)cdda->cdda_data;
28613         com->uscsi_buflen = buflen;
28614         com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28615 
28616         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28617             SD_PATH_STANDARD);
28618 
28619         kmem_free(cdda, sizeof (struct cdrom_cdda));
28620         kmem_free(com, sizeof (*com));
28621         return (rval);
28622 }
28623 
28624 
28625 /*
28626  *    Function: sr_read_cdxa()
28627  *
28628  * Description: This routine is the driver entry point for handling CD-ROM
28629  *              ioctl requests to return CD-XA (Extended Architecture) data.
28630  *              (CDROMCDXA).
28631  *
28632  *   Arguments: dev     - the device 'dev_t'
28633  *              data    - pointer to user provided CD-XA structure specifying
28634  *                        the data starting address, transfer length, and format
28635  *              flag    - this argument is a pass through to ddi_copyxxx()
28636  *                        directly from the mode argument of ioctl().
28637  *
28638  * Return Code: the code returned by sd_send_scsi_cmd()
28639  *              EFAULT if ddi_copyxxx() fails
28640  *              ENXIO if fail ddi_get_soft_state
28641  *              EINVAL if data pointer is NULL
28642  */
28643 
28644 static int
28645 sr_read_cdxa(dev_t dev, caddr_t data, int flag)
28646 {
28647         struct sd_lun           *un;
28648         struct uscsi_cmd        *com;
28649         struct cdrom_cdxa       *cdxa;
28650         int                     rval;
28651         size_t                  buflen;
28652         char                    cdb[CDB_GROUP5];
28653         uchar_t                 read_flags;
28654 
28655 #ifdef _MULTI_DATAMODEL
28656         /* To support ILP32 applications in an LP64 world */
28657         struct cdrom_cdxa32             cdrom_cdxa32;
28658         struct cdrom_cdxa32             *cdxa32 = &cdrom_cdxa32;
28659 #endif /* _MULTI_DATAMODEL */
28660 
28661         if (data == NULL) {
28662                 return (EINVAL);
28663         }
28664 
28665         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28666                 return (ENXIO);
28667         }
28668 
28669         cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP);
28670 
28671 #ifdef _MULTI_DATAMODEL
28672         switch (ddi_model_convert_from(flag & FMODELS)) {
28673         case DDI_MODEL_ILP32:
28674                 if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) {
28675                         kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28676                         return (EFAULT);
28677                 }
28678                 /*
28679                  * Convert the ILP32 uscsi data from the
28680                  * application to LP64 for internal use.
28681                  */
28682                 cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa);
28683                 break;
28684         case DDI_MODEL_NONE:
28685                 if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28686                         kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28687                         return (EFAULT);
28688                 }
28689                 break;
28690         }
28691 #else /* ! _MULTI_DATAMODEL */
28692         if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28693                 kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28694                 return (EFAULT);
28695         }
28696 #endif /* _MULTI_DATAMODEL */
28697 
28698         /*
28699          * Since MMC-2 expects max 3 bytes for length, check if the
28700          * length input is greater than 3 bytes
28701          */
28702         if ((cdxa->cdxa_length & 0xFF000000) != 0) {
28703                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: "
28704                     "cdrom transfer length too large: %d (limit %d)\n",
28705                     cdxa->cdxa_length, 0xFFFFFF);
28706                 kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28707                 return (EINVAL);
28708         }
28709 
28710         switch (cdxa->cdxa_format) {
28711         case CDROM_XA_DATA:
28712                 buflen = CDROM_BLK_2048 * cdxa->cdxa_length;
28713                 read_flags = 0x10;
28714                 break;
28715         case CDROM_XA_SECTOR_DATA:
28716                 buflen = CDROM_BLK_2352 * cdxa->cdxa_length;
28717                 read_flags = 0xf8;
28718                 break;
28719         case CDROM_XA_DATA_W_ERROR:
28720                 buflen = CDROM_BLK_2646 * cdxa->cdxa_length;
28721                 read_flags = 0xfc;
28722                 break;
28723         default:
28724                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28725                     "sr_read_cdxa: Format '0x%x' Not Supported\n",
28726                     cdxa->cdxa_format);
28727                 kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28728                 return (EINVAL);
28729         }
28730 
28731         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28732         bzero(cdb, CDB_GROUP5);
28733         if (un->un_f_mmc_cap == TRUE) {
28734                 cdb[0] = (char)SCMD_READ_CD;
28735                 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28736                 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28737                 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28738                 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28739                 cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28740                 cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28741                 cdb[8] = ((cdxa->cdxa_length) & 0x000000ff);
28742                 cdb[9] = (char)read_flags;
28743         } else {
28744                 /*
28745                  * Note: A vendor specific command (0xDB) is being used her to
28746                  * request a read of all subcodes.
28747                  */
28748                 cdb[0] = (char)SCMD_READ_CDXA;
28749                 cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28750                 cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28751                 cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28752                 cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28753                 cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24);
28754                 cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28755                 cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28756                 cdb[9] = ((cdxa->cdxa_length) & 0x000000ff);
28757                 cdb[10] = cdxa->cdxa_format;
28758         }
28759         com->uscsi_cdb          = cdb;
28760         com->uscsi_cdblen  = CDB_GROUP5;
28761         com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data;
28762         com->uscsi_buflen  = buflen;
28763         com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28764         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28765             SD_PATH_STANDARD);
28766         kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28767         kmem_free(com, sizeof (*com));
28768         return (rval);
28769 }
28770 
28771 
28772 /*
28773  *    Function: sr_eject()
28774  *
28775  * Description: This routine is the driver entry point for handling CD-ROM
28776  *              eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT)
28777  *
28778  *   Arguments: dev     - the device 'dev_t'
28779  *
28780  * Return Code: the code returned by sd_send_scsi_cmd()
28781  */
28782 
28783 static int
28784 sr_eject(dev_t dev)
28785 {
28786         struct sd_lun   *un;
28787         int             rval;
28788         sd_ssc_t        *ssc;
28789 
28790         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28791             (un->un_state == SD_STATE_OFFLINE)) {
28792                 return (ENXIO);
28793         }
28794 
28795         /*
28796          * To prevent race conditions with the eject
28797          * command, keep track of an eject command as
28798          * it progresses. If we are already handling
28799          * an eject command in the driver for the given
28800          * unit and another request to eject is received
28801          * immediately return EAGAIN so we don't lose
28802          * the command if the current eject command fails.
28803          */
28804         mutex_enter(SD_MUTEX(un));
28805         if (un->un_f_ejecting == TRUE) {
28806                 mutex_exit(SD_MUTEX(un));
28807                 return (EAGAIN);
28808         }
28809         un->un_f_ejecting = TRUE;
28810         mutex_exit(SD_MUTEX(un));
28811 
28812         ssc = sd_ssc_init(un);
28813         rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
28814             SD_PATH_STANDARD);
28815         sd_ssc_fini(ssc);
28816 
28817         if (rval != 0) {
28818                 mutex_enter(SD_MUTEX(un));
28819                 un->un_f_ejecting = FALSE;
28820                 mutex_exit(SD_MUTEX(un));
28821                 return (rval);
28822         }
28823 
28824         ssc = sd_ssc_init(un);
28825         rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
28826             SD_TARGET_EJECT, SD_PATH_STANDARD);
28827         sd_ssc_fini(ssc);
28828 
28829         if (rval == 0) {
28830                 mutex_enter(SD_MUTEX(un));
28831                 sr_ejected(un);
28832                 un->un_mediastate = DKIO_EJECTED;
28833                 un->un_f_ejecting = FALSE;
28834                 cv_broadcast(&un->un_state_cv);
28835                 mutex_exit(SD_MUTEX(un));
28836         } else {
28837                 mutex_enter(SD_MUTEX(un));
28838                 un->un_f_ejecting = FALSE;
28839                 mutex_exit(SD_MUTEX(un));
28840         }
28841         return (rval);
28842 }
28843 
28844 
28845 /*
28846  *    Function: sr_ejected()
28847  *
28848  * Description: This routine updates the soft state structure to invalidate the
28849  *              geometry information after the media has been ejected or a
28850  *              media eject has been detected.
28851  *
28852  *   Arguments: un - driver soft state (unit) structure
28853  */
28854 
28855 static void
28856 sr_ejected(struct sd_lun *un)
28857 {
28858         struct sd_errstats *stp;
28859 
28860         ASSERT(un != NULL);
28861         ASSERT(mutex_owned(SD_MUTEX(un)));
28862 
28863         un->un_f_blockcount_is_valid = FALSE;
28864         un->un_f_tgt_blocksize_is_valid      = FALSE;
28865         mutex_exit(SD_MUTEX(un));
28866         cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
28867         mutex_enter(SD_MUTEX(un));
28868 
28869         if (un->un_errstats != NULL) {
28870                 stp = (struct sd_errstats *)un->un_errstats->ks_data;
28871                 stp->sd_capacity.value.ui64 = 0;
28872         }
28873 }
28874 
28875 
28876 /*
28877  *    Function: sr_check_wp()
28878  *
28879  * Description: This routine checks the write protection of a removable
28880  *      media disk and hotpluggable devices via the write protect bit of
28881  *      the Mode Page Header device specific field. Some devices choke
28882  *      on unsupported mode page. In order to workaround this issue,
28883  *      this routine has been implemented to use 0x3f mode page(request
28884  *      for all pages) for all device types.
28885  *
28886  *   Arguments: dev             - the device 'dev_t'
28887  *
28888  * Return Code: int indicating if the device is write protected (1) or not (0)
28889  *
28890  *     Context: Kernel thread.
28891  *
28892  */
28893 
28894 static int
28895 sr_check_wp(dev_t dev)
28896 {
28897         struct sd_lun   *un;
28898         uchar_t         device_specific;
28899         uchar_t         *sense;
28900         int             hdrlen;
28901         int             rval = FALSE;
28902         int             status;
28903         sd_ssc_t        *ssc;
28904 
28905         /*
28906          * Note: The return codes for this routine should be reworked to
28907          * properly handle the case of a NULL softstate.
28908          */
28909         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28910                 return (FALSE);
28911         }
28912 
28913         if (un->un_f_cfg_is_atapi == TRUE) {
28914                 /*
28915                  * The mode page contents are not required; set the allocation
28916                  * length for the mode page header only
28917                  */
28918                 hdrlen = MODE_HEADER_LENGTH_GRP2;
28919                 sense = kmem_zalloc(hdrlen, KM_SLEEP);
28920                 ssc = sd_ssc_init(un);
28921                 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, hdrlen,
28922                     MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
28923                 sd_ssc_fini(ssc);
28924                 if (status != 0)
28925                         goto err_exit;
28926                 device_specific =
28927                     ((struct mode_header_grp2 *)sense)->device_specific;
28928         } else {
28929                 hdrlen = MODE_HEADER_LENGTH;
28930                 sense = kmem_zalloc(hdrlen, KM_SLEEP);
28931                 ssc = sd_ssc_init(un);
28932                 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, hdrlen,
28933                     MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
28934                 sd_ssc_fini(ssc);
28935                 if (status != 0)
28936                         goto err_exit;
28937                 device_specific =
28938                     ((struct mode_header *)sense)->device_specific;
28939         }
28940 
28941 
28942         /*
28943          * Write protect mode sense failed; not all disks
28944          * understand this query. Return FALSE assuming that
28945          * these devices are not writable.
28946          */
28947         if (device_specific & WRITE_PROTECT) {
28948                 rval = TRUE;
28949         }
28950 
28951 err_exit:
28952         kmem_free(sense, hdrlen);
28953         return (rval);
28954 }
28955 
28956 /*
28957  *    Function: sr_volume_ctrl()
28958  *
28959  * Description: This routine is the driver entry point for handling CD-ROM
28960  *              audio output volume ioctl requests. (CDROMVOLCTRL)
28961  *
28962  *   Arguments: dev     - the device 'dev_t'
28963  *              data    - pointer to user audio volume control structure
28964  *              flag    - this argument is a pass through to ddi_copyxxx()
28965  *                        directly from the mode argument of ioctl().
28966  *
28967  * Return Code: the code returned by sd_send_scsi_cmd()
28968  *              EFAULT if ddi_copyxxx() fails
28969  *              ENXIO if fail ddi_get_soft_state
28970  *              EINVAL if data pointer is NULL
28971  *
28972  */
28973 
28974 static int
28975 sr_volume_ctrl(dev_t dev, caddr_t data, int flag)
28976 {
28977         struct sd_lun           *un;
28978         struct cdrom_volctrl    volume;
28979         struct cdrom_volctrl    *vol = &volume;
28980         uchar_t                 *sense_page;
28981         uchar_t                 *select_page;
28982         uchar_t                 *sense;
28983         uchar_t                 *select;
28984         int                     sense_buflen;
28985         int                     select_buflen;
28986         int                     rval;
28987         sd_ssc_t                *ssc;
28988 
28989         if (data == NULL) {
28990                 return (EINVAL);
28991         }
28992 
28993         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28994             (un->un_state == SD_STATE_OFFLINE)) {
28995                 return (ENXIO);
28996         }
28997 
28998         if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) {
28999                 return (EFAULT);
29000         }
29001 
29002         if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29003                 struct mode_header_grp2         *sense_mhp;
29004                 struct mode_header_grp2         *select_mhp;
29005                 int                             bd_len;
29006 
29007                 sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN;
29008                 select_buflen = MODE_HEADER_LENGTH_GRP2 +
29009                     MODEPAGE_AUDIO_CTRL_LEN;
29010                 sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
29011                 select = kmem_zalloc(select_buflen, KM_SLEEP);
29012                 ssc = sd_ssc_init(un);
29013                 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
29014                     sense_buflen, MODEPAGE_AUDIO_CTRL,
29015                     SD_PATH_STANDARD);
29016                 sd_ssc_fini(ssc);
29017 
29018                 if (rval != 0) {
29019                         SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
29020                             "sr_volume_ctrl: Mode Sense Failed\n");
29021                         kmem_free(sense, sense_buflen);
29022                         kmem_free(select, select_buflen);
29023                         return (rval);
29024                 }
29025                 sense_mhp = (struct mode_header_grp2 *)sense;
29026                 select_mhp = (struct mode_header_grp2 *)select;
29027                 bd_len = (sense_mhp->bdesc_length_hi << 8) |
29028                     sense_mhp->bdesc_length_lo;
29029                 if (bd_len > MODE_BLK_DESC_LENGTH) {
29030                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29031                             "sr_volume_ctrl: Mode Sense returned invalid "
29032                             "block descriptor length\n");
29033                         kmem_free(sense, sense_buflen);
29034                         kmem_free(select, select_buflen);
29035                         return (EIO);
29036                 }
29037                 sense_page = (uchar_t *)
29038                     (sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
29039                 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2);
29040                 select_mhp->length_msb = 0;
29041                 select_mhp->length_lsb = 0;
29042                 select_mhp->bdesc_length_hi = 0;
29043                 select_mhp->bdesc_length_lo = 0;
29044         } else {
29045                 struct mode_header              *sense_mhp, *select_mhp;
29046 
29047                 sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29048                 select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29049                 sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
29050                 select = kmem_zalloc(select_buflen, KM_SLEEP);
29051                 ssc = sd_ssc_init(un);
29052                 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
29053                     sense_buflen, MODEPAGE_AUDIO_CTRL,
29054                     SD_PATH_STANDARD);
29055                 sd_ssc_fini(ssc);
29056 
29057                 if (rval != 0) {
29058                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29059                             "sr_volume_ctrl: Mode Sense Failed\n");
29060                         kmem_free(sense, sense_buflen);
29061                         kmem_free(select, select_buflen);
29062                         return (rval);
29063                 }
29064                 sense_mhp  = (struct mode_header *)sense;
29065                 select_mhp = (struct mode_header *)select;
29066                 if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) {
29067                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29068                             "sr_volume_ctrl: Mode Sense returned invalid "
29069                             "block descriptor length\n");
29070                         kmem_free(sense, sense_buflen);
29071                         kmem_free(select, select_buflen);
29072                         return (EIO);
29073                 }
29074                 sense_page = (uchar_t *)
29075                     (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
29076                 select_page = (uchar_t *)(select + MODE_HEADER_LENGTH);
29077                 select_mhp->length = 0;
29078                 select_mhp->bdesc_length = 0;
29079         }
29080         /*
29081          * Note: An audio control data structure could be created and overlayed
29082          * on the following in place of the array indexing method implemented.
29083          */
29084 
29085         /* Build the select data for the user volume data */
29086         select_page[0] = MODEPAGE_AUDIO_CTRL;
29087         select_page[1] = 0xE;
29088         /* Set the immediate bit */
29089         select_page[2] = 0x04;
29090         /* Zero out reserved fields */
29091         select_page[3] = 0x00;
29092         select_page[4] = 0x00;
29093         /* Return sense data for fields not to be modified */
29094         select_page[5] = sense_page[5];
29095         select_page[6] = sense_page[6];
29096         select_page[7] = sense_page[7];
29097         /* Set the user specified volume levels for channel 0 and 1 */
29098         select_page[8] = 0x01;
29099         select_page[9] = vol->channel0;
29100         select_page[10] = 0x02;
29101         select_page[11] = vol->channel1;
29102         /* Channel 2 and 3 are currently unsupported so return the sense data */
29103         select_page[12] = sense_page[12];
29104         select_page[13] = sense_page[13];
29105         select_page[14] = sense_page[14];
29106         select_page[15] = sense_page[15];
29107 
29108         ssc = sd_ssc_init(un);
29109         if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29110                 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, select,
29111                     select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29112         } else {
29113                 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
29114                     select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29115         }
29116         sd_ssc_fini(ssc);
29117 
29118         kmem_free(sense, sense_buflen);
29119         kmem_free(select, select_buflen);
29120         return (rval);
29121 }
29122 
29123 
29124 /*
29125  *    Function: sr_read_sony_session_offset()
29126  *
29127  * Description: This routine is the driver entry point for handling CD-ROM
29128  *              ioctl requests for session offset information. (CDROMREADOFFSET)
29129  *              The address of the first track in the last session of a
29130  *              multi-session CD-ROM is returned
29131  *
29132  *              Note: This routine uses a vendor specific key value in the
29133  *              command control field without implementing any vendor check here
29134  *              or in the ioctl routine.
29135  *
29136  *   Arguments: dev     - the device 'dev_t'
29137  *              data    - pointer to an int to hold the requested address
29138  *              flag    - this argument is a pass through to ddi_copyxxx()
29139  *                        directly from the mode argument of ioctl().
29140  *
29141  * Return Code: the code returned by sd_send_scsi_cmd()
29142  *              EFAULT if ddi_copyxxx() fails
29143  *              ENXIO if fail ddi_get_soft_state
29144  *              EINVAL if data pointer is NULL
29145  */
29146 
29147 static int
29148 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag)
29149 {
29150         struct sd_lun           *un;
29151         struct uscsi_cmd        *com;
29152         caddr_t                 buffer;
29153         char                    cdb[CDB_GROUP1];
29154         int                     session_offset = 0;
29155         int                     rval;
29156 
29157         if (data == NULL) {
29158                 return (EINVAL);
29159         }
29160 
29161         if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29162             (un->un_state == SD_STATE_OFFLINE)) {
29163                 return (ENXIO);
29164         }
29165 
29166         buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP);
29167         bzero(cdb, CDB_GROUP1);
29168         cdb[0] = SCMD_READ_TOC;
29169         /*
29170          * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
29171          * (4 byte TOC response header + 8 byte response data)
29172          */
29173         cdb[8] = SONY_SESSION_OFFSET_LEN;
29174         /* Byte 9 is the control byte. A vendor specific value is used */
29175         cdb[9] = SONY_SESSION_OFFSET_KEY;
29176         com = kmem_zalloc(sizeof (*com), KM_SLEEP);
29177         com->uscsi_cdb = cdb;
29178         com->uscsi_cdblen = CDB_GROUP1;
29179         com->uscsi_bufaddr = buffer;
29180         com->uscsi_buflen = SONY_SESSION_OFFSET_LEN;
29181         com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
29182 
29183         rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
29184             SD_PATH_STANDARD);
29185         if (rval != 0) {
29186                 kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29187                 kmem_free(com, sizeof (*com));
29188                 return (rval);
29189         }
29190         if (buffer[1] == SONY_SESSION_OFFSET_VALID) {
29191                 session_offset =
29192                     ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
29193                     ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
29194                 /*
29195                  * Offset returned offset in current lbasize block's. Convert to
29196                  * 2k block's to return to the user
29197                  */
29198                 if (un->un_tgt_blocksize == CDROM_BLK_512) {
29199                         session_offset >>= 2;
29200                 } else if (un->un_tgt_blocksize == CDROM_BLK_1024) {
29201                         session_offset >>= 1;
29202                 }
29203         }
29204 
29205         if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) {
29206                 rval = EFAULT;
29207         }
29208 
29209         kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29210         kmem_free(com, sizeof (*com));
29211         return (rval);
29212 }
29213 
29214 
29215 /*
29216  *    Function: sd_wm_cache_constructor()
29217  *
29218  * Description: Cache Constructor for the wmap cache for the read/modify/write
29219  *              devices.
29220  *
29221  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29222  *              un      - sd_lun structure for the device.
29223  *              flag    - the km flags passed to constructor
29224  *
29225  * Return Code: 0 on success.
29226  *              -1 on failure.
29227  */
29228 
29229 /*ARGSUSED*/
29230 static int
29231 sd_wm_cache_constructor(void *wm, void *un, int flags)
29232 {
29233         bzero(wm, sizeof (struct sd_w_map));
29234         cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL);
29235         return (0);
29236 }
29237 
29238 
29239 /*
29240  *    Function: sd_wm_cache_destructor()
29241  *
29242  * Description: Cache destructor for the wmap cache for the read/modify/write
29243  *              devices.
29244  *
29245  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29246  *              un      - sd_lun structure for the device.
29247  */
29248 /*ARGSUSED*/
29249 static void
29250 sd_wm_cache_destructor(void *wm, void *un)
29251 {
29252         cv_destroy(&((struct sd_w_map *)wm)->wm_avail);
29253 }
29254 
29255 
29256 /*
29257  *    Function: sd_range_lock()
29258  *
29259  * Description: Lock the range of blocks specified as parameter to ensure
29260  *              that read, modify write is atomic and no other i/o writes
29261  *              to the same location. The range is specified in terms
29262  *              of start and end blocks. Block numbers are the actual
29263  *              media block numbers and not system.
29264  *
29265  *   Arguments: un      - sd_lun structure for the device.
29266  *              startb - The starting block number
29267  *              endb - The end block number
29268  *              typ - type of i/o - simple/read_modify_write
29269  *
29270  * Return Code: wm  - pointer to the wmap structure.
29271  *
29272  *     Context: This routine can sleep.
29273  */
29274 
29275 static struct sd_w_map *
29276 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ)
29277 {
29278         struct sd_w_map *wmp = NULL;
29279         struct sd_w_map *sl_wmp = NULL;
29280         struct sd_w_map *tmp_wmp;
29281         wm_state state = SD_WM_CHK_LIST;
29282 
29283 
29284         ASSERT(un != NULL);
29285         ASSERT(!mutex_owned(SD_MUTEX(un)));
29286 
29287         mutex_enter(SD_MUTEX(un));
29288 
29289         while (state != SD_WM_DONE) {
29290 
29291                 switch (state) {
29292                 case SD_WM_CHK_LIST:
29293                         /*
29294                          * This is the starting state. Check the wmap list
29295                          * to see if the range is currently available.
29296                          */
29297                         if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) {
29298                                 /*
29299                                  * If this is a simple write and no rmw
29300                                  * i/o is pending then try to lock the
29301                                  * range as the range should be available.
29302                                  */
29303                                 state = SD_WM_LOCK_RANGE;
29304                         } else {
29305                                 tmp_wmp = sd_get_range(un, startb, endb);
29306                                 if (tmp_wmp != NULL) {
29307                                         if ((wmp != NULL) && ONLIST(un, wmp)) {
29308                                                 /*
29309                                                  * Should not keep onlist wmps
29310                                                  * while waiting this macro
29311                                                  * will also do wmp = NULL;
29312                                                  */
29313                                                 FREE_ONLIST_WMAP(un, wmp);
29314                                         }
29315                                         /*
29316                                          * sl_wmp is the wmap on which wait
29317                                          * is done, since the tmp_wmp points
29318                                          * to the inuse wmap, set sl_wmp to
29319                                          * tmp_wmp and change the state to sleep
29320                                          */
29321                                         sl_wmp = tmp_wmp;
29322                                         state = SD_WM_WAIT_MAP;
29323                                 } else {
29324                                         state = SD_WM_LOCK_RANGE;
29325                                 }
29326 
29327                         }
29328                         break;
29329 
29330                 case SD_WM_LOCK_RANGE:
29331                         ASSERT(un->un_wm_cache);
29332                         /*
29333                          * The range need to be locked, try to get a wmap.
29334                          * First attempt it with NO_SLEEP, want to avoid a sleep
29335                          * if possible as we will have to release the sd mutex
29336                          * if we have to sleep.
29337                          */
29338                         if (wmp == NULL)
29339                                 wmp = kmem_cache_alloc(un->un_wm_cache,
29340                                     KM_NOSLEEP);
29341                         if (wmp == NULL) {
29342                                 mutex_exit(SD_MUTEX(un));
29343                                 _NOTE(DATA_READABLE_WITHOUT_LOCK
29344                                     (sd_lun::un_wm_cache))
29345                                 wmp = kmem_cache_alloc(un->un_wm_cache,
29346                                     KM_SLEEP);
29347                                 mutex_enter(SD_MUTEX(un));
29348                                 /*
29349                                  * we released the mutex so recheck and go to
29350                                  * check list state.
29351                                  */
29352                                 state = SD_WM_CHK_LIST;
29353                         } else {
29354                                 /*
29355                                  * We exit out of state machine since we
29356                                  * have the wmap. Do the housekeeping first.
29357                                  * place the wmap on the wmap list if it is not
29358                                  * on it already and then set the state to done.
29359                                  */
29360                                 wmp->wm_start = startb;
29361                                 wmp->wm_end = endb;
29362                                 wmp->wm_flags = typ | SD_WM_BUSY;
29363                                 if (typ & SD_WTYPE_RMW) {
29364                                         un->un_rmw_count++;
29365                                 }
29366                                 /*
29367                                  * If not already on the list then link
29368                                  */
29369                                 if (!ONLIST(un, wmp)) {
29370                                         wmp->wm_next = un->un_wm;
29371                                         wmp->wm_prev = NULL;
29372                                         if (wmp->wm_next)
29373                                                 wmp->wm_next->wm_prev = wmp;
29374                                         un->un_wm = wmp;
29375                                 }
29376                                 state = SD_WM_DONE;
29377                         }
29378                         break;
29379 
29380                 case SD_WM_WAIT_MAP:
29381                         ASSERT(sl_wmp->wm_flags & SD_WM_BUSY);
29382                         /*
29383                          * Wait is done on sl_wmp, which is set in the
29384                          * check_list state.
29385                          */
29386                         sl_wmp->wm_wanted_count++;
29387                         cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un));
29388                         sl_wmp->wm_wanted_count--;
29389                         /*
29390                          * We can reuse the memory from the completed sl_wmp
29391                          * lock range for our new lock, but only if noone is
29392                          * waiting for it.
29393                          */
29394                         ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY));
29395                         if (sl_wmp->wm_wanted_count == 0) {
29396                                 if (wmp != NULL) {
29397                                         CHK_N_FREEWMP(un, wmp);
29398                                 }
29399                                 wmp = sl_wmp;
29400                         }
29401                         sl_wmp = NULL;
29402                         /*
29403                          * After waking up, need to recheck for availability of
29404                          * range.
29405                          */
29406                         state = SD_WM_CHK_LIST;
29407                         break;
29408 
29409                 default:
29410                         panic("sd_range_lock: "
29411                             "Unknown state %d in sd_range_lock", state);
29412                         /*NOTREACHED*/
29413                 } /* switch(state) */
29414 
29415         } /* while(state != SD_WM_DONE) */
29416 
29417         mutex_exit(SD_MUTEX(un));
29418 
29419         ASSERT(wmp != NULL);
29420 
29421         return (wmp);
29422 }
29423 
29424 
29425 /*
29426  *    Function: sd_get_range()
29427  *
29428  * Description: Find if there any overlapping I/O to this one
29429  *              Returns the write-map of 1st such I/O, NULL otherwise.
29430  *
29431  *   Arguments: un      - sd_lun structure for the device.
29432  *              startb - The starting block number
29433  *              endb - The end block number
29434  *
29435  * Return Code: wm  - pointer to the wmap structure.
29436  */
29437 
29438 static struct sd_w_map *
29439 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb)
29440 {
29441         struct sd_w_map *wmp;
29442 
29443         ASSERT(un != NULL);
29444 
29445         for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) {
29446                 if (!(wmp->wm_flags & SD_WM_BUSY)) {
29447                         continue;
29448                 }
29449                 if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) {
29450                         break;
29451                 }
29452                 if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) {
29453                         break;
29454                 }
29455         }
29456 
29457         return (wmp);
29458 }
29459 
29460 
29461 /*
29462  *    Function: sd_free_inlist_wmap()
29463  *
29464  * Description: Unlink and free a write map struct.
29465  *
29466  *   Arguments: un      - sd_lun structure for the device.
29467  *              wmp     - sd_w_map which needs to be unlinked.
29468  */
29469 
29470 static void
29471 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp)
29472 {
29473         ASSERT(un != NULL);
29474 
29475         if (un->un_wm == wmp) {
29476                 un->un_wm = wmp->wm_next;
29477         } else {
29478                 wmp->wm_prev->wm_next = wmp->wm_next;
29479         }
29480 
29481         if (wmp->wm_next) {
29482                 wmp->wm_next->wm_prev = wmp->wm_prev;
29483         }
29484 
29485         wmp->wm_next = wmp->wm_prev = NULL;
29486 
29487         kmem_cache_free(un->un_wm_cache, wmp);
29488 }
29489 
29490 
29491 /*
29492  *    Function: sd_range_unlock()
29493  *
29494  * Description: Unlock the range locked by wm.
29495  *              Free write map if nobody else is waiting on it.
29496  *
29497  *   Arguments: un      - sd_lun structure for the device.
29498  *              wmp     - sd_w_map which needs to be unlinked.
29499  */
29500 
29501 static void
29502 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm)
29503 {
29504         ASSERT(un != NULL);
29505         ASSERT(wm != NULL);
29506         ASSERT(!mutex_owned(SD_MUTEX(un)));
29507 
29508         mutex_enter(SD_MUTEX(un));
29509 
29510         if (wm->wm_flags & SD_WTYPE_RMW) {
29511                 un->un_rmw_count--;
29512         }
29513 
29514         if (wm->wm_wanted_count) {
29515                 wm->wm_flags = 0;
29516                 /*
29517                  * Broadcast that the wmap is available now.
29518                  */
29519                 cv_broadcast(&wm->wm_avail);
29520         } else {
29521                 /*
29522                  * If no one is waiting on the map, it should be free'ed.
29523                  */
29524                 sd_free_inlist_wmap(un, wm);
29525         }
29526 
29527         mutex_exit(SD_MUTEX(un));
29528 }
29529 
29530 
29531 /*
29532  *    Function: sd_read_modify_write_task
29533  *
29534  * Description: Called from a taskq thread to initiate the write phase of
29535  *              a read-modify-write request.  This is used for targets where
29536  *              un->un_sys_blocksize != un->un_tgt_blocksize.
29537  *
29538  *   Arguments: arg - a pointer to the buf(9S) struct for the write command.
29539  *
29540  *     Context: Called under taskq thread context.
29541  */
29542 
29543 static void
29544 sd_read_modify_write_task(void *arg)
29545 {
29546         struct sd_mapblocksize_info     *bsp;
29547         struct buf      *bp;
29548         struct sd_xbuf  *xp;
29549         struct sd_lun   *un;
29550 
29551         bp = arg;       /* The bp is given in arg */
29552         ASSERT(bp != NULL);
29553 
29554         /* Get the pointer to the layer-private data struct */
29555         xp = SD_GET_XBUF(bp);
29556         ASSERT(xp != NULL);
29557         bsp = xp->xb_private;
29558         ASSERT(bsp != NULL);
29559 
29560         un = SD_GET_UN(bp);
29561         ASSERT(un != NULL);
29562         ASSERT(!mutex_owned(SD_MUTEX(un)));
29563 
29564         SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29565             "sd_read_modify_write_task: entry: buf:0x%p\n", bp);
29566 
29567         /*
29568          * This is the write phase of a read-modify-write request, called
29569          * under the context of a taskq thread in response to the completion
29570          * of the read portion of the rmw request completing under interrupt
29571          * context. The write request must be sent from here down the iostart
29572          * chain as if it were being sent from sd_mapblocksize_iostart(), so
29573          * we use the layer index saved in the layer-private data area.
29574          */
29575         SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp);
29576 
29577         SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29578             "sd_read_modify_write_task: exit: buf:0x%p\n", bp);
29579 }
29580 
29581 
29582 /*
29583  *    Function: sddump_do_read_of_rmw()
29584  *
29585  * Description: This routine will be called from sddump, If sddump is called
29586  *              with an I/O which not aligned on device blocksize boundary
29587  *              then the write has to be converted to read-modify-write.
29588  *              Do the read part here in order to keep sddump simple.
29589  *              Note - That the sd_mutex is held across the call to this
29590  *              routine.
29591  *
29592  *   Arguments: un      - sd_lun
29593  *              blkno   - block number in terms of media block size.
29594  *              nblk    - number of blocks.
29595  *              bpp     - pointer to pointer to the buf structure. On return
29596  *                      from this function, *bpp points to the valid buffer
29597  *                      to which the write has to be done.
29598  *
29599  * Return Code: 0 for success or errno-type return code
29600  */
29601 
29602 static int
29603 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
29604     struct buf **bpp)
29605 {
29606         int err;
29607         int i;
29608         int rval;
29609         struct buf *bp;
29610         struct scsi_pkt *pkt = NULL;
29611         uint32_t target_blocksize;
29612 
29613         ASSERT(un != NULL);
29614         ASSERT(mutex_owned(SD_MUTEX(un)));
29615 
29616         target_blocksize = un->un_tgt_blocksize;
29617 
29618         mutex_exit(SD_MUTEX(un));
29619 
29620         bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL,
29621             (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL);
29622         if (bp == NULL) {
29623                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29624                     "no resources for dumping; giving up");
29625                 err = ENOMEM;
29626                 goto done;
29627         }
29628 
29629         rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL,
29630             blkno, nblk);
29631         if (rval != 0) {
29632                 scsi_free_consistent_buf(bp);
29633                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29634                     "no resources for dumping; giving up");
29635                 err = ENOMEM;
29636                 goto done;
29637         }
29638 
29639         pkt->pkt_flags |= FLAG_NOINTR;
29640 
29641         err = EIO;
29642         for (i = 0; i < SD_NDUMP_RETRIES; i++) {
29643 
29644                 /*
29645                  * Scsi_poll returns 0 (success) if the command completes and
29646                  * the status block is STATUS_GOOD.  We should only check
29647                  * errors if this condition is not true.  Even then we should
29648                  * send our own request sense packet only if we have a check
29649                  * condition and auto request sense has not been performed by
29650                  * the hba.
29651                  */
29652                 SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n");
29653 
29654                 if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) {
29655                         err = 0;
29656                         break;
29657                 }
29658 
29659                 /*
29660                  * Check CMD_DEV_GONE 1st, give up if device is gone,
29661                  * no need to read RQS data.
29662                  */
29663                 if (pkt->pkt_reason == CMD_DEV_GONE) {
29664                         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29665                             "Error while dumping state with rmw..."
29666                             "Device is gone\n");
29667                         break;
29668                 }
29669 
29670                 if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) {
29671                         SD_INFO(SD_LOG_DUMP, un,
29672                             "sddump: read failed with CHECK, try # %d\n", i);
29673                         if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) {
29674                                 (void) sd_send_polled_RQS(un);
29675                         }
29676 
29677                         continue;
29678                 }
29679 
29680                 if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) {
29681                         int reset_retval = 0;
29682 
29683                         SD_INFO(SD_LOG_DUMP, un,
29684                             "sddump: read failed with BUSY, try # %d\n", i);
29685 
29686                         if (un->un_f_lun_reset_enabled == TRUE) {
29687                                 reset_retval = scsi_reset(SD_ADDRESS(un),
29688                                     RESET_LUN);
29689                         }
29690                         if (reset_retval == 0) {
29691                                 (void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
29692                         }
29693                         (void) sd_send_polled_RQS(un);
29694 
29695                 } else {
29696                         SD_INFO(SD_LOG_DUMP, un,
29697                             "sddump: read failed with 0x%x, try # %d\n",
29698                             SD_GET_PKT_STATUS(pkt), i);
29699                         mutex_enter(SD_MUTEX(un));
29700                         sd_reset_target(un, pkt);
29701                         mutex_exit(SD_MUTEX(un));
29702                 }
29703 
29704                 /*
29705                  * If we are not getting anywhere with lun/target resets,
29706                  * let's reset the bus.
29707                  */
29708                 if (i > SD_NDUMP_RETRIES/2) {
29709                         (void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
29710                         (void) sd_send_polled_RQS(un);
29711                 }
29712 
29713         }
29714         scsi_destroy_pkt(pkt);
29715 
29716         if (err != 0) {
29717                 scsi_free_consistent_buf(bp);
29718                 *bpp = NULL;
29719         } else {
29720                 *bpp = bp;
29721         }
29722 
29723 done:
29724         mutex_enter(SD_MUTEX(un));
29725         return (err);
29726 }
29727 
29728 
29729 /*
29730  *    Function: sd_failfast_flushq
29731  *
29732  * Description: Take all bp's on the wait queue that have B_FAILFAST set
29733  *              in b_flags and move them onto the failfast queue, then kick
29734  *              off a thread to return all bp's on the failfast queue to
29735  *              their owners with an error set.
29736  *
29737  *   Arguments: un - pointer to the soft state struct for the instance.
29738  *
29739  *     Context: may execute in interrupt context.
29740  */
29741 
29742 static void
29743 sd_failfast_flushq(struct sd_lun *un)
29744 {
29745         struct buf *bp;
29746         struct buf *next_waitq_bp;
29747         struct buf *prev_waitq_bp = NULL;
29748 
29749         ASSERT(un != NULL);
29750         ASSERT(mutex_owned(SD_MUTEX(un)));
29751         ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE);
29752         ASSERT(un->un_failfast_bp == NULL);
29753 
29754         SD_TRACE(SD_LOG_IO_FAILFAST, un,
29755             "sd_failfast_flushq: entry: un:0x%p\n", un);
29756 
29757         /*
29758          * Check if we should flush all bufs when entering failfast state, or
29759          * just those with B_FAILFAST set.
29760          */
29761         if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) {
29762                 /*
29763                  * Move *all* bp's on the wait queue to the failfast flush
29764                  * queue, including those that do NOT have B_FAILFAST set.
29765                  */
29766                 if (un->un_failfast_headp == NULL) {
29767                         ASSERT(un->un_failfast_tailp == NULL);
29768                         un->un_failfast_headp = un->un_waitq_headp;
29769                 } else {
29770                         ASSERT(un->un_failfast_tailp != NULL);
29771                         un->un_failfast_tailp->av_forw = un->un_waitq_headp;
29772                 }
29773 
29774                 un->un_failfast_tailp = un->un_waitq_tailp;
29775 
29776                 /* update kstat for each bp moved out of the waitq */
29777                 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) {
29778                         SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29779                 }
29780 
29781                 /* empty the waitq */
29782                 un->un_waitq_headp = un->un_waitq_tailp = NULL;
29783 
29784         } else {
29785                 /*
29786                  * Go thru the wait queue, pick off all entries with
29787                  * B_FAILFAST set, and move these onto the failfast queue.
29788                  */
29789                 for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) {
29790                         /*
29791                          * Save the pointer to the next bp on the wait queue,
29792                          * so we get to it on the next iteration of this loop.
29793                          */
29794                         next_waitq_bp = bp->av_forw;
29795 
29796                         /*
29797                          * If this bp from the wait queue does NOT have
29798                          * B_FAILFAST set, just move on to the next element
29799                          * in the wait queue. Note, this is the only place
29800                          * where it is correct to set prev_waitq_bp.
29801                          */
29802                         if ((bp->b_flags & B_FAILFAST) == 0) {
29803                                 prev_waitq_bp = bp;
29804                                 continue;
29805                         }
29806 
29807                         /*
29808                          * Remove the bp from the wait queue.
29809                          */
29810                         if (bp == un->un_waitq_headp) {
29811                                 /* The bp is the first element of the waitq. */
29812                                 un->un_waitq_headp = next_waitq_bp;
29813                                 if (un->un_waitq_headp == NULL) {
29814                                         /* The wait queue is now empty */
29815                                         un->un_waitq_tailp = NULL;
29816                                 }
29817                         } else {
29818                                 /*
29819                                  * The bp is either somewhere in the middle
29820                                  * or at the end of the wait queue.
29821                                  */
29822                                 ASSERT(un->un_waitq_headp != NULL);
29823                                 ASSERT(prev_waitq_bp != NULL);
29824                                 ASSERT((prev_waitq_bp->b_flags & B_FAILFAST)
29825                                     == 0);
29826                                 if (bp == un->un_waitq_tailp) {
29827                                         /* bp is the last entry on the waitq. */
29828                                         ASSERT(next_waitq_bp == NULL);
29829                                         un->un_waitq_tailp = prev_waitq_bp;
29830                                 }
29831                                 prev_waitq_bp->av_forw = next_waitq_bp;
29832                         }
29833                         bp->av_forw = NULL;
29834 
29835                         /*
29836                          * update kstat since the bp is moved out of
29837                          * the waitq
29838                          */
29839                         SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29840 
29841                         /*
29842                          * Now put the bp onto the failfast queue.
29843                          */
29844                         if (un->un_failfast_headp == NULL) {
29845                                 /* failfast queue is currently empty */
29846                                 ASSERT(un->un_failfast_tailp == NULL);
29847                                 un->un_failfast_headp =
29848                                     un->un_failfast_tailp = bp;
29849                         } else {
29850                                 /* Add the bp to the end of the failfast q */
29851                                 ASSERT(un->un_failfast_tailp != NULL);
29852                                 ASSERT(un->un_failfast_tailp->b_flags &
29853                                     B_FAILFAST);
29854                                 un->un_failfast_tailp->av_forw = bp;
29855                                 un->un_failfast_tailp = bp;
29856                         }
29857                 }
29858         }
29859 
29860         /*
29861          * Now return all bp's on the failfast queue to their owners.
29862          */
29863         while ((bp = un->un_failfast_headp) != NULL) {
29864 
29865                 un->un_failfast_headp = bp->av_forw;
29866                 if (un->un_failfast_headp == NULL) {
29867                         un->un_failfast_tailp = NULL;
29868                 }
29869 
29870                 /*
29871                  * We want to return the bp with a failure error code, but
29872                  * we do not want a call to sd_start_cmds() to occur here,
29873                  * so use sd_return_failed_command_no_restart() instead of
29874                  * sd_return_failed_command().
29875                  */
29876                 sd_return_failed_command_no_restart(un, bp, EIO);
29877         }
29878 
29879         /* Flush the xbuf queues if required. */
29880         if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) {
29881                 ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback);
29882         }
29883 
29884         SD_TRACE(SD_LOG_IO_FAILFAST, un,
29885             "sd_failfast_flushq: exit: un:0x%p\n", un);
29886 }
29887 
29888 
29889 /*
29890  *    Function: sd_failfast_flushq_callback
29891  *
29892  * Description: Return TRUE if the given bp meets the criteria for failfast
29893  *              flushing. Used with ddi_xbuf_flushq(9F).
29894  *
29895  *   Arguments: bp - ptr to buf struct to be examined.
29896  *
29897  *     Context: Any
29898  */
29899 
29900 static int
29901 sd_failfast_flushq_callback(struct buf *bp)
29902 {
29903         /*
29904          * Return TRUE if (1) we want to flush ALL bufs when the failfast
29905          * state is entered; OR (2) the given bp has B_FAILFAST set.
29906          */
29907         return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) ||
29908             (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE);
29909 }
29910 
29911 
29912 
29913 /*
29914  * Function: sd_setup_next_xfer
29915  *
29916  * Description: Prepare next I/O operation using DMA_PARTIAL
29917  *
29918  */
29919 
29920 static int
29921 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
29922     struct scsi_pkt *pkt, struct sd_xbuf *xp)
29923 {
29924         ssize_t num_blks_not_xfered;
29925         daddr_t strt_blk_num;
29926         ssize_t bytes_not_xfered;
29927         int     rval;
29928 
29929         ASSERT(pkt->pkt_resid == 0);
29930 
29931         /*
29932          * Calculate next block number and amount to be transferred.
29933          *
29934          * How much data NOT transfered to the HBA yet.
29935          */
29936         bytes_not_xfered = xp->xb_dma_resid;
29937 
29938         /*
29939          * figure how many blocks NOT transfered to the HBA yet.
29940          */
29941         num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered);
29942 
29943         /*
29944          * set starting block number to the end of what WAS transfered.
29945          */
29946         strt_blk_num = xp->xb_blkno +
29947             SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered);
29948 
29949         /*
29950          * Move pkt to the next portion of the xfer.  sd_setup_next_rw_pkt
29951          * will call scsi_initpkt with NULL_FUNC so we do not have to release
29952          * the disk mutex here.
29953          */
29954         rval = sd_setup_next_rw_pkt(un, pkt, bp,
29955             strt_blk_num, num_blks_not_xfered);
29956 
29957         if (rval == 0) {
29958 
29959                 /*
29960                  * Success.
29961                  *
29962                  * Adjust things if there are still more blocks to be
29963                  * transfered.
29964                  */
29965                 xp->xb_dma_resid = pkt->pkt_resid;
29966                 pkt->pkt_resid = 0;
29967 
29968                 return (1);
29969         }
29970 
29971         /*
29972          * There's really only one possible return value from
29973          * sd_setup_next_rw_pkt which occurs when scsi_init_pkt
29974          * returns NULL.
29975          */
29976         ASSERT(rval == SD_PKT_ALLOC_FAILURE);
29977 
29978         bp->b_resid = bp->b_bcount;
29979         bp->b_flags |= B_ERROR;
29980 
29981         scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29982             "Error setting up next portion of DMA transfer\n");
29983 
29984         return (0);
29985 }
29986 
29987 /*
29988  *    Function: sd_panic_for_res_conflict
29989  *
29990  * Description: Call panic with a string formatted with "Reservation Conflict"
29991  *              and a human readable identifier indicating the SD instance
29992  *              that experienced the reservation conflict.
29993  *
29994  *   Arguments: un - pointer to the soft state struct for the instance.
29995  *
29996  *     Context: may execute in interrupt context.
29997  */
29998 
29999 #define SD_RESV_CONFLICT_FMT_LEN 40
30000 void
30001 sd_panic_for_res_conflict(struct sd_lun *un)
30002 {
30003         char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN];
30004         char path_str[MAXPATHLEN];
30005 
30006         (void) snprintf(panic_str, sizeof (panic_str),
30007             "Reservation Conflict\nDisk: %s",
30008             ddi_pathname(SD_DEVINFO(un), path_str));
30009 
30010         panic(panic_str);
30011 }
30012 
30013 /*
30014  * Note: The following sd_faultinjection_ioctl( ) routines implement
30015  * driver support for handling fault injection for error analysis
30016  * causing faults in multiple layers of the driver.
30017  *
30018  */
30019 
30020 #ifdef SD_FAULT_INJECTION
30021 static uint_t   sd_fault_injection_on = 0;
30022 
30023 /*
30024  *    Function: sd_faultinjection_ioctl()
30025  *
30026  * Description: This routine is the driver entry point for handling
30027  *              faultinjection ioctls to inject errors into the
30028  *              layer model
30029  *
30030  *   Arguments: cmd     - the ioctl cmd received
30031  *              arg     - the arguments from user and returns
30032  */
30033 
30034 static void
30035 sd_faultinjection_ioctl(int cmd, intptr_t arg,  struct sd_lun *un)
30036 {
30037         uint_t i = 0;
30038         uint_t rval;
30039 
30040         SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n");
30041 
30042         mutex_enter(SD_MUTEX(un));
30043 
30044         switch (cmd) {
30045         case SDIOCRUN:
30046                 /* Allow pushed faults to be injected */
30047                 SD_INFO(SD_LOG_SDTEST, un,
30048                     "sd_faultinjection_ioctl: Injecting Fault Run\n");
30049 
30050                 sd_fault_injection_on = 1;
30051 
30052                 SD_INFO(SD_LOG_IOERR, un,
30053                     "sd_faultinjection_ioctl: run finished\n");
30054                 break;
30055 
30056         case SDIOCSTART:
30057                 /* Start Injection Session */
30058                 SD_INFO(SD_LOG_SDTEST, un,
30059                     "sd_faultinjection_ioctl: Injecting Fault Start\n");
30060 
30061                 sd_fault_injection_on = 0;
30062                 un->sd_injection_mask = 0xFFFFFFFF;
30063                 for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30064                         un->sd_fi_fifo_pkt[i] = NULL;
30065                         un->sd_fi_fifo_xb[i] = NULL;
30066                         un->sd_fi_fifo_un[i] = NULL;
30067                         un->sd_fi_fifo_arq[i] = NULL;
30068                 }
30069                 un->sd_fi_fifo_start = 0;
30070                 un->sd_fi_fifo_end = 0;
30071 
30072                 mutex_enter(&(un->un_fi_mutex));
30073                 un->sd_fi_log[0] = '\0';
30074                 un->sd_fi_buf_len = 0;
30075                 mutex_exit(&(un->un_fi_mutex));
30076 
30077                 SD_INFO(SD_LOG_IOERR, un,
30078                     "sd_faultinjection_ioctl: start finished\n");
30079                 break;
30080 
30081         case SDIOCSTOP:
30082                 /* Stop Injection Session */
30083                 SD_INFO(SD_LOG_SDTEST, un,
30084                     "sd_faultinjection_ioctl: Injecting Fault Stop\n");
30085                 sd_fault_injection_on = 0;
30086                 un->sd_injection_mask = 0x0;
30087 
30088                 /* Empty stray or unuseds structs from fifo */
30089                 for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30090                         if (un->sd_fi_fifo_pkt[i] != NULL) {
30091                                 kmem_free(un->sd_fi_fifo_pkt[i],
30092                                     sizeof (struct sd_fi_pkt));
30093                         }
30094                         if (un->sd_fi_fifo_xb[i] != NULL) {
30095                                 kmem_free(un->sd_fi_fifo_xb[i],
30096                                     sizeof (struct sd_fi_xb));
30097                         }
30098                         if (un->sd_fi_fifo_un[i] != NULL) {
30099                                 kmem_free(un->sd_fi_fifo_un[i],
30100                                     sizeof (struct sd_fi_un));
30101                         }
30102                         if (un->sd_fi_fifo_arq[i] != NULL) {
30103                                 kmem_free(un->sd_fi_fifo_arq[i],
30104                                     sizeof (struct sd_fi_arq));
30105                         }
30106                         un->sd_fi_fifo_pkt[i] = NULL;
30107                         un->sd_fi_fifo_un[i] = NULL;
30108                         un->sd_fi_fifo_xb[i] = NULL;
30109                         un->sd_fi_fifo_arq[i] = NULL;
30110                 }
30111                 un->sd_fi_fifo_start = 0;
30112                 un->sd_fi_fifo_end = 0;
30113 
30114                 SD_INFO(SD_LOG_IOERR, un,
30115                     "sd_faultinjection_ioctl: stop finished\n");
30116                 break;
30117 
30118         case SDIOCINSERTPKT:
30119                 /* Store a packet struct to be pushed onto fifo */
30120                 SD_INFO(SD_LOG_SDTEST, un,
30121                     "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n");
30122 
30123                 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30124 
30125                 sd_fault_injection_on = 0;
30126 
30127                 /* No more that SD_FI_MAX_ERROR allowed in Queue */
30128                 if (un->sd_fi_fifo_pkt[i] != NULL) {
30129                         kmem_free(un->sd_fi_fifo_pkt[i],
30130                             sizeof (struct sd_fi_pkt));
30131                 }
30132                 if (arg != NULL) {
30133                         un->sd_fi_fifo_pkt[i] =
30134                             kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP);
30135                         if (un->sd_fi_fifo_pkt[i] == NULL) {
30136                                 /* Alloc failed don't store anything */
30137                                 break;
30138                         }
30139                         rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i],
30140                             sizeof (struct sd_fi_pkt), 0);
30141                         if (rval == -1) {
30142                                 kmem_free(un->sd_fi_fifo_pkt[i],
30143                                     sizeof (struct sd_fi_pkt));
30144                                 un->sd_fi_fifo_pkt[i] = NULL;
30145                         }
30146                 } else {
30147                         SD_INFO(SD_LOG_IOERR, un,
30148                             "sd_faultinjection_ioctl: pkt null\n");
30149                 }
30150                 break;
30151 
30152         case SDIOCINSERTXB:
30153                 /* Store a xb struct to be pushed onto fifo */
30154                 SD_INFO(SD_LOG_SDTEST, un,
30155                     "sd_faultinjection_ioctl: Injecting Fault Insert XB\n");
30156 
30157                 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30158 
30159                 sd_fault_injection_on = 0;
30160 
30161                 if (un->sd_fi_fifo_xb[i] != NULL) {
30162                         kmem_free(un->sd_fi_fifo_xb[i],
30163                             sizeof (struct sd_fi_xb));
30164                         un->sd_fi_fifo_xb[i] = NULL;
30165                 }
30166                 if (arg != NULL) {
30167                         un->sd_fi_fifo_xb[i] =
30168                             kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP);
30169                         if (un->sd_fi_fifo_xb[i] == NULL) {
30170                                 /* Alloc failed don't store anything */
30171                                 break;
30172                         }
30173                         rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i],
30174                             sizeof (struct sd_fi_xb), 0);
30175 
30176                         if (rval == -1) {
30177                                 kmem_free(un->sd_fi_fifo_xb[i],
30178                                     sizeof (struct sd_fi_xb));
30179                                 un->sd_fi_fifo_xb[i] = NULL;
30180                         }
30181                 } else {
30182                         SD_INFO(SD_LOG_IOERR, un,
30183                             "sd_faultinjection_ioctl: xb null\n");
30184                 }
30185                 break;
30186 
30187         case SDIOCINSERTUN:
30188                 /* Store a un struct to be pushed onto fifo */
30189                 SD_INFO(SD_LOG_SDTEST, un,
30190                     "sd_faultinjection_ioctl: Injecting Fault Insert UN\n");
30191 
30192                 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30193 
30194                 sd_fault_injection_on = 0;
30195 
30196                 if (un->sd_fi_fifo_un[i] != NULL) {
30197                         kmem_free(un->sd_fi_fifo_un[i],
30198                             sizeof (struct sd_fi_un));
30199                         un->sd_fi_fifo_un[i] = NULL;
30200                 }
30201                 if (arg != NULL) {
30202                         un->sd_fi_fifo_un[i] =
30203                             kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP);
30204                         if (un->sd_fi_fifo_un[i] == NULL) {
30205                                 /* Alloc failed don't store anything */
30206                                 break;
30207                         }
30208                         rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i],
30209                             sizeof (struct sd_fi_un), 0);
30210                         if (rval == -1) {
30211                                 kmem_free(un->sd_fi_fifo_un[i],
30212                                     sizeof (struct sd_fi_un));
30213                                 un->sd_fi_fifo_un[i] = NULL;
30214                         }
30215 
30216                 } else {
30217                         SD_INFO(SD_LOG_IOERR, un,
30218                             "sd_faultinjection_ioctl: un null\n");
30219                 }
30220 
30221                 break;
30222 
30223         case SDIOCINSERTARQ:
30224                 /* Store a arq struct to be pushed onto fifo */
30225                 SD_INFO(SD_LOG_SDTEST, un,
30226                     "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n");
30227                 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30228 
30229                 sd_fault_injection_on = 0;
30230 
30231                 if (un->sd_fi_fifo_arq[i] != NULL) {
30232                         kmem_free(un->sd_fi_fifo_arq[i],
30233                             sizeof (struct sd_fi_arq));
30234                         un->sd_fi_fifo_arq[i] = NULL;
30235                 }
30236                 if (arg != NULL) {
30237                         un->sd_fi_fifo_arq[i] =
30238                             kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP);
30239                         if (un->sd_fi_fifo_arq[i] == NULL) {
30240                                 /* Alloc failed don't store anything */
30241                                 break;
30242                         }
30243                         rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i],
30244                             sizeof (struct sd_fi_arq), 0);
30245                         if (rval == -1) {
30246                                 kmem_free(un->sd_fi_fifo_arq[i],
30247                                     sizeof (struct sd_fi_arq));
30248                                 un->sd_fi_fifo_arq[i] = NULL;
30249                         }
30250 
30251                 } else {
30252                         SD_INFO(SD_LOG_IOERR, un,
30253                             "sd_faultinjection_ioctl: arq null\n");
30254                 }
30255 
30256                 break;
30257 
30258         case SDIOCPUSH:
30259                 /* Push stored xb, pkt, un, and arq onto fifo */
30260                 sd_fault_injection_on = 0;
30261 
30262                 if (arg != NULL) {
30263                         rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0);
30264                         if (rval != -1 &&
30265                             un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30266                                 un->sd_fi_fifo_end += i;
30267                         }
30268                 } else {
30269                         SD_INFO(SD_LOG_IOERR, un,
30270                             "sd_faultinjection_ioctl: push arg null\n");
30271                         if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30272                                 un->sd_fi_fifo_end++;
30273                         }
30274                 }
30275                 SD_INFO(SD_LOG_IOERR, un,
30276                     "sd_faultinjection_ioctl: push to end=%d\n",
30277                     un->sd_fi_fifo_end);
30278                 break;
30279 
30280         case SDIOCRETRIEVE:
30281                 /* Return buffer of log from Injection session */
30282                 SD_INFO(SD_LOG_SDTEST, un,
30283                     "sd_faultinjection_ioctl: Injecting Fault Retreive");
30284 
30285                 sd_fault_injection_on = 0;
30286 
30287                 mutex_enter(&(un->un_fi_mutex));
30288                 rval = ddi_copyout(un->sd_fi_log, (void *)arg,
30289                     un->sd_fi_buf_len+1, 0);
30290                 mutex_exit(&(un->un_fi_mutex));
30291 
30292                 if (rval == -1) {
30293                         /*
30294                          * arg is possibly invalid setting
30295                          * it to NULL for return
30296                          */
30297                         arg = NULL;
30298                 }
30299                 break;
30300         }
30301 
30302         mutex_exit(SD_MUTEX(un));
30303         SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: exit\n");
30304 }
30305 
30306 
30307 /*
30308  *    Function: sd_injection_log()
30309  *
30310  * Description: This routine adds buff to the already existing injection log
30311  *              for retrieval via faultinjection_ioctl for use in fault
30312  *              detection and recovery
30313  *
30314  *   Arguments: buf - the string to add to the log
30315  */
30316 
30317 static void
30318 sd_injection_log(char *buf, struct sd_lun *un)
30319 {
30320         uint_t len;
30321 
30322         ASSERT(un != NULL);
30323         ASSERT(buf != NULL);
30324 
30325         mutex_enter(&(un->un_fi_mutex));
30326 
30327         len = min(strlen(buf), 255);
30328         /* Add logged value to Injection log to be returned later */
30329         if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) {
30330                 uint_t  offset = strlen((char *)un->sd_fi_log);
30331                 char *destp = (char *)un->sd_fi_log + offset;
30332                 int i;
30333                 for (i = 0; i < len; i++) {
30334                         *destp++ = *buf++;
30335                 }
30336                 un->sd_fi_buf_len += len;
30337                 un->sd_fi_log[un->sd_fi_buf_len] = '\0';
30338         }
30339 
30340         mutex_exit(&(un->un_fi_mutex));
30341 }
30342 
30343 
30344 /*
30345  *    Function: sd_faultinjection()
30346  *
30347  * Description: This routine takes the pkt and changes its
30348  *              content based on error injection scenerio.
30349  *
30350  *   Arguments: pktp    - packet to be changed
30351  */
30352 
30353 static void
30354 sd_faultinjection(struct scsi_pkt *pktp)
30355 {
30356         uint_t i;
30357         struct sd_fi_pkt *fi_pkt;
30358         struct sd_fi_xb *fi_xb;
30359         struct sd_fi_un *fi_un;
30360         struct sd_fi_arq *fi_arq;
30361         struct buf *bp;
30362         struct sd_xbuf *xb;
30363         struct sd_lun *un;
30364 
30365         ASSERT(pktp != NULL);
30366 
30367         /* pull bp xb and un from pktp */
30368         bp = (struct buf *)pktp->pkt_private;
30369         xb = SD_GET_XBUF(bp);
30370         un = SD_GET_UN(bp);
30371 
30372         ASSERT(un != NULL);
30373 
30374         mutex_enter(SD_MUTEX(un));
30375 
30376         SD_TRACE(SD_LOG_SDTEST, un,
30377             "sd_faultinjection: entry Injection from sdintr\n");
30378 
30379         /* if injection is off return */
30380         if (sd_fault_injection_on == 0 ||
30381             un->sd_fi_fifo_start == un->sd_fi_fifo_end) {
30382                 mutex_exit(SD_MUTEX(un));
30383                 return;
30384         }
30385 
30386         SD_INFO(SD_LOG_SDTEST, un,
30387             "sd_faultinjection: is working for copying\n");
30388 
30389         /* take next set off fifo */
30390         i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR;
30391 
30392         fi_pkt = un->sd_fi_fifo_pkt[i];
30393         fi_xb = un->sd_fi_fifo_xb[i];
30394         fi_un = un->sd_fi_fifo_un[i];
30395         fi_arq = un->sd_fi_fifo_arq[i];
30396 
30397 
30398         /* set variables accordingly */
30399         /* set pkt if it was on fifo */
30400         if (fi_pkt != NULL) {
30401                 SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags");
30402                 SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp");
30403                 if (fi_pkt->pkt_cdbp != 0xff)
30404                         SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp");
30405                 SD_CONDSET(pktp, pkt, pkt_state, "pkt_state");
30406                 SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics");
30407                 SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason");
30408 
30409         }
30410         /* set xb if it was on fifo */
30411         if (fi_xb != NULL) {
30412                 SD_CONDSET(xb, xb, xb_blkno, "xb_blkno");
30413                 SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid");
30414                 if (fi_xb->xb_retry_count != 0)
30415                         SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count");
30416                 SD_CONDSET(xb, xb, xb_victim_retry_count,
30417                     "xb_victim_retry_count");
30418                 SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status");
30419                 SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state");
30420                 SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid");
30421 
30422                 /* copy in block data from sense */
30423                 /*
30424                  * if (fi_xb->xb_sense_data[0] != -1) {
30425                  *      bcopy(fi_xb->xb_sense_data, xb->xb_sense_data,
30426                  *      SENSE_LENGTH);
30427                  * }
30428                  */
30429                 bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, SENSE_LENGTH);
30430 
30431                 /* copy in extended sense codes */
30432                 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30433                     xb, es_code, "es_code");
30434                 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30435                     xb, es_key, "es_key");
30436                 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30437                     xb, es_add_code, "es_add_code");
30438                 SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30439                     xb, es_qual_code, "es_qual_code");
30440                 struct scsi_extended_sense *esp;
30441                 esp = (struct scsi_extended_sense *)xb->xb_sense_data;
30442                 esp->es_class = CLASS_EXTENDED_SENSE;
30443         }
30444 
30445         /* set un if it was on fifo */
30446         if (fi_un != NULL) {
30447                 SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb");
30448                 SD_CONDSET(un, un, un_ctype, "un_ctype");
30449                 SD_CONDSET(un, un, un_reset_retry_count,
30450                     "un_reset_retry_count");
30451                 SD_CONDSET(un, un, un_reservation_type, "un_reservation_type");
30452                 SD_CONDSET(un, un, un_resvd_status, "un_resvd_status");
30453                 SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled");
30454                 SD_CONDSET(un, un, un_f_allow_bus_device_reset,
30455                     "un_f_allow_bus_device_reset");
30456                 SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing");
30457 
30458         }
30459 
30460         /* copy in auto request sense if it was on fifo */
30461         if (fi_arq != NULL) {
30462                 bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq));
30463         }
30464 
30465         /* free structs */
30466         if (un->sd_fi_fifo_pkt[i] != NULL) {
30467                 kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt));
30468         }
30469         if (un->sd_fi_fifo_xb[i] != NULL) {
30470                 kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb));
30471         }
30472         if (un->sd_fi_fifo_un[i] != NULL) {
30473                 kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un));
30474         }
30475         if (un->sd_fi_fifo_arq[i] != NULL) {
30476                 kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq));
30477         }
30478 
30479         /*
30480          * kmem_free does not gurantee to set to NULL
30481          * since we uses these to determine if we set
30482          * values or not lets confirm they are always
30483          * NULL after free
30484          */
30485         un->sd_fi_fifo_pkt[i] = NULL;
30486         un->sd_fi_fifo_un[i] = NULL;
30487         un->sd_fi_fifo_xb[i] = NULL;
30488         un->sd_fi_fifo_arq[i] = NULL;
30489 
30490         un->sd_fi_fifo_start++;
30491 
30492         mutex_exit(SD_MUTEX(un));
30493 
30494         SD_INFO(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n");
30495 }
30496 
30497 #endif /* SD_FAULT_INJECTION */
30498 
30499 /*
30500  * This routine is invoked in sd_unit_attach(). Before calling it, the
30501  * properties in conf file should be processed already, and "hotpluggable"
30502  * property was processed also.
30503  *
30504  * The sd driver distinguishes 3 different type of devices: removable media,
30505  * non-removable media, and hotpluggable. Below the differences are defined:
30506  *
30507  * 1. Device ID
30508  *
30509  *     The device ID of a device is used to identify this device. Refer to
30510  *     ddi_devid_register(9F).
30511  *
30512  *     For a non-removable media disk device which can provide 0x80 or 0x83
30513  *     VPD page (refer to INQUIRY command of SCSI SPC specification), a unique
30514  *     device ID is created to identify this device. For other non-removable
30515  *     media devices, a default device ID is created only if this device has
30516  *     at least 2 alter cylinders. Otherwise, this device has no devid.
30517  *
30518  *     -------------------------------------------------------
30519  *     removable media   hotpluggable  | Can Have Device ID
30520  *     -------------------------------------------------------
30521  *         false             false     |     Yes
30522  *         false             true      |     Yes
30523  *         true                x       |     No
30524  *     ------------------------------------------------------
30525  *
30526  *
30527  * 2. SCSI group 4 commands
30528  *
30529  *     In SCSI specs, only some commands in group 4 command set can use
30530  *     8-byte addresses that can be used to access >2TB storage spaces.
30531  *     Other commands have no such capability. Without supporting group4,
30532  *     it is impossible to make full use of storage spaces of a disk with
30533  *     capacity larger than 2TB.
30534  *
30535  *     -----------------------------------------------
30536  *     removable media   hotpluggable   LP64  |  Group
30537  *     -----------------------------------------------
30538  *           false          false       false |   1
30539  *           false          false       true  |   4
30540  *           false          true        false |   1
30541  *           false          true        true  |   4
30542  *           true             x           x   |   5
30543  *     -----------------------------------------------
30544  *
30545  *
30546  * 3. Check for VTOC Label
30547  *
30548  *     If a direct-access disk has no EFI label, sd will check if it has a
30549  *     valid VTOC label. Now, sd also does that check for removable media
30550  *     and hotpluggable devices.
30551  *
30552  *     --------------------------------------------------------------
30553  *     Direct-Access   removable media    hotpluggable |  Check Label
30554  *     -------------------------------------------------------------
30555  *         false          false           false        |   No
30556  *         false          false           true         |   No
30557  *         false          true            false        |   Yes
30558  *         false          true            true         |   Yes
30559  *         true            x                x          |   Yes
30560  *     --------------------------------------------------------------
30561  *
30562  *
30563  * 4. Building default VTOC label
30564  *
30565  *     As section 3 says, sd checks if some kinds of devices have VTOC label.
30566  *     If those devices have no valid VTOC label, sd(7d) will attempt to
30567  *     create default VTOC for them. Currently sd creates default VTOC label
30568  *     for all devices on x86 platform (VTOC_16), but only for removable
30569  *     media devices on SPARC (VTOC_8).
30570  *
30571  *     -----------------------------------------------------------
30572  *       removable media hotpluggable platform   |   Default Label
30573  *     -----------------------------------------------------------
30574  *             false          false    sparc     |     No
30575  *             false          true      x86      |     Yes
30576  *             false          true     sparc     |     Yes
30577  *             true             x        x       |     Yes
30578  *     ----------------------------------------------------------
30579  *
30580  *
30581  * 5. Supported blocksizes of target devices
30582  *
30583  *     Sd supports non-512-byte blocksize for removable media devices only.
30584  *     For other devices, only 512-byte blocksize is supported. This may be
30585  *     changed in near future because some RAID devices require non-512-byte
30586  *     blocksize
30587  *
30588  *     -----------------------------------------------------------
30589  *     removable media    hotpluggable    | non-512-byte blocksize
30590  *     -----------------------------------------------------------
30591  *           false          false         |   No
30592  *           false          true          |   No
30593  *           true             x           |   Yes
30594  *     -----------------------------------------------------------
30595  *
30596  *
30597  * 6. Automatic mount & unmount
30598  *
30599  *     Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query
30600  *     if a device is removable media device. It return 1 for removable media
30601  *     devices, and 0 for others.
30602  *
30603  *     The automatic mounting subsystem should distinguish between the types
30604  *     of devices and apply automounting policies to each.
30605  *
30606  *
30607  * 7. fdisk partition management
30608  *
30609  *     Fdisk is traditional partition method on x86 platform. Sd(7d) driver
30610  *     just supports fdisk partitions on x86 platform. On sparc platform, sd
30611  *     doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize
30612  *     fdisk partitions on both x86 and SPARC platform.
30613  *
30614  *     -----------------------------------------------------------
30615  *       platform   removable media  USB/1394  |  fdisk supported
30616  *     -----------------------------------------------------------
30617  *        x86         X               X        |       true
30618  *     ------------------------------------------------------------
30619  *        sparc       X               X        |       false
30620  *     ------------------------------------------------------------
30621  *
30622  *
30623  * 8. MBOOT/MBR
30624  *
30625  *     Although sd(7d) doesn't support fdisk on SPARC platform, it does support
30626  *     read/write mboot for removable media devices on sparc platform.
30627  *
30628  *     -----------------------------------------------------------
30629  *       platform   removable media  USB/1394  |  mboot supported
30630  *     -----------------------------------------------------------
30631  *        x86         X               X        |       true
30632  *     ------------------------------------------------------------
30633  *        sparc      false           false     |       false
30634  *        sparc      false           true      |       true
30635  *        sparc      true            false     |       true
30636  *        sparc      true            true      |       true
30637  *     ------------------------------------------------------------
30638  *
30639  *
30640  * 9.  error handling during opening device
30641  *
30642  *     If failed to open a disk device, an errno is returned. For some kinds
30643  *     of errors, different errno is returned depending on if this device is
30644  *     a removable media device. This brings USB/1394 hard disks in line with
30645  *     expected hard disk behavior. It is not expected that this breaks any
30646  *     application.
30647  *
30648  *     ------------------------------------------------------
30649  *       removable media    hotpluggable   |  errno
30650  *     ------------------------------------------------------
30651  *             false          false        |   EIO
30652  *             false          true         |   EIO
30653  *             true             x          |   ENXIO
30654  *     ------------------------------------------------------
30655  *
30656  *
30657  * 11. ioctls: DKIOCEJECT, CDROMEJECT
30658  *
30659  *     These IOCTLs are applicable only to removable media devices.
30660  *
30661  *     -----------------------------------------------------------
30662  *       removable media    hotpluggable   |DKIOCEJECT, CDROMEJECT
30663  *     -----------------------------------------------------------
30664  *             false          false        |     No
30665  *             false          true         |     No
30666  *             true            x           |     Yes
30667  *     -----------------------------------------------------------
30668  *
30669  *
30670  * 12. Kstats for partitions
30671  *
30672  *     sd creates partition kstat for non-removable media devices. USB and
30673  *     Firewire hard disks now have partition kstats
30674  *
30675  *      ------------------------------------------------------
30676  *       removable media    hotpluggable   |   kstat
30677  *      ------------------------------------------------------
30678  *             false          false        |    Yes
30679  *             false          true         |    Yes
30680  *             true             x          |    No
30681  *       ------------------------------------------------------
30682  *
30683  *
30684  * 13. Removable media & hotpluggable properties
30685  *
30686  *     Sd driver creates a "removable-media" property for removable media
30687  *     devices. Parent nexus drivers create a "hotpluggable" property if
30688  *     it supports hotplugging.
30689  *
30690  *     ---------------------------------------------------------------------
30691  *     removable media   hotpluggable |  "removable-media"   " hotpluggable"
30692  *     ---------------------------------------------------------------------
30693  *       false            false       |    No                   No
30694  *       false            true        |    No                   Yes
30695  *       true             false       |    Yes                  No
30696  *       true             true        |    Yes                  Yes
30697  *     ---------------------------------------------------------------------
30698  *
30699  *
30700  * 14. Power Management
30701  *
30702  *     sd only power manages removable media devices or devices that support
30703  *     LOG_SENSE or have a "pm-capable" property  (PSARC/2002/250)
30704  *
30705  *     A parent nexus that supports hotplugging can also set "pm-capable"
30706  *     if the disk can be power managed.
30707  *
30708  *     ------------------------------------------------------------
30709  *       removable media hotpluggable pm-capable  |   power manage
30710  *     ------------------------------------------------------------
30711  *             false          false     false     |     No
30712  *             false          false     true      |     Yes
30713  *             false          true      false     |     No
30714  *             false          true      true      |     Yes
30715  *             true             x        x        |     Yes
30716  *     ------------------------------------------------------------
30717  *
30718  *      USB and firewire hard disks can now be power managed independently
30719  *      of the framebuffer
30720  *
30721  *
30722  * 15. Support for USB disks with capacity larger than 1TB
30723  *
30724  *     Currently, sd doesn't permit a fixed disk device with capacity
30725  *     larger than 1TB to be used in a 32-bit operating system environment.
30726  *     However, sd doesn't do that for removable media devices. Instead, it
30727  *     assumes that removable media devices cannot have a capacity larger
30728  *     than 1TB. Therefore, using those devices on 32-bit system is partially
30729  *     supported, which can cause some unexpected results.
30730  *
30731  *     ---------------------------------------------------------------------
30732  *       removable media    USB/1394 | Capacity > 1TB |   Used in 32-bit env
30733  *     ---------------------------------------------------------------------
30734  *             false          false  |   true         |     no
30735  *             false          true   |   true         |     no
30736  *             true           false  |   true         |     Yes
30737  *             true           true   |   true         |     Yes
30738  *     ---------------------------------------------------------------------
30739  *
30740  *
30741  * 16. Check write-protection at open time
30742  *
30743  *     When a removable media device is being opened for writing without NDELAY
30744  *     flag, sd will check if this device is writable. If attempting to open
30745  *     without NDELAY flag a write-protected device, this operation will abort.
30746  *
30747  *     ------------------------------------------------------------
30748  *       removable media    USB/1394   |   WP Check
30749  *     ------------------------------------------------------------
30750  *             false          false    |     No
30751  *             false          true     |     No
30752  *             true           false    |     Yes
30753  *             true           true     |     Yes
30754  *     ------------------------------------------------------------
30755  *
30756  *
30757  * 17. syslog when corrupted VTOC is encountered
30758  *
30759  *      Currently, if an invalid VTOC is encountered, sd only print syslog
30760  *      for fixed SCSI disks.
30761  *     ------------------------------------------------------------
30762  *       removable media    USB/1394   |   print syslog
30763  *     ------------------------------------------------------------
30764  *             false          false    |     Yes
30765  *             false          true     |     No
30766  *             true           false    |     No
30767  *             true           true     |     No
30768  *     ------------------------------------------------------------
30769  */
30770 static void
30771 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi)
30772 {
30773         int     pm_cap;
30774 
30775         ASSERT(un->un_sd);
30776         ASSERT(un->un_sd->sd_inq);
30777 
30778         /*
30779          * Enable SYNC CACHE support for all devices.
30780          */
30781         un->un_f_sync_cache_supported = TRUE;
30782 
30783         /*
30784          * Set the sync cache required flag to false.
30785          * This would ensure that there is no SYNC CACHE
30786          * sent when there are no writes
30787          */
30788         un->un_f_sync_cache_required = FALSE;
30789 
30790         if (un->un_sd->sd_inq->inq_rmb) {
30791                 /*
30792                  * The media of this device is removable. And for this kind
30793                  * of devices, it is possible to change medium after opening
30794                  * devices. Thus we should support this operation.
30795                  */
30796                 un->un_f_has_removable_media = TRUE;
30797 
30798                 /*
30799                  * support non-512-byte blocksize of removable media devices
30800                  */
30801                 un->un_f_non_devbsize_supported = TRUE;
30802 
30803                 /*
30804                  * Assume that all removable media devices support DOOR_LOCK
30805                  */
30806                 un->un_f_doorlock_supported = TRUE;
30807 
30808                 /*
30809                  * For a removable media device, it is possible to be opened
30810                  * with NDELAY flag when there is no media in drive, in this
30811                  * case we don't care if device is writable. But if without
30812                  * NDELAY flag, we need to check if media is write-protected.
30813                  */
30814                 un->un_f_chk_wp_open = TRUE;
30815 
30816                 /*
30817                  * need to start a SCSI watch thread to monitor media state,
30818                  * when media is being inserted or ejected, notify syseventd.
30819                  */
30820                 un->un_f_monitor_media_state = TRUE;
30821 
30822                 /*
30823                  * Some devices don't support START_STOP_UNIT command.
30824                  * Therefore, we'd better check if a device supports it
30825                  * before sending it.
30826                  */
30827                 un->un_f_check_start_stop = TRUE;
30828 
30829                 /*
30830                  * support eject media ioctl:
30831                  *              FDEJECT, DKIOCEJECT, CDROMEJECT
30832                  */
30833                 un->un_f_eject_media_supported = TRUE;
30834 
30835                 /*
30836                  * Because many removable-media devices don't support
30837                  * LOG_SENSE, we couldn't use this command to check if
30838                  * a removable media device support power-management.
30839                  * We assume that they support power-management via
30840                  * START_STOP_UNIT command and can be spun up and down
30841                  * without limitations.
30842                  */
30843                 un->un_f_pm_supported = TRUE;
30844 
30845                 /*
30846                  * Need to create a zero length (Boolean) property
30847                  * removable-media for the removable media devices.
30848                  * Note that the return value of the property is not being
30849                  * checked, since if unable to create the property
30850                  * then do not want the attach to fail altogether. Consistent
30851                  * with other property creation in attach.
30852                  */
30853                 (void) ddi_prop_create(DDI_DEV_T_NONE, devi,
30854                     DDI_PROP_CANSLEEP, "removable-media", NULL, 0);
30855 
30856         } else {
30857                 /*
30858                  * create device ID for device
30859                  */
30860                 un->un_f_devid_supported = TRUE;
30861 
30862                 /*
30863                  * Spin up non-removable-media devices once it is attached
30864                  */
30865                 un->un_f_attach_spinup = TRUE;
30866 
30867                 /*
30868                  * According to SCSI specification, Sense data has two kinds of
30869                  * format: fixed format, and descriptor format. At present, we
30870                  * don't support descriptor format sense data for removable
30871                  * media.
30872                  */
30873                 if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) {
30874                         un->un_f_descr_format_supported = TRUE;
30875                 }
30876 
30877                 /*
30878                  * kstats are created only for non-removable media devices.
30879                  *
30880                  * Set this in sd.conf to 0 in order to disable kstats.  The
30881                  * default is 1, so they are enabled by default.
30882                  */
30883                 un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY,
30884                     SD_DEVINFO(un), DDI_PROP_DONTPASS,
30885                     "enable-partition-kstats", 1));
30886 
30887                 /*
30888                  * Check if HBA has set the "pm-capable" property.
30889                  * If "pm-capable" exists and is non-zero then we can
30890                  * power manage the device without checking the start/stop
30891                  * cycle count log sense page.
30892                  *
30893                  * If "pm-capable" exists and is set to be false (0),
30894                  * then we should not power manage the device.
30895                  *
30896                  * If "pm-capable" doesn't exist then pm_cap will
30897                  * be set to SD_PM_CAPABLE_UNDEFINED (-1).  In this case,
30898                  * sd will check the start/stop cycle count log sense page
30899                  * and power manage the device if the cycle count limit has
30900                  * not been exceeded.
30901                  */
30902                 pm_cap = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
30903                     DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED);
30904                 if (SD_PM_CAPABLE_IS_UNDEFINED(pm_cap)) {
30905                         un->un_f_log_sense_supported = TRUE;
30906                         if (!un->un_f_power_condition_disabled &&
30907                             SD_INQUIRY(un)->inq_ansi == 6) {
30908                                 un->un_f_power_condition_supported = TRUE;
30909                         }
30910                 } else {
30911                         /*
30912                          * pm-capable property exists.
30913                          *
30914                          * Convert "TRUE" values for pm_cap to
30915                          * SD_PM_CAPABLE_IS_TRUE to make it easier to check
30916                          * later. "TRUE" values are any values defined in
30917                          * inquiry.h.
30918                          */
30919                         if (SD_PM_CAPABLE_IS_FALSE(pm_cap)) {
30920                                 un->un_f_log_sense_supported = FALSE;
30921                         } else {
30922                                 /* SD_PM_CAPABLE_IS_TRUE case */
30923                                 un->un_f_pm_supported = TRUE;
30924                                 if (!un->un_f_power_condition_disabled &&
30925                                     SD_PM_CAPABLE_IS_SPC_4(pm_cap)) {
30926                                         un->un_f_power_condition_supported =
30927                                             TRUE;
30928                                 }
30929                                 if (SD_PM_CAP_LOG_SUPPORTED(pm_cap)) {
30930                                         un->un_f_log_sense_supported = TRUE;
30931                                         un->un_f_pm_log_sense_smart =
30932                                             SD_PM_CAP_SMART_LOG(pm_cap);
30933                                 }
30934                         }
30935 
30936                         SD_INFO(SD_LOG_ATTACH_DETACH, un,
30937                             "sd_unit_attach: un:0x%p pm-capable "
30938                             "property set to %d.\n", un, un->un_f_pm_supported);
30939                 }
30940         }
30941 
30942         if (un->un_f_is_hotpluggable) {
30943 
30944                 /*
30945                  * Have to watch hotpluggable devices as well, since
30946                  * that's the only way for userland applications to
30947                  * detect hot removal while device is busy/mounted.
30948                  */
30949                 un->un_f_monitor_media_state = TRUE;
30950 
30951                 un->un_f_check_start_stop = TRUE;
30952 
30953         }
30954 }
30955 
30956 /*
30957  * sd_tg_rdwr:
30958  * Provides rdwr access for cmlb via sd_tgops. The start_block is
30959  * in sys block size, req_length in bytes.
30960  *
30961  */
30962 static int
30963 sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
30964     diskaddr_t start_block, size_t reqlength, void *tg_cookie)
30965 {
30966         struct sd_lun *un;
30967         int path_flag = (int)(uintptr_t)tg_cookie;
30968         char *dkl = NULL;
30969         diskaddr_t real_addr = start_block;
30970         diskaddr_t first_byte, end_block;
30971 
30972         size_t  buffer_size = reqlength;
30973         int rval = 0;
30974         diskaddr_t      cap;
30975         uint32_t        lbasize;
30976         sd_ssc_t        *ssc;
30977 
30978         un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
30979         if (un == NULL)
30980                 return (ENXIO);
30981 
30982         if (cmd != TG_READ && cmd != TG_WRITE)
30983                 return (EINVAL);
30984 
30985         ssc = sd_ssc_init(un);
30986         mutex_enter(SD_MUTEX(un));
30987         if (un->un_f_tgt_blocksize_is_valid == FALSE) {
30988                 mutex_exit(SD_MUTEX(un));
30989                 rval = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
30990                     &lbasize, path_flag);
30991                 if (rval != 0)
30992                         goto done1;
30993                 mutex_enter(SD_MUTEX(un));
30994                 sd_update_block_info(un, lbasize, cap);
30995                 if ((un->un_f_tgt_blocksize_is_valid == FALSE)) {
30996                         mutex_exit(SD_MUTEX(un));
30997                         rval = EIO;
30998                         goto done;
30999                 }
31000         }
31001 
31002         if (NOT_DEVBSIZE(un)) {
31003                 /*
31004                  * sys_blocksize != tgt_blocksize, need to re-adjust
31005                  * blkno and save the index to beginning of dk_label
31006                  */
31007                 first_byte  = SD_SYSBLOCKS2BYTES(start_block);
31008                 real_addr = first_byte / un->un_tgt_blocksize;
31009 
31010                 end_block = (first_byte + reqlength +
31011                     un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
31012 
31013                 /* round up buffer size to multiple of target block size */
31014                 buffer_size = (end_block - real_addr) * un->un_tgt_blocksize;
31015 
31016                 SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_tg_rdwr",
31017                     "label_addr: 0x%x allocation size: 0x%x\n",
31018                     real_addr, buffer_size);
31019 
31020                 if (((first_byte % un->un_tgt_blocksize) != 0) ||
31021                     (reqlength % un->un_tgt_blocksize) != 0)
31022                         /* the request is not aligned */
31023                         dkl = kmem_zalloc(buffer_size, KM_SLEEP);
31024         }
31025 
31026         /*
31027          * The MMC standard allows READ CAPACITY to be
31028          * inaccurate by a bounded amount (in the interest of
31029          * response latency).  As a result, failed READs are
31030          * commonplace (due to the reading of metadata and not
31031          * data). Depending on the per-Vendor/drive Sense data,
31032          * the failed READ can cause many (unnecessary) retries.
31033          */
31034 
31035         if (ISCD(un) && (cmd == TG_READ) &&
31036             (un->un_f_blockcount_is_valid == TRUE) &&
31037             ((start_block == (un->un_blockcount - 1))||
31038             (start_block == (un->un_blockcount - 2)))) {
31039                         path_flag = SD_PATH_DIRECT_PRIORITY;
31040         }
31041 
31042         mutex_exit(SD_MUTEX(un));
31043         if (cmd == TG_READ) {
31044                 rval = sd_send_scsi_READ(ssc, (dkl != NULL)? dkl: bufaddr,
31045                     buffer_size, real_addr, path_flag);
31046                 if (dkl != NULL)
31047                         bcopy(dkl + SD_TGTBYTEOFFSET(un, start_block,
31048                             real_addr), bufaddr, reqlength);
31049         } else {
31050                 if (dkl) {
31051                         rval = sd_send_scsi_READ(ssc, dkl, buffer_size,
31052                             real_addr, path_flag);
31053                         if (rval) {
31054                                 goto done1;
31055                         }
31056                         bcopy(bufaddr, dkl + SD_TGTBYTEOFFSET(un, start_block,
31057                             real_addr), reqlength);
31058                 }
31059                 rval = sd_send_scsi_WRITE(ssc, (dkl != NULL)? dkl: bufaddr,
31060                     buffer_size, real_addr, path_flag);
31061         }
31062 
31063 done1:
31064         if (dkl != NULL)
31065                 kmem_free(dkl, buffer_size);
31066 
31067         if (rval != 0) {
31068                 if (rval == EIO)
31069                         sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
31070                 else
31071                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
31072         }
31073 done:
31074         sd_ssc_fini(ssc);
31075         return (rval);
31076 }
31077 
31078 
31079 static int
31080 sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie)
31081 {
31082 
31083         struct sd_lun *un;
31084         diskaddr_t      cap;
31085         uint32_t        lbasize;
31086         int             path_flag = (int)(uintptr_t)tg_cookie;
31087         int             ret = 0;
31088 
31089         un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
31090         if (un == NULL)
31091                 return (ENXIO);
31092 
31093         switch (cmd) {
31094         case TG_GETPHYGEOM:
31095         case TG_GETVIRTGEOM:
31096         case TG_GETCAPACITY:
31097         case TG_GETBLOCKSIZE:
31098                 mutex_enter(SD_MUTEX(un));
31099 
31100                 if ((un->un_f_blockcount_is_valid == TRUE) &&
31101                     (un->un_f_tgt_blocksize_is_valid == TRUE)) {
31102                         cap = un->un_blockcount;
31103                         lbasize = un->un_tgt_blocksize;
31104                         mutex_exit(SD_MUTEX(un));
31105                 } else {
31106                         sd_ssc_t        *ssc;
31107                         mutex_exit(SD_MUTEX(un));
31108                         ssc = sd_ssc_init(un);
31109                         ret = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
31110                             &lbasize, path_flag);
31111                         if (ret != 0) {
31112                                 if (ret == EIO)
31113                                         sd_ssc_assessment(ssc,
31114                                             SD_FMT_STATUS_CHECK);
31115                                 else
31116                                         sd_ssc_assessment(ssc,
31117                                             SD_FMT_IGNORE);
31118                                 sd_ssc_fini(ssc);
31119                                 return (ret);
31120                         }
31121                         sd_ssc_fini(ssc);
31122                         mutex_enter(SD_MUTEX(un));
31123                         sd_update_block_info(un, lbasize, cap);
31124                         if ((un->un_f_blockcount_is_valid == FALSE) ||
31125                             (un->un_f_tgt_blocksize_is_valid == FALSE)) {
31126                                 mutex_exit(SD_MUTEX(un));
31127                                 return (EIO);
31128                         }
31129                         mutex_exit(SD_MUTEX(un));
31130                 }
31131 
31132                 if (cmd == TG_GETCAPACITY) {
31133                         *(diskaddr_t *)arg = cap;
31134                         return (0);
31135                 }
31136 
31137                 if (cmd == TG_GETBLOCKSIZE) {
31138                         *(uint32_t *)arg = lbasize;
31139                         return (0);
31140                 }
31141 
31142                 if (cmd == TG_GETPHYGEOM)
31143                         ret = sd_get_physical_geometry(un, (cmlb_geom_t *)arg,
31144                             cap, lbasize, path_flag);
31145                 else
31146                         /* TG_GETVIRTGEOM */
31147                         ret = sd_get_virtual_geometry(un,
31148                             (cmlb_geom_t *)arg, cap, lbasize);
31149 
31150                 return (ret);
31151 
31152         case TG_GETATTR:
31153                 mutex_enter(SD_MUTEX(un));
31154                 ((tg_attribute_t *)arg)->media_is_writable =
31155                     un->un_f_mmc_writable_media;
31156                 ((tg_attribute_t *)arg)->media_is_solid_state =
31157                     un->un_f_is_solid_state;
31158                 ((tg_attribute_t *)arg)->media_is_rotational =
31159                     un->un_f_is_rotational;
31160                 mutex_exit(SD_MUTEX(un));
31161                 return (0);
31162         default:
31163                 return (ENOTTY);
31164 
31165         }
31166 }
31167 
31168 /*
31169  *    Function: sd_ssc_ereport_post
31170  *
31171  * Description: Will be called when SD driver need to post an ereport.
31172  *
31173  *    Context: Kernel thread or interrupt context.
31174  */
31175 
31176 #define DEVID_IF_KNOWN(d) "devid", DATA_TYPE_STRING, (d) ? (d) : "unknown"
31177 
31178 static void
31179 sd_ssc_ereport_post(sd_ssc_t *ssc, enum sd_driver_assessment drv_assess)
31180 {
31181         int uscsi_path_instance = 0;
31182         uchar_t uscsi_pkt_reason;
31183         uint32_t uscsi_pkt_state;
31184         uint32_t uscsi_pkt_statistics;
31185         uint64_t uscsi_ena;
31186         uchar_t op_code;
31187         uint8_t *sensep;
31188         union scsi_cdb *cdbp;
31189         uint_t cdblen = 0;
31190         uint_t senlen = 0;
31191         struct sd_lun *un;
31192         dev_info_t *dip;
31193         char *devid;
31194         int ssc_invalid_flags = SSC_FLAGS_INVALID_PKT_REASON |
31195             SSC_FLAGS_INVALID_STATUS |
31196             SSC_FLAGS_INVALID_SENSE |
31197             SSC_FLAGS_INVALID_DATA;
31198         char assessment[16];
31199 
31200         ASSERT(ssc != NULL);
31201         ASSERT(ssc->ssc_uscsi_cmd != NULL);
31202         ASSERT(ssc->ssc_uscsi_info != NULL);
31203 
31204         un = ssc->ssc_un;
31205         ASSERT(un != NULL);
31206 
31207         dip = un->un_sd->sd_dev;
31208 
31209         /*
31210          * Get the devid:
31211          *      devid will only be passed to non-transport error reports.
31212          */
31213         devid = DEVI(dip)->devi_devid_str;
31214 
31215         /*
31216          * If we are syncing or dumping, the command will not be executed
31217          * so we bypass this situation.
31218          */
31219         if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
31220             (un->un_state == SD_STATE_DUMPING))
31221                 return;
31222 
31223         uscsi_pkt_reason = ssc->ssc_uscsi_info->ui_pkt_reason;
31224         uscsi_path_instance = ssc->ssc_uscsi_cmd->uscsi_path_instance;
31225         uscsi_pkt_state = ssc->ssc_uscsi_info->ui_pkt_state;
31226         uscsi_pkt_statistics = ssc->ssc_uscsi_info->ui_pkt_statistics;
31227         uscsi_ena = ssc->ssc_uscsi_info->ui_ena;
31228 
31229         sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
31230         cdbp = (union scsi_cdb *)ssc->ssc_uscsi_cmd->uscsi_cdb;
31231 
31232         /* In rare cases, EG:DOORLOCK, the cdb could be NULL */
31233         if (cdbp == NULL) {
31234                 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
31235                     "sd_ssc_ereport_post meet empty cdb\n");
31236                 return;
31237         }
31238 
31239         op_code = cdbp->scc_cmd;
31240 
31241         cdblen = (int)ssc->ssc_uscsi_cmd->uscsi_cdblen;
31242         senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
31243             ssc->ssc_uscsi_cmd->uscsi_rqresid);
31244 
31245         if (senlen > 0)
31246                 ASSERT(sensep != NULL);
31247 
31248         /*
31249          * Initialize drv_assess to corresponding values.
31250          * SD_FM_DRV_FATAL will be mapped to "fail" or "fatal" depending
31251          * on the sense-key returned back.
31252          */
31253         switch (drv_assess) {
31254                 case SD_FM_DRV_RECOVERY:
31255                         (void) sprintf(assessment, "%s", "recovered");
31256                         break;
31257                 case SD_FM_DRV_RETRY:
31258                         (void) sprintf(assessment, "%s", "retry");
31259                         break;
31260                 case SD_FM_DRV_NOTICE:
31261                         (void) sprintf(assessment, "%s", "info");
31262                         break;
31263                 case SD_FM_DRV_FATAL:
31264                 default:
31265                         (void) sprintf(assessment, "%s", "unknown");
31266         }
31267         /*
31268          * If drv_assess == SD_FM_DRV_RECOVERY, this should be a recovered
31269          * command, we will post ereport.io.scsi.cmd.disk.recovered.
31270          * driver-assessment will always be "recovered" here.
31271          */
31272         if (drv_assess == SD_FM_DRV_RECOVERY) {
31273                 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL,
31274                     "cmd.disk.recovered", uscsi_ena, devid, NULL,
31275                     DDI_NOSLEEP, NULL,
31276                     FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31277                     DEVID_IF_KNOWN(devid),
31278                     "driver-assessment", DATA_TYPE_STRING, assessment,
31279                     "op-code", DATA_TYPE_UINT8, op_code,
31280                     "cdb", DATA_TYPE_UINT8_ARRAY,
31281                     cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31282                     "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31283                     "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31284                     "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31285                     NULL);
31286                 return;
31287         }
31288 
31289         /*
31290          * If there is un-expected/un-decodable data, we should post
31291          * ereport.io.scsi.cmd.disk.dev.uderr.
31292          * driver-assessment will be set based on parameter drv_assess.
31293          * SSC_FLAGS_INVALID_SENSE - invalid sense data sent back.
31294          * SSC_FLAGS_INVALID_PKT_REASON - invalid pkt-reason encountered.
31295          * SSC_FLAGS_INVALID_STATUS - invalid stat-code encountered.
31296          * SSC_FLAGS_INVALID_DATA - invalid data sent back.
31297          */
31298         if (ssc->ssc_flags & ssc_invalid_flags) {
31299                 if (ssc->ssc_flags & SSC_FLAGS_INVALID_SENSE) {
31300                         scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31301                             NULL, "cmd.disk.dev.uderr", uscsi_ena, devid,
31302                             NULL, DDI_NOSLEEP, NULL,
31303                             FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31304                             DEVID_IF_KNOWN(devid),
31305                             "driver-assessment", DATA_TYPE_STRING,
31306                             drv_assess == SD_FM_DRV_FATAL ?
31307                             "fail" : assessment,
31308                             "op-code", DATA_TYPE_UINT8, op_code,
31309                             "cdb", DATA_TYPE_UINT8_ARRAY,
31310                             cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31311                             "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31312                             "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31313                             "pkt-stats", DATA_TYPE_UINT32,
31314                             uscsi_pkt_statistics,
31315                             "stat-code", DATA_TYPE_UINT8,
31316                             ssc->ssc_uscsi_cmd->uscsi_status,
31317                             "un-decode-info", DATA_TYPE_STRING,
31318                             ssc->ssc_info,
31319                             "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31320                             senlen, sensep,
31321                             NULL);
31322                 } else {
31323                         /*
31324                          * For other type of invalid data, the
31325                          * un-decode-value field would be empty because the
31326                          * un-decodable content could be seen from upper
31327                          * level payload or inside un-decode-info.
31328                          */
31329                         scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31330                             NULL,
31331                             "cmd.disk.dev.uderr", uscsi_ena, devid,
31332                             NULL, DDI_NOSLEEP, NULL,
31333                             FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31334                             DEVID_IF_KNOWN(devid),
31335                             "driver-assessment", DATA_TYPE_STRING,
31336                             drv_assess == SD_FM_DRV_FATAL ?
31337                             "fail" : assessment,
31338                             "op-code", DATA_TYPE_UINT8, op_code,
31339                             "cdb", DATA_TYPE_UINT8_ARRAY,
31340                             cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31341                             "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31342                             "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31343                             "pkt-stats", DATA_TYPE_UINT32,
31344                             uscsi_pkt_statistics,
31345                             "stat-code", DATA_TYPE_UINT8,
31346                             ssc->ssc_uscsi_cmd->uscsi_status,
31347                             "un-decode-info", DATA_TYPE_STRING,
31348                             ssc->ssc_info,
31349                             "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31350                             0, NULL,
31351                             NULL);
31352                 }
31353                 ssc->ssc_flags &= ~ssc_invalid_flags;
31354                 return;
31355         }
31356 
31357         if (uscsi_pkt_reason != CMD_CMPLT ||
31358             (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)) {
31359                 /*
31360                  * pkt-reason != CMD_CMPLT or SSC_FLAGS_TRAN_ABORT was
31361                  * set inside sd_start_cmds due to errors(bad packet or
31362                  * fatal transport error), we should take it as a
31363                  * transport error, so we post ereport.io.scsi.cmd.disk.tran.
31364                  * driver-assessment will be set based on drv_assess.
31365                  * We will set devid to NULL because it is a transport
31366                  * error.
31367                  */
31368                 if (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)
31369                         ssc->ssc_flags &= ~SSC_FLAGS_TRAN_ABORT;
31370 
31371                 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL,
31372                     "cmd.disk.tran", uscsi_ena, NULL, NULL, DDI_NOSLEEP, NULL,
31373                     FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31374                     DEVID_IF_KNOWN(devid),
31375                     "driver-assessment", DATA_TYPE_STRING,
31376                     drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31377                     "op-code", DATA_TYPE_UINT8, op_code,
31378                     "cdb", DATA_TYPE_UINT8_ARRAY,
31379                     cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31380                     "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31381                     "pkt-state", DATA_TYPE_UINT8, uscsi_pkt_state,
31382                     "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31383                     NULL);
31384         } else {
31385                 /*
31386                  * If we got here, we have a completed command, and we need
31387                  * to further investigate the sense data to see what kind
31388                  * of ereport we should post.
31389                  * No ereport is needed if sense-key is KEY_RECOVERABLE_ERROR
31390                  * and asc/ascq is "ATA PASS-THROUGH INFORMATION AVAILABLE".
31391                  * Post ereport.io.scsi.cmd.disk.dev.rqs.merr if sense-key is
31392                  * KEY_MEDIUM_ERROR.
31393                  * Post ereport.io.scsi.cmd.disk.dev.rqs.derr otherwise.
31394                  * driver-assessment will be set based on the parameter
31395                  * drv_assess.
31396                  */
31397                 if (senlen > 0) {
31398                         /*
31399                          * Here we have sense data available.
31400                          */
31401                         uint8_t sense_key = scsi_sense_key(sensep);
31402                         uint8_t sense_asc = scsi_sense_asc(sensep);
31403                         uint8_t sense_ascq = scsi_sense_ascq(sensep);
31404 
31405                         if (sense_key == KEY_RECOVERABLE_ERROR &&
31406                             sense_asc == 0x00 && sense_ascq == 0x1d)
31407                                 return;
31408 
31409                         if (sense_key == KEY_MEDIUM_ERROR) {
31410                                 /*
31411                                  * driver-assessment should be "fatal" if
31412                                  * drv_assess is SD_FM_DRV_FATAL.
31413                                  */
31414                                 scsi_fm_ereport_post(un->un_sd,
31415                                     uscsi_path_instance, NULL,
31416                                     "cmd.disk.dev.rqs.merr",
31417                                     uscsi_ena, devid, NULL, DDI_NOSLEEP, NULL,
31418                                     FM_VERSION, DATA_TYPE_UINT8,
31419                                     FM_EREPORT_VERS0,
31420                                     DEVID_IF_KNOWN(devid),
31421                                     "driver-assessment",
31422                                     DATA_TYPE_STRING,
31423                                     drv_assess == SD_FM_DRV_FATAL ?
31424                                     "fatal" : assessment,
31425                                     "op-code",
31426                                     DATA_TYPE_UINT8, op_code,
31427                                     "cdb",
31428                                     DATA_TYPE_UINT8_ARRAY, cdblen,
31429                                     ssc->ssc_uscsi_cmd->uscsi_cdb,
31430                                     "pkt-reason",
31431                                     DATA_TYPE_UINT8, uscsi_pkt_reason,
31432                                     "pkt-state",
31433                                     DATA_TYPE_UINT8, uscsi_pkt_state,
31434                                     "pkt-stats",
31435                                     DATA_TYPE_UINT32,
31436                                     uscsi_pkt_statistics,
31437                                     "stat-code",
31438                                     DATA_TYPE_UINT8,
31439                                     ssc->ssc_uscsi_cmd->uscsi_status,
31440                                     "key",
31441                                     DATA_TYPE_UINT8,
31442                                     scsi_sense_key(sensep),
31443                                     "asc",
31444                                     DATA_TYPE_UINT8,
31445                                     scsi_sense_asc(sensep),
31446                                     "ascq",
31447                                     DATA_TYPE_UINT8,
31448                                     scsi_sense_ascq(sensep),
31449                                     "sense-data",
31450                                     DATA_TYPE_UINT8_ARRAY,
31451                                     senlen, sensep,
31452                                     "lba",
31453                                     DATA_TYPE_UINT64,
31454                                     ssc->ssc_uscsi_info->ui_lba,
31455                                     NULL);
31456                         } else {
31457                                 /*
31458                                  * if sense-key == 0x4(hardware
31459                                  * error), driver-assessment should
31460                                  * be "fatal" if drv_assess is
31461                                  * SD_FM_DRV_FATAL.
31462                                  */
31463                                 scsi_fm_ereport_post(un->un_sd,
31464                                     uscsi_path_instance, NULL,
31465                                     "cmd.disk.dev.rqs.derr",
31466                                     uscsi_ena, devid,
31467                                     NULL, DDI_NOSLEEP, NULL,
31468                                     FM_VERSION,
31469                                     DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31470                                     DEVID_IF_KNOWN(devid),
31471                                     "driver-assessment",
31472                                     DATA_TYPE_STRING,
31473                                     drv_assess == SD_FM_DRV_FATAL ?
31474                                     (sense_key == 0x4 ?
31475                                     "fatal" : "fail") : assessment,
31476                                     "op-code",
31477                                     DATA_TYPE_UINT8, op_code,
31478                                     "cdb",
31479                                     DATA_TYPE_UINT8_ARRAY, cdblen,
31480                                     ssc->ssc_uscsi_cmd->uscsi_cdb,
31481                                     "pkt-reason",
31482                                     DATA_TYPE_UINT8, uscsi_pkt_reason,
31483                                     "pkt-state",
31484                                     DATA_TYPE_UINT8, uscsi_pkt_state,
31485                                     "pkt-stats",
31486                                     DATA_TYPE_UINT32,
31487                                     uscsi_pkt_statistics,
31488                                     "stat-code",
31489                                     DATA_TYPE_UINT8,
31490                                     ssc->ssc_uscsi_cmd->uscsi_status,
31491                                     "key",
31492                                     DATA_TYPE_UINT8,
31493                                     scsi_sense_key(sensep),
31494                                     "asc",
31495                                     DATA_TYPE_UINT8,
31496                                     scsi_sense_asc(sensep),
31497                                     "ascq",
31498                                     DATA_TYPE_UINT8,
31499                                     scsi_sense_ascq(sensep),
31500                                     "sense-data",
31501                                     DATA_TYPE_UINT8_ARRAY,
31502                                     senlen, sensep,
31503                                     NULL);
31504                         }
31505                 } else {
31506                         /*
31507                          * For stat_code == STATUS_GOOD, this is not a
31508                          * hardware error.
31509                          */
31510                         if (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD)
31511                                 return;
31512 
31513                         /*
31514                          * Post ereport.io.scsi.cmd.disk.dev.serr if we got the
31515                          * stat-code but with sense data unavailable.
31516                          * driver-assessment will be set based on parameter
31517                          * drv_assess.
31518                          */
31519                         scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31520                             NULL,
31521                             "cmd.disk.dev.serr", uscsi_ena,
31522                             devid, NULL, DDI_NOSLEEP, NULL,
31523                             FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31524                             DEVID_IF_KNOWN(devid),
31525                             "driver-assessment", DATA_TYPE_STRING,
31526                             drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31527                             "op-code", DATA_TYPE_UINT8, op_code,
31528                             "cdb",
31529                             DATA_TYPE_UINT8_ARRAY,
31530                             cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31531                             "pkt-reason",
31532                             DATA_TYPE_UINT8, uscsi_pkt_reason,
31533                             "pkt-state",
31534                             DATA_TYPE_UINT8, uscsi_pkt_state,
31535                             "pkt-stats",
31536                             DATA_TYPE_UINT32, uscsi_pkt_statistics,
31537                             "stat-code",
31538                             DATA_TYPE_UINT8,
31539                             ssc->ssc_uscsi_cmd->uscsi_status,
31540                             NULL);
31541                 }
31542         }
31543 }
31544 
31545 /*
31546  *     Function: sd_ssc_extract_info
31547  *
31548  * Description: Extract information available to help generate ereport.
31549  *
31550  *     Context: Kernel thread or interrupt context.
31551  */
31552 static void
31553 sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un, struct scsi_pkt *pktp,
31554     struct buf *bp, struct sd_xbuf *xp)
31555 {
31556         size_t senlen = 0;
31557         union scsi_cdb *cdbp;
31558         int path_instance;
31559         /*
31560          * Need scsi_cdb_size array to determine the cdb length.
31561          */
31562         extern uchar_t  scsi_cdb_size[];
31563 
31564         ASSERT(un != NULL);
31565         ASSERT(pktp != NULL);
31566         ASSERT(bp != NULL);
31567         ASSERT(xp != NULL);
31568         ASSERT(ssc != NULL);
31569         ASSERT(mutex_owned(SD_MUTEX(un)));
31570 
31571         /*
31572          * Transfer the cdb buffer pointer here.
31573          */
31574         cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
31575 
31576         ssc->ssc_uscsi_cmd->uscsi_cdblen = scsi_cdb_size[GETGROUP(cdbp)];
31577         ssc->ssc_uscsi_cmd->uscsi_cdb = (caddr_t)cdbp;
31578 
31579         /*
31580          * Transfer the sense data buffer pointer if sense data is available,
31581          * calculate the sense data length first.
31582          */
31583         if ((xp->xb_sense_state & STATE_XARQ_DONE) ||
31584             (xp->xb_sense_state & STATE_ARQ_DONE)) {
31585                 /*
31586                  * For arq case, we will enter here.
31587                  */
31588                 if (xp->xb_sense_state & STATE_XARQ_DONE) {
31589                         senlen = MAX_SENSE_LENGTH - xp->xb_sense_resid;
31590                 } else {
31591                         senlen = SENSE_LENGTH;
31592                 }
31593         } else {
31594                 /*
31595                  * For non-arq case, we will enter this branch.
31596                  */
31597                 if (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK &&
31598                     (xp->xb_sense_state & STATE_XFERRED_DATA)) {
31599                         senlen = SENSE_LENGTH - xp->xb_sense_resid;
31600                 }
31601 
31602         }
31603 
31604         ssc->ssc_uscsi_cmd->uscsi_rqlen = (senlen & 0xff);
31605         ssc->ssc_uscsi_cmd->uscsi_rqresid = 0;
31606         ssc->ssc_uscsi_cmd->uscsi_rqbuf = (caddr_t)xp->xb_sense_data;
31607 
31608         ssc->ssc_uscsi_cmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
31609 
31610         /*
31611          * Only transfer path_instance when scsi_pkt was properly allocated.
31612          */
31613         path_instance = pktp->pkt_path_instance;
31614         if (scsi_pkt_allocated_correctly(pktp) && path_instance)
31615                 ssc->ssc_uscsi_cmd->uscsi_path_instance = path_instance;
31616         else
31617                 ssc->ssc_uscsi_cmd->uscsi_path_instance = 0;
31618 
31619         /*
31620          * Copy in the other fields we may need when posting ereport.
31621          */
31622         ssc->ssc_uscsi_info->ui_pkt_reason = pktp->pkt_reason;
31623         ssc->ssc_uscsi_info->ui_pkt_state = pktp->pkt_state;
31624         ssc->ssc_uscsi_info->ui_pkt_statistics = pktp->pkt_statistics;
31625         ssc->ssc_uscsi_info->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
31626 
31627         /*
31628          * For partially read/write command, we will not create ena
31629          * in case of a successful command be reconized as recovered.
31630          */
31631         if ((pktp->pkt_reason == CMD_CMPLT) &&
31632             (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD) &&
31633             (senlen == 0)) {
31634                 return;
31635         }
31636 
31637         /*
31638          * To associate ereports of a single command execution flow, we
31639          * need a shared ena for a specific command.
31640          */
31641         if (xp->xb_ena == 0)
31642                 xp->xb_ena = fm_ena_generate(0, FM_ENA_FMT1);
31643         ssc->ssc_uscsi_info->ui_ena = xp->xb_ena;
31644 }
31645 
31646 
31647 /*
31648  *     Function: sd_check_bdc_vpd
31649  *
31650  * Description: Query the optional INQUIRY VPD page 0xb1. If the device
31651  *              supports VPD page 0xb1, sd examines the MEDIUM ROTATION
31652  *              RATE.
31653  *
31654  *              Set the following based on RPM value:
31655  *              = 0     device is not solid state, non-rotational
31656  *              = 1     device is solid state, non-rotational
31657  *              > 1  device is not solid state, rotational
31658  *
31659  *     Context: Kernel thread or interrupt context.
31660  */
31661 
31662 static void
31663 sd_check_bdc_vpd(sd_ssc_t *ssc)
31664 {
31665         int             rval            = 0;
31666         uchar_t         *inqb1          = NULL;
31667         size_t          inqb1_len       = MAX_INQUIRY_SIZE;
31668         size_t          inqb1_resid     = 0;
31669         struct sd_lun   *un;
31670 
31671         ASSERT(ssc != NULL);
31672         un = ssc->ssc_un;
31673         ASSERT(un != NULL);
31674         ASSERT(!mutex_owned(SD_MUTEX(un)));
31675 
31676         mutex_enter(SD_MUTEX(un));
31677         un->un_f_is_rotational = TRUE;
31678         un->un_f_is_solid_state = FALSE;
31679 
31680         if (ISCD(un)) {
31681                 mutex_exit(SD_MUTEX(un));
31682                 return;
31683         }
31684 
31685         if (sd_check_vpd_page_support(ssc) == 0 &&
31686             un->un_vpd_page_mask & SD_VPD_DEV_CHARACTER_PG) {
31687                 mutex_exit(SD_MUTEX(un));
31688                 /* collect page b1 data */
31689                 inqb1 = kmem_zalloc(inqb1_len, KM_SLEEP);
31690 
31691                 rval = sd_send_scsi_INQUIRY(ssc, inqb1, inqb1_len,
31692                     0x01, 0xB1, &inqb1_resid);
31693 
31694                 if (rval == 0 && (inqb1_len - inqb1_resid > 5)) {
31695                         SD_TRACE(SD_LOG_COMMON, un,
31696                             "sd_check_bdc_vpd: \
31697                             successfully get VPD page: %x \
31698                             PAGE LENGTH: %x BYTE 4: %x \
31699                             BYTE 5: %x", inqb1[1], inqb1[3], inqb1[4],
31700                             inqb1[5]);
31701 
31702                         mutex_enter(SD_MUTEX(un));
31703                         /*
31704                          * Check the MEDIUM ROTATION RATE.
31705                          */
31706                         if (inqb1[4] == 0) {
31707                                 if (inqb1[5] == 0) {
31708                                         un->un_f_is_rotational = FALSE;
31709                                 } else if (inqb1[5] == 1) {
31710                                         un->un_f_is_rotational = FALSE;
31711                                         un->un_f_is_solid_state = TRUE;
31712                                         /*
31713                                          * Solid state drives don't need
31714                                          * disksort.
31715                                          */
31716                                         un->un_f_disksort_disabled = TRUE;
31717                                 }
31718                         }
31719                         mutex_exit(SD_MUTEX(un));
31720                 } else if (rval != 0) {
31721                         sd_ssc_assessment(ssc, SD_FMT_IGNORE);
31722                 }
31723 
31724                 kmem_free(inqb1, inqb1_len);
31725         } else {
31726                 mutex_exit(SD_MUTEX(un));
31727         }
31728 }
31729 
31730 /*
31731  *      Function: sd_check_emulation_mode
31732  *
31733  *   Description: Check whether the SSD is at emulation mode
31734  *                by issuing READ_CAPACITY_16 to see whether
31735  *                we can get physical block size of the drive.
31736  *
31737  *       Context: Kernel thread or interrupt context.
31738  */
31739 
31740 static void
31741 sd_check_emulation_mode(sd_ssc_t *ssc)
31742 {
31743         int             rval = 0;
31744         uint64_t        capacity;
31745         uint_t          lbasize;
31746         uint_t          pbsize;
31747         int             i;
31748         int             devid_len;
31749         struct sd_lun   *un;
31750 
31751         ASSERT(ssc != NULL);
31752         un = ssc->ssc_un;
31753         ASSERT(un != NULL);
31754         ASSERT(!mutex_owned(SD_MUTEX(un)));
31755 
31756         mutex_enter(SD_MUTEX(un));
31757         if (ISCD(un)) {
31758                 mutex_exit(SD_MUTEX(un));
31759                 return;
31760         }
31761 
31762         if (un->un_f_descr_format_supported) {
31763                 mutex_exit(SD_MUTEX(un));
31764                 rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
31765                     &pbsize, SD_PATH_DIRECT);
31766                 mutex_enter(SD_MUTEX(un));
31767 
31768                 if (rval != 0) {
31769                         un->un_phy_blocksize = DEV_BSIZE;
31770                 } else {
31771                         if (!ISP2(pbsize % DEV_BSIZE) || pbsize == 0) {
31772                                 un->un_phy_blocksize = DEV_BSIZE;
31773                         } else if (pbsize > un->un_phy_blocksize) {
31774                                 /*
31775                                  * Don't reset the physical blocksize
31776                                  * unless we've detected a larger value.
31777                                  */
31778                                 un->un_phy_blocksize = pbsize;
31779                         }
31780                 }
31781         }
31782 
31783         for (i = 0; i < sd_flash_dev_table_size; i++) {
31784                 devid_len = (int)strlen(sd_flash_dev_table[i]);
31785                 if (sd_sdconf_id_match(un, sd_flash_dev_table[i], devid_len)
31786                     == SD_SUCCESS) {
31787                         un->un_phy_blocksize = SSD_SECSIZE;
31788                         if (un->un_f_is_solid_state &&
31789                             un->un_phy_blocksize != un->un_tgt_blocksize)
31790                                 un->un_f_enable_rmw = TRUE;
31791                 }
31792         }
31793 
31794         mutex_exit(SD_MUTEX(un));
31795 }